]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_ext2.c
75afb320ecc1cf555072c37b7d33eac37ee6db18
[karo-tx-uboot.git] / common / cmd_ext2.c
1 /*
2  * (C) Copyright 2004
3  * esd gmbh <www.esd-electronics.com>
4  * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
5  *
6  * made from cmd_reiserfs by
7  *
8  * (C) Copyright 2003 - 2004
9  * Sysgo Real-Time Solutions, AG <www.elinos.com>
10  * Pavel Bartusek <pba@sysgo.com>
11  *
12  * See file CREDITS for list of people who contributed to this
13  * project.
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License as
17  * published by the Free Software Foundation; either version 2 of
18  * the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28  * MA 02111-1307 USA
29  *
30  */
31
32 /*
33  * Ext2fs support
34  */
35 #include <common.h>
36
37 #if (CONFIG_COMMANDS & CFG_CMD_EXT2)
38 #include <config.h>
39 #include <command.h>
40 #include <image.h>
41 #include <linux/ctype.h>
42 #include <asm/byteorder.h>
43 #include <ext2fs.h>
44
45 #ifndef CONFIG_DOS_PARTITION
46 #error DOS partition support must be selected
47 #endif
48
49 /* #define      EXT2_DEBUG */
50
51 #ifdef  EXT2_DEBUG
52 #define PRINTF(fmt,args...)     printf (fmt ,##args)
53 #else
54 #define PRINTF(fmt,args...)
55 #endif
56
57 static block_dev_desc_t *get_dev (char* ifname, int dev)
58 {
59 #if (CONFIG_COMMANDS & CFG_CMD_IDE)
60         if (strncmp(ifname,"ide",3)==0) {
61                 extern block_dev_desc_t * ide_get_dev(int dev);
62                 return((dev >= CFG_IDE_MAXDEVICE) ? NULL : ide_get_dev(dev));
63         }
64 #endif
65 #if (CONFIG_COMMANDS & CFG_CMD_SCSI)
66         if (strncmp(ifname,"scsi",4)==0) {
67                 extern block_dev_desc_t * scsi_get_dev(int dev);
68                 return((dev >= CFG_SCSI_MAXDEVICE) ? NULL : scsi_get_dev(dev));
69         }
70 #endif
71 #if ((CONFIG_COMMANDS & CFG_CMD_USB) && defined(CONFIG_USB_STORAGE))
72         if (strncmp(ifname,"usb",3)==0) {
73                 extern block_dev_desc_t * usb_stor_get_dev(int dev);
74                 return((dev >= USB_MAX_STOR_DEV) ? NULL : usb_stor_get_dev(dev));
75         }
76 #endif
77 #if defined(CONFIG_MMC)
78         if (strncmp(ifname,"mmc",3)==0) {
79                 extern block_dev_desc_t *  mmc_get_dev(int dev);
80                 return((dev >= 1) ? NULL : mmc_get_dev(dev));
81         }
82 #endif
83 #if defined(CONFIG_SYSTEMACE)
84         if (strcmp(ifname,"ace")==0) {
85                 extern block_dev_desc_t *  systemace_get_dev(int dev);
86                 return((dev >= 1) ? NULL : systemace_get_dev(dev));
87         }
88 #endif
89         return(NULL);
90 }
91
92 int do_ext2ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
93 {
94         char *filename = "/";
95         int dev=0;
96         int part=1;
97         char *ep;
98         block_dev_desc_t *dev_desc=NULL;
99         int part_length;
100
101         if (argc < 3) {
102                 printf ("Usage:\n%s\n", cmdtp->usage);
103                 return(1);
104         }
105         dev = (int)simple_strtoul (argv[2], &ep, 16);
106         dev_desc=get_dev(argv[1],dev);
107
108         if (dev_desc == NULL) {
109                 printf ("\n** Block device %s %d not supported\n", argv[1], dev);
110                 return(1);
111         }
112
113         if (*ep) {
114                 if (*ep != ':') {
115                         puts ("\n** Invalid boot device, use `dev[:part]' **\n");
116                         return(1);
117                 }
118                 part = (int)simple_strtoul(++ep, NULL, 16);
119         }
120
121         if (argc == 4) {
122             filename = argv[3];
123         }
124
125         PRINTF("Using device %s %d:%d, directory: %s\n", argv[1], dev, part, filename);
126
127         if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) {
128                 printf ("** Bad partition - %s %d:%d **\n",  argv[1], dev, part);
129                 ext2fs_close();
130                 return(1);
131         }
132
133         if (!ext2fs_mount(part_length)) {
134                 printf ("** Bad ext2 partition or disk - %s %d:%d **\n",  argv[1], dev, part);
135                 ext2fs_close();
136                 return(1);
137         }
138
139         if (ext2fs_ls (filename)) {
140                 printf ("** Error ext2fs_ls() **\n");
141                 ext2fs_close();
142                 return(1);
143         };
144
145         ext2fs_close();
146
147         return(0);
148 }
149
150 U_BOOT_CMD(
151         ext2ls, 4,      1,      do_ext2ls,
152         "ext2ls- list files in a directory (default /)\n",
153         "<interface> <dev[:part]> [directory]\n"
154         "    - list files from 'dev' on 'interface' in a 'directory'\n"
155 );
156
157 /******************************************************************************
158  * Ext2fs boot command intepreter. Derived from diskboot
159  */
160 int do_ext2load (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
161 {
162         char *filename = NULL;
163         char *ep;
164         int dev, part = 1;
165         ulong addr = 0, part_length, filelen;
166         disk_partition_t info;
167         block_dev_desc_t *dev_desc = NULL;
168         char buf [12];
169         unsigned long count;
170         char *addr_str;
171
172         switch (argc) {
173         case 3:
174                 addr_str = getenv("loadaddr");
175                 if (addr_str != NULL) {
176                         addr = simple_strtoul (addr_str, NULL, 16);
177                 } else {
178                         addr = CFG_LOAD_ADDR;
179                 }
180                 filename = getenv ("bootfile");
181                 count = 0;
182                 break;
183         case 4:
184                 addr = simple_strtoul (argv[3], NULL, 16);
185                 filename = getenv ("bootfile");
186                 count = 0;
187                 break;
188         case 5:
189                 addr = simple_strtoul (argv[3], NULL, 16);
190                 filename = argv[4];
191                 count = 0;
192                 break;
193         case 6:
194                 addr = simple_strtoul (argv[3], NULL, 16);
195                 filename = argv[4];
196                 count = simple_strtoul (argv[5], NULL, 16);
197                 break;
198
199         default:
200                 printf ("Usage:\n%s\n", cmdtp->usage);
201                 return(1);
202         }
203
204         if (!filename) {
205                 puts ("\n** No boot file defined **\n");
206                 return(1);
207         }
208
209         dev = (int)simple_strtoul (argv[2], &ep, 16);
210         dev_desc=get_dev(argv[1],dev);
211         if (dev_desc==NULL) {
212                 printf ("\n** Block device %s %d not supported\n", argv[1], dev);
213                 return(1);
214         }
215         if (*ep) {
216                 if (*ep != ':') {
217                         puts ("\n** Invalid boot device, use `dev[:part]' **\n");
218                         return(1);
219                 }
220                 part = (int)simple_strtoul(++ep, NULL, 16);
221         }
222
223         PRINTF("Using device %s%d, partition %d\n", argv[1], dev, part);
224
225         if (part != 0) {
226                 if (get_partition_info (&dev_desc[dev], part, &info)) {
227                         printf ("** Bad partition %d **\n", part);
228                         return(1);
229                 }
230
231                 if (strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) {
232                         printf ("\n** Invalid partition type \"%.32s\""
233                                 " (expect \"" BOOT_PART_TYPE "\")\n",
234                                 info.type);
235                         return(1);
236                 }
237                 PRINTF ("\nLoading from block device %s device %d, partition %d: "
238                         "Name: %.32s  Type: %.32s  File:%s\n",
239                         argv[1], dev, part, info.name, info.type, filename);
240         } else {
241                 PRINTF ("\nLoading from block device %s device %d, File:%s\n",
242                         argv[1], dev, filename);
243         }
244
245
246         if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) {
247                 printf ("** Bad partition - %s %d:%d **\n",  argv[1], dev, part);
248                 ext2fs_close();
249                 return(1);
250         }
251
252         if (!ext2fs_mount(part_length)) {
253                 printf ("** Bad ext2 partition or disk - %s %d:%d **\n",  argv[1], dev, part);
254                 ext2fs_close();
255                 return(1);
256         }
257
258         filelen = ext2fs_open(filename);
259         if (filelen < 0) {
260                 printf("** File not found %s\n", filename);
261                 ext2fs_close();
262                 return(1);
263         }
264         if ((count < filelen) && (count != 0)) {
265             filelen = count;
266         }
267
268         if (ext2fs_read((char *)addr, filelen) != filelen) {
269                 printf("\n** Unable to read \"%s\" from %s %d:%d **\n", filename, argv[1], dev, part);
270                 ext2fs_close();
271                 return(1);
272         }
273
274         ext2fs_close();
275
276         /* Loading ok, update default load address */
277         load_addr = addr;
278
279         printf ("\n%ld bytes read\n", filelen);
280         sprintf(buf, "%lX", filelen);
281         setenv("filesize", buf);
282
283         return(filelen);
284 }
285
286 U_BOOT_CMD(
287         ext2load,       6,      0,      do_ext2load,
288         "ext2load- load binary file from a Ext2 filesystem\n",
289         "<interface> <dev[:part]> [addr] [filename] [bytes]\n"
290         "    - load binary file 'filename' from 'dev' on 'interface'\n"
291         "      to address 'addr' from ext2 filesystem\n"
292 );
293
294 #endif  /* CONFIG_COMMANDS & CFG_CMD_EXT2 */