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.
20 #include <onenand_uboot.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/partitions.h>
23 #include <linux/err.h>
24 #include <ubi_uboot.h>
25 #include <linux/errno.h>
26 #include <jffs2/load_kernel.h>
29 #define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__)
31 /* Private own data */
32 static struct ubi_device *ubi;
34 #ifdef CONFIG_CMD_UBIFS
35 int ubifs_is_mounted(void);
36 void cmd_ubifs_umount(void);
39 static void display_volume_info(struct ubi_device *ubi)
43 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
45 continue; /* Empty record */
46 ubi_dump_vol_info(ubi->volumes[i]);
50 static void display_ubi_info(struct ubi_device *ubi)
52 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
53 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
54 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
55 ubi->peb_size, ubi->peb_size >> 10);
56 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
57 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
58 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
59 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
60 ubi_msg("VID header offset: %d (aligned %d)",
61 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
62 ubi_msg("data offset: %d", ubi->leb_start);
63 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
64 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
65 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
66 ubi_msg("number of user volumes: %d",
67 ubi->vol_count - UBI_INT_VOL_COUNT);
68 ubi_msg("available PEBs: %d", ubi->avail_pebs);
69 ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
70 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
72 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
75 static int ubi_info(int layout)
78 display_volume_info(ubi);
80 display_ubi_info(ubi);
85 static int ubi_check_volumename(const struct ubi_volume *vol, char *name)
87 return strcmp(vol->name, name);
90 static int ubi_check(char *name)
94 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
96 continue; /* Empty record */
98 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)
152 struct ubi_mkvol_req req;
156 req.vol_type = UBI_DYNAMIC_VOLUME;
158 req.vol_type = UBI_STATIC_VOLUME;
164 strcpy(req.name, volume);
165 req.name_len = strlen(volume);
166 req.name[req.name_len] = '\0';
168 /* It's duplicated at drivers/mtd/ubi/cdev.c */
169 err = verify_mkvol_req(ubi, &req);
171 printf("verify_mkvol_req failed %d\n", err);
174 printf("Creating %s volume %s of size %lld\n",
175 dynamic ? "dynamic" : "static", volume, size);
176 /* Call real ubi create volume */
177 return ubi_create_volume(ubi, &req);
180 static struct ubi_volume *ubi_find_volume(char *volume)
182 struct ubi_volume *vol = NULL;
185 for (i = 0; i < ubi->vtbl_slots; i++) {
186 vol = ubi->volumes[i];
187 if (vol && !strcmp(vol->name, volume))
191 printf("Volume %s not found!\n", volume);
195 static int ubi_remove_vol(char *volume)
197 int err, reserved_pebs, i;
198 struct ubi_volume *vol;
200 vol = ubi_find_volume(volume);
204 printf("Remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
207 printf("It's read-only mode\n");
212 err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL);
214 printf("Error changing Vol tabel record err=%x\n", err);
217 reserved_pebs = vol->reserved_pebs;
218 for (i = 0; i < vol->reserved_pebs; i++) {
219 err = ubi_eba_unmap_leb(ubi, vol, i);
225 ubi->volumes[vol->vol_id]->eba_tbl = NULL;
226 ubi->volumes[vol->vol_id] = NULL;
228 ubi->rsvd_pebs -= reserved_pebs;
229 ubi->avail_pebs += reserved_pebs;
230 i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
232 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
233 ubi->avail_pebs -= i;
235 ubi->beb_rsvd_pebs += i;
237 ubi_msg("reserve more %d PEBs", i);
243 ubi_err(ubi, "cannot remove volume %s, error %d", volume, err);
249 static int ubi_volume_continue_write(char *volume, void *buf, size_t size)
252 struct ubi_volume *vol;
254 vol = ubi_find_volume(volume);
258 err = ubi_more_update_data(ubi, vol, buf, size);
260 printf("Couldnt or partially wrote data\n");
267 err = ubi_check_volume(ubi, vol->vol_id);
272 ubi_warn(ubi, "volume %d on UBI device %d is corrupt",
273 vol->vol_id, ubi->ubi_num);
278 ubi_gluebi_updated(vol);
284 int ubi_volume_begin_write(char *volume, void *buf, size_t size,
289 struct ubi_volume *vol;
291 vol = ubi_find_volume(volume);
295 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
296 if (size > rsvd_bytes) {
297 printf("size > volume size! Aborting!\n");
301 err = ubi_start_update(ubi, vol, full_size);
303 printf("Cannot start volume update\n");
307 return ubi_volume_continue_write(volume, buf, size);
310 int ubi_volume_write(char *volume, void *buf, size_t size)
312 return ubi_volume_begin_write(volume, buf, size, size);
315 int ubi_volume_read(char *volume, char *buf, size_t size)
317 int err, lnum, off, len, tbuf_size;
319 unsigned long long tmp;
320 struct ubi_volume *vol;
324 vol = ubi_find_volume(volume);
332 if (vol->upd_marker) {
333 printf("damaged volume, update marker is set");
336 if (offp == vol->used_bytes)
340 printf("No size specified -> Using max size (%lld)\n", vol->used_bytes);
341 size = vol->used_bytes;
344 printf("Read %zu bytes from volume %s to %p\n", size, volume, buf);
347 printf("read from corrupted volume %d", vol->vol_id);
348 if (offp + size > vol->used_bytes)
349 size = vol->used_bytes - offp;
351 tbuf_size = vol->usable_leb_size;
352 if (size < tbuf_size)
353 tbuf_size = ALIGN(size, ubi->min_io_size);
354 tbuf = malloc_cache_aligned(tbuf_size);
359 len = size > tbuf_size ? tbuf_size : size;
362 off = do_div(tmp, vol->usable_leb_size);
366 if (off + len >= vol->usable_leb_size)
367 len = vol->usable_leb_size - off;
369 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
371 printf("read err %x\n", err);
376 if (off == vol->usable_leb_size) {
378 off -= vol->usable_leb_size;
384 memcpy(buf, tbuf, len);
387 len = size > tbuf_size ? tbuf_size : size;
391 env_set_hex("filesize", len_read);
397 static int ubi_dev_scan(struct mtd_info *info, const char *vid_header_offset)
399 char ubi_mtd_param_buffer[80];
402 if (!vid_header_offset)
403 sprintf(ubi_mtd_param_buffer, "%s", info->name);
405 sprintf(ubi_mtd_param_buffer, "%s,%s", info->name,
408 err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
421 if (mtdparts_init() != 0) {
422 printf("Error initializing mtdparts!\n");
426 #ifdef CONFIG_CMD_UBIFS
428 * Automatically unmount UBIFS partition when user
429 * changes the UBI device. Otherwise the following
430 * UBIFS commands will crash.
432 if (ubifs_is_mounted())
437 * Call ubi_exit() before re-initializing the UBI subsystem
447 int ubi_part(char *part_name, const char *vid_header_offset)
449 struct mtd_info *mtd;
455 mtd = get_mtd_device_nm(part_name);
457 printf("Partition %s not found!\n", part_name);
462 err = ubi_dev_scan(mtd, vid_header_offset);
464 printf("UBI init error %d\n", err);
465 printf("Please check, if the correct MTD partition is used (size big enough?)\n");
469 ubi = ubi_devices[0];
474 static int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
480 return CMD_RET_USAGE;
483 if (strcmp(argv[1], "detach") == 0) {
485 return CMD_RET_USAGE;
491 if (strcmp(argv[1], "part") == 0) {
492 const char *vid_header_offset = NULL;
494 /* Print current partition */
497 printf("Error, no UBI device selected!\n");
501 printf("Device %d: %s, MTD partition %s\n",
502 ubi->ubi_num, ubi->ubi_name, ubi->mtd->name);
507 return CMD_RET_USAGE;
510 vid_header_offset = argv[3];
512 return ubi_part(argv[2], vid_header_offset);
515 if ((strcmp(argv[1], "part") != 0) && !ubi) {
516 printf("Error, no UBI device selected!\n");
520 if (strcmp(argv[1], "info") == 0) {
522 if (argc > 2 && !strncmp(argv[2], "l", 1))
524 return ubi_info(layout);
527 if (strcmp(argv[1], "check") == 0) {
529 return ubi_check(argv[2]);
531 printf("Error, no volume name passed\n");
535 if (strncmp(argv[1], "create", 6) == 0) {
536 int dynamic = 1; /* default: dynamic volume */
537 int id = UBI_VOL_NUM_AUTO;
539 /* Use maximum available size */
542 /* E.g., create volume size type vol_id */
544 id = simple_strtoull(argv[5], NULL, 16);
548 /* E.g., create volume size type */
550 if (strncmp(argv[4], "s", 1) == 0)
552 else if (strncmp(argv[4], "d", 1) != 0) {
553 printf("Incorrect type\n");
558 /* E.g., create volume size */
560 if (argv[3][0] != '-')
561 size = simple_strtoull(argv[3], NULL, 16);
564 /* Use maximum available size */
566 size = (int64_t)ubi->avail_pebs * ubi->leb_size;
567 printf("No size specified -> Using max size (%lld)\n", size);
569 /* E.g., create volume */
571 return ubi_create_vol(argv[2], size, dynamic, id);
574 if (strncmp(argv[1], "remove", 6) == 0) {
575 /* E.g., remove volume */
577 return ubi_remove_vol(argv[2]);
580 if (strncmp(argv[1], "write", 5) == 0) {
584 printf("Please see usage\n");
588 addr = simple_strtoul(argv[2], NULL, 16);
589 size = simple_strtoul(argv[4], NULL, 16);
591 if (strlen(argv[1]) == 10 &&
592 strncmp(argv[1] + 5, ".part", 5) == 0) {
594 ret = ubi_volume_continue_write(argv[3],
598 full_size = simple_strtoul(argv[5], NULL, 16);
599 ret = ubi_volume_begin_write(argv[3],
600 (void *)addr, size, full_size);
603 ret = ubi_volume_write(argv[3], (void *)addr, size);
606 printf("%lld bytes written to volume %s\n", size,
613 if (strncmp(argv[1], "read", 4) == 0) {
616 /* E.g., read volume size */
618 size = simple_strtoul(argv[4], NULL, 16);
622 /* E.g., read volume */
624 addr = simple_strtoul(argv[2], NULL, 16);
629 return ubi_volume_read(argv[3], (char *)addr, size);
633 printf("Please see usage\n");
641 " - detach ubi from a mtd partition\n"
642 "ubi part [part] [offset]\n"
643 " - Show or set current partition (with optional VID"
645 "ubi info [l[ayout]]"
646 " - Display volume and ubi layout information\n"
647 "ubi check volumename"
648 " - check if volumename exists\n"
649 "ubi create[vol] volume [size] [type] [id]\n"
650 " - create volume name with size ('-' for maximum"
652 "ubi write[vol] address volume size"
653 " - Write volume from address with size\n"
654 "ubi write.part address volume size [fullsize]\n"
655 " - Write part of a volume from address\n"
656 "ubi read[vol] address volume [size]"
657 " - Read volume to address with size\n"
658 "ubi remove[vol] volume"
661 " volume: character name\n"
662 " size: specified in bytes\n"
663 " type: s[tatic] or d[ynamic] (default=dynamic)"