blob: 63bd55263a29611037bf75395bc5622d0a2e2d9b [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>
Simon Glassc06c1be2020-05-10 11:40:08 -060080#include <linux/bug.h>
Wolfgang Denk47f57792005-08-08 01:03:24 +020081#include <linux/list.h>
82#include <linux/ctype.h>
Wolfgang Denk47f57792005-08-08 01:03:24 +020083#include <cramfs/cramfs_fs.h>
84
Jon Loeligerd76b5c12007-07-08 18:02:23 -050085#if defined(CONFIG_CMD_NAND)
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090086#include <linux/mtd/rawnand.h>
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020087#include <nand.h>
Jon Loeligerd76b5c12007-07-08 18:02:23 -050088#endif
Kyungmin Park25383742008-08-27 14:45:20 +090089
90#if defined(CONFIG_CMD_ONENAND)
91#include <linux/mtd/mtd.h>
92#include <linux/mtd/onenand.h>
93#include <onenand_uboot.h>
94#endif
95
Wolfgang Denk47f57792005-08-08 01:03:24 +020096/* enable/disable debugging messages */
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +010097#define DEBUG_JFFS
98#undef DEBUG_JFFS
Wolfgang Denk47f57792005-08-08 01:03:24 +020099
Michal Simek6ed8c712007-08-06 23:31:49 +0200100#ifdef DEBUG_JFFS
Wolfgang Denk47f57792005-08-08 01:03:24 +0200101# define DEBUGF(fmt, args...) printf(fmt ,##args)
102#else
103# define DEBUGF(fmt, args...)
104#endif
105
106/* special size referring to all the remaining space in a partition */
107#define SIZE_REMAINING 0xFFFFFFFF
108
109/* special offset value, it is used when not provided by user
110 *
111 * this value is used temporarily during parsing, later such offests
112 * are recalculated */
113#define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
114
115/* minimum partition size */
116#define MIN_PART_SIZE 4096
117
118/* this flag needs to be set in part_info struct mask_flags
119 * field for read-only partitions */
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100120#define MTD_WRITEABLE_CMD 1
Wolfgang Denk47f57792005-08-08 01:03:24 +0200121
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100122/* current active device and partition number */
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200123#ifdef CONFIG_CMD_MTDPARTS
124/* Use the ones declared in cmd_mtdparts.c */
125extern struct mtd_device *current_mtd_dev;
126extern u8 current_mtd_partnum;
127#else
128/* Use local ones */
129struct mtd_device *current_mtd_dev = NULL;
130u8 current_mtd_partnum = 0;
131#endif
Wolfgang Denk47f57792005-08-08 01:03:24 +0200132
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100133#if defined(CONFIG_CMD_CRAMFS)
134extern int cramfs_check (struct part_info *info);
135extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
136extern int cramfs_ls (struct part_info *info, char *filename);
137extern int cramfs_info (struct part_info *info);
138#else
139/* defining empty macros for function names is ugly but avoids ifdef clutter
140 * all over the code */
141#define cramfs_check(x) (0)
142#define cramfs_load(x,y,z) (-1)
143#define cramfs_ls(x,y) (0)
144#define cramfs_info(x) (0)
145#endif
Wolfgang Denk47f57792005-08-08 01:03:24 +0200146
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100147#ifndef CONFIG_CMD_MTDPARTS
Wolfgang Denk47f57792005-08-08 01:03:24 +0200148/**
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100149 * Check device number to be within valid range for given device type.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200150 *
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100151 * @param dev device to validate
152 * @return 0 if device is valid, 1 otherwise
wdenkc6097192002-11-03 00:24:07 +0000153 */
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100154static int mtd_device_validate(u8 type, u8 num, u32 *size)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200155{
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100156 if (type == MTD_DEV_TYPE_NOR) {
157#if defined(CONFIG_CMD_FLASH)
158 if (num < CONFIG_SYS_MAX_FLASH_BANKS) {
159 extern flash_info_t flash_info[];
160 *size = flash_info[num].size;
wdenkad276f22004-01-04 16:28:35 +0000161
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100162 return 0;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200163 }
wdenkc6097192002-11-03 00:24:07 +0000164
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100165 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
166 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_FLASH_BANKS - 1);
167#else
168 printf("support for FLASH devices not present\n");
169#endif
170 } else if (type == MTD_DEV_TYPE_NAND) {
171#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
Grygorii Strashko6d2a26682017-06-26 19:12:55 -0500172 struct mtd_info *mtd = get_nand_dev_by_index(num);
173 if (mtd) {
174 *size = mtd->size;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100175 return 0;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200176 }
wdenkda04a8b2004-08-02 23:22:59 +0000177
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100178 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
179 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1);
180#else
181 printf("support for NAND devices not present\n");
182#endif
183 } else if (type == MTD_DEV_TYPE_ONENAND) {
184#if defined(CONFIG_CMD_ONENAND)
185 *size = onenand_mtd.size;
186 return 0;
187#else
188 printf("support for OneNAND devices not present\n");
189#endif
190 } else
191 printf("Unknown defice type %d\n", type);
wdenkc6097192002-11-03 00:24:07 +0000192
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100193 return 1;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200194}
wdenk8886a662004-04-18 19:43:36 +0000195
Wolfgang Denk47f57792005-08-08 01:03:24 +0200196/**
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100197 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
198 * return device type and number.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200199 *
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100200 * @param id string describing device id
201 * @param ret_id output pointer to next char after parse completes (output)
202 * @param dev_type parsed device type (output)
203 * @param dev_num parsed device number (output)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200204 * @return 0 on success, 1 otherwise
205 */
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100206static int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
wdenkc6097192002-11-03 00:24:07 +0000207{
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100208 const char *p = id;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200209
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100210 *dev_type = 0;
211 if (strncmp(p, "nand", 4) == 0) {
212 *dev_type = MTD_DEV_TYPE_NAND;
213 p += 4;
214 } else if (strncmp(p, "nor", 3) == 0) {
215 *dev_type = MTD_DEV_TYPE_NOR;
216 p += 3;
217 } else if (strncmp(p, "onenand", 7) == 0) {
218 *dev_type = MTD_DEV_TYPE_ONENAND;
219 p += 7;
220 } else {
221 printf("incorrect device type in %s\n", id);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200222 return 1;
223 }
wdenkc6097192002-11-03 00:24:07 +0000224
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100225 if (!isdigit(*p)) {
226 printf("incorrect device number in %s\n", id);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200227 return 1;
228 }
229
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100230 *dev_num = simple_strtoul(p, (char **)&p, 0);
231 if (ret_id)
232 *ret_id = p;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200233 return 0;
234}
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100235
Wolfgang Denk47f57792005-08-08 01:03:24 +0200236/*
237 * 'Static' version of command line mtdparts_init() routine. Single partition on
238 * a single device configuration.
239 */
240
241/**
Tomasz Figa63440e02008-12-30 18:35:57 +0100242 * Calculate sector size.
243 *
244 * @return sector size
245 */
246static inline u32 get_part_sector_size_nand(struct mtdids *id)
247{
248#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
Scott Wood08364d92016-05-30 13:57:54 -0500249 struct mtd_info *mtd;
Tomasz Figa63440e02008-12-30 18:35:57 +0100250
Grygorii Strashko6d2a26682017-06-26 19:12:55 -0500251 mtd = get_nand_dev_by_index(id->num);
Tomasz Figa63440e02008-12-30 18:35:57 +0100252
Scott Wood08364d92016-05-30 13:57:54 -0500253 return mtd->erasesize;
Tomasz Figa63440e02008-12-30 18:35:57 +0100254#else
255 BUG();
256 return 0;
257#endif
258}
259
260static inline u32 get_part_sector_size_nor(struct mtdids *id, struct part_info *part)
261{
262#if defined(CONFIG_CMD_FLASH)
263 extern flash_info_t flash_info[];
264
265 u32 end_phys, start_phys, sector_size = 0, size = 0;
266 int i;
267 flash_info_t *flash;
268
269 flash = &flash_info[id->num];
270
271 start_phys = flash->start[0] + part->offset;
Peter Tyser5e26c2a2010-12-30 15:47:56 -0600272 end_phys = start_phys + part->size - 1;
Tomasz Figa63440e02008-12-30 18:35:57 +0100273
274 for (i = 0; i < flash->sector_count; i++) {
275 if (flash->start[i] >= end_phys)
276 break;
277
278 if (flash->start[i] >= start_phys) {
279 if (i == flash->sector_count - 1) {
280 size = flash->start[0] + flash->size - flash->start[i];
281 } else {
282 size = flash->start[i+1] - flash->start[i];
283 }
284
285 if (sector_size < size)
286 sector_size = size;
287 }
288 }
289
290 return sector_size;
291#else
292 BUG();
293 return 0;
294#endif
295}
296
297static inline u32 get_part_sector_size_onenand(void)
298{
299#if defined(CONFIG_CMD_ONENAND)
300 struct mtd_info *mtd;
301
302 mtd = &onenand_mtd;
303
304 return mtd->erasesize;
305#else
306 BUG();
307 return 0;
308#endif
309}
310
311static inline u32 get_part_sector_size(struct mtdids *id, struct part_info *part)
312{
313 if (id->type == MTD_DEV_TYPE_NAND)
314 return get_part_sector_size_nand(id);
315 else if (id->type == MTD_DEV_TYPE_NOR)
316 return get_part_sector_size_nor(id, part);
317 else if (id->type == MTD_DEV_TYPE_ONENAND)
318 return get_part_sector_size_onenand();
319 else
320 DEBUGF("Error: Unknown device type.\n");
321
322 return 0;
323}
324
325/**
Wolfgang Denk47f57792005-08-08 01:03:24 +0200326 * Parse and initialize global mtdids mapping and create global
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200327 * device/partition list.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200328 *
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200329 * 'Static' version of command line mtdparts_init() routine. Single partition on
330 * a single device configuration.
331 *
Wolfgang Denk47f57792005-08-08 01:03:24 +0200332 * @return 0 on success, 1 otherwise
333 */
334int mtdparts_init(void)
335{
336 static int initialized = 0;
337 u32 size;
338 char *dev_name;
339
340 DEBUGF("\n---mtdparts_init---\n");
341 if (!initialized) {
Wolfgang Denkc0d487b2005-08-09 21:41:20 +0200342 struct mtdids *id;
343 struct part_info *part;
344
Wolfgang Denk47f57792005-08-08 01:03:24 +0200345 initialized = 1;
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200346 current_mtd_dev = (struct mtd_device *)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200347 malloc(sizeof(struct mtd_device) +
348 sizeof(struct part_info) +
349 sizeof(struct mtdids));
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200350 if (!current_mtd_dev) {
Wolfgang Denk47f57792005-08-08 01:03:24 +0200351 printf("out of memory\n");
352 return 1;
353 }
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200354 memset(current_mtd_dev, 0, sizeof(struct mtd_device) +
355 sizeof(struct part_info) + sizeof(struct mtdids));
Wolfgang Denk47f57792005-08-08 01:03:24 +0200356
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200357 id = (struct mtdids *)(current_mtd_dev + 1);
Wolfgang Denkc0d487b2005-08-09 21:41:20 +0200358 part = (struct part_info *)(id + 1);
wdenkc6097192002-11-03 00:24:07 +0000359
Wolfgang Denk47f57792005-08-08 01:03:24 +0200360 /* id */
361 id->mtd_id = "single part";
wdenkc6097192002-11-03 00:24:07 +0000362
Wolfgang Denk47f57792005-08-08 01:03:24 +0200363#if defined(CONFIG_JFFS2_DEV)
364 dev_name = CONFIG_JFFS2_DEV;
wdenkc6097192002-11-03 00:24:07 +0000365#else
Wolfgang Denk47f57792005-08-08 01:03:24 +0200366 dev_name = "nor0";
wdenkc6097192002-11-03 00:24:07 +0000367#endif
368
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100369 if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
370 (mtd_device_validate(id->type, id->num, &size) != 0)) {
Wolfgang Denk47f57792005-08-08 01:03:24 +0200371 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200372 free(current_mtd_dev);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200373 return 1;
374 }
375 id->size = size;
376 INIT_LIST_HEAD(&id->link);
377
378 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
379 id->type, id->num, id->size, id->mtd_id);
380
381 /* partition */
382 part->name = "static";
383 part->auto_name = 0;
384
385#if defined(CONFIG_JFFS2_PART_SIZE)
386 part->size = CONFIG_JFFS2_PART_SIZE;
387#else
388 part->size = SIZE_REMAINING;
389#endif
wdenkc6097192002-11-03 00:24:07 +0000390
Wolfgang Denk47f57792005-08-08 01:03:24 +0200391#if defined(CONFIG_JFFS2_PART_OFFSET)
392 part->offset = CONFIG_JFFS2_PART_OFFSET;
393#else
394 part->offset = 0x00000000;
wdenkc6097192002-11-03 00:24:07 +0000395#endif
396
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200397 part->dev = current_mtd_dev;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200398 INIT_LIST_HEAD(&part->link);
399
400 /* recalculate size if needed */
401 if (part->size == SIZE_REMAINING)
402 part->size = id->size - part->offset;
wdenkc6097192002-11-03 00:24:07 +0000403
Mike Frysingerb5125d22010-01-08 08:03:06 -0500404 part->sector_size = get_part_sector_size(id, part);
405
Wolfgang Denk47f57792005-08-08 01:03:24 +0200406 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
407 part->name, part->size, part->offset);
408
409 /* device */
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200410 current_mtd_dev->id = id;
411 INIT_LIST_HEAD(&current_mtd_dev->link);
412 current_mtd_dev->num_parts = 1;
413 INIT_LIST_HEAD(&current_mtd_dev->parts);
414 list_add(&part->link, &current_mtd_dev->parts);
wdenk8886a662004-04-18 19:43:36 +0000415 }
Wolfgang Denk47f57792005-08-08 01:03:24 +0200416
wdenk8886a662004-04-18 19:43:36 +0000417 return 0;
418}
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100419#endif /* #ifndef CONFIG_CMD_MTDPARTS */
wdenk8886a662004-04-18 19:43:36 +0000420
Wolfgang Denk47f57792005-08-08 01:03:24 +0200421/**
422 * Return pointer to the partition of a requested number from a requested
423 * device.
424 *
425 * @param dev device that is to be searched for a partition
426 * @param part_num requested partition number
427 * @return pointer to the part_info, NULL otherwise
428 */
429static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
wdenk8886a662004-04-18 19:43:36 +0000430{
Wolfgang Denk47f57792005-08-08 01:03:24 +0200431 struct list_head *entry;
432 struct part_info *part;
433 int num;
wdenk8886a662004-04-18 19:43:36 +0000434
Wolfgang Denk47f57792005-08-08 01:03:24 +0200435 if (!dev)
436 return NULL;
wdenk8886a662004-04-18 19:43:36 +0000437
Wolfgang Denk47f57792005-08-08 01:03:24 +0200438 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
439 part_num, MTD_DEV_TYPE(dev->id->type),
440 dev->id->num, dev->id->mtd_id);
wdenk8886a662004-04-18 19:43:36 +0000441
Wolfgang Denk47f57792005-08-08 01:03:24 +0200442 if (part_num >= dev->num_parts) {
443 printf("invalid partition number %d for device %s%d (%s)\n",
444 part_num, MTD_DEV_TYPE(dev->id->type),
445 dev->id->num, dev->id->mtd_id);
446 return NULL;
447 }
wdenk8886a662004-04-18 19:43:36 +0000448
Wolfgang Denk47f57792005-08-08 01:03:24 +0200449 /* locate partition number, return it */
450 num = 0;
451 list_for_each(entry, &dev->parts) {
452 part = list_entry(entry, struct part_info, link);
453
454 if (part_num == num++) {
455 return part;
456 }
wdenkc6097192002-11-03 00:24:07 +0000457 }
Wolfgang Denk47f57792005-08-08 01:03:24 +0200458
459 return NULL;
wdenkc6097192002-11-03 00:24:07 +0000460}
wdenk8886a662004-04-18 19:43:36 +0000461
Michal Simek6ed8c712007-08-06 23:31:49 +0200462/***************************************************/
Bin Meng75574052016-02-05 19:30:11 -0800463/* U-Boot commands */
Michal Simek6ed8c712007-08-06 23:31:49 +0200464/***************************************************/
wdenkae65f502004-01-03 21:24:46 +0000465
Wolfgang Denk47f57792005-08-08 01:03:24 +0200466/**
467 * Routine implementing fsload u-boot command. This routine tries to load
468 * a requested file from jffs2/cramfs filesystem on a current partition.
469 *
470 * @param cmdtp command internal data
471 * @param flag command flag
472 * @param argc number of arguments supplied to the command
473 * @param argv arguments list
474 * @return 0 on success, 1 otherwise
475 */
Simon Glassed38aef2020-05-10 11:40:03 -0600476int do_jffs2_fsload(struct cmd_tbl *cmdtp, int flag, int argc,
477 char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000478{
wdenkf2140d52004-07-01 16:30:44 +0000479 char *fsname;
wdenk3c711762004-06-09 13:37:52 +0000480 char *filename;
wdenkf2140d52004-07-01 16:30:44 +0000481 int size;
482 struct part_info *part;
Simon Glass892265d2019-12-28 10:45:02 -0700483 ulong offset = image_load_addr;
wdenk3c711762004-06-09 13:37:52 +0000484
485 /* pre-set Boot file name */
Simon Glass64b723f2017-08-03 12:22:12 -0600486 filename = env_get("bootfile");
487 if (!filename)
wdenk3c711762004-06-09 13:37:52 +0000488 filename = "uImage";
wdenk3c711762004-06-09 13:37:52 +0000489
wdenkc6097192002-11-03 00:24:07 +0000490 if (argc == 2) {
491 filename = argv[1];
492 }
493 if (argc == 3) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600494 offset = hextoul(argv[1], NULL);
Simon Glass892265d2019-12-28 10:45:02 -0700495 image_load_addr = offset;
wdenkc6097192002-11-03 00:24:07 +0000496 filename = argv[2];
497 }
498
Wolfgang Denk47f57792005-08-08 01:03:24 +0200499 /* make sure we are in sync with env variables */
500 if (mtdparts_init() !=0)
501 return 1;
502
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200503 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
wdenkc6097192002-11-03 00:24:07 +0000504
Michal Simek40277b32007-09-15 00:03:35 +0200505 /* check partition type for cramfs */
506 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
wdenkae65f502004-01-03 21:24:46 +0000507 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
508
509 if (cramfs_check(part)) {
510 size = cramfs_load ((char *) offset, part, filename);
511 } else {
Michal Simek40277b32007-09-15 00:03:35 +0200512 /* if this is not cramfs assume jffs2 */
wdenkae65f502004-01-03 21:24:46 +0000513 size = jffs2_1pass_load((char *)offset, part, filename);
514 }
wdenkc6097192002-11-03 00:24:07 +0000515
516 if (size > 0) {
wdenkae65f502004-01-03 21:24:46 +0000517 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
518 fsname, size, offset);
Simon Glass4d949a22017-08-03 12:22:10 -0600519 env_set_hex("filesize", size);
wdenkc6097192002-11-03 00:24:07 +0000520 } else {
wdenkae65f502004-01-03 21:24:46 +0000521 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
wdenkc6097192002-11-03 00:24:07 +0000522 }
523
524 return !(size > 0);
525 }
Wolfgang Denk79117cd2005-08-16 09:32:45 +0200526 return 1;
wdenkc6097192002-11-03 00:24:07 +0000527}
528
Wolfgang Denk47f57792005-08-08 01:03:24 +0200529/**
530 * Routine implementing u-boot ls command which lists content of a given
531 * directory on a current partition.
532 *
533 * @param cmdtp command internal data
534 * @param flag command flag
535 * @param argc number of arguments supplied to the command
536 * @param argv arguments list
537 * @return 0 on success, 1 otherwise
538 */
Simon Glassed38aef2020-05-10 11:40:03 -0600539int do_jffs2_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000540{
wdenk8d414a72005-06-10 10:00:19 +0000541 char *filename = "/";
wdenkc6097192002-11-03 00:24:07 +0000542 int ret;
543 struct part_info *part;
544
545 if (argc == 2)
546 filename = argv[1];
547
Wolfgang Denk47f57792005-08-08 01:03:24 +0200548 /* make sure we are in sync with env variables */
549 if (mtdparts_init() !=0)
550 return 1;
551
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200552 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
wdenkc6097192002-11-03 00:24:07 +0000553
wdenkae65f502004-01-03 21:24:46 +0000554 /* check partition type for cramfs */
555 if (cramfs_check(part)) {
556 ret = cramfs_ls (part, filename);
557 } else {
Michal Simek40277b32007-09-15 00:03:35 +0200558 /* if this is not cramfs assume jffs2 */
wdenkae65f502004-01-03 21:24:46 +0000559 ret = jffs2_1pass_ls(part, filename);
560 }
wdenkc6097192002-11-03 00:24:07 +0000561
Wolfgang Denk79117cd2005-08-16 09:32:45 +0200562 return ret ? 0 : 1;
wdenkc6097192002-11-03 00:24:07 +0000563 }
Wolfgang Denk79117cd2005-08-16 09:32:45 +0200564 return 1;
wdenkc6097192002-11-03 00:24:07 +0000565}
566
Wolfgang Denk47f57792005-08-08 01:03:24 +0200567/**
568 * Routine implementing u-boot fsinfo command. This routine prints out
569 * miscellaneous filesystem informations/statistics.
570 *
571 * @param cmdtp command internal data
572 * @param flag command flag
573 * @param argc number of arguments supplied to the command
574 * @param argv arguments list
575 * @return 0 on success, 1 otherwise
576 */
Simon Glassed38aef2020-05-10 11:40:03 -0600577int do_jffs2_fsinfo(struct cmd_tbl *cmdtp, int flag, int argc,
578 char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000579{
wdenkc6097192002-11-03 00:24:07 +0000580 struct part_info *part;
Michal Simek40277b32007-09-15 00:03:35 +0200581 char *fsname;
wdenkae65f502004-01-03 21:24:46 +0000582 int ret;
wdenkc6097192002-11-03 00:24:07 +0000583
Wolfgang Denk47f57792005-08-08 01:03:24 +0200584 /* make sure we are in sync with env variables */
585 if (mtdparts_init() !=0)
586 return 1;
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200587
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200588 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
wdenkae65f502004-01-03 21:24:46 +0000589
590 /* check partition type for cramfs */
Michal Simek40277b32007-09-15 00:03:35 +0200591 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
592 printf("### filesystem type is %s\n", fsname);
wdenkc6097192002-11-03 00:24:07 +0000593
wdenkae65f502004-01-03 21:24:46 +0000594 if (cramfs_check(part)) {
595 ret = cramfs_info (part);
596 } else {
Michal Simek40277b32007-09-15 00:03:35 +0200597 /* if this is not cramfs assume jffs2 */
wdenkae65f502004-01-03 21:24:46 +0000598 ret = jffs2_1pass_info(part);
599 }
wdenkc6097192002-11-03 00:24:07 +0000600
Wolfgang Denk79117cd2005-08-16 09:32:45 +0200601 return ret ? 0 : 1;
wdenkc6097192002-11-03 00:24:07 +0000602 }
Wolfgang Denk47f57792005-08-08 01:03:24 +0200603 return 1;
604}
Wolfgang Denk47f57792005-08-08 01:03:24 +0200605
606/***************************************************/
wdenkf287a242003-07-01 21:06:45 +0000607U_BOOT_CMD(
608 fsload, 3, 0, do_jffs2_fsload,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600609 "load binary file from a filesystem image",
wdenk57b2d802003-06-27 21:31:46 +0000610 "[ off ] [ filename ]\n"
611 " - load binary file from flash bank\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200612 " with offset 'off'"
wdenk57b2d802003-06-27 21:31:46 +0000613);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200614U_BOOT_CMD(
Siva Durga Prasad Paladugub9a9afd2017-05-30 14:18:55 +0200615 fsls, 2, 1, do_jffs2_ls,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600616 "list files in a directory (default /)",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200617 "[ directory ]"
Wolfgang Denk47f57792005-08-08 01:03:24 +0200618);
wdenk57b2d802003-06-27 21:31:46 +0000619
wdenkf287a242003-07-01 21:06:45 +0000620U_BOOT_CMD(
621 fsinfo, 1, 1, do_jffs2_fsinfo,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600622 "print information about filesystems",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200623 ""
wdenk57b2d802003-06-27 21:31:46 +0000624);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200625/***************************************************/