blob: 5be6a960e7ac2352240e28dff1457cd1a3fd4f61 [file] [log] [blame]
wdenk591dda52002-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
Jon Loeliger80eb47c2007-07-09 17:56:50 -050028#if defined(CONFIG_CMD_FDOS)
wdenk591dda52002-11-18 00:14:45 +000029#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/*-----------------------------------------------------------------------------
wdenk57b2d802003-06-27 21:31:46 +000040 * dos_open --
wdenk591dda52002-11-18 00:14:45 +000041 *-----------------------------------------------------------------------------
42 */
43int dos_open(char *name)
44{
45 int lg;
46 int entry;
47 char *fname;
wdenk57b2d802003-06-27 21:31:46 +000048
wdenk591dda52002-11-18 00:14:45 +000049 /* We need to suppress the " char around the name */
50 if (name [0] == '"') {
wdenk57b2d802003-06-27 21:31:46 +000051 name ++;
wdenk591dda52002-11-18 00:14:45 +000052 }
53 lg = strlen (name);
54 if (name [lg - 1] == '"') {
wdenk57b2d802003-06-27 21:31:46 +000055 name [lg - 1] = '\0';
wdenk591dda52002-11-18 00:14:45 +000056 }
57
58 /* Open file system */
59 if (fs_init (&fs) < 0) {
wdenk57b2d802003-06-27 21:31:46 +000060 return -1;
wdenk591dda52002-11-18 00:14:45 +000061 }
62
63 /* Init the file descriptor */
64 file.name = name;
65 file.fs = &fs;
wdenk57b2d802003-06-27 21:31:46 +000066
wdenk591dda52002-11-18 00:14:45 +000067 /* find the subdirectory containing the file */
68 if (open_subdir (&file) < 0) {
wdenk57b2d802003-06-27 21:31:46 +000069 return (-1);
wdenk591dda52002-11-18 00:14:45 +000070 }
71
72 fname = basename (name);
73
74 /* if we try to open root directory */
75 if (*fname == '\0') {
wdenk57b2d802003-06-27 21:31:46 +000076 file.file = file.subdir;
77 return (0);
wdenk591dda52002-11-18 00:14:45 +000078 }
wdenk57b2d802003-06-27 21:31:46 +000079
wdenk591dda52002-11-18 00:14:45 +000080 /* find the file in the subdir */
81 entry = 0;
82 if (vfat_lookup (&file.subdir,
wdenk57b2d802003-06-27 21:31:46 +000083 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);
wdenk591dda52002-11-18 00:14:45 +000094 }
95
96 return 0;
97}
98
99/*-----------------------------------------------------------------------------
wdenk57b2d802003-06-27 21:31:46 +0000100 * dos_read --
wdenk591dda52002-11-18 00:14:45 +0000101 *-----------------------------------------------------------------------------
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)) {
wdenk57b2d802003-06-27 21:31:46 +0000109 printf ("Unable to boot %s !!\n", file.name);
110 return (-1);
wdenk591dda52002-11-18 00:14:45 +0000111 }
112 while (read < file.file.FileSize) {
wdenk57b2d802003-06-27 21:31:46 +0000113 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;
wdenk591dda52002-11-18 00:14:45 +0000125 }
126 return (read);
127}
128/*-----------------------------------------------------------------------------
wdenk57b2d802003-06-27 21:31:46 +0000129 * dos_dir --
wdenk591dda52002-11-18 00:14:45 +0000130 *-----------------------------------------------------------------------------
131 */
132int dos_dir (void)
133{
134 int entry;
135 Directory_t dir;
136 char *name;
wdenk57b2d802003-06-27 21:31:46 +0000137
138
wdenk591dda52002-11-18 00:14:45 +0000139 if ((file.file.dir.attr & ATTR_DIRECTORY) == 0) {
wdenk57b2d802003-06-27 21:31:46 +0000140 printf ("%s: not a directory !!\n", file.name);
141 return (1);
wdenk591dda52002-11-18 00:14:45 +0000142 }
143 entry = 0;
144 if ((name = malloc (MAX_VNAMELEN + 1)) == NULL) {
wdenk57b2d802003-06-27 21:31:46 +0000145 PRINTF ("Allcation error\n");
146 return (1);
wdenk591dda52002-11-18 00:14:45 +0000147 }
wdenk57b2d802003-06-27 21:31:46 +0000148
wdenk591dda52002-11-18 00:14:45 +0000149 while (vfat_lookup (&file.file,
wdenk57b2d802003-06-27 21:31:46 +0000150 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
wdenk591dda52002-11-18 00:14:45 +0000170 }
171 free (name);
172 return (0);
173}
174
175#endif