blob: 79b1e2fb62f82249b57b8251d3b51c0e9625d969 [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#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>
Jon Loeligerbaa26db2007-07-08 17:51:39 -050043#if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
wdenkb05dcb52005-03-04 11:27:31 +000044#include <usb.h>
45#endif
stroesebcd0be52004-12-16 17:33:10 +000046
richardretanubunfbd85ad2008-10-06 16:10:53 -040047#if !defined(CONFIG_DOS_PARTITION) && !defined(CONFIG_EFI_PARTITION)
48#error DOS or EFI partition support must be selected
stroesebcd0be52004-12-16 17:33:10 +000049#endif
50
51/* #define EXT2_DEBUG */
52
53#ifdef EXT2_DEBUG
54#define PRINTF(fmt,args...) printf (fmt ,##args)
55#else
56#define PRINTF(fmt,args...)
57#endif
58
Wolfgang Denk54841ab2010-06-28 22:00:46 +020059int do_ext2ls (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
stroesebcd0be52004-12-16 17:33:10 +000060{
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
Wolfgang Denk47e26b12010-07-17 01:06:04 +020068 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +000069 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +020070
stroesebcd0be52004-12-16 17:33:10 +000071 dev = (int)simple_strtoul (argv[2], &ep, 16);
Grant Likely735dd972007-02-20 09:04:34 +010072 dev_desc = get_dev(argv[1],dev);
stroesebcd0be52004-12-16 17:33:10 +000073
74 if (dev_desc == NULL) {
75 printf ("\n** Block device %s %d not supported\n", argv[1], dev);
Wolfgang Denk4b751142009-07-28 22:07:37 +020076 return 1;
stroesebcd0be52004-12-16 17:33:10 +000077 }
78
79 if (*ep) {
80 if (*ep != ':') {
81 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
Wolfgang Denk4b751142009-07-28 22:07:37 +020082 return 1;
stroesebcd0be52004-12-16 17:33:10 +000083 }
84 part = (int)simple_strtoul(++ep, NULL, 16);
85 }
86
Wolfgang Denk4b751142009-07-28 22:07:37 +020087 if (argc == 4)
88 filename = argv[3];
stroesebcd0be52004-12-16 17:33:10 +000089
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();
Wolfgang Denk4b751142009-07-28 22:07:37 +020095 return 1;
stroesebcd0be52004-12-16 17:33:10 +000096 }
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();
Wolfgang Denk4b751142009-07-28 22:07:37 +0200101 return 1;
stroesebcd0be52004-12-16 17:33:10 +0000102 }
103
104 if (ext2fs_ls (filename)) {
105 printf ("** Error ext2fs_ls() **\n");
106 ext2fs_close();
Wolfgang Denk4b751142009-07-28 22:07:37 +0200107 return 1;
stroesebcd0be52004-12-16 17:33:10 +0000108 };
109
110 ext2fs_close();
111
Wolfgang Denk4b751142009-07-28 22:07:37 +0200112 return 0;
stroesebcd0be52004-12-16 17:33:10 +0000113}
114
115U_BOOT_CMD(
116 ext2ls, 4, 1, do_ext2ls,
Peter Tyser2fb26042009-01-27 18:03:12 -0600117 "list files in a directory (default /)",
stroesebcd0be52004-12-16 17:33:10 +0000118 "<interface> <dev[:part]> [directory]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200119 " - list files from 'dev' on 'interface' in a 'directory'"
stroesebcd0be52004-12-16 17:33:10 +0000120);
121
122/******************************************************************************
123 * Ext2fs boot command intepreter. Derived from diskboot
124 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200125int do_ext2load (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
stroesebcd0be52004-12-16 17:33:10 +0000126{
127 char *filename = NULL;
128 char *ep;
wdenk20a80412005-02-04 15:02:06 +0000129 int dev, part = 1;
Gao Guanhua351f40c2009-04-14 14:37:35 +0800130 ulong addr = 0, part_length;
131 int filelen;
stroesebcd0be52004-12-16 17:33:10 +0000132 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");
Wolfgang Denk4b751142009-07-28 22:07:37 +0200141 if (addr_str != NULL)
stroesebcd0be52004-12-16 17:33:10 +0000142 addr = simple_strtoul (addr_str, NULL, 16);
Wolfgang Denk4b751142009-07-28 22:07:37 +0200143 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200144 addr = CONFIG_SYS_LOAD_ADDR;
Wolfgang Denk4b751142009-07-28 22:07:37 +0200145
stroesebcd0be52004-12-16 17:33:10 +0000146 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:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000166 return CMD_RET_USAGE;
stroesebcd0be52004-12-16 17:33:10 +0000167 }
168
169 if (!filename) {
Wolfgang Denk4b751142009-07-28 22:07:37 +0200170 puts ("** No boot file defined **\n");
171 return 1;
stroesebcd0be52004-12-16 17:33:10 +0000172 }
173
174 dev = (int)simple_strtoul (argv[2], &ep, 16);
Grant Likely735dd972007-02-20 09:04:34 +0100175 dev_desc = get_dev(argv[1],dev);
stroesebcd0be52004-12-16 17:33:10 +0000176 if (dev_desc==NULL) {
Wolfgang Denk4b751142009-07-28 22:07:37 +0200177 printf ("** Block device %s %d not supported\n", argv[1], dev);
178 return 1;
stroesebcd0be52004-12-16 17:33:10 +0000179 }
180 if (*ep) {
181 if (*ep != ':') {
Wolfgang Denk4b751142009-07-28 22:07:37 +0200182 puts ("** Invalid boot device, use `dev[:part]' **\n");
183 return 1;
stroesebcd0be52004-12-16 17:33:10 +0000184 }
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) {
wdenkb05dcb52005-03-04 11:27:31 +0000191 if (get_partition_info (dev_desc, part, &info)) {
stroesebcd0be52004-12-16 17:33:10 +0000192 printf ("** Bad partition %d **\n", part);
Wolfgang Denk4b751142009-07-28 22:07:37 +0200193 return 1;
stroesebcd0be52004-12-16 17:33:10 +0000194 }
195
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200196 if (strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) {
Wolfgang Denk4b751142009-07-28 22:07:37 +0200197 printf ("** Invalid partition type \"%.32s\""
stroesebcd0be52004-12-16 17:33:10 +0000198 " (expect \"" BOOT_PART_TYPE "\")\n",
199 info.type);
Wolfgang Denk4b751142009-07-28 22:07:37 +0200200 return 1;
stroesebcd0be52004-12-16 17:33:10 +0000201 }
Wolfgang Denk4b751142009-07-28 22:07:37 +0200202 printf ("Loading file \"%s\" "
203 "from %s device %d:%d (%.32s)\n",
204 filename,
205 argv[1], dev, part, info.name);
stroesebcd0be52004-12-16 17:33:10 +0000206 } else {
Wolfgang Denk4b751142009-07-28 22:07:37 +0200207 printf ("Loading file \"%s\" from %s device %d\n",
208 filename, argv[1], dev);
stroesebcd0be52004-12-16 17:33:10 +0000209 }
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();
Wolfgang Denk4b751142009-07-28 22:07:37 +0200215 return 1;
stroesebcd0be52004-12-16 17:33:10 +0000216 }
217
218 if (!ext2fs_mount(part_length)) {
Wolfgang Denk4b751142009-07-28 22:07:37 +0200219 printf ("** Bad ext2 partition or disk - %s %d:%d **\n",
220 argv[1], dev, part);
stroesebcd0be52004-12-16 17:33:10 +0000221 ext2fs_close();
Wolfgang Denk4b751142009-07-28 22:07:37 +0200222 return 1;
stroesebcd0be52004-12-16 17:33:10 +0000223 }
224
225 filelen = ext2fs_open(filename);
226 if (filelen < 0) {
227 printf("** File not found %s\n", filename);
228 ext2fs_close();
Wolfgang Denk4b751142009-07-28 22:07:37 +0200229 return 1;
stroesebcd0be52004-12-16 17:33:10 +0000230 }
231 if ((count < filelen) && (count != 0)) {
232 filelen = count;
233 }
234
235 if (ext2fs_read((char *)addr, filelen) != filelen) {
Wolfgang Denk4b751142009-07-28 22:07:37 +0200236 printf("** Unable to read \"%s\" from %s %d:%d **\n",
237 filename, argv[1], dev, part);
stroesebcd0be52004-12-16 17:33:10 +0000238 ext2fs_close();
Wolfgang Denk4b751142009-07-28 22:07:37 +0200239 return 1;
stroesebcd0be52004-12-16 17:33:10 +0000240 }
241
242 ext2fs_close();
243
244 /* Loading ok, update default load address */
245 load_addr = addr;
246
Wolfgang Denk4b751142009-07-28 22:07:37 +0200247 printf ("%d bytes read\n", filelen);
Wolfgang Denk4d9eab82009-04-28 08:50:31 +0200248 sprintf(buf, "%X", filelen);
stroesebcd0be52004-12-16 17:33:10 +0000249 setenv("filesize", buf);
250
Wolfgang Denk4b751142009-07-28 22:07:37 +0200251 return 0;
stroesebcd0be52004-12-16 17:33:10 +0000252}
253
254U_BOOT_CMD(
255 ext2load, 6, 0, do_ext2load,
Peter Tyser2fb26042009-01-27 18:03:12 -0600256 "load binary file from a Ext2 filesystem",
stroesebcd0be52004-12-16 17:33:10 +0000257 "<interface> <dev[:part]> [addr] [filename] [bytes]\n"
258 " - load binary file 'filename' from 'dev' on 'interface'\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200259 " to address 'addr' from ext2 filesystem"
stroesebcd0be52004-12-16 17:33:10 +0000260);