1 /*
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 #include <malloc.h>
28 
29 #include "dos.h"
30 #include "fdos.h"
31 
32 
33 const char *month [] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
34 
35 Fs_t    fs;
36 File_t  file;
37 
38 /*-----------------------------------------------------------------------------
39  * dos_open --
40  *-----------------------------------------------------------------------------
41  */
dos_open(char * name)42 int dos_open(char *name)
43 {
44     int lg;
45     int entry;
46     char *fname;
47 
48     /* We need to suppress the " char around the name                        */
49     if (name [0] == '"') {
50 	name ++;
51     }
52     lg = strlen (name);
53     if (name [lg - 1] == '"') {
54 	name [lg - 1] = '\0';
55     }
56 
57     /* Open file system                                                      */
58     if (fs_init (&fs) < 0) {
59 	return -1;
60     }
61 
62     /* Init the file descriptor                                              */
63     file.name = name;
64     file.fs = &fs;
65 
66     /* find the subdirectory containing the file                             */
67     if (open_subdir (&file) < 0) {
68 	return (-1);
69     }
70 
71     fname = basename (name);
72 
73     /* if we try to open root directory                                      */
74     if (*fname == '\0') {
75 	file.file = file.subdir;
76 	return (0);
77     }
78 
79     /* find the file in the subdir                                           */
80     entry = 0;
81     if (vfat_lookup (&file.subdir,
82 		     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);
93     }
94 
95     return 0;
96 }
97 
98 /*-----------------------------------------------------------------------------
99  * dos_read --
100  *-----------------------------------------------------------------------------
101  */
dos_read(ulong addr)102 int 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)) {
108 	printf ("Unable to boot %s !!\n", file.name);
109 	return (-1);
110     }
111     while (read < file.file.FileSize) {
112 	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;
124     }
125     return (read);
126 }
127 /*-----------------------------------------------------------------------------
128  * dos_dir --
129  *-----------------------------------------------------------------------------
130  */
dos_dir(void)131 int dos_dir (void)
132 {
133     int entry;
134     Directory_t dir;
135     char *name;
136 
137 
138     if ((file.file.dir.attr & ATTR_DIRECTORY) == 0) {
139 	printf ("%s: not a directory !!\n", file.name);
140 	return (1);
141     }
142     entry = 0;
143     if ((name = malloc (MAX_VNAMELEN + 1)) == NULL) {
144 	PRINTF ("Allcation error\n");
145 	return (1);
146     }
147 
148     while (vfat_lookup (&file.file,
149 			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 
169     }
170     free (name);
171     return (0);
172 }
173