2 * Unsorted Block Image commands
4 * Copyright (C) 2008 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
7 * Copyright 2008-2009 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.
22 #include <onenand_uboot.h>
23 #include <dm/devres.h>
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/partitions.h>
26 #include <linux/err.h>
27 #include <ubi_uboot.h>
28 #include <linux/errno.h>
29 #include <jffs2/load_kernel.h>
30 #include <linux/log2.h>
33 #define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__)
35 /* Private own data */
36 static struct ubi_device
*ubi
;
38 #ifdef CONFIG_CMD_UBIFS
39 #include <ubifs_uboot.h>
42 static void display_volume_info(struct ubi_device
*ubi
)
46 for (i
= 0; i
< (ubi
->vtbl_slots
+ 1); i
++) {
48 continue; /* Empty record */
49 ubi_dump_vol_info(ubi
->volumes
[i
]);
53 static void display_ubi_info(struct ubi_device
*ubi
)
55 ubi_msg("MTD device name: \"%s\"", ubi
->mtd
->name
);
56 ubi_msg("MTD device size: %llu MiB", ubi
->flash_size
>> 20);
57 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
58 ubi
->peb_size
, ubi
->peb_size
>> 10);
59 ubi_msg("logical eraseblock size: %d bytes", ubi
->leb_size
);
60 ubi_msg("number of good PEBs: %d", ubi
->good_peb_count
);
61 ubi_msg("number of bad PEBs: %d", ubi
->bad_peb_count
);
62 ubi_msg("smallest flash I/O unit: %d", ubi
->min_io_size
);
63 ubi_msg("VID header offset: %d (aligned %d)",
64 ubi
->vid_hdr_offset
, ubi
->vid_hdr_aloffset
);
65 ubi_msg("data offset: %d", ubi
->leb_start
);
66 ubi_msg("max. allowed volumes: %d", ubi
->vtbl_slots
);
67 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD
);
68 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT
);
69 ubi_msg("number of user volumes: %d",
70 ubi
->vol_count
- UBI_INT_VOL_COUNT
);
71 ubi_msg("available PEBs: %d", ubi
->avail_pebs
);
72 ubi_msg("total number of reserved PEBs: %d", ubi
->rsvd_pebs
);
73 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
75 ubi_msg("max/mean erase counter: %d/%d", ubi
->max_ec
, ubi
->mean_ec
);
78 static int ubi_info(int layout
)
81 display_volume_info(ubi
);
83 display_ubi_info(ubi
);
88 static int ubi_list(const char *var
, int numeric
)
90 size_t namelen
, len
, size
;
95 for (i
= 0; i
< (ubi
->vtbl_slots
+ 1); i
++) {
98 if (ubi
->volumes
[i
]->vol_id
>= UBI_INTERNAL_VOL_START
)
101 ubi
->volumes
[i
]->vol_id
,
102 ubi
->volumes
[i
]->name
);
113 for (i
= 0; i
< (ubi
->vtbl_slots
+ 1); i
++) {
114 if (!ubi
->volumes
[i
])
116 if (ubi
->volumes
[i
]->vol_id
>= UBI_INTERNAL_VOL_START
)
120 namelen
= 10; /* strlen(stringify(INT_MAX)) */
122 namelen
= strlen(ubi
->volumes
[i
]->name
);
124 if (len
+ namelen
+ 1 > size
) {
125 size
= roundup_pow_of_two(len
+ namelen
+ 1) * 2;
126 str2
= realloc(str
, size
);
138 len
+= sprintf(str
+ len
, "%d", ubi
->volumes
[i
]->vol_id
) + 1;
140 memcpy(str
+ len
, ubi
->volumes
[i
]->name
, namelen
);
152 static int ubi_check_volumename(const struct ubi_volume
*vol
, char *name
)
154 return strcmp(vol
->name
, name
);
157 static int ubi_check(char *name
)
161 for (i
= 0; i
< (ubi
->vtbl_slots
+ 1); i
++) {
162 if (!ubi
->volumes
[i
])
163 continue; /* Empty record */
165 if (!ubi_check_volumename(ubi
->volumes
[i
], name
))
172 static int verify_mkvol_req(const struct ubi_device
*ubi
,
173 const struct ubi_mkvol_req
*req
)
177 if (req
->bytes
< 0 || req
->alignment
< 0 || req
->vol_type
< 0 ||
181 if ((req
->vol_id
< 0 || req
->vol_id
>= ubi
->vtbl_slots
) &&
182 req
->vol_id
!= UBI_VOL_NUM_AUTO
)
185 if (req
->alignment
== 0)
188 if (req
->bytes
== 0) {
189 printf("No space left in UBI device!\n");
194 if (req
->vol_type
!= UBI_DYNAMIC_VOLUME
&&
195 req
->vol_type
!= UBI_STATIC_VOLUME
)
198 if (req
->alignment
> ubi
->leb_size
)
201 n
= req
->alignment
% ubi
->min_io_size
;
202 if (req
->alignment
!= 1 && n
)
205 if (req
->name_len
> UBI_VOL_NAME_MAX
) {
206 printf("Name too long!\n");
216 static int ubi_create_vol(char *volume
, int64_t size
, int dynamic
, int vol_id
,
219 struct ubi_mkvol_req req
;
223 req
.vol_type
= UBI_DYNAMIC_VOLUME
;
225 req
.vol_type
= UBI_STATIC_VOLUME
;
231 strcpy(req
.name
, volume
);
232 req
.name_len
= strlen(volume
);
233 req
.name
[req
.name_len
] = '\0';
236 req
.flags
|= UBI_VOL_SKIP_CRC_CHECK_FLG
;
238 /* It's duplicated at drivers/mtd/ubi/cdev.c */
239 err
= verify_mkvol_req(ubi
, &req
);
241 printf("verify_mkvol_req failed %d\n", err
);
244 printf("Creating %s volume %s of size %lld\n",
245 dynamic
? "dynamic" : "static", volume
, size
);
246 /* Call real ubi create volume */
247 return ubi_create_volume(ubi
, &req
);
250 static struct ubi_volume
*ubi_find_volume(char *volume
)
252 struct ubi_volume
*vol
= NULL
;
255 for (i
= 0; i
< ubi
->vtbl_slots
; i
++) {
256 vol
= ubi
->volumes
[i
];
257 if (vol
&& !strcmp(vol
->name
, volume
))
261 printf("Volume %s not found!\n", volume
);
265 static int ubi_remove_vol(char *volume
)
267 int err
, reserved_pebs
, i
;
268 struct ubi_volume
*vol
;
270 vol
= ubi_find_volume(volume
);
274 printf("Remove UBI volume %s (id %d)\n", vol
->name
, vol
->vol_id
);
277 printf("It's read-only mode\n");
282 err
= ubi_change_vtbl_record(ubi
, vol
->vol_id
, NULL
);
284 printf("Error changing Vol tabel record err=%x\n", err
);
287 reserved_pebs
= vol
->reserved_pebs
;
288 for (i
= 0; i
< vol
->reserved_pebs
; i
++) {
289 err
= ubi_eba_unmap_leb(ubi
, vol
, i
);
295 ubi
->volumes
[vol
->vol_id
]->eba_tbl
= NULL
;
296 ubi
->volumes
[vol
->vol_id
] = NULL
;
298 ubi
->rsvd_pebs
-= reserved_pebs
;
299 ubi
->avail_pebs
+= reserved_pebs
;
300 i
= ubi
->beb_rsvd_level
- ubi
->beb_rsvd_pebs
;
302 i
= ubi
->avail_pebs
>= i
? i
: ubi
->avail_pebs
;
303 ubi
->avail_pebs
-= i
;
305 ubi
->beb_rsvd_pebs
+= i
;
307 ubi_msg("reserve more %d PEBs", i
);
313 ubi_err(ubi
, "cannot remove volume %s, error %d", volume
, err
);
319 static int ubi_rename_vol(char *oldname
, char *newname
)
321 struct ubi_volume
*vol
;
322 struct ubi_rename_entry rename
;
323 struct ubi_volume_desc desc
;
324 struct list_head list
;
326 vol
= ubi_find_volume(oldname
);
328 printf("%s: volume %s doesn't exist\n", __func__
, oldname
);
332 if (!ubi_check(newname
)) {
333 printf("%s: volume %s already exist\n", __func__
, newname
);
337 printf("Rename UBI volume %s to %s\n", oldname
, newname
);
340 printf("%s: ubi device is in read-only mode\n", __func__
);
344 rename
.new_name_len
= strlen(newname
);
345 strcpy(rename
.new_name
, newname
);
350 INIT_LIST_HEAD(&rename
.list
);
351 INIT_LIST_HEAD(&list
);
352 list_add(&rename
.list
, &list
);
354 return ubi_rename_volumes(ubi
, &list
);
357 static int ubi_volume_continue_write(char *volume
, void *buf
, size_t size
)
360 struct ubi_volume
*vol
;
362 vol
= ubi_find_volume(volume
);
366 err
= ubi_more_update_data(ubi
, vol
, buf
, size
);
368 printf("Couldnt or partially wrote data\n");
375 err
= ubi_check_volume(ubi
, vol
->vol_id
);
380 ubi_warn(ubi
, "volume %d on UBI device %d is corrupt",
381 vol
->vol_id
, ubi
->ubi_num
);
386 ubi_gluebi_updated(vol
);
392 int ubi_volume_begin_write(char *volume
, void *buf
, size_t size
,
397 struct ubi_volume
*vol
;
399 vol
= ubi_find_volume(volume
);
403 rsvd_bytes
= vol
->reserved_pebs
* (ubi
->leb_size
- vol
->data_pad
);
404 if (size
> rsvd_bytes
) {
405 printf("size > volume size! Aborting!\n");
409 err
= ubi_start_update(ubi
, vol
, full_size
);
411 printf("Cannot start volume update\n");
415 return ubi_volume_continue_write(volume
, buf
, size
);
418 int ubi_volume_write(char *volume
, void *buf
, size_t size
)
420 return ubi_volume_begin_write(volume
, buf
, size
, size
);
423 int ubi_volume_read(char *volume
, char *buf
, size_t size
)
425 int err
, lnum
, off
, len
, tbuf_size
;
427 unsigned long long tmp
;
428 struct ubi_volume
*vol
;
432 vol
= ubi_find_volume(volume
);
440 if (vol
->upd_marker
) {
441 printf("damaged volume, update marker is set");
444 if (offp
== vol
->used_bytes
)
448 printf("No size specified -> Using max size (%lld)\n", vol
->used_bytes
);
449 size
= vol
->used_bytes
;
452 printf("Read %zu bytes from volume %s to %p\n", size
, volume
, buf
);
455 printf("read from corrupted volume %d", vol
->vol_id
);
456 if (offp
+ size
> vol
->used_bytes
)
457 size
= vol
->used_bytes
- offp
;
459 tbuf_size
= vol
->usable_leb_size
;
460 if (size
< tbuf_size
)
461 tbuf_size
= ALIGN(size
, ubi
->min_io_size
);
462 tbuf
= malloc_cache_aligned(tbuf_size
);
467 len
= size
> tbuf_size
? tbuf_size
: size
;
470 off
= do_div(tmp
, vol
->usable_leb_size
);
474 if (off
+ len
>= vol
->usable_leb_size
)
475 len
= vol
->usable_leb_size
- off
;
477 err
= ubi_eba_read_leb(ubi
, vol
, lnum
, tbuf
, off
, len
, 0);
479 printf("read err %x\n", err
);
484 if (off
== vol
->usable_leb_size
) {
486 off
-= vol
->usable_leb_size
;
492 memcpy(buf
, tbuf
, len
);
495 len
= size
> tbuf_size
? tbuf_size
: size
;
499 env_set_hex("filesize", len_read
);
505 static int ubi_dev_scan(struct mtd_info
*info
, const char *vid_header_offset
)
507 char ubi_mtd_param_buffer
[80];
510 if (!vid_header_offset
)
511 sprintf(ubi_mtd_param_buffer
, "%s", info
->name
);
513 sprintf(ubi_mtd_param_buffer
, "%s,%s", info
->name
,
516 err
= ubi_mtd_param_parse(ubi_mtd_param_buffer
, NULL
);
527 static int ubi_set_skip_check(char *volume
, bool skip_check
)
529 struct ubi_vtbl_record vtbl_rec
;
530 struct ubi_volume
*vol
;
532 vol
= ubi_find_volume(volume
);
536 printf("%sing skip_check on volume %s\n",
537 skip_check
? "Sett" : "Clear", volume
);
539 vtbl_rec
= ubi
->vtbl
[vol
->vol_id
];
541 vtbl_rec
.flags
|= UBI_VTBL_SKIP_CRC_CHECK_FLG
;
544 vtbl_rec
.flags
&= ~UBI_VTBL_SKIP_CRC_CHECK_FLG
;
548 return ubi_change_vtbl_record(ubi
, vol
->vol_id
, &vtbl_rec
);
551 static int ubi_detach(void)
553 #ifdef CONFIG_CMD_UBIFS
555 * Automatically unmount UBIFS partition when user
556 * changes the UBI device. Otherwise the following
557 * UBIFS commands will crash.
559 if (ubifs_is_mounted())
564 * Call ubi_exit() before re-initializing the UBI subsystem
574 int ubi_part(char *part_name
, const char *vid_header_offset
)
576 struct mtd_info
*mtd
;
579 if (ubi
&& ubi
->mtd
&& !strcmp(ubi
->mtd
->name
, part_name
)) {
580 printf("UBI partition '%s' already selected\n", part_name
);
587 mtd
= get_mtd_device_nm(part_name
);
589 printf("Partition %s not found!\n", part_name
);
594 err
= ubi_dev_scan(mtd
, vid_header_offset
);
596 printf("UBI init error %d\n", err
);
597 printf("Please check, if the correct MTD partition is used (size big enough?)\n");
601 ubi
= ubi_devices
[0];
606 static int do_ubi(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[])
610 bool skipcheck
= false;
613 return CMD_RET_USAGE
;
615 if (strcmp(argv
[1], "detach") == 0)
618 if (strcmp(argv
[1], "part") == 0) {
619 const char *vid_header_offset
= NULL
;
621 /* Print current partition */
624 printf("Error, no UBI device selected!\n");
628 printf("Device %d: %s, MTD partition %s\n",
629 ubi
->ubi_num
, ubi
->ubi_name
, ubi
->mtd
->name
);
634 return CMD_RET_USAGE
;
637 vid_header_offset
= argv
[3];
639 return ubi_part(argv
[2], vid_header_offset
);
642 if ((strcmp(argv
[1], "part") != 0) && !ubi
) {
643 printf("Error, no UBI device selected!\n");
647 if (strcmp(argv
[1], "info") == 0) {
649 if (argc
> 2 && !strncmp(argv
[2], "l", 1))
651 return ubi_info(layout
);
654 if (strcmp(argv
[1], "list") == 0) {
656 if (argc
>= 3 && argv
[2][0] == '-') {
657 if (strcmp(argv
[2], "-numeric") == 0)
660 return CMD_RET_USAGE
;
662 if (!numeric
&& argc
!= 2 && argc
!= 3)
663 return CMD_RET_USAGE
;
664 if (numeric
&& argc
!= 3 && argc
!= 4)
665 return CMD_RET_USAGE
;
666 return ubi_list(argv
[numeric
? 3 : 2], numeric
);
669 if (strcmp(argv
[1], "check") == 0) {
671 return ubi_check(argv
[2]);
673 printf("Error, no volume name passed\n");
677 if (strncmp(argv
[1], "create", 6) == 0) {
678 int dynamic
= 1; /* default: dynamic volume */
679 int id
= UBI_VOL_NUM_AUTO
;
681 /* Use maximum available size */
684 /* E.g., create volume with "skipcheck" bit set */
686 skipcheck
= strncmp(argv
[6], "--skipcheck", 11) == 0;
690 /* E.g., create volume size type vol_id */
692 id
= simple_strtoull(argv
[5], NULL
, 16);
696 /* E.g., create volume size type */
698 if (strncmp(argv
[4], "s", 1) == 0)
700 else if (strncmp(argv
[4], "d", 1) != 0) {
701 printf("Incorrect type\n");
706 /* E.g., create volume size */
708 if (argv
[3][0] != '-')
709 size
= simple_strtoull(argv
[3], NULL
, 16);
712 /* Use maximum available size */
714 size
= (int64_t)ubi
->avail_pebs
* ubi
->leb_size
;
715 printf("No size specified -> Using max size (%lld)\n", size
);
717 /* E.g., create volume */
719 return ubi_create_vol(argv
[2], size
, dynamic
, id
,
724 if (strncmp(argv
[1], "remove", 6) == 0) {
725 /* E.g., remove volume */
727 return ubi_remove_vol(argv
[2]);
730 if (IS_ENABLED(CONFIG_CMD_UBI_RENAME
) && !strncmp(argv
[1], "rename", 6))
731 return ubi_rename_vol(argv
[2], argv
[3]);
733 if (strncmp(argv
[1], "skipcheck", 9) == 0) {
734 /* E.g., change skip_check flag */
736 skipcheck
= strncmp(argv
[3], "on", 2) == 0;
737 return ubi_set_skip_check(argv
[2], skipcheck
);
741 if (strncmp(argv
[1], "write", 5) == 0) {
745 printf("Please see usage\n");
749 addr
= hextoul(argv
[2], NULL
);
750 size
= hextoul(argv
[4], NULL
);
752 if (strlen(argv
[1]) == 10 &&
753 strncmp(argv
[1] + 5, ".part", 5) == 0) {
755 ret
= ubi_volume_continue_write(argv
[3],
759 full_size
= hextoul(argv
[5], NULL
);
760 ret
= ubi_volume_begin_write(argv
[3],
761 (void *)addr
, size
, full_size
);
764 ret
= ubi_volume_write(argv
[3], (void *)addr
, size
);
767 printf("%lld bytes written to volume %s\n", size
,
774 if (strncmp(argv
[1], "read", 4) == 0) {
777 /* E.g., read volume size */
779 size
= hextoul(argv
[4], NULL
);
783 /* E.g., read volume */
785 addr
= hextoul(argv
[2], NULL
);
790 return ubi_volume_read(argv
[3], (char *)addr
, size
);
794 printf("Please see usage\n");
802 " - detach ubi from a mtd partition\n"
803 "ubi part [part] [offset]\n"
804 " - Show or set current partition (with optional VID"
806 "ubi info [l[ayout]]"
807 " - Display volume and ubi layout information\n"
809 " - print the list of volumes\n"
810 "ubi list [flags] <varname>"
811 " - set environment variable to the list of volumes"
812 " (flags can be -numeric)\n"
813 "ubi check volumename"
814 " - check if volumename exists\n"
815 "ubi create[vol] volume [size] [type] [id] [--skipcheck]\n"
816 " - create volume name with size ('-' for maximum"
818 "ubi write[vol] address volume size"
819 " - Write volume from address with size\n"
820 "ubi write.part address volume size [fullsize]\n"
821 " - Write part of a volume from address\n"
822 "ubi read[vol] address volume [size]"
823 " - Read volume to address with size\n"
824 "ubi remove[vol] volume"
826 #if IS_ENABLED(CONFIG_CMD_UBI_RENAME)
827 "ubi rename oldname newname\n"
829 "ubi skipcheck volume on/off - Set or clear skip_check flag in volume header\n"
831 " volume: character name\n"
832 " size: specified in bytes\n"
833 " type: s[tatic] or d[ynamic] (default=dynamic)"