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