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