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 <ubi_uboot.h>
23 #include <asm/errno.h>
24 #include <jffs2/load_kernel.h>
26 #define DEV_TYPE_NONE 0
27 #define DEV_TYPE_NAND 1
28 #define DEV_TYPE_ONENAND 2
29 #define DEV_TYPE_NOR 3
31 /* Private own data */
32 static struct ubi_device *ubi;
33 static char buffer[80];
34 static int ubi_initialized;
40 struct mtd_info *mtd_info;
43 static struct selected_dev ubi_dev;
45 #ifdef CONFIG_CMD_UBIFS
46 int ubifs_is_mounted(void);
47 void cmd_ubifs_umount(void);
50 static void ubi_dump_vol_info(const struct ubi_volume *vol)
52 ubi_msg("volume information dump:");
53 ubi_msg("vol_id %d", vol->vol_id);
54 ubi_msg("reserved_pebs %d", vol->reserved_pebs);
55 ubi_msg("alignment %d", vol->alignment);
56 ubi_msg("data_pad %d", vol->data_pad);
57 ubi_msg("vol_type %d", vol->vol_type);
58 ubi_msg("name_len %d", vol->name_len);
59 ubi_msg("usable_leb_size %d", vol->usable_leb_size);
60 ubi_msg("used_ebs %d", vol->used_ebs);
61 ubi_msg("used_bytes %lld", vol->used_bytes);
62 ubi_msg("last_eb_bytes %d", vol->last_eb_bytes);
63 ubi_msg("corrupted %d", vol->corrupted);
64 ubi_msg("upd_marker %d", vol->upd_marker);
66 if (vol->name_len <= UBI_VOL_NAME_MAX &&
67 strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
68 ubi_msg("name %s", vol->name);
70 ubi_msg("the 1st 5 characters of the name: %c%c%c%c%c",
71 vol->name[0], vol->name[1], vol->name[2],
72 vol->name[3], vol->name[4]);
77 static void display_volume_info(struct ubi_device *ubi)
81 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
83 continue; /* Empty record */
84 ubi_dump_vol_info(ubi->volumes[i]);
88 static void display_ubi_info(struct ubi_device *ubi)
90 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
91 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
92 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
93 ubi->peb_size, ubi->peb_size >> 10);
94 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
95 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
96 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
97 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
98 ubi_msg("VID header offset: %d (aligned %d)",
99 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
100 ubi_msg("data offset: %d", ubi->leb_start);
101 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
102 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
103 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
104 ubi_msg("number of user volumes: %d",
105 ubi->vol_count - UBI_INT_VOL_COUNT);
106 ubi_msg("available PEBs: %d", ubi->avail_pebs);
107 ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
108 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
110 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
113 static int ubi_info(int layout)
116 display_volume_info(ubi);
118 display_ubi_info(ubi);
123 static int verify_mkvol_req(const struct ubi_device *ubi,
124 const struct ubi_mkvol_req *req)
126 int n, err = -EINVAL;
128 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
132 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
133 req->vol_id != UBI_VOL_NUM_AUTO)
136 if (req->alignment == 0)
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) {
160 printf("bad volume creation request");
164 static int ubi_create_vol(char *volume, int size, int dynamic)
166 struct ubi_mkvol_req req;
170 req.vol_type = UBI_DYNAMIC_VOLUME;
172 req.vol_type = UBI_STATIC_VOLUME;
174 req.vol_id = UBI_VOL_NUM_AUTO;
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 %d\n",
189 dynamic ? "dynamic" : "static", volume, size);
190 /* Call real ubi create volume */
191 return ubi_create_volume(ubi, &req);
194 static int ubi_remove_vol(char *volume)
196 int i, err, reserved_pebs;
197 int found = 0, vol_id = 0;
198 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)) {
203 printf("Volume %s found at valid %d\n", volume, i);
210 printf("%s volume not found\n", volume);
213 printf("remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
216 printf("It's read-only mode\n");
221 err = ubi_change_vtbl_record(ubi, vol_id, NULL);
223 printf("Error changing Vol tabel record err=%x\n", err);
226 reserved_pebs = vol->reserved_pebs;
227 for (i = 0; i < vol->reserved_pebs; i++) {
228 err = ubi_eba_unmap_leb(ubi, vol, i);
234 ubi->volumes[vol_id]->eba_tbl = NULL;
235 ubi->volumes[vol_id] = NULL;
237 ubi->rsvd_pebs -= reserved_pebs;
238 ubi->avail_pebs += reserved_pebs;
239 i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
241 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
242 ubi->avail_pebs -= i;
244 ubi->beb_rsvd_pebs += i;
246 ubi_msg("reserve more %d PEBs", i);
252 ubi_err("cannot remove volume %d, error %d", vol_id, err);
256 static int ubi_volume_write(char *volume, void *buf, size_t size)
261 struct ubi_volume *vol;
263 for (i = 0; i < ubi->vtbl_slots; i++) {
264 vol = ubi->volumes[i];
265 if (vol && !strcmp(vol->name, volume)) {
266 printf("Volume \"%s\" found at volume id %d\n", volume, i);
272 printf("%s volume not found\n", volume);
275 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
276 if (size < 0 || size > rsvd_bytes) {
277 printf("rsvd_bytes=%d vol->reserved_pebs=%d ubi->leb_size=%d\n",
278 rsvd_bytes, vol->reserved_pebs, ubi->leb_size);
279 printf("vol->data_pad=%d\n", vol->data_pad);
280 printf("Size > volume size !!\n");
284 err = ubi_start_update(ubi, vol, size);
286 printf("Cannot start volume update\n");
290 err = ubi_more_update_data(ubi, vol, buf, size);
292 printf("Couldnt or partially wrote data \n");
299 err = ubi_check_volume(ubi, vol->vol_id);
304 ubi_warn("volume %d on UBI device %d is corrupted",
305 vol->vol_id, ubi->ubi_num);
310 ubi_gluebi_updated(vol);
316 static int ubi_volume_read(char *volume, char *buf, size_t size)
318 int err, lnum, off, len, tbuf_size, i = 0;
319 size_t count_save = size;
321 unsigned long long tmp;
322 struct ubi_volume *vol = NULL;
325 for (i = 0; i < ubi->vtbl_slots; i++) {
326 vol = ubi->volumes[i];
327 if (vol && !strcmp(vol->name, volume)) {
328 printf("Volume %s found at volume id %d\n",
329 volume, vol->vol_id);
333 if (i == ubi->vtbl_slots) {
334 printf("%s volume not found\n", volume);
338 printf("read %i bytes from volume %d to %x(buf address)\n",
339 (int) size, vol->vol_id, (unsigned)buf);
345 if (vol->upd_marker) {
346 printf("damaged volume, update marker is set");
349 if (offp == vol->used_bytes)
353 printf("Read [%lu] bytes\n", (unsigned long) vol->used_bytes);
354 size = vol->used_bytes;
358 printf("read from corrupted volume %d", vol->vol_id);
359 if (offp + size > vol->used_bytes)
360 count_save = size = vol->used_bytes - offp;
362 tbuf_size = vol->usable_leb_size;
363 if (size < tbuf_size)
364 tbuf_size = ALIGN(size, ubi->min_io_size);
365 tbuf = malloc(tbuf_size);
370 len = size > tbuf_size ? tbuf_size : size;
373 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);
385 if (off == vol->usable_leb_size) {
387 off -= vol->usable_leb_size;
393 memcpy(buf, tbuf, len);
396 len = size > tbuf_size ? tbuf_size : size;
400 return err ? err : count_save - size;
403 static int ubi_dev_scan(struct mtd_info *info, char *ubidev,
404 const char *vid_header_offset)
406 struct mtd_device *dev;
407 struct part_info *part;
408 struct mtd_partition mtd_part;
409 char ubi_mtd_param_buffer[80];
413 if (find_dev_and_part(ubidev, &dev, &pnum, &part) != 0)
416 sprintf(buffer, "mtd=%d", pnum);
417 memset(&mtd_part, 0, sizeof(mtd_part));
418 mtd_part.name = buffer;
419 mtd_part.size = part->size;
420 mtd_part.offset = part->offset;
421 add_mtd_partitions(info, &mtd_part, 1);
423 strcpy(ubi_mtd_param_buffer, buffer);
424 if (vid_header_offset)
425 sprintf(ubi_mtd_param_buffer, "mtd=%d,%s", pnum,
427 err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
429 del_mtd_partitions(info);
435 del_mtd_partitions(info);
444 static int do_ubi(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
451 return cmd_usage(cmdtp);
453 if (mtdparts_init() != 0) {
454 printf("Error initializing mtdparts!\n");
458 if (strcmp(argv[1], "part") == 0) {
460 struct mtd_device *dev;
461 struct part_info *part;
462 const char *vid_header_offset = NULL;
465 /* Print current partition */
467 if (!ubi_dev.selected) {
468 printf("Error, no UBI device/partition selected!\n");
472 printf("Device %d: %s, partition %s\n",
473 ubi_dev.nr, ubi_dev.mtd_info->name, ubi_dev.part_name);
478 return cmd_usage(cmdtp);
480 #ifdef CONFIG_CMD_UBIFS
482 * Automatically unmount UBIFS partition when user
483 * changes the UBI device. Otherwise the following
484 * UBIFS commands will crash.
486 if (ubifs_is_mounted())
490 /* todo: get dev number for NAND... */
494 * Call ubi_exit() before re-initializing the UBI subsystem
496 if (ubi_initialized) {
498 del_mtd_partitions(ubi_dev.mtd_info);
502 * Search the mtd device number where this partition
505 if (find_dev_and_part(argv[2], &dev, &pnum, &part)) {
506 printf("Partition %s not found!\n", argv[2]);
509 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(dev->id->type), dev->id->num);
510 ubi_dev.mtd_info = get_mtd_device_nm(mtd_dev);
511 if (IS_ERR(ubi_dev.mtd_info)) {
512 printf("Partition %s not found on device %s!\n", argv[2], mtd_dev);
516 ubi_dev.selected = 1;
519 vid_header_offset = argv[3];
520 strcpy(ubi_dev.part_name, argv[2]);
521 err = ubi_dev_scan(ubi_dev.mtd_info, ubi_dev.part_name,
524 printf("UBI init error %d\n", err);
525 ubi_dev.selected = 0;
529 ubi = ubi_devices[0];
534 if ((strcmp(argv[1], "part") != 0) && (!ubi_dev.selected)) {
535 printf("Error, no UBI device/partition selected!\n");
539 if (strcmp(argv[1], "info") == 0) {
541 if (argc > 2 && !strncmp(argv[2], "l", 1))
543 return ubi_info(layout);
546 if (strncmp(argv[1], "create", 6) == 0) {
547 int dynamic = 1; /* default: dynamic volume */
549 /* Use maximum available size */
552 /* E.g., create volume size type */
554 if (strncmp(argv[4], "s", 1) == 0)
556 else if (strncmp(argv[4], "d", 1) != 0) {
557 printf("Incorrect type\n");
562 /* E.g., create volume size */
564 size = simple_strtoul(argv[3], NULL, 16);
567 /* Use maximum available size */
569 size = ubi->avail_pebs * ubi->leb_size;
570 /* E.g., create volume */
572 return ubi_create_vol(argv[2], size, dynamic);
575 if (strncmp(argv[1], "remove", 6) == 0) {
576 /* E.g., remove volume */
578 return ubi_remove_vol(argv[2]);
581 if (strncmp(argv[1], "write", 5) == 0) {
583 printf("Please see usage\n");
587 addr = simple_strtoul(argv[2], NULL, 16);
588 size = simple_strtoul(argv[4], NULL, 16);
590 return ubi_volume_write(argv[3], (void *)addr, size);
593 if (strncmp(argv[1], "read", 4) == 0) {
596 /* E.g., read volume size */
598 size = simple_strtoul(argv[4], NULL, 16);
602 /* E.g., read volume */
604 addr = simple_strtoul(argv[2], NULL, 16);
609 return ubi_volume_read(argv[3], (char *)addr, size);
612 printf("Please see usage\n");
619 "part [part] [offset]\n"
620 " - Show or set current partition (with optional VID"
622 "ubi info [l[ayout]]"
623 " - Display volume and ubi layout information\n"
624 "ubi create[vol] volume [size] [type]"
625 " - create volume name with size\n"
626 "ubi write[vol] address volume size"
627 " - Write volume from address with size\n"
628 "ubi read[vol] address volume [size]"
629 " - Read volume to address with size\n"
630 "ubi remove[vol] volume"
633 " volume: character name\n"
634 " size: specified in bytes\n"
635 " type: s[tatic] or d[ynamic] (default=dynamic)"