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