Fixed tools/env utilities
[u-boot-openmoko/mini2440.git] / common / cmd_ext2.c
blobf569406432264f0a61eba2182d1ecd89ebb9d126
1 /*
2 * (C) Copyright 2004
3 * esd gmbh <www.esd-electronics.com>
4 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
6 * made from cmd_reiserfs by
8 * (C) Copyright 2003 - 2004
9 * Sysgo Real-Time Solutions, AG <www.elinos.com>
10 * Pavel Bartusek <pba@sysgo.com>
12 * See file CREDITS for list of people who contributed to this
13 * project.
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.
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.
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
33 * Ext2fs support
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
47 #ifndef CONFIG_DOS_PARTITION
48 #error DOS partition support must be selected
49 #endif
51 /* #define EXT2_DEBUG */
53 #ifdef EXT2_DEBUG
54 #define PRINTF(fmt,args...) printf (fmt ,##args)
55 #else
56 #define PRINTF(fmt,args...)
57 #endif
59 int do_ext2ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
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;
68 if (argc < 3) {
69 printf ("Usage:\n%s\n", cmdtp->usage);
70 return(1);
72 dev = (int)simple_strtoul (argv[2], &ep, 16);
73 dev_desc = get_dev(argv[1],dev);
75 if (dev_desc == NULL) {
76 printf ("\n** Block device %s %d not supported\n", argv[1], dev);
77 return(1);
80 if (*ep) {
81 if (*ep != ':') {
82 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
83 return(1);
85 part = (int)simple_strtoul(++ep, NULL, 16);
88 if (argc == 4) {
89 filename = argv[3];
92 PRINTF("Using device %s %d:%d, directory: %s\n", argv[1], dev, part, filename);
94 if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) {
95 printf ("** Bad partition - %s %d:%d **\n", argv[1], dev, part);
96 ext2fs_close();
97 return(1);
100 if (!ext2fs_mount(part_length)) {
101 printf ("** Bad ext2 partition or disk - %s %d:%d **\n", argv[1], dev, part);
102 ext2fs_close();
103 return(1);
106 if (ext2fs_ls (filename)) {
107 printf ("** Error ext2fs_ls() **\n");
108 ext2fs_close();
109 return(1);
112 ext2fs_close();
114 return(0);
117 U_BOOT_CMD(
118 ext2ls, 4, 1, do_ext2ls,
119 "ext2ls - list files in a directory (default /)\n",
120 "<interface> <dev[:part]> [directory]\n"
121 " - list files from 'dev' on 'interface' in a 'directory'\n"
124 /******************************************************************************
125 * Ext2fs boot command intepreter. Derived from diskboot
127 int do_ext2load (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
129 char *filename = NULL;
130 char *ep;
131 int dev, part = 1;
132 ulong addr = 0, part_length, filelen;
133 disk_partition_t info;
134 block_dev_desc_t *dev_desc = NULL;
135 char buf [12];
136 unsigned long count;
137 char *addr_str;
139 switch (argc) {
140 case 3:
141 addr_str = getenv("loadaddr");
142 if (addr_str != NULL) {
143 addr = simple_strtoul (addr_str, NULL, 16);
144 } else {
145 addr = CFG_LOAD_ADDR;
147 filename = getenv ("bootfile");
148 count = 0;
149 break;
150 case 4:
151 addr = simple_strtoul (argv[3], NULL, 16);
152 filename = getenv ("bootfile");
153 count = 0;
154 break;
155 case 5:
156 addr = simple_strtoul (argv[3], NULL, 16);
157 filename = argv[4];
158 count = 0;
159 break;
160 case 6:
161 addr = simple_strtoul (argv[3], NULL, 16);
162 filename = argv[4];
163 count = simple_strtoul (argv[5], NULL, 16);
164 break;
166 default:
167 printf ("Usage:\n%s\n", cmdtp->usage);
168 return(1);
171 if (!filename) {
172 puts ("\n** No boot file defined **\n");
173 return(1);
176 dev = (int)simple_strtoul (argv[2], &ep, 16);
177 dev_desc = get_dev(argv[1],dev);
178 if (dev_desc==NULL) {
179 printf ("\n** Block device %s %d not supported\n", argv[1], dev);
180 return(1);
182 if (*ep) {
183 if (*ep != ':') {
184 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
185 return(1);
187 part = (int)simple_strtoul(++ep, NULL, 16);
190 PRINTF("Using device %s%d, partition %d\n", argv[1], dev, part);
192 if (part != 0) {
193 if (get_partition_info (dev_desc, part, &info)) {
194 printf ("** Bad partition %d **\n", part);
195 return(1);
198 if (strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) {
199 printf ("\n** Invalid partition type \"%.32s\""
200 " (expect \"" BOOT_PART_TYPE "\")\n",
201 info.type);
202 return(1);
204 PRINTF ("\nLoading from block device %s device %d, partition %d: "
205 "Name: %.32s Type: %.32s File:%s\n",
206 argv[1], dev, part, info.name, info.type, filename);
207 } else {
208 PRINTF ("\nLoading from block device %s device %d, File:%s\n",
209 argv[1], dev, filename);
213 if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) {
214 printf ("** Bad partition - %s %d:%d **\n", argv[1], dev, part);
215 ext2fs_close();
216 return(1);
219 if (!ext2fs_mount(part_length)) {
220 printf ("** Bad ext2 partition or disk - %s %d:%d **\n", argv[1], dev, part);
221 ext2fs_close();
222 return(1);
225 filelen = ext2fs_open(filename);
226 if (filelen < 0) {
227 printf("** File not found %s\n", filename);
228 ext2fs_close();
229 return(1);
231 if ((count < filelen) && (count != 0)) {
232 filelen = count;
235 if (ext2fs_read((char *)addr, filelen) != filelen) {
236 printf("\n** Unable to read \"%s\" from %s %d:%d **\n", filename, argv[1], dev, part);
237 ext2fs_close();
238 return(1);
241 ext2fs_close();
243 /* Loading ok, update default load address */
244 load_addr = addr;
246 printf ("\n%ld bytes read\n", filelen);
247 sprintf(buf, "%lX", filelen);
248 setenv("filesize", buf);
250 return(filelen);
253 U_BOOT_CMD(
254 ext2load, 6, 0, do_ext2load,
255 "ext2load- load binary file from a Ext2 filesystem\n",
256 "<interface> <dev[:part]> [addr] [filename] [bytes]\n"
257 " - load binary file 'filename' from 'dev' on 'interface'\n"
258 " to address 'addr' from ext2 filesystem\n"