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