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