1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <common.h>
36#include <part.h>
37#include <config.h>
38#include <command.h>
39#include <image.h>
40#include <linux/ctype.h>
41#include <asm/byteorder.h>
42#include <ext2fs.h>
43#if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
44#include <usb.h>
45#endif
46
47#if !defined(CONFIG_DOS_PARTITION) && !defined(CONFIG_EFI_PARTITION)
48#error DOS or EFI partition support must be selected
49#endif
50
51
52
53#ifdef EXT2_DEBUG
54#define PRINTF(fmt,args...) printf (fmt ,##args)
55#else
56#define PRINTF(fmt,args...)
57#endif
58
59int do_ext2ls (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
60{
61 char *filename = "/";
62 int dev=0;
63 int part=1;
64 char *ep;
65 block_dev_desc_t *dev_desc=NULL;
66 int part_length;
67
68 if (argc < 3)
69 return cmd_usage(cmdtp);
70
71 dev = (int)simple_strtoul (argv[2], &ep, 16);
72 dev_desc = get_dev(argv[1],dev);
73
74 if (dev_desc == NULL) {
75 printf ("\n** Block device %s %d not supported\n", argv[1], dev);
76 return 1;
77 }
78
79 if (*ep) {
80 if (*ep != ':') {
81 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
82 return 1;
83 }
84 part = (int)simple_strtoul(++ep, NULL, 16);
85 }
86
87 if (argc == 4)
88 filename = argv[3];
89
90 PRINTF("Using device %s %d:%d, directory: %s\n", argv[1], dev, part, filename);
91
92 if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) {
93 printf ("** Bad partition - %s %d:%d **\n", argv[1], dev, part);
94 ext2fs_close();
95 return 1;
96 }
97
98 if (!ext2fs_mount(part_length)) {
99 printf ("** Bad ext2 partition or disk - %s %d:%d **\n", argv[1], dev, part);
100 ext2fs_close();
101 return 1;
102 }
103
104 if (ext2fs_ls (filename)) {
105 printf ("** Error ext2fs_ls() **\n");
106 ext2fs_close();
107 return 1;
108 };
109
110 ext2fs_close();
111
112 return 0;
113}
114
115U_BOOT_CMD(
116 ext2ls, 4, 1, do_ext2ls,
117 "list files in a directory (default /)",
118 "<interface> <dev[:part]> [directory]\n"
119 " - list files from 'dev' on 'interface' in a 'directory'"
120);
121
122
123
124
125int do_ext2load (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
126{
127 char *filename = NULL;
128 char *ep;
129 int dev, part = 1;
130 ulong addr = 0, part_length;
131 int filelen;
132 disk_partition_t info;
133 block_dev_desc_t *dev_desc = NULL;
134 char buf [12];
135 unsigned long count;
136 char *addr_str;
137
138 switch (argc) {
139 case 3:
140 addr_str = getenv("loadaddr");
141 if (addr_str != NULL)
142 addr = simple_strtoul (addr_str, NULL, 16);
143 else
144 addr = CONFIG_SYS_LOAD_ADDR;
145
146 filename = getenv ("bootfile");
147 count = 0;
148 break;
149 case 4:
150 addr = simple_strtoul (argv[3], NULL, 16);
151 filename = getenv ("bootfile");
152 count = 0;
153 break;
154 case 5:
155 addr = simple_strtoul (argv[3], NULL, 16);
156 filename = argv[4];
157 count = 0;
158 break;
159 case 6:
160 addr = simple_strtoul (argv[3], NULL, 16);
161 filename = argv[4];
162 count = simple_strtoul (argv[5], NULL, 16);
163 break;
164
165 default:
166 return cmd_usage(cmdtp);
167 }
168
169 if (!filename) {
170 puts ("** No boot file defined **\n");
171 return 1;
172 }
173
174 dev = (int)simple_strtoul (argv[2], &ep, 16);
175 dev_desc = get_dev(argv[1],dev);
176 if (dev_desc==NULL) {
177 printf ("** Block device %s %d not supported\n", argv[1], dev);
178 return 1;
179 }
180 if (*ep) {
181 if (*ep != ':') {
182 puts ("** Invalid boot device, use `dev[:part]' **\n");
183 return 1;
184 }
185 part = (int)simple_strtoul(++ep, NULL, 16);
186 }
187
188 PRINTF("Using device %s%d, partition %d\n", argv[1], dev, part);
189
190 if (part != 0) {
191 if (get_partition_info (dev_desc, part, &info)) {
192 printf ("** Bad partition %d **\n", part);
193 return 1;
194 }
195
196 if (strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) {
197 printf ("** Invalid partition type \"%.32s\""
198 " (expect \"" BOOT_PART_TYPE "\")\n",
199 info.type);
200 return 1;
201 }
202 printf ("Loading file \"%s\" "
203 "from %s device %d:%d (%.32s)\n",
204 filename,
205 argv[1], dev, part, info.name);
206 } else {
207 printf ("Loading file \"%s\" from %s device %d\n",
208 filename, argv[1], dev);
209 }
210
211
212 if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) {
213 printf ("** Bad partition - %s %d:%d **\n", argv[1], dev, part);
214 ext2fs_close();
215 return 1;
216 }
217
218 if (!ext2fs_mount(part_length)) {
219 printf ("** Bad ext2 partition or disk - %s %d:%d **\n",
220 argv[1], dev, part);
221 ext2fs_close();
222 return 1;
223 }
224
225 filelen = ext2fs_open(filename);
226 if (filelen < 0) {
227 printf("** File not found %s\n", filename);
228 ext2fs_close();
229 return 1;
230 }
231 if ((count < filelen) && (count != 0)) {
232 filelen = count;
233 }
234
235 if (ext2fs_read((char *)addr, filelen) != filelen) {
236 printf("** Unable to read \"%s\" from %s %d:%d **\n",
237 filename, argv[1], dev, part);
238 ext2fs_close();
239 return 1;
240 }
241
242 ext2fs_close();
243
244
245 load_addr = addr;
246
247 printf ("%d bytes read\n", filelen);
248 sprintf(buf, "%X", filelen);
249 setenv("filesize", buf);
250
251 return 0;
252}
253
254U_BOOT_CMD(
255 ext2load, 6, 0, do_ext2load,
256 "load binary file from a Ext2 filesystem",
257 "<interface> <dev[:part]> [addr] [filename] [bytes]\n"
258 " - load binary file 'filename' from 'dev' on 'interface'\n"
259 " to address 'addr' from ext2 filesystem"
260);
261