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 #define DEV_TYPE_NONE 0
31 #define DEV_TYPE_NAND 1
32 #define DEV_TYPE_ONENAND 2
33 #define DEV_TYPE_NOR 3
35 /* Private own data */
36 static struct ubi_device *ubi;
37 static char buffer[80];
38 static int ubi_initialized;
44 struct mtd_info *mtd_info;
47 static struct selected_dev ubi_dev;
49 #ifdef CONFIG_CMD_UBIFS
50 int ubifs_is_mounted(void);
51 void cmd_ubifs_umount(void);
54 static void display_volume_info(struct ubi_device *ubi)
58 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
60 continue; /* Empty record */
61 ubi_dump_vol_info(ubi->volumes[i]);
65 static void display_ubi_info(struct ubi_device *ubi)
67 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
68 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
69 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
70 ubi->peb_size, ubi->peb_size >> 10);
71 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
72 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
73 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
74 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
75 ubi_msg("VID header offset: %d (aligned %d)",
76 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
77 ubi_msg("data offset: %d", ubi->leb_start);
78 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
79 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
80 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
81 ubi_msg("number of user volumes: %d",
82 ubi->vol_count - UBI_INT_VOL_COUNT);
83 ubi_msg("available PEBs: %d", ubi->avail_pebs);
84 ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
85 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
87 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
90 static int ubi_info(int layout)
93 display_volume_info(ubi);
95 display_ubi_info(ubi);
100 static int ubi_check_volumename(const struct ubi_volume *vol, char *name)
102 return strcmp(vol->name, name);
105 static int ubi_check(char *name)
109 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
110 if (!ubi->volumes[i])
111 continue; /* Empty record */
113 if (!ubi_check_volumename(ubi->volumes[i], name))
121 static int verify_mkvol_req(const struct ubi_device *ubi,
122 const struct ubi_mkvol_req *req)
126 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
130 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
131 req->vol_id != UBI_VOL_NUM_AUTO)
134 if (req->alignment == 0)
137 if (req->bytes == 0) {
138 printf("No space left in UBI device!\n");
143 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
144 req->vol_type != UBI_STATIC_VOLUME)
147 if (req->alignment > ubi->leb_size)
150 n = req->alignment % ubi->min_io_size;
151 if (req->alignment != 1 && n)
154 if (req->name_len > UBI_VOL_NAME_MAX) {
155 printf("Name too long!\n");
165 static int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id)
167 struct ubi_mkvol_req req;
171 req.vol_type = UBI_DYNAMIC_VOLUME;
173 req.vol_type = UBI_STATIC_VOLUME;
179 strcpy(req.name, volume);
180 req.name_len = strlen(volume);
181 req.name[req.name_len] = '\0';
183 /* It's duplicated at drivers/mtd/ubi/cdev.c */
184 err = verify_mkvol_req(ubi, &req);
186 printf("verify_mkvol_req failed %d\n", err);
189 printf("Creating %s volume %s of size %lld\n",
190 dynamic ? "dynamic" : "static", volume, size);
191 /* Call real ubi create volume */
192 return ubi_create_volume(ubi, &req);
195 static struct ubi_volume *ubi_find_volume(char *volume)
197 struct ubi_volume *vol = NULL;
200 for (i = 0; i < ubi->vtbl_slots; i++) {
201 vol = ubi->volumes[i];
202 if (vol && !strcmp(vol->name, volume))
206 printf("Volume %s not found!\n", volume);
210 static int ubi_remove_vol(char *volume)
212 int err, reserved_pebs, i;
213 struct ubi_volume *vol;
215 vol = ubi_find_volume(volume);
219 printf("Remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
222 printf("It's read-only mode\n");
227 err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL);
229 printf("Error changing Vol tabel record err=%x\n", err);
232 reserved_pebs = vol->reserved_pebs;
233 for (i = 0; i < vol->reserved_pebs; i++) {
234 err = ubi_eba_unmap_leb(ubi, vol, i);
240 ubi->volumes[vol->vol_id]->eba_tbl = NULL;
241 ubi->volumes[vol->vol_id] = NULL;
243 ubi->rsvd_pebs -= reserved_pebs;
244 ubi->avail_pebs += reserved_pebs;
245 i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
247 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
248 ubi->avail_pebs -= i;
250 ubi->beb_rsvd_pebs += i;
252 ubi_msg("reserve more %d PEBs", i);
258 ubi_err(ubi, "cannot remove volume %s, error %d", volume, err);
264 static int ubi_volume_continue_write(char *volume, void *buf, size_t size)
267 struct ubi_volume *vol;
269 vol = ubi_find_volume(volume);
273 err = ubi_more_update_data(ubi, vol, buf, size);
275 printf("Couldnt or partially wrote data\n");
282 err = ubi_check_volume(ubi, vol->vol_id);
287 ubi_warn(ubi, "volume %d on UBI device %d is corrupt",
288 vol->vol_id, ubi->ubi_num);
293 ubi_gluebi_updated(vol);
299 int ubi_volume_begin_write(char *volume, void *buf, size_t size,
304 struct ubi_volume *vol;
306 vol = ubi_find_volume(volume);
310 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
311 if (size > rsvd_bytes) {
312 printf("size > volume size! Aborting!\n");
316 err = ubi_start_update(ubi, vol, full_size);
318 printf("Cannot start volume update\n");
322 return ubi_volume_continue_write(volume, buf, size);
325 int ubi_volume_write(char *volume, void *buf, size_t size)
327 return ubi_volume_begin_write(volume, buf, size, size);
330 int ubi_volume_read(char *volume, char *buf, size_t size)
332 int err, lnum, off, len, tbuf_size;
334 unsigned long long tmp;
335 struct ubi_volume *vol;
339 vol = ubi_find_volume(volume);
347 if (vol->upd_marker) {
348 printf("damaged volume, update marker is set");
351 if (offp == vol->used_bytes)
355 printf("No size specified -> Using max size (%lld)\n", vol->used_bytes);
356 size = vol->used_bytes;
359 printf("Read %zu bytes from volume %s to %p\n", size, volume, buf);
362 printf("read from corrupted volume %d", vol->vol_id);
363 if (offp + size > vol->used_bytes)
364 size = vol->used_bytes - offp;
366 tbuf_size = vol->usable_leb_size;
367 if (size < tbuf_size)
368 tbuf_size = ALIGN(size, ubi->min_io_size);
369 tbuf = malloc_cache_aligned(tbuf_size);
374 len = size > tbuf_size ? tbuf_size : size;
377 off = do_div(tmp, vol->usable_leb_size);
381 if (off + len >= vol->usable_leb_size)
382 len = vol->usable_leb_size - off;
384 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
386 printf("read err %x\n", err);
391 if (off == vol->usable_leb_size) {
393 off -= vol->usable_leb_size;
399 memcpy(buf, tbuf, len);
402 len = size > tbuf_size ? tbuf_size : size;
406 env_set_hex("filesize", len_read);
412 static int ubi_dev_scan(struct mtd_info *info, char *ubidev,
413 const char *vid_header_offset)
415 struct mtd_device *dev;
416 struct part_info *part;
417 struct mtd_partition mtd_part;
418 char ubi_mtd_param_buffer[80];
422 if (find_dev_and_part(ubidev, &dev, &pnum, &part) != 0)
425 sprintf(buffer, "mtd=%d", pnum);
426 memset(&mtd_part, 0, sizeof(mtd_part));
427 mtd_part.name = buffer;
428 mtd_part.size = part->size;
429 mtd_part.offset = part->offset;
430 add_mtd_partitions(info, &mtd_part, 1);
432 strcpy(ubi_mtd_param_buffer, buffer);
433 if (vid_header_offset)
434 sprintf(ubi_mtd_param_buffer, "mtd=%d,%s", pnum,
436 err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
438 del_mtd_partitions(info);
444 del_mtd_partitions(info);
455 if (mtdparts_init() != 0) {
456 printf("Error initializing mtdparts!\n");
460 #ifdef CONFIG_CMD_UBIFS
462 * Automatically unmount UBIFS partition when user
463 * changes the UBI device. Otherwise the following
464 * UBIFS commands will crash.
466 if (ubifs_is_mounted())
471 * Call ubi_exit() before re-initializing the UBI subsystem
473 if (ubi_initialized) {
475 del_mtd_partitions(ubi_dev.mtd_info);
479 ubi_dev.selected = 0;
483 int ubi_part(char *part_name, const char *vid_header_offset)
487 struct mtd_device *dev;
488 struct part_info *part;
493 * Search the mtd device number where this partition
496 if (find_dev_and_part(part_name, &dev, &pnum, &part)) {
497 printf("Partition %s not found!\n", part_name);
500 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(dev->id->type), dev->id->num);
501 ubi_dev.mtd_info = get_mtd_device_nm(mtd_dev);
502 if (IS_ERR(ubi_dev.mtd_info)) {
503 printf("Partition %s not found on device %s!\n", part_name,
508 ubi_dev.selected = 1;
510 strcpy(ubi_dev.part_name, part_name);
511 err = ubi_dev_scan(ubi_dev.mtd_info, ubi_dev.part_name,
514 printf("UBI init error %d\n", err);
515 printf("Please check, if the correct MTD partition is used (size big enough?)\n");
516 ubi_dev.selected = 0;
520 ubi = ubi_devices[0];
525 static int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
531 return CMD_RET_USAGE;
534 if (strcmp(argv[1], "detach") == 0) {
536 return CMD_RET_USAGE;
542 if (strcmp(argv[1], "part") == 0) {
543 const char *vid_header_offset = NULL;
545 /* Print current partition */
547 if (!ubi_dev.selected) {
548 printf("Error, no UBI device/partition selected!\n");
552 printf("Device %d: %s, partition %s\n",
553 ubi_dev.nr, ubi_dev.mtd_info->name, ubi_dev.part_name);
558 return CMD_RET_USAGE;
561 vid_header_offset = argv[3];
563 return ubi_part(argv[2], vid_header_offset);
566 if ((strcmp(argv[1], "part") != 0) && (!ubi_dev.selected)) {
567 printf("Error, no UBI device/partition selected!\n");
571 if (strcmp(argv[1], "info") == 0) {
573 if (argc > 2 && !strncmp(argv[2], "l", 1))
575 return ubi_info(layout);
578 if (strcmp(argv[1], "check") == 0) {
580 return ubi_check(argv[2]);
582 printf("Error, no volume name passed\n");
586 if (strncmp(argv[1], "create", 6) == 0) {
587 int dynamic = 1; /* default: dynamic volume */
588 int id = UBI_VOL_NUM_AUTO;
590 /* Use maximum available size */
593 /* E.g., create volume size type vol_id */
595 id = simple_strtoull(argv[5], NULL, 16);
599 /* E.g., create volume size type */
601 if (strncmp(argv[4], "s", 1) == 0)
603 else if (strncmp(argv[4], "d", 1) != 0) {
604 printf("Incorrect type\n");
609 /* E.g., create volume size */
611 if (argv[3][0] != '-')
612 size = simple_strtoull(argv[3], NULL, 16);
615 /* Use maximum available size */
617 size = (int64_t)ubi->avail_pebs * ubi->leb_size;
618 printf("No size specified -> Using max size (%lld)\n", size);
620 /* E.g., create volume */
622 return ubi_create_vol(argv[2], size, dynamic, id);
625 if (strncmp(argv[1], "remove", 6) == 0) {
626 /* E.g., remove volume */
628 return ubi_remove_vol(argv[2]);
631 if (strncmp(argv[1], "write", 5) == 0) {
635 printf("Please see usage\n");
639 addr = simple_strtoul(argv[2], NULL, 16);
640 size = simple_strtoul(argv[4], NULL, 16);
642 if (strlen(argv[1]) == 10 &&
643 strncmp(argv[1] + 5, ".part", 5) == 0) {
645 ret = ubi_volume_continue_write(argv[3],
649 full_size = simple_strtoul(argv[5], NULL, 16);
650 ret = ubi_volume_begin_write(argv[3],
651 (void *)addr, size, full_size);
654 ret = ubi_volume_write(argv[3], (void *)addr, size);
657 printf("%lld bytes written to volume %s\n", size,
664 if (strncmp(argv[1], "read", 4) == 0) {
667 /* E.g., read volume size */
669 size = simple_strtoul(argv[4], NULL, 16);
673 /* E.g., read volume */
675 addr = simple_strtoul(argv[2], NULL, 16);
680 return ubi_volume_read(argv[3], (char *)addr, size);
684 printf("Please see usage\n");
692 " - detach ubi from a mtd partition\n"
693 "ubi part [part] [offset]\n"
694 " - Show or set current partition (with optional VID"
696 "ubi info [l[ayout]]"
697 " - Display volume and ubi layout information\n"
698 "ubi check volumename"
699 " - check if volumename exists\n"
700 "ubi create[vol] volume [size] [type] [id]\n"
701 " - create volume name with size ('-' for maximum"
703 "ubi write[vol] address volume size"
704 " - Write volume from address with size\n"
705 "ubi write.part address volume size [fullsize]\n"
706 " - Write part of a volume from address\n"
707 "ubi read[vol] address volume [size]"
708 " - Read volume to address with size\n"
709 "ubi remove[vol] volume"
712 " volume: character name\n"
713 " size: specified in bytes\n"
714 " type: s[tatic] or d[ynamic] (default=dynamic)"