wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 4 | * |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 5 | * (C) Copyright 2002 |
| 6 | * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de> |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 7 | * |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 8 | * (C) Copyright 2003 |
| 9 | * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 10 | * |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 11 | * (C) Copyright 2005 |
| 12 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 13 | * |
| 14 | * Added support for reading flash partition table from environment. |
| 15 | * Parsing routines are based on driver/mtd/cmdline.c from the linux 2.4 |
| 16 | * kernel tree. |
| 17 | * |
| 18 | * $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $ |
| 19 | * Copyright 2002 SYSGO Real-Time Solutions GmbH |
| 20 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 21 | * SPDX-License-Identifier: GPL-2.0+ |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * Three environment variables are used by the parsing routines: |
| 26 | * |
| 27 | * 'partition' - keeps current partition identifier |
| 28 | * |
| 29 | * partition := <part-id> |
| 30 | * <part-id> := <dev-id>,part_num |
| 31 | * |
Wolfgang Denk | eb95c85 | 2005-08-10 15:14:32 +0200 | [diff] [blame] | 32 | * |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 33 | * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping |
| 34 | * |
| 35 | * mtdids=<idmap>[,<idmap>,...] |
| 36 | * |
| 37 | * <idmap> := <dev-id>=<mtd-id> |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 38 | * <dev-id> := 'nand'|'nor'|'onenand'<dev-num> |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 39 | * <dev-num> := mtd device number, 0... |
| 40 | * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name) |
| 41 | * |
| 42 | * |
| 43 | * 'mtdparts' - partition list |
| 44 | * |
| 45 | * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...] |
| 46 | * |
| 47 | * <mtd-def> := <mtd-id>:<part-def>[,<part-def>...] |
| 48 | * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name) |
| 49 | * <part-def> := <size>[@<offset>][<name>][<ro-flag>] |
| 50 | * <size> := standard linux memsize OR '-' to denote all remaining space |
| 51 | * <offset> := partition start offset within the device |
| 52 | * <name> := '(' NAME ')' |
| 53 | * <ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel) |
| 54 | * |
| 55 | * Notes: |
| 56 | * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping |
| 57 | * - if the above variables are not set defaults for a given target are used |
| 58 | * |
| 59 | * Examples: |
| 60 | * |
| 61 | * 1 NOR Flash, with 1 single writable partition: |
| 62 | * mtdids=nor0=edb7312-nor |
| 63 | * mtdparts=mtdparts=edb7312-nor:- |
| 64 | * |
| 65 | * 1 NOR Flash with 2 partitions, 1 NAND with one |
| 66 | * mtdids=nor0=edb7312-nor,nand0=edb7312-nand |
| 67 | * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home) |
| 68 | * |
| 69 | */ |
| 70 | |
| 71 | /* |
Michal Simek | 40277b3 | 2007-09-15 00:03:35 +0200 | [diff] [blame] | 72 | * JFFS2/CRAMFS support |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 73 | */ |
| 74 | #include <common.h> |
| 75 | #include <command.h> |
| 76 | #include <malloc.h> |
| 77 | #include <jffs2/jffs2.h> |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 78 | #include <linux/list.h> |
| 79 | #include <linux/ctype.h> |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 80 | #include <cramfs/cramfs_fs.h> |
| 81 | |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 82 | #if defined(CONFIG_CMD_NAND) |
Bartlomiej Sieka | 582f1a3 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 83 | #include <linux/mtd/nand.h> |
Wolfgang Denk | 7b9bc3a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 84 | #include <nand.h> |
Jon Loeliger | d76b5c1 | 2007-07-08 18:02:23 -0500 | [diff] [blame] | 85 | #endif |
Kyungmin Park | 2538374 | 2008-08-27 14:45:20 +0900 | [diff] [blame] | 86 | |
| 87 | #if defined(CONFIG_CMD_ONENAND) |
| 88 | #include <linux/mtd/mtd.h> |
| 89 | #include <linux/mtd/onenand.h> |
| 90 | #include <onenand_uboot.h> |
| 91 | #endif |
| 92 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 93 | /* enable/disable debugging messages */ |
Bartlomiej Sieka | 6eed2ab | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 94 | #define DEBUG_JFFS |
| 95 | #undef DEBUG_JFFS |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 96 | |
Michal Simek | 6ed8c71 | 2007-08-06 23:31:49 +0200 | [diff] [blame] | 97 | #ifdef DEBUG_JFFS |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 98 | # define DEBUGF(fmt, args...) printf(fmt ,##args) |
| 99 | #else |
| 100 | # define DEBUGF(fmt, args...) |
| 101 | #endif |
| 102 | |
| 103 | /* special size referring to all the remaining space in a partition */ |
| 104 | #define SIZE_REMAINING 0xFFFFFFFF |
| 105 | |
| 106 | /* special offset value, it is used when not provided by user |
| 107 | * |
| 108 | * this value is used temporarily during parsing, later such offests |
| 109 | * are recalculated */ |
| 110 | #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF |
| 111 | |
| 112 | /* minimum partition size */ |
| 113 | #define MIN_PART_SIZE 4096 |
| 114 | |
| 115 | /* this flag needs to be set in part_info struct mask_flags |
| 116 | * field for read-only partitions */ |
Bartlomiej Sieka | 6eed2ab | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 117 | #define MTD_WRITEABLE_CMD 1 |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 118 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 119 | /* current active device and partition number */ |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 120 | #ifdef CONFIG_CMD_MTDPARTS |
| 121 | /* Use the ones declared in cmd_mtdparts.c */ |
| 122 | extern struct mtd_device *current_mtd_dev; |
| 123 | extern u8 current_mtd_partnum; |
| 124 | #else |
| 125 | /* Use local ones */ |
| 126 | struct mtd_device *current_mtd_dev = NULL; |
| 127 | u8 current_mtd_partnum = 0; |
| 128 | #endif |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 129 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 130 | #if defined(CONFIG_CMD_CRAMFS) |
| 131 | extern int cramfs_check (struct part_info *info); |
| 132 | extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename); |
| 133 | extern int cramfs_ls (struct part_info *info, char *filename); |
| 134 | extern int cramfs_info (struct part_info *info); |
| 135 | #else |
| 136 | /* defining empty macros for function names is ugly but avoids ifdef clutter |
| 137 | * all over the code */ |
| 138 | #define cramfs_check(x) (0) |
| 139 | #define cramfs_load(x,y,z) (-1) |
| 140 | #define cramfs_ls(x,y) (0) |
| 141 | #define cramfs_info(x) (0) |
| 142 | #endif |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 143 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 144 | #ifndef CONFIG_CMD_MTDPARTS |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 145 | /** |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 146 | * Check device number to be within valid range for given device type. |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 147 | * |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 148 | * @param dev device to validate |
| 149 | * @return 0 if device is valid, 1 otherwise |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 150 | */ |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 151 | static int mtd_device_validate(u8 type, u8 num, u32 *size) |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 152 | { |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 153 | if (type == MTD_DEV_TYPE_NOR) { |
| 154 | #if defined(CONFIG_CMD_FLASH) |
| 155 | if (num < CONFIG_SYS_MAX_FLASH_BANKS) { |
| 156 | extern flash_info_t flash_info[]; |
| 157 | *size = flash_info[num].size; |
wdenk | ad276f2 | 2004-01-04 16:28:35 +0000 | [diff] [blame] | 158 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 159 | return 0; |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 160 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 161 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 162 | printf("no such FLASH device: %s%d (valid range 0 ... %d\n", |
| 163 | MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_FLASH_BANKS - 1); |
| 164 | #else |
| 165 | printf("support for FLASH devices not present\n"); |
| 166 | #endif |
| 167 | } else if (type == MTD_DEV_TYPE_NAND) { |
| 168 | #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND) |
Grygorii Strashko | 6d2a2668 | 2017-06-26 19:12:55 -0500 | [diff] [blame] | 169 | struct mtd_info *mtd = get_nand_dev_by_index(num); |
| 170 | if (mtd) { |
| 171 | *size = mtd->size; |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 172 | return 0; |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 173 | } |
wdenk | da04a8b | 2004-08-02 23:22:59 +0000 | [diff] [blame] | 174 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 175 | printf("no such NAND device: %s%d (valid range 0 ... %d)\n", |
| 176 | MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1); |
| 177 | #else |
| 178 | printf("support for NAND devices not present\n"); |
| 179 | #endif |
| 180 | } else if (type == MTD_DEV_TYPE_ONENAND) { |
| 181 | #if defined(CONFIG_CMD_ONENAND) |
| 182 | *size = onenand_mtd.size; |
| 183 | return 0; |
| 184 | #else |
| 185 | printf("support for OneNAND devices not present\n"); |
| 186 | #endif |
| 187 | } else |
| 188 | printf("Unknown defice type %d\n", type); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 189 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 190 | return 1; |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 191 | } |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 192 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 193 | /** |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 194 | * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>, |
| 195 | * return device type and number. |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 196 | * |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 197 | * @param id string describing device id |
| 198 | * @param ret_id output pointer to next char after parse completes (output) |
| 199 | * @param dev_type parsed device type (output) |
| 200 | * @param dev_num parsed device number (output) |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 201 | * @return 0 on success, 1 otherwise |
| 202 | */ |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 203 | static int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 204 | { |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 205 | const char *p = id; |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 206 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 207 | *dev_type = 0; |
| 208 | if (strncmp(p, "nand", 4) == 0) { |
| 209 | *dev_type = MTD_DEV_TYPE_NAND; |
| 210 | p += 4; |
| 211 | } else if (strncmp(p, "nor", 3) == 0) { |
| 212 | *dev_type = MTD_DEV_TYPE_NOR; |
| 213 | p += 3; |
| 214 | } else if (strncmp(p, "onenand", 7) == 0) { |
| 215 | *dev_type = MTD_DEV_TYPE_ONENAND; |
| 216 | p += 7; |
| 217 | } else { |
| 218 | printf("incorrect device type in %s\n", id); |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 219 | return 1; |
| 220 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 221 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 222 | if (!isdigit(*p)) { |
| 223 | printf("incorrect device number in %s\n", id); |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 224 | return 1; |
| 225 | } |
| 226 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 227 | *dev_num = simple_strtoul(p, (char **)&p, 0); |
| 228 | if (ret_id) |
| 229 | *ret_id = p; |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 230 | return 0; |
| 231 | } |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 232 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 233 | /* |
| 234 | * 'Static' version of command line mtdparts_init() routine. Single partition on |
| 235 | * a single device configuration. |
| 236 | */ |
| 237 | |
| 238 | /** |
Tomasz Figa | 63440e0 | 2008-12-30 18:35:57 +0100 | [diff] [blame] | 239 | * Calculate sector size. |
| 240 | * |
| 241 | * @return sector size |
| 242 | */ |
| 243 | static inline u32 get_part_sector_size_nand(struct mtdids *id) |
| 244 | { |
| 245 | #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND) |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 246 | struct mtd_info *mtd; |
Tomasz Figa | 63440e0 | 2008-12-30 18:35:57 +0100 | [diff] [blame] | 247 | |
Grygorii Strashko | 6d2a2668 | 2017-06-26 19:12:55 -0500 | [diff] [blame] | 248 | mtd = get_nand_dev_by_index(id->num); |
Tomasz Figa | 63440e0 | 2008-12-30 18:35:57 +0100 | [diff] [blame] | 249 | |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 250 | return mtd->erasesize; |
Tomasz Figa | 63440e0 | 2008-12-30 18:35:57 +0100 | [diff] [blame] | 251 | #else |
| 252 | BUG(); |
| 253 | return 0; |
| 254 | #endif |
| 255 | } |
| 256 | |
| 257 | static inline u32 get_part_sector_size_nor(struct mtdids *id, struct part_info *part) |
| 258 | { |
| 259 | #if defined(CONFIG_CMD_FLASH) |
| 260 | extern flash_info_t flash_info[]; |
| 261 | |
| 262 | u32 end_phys, start_phys, sector_size = 0, size = 0; |
| 263 | int i; |
| 264 | flash_info_t *flash; |
| 265 | |
| 266 | flash = &flash_info[id->num]; |
| 267 | |
| 268 | start_phys = flash->start[0] + part->offset; |
Peter Tyser | 5e26c2a | 2010-12-30 15:47:56 -0600 | [diff] [blame] | 269 | end_phys = start_phys + part->size - 1; |
Tomasz Figa | 63440e0 | 2008-12-30 18:35:57 +0100 | [diff] [blame] | 270 | |
| 271 | for (i = 0; i < flash->sector_count; i++) { |
| 272 | if (flash->start[i] >= end_phys) |
| 273 | break; |
| 274 | |
| 275 | if (flash->start[i] >= start_phys) { |
| 276 | if (i == flash->sector_count - 1) { |
| 277 | size = flash->start[0] + flash->size - flash->start[i]; |
| 278 | } else { |
| 279 | size = flash->start[i+1] - flash->start[i]; |
| 280 | } |
| 281 | |
| 282 | if (sector_size < size) |
| 283 | sector_size = size; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | return sector_size; |
| 288 | #else |
| 289 | BUG(); |
| 290 | return 0; |
| 291 | #endif |
| 292 | } |
| 293 | |
| 294 | static inline u32 get_part_sector_size_onenand(void) |
| 295 | { |
| 296 | #if defined(CONFIG_CMD_ONENAND) |
| 297 | struct mtd_info *mtd; |
| 298 | |
| 299 | mtd = &onenand_mtd; |
| 300 | |
| 301 | return mtd->erasesize; |
| 302 | #else |
| 303 | BUG(); |
| 304 | return 0; |
| 305 | #endif |
| 306 | } |
| 307 | |
| 308 | static inline u32 get_part_sector_size(struct mtdids *id, struct part_info *part) |
| 309 | { |
| 310 | if (id->type == MTD_DEV_TYPE_NAND) |
| 311 | return get_part_sector_size_nand(id); |
| 312 | else if (id->type == MTD_DEV_TYPE_NOR) |
| 313 | return get_part_sector_size_nor(id, part); |
| 314 | else if (id->type == MTD_DEV_TYPE_ONENAND) |
| 315 | return get_part_sector_size_onenand(); |
| 316 | else |
| 317 | DEBUGF("Error: Unknown device type.\n"); |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | /** |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 323 | * Parse and initialize global mtdids mapping and create global |
Wolfgang Denk | eb95c85 | 2005-08-10 15:14:32 +0200 | [diff] [blame] | 324 | * device/partition list. |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 325 | * |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 326 | * 'Static' version of command line mtdparts_init() routine. Single partition on |
| 327 | * a single device configuration. |
| 328 | * |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 329 | * @return 0 on success, 1 otherwise |
| 330 | */ |
| 331 | int mtdparts_init(void) |
| 332 | { |
| 333 | static int initialized = 0; |
| 334 | u32 size; |
| 335 | char *dev_name; |
| 336 | |
| 337 | DEBUGF("\n---mtdparts_init---\n"); |
| 338 | if (!initialized) { |
Wolfgang Denk | c0d487b | 2005-08-09 21:41:20 +0200 | [diff] [blame] | 339 | struct mtdids *id; |
| 340 | struct part_info *part; |
| 341 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 342 | initialized = 1; |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 343 | current_mtd_dev = (struct mtd_device *) |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 344 | malloc(sizeof(struct mtd_device) + |
| 345 | sizeof(struct part_info) + |
| 346 | sizeof(struct mtdids)); |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 347 | if (!current_mtd_dev) { |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 348 | printf("out of memory\n"); |
| 349 | return 1; |
| 350 | } |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 351 | memset(current_mtd_dev, 0, sizeof(struct mtd_device) + |
| 352 | sizeof(struct part_info) + sizeof(struct mtdids)); |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 353 | |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 354 | id = (struct mtdids *)(current_mtd_dev + 1); |
Wolfgang Denk | c0d487b | 2005-08-09 21:41:20 +0200 | [diff] [blame] | 355 | part = (struct part_info *)(id + 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 356 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 357 | /* id */ |
| 358 | id->mtd_id = "single part"; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 359 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 360 | #if defined(CONFIG_JFFS2_DEV) |
| 361 | dev_name = CONFIG_JFFS2_DEV; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 362 | #else |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 363 | dev_name = "nor0"; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 364 | #endif |
| 365 | |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 366 | if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) || |
| 367 | (mtd_device_validate(id->type, id->num, &size) != 0)) { |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 368 | printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num); |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 369 | free(current_mtd_dev); |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 370 | return 1; |
| 371 | } |
| 372 | id->size = size; |
| 373 | INIT_LIST_HEAD(&id->link); |
| 374 | |
| 375 | DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n", |
| 376 | id->type, id->num, id->size, id->mtd_id); |
| 377 | |
| 378 | /* partition */ |
| 379 | part->name = "static"; |
| 380 | part->auto_name = 0; |
| 381 | |
| 382 | #if defined(CONFIG_JFFS2_PART_SIZE) |
| 383 | part->size = CONFIG_JFFS2_PART_SIZE; |
| 384 | #else |
| 385 | part->size = SIZE_REMAINING; |
| 386 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 387 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 388 | #if defined(CONFIG_JFFS2_PART_OFFSET) |
| 389 | part->offset = CONFIG_JFFS2_PART_OFFSET; |
| 390 | #else |
| 391 | part->offset = 0x00000000; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 392 | #endif |
| 393 | |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 394 | part->dev = current_mtd_dev; |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 395 | INIT_LIST_HEAD(&part->link); |
| 396 | |
| 397 | /* recalculate size if needed */ |
| 398 | if (part->size == SIZE_REMAINING) |
| 399 | part->size = id->size - part->offset; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 400 | |
Mike Frysinger | b5125d2 | 2010-01-08 08:03:06 -0500 | [diff] [blame] | 401 | part->sector_size = get_part_sector_size(id, part); |
| 402 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 403 | DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n", |
| 404 | part->name, part->size, part->offset); |
| 405 | |
| 406 | /* device */ |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 407 | current_mtd_dev->id = id; |
| 408 | INIT_LIST_HEAD(¤t_mtd_dev->link); |
| 409 | current_mtd_dev->num_parts = 1; |
| 410 | INIT_LIST_HEAD(¤t_mtd_dev->parts); |
| 411 | list_add(&part->link, ¤t_mtd_dev->parts); |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 412 | } |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 413 | |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 414 | return 0; |
| 415 | } |
Stefan Roese | b1423dd | 2009-03-19 13:30:36 +0100 | [diff] [blame] | 416 | #endif /* #ifndef CONFIG_CMD_MTDPARTS */ |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 417 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 418 | /** |
| 419 | * Return pointer to the partition of a requested number from a requested |
| 420 | * device. |
| 421 | * |
| 422 | * @param dev device that is to be searched for a partition |
| 423 | * @param part_num requested partition number |
| 424 | * @return pointer to the part_info, NULL otherwise |
| 425 | */ |
| 426 | static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num) |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 427 | { |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 428 | struct list_head *entry; |
| 429 | struct part_info *part; |
| 430 | int num; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 431 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 432 | if (!dev) |
| 433 | return NULL; |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 434 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 435 | DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n", |
| 436 | part_num, MTD_DEV_TYPE(dev->id->type), |
| 437 | dev->id->num, dev->id->mtd_id); |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 438 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 439 | if (part_num >= dev->num_parts) { |
| 440 | printf("invalid partition number %d for device %s%d (%s)\n", |
| 441 | part_num, MTD_DEV_TYPE(dev->id->type), |
| 442 | dev->id->num, dev->id->mtd_id); |
| 443 | return NULL; |
| 444 | } |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 445 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 446 | /* locate partition number, return it */ |
| 447 | num = 0; |
| 448 | list_for_each(entry, &dev->parts) { |
| 449 | part = list_entry(entry, struct part_info, link); |
| 450 | |
| 451 | if (part_num == num++) { |
| 452 | return part; |
| 453 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 454 | } |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 455 | |
| 456 | return NULL; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 457 | } |
wdenk | 8886a66 | 2004-04-18 19:43:36 +0000 | [diff] [blame] | 458 | |
Michal Simek | 6ed8c71 | 2007-08-06 23:31:49 +0200 | [diff] [blame] | 459 | /***************************************************/ |
Bin Meng | 7557405 | 2016-02-05 19:30:11 -0800 | [diff] [blame] | 460 | /* U-Boot commands */ |
Michal Simek | 6ed8c71 | 2007-08-06 23:31:49 +0200 | [diff] [blame] | 461 | /***************************************************/ |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 462 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 463 | /** |
| 464 | * Routine implementing fsload u-boot command. This routine tries to load |
| 465 | * a requested file from jffs2/cramfs filesystem on a current partition. |
| 466 | * |
| 467 | * @param cmdtp command internal data |
| 468 | * @param flag command flag |
| 469 | * @param argc number of arguments supplied to the command |
| 470 | * @param argv arguments list |
| 471 | * @return 0 on success, 1 otherwise |
| 472 | */ |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 473 | int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 474 | { |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 475 | char *fsname; |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 476 | char *filename; |
wdenk | f2140d5 | 2004-07-01 16:30:44 +0000 | [diff] [blame] | 477 | int size; |
| 478 | struct part_info *part; |
| 479 | ulong offset = load_addr; |
wdenk | 3c71176 | 2004-06-09 13:37:52 +0000 | [diff] [blame] | 480 | |
| 481 | /* pre-set Boot file name */ |
| 482 | if ((filename = getenv("bootfile")) == NULL) { |
| 483 | filename = "uImage"; |
| 484 | } |
| 485 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 486 | if (argc == 2) { |
| 487 | filename = argv[1]; |
| 488 | } |
| 489 | if (argc == 3) { |
| 490 | offset = simple_strtoul(argv[1], NULL, 16); |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 491 | load_addr = offset; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 492 | filename = argv[2]; |
| 493 | } |
| 494 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 495 | /* make sure we are in sync with env variables */ |
| 496 | if (mtdparts_init() !=0) |
| 497 | return 1; |
| 498 | |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 499 | if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 500 | |
Michal Simek | 40277b3 | 2007-09-15 00:03:35 +0200 | [diff] [blame] | 501 | /* check partition type for cramfs */ |
| 502 | fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2"); |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 503 | printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset); |
| 504 | |
| 505 | if (cramfs_check(part)) { |
| 506 | size = cramfs_load ((char *) offset, part, filename); |
| 507 | } else { |
Michal Simek | 40277b3 | 2007-09-15 00:03:35 +0200 | [diff] [blame] | 508 | /* if this is not cramfs assume jffs2 */ |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 509 | size = jffs2_1pass_load((char *)offset, part, filename); |
| 510 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 511 | |
| 512 | if (size > 0) { |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 513 | printf("### %s load complete: %d bytes loaded to 0x%lx\n", |
| 514 | fsname, size, offset); |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 515 | env_set_hex("filesize", size); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 516 | } else { |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 517 | printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | return !(size > 0); |
| 521 | } |
Wolfgang Denk | 79117cd | 2005-08-16 09:32:45 +0200 | [diff] [blame] | 522 | return 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 525 | /** |
| 526 | * Routine implementing u-boot ls command which lists content of a given |
| 527 | * directory on a current partition. |
| 528 | * |
| 529 | * @param cmdtp command internal data |
| 530 | * @param flag command flag |
| 531 | * @param argc number of arguments supplied to the command |
| 532 | * @param argv arguments list |
| 533 | * @return 0 on success, 1 otherwise |
| 534 | */ |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 535 | int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 536 | { |
wdenk | 8d414a7 | 2005-06-10 10:00:19 +0000 | [diff] [blame] | 537 | char *filename = "/"; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 538 | int ret; |
| 539 | struct part_info *part; |
| 540 | |
| 541 | if (argc == 2) |
| 542 | filename = argv[1]; |
| 543 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 544 | /* make sure we are in sync with env variables */ |
| 545 | if (mtdparts_init() !=0) |
| 546 | return 1; |
| 547 | |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 548 | if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 549 | |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 550 | /* check partition type for cramfs */ |
| 551 | if (cramfs_check(part)) { |
| 552 | ret = cramfs_ls (part, filename); |
| 553 | } else { |
Michal Simek | 40277b3 | 2007-09-15 00:03:35 +0200 | [diff] [blame] | 554 | /* if this is not cramfs assume jffs2 */ |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 555 | ret = jffs2_1pass_ls(part, filename); |
| 556 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 557 | |
Wolfgang Denk | 79117cd | 2005-08-16 09:32:45 +0200 | [diff] [blame] | 558 | return ret ? 0 : 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 559 | } |
Wolfgang Denk | 79117cd | 2005-08-16 09:32:45 +0200 | [diff] [blame] | 560 | return 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 563 | /** |
| 564 | * Routine implementing u-boot fsinfo command. This routine prints out |
| 565 | * miscellaneous filesystem informations/statistics. |
| 566 | * |
| 567 | * @param cmdtp command internal data |
| 568 | * @param flag command flag |
| 569 | * @param argc number of arguments supplied to the command |
| 570 | * @param argv arguments list |
| 571 | * @return 0 on success, 1 otherwise |
| 572 | */ |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 573 | int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 574 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 575 | struct part_info *part; |
Michal Simek | 40277b3 | 2007-09-15 00:03:35 +0200 | [diff] [blame] | 576 | char *fsname; |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 577 | int ret; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 578 | |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 579 | /* make sure we are in sync with env variables */ |
| 580 | if (mtdparts_init() !=0) |
| 581 | return 1; |
Wolfgang Denk | eb95c85 | 2005-08-10 15:14:32 +0200 | [diff] [blame] | 582 | |
Stefan Roese | 4fa1f2d | 2009-05-16 12:04:22 +0200 | [diff] [blame] | 583 | if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){ |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 584 | |
| 585 | /* check partition type for cramfs */ |
Michal Simek | 40277b3 | 2007-09-15 00:03:35 +0200 | [diff] [blame] | 586 | fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2"); |
| 587 | printf("### filesystem type is %s\n", fsname); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 588 | |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 589 | if (cramfs_check(part)) { |
| 590 | ret = cramfs_info (part); |
| 591 | } else { |
Michal Simek | 40277b3 | 2007-09-15 00:03:35 +0200 | [diff] [blame] | 592 | /* if this is not cramfs assume jffs2 */ |
wdenk | ae65f50 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 593 | ret = jffs2_1pass_info(part); |
| 594 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 595 | |
Wolfgang Denk | 79117cd | 2005-08-16 09:32:45 +0200 | [diff] [blame] | 596 | return ret ? 0 : 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 597 | } |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 598 | return 1; |
| 599 | } |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 600 | |
| 601 | /***************************************************/ |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 602 | U_BOOT_CMD( |
| 603 | fsload, 3, 0, do_jffs2_fsload, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 604 | "load binary file from a filesystem image", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 605 | "[ off ] [ filename ]\n" |
| 606 | " - load binary file from flash bank\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 607 | " with offset 'off'" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 608 | ); |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 609 | U_BOOT_CMD( |
Siva Durga Prasad Paladugu | b9a9afd | 2017-05-30 14:18:55 +0200 | [diff] [blame] | 610 | fsls, 2, 1, do_jffs2_ls, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 611 | "list files in a directory (default /)", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 612 | "[ directory ]" |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 613 | ); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 614 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 615 | U_BOOT_CMD( |
| 616 | fsinfo, 1, 1, do_jffs2_fsinfo, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 617 | "print information about filesystems", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 618 | "" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 619 | ); |
Wolfgang Denk | 47f5779 | 2005-08-08 01:03:24 +0200 | [diff] [blame] | 620 | /***************************************************/ |