blob: 8963f425f5f6c875a0f94032688b78a08b7f6a41 [file] [log] [blame]
wdenk2262cfe2002-11-18 00:14:45 +00001/*
2 * (C) Copyright 2002
3 * Stäubli Faverges - <www.staubli.com>
4 * 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>
27
28#if (CONFIG_COMMANDS & CFG_CMD_FDOS)
29#include <malloc.h>
30#include "dos.h"
31#include "fdos.h"
32
33
34const char *month [] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
35
36Fs_t fs;
37File_t file;
38
39/*-----------------------------------------------------------------------------
40 * dos_open --
41 *-----------------------------------------------------------------------------
42 */
43int dos_open(char *name)
44{
45 int lg;
46 int entry;
47 char *fname;
48
49 /* We need to suppress the " char around the name */
50 if (name [0] == '"') {
51 name ++;
52 }
53 lg = strlen (name);
54 if (name [lg - 1] == '"') {
55 name [lg - 1] = '\0';
56 }
57
58 /* Open file system */
59 if (fs_init (&fs) < 0) {
60 return -1;
61 }
62
63 /* Init the file descriptor */
64 file.name = name;
65 file.fs = &fs;
66
67 /* find the subdirectory containing the file */
68 if (open_subdir (&file) < 0) {
69 return (-1);
70 }
71
72 fname = basename (name);
73
74 /* if we try to open root directory */
75 if (*fname == '\0') {
76 file.file = file.subdir;
77 return (0);
78 }
79
80 /* find the file in the subdir */
81 entry = 0;
82 if (vfat_lookup (&file.subdir,
83 file.fs,
84 &file.file.dir,
85 &entry,
86 0,
87 fname,
88 ACCEPT_DIR | ACCEPT_PLAIN | SINGLE | DO_OPEN,
89 0,
90 &file.file) != 0) {
91 /* File not found */
92 printf ("File not found\n");
93 return (-1);
94 }
95
96 return 0;
97}
98
99/*-----------------------------------------------------------------------------
100 * dos_read --
101 *-----------------------------------------------------------------------------
102 */
103int dos_read (ulong addr)
104{
105 int read = 0, nb;
106
107 /* Try to boot a directory ? */
108 if (file.file.dir.attr & (ATTR_DIRECTORY | ATTR_VOLUME)) {
109 printf ("Unable to boot %s !!\n", file.name);
110 return (-1);
111 }
112 while (read < file.file.FileSize) {
113 PRINTF ("read_file (%ld)\n", (file.file.FileSize - read));
114 nb = read_file (&fs,
115 &file.file,
116 (char *)addr + read,
117 read,
118 (file.file.FileSize - read));
119 PRINTF ("read_file -> %d\n", nb);
120 if (nb < 0) {
121 printf ("read error\n");
122 return (-1);
123 }
124 read += nb;
125 }
126 return (read);
127}
128/*-----------------------------------------------------------------------------
129 * dos_dir --
130 *-----------------------------------------------------------------------------
131 */
132int dos_dir (void)
133{
134 int entry;
135 Directory_t dir;
136 char *name;
137
138
139 if ((file.file.dir.attr & ATTR_DIRECTORY) == 0) {
140 printf ("%s: not a directory !!\n", file.name);
141 return (1);
142 }
143 entry = 0;
144 if ((name = malloc (MAX_VNAMELEN + 1)) == NULL) {
145 PRINTF ("Allcation error\n");
146 return (1);
147 }
148
149 while (vfat_lookup (&file.file,
150 file.fs,
151 &dir,
152 &entry,
153 0,
154 NULL,
155 ACCEPT_DIR | ACCEPT_PLAIN | MATCH_ANY,
156 name,
157 NULL) == 0) {
158 /* Display file info */
159 printf ("%3.3s %9d %s %02d %04d %02d:%02d:%02d %s\n",
160 (dir.attr & ATTR_DIRECTORY) ? "dir" : " ",
161 __le32_to_cpu (dir.size),
162 month [DOS_MONTH (&dir) - 1],
163 DOS_DAY (&dir),
164 DOS_YEAR (&dir),
165 DOS_HOUR (&dir),
166 DOS_MINUTE (&dir),
167 DOS_SEC (&dir),
168 name);
169
170 }
171 free (name);
172 return (0);
173}
174
175#endif