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>
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_check_volumename(const struct ubi_volume *vol, char *name)
89 return strcmp(vol->name, name);
92 static int ubi_check(char *name)
96 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
98 continue; /* Empty record */
100 if (!ubi_check_volumename(ubi->volumes[i], name))
107 static int verify_mkvol_req(const struct ubi_device *ubi,
108 const struct ubi_mkvol_req *req)
112 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
116 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
117 req->vol_id != UBI_VOL_NUM_AUTO)
120 if (req->alignment == 0)
123 if (req->bytes == 0) {
124 printf("No space left in UBI device!\n");
129 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
130 req->vol_type != UBI_STATIC_VOLUME)
133 if (req->alignment > ubi->leb_size)
136 n = req->alignment % ubi->min_io_size;
137 if (req->alignment != 1 && n)
140 if (req->name_len > UBI_VOL_NAME_MAX) {
141 printf("Name too long!\n");
151 static int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id,
154 struct ubi_mkvol_req req;
158 req.vol_type = UBI_DYNAMIC_VOLUME;
160 req.vol_type = UBI_STATIC_VOLUME;
166 strcpy(req.name, volume);
167 req.name_len = strlen(volume);
168 req.name[req.name_len] = '\0';
171 req.flags |= UBI_VOL_SKIP_CRC_CHECK_FLG;
173 /* It's duplicated at drivers/mtd/ubi/cdev.c */
174 err = verify_mkvol_req(ubi, &req);
176 printf("verify_mkvol_req failed %d\n", err);
179 printf("Creating %s volume %s of size %lld\n",
180 dynamic ? "dynamic" : "static", volume, size);
181 /* Call real ubi create volume */
182 return ubi_create_volume(ubi, &req);
185 static struct ubi_volume *ubi_find_volume(char *volume)
187 struct ubi_volume *vol = NULL;
190 for (i = 0; i < ubi->vtbl_slots; i++) {
191 vol = ubi->volumes[i];
192 if (vol && !strcmp(vol->name, volume))
196 printf("Volume %s not found!\n", volume);
200 static int ubi_remove_vol(char *volume)
202 int err, reserved_pebs, i;
203 struct ubi_volume *vol;
205 vol = ubi_find_volume(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->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->vol_id]->eba_tbl = NULL;
231 ubi->volumes[vol->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(ubi, "cannot remove volume %s, error %d", volume, err);
254 static int ubi_rename_vol(char *oldname, char *newname)
256 struct ubi_volume *vol;
257 struct ubi_rename_entry rename;
258 struct ubi_volume_desc desc;
259 struct list_head list;
261 vol = ubi_find_volume(oldname);
263 printf("%s: volume %s doesn't exist\n", __func__, oldname);
267 if (!ubi_check(newname)) {
268 printf("%s: volume %s already exist\n", __func__, newname);
272 printf("Rename UBI volume %s to %s\n", oldname, newname);
275 printf("%s: ubi device is in read-only mode\n", __func__);
279 rename.new_name_len = strlen(newname);
280 strcpy(rename.new_name, newname);
285 INIT_LIST_HEAD(&rename.list);
286 INIT_LIST_HEAD(&list);
287 list_add(&rename.list, &list);
289 return ubi_rename_volumes(ubi, &list);
292 static int ubi_volume_continue_write(char *volume, void *buf, size_t size)
295 struct ubi_volume *vol;
297 vol = ubi_find_volume(volume);
301 err = ubi_more_update_data(ubi, vol, buf, size);
303 printf("Couldnt or partially wrote data\n");
310 err = ubi_check_volume(ubi, vol->vol_id);
315 ubi_warn(ubi, "volume %d on UBI device %d is corrupt",
316 vol->vol_id, ubi->ubi_num);
321 ubi_gluebi_updated(vol);
327 int ubi_volume_begin_write(char *volume, void *buf, size_t size,
332 struct ubi_volume *vol;
334 vol = ubi_find_volume(volume);
338 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
339 if (size > rsvd_bytes) {
340 printf("size > volume size! Aborting!\n");
344 err = ubi_start_update(ubi, vol, full_size);
346 printf("Cannot start volume update\n");
350 return ubi_volume_continue_write(volume, buf, size);
353 int ubi_volume_write(char *volume, void *buf, size_t size)
355 return ubi_volume_begin_write(volume, buf, size, size);
358 int ubi_volume_read(char *volume, char *buf, size_t size)
360 int err, lnum, off, len, tbuf_size;
362 unsigned long long tmp;
363 struct ubi_volume *vol;
367 vol = ubi_find_volume(volume);
375 if (vol->upd_marker) {
376 printf("damaged volume, update marker is set");
379 if (offp == vol->used_bytes)
383 printf("No size specified -> Using max size (%lld)\n", vol->used_bytes);
384 size = vol->used_bytes;
387 printf("Read %zu bytes from volume %s to %p\n", size, volume, buf);
390 printf("read from corrupted volume %d", vol->vol_id);
391 if (offp + size > vol->used_bytes)
392 size = vol->used_bytes - offp;
394 tbuf_size = vol->usable_leb_size;
395 if (size < tbuf_size)
396 tbuf_size = ALIGN(size, ubi->min_io_size);
397 tbuf = malloc_cache_aligned(tbuf_size);
402 len = size > tbuf_size ? tbuf_size : size;
405 off = do_div(tmp, vol->usable_leb_size);
409 if (off + len >= vol->usable_leb_size)
410 len = vol->usable_leb_size - off;
412 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
414 printf("read err %x\n", err);
419 if (off == vol->usable_leb_size) {
421 off -= vol->usable_leb_size;
427 memcpy(buf, tbuf, len);
430 len = size > tbuf_size ? tbuf_size : size;
434 env_set_hex("filesize", len_read);
440 static int ubi_dev_scan(struct mtd_info *info, const char *vid_header_offset)
442 char ubi_mtd_param_buffer[80];
445 if (!vid_header_offset)
446 sprintf(ubi_mtd_param_buffer, "%s", info->name);
448 sprintf(ubi_mtd_param_buffer, "%s,%s", info->name,
451 err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
462 static int ubi_set_skip_check(char *volume, bool skip_check)
464 struct ubi_vtbl_record vtbl_rec;
465 struct ubi_volume *vol;
467 vol = ubi_find_volume(volume);
471 printf("%sing skip_check on volume %s\n",
472 skip_check ? "Sett" : "Clear", volume);
474 vtbl_rec = ubi->vtbl[vol->vol_id];
476 vtbl_rec.flags |= UBI_VTBL_SKIP_CRC_CHECK_FLG;
479 vtbl_rec.flags &= ~UBI_VTBL_SKIP_CRC_CHECK_FLG;
483 return ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
486 static int ubi_detach(void)
488 #ifdef CONFIG_CMD_UBIFS
490 * Automatically unmount UBIFS partition when user
491 * changes the UBI device. Otherwise the following
492 * UBIFS commands will crash.
494 if (ubifs_is_mounted())
499 * Call ubi_exit() before re-initializing the UBI subsystem
509 int ubi_part(char *part_name, const char *vid_header_offset)
511 struct mtd_info *mtd;
517 mtd = get_mtd_device_nm(part_name);
519 printf("Partition %s not found!\n", part_name);
524 err = ubi_dev_scan(mtd, vid_header_offset);
526 printf("UBI init error %d\n", err);
527 printf("Please check, if the correct MTD partition is used (size big enough?)\n");
531 ubi = ubi_devices[0];
536 static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
540 bool skipcheck = false;
543 return CMD_RET_USAGE;
545 if (strcmp(argv[1], "detach") == 0)
548 if (strcmp(argv[1], "part") == 0) {
549 const char *vid_header_offset = NULL;
551 /* Print current partition */
554 printf("Error, no UBI device selected!\n");
558 printf("Device %d: %s, MTD partition %s\n",
559 ubi->ubi_num, ubi->ubi_name, ubi->mtd->name);
564 return CMD_RET_USAGE;
567 vid_header_offset = argv[3];
569 return ubi_part(argv[2], vid_header_offset);
572 if ((strcmp(argv[1], "part") != 0) && !ubi) {
573 printf("Error, no UBI device selected!\n");
577 if (strcmp(argv[1], "info") == 0) {
579 if (argc > 2 && !strncmp(argv[2], "l", 1))
581 return ubi_info(layout);
584 if (strcmp(argv[1], "check") == 0) {
586 return ubi_check(argv[2]);
588 printf("Error, no volume name passed\n");
592 if (strncmp(argv[1], "create", 6) == 0) {
593 int dynamic = 1; /* default: dynamic volume */
594 int id = UBI_VOL_NUM_AUTO;
596 /* Use maximum available size */
599 /* E.g., create volume with "skipcheck" bit set */
601 skipcheck = strncmp(argv[6], "--skipcheck", 11) == 0;
605 /* E.g., create volume size type vol_id */
607 id = simple_strtoull(argv[5], NULL, 16);
611 /* E.g., create volume size type */
613 if (strncmp(argv[4], "s", 1) == 0)
615 else if (strncmp(argv[4], "d", 1) != 0) {
616 printf("Incorrect type\n");
621 /* E.g., create volume size */
623 if (argv[3][0] != '-')
624 size = simple_strtoull(argv[3], NULL, 16);
627 /* Use maximum available size */
629 size = (int64_t)ubi->avail_pebs * ubi->leb_size;
630 printf("No size specified -> Using max size (%lld)\n", size);
632 /* E.g., create volume */
634 return ubi_create_vol(argv[2], size, dynamic, id,
639 if (strncmp(argv[1], "remove", 6) == 0) {
640 /* E.g., remove volume */
642 return ubi_remove_vol(argv[2]);
645 if (IS_ENABLED(CONFIG_CMD_UBI_RENAME) && !strncmp(argv[1], "rename", 6))
646 return ubi_rename_vol(argv[2], argv[3]);
648 if (strncmp(argv[1], "skipcheck", 9) == 0) {
649 /* E.g., change skip_check flag */
651 skipcheck = strncmp(argv[3], "on", 2) == 0;
652 return ubi_set_skip_check(argv[2], skipcheck);
656 if (strncmp(argv[1], "write", 5) == 0) {
660 printf("Please see usage\n");
664 addr = hextoul(argv[2], NULL);
665 size = hextoul(argv[4], NULL);
667 if (strlen(argv[1]) == 10 &&
668 strncmp(argv[1] + 5, ".part", 5) == 0) {
670 ret = ubi_volume_continue_write(argv[3],
674 full_size = hextoul(argv[5], NULL);
675 ret = ubi_volume_begin_write(argv[3],
676 (void *)addr, size, full_size);
679 ret = ubi_volume_write(argv[3], (void *)addr, size);
682 printf("%lld bytes written to volume %s\n", size,
689 if (strncmp(argv[1], "read", 4) == 0) {
692 /* E.g., read volume size */
694 size = hextoul(argv[4], NULL);
698 /* E.g., read volume */
700 addr = hextoul(argv[2], NULL);
705 return ubi_volume_read(argv[3], (char *)addr, size);
709 printf("Please see usage\n");
717 " - detach ubi from a mtd partition\n"
718 "ubi part [part] [offset]\n"
719 " - Show or set current partition (with optional VID"
721 "ubi info [l[ayout]]"
722 " - Display volume and ubi layout information\n"
723 "ubi check volumename"
724 " - check if volumename exists\n"
725 "ubi create[vol] volume [size] [type] [id] [--skipcheck]\n"
726 " - create volume name with size ('-' for maximum"
728 "ubi write[vol] address volume size"
729 " - Write volume from address with size\n"
730 "ubi write.part address volume size [fullsize]\n"
731 " - Write part of a volume from address\n"
732 "ubi read[vol] address volume [size]"
733 " - Read volume to address with size\n"
734 "ubi remove[vol] volume"
736 #if IS_ENABLED(CONFIG_CMD_UBI_RENAME)
737 "ubi rename oldname newname\n"
739 "ubi skipcheck volume on/off - Set or clear skip_check flag in volume header\n"
741 " volume: character name\n"
742 " size: specified in bytes\n"
743 " type: s[tatic] or d[ynamic] (default=dynamic)"