blob: 4c556df57a1568e581c98aff96c99ccabc56af2c [file] [log] [blame]
wdenk591dda52002-11-18 00:14:45 +00001/*
2 * (C) Copyright 2002
Albert ARIBAUD60fbc8d2011-08-04 18:45:45 +02003 * Stäubli Faverges - <www.staubli.com>
wdenk591dda52002-11-18 00:14:45 +00004 * Pierre AUBERT p.aubert@staubli.com
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25#include <common.h>
26#include <config.h>
wdenk591dda52002-11-18 00:14:45 +000027#include <malloc.h>
Jean-Christophe PLAGNIOL-VILLARD39b9fc92008-08-31 04:24:56 +020028
wdenk591dda52002-11-18 00:14:45 +000029#include "dos.h"
30#include "fdos.h"
31
32
33const char *month [] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
34
35Fs_t fs;
36File_t file;
37
38/*-----------------------------------------------------------------------------
wdenk57b2d802003-06-27 21:31:46 +000039 * dos_open --
wdenk591dda52002-11-18 00:14:45 +000040 *-----------------------------------------------------------------------------
41 */
42int dos_open(char *name)
43{
44 int lg;
45 int entry;
46 char *fname;
wdenk57b2d802003-06-27 21:31:46 +000047
wdenk591dda52002-11-18 00:14:45 +000048 /* We need to suppress the " char around the name */
49 if (name [0] == '"') {
wdenk57b2d802003-06-27 21:31:46 +000050 name ++;
wdenk591dda52002-11-18 00:14:45 +000051 }
52 lg = strlen (name);
53 if (name [lg - 1] == '"') {
wdenk57b2d802003-06-27 21:31:46 +000054 name [lg - 1] = '\0';
wdenk591dda52002-11-18 00:14:45 +000055 }
56
57 /* Open file system */
58 if (fs_init (&fs) < 0) {
wdenk57b2d802003-06-27 21:31:46 +000059 return -1;
wdenk591dda52002-11-18 00:14:45 +000060 }
61
62 /* Init the file descriptor */
63 file.name = name;
64 file.fs = &fs;
wdenk57b2d802003-06-27 21:31:46 +000065
wdenk591dda52002-11-18 00:14:45 +000066 /* find the subdirectory containing the file */
67 if (open_subdir (&file) < 0) {
wdenk57b2d802003-06-27 21:31:46 +000068 return (-1);
wdenk591dda52002-11-18 00:14:45 +000069 }
70
71 fname = basename (name);
72
73 /* if we try to open root directory */
74 if (*fname == '\0') {
wdenk57b2d802003-06-27 21:31:46 +000075 file.file = file.subdir;
76 return (0);
wdenk591dda52002-11-18 00:14:45 +000077 }
wdenk57b2d802003-06-27 21:31:46 +000078
wdenk591dda52002-11-18 00:14:45 +000079 /* find the file in the subdir */
80 entry = 0;
81 if (vfat_lookup (&file.subdir,
wdenk57b2d802003-06-27 21:31:46 +000082 file.fs,
83 &file.file.dir,
84 &entry,
85 0,
86 fname,
87 ACCEPT_DIR | ACCEPT_PLAIN | SINGLE | DO_OPEN,
88 0,
89 &file.file) != 0) {
90 /* File not found */
91 printf ("File not found\n");
92 return (-1);
wdenk591dda52002-11-18 00:14:45 +000093 }
94
95 return 0;
96}
97
98/*-----------------------------------------------------------------------------
wdenk57b2d802003-06-27 21:31:46 +000099 * dos_read --
wdenk591dda52002-11-18 00:14:45 +0000100 *-----------------------------------------------------------------------------
101 */
102int dos_read (ulong addr)
103{
104 int read = 0, nb;
105
106 /* Try to boot a directory ? */
107 if (file.file.dir.attr & (ATTR_DIRECTORY | ATTR_VOLUME)) {
wdenk57b2d802003-06-27 21:31:46 +0000108 printf ("Unable to boot %s !!\n", file.name);
109 return (-1);
wdenk591dda52002-11-18 00:14:45 +0000110 }
111 while (read < file.file.FileSize) {
wdenk57b2d802003-06-27 21:31:46 +0000112 PRINTF ("read_file (%ld)\n", (file.file.FileSize - read));
113 nb = read_file (&fs,
114 &file.file,
115 (char *)addr + read,
116 read,
117 (file.file.FileSize - read));
118 PRINTF ("read_file -> %d\n", nb);
119 if (nb < 0) {
120 printf ("read error\n");
121 return (-1);
122 }
123 read += nb;
wdenk591dda52002-11-18 00:14:45 +0000124 }
125 return (read);
126}
127/*-----------------------------------------------------------------------------
wdenk57b2d802003-06-27 21:31:46 +0000128 * dos_dir --
wdenk591dda52002-11-18 00:14:45 +0000129 *-----------------------------------------------------------------------------
130 */
131int dos_dir (void)
132{
133 int entry;
134 Directory_t dir;
135 char *name;
wdenk57b2d802003-06-27 21:31:46 +0000136
137
wdenk591dda52002-11-18 00:14:45 +0000138 if ((file.file.dir.attr & ATTR_DIRECTORY) == 0) {
wdenk57b2d802003-06-27 21:31:46 +0000139 printf ("%s: not a directory !!\n", file.name);
140 return (1);
wdenk591dda52002-11-18 00:14:45 +0000141 }
142 entry = 0;
143 if ((name = malloc (MAX_VNAMELEN + 1)) == NULL) {
wdenk57b2d802003-06-27 21:31:46 +0000144 PRINTF ("Allcation error\n");
145 return (1);
wdenk591dda52002-11-18 00:14:45 +0000146 }
wdenk57b2d802003-06-27 21:31:46 +0000147
wdenk591dda52002-11-18 00:14:45 +0000148 while (vfat_lookup (&file.file,
wdenk57b2d802003-06-27 21:31:46 +0000149 file.fs,
150 &dir,
151 &entry,
152 0,
153 NULL,
154 ACCEPT_DIR | ACCEPT_PLAIN | MATCH_ANY,
155 name,
156 NULL) == 0) {
157 /* Display file info */
158 printf ("%3.3s %9d %s %02d %04d %02d:%02d:%02d %s\n",
159 (dir.attr & ATTR_DIRECTORY) ? "dir" : " ",
160 __le32_to_cpu (dir.size),
161 month [DOS_MONTH (&dir) - 1],
162 DOS_DAY (&dir),
163 DOS_YEAR (&dir),
164 DOS_HOUR (&dir),
165 DOS_MINUTE (&dir),
166 DOS_SEC (&dir),
167 name);
168
wdenk591dda52002-11-18 00:14:45 +0000169 }
170 free (name);
171 return (0);
172}