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