2 * Unsorted Block Image commands
4 * Copyright (C) 2008 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
7 * Copyright 2008 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
30 /* Private own data */
31 static struct ubi_device *ubi;
34 char dev_name[32]; /* NAND/OneNAND etc */
38 struct mtd_info *mtd_info;
41 static struct selected_dev ubi_dev;
43 static void ubi_dump_vol_info(const struct ubi_volume *vol)
45 ubi_msg("volume information dump:");
46 ubi_msg("vol_id %d", vol->vol_id);
47 ubi_msg("reserved_pebs %d", vol->reserved_pebs);
48 ubi_msg("alignment %d", vol->alignment);
49 ubi_msg("data_pad %d", vol->data_pad);
50 ubi_msg("vol_type %d", vol->vol_type);
51 ubi_msg("name_len %d", vol->name_len);
52 ubi_msg("usable_leb_size %d", vol->usable_leb_size);
53 ubi_msg("used_ebs %d", vol->used_ebs);
54 ubi_msg("used_bytes %lld", vol->used_bytes);
55 ubi_msg("last_eb_bytes %d", vol->last_eb_bytes);
56 ubi_msg("corrupted %d", vol->corrupted);
57 ubi_msg("upd_marker %d", vol->upd_marker);
59 if (vol->name_len <= UBI_VOL_NAME_MAX &&
60 strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
61 ubi_msg("name %s", vol->name);
63 ubi_msg("the 1st 5 characters of the name: %c%c%c%c%c",
64 vol->name[0], vol->name[1], vol->name[2],
65 vol->name[3], vol->name[4]);
70 static void display_volume_info(struct ubi_device *ubi)
74 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
76 continue; /* Empty record */
77 ubi_dump_vol_info(ubi->volumes[i]);
81 static void display_ubi_info(struct ubi_device *ubi)
83 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
84 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
85 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
86 ubi->peb_size, ubi->peb_size >> 10);
87 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
88 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
89 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
90 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
91 ubi_msg("VID header offset: %d (aligned %d)",
92 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
93 ubi_msg("data offset: %d", ubi->leb_start);
94 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
95 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
96 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
97 ubi_msg("number of user volumes: %d",
98 ubi->vol_count - UBI_INT_VOL_COUNT);
99 ubi_msg("available PEBs: %d", ubi->avail_pebs);
100 ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
101 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
103 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
106 static int ubi_info(int layout)
109 display_volume_info(ubi);
111 display_ubi_info(ubi);
116 static int parse_num(size_t *num, const char *token)
121 n = (size_t) ustrtoul(token, &endp, 0);
129 static int verify_mkvol_req(const struct ubi_device *ubi,
130 const struct ubi_mkvol_req *req)
132 int n, err = -EINVAL;
134 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
138 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
139 req->vol_id != UBI_VOL_NUM_AUTO)
142 if (req->alignment == 0)
148 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
149 req->vol_type != UBI_STATIC_VOLUME)
152 if (req->alignment > ubi->leb_size)
155 n = req->alignment % ubi->min_io_size;
156 if (req->alignment != 1 && n)
159 if (req->name_len > UBI_VOL_NAME_MAX) {
166 printf("bad volume creation request");
170 static int ubi_create_vol(char *volume, int size, int dynamic)
172 struct ubi_mkvol_req req;
176 req.vol_type = UBI_DYNAMIC_VOLUME;
178 req.vol_type = UBI_STATIC_VOLUME;
180 req.vol_id = UBI_VOL_NUM_AUTO;
184 strcpy(req.name, volume);
185 req.name_len = strlen(volume);
186 req.name[req.name_len] = '\0';
188 /* It's duplicated at drivers/mtd/ubi/cdev.c */
189 err = verify_mkvol_req(ubi, &req);
191 printf("verify_mkvol_req failed %d\n", err);
194 printf("Creating %s volume %s of size %d\n",
195 dynamic ? "dynamic" : "static", volume, size);
196 /* Call real ubi create volume */
197 return ubi_create_volume(ubi, &req);
200 static int ubi_remove_vol(char *volume)
202 int i, err, reserved_pebs;
203 int found = 0, vol_id = 0;
204 struct ubi_volume *vol;
206 for (i = 0; i < ubi->vtbl_slots; i++) {
207 vol = ubi->volumes[i];
208 if (vol && !strcmp(vol->name, volume)) {
209 printf("Volume %s found at valid %d\n", volume, i);
216 printf("%s volume not found\n", 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_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_id]->eba_tbl = NULL;
241 ubi->volumes[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("cannot remove volume %d, error %d", vol_id, err);
262 static int ubi_volume_write(char *volume, void *buf, size_t size)
267 struct ubi_volume *vol;
269 for (i = 0; i < ubi->vtbl_slots; i++) {
270 vol = ubi->volumes[i];
271 if (vol && !strcmp(vol->name, volume)) {
272 printf("Volume \"%s\" found at volume id %d\n", volume, i);
278 printf("%s volume not found\n", volume);
281 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
282 if (size < 0 || size > rsvd_bytes) {
283 printf("rsvd_bytes=%d vol->reserved_pebs=%d ubi->leb_size=%d\n",
284 rsvd_bytes, vol->reserved_pebs, ubi->leb_size);
285 printf("vol->data_pad=%d\n", vol->data_pad);
286 printf("Size > volume size !!\n");
290 err = ubi_start_update(ubi, vol, size);
292 printf("Cannot start volume update\n");
296 err = ubi_more_update_data(ubi, vol, buf, size);
298 printf("Couldnt or partially wrote data \n");
305 err = ubi_check_volume(ubi, vol->vol_id);
310 ubi_warn("volume %d on UBI device %d is corrupted",
311 vol->vol_id, ubi->ubi_num);
316 ubi_gluebi_updated(vol);
322 static int ubi_volume_read(char *volume, char *buf, size_t size)
324 int err, lnum, off, len, tbuf_size, i = 0;
325 size_t count_save = size;
327 unsigned long long tmp;
328 struct ubi_volume *vol = NULL;
331 for (i = 0; i < ubi->vtbl_slots; i++) {
332 vol = ubi->volumes[i];
333 if (vol && !strcmp(vol->name, volume)) {
334 printf("Volume %s found at volume id %d\n",
335 volume, vol->vol_id);
339 if (i == ubi->vtbl_slots) {
340 printf("%s volume not found\n", volume);
344 printf("read %i bytes from volume %d to %x(buf address)\n",
345 (int) size, vol->vol_id, (unsigned)buf);
351 if (vol->upd_marker) {
352 printf("damaged volume, update marker is set");
355 if (offp == vol->used_bytes)
359 printf("Read [%lu] bytes\n", (unsigned long) vol->used_bytes);
360 size = vol->used_bytes;
364 printf("read from corrupted volume %d", vol->vol_id);
365 if (offp + size > vol->used_bytes)
366 count_save = size = vol->used_bytes - offp;
368 tbuf_size = vol->usable_leb_size;
369 if (size < tbuf_size)
370 tbuf_size = ALIGN(size, ubi->min_io_size);
371 tbuf = malloc(tbuf_size);
376 len = size > tbuf_size ? tbuf_size : size;
379 off = do_div(tmp, vol->usable_leb_size);
381 printf("off=%d lnum=%d\n", off, lnum);
383 if (off + len >= vol->usable_leb_size)
384 len = vol->usable_leb_size - off;
386 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
388 printf("read err %x\n", err);
392 if (off == vol->usable_leb_size) {
394 off -= vol->usable_leb_size;
400 printf("buf = %x\n", (unsigned)buf);
401 memcpy(buf, tbuf, len);
402 printf("buf[0] = %x\n", buf[0]);
405 len = size > tbuf_size ? tbuf_size : size;
409 return err ? err : count_save - size;
412 static int ubi_dev_scan(struct mtd_info *info, char *ubidev)
414 struct mtd_device *dev;
415 struct part_info *part;
416 struct mtd_partition mtd_part;
421 if (mtdparts_init() != 0)
424 if (find_dev_and_part(ubidev, &dev, &pnum, &part) != 0)
427 sprintf(buffer, "mtd=%d", pnum);
428 memset(&mtd_part, 0, sizeof(mtd_part));
429 mtd_part.name = buffer;
430 mtd_part.size = part->size;
431 mtd_part.offset = part->offset;
432 add_mtd_partitions(info, &mtd_part, 1);
434 err = ubi_mtd_param_parse(buffer, NULL);
436 del_mtd_partitions(info);
442 del_mtd_partitions(info);
449 static int do_ubi(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
456 printf("Usage:\n%s\n", cmdtp->usage);
460 if (strcmp(argv[1], "part") == 0) {
461 /* Print current partition */
463 if (ubi_dev.type == DEV_TYPE_NONE) {
464 printf("Error, no UBI device/partition selected!\n");
468 printf("%s Device %d: %s, partition %s\n", ubi_dev.dev_name,
469 ubi_dev.nr, ubi_dev.mtd_info->name, ubi_dev.part_name);
474 printf("Usage:\n%s\n", cmdtp->usage);
478 /* todo: get dev number for NAND... */
482 * Check for nand|onenand selection
484 #if defined(CONFIG_CMD_NAND)
485 if (strcmp(argv[2], "nand") == 0) {
486 strcpy(ubi_dev.dev_name, "NAND");
487 ubi_dev.type = DEV_TYPE_NAND;
488 ubi_dev.mtd_info = &nand_info[ubi_dev.nr];
491 #if defined(CONFIG_CMD_ONENAND)
492 if (strcmp(argv[2], "onenand") == 0) {
493 strcpy(ubi_dev.dev_name, "OneNAND");
494 ubi_dev.type = DEV_TYPE_ONENAND;
495 ubi_dev.mtd_info = &onenand_mtd;
499 if (ubi_dev.type == DEV_TYPE_NONE) {
500 printf("Error, no UBI device/partition selected!\n");
504 strcpy(ubi_dev.part_name, argv[3]);
505 err = ubi_dev_scan(ubi_dev.mtd_info, ubi_dev.part_name);
507 printf("UBI init error %d\n", err);
511 ubi = ubi_devices[0];
516 if ((strcmp(argv[1], "part") != 0) && (ubi_dev.type == DEV_TYPE_NONE)) {
517 printf("Error, no UBI device/partition selected!\n");
521 if (strcmp(argv[1], "info") == 0) {
523 if (argc > 2 && !strncmp(argv[2], "l", 1))
525 return ubi_info(layout);
528 if (strncmp(argv[1], "create", 6) == 0) {
529 int dynamic = 1; /* default: dynamic volume */
531 /* Use maximum available size */
534 /* E.g., create volume size type */
536 if (strncmp(argv[4], "s", 1) == 0)
538 else if (strncmp(argv[4], "d", 1) != 0) {
539 printf("Incorrect type\n");
544 /* E.g., create volume size */
546 err = parse_num(&size, argv[3]);
548 printf("Incorrect type\n");
553 /* Use maximum available size */
555 size = ubi->avail_pebs * ubi->leb_size;
556 /* E.g., create volume */
558 return ubi_create_vol(argv[2], size, dynamic);
561 if (strncmp(argv[1], "remove", 6) == 0) {
562 /* E.g., remove volume */
564 return ubi_remove_vol(argv[2]);
567 if (strncmp(argv[1], "write", 5) == 0) {
569 printf("Please see usage\n");
573 addr = simple_strtoul(argv[2], NULL, 16);
574 err = parse_num(&size, argv[4]);
576 printf("Please see usage\n");
580 return ubi_volume_write(argv[3], (void *)addr, size);
583 if (strncmp(argv[1], "read", 4) == 0) {
586 /* E.g., read volume size */
588 err = parse_num(&size, argv[4]);
590 printf("Please see usage\n");
596 /* E.g., read volume */
598 addr = simple_strtoul(argv[2], NULL, 16);
603 return ubi_volume_read(argv[3], (char *)addr, size);
606 printf("Please see usage\n");
610 U_BOOT_CMD(ubi, 6, 1, do_ubi,
611 "ubi - ubi commands\n",
612 "part [nand|onenand] [part]"
613 " - Show or set current partition\n"
614 "ubi info [l[ayout]]"
615 " - Display volume and ubi layout information\n"
616 "ubi create[vol] volume [size] [type]"
617 " - create volume name with size\n"
618 "ubi write[vol] address volume size"
619 " - Write volume from address with size\n"
620 "ubi read[vol] address volume [size]"
621 " - Read volume to address with size\n"
622 "ubi remove[vol] volume"
625 " volume: charater name\n"
626 " size: KiB, MiB, GiB, and bytes\n"
627 " type: s[tatic] or d[ynamic] (default=dynamic)\n"