2 * Unsorted Block Image commands
4 * Copyright (C) 2008 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
7 * Copyright 2008 Stefan Roese <sr@denx.de>, DENX Software Engineering
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
19 #include <onenand_uboot.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/partitions.h>
22 #include <ubi_uboot.h>
23 #include <asm/errno.h>
24 #include <jffs2/load_kernel.h>
26 #define DEV_TYPE_NONE 0
27 #define DEV_TYPE_NAND 1
28 #define DEV_TYPE_ONENAND 2
29 #define DEV_TYPE_NOR 3
31 /* Private own data */
32 static struct ubi_device
*ubi
;
33 static char buffer
[80];
34 static int ubi_initialized
;
37 char dev_name
[32]; /* NAND/OneNAND etc */
41 struct mtd_info
*mtd_info
;
44 static struct selected_dev ubi_dev
;
46 static void ubi_dump_vol_info(const struct ubi_volume
*vol
)
48 ubi_msg("volume information dump:");
49 ubi_msg("vol_id %d", vol
->vol_id
);
50 ubi_msg("reserved_pebs %d", vol
->reserved_pebs
);
51 ubi_msg("alignment %d", vol
->alignment
);
52 ubi_msg("data_pad %d", vol
->data_pad
);
53 ubi_msg("vol_type %d", vol
->vol_type
);
54 ubi_msg("name_len %d", vol
->name_len
);
55 ubi_msg("usable_leb_size %d", vol
->usable_leb_size
);
56 ubi_msg("used_ebs %d", vol
->used_ebs
);
57 ubi_msg("used_bytes %lld", vol
->used_bytes
);
58 ubi_msg("last_eb_bytes %d", vol
->last_eb_bytes
);
59 ubi_msg("corrupted %d", vol
->corrupted
);
60 ubi_msg("upd_marker %d", vol
->upd_marker
);
62 if (vol
->name_len
<= UBI_VOL_NAME_MAX
&&
63 strnlen(vol
->name
, vol
->name_len
+ 1) == vol
->name_len
) {
64 ubi_msg("name %s", vol
->name
);
66 ubi_msg("the 1st 5 characters of the name: %c%c%c%c%c",
67 vol
->name
[0], vol
->name
[1], vol
->name
[2],
68 vol
->name
[3], vol
->name
[4]);
73 static void display_volume_info(struct ubi_device
*ubi
)
77 for (i
= 0; i
< (ubi
->vtbl_slots
+ 1); i
++) {
79 continue; /* Empty record */
80 ubi_dump_vol_info(ubi
->volumes
[i
]);
84 static void display_ubi_info(struct ubi_device
*ubi
)
86 ubi_msg("MTD device name: \"%s\"", ubi
->mtd
->name
);
87 ubi_msg("MTD device size: %llu MiB", ubi
->flash_size
>> 20);
88 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
89 ubi
->peb_size
, ubi
->peb_size
>> 10);
90 ubi_msg("logical eraseblock size: %d bytes", ubi
->leb_size
);
91 ubi_msg("number of good PEBs: %d", ubi
->good_peb_count
);
92 ubi_msg("number of bad PEBs: %d", ubi
->bad_peb_count
);
93 ubi_msg("smallest flash I/O unit: %d", ubi
->min_io_size
);
94 ubi_msg("VID header offset: %d (aligned %d)",
95 ubi
->vid_hdr_offset
, ubi
->vid_hdr_aloffset
);
96 ubi_msg("data offset: %d", ubi
->leb_start
);
97 ubi_msg("max. allowed volumes: %d", ubi
->vtbl_slots
);
98 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD
);
99 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT
);
100 ubi_msg("number of user volumes: %d",
101 ubi
->vol_count
- UBI_INT_VOL_COUNT
);
102 ubi_msg("available PEBs: %d", ubi
->avail_pebs
);
103 ubi_msg("total number of reserved PEBs: %d", ubi
->rsvd_pebs
);
104 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
106 ubi_msg("max/mean erase counter: %d/%d", ubi
->max_ec
, ubi
->mean_ec
);
109 static int ubi_info(int layout
)
112 display_volume_info(ubi
);
114 display_ubi_info(ubi
);
119 static int verify_mkvol_req(const struct ubi_device
*ubi
,
120 const struct ubi_mkvol_req
*req
)
122 int n
, err
= -EINVAL
;
124 if (req
->bytes
< 0 || req
->alignment
< 0 || req
->vol_type
< 0 ||
128 if ((req
->vol_id
< 0 || req
->vol_id
>= ubi
->vtbl_slots
) &&
129 req
->vol_id
!= UBI_VOL_NUM_AUTO
)
132 if (req
->alignment
== 0)
138 if (req
->vol_type
!= UBI_DYNAMIC_VOLUME
&&
139 req
->vol_type
!= UBI_STATIC_VOLUME
)
142 if (req
->alignment
> ubi
->leb_size
)
145 n
= req
->alignment
% ubi
->min_io_size
;
146 if (req
->alignment
!= 1 && n
)
149 if (req
->name_len
> UBI_VOL_NAME_MAX
) {
156 printf("bad volume creation request");
160 static int ubi_create_vol(char *volume
, int size
, int dynamic
)
162 struct ubi_mkvol_req req
;
166 req
.vol_type
= UBI_DYNAMIC_VOLUME
;
168 req
.vol_type
= UBI_STATIC_VOLUME
;
170 req
.vol_id
= UBI_VOL_NUM_AUTO
;
174 strcpy(req
.name
, volume
);
175 req
.name_len
= strlen(volume
);
176 req
.name
[req
.name_len
] = '\0';
178 /* It's duplicated at drivers/mtd/ubi/cdev.c */
179 err
= verify_mkvol_req(ubi
, &req
);
181 printf("verify_mkvol_req failed %d\n", err
);
184 printf("Creating %s volume %s of size %d\n",
185 dynamic
? "dynamic" : "static", volume
, size
);
186 /* Call real ubi create volume */
187 return ubi_create_volume(ubi
, &req
);
190 static int ubi_remove_vol(char *volume
)
192 int i
, err
, reserved_pebs
;
193 int found
= 0, vol_id
= 0;
194 struct ubi_volume
*vol
;
196 for (i
= 0; i
< ubi
->vtbl_slots
; i
++) {
197 vol
= ubi
->volumes
[i
];
198 if (vol
&& !strcmp(vol
->name
, volume
)) {
199 printf("Volume %s found at valid %d\n", volume
, i
);
206 printf("%s volume not found\n", volume
);
209 printf("remove UBI volume %s (id %d)\n", vol
->name
, vol
->vol_id
);
212 printf("It's read-only mode\n");
217 err
= ubi_change_vtbl_record(ubi
, vol_id
, NULL
);
219 printf("Error changing Vol tabel record err=%x\n", err
);
222 reserved_pebs
= vol
->reserved_pebs
;
223 for (i
= 0; i
< vol
->reserved_pebs
; i
++) {
224 err
= ubi_eba_unmap_leb(ubi
, vol
, i
);
230 ubi
->volumes
[vol_id
]->eba_tbl
= NULL
;
231 ubi
->volumes
[vol_id
] = NULL
;
233 ubi
->rsvd_pebs
-= reserved_pebs
;
234 ubi
->avail_pebs
+= reserved_pebs
;
235 i
= ubi
->beb_rsvd_level
- ubi
->beb_rsvd_pebs
;
237 i
= ubi
->avail_pebs
>= i
? i
: ubi
->avail_pebs
;
238 ubi
->avail_pebs
-= i
;
240 ubi
->beb_rsvd_pebs
+= i
;
242 ubi_msg("reserve more %d PEBs", i
);
248 ubi_err("cannot remove volume %d, error %d", vol_id
, err
);
252 static int ubi_volume_write(char *volume
, void *buf
, size_t size
)
257 struct ubi_volume
*vol
;
259 for (i
= 0; i
< ubi
->vtbl_slots
; i
++) {
260 vol
= ubi
->volumes
[i
];
261 if (vol
&& !strcmp(vol
->name
, volume
)) {
262 printf("Volume \"%s\" found at volume id %d\n", volume
, i
);
268 printf("%s volume not found\n", volume
);
271 rsvd_bytes
= vol
->reserved_pebs
* (ubi
->leb_size
- vol
->data_pad
);
272 if (size
< 0 || size
> rsvd_bytes
) {
273 printf("rsvd_bytes=%d vol->reserved_pebs=%d ubi->leb_size=%d\n",
274 rsvd_bytes
, vol
->reserved_pebs
, ubi
->leb_size
);
275 printf("vol->data_pad=%d\n", vol
->data_pad
);
276 printf("Size > volume size !!\n");
280 err
= ubi_start_update(ubi
, vol
, size
);
282 printf("Cannot start volume update\n");
286 err
= ubi_more_update_data(ubi
, vol
, buf
, size
);
288 printf("Couldnt or partially wrote data \n");
295 err
= ubi_check_volume(ubi
, vol
->vol_id
);
300 ubi_warn("volume %d on UBI device %d is corrupted",
301 vol
->vol_id
, ubi
->ubi_num
);
306 ubi_gluebi_updated(vol
);
312 static int ubi_volume_read(char *volume
, char *buf
, size_t size
)
314 int err
, lnum
, off
, len
, tbuf_size
, i
= 0;
315 size_t count_save
= size
;
317 unsigned long long tmp
;
318 struct ubi_volume
*vol
= NULL
;
321 for (i
= 0; i
< ubi
->vtbl_slots
; i
++) {
322 vol
= ubi
->volumes
[i
];
323 if (vol
&& !strcmp(vol
->name
, volume
)) {
324 printf("Volume %s found at volume id %d\n",
325 volume
, vol
->vol_id
);
329 if (i
== ubi
->vtbl_slots
) {
330 printf("%s volume not found\n", volume
);
334 printf("read %i bytes from volume %d to %x(buf address)\n",
335 (int) size
, vol
->vol_id
, (unsigned)buf
);
341 if (vol
->upd_marker
) {
342 printf("damaged volume, update marker is set");
345 if (offp
== vol
->used_bytes
)
349 printf("Read [%lu] bytes\n", (unsigned long) vol
->used_bytes
);
350 size
= vol
->used_bytes
;
354 printf("read from corrupted volume %d", vol
->vol_id
);
355 if (offp
+ size
> vol
->used_bytes
)
356 count_save
= size
= vol
->used_bytes
- offp
;
358 tbuf_size
= vol
->usable_leb_size
;
359 if (size
< tbuf_size
)
360 tbuf_size
= ALIGN(size
, ubi
->min_io_size
);
361 tbuf
= malloc(tbuf_size
);
366 len
= size
> tbuf_size
? tbuf_size
: size
;
369 off
= do_div(tmp
, vol
->usable_leb_size
);
372 if (off
+ len
>= vol
->usable_leb_size
)
373 len
= vol
->usable_leb_size
- off
;
375 err
= ubi_eba_read_leb(ubi
, vol
, lnum
, tbuf
, off
, len
, 0);
377 printf("read err %x\n", err
);
381 if (off
== vol
->usable_leb_size
) {
383 off
-= vol
->usable_leb_size
;
389 memcpy(buf
, tbuf
, len
);
392 len
= size
> tbuf_size
? tbuf_size
: size
;
396 return err
? err
: count_save
- size
;
399 static int ubi_dev_scan(struct mtd_info
*info
, char *ubidev
)
401 struct mtd_device
*dev
;
402 struct part_info
*part
;
403 struct mtd_partition mtd_part
;
407 if (mtdparts_init() != 0)
410 if (find_dev_and_part(ubidev
, &dev
, &pnum
, &part
) != 0)
413 sprintf(buffer
, "mtd=%d", pnum
);
414 memset(&mtd_part
, 0, sizeof(mtd_part
));
415 mtd_part
.name
= buffer
;
416 mtd_part
.size
= part
->size
;
417 mtd_part
.offset
= part
->offset
;
418 add_mtd_partitions(info
, &mtd_part
, 1);
420 err
= ubi_mtd_param_parse(buffer
, NULL
);
422 del_mtd_partitions(info
);
428 del_mtd_partitions(info
);
437 static int do_ubi(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
448 if (strcmp(argv
[1], "part") == 0) {
449 /* Print current partition */
451 if (ubi_dev
.type
== DEV_TYPE_NONE
) {
452 printf("Error, no UBI device/partition selected!\n");
456 printf("%s Device %d: %s, partition %s\n", ubi_dev
.dev_name
,
457 ubi_dev
.nr
, ubi_dev
.mtd_info
->name
, ubi_dev
.part_name
);
466 /* todo: get dev number for NAND... */
470 * Call ubi_exit() before re-initializing the UBI subsystem
472 if (ubi_initialized
) {
474 del_mtd_partitions(ubi_dev
.mtd_info
);
478 * Check for nand|onenand selection
480 #if defined(CONFIG_CMD_NAND)
481 if (strcmp(argv
[2], "nand") == 0) {
482 strcpy(ubi_dev
.dev_name
, "NAND");
483 ubi_dev
.type
= DEV_TYPE_NAND
;
484 ubi_dev
.mtd_info
= &nand_info
[ubi_dev
.nr
];
487 #if defined(CONFIG_FLASH_CFI_MTD)
488 if (strcmp(argv
[2], "nor") == 0) {
489 strcpy(ubi_dev
.dev_name
, "NOR");
490 ubi_dev
.type
= DEV_TYPE_NOR
;
491 ubi_dev
.mtd_info
= get_mtd_device_nm(CFI_MTD_DEV_NAME
);
494 #if defined(CONFIG_CMD_ONENAND)
495 if (strcmp(argv
[2], "onenand") == 0) {
496 strcpy(ubi_dev
.dev_name
, "OneNAND");
497 ubi_dev
.type
= DEV_TYPE_ONENAND
;
498 ubi_dev
.mtd_info
= &onenand_mtd
;
502 if (ubi_dev
.type
== DEV_TYPE_NONE
) {
503 printf("Error, no UBI device/partition selected!\n");
507 strcpy(ubi_dev
.part_name
, argv
[3]);
508 err
= ubi_dev_scan(ubi_dev
.mtd_info
, ubi_dev
.part_name
);
510 printf("UBI init error %d\n", err
);
511 ubi_dev
.type
= DEV_TYPE_NONE
;
515 ubi
= ubi_devices
[0];
520 if ((strcmp(argv
[1], "part") != 0) && (ubi_dev
.type
== DEV_TYPE_NONE
)) {
521 printf("Error, no UBI device/partition selected!\n");
525 if (strcmp(argv
[1], "info") == 0) {
527 if (argc
> 2 && !strncmp(argv
[2], "l", 1))
529 return ubi_info(layout
);
532 if (strncmp(argv
[1], "create", 6) == 0) {
533 int dynamic
= 1; /* default: dynamic volume */
535 /* Use maximum available size */
538 /* E.g., create volume size type */
540 if (strncmp(argv
[4], "s", 1) == 0)
542 else if (strncmp(argv
[4], "d", 1) != 0) {
543 printf("Incorrect type\n");
548 /* E.g., create volume size */
550 size
= simple_strtoul(argv
[3], NULL
, 16);
553 /* Use maximum available size */
555 size
= ubi
->avail_pebs
* ubi
->leb_size
;
556 /* E.g., create volume */
558 return ubi_create_vol(argv
[2], size
, dynamic
);
561 if (strncmp(argv
[1], "remove", 6) == 0) {
562 /* E.g., remove volume */
564 return ubi_remove_vol(argv
[2]);
567 if (strncmp(argv
[1], "write", 5) == 0) {
569 printf("Please see usage\n");
573 addr
= simple_strtoul(argv
[2], NULL
, 16);
574 size
= simple_strtoul(argv
[4], NULL
, 16);
576 return ubi_volume_write(argv
[3], (void *)addr
, size
);
579 if (strncmp(argv
[1], "read", 4) == 0) {
582 /* E.g., read volume size */
584 size
= simple_strtoul(argv
[4], NULL
, 16);
588 /* E.g., read volume */
590 addr
= simple_strtoul(argv
[2], NULL
, 16);
595 return ubi_volume_read(argv
[3], (char *)addr
, size
);
598 printf("Please see usage\n");
602 U_BOOT_CMD(ubi
, 6, 1, do_ubi
,
604 "part [nand|nor|onenand] [part]"
605 " - Show or set current partition\n"
606 "ubi info [l[ayout]]"
607 " - Display volume and ubi layout information\n"
608 "ubi create[vol] volume [size] [type]"
609 " - create volume name with size\n"
610 "ubi write[vol] address volume size"
611 " - Write volume from address with size\n"
612 "ubi read[vol] address volume [size]"
613 " - Read volume to address with size\n"
614 "ubi remove[vol] volume"
617 " volume: charater name\n"
618 " size: KiB, MiB, GiB, and bytes\n"
619 " type: s[tatic] or d[ynamic] (default=dynamic)\n"