blob: 64621f2546602af59bcf862d7628b04ae50f64f2 [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>
75#include <malloc.h>
76#include <jffs2/jffs2.h>
Wolfgang Denk47f57792005-08-08 01:03:24 +020077#include <linux/list.h>
78#include <linux/ctype.h>
Wolfgang Denk47f57792005-08-08 01:03:24 +020079#include <cramfs/cramfs_fs.h>
80
Jon Loeligerd76b5c12007-07-08 18:02:23 -050081#if defined(CONFIG_CMD_NAND)
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090082#include <linux/mtd/rawnand.h>
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020083#include <nand.h>
Jon Loeligerd76b5c12007-07-08 18:02:23 -050084#endif
Kyungmin Park25383742008-08-27 14:45:20 +090085
86#if defined(CONFIG_CMD_ONENAND)
87#include <linux/mtd/mtd.h>
88#include <linux/mtd/onenand.h>
89#include <onenand_uboot.h>
90#endif
91
Wolfgang Denk47f57792005-08-08 01:03:24 +020092/* enable/disable debugging messages */
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +010093#define DEBUG_JFFS
94#undef DEBUG_JFFS
Wolfgang Denk47f57792005-08-08 01:03:24 +020095
Michal Simek6ed8c712007-08-06 23:31:49 +020096#ifdef DEBUG_JFFS
Wolfgang Denk47f57792005-08-08 01:03:24 +020097# define DEBUGF(fmt, args...) printf(fmt ,##args)
98#else
99# define DEBUGF(fmt, args...)
100#endif
101
102/* special size referring to all the remaining space in a partition */
103#define SIZE_REMAINING 0xFFFFFFFF
104
105/* special offset value, it is used when not provided by user
106 *
107 * this value is used temporarily during parsing, later such offests
108 * are recalculated */
109#define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
110
111/* minimum partition size */
112#define MIN_PART_SIZE 4096
113
114/* this flag needs to be set in part_info struct mask_flags
115 * field for read-only partitions */
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100116#define MTD_WRITEABLE_CMD 1
Wolfgang Denk47f57792005-08-08 01:03:24 +0200117
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100118/* current active device and partition number */
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200119#ifdef CONFIG_CMD_MTDPARTS
120/* Use the ones declared in cmd_mtdparts.c */
121extern struct mtd_device *current_mtd_dev;
122extern u8 current_mtd_partnum;
123#else
124/* Use local ones */
125struct mtd_device *current_mtd_dev = NULL;
126u8 current_mtd_partnum = 0;
127#endif
Wolfgang Denk47f57792005-08-08 01:03:24 +0200128
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100129#if defined(CONFIG_CMD_CRAMFS)
130extern int cramfs_check (struct part_info *info);
131extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
132extern int cramfs_ls (struct part_info *info, char *filename);
133extern int cramfs_info (struct part_info *info);
134#else
135/* defining empty macros for function names is ugly but avoids ifdef clutter
136 * all over the code */
137#define cramfs_check(x) (0)
138#define cramfs_load(x,y,z) (-1)
139#define cramfs_ls(x,y) (0)
140#define cramfs_info(x) (0)
141#endif
Wolfgang Denk47f57792005-08-08 01:03:24 +0200142
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100143#ifndef CONFIG_CMD_MTDPARTS
Wolfgang Denk47f57792005-08-08 01:03:24 +0200144/**
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100145 * Check device number to be within valid range for given device type.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200146 *
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100147 * @param dev device to validate
148 * @return 0 if device is valid, 1 otherwise
wdenkc6097192002-11-03 00:24:07 +0000149 */
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100150static int mtd_device_validate(u8 type, u8 num, u32 *size)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200151{
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100152 if (type == MTD_DEV_TYPE_NOR) {
153#if defined(CONFIG_CMD_FLASH)
154 if (num < CONFIG_SYS_MAX_FLASH_BANKS) {
155 extern flash_info_t flash_info[];
156 *size = flash_info[num].size;
wdenkad276f22004-01-04 16:28:35 +0000157
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100158 return 0;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200159 }
wdenkc6097192002-11-03 00:24:07 +0000160
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100161 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
162 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_FLASH_BANKS - 1);
163#else
164 printf("support for FLASH devices not present\n");
165#endif
166 } else if (type == MTD_DEV_TYPE_NAND) {
167#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
Grygorii Strashko6d2a26682017-06-26 19:12:55 -0500168 struct mtd_info *mtd = get_nand_dev_by_index(num);
169 if (mtd) {
170 *size = mtd->size;
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100171 return 0;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200172 }
wdenkda04a8b2004-08-02 23:22:59 +0000173
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100174 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
175 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1);
176#else
177 printf("support for NAND devices not present\n");
178#endif
179 } else if (type == MTD_DEV_TYPE_ONENAND) {
180#if defined(CONFIG_CMD_ONENAND)
181 *size = onenand_mtd.size;
182 return 0;
183#else
184 printf("support for OneNAND devices not present\n");
185#endif
186 } else
187 printf("Unknown defice type %d\n", type);
wdenkc6097192002-11-03 00:24:07 +0000188
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100189 return 1;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200190}
wdenk8886a662004-04-18 19:43:36 +0000191
Wolfgang Denk47f57792005-08-08 01:03:24 +0200192/**
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100193 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
194 * return device type and number.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200195 *
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100196 * @param id string describing device id
197 * @param ret_id output pointer to next char after parse completes (output)
198 * @param dev_type parsed device type (output)
199 * @param dev_num parsed device number (output)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200200 * @return 0 on success, 1 otherwise
201 */
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100202static int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
wdenkc6097192002-11-03 00:24:07 +0000203{
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100204 const char *p = id;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200205
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100206 *dev_type = 0;
207 if (strncmp(p, "nand", 4) == 0) {
208 *dev_type = MTD_DEV_TYPE_NAND;
209 p += 4;
210 } else if (strncmp(p, "nor", 3) == 0) {
211 *dev_type = MTD_DEV_TYPE_NOR;
212 p += 3;
213 } else if (strncmp(p, "onenand", 7) == 0) {
214 *dev_type = MTD_DEV_TYPE_ONENAND;
215 p += 7;
216 } else {
217 printf("incorrect device type in %s\n", id);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200218 return 1;
219 }
wdenkc6097192002-11-03 00:24:07 +0000220
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100221 if (!isdigit(*p)) {
222 printf("incorrect device number in %s\n", id);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200223 return 1;
224 }
225
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100226 *dev_num = simple_strtoul(p, (char **)&p, 0);
227 if (ret_id)
228 *ret_id = p;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200229 return 0;
230}
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100231
Wolfgang Denk47f57792005-08-08 01:03:24 +0200232/*
233 * 'Static' version of command line mtdparts_init() routine. Single partition on
234 * a single device configuration.
235 */
236
237/**
Tomasz Figa63440e02008-12-30 18:35:57 +0100238 * Calculate sector size.
239 *
240 * @return sector size
241 */
242static inline u32 get_part_sector_size_nand(struct mtdids *id)
243{
244#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
Scott Wood08364d92016-05-30 13:57:54 -0500245 struct mtd_info *mtd;
Tomasz Figa63440e02008-12-30 18:35:57 +0100246
Grygorii Strashko6d2a26682017-06-26 19:12:55 -0500247 mtd = get_nand_dev_by_index(id->num);
Tomasz Figa63440e02008-12-30 18:35:57 +0100248
Scott Wood08364d92016-05-30 13:57:54 -0500249 return mtd->erasesize;
Tomasz Figa63440e02008-12-30 18:35:57 +0100250#else
251 BUG();
252 return 0;
253#endif
254}
255
256static inline u32 get_part_sector_size_nor(struct mtdids *id, struct part_info *part)
257{
258#if defined(CONFIG_CMD_FLASH)
259 extern flash_info_t flash_info[];
260
261 u32 end_phys, start_phys, sector_size = 0, size = 0;
262 int i;
263 flash_info_t *flash;
264
265 flash = &flash_info[id->num];
266
267 start_phys = flash->start[0] + part->offset;
Peter Tyser5e26c2a2010-12-30 15:47:56 -0600268 end_phys = start_phys + part->size - 1;
Tomasz Figa63440e02008-12-30 18:35:57 +0100269
270 for (i = 0; i < flash->sector_count; i++) {
271 if (flash->start[i] >= end_phys)
272 break;
273
274 if (flash->start[i] >= start_phys) {
275 if (i == flash->sector_count - 1) {
276 size = flash->start[0] + flash->size - flash->start[i];
277 } else {
278 size = flash->start[i+1] - flash->start[i];
279 }
280
281 if (sector_size < size)
282 sector_size = size;
283 }
284 }
285
286 return sector_size;
287#else
288 BUG();
289 return 0;
290#endif
291}
292
293static inline u32 get_part_sector_size_onenand(void)
294{
295#if defined(CONFIG_CMD_ONENAND)
296 struct mtd_info *mtd;
297
298 mtd = &onenand_mtd;
299
300 return mtd->erasesize;
301#else
302 BUG();
303 return 0;
304#endif
305}
306
307static inline u32 get_part_sector_size(struct mtdids *id, struct part_info *part)
308{
309 if (id->type == MTD_DEV_TYPE_NAND)
310 return get_part_sector_size_nand(id);
311 else if (id->type == MTD_DEV_TYPE_NOR)
312 return get_part_sector_size_nor(id, part);
313 else if (id->type == MTD_DEV_TYPE_ONENAND)
314 return get_part_sector_size_onenand();
315 else
316 DEBUGF("Error: Unknown device type.\n");
317
318 return 0;
319}
320
321/**
Wolfgang Denk47f57792005-08-08 01:03:24 +0200322 * Parse and initialize global mtdids mapping and create global
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200323 * device/partition list.
Wolfgang Denk47f57792005-08-08 01:03:24 +0200324 *
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200325 * 'Static' version of command line mtdparts_init() routine. Single partition on
326 * a single device configuration.
327 *
Wolfgang Denk47f57792005-08-08 01:03:24 +0200328 * @return 0 on success, 1 otherwise
329 */
330int mtdparts_init(void)
331{
332 static int initialized = 0;
333 u32 size;
334 char *dev_name;
335
336 DEBUGF("\n---mtdparts_init---\n");
337 if (!initialized) {
Wolfgang Denkc0d487b2005-08-09 21:41:20 +0200338 struct mtdids *id;
339 struct part_info *part;
340
Wolfgang Denk47f57792005-08-08 01:03:24 +0200341 initialized = 1;
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200342 current_mtd_dev = (struct mtd_device *)
Wolfgang Denk47f57792005-08-08 01:03:24 +0200343 malloc(sizeof(struct mtd_device) +
344 sizeof(struct part_info) +
345 sizeof(struct mtdids));
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200346 if (!current_mtd_dev) {
Wolfgang Denk47f57792005-08-08 01:03:24 +0200347 printf("out of memory\n");
348 return 1;
349 }
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200350 memset(current_mtd_dev, 0, sizeof(struct mtd_device) +
351 sizeof(struct part_info) + sizeof(struct mtdids));
Wolfgang Denk47f57792005-08-08 01:03:24 +0200352
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200353 id = (struct mtdids *)(current_mtd_dev + 1);
Wolfgang Denkc0d487b2005-08-09 21:41:20 +0200354 part = (struct part_info *)(id + 1);
wdenkc6097192002-11-03 00:24:07 +0000355
Wolfgang Denk47f57792005-08-08 01:03:24 +0200356 /* id */
357 id->mtd_id = "single part";
wdenkc6097192002-11-03 00:24:07 +0000358
Wolfgang Denk47f57792005-08-08 01:03:24 +0200359#if defined(CONFIG_JFFS2_DEV)
360 dev_name = CONFIG_JFFS2_DEV;
wdenkc6097192002-11-03 00:24:07 +0000361#else
Wolfgang Denk47f57792005-08-08 01:03:24 +0200362 dev_name = "nor0";
wdenkc6097192002-11-03 00:24:07 +0000363#endif
364
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100365 if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
366 (mtd_device_validate(id->type, id->num, &size) != 0)) {
Wolfgang Denk47f57792005-08-08 01:03:24 +0200367 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200368 free(current_mtd_dev);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200369 return 1;
370 }
371 id->size = size;
372 INIT_LIST_HEAD(&id->link);
373
374 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
375 id->type, id->num, id->size, id->mtd_id);
376
377 /* partition */
378 part->name = "static";
379 part->auto_name = 0;
380
381#if defined(CONFIG_JFFS2_PART_SIZE)
382 part->size = CONFIG_JFFS2_PART_SIZE;
383#else
384 part->size = SIZE_REMAINING;
385#endif
wdenkc6097192002-11-03 00:24:07 +0000386
Wolfgang Denk47f57792005-08-08 01:03:24 +0200387#if defined(CONFIG_JFFS2_PART_OFFSET)
388 part->offset = CONFIG_JFFS2_PART_OFFSET;
389#else
390 part->offset = 0x00000000;
wdenkc6097192002-11-03 00:24:07 +0000391#endif
392
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200393 part->dev = current_mtd_dev;
Wolfgang Denk47f57792005-08-08 01:03:24 +0200394 INIT_LIST_HEAD(&part->link);
395
396 /* recalculate size if needed */
397 if (part->size == SIZE_REMAINING)
398 part->size = id->size - part->offset;
wdenkc6097192002-11-03 00:24:07 +0000399
Mike Frysingerb5125d22010-01-08 08:03:06 -0500400 part->sector_size = get_part_sector_size(id, part);
401
Wolfgang Denk47f57792005-08-08 01:03:24 +0200402 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
403 part->name, part->size, part->offset);
404
405 /* device */
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200406 current_mtd_dev->id = id;
407 INIT_LIST_HEAD(&current_mtd_dev->link);
408 current_mtd_dev->num_parts = 1;
409 INIT_LIST_HEAD(&current_mtd_dev->parts);
410 list_add(&part->link, &current_mtd_dev->parts);
wdenk8886a662004-04-18 19:43:36 +0000411 }
Wolfgang Denk47f57792005-08-08 01:03:24 +0200412
wdenk8886a662004-04-18 19:43:36 +0000413 return 0;
414}
Stefan Roeseb1423dd2009-03-19 13:30:36 +0100415#endif /* #ifndef CONFIG_CMD_MTDPARTS */
wdenk8886a662004-04-18 19:43:36 +0000416
Wolfgang Denk47f57792005-08-08 01:03:24 +0200417/**
418 * Return pointer to the partition of a requested number from a requested
419 * device.
420 *
421 * @param dev device that is to be searched for a partition
422 * @param part_num requested partition number
423 * @return pointer to the part_info, NULL otherwise
424 */
425static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
wdenk8886a662004-04-18 19:43:36 +0000426{
Wolfgang Denk47f57792005-08-08 01:03:24 +0200427 struct list_head *entry;
428 struct part_info *part;
429 int num;
wdenk8886a662004-04-18 19:43:36 +0000430
Wolfgang Denk47f57792005-08-08 01:03:24 +0200431 if (!dev)
432 return NULL;
wdenk8886a662004-04-18 19:43:36 +0000433
Wolfgang Denk47f57792005-08-08 01:03:24 +0200434 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
435 part_num, MTD_DEV_TYPE(dev->id->type),
436 dev->id->num, dev->id->mtd_id);
wdenk8886a662004-04-18 19:43:36 +0000437
Wolfgang Denk47f57792005-08-08 01:03:24 +0200438 if (part_num >= dev->num_parts) {
439 printf("invalid partition number %d for device %s%d (%s)\n",
440 part_num, MTD_DEV_TYPE(dev->id->type),
441 dev->id->num, dev->id->mtd_id);
442 return NULL;
443 }
wdenk8886a662004-04-18 19:43:36 +0000444
Wolfgang Denk47f57792005-08-08 01:03:24 +0200445 /* locate partition number, return it */
446 num = 0;
447 list_for_each(entry, &dev->parts) {
448 part = list_entry(entry, struct part_info, link);
449
450 if (part_num == num++) {
451 return part;
452 }
wdenkc6097192002-11-03 00:24:07 +0000453 }
Wolfgang Denk47f57792005-08-08 01:03:24 +0200454
455 return NULL;
wdenkc6097192002-11-03 00:24:07 +0000456}
wdenk8886a662004-04-18 19:43:36 +0000457
Michal Simek6ed8c712007-08-06 23:31:49 +0200458/***************************************************/
Bin Meng75574052016-02-05 19:30:11 -0800459/* U-Boot commands */
Michal Simek6ed8c712007-08-06 23:31:49 +0200460/***************************************************/
wdenkae65f502004-01-03 21:24:46 +0000461
Wolfgang Denk47f57792005-08-08 01:03:24 +0200462/**
463 * Routine implementing fsload u-boot command. This routine tries to load
464 * a requested file from jffs2/cramfs filesystem on a current partition.
465 *
466 * @param cmdtp command internal data
467 * @param flag command flag
468 * @param argc number of arguments supplied to the command
469 * @param argv arguments list
470 * @return 0 on success, 1 otherwise
471 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200472int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +0000473{
wdenkf2140d52004-07-01 16:30:44 +0000474 char *fsname;
wdenk3c711762004-06-09 13:37:52 +0000475 char *filename;
wdenkf2140d52004-07-01 16:30:44 +0000476 int size;
477 struct part_info *part;
478 ulong offset = load_addr;
wdenk3c711762004-06-09 13:37:52 +0000479
480 /* pre-set Boot file name */
Simon Glass64b723f2017-08-03 12:22:12 -0600481 filename = env_get("bootfile");
482 if (!filename)
wdenk3c711762004-06-09 13:37:52 +0000483 filename = "uImage";
wdenk3c711762004-06-09 13:37:52 +0000484
wdenkc6097192002-11-03 00:24:07 +0000485 if (argc == 2) {
486 filename = argv[1];
487 }
488 if (argc == 3) {
489 offset = simple_strtoul(argv[1], NULL, 16);
wdenk42c05472004-03-23 22:14:11 +0000490 load_addr = offset;
wdenkc6097192002-11-03 00:24:07 +0000491 filename = argv[2];
492 }
493
Wolfgang Denk47f57792005-08-08 01:03:24 +0200494 /* make sure we are in sync with env variables */
495 if (mtdparts_init() !=0)
496 return 1;
497
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200498 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
wdenkc6097192002-11-03 00:24:07 +0000499
Michal Simek40277b32007-09-15 00:03:35 +0200500 /* check partition type for cramfs */
501 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
wdenkae65f502004-01-03 21:24:46 +0000502 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
503
504 if (cramfs_check(part)) {
505 size = cramfs_load ((char *) offset, part, filename);
506 } else {
Michal Simek40277b32007-09-15 00:03:35 +0200507 /* if this is not cramfs assume jffs2 */
wdenkae65f502004-01-03 21:24:46 +0000508 size = jffs2_1pass_load((char *)offset, part, filename);
509 }
wdenkc6097192002-11-03 00:24:07 +0000510
511 if (size > 0) {
wdenkae65f502004-01-03 21:24:46 +0000512 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
513 fsname, size, offset);
Simon Glass4d949a22017-08-03 12:22:10 -0600514 env_set_hex("filesize", size);
wdenkc6097192002-11-03 00:24:07 +0000515 } else {
wdenkae65f502004-01-03 21:24:46 +0000516 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
wdenkc6097192002-11-03 00:24:07 +0000517 }
518
519 return !(size > 0);
520 }
Wolfgang Denk79117cd2005-08-16 09:32:45 +0200521 return 1;
wdenkc6097192002-11-03 00:24:07 +0000522}
523
Wolfgang Denk47f57792005-08-08 01:03:24 +0200524/**
525 * Routine implementing u-boot ls command which lists content of a given
526 * directory on a current partition.
527 *
528 * @param cmdtp command internal data
529 * @param flag command flag
530 * @param argc number of arguments supplied to the command
531 * @param argv arguments list
532 * @return 0 on success, 1 otherwise
533 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200534int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +0000535{
wdenk8d414a72005-06-10 10:00:19 +0000536 char *filename = "/";
wdenkc6097192002-11-03 00:24:07 +0000537 int ret;
538 struct part_info *part;
539
540 if (argc == 2)
541 filename = argv[1];
542
Wolfgang Denk47f57792005-08-08 01:03:24 +0200543 /* make sure we are in sync with env variables */
544 if (mtdparts_init() !=0)
545 return 1;
546
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200547 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
wdenkc6097192002-11-03 00:24:07 +0000548
wdenkae65f502004-01-03 21:24:46 +0000549 /* check partition type for cramfs */
550 if (cramfs_check(part)) {
551 ret = cramfs_ls (part, filename);
552 } else {
Michal Simek40277b32007-09-15 00:03:35 +0200553 /* if this is not cramfs assume jffs2 */
wdenkae65f502004-01-03 21:24:46 +0000554 ret = jffs2_1pass_ls(part, filename);
555 }
wdenkc6097192002-11-03 00:24:07 +0000556
Wolfgang Denk79117cd2005-08-16 09:32:45 +0200557 return ret ? 0 : 1;
wdenkc6097192002-11-03 00:24:07 +0000558 }
Wolfgang Denk79117cd2005-08-16 09:32:45 +0200559 return 1;
wdenkc6097192002-11-03 00:24:07 +0000560}
561
Wolfgang Denk47f57792005-08-08 01:03:24 +0200562/**
563 * Routine implementing u-boot fsinfo command. This routine prints out
564 * miscellaneous filesystem informations/statistics.
565 *
566 * @param cmdtp command internal data
567 * @param flag command flag
568 * @param argc number of arguments supplied to the command
569 * @param argv arguments list
570 * @return 0 on success, 1 otherwise
571 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200572int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +0000573{
wdenkc6097192002-11-03 00:24:07 +0000574 struct part_info *part;
Michal Simek40277b32007-09-15 00:03:35 +0200575 char *fsname;
wdenkae65f502004-01-03 21:24:46 +0000576 int ret;
wdenkc6097192002-11-03 00:24:07 +0000577
Wolfgang Denk47f57792005-08-08 01:03:24 +0200578 /* make sure we are in sync with env variables */
579 if (mtdparts_init() !=0)
580 return 1;
Wolfgang Denkeb95c852005-08-10 15:14:32 +0200581
Stefan Roese4fa1f2d2009-05-16 12:04:22 +0200582 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
wdenkae65f502004-01-03 21:24:46 +0000583
584 /* check partition type for cramfs */
Michal Simek40277b32007-09-15 00:03:35 +0200585 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
586 printf("### filesystem type is %s\n", fsname);
wdenkc6097192002-11-03 00:24:07 +0000587
wdenkae65f502004-01-03 21:24:46 +0000588 if (cramfs_check(part)) {
589 ret = cramfs_info (part);
590 } else {
Michal Simek40277b32007-09-15 00:03:35 +0200591 /* if this is not cramfs assume jffs2 */
wdenkae65f502004-01-03 21:24:46 +0000592 ret = jffs2_1pass_info(part);
593 }
wdenkc6097192002-11-03 00:24:07 +0000594
Wolfgang Denk79117cd2005-08-16 09:32:45 +0200595 return ret ? 0 : 1;
wdenkc6097192002-11-03 00:24:07 +0000596 }
Wolfgang Denk47f57792005-08-08 01:03:24 +0200597 return 1;
598}
Wolfgang Denk47f57792005-08-08 01:03:24 +0200599
600/***************************************************/
wdenkf287a242003-07-01 21:06:45 +0000601U_BOOT_CMD(
602 fsload, 3, 0, do_jffs2_fsload,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600603 "load binary file from a filesystem image",
wdenk57b2d802003-06-27 21:31:46 +0000604 "[ off ] [ filename ]\n"
605 " - load binary file from flash bank\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200606 " with offset 'off'"
wdenk57b2d802003-06-27 21:31:46 +0000607);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200608U_BOOT_CMD(
Siva Durga Prasad Paladugub9a9afd2017-05-30 14:18:55 +0200609 fsls, 2, 1, do_jffs2_ls,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600610 "list files in a directory (default /)",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200611 "[ directory ]"
Wolfgang Denk47f57792005-08-08 01:03:24 +0200612);
wdenk57b2d802003-06-27 21:31:46 +0000613
wdenkf287a242003-07-01 21:06:45 +0000614U_BOOT_CMD(
615 fsinfo, 1, 1, do_jffs2_fsinfo,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600616 "print information about filesystems",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200617 ""
wdenk57b2d802003-06-27 21:31:46 +0000618);
Wolfgang Denk47f57792005-08-08 01:03:24 +0200619/***************************************************/