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));
66 #if defined(CONFIG_SYSTEMACE)
67 if (strcmp(ifname,"ace")==0) {
68 extern block_dev_desc_t * systemace_get_dev(int dev);
69 return(systemace_get_dev(dev));
76 int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
82 block_dev_desc_t *dev_desc=NULL;
88 printf ("usage: fatload <interface> <dev[:part]> <addr> <filename> [bytes]\n");
91 dev = (int)simple_strtoul (argv[2], &ep, 16);
92 dev_desc=get_dev(argv[1],dev);
94 puts ("\n** Invalid boot device **\n");
99 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
102 part = (int)simple_strtoul(++ep, NULL, 16);
104 if (fat_register_device(dev_desc,part)!=0) {
105 printf ("\n** Unable to use %s %d:%d for fatload **\n",argv[1],dev,part);
108 offset = simple_strtoul (argv[3], NULL, 16);
110 count = simple_strtoul (argv[5], NULL, 16);
113 size = file_fat_read (argv[4], (unsigned char *) offset, count);
116 printf("\n** Unable to read \"%s\" from %s %d:%d **\n",argv[4],argv[1],dev,part);
118 printf ("\n%ld bytes read\n", size);
120 sprintf(buf, "%lX", size);
121 setenv("filesize", buf);
129 fatload, 6, 0, do_fat_fsload,
130 "fatload - load binary file from a dos filesystem\n",
131 "<interface> <dev[:part]> <addr> <filename> [bytes]\n"
132 " - load binary file 'filename' from 'dev' on 'interface'\n"
133 " to address 'addr' from dos filesystem\n"
136 int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
138 char *filename = "/";
143 block_dev_desc_t *dev_desc=NULL;
146 printf ("usage: fatls <interface> <dev[:part]> [directory]\n");
149 dev = (int)simple_strtoul (argv[2], &ep, 16);
150 dev_desc=get_dev(argv[1],dev);
151 if (dev_desc==NULL) {
152 puts ("\n** Invalid boot device **\n");
157 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
160 part = (int)simple_strtoul(++ep, NULL, 16);
162 if (fat_register_device(dev_desc,part)!=0) {
163 printf ("\n** Unable to use %s %d:%d for fatls **\n",argv[1],dev,part);
167 ret = file_fat_ls (argv[3]);
169 ret = file_fat_ls (filename);
172 printf("No Fat FS detected\n");
177 fatls, 4, 1, do_fat_ls,
178 "fatls - list files in a directory (default /)\n",
179 "<interface> <dev[:part]> [directory]\n"
180 " - list files from 'dev' on 'interface' in a 'directory'\n"
183 int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
188 block_dev_desc_t *dev_desc=NULL;
191 printf ("usage: fatinfo <interface> <dev[:part]>\n");
194 dev = (int)simple_strtoul (argv[2], &ep, 16);
195 dev_desc=get_dev(argv[1],dev);
196 if (dev_desc==NULL) {
197 puts ("\n** Invalid boot device **\n");
202 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
205 part = (int)simple_strtoul(++ep, NULL, 16);
207 if (fat_register_device(dev_desc,part)!=0) {
208 printf ("\n** Unable to use %s %d:%d for fatinfo **\n",argv[1],dev,part);
211 return (file_fat_detectfs ());
215 fatinfo, 3, 1, do_fat_fsinfo,
216 "fatinfo - print information about filesystem\n",
217 "<interface> <dev[:part]>\n"
218 " - print information about filesystem from 'dev' on 'interface'\n"
221 #ifdef NOT_IMPLEMENTED_YET
222 /* find first device whose first partition is a DOS filesystem */
223 int find_fat_partition (void)
226 block_dev_desc_t *dev_desc;
227 unsigned char *part_table;
228 unsigned char buffer[ATA_BLOCKSIZE];
230 for (i = 0; i < CFG_IDE_MAXDEVICE; i++) {
231 dev_desc = ide_get_dev (i);
233 debug ("couldn't get ide device!\n");
236 if (dev_desc->part_type == PART_TYPE_DOS) {
238 block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
239 debug ("can't perform block_read!\n");
242 part_table = &buffer[0x1be]; /* start with partition #4 */
243 for (j = 0; j < 4; j++) {
244 if ((part_table[4] == 1 || /* 12-bit FAT */
245 part_table[4] == 4 || /* 16-bit FAT */
246 part_table[4] == 6) && /* > 32Meg part */
247 part_table[0] == 0x80) { /* bootable? */
249 part_offset = part_table[11];
251 part_offset |= part_table[10];
253 part_offset |= part_table[9];
255 part_offset |= part_table[8];
256 debug ("found partition start at %ld\n", part_offset);
264 debug ("no valid devices found!\n");
269 do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
278 printf ("needs an argument!\n");
282 bknum = simple_strtoul (argv[1], NULL, 10);
284 if (disk_read (0, bknum, block) != 0) {
285 printf ("Error: reading block\n");
288 printf ("FAT dump: %d\n", bknum);
289 hexdump (512, block);
294 int disk_read (__u32 startblock, __u32 getsize, __u8 *bufptr)
297 block_dev_desc_t *dev_desc;
300 if (find_fat_partition () != 0)
304 dev_desc = ide_get_dev (curr_dev);
306 debug ("couldn't get ide device\n");
310 tot = dev_desc->block_read (0, startblock + part_offset,
311 getsize, (ulong *) bufptr);
313 /* should we do this here?
314 flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz);
320 debug ("unable to read from device!\n");
326 static int isprint (unsigned char ch)
328 if (ch >= 32 && ch < 127)
335 void hexdump (int cnt, unsigned char *data)
343 printf ("%04X : ", offset);
349 for (i = 0; i < run; i++)
350 printf ("%02X ", (unsigned int) data[i]);
352 for (i = 0; i < run; i++)
353 printf ("%c", isprint (data[i]) ? data[i] : '.');
359 #endif /* NOT_IMPLEMENTED_YET */
361 #endif /* CFG_CMD_FAT */