]> Git Repo - J-u-boot.git/blame - cmd/jffs2.c
command: Remove the cmd_tbl_t typedef
[J-u-boot.git] / cmd / jffs2.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
c609719b
WD
2/*
3 * (C) Copyright 2002
4 * Wolfgang Denk, DENX Software Engineering, [email protected].
700a0c64 5 *
dd875c76
WD
6 * (C) Copyright 2002
7 * Robert Schwebel, Pengutronix, <[email protected]>
700a0c64 8 *
dd875c76
WD
9 * (C) Copyright 2003
10 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <[email protected]>
c609719b 11 *
700a0c64
WD
12 * (C) Copyright 2005
13 * Wolfgang Denk, DENX Software Engineering, [email protected].
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
700a0c64
WD
21 */
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 *
8f79e4c2 31 *
700a0c64
WD
32 * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
33 *
34 * mtdids=<idmap>[,<idmap>,...]
35 *
36 * <idmap> := <dev-id>=<mtd-id>
1a7f8cce 37 * <dev-id> := 'nand'|'nor'|'onenand'<dev-num>
700a0c64
WD
38 * <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/*
991b089d 71 * JFFS2/CRAMFS support
700a0c64
WD
72 */
73#include <common.h>
74#include <command.h>
c7694dd4 75#include <env.h>
b79fdc76 76#include <flash.h>
8e8ccfe1 77#include <image.h>
700a0c64
WD
78#include <malloc.h>
79#include <jffs2/jffs2.h>
700a0c64
WD
80#include <linux/list.h>
81#include <linux/ctype.h>
700a0c64
WD
82#include <cramfs/cramfs_fs.h>
83
c76fe474 84#if defined(CONFIG_CMD_NAND)
6ae3900a 85#include <linux/mtd/rawnand.h>
ac7eb8a3 86#include <nand.h>
c76fe474 87#endif
1a7f8cce
KP
88
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
700a0c64 95/* enable/disable debugging messages */
038ccac5
BS
96#define DEBUG_JFFS
97#undef DEBUG_JFFS
700a0c64 98
ab4b956d 99#ifdef DEBUG_JFFS
700a0c64
WD
100# 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 */
038ccac5 119#define MTD_WRITEABLE_CMD 1
700a0c64 120
68d7d651 121/* current active device and partition number */
76b5883d
SR
122#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
700a0c64 131
68d7d651
SR
132#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
700a0c64 145
68d7d651 146#ifndef CONFIG_CMD_MTDPARTS
700a0c64 147/**
68d7d651 148 * Check device number to be within valid range for given device type.
700a0c64 149 *
68d7d651
SR
150 * @param dev device to validate
151 * @return 0 if device is valid, 1 otherwise
c609719b 152 */
68d7d651 153static int mtd_device_validate(u8 type, u8 num, u32 *size)
700a0c64 154{
68d7d651
SR
155 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;
180d3f74 160
68d7d651 161 return 0;
700a0c64 162 }
c609719b 163
68d7d651
SR
164 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)
bfdba68e
GS
171 struct mtd_info *mtd = get_nand_dev_by_index(num);
172 if (mtd) {
173 *size = mtd->size;
68d7d651 174 return 0;
700a0c64 175 }
6705d81e 176
68d7d651
SR
177 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);
c609719b 191
68d7d651 192 return 1;
700a0c64 193}
998eaaec 194
700a0c64 195/**
68d7d651
SR
196 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
197 * return device type and number.
700a0c64 198 *
68d7d651
SR
199 * @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)
700a0c64
WD
203 * @return 0 on success, 1 otherwise
204 */
68d7d651 205static int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
c609719b 206{
68d7d651 207 const char *p = id;
700a0c64 208
68d7d651
SR
209 *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);
700a0c64
WD
221 return 1;
222 }
c609719b 223
68d7d651
SR
224 if (!isdigit(*p)) {
225 printf("incorrect device number in %s\n", id);
700a0c64
WD
226 return 1;
227 }
228
68d7d651
SR
229 *dev_num = simple_strtoul(p, (char **)&p, 0);
230 if (ret_id)
231 *ret_id = p;
700a0c64
WD
232 return 0;
233}
68d7d651 234
700a0c64
WD
235/*
236 * 'Static' version of command line mtdparts_init() routine. Single partition on
237 * a single device configuration.
238 */
239
b5b004ad
TF
240/**
241 * 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)
151c06ec 248 struct mtd_info *mtd;
b5b004ad 249
bfdba68e 250 mtd = get_nand_dev_by_index(id->num);
b5b004ad 251
151c06ec 252 return mtd->erasesize;
b5b004ad
TF
253#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;
141053d6 271 end_phys = start_phys + part->size - 1;
b5b004ad
TF
272
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
700a0c64
WD
324/**
325 * Parse and initialize global mtdids mapping and create global
8f79e4c2 326 * device/partition list.
700a0c64 327 *
76b5883d
SR
328 * 'Static' version of command line mtdparts_init() routine. Single partition on
329 * a single device configuration.
330 *
700a0c64
WD
331 * @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) {
c4e0e686
WD
341 struct mtdids *id;
342 struct part_info *part;
343
700a0c64 344 initialized = 1;
76b5883d 345 current_mtd_dev = (struct mtd_device *)
700a0c64
WD
346 malloc(sizeof(struct mtd_device) +
347 sizeof(struct part_info) +
348 sizeof(struct mtdids));
76b5883d 349 if (!current_mtd_dev) {
700a0c64
WD
350 printf("out of memory\n");
351 return 1;
352 }
76b5883d
SR
353 memset(current_mtd_dev, 0, sizeof(struct mtd_device) +
354 sizeof(struct part_info) + sizeof(struct mtdids));
700a0c64 355
76b5883d 356 id = (struct mtdids *)(current_mtd_dev + 1);
c4e0e686 357 part = (struct part_info *)(id + 1);
c609719b 358
700a0c64
WD
359 /* id */
360 id->mtd_id = "single part";
c609719b 361
700a0c64
WD
362#if defined(CONFIG_JFFS2_DEV)
363 dev_name = CONFIG_JFFS2_DEV;
c609719b 364#else
700a0c64 365 dev_name = "nor0";
c609719b
WD
366#endif
367
68d7d651
SR
368 if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
369 (mtd_device_validate(id->type, id->num, &size) != 0)) {
700a0c64 370 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
76b5883d 371 free(current_mtd_dev);
700a0c64
WD
372 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
c609719b 389
700a0c64
WD
390#if defined(CONFIG_JFFS2_PART_OFFSET)
391 part->offset = CONFIG_JFFS2_PART_OFFSET;
392#else
393 part->offset = 0x00000000;
c609719b
WD
394#endif
395
76b5883d 396 part->dev = current_mtd_dev;
700a0c64
WD
397 INIT_LIST_HEAD(&part->link);
398
399 /* recalculate size if needed */
400 if (part->size == SIZE_REMAINING)
401 part->size = id->size - part->offset;
c609719b 402
2903ad33
MF
403 part->sector_size = get_part_sector_size(id, part);
404
700a0c64
WD
405 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
406 part->name, part->size, part->offset);
407
408 /* device */
76b5883d
SR
409 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);
c609719b 414 }
700a0c64 415
c609719b
WD
416 return 0;
417}
68d7d651 418#endif /* #ifndef CONFIG_CMD_MTDPARTS */
998eaaec 419
700a0c64
WD
420/**
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)
998eaaec 429{
700a0c64
WD
430 struct list_head *entry;
431 struct part_info *part;
432 int num;
998eaaec 433
700a0c64
WD
434 if (!dev)
435 return NULL;
998eaaec 436
700a0c64
WD
437 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);
998eaaec 440
700a0c64
WD
441 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 }
998eaaec 447
700a0c64
WD
448 /* 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 }
998eaaec 456 }
700a0c64
WD
457
458 return NULL;
998eaaec
WD
459}
460
ab4b956d 461/***************************************************/
a187559e 462/* U-Boot commands */
ab4b956d 463/***************************************************/
dd875c76 464
700a0c64
WD
465/**
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 */
09140113
SG
475int do_jffs2_fsload(struct cmd_tbl *cmdtp, int flag, int argc,
476 char *const argv[])
c609719b 477{
39539887 478 char *fsname;
f39748ae 479 char *filename;
39539887
WD
480 int size;
481 struct part_info *part;
bb872dd9 482 ulong offset = image_load_addr;
f39748ae
WD
483
484 /* pre-set Boot file name */
00caae6d
SG
485 filename = env_get("bootfile");
486 if (!filename)
f39748ae 487 filename = "uImage";
f39748ae 488
c609719b
WD
489 if (argc == 2) {
490 filename = argv[1];
491 }
492 if (argc == 3) {
493 offset = simple_strtoul(argv[1], NULL, 16);
bb872dd9 494 image_load_addr = offset;
c609719b
WD
495 filename = argv[2];
496 }
497
700a0c64
WD
498 /* make sure we are in sync with env variables */
499 if (mtdparts_init() !=0)
500 return 1;
501
76b5883d 502 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
c609719b 503
991b089d
MS
504 /* check partition type for cramfs */
505 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
dd875c76
WD
506 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
507
508 if (cramfs_check(part)) {
509 size = cramfs_load ((char *) offset, part, filename);
510 } else {
991b089d 511 /* if this is not cramfs assume jffs2 */
dd875c76
WD
512 size = jffs2_1pass_load((char *)offset, part, filename);
513 }
c609719b
WD
514
515 if (size > 0) {
dd875c76
WD
516 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
517 fsname, size, offset);
018f5303 518 env_set_hex("filesize", size);
c609719b 519 } else {
dd875c76 520 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
c609719b
WD
521 }
522
523 return !(size > 0);
524 }
87b8bd5a 525 return 1;
c609719b
WD
526}
527
700a0c64
WD
528/**
529 * Routine implementing u-boot ls command which lists content of a given
530 * directory on a current partition.
531 *
532 * @param cmdtp command internal data
533 * @param flag command flag
534 * @param argc number of arguments supplied to the command
535 * @param argv arguments list
536 * @return 0 on success, 1 otherwise
537 */
09140113 538int do_jffs2_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
c609719b 539{
a87589da 540 char *filename = "/";
c609719b
WD
541 int ret;
542 struct part_info *part;
543
544 if (argc == 2)
545 filename = argv[1];
546
700a0c64
WD
547 /* make sure we are in sync with env variables */
548 if (mtdparts_init() !=0)
549 return 1;
550
76b5883d 551 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
c609719b 552
dd875c76
WD
553 /* check partition type for cramfs */
554 if (cramfs_check(part)) {
555 ret = cramfs_ls (part, filename);
556 } else {
991b089d 557 /* if this is not cramfs assume jffs2 */
dd875c76
WD
558 ret = jffs2_1pass_ls(part, filename);
559 }
c609719b 560
87b8bd5a 561 return ret ? 0 : 1;
c609719b 562 }
87b8bd5a 563 return 1;
c609719b
WD
564}
565
700a0c64
WD
566/**
567 * Routine implementing u-boot fsinfo command. This routine prints out
568 * miscellaneous filesystem informations/statistics.
569 *
570 * @param cmdtp command internal data
571 * @param flag command flag
572 * @param argc number of arguments supplied to the command
573 * @param argv arguments list
574 * @return 0 on success, 1 otherwise
575 */
09140113
SG
576int do_jffs2_fsinfo(struct cmd_tbl *cmdtp, int flag, int argc,
577 char *const argv[])
c609719b 578{
c609719b 579 struct part_info *part;
991b089d 580 char *fsname;
dd875c76 581 int ret;
c609719b 582
700a0c64
WD
583 /* make sure we are in sync with env variables */
584 if (mtdparts_init() !=0)
585 return 1;
8f79e4c2 586
76b5883d 587 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
dd875c76
WD
588
589 /* check partition type for cramfs */
991b089d
MS
590 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
591 printf("### filesystem type is %s\n", fsname);
c609719b 592
dd875c76
WD
593 if (cramfs_check(part)) {
594 ret = cramfs_info (part);
595 } else {
991b089d 596 /* if this is not cramfs assume jffs2 */
dd875c76
WD
597 ret = jffs2_1pass_info(part);
598 }
c609719b 599
87b8bd5a 600 return ret ? 0 : 1;
c609719b 601 }
87b8bd5a 602 return 1;
c609719b
WD
603}
604
700a0c64 605/***************************************************/
0d498393
WD
606U_BOOT_CMD(
607 fsload, 3, 0, do_jffs2_fsload,
2fb2604d 608 "load binary file from a filesystem image",
8bde7f77
WD
609 "[ off ] [ filename ]\n"
610 " - load binary file from flash bank\n"
a89c33db 611 " with offset 'off'"
8bde7f77 612);
700a0c64 613U_BOOT_CMD(
314f6362 614 fsls, 2, 1, do_jffs2_ls,
2fb2604d 615 "list files in a directory (default /)",
a89c33db 616 "[ directory ]"
700a0c64 617);
8bde7f77 618
0d498393
WD
619U_BOOT_CMD(
620 fsinfo, 1, 1, do_jffs2_fsinfo,
2fb2604d 621 "print information about filesystems",
a89c33db 622 ""
8bde7f77 623);
700a0c64 624/***************************************************/
This page took 0.605738 seconds and 4 git commands to generate.