blob: 7bd4b519508430b7beacbaff55133ddb871c6715 [file] [log] [blame]
wdenk71f95112003-06-15 22:40:42 +00001/*
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>
wdenk71f95112003-06-15 22:40:42 +000029#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
wdenk7205e402003-09-10 22:30:53 +000039
wdenk7205e402003-09-10 22:30:53 +000040block_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
wdenk71f95112003-06-15 22:40:42 +000069
70int 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;
wdenkfbe4b5c2003-10-06 21:55:32 +000075 char buf [12];
wdenk7205e402003-09-10 22:30:53 +000076 block_dev_desc_t *dev_desc=NULL;
77 int dev=0;
78 int part=1;
79 char *ep;
wdenk71f95112003-06-15 22:40:42 +000080
wdenk7205e402003-09-10 22:30:53 +000081 if (argc < 5) {
82 printf ("usage: fatload <interface> <dev[:part]> <addr> <filename> [bytes]\n");
wdenk71f95112003-06-15 22:40:42 +000083 return (0);
84 }
wdenk7205e402003-09-10 22:30:53 +000085 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);
wdenk71f95112003-06-15 22:40:42 +0000105 else
106 count = 0;
wdenk7205e402003-09-10 22:30:53 +0000107 size = file_fat_read (argv[4], (unsigned char *) offset, count);
wdenk71f95112003-06-15 22:40:42 +0000108
wdenkfbe4b5c2003-10-06 21:55:32 +0000109 if(size==-1) {
wdenk7205e402003-09-10 22:30:53 +0000110 printf("\n** Unable to read \"%s\" from %s %d:%d **\n",argv[4],argv[1],dev,part);
wdenkfbe4b5c2003-10-06 21:55:32 +0000111 } else {
wdenk7205e402003-09-10 22:30:53 +0000112 printf ("\n%ld bytes read\n", size);
wdenk71f95112003-06-15 22:40:42 +0000113
wdenkfbe4b5c2003-10-06 21:55:32 +0000114 sprintf(buf, "%lX", size);
115 setenv("filesize", buf);
116 }
117
wdenk71f95112003-06-15 22:40:42 +0000118 return size;
119}
120
wdenk7205e402003-09-10 22:30:53 +0000121
wdenk0d498392003-07-01 21:06:45 +0000122U_BOOT_CMD(
wdenk7205e402003-09-10 22:30:53 +0000123 fatload, 6, 0, do_fat_fsload,
wdenkb0fce992003-06-29 21:03:46 +0000124 "fatload - load binary file from a dos filesystem\n",
wdenk7205e402003-09-10 22:30:53 +0000125 "<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"
wdenkb0fce992003-06-29 21:03:46 +0000128);
129
wdenk71f95112003-06-15 22:40:42 +0000130int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
131{
132 char *filename = "/";
133 int ret;
wdenk7205e402003-09-10 22:30:53 +0000134 int dev=0;
135 int part=1;
136 char *ep;
137 block_dev_desc_t *dev_desc=NULL;
wdenk71f95112003-06-15 22:40:42 +0000138
wdenk7205e402003-09-10 22:30:53 +0000139 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]);
wdenk71f95112003-06-15 22:40:42 +0000162 else
163 ret = file_fat_ls (filename);
164
wdenk7205e402003-09-10 22:30:53 +0000165 if(ret!=0)
166 printf("No Fat FS detected\n");
wdenk71f95112003-06-15 22:40:42 +0000167 return (ret);
168}
169
wdenk0d498392003-07-01 21:06:45 +0000170U_BOOT_CMD(
wdenk7205e402003-09-10 22:30:53 +0000171 fatls, 4, 1, do_fat_ls,
wdenkb0fce992003-06-29 21:03:46 +0000172 "fatls - list files in a directory (default /)\n",
wdenk7205e402003-09-10 22:30:53 +0000173 "<interface> <dev[:part]> [directory]\n"
174 " - list files from 'dev' on 'interface' in a 'directory'\n"
wdenkb0fce992003-06-29 21:03:46 +0000175);
176
wdenk71f95112003-06-15 22:40:42 +0000177int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
178{
wdenk7205e402003-09-10 22:30:53 +0000179 int dev=0;
180 int part=1;
181 char *ep;
182 block_dev_desc_t *dev_desc=NULL;
wdenk71f95112003-06-15 22:40:42 +0000183
wdenk7205e402003-09-10 22:30:53 +0000184 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 ());
wdenk71f95112003-06-15 22:40:42 +0000206}
207
wdenk0d498392003-07-01 21:06:45 +0000208U_BOOT_CMD(
wdenk7205e402003-09-10 22:30:53 +0000209 fatinfo, 3, 1, do_fat_fsinfo,
wdenkb0fce992003-06-29 21:03:46 +0000210 "fatinfo - print information about filesystem\n",
wdenk7205e402003-09-10 22:30:53 +0000211 "<interface> <dev[:part]>\n"
212 " - print information about filesystem from 'dev' on 'interface'\n"
wdenkb0fce992003-06-29 21:03:46 +0000213);
214
wdenk71f95112003-06-15 22:40:42 +0000215#ifdef NOT_IMPLEMENTED_YET
216/* find first device whose first partition is a DOS filesystem */
217int 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
262int
263do_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
288int 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
320static int isprint (unsigned char ch)
321{
322 if (ch >= 32 && ch < 127)
323 return (1);
324
325 return (0);
326}
327
328
329void 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 */