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