]>
Commit | Line | Data |
---|---|---|
694a0b3f KP |
1 | /* |
2 | * Unsorted Block Image commands | |
3 | * | |
4 | * Copyright (C) 2008 Samsung Electronics | |
5 | * Kyungmin Park <[email protected]> | |
6 | * | |
2d579e50 | 7 | * Copyright 2008-2009 Stefan Roese <[email protected]>, DENX Software Engineering |
694a0b3f KP |
8 | * |
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. | |
12 | */ | |
13 | ||
14 | #include <common.h> | |
15 | #include <command.h> | |
16 | #include <exports.h> | |
6e295186 | 17 | #include <memalign.h> |
694a0b3f KP |
18 | #include <nand.h> |
19 | #include <onenand_uboot.h> | |
20 | #include <linux/mtd/mtd.h> | |
21 | #include <linux/mtd/partitions.h> | |
ff94bc40 | 22 | #include <linux/err.h> |
694a0b3f | 23 | #include <ubi_uboot.h> |
1221ce45 | 24 | #include <linux/errno.h> |
694a0b3f KP |
25 | #include <jffs2/load_kernel.h> |
26 | ||
147162da JH |
27 | #undef ubi_msg |
28 | #define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__) | |
29 | ||
694a0b3f KP |
30 | #define DEV_TYPE_NONE 0 |
31 | #define DEV_TYPE_NAND 1 | |
32 | #define DEV_TYPE_ONENAND 2 | |
25ea652e | 33 | #define DEV_TYPE_NOR 3 |
694a0b3f KP |
34 | |
35 | /* Private own data */ | |
36 | static struct ubi_device *ubi; | |
a5c40670 | 37 | static char buffer[80]; |
2ee951ba | 38 | static int ubi_initialized; |
694a0b3f KP |
39 | |
40 | struct selected_dev { | |
694a0b3f | 41 | char part_name[80]; |
2d579e50 | 42 | int selected; |
694a0b3f KP |
43 | int nr; |
44 | struct mtd_info *mtd_info; | |
45 | }; | |
46 | ||
47 | static struct selected_dev ubi_dev; | |
48 | ||
2f15cfd1 SR |
49 | #ifdef CONFIG_CMD_UBIFS |
50 | int ubifs_is_mounted(void); | |
51 | void cmd_ubifs_umount(void); | |
52 | #endif | |
53 | ||
694a0b3f KP |
54 | static void display_volume_info(struct ubi_device *ubi) |
55 | { | |
56 | int i; | |
57 | ||
58 | for (i = 0; i < (ubi->vtbl_slots + 1); i++) { | |
59 | if (!ubi->volumes[i]) | |
60 | continue; /* Empty record */ | |
61 | ubi_dump_vol_info(ubi->volumes[i]); | |
62 | } | |
63 | } | |
64 | ||
65 | static void display_ubi_info(struct ubi_device *ubi) | |
66 | { | |
67 | ubi_msg("MTD device name: \"%s\"", ubi->mtd->name); | |
68 | ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20); | |
69 | ubi_msg("physical eraseblock size: %d bytes (%d KiB)", | |
70 | ubi->peb_size, ubi->peb_size >> 10); | |
71 | ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size); | |
72 | ubi_msg("number of good PEBs: %d", ubi->good_peb_count); | |
73 | ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count); | |
74 | ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size); | |
75 | ubi_msg("VID header offset: %d (aligned %d)", | |
76 | ubi->vid_hdr_offset, ubi->vid_hdr_aloffset); | |
77 | ubi_msg("data offset: %d", ubi->leb_start); | |
78 | ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots); | |
79 | ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD); | |
80 | ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT); | |
81 | ubi_msg("number of user volumes: %d", | |
82 | ubi->vol_count - UBI_INT_VOL_COUNT); | |
83 | ubi_msg("available PEBs: %d", ubi->avail_pebs); | |
84 | ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs); | |
85 | ubi_msg("number of PEBs reserved for bad PEB handling: %d", | |
86 | ubi->beb_rsvd_pebs); | |
87 | ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec); | |
88 | } | |
89 | ||
90 | static int ubi_info(int layout) | |
91 | { | |
92 | if (layout) | |
93 | display_volume_info(ubi); | |
94 | else | |
95 | display_ubi_info(ubi); | |
96 | ||
97 | return 0; | |
98 | } | |
99 | ||
f9f4d809 HS |
100 | static int ubi_check_volumename(const struct ubi_volume *vol, char *name) |
101 | { | |
102 | return strcmp(vol->name, name); | |
103 | } | |
104 | ||
105 | static int ubi_check(char *name) | |
106 | { | |
107 | int i; | |
108 | ||
109 | for (i = 0; i < (ubi->vtbl_slots + 1); i++) { | |
110 | if (!ubi->volumes[i]) | |
111 | continue; /* Empty record */ | |
112 | ||
113 | if (!ubi_check_volumename(ubi->volumes[i], name)) | |
114 | return 0; | |
115 | } | |
116 | ||
6d0f4526 | 117 | return 1; |
f9f4d809 HS |
118 | } |
119 | ||
120 | ||
694a0b3f KP |
121 | static int verify_mkvol_req(const struct ubi_device *ubi, |
122 | const struct ubi_mkvol_req *req) | |
123 | { | |
7f5d8a4d | 124 | int n, err = EINVAL; |
694a0b3f KP |
125 | |
126 | if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 || | |
127 | req->name_len < 0) | |
128 | goto bad; | |
129 | ||
130 | if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) && | |
131 | req->vol_id != UBI_VOL_NUM_AUTO) | |
132 | goto bad; | |
133 | ||
134 | if (req->alignment == 0) | |
135 | goto bad; | |
136 | ||
7f5d8a4d SR |
137 | if (req->bytes == 0) { |
138 | printf("No space left in UBI device!\n"); | |
139 | err = ENOMEM; | |
694a0b3f | 140 | goto bad; |
7f5d8a4d | 141 | } |
694a0b3f KP |
142 | |
143 | if (req->vol_type != UBI_DYNAMIC_VOLUME && | |
144 | req->vol_type != UBI_STATIC_VOLUME) | |
145 | goto bad; | |
146 | ||
147 | if (req->alignment > ubi->leb_size) | |
148 | goto bad; | |
149 | ||
150 | n = req->alignment % ubi->min_io_size; | |
151 | if (req->alignment != 1 && n) | |
152 | goto bad; | |
153 | ||
154 | if (req->name_len > UBI_VOL_NAME_MAX) { | |
7f5d8a4d SR |
155 | printf("Name too long!\n"); |
156 | err = ENAMETOOLONG; | |
694a0b3f KP |
157 | goto bad; |
158 | } | |
159 | ||
160 | return 0; | |
161 | bad: | |
694a0b3f KP |
162 | return err; |
163 | } | |
164 | ||
00612422 | 165 | static int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id) |
694a0b3f KP |
166 | { |
167 | struct ubi_mkvol_req req; | |
168 | int err; | |
169 | ||
170 | if (dynamic) | |
171 | req.vol_type = UBI_DYNAMIC_VOLUME; | |
172 | else | |
173 | req.vol_type = UBI_STATIC_VOLUME; | |
174 | ||
00612422 | 175 | req.vol_id = vol_id; |
694a0b3f KP |
176 | req.alignment = 1; |
177 | req.bytes = size; | |
178 | ||
179 | strcpy(req.name, volume); | |
180 | req.name_len = strlen(volume); | |
181 | req.name[req.name_len] = '\0'; | |
182 | req.padding1 = 0; | |
183 | /* It's duplicated at drivers/mtd/ubi/cdev.c */ | |
184 | err = verify_mkvol_req(ubi, &req); | |
185 | if (err) { | |
186 | printf("verify_mkvol_req failed %d\n", err); | |
187 | return err; | |
188 | } | |
dd7185f1 | 189 | printf("Creating %s volume %s of size %lld\n", |
694a0b3f KP |
190 | dynamic ? "dynamic" : "static", volume, size); |
191 | /* Call real ubi create volume */ | |
192 | return ubi_create_volume(ubi, &req); | |
193 | } | |
194 | ||
7f5d8a4d | 195 | static struct ubi_volume *ubi_find_volume(char *volume) |
694a0b3f | 196 | { |
3b653fdb | 197 | struct ubi_volume *vol = NULL; |
7f5d8a4d | 198 | int i; |
694a0b3f KP |
199 | |
200 | for (i = 0; i < ubi->vtbl_slots; i++) { | |
201 | vol = ubi->volumes[i]; | |
7f5d8a4d SR |
202 | if (vol && !strcmp(vol->name, volume)) |
203 | return vol; | |
694a0b3f | 204 | } |
7f5d8a4d SR |
205 | |
206 | printf("Volume %s not found!\n", volume); | |
207 | return NULL; | |
208 | } | |
209 | ||
210 | static int ubi_remove_vol(char *volume) | |
211 | { | |
212 | int err, reserved_pebs, i; | |
213 | struct ubi_volume *vol; | |
214 | ||
215 | vol = ubi_find_volume(volume); | |
216 | if (vol == NULL) | |
217 | return ENODEV; | |
218 | ||
219 | printf("Remove UBI volume %s (id %d)\n", vol->name, vol->vol_id); | |
694a0b3f KP |
220 | |
221 | if (ubi->ro_mode) { | |
222 | printf("It's read-only mode\n"); | |
7f5d8a4d | 223 | err = EROFS; |
694a0b3f KP |
224 | goto out_err; |
225 | } | |
226 | ||
7f5d8a4d | 227 | err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL); |
694a0b3f KP |
228 | if (err) { |
229 | printf("Error changing Vol tabel record err=%x\n", err); | |
230 | goto out_err; | |
231 | } | |
232 | reserved_pebs = vol->reserved_pebs; | |
233 | for (i = 0; i < vol->reserved_pebs; i++) { | |
234 | err = ubi_eba_unmap_leb(ubi, vol, i); | |
235 | if (err) | |
236 | goto out_err; | |
237 | } | |
238 | ||
239 | kfree(vol->eba_tbl); | |
7f5d8a4d SR |
240 | ubi->volumes[vol->vol_id]->eba_tbl = NULL; |
241 | ubi->volumes[vol->vol_id] = NULL; | |
694a0b3f KP |
242 | |
243 | ubi->rsvd_pebs -= reserved_pebs; | |
244 | ubi->avail_pebs += reserved_pebs; | |
245 | i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs; | |
246 | if (i > 0) { | |
247 | i = ubi->avail_pebs >= i ? i : ubi->avail_pebs; | |
248 | ubi->avail_pebs -= i; | |
249 | ubi->rsvd_pebs += i; | |
250 | ubi->beb_rsvd_pebs += i; | |
251 | if (i > 0) | |
252 | ubi_msg("reserve more %d PEBs", i); | |
253 | } | |
254 | ubi->vol_count -= 1; | |
255 | ||
256 | return 0; | |
257 | out_err: | |
0195a7bb | 258 | ubi_err(ubi, "cannot remove volume %s, error %d", volume, err); |
7f5d8a4d SR |
259 | if (err < 0) |
260 | err = -err; | |
694a0b3f KP |
261 | return err; |
262 | } | |
263 | ||
0e350f81 | 264 | static int ubi_volume_continue_write(char *volume, void *buf, size_t size) |
694a0b3f | 265 | { |
7f5d8a4d | 266 | int err = 1; |
694a0b3f KP |
267 | struct ubi_volume *vol; |
268 | ||
7f5d8a4d SR |
269 | vol = ubi_find_volume(volume); |
270 | if (vol == NULL) | |
271 | return ENODEV; | |
272 | ||
694a0b3f KP |
273 | err = ubi_more_update_data(ubi, vol, buf, size); |
274 | if (err < 0) { | |
7f5d8a4d SR |
275 | printf("Couldnt or partially wrote data\n"); |
276 | return -err; | |
694a0b3f KP |
277 | } |
278 | ||
279 | if (err) { | |
280 | size = err; | |
281 | ||
282 | err = ubi_check_volume(ubi, vol->vol_id); | |
7f5d8a4d SR |
283 | if (err < 0) |
284 | return -err; | |
694a0b3f KP |
285 | |
286 | if (err) { | |
0195a7bb HS |
287 | ubi_warn(ubi, "volume %d on UBI device %d is corrupt", |
288 | vol->vol_id, ubi->ubi_num); | |
694a0b3f KP |
289 | vol->corrupted = 1; |
290 | } | |
291 | ||
292 | vol->checked = 1; | |
293 | ubi_gluebi_updated(vol); | |
294 | } | |
295 | ||
296 | return 0; | |
297 | } | |
298 | ||
cc734f5a PB |
299 | int ubi_volume_begin_write(char *volume, void *buf, size_t size, |
300 | size_t full_size) | |
301 | { | |
302 | int err = 1; | |
303 | int rsvd_bytes = 0; | |
304 | struct ubi_volume *vol; | |
305 | ||
306 | vol = ubi_find_volume(volume); | |
307 | if (vol == NULL) | |
308 | return ENODEV; | |
309 | ||
310 | rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad); | |
18f41f2f | 311 | if (size > rsvd_bytes) { |
cc734f5a PB |
312 | printf("size > volume size! Aborting!\n"); |
313 | return EINVAL; | |
314 | } | |
315 | ||
316 | err = ubi_start_update(ubi, vol, full_size); | |
317 | if (err < 0) { | |
318 | printf("Cannot start volume update\n"); | |
319 | return -err; | |
320 | } | |
321 | ||
322 | return ubi_volume_continue_write(volume, buf, size); | |
323 | } | |
324 | ||
325 | int ubi_volume_write(char *volume, void *buf, size_t size) | |
326 | { | |
327 | return ubi_volume_begin_write(volume, buf, size, size); | |
328 | } | |
329 | ||
71829067 | 330 | int ubi_volume_read(char *volume, char *buf, size_t size) |
694a0b3f | 331 | { |
7f5d8a4d | 332 | int err, lnum, off, len, tbuf_size; |
694a0b3f KP |
333 | void *tbuf; |
334 | unsigned long long tmp; | |
7f5d8a4d | 335 | struct ubi_volume *vol; |
694a0b3f | 336 | loff_t offp = 0; |
985fa93e | 337 | size_t len_read; |
694a0b3f | 338 | |
7f5d8a4d SR |
339 | vol = ubi_find_volume(volume); |
340 | if (vol == NULL) | |
341 | return ENODEV; | |
694a0b3f | 342 | |
694a0b3f KP |
343 | if (vol->updating) { |
344 | printf("updating"); | |
7f5d8a4d | 345 | return EBUSY; |
694a0b3f KP |
346 | } |
347 | if (vol->upd_marker) { | |
348 | printf("damaged volume, update marker is set"); | |
7f5d8a4d | 349 | return EBADF; |
694a0b3f KP |
350 | } |
351 | if (offp == vol->used_bytes) | |
352 | return 0; | |
353 | ||
354 | if (size == 0) { | |
7f5d8a4d | 355 | printf("No size specified -> Using max size (%lld)\n", vol->used_bytes); |
694a0b3f KP |
356 | size = vol->used_bytes; |
357 | } | |
358 | ||
359 | if (vol->corrupted) | |
360 | printf("read from corrupted volume %d", vol->vol_id); | |
361 | if (offp + size > vol->used_bytes) | |
2984fd16 | 362 | size = vol->used_bytes - offp; |
694a0b3f KP |
363 | |
364 | tbuf_size = vol->usable_leb_size; | |
365 | if (size < tbuf_size) | |
366 | tbuf_size = ALIGN(size, ubi->min_io_size); | |
4519668b | 367 | tbuf = malloc_cache_aligned(tbuf_size); |
694a0b3f KP |
368 | if (!tbuf) { |
369 | printf("NO MEM\n"); | |
7f5d8a4d | 370 | return ENOMEM; |
694a0b3f KP |
371 | } |
372 | len = size > tbuf_size ? tbuf_size : size; | |
373 | ||
374 | tmp = offp; | |
375 | off = do_div(tmp, vol->usable_leb_size); | |
376 | lnum = tmp; | |
985fa93e | 377 | len_read = size; |
694a0b3f KP |
378 | do { |
379 | if (off + len >= vol->usable_leb_size) | |
380 | len = vol->usable_leb_size - off; | |
381 | ||
382 | err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0); | |
383 | if (err) { | |
384 | printf("read err %x\n", err); | |
7f5d8a4d | 385 | err = -err; |
694a0b3f KP |
386 | break; |
387 | } | |
388 | off += len; | |
389 | if (off == vol->usable_leb_size) { | |
390 | lnum += 1; | |
391 | off -= vol->usable_leb_size; | |
392 | } | |
393 | ||
394 | size -= len; | |
395 | offp += len; | |
396 | ||
694a0b3f | 397 | memcpy(buf, tbuf, len); |
694a0b3f KP |
398 | |
399 | buf += len; | |
400 | len = size > tbuf_size ? tbuf_size : size; | |
401 | } while (size); | |
402 | ||
985fa93e HD |
403 | if (!size) |
404 | env_set_hex("filesize", len_read); | |
405 | ||
694a0b3f | 406 | free(tbuf); |
7f5d8a4d | 407 | return err; |
694a0b3f KP |
408 | } |
409 | ||
25c8f400 SK |
410 | static int ubi_dev_scan(struct mtd_info *info, char *ubidev, |
411 | const char *vid_header_offset) | |
694a0b3f KP |
412 | { |
413 | struct mtd_device *dev; | |
414 | struct part_info *part; | |
415 | struct mtd_partition mtd_part; | |
25c8f400 | 416 | char ubi_mtd_param_buffer[80]; |
694a0b3f KP |
417 | u8 pnum; |
418 | int err; | |
419 | ||
694a0b3f KP |
420 | if (find_dev_and_part(ubidev, &dev, &pnum, &part) != 0) |
421 | return 1; | |
422 | ||
423 | sprintf(buffer, "mtd=%d", pnum); | |
424 | memset(&mtd_part, 0, sizeof(mtd_part)); | |
425 | mtd_part.name = buffer; | |
426 | mtd_part.size = part->size; | |
427 | mtd_part.offset = part->offset; | |
428 | add_mtd_partitions(info, &mtd_part, 1); | |
429 | ||
25c8f400 SK |
430 | strcpy(ubi_mtd_param_buffer, buffer); |
431 | if (vid_header_offset) | |
432 | sprintf(ubi_mtd_param_buffer, "mtd=%d,%s", pnum, | |
433 | vid_header_offset); | |
434 | err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL); | |
694a0b3f KP |
435 | if (err) { |
436 | del_mtd_partitions(info); | |
7f5d8a4d | 437 | return -err; |
694a0b3f KP |
438 | } |
439 | ||
440 | err = ubi_init(); | |
441 | if (err) { | |
442 | del_mtd_partitions(info); | |
7f5d8a4d | 443 | return -err; |
694a0b3f KP |
444 | } |
445 | ||
2ee951ba SR |
446 | ubi_initialized = 1; |
447 | ||
694a0b3f KP |
448 | return 0; |
449 | } | |
450 | ||
cddfc97d | 451 | int ubi_detach(void) |
694a0b3f | 452 | { |
c203ef5d AH |
453 | if (mtdparts_init() != 0) { |
454 | printf("Error initializing mtdparts!\n"); | |
455 | return 1; | |
456 | } | |
457 | ||
71829067 JH |
458 | #ifdef CONFIG_CMD_UBIFS |
459 | /* | |
460 | * Automatically unmount UBIFS partition when user | |
461 | * changes the UBI device. Otherwise the following | |
462 | * UBIFS commands will crash. | |
463 | */ | |
464 | if (ubifs_is_mounted()) | |
465 | cmd_ubifs_umount(); | |
466 | #endif | |
467 | ||
71829067 JH |
468 | /* |
469 | * Call ubi_exit() before re-initializing the UBI subsystem | |
470 | */ | |
471 | if (ubi_initialized) { | |
472 | ubi_exit(); | |
473 | del_mtd_partitions(ubi_dev.mtd_info); | |
cddfc97d | 474 | ubi_initialized = 0; |
71829067 JH |
475 | } |
476 | ||
cddfc97d HS |
477 | ubi_dev.selected = 0; |
478 | return 0; | |
479 | } | |
480 | ||
481 | int ubi_part(char *part_name, const char *vid_header_offset) | |
482 | { | |
483 | int err = 0; | |
484 | char mtd_dev[16]; | |
485 | struct mtd_device *dev; | |
486 | struct part_info *part; | |
487 | u8 pnum; | |
488 | ||
489 | ubi_detach(); | |
71829067 JH |
490 | /* |
491 | * Search the mtd device number where this partition | |
492 | * is located | |
493 | */ | |
494 | if (find_dev_and_part(part_name, &dev, &pnum, &part)) { | |
495 | printf("Partition %s not found!\n", part_name); | |
496 | return 1; | |
497 | } | |
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", part_name, | |
502 | mtd_dev); | |
503 | return 1; | |
504 | } | |
505 | ||
506 | ubi_dev.selected = 1; | |
507 | ||
508 | strcpy(ubi_dev.part_name, part_name); | |
509 | err = ubi_dev_scan(ubi_dev.mtd_info, ubi_dev.part_name, | |
510 | vid_header_offset); | |
511 | if (err) { | |
512 | printf("UBI init error %d\n", err); | |
513 | ubi_dev.selected = 0; | |
514 | return err; | |
515 | } | |
516 | ||
517 | ubi = ubi_devices[0]; | |
518 | ||
519 | return 0; | |
520 | } | |
521 | ||
522 | static int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
523 | { | |
dd7185f1 | 524 | int64_t size = 0; |
71829067 JH |
525 | ulong addr = 0; |
526 | ||
527 | if (argc < 2) | |
528 | return CMD_RET_USAGE; | |
529 | ||
cddfc97d HS |
530 | |
531 | if (strcmp(argv[1], "detach") == 0) { | |
532 | if (argc < 2) | |
533 | return CMD_RET_USAGE; | |
534 | ||
535 | return ubi_detach(); | |
536 | } | |
537 | ||
538 | ||
694a0b3f | 539 | if (strcmp(argv[1], "part") == 0) { |
25c8f400 | 540 | const char *vid_header_offset = NULL; |
2d579e50 | 541 | |
694a0b3f KP |
542 | /* Print current partition */ |
543 | if (argc == 2) { | |
2d579e50 | 544 | if (!ubi_dev.selected) { |
694a0b3f KP |
545 | printf("Error, no UBI device/partition selected!\n"); |
546 | return 1; | |
547 | } | |
548 | ||
2d579e50 | 549 | printf("Device %d: %s, partition %s\n", |
694a0b3f KP |
550 | ubi_dev.nr, ubi_dev.mtd_info->name, ubi_dev.part_name); |
551 | return 0; | |
552 | } | |
553 | ||
47e26b1b | 554 | if (argc < 3) |
4c12eeb8 | 555 | return CMD_RET_USAGE; |
694a0b3f | 556 | |
25c8f400 SK |
557 | if (argc > 3) |
558 | vid_header_offset = argv[3]; | |
694a0b3f | 559 | |
71829067 | 560 | return ubi_part(argv[2], vid_header_offset); |
694a0b3f KP |
561 | } |
562 | ||
2d579e50 | 563 | if ((strcmp(argv[1], "part") != 0) && (!ubi_dev.selected)) { |
694a0b3f KP |
564 | printf("Error, no UBI device/partition selected!\n"); |
565 | return 1; | |
566 | } | |
567 | ||
568 | if (strcmp(argv[1], "info") == 0) { | |
569 | int layout = 0; | |
570 | if (argc > 2 && !strncmp(argv[2], "l", 1)) | |
571 | layout = 1; | |
572 | return ubi_info(layout); | |
573 | } | |
574 | ||
f9f4d809 HS |
575 | if (strcmp(argv[1], "check") == 0) { |
576 | if (argc > 2) | |
577 | return ubi_check(argv[2]); | |
578 | ||
579 | printf("Error, no volume name passed\n"); | |
580 | return 1; | |
581 | } | |
582 | ||
694a0b3f KP |
583 | if (strncmp(argv[1], "create", 6) == 0) { |
584 | int dynamic = 1; /* default: dynamic volume */ | |
00612422 | 585 | int id = UBI_VOL_NUM_AUTO; |
694a0b3f KP |
586 | |
587 | /* Use maximum available size */ | |
588 | size = 0; | |
589 | ||
00612422 LM |
590 | /* E.g., create volume size type vol_id */ |
591 | if (argc == 6) { | |
592 | id = simple_strtoull(argv[5], NULL, 16); | |
593 | argc--; | |
594 | } | |
595 | ||
694a0b3f KP |
596 | /* E.g., create volume size type */ |
597 | if (argc == 5) { | |
598 | if (strncmp(argv[4], "s", 1) == 0) | |
599 | dynamic = 0; | |
600 | else if (strncmp(argv[4], "d", 1) != 0) { | |
601 | printf("Incorrect type\n"); | |
602 | return 1; | |
603 | } | |
604 | argc--; | |
605 | } | |
606 | /* E.g., create volume size */ | |
607 | if (argc == 4) { | |
f59f07ec LM |
608 | if (argv[3][0] != '-') |
609 | size = simple_strtoull(argv[3], NULL, 16); | |
694a0b3f KP |
610 | argc--; |
611 | } | |
612 | /* Use maximum available size */ | |
7f5d8a4d | 613 | if (!size) { |
dd7185f1 PB |
614 | size = (int64_t)ubi->avail_pebs * ubi->leb_size; |
615 | printf("No size specified -> Using max size (%lld)\n", size); | |
7f5d8a4d | 616 | } |
694a0b3f KP |
617 | /* E.g., create volume */ |
618 | if (argc == 3) | |
00612422 | 619 | return ubi_create_vol(argv[2], size, dynamic, id); |
694a0b3f KP |
620 | } |
621 | ||
622 | if (strncmp(argv[1], "remove", 6) == 0) { | |
623 | /* E.g., remove volume */ | |
624 | if (argc == 3) | |
625 | return ubi_remove_vol(argv[2]); | |
626 | } | |
627 | ||
628 | if (strncmp(argv[1], "write", 5) == 0) { | |
71829067 JH |
629 | int ret; |
630 | ||
694a0b3f KP |
631 | if (argc < 5) { |
632 | printf("Please see usage\n"); | |
633 | return 1; | |
634 | } | |
635 | ||
636 | addr = simple_strtoul(argv[2], NULL, 16); | |
a5c40670 | 637 | size = simple_strtoul(argv[4], NULL, 16); |
694a0b3f | 638 | |
cc734f5a PB |
639 | if (strlen(argv[1]) == 10 && |
640 | strncmp(argv[1] + 5, ".part", 5) == 0) { | |
641 | if (argc < 6) { | |
642 | ret = ubi_volume_continue_write(argv[3], | |
643 | (void *)addr, size); | |
644 | } else { | |
645 | size_t full_size; | |
646 | full_size = simple_strtoul(argv[5], NULL, 16); | |
647 | ret = ubi_volume_begin_write(argv[3], | |
648 | (void *)addr, size, full_size); | |
649 | } | |
650 | } else { | |
651 | ret = ubi_volume_write(argv[3], (void *)addr, size); | |
652 | } | |
71829067 | 653 | if (!ret) { |
dd7185f1 | 654 | printf("%lld bytes written to volume %s\n", size, |
71829067 JH |
655 | argv[3]); |
656 | } | |
657 | ||
658 | return ret; | |
694a0b3f KP |
659 | } |
660 | ||
661 | if (strncmp(argv[1], "read", 4) == 0) { | |
662 | size = 0; | |
663 | ||
664 | /* E.g., read volume size */ | |
665 | if (argc == 5) { | |
a5c40670 | 666 | size = simple_strtoul(argv[4], NULL, 16); |
694a0b3f KP |
667 | argc--; |
668 | } | |
669 | ||
670 | /* E.g., read volume */ | |
671 | if (argc == 4) { | |
672 | addr = simple_strtoul(argv[2], NULL, 16); | |
673 | argc--; | |
674 | } | |
675 | ||
71829067 | 676 | if (argc == 3) { |
dd7185f1 | 677 | printf("Read %lld bytes from volume %s to %lx\n", size, |
71829067 JH |
678 | argv[3], addr); |
679 | ||
694a0b3f | 680 | return ubi_volume_read(argv[3], (char *)addr, size); |
71829067 | 681 | } |
694a0b3f KP |
682 | } |
683 | ||
684 | printf("Please see usage\n"); | |
7f5d8a4d | 685 | return 1; |
694a0b3f KP |
686 | } |
687 | ||
388a29d0 FM |
688 | U_BOOT_CMD( |
689 | ubi, 6, 1, do_ubi, | |
2fb2604d | 690 | "ubi commands", |
cddfc97d HS |
691 | "detach" |
692 | " - detach ubi from a mtd partition\n" | |
693 | "ubi part [part] [offset]\n" | |
25c8f400 SK |
694 | " - Show or set current partition (with optional VID" |
695 | " header offset)\n" | |
694a0b3f KP |
696 | "ubi info [l[ayout]]" |
697 | " - Display volume and ubi layout information\n" | |
f9f4d809 HS |
698 | "ubi check volumename" |
699 | " - check if volumename exists\n" | |
f59f07ec LM |
700 | "ubi create[vol] volume [size] [type] [id]\n" |
701 | " - create volume name with size ('-' for maximum" | |
702 | " available size)\n" | |
694a0b3f KP |
703 | "ubi write[vol] address volume size" |
704 | " - Write volume from address with size\n" | |
cc734f5a PB |
705 | "ubi write.part address volume size [fullsize]\n" |
706 | " - Write part of a volume from address\n" | |
694a0b3f KP |
707 | "ubi read[vol] address volume [size]" |
708 | " - Read volume to address with size\n" | |
709 | "ubi remove[vol] volume" | |
710 | " - Remove volume\n" | |
711 | "[Legends]\n" | |
f6ca3b70 AW |
712 | " volume: character name\n" |
713 | " size: specified in bytes\n" | |
a89c33db | 714 | " type: s[tatic] or d[ynamic] (default=dynamic)" |
694a0b3f | 715 | ); |