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.
19 #include <onenand_uboot.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/partitions.h>
22 #include <linux/err.h>
23 #include <ubi_uboot.h>
24 #include <linux/errno.h>
25 #include <jffs2/load_kernel.h>
28 #define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__)
30 /* Private own data */
31 static struct ubi_device *ubi;
32 static char buffer[80];
33 static int ubi_initialized;
39 struct mtd_info *mtd_info;
42 static struct selected_dev ubi_dev;
44 #ifdef CONFIG_CMD_UBIFS
45 int ubifs_is_mounted(void);
46 void cmd_ubifs_umount(void);
49 static void display_volume_info(struct ubi_device *ubi)
53 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
55 continue; /* Empty record */
56 ubi_dump_vol_info(ubi->volumes[i]);
60 static void display_ubi_info(struct ubi_device *ubi)
62 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
63 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
64 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
65 ubi->peb_size, ubi->peb_size >> 10);
66 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
67 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
68 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
69 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
70 ubi_msg("VID header offset: %d (aligned %d)",
71 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
72 ubi_msg("data offset: %d", ubi->leb_start);
73 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
74 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
75 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
76 ubi_msg("number of user volumes: %d",
77 ubi->vol_count - UBI_INT_VOL_COUNT);
78 ubi_msg("available PEBs: %d", ubi->avail_pebs);
79 ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
80 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
82 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
85 static int ubi_info(int layout)
88 display_volume_info(ubi);
90 display_ubi_info(ubi);
95 static int ubi_check_volumename(const struct ubi_volume *vol, char *name)
97 return strcmp(vol->name, name);
100 static int ubi_check(char *name)
104 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
105 if (!ubi->volumes[i])
106 continue; /* Empty record */
108 if (!ubi_check_volumename(ubi->volumes[i], name))
116 static int verify_mkvol_req(const struct ubi_device *ubi,
117 const struct ubi_mkvol_req *req)
121 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
125 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
126 req->vol_id != UBI_VOL_NUM_AUTO)
129 if (req->alignment == 0)
132 if (req->bytes == 0) {
133 printf("No space left in UBI device!\n");
138 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
139 req->vol_type != UBI_STATIC_VOLUME)
142 if (req->alignment > ubi->leb_size)
145 n = req->alignment % ubi->min_io_size;
146 if (req->alignment != 1 && n)
149 if (req->name_len > UBI_VOL_NAME_MAX) {
150 printf("Name too long!\n");
160 static int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id)
162 struct ubi_mkvol_req req;
166 req.vol_type = UBI_DYNAMIC_VOLUME;
168 req.vol_type = UBI_STATIC_VOLUME;
174 strcpy(req.name, volume);
175 req.name_len = strlen(volume);
176 req.name[req.name_len] = '\0';
178 /* It's duplicated at drivers/mtd/ubi/cdev.c */
179 err = verify_mkvol_req(ubi, &req);
181 printf("verify_mkvol_req failed %d\n", err);
184 printf("Creating %s volume %s of size %lld\n",
185 dynamic ? "dynamic" : "static", volume, size);
186 /* Call real ubi create volume */
187 return ubi_create_volume(ubi, &req);
190 static struct ubi_volume *ubi_find_volume(char *volume)
192 struct ubi_volume *vol = NULL;
195 for (i = 0; i < ubi->vtbl_slots; i++) {
196 vol = ubi->volumes[i];
197 if (vol && !strcmp(vol->name, volume))
201 printf("Volume %s not found!\n", volume);
205 static int ubi_remove_vol(char *volume)
207 int err, reserved_pebs, i;
208 struct ubi_volume *vol;
210 vol = ubi_find_volume(volume);
214 printf("Remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
217 printf("It's read-only mode\n");
222 err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL);
224 printf("Error changing Vol tabel record err=%x\n", err);
227 reserved_pebs = vol->reserved_pebs;
228 for (i = 0; i < vol->reserved_pebs; i++) {
229 err = ubi_eba_unmap_leb(ubi, vol, i);
235 ubi->volumes[vol->vol_id]->eba_tbl = NULL;
236 ubi->volumes[vol->vol_id] = NULL;
238 ubi->rsvd_pebs -= reserved_pebs;
239 ubi->avail_pebs += reserved_pebs;
240 i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
242 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
243 ubi->avail_pebs -= i;
245 ubi->beb_rsvd_pebs += i;
247 ubi_msg("reserve more %d PEBs", i);
253 ubi_err(ubi, "cannot remove volume %s, error %d", volume, err);
259 static int ubi_volume_continue_write(char *volume, void *buf, size_t size)
262 struct ubi_volume *vol;
264 vol = ubi_find_volume(volume);
268 err = ubi_more_update_data(ubi, vol, buf, size);
270 printf("Couldnt or partially wrote data\n");
277 err = ubi_check_volume(ubi, vol->vol_id);
282 ubi_warn(ubi, "volume %d on UBI device %d is corrupt",
283 vol->vol_id, ubi->ubi_num);
288 ubi_gluebi_updated(vol);
294 int ubi_volume_begin_write(char *volume, void *buf, size_t size,
299 struct ubi_volume *vol;
301 vol = ubi_find_volume(volume);
305 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
306 if (size > rsvd_bytes) {
307 printf("size > volume size! Aborting!\n");
311 err = ubi_start_update(ubi, vol, full_size);
313 printf("Cannot start volume update\n");
317 return ubi_volume_continue_write(volume, buf, size);
320 int ubi_volume_write(char *volume, void *buf, size_t size)
322 return ubi_volume_begin_write(volume, buf, size, size);
325 int ubi_volume_read(char *volume, char *buf, size_t size)
327 int err, lnum, off, len, tbuf_size;
329 unsigned long long tmp;
330 struct ubi_volume *vol;
334 vol = ubi_find_volume(volume);
342 if (vol->upd_marker) {
343 printf("damaged volume, update marker is set");
346 if (offp == vol->used_bytes)
350 printf("No size specified -> Using max size (%lld)\n", vol->used_bytes);
351 size = vol->used_bytes;
354 printf("Read %zu bytes from volume %s to %p\n", size, volume, buf);
357 printf("read from corrupted volume %d", vol->vol_id);
358 if (offp + size > vol->used_bytes)
359 size = vol->used_bytes - offp;
361 tbuf_size = vol->usable_leb_size;
362 if (size < tbuf_size)
363 tbuf_size = ALIGN(size, ubi->min_io_size);
364 tbuf = malloc_cache_aligned(tbuf_size);
369 len = size > tbuf_size ? tbuf_size : size;
372 off = do_div(tmp, vol->usable_leb_size);
376 if (off + len >= vol->usable_leb_size)
377 len = vol->usable_leb_size - off;
379 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
381 printf("read err %x\n", err);
386 if (off == vol->usable_leb_size) {
388 off -= vol->usable_leb_size;
394 memcpy(buf, tbuf, len);
397 len = size > tbuf_size ? tbuf_size : size;
401 env_set_hex("filesize", len_read);
407 static int ubi_dev_scan(struct mtd_info *info, char *ubidev,
408 const char *vid_header_offset)
410 struct mtd_device *dev;
411 struct part_info *part;
412 struct mtd_partition mtd_part;
413 char ubi_mtd_param_buffer[80];
417 if (find_dev_and_part(ubidev, &dev, &pnum, &part) != 0)
420 sprintf(buffer, "mtd=%d", pnum);
421 memset(&mtd_part, 0, sizeof(mtd_part));
422 mtd_part.name = buffer;
423 mtd_part.size = part->size;
424 mtd_part.offset = part->offset;
425 add_mtd_partitions(info, &mtd_part, 1);
427 strcpy(ubi_mtd_param_buffer, buffer);
428 if (vid_header_offset)
429 sprintf(ubi_mtd_param_buffer, "mtd=%d,%s", pnum,
431 err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
433 del_mtd_partitions(info);
439 del_mtd_partitions(info);
450 if (mtdparts_init() != 0) {
451 printf("Error initializing mtdparts!\n");
455 #ifdef CONFIG_CMD_UBIFS
457 * Automatically unmount UBIFS partition when user
458 * changes the UBI device. Otherwise the following
459 * UBIFS commands will crash.
461 if (ubifs_is_mounted())
466 * Call ubi_exit() before re-initializing the UBI subsystem
468 if (ubi_initialized) {
470 del_mtd_partitions(ubi_dev.mtd_info);
474 ubi_dev.selected = 0;
478 int ubi_part(char *part_name, const char *vid_header_offset)
482 struct mtd_device *dev;
483 struct part_info *part;
488 * Search the mtd device number where this partition
491 if (find_dev_and_part(part_name, &dev, &pnum, &part)) {
492 printf("Partition %s not found!\n", part_name);
495 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(dev->id->type), dev->id->num);
496 ubi_dev.mtd_info = get_mtd_device_nm(mtd_dev);
497 if (IS_ERR(ubi_dev.mtd_info)) {
498 printf("Partition %s not found on device %s!\n", part_name,
503 ubi_dev.selected = 1;
505 strcpy(ubi_dev.part_name, part_name);
506 err = ubi_dev_scan(ubi_dev.mtd_info, ubi_dev.part_name,
509 printf("UBI init error %d\n", err);
510 printf("Please check, if the correct MTD partition is used (size big enough?)\n");
511 ubi_dev.selected = 0;
515 ubi = ubi_devices[0];
520 static int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
526 return CMD_RET_USAGE;
529 if (strcmp(argv[1], "detach") == 0) {
531 return CMD_RET_USAGE;
537 if (strcmp(argv[1], "part") == 0) {
538 const char *vid_header_offset = NULL;
540 /* Print current partition */
542 if (!ubi_dev.selected) {
543 printf("Error, no UBI device/partition selected!\n");
547 printf("Device %d: %s, partition %s\n",
548 ubi_dev.nr, ubi_dev.mtd_info->name, ubi_dev.part_name);
553 return CMD_RET_USAGE;
556 vid_header_offset = argv[3];
558 return ubi_part(argv[2], vid_header_offset);
561 if ((strcmp(argv[1], "part") != 0) && (!ubi_dev.selected)) {
562 printf("Error, no UBI device/partition selected!\n");
566 if (strcmp(argv[1], "info") == 0) {
568 if (argc > 2 && !strncmp(argv[2], "l", 1))
570 return ubi_info(layout);
573 if (strcmp(argv[1], "check") == 0) {
575 return ubi_check(argv[2]);
577 printf("Error, no volume name passed\n");
581 if (strncmp(argv[1], "create", 6) == 0) {
582 int dynamic = 1; /* default: dynamic volume */
583 int id = UBI_VOL_NUM_AUTO;
585 /* Use maximum available size */
588 /* E.g., create volume size type vol_id */
590 id = simple_strtoull(argv[5], NULL, 16);
594 /* E.g., create volume size type */
596 if (strncmp(argv[4], "s", 1) == 0)
598 else if (strncmp(argv[4], "d", 1) != 0) {
599 printf("Incorrect type\n");
604 /* E.g., create volume size */
606 if (argv[3][0] != '-')
607 size = simple_strtoull(argv[3], NULL, 16);
610 /* Use maximum available size */
612 size = (int64_t)ubi->avail_pebs * ubi->leb_size;
613 printf("No size specified -> Using max size (%lld)\n", size);
615 /* E.g., create volume */
617 return ubi_create_vol(argv[2], size, dynamic, id);
620 if (strncmp(argv[1], "remove", 6) == 0) {
621 /* E.g., remove volume */
623 return ubi_remove_vol(argv[2]);
626 if (strncmp(argv[1], "write", 5) == 0) {
630 printf("Please see usage\n");
634 addr = simple_strtoul(argv[2], NULL, 16);
635 size = simple_strtoul(argv[4], NULL, 16);
637 if (strlen(argv[1]) == 10 &&
638 strncmp(argv[1] + 5, ".part", 5) == 0) {
640 ret = ubi_volume_continue_write(argv[3],
644 full_size = simple_strtoul(argv[5], NULL, 16);
645 ret = ubi_volume_begin_write(argv[3],
646 (void *)addr, size, full_size);
649 ret = ubi_volume_write(argv[3], (void *)addr, size);
652 printf("%lld bytes written to volume %s\n", size,
659 if (strncmp(argv[1], "read", 4) == 0) {
662 /* E.g., read volume size */
664 size = simple_strtoul(argv[4], NULL, 16);
668 /* E.g., read volume */
670 addr = simple_strtoul(argv[2], NULL, 16);
675 return ubi_volume_read(argv[3], (char *)addr, size);
679 printf("Please see usage\n");
687 " - detach ubi from a mtd partition\n"
688 "ubi part [part] [offset]\n"
689 " - Show or set current partition (with optional VID"
691 "ubi info [l[ayout]]"
692 " - Display volume and ubi layout information\n"
693 "ubi check volumename"
694 " - check if volumename exists\n"
695 "ubi create[vol] volume [size] [type] [id]\n"
696 " - create volume name with size ('-' for maximum"
698 "ubi write[vol] address volume size"
699 " - Write volume from address with size\n"
700 "ubi write.part address volume size [fullsize]\n"
701 " - Write part of a volume from address\n"
702 "ubi read[vol] address volume [size]"
703 " - Read volume to address with size\n"
704 "ubi remove[vol] volume"
707 " volume: character name\n"
708 " size: specified in bytes\n"
709 " type: s[tatic] or d[ynamic] (default=dynamic)"