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.
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>
31 #define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__)
33 /* Private own data */
34 static struct ubi_device *ubi;
36 #ifdef CONFIG_CMD_UBIFS
37 #include <ubifs_uboot.h>
40 static void display_volume_info(struct ubi_device *ubi)
44 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
46 continue; /* Empty record */
47 ubi_dump_vol_info(ubi->volumes[i]);
51 static void display_ubi_info(struct ubi_device *ubi)
53 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
54 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
55 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
56 ubi->peb_size, ubi->peb_size >> 10);
57 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
58 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
59 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
60 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
61 ubi_msg("VID header offset: %d (aligned %d)",
62 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
63 ubi_msg("data offset: %d", ubi->leb_start);
64 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
65 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
66 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
67 ubi_msg("number of user volumes: %d",
68 ubi->vol_count - UBI_INT_VOL_COUNT);
69 ubi_msg("available PEBs: %d", ubi->avail_pebs);
70 ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
71 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
73 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
76 static int ubi_info(int layout)
79 display_volume_info(ubi);
81 display_ubi_info(ubi);
86 static int ubi_check_volumename(const struct ubi_volume *vol, char *name)
88 return strcmp(vol->name, name);
91 static int ubi_check(char *name)
95 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
97 continue; /* Empty record */
99 if (!ubi_check_volumename(ubi->volumes[i], name))
106 static int verify_mkvol_req(const struct ubi_device *ubi,
107 const struct ubi_mkvol_req *req)
111 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
115 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
116 req->vol_id != UBI_VOL_NUM_AUTO)
119 if (req->alignment == 0)
122 if (req->bytes == 0) {
123 printf("No space left in UBI device!\n");
128 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
129 req->vol_type != UBI_STATIC_VOLUME)
132 if (req->alignment > ubi->leb_size)
135 n = req->alignment % ubi->min_io_size;
136 if (req->alignment != 1 && n)
139 if (req->name_len > UBI_VOL_NAME_MAX) {
140 printf("Name too long!\n");
150 static int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id,
153 struct ubi_mkvol_req req;
157 req.vol_type = UBI_DYNAMIC_VOLUME;
159 req.vol_type = UBI_STATIC_VOLUME;
165 strcpy(req.name, volume);
166 req.name_len = strlen(volume);
167 req.name[req.name_len] = '\0';
170 req.flags |= UBI_VOL_SKIP_CRC_CHECK_FLG;
172 /* It's duplicated at drivers/mtd/ubi/cdev.c */
173 err = verify_mkvol_req(ubi, &req);
175 printf("verify_mkvol_req failed %d\n", err);
178 printf("Creating %s volume %s of size %lld\n",
179 dynamic ? "dynamic" : "static", volume, size);
180 /* Call real ubi create volume */
181 return ubi_create_volume(ubi, &req);
184 static struct ubi_volume *ubi_find_volume(char *volume)
186 struct ubi_volume *vol = NULL;
189 for (i = 0; i < ubi->vtbl_slots; i++) {
190 vol = ubi->volumes[i];
191 if (vol && !strcmp(vol->name, volume))
195 printf("Volume %s not found!\n", volume);
199 static int ubi_remove_vol(char *volume)
201 int err, reserved_pebs, i;
202 struct ubi_volume *vol;
204 vol = ubi_find_volume(volume);
208 printf("Remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
211 printf("It's read-only mode\n");
216 err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL);
218 printf("Error changing Vol tabel record err=%x\n", err);
221 reserved_pebs = vol->reserved_pebs;
222 for (i = 0; i < vol->reserved_pebs; i++) {
223 err = ubi_eba_unmap_leb(ubi, vol, i);
229 ubi->volumes[vol->vol_id]->eba_tbl = NULL;
230 ubi->volumes[vol->vol_id] = NULL;
232 ubi->rsvd_pebs -= reserved_pebs;
233 ubi->avail_pebs += reserved_pebs;
234 i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
236 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
237 ubi->avail_pebs -= i;
239 ubi->beb_rsvd_pebs += i;
241 ubi_msg("reserve more %d PEBs", i);
247 ubi_err(ubi, "cannot remove volume %s, error %d", volume, err);
253 static int ubi_volume_continue_write(char *volume, void *buf, size_t size)
256 struct ubi_volume *vol;
258 vol = ubi_find_volume(volume);
262 err = ubi_more_update_data(ubi, vol, buf, size);
264 printf("Couldnt or partially wrote data\n");
271 err = ubi_check_volume(ubi, vol->vol_id);
276 ubi_warn(ubi, "volume %d on UBI device %d is corrupt",
277 vol->vol_id, ubi->ubi_num);
282 ubi_gluebi_updated(vol);
288 int ubi_volume_begin_write(char *volume, void *buf, size_t size,
293 struct ubi_volume *vol;
295 vol = ubi_find_volume(volume);
299 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
300 if (size > rsvd_bytes) {
301 printf("size > volume size! Aborting!\n");
305 err = ubi_start_update(ubi, vol, full_size);
307 printf("Cannot start volume update\n");
311 return ubi_volume_continue_write(volume, buf, size);
314 int ubi_volume_write(char *volume, void *buf, size_t size)
316 return ubi_volume_begin_write(volume, buf, size, size);
319 int ubi_volume_read(char *volume, char *buf, size_t size)
321 int err, lnum, off, len, tbuf_size;
323 unsigned long long tmp;
324 struct ubi_volume *vol;
328 vol = ubi_find_volume(volume);
336 if (vol->upd_marker) {
337 printf("damaged volume, update marker is set");
340 if (offp == vol->used_bytes)
344 printf("No size specified -> Using max size (%lld)\n", vol->used_bytes);
345 size = vol->used_bytes;
348 printf("Read %zu bytes from volume %s to %p\n", size, volume, buf);
351 printf("read from corrupted volume %d", vol->vol_id);
352 if (offp + size > vol->used_bytes)
353 size = vol->used_bytes - offp;
355 tbuf_size = vol->usable_leb_size;
356 if (size < tbuf_size)
357 tbuf_size = ALIGN(size, ubi->min_io_size);
358 tbuf = malloc_cache_aligned(tbuf_size);
363 len = size > tbuf_size ? tbuf_size : size;
366 off = do_div(tmp, vol->usable_leb_size);
370 if (off + len >= vol->usable_leb_size)
371 len = vol->usable_leb_size - off;
373 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
375 printf("read err %x\n", err);
380 if (off == vol->usable_leb_size) {
382 off -= vol->usable_leb_size;
388 memcpy(buf, tbuf, len);
391 len = size > tbuf_size ? tbuf_size : size;
395 env_set_hex("filesize", len_read);
401 static int ubi_dev_scan(struct mtd_info *info, const char *vid_header_offset)
403 char ubi_mtd_param_buffer[80];
406 if (!vid_header_offset)
407 sprintf(ubi_mtd_param_buffer, "%s", info->name);
409 sprintf(ubi_mtd_param_buffer, "%s,%s", info->name,
412 err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
423 static int ubi_set_skip_check(char *volume, bool skip_check)
425 struct ubi_vtbl_record vtbl_rec;
426 struct ubi_volume *vol;
428 vol = ubi_find_volume(volume);
432 printf("%sing skip_check on volume %s\n",
433 skip_check ? "Sett" : "Clear", volume);
435 vtbl_rec = ubi->vtbl[vol->vol_id];
437 vtbl_rec.flags |= UBI_VTBL_SKIP_CRC_CHECK_FLG;
440 vtbl_rec.flags &= ~UBI_VTBL_SKIP_CRC_CHECK_FLG;
444 return ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
447 static int ubi_detach(void)
449 #ifdef CONFIG_CMD_UBIFS
451 * Automatically unmount UBIFS partition when user
452 * changes the UBI device. Otherwise the following
453 * UBIFS commands will crash.
455 if (ubifs_is_mounted())
460 * Call ubi_exit() before re-initializing the UBI subsystem
470 int ubi_part(char *part_name, const char *vid_header_offset)
472 struct mtd_info *mtd;
478 mtd = get_mtd_device_nm(part_name);
480 printf("Partition %s not found!\n", part_name);
485 err = ubi_dev_scan(mtd, vid_header_offset);
487 printf("UBI init error %d\n", err);
488 printf("Please check, if the correct MTD partition is used (size big enough?)\n");
492 ubi = ubi_devices[0];
497 static int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
501 bool skipcheck = false;
504 return CMD_RET_USAGE;
506 if (strcmp(argv[1], "detach") == 0)
509 if (strcmp(argv[1], "part") == 0) {
510 const char *vid_header_offset = NULL;
512 /* Print current partition */
515 printf("Error, no UBI device selected!\n");
519 printf("Device %d: %s, MTD partition %s\n",
520 ubi->ubi_num, ubi->ubi_name, ubi->mtd->name);
525 return CMD_RET_USAGE;
528 vid_header_offset = argv[3];
530 return ubi_part(argv[2], vid_header_offset);
533 if ((strcmp(argv[1], "part") != 0) && !ubi) {
534 printf("Error, no UBI device selected!\n");
538 if (strcmp(argv[1], "info") == 0) {
540 if (argc > 2 && !strncmp(argv[2], "l", 1))
542 return ubi_info(layout);
545 if (strcmp(argv[1], "check") == 0) {
547 return ubi_check(argv[2]);
549 printf("Error, no volume name passed\n");
553 if (strncmp(argv[1], "create", 6) == 0) {
554 int dynamic = 1; /* default: dynamic volume */
555 int id = UBI_VOL_NUM_AUTO;
557 /* Use maximum available size */
560 /* E.g., create volume with "skipcheck" bit set */
562 skipcheck = strncmp(argv[6], "--skipcheck", 11) == 0;
566 /* E.g., create volume size type vol_id */
568 id = simple_strtoull(argv[5], NULL, 16);
572 /* E.g., create volume size type */
574 if (strncmp(argv[4], "s", 1) == 0)
576 else if (strncmp(argv[4], "d", 1) != 0) {
577 printf("Incorrect type\n");
582 /* E.g., create volume size */
584 if (argv[3][0] != '-')
585 size = simple_strtoull(argv[3], NULL, 16);
588 /* Use maximum available size */
590 size = (int64_t)ubi->avail_pebs * ubi->leb_size;
591 printf("No size specified -> Using max size (%lld)\n", size);
593 /* E.g., create volume */
595 return ubi_create_vol(argv[2], size, dynamic, id,
600 if (strncmp(argv[1], "remove", 6) == 0) {
601 /* E.g., remove volume */
603 return ubi_remove_vol(argv[2]);
606 if (strncmp(argv[1], "skipcheck", 9) == 0) {
607 /* E.g., change skip_check flag */
609 skipcheck = strncmp(argv[3], "on", 2) == 0;
610 return ubi_set_skip_check(argv[2], skipcheck);
614 if (strncmp(argv[1], "write", 5) == 0) {
618 printf("Please see usage\n");
622 addr = simple_strtoul(argv[2], NULL, 16);
623 size = simple_strtoul(argv[4], NULL, 16);
625 if (strlen(argv[1]) == 10 &&
626 strncmp(argv[1] + 5, ".part", 5) == 0) {
628 ret = ubi_volume_continue_write(argv[3],
632 full_size = simple_strtoul(argv[5], NULL, 16);
633 ret = ubi_volume_begin_write(argv[3],
634 (void *)addr, size, full_size);
637 ret = ubi_volume_write(argv[3], (void *)addr, size);
640 printf("%lld bytes written to volume %s\n", size,
647 if (strncmp(argv[1], "read", 4) == 0) {
650 /* E.g., read volume size */
652 size = simple_strtoul(argv[4], NULL, 16);
656 /* E.g., read volume */
658 addr = simple_strtoul(argv[2], NULL, 16);
663 return ubi_volume_read(argv[3], (char *)addr, size);
667 printf("Please see usage\n");
675 " - detach ubi from a mtd partition\n"
676 "ubi part [part] [offset]\n"
677 " - Show or set current partition (with optional VID"
679 "ubi info [l[ayout]]"
680 " - Display volume and ubi layout information\n"
681 "ubi check volumename"
682 " - check if volumename exists\n"
683 "ubi create[vol] volume [size] [type] [id] [--skipcheck]\n"
684 " - create volume name with size ('-' for maximum"
686 "ubi write[vol] address volume size"
687 " - Write volume from address with size\n"
688 "ubi write.part address volume size [fullsize]\n"
689 " - Write part of a volume from address\n"
690 "ubi read[vol] address volume [size]"
691 " - Read volume to address with size\n"
692 "ubi remove[vol] volume"
694 "ubi skipcheck volume on/off - Set or clear skip_check flag in volume header\n"
696 " volume: character name\n"
697 " size: specified in bytes\n"
698 " type: s[tatic] or d[ynamic] (default=dynamic)"