2 * Unsorted Block Image commands
4 * Copyright (C) 2008 Samsung Electronics
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.
21 #include <onenand_uboot.h>
22 #include <dm/devres.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/partitions.h>
25 #include <linux/err.h>
26 #include <ubi_uboot.h>
27 #include <linux/errno.h>
28 #include <jffs2/load_kernel.h>
29 #include <linux/log2.h>
32 #define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__)
34 /* Private own data */
35 static struct ubi_device *ubi;
37 #ifdef CONFIG_CMD_UBIFS
38 #include <ubifs_uboot.h>
41 static void display_volume_info(struct ubi_device *ubi)
45 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
47 continue; /* Empty record */
48 ubi_dump_vol_info(ubi->volumes[i]);
52 static void display_ubi_info(struct ubi_device *ubi)
54 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
55 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
56 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
57 ubi->peb_size, ubi->peb_size >> 10);
58 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
59 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
60 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
61 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
62 ubi_msg("VID header offset: %d (aligned %d)",
63 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
64 ubi_msg("data offset: %d", ubi->leb_start);
65 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
66 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
67 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
68 ubi_msg("number of user volumes: %d",
69 ubi->vol_count - UBI_INT_VOL_COUNT);
70 ubi_msg("available PEBs: %d", ubi->avail_pebs);
71 ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
72 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
74 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
77 static int ubi_info(int layout)
80 display_volume_info(ubi);
82 display_ubi_info(ubi);
87 static int ubi_list(const char *var, int numeric)
89 size_t namelen, len, size;
94 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
97 if (ubi->volumes[i]->vol_id >= UBI_INTERNAL_VOL_START)
100 ubi->volumes[i]->vol_id,
101 ubi->volumes[i]->name);
112 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
113 if (!ubi->volumes[i])
115 if (ubi->volumes[i]->vol_id >= UBI_INTERNAL_VOL_START)
119 namelen = 10; /* strlen(stringify(INT_MAX)) */
121 namelen = strlen(ubi->volumes[i]->name);
123 if (len + namelen + 1 > size) {
124 size = roundup_pow_of_two(len + namelen + 1) * 2;
125 str2 = realloc(str, size);
137 len += sprintf(str + len, "%d", ubi->volumes[i]->vol_id) + 1;
139 memcpy(str + len, ubi->volumes[i]->name, namelen);
151 static int ubi_check_volumename(const struct ubi_volume *vol, char *name)
153 return strcmp(vol->name, name);
156 static int ubi_check(char *name)
160 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
161 if (!ubi->volumes[i])
162 continue; /* Empty record */
164 if (!ubi_check_volumename(ubi->volumes[i], name))
171 static int verify_mkvol_req(const struct ubi_device *ubi,
172 const struct ubi_mkvol_req *req)
176 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
180 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
181 req->vol_id != UBI_VOL_NUM_AUTO)
184 if (req->alignment == 0)
187 if (req->bytes == 0) {
188 printf("No space left in UBI device!\n");
193 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
194 req->vol_type != UBI_STATIC_VOLUME)
197 if (req->alignment > ubi->leb_size)
200 n = req->alignment % ubi->min_io_size;
201 if (req->alignment != 1 && n)
204 if (req->name_len > UBI_VOL_NAME_MAX) {
205 printf("Name too long!\n");
215 static int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id,
218 struct ubi_mkvol_req req;
222 req.vol_type = UBI_DYNAMIC_VOLUME;
224 req.vol_type = UBI_STATIC_VOLUME;
230 strcpy(req.name, volume);
231 req.name_len = strlen(volume);
232 req.name[req.name_len] = '\0';
235 req.flags |= UBI_VOL_SKIP_CRC_CHECK_FLG;
237 /* It's duplicated at drivers/mtd/ubi/cdev.c */
238 err = verify_mkvol_req(ubi, &req);
240 printf("verify_mkvol_req failed %d\n", err);
243 printf("Creating %s volume %s of size %lld\n",
244 dynamic ? "dynamic" : "static", volume, size);
245 /* Call real ubi create volume */
246 return ubi_create_volume(ubi, &req);
249 static struct ubi_volume *ubi_find_volume(char *volume)
251 struct ubi_volume *vol;
254 for (i = 0; i < ubi->vtbl_slots; i++) {
255 vol = ubi->volumes[i];
256 if (vol && !strcmp(vol->name, volume))
260 printf("Volume %s not found!\n", volume);
264 static int ubi_remove_vol(char *volume)
266 int err, reserved_pebs, i;
267 struct ubi_volume *vol;
269 vol = ubi_find_volume(volume);
273 printf("Remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
276 printf("It's read-only mode\n");
281 err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL);
283 printf("Error changing Vol tabel record err=%x\n", err);
286 reserved_pebs = vol->reserved_pebs;
287 for (i = 0; i < vol->reserved_pebs; i++) {
288 err = ubi_eba_unmap_leb(ubi, vol, i);
294 ubi->volumes[vol->vol_id]->eba_tbl = NULL;
295 ubi->volumes[vol->vol_id] = NULL;
297 ubi->rsvd_pebs -= reserved_pebs;
298 ubi->avail_pebs += reserved_pebs;
299 i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
301 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
302 ubi->avail_pebs -= i;
304 ubi->beb_rsvd_pebs += i;
306 ubi_msg("reserve more %d PEBs", i);
312 ubi_err(ubi, "cannot remove volume %s, error %d", volume, err);
318 static int ubi_rename_vol(char *oldname, char *newname)
320 struct ubi_volume *vol;
321 struct ubi_rename_entry rename;
322 struct ubi_volume_desc desc;
323 struct list_head list;
325 vol = ubi_find_volume(oldname);
327 printf("%s: volume %s doesn't exist\n", __func__, oldname);
331 if (!ubi_check(newname)) {
332 printf("%s: volume %s already exist\n", __func__, newname);
336 printf("Rename UBI volume %s to %s\n", oldname, newname);
339 printf("%s: ubi device is in read-only mode\n", __func__);
343 rename.new_name_len = strlen(newname);
344 strcpy(rename.new_name, newname);
349 INIT_LIST_HEAD(&rename.list);
350 INIT_LIST_HEAD(&list);
351 list_add(&rename.list, &list);
353 return ubi_rename_volumes(ubi, &list);
356 static int ubi_volume_continue_write(char *volume, void *buf, size_t size)
359 struct ubi_volume *vol;
361 vol = ubi_find_volume(volume);
365 if (!vol->updating) {
366 printf("UBI volume update was not initiated\n");
370 err = ubi_more_update_data(ubi, vol, buf, size);
372 printf("Couldnt or partially wrote data\n");
379 err = ubi_check_volume(ubi, vol->vol_id);
384 ubi_warn(ubi, "volume %d on UBI device %d is corrupt",
385 vol->vol_id, ubi->ubi_num);
390 ubi_gluebi_updated(vol);
396 int ubi_volume_begin_write(char *volume, void *buf, size_t size,
401 struct ubi_volume *vol;
403 vol = ubi_find_volume(volume);
407 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
408 if (size > rsvd_bytes) {
409 printf("size > volume size! Aborting!\n");
413 err = ubi_start_update(ubi, vol, full_size);
415 printf("Cannot start volume update\n");
419 /* The volume is just wiped out */
423 return ubi_volume_continue_write(volume, buf, size);
426 int ubi_volume_write(char *volume, void *buf, size_t size)
428 return ubi_volume_begin_write(volume, buf, size, size);
431 int ubi_volume_read(char *volume, char *buf, size_t size)
433 int err, lnum, off, len, tbuf_size;
435 unsigned long long tmp;
436 struct ubi_volume *vol;
440 vol = ubi_find_volume(volume);
448 if (vol->upd_marker) {
449 printf("damaged volume, update marker is set");
452 if (offp == vol->used_bytes)
456 printf("No size specified -> Using max size (%lld)\n", vol->used_bytes);
457 size = vol->used_bytes;
460 printf("Read %zu bytes from volume %s to %p\n", size, volume, buf);
463 printf("read from corrupted volume %d", vol->vol_id);
464 if (offp + size > vol->used_bytes)
465 size = vol->used_bytes - offp;
467 tbuf_size = vol->usable_leb_size;
468 if (size < tbuf_size)
469 tbuf_size = ALIGN(size, ubi->min_io_size);
470 tbuf = malloc_cache_aligned(tbuf_size);
475 len = size > tbuf_size ? tbuf_size : size;
478 off = do_div(tmp, vol->usable_leb_size);
482 if (off + len >= vol->usable_leb_size)
483 len = vol->usable_leb_size - off;
485 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
487 printf("read err %x\n", err);
492 if (off == vol->usable_leb_size) {
494 off -= vol->usable_leb_size;
500 memcpy(buf, tbuf, len);
503 len = size > tbuf_size ? tbuf_size : size;
507 env_set_hex("filesize", len_read);
513 static int ubi_dev_scan(struct mtd_info *info, const char *vid_header_offset)
515 char ubi_mtd_param_buffer[80];
518 if (!vid_header_offset)
519 sprintf(ubi_mtd_param_buffer, "%s", info->name);
521 sprintf(ubi_mtd_param_buffer, "%s,%s", info->name,
524 err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
535 static int ubi_set_skip_check(char *volume, bool skip_check)
537 struct ubi_vtbl_record vtbl_rec;
538 struct ubi_volume *vol;
540 vol = ubi_find_volume(volume);
544 printf("%sing skip_check on volume %s\n",
545 skip_check ? "Sett" : "Clear", volume);
547 vtbl_rec = ubi->vtbl[vol->vol_id];
549 vtbl_rec.flags |= UBI_VTBL_SKIP_CRC_CHECK_FLG;
552 vtbl_rec.flags &= ~UBI_VTBL_SKIP_CRC_CHECK_FLG;
556 return ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
559 static int ubi_detach(void)
561 #ifdef CONFIG_CMD_UBIFS
563 * Automatically unmount UBIFS partition when user
564 * changes the UBI device. Otherwise the following
565 * UBIFS commands will crash.
567 if (ubifs_is_mounted())
572 * Call ubi_exit() before re-initializing the UBI subsystem
582 int ubi_part(char *part_name, const char *vid_header_offset)
584 struct mtd_info *mtd;
587 if (ubi && ubi->mtd && !strcmp(ubi->mtd->name, part_name)) {
588 printf("UBI partition '%s' already selected\n", part_name);
595 mtd = get_mtd_device_nm(part_name);
597 printf("Partition %s not found!\n", part_name);
602 err = ubi_dev_scan(mtd, vid_header_offset);
604 printf("UBI init error %d\n", err);
605 printf("Please check, if the correct MTD partition is used (size big enough?)\n");
609 ubi = ubi_devices[0];
614 static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
618 bool skipcheck = false;
621 return CMD_RET_USAGE;
623 if (strcmp(argv[1], "detach") == 0)
626 if (strcmp(argv[1], "part") == 0) {
627 const char *vid_header_offset = NULL;
629 /* Print current partition */
632 printf("Error, no UBI device selected!\n");
636 printf("Device %d: %s, MTD partition %s\n",
637 ubi->ubi_num, ubi->ubi_name, ubi->mtd->name);
642 return CMD_RET_USAGE;
645 vid_header_offset = argv[3];
647 return ubi_part(argv[2], vid_header_offset);
650 if ((strcmp(argv[1], "part") != 0) && !ubi) {
651 printf("Error, no UBI device selected!\n");
655 if (strcmp(argv[1], "info") == 0) {
657 if (argc > 2 && !strncmp(argv[2], "l", 1))
659 return ubi_info(layout);
662 if (strcmp(argv[1], "list") == 0) {
664 if (argc >= 3 && argv[2][0] == '-') {
665 if (strcmp(argv[2], "-numeric") == 0)
668 return CMD_RET_USAGE;
670 if (!numeric && argc != 2 && argc != 3)
671 return CMD_RET_USAGE;
672 if (numeric && argc != 3 && argc != 4)
673 return CMD_RET_USAGE;
674 return ubi_list(argv[numeric ? 3 : 2], numeric);
677 if (strcmp(argv[1], "check") == 0) {
679 return ubi_check(argv[2]);
681 printf("Error, no volume name passed\n");
685 if (strncmp(argv[1], "create", 6) == 0) {
686 int dynamic = 1; /* default: dynamic volume */
687 int id = UBI_VOL_NUM_AUTO;
689 /* Use maximum available size */
692 /* E.g., create volume with "skipcheck" bit set */
694 skipcheck = strncmp(argv[6], "--skipcheck", 11) == 0;
698 /* E.g., create volume size type vol_id */
700 id = simple_strtoull(argv[5], NULL, 16);
704 /* E.g., create volume size type */
706 if (strncmp(argv[4], "s", 1) == 0)
708 else if (strncmp(argv[4], "d", 1) != 0) {
709 printf("Incorrect type\n");
714 /* E.g., create volume size */
716 if (argv[3][0] != '-')
717 size = simple_strtoull(argv[3], NULL, 16);
720 /* Use maximum available size */
722 size = (int64_t)ubi->avail_pebs * ubi->leb_size;
723 printf("No size specified -> Using max size (%lld)\n", size);
725 /* E.g., create volume */
727 return ubi_create_vol(argv[2], size, dynamic, id,
732 if (strncmp(argv[1], "remove", 6) == 0) {
733 /* E.g., remove volume */
735 return ubi_remove_vol(argv[2]);
738 if (IS_ENABLED(CONFIG_CMD_UBI_RENAME) && !strncmp(argv[1], "rename", 6))
739 return ubi_rename_vol(argv[2], argv[3]);
741 if (strncmp(argv[1], "skipcheck", 9) == 0) {
742 /* E.g., change skip_check flag */
744 skipcheck = strncmp(argv[3], "on", 2) == 0;
745 return ubi_set_skip_check(argv[2], skipcheck);
749 if (strncmp(argv[1], "write", 5) == 0) {
753 printf("Please see usage\n");
757 addr = hextoul(argv[2], NULL);
758 size = hextoul(argv[4], NULL);
760 if (strlen(argv[1]) == 10 &&
761 strncmp(argv[1] + 5, ".part", 5) == 0) {
763 ret = ubi_volume_continue_write(argv[3],
767 full_size = hextoul(argv[5], NULL);
768 ret = ubi_volume_begin_write(argv[3],
769 (void *)addr, size, full_size);
772 ret = ubi_volume_write(argv[3], (void *)addr, size);
775 printf("%lld bytes written to volume %s\n", size,
782 if (strncmp(argv[1], "read", 4) == 0) {
785 /* E.g., read volume size */
787 size = hextoul(argv[4], NULL);
791 /* E.g., read volume */
793 addr = hextoul(argv[2], NULL);
798 return ubi_volume_read(argv[3], (char *)addr, size);
802 printf("Please see usage\n");
810 " - detach ubi from a mtd partition\n"
811 "ubi part [part] [offset]\n"
812 " - Show or set current partition (with optional VID"
814 "ubi info [l[ayout]]"
815 " - Display volume and ubi layout information\n"
817 " - print the list of volumes\n"
818 "ubi list [flags] <varname>"
819 " - set environment variable to the list of volumes"
820 " (flags can be -numeric)\n"
821 "ubi check volumename"
822 " - check if volumename exists\n"
823 "ubi create[vol] volume [size] [type] [id] [--skipcheck]\n"
824 " - create volume name with size ('-' for maximum"
826 "ubi write[vol] address volume size"
827 " - Write volume from address with size\n"
828 "ubi write.part address volume size [fullsize]\n"
829 " - Write part of a volume from address\n"
830 "ubi read[vol] address volume [size]"
831 " - Read volume to address with size\n"
832 "ubi remove[vol] volume"
834 #if IS_ENABLED(CONFIG_CMD_UBI_RENAME)
835 "ubi rename oldname newname\n"
837 "ubi skipcheck volume on/off - Set or clear skip_check flag in volume header\n"
839 " volume: character name\n"
840 " size: specified in bytes\n"
841 " type: s[tatic] or d[ynamic] (default=dynamic)"