]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_fat.c
* Patches by Xianghua Xiao, 15 Oct 2003:
[karo-tx-uboot.git] / common / cmd_fat.c
1 /*
2  * (C) Copyright 2002
3  * Richard Jones, rjones@nexus-tech.net
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
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.
12  *
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.
17  *
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,
21  * MA 02111-1307 USA
22  */
23
24 /*
25  * Boot support
26  */
27 #include <common.h>
28 #include <command.h>
29 #include <s_record.h>
30 #include <net.h>
31 #include <ata.h>
32
33 #if (CONFIG_COMMANDS & CFG_CMD_FAT)
34
35 #undef  DEBUG
36
37 #include <fat.h>
38
39
40 block_dev_desc_t *get_dev (char* ifname, int dev)
41 {
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));
46         }
47 #endif
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));
52         }
53 #endif
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));
58         }
59 #endif
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));
64         }
65 #endif
66         return NULL;
67 }
68
69
70 int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
71 {
72         long size;
73         unsigned long offset;
74         unsigned long count;
75         char buf [12];
76         block_dev_desc_t *dev_desc=NULL;
77         int dev=0;
78         int part=1;
79         char *ep;
80
81         if (argc < 5) {
82                 printf ("usage: fatload <interface> <dev[:part]> <addr> <filename> [bytes]\n");
83                 return (0);
84         }
85         dev = (int)simple_strtoul (argv[2], &ep, 16);
86         dev_desc=get_dev(argv[1],dev);
87         if (dev_desc==NULL) {
88                 puts ("\n** Invalid boot device **\n");
89                 return 1;
90         }
91         if (*ep) {
92                 if (*ep != ':') {
93                         puts ("\n** Invalid boot device, use `dev[:part]' **\n");
94                         return 1;
95                 }
96                 part = (int)simple_strtoul(++ep, NULL, 16);
97         }
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);
100                 return 1;
101         }
102         offset = simple_strtoul (argv[3], NULL, 16);
103         if (argc == 6)
104                 count = simple_strtoul (argv[5], NULL, 16);
105         else
106                 count = 0;
107         size = file_fat_read (argv[4], (unsigned char *) offset, count);
108
109         if(size==-1) {
110                 printf("\n** Unable to read \"%s\" from %s %d:%d **\n",argv[4],argv[1],dev,part);
111         } else {
112                 printf ("\n%ld bytes read\n", size);
113
114                 sprintf(buf, "%lX", size);
115                 setenv("filesize", buf);
116         }
117
118         return size;
119 }
120
121
122 U_BOOT_CMD(
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"
128 );
129
130 int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
131 {
132         char *filename = "/";
133         int ret;
134         int dev=0;
135         int part=1;
136         char *ep;
137         block_dev_desc_t *dev_desc=NULL;
138
139         if (argc < 3) {
140                 printf ("usage: fatls <interface> <dev[:part]> [directory]\n");
141                 return (0);
142         }
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");
147                 return 1;
148         }
149         if (*ep) {
150                 if (*ep != ':') {
151                         puts ("\n** Invalid boot device, use `dev[:part]' **\n");
152                         return 1;
153                 }
154                 part = (int)simple_strtoul(++ep, NULL, 16);
155         }
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);
158                 return 1;
159         }
160         if (argc == 4)
161                 ret = file_fat_ls (argv[3]);
162         else
163                 ret = file_fat_ls (filename);
164
165         if(ret!=0)
166                 printf("No Fat FS detected\n");
167         return (ret);
168 }
169
170 U_BOOT_CMD(
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"
175 );
176
177 int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
178 {
179         int dev=0;
180         int part=1;
181         char *ep;
182         block_dev_desc_t *dev_desc=NULL;
183
184         if (argc < 2) {
185                 printf ("usage: fatinfo <interface> <dev[:part]>\n");
186                 return (0);
187         }
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");
192                 return 1;
193         }
194         if (*ep) {
195                 if (*ep != ':') {
196                         puts ("\n** Invalid boot device, use `dev[:part]' **\n");
197                         return 1;
198                 }
199                 part = (int)simple_strtoul(++ep, NULL, 16);
200         }
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);
203                 return 1;
204         }
205         return (file_fat_detectfs ());
206 }
207
208 U_BOOT_CMD(
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"
213 );
214
215 #ifdef NOT_IMPLEMENTED_YET
216 /* find first device whose first partition is a DOS filesystem */
217 int find_fat_partition (void)
218 {
219         int i, j;
220         block_dev_desc_t *dev_desc;
221         unsigned char *part_table;
222         unsigned char buffer[ATA_BLOCKSIZE];
223
224         for (i = 0; i < CFG_IDE_MAXDEVICE; i++) {
225                 dev_desc = ide_get_dev (i);
226                 if (!dev_desc) {
227                         debug ("couldn't get ide device!\n");
228                         return (-1);
229                 }
230                 if (dev_desc->part_type == PART_TYPE_DOS) {
231                         if (dev_desc->
232                                 block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
233                                 debug ("can't perform block_read!\n");
234                                 return (-1);
235                         }
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? */
242                                         curr_dev = i;
243                                         part_offset = part_table[11];
244                                         part_offset <<= 8;
245                                         part_offset |= part_table[10];
246                                         part_offset <<= 8;
247                                         part_offset |= part_table[9];
248                                         part_offset <<= 8;
249                                         part_offset |= part_table[8];
250                                         debug ("found partition start at %ld\n", part_offset);
251                                         return (0);
252                                 }
253                                 part_table += 16;
254                         }
255                 }
256         }
257
258         debug ("no valid devices found!\n");
259         return (-1);
260 }
261
262 int
263 do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
264 {
265         __u8 block[1024];
266         int ret;
267         int bknum;
268
269         ret = 0;
270
271         if (argc != 2) {
272                 printf ("needs an argument!\n");
273                 return (0);
274         }
275
276         bknum = simple_strtoul (argv[1], NULL, 10);
277
278         if (disk_read (0, bknum, block) != 0) {
279                 printf ("Error: reading block\n");
280                 return -1;
281         }
282         printf ("FAT dump: %d\n", bknum);
283         hexdump (512, block);
284
285         return (ret);
286 }
287
288 int disk_read (__u32 startblock, __u32 getsize, __u8 *bufptr)
289 {
290         ulong tot;
291         block_dev_desc_t *dev_desc;
292
293         if (curr_dev < 0) {
294                 if (find_fat_partition () != 0)
295                         return (-1);
296         }
297
298         dev_desc = ide_get_dev (curr_dev);
299         if (!dev_desc) {
300                 debug ("couldn't get ide device\n");
301                 return (-1);
302         }
303
304         tot = dev_desc->block_read (0, startblock + part_offset,
305                                     getsize, (ulong *) bufptr);
306
307         /* should we do this here?
308            flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz);
309          */
310
311         if (tot == getsize)
312                 return (0);
313
314         debug ("unable to read from device!\n");
315
316         return (-1);
317 }
318
319
320 static int isprint (unsigned char ch)
321 {
322         if (ch >= 32 && ch < 127)
323                 return (1);
324
325         return (0);
326 }
327
328
329 void hexdump (int cnt, unsigned char *data)
330 {
331         int i;
332         int run;
333         int offset;
334
335         offset = 0;
336         while (cnt) {
337                 printf ("%04X : ", offset);
338                 if (cnt >= 16)
339                         run = 16;
340                 else
341                         run = cnt;
342                 cnt -= run;
343                 for (i = 0; i < run; i++)
344                         printf ("%02X ", (unsigned int) data[i]);
345                 printf (": ");
346                 for (i = 0; i < run; i++)
347                         printf ("%c", isprint (data[i]) ? data[i] : '.');
348                 printf ("\n");
349                 data = &data[16];
350                 offset += run;
351         }
352 }
353 #endif  /* NOT_IMPLEMENTED_YET */
354
355 #endif  /* CFG_CMD_FAT */