stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 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> |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 36 | #include <part.h> |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 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> |
Jon Loeliger | 54324d0 | 2007-07-08 17:51:39 -0500 | [diff] [blame] | 43 | #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE) |
wdenk | 286dca8 | 2005-03-04 11:27:31 +0000 | [diff] [blame] | 44 | #include <usb.h> |
| 45 | #endif |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 46 | |
richardretanubun | 05b7735 | 2008-10-06 16:10:53 -0400 | [diff] [blame] | 47 | #if !defined(CONFIG_DOS_PARTITION) && !defined(CONFIG_EFI_PARTITION) |
| 48 | #error DOS or EFI partition support must be selected |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 49 | #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 Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 59 | int do_ext2ls (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 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) { |
Peter Tyser | ddb3af9 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 69 | cmd_usage(cmdtp); |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 70 | return 1; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 71 | } |
| 72 | dev = (int)simple_strtoul (argv[2], &ep, 16); |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 73 | dev_desc = get_dev(argv[1],dev); |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 74 | |
| 75 | if (dev_desc == NULL) { |
| 76 | printf ("\n** Block device %s %d not supported\n", argv[1], dev); |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 77 | return 1; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | if (*ep) { |
| 81 | if (*ep != ':') { |
| 82 | puts ("\n** Invalid boot device, use `dev[:part]' **\n"); |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 83 | return 1; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 84 | } |
| 85 | part = (int)simple_strtoul(++ep, NULL, 16); |
| 86 | } |
| 87 | |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 88 | if (argc == 4) |
| 89 | filename = argv[3]; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 90 | |
| 91 | PRINTF("Using device %s %d:%d, directory: %s\n", argv[1], dev, part, filename); |
| 92 | |
| 93 | if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) { |
| 94 | printf ("** Bad partition - %s %d:%d **\n", argv[1], dev, part); |
| 95 | ext2fs_close(); |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 96 | return 1; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | if (!ext2fs_mount(part_length)) { |
| 100 | printf ("** Bad ext2 partition or disk - %s %d:%d **\n", argv[1], dev, part); |
| 101 | ext2fs_close(); |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 102 | return 1; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | if (ext2fs_ls (filename)) { |
| 106 | printf ("** Error ext2fs_ls() **\n"); |
| 107 | ext2fs_close(); |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 108 | return 1; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | ext2fs_close(); |
| 112 | |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 113 | return 0; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | U_BOOT_CMD( |
| 117 | ext2ls, 4, 1, do_ext2ls, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 118 | "list files in a directory (default /)", |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 119 | "<interface> <dev[:part]> [directory]\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 120 | " - list files from 'dev' on 'interface' in a 'directory'" |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 121 | ); |
| 122 | |
| 123 | /****************************************************************************** |
| 124 | * Ext2fs boot command intepreter. Derived from diskboot |
| 125 | */ |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 126 | int do_ext2load (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 127 | { |
| 128 | char *filename = NULL; |
| 129 | char *ep; |
wdenk | b7345fc | 2005-02-04 15:02:06 +0000 | [diff] [blame] | 130 | int dev, part = 1; |
Gao Guanhua | 3a5c264 | 2009-04-14 14:37:35 +0800 | [diff] [blame] | 131 | ulong addr = 0, part_length; |
| 132 | int filelen; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 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; |
| 138 | |
| 139 | switch (argc) { |
| 140 | case 3: |
| 141 | addr_str = getenv("loadaddr"); |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 142 | if (addr_str != NULL) |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 143 | addr = simple_strtoul (addr_str, NULL, 16); |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 144 | else |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 145 | addr = CONFIG_SYS_LOAD_ADDR; |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 146 | |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 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; |
| 165 | |
| 166 | default: |
Peter Tyser | ddb3af9 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 167 | cmd_usage(cmdtp); |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 168 | return 1; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | if (!filename) { |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 172 | puts ("** No boot file defined **\n"); |
| 173 | return 1; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | dev = (int)simple_strtoul (argv[2], &ep, 16); |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 177 | dev_desc = get_dev(argv[1],dev); |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 178 | if (dev_desc==NULL) { |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 179 | printf ("** Block device %s %d not supported\n", argv[1], dev); |
| 180 | return 1; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 181 | } |
| 182 | if (*ep) { |
| 183 | if (*ep != ':') { |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 184 | puts ("** Invalid boot device, use `dev[:part]' **\n"); |
| 185 | return 1; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 186 | } |
| 187 | part = (int)simple_strtoul(++ep, NULL, 16); |
| 188 | } |
| 189 | |
| 190 | PRINTF("Using device %s%d, partition %d\n", argv[1], dev, part); |
| 191 | |
| 192 | if (part != 0) { |
wdenk | 286dca8 | 2005-03-04 11:27:31 +0000 | [diff] [blame] | 193 | if (get_partition_info (dev_desc, part, &info)) { |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 194 | printf ("** Bad partition %d **\n", part); |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 195 | return 1; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 198 | if (strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) { |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 199 | printf ("** Invalid partition type \"%.32s\"" |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 200 | " (expect \"" BOOT_PART_TYPE "\")\n", |
| 201 | info.type); |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 202 | return 1; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 203 | } |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 204 | printf ("Loading file \"%s\" " |
| 205 | "from %s device %d:%d (%.32s)\n", |
| 206 | filename, |
| 207 | argv[1], dev, part, info.name); |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 208 | } else { |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 209 | printf ("Loading file \"%s\" from %s device %d\n", |
| 210 | filename, argv[1], dev); |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | |
| 214 | if ((part_length = ext2fs_set_blk_dev(dev_desc, part)) == 0) { |
| 215 | printf ("** Bad partition - %s %d:%d **\n", argv[1], dev, part); |
| 216 | ext2fs_close(); |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 217 | return 1; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | if (!ext2fs_mount(part_length)) { |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 221 | printf ("** Bad ext2 partition or disk - %s %d:%d **\n", |
| 222 | argv[1], dev, part); |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 223 | ext2fs_close(); |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 224 | return 1; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | filelen = ext2fs_open(filename); |
| 228 | if (filelen < 0) { |
| 229 | printf("** File not found %s\n", filename); |
| 230 | ext2fs_close(); |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 231 | return 1; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 232 | } |
| 233 | if ((count < filelen) && (count != 0)) { |
| 234 | filelen = count; |
| 235 | } |
| 236 | |
| 237 | if (ext2fs_read((char *)addr, filelen) != filelen) { |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 238 | printf("** Unable to read \"%s\" from %s %d:%d **\n", |
| 239 | filename, argv[1], dev, part); |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 240 | ext2fs_close(); |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 241 | return 1; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | ext2fs_close(); |
| 245 | |
| 246 | /* Loading ok, update default load address */ |
| 247 | load_addr = addr; |
| 248 | |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 249 | printf ("%d bytes read\n", filelen); |
Wolfgang Denk | 0266428 | 2009-04-28 08:50:31 +0200 | [diff] [blame] | 250 | sprintf(buf, "%X", filelen); |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 251 | setenv("filesize", buf); |
| 252 | |
Wolfgang Denk | ec965ff | 2009-07-28 22:07:37 +0200 | [diff] [blame] | 253 | return 0; |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | U_BOOT_CMD( |
| 257 | ext2load, 6, 0, do_ext2load, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 258 | "load binary file from a Ext2 filesystem", |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 259 | "<interface> <dev[:part]> [addr] [filename] [bytes]\n" |
| 260 | " - load binary file 'filename' from 'dev' on 'interface'\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 261 | " to address 'addr' from ext2 filesystem" |
stroese | 084f67c | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 262 | ); |