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