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;
32 static char buffer[80];
35 char dev_name[32]; /* NAND/OneNAND etc */
39 struct mtd_info *mtd_info;
42 static struct selected_dev ubi_dev;
44 static void ubi_dump_vol_info(const struct ubi_volume *vol)
46 ubi_msg("volume information dump:");
47 ubi_msg("vol_id %d", vol->vol_id);
48 ubi_msg("reserved_pebs %d", vol->reserved_pebs);
49 ubi_msg("alignment %d", vol->alignment);
50 ubi_msg("data_pad %d", vol->data_pad);
51 ubi_msg("vol_type %d", vol->vol_type);
52 ubi_msg("name_len %d", vol->name_len);
53 ubi_msg("usable_leb_size %d", vol->usable_leb_size);
54 ubi_msg("used_ebs %d", vol->used_ebs);
55 ubi_msg("used_bytes %lld", vol->used_bytes);
56 ubi_msg("last_eb_bytes %d", vol->last_eb_bytes);
57 ubi_msg("corrupted %d", vol->corrupted);
58 ubi_msg("upd_marker %d", vol->upd_marker);
60 if (vol->name_len <= UBI_VOL_NAME_MAX &&
61 strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
62 ubi_msg("name %s", vol->name);
64 ubi_msg("the 1st 5 characters of the name: %c%c%c%c%c",
65 vol->name[0], vol->name[1], vol->name[2],
66 vol->name[3], vol->name[4]);
71 static void display_volume_info(struct ubi_device *ubi)
75 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
77 continue; /* Empty record */
78 ubi_dump_vol_info(ubi->volumes[i]);
82 static void display_ubi_info(struct ubi_device *ubi)
84 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
85 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
86 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
87 ubi->peb_size, ubi->peb_size >> 10);
88 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
89 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
90 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
91 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
92 ubi_msg("VID header offset: %d (aligned %d)",
93 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
94 ubi_msg("data offset: %d", ubi->leb_start);
95 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
96 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
97 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
98 ubi_msg("number of user volumes: %d",
99 ubi->vol_count - UBI_INT_VOL_COUNT);
100 ubi_msg("available PEBs: %d", ubi->avail_pebs);
101 ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
102 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
104 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
107 static int ubi_info(int layout)
110 display_volume_info(ubi);
112 display_ubi_info(ubi);
117 static int verify_mkvol_req(const struct ubi_device *ubi,
118 const struct ubi_mkvol_req *req)
120 int n, err = -EINVAL;
122 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
126 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
127 req->vol_id != UBI_VOL_NUM_AUTO)
130 if (req->alignment == 0)
136 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
137 req->vol_type != UBI_STATIC_VOLUME)
140 if (req->alignment > ubi->leb_size)
143 n = req->alignment % ubi->min_io_size;
144 if (req->alignment != 1 && n)
147 if (req->name_len > UBI_VOL_NAME_MAX) {
154 printf("bad volume creation request");
158 static int ubi_create_vol(char *volume, int size, int dynamic)
160 struct ubi_mkvol_req req;
164 req.vol_type = UBI_DYNAMIC_VOLUME;
166 req.vol_type = UBI_STATIC_VOLUME;
168 req.vol_id = UBI_VOL_NUM_AUTO;
172 strcpy(req.name, volume);
173 req.name_len = strlen(volume);
174 req.name[req.name_len] = '\0';
176 /* It's duplicated at drivers/mtd/ubi/cdev.c */
177 err = verify_mkvol_req(ubi, &req);
179 printf("verify_mkvol_req failed %d\n", err);
182 printf("Creating %s volume %s of size %d\n",
183 dynamic ? "dynamic" : "static", volume, size);
184 /* Call real ubi create volume */
185 return ubi_create_volume(ubi, &req);
188 static int ubi_remove_vol(char *volume)
190 int i, err, reserved_pebs;
191 int found = 0, vol_id = 0;
192 struct ubi_volume *vol;
194 for (i = 0; i < ubi->vtbl_slots; i++) {
195 vol = ubi->volumes[i];
196 if (vol && !strcmp(vol->name, volume)) {
197 printf("Volume %s found at valid %d\n", volume, i);
204 printf("%s volume not found\n", volume);
207 printf("remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
210 printf("It's read-only mode\n");
215 err = ubi_change_vtbl_record(ubi, vol_id, NULL);
217 printf("Error changing Vol tabel record err=%x\n", err);
220 reserved_pebs = vol->reserved_pebs;
221 for (i = 0; i < vol->reserved_pebs; i++) {
222 err = ubi_eba_unmap_leb(ubi, vol, i);
228 ubi->volumes[vol_id]->eba_tbl = NULL;
229 ubi->volumes[vol_id] = NULL;
231 ubi->rsvd_pebs -= reserved_pebs;
232 ubi->avail_pebs += reserved_pebs;
233 i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
235 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
236 ubi->avail_pebs -= i;
238 ubi->beb_rsvd_pebs += i;
240 ubi_msg("reserve more %d PEBs", i);
246 ubi_err("cannot remove volume %d, error %d", vol_id, err);
250 static int ubi_volume_write(char *volume, void *buf, size_t size)
255 struct ubi_volume *vol;
257 for (i = 0; i < ubi->vtbl_slots; i++) {
258 vol = ubi->volumes[i];
259 if (vol && !strcmp(vol->name, volume)) {
260 printf("Volume \"%s\" found at volume id %d\n", volume, i);
266 printf("%s volume not found\n", volume);
269 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
270 if (size < 0 || size > rsvd_bytes) {
271 printf("rsvd_bytes=%d vol->reserved_pebs=%d ubi->leb_size=%d\n",
272 rsvd_bytes, vol->reserved_pebs, ubi->leb_size);
273 printf("vol->data_pad=%d\n", vol->data_pad);
274 printf("Size > volume size !!\n");
278 err = ubi_start_update(ubi, vol, size);
280 printf("Cannot start volume update\n");
284 err = ubi_more_update_data(ubi, vol, buf, size);
286 printf("Couldnt or partially wrote data \n");
293 err = ubi_check_volume(ubi, vol->vol_id);
298 ubi_warn("volume %d on UBI device %d is corrupted",
299 vol->vol_id, ubi->ubi_num);
304 ubi_gluebi_updated(vol);
310 static int ubi_volume_read(char *volume, char *buf, size_t size)
312 int err, lnum, off, len, tbuf_size, i = 0;
313 size_t count_save = size;
315 unsigned long long tmp;
316 struct ubi_volume *vol = NULL;
319 for (i = 0; i < ubi->vtbl_slots; i++) {
320 vol = ubi->volumes[i];
321 if (vol && !strcmp(vol->name, volume)) {
322 printf("Volume %s found at volume id %d\n",
323 volume, vol->vol_id);
327 if (i == ubi->vtbl_slots) {
328 printf("%s volume not found\n", volume);
332 printf("read %i bytes from volume %d to %x(buf address)\n",
333 (int) size, vol->vol_id, (unsigned)buf);
339 if (vol->upd_marker) {
340 printf("damaged volume, update marker is set");
343 if (offp == vol->used_bytes)
347 printf("Read [%lu] bytes\n", (unsigned long) vol->used_bytes);
348 size = vol->used_bytes;
352 printf("read from corrupted volume %d", vol->vol_id);
353 if (offp + size > vol->used_bytes)
354 count_save = size = vol->used_bytes - offp;
356 tbuf_size = vol->usable_leb_size;
357 if (size < tbuf_size)
358 tbuf_size = ALIGN(size, ubi->min_io_size);
359 tbuf = malloc(tbuf_size);
364 len = size > tbuf_size ? tbuf_size : size;
367 off = do_div(tmp, vol->usable_leb_size);
370 if (off + len >= vol->usable_leb_size)
371 len = vol->usable_leb_size - off;
373 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
375 printf("read err %x\n", err);
379 if (off == vol->usable_leb_size) {
381 off -= vol->usable_leb_size;
387 memcpy(buf, tbuf, len);
390 len = size > tbuf_size ? tbuf_size : size;
394 return err ? err : count_save - size;
397 static int ubi_dev_scan(struct mtd_info *info, char *ubidev)
399 struct mtd_device *dev;
400 struct part_info *part;
401 struct mtd_partition mtd_part;
405 if (mtdparts_init() != 0)
408 if (find_dev_and_part(ubidev, &dev, &pnum, &part) != 0)
411 sprintf(buffer, "mtd=%d", pnum);
412 memset(&mtd_part, 0, sizeof(mtd_part));
413 mtd_part.name = buffer;
414 mtd_part.size = part->size;
415 mtd_part.offset = part->offset;
416 add_mtd_partitions(info, &mtd_part, 1);
418 err = ubi_mtd_param_parse(buffer, NULL);
420 del_mtd_partitions(info);
426 del_mtd_partitions(info);
433 static int do_ubi(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
440 printf("Usage:\n%s\n", cmdtp->usage);
444 if (strcmp(argv[1], "part") == 0) {
445 /* Print current partition */
447 if (ubi_dev.type == DEV_TYPE_NONE) {
448 printf("Error, no UBI device/partition selected!\n");
452 printf("%s Device %d: %s, partition %s\n", ubi_dev.dev_name,
453 ubi_dev.nr, ubi_dev.mtd_info->name, ubi_dev.part_name);
458 printf("Usage:\n%s\n", cmdtp->usage);
462 /* todo: get dev number for NAND... */
466 * Check for nand|onenand selection
468 #if defined(CONFIG_CMD_NAND)
469 if (strcmp(argv[2], "nand") == 0) {
470 strcpy(ubi_dev.dev_name, "NAND");
471 ubi_dev.type = DEV_TYPE_NAND;
472 ubi_dev.mtd_info = &nand_info[ubi_dev.nr];
475 #if defined(CONFIG_CMD_ONENAND)
476 if (strcmp(argv[2], "onenand") == 0) {
477 strcpy(ubi_dev.dev_name, "OneNAND");
478 ubi_dev.type = DEV_TYPE_ONENAND;
479 ubi_dev.mtd_info = &onenand_mtd;
483 if (ubi_dev.type == DEV_TYPE_NONE) {
484 printf("Error, no UBI device/partition selected!\n");
488 strcpy(ubi_dev.part_name, argv[3]);
489 err = ubi_dev_scan(ubi_dev.mtd_info, ubi_dev.part_name);
491 printf("UBI init error %d\n", err);
495 ubi = ubi_devices[0];
500 if ((strcmp(argv[1], "part") != 0) && (ubi_dev.type == DEV_TYPE_NONE)) {
501 printf("Error, no UBI device/partition selected!\n");
505 if (strcmp(argv[1], "info") == 0) {
507 if (argc > 2 && !strncmp(argv[2], "l", 1))
509 return ubi_info(layout);
512 if (strncmp(argv[1], "create", 6) == 0) {
513 int dynamic = 1; /* default: dynamic volume */
515 /* Use maximum available size */
518 /* E.g., create volume size type */
520 if (strncmp(argv[4], "s", 1) == 0)
522 else if (strncmp(argv[4], "d", 1) != 0) {
523 printf("Incorrect type\n");
528 /* E.g., create volume size */
530 addr = simple_strtoul(argv[3], NULL, 16);
533 /* Use maximum available size */
535 size = ubi->avail_pebs * ubi->leb_size;
536 /* E.g., create volume */
538 return ubi_create_vol(argv[2], size, dynamic);
541 if (strncmp(argv[1], "remove", 6) == 0) {
542 /* E.g., remove volume */
544 return ubi_remove_vol(argv[2]);
547 if (strncmp(argv[1], "write", 5) == 0) {
549 printf("Please see usage\n");
553 addr = simple_strtoul(argv[2], NULL, 16);
554 size = simple_strtoul(argv[4], NULL, 16);
556 return ubi_volume_write(argv[3], (void *)addr, size);
559 if (strncmp(argv[1], "read", 4) == 0) {
562 /* E.g., read volume size */
564 size = simple_strtoul(argv[4], NULL, 16);
568 /* E.g., read volume */
570 addr = simple_strtoul(argv[2], NULL, 16);
575 return ubi_volume_read(argv[3], (char *)addr, size);
578 printf("Please see usage\n");
582 U_BOOT_CMD(ubi, 6, 1, do_ubi,
583 "ubi - ubi commands\n",
584 "part [nand|onenand] [part]"
585 " - Show or set current partition\n"
586 "ubi info [l[ayout]]"
587 " - Display volume and ubi layout information\n"
588 "ubi create[vol] volume [size] [type]"
589 " - create volume name with size\n"
590 "ubi write[vol] address volume size"
591 " - Write volume from address with size\n"
592 "ubi read[vol] address volume [size]"
593 " - Read volume to address with size\n"
594 "ubi remove[vol] volume"
597 " volume: charater name\n"
598 " size: KiB, MiB, GiB, and bytes\n"
599 " type: s[tatic] or d[ynamic] (default=dynamic)\n"