blob: afaf299569bf612b891ae04917585d28d7a2c3f3 [file] [log] [blame]
wdenk7a428cc2003-06-15 22:40:42 +00001/*
2 * (C) Copyright 2002
3 * Richard Jones, rjones@nexus-tech.net
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/*
25 * Boot support
26 */
27#include <common.h>
28#include <command.h>
wdenk7a428cc2003-06-15 22:40:42 +000029#include <s_record.h>
30#include <net.h>
31#include <ata.h>
Grant Likelyffc2dd72007-02-20 09:04:34 +010032#include <part.h>
wdenk7a428cc2003-06-15 22:40:42 +000033
34#if (CONFIG_COMMANDS & CFG_CMD_FAT)
35
36#undef DEBUG
37
38#include <fat.h>
39
wdenk2c9b05d2003-09-10 22:30:53 +000040
wdenk7a428cc2003-06-15 22:40:42 +000041int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
42{
43 long size;
44 unsigned long offset;
45 unsigned long count;
wdenk7182b0f2003-10-06 21:55:32 +000046 char buf [12];
wdenk2c9b05d2003-09-10 22:30:53 +000047 block_dev_desc_t *dev_desc=NULL;
48 int dev=0;
49 int part=1;
50 char *ep;
wdenk7a428cc2003-06-15 22:40:42 +000051
wdenk2c9b05d2003-09-10 22:30:53 +000052 if (argc < 5) {
53 printf ("usage: fatload <interface> <dev[:part]> <addr> <filename> [bytes]\n");
wdenk79cf7572004-08-28 21:09:14 +000054 return 1;
wdenk7a428cc2003-06-15 22:40:42 +000055 }
wdenk2c9b05d2003-09-10 22:30:53 +000056 dev = (int)simple_strtoul (argv[2], &ep, 16);
57 dev_desc=get_dev(argv[1],dev);
58 if (dev_desc==NULL) {
59 puts ("\n** Invalid boot device **\n");
60 return 1;
61 }
62 if (*ep) {
63 if (*ep != ':') {
64 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
65 return 1;
66 }
67 part = (int)simple_strtoul(++ep, NULL, 16);
68 }
69 if (fat_register_device(dev_desc,part)!=0) {
70 printf ("\n** Unable to use %s %d:%d for fatload **\n",argv[1],dev,part);
71 return 1;
72 }
73 offset = simple_strtoul (argv[3], NULL, 16);
74 if (argc == 6)
75 count = simple_strtoul (argv[5], NULL, 16);
wdenk7a428cc2003-06-15 22:40:42 +000076 else
77 count = 0;
wdenk2c9b05d2003-09-10 22:30:53 +000078 size = file_fat_read (argv[4], (unsigned char *) offset, count);
wdenk7a428cc2003-06-15 22:40:42 +000079
wdenk7182b0f2003-10-06 21:55:32 +000080 if(size==-1) {
wdenk2c9b05d2003-09-10 22:30:53 +000081 printf("\n** Unable to read \"%s\" from %s %d:%d **\n",argv[4],argv[1],dev,part);
wdenk79cf7572004-08-28 21:09:14 +000082 return 1;
wdenk7182b0f2003-10-06 21:55:32 +000083 }
84
wdenk79cf7572004-08-28 21:09:14 +000085 printf ("\n%ld bytes read\n", size);
86
87 sprintf(buf, "%lX", size);
88 setenv("filesize", buf);
89
90 return 0;
wdenk7a428cc2003-06-15 22:40:42 +000091}
92
wdenk2c9b05d2003-09-10 22:30:53 +000093
wdenkf287a242003-07-01 21:06:45 +000094U_BOOT_CMD(
wdenk2c9b05d2003-09-10 22:30:53 +000095 fatload, 6, 0, do_fat_fsload,
wdenkf12e3962003-06-29 21:03:46 +000096 "fatload - load binary file from a dos filesystem\n",
wdenk2c9b05d2003-09-10 22:30:53 +000097 "<interface> <dev[:part]> <addr> <filename> [bytes]\n"
98 " - load binary file 'filename' from 'dev' on 'interface'\n"
99 " to address 'addr' from dos filesystem\n"
wdenkf12e3962003-06-29 21:03:46 +0000100);
101
wdenk7a428cc2003-06-15 22:40:42 +0000102int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
103{
104 char *filename = "/";
105 int ret;
wdenk2c9b05d2003-09-10 22:30:53 +0000106 int dev=0;
107 int part=1;
108 char *ep;
109 block_dev_desc_t *dev_desc=NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000110
wdenk2c9b05d2003-09-10 22:30:53 +0000111 if (argc < 3) {
112 printf ("usage: fatls <interface> <dev[:part]> [directory]\n");
113 return (0);
114 }
115 dev = (int)simple_strtoul (argv[2], &ep, 16);
116 dev_desc=get_dev(argv[1],dev);
117 if (dev_desc==NULL) {
118 puts ("\n** Invalid boot device **\n");
119 return 1;
120 }
121 if (*ep) {
122 if (*ep != ':') {
123 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
124 return 1;
125 }
126 part = (int)simple_strtoul(++ep, NULL, 16);
127 }
128 if (fat_register_device(dev_desc,part)!=0) {
129 printf ("\n** Unable to use %s %d:%d for fatls **\n",argv[1],dev,part);
130 return 1;
131 }
132 if (argc == 4)
133 ret = file_fat_ls (argv[3]);
wdenk7a428cc2003-06-15 22:40:42 +0000134 else
135 ret = file_fat_ls (filename);
136
wdenk2c9b05d2003-09-10 22:30:53 +0000137 if(ret!=0)
138 printf("No Fat FS detected\n");
wdenk7a428cc2003-06-15 22:40:42 +0000139 return (ret);
140}
141
wdenkf287a242003-07-01 21:06:45 +0000142U_BOOT_CMD(
wdenk2c9b05d2003-09-10 22:30:53 +0000143 fatls, 4, 1, do_fat_ls,
wdenkf12e3962003-06-29 21:03:46 +0000144 "fatls - list files in a directory (default /)\n",
wdenk2c9b05d2003-09-10 22:30:53 +0000145 "<interface> <dev[:part]> [directory]\n"
146 " - list files from 'dev' on 'interface' in a 'directory'\n"
wdenkf12e3962003-06-29 21:03:46 +0000147);
148
wdenk7a428cc2003-06-15 22:40:42 +0000149int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
150{
wdenk2c9b05d2003-09-10 22:30:53 +0000151 int dev=0;
152 int part=1;
153 char *ep;
154 block_dev_desc_t *dev_desc=NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000155
wdenk2c9b05d2003-09-10 22:30:53 +0000156 if (argc < 2) {
157 printf ("usage: fatinfo <interface> <dev[:part]>\n");
158 return (0);
159 }
160 dev = (int)simple_strtoul (argv[2], &ep, 16);
161 dev_desc=get_dev(argv[1],dev);
162 if (dev_desc==NULL) {
163 puts ("\n** Invalid boot device **\n");
164 return 1;
165 }
166 if (*ep) {
167 if (*ep != ':') {
168 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
169 return 1;
170 }
171 part = (int)simple_strtoul(++ep, NULL, 16);
172 }
173 if (fat_register_device(dev_desc,part)!=0) {
174 printf ("\n** Unable to use %s %d:%d for fatinfo **\n",argv[1],dev,part);
175 return 1;
176 }
177 return (file_fat_detectfs ());
wdenk7a428cc2003-06-15 22:40:42 +0000178}
179
wdenkf287a242003-07-01 21:06:45 +0000180U_BOOT_CMD(
wdenk2c9b05d2003-09-10 22:30:53 +0000181 fatinfo, 3, 1, do_fat_fsinfo,
wdenkf12e3962003-06-29 21:03:46 +0000182 "fatinfo - print information about filesystem\n",
wdenk2c9b05d2003-09-10 22:30:53 +0000183 "<interface> <dev[:part]>\n"
184 " - print information about filesystem from 'dev' on 'interface'\n"
wdenkf12e3962003-06-29 21:03:46 +0000185);
186
wdenk7a428cc2003-06-15 22:40:42 +0000187#ifdef NOT_IMPLEMENTED_YET
188/* find first device whose first partition is a DOS filesystem */
189int find_fat_partition (void)
190{
191 int i, j;
192 block_dev_desc_t *dev_desc;
193 unsigned char *part_table;
194 unsigned char buffer[ATA_BLOCKSIZE];
195
196 for (i = 0; i < CFG_IDE_MAXDEVICE; i++) {
197 dev_desc = ide_get_dev (i);
198 if (!dev_desc) {
199 debug ("couldn't get ide device!\n");
200 return (-1);
201 }
202 if (dev_desc->part_type == PART_TYPE_DOS) {
203 if (dev_desc->
204 block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
205 debug ("can't perform block_read!\n");
206 return (-1);
207 }
208 part_table = &buffer[0x1be]; /* start with partition #4 */
209 for (j = 0; j < 4; j++) {
210 if ((part_table[4] == 1 || /* 12-bit FAT */
211 part_table[4] == 4 || /* 16-bit FAT */
212 part_table[4] == 6) && /* > 32Meg part */
213 part_table[0] == 0x80) { /* bootable? */
214 curr_dev = i;
215 part_offset = part_table[11];
216 part_offset <<= 8;
217 part_offset |= part_table[10];
218 part_offset <<= 8;
219 part_offset |= part_table[9];
220 part_offset <<= 8;
221 part_offset |= part_table[8];
222 debug ("found partition start at %ld\n", part_offset);
223 return (0);
224 }
225 part_table += 16;
226 }
227 }
228 }
229
230 debug ("no valid devices found!\n");
231 return (-1);
232}
233
234int
235do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
236{
237 __u8 block[1024];
238 int ret;
239 int bknum;
240
241 ret = 0;
242
243 if (argc != 2) {
244 printf ("needs an argument!\n");
245 return (0);
246 }
247
248 bknum = simple_strtoul (argv[1], NULL, 10);
249
250 if (disk_read (0, bknum, block) != 0) {
251 printf ("Error: reading block\n");
252 return -1;
253 }
254 printf ("FAT dump: %d\n", bknum);
255 hexdump (512, block);
256
257 return (ret);
258}
259
260int disk_read (__u32 startblock, __u32 getsize, __u8 *bufptr)
261{
262 ulong tot;
263 block_dev_desc_t *dev_desc;
264
265 if (curr_dev < 0) {
266 if (find_fat_partition () != 0)
267 return (-1);
268 }
269
270 dev_desc = ide_get_dev (curr_dev);
271 if (!dev_desc) {
272 debug ("couldn't get ide device\n");
273 return (-1);
274 }
275
276 tot = dev_desc->block_read (0, startblock + part_offset,
277 getsize, (ulong *) bufptr);
278
279 /* should we do this here?
280 flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz);
281 */
282
283 if (tot == getsize)
284 return (0);
285
286 debug ("unable to read from device!\n");
287
288 return (-1);
289}
290
291
292static int isprint (unsigned char ch)
293{
294 if (ch >= 32 && ch < 127)
295 return (1);
296
297 return (0);
298}
299
300
301void hexdump (int cnt, unsigned char *data)
302{
303 int i;
304 int run;
305 int offset;
306
307 offset = 0;
308 while (cnt) {
309 printf ("%04X : ", offset);
310 if (cnt >= 16)
311 run = 16;
312 else
313 run = cnt;
314 cnt -= run;
315 for (i = 0; i < run; i++)
316 printf ("%02X ", (unsigned int) data[i]);
317 printf (": ");
318 for (i = 0; i < run; i++)
319 printf ("%c", isprint (data[i]) ? data[i] : '.');
320 printf ("\n");
321 data = &data[16];
322 offset += run;
323 }
324}
325#endif /* NOT_IMPLEMENTED_YET */
326
327#endif /* CFG_CMD_FAT */