blob: 748a73e2af910b338d12ea2991a73bb329d22cad [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00002/*
3 * (C) Copyright 2002
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Wolfgang Denk47f57792005-08-08 01:03:24 +02005 *
wdenkae65f502004-01-03 21:24:46 +00006 * (C) Copyright 2002
7 * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
Wolfgang Denk47f57792005-08-08 01:03:24 +02008 *
wdenkae65f502004-01-03 21:24:46 +00009 * (C) Copyright 2003
10 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
wdenkc6097192002-11-03 00:24:07 +000011 *
Wolfgang Denk47f57792005-08-08 01:03:24 +020012 * (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
Wolfgang Denk47f57792005-08-08 01:03:24 +020021 */
22
23/*
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 Denkeb95c852005-08-10 15:14:32 +020031 *
Wolfgang Denk47f57792005-08-08 01:03:24 +020032 * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
33 *
34 * mtdids=<idmap>[,<idmap>,...]
35 *
36 * <idmap> := <dev-id>=<mtd-id>
Kyungmin Park25383742008-08-27 14:45:20 +090037 * <dev-id> := 'nand'|'nor'|'onenand'<dev-num>
Wolfgang Denk47f57792005-08-08 01:03:24 +020038 * <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 Simek40277b32007-09-15 00:03:35 +020071 * JFFS2/CRAMFS support
Wolfgang Denk47f57792005-08-08 01:03:24 +020072 */
73#include <common.h>
74#include <command.h>
Simon Glass313112a2019-08-01 09:46:46 -060075#include <env.h>
Simon Glass8e201882020-05-10 11:39:54 -060076#include <flash.h>
Simon Glass85f13782019-12-28 10:45:03 -070077#include <image.h>
Wolfgang Denk47f57792005-08-08 01:03:24 +020078#include <malloc.h>
79#include <jffs2/jffs2.h>
Wolfgang Denk47f57792005-08-08 01:03:24 +020080#include <linux/list.h>
81#include <linux/ctype.h>
Wolfgang Denk47f57792005-08-08 01:03:24 +020082#include <cramfs/cramfs_fs.h>
83
Jon Loeligerd76b5c12007-07-08 18:02:23 -050084#if defined(CONFIG_CMD_NAND)
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090085#include <linux/mtd/rawnand.h>
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020086#include <nand.h>
Jon Loeligerd76b5c12007-07-08 18:02:23 -050087#endif
Kyungmin Park25383742008-08-27 14:45:20 +090088
89#if defined(CONFIG_CMD_ONENAND)
90#include <linux/mtd/mtd.h>
91#include <linux/mtd/onenand.h>
92#include <onenand_uboot.h>
93#endif
94
Wolfgang Denk47f57792005-08-08 01:03:24 +020095/* enable/disable debugging messages */
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +010096#define DEBUG_JFFS
97#undef DEBUG_JFFS
Wolfgang Denk47f57792005-08-08 01:03:24 +020098
Michal Simek6ed8c712007-08-06 23:31:49 +020099#ifdef DEBUG_JFFS
Wolfgang Denk47f57792005-08-08 01:03:24 +0200100# define DEBUGF(fmt, args...) printf(fmt ,##args)
101#else
102# define DEBUGF(fmt, args...)
103#endif
104
105/* special size referring to all the remaining space in a partition */
106#define SIZE_REMAINING 0xFFFFFFFF
107
108/* special offset value, it is used when not provided by user
109 *
110 * this value is used temporarily during parsing, later such offests
111 * are recalculated */
112#define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
113
114/* minimum partition size */
115#define MIN_PART_SIZE 4096
116
117/* this flag needs to be set in part_info struct mask_flags
118 * field for read-only partitions */
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100119#define MTD_WRITEABLE_CMD 1
Wolfgang Denk47f57792005-08-08 01:03:24 +0200120
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100121/* current active device and partition number */
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200122#ifdef CONFIG_CMD_MTDPARTS
123/* Use the ones declared in cmd_mtdparts.c */
124extern struct mtd_device *current_mtd_dev;
125extern u8 current_mtd_partnum;
126#else
127/* Use local ones */
128struct mtd_device *current_mtd_dev = NULL;
129u8 current_mtd_partnum = 0;
130#endif
Wolfgang Denk47f57792005-08-08 01:03:24 +0200131
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100132#if defined(CONFIG_CMD_CRAMFS)
133extern int cramfs_check (struct part_info *info);
134extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
135extern int cramfs_ls (struct part_info *info, char *filename);
136extern int cramfs_info (struct part_info *info);
137#else
138/* defining empty macros for function names is ugly but avoids ifdef clutter
139 * all over the code */
140#define cramfs_check(x) (0)
141#define cramfs_load(x,y,z) (-1)
142#define cramfs_ls(x,y) (0)
143#define cramfs_info(x) (0)
144#endif
Wolfgang Denk47f57792005-08-08 01:03:24 +0200145
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100146#ifndef CONFIG_CMD_MTDPARTS
Wolfgang Denk47f57792005-08-08 01:03:24 +0200147/**
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100148 * Check device number to be within valid range for given device type.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200149 *
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100150 * @param dev device to validate
151 * @return 0 if device is valid, 1 otherwise
wdenkc6097192002-11-03 00:24:07 +0000152 */
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100153static int mtd_device_validate(u8 type, u8 num, u32 *size)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200154{
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100155 if (type == MTD_DEV_TYPE_NOR) {
156#if defined(CONFIG_CMD_FLASH)
157 if (num < CONFIG_SYS_MAX_FLASH_BANKS) {
158 extern flash_info_t flash_info[];
159 *size = flash_info[num].size;
wdenkad276f22004-01-04 16:28:35 +0000160
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100161 return 0;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200162 }
wdenkc6097192002-11-03 00:24:07 +0000163
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100164 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
165 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_FLASH_BANKS - 1);
166#else
167 printf("support for FLASH devices not present\n");
168#endif
169 } else if (type == MTD_DEV_TYPE_NAND) {
170#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
Grygorii Strashko6d2a26682017-06-26 19:12:55 -0500171 struct mtd_info *mtd = get_nand_dev_by_index(num);
172 if (mtd) {
173 *size = mtd->size;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100174 return 0;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200175 }
wdenkda04a8b2004-08-02 23:22:59 +0000176
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100177 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
178 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1);
179#else
180 printf("support for NAND devices not present\n");
181#endif
182 } else if (type == MTD_DEV_TYPE_ONENAND) {
183#if defined(CONFIG_CMD_ONENAND)
184 *size = onenand_mtd.size;
185 return 0;
186#else
187 printf("support for OneNAND devices not present\n");
188#endif
189 } else
190 printf("Unknown defice type %d\n", type);
wdenkc6097192002-11-03 00:24:07 +0000191
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100192 return 1;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200193}
wdenk8886a662004-04-18 19:43:36 +0000194
Wolfgang Denk47f57792005-08-08 01:03:24 +0200195/**
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100196 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
197 * return device type and number.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200198 *
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100199 * @param id string describing device id
200 * @param ret_id output pointer to next char after parse completes (output)
201 * @param dev_type parsed device type (output)
202 * @param dev_num parsed device number (output)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200203 * @return 0 on success, 1 otherwise
204 */
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100205static int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
wdenkc6097192002-11-03 00:24:07 +0000206{
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100207 const char *p = id;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200208
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100209 *dev_type = 0;
210 if (strncmp(p, "nand", 4) == 0) {
211 *dev_type = MTD_DEV_TYPE_NAND;
212 p += 4;
213 } else if (strncmp(p, "nor", 3) == 0) {
214 *dev_type = MTD_DEV_TYPE_NOR;
215 p += 3;
216 } else if (strncmp(p, "onenand", 7) == 0) {
217 *dev_type = MTD_DEV_TYPE_ONENAND;
218 p += 7;
219 } else {
220 printf("incorrect device type in %s\n", id);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200221 return 1;
222 }
wdenkc6097192002-11-03 00:24:07 +0000223
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100224 if (!isdigit(*p)) {
225 printf("incorrect device number in %s\n", id);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200226 return 1;
227 }
228
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100229 *dev_num = simple_strtoul(p, (char **)&p, 0);
230 if (ret_id)
231 *ret_id = p;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200232 return 0;
233}
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100234
Wolfgang Denk47f57792005-08-08 01:03:24 +0200235/*
236 * 'Static' version of command line mtdparts_init() routine. Single partition on
237 * a single device configuration.
238 */
239
240/**
Tomasz Figa63440e02008-12-30 18:35:57 +0100241 * Calculate sector size.
242 *
243 * @return sector size
244 */
245static inline u32 get_part_sector_size_nand(struct mtdids *id)
246{
247#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
Scott Wood08364d92016-05-30 13:57:54 -0500248 struct mtd_info *mtd;
Tomasz Figa63440e02008-12-30 18:35:57 +0100249
Grygorii Strashko6d2a26682017-06-26 19:12:55 -0500250 mtd = get_nand_dev_by_index(id->num);
Tomasz Figa63440e02008-12-30 18:35:57 +0100251
Scott Wood08364d92016-05-30 13:57:54 -0500252 return mtd->erasesize;
Tomasz Figa63440e02008-12-30 18:35:57 +0100253#else
254 BUG();
255 return 0;
256#endif
257}
258
259static inline u32 get_part_sector_size_nor(struct mtdids *id, struct part_info *part)
260{
261#if defined(CONFIG_CMD_FLASH)
262 extern flash_info_t flash_info[];
263
264 u32 end_phys, start_phys, sector_size = 0, size = 0;
265 int i;
266 flash_info_t *flash;
267
268 flash = &flash_info[id->num];
269
270 start_phys = flash->start[0] + part->offset;
Peter Tyser5e26c2a2010-12-30 15:47:56 -0600271 end_phys = start_phys + part->size - 1;
Tomasz Figa63440e02008-12-30 18:35:57 +0100272
273 for (i = 0; i < flash->sector_count; i++) {
274 if (flash->start[i] >= end_phys)
275 break;
276
277 if (flash->start[i] >= start_phys) {
278 if (i == flash->sector_count - 1) {
279 size = flash->start[0] + flash->size - flash->start[i];
280 } else {
281 size = flash->start[i+1] - flash->start[i];
282 }
283
284 if (sector_size < size)
285 sector_size = size;
286 }
287 }
288
289 return sector_size;
290#else
291 BUG();
292 return 0;
293#endif
294}
295
296static inline u32 get_part_sector_size_onenand(void)
297{
298#if defined(CONFIG_CMD_ONENAND)
299 struct mtd_info *mtd;
300
301 mtd = &onenand_mtd;
302
303 return mtd->erasesize;
304#else
305 BUG();
306 return 0;
307#endif
308}
309
310static inline u32 get_part_sector_size(struct mtdids *id, struct part_info *part)
311{
312 if (id->type == MTD_DEV_TYPE_NAND)
313 return get_part_sector_size_nand(id);
314 else if (id->type == MTD_DEV_TYPE_NOR)
315 return get_part_sector_size_nor(id, part);
316 else if (id->type == MTD_DEV_TYPE_ONENAND)
317 return get_part_sector_size_onenand();
318 else
319 DEBUGF("Error: Unknown device type.\n");
320
321 return 0;
322}
323
324/**
Wolfgang Denk47f57792005-08-08 01:03:24 +0200325 * Parse and initialize global mtdids mapping and create global
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200326 * device/partition list.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200327 *
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200328 * 'Static' version of command line mtdparts_init() routine. Single partition on
329 * a single device configuration.
330 *
Wolfgang Denk47f57792005-08-08 01:03:24 +0200331 * @return 0 on success, 1 otherwise
332 */
333int mtdparts_init(void)
334{
335 static int initialized = 0;
336 u32 size;
337 char *dev_name;
338
339 DEBUGF("\n---mtdparts_init---\n");
340 if (!initialized) {
Wolfgang Denkc0d487b2005-08-09 21:41:20 +0200341 struct mtdids *id;
342 struct part_info *part;
343
Wolfgang Denk47f57792005-08-08 01:03:24 +0200344 initialized = 1;
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200345 current_mtd_dev = (struct mtd_device *)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200346 malloc(sizeof(struct mtd_device) +
347 sizeof(struct part_info) +
348 sizeof(struct mtdids));
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200349 if (!current_mtd_dev) {
Wolfgang Denk47f57792005-08-08 01:03:24 +0200350 printf("out of memory\n");
351 return 1;
352 }
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200353 memset(current_mtd_dev, 0, sizeof(struct mtd_device) +
354 sizeof(struct part_info) + sizeof(struct mtdids));
Wolfgang Denk47f57792005-08-08 01:03:24 +0200355
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200356 id = (struct mtdids *)(current_mtd_dev + 1);
Wolfgang Denkc0d487b2005-08-09 21:41:20 +0200357 part = (struct part_info *)(id + 1);
wdenkc6097192002-11-03 00:24:07 +0000358
Wolfgang Denk47f57792005-08-08 01:03:24 +0200359 /* id */
360 id->mtd_id = "single part";
wdenkc6097192002-11-03 00:24:07 +0000361
Wolfgang Denk47f57792005-08-08 01:03:24 +0200362#if defined(CONFIG_JFFS2_DEV)
363 dev_name = CONFIG_JFFS2_DEV;
wdenkc6097192002-11-03 00:24:07 +0000364#else
Wolfgang Denk47f57792005-08-08 01:03:24 +0200365 dev_name = "nor0";
wdenkc6097192002-11-03 00:24:07 +0000366#endif
367
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100368 if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
369 (mtd_device_validate(id->type, id->num, &size) != 0)) {
Wolfgang Denk47f57792005-08-08 01:03:24 +0200370 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200371 free(current_mtd_dev);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200372 return 1;
373 }
374 id->size = size;
375 INIT_LIST_HEAD(&id->link);
376
377 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
378 id->type, id->num, id->size, id->mtd_id);
379
380 /* partition */
381 part->name = "static";
382 part->auto_name = 0;
383
384#if defined(CONFIG_JFFS2_PART_SIZE)
385 part->size = CONFIG_JFFS2_PART_SIZE;
386#else
387 part->size = SIZE_REMAINING;
388#endif
wdenkc6097192002-11-03 00:24:07 +0000389
Wolfgang Denk47f57792005-08-08 01:03:24 +0200390#if defined(CONFIG_JFFS2_PART_OFFSET)
391 part->offset = CONFIG_JFFS2_PART_OFFSET;
392#else
393 part->offset = 0x00000000;
wdenkc6097192002-11-03 00:24:07 +0000394#endif
395
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200396 part->dev = current_mtd_dev;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200397 INIT_LIST_HEAD(&part->link);
398
399 /* recalculate size if needed */
400 if (part->size == SIZE_REMAINING)
401 part->size = id->size - part->offset;
wdenkc6097192002-11-03 00:24:07 +0000402
Mike Frysingerb5125d22010-01-08 08:03:06 -0500403 part->sector_size = get_part_sector_size(id, part);
404
Wolfgang Denk47f57792005-08-08 01:03:24 +0200405 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
406 part->name, part->size, part->offset);
407
408 /* device */
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200409 current_mtd_dev->id = id;
410 INIT_LIST_HEAD(&current_mtd_dev->link);
411 current_mtd_dev->num_parts = 1;
412 INIT_LIST_HEAD(&current_mtd_dev->parts);
413 list_add(&part->link, &current_mtd_dev->parts);
wdenk8886a662004-04-18 19:43:36 +0000414 }
Wolfgang Denk47f57792005-08-08 01:03:24 +0200415
wdenk8886a662004-04-18 19:43:36 +0000416 return 0;
417}
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100418#endif /* #ifndef CONFIG_CMD_MTDPARTS */
wdenk8886a662004-04-18 19:43:36 +0000419
Wolfgang Denk47f57792005-08-08 01:03:24 +0200420/**
421 * Return pointer to the partition of a requested number from a requested
422 * device.
423 *
424 * @param dev device that is to be searched for a partition
425 * @param part_num requested partition number
426 * @return pointer to the part_info, NULL otherwise
427 */
428static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
wdenk8886a662004-04-18 19:43:36 +0000429{
Wolfgang Denk47f57792005-08-08 01:03:24 +0200430 struct list_head *entry;
431 struct part_info *part;
432 int num;
wdenk8886a662004-04-18 19:43:36 +0000433
Wolfgang Denk47f57792005-08-08 01:03:24 +0200434 if (!dev)
435 return NULL;
wdenk8886a662004-04-18 19:43:36 +0000436
Wolfgang Denk47f57792005-08-08 01:03:24 +0200437 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
438 part_num, MTD_DEV_TYPE(dev->id->type),
439 dev->id->num, dev->id->mtd_id);
wdenk8886a662004-04-18 19:43:36 +0000440
Wolfgang Denk47f57792005-08-08 01:03:24 +0200441 if (part_num >= dev->num_parts) {
442 printf("invalid partition number %d for device %s%d (%s)\n",
443 part_num, MTD_DEV_TYPE(dev->id->type),
444 dev->id->num, dev->id->mtd_id);
445 return NULL;
446 }
wdenk8886a662004-04-18 19:43:36 +0000447
Wolfgang Denk47f57792005-08-08 01:03:24 +0200448 /* locate partition number, return it */
449 num = 0;
450 list_for_each(entry, &dev->parts) {
451 part = list_entry(entry, struct part_info, link);
452
453 if (part_num == num++) {
454 return part;
455 }
wdenkc6097192002-11-03 00:24:07 +0000456 }
Wolfgang Denk47f57792005-08-08 01:03:24 +0200457
458 return NULL;
wdenkc6097192002-11-03 00:24:07 +0000459}
wdenk8886a662004-04-18 19:43:36 +0000460
Michal Simek6ed8c712007-08-06 23:31:49 +0200461/***************************************************/
Bin Meng75574052016-02-05 19:30:11 -0800462/* U-Boot commands */
Michal Simek6ed8c712007-08-06 23:31:49 +0200463/***************************************************/
wdenkae65f502004-01-03 21:24:46 +0000464
Wolfgang Denk47f57792005-08-08 01:03:24 +0200465/**
466 * Routine implementing fsload u-boot command. This routine tries to load
467 * a requested file from jffs2/cramfs filesystem on a current partition.
468 *
469 * @param cmdtp command internal data
470 * @param flag command flag
471 * @param argc number of arguments supplied to the command
472 * @param argv arguments list
473 * @return 0 on success, 1 otherwise
474 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200475int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +0000476{
wdenkf2140d52004-07-01 16:30:44 +0000477 char *fsname;
wdenk3c711762004-06-09 13:37:52 +0000478 char *filename;
wdenkf2140d52004-07-01 16:30:44 +0000479 int size;
480 struct part_info *part;
Simon Glass892265d2019-12-28 10:45:02 -0700481 ulong offset = image_load_addr;
wdenk3c711762004-06-09 13:37:52 +0000482
483 /* pre-set Boot file name */
Simon Glass64b723f2017-08-03 12:22:12 -0600484 filename = env_get("bootfile");
485 if (!filename)
wdenk3c711762004-06-09 13:37:52 +0000486 filename = "uImage";
wdenk3c711762004-06-09 13:37:52 +0000487
wdenkc6097192002-11-03 00:24:07 +0000488 if (argc == 2) {
489 filename = argv[1];
490 }
491 if (argc == 3) {
492 offset = simple_strtoul(argv[1], NULL, 16);
Simon Glass892265d2019-12-28 10:45:02 -0700493 image_load_addr = offset;
wdenkc6097192002-11-03 00:24:07 +0000494 filename = argv[2];
495 }
496
Wolfgang Denk47f57792005-08-08 01:03:24 +0200497 /* make sure we are in sync with env variables */
498 if (mtdparts_init() !=0)
499 return 1;
500
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200501 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
wdenkc6097192002-11-03 00:24:07 +0000502
Michal Simek40277b32007-09-15 00:03:35 +0200503 /* check partition type for cramfs */
504 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
wdenkae65f502004-01-03 21:24:46 +0000505 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
506
507 if (cramfs_check(part)) {
508 size = cramfs_load ((char *) offset, part, filename);
509 } else {
Michal Simek40277b32007-09-15 00:03:35 +0200510 /* if this is not cramfs assume jffs2 */
wdenkae65f502004-01-03 21:24:46 +0000511 size = jffs2_1pass_load((char *)offset, part, filename);
512 }
wdenkc6097192002-11-03 00:24:07 +0000513
514 if (size > 0) {
wdenkae65f502004-01-03 21:24:46 +0000515 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
516 fsname, size, offset);
Simon Glass4d949a22017-08-03 12:22:10 -0600517 env_set_hex("filesize", size);
wdenkc6097192002-11-03 00:24:07 +0000518 } else {
wdenkae65f502004-01-03 21:24:46 +0000519 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
wdenkc6097192002-11-03 00:24:07 +0000520 }
521
522 return !(size > 0);
523 }
Wolfgang Denk79117cd2005-08-16 09:32:45 +0200524 return 1;
wdenkc6097192002-11-03 00:24:07 +0000525}
526
Wolfgang Denk47f57792005-08-08 01:03:24 +0200527/**
528 * Routine implementing u-boot ls command which lists content of a given
529 * directory on a current partition.
530 *
531 * @param cmdtp command internal data
532 * @param flag command flag
533 * @param argc number of arguments supplied to the command
534 * @param argv arguments list
535 * @return 0 on success, 1 otherwise
536 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200537int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +0000538{
wdenk8d414a72005-06-10 10:00:19 +0000539 char *filename = "/";
wdenkc6097192002-11-03 00:24:07 +0000540 int ret;
541 struct part_info *part;
542
543 if (argc == 2)
544 filename = argv[1];
545
Wolfgang Denk47f57792005-08-08 01:03:24 +0200546 /* make sure we are in sync with env variables */
547 if (mtdparts_init() !=0)
548 return 1;
549
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200550 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
wdenkc6097192002-11-03 00:24:07 +0000551
wdenkae65f502004-01-03 21:24:46 +0000552 /* check partition type for cramfs */
553 if (cramfs_check(part)) {
554 ret = cramfs_ls (part, filename);
555 } else {
Michal Simek40277b32007-09-15 00:03:35 +0200556 /* if this is not cramfs assume jffs2 */
wdenkae65f502004-01-03 21:24:46 +0000557 ret = jffs2_1pass_ls(part, filename);
558 }
wdenkc6097192002-11-03 00:24:07 +0000559
Wolfgang Denk79117cd2005-08-16 09:32:45 +0200560 return ret ? 0 : 1;
wdenkc6097192002-11-03 00:24:07 +0000561 }
Wolfgang Denk79117cd2005-08-16 09:32:45 +0200562 return 1;
wdenkc6097192002-11-03 00:24:07 +0000563}
564
Wolfgang Denk47f57792005-08-08 01:03:24 +0200565/**
566 * Routine implementing u-boot fsinfo command. This routine prints out
567 * miscellaneous filesystem informations/statistics.
568 *
569 * @param cmdtp command internal data
570 * @param flag command flag
571 * @param argc number of arguments supplied to the command
572 * @param argv arguments list
573 * @return 0 on success, 1 otherwise
574 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200575int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +0000576{
wdenkc6097192002-11-03 00:24:07 +0000577 struct part_info *part;
Michal Simek40277b32007-09-15 00:03:35 +0200578 char *fsname;
wdenkae65f502004-01-03 21:24:46 +0000579 int ret;
wdenkc6097192002-11-03 00:24:07 +0000580
Wolfgang Denk47f57792005-08-08 01:03:24 +0200581 /* make sure we are in sync with env variables */
582 if (mtdparts_init() !=0)
583 return 1;
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200584
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200585 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
wdenkae65f502004-01-03 21:24:46 +0000586
587 /* check partition type for cramfs */
Michal Simek40277b32007-09-15 00:03:35 +0200588 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
589 printf("### filesystem type is %s\n", fsname);
wdenkc6097192002-11-03 00:24:07 +0000590
wdenkae65f502004-01-03 21:24:46 +0000591 if (cramfs_check(part)) {
592 ret = cramfs_info (part);
593 } else {
Michal Simek40277b32007-09-15 00:03:35 +0200594 /* if this is not cramfs assume jffs2 */
wdenkae65f502004-01-03 21:24:46 +0000595 ret = jffs2_1pass_info(part);
596 }
wdenkc6097192002-11-03 00:24:07 +0000597
Wolfgang Denk79117cd2005-08-16 09:32:45 +0200598 return ret ? 0 : 1;
wdenkc6097192002-11-03 00:24:07 +0000599 }
Wolfgang Denk47f57792005-08-08 01:03:24 +0200600 return 1;
601}
Wolfgang Denk47f57792005-08-08 01:03:24 +0200602
603/***************************************************/
wdenkf287a242003-07-01 21:06:45 +0000604U_BOOT_CMD(
605 fsload, 3, 0, do_jffs2_fsload,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600606 "load binary file from a filesystem image",
wdenk57b2d802003-06-27 21:31:46 +0000607 "[ off ] [ filename ]\n"
608 " - load binary file from flash bank\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200609 " with offset 'off'"
wdenk57b2d802003-06-27 21:31:46 +0000610);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200611U_BOOT_CMD(
Siva Durga Prasad Paladugub9a9afd2017-05-30 14:18:55 +0200612 fsls, 2, 1, do_jffs2_ls,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600613 "list files in a directory (default /)",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200614 "[ directory ]"
Wolfgang Denk47f57792005-08-08 01:03:24 +0200615);
wdenk57b2d802003-06-27 21:31:46 +0000616
wdenkf287a242003-07-01 21:06:45 +0000617U_BOOT_CMD(
618 fsinfo, 1, 1, do_jffs2_fsinfo,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600619 "print information about filesystems",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200620 ""
wdenk57b2d802003-06-27 21:31:46 +0000621);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200622/***************************************************/