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