blob: 6844c103f22290fa70f6fe46a3397684bbf5e07a [file] [log] [blame]
wdenk7a428cc2003-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>
wdenk7a428cc2003-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
wdenk2c9b05d2003-09-10 22:30:53 +000039
wdenk2c9b05d2003-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
wdenkef893942004-02-23 16:11:30 +000066#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));
70 }
71#endif
wdenk2c9b05d2003-09-10 22:30:53 +000072 return NULL;
73}
74
wdenk7a428cc2003-06-15 22:40:42 +000075
76int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
77{
78 long size;
79 unsigned long offset;
80 unsigned long count;
wdenk7182b0f2003-10-06 21:55:32 +000081 char buf [12];
wdenk2c9b05d2003-09-10 22:30:53 +000082 block_dev_desc_t *dev_desc=NULL;
83 int dev=0;
84 int part=1;
85 char *ep;
wdenk7a428cc2003-06-15 22:40:42 +000086
wdenk2c9b05d2003-09-10 22:30:53 +000087 if (argc < 5) {
88 printf ("usage: fatload <interface> <dev[:part]> <addr> <filename> [bytes]\n");
wdenk79cf7572004-08-28 21:09:14 +000089 return 1;
wdenk7a428cc2003-06-15 22:40:42 +000090 }
wdenk2c9b05d2003-09-10 22:30:53 +000091 dev = (int)simple_strtoul (argv[2], &ep, 16);
92 dev_desc=get_dev(argv[1],dev);
93 if (dev_desc==NULL) {
94 puts ("\n** Invalid boot device **\n");
95 return 1;
96 }
97 if (*ep) {
98 if (*ep != ':') {
99 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
100 return 1;
101 }
102 part = (int)simple_strtoul(++ep, NULL, 16);
103 }
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);
106 return 1;
107 }
108 offset = simple_strtoul (argv[3], NULL, 16);
109 if (argc == 6)
110 count = simple_strtoul (argv[5], NULL, 16);
wdenk7a428cc2003-06-15 22:40:42 +0000111 else
112 count = 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000113 size = file_fat_read (argv[4], (unsigned char *) offset, count);
wdenk7a428cc2003-06-15 22:40:42 +0000114
wdenk7182b0f2003-10-06 21:55:32 +0000115 if(size==-1) {
wdenk2c9b05d2003-09-10 22:30:53 +0000116 printf("\n** Unable to read \"%s\" from %s %d:%d **\n",argv[4],argv[1],dev,part);
wdenk79cf7572004-08-28 21:09:14 +0000117 return 1;
wdenk7182b0f2003-10-06 21:55:32 +0000118 }
119
wdenk79cf7572004-08-28 21:09:14 +0000120 printf ("\n%ld bytes read\n", size);
121
122 sprintf(buf, "%lX", size);
123 setenv("filesize", buf);
124
125 return 0;
wdenk7a428cc2003-06-15 22:40:42 +0000126}
127
wdenk2c9b05d2003-09-10 22:30:53 +0000128
wdenkf287a242003-07-01 21:06:45 +0000129U_BOOT_CMD(
wdenk2c9b05d2003-09-10 22:30:53 +0000130 fatload, 6, 0, do_fat_fsload,
wdenkf12e3962003-06-29 21:03:46 +0000131 "fatload - load binary file from a dos filesystem\n",
wdenk2c9b05d2003-09-10 22:30:53 +0000132 "<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"
wdenkf12e3962003-06-29 21:03:46 +0000135);
136
wdenk7a428cc2003-06-15 22:40:42 +0000137int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
138{
139 char *filename = "/";
140 int ret;
wdenk2c9b05d2003-09-10 22:30:53 +0000141 int dev=0;
142 int part=1;
143 char *ep;
144 block_dev_desc_t *dev_desc=NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000145
wdenk2c9b05d2003-09-10 22:30:53 +0000146 if (argc < 3) {
147 printf ("usage: fatls <interface> <dev[:part]> [directory]\n");
148 return (0);
149 }
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");
154 return 1;
155 }
156 if (*ep) {
157 if (*ep != ':') {
158 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
159 return 1;
160 }
161 part = (int)simple_strtoul(++ep, NULL, 16);
162 }
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);
165 return 1;
166 }
167 if (argc == 4)
168 ret = file_fat_ls (argv[3]);
wdenk7a428cc2003-06-15 22:40:42 +0000169 else
170 ret = file_fat_ls (filename);
171
wdenk2c9b05d2003-09-10 22:30:53 +0000172 if(ret!=0)
173 printf("No Fat FS detected\n");
wdenk7a428cc2003-06-15 22:40:42 +0000174 return (ret);
175}
176
wdenkf287a242003-07-01 21:06:45 +0000177U_BOOT_CMD(
wdenk2c9b05d2003-09-10 22:30:53 +0000178 fatls, 4, 1, do_fat_ls,
wdenkf12e3962003-06-29 21:03:46 +0000179 "fatls - list files in a directory (default /)\n",
wdenk2c9b05d2003-09-10 22:30:53 +0000180 "<interface> <dev[:part]> [directory]\n"
181 " - list files from 'dev' on 'interface' in a 'directory'\n"
wdenkf12e3962003-06-29 21:03:46 +0000182);
183
wdenk7a428cc2003-06-15 22:40:42 +0000184int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
185{
wdenk2c9b05d2003-09-10 22:30:53 +0000186 int dev=0;
187 int part=1;
188 char *ep;
189 block_dev_desc_t *dev_desc=NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000190
wdenk2c9b05d2003-09-10 22:30:53 +0000191 if (argc < 2) {
192 printf ("usage: fatinfo <interface> <dev[:part]>\n");
193 return (0);
194 }
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");
199 return 1;
200 }
201 if (*ep) {
202 if (*ep != ':') {
203 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
204 return 1;
205 }
206 part = (int)simple_strtoul(++ep, NULL, 16);
207 }
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);
210 return 1;
211 }
212 return (file_fat_detectfs ());
wdenk7a428cc2003-06-15 22:40:42 +0000213}
214
wdenkf287a242003-07-01 21:06:45 +0000215U_BOOT_CMD(
wdenk2c9b05d2003-09-10 22:30:53 +0000216 fatinfo, 3, 1, do_fat_fsinfo,
wdenkf12e3962003-06-29 21:03:46 +0000217 "fatinfo - print information about filesystem\n",
wdenk2c9b05d2003-09-10 22:30:53 +0000218 "<interface> <dev[:part]>\n"
219 " - print information about filesystem from 'dev' on 'interface'\n"
wdenkf12e3962003-06-29 21:03:46 +0000220);
221
wdenk7a428cc2003-06-15 22:40:42 +0000222#ifdef NOT_IMPLEMENTED_YET
223/* find first device whose first partition is a DOS filesystem */
224int find_fat_partition (void)
225{
226 int i, j;
227 block_dev_desc_t *dev_desc;
228 unsigned char *part_table;
229 unsigned char buffer[ATA_BLOCKSIZE];
230
231 for (i = 0; i < CFG_IDE_MAXDEVICE; i++) {
232 dev_desc = ide_get_dev (i);
233 if (!dev_desc) {
234 debug ("couldn't get ide device!\n");
235 return (-1);
236 }
237 if (dev_desc->part_type == PART_TYPE_DOS) {
238 if (dev_desc->
239 block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
240 debug ("can't perform block_read!\n");
241 return (-1);
242 }
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? */
249 curr_dev = i;
250 part_offset = part_table[11];
251 part_offset <<= 8;
252 part_offset |= part_table[10];
253 part_offset <<= 8;
254 part_offset |= part_table[9];
255 part_offset <<= 8;
256 part_offset |= part_table[8];
257 debug ("found partition start at %ld\n", part_offset);
258 return (0);
259 }
260 part_table += 16;
261 }
262 }
263 }
264
265 debug ("no valid devices found!\n");
266 return (-1);
267}
268
269int
270do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
271{
272 __u8 block[1024];
273 int ret;
274 int bknum;
275
276 ret = 0;
277
278 if (argc != 2) {
279 printf ("needs an argument!\n");
280 return (0);
281 }
282
283 bknum = simple_strtoul (argv[1], NULL, 10);
284
285 if (disk_read (0, bknum, block) != 0) {
286 printf ("Error: reading block\n");
287 return -1;
288 }
289 printf ("FAT dump: %d\n", bknum);
290 hexdump (512, block);
291
292 return (ret);
293}
294
295int disk_read (__u32 startblock, __u32 getsize, __u8 *bufptr)
296{
297 ulong tot;
298 block_dev_desc_t *dev_desc;
299
300 if (curr_dev < 0) {
301 if (find_fat_partition () != 0)
302 return (-1);
303 }
304
305 dev_desc = ide_get_dev (curr_dev);
306 if (!dev_desc) {
307 debug ("couldn't get ide device\n");
308 return (-1);
309 }
310
311 tot = dev_desc->block_read (0, startblock + part_offset,
312 getsize, (ulong *) bufptr);
313
314 /* should we do this here?
315 flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz);
316 */
317
318 if (tot == getsize)
319 return (0);
320
321 debug ("unable to read from device!\n");
322
323 return (-1);
324}
325
326
327static int isprint (unsigned char ch)
328{
329 if (ch >= 32 && ch < 127)
330 return (1);
331
332 return (0);
333}
334
335
336void hexdump (int cnt, unsigned char *data)
337{
338 int i;
339 int run;
340 int offset;
341
342 offset = 0;
343 while (cnt) {
344 printf ("%04X : ", offset);
345 if (cnt >= 16)
346 run = 16;
347 else
348 run = cnt;
349 cnt -= run;
350 for (i = 0; i < run; i++)
351 printf ("%02X ", (unsigned int) data[i]);
352 printf (": ");
353 for (i = 0; i < run; i++)
354 printf ("%c", isprint (data[i]) ? data[i] : '.');
355 printf ("\n");
356 data = &data[16];
357 offset += run;
358 }
359}
360#endif /* NOT_IMPLEMENTED_YET */
361
362#endif /* CFG_CMD_FAT */