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 #include <ubifs_uboot.h>
53 static void display_volume_info(struct ubi_device *ubi)
57 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
59 continue; /* Empty record */
60 ubi_dump_vol_info(ubi->volumes[i]);
64 static void display_ubi_info(struct ubi_device *ubi)
66 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
67 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
68 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
69 ubi->peb_size, ubi->peb_size >> 10);
70 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
71 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
72 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
73 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
74 ubi_msg("VID header offset: %d (aligned %d)",
75 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
76 ubi_msg("data offset: %d", ubi->leb_start);
77 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
78 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
79 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
80 ubi_msg("number of user volumes: %d",
81 ubi->vol_count - UBI_INT_VOL_COUNT);
82 ubi_msg("available PEBs: %d", ubi->avail_pebs);
83 ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
84 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
86 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
89 static int ubi_info(int layout)
92 display_volume_info(ubi);
94 display_ubi_info(ubi);
99 static int ubi_check_volumename(const struct ubi_volume *vol, char *name)
101 return strcmp(vol->name, name);
104 static int ubi_check(char *name)
108 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
109 if (!ubi->volumes[i])
110 continue; /* Empty record */
112 if (!ubi_check_volumename(ubi->volumes[i], name))
120 static int verify_mkvol_req(const struct ubi_device *ubi,
121 const struct ubi_mkvol_req *req)
125 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
129 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
130 req->vol_id != UBI_VOL_NUM_AUTO)
133 if (req->alignment == 0)
136 if (req->bytes == 0) {
137 printf("No space left in UBI device!\n");
142 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
143 req->vol_type != UBI_STATIC_VOLUME)
146 if (req->alignment > ubi->leb_size)
149 n = req->alignment % ubi->min_io_size;
150 if (req->alignment != 1 && n)
153 if (req->name_len > UBI_VOL_NAME_MAX) {
154 printf("Name too long!\n");
164 static int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id)
166 struct ubi_mkvol_req req;
170 req.vol_type = UBI_DYNAMIC_VOLUME;
172 req.vol_type = UBI_STATIC_VOLUME;
178 strcpy(req.name, volume);
179 req.name_len = strlen(volume);
180 req.name[req.name_len] = '\0';
182 /* It's duplicated at drivers/mtd/ubi/cdev.c */
183 err = verify_mkvol_req(ubi, &req);
185 printf("verify_mkvol_req failed %d\n", err);
188 printf("Creating %s volume %s of size %lld\n",
189 dynamic ? "dynamic" : "static", volume, size);
190 /* Call real ubi create volume */
191 return ubi_create_volume(ubi, &req);
194 static struct ubi_volume *ubi_find_volume(char *volume)
196 struct ubi_volume *vol = NULL;
199 for (i = 0; i < ubi->vtbl_slots; i++) {
200 vol = ubi->volumes[i];
201 if (vol && !strcmp(vol->name, volume))
205 printf("Volume %s not found!\n", volume);
209 static int ubi_remove_vol(char *volume)
211 int err, reserved_pebs, i;
212 struct ubi_volume *vol;
214 vol = ubi_find_volume(volume);
218 printf("Remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
221 printf("It's read-only mode\n");
226 err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL);
228 printf("Error changing Vol tabel record err=%x\n", err);
231 reserved_pebs = vol->reserved_pebs;
232 for (i = 0; i < vol->reserved_pebs; i++) {
233 err = ubi_eba_unmap_leb(ubi, vol, i);
239 ubi->volumes[vol->vol_id]->eba_tbl = NULL;
240 ubi->volumes[vol->vol_id] = NULL;
242 ubi->rsvd_pebs -= reserved_pebs;
243 ubi->avail_pebs += reserved_pebs;
244 i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
246 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
247 ubi->avail_pebs -= i;
249 ubi->beb_rsvd_pebs += i;
251 ubi_msg("reserve more %d PEBs", i);
257 ubi_err(ubi, "cannot remove volume %s, error %d", volume, err);
263 static int ubi_volume_continue_write(char *volume, void *buf, size_t size)
266 struct ubi_volume *vol;
268 vol = ubi_find_volume(volume);
272 err = ubi_more_update_data(ubi, vol, buf, size);
274 printf("Couldnt or partially wrote data\n");
281 err = ubi_check_volume(ubi, vol->vol_id);
286 ubi_warn(ubi, "volume %d on UBI device %d is corrupt",
287 vol->vol_id, ubi->ubi_num);
292 ubi_gluebi_updated(vol);
298 int ubi_volume_begin_write(char *volume, void *buf, size_t size,
303 struct ubi_volume *vol;
305 vol = ubi_find_volume(volume);
309 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
310 if (size > rsvd_bytes) {
311 printf("size > volume size! Aborting!\n");
315 err = ubi_start_update(ubi, vol, full_size);
317 printf("Cannot start volume update\n");
321 return ubi_volume_continue_write(volume, buf, size);
324 int ubi_volume_write(char *volume, void *buf, size_t size)
326 return ubi_volume_begin_write(volume, buf, size, size);
329 int ubi_volume_read(char *volume, char *buf, size_t size)
331 int err, lnum, off, len, tbuf_size;
333 unsigned long long tmp;
334 struct ubi_volume *vol;
338 vol = ubi_find_volume(volume);
346 if (vol->upd_marker) {
347 printf("damaged volume, update marker is set");
350 if (offp == vol->used_bytes)
354 printf("No size specified -> Using max size (%lld)\n", vol->used_bytes);
355 size = vol->used_bytes;
358 printf("Read %zu bytes from volume %s to %p\n", size, volume, buf);
361 printf("read from corrupted volume %d", vol->vol_id);
362 if (offp + size > vol->used_bytes)
363 size = vol->used_bytes - offp;
365 tbuf_size = vol->usable_leb_size;
366 if (size < tbuf_size)
367 tbuf_size = ALIGN(size, ubi->min_io_size);
368 tbuf = malloc_cache_aligned(tbuf_size);
373 len = size > tbuf_size ? tbuf_size : size;
376 off = do_div(tmp, vol->usable_leb_size);
380 if (off + len >= vol->usable_leb_size)
381 len = vol->usable_leb_size - off;
383 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
385 printf("read err %x\n", err);
390 if (off == vol->usable_leb_size) {
392 off -= vol->usable_leb_size;
398 memcpy(buf, tbuf, len);
401 len = size > tbuf_size ? tbuf_size : size;
405 env_set_hex("filesize", len_read);
411 static int ubi_dev_scan(struct mtd_info *info, char *ubidev,
412 const char *vid_header_offset)
414 struct mtd_device *dev;
415 struct part_info *part;
416 struct mtd_partition mtd_part;
417 char ubi_mtd_param_buffer[80];
421 if (find_dev_and_part(ubidev, &dev, &pnum, &part) != 0)
424 sprintf(buffer, "mtd=%d", pnum);
425 memset(&mtd_part, 0, sizeof(mtd_part));
426 mtd_part.name = buffer;
427 mtd_part.size = part->size;
428 mtd_part.offset = part->offset;
429 add_mtd_partitions(info, &mtd_part, 1);
431 strcpy(ubi_mtd_param_buffer, buffer);
432 if (vid_header_offset)
433 sprintf(ubi_mtd_param_buffer, "mtd=%d,%s", pnum,
435 err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
437 del_mtd_partitions(info);
443 del_mtd_partitions(info);
454 if (mtdparts_init() != 0) {
455 printf("Error initializing mtdparts!\n");
459 #ifdef CONFIG_CMD_UBIFS
461 * Automatically unmount UBIFS partition when user
462 * changes the UBI device. Otherwise the following
463 * UBIFS commands will crash.
465 if (ubifs_is_mounted())
470 * Call ubi_exit() before re-initializing the UBI subsystem
472 if (ubi_initialized) {
474 del_mtd_partitions(ubi_dev.mtd_info);
478 ubi_dev.selected = 0;
482 int ubi_part(char *part_name, const char *vid_header_offset)
486 struct mtd_device *dev;
487 struct part_info *part;
492 * Search the mtd device number where this partition
495 if (find_dev_and_part(part_name, &dev, &pnum, &part)) {
496 printf("Partition %s not found!\n", part_name);
499 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(dev->id->type), dev->id->num);
500 ubi_dev.mtd_info = get_mtd_device_nm(mtd_dev);
501 if (IS_ERR(ubi_dev.mtd_info)) {
502 printf("Partition %s not found on device %s!\n", part_name,
507 ubi_dev.selected = 1;
509 strcpy(ubi_dev.part_name, part_name);
510 err = ubi_dev_scan(ubi_dev.mtd_info, ubi_dev.part_name,
513 printf("UBI init error %d\n", err);
514 printf("Please check, if the correct MTD partition is used (size big enough?)\n");
515 ubi_dev.selected = 0;
519 ubi = ubi_devices[0];
524 static int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
530 return CMD_RET_USAGE;
533 if (strcmp(argv[1], "detach") == 0) {
535 return CMD_RET_USAGE;
541 if (strcmp(argv[1], "part") == 0) {
542 const char *vid_header_offset = NULL;
544 /* Print current partition */
546 if (!ubi_dev.selected) {
547 printf("Error, no UBI device/partition selected!\n");
551 printf("Device %d: %s, partition %s\n",
552 ubi_dev.nr, ubi_dev.mtd_info->name, ubi_dev.part_name);
557 return CMD_RET_USAGE;
560 vid_header_offset = argv[3];
562 return ubi_part(argv[2], vid_header_offset);
565 if ((strcmp(argv[1], "part") != 0) && (!ubi_dev.selected)) {
566 printf("Error, no UBI device/partition selected!\n");
570 if (strcmp(argv[1], "info") == 0) {
572 if (argc > 2 && !strncmp(argv[2], "l", 1))
574 return ubi_info(layout);
577 if (strcmp(argv[1], "check") == 0) {
579 return ubi_check(argv[2]);
581 printf("Error, no volume name passed\n");
585 if (strncmp(argv[1], "create", 6) == 0) {
586 int dynamic = 1; /* default: dynamic volume */
587 int id = UBI_VOL_NUM_AUTO;
589 /* Use maximum available size */
592 /* E.g., create volume size type vol_id */
594 id = simple_strtoull(argv[5], NULL, 16);
598 /* E.g., create volume size type */
600 if (strncmp(argv[4], "s", 1) == 0)
602 else if (strncmp(argv[4], "d", 1) != 0) {
603 printf("Incorrect type\n");
608 /* E.g., create volume size */
610 if (argv[3][0] != '-')
611 size = simple_strtoull(argv[3], NULL, 16);
614 /* Use maximum available size */
616 size = (int64_t)ubi->avail_pebs * ubi->leb_size;
617 printf("No size specified -> Using max size (%lld)\n", size);
619 /* E.g., create volume */
621 return ubi_create_vol(argv[2], size, dynamic, id);
624 if (strncmp(argv[1], "remove", 6) == 0) {
625 /* E.g., remove volume */
627 return ubi_remove_vol(argv[2]);
630 if (strncmp(argv[1], "write", 5) == 0) {
634 printf("Please see usage\n");
638 addr = simple_strtoul(argv[2], NULL, 16);
639 size = simple_strtoul(argv[4], NULL, 16);
641 if (strlen(argv[1]) == 10 &&
642 strncmp(argv[1] + 5, ".part", 5) == 0) {
644 ret = ubi_volume_continue_write(argv[3],
648 full_size = simple_strtoul(argv[5], NULL, 16);
649 ret = ubi_volume_begin_write(argv[3],
650 (void *)addr, size, full_size);
653 ret = ubi_volume_write(argv[3], (void *)addr, size);
656 printf("%lld bytes written to volume %s\n", size,
663 if (strncmp(argv[1], "read", 4) == 0) {
666 /* E.g., read volume size */
668 size = simple_strtoul(argv[4], NULL, 16);
672 /* E.g., read volume */
674 addr = simple_strtoul(argv[2], NULL, 16);
679 return ubi_volume_read(argv[3], (char *)addr, size);
683 printf("Please see usage\n");
691 " - detach ubi from a mtd partition\n"
692 "ubi part [part] [offset]\n"
693 " - Show or set current partition (with optional VID"
695 "ubi info [l[ayout]]"
696 " - Display volume and ubi layout information\n"
697 "ubi check volumename"
698 " - check if volumename exists\n"
699 "ubi create[vol] volume [size] [type] [id]\n"
700 " - create volume name with size ('-' for maximum"
702 "ubi write[vol] address volume size"
703 " - Write volume from address with size\n"
704 "ubi write.part address volume size [fullsize]\n"
705 " - Write part of a volume from address\n"
706 "ubi read[vol] address volume [size]"
707 " - Read volume to address with size\n"
708 "ubi remove[vol] volume"
711 " volume: character name\n"
712 " size: specified in bytes\n"
713 " type: s[tatic] or d[ynamic] (default=dynamic)"