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,
34 #if defined(CONFIG_CMD_FAT)
41 int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
47 block_dev_desc_t *dev_desc=NULL;
53 printf ("usage: fatload <interface> <dev[:part]> <addr> <filename> [bytes]\n");
56 dev = (int)simple_strtoul (argv[2], &ep, 16);
57 dev_desc=get_dev(argv[1],dev);
59 puts ("\n** Invalid boot device **\n");
64 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
67 part = (int)simple_strtoul(++ep, NULL, 16);
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);
73 offset = simple_strtoul (argv[3], NULL, 16);
75 count = simple_strtoul (argv[5], NULL, 16);
78 size = file_fat_read (argv[4], (unsigned char *) offset, count);
81 printf("\n** Unable to read \"%s\" from %s %d:%d **\n",argv[4],argv[1],dev,part);
85 printf ("\n%ld bytes read\n", size);
87 sprintf(buf, "%lX", size);
88 setenv("filesize", buf);
95 fatload, 6, 0, do_fat_fsload,
96 "fatload - load binary file from a dos filesystem\n",
97 "<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"
102 int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
104 char *filename = "/";
109 block_dev_desc_t *dev_desc=NULL;
112 printf ("usage: fatls <interface> <dev[:part]> [directory]\n");
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");
123 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
126 part = (int)simple_strtoul(++ep, NULL, 16);
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);
133 ret = file_fat_ls (argv[3]);
135 ret = file_fat_ls (filename);
138 printf("No Fat FS detected\n");
143 fatls, 4, 1, do_fat_ls,
144 "fatls - list files in a directory (default /)\n",
145 "<interface> <dev[:part]> [directory]\n"
146 " - list files from 'dev' on 'interface' in a 'directory'\n"
149 int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
154 block_dev_desc_t *dev_desc=NULL;
157 printf ("usage: fatinfo <interface> <dev[:part]>\n");
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");
168 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
171 part = (int)simple_strtoul(++ep, NULL, 16);
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);
177 return (file_fat_detectfs ());
181 fatinfo, 3, 1, do_fat_fsinfo,
182 "fatinfo - print information about filesystem\n",
183 "<interface> <dev[:part]>\n"
184 " - print information about filesystem from 'dev' on 'interface'\n"
187 #ifdef NOT_IMPLEMENTED_YET
188 /* find first device whose first partition is a DOS filesystem */
189 int find_fat_partition (void)
192 block_dev_desc_t *dev_desc;
193 unsigned char *part_table;
194 unsigned char buffer[ATA_BLOCKSIZE];
196 for (i = 0; i < CFG_IDE_MAXDEVICE; i++) {
197 dev_desc = ide_get_dev (i);
199 debug ("couldn't get ide device!\n");
202 if (dev_desc->part_type == PART_TYPE_DOS) {
204 block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
205 debug ("can't perform block_read!\n");
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? */
215 part_offset = part_table[11];
217 part_offset |= part_table[10];
219 part_offset |= part_table[9];
221 part_offset |= part_table[8];
222 debug ("found partition start at %ld\n", part_offset);
230 debug ("no valid devices found!\n");
235 do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
244 printf ("needs an argument!\n");
248 bknum = simple_strtoul (argv[1], NULL, 10);
250 if (disk_read (0, bknum, block) != 0) {
251 printf ("Error: reading block\n");
254 printf ("FAT dump: %d\n", bknum);
255 hexdump (512, block);
260 int disk_read (__u32 startblock, __u32 getsize, __u8 *bufptr)
263 block_dev_desc_t *dev_desc;
266 if (find_fat_partition () != 0)
270 dev_desc = ide_get_dev (curr_dev);
272 debug ("couldn't get ide device\n");
276 tot = dev_desc->block_read (0, startblock + part_offset,
277 getsize, (ulong *) bufptr);
279 /* should we do this here?
280 flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz);
286 debug ("unable to read from device!\n");
292 static int isprint (unsigned char ch)
294 if (ch >= 32 && ch < 127)
301 void hexdump (int cnt, unsigned char *data)
309 printf ("%04X : ", offset);
315 for (i = 0; i < run; i++)
316 printf ("%02X ", (unsigned int) data[i]);
318 for (i = 0; i < run; i++)
319 printf ("%c", isprint (data[i]) ? data[i] : '.');
325 #endif /* NOT_IMPLEMENTED_YET */