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 printf("Rename UBI volume %s to %s\n", oldname, newname);
270 printf("%s: ubi device is in read-only mode\n", __func__);
274 rename.new_name_len = strlen(newname);
275 strcpy(rename.new_name, newname);
280 INIT_LIST_HEAD(&rename.list);
281 INIT_LIST_HEAD(&list);
282 list_add(&rename.list, &list);
284 return ubi_rename_volumes(ubi, &list);
287 static int ubi_volume_continue_write(char *volume, void *buf, size_t size)
290 struct ubi_volume *vol;
292 vol = ubi_find_volume(volume);
296 err = ubi_more_update_data(ubi, vol, buf, size);
298 printf("Couldnt or partially wrote data\n");
305 err = ubi_check_volume(ubi, vol->vol_id);
310 ubi_warn(ubi, "volume %d on UBI device %d is corrupt",
311 vol->vol_id, ubi->ubi_num);
316 ubi_gluebi_updated(vol);
322 int ubi_volume_begin_write(char *volume, void *buf, size_t size,
327 struct ubi_volume *vol;
329 vol = ubi_find_volume(volume);
333 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
334 if (size > rsvd_bytes) {
335 printf("size > volume size! Aborting!\n");
339 err = ubi_start_update(ubi, vol, full_size);
341 printf("Cannot start volume update\n");
345 return ubi_volume_continue_write(volume, buf, size);
348 int ubi_volume_write(char *volume, void *buf, size_t size)
350 return ubi_volume_begin_write(volume, buf, size, size);
353 int ubi_volume_read(char *volume, char *buf, size_t size)
355 int err, lnum, off, len, tbuf_size;
357 unsigned long long tmp;
358 struct ubi_volume *vol;
362 vol = ubi_find_volume(volume);
370 if (vol->upd_marker) {
371 printf("damaged volume, update marker is set");
374 if (offp == vol->used_bytes)
378 printf("No size specified -> Using max size (%lld)\n", vol->used_bytes);
379 size = vol->used_bytes;
382 printf("Read %zu bytes from volume %s to %p\n", size, volume, buf);
385 printf("read from corrupted volume %d", vol->vol_id);
386 if (offp + size > vol->used_bytes)
387 size = vol->used_bytes - offp;
389 tbuf_size = vol->usable_leb_size;
390 if (size < tbuf_size)
391 tbuf_size = ALIGN(size, ubi->min_io_size);
392 tbuf = malloc_cache_aligned(tbuf_size);
397 len = size > tbuf_size ? tbuf_size : size;
400 off = do_div(tmp, vol->usable_leb_size);
404 if (off + len >= vol->usable_leb_size)
405 len = vol->usable_leb_size - off;
407 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
409 printf("read err %x\n", err);
414 if (off == vol->usable_leb_size) {
416 off -= vol->usable_leb_size;
422 memcpy(buf, tbuf, len);
425 len = size > tbuf_size ? tbuf_size : size;
429 env_set_hex("filesize", len_read);
435 static int ubi_dev_scan(struct mtd_info *info, const char *vid_header_offset)
437 char ubi_mtd_param_buffer[80];
440 if (!vid_header_offset)
441 sprintf(ubi_mtd_param_buffer, "%s", info->name);
443 sprintf(ubi_mtd_param_buffer, "%s,%s", info->name,
446 err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
457 static int ubi_set_skip_check(char *volume, bool skip_check)
459 struct ubi_vtbl_record vtbl_rec;
460 struct ubi_volume *vol;
462 vol = ubi_find_volume(volume);
466 printf("%sing skip_check on volume %s\n",
467 skip_check ? "Sett" : "Clear", volume);
469 vtbl_rec = ubi->vtbl[vol->vol_id];
471 vtbl_rec.flags |= UBI_VTBL_SKIP_CRC_CHECK_FLG;
474 vtbl_rec.flags &= ~UBI_VTBL_SKIP_CRC_CHECK_FLG;
478 return ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
481 static int ubi_detach(void)
483 #ifdef CONFIG_CMD_UBIFS
485 * Automatically unmount UBIFS partition when user
486 * changes the UBI device. Otherwise the following
487 * UBIFS commands will crash.
489 if (ubifs_is_mounted())
494 * Call ubi_exit() before re-initializing the UBI subsystem
504 int ubi_part(char *part_name, const char *vid_header_offset)
506 struct mtd_info *mtd;
512 mtd = get_mtd_device_nm(part_name);
514 printf("Partition %s not found!\n", part_name);
519 err = ubi_dev_scan(mtd, vid_header_offset);
521 printf("UBI init error %d\n", err);
522 printf("Please check, if the correct MTD partition is used (size big enough?)\n");
526 ubi = ubi_devices[0];
531 static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
535 bool skipcheck = false;
538 return CMD_RET_USAGE;
540 if (strcmp(argv[1], "detach") == 0)
543 if (strcmp(argv[1], "part") == 0) {
544 const char *vid_header_offset = NULL;
546 /* Print current partition */
549 printf("Error, no UBI device selected!\n");
553 printf("Device %d: %s, MTD partition %s\n",
554 ubi->ubi_num, ubi->ubi_name, ubi->mtd->name);
559 return CMD_RET_USAGE;
562 vid_header_offset = argv[3];
564 return ubi_part(argv[2], vid_header_offset);
567 if ((strcmp(argv[1], "part") != 0) && !ubi) {
568 printf("Error, no UBI device selected!\n");
572 if (strcmp(argv[1], "info") == 0) {
574 if (argc > 2 && !strncmp(argv[2], "l", 1))
576 return ubi_info(layout);
579 if (strcmp(argv[1], "check") == 0) {
581 return ubi_check(argv[2]);
583 printf("Error, no volume name passed\n");
587 if (strncmp(argv[1], "create", 6) == 0) {
588 int dynamic = 1; /* default: dynamic volume */
589 int id = UBI_VOL_NUM_AUTO;
591 /* Use maximum available size */
594 /* E.g., create volume with "skipcheck" bit set */
596 skipcheck = strncmp(argv[6], "--skipcheck", 11) == 0;
600 /* E.g., create volume size type vol_id */
602 id = simple_strtoull(argv[5], NULL, 16);
606 /* E.g., create volume size type */
608 if (strncmp(argv[4], "s", 1) == 0)
610 else if (strncmp(argv[4], "d", 1) != 0) {
611 printf("Incorrect type\n");
616 /* E.g., create volume size */
618 if (argv[3][0] != '-')
619 size = simple_strtoull(argv[3], NULL, 16);
622 /* Use maximum available size */
624 size = (int64_t)ubi->avail_pebs * ubi->leb_size;
625 printf("No size specified -> Using max size (%lld)\n", size);
627 /* E.g., create volume */
629 return ubi_create_vol(argv[2], size, dynamic, id,
634 if (strncmp(argv[1], "remove", 6) == 0) {
635 /* E.g., remove volume */
637 return ubi_remove_vol(argv[2]);
640 if (IS_ENABLED(CONFIG_CMD_UBI_RENAME) && !strncmp(argv[1], "rename", 6))
641 return ubi_rename_vol(argv[2], argv[3]);
643 if (strncmp(argv[1], "skipcheck", 9) == 0) {
644 /* E.g., change skip_check flag */
646 skipcheck = strncmp(argv[3], "on", 2) == 0;
647 return ubi_set_skip_check(argv[2], skipcheck);
651 if (strncmp(argv[1], "write", 5) == 0) {
655 printf("Please see usage\n");
659 addr = simple_strtoul(argv[2], NULL, 16);
660 size = simple_strtoul(argv[4], NULL, 16);
662 if (strlen(argv[1]) == 10 &&
663 strncmp(argv[1] + 5, ".part", 5) == 0) {
665 ret = ubi_volume_continue_write(argv[3],
669 full_size = simple_strtoul(argv[5], NULL, 16);
670 ret = ubi_volume_begin_write(argv[3],
671 (void *)addr, size, full_size);
674 ret = ubi_volume_write(argv[3], (void *)addr, size);
677 printf("%lld bytes written to volume %s\n", size,
684 if (strncmp(argv[1], "read", 4) == 0) {
687 /* E.g., read volume size */
689 size = simple_strtoul(argv[4], NULL, 16);
693 /* E.g., read volume */
695 addr = simple_strtoul(argv[2], NULL, 16);
700 return ubi_volume_read(argv[3], (char *)addr, size);
704 printf("Please see usage\n");
712 " - detach ubi from a mtd partition\n"
713 "ubi part [part] [offset]\n"
714 " - Show or set current partition (with optional VID"
716 "ubi info [l[ayout]]"
717 " - Display volume and ubi layout information\n"
718 "ubi check volumename"
719 " - check if volumename exists\n"
720 "ubi create[vol] volume [size] [type] [id] [--skipcheck]\n"
721 " - create volume name with size ('-' for maximum"
723 "ubi write[vol] address volume size"
724 " - Write volume from address with size\n"
725 "ubi write.part address volume size [fullsize]\n"
726 " - Write part of a volume from address\n"
727 "ubi read[vol] address volume [size]"
728 " - Read volume to address with size\n"
729 "ubi remove[vol] volume"
731 #if IS_ENABLED(CONFIG_CMD_UBI_RENAME)
732 "ubi rename oldname newname\n"
734 "ubi skipcheck volume on/off - Set or clear skip_check flag in volume header\n"
736 " volume: character name\n"
737 " size: specified in bytes\n"
738 " type: s[tatic] or d[ynamic] (default=dynamic)"