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