blob: 8bd2b476e5448f2067f19b839194380cda93337c [file] [log] [blame]
stroesebcd0be52004-12-16 17:33:10 +00001/*
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>
Grant Likely735dd972007-02-20 09:04:34 +010036#include <part.h>
stroesebcd0be52004-12-16 17:33:10 +000037
Jon Loeligerbaa26db2007-07-08 17:51:39 -050038#if defined(CONFIG_CMD_EXT2)
stroesebcd0be52004-12-16 17:33:10 +000039#include <config.h>
40#include <command.h>
41#include <image.h>
42#include <linux/ctype.h>
43#include <asm/byteorder.h>
44#include <ext2fs.h>
Jon Loeligerbaa26db2007-07-08 17:51:39 -050045#if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
wdenkb05dcb52005-03-04 11:27:31 +000046#include <usb.h>
47#endif
stroesebcd0be52004-12-16 17:33:10 +000048
49#ifndef CONFIG_DOS_PARTITION
50#error DOS partition support must be selected
51#endif
52
53/* #define EXT2_DEBUG */
54
55#ifdef EXT2_DEBUG
56#define PRINTF(fmt,args...) printf (fmt ,##args)
57#else
58#define PRINTF(fmt,args...)
59#endif
60
stroesebcd0be52004-12-16 17:33:10 +000061int do_ext2ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
62{
63 char *filename = "/";
64 int dev=0;
65 int part=1;
66 char *ep;
67 block_dev_desc_t *dev_desc=NULL;
68 int part_length;
69
70 if (argc < 3) {
71 printf ("Usage:\n%s\n", cmdtp->usage);
72 return(1);
73 }
74 dev = (int)simple_strtoul (argv[2], &ep, 16);
Grant Likely735dd972007-02-20 09:04:34 +010075 dev_desc = get_dev(argv[1],dev);
stroesebcd0be52004-12-16 17:33:10 +000076
77 if (dev_desc == NULL) {
78 printf ("\n** Block device %s %d not supported\n", argv[1], dev);
79 return(1);
80 }
81
82 if (*ep) {
83 if (*ep != ':') {
84 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
85 return(1);
86 }
87 part = (int)simple_strtoul(++ep, NULL, 16);
88 }
89
90 if (argc == 4) {
91 filename = argv[3];
92 }
93
94 PRINTF("Using device %s %d:%d, directory: %s\n", argv[1], dev, part, filename);
95
96 if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) {
97 printf ("** Bad partition - %s %d:%d **\n", argv[1], dev, part);
98 ext2fs_close();
99 return(1);
100 }
101
102 if (!ext2fs_mount(part_length)) {
103 printf ("** Bad ext2 partition or disk - %s %d:%d **\n", argv[1], dev, part);
104 ext2fs_close();
105 return(1);
106 }
107
108 if (ext2fs_ls (filename)) {
109 printf ("** Error ext2fs_ls() **\n");
110 ext2fs_close();
111 return(1);
112 };
113
114 ext2fs_close();
115
116 return(0);
117}
118
119U_BOOT_CMD(
120 ext2ls, 4, 1, do_ext2ls,
Stefan Roesedc917012005-10-14 15:34:07 +0200121 "ext2ls - list files in a directory (default /)\n",
stroesebcd0be52004-12-16 17:33:10 +0000122 "<interface> <dev[:part]> [directory]\n"
123 " - list files from 'dev' on 'interface' in a 'directory'\n"
124);
125
126/******************************************************************************
127 * Ext2fs boot command intepreter. Derived from diskboot
128 */
129int do_ext2load (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
130{
131 char *filename = NULL;
132 char *ep;
wdenk20a80412005-02-04 15:02:06 +0000133 int dev, part = 1;
stroesebcd0be52004-12-16 17:33:10 +0000134 ulong addr = 0, part_length, filelen;
135 disk_partition_t info;
136 block_dev_desc_t *dev_desc = NULL;
137 char buf [12];
138 unsigned long count;
139 char *addr_str;
140
141 switch (argc) {
142 case 3:
143 addr_str = getenv("loadaddr");
144 if (addr_str != NULL) {
145 addr = simple_strtoul (addr_str, NULL, 16);
146 } else {
147 addr = CFG_LOAD_ADDR;
148 }
149 filename = getenv ("bootfile");
150 count = 0;
151 break;
152 case 4:
153 addr = simple_strtoul (argv[3], NULL, 16);
154 filename = getenv ("bootfile");
155 count = 0;
156 break;
157 case 5:
158 addr = simple_strtoul (argv[3], NULL, 16);
159 filename = argv[4];
160 count = 0;
161 break;
162 case 6:
163 addr = simple_strtoul (argv[3], NULL, 16);
164 filename = argv[4];
165 count = simple_strtoul (argv[5], NULL, 16);
166 break;
167
168 default:
169 printf ("Usage:\n%s\n", cmdtp->usage);
170 return(1);
171 }
172
173 if (!filename) {
174 puts ("\n** No boot file defined **\n");
175 return(1);
176 }
177
178 dev = (int)simple_strtoul (argv[2], &ep, 16);
Grant Likely735dd972007-02-20 09:04:34 +0100179 dev_desc = get_dev(argv[1],dev);
stroesebcd0be52004-12-16 17:33:10 +0000180 if (dev_desc==NULL) {
181 printf ("\n** Block device %s %d not supported\n", argv[1], dev);
182 return(1);
183 }
184 if (*ep) {
185 if (*ep != ':') {
186 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
187 return(1);
188 }
189 part = (int)simple_strtoul(++ep, NULL, 16);
190 }
191
192 PRINTF("Using device %s%d, partition %d\n", argv[1], dev, part);
193
194 if (part != 0) {
wdenkb05dcb52005-03-04 11:27:31 +0000195 if (get_partition_info (dev_desc, part, &info)) {
stroesebcd0be52004-12-16 17:33:10 +0000196 printf ("** Bad partition %d **\n", part);
197 return(1);
198 }
199
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200200 if (strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) {
stroesebcd0be52004-12-16 17:33:10 +0000201 printf ("\n** Invalid partition type \"%.32s\""
202 " (expect \"" BOOT_PART_TYPE "\")\n",
203 info.type);
204 return(1);
205 }
206 PRINTF ("\nLoading from block device %s device %d, partition %d: "
207 "Name: %.32s Type: %.32s File:%s\n",
208 argv[1], dev, part, info.name, info.type, filename);
209 } else {
210 PRINTF ("\nLoading from block device %s device %d, File:%s\n",
211 argv[1], dev, filename);
212 }
213
214
215 if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) {
216 printf ("** Bad partition - %s %d:%d **\n", argv[1], dev, part);
217 ext2fs_close();
218 return(1);
219 }
220
221 if (!ext2fs_mount(part_length)) {
222 printf ("** Bad ext2 partition or disk - %s %d:%d **\n", argv[1], dev, part);
223 ext2fs_close();
224 return(1);
225 }
226
227 filelen = ext2fs_open(filename);
228 if (filelen < 0) {
229 printf("** File not found %s\n", filename);
230 ext2fs_close();
231 return(1);
232 }
233 if ((count < filelen) && (count != 0)) {
234 filelen = count;
235 }
236
237 if (ext2fs_read((char *)addr, filelen) != filelen) {
238 printf("\n** Unable to read \"%s\" from %s %d:%d **\n", filename, argv[1], dev, part);
239 ext2fs_close();
240 return(1);
241 }
242
243 ext2fs_close();
244
245 /* Loading ok, update default load address */
246 load_addr = addr;
247
248 printf ("\n%ld bytes read\n", filelen);
249 sprintf(buf, "%lX", filelen);
250 setenv("filesize", buf);
251
252 return(filelen);
253}
254
255U_BOOT_CMD(
256 ext2load, 6, 0, do_ext2load,
257 "ext2load- load binary file from a Ext2 filesystem\n",
258 "<interface> <dev[:part]> [addr] [filename] [bytes]\n"
259 " - load binary file 'filename' from 'dev' on 'interface'\n"
260 " to address 'addr' from ext2 filesystem\n"
261);
262
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500263#endif