]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_fat.c
* Update TRAB auto update code
[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
41
42 block_dev_desc_t *get_dev (char* ifname, int dev)
43 {
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));
48         }
49 #endif
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));
54         }
55 #endif
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));
60         }
61 #endif
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));
66         }
67 #endif
68         return NULL;
69 }
70
71
72 int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
73 {
74         long size;
75         unsigned long offset;
76         unsigned long count;
77         char buf [12];
78         block_dev_desc_t *dev_desc=NULL;
79         int dev=0;
80         int part=1;
81         char *ep;
82
83         if (argc < 5) {
84                 printf ("usage: fatload <interface> <dev[:part]> <addr> <filename> [bytes]\n");
85                 return (0);
86         }
87         dev = (int)simple_strtoul (argv[2], &ep, 16);
88         dev_desc=get_dev(argv[1],dev);
89         if (dev_desc==NULL) {
90                 puts ("\n** Invalid boot device **\n");
91                 return 1;
92         }
93         if (*ep) {
94                 if (*ep != ':') {
95                         puts ("\n** Invalid boot device, use `dev[:part]' **\n");
96                         return 1;
97                 }
98                 part = (int)simple_strtoul(++ep, NULL, 16);
99         }
100         if (fat_register_device(dev_desc,part)!=0) {
101                 printf ("\n** Unable to use %s %d:%d for fatload **\n",argv[1],dev,part);
102                 return 1;
103         }
104         offset = simple_strtoul (argv[3], NULL, 16);
105         if (argc == 6)
106                 count = simple_strtoul (argv[5], NULL, 16);
107         else
108                 count = 0;
109         size = file_fat_read (argv[4], (unsigned char *) offset, count);
110
111         if(size==-1) {
112                 printf("\n** Unable to read \"%s\" from %s %d:%d **\n",argv[4],argv[1],dev,part);
113         } else {
114                 printf ("\n%ld bytes read\n", size);
115
116                 sprintf(buf, "%lX", size);
117                 setenv("filesize", buf);
118         }
119
120         return size;
121 }
122
123
124
125
126 U_BOOT_CMD(
127         fatload,        6,      0,      do_fat_fsload,
128         "fatload - load binary file from a dos filesystem\n",
129         "<interface> <dev[:part]>  <addr> <filename> [bytes]\n"
130         "    - load binary file 'filename' from 'dev' on 'interface'\n"
131         "      to address 'addr' from dos filesystem\n"
132 );
133
134 int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
135 {
136         char *filename = "/";
137         int ret;
138         int dev=0;
139         int part=1;
140         char *ep;
141         block_dev_desc_t *dev_desc=NULL;
142
143         if (argc < 3) {
144                 printf ("usage: fatls <interface> <dev[:part]> [directory]\n");
145                 return (0);
146         }
147         dev = (int)simple_strtoul (argv[2], &ep, 16);
148         dev_desc=get_dev(argv[1],dev);
149         if (dev_desc==NULL) {
150                 puts ("\n** Invalid boot device **\n");
151                 return 1;
152         }
153         if (*ep) {
154                 if (*ep != ':') {
155                         puts ("\n** Invalid boot device, use `dev[:part]' **\n");
156                         return 1;
157                 }
158                 part = (int)simple_strtoul(++ep, NULL, 16);
159         }
160         if (fat_register_device(dev_desc,part)!=0) {
161                 printf ("\n** Unable to use %s %d:%d for fatls **\n",argv[1],dev,part);
162                 return 1;
163         }
164         if (argc == 4)
165                 ret = file_fat_ls (argv[3]);
166         else
167                 ret = file_fat_ls (filename);
168
169         if(ret!=0)
170                 printf("No Fat FS detected\n");
171         return (ret);
172 }
173
174 U_BOOT_CMD(
175         fatls,  4,      1,      do_fat_ls,
176         "fatls   - list files in a directory (default /)\n",
177         "<interface> <dev[:part]> [directory]\n"
178         "    - list files from 'dev' on 'interface' in a 'directory'\n"
179 );
180
181 int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
182 {
183         int dev=0;
184         int part=1;
185         char *ep;
186         block_dev_desc_t *dev_desc=NULL;
187
188         if (argc < 2) {
189                 printf ("usage: fatinfo <interface> <dev[:part]>\n");
190                 return (0);
191         }
192         dev = (int)simple_strtoul (argv[2], &ep, 16);
193         dev_desc=get_dev(argv[1],dev);
194         if (dev_desc==NULL) {
195                 puts ("\n** Invalid boot device **\n");
196                 return 1;
197         }
198         if (*ep) {
199                 if (*ep != ':') {
200                         puts ("\n** Invalid boot device, use `dev[:part]' **\n");
201                         return 1;
202                 }
203                 part = (int)simple_strtoul(++ep, NULL, 16);
204         }
205         if (fat_register_device(dev_desc,part)!=0) {
206                 printf ("\n** Unable to use %s %d:%d for fatinfo **\n",argv[1],dev,part);
207                 return 1;
208         }
209         return (file_fat_detectfs ());
210 }
211
212 U_BOOT_CMD(
213         fatinfo,        3,      1,      do_fat_fsinfo,
214         "fatinfo - print information about filesystem\n",
215         "<interface> <dev[:part]>\n"
216         "    - print information about filesystem from 'dev' on 'interface'\n"
217 );
218
219 #ifdef NOT_IMPLEMENTED_YET
220 /* find first device whose first partition is a DOS filesystem */
221 int find_fat_partition (void)
222 {
223         int i, j;
224         block_dev_desc_t *dev_desc;
225         unsigned char *part_table;
226         unsigned char buffer[ATA_BLOCKSIZE];
227
228         for (i = 0; i < CFG_IDE_MAXDEVICE; i++) {
229                 dev_desc = ide_get_dev (i);
230                 if (!dev_desc) {
231                         debug ("couldn't get ide device!\n");
232                         return (-1);
233                 }
234                 if (dev_desc->part_type == PART_TYPE_DOS) {
235                         if (dev_desc->
236                                 block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
237                                 debug ("can't perform block_read!\n");
238                                 return (-1);
239                         }
240                         part_table = &buffer[0x1be];    /* start with partition #4 */
241                         for (j = 0; j < 4; j++) {
242                                 if ((part_table[4] == 1 ||      /* 12-bit FAT */
243                                      part_table[4] == 4 ||      /* 16-bit FAT */
244                                      part_table[4] == 6) &&     /* > 32Meg part */
245                                     part_table[0] == 0x80) {    /* bootable? */
246                                         curr_dev = i;
247                                         part_offset = part_table[11];
248                                         part_offset <<= 8;
249                                         part_offset |= part_table[10];
250                                         part_offset <<= 8;
251                                         part_offset |= part_table[9];
252                                         part_offset <<= 8;
253                                         part_offset |= part_table[8];
254                                         debug ("found partition start at %ld\n", part_offset);
255                                         return (0);
256                                 }
257                                 part_table += 16;
258                         }
259                 }
260         }
261
262         debug ("no valid devices found!\n");
263         return (-1);
264 }
265
266 int
267 do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
268 {
269         __u8 block[1024];
270         int ret;
271         int bknum;
272
273         ret = 0;
274
275         if (argc != 2) {
276                 printf ("needs an argument!\n");
277                 return (0);
278         }
279
280         bknum = simple_strtoul (argv[1], NULL, 10);
281
282         if (disk_read (0, bknum, block) != 0) {
283                 printf ("Error: reading block\n");
284                 return -1;
285         }
286         printf ("FAT dump: %d\n", bknum);
287         hexdump (512, block);
288
289         return (ret);
290 }
291
292 int disk_read (__u32 startblock, __u32 getsize, __u8 *bufptr)
293 {
294         ulong tot;
295         block_dev_desc_t *dev_desc;
296
297         if (curr_dev < 0) {
298                 if (find_fat_partition () != 0)
299                         return (-1);
300         }
301
302         dev_desc = ide_get_dev (curr_dev);
303         if (!dev_desc) {
304                 debug ("couldn't get ide device\n");
305                 return (-1);
306         }
307
308         tot = dev_desc->block_read (0, startblock + part_offset,
309                                     getsize, (ulong *) bufptr);
310
311         /* should we do this here?
312            flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz);
313          */
314
315         if (tot == getsize)
316                 return (0);
317
318         debug ("unable to read from device!\n");
319
320         return (-1);
321 }
322
323
324 static int isprint (unsigned char ch)
325 {
326         if (ch >= 32 && ch < 127)
327                 return (1);
328
329         return (0);
330 }
331
332
333 void hexdump (int cnt, unsigned char *data)
334 {
335         int i;
336         int run;
337         int offset;
338
339         offset = 0;
340         while (cnt) {
341                 printf ("%04X : ", offset);
342                 if (cnt >= 16)
343                         run = 16;
344                 else
345                         run = cnt;
346                 cnt -= run;
347                 for (i = 0; i < run; i++)
348                         printf ("%02X ", (unsigned int) data[i]);
349                 printf (": ");
350                 for (i = 0; i < run; i++)
351                         printf ("%c", isprint (data[i]) ? data[i] : '.');
352                 printf ("\n");
353                 data = &data[16];
354                 offset += run;
355         }
356 }
357 #endif  /* NOT_IMPLEMENTED_YET */
358
359 #endif  /* CFG_CMD_FAT */