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