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>
27 #define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__)
29 #define DEV_TYPE_NONE 0
30 #define DEV_TYPE_NAND 1
31 #define DEV_TYPE_ONENAND 2
32 #define DEV_TYPE_NOR 3
34 /* Private own data */
35 static struct ubi_device
*ubi
;
36 static char buffer
[80];
37 static int ubi_initialized
;
43 struct mtd_info
*mtd_info
;
46 static struct selected_dev ubi_dev
;
48 #ifdef CONFIG_CMD_UBIFS
49 int ubifs_is_mounted(void);
50 void cmd_ubifs_umount(void);
53 static void ubi_dump_vol_info(const struct ubi_volume
*vol
)
55 ubi_msg("volume information dump:");
56 ubi_msg("vol_id %d", vol
->vol_id
);
57 ubi_msg("reserved_pebs %d", vol
->reserved_pebs
);
58 ubi_msg("alignment %d", vol
->alignment
);
59 ubi_msg("data_pad %d", vol
->data_pad
);
60 ubi_msg("vol_type %d", vol
->vol_type
);
61 ubi_msg("name_len %d", vol
->name_len
);
62 ubi_msg("usable_leb_size %d", vol
->usable_leb_size
);
63 ubi_msg("used_ebs %d", vol
->used_ebs
);
64 ubi_msg("used_bytes %lld", vol
->used_bytes
);
65 ubi_msg("last_eb_bytes %d", vol
->last_eb_bytes
);
66 ubi_msg("corrupted %d", vol
->corrupted
);
67 ubi_msg("upd_marker %d", vol
->upd_marker
);
69 if (vol
->name_len
<= UBI_VOL_NAME_MAX
&&
70 strnlen(vol
->name
, vol
->name_len
+ 1) == vol
->name_len
) {
71 ubi_msg("name %s", vol
->name
);
73 ubi_msg("the 1st 5 characters of the name: %c%c%c%c%c",
74 vol
->name
[0], vol
->name
[1], vol
->name
[2],
75 vol
->name
[3], vol
->name
[4]);
80 static void display_volume_info(struct ubi_device
*ubi
)
84 for (i
= 0; i
< (ubi
->vtbl_slots
+ 1); i
++) {
86 continue; /* Empty record */
87 ubi_dump_vol_info(ubi
->volumes
[i
]);
91 static void display_ubi_info(struct ubi_device
*ubi
)
93 ubi_msg("MTD device name: \"%s\"", ubi
->mtd
->name
);
94 ubi_msg("MTD device size: %llu MiB", ubi
->flash_size
>> 20);
95 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
96 ubi
->peb_size
, ubi
->peb_size
>> 10);
97 ubi_msg("logical eraseblock size: %d bytes", ubi
->leb_size
);
98 ubi_msg("number of good PEBs: %d", ubi
->good_peb_count
);
99 ubi_msg("number of bad PEBs: %d", ubi
->bad_peb_count
);
100 ubi_msg("smallest flash I/O unit: %d", ubi
->min_io_size
);
101 ubi_msg("VID header offset: %d (aligned %d)",
102 ubi
->vid_hdr_offset
, ubi
->vid_hdr_aloffset
);
103 ubi_msg("data offset: %d", ubi
->leb_start
);
104 ubi_msg("max. allowed volumes: %d", ubi
->vtbl_slots
);
105 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD
);
106 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT
);
107 ubi_msg("number of user volumes: %d",
108 ubi
->vol_count
- UBI_INT_VOL_COUNT
);
109 ubi_msg("available PEBs: %d", ubi
->avail_pebs
);
110 ubi_msg("total number of reserved PEBs: %d", ubi
->rsvd_pebs
);
111 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
113 ubi_msg("max/mean erase counter: %d/%d", ubi
->max_ec
, ubi
->mean_ec
);
116 static int ubi_info(int layout
)
119 display_volume_info(ubi
);
121 display_ubi_info(ubi
);
126 static int verify_mkvol_req(const struct ubi_device
*ubi
,
127 const struct ubi_mkvol_req
*req
)
131 if (req
->bytes
< 0 || req
->alignment
< 0 || req
->vol_type
< 0 ||
135 if ((req
->vol_id
< 0 || req
->vol_id
>= ubi
->vtbl_slots
) &&
136 req
->vol_id
!= UBI_VOL_NUM_AUTO
)
139 if (req
->alignment
== 0)
142 if (req
->bytes
== 0) {
143 printf("No space left in UBI device!\n");
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
) {
160 printf("Name too long!\n");
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 struct ubi_volume
*ubi_find_volume(char *volume
)
202 struct ubi_volume
*vol
= NULL
;
205 for (i
= 0; i
< ubi
->vtbl_slots
; i
++) {
206 vol
= ubi
->volumes
[i
];
207 if (vol
&& !strcmp(vol
->name
, volume
))
211 printf("Volume %s not found!\n", volume
);
215 static int ubi_remove_vol(char *volume
)
217 int err
, reserved_pebs
, i
;
218 struct ubi_volume
*vol
;
220 vol
= ubi_find_volume(volume
);
224 printf("Remove UBI volume %s (id %d)\n", vol
->name
, vol
->vol_id
);
227 printf("It's read-only mode\n");
232 err
= ubi_change_vtbl_record(ubi
, vol
->vol_id
, NULL
);
234 printf("Error changing Vol tabel record err=%x\n", err
);
237 reserved_pebs
= vol
->reserved_pebs
;
238 for (i
= 0; i
< vol
->reserved_pebs
; i
++) {
239 err
= ubi_eba_unmap_leb(ubi
, vol
, i
);
245 ubi
->volumes
[vol
->vol_id
]->eba_tbl
= NULL
;
246 ubi
->volumes
[vol
->vol_id
] = NULL
;
248 ubi
->rsvd_pebs
-= reserved_pebs
;
249 ubi
->avail_pebs
+= reserved_pebs
;
250 i
= ubi
->beb_rsvd_level
- ubi
->beb_rsvd_pebs
;
252 i
= ubi
->avail_pebs
>= i
? i
: ubi
->avail_pebs
;
253 ubi
->avail_pebs
-= i
;
255 ubi
->beb_rsvd_pebs
+= i
;
257 ubi_msg("reserve more %d PEBs", i
);
263 ubi_err("cannot remove volume %s, error %d", volume
, err
);
269 int ubi_volume_write(char *volume
, void *buf
, size_t size
)
273 struct ubi_volume
*vol
;
275 vol
= ubi_find_volume(volume
);
279 rsvd_bytes
= vol
->reserved_pebs
* (ubi
->leb_size
- vol
->data_pad
);
280 if (size
< 0 || size
> rsvd_bytes
) {
281 printf("size > volume size! Aborting!\n");
285 err
= ubi_start_update(ubi
, vol
, size
);
287 printf("Cannot start volume update\n");
291 err
= ubi_more_update_data(ubi
, vol
, buf
, size
);
293 printf("Couldnt or partially wrote data\n");
300 err
= ubi_check_volume(ubi
, vol
->vol_id
);
305 ubi_warn("volume %d on UBI device %d is corrupted",
306 vol
->vol_id
, ubi
->ubi_num
);
311 ubi_gluebi_updated(vol
);
317 int ubi_volume_read(char *volume
, char *buf
, size_t size
)
319 int err
, lnum
, off
, len
, tbuf_size
;
321 unsigned long long tmp
;
322 struct ubi_volume
*vol
;
325 vol
= ubi_find_volume(volume
);
333 if (vol
->upd_marker
) {
334 printf("damaged volume, update marker is set");
337 if (offp
== vol
->used_bytes
)
341 printf("No size specified -> Using max size (%lld)\n", vol
->used_bytes
);
342 size
= vol
->used_bytes
;
346 printf("read from corrupted volume %d", vol
->vol_id
);
347 if (offp
+ size
> vol
->used_bytes
)
348 size
= vol
->used_bytes
- offp
;
350 tbuf_size
= vol
->usable_leb_size
;
351 if (size
< tbuf_size
)
352 tbuf_size
= ALIGN(size
, ubi
->min_io_size
);
353 tbuf
= malloc(tbuf_size
);
358 len
= size
> tbuf_size
? tbuf_size
: size
;
361 off
= do_div(tmp
, vol
->usable_leb_size
);
364 if (off
+ len
>= vol
->usable_leb_size
)
365 len
= vol
->usable_leb_size
- off
;
367 err
= ubi_eba_read_leb(ubi
, vol
, lnum
, tbuf
, off
, len
, 0);
369 printf("read err %x\n", err
);
374 if (off
== vol
->usable_leb_size
) {
376 off
-= vol
->usable_leb_size
;
382 memcpy(buf
, tbuf
, len
);
385 len
= size
> tbuf_size
? tbuf_size
: size
;
392 static int ubi_dev_scan(struct mtd_info
*info
, char *ubidev
,
393 const char *vid_header_offset
)
395 struct mtd_device
*dev
;
396 struct part_info
*part
;
397 struct mtd_partition mtd_part
;
398 char ubi_mtd_param_buffer
[80];
402 if (find_dev_and_part(ubidev
, &dev
, &pnum
, &part
) != 0)
405 sprintf(buffer
, "mtd=%d", pnum
);
406 memset(&mtd_part
, 0, sizeof(mtd_part
));
407 mtd_part
.name
= buffer
;
408 mtd_part
.size
= part
->size
;
409 mtd_part
.offset
= part
->offset
;
410 add_mtd_partitions(info
, &mtd_part
, 1);
412 strcpy(ubi_mtd_param_buffer
, buffer
);
413 if (vid_header_offset
)
414 sprintf(ubi_mtd_param_buffer
, "mtd=%d,%s", pnum
,
416 err
= ubi_mtd_param_parse(ubi_mtd_param_buffer
, NULL
);
418 del_mtd_partitions(info
);
424 del_mtd_partitions(info
);
433 int ubi_part(char *part_name
, const char *vid_header_offset
)
437 struct mtd_device
*dev
;
438 struct part_info
*part
;
441 if (mtdparts_init() != 0) {
442 printf("Error initializing mtdparts!\n");
446 #ifdef CONFIG_CMD_UBIFS
448 * Automatically unmount UBIFS partition when user
449 * changes the UBI device. Otherwise the following
450 * UBIFS commands will crash.
452 if (ubifs_is_mounted())
456 /* todo: get dev number for NAND... */
460 * Call ubi_exit() before re-initializing the UBI subsystem
462 if (ubi_initialized
) {
464 del_mtd_partitions(ubi_dev
.mtd_info
);
468 * Search the mtd device number where this partition
471 if (find_dev_and_part(part_name
, &dev
, &pnum
, &part
)) {
472 printf("Partition %s not found!\n", part_name
);
475 sprintf(mtd_dev
, "%s%d", MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
);
476 ubi_dev
.mtd_info
= get_mtd_device_nm(mtd_dev
);
477 if (IS_ERR(ubi_dev
.mtd_info
)) {
478 printf("Partition %s not found on device %s!\n", part_name
,
483 ubi_dev
.selected
= 1;
485 strcpy(ubi_dev
.part_name
, part_name
);
486 err
= ubi_dev_scan(ubi_dev
.mtd_info
, ubi_dev
.part_name
,
489 printf("UBI init error %d\n", err
);
490 ubi_dev
.selected
= 0;
494 ubi
= ubi_devices
[0];
499 static int do_ubi(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
505 return CMD_RET_USAGE
;
507 if (strcmp(argv
[1], "part") == 0) {
508 const char *vid_header_offset
= NULL
;
510 /* Print current partition */
512 if (!ubi_dev
.selected
) {
513 printf("Error, no UBI device/partition selected!\n");
517 printf("Device %d: %s, partition %s\n",
518 ubi_dev
.nr
, ubi_dev
.mtd_info
->name
, ubi_dev
.part_name
);
523 return CMD_RET_USAGE
;
526 vid_header_offset
= argv
[3];
528 return ubi_part(argv
[2], vid_header_offset
);
531 if ((strcmp(argv
[1], "part") != 0) && (!ubi_dev
.selected
)) {
532 printf("Error, no UBI device/partition selected!\n");
536 if (strcmp(argv
[1], "info") == 0) {
538 if (argc
> 2 && !strncmp(argv
[2], "l", 1))
540 return ubi_info(layout
);
543 if (strncmp(argv
[1], "create", 6) == 0) {
544 int dynamic
= 1; /* default: dynamic volume */
546 /* Use maximum available size */
549 /* E.g., create volume size type */
551 if (strncmp(argv
[4], "s", 1) == 0)
553 else if (strncmp(argv
[4], "d", 1) != 0) {
554 printf("Incorrect type\n");
559 /* E.g., create volume size */
561 size
= simple_strtoul(argv
[3], NULL
, 16);
564 /* Use maximum available size */
566 size
= ubi
->avail_pebs
* ubi
->leb_size
;
567 printf("No size specified -> Using max size (%u)\n", size
);
569 /* E.g., create volume */
571 return ubi_create_vol(argv
[2], size
, dynamic
);
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 ret
= ubi_volume_write(argv
[3], (void *)addr
, size
);
593 printf("%d bytes written to volume %s\n", size
,
600 if (strncmp(argv
[1], "read", 4) == 0) {
603 /* E.g., read volume size */
605 size
= simple_strtoul(argv
[4], NULL
, 16);
609 /* E.g., read volume */
611 addr
= simple_strtoul(argv
[2], NULL
, 16);
616 printf("Read %d bytes from volume %s to %lx\n", size
,
619 return ubi_volume_read(argv
[3], (char *)addr
, size
);
623 printf("Please see usage\n");
630 "part [part] [offset]\n"
631 " - Show or set current partition (with optional VID"
633 "ubi info [l[ayout]]"
634 " - Display volume and ubi layout information\n"
635 "ubi create[vol] volume [size] [type]"
636 " - create volume name with size\n"
637 "ubi write[vol] address volume size"
638 " - Write volume from address with size\n"
639 "ubi read[vol] address volume [size]"
640 " - Read volume to address with size\n"
641 "ubi remove[vol] volume"
644 " volume: character name\n"
645 " size: specified in bytes\n"
646 " type: s[tatic] or d[ynamic] (default=dynamic)"