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 static void ubi_dump_vol_info(const struct ubi_volume
*vol
)
47 ubi_msg("volume information dump:");
48 ubi_msg("vol_id %d", vol
->vol_id
);
49 ubi_msg("reserved_pebs %d", vol
->reserved_pebs
);
50 ubi_msg("alignment %d", vol
->alignment
);
51 ubi_msg("data_pad %d", vol
->data_pad
);
52 ubi_msg("vol_type %d", vol
->vol_type
);
53 ubi_msg("name_len %d", vol
->name_len
);
54 ubi_msg("usable_leb_size %d", vol
->usable_leb_size
);
55 ubi_msg("used_ebs %d", vol
->used_ebs
);
56 ubi_msg("used_bytes %lld", vol
->used_bytes
);
57 ubi_msg("last_eb_bytes %d", vol
->last_eb_bytes
);
58 ubi_msg("corrupted %d", vol
->corrupted
);
59 ubi_msg("upd_marker %d", vol
->upd_marker
);
61 if (vol
->name_len
<= UBI_VOL_NAME_MAX
&&
62 strnlen(vol
->name
, vol
->name_len
+ 1) == vol
->name_len
) {
63 ubi_msg("name %s", vol
->name
);
65 ubi_msg("the 1st 5 characters of the name: %c%c%c%c%c",
66 vol
->name
[0], vol
->name
[1], vol
->name
[2],
67 vol
->name
[3], vol
->name
[4]);
72 static void display_volume_info(struct ubi_device
*ubi
)
76 for (i
= 0; i
< (ubi
->vtbl_slots
+ 1); i
++) {
78 continue; /* Empty record */
79 ubi_dump_vol_info(ubi
->volumes
[i
]);
83 static void display_ubi_info(struct ubi_device
*ubi
)
85 ubi_msg("MTD device name: \"%s\"", ubi
->mtd
->name
);
86 ubi_msg("MTD device size: %llu MiB", ubi
->flash_size
>> 20);
87 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
88 ubi
->peb_size
, ubi
->peb_size
>> 10);
89 ubi_msg("logical eraseblock size: %d bytes", ubi
->leb_size
);
90 ubi_msg("number of good PEBs: %d", ubi
->good_peb_count
);
91 ubi_msg("number of bad PEBs: %d", ubi
->bad_peb_count
);
92 ubi_msg("smallest flash I/O unit: %d", ubi
->min_io_size
);
93 ubi_msg("VID header offset: %d (aligned %d)",
94 ubi
->vid_hdr_offset
, ubi
->vid_hdr_aloffset
);
95 ubi_msg("data offset: %d", ubi
->leb_start
);
96 ubi_msg("max. allowed volumes: %d", ubi
->vtbl_slots
);
97 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD
);
98 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT
);
99 ubi_msg("number of user volumes: %d",
100 ubi
->vol_count
- UBI_INT_VOL_COUNT
);
101 ubi_msg("available PEBs: %d", ubi
->avail_pebs
);
102 ubi_msg("total number of reserved PEBs: %d", ubi
->rsvd_pebs
);
103 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
105 ubi_msg("max/mean erase counter: %d/%d", ubi
->max_ec
, ubi
->mean_ec
);
108 static int ubi_info(int layout
)
111 display_volume_info(ubi
);
113 display_ubi_info(ubi
);
118 static int verify_mkvol_req(const struct ubi_device
*ubi
,
119 const struct ubi_mkvol_req
*req
)
121 int n
, err
= -EINVAL
;
123 if (req
->bytes
< 0 || req
->alignment
< 0 || req
->vol_type
< 0 ||
127 if ((req
->vol_id
< 0 || req
->vol_id
>= ubi
->vtbl_slots
) &&
128 req
->vol_id
!= UBI_VOL_NUM_AUTO
)
131 if (req
->alignment
== 0)
137 if (req
->vol_type
!= UBI_DYNAMIC_VOLUME
&&
138 req
->vol_type
!= UBI_STATIC_VOLUME
)
141 if (req
->alignment
> ubi
->leb_size
)
144 n
= req
->alignment
% ubi
->min_io_size
;
145 if (req
->alignment
!= 1 && n
)
148 if (req
->name_len
> UBI_VOL_NAME_MAX
) {
155 printf("bad volume creation request");
159 static int ubi_create_vol(char *volume
, int size
, int dynamic
)
161 struct ubi_mkvol_req req
;
165 req
.vol_type
= UBI_DYNAMIC_VOLUME
;
167 req
.vol_type
= UBI_STATIC_VOLUME
;
169 req
.vol_id
= UBI_VOL_NUM_AUTO
;
173 strcpy(req
.name
, volume
);
174 req
.name_len
= strlen(volume
);
175 req
.name
[req
.name_len
] = '\0';
177 /* It's duplicated at drivers/mtd/ubi/cdev.c */
178 err
= verify_mkvol_req(ubi
, &req
);
180 printf("verify_mkvol_req failed %d\n", err
);
183 printf("Creating %s volume %s of size %d\n",
184 dynamic
? "dynamic" : "static", volume
, size
);
185 /* Call real ubi create volume */
186 return ubi_create_volume(ubi
, &req
);
189 static int ubi_remove_vol(char *volume
)
191 int i
, err
, reserved_pebs
;
192 int found
= 0, vol_id
= 0;
193 struct ubi_volume
*vol
;
195 for (i
= 0; i
< ubi
->vtbl_slots
; i
++) {
196 vol
= ubi
->volumes
[i
];
197 if (vol
&& !strcmp(vol
->name
, volume
)) {
198 printf("Volume %s found at valid %d\n", volume
, i
);
205 printf("%s volume not found\n", volume
);
208 printf("remove UBI volume %s (id %d)\n", vol
->name
, vol
->vol_id
);
211 printf("It's read-only mode\n");
216 err
= ubi_change_vtbl_record(ubi
, vol_id
, NULL
);
218 printf("Error changing Vol tabel record err=%x\n", err
);
221 reserved_pebs
= vol
->reserved_pebs
;
222 for (i
= 0; i
< vol
->reserved_pebs
; i
++) {
223 err
= ubi_eba_unmap_leb(ubi
, vol
, i
);
229 ubi
->volumes
[vol_id
]->eba_tbl
= NULL
;
230 ubi
->volumes
[vol_id
] = NULL
;
232 ubi
->rsvd_pebs
-= reserved_pebs
;
233 ubi
->avail_pebs
+= reserved_pebs
;
234 i
= ubi
->beb_rsvd_level
- ubi
->beb_rsvd_pebs
;
236 i
= ubi
->avail_pebs
>= i
? i
: ubi
->avail_pebs
;
237 ubi
->avail_pebs
-= i
;
239 ubi
->beb_rsvd_pebs
+= i
;
241 ubi_msg("reserve more %d PEBs", i
);
247 ubi_err("cannot remove volume %d, error %d", vol_id
, err
);
251 static int ubi_volume_write(char *volume
, void *buf
, size_t size
)
256 struct ubi_volume
*vol
;
258 for (i
= 0; i
< ubi
->vtbl_slots
; i
++) {
259 vol
= ubi
->volumes
[i
];
260 if (vol
&& !strcmp(vol
->name
, volume
)) {
261 printf("Volume \"%s\" found at volume id %d\n", volume
, i
);
267 printf("%s volume not found\n", volume
);
270 rsvd_bytes
= vol
->reserved_pebs
* (ubi
->leb_size
- vol
->data_pad
);
271 if (size
< 0 || size
> rsvd_bytes
) {
272 printf("rsvd_bytes=%d vol->reserved_pebs=%d ubi->leb_size=%d\n",
273 rsvd_bytes
, vol
->reserved_pebs
, ubi
->leb_size
);
274 printf("vol->data_pad=%d\n", vol
->data_pad
);
275 printf("Size > volume size !!\n");
279 err
= ubi_start_update(ubi
, vol
, size
);
281 printf("Cannot start volume update\n");
285 err
= ubi_more_update_data(ubi
, vol
, buf
, size
);
287 printf("Couldnt or partially wrote data \n");
294 err
= ubi_check_volume(ubi
, vol
->vol_id
);
299 ubi_warn("volume %d on UBI device %d is corrupted",
300 vol
->vol_id
, ubi
->ubi_num
);
305 ubi_gluebi_updated(vol
);
311 static int ubi_volume_read(char *volume
, char *buf
, size_t size
)
313 int err
, lnum
, off
, len
, tbuf_size
, i
= 0;
314 size_t count_save
= size
;
316 unsigned long long tmp
;
317 struct ubi_volume
*vol
= NULL
;
320 for (i
= 0; i
< ubi
->vtbl_slots
; i
++) {
321 vol
= ubi
->volumes
[i
];
322 if (vol
&& !strcmp(vol
->name
, volume
)) {
323 printf("Volume %s found at volume id %d\n",
324 volume
, vol
->vol_id
);
328 if (i
== ubi
->vtbl_slots
) {
329 printf("%s volume not found\n", volume
);
333 printf("read %i bytes from volume %d to %x(buf address)\n",
334 (int) size
, vol
->vol_id
, (unsigned)buf
);
340 if (vol
->upd_marker
) {
341 printf("damaged volume, update marker is set");
344 if (offp
== vol
->used_bytes
)
348 printf("Read [%lu] bytes\n", (unsigned long) vol
->used_bytes
);
349 size
= vol
->used_bytes
;
353 printf("read from corrupted volume %d", vol
->vol_id
);
354 if (offp
+ size
> vol
->used_bytes
)
355 count_save
= size
= vol
->used_bytes
- offp
;
357 tbuf_size
= vol
->usable_leb_size
;
358 if (size
< tbuf_size
)
359 tbuf_size
= ALIGN(size
, ubi
->min_io_size
);
360 tbuf
= malloc(tbuf_size
);
365 len
= size
> tbuf_size
? tbuf_size
: size
;
368 off
= do_div(tmp
, vol
->usable_leb_size
);
371 if (off
+ len
>= vol
->usable_leb_size
)
372 len
= vol
->usable_leb_size
- off
;
374 err
= ubi_eba_read_leb(ubi
, vol
, lnum
, tbuf
, off
, len
, 0);
376 printf("read err %x\n", err
);
380 if (off
== vol
->usable_leb_size
) {
382 off
-= vol
->usable_leb_size
;
388 memcpy(buf
, tbuf
, len
);
391 len
= size
> tbuf_size
? tbuf_size
: size
;
395 return err
? err
: count_save
- size
;
398 static int ubi_dev_scan(struct mtd_info
*info
, char *ubidev
,
399 const char *vid_header_offset
)
401 struct mtd_device
*dev
;
402 struct part_info
*part
;
403 struct mtd_partition mtd_part
;
404 char ubi_mtd_param_buffer
[80];
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 strcpy(ubi_mtd_param_buffer
, buffer
);
419 if (vid_header_offset
)
420 sprintf(ubi_mtd_param_buffer
, "mtd=%d,%s", pnum
,
422 err
= ubi_mtd_param_parse(ubi_mtd_param_buffer
, NULL
);
424 del_mtd_partitions(info
);
430 del_mtd_partitions(info
);
439 static int do_ubi(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
450 if (mtdparts_init() != 0) {
451 printf("Error initializing mtdparts!\n");
455 if (strcmp(argv
[1], "part") == 0) {
457 struct mtd_device
*dev
;
458 struct part_info
*part
;
459 const char *vid_header_offset
= NULL
;
462 /* Print current partition */
464 if (!ubi_dev
.selected
) {
465 printf("Error, no UBI device/partition selected!\n");
469 printf("Device %d: %s, partition %s\n",
470 ubi_dev
.nr
, ubi_dev
.mtd_info
->name
, ubi_dev
.part_name
);
479 /* todo: get dev number for NAND... */
483 * Call ubi_exit() before re-initializing the UBI subsystem
485 if (ubi_initialized
) {
487 del_mtd_partitions(ubi_dev
.mtd_info
);
491 * Search the mtd device number where this partition
494 if (find_dev_and_part(argv
[2], &dev
, &pnum
, &part
)) {
495 printf("Partition %s not found!\n", argv
[2]);
498 sprintf(mtd_dev
, "%s%d", MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
);
499 ubi_dev
.mtd_info
= get_mtd_device_nm(mtd_dev
);
500 if (IS_ERR(ubi_dev
.mtd_info
)) {
501 printf("Partition %s not found on device %s!\n", argv
[2], mtd_dev
);
505 ubi_dev
.selected
= 1;
508 vid_header_offset
= argv
[3];
509 strcpy(ubi_dev
.part_name
, argv
[2]);
510 err
= ubi_dev_scan(ubi_dev
.mtd_info
, ubi_dev
.part_name
,
513 printf("UBI init error %d\n", err
);
514 ubi_dev
.selected
= 0;
518 ubi
= ubi_devices
[0];
523 if ((strcmp(argv
[1], "part") != 0) && (!ubi_dev
.selected
)) {
524 printf("Error, no UBI device/partition selected!\n");
528 if (strcmp(argv
[1], "info") == 0) {
530 if (argc
> 2 && !strncmp(argv
[2], "l", 1))
532 return ubi_info(layout
);
535 if (strncmp(argv
[1], "create", 6) == 0) {
536 int dynamic
= 1; /* default: dynamic volume */
538 /* Use maximum available size */
541 /* E.g., create volume size type */
543 if (strncmp(argv
[4], "s", 1) == 0)
545 else if (strncmp(argv
[4], "d", 1) != 0) {
546 printf("Incorrect type\n");
551 /* E.g., create volume size */
553 size
= simple_strtoul(argv
[3], NULL
, 16);
556 /* Use maximum available size */
558 size
= ubi
->avail_pebs
* ubi
->leb_size
;
559 /* E.g., create volume */
561 return ubi_create_vol(argv
[2], size
, dynamic
);
564 if (strncmp(argv
[1], "remove", 6) == 0) {
565 /* E.g., remove volume */
567 return ubi_remove_vol(argv
[2]);
570 if (strncmp(argv
[1], "write", 5) == 0) {
572 printf("Please see usage\n");
576 addr
= simple_strtoul(argv
[2], NULL
, 16);
577 size
= simple_strtoul(argv
[4], NULL
, 16);
579 return ubi_volume_write(argv
[3], (void *)addr
, size
);
582 if (strncmp(argv
[1], "read", 4) == 0) {
585 /* E.g., read volume size */
587 size
= simple_strtoul(argv
[4], NULL
, 16);
591 /* E.g., read volume */
593 addr
= simple_strtoul(argv
[2], NULL
, 16);
598 return ubi_volume_read(argv
[3], (char *)addr
, size
);
601 printf("Please see usage\n");
605 U_BOOT_CMD(ubi
, 6, 1, do_ubi
,
607 "part [part] [offset]\n"
608 " - Show or set current partition (with optional VID"
610 "ubi info [l[ayout]]"
611 " - Display volume and ubi layout information\n"
612 "ubi create[vol] volume [size] [type]"
613 " - create volume name with size\n"
614 "ubi write[vol] address volume size"
615 " - Write volume from address with size\n"
616 "ubi read[vol] address volume [size]"
617 " - Read volume to address with size\n"
618 "ubi remove[vol] volume"
621 " volume: charater name\n"
622 " size: KiB, MiB, GiB, and bytes\n"
623 " type: s[tatic] or d[ynamic] (default=dynamic)"