stroese | bcd0be5 | 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 | 735dd97 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 36 | #include <part.h> |
stroese | bcd0be5 | 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 | baa26db | 2007-07-08 17:51:39 -0500 | [diff] [blame] | 43 | #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE) |
wdenk | b05dcb5 | 2005-03-04 11:27:31 +0000 | [diff] [blame] | 44 | #include <usb.h> |
| 45 | #endif |
stroese | bcd0be5 | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 46 | |
richardretanubun | fbd85ad | 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 | bcd0be5 | 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 | |
stroese | bcd0be5 | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 59 | int do_ext2ls (cmd_tbl_t *cmdtp, int flag, int argc, char *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) { |
Peter Tyser | 62c3ae7 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 69 | cmd_usage(cmdtp); |
stroese | bcd0be5 | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 70 | return(1); |
| 71 | } |
| 72 | dev = (int)simple_strtoul (argv[2], &ep, 16); |
Grant Likely | 735dd97 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 73 | dev_desc = get_dev(argv[1],dev); |
stroese | bcd0be5 | 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); |
| 77 | return(1); |
| 78 | } |
| 79 | |
| 80 | if (*ep) { |
| 81 | if (*ep != ':') { |
| 82 | puts ("\n** Invalid boot device, use `dev[:part]' **\n"); |
| 83 | return(1); |
| 84 | } |
| 85 | part = (int)simple_strtoul(++ep, NULL, 16); |
| 86 | } |
| 87 | |
| 88 | if (argc == 4) { |
| 89 | filename = argv[3]; |
| 90 | } |
| 91 | |
| 92 | PRINTF("Using device %s %d:%d, directory: %s\n", argv[1], dev, part, filename); |
| 93 | |
| 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); |
| 98 | } |
| 99 | |
| 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); |
| 104 | } |
| 105 | |
| 106 | if (ext2fs_ls (filename)) { |
| 107 | printf ("** Error ext2fs_ls() **\n"); |
| 108 | ext2fs_close(); |
| 109 | return(1); |
| 110 | }; |
| 111 | |
| 112 | ext2fs_close(); |
| 113 | |
| 114 | return(0); |
| 115 | } |
| 116 | |
| 117 | U_BOOT_CMD( |
| 118 | ext2ls, 4, 1, do_ext2ls, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 119 | "list files in a directory (default /)", |
stroese | bcd0be5 | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 120 | "<interface> <dev[:part]> [directory]\n" |
| 121 | " - list files from 'dev' on 'interface' in a 'directory'\n" |
| 122 | ); |
| 123 | |
| 124 | /****************************************************************************** |
| 125 | * Ext2fs boot command intepreter. Derived from diskboot |
| 126 | */ |
| 127 | int do_ext2load (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 128 | { |
| 129 | char *filename = NULL; |
| 130 | char *ep; |
wdenk | 20a8041 | 2005-02-04 15:02:06 +0000 | [diff] [blame] | 131 | int dev, part = 1; |
Gao Guanhua | 351f40c | 2009-04-14 14:37:35 +0800 | [diff] [blame] | 132 | ulong addr = 0, part_length; |
| 133 | int filelen; |
stroese | bcd0be5 | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 134 | disk_partition_t info; |
| 135 | block_dev_desc_t *dev_desc = NULL; |
| 136 | char buf [12]; |
| 137 | unsigned long count; |
| 138 | char *addr_str; |
| 139 | |
| 140 | switch (argc) { |
| 141 | case 3: |
| 142 | addr_str = getenv("loadaddr"); |
| 143 | if (addr_str != NULL) { |
| 144 | addr = simple_strtoul (addr_str, NULL, 16); |
| 145 | } else { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 146 | addr = CONFIG_SYS_LOAD_ADDR; |
stroese | bcd0be5 | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 147 | } |
| 148 | filename = getenv ("bootfile"); |
| 149 | count = 0; |
| 150 | break; |
| 151 | case 4: |
| 152 | addr = simple_strtoul (argv[3], NULL, 16); |
| 153 | filename = getenv ("bootfile"); |
| 154 | count = 0; |
| 155 | break; |
| 156 | case 5: |
| 157 | addr = simple_strtoul (argv[3], NULL, 16); |
| 158 | filename = argv[4]; |
| 159 | count = 0; |
| 160 | break; |
| 161 | case 6: |
| 162 | addr = simple_strtoul (argv[3], NULL, 16); |
| 163 | filename = argv[4]; |
| 164 | count = simple_strtoul (argv[5], NULL, 16); |
| 165 | break; |
| 166 | |
| 167 | default: |
Peter Tyser | 62c3ae7 | 2009-01-27 18:03:10 -0600 | [diff] [blame] | 168 | cmd_usage(cmdtp); |
stroese | bcd0be5 | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 169 | return(1); |
| 170 | } |
| 171 | |
| 172 | if (!filename) { |
| 173 | puts ("\n** No boot file defined **\n"); |
| 174 | return(1); |
| 175 | } |
| 176 | |
| 177 | dev = (int)simple_strtoul (argv[2], &ep, 16); |
Grant Likely | 735dd97 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 178 | dev_desc = get_dev(argv[1],dev); |
stroese | bcd0be5 | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 179 | if (dev_desc==NULL) { |
| 180 | printf ("\n** Block device %s %d not supported\n", argv[1], dev); |
| 181 | return(1); |
| 182 | } |
| 183 | if (*ep) { |
| 184 | if (*ep != ':') { |
| 185 | puts ("\n** Invalid boot device, use `dev[:part]' **\n"); |
| 186 | return(1); |
| 187 | } |
| 188 | part = (int)simple_strtoul(++ep, NULL, 16); |
| 189 | } |
| 190 | |
| 191 | PRINTF("Using device %s%d, partition %d\n", argv[1], dev, part); |
| 192 | |
| 193 | if (part != 0) { |
wdenk | b05dcb5 | 2005-03-04 11:27:31 +0000 | [diff] [blame] | 194 | if (get_partition_info (dev_desc, part, &info)) { |
stroese | bcd0be5 | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 195 | printf ("** Bad partition %d **\n", part); |
| 196 | return(1); |
| 197 | } |
| 198 | |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 199 | if (strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) { |
stroese | bcd0be5 | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 200 | printf ("\n** Invalid partition type \"%.32s\"" |
| 201 | " (expect \"" BOOT_PART_TYPE "\")\n", |
| 202 | info.type); |
| 203 | return(1); |
| 204 | } |
| 205 | PRINTF ("\nLoading from block device %s device %d, partition %d: " |
| 206 | "Name: %.32s Type: %.32s File:%s\n", |
| 207 | argv[1], dev, part, info.name, info.type, filename); |
| 208 | } else { |
| 209 | PRINTF ("\nLoading from block device %s device %d, File:%s\n", |
| 210 | argv[1], dev, filename); |
| 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(); |
| 217 | return(1); |
| 218 | } |
| 219 | |
| 220 | if (!ext2fs_mount(part_length)) { |
| 221 | printf ("** Bad ext2 partition or disk - %s %d:%d **\n", argv[1], dev, part); |
| 222 | ext2fs_close(); |
| 223 | return(1); |
| 224 | } |
| 225 | |
| 226 | filelen = ext2fs_open(filename); |
| 227 | if (filelen < 0) { |
| 228 | printf("** File not found %s\n", filename); |
| 229 | ext2fs_close(); |
| 230 | return(1); |
| 231 | } |
| 232 | if ((count < filelen) && (count != 0)) { |
| 233 | filelen = count; |
| 234 | } |
| 235 | |
| 236 | if (ext2fs_read((char *)addr, filelen) != filelen) { |
| 237 | printf("\n** Unable to read \"%s\" from %s %d:%d **\n", filename, argv[1], dev, part); |
| 238 | ext2fs_close(); |
| 239 | return(1); |
| 240 | } |
| 241 | |
| 242 | ext2fs_close(); |
| 243 | |
| 244 | /* Loading ok, update default load address */ |
| 245 | load_addr = addr; |
| 246 | |
Wolfgang Denk | 4d9eab8 | 2009-04-28 08:50:31 +0200 | [diff] [blame^] | 247 | printf ("\n%d bytes read\n", filelen); |
| 248 | sprintf(buf, "%X", filelen); |
stroese | bcd0be5 | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 249 | setenv("filesize", buf); |
| 250 | |
| 251 | return(filelen); |
| 252 | } |
| 253 | |
| 254 | U_BOOT_CMD( |
| 255 | ext2load, 6, 0, do_ext2load, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 256 | "load binary file from a Ext2 filesystem", |
stroese | bcd0be5 | 2004-12-16 17:33:10 +0000 | [diff] [blame] | 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\n" |
| 260 | ); |