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
);
120 printf ("\n%ld bytes read\n", size
);
122 sprintf(buf
, "%lX", size
);
123 setenv("filesize", buf
);
130 fatload
, 6, 0, do_fat_fsload
,
131 "fatload - load binary file from a dos filesystem\n",
132 "<interface> <dev[:part]> <addr> <filename> [bytes]\n"
133 " - load binary file 'filename' from 'dev' on 'interface'\n"
134 " to address 'addr' from dos filesystem\n"
137 int do_fat_ls (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
139 char *filename
= "/";
144 block_dev_desc_t
*dev_desc
=NULL
;
147 printf ("usage: fatls <interface> <dev[:part]> [directory]\n");
150 dev
= (int)simple_strtoul (argv
[2], &ep
, 16);
151 dev_desc
=get_dev(argv
[1],dev
);
152 if (dev_desc
==NULL
) {
153 puts ("\n** Invalid boot device **\n");
158 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
161 part
= (int)simple_strtoul(++ep
, NULL
, 16);
163 if (fat_register_device(dev_desc
,part
)!=0) {
164 printf ("\n** Unable to use %s %d:%d for fatls **\n",argv
[1],dev
,part
);
168 ret
= file_fat_ls (argv
[3]);
170 ret
= file_fat_ls (filename
);
173 printf("No Fat FS detected\n");
178 fatls
, 4, 1, do_fat_ls
,
179 "fatls - list files in a directory (default /)\n",
180 "<interface> <dev[:part]> [directory]\n"
181 " - list files from 'dev' on 'interface' in a 'directory'\n"
184 int do_fat_fsinfo (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
189 block_dev_desc_t
*dev_desc
=NULL
;
192 printf ("usage: fatinfo <interface> <dev[:part]>\n");
195 dev
= (int)simple_strtoul (argv
[2], &ep
, 16);
196 dev_desc
=get_dev(argv
[1],dev
);
197 if (dev_desc
==NULL
) {
198 puts ("\n** Invalid boot device **\n");
203 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
206 part
= (int)simple_strtoul(++ep
, NULL
, 16);
208 if (fat_register_device(dev_desc
,part
)!=0) {
209 printf ("\n** Unable to use %s %d:%d for fatinfo **\n",argv
[1],dev
,part
);
212 return (file_fat_detectfs ());
216 fatinfo
, 3, 1, do_fat_fsinfo
,
217 "fatinfo - print information about filesystem\n",
218 "<interface> <dev[:part]>\n"
219 " - print information about filesystem from 'dev' on 'interface'\n"
222 #ifdef NOT_IMPLEMENTED_YET
223 /* find first device whose first partition is a DOS filesystem */
224 int find_fat_partition (void)
227 block_dev_desc_t
*dev_desc
;
228 unsigned char *part_table
;
229 unsigned char buffer
[ATA_BLOCKSIZE
];
231 for (i
= 0; i
< CFG_IDE_MAXDEVICE
; i
++) {
232 dev_desc
= ide_get_dev (i
);
234 debug ("couldn't get ide device!\n");
237 if (dev_desc
->part_type
== PART_TYPE_DOS
) {
239 block_read (dev_desc
->dev
, 0, 1, (ulong
*) buffer
) != 1) {
240 debug ("can't perform block_read!\n");
243 part_table
= &buffer
[0x1be]; /* start with partition #4 */
244 for (j
= 0; j
< 4; j
++) {
245 if ((part_table
[4] == 1 || /* 12-bit FAT */
246 part_table
[4] == 4 || /* 16-bit FAT */
247 part_table
[4] == 6) && /* > 32Meg part */
248 part_table
[0] == 0x80) { /* bootable? */
250 part_offset
= part_table
[11];
252 part_offset
|= part_table
[10];
254 part_offset
|= part_table
[9];
256 part_offset
|= part_table
[8];
257 debug ("found partition start at %ld\n", part_offset
);
265 debug ("no valid devices found!\n");
270 do_fat_dump (cmd_tbl_t
*cmdtp
, bd_t
*bd
, int flag
, int argc
, char *argv
[])
279 printf ("needs an argument!\n");
283 bknum
= simple_strtoul (argv
[1], NULL
, 10);
285 if (disk_read (0, bknum
, block
) != 0) {
286 printf ("Error: reading block\n");
289 printf ("FAT dump: %d\n", bknum
);
290 hexdump (512, block
);
295 int disk_read (__u32 startblock
, __u32 getsize
, __u8
*bufptr
)
298 block_dev_desc_t
*dev_desc
;
301 if (find_fat_partition () != 0)
305 dev_desc
= ide_get_dev (curr_dev
);
307 debug ("couldn't get ide device\n");
311 tot
= dev_desc
->block_read (0, startblock
+ part_offset
,
312 getsize
, (ulong
*) bufptr
);
314 /* should we do this here?
315 flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz);
321 debug ("unable to read from device!\n");
327 static int isprint (unsigned char ch
)
329 if (ch
>= 32 && ch
< 127)
336 void hexdump (int cnt
, unsigned char *data
)
344 printf ("%04X : ", offset
);
350 for (i
= 0; i
< run
; i
++)
351 printf ("%02X ", (unsigned int) data
[i
]);
353 for (i
= 0; i
< run
; i
++)
354 printf ("%c", isprint (data
[i
]) ? data
[i
] : '.');
360 #endif /* NOT_IMPLEMENTED_YET */
362 #endif /* CFG_CMD_FAT */