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