Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2011 - 2012 Samsung Electronics |
| 3 | * EXT2/4 filesystem implementation in Uboot by |
| 4 | * Uma Shankar <uma.shankar@samsung.com> |
| 5 | * Manjunatha C Achar <a.manjunatha@samsung.com> |
| 6 | * |
| 7 | * Ext4fs support |
| 8 | * made from existing cmd_ext2.c file of Uboot |
| 9 | * |
| 10 | * (C) Copyright 2004 |
| 11 | * esd gmbh <www.esd-electronics.com> |
| 12 | * Reinhard Arlt <reinhard.arlt@esd-electronics.com> |
| 13 | * |
| 14 | * made from cmd_reiserfs by |
| 15 | * |
| 16 | * (C) Copyright 2003 - 2004 |
| 17 | * Sysgo Real-Time Solutions, AG <www.elinos.com> |
| 18 | * Pavel Bartusek <pba@sysgo.com> |
| 19 | * |
| 20 | * This program is free software; you can redistribute it and/or |
| 21 | * modify it under the terms of the GNU General Public License as |
| 22 | * published by the Free Software Foundation; either version 2 of |
| 23 | * the License, or (at your option) any later version. |
| 24 | * |
| 25 | * This program is distributed in the hope that it will be useful, |
| 26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 28 | * GNU General Public License for more details. |
| 29 | * |
| 30 | * You should have received a copy of the GNU General Public License |
| 31 | * along with this program; if not, write to the Free Software |
| 32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 33 | * MA 02111-1307 USA |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | /* |
| 38 | * Changelog: |
| 39 | * 0.1 - Newly created file for ext4fs support. Taken from cmd_ext2.c |
| 40 | * file in uboot. Added ext4fs ls load and write support. |
| 41 | */ |
| 42 | |
| 43 | #include <common.h> |
| 44 | #include <part.h> |
| 45 | #include <config.h> |
| 46 | #include <command.h> |
| 47 | #include <image.h> |
| 48 | #include <linux/ctype.h> |
| 49 | #include <asm/byteorder.h> |
| 50 | #include <ext_common.h> |
| 51 | #include <ext4fs.h> |
| 52 | #include <linux/stat.h> |
| 53 | #include <malloc.h> |
| 54 | |
| 55 | #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE) |
| 56 | #include <usb.h> |
| 57 | #endif |
| 58 | |
| 59 | #if !defined(CONFIG_DOS_PARTITION) && !defined(CONFIG_EFI_PARTITION) |
| 60 | #error DOS or EFI partition support must be selected |
| 61 | #endif |
| 62 | |
| 63 | #define DOS_PART_MAGIC_OFFSET 0x1fe |
| 64 | #define DOS_FS_TYPE_OFFSET 0x36 |
| 65 | #define DOS_FS32_TYPE_OFFSET 0x52 |
| 66 | |
| 67 | int do_ext_load(cmd_tbl_t *cmdtp, int flag, int argc, |
| 68 | char *const argv[]) |
| 69 | { |
| 70 | char *filename = NULL; |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 71 | int dev, part; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 72 | ulong addr = 0; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 73 | int filelen; |
| 74 | disk_partition_t info; |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 75 | block_dev_desc_t *dev_desc; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 76 | char buf[12]; |
| 77 | unsigned long count; |
| 78 | const char *addr_str; |
| 79 | |
| 80 | count = 0; |
| 81 | addr = simple_strtoul(argv[3], NULL, 16); |
| 82 | filename = getenv("bootfile"); |
| 83 | switch (argc) { |
| 84 | case 3: |
| 85 | addr_str = getenv("loadaddr"); |
| 86 | if (addr_str != NULL) |
| 87 | addr = simple_strtoul(addr_str, NULL, 16); |
| 88 | else |
| 89 | addr = CONFIG_SYS_LOAD_ADDR; |
| 90 | |
| 91 | break; |
| 92 | case 4: |
| 93 | break; |
| 94 | case 5: |
| 95 | filename = argv[4]; |
| 96 | break; |
| 97 | case 6: |
| 98 | filename = argv[4]; |
| 99 | count = simple_strtoul(argv[5], NULL, 16); |
| 100 | break; |
| 101 | |
| 102 | default: |
| 103 | return cmd_usage(cmdtp); |
| 104 | } |
| 105 | |
| 106 | if (!filename) { |
| 107 | puts("** No boot file defined **\n"); |
| 108 | return 1; |
| 109 | } |
| 110 | |
Stephen Warren | 10a37fd | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 111 | part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1); |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 112 | if (part < 0) |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 113 | return 1; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 114 | |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 115 | dev = dev_desc->dev; |
| 116 | printf("Loading file \"%s\" from %s device %d%c%c\n", |
| 117 | filename, argv[1], dev, |
| 118 | part ? ':' : ' ', part ? part + '0' : ' '); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 119 | |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 120 | ext4fs_set_blk_dev(dev_desc, &info); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 121 | |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 122 | if (!ext4fs_mount(info.size)) { |
| 123 | printf("** Bad ext2 partition or disk - %s %d:%d **\n", |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 124 | argv[1], dev, part); |
| 125 | ext4fs_close(); |
| 126 | goto fail; |
| 127 | } |
| 128 | |
| 129 | filelen = ext4fs_open(filename); |
| 130 | if (filelen < 0) { |
| 131 | printf("** File not found %s\n", filename); |
| 132 | ext4fs_close(); |
| 133 | goto fail; |
| 134 | } |
| 135 | if ((count < filelen) && (count != 0)) |
| 136 | filelen = count; |
| 137 | |
| 138 | if (ext4fs_read((char *)addr, filelen) != filelen) { |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 139 | printf("** Unable to read \"%s\" from %s %d:%d **\n", |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 140 | filename, argv[1], dev, part); |
| 141 | ext4fs_close(); |
| 142 | goto fail; |
| 143 | } |
| 144 | |
| 145 | ext4fs_close(); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 146 | /* Loading ok, update default load address */ |
| 147 | load_addr = addr; |
| 148 | |
| 149 | printf("%d bytes read\n", filelen); |
| 150 | sprintf(buf, "%X", filelen); |
| 151 | setenv("filesize", buf); |
| 152 | |
| 153 | return 0; |
| 154 | fail: |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 155 | return 1; |
| 156 | } |
| 157 | |
| 158 | int do_ext_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
| 159 | { |
| 160 | const char *filename = "/"; |
| 161 | int dev; |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 162 | int part; |
| 163 | block_dev_desc_t *dev_desc; |
| 164 | disk_partition_t info; |
| 165 | |
| 166 | if (argc < 2) |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 167 | return cmd_usage(cmdtp); |
| 168 | |
Stephen Warren | 10a37fd | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 169 | part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1); |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 170 | if (part < 0) |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 171 | return 1; |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 172 | |
| 173 | if (argc == 4) |
| 174 | filename = argv[3]; |
| 175 | |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 176 | dev = dev_desc->dev; |
| 177 | ext4fs_set_blk_dev(dev_desc, &info); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 178 | |
Rob Herring | 8118081 | 2012-08-23 11:31:46 +0000 | [diff] [blame] | 179 | if (!ext4fs_mount(info.size)) { |
| 180 | printf("** Bad ext2 partition or disk - %s %d:%d **\n", |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 181 | argv[1], dev, part); |
| 182 | ext4fs_close(); |
| 183 | goto fail; |
| 184 | } |
| 185 | |
| 186 | if (ext4fs_ls(filename)) { |
| 187 | printf("** Error extfs_ls() **\n"); |
| 188 | ext4fs_close(); |
| 189 | goto fail; |
| 190 | }; |
| 191 | |
| 192 | ext4fs_close(); |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 193 | return 0; |
| 194 | |
| 195 | fail: |
Uma Shankar | a159643 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 196 | return 1; |
| 197 | } |