3 * Richard Jones, rjones@nexus-tech.net
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
33 #if (CONFIG_COMMANDS & CFG_CMD_FAT)
40 block_dev_desc_t *get_dev (char* ifname, int dev)
42 #if (CONFIG_COMMANDS & CFG_CMD_IDE)
43 if (strncmp(ifname,"ide",3)==0) {
44 extern block_dev_desc_t * ide_get_dev(int dev);
45 return(ide_get_dev(dev));
48 #if (CONFIG_COMMANDS & CFG_CMD_SCSI)
49 if (strncmp(ifname,"scsi",4)==0) {
50 extern block_dev_desc_t * scsi_get_dev(int dev);
51 return(scsi_get_dev(dev));
54 #if ((CONFIG_COMMANDS & CFG_CMD_USB) && defined(CONFIG_USB_STORAGE))
55 if (strncmp(ifname,"usb",3)==0) {
56 extern block_dev_desc_t * usb_stor_get_dev(int dev);
57 return(usb_stor_get_dev(dev));
60 #if defined(CONFIG_MMC)
61 if (strncmp(ifname,"mmc",3)==0) {
62 extern block_dev_desc_t * mmc_get_dev(int dev);
63 return(mmc_get_dev(dev));
70 int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
76 block_dev_desc_t *dev_desc=NULL;
82 printf ("usage: fatload <interface> <dev[:part]> <addr> <filename> [bytes]\n");
85 dev = (int)simple_strtoul (argv[2], &ep, 16);
86 dev_desc=get_dev(argv[1],dev);
88 puts ("\n** Invalid boot device **\n");
93 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
96 part = (int)simple_strtoul(++ep, NULL, 16);
98 if (fat_register_device(dev_desc,part)!=0) {
99 printf ("\n** Unable to use %s %d:%d for fatload **\n",argv[1],dev,part);
102 offset = simple_strtoul (argv[3], NULL, 16);
104 count = simple_strtoul (argv[5], NULL, 16);
107 size = file_fat_read (argv[4], (unsigned char *) offset, count);
110 printf("\n** Unable to read \"%s\" from %s %d:%d **\n",argv[4],argv[1],dev,part);
112 printf ("\n%ld bytes read\n", size);
114 sprintf(buf, "%lX", size);
115 setenv("filesize", buf);
123 fatload, 6, 0, do_fat_fsload,
124 "fatload - load binary file from a dos filesystem\n",
125 "<interface> <dev[:part]> <addr> <filename> [bytes]\n"
126 " - load binary file 'filename' from 'dev' on 'interface'\n"
127 " to address 'addr' from dos filesystem\n"
130 int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
132 char *filename = "/";
137 block_dev_desc_t *dev_desc=NULL;
140 printf ("usage: fatls <interface> <dev[:part]> [directory]\n");
143 dev = (int)simple_strtoul (argv[2], &ep, 16);
144 dev_desc=get_dev(argv[1],dev);
145 if (dev_desc==NULL) {
146 puts ("\n** Invalid boot device **\n");
151 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
154 part = (int)simple_strtoul(++ep, NULL, 16);
156 if (fat_register_device(dev_desc,part)!=0) {
157 printf ("\n** Unable to use %s %d:%d for fatls **\n",argv[1],dev,part);
161 ret = file_fat_ls (argv[3]);
163 ret = file_fat_ls (filename);
166 printf("No Fat FS detected\n");
171 fatls, 4, 1, do_fat_ls,
172 "fatls - list files in a directory (default /)\n",
173 "<interface> <dev[:part]> [directory]\n"
174 " - list files from 'dev' on 'interface' in a 'directory'\n"
177 int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
182 block_dev_desc_t *dev_desc=NULL;
185 printf ("usage: fatinfo <interface> <dev[:part]>\n");
188 dev = (int)simple_strtoul (argv[2], &ep, 16);
189 dev_desc=get_dev(argv[1],dev);
190 if (dev_desc==NULL) {
191 puts ("\n** Invalid boot device **\n");
196 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
199 part = (int)simple_strtoul(++ep, NULL, 16);
201 if (fat_register_device(dev_desc,part)!=0) {
202 printf ("\n** Unable to use %s %d:%d for fatinfo **\n",argv[1],dev,part);
205 return (file_fat_detectfs ());
209 fatinfo, 3, 1, do_fat_fsinfo,
210 "fatinfo - print information about filesystem\n",
211 "<interface> <dev[:part]>\n"
212 " - print information about filesystem from 'dev' on 'interface'\n"
215 #ifdef NOT_IMPLEMENTED_YET
216 /* find first device whose first partition is a DOS filesystem */
217 int find_fat_partition (void)
220 block_dev_desc_t *dev_desc;
221 unsigned char *part_table;
222 unsigned char buffer[ATA_BLOCKSIZE];
224 for (i = 0; i < CFG_IDE_MAXDEVICE; i++) {
225 dev_desc = ide_get_dev (i);
227 debug ("couldn't get ide device!\n");
230 if (dev_desc->part_type == PART_TYPE_DOS) {
232 block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
233 debug ("can't perform block_read!\n");
236 part_table = &buffer[0x1be]; /* start with partition #4 */
237 for (j = 0; j < 4; j++) {
238 if ((part_table[4] == 1 || /* 12-bit FAT */
239 part_table[4] == 4 || /* 16-bit FAT */
240 part_table[4] == 6) && /* > 32Meg part */
241 part_table[0] == 0x80) { /* bootable? */
243 part_offset = part_table[11];
245 part_offset |= part_table[10];
247 part_offset |= part_table[9];
249 part_offset |= part_table[8];
250 debug ("found partition start at %ld\n", part_offset);
258 debug ("no valid devices found!\n");
263 do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
272 printf ("needs an argument!\n");
276 bknum = simple_strtoul (argv[1], NULL, 10);
278 if (disk_read (0, bknum, block) != 0) {
279 printf ("Error: reading block\n");
282 printf ("FAT dump: %d\n", bknum);
283 hexdump (512, block);
288 int disk_read (__u32 startblock, __u32 getsize, __u8 *bufptr)
291 block_dev_desc_t *dev_desc;
294 if (find_fat_partition () != 0)
298 dev_desc = ide_get_dev (curr_dev);
300 debug ("couldn't get ide device\n");
304 tot = dev_desc->block_read (0, startblock + part_offset,
305 getsize, (ulong *) bufptr);
307 /* should we do this here?
308 flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz);
314 debug ("unable to read from device!\n");
320 static int isprint (unsigned char ch)
322 if (ch >= 32 && ch < 127)
329 void hexdump (int cnt, unsigned char *data)
337 printf ("%04X : ", offset);
343 for (i = 0; i < run; i++)
344 printf ("%02X ", (unsigned int) data[i]);
346 for (i = 0; i < run; i++)
347 printf ("%c", isprint (data[i]) ? data[i] : '.');
353 #endif /* NOT_IMPLEMENTED_YET */
355 #endif /* CFG_CMD_FAT */