Merge branch 'master' of git://www.denx.de/git/u-boot-fdt
[u-boot-openmoko.git] / fs / fdos / fdos.c
blob5be6a960e7ac2352240e28dff1457cd1a3fd4f61
1 /*
2 * (C) Copyright 2002
3 * Stäubli Faverges - <www.staubli.com>
4 * Pierre AUBERT p.aubert@staubli.com
6 * See file CREDITS for list of people who contributed to this
7 * project.
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.
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.
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
25 #include <common.h>
26 #include <config.h>
28 #if defined(CONFIG_CMD_FDOS)
29 #include <malloc.h>
30 #include "dos.h"
31 #include "fdos.h"
34 const char *month [] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
36 Fs_t fs;
37 File_t file;
39 /*-----------------------------------------------------------------------------
40 * dos_open --
41 *-----------------------------------------------------------------------------
43 int dos_open(char *name)
45 int lg;
46 int entry;
47 char *fname;
49 /* We need to suppress the " char around the name */
50 if (name [0] == '"') {
51 name ++;
53 lg = strlen (name);
54 if (name [lg - 1] == '"') {
55 name [lg - 1] = '\0';
58 /* Open file system */
59 if (fs_init (&fs) < 0) {
60 return -1;
63 /* Init the file descriptor */
64 file.name = name;
65 file.fs = &fs;
67 /* find the subdirectory containing the file */
68 if (open_subdir (&file) < 0) {
69 return (-1);
72 fname = basename (name);
74 /* if we try to open root directory */
75 if (*fname == '\0') {
76 file.file = file.subdir;
77 return (0);
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,
87 fname,
88 ACCEPT_DIR | ACCEPT_PLAIN | SINGLE | DO_OPEN,
90 &file.file) != 0) {
91 /* File not found */
92 printf ("File not found\n");
93 return (-1);
96 return 0;
99 /*-----------------------------------------------------------------------------
100 * dos_read --
101 *-----------------------------------------------------------------------------
103 int dos_read (ulong addr)
105 int read = 0, nb;
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);
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);
124 read += nb;
126 return (read);
128 /*-----------------------------------------------------------------------------
129 * dos_dir --
130 *-----------------------------------------------------------------------------
132 int dos_dir (void)
134 int entry;
135 Directory_t dir;
136 char *name;
139 if ((file.file.dir.attr & ATTR_DIRECTORY) == 0) {
140 printf ("%s: not a directory !!\n", file.name);
141 return (1);
143 entry = 0;
144 if ((name = malloc (MAX_VNAMELEN + 1)) == NULL) {
145 PRINTF ("Allcation error\n");
146 return (1);
149 while (vfat_lookup (&file.file,
150 file.fs,
151 &dir,
152 &entry,
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);
171 free (name);
172 return (0);
175 #endif