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
)
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)
139 if (req
->bytes
== 0) {
140 printf("No space left in UBI device!\n");
145 if (req
->vol_type
!= UBI_DYNAMIC_VOLUME
&&
146 req
->vol_type
!= UBI_STATIC_VOLUME
)
149 if (req
->alignment
> ubi
->leb_size
)
152 n
= req
->alignment
% ubi
->min_io_size
;
153 if (req
->alignment
!= 1 && n
)
156 if (req
->name_len
> UBI_VOL_NAME_MAX
) {
157 printf("Name too long!\n");
167 static int ubi_create_vol(char *volume
, int size
, int dynamic
)
169 struct ubi_mkvol_req req
;
173 req
.vol_type
= UBI_DYNAMIC_VOLUME
;
175 req
.vol_type
= UBI_STATIC_VOLUME
;
177 req
.vol_id
= UBI_VOL_NUM_AUTO
;
181 strcpy(req
.name
, volume
);
182 req
.name_len
= strlen(volume
);
183 req
.name
[req
.name_len
] = '\0';
185 /* It's duplicated at drivers/mtd/ubi/cdev.c */
186 err
= verify_mkvol_req(ubi
, &req
);
188 printf("verify_mkvol_req failed %d\n", err
);
191 printf("Creating %s volume %s of size %d\n",
192 dynamic
? "dynamic" : "static", volume
, size
);
193 /* Call real ubi create volume */
194 return ubi_create_volume(ubi
, &req
);
197 static struct ubi_volume
*ubi_find_volume(char *volume
)
199 struct ubi_volume
*vol
= NULL
;
202 for (i
= 0; i
< ubi
->vtbl_slots
; i
++) {
203 vol
= ubi
->volumes
[i
];
204 if (vol
&& !strcmp(vol
->name
, volume
))
208 printf("Volume %s not found!\n", volume
);
212 static int ubi_remove_vol(char *volume
)
214 int err
, reserved_pebs
, i
;
215 struct ubi_volume
*vol
;
217 vol
= ubi_find_volume(volume
);
221 printf("Remove UBI volume %s (id %d)\n", vol
->name
, vol
->vol_id
);
224 printf("It's read-only mode\n");
229 err
= ubi_change_vtbl_record(ubi
, vol
->vol_id
, NULL
);
231 printf("Error changing Vol tabel record err=%x\n", err
);
234 reserved_pebs
= vol
->reserved_pebs
;
235 for (i
= 0; i
< vol
->reserved_pebs
; i
++) {
236 err
= ubi_eba_unmap_leb(ubi
, vol
, i
);
242 ubi
->volumes
[vol
->vol_id
]->eba_tbl
= NULL
;
243 ubi
->volumes
[vol
->vol_id
] = NULL
;
245 ubi
->rsvd_pebs
-= reserved_pebs
;
246 ubi
->avail_pebs
+= reserved_pebs
;
247 i
= ubi
->beb_rsvd_level
- ubi
->beb_rsvd_pebs
;
249 i
= ubi
->avail_pebs
>= i
? i
: ubi
->avail_pebs
;
250 ubi
->avail_pebs
-= i
;
252 ubi
->beb_rsvd_pebs
+= i
;
254 ubi_msg("reserve more %d PEBs", i
);
260 ubi_err("cannot remove volume %s, error %d", volume
, err
);
266 static int ubi_volume_write(char *volume
, void *buf
, size_t size
)
270 struct ubi_volume
*vol
;
272 vol
= ubi_find_volume(volume
);
276 rsvd_bytes
= vol
->reserved_pebs
* (ubi
->leb_size
- vol
->data_pad
);
277 if (size
< 0 || size
> rsvd_bytes
) {
278 printf("size > volume size! Aborting!\n");
282 err
= ubi_start_update(ubi
, vol
, size
);
284 printf("Cannot start volume update\n");
288 err
= ubi_more_update_data(ubi
, vol
, buf
, size
);
290 printf("Couldnt or partially wrote data\n");
297 err
= ubi_check_volume(ubi
, vol
->vol_id
);
302 ubi_warn("volume %d on UBI device %d is corrupted",
303 vol
->vol_id
, ubi
->ubi_num
);
308 ubi_gluebi_updated(vol
);
311 printf("%d bytes written to volume %s\n", size
, volume
);
316 static int ubi_volume_read(char *volume
, char *buf
, size_t size
)
318 int err
, lnum
, off
, len
, tbuf_size
;
320 unsigned long long tmp
;
321 struct ubi_volume
*vol
;
324 vol
= ubi_find_volume(volume
);
328 printf("Read %d bytes from volume %s to %p\n", size
, volume
, buf
);
334 if (vol
->upd_marker
) {
335 printf("damaged volume, update marker is set");
338 if (offp
== vol
->used_bytes
)
342 printf("No size specified -> Using max size (%lld)\n", vol
->used_bytes
);
343 size
= vol
->used_bytes
;
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(tbuf_size
);
359 len
= size
> tbuf_size
? tbuf_size
: size
;
362 off
= do_div(tmp
, vol
->usable_leb_size
);
365 if (off
+ len
>= vol
->usable_leb_size
)
366 len
= vol
->usable_leb_size
- off
;
368 err
= ubi_eba_read_leb(ubi
, vol
, lnum
, tbuf
, off
, len
, 0);
370 printf("read err %x\n", err
);
375 if (off
== vol
->usable_leb_size
) {
377 off
-= vol
->usable_leb_size
;
383 memcpy(buf
, tbuf
, len
);
386 len
= size
> tbuf_size
? tbuf_size
: size
;
393 static int ubi_dev_scan(struct mtd_info
*info
, char *ubidev
,
394 const char *vid_header_offset
)
396 struct mtd_device
*dev
;
397 struct part_info
*part
;
398 struct mtd_partition mtd_part
;
399 char ubi_mtd_param_buffer
[80];
403 if (find_dev_and_part(ubidev
, &dev
, &pnum
, &part
) != 0)
406 sprintf(buffer
, "mtd=%d", pnum
);
407 memset(&mtd_part
, 0, sizeof(mtd_part
));
408 mtd_part
.name
= buffer
;
409 mtd_part
.size
= part
->size
;
410 mtd_part
.offset
= part
->offset
;
411 add_mtd_partitions(info
, &mtd_part
, 1);
413 strcpy(ubi_mtd_param_buffer
, buffer
);
414 if (vid_header_offset
)
415 sprintf(ubi_mtd_param_buffer
, "mtd=%d,%s", pnum
,
417 err
= ubi_mtd_param_parse(ubi_mtd_param_buffer
, NULL
);
419 del_mtd_partitions(info
);
425 del_mtd_partitions(info
);
434 static int do_ubi(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
441 return cmd_usage(cmdtp
);
443 if (mtdparts_init() != 0) {
444 printf("Error initializing mtdparts!\n");
448 if (strcmp(argv
[1], "part") == 0) {
450 struct mtd_device
*dev
;
451 struct part_info
*part
;
452 const char *vid_header_offset
= NULL
;
455 /* Print current partition */
457 if (!ubi_dev
.selected
) {
458 printf("Error, no UBI device/partition selected!\n");
462 printf("Device %d: %s, partition %s\n",
463 ubi_dev
.nr
, ubi_dev
.mtd_info
->name
, ubi_dev
.part_name
);
468 return cmd_usage(cmdtp
);
470 #ifdef CONFIG_CMD_UBIFS
472 * Automatically unmount UBIFS partition when user
473 * changes the UBI device. Otherwise the following
474 * UBIFS commands will crash.
476 if (ubifs_is_mounted())
480 /* todo: get dev number for NAND... */
484 * Call ubi_exit() before re-initializing the UBI subsystem
486 if (ubi_initialized
) {
488 del_mtd_partitions(ubi_dev
.mtd_info
);
492 * Search the mtd device number where this partition
495 if (find_dev_and_part(argv
[2], &dev
, &pnum
, &part
)) {
496 printf("Partition %s not found!\n", argv
[2]);
499 sprintf(mtd_dev
, "%s%d", MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
);
500 ubi_dev
.mtd_info
= get_mtd_device_nm(mtd_dev
);
501 if (IS_ERR(ubi_dev
.mtd_info
)) {
502 printf("Partition %s not found on device %s!\n", argv
[2], mtd_dev
);
506 ubi_dev
.selected
= 1;
509 vid_header_offset
= argv
[3];
510 strcpy(ubi_dev
.part_name
, argv
[2]);
511 err
= ubi_dev_scan(ubi_dev
.mtd_info
, ubi_dev
.part_name
,
514 printf("UBI init error %d\n", err
);
515 ubi_dev
.selected
= 0;
519 ubi
= ubi_devices
[0];
524 if ((strcmp(argv
[1], "part") != 0) && (!ubi_dev
.selected
)) {
525 printf("Error, no UBI device/partition selected!\n");
529 if (strcmp(argv
[1], "info") == 0) {
531 if (argc
> 2 && !strncmp(argv
[2], "l", 1))
533 return ubi_info(layout
);
536 if (strncmp(argv
[1], "create", 6) == 0) {
537 int dynamic
= 1; /* default: dynamic volume */
539 /* Use maximum available size */
542 /* E.g., create volume size type */
544 if (strncmp(argv
[4], "s", 1) == 0)
546 else if (strncmp(argv
[4], "d", 1) != 0) {
547 printf("Incorrect type\n");
552 /* E.g., create volume size */
554 size
= simple_strtoul(argv
[3], NULL
, 16);
557 /* Use maximum available size */
559 size
= ubi
->avail_pebs
* ubi
->leb_size
;
560 printf("No size specified -> Using max size (%u)\n", size
);
562 /* E.g., create volume */
564 return ubi_create_vol(argv
[2], size
, dynamic
);
567 if (strncmp(argv
[1], "remove", 6) == 0) {
568 /* E.g., remove volume */
570 return ubi_remove_vol(argv
[2]);
573 if (strncmp(argv
[1], "write", 5) == 0) {
575 printf("Please see usage\n");
579 addr
= simple_strtoul(argv
[2], NULL
, 16);
580 size
= simple_strtoul(argv
[4], NULL
, 16);
582 return ubi_volume_write(argv
[3], (void *)addr
, size
);
585 if (strncmp(argv
[1], "read", 4) == 0) {
588 /* E.g., read volume size */
590 size
= simple_strtoul(argv
[4], NULL
, 16);
594 /* E.g., read volume */
596 addr
= simple_strtoul(argv
[2], NULL
, 16);
601 return ubi_volume_read(argv
[3], (char *)addr
, size
);
604 printf("Please see usage\n");
611 "part [part] [offset]\n"
612 " - Show or set current partition (with optional VID"
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: character name\n"
626 " size: specified in bytes\n"
627 " type: s[tatic] or d[ynamic] (default=dynamic)"