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)
42 block_dev_desc_t *get_dev (char* ifname, int dev)
44 #if (CONFIG_COMMANDS & CFG_CMD_IDE)
45 if (strncmp(ifname,"ide",3)==0) {
46 extern block_dev_desc_t * ide_get_dev(int dev);
47 return(ide_get_dev(dev));
50 #if (CONFIG_COMMANDS & CFG_CMD_SCSI)
51 if (strncmp(ifname,"scsi",4)==0) {
52 extern block_dev_desc_t * scsi_get_dev(int dev);
53 return(scsi_get_dev(dev));
56 #if ((CONFIG_COMMANDS & CFG_CMD_USB) && defined(CONFIG_USB_STORAGE))
57 if (strncmp(ifname,"usb",3)==0) {
58 extern block_dev_desc_t * usb_stor_get_dev(int dev);
59 return(usb_stor_get_dev(dev));
62 #if defined(CONFIG_MMC)
63 if (strncmp(ifname,"mmc",3)==0) {
64 extern block_dev_desc_t * mmc_get_dev(int dev);
65 return(mmc_get_dev(dev));
72 int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
77 block_dev_desc_t *dev_desc=NULL;
83 printf ("usage: fatload <interface> <dev[:part]> <addr> <filename> [bytes]\n");
86 dev = (int)simple_strtoul (argv[2], &ep, 16);
87 dev_desc=get_dev(argv[1],dev);
89 puts ("\n** Invalid boot device **\n");
94 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
97 part = (int)simple_strtoul(++ep, NULL, 16);
99 if (fat_register_device(dev_desc,part)!=0) {
100 printf ("\n** Unable to use %s %d:%d for fatload **\n",argv[1],dev,part);
103 offset = simple_strtoul (argv[3], NULL, 16);
105 count = simple_strtoul (argv[5], NULL, 16);
108 size = file_fat_read (argv[4], (unsigned char *) offset, count);
111 printf("\n** Unable to read \"%s\" from %s %d:%d **\n",argv[4],argv[1],dev,part);
113 printf ("\n%ld bytes read\n", size);
122 fatload, 6, 0, do_fat_fsload,
123 "fatload - load binary file from a dos filesystem\n",
124 "<interface> <dev[:part]> <addr> <filename> [bytes]\n"
125 " - load binary file 'filename' from 'dev' on 'interface'\n"
126 " to address 'addr' from dos filesystem\n"
129 int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
131 char *filename = "/";
136 block_dev_desc_t *dev_desc=NULL;
139 printf ("usage: fatls <interface> <dev[:part]> [directory]\n");
142 dev = (int)simple_strtoul (argv[2], &ep, 16);
143 dev_desc=get_dev(argv[1],dev);
144 if (dev_desc==NULL) {
145 puts ("\n** Invalid boot device **\n");
150 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
153 part = (int)simple_strtoul(++ep, NULL, 16);
155 if (fat_register_device(dev_desc,part)!=0) {
156 printf ("\n** Unable to use %s %d:%d for fatls **\n",argv[1],dev,part);
160 ret = file_fat_ls (argv[3]);
162 ret = file_fat_ls (filename);
165 printf("No Fat FS detected\n");
170 fatls, 4, 1, do_fat_ls,
171 "fatls - list files in a directory (default /)\n",
172 "<interface> <dev[:part]> [directory]\n"
173 " - list files from 'dev' on 'interface' in a 'directory'\n"
176 int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
181 block_dev_desc_t *dev_desc=NULL;
184 printf ("usage: fatinfo <interface> <dev[:part]>\n");
187 dev = (int)simple_strtoul (argv[2], &ep, 16);
188 dev_desc=get_dev(argv[1],dev);
189 if (dev_desc==NULL) {
190 puts ("\n** Invalid boot device **\n");
195 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
198 part = (int)simple_strtoul(++ep, NULL, 16);
200 if (fat_register_device(dev_desc,part)!=0) {
201 printf ("\n** Unable to use %s %d:%d for fatinfo **\n",argv[1],dev,part);
204 return (file_fat_detectfs ());
208 fatinfo, 3, 1, do_fat_fsinfo,
209 "fatinfo - print information about filesystem\n",
210 "<interface> <dev[:part]>\n"
211 " - print information about filesystem from 'dev' on 'interface'\n"
214 #ifdef NOT_IMPLEMENTED_YET
215 /* find first device whose first partition is a DOS filesystem */
216 int find_fat_partition (void)
219 block_dev_desc_t *dev_desc;
220 unsigned char *part_table;
221 unsigned char buffer[ATA_BLOCKSIZE];
223 for (i = 0; i < CFG_IDE_MAXDEVICE; i++) {
224 dev_desc = ide_get_dev (i);
226 debug ("couldn't get ide device!\n");
229 if (dev_desc->part_type == PART_TYPE_DOS) {
231 block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
232 debug ("can't perform block_read!\n");
235 part_table = &buffer[0x1be]; /* start with partition #4 */
236 for (j = 0; j < 4; j++) {
237 if ((part_table[4] == 1 || /* 12-bit FAT */
238 part_table[4] == 4 || /* 16-bit FAT */
239 part_table[4] == 6) && /* > 32Meg part */
240 part_table[0] == 0x80) { /* bootable? */
242 part_offset = part_table[11];
244 part_offset |= part_table[10];
246 part_offset |= part_table[9];
248 part_offset |= part_table[8];
249 debug ("found partition start at %ld\n", part_offset);
257 debug ("no valid devices found!\n");
262 do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
271 printf ("needs an argument!\n");
275 bknum = simple_strtoul (argv[1], NULL, 10);
277 if (disk_read (0, bknum, block) != 0) {
278 printf ("Error: reading block\n");
281 printf ("FAT dump: %d\n", bknum);
282 hexdump (512, block);
287 int disk_read (__u32 startblock, __u32 getsize, __u8 *bufptr)
290 block_dev_desc_t *dev_desc;
293 if (find_fat_partition () != 0)
297 dev_desc = ide_get_dev (curr_dev);
299 debug ("couldn't get ide device\n");
303 tot = dev_desc->block_read (0, startblock + part_offset,
304 getsize, (ulong *) bufptr);
306 /* should we do this here?
307 flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz);
313 debug ("unable to read from device!\n");
319 static int isprint (unsigned char ch)
321 if (ch >= 32 && ch < 127)
328 void hexdump (int cnt, unsigned char *data)
336 printf ("%04X : ", offset);
342 for (i = 0; i < run; i++)
343 printf ("%02X ", (unsigned int) data[i]);
345 for (i = 0; i < run; i++)
346 printf ("%c", isprint (data[i]) ? data[i] : '.');
352 #endif /* NOT_IMPLEMENTED_YET */
354 #endif /* CFG_CMD_FAT */