]> git.sur5r.net Git - u-boot/blob - common/cmd_ubi.c
UBI: Add UBI command support
[u-boot] / 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
30 /* Private own data */
31 static struct ubi_device *ubi;
32
33 struct selected_dev {
34         char dev_name[32];      /* NAND/OneNAND etc */
35         char part_name[80];
36         int type;
37         int nr;
38         struct mtd_info *mtd_info;
39 };
40
41 static struct selected_dev ubi_dev;
42
43 static void ubi_dump_vol_info(const struct ubi_volume *vol)
44 {
45         ubi_msg("volume information dump:");
46         ubi_msg("vol_id          %d", vol->vol_id);
47         ubi_msg("reserved_pebs   %d", vol->reserved_pebs);
48         ubi_msg("alignment       %d", vol->alignment);
49         ubi_msg("data_pad        %d", vol->data_pad);
50         ubi_msg("vol_type        %d", vol->vol_type);
51         ubi_msg("name_len        %d", vol->name_len);
52         ubi_msg("usable_leb_size %d", vol->usable_leb_size);
53         ubi_msg("used_ebs        %d", vol->used_ebs);
54         ubi_msg("used_bytes      %lld", vol->used_bytes);
55         ubi_msg("last_eb_bytes   %d", vol->last_eb_bytes);
56         ubi_msg("corrupted       %d", vol->corrupted);
57         ubi_msg("upd_marker      %d", vol->upd_marker);
58
59         if (vol->name_len <= UBI_VOL_NAME_MAX &&
60                 strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
61                 ubi_msg("name            %s", vol->name);
62         } else {
63                 ubi_msg("the 1st 5 characters of the name: %c%c%c%c%c",
64                                 vol->name[0], vol->name[1], vol->name[2],
65                                 vol->name[3], vol->name[4]);
66         }
67         printf("\n");
68 }
69
70 static void display_volume_info(struct ubi_device *ubi)
71 {
72         int i;
73
74         for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
75                 if (!ubi->volumes[i])
76                         continue;       /* Empty record */
77                 ubi_dump_vol_info(ubi->volumes[i]);
78         }
79 }
80
81 static void display_ubi_info(struct ubi_device *ubi)
82 {
83         ubi_msg("MTD device name:            \"%s\"", ubi->mtd->name);
84         ubi_msg("MTD device size:            %llu MiB", ubi->flash_size >> 20);
85         ubi_msg("physical eraseblock size:   %d bytes (%d KiB)",
86                         ubi->peb_size, ubi->peb_size >> 10);
87         ubi_msg("logical eraseblock size:    %d bytes", ubi->leb_size);
88         ubi_msg("number of good PEBs:        %d", ubi->good_peb_count);
89         ubi_msg("number of bad PEBs:         %d", ubi->bad_peb_count);
90         ubi_msg("smallest flash I/O unit:    %d", ubi->min_io_size);
91         ubi_msg("VID header offset:          %d (aligned %d)",
92                         ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
93         ubi_msg("data offset:                %d", ubi->leb_start);
94         ubi_msg("max. allowed volumes:       %d", ubi->vtbl_slots);
95         ubi_msg("wear-leveling threshold:    %d", CONFIG_MTD_UBI_WL_THRESHOLD);
96         ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
97         ubi_msg("number of user volumes:     %d",
98                         ubi->vol_count - UBI_INT_VOL_COUNT);
99         ubi_msg("available PEBs:             %d", ubi->avail_pebs);
100         ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
101         ubi_msg("number of PEBs reserved for bad PEB handling: %d",
102                         ubi->beb_rsvd_pebs);
103         ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
104 }
105
106 static int ubi_info(int layout)
107 {
108         if (layout)
109                 display_volume_info(ubi);
110         else
111                 display_ubi_info(ubi);
112
113         return 0;
114 }
115
116 static int parse_num(size_t *num, const char *token)
117 {
118         char *endp;
119         size_t n;
120
121         n = (size_t) ustrtoul(token, &endp, 0);
122         if (*endp)
123                 return -EINVAL;
124
125         *num = n;
126         return 0;
127 }
128
129 static int verify_mkvol_req(const struct ubi_device *ubi,
130                             const struct ubi_mkvol_req *req)
131 {
132         int n, err = -EINVAL;
133
134         if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
135             req->name_len < 0)
136                 goto bad;
137
138         if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
139             req->vol_id != UBI_VOL_NUM_AUTO)
140                 goto bad;
141
142         if (req->alignment == 0)
143                 goto bad;
144
145         if (req->bytes == 0)
146                 goto bad;
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                 err = -ENAMETOOLONG;
161                 goto bad;
162         }
163
164         return 0;
165 bad:
166         printf("bad volume creation request");
167         return err;
168 }
169
170 static int ubi_create_vol(char *volume, int 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 %d\n",
195                 dynamic ? "dynamic" : "static", volume, size);
196         /* Call real ubi create volume */
197         return ubi_create_volume(ubi, &req);
198 }
199
200 static int ubi_remove_vol(char *volume)
201 {
202         int i, err, reserved_pebs;
203         int found = 0, vol_id = 0;
204         struct ubi_volume *vol;
205
206         for (i = 0; i < ubi->vtbl_slots; i++) {
207                 vol = ubi->volumes[i];
208                 if (vol && !strcmp(vol->name, volume)) {
209                         printf("Volume %s found at valid %d\n", volume, i);
210                         vol_id = i;
211                         found = 1;
212                         break;
213                 }
214         }
215         if (!found) {
216                 printf("%s volume not found\n", volume);
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_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_id]->eba_tbl = NULL;
241         ubi->volumes[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("cannot remove volume %d, error %d", vol_id, err);
259         return err;
260 }
261
262 static int ubi_volume_write(char *volume, void *buf, size_t size)
263 {
264         int i = 0, err = -1;
265         int rsvd_bytes = 0;
266         int found = 0;
267         struct ubi_volume *vol;
268
269         for (i = 0; i < ubi->vtbl_slots; i++) {
270                 vol = ubi->volumes[i];
271                 if (vol && !strcmp(vol->name, volume)) {
272                         printf("Volume \"%s\" found at volume id %d\n", volume, i);
273                         found = 1;
274                         break;
275                 }
276         }
277         if (!found) {
278                 printf("%s volume not found\n", volume);
279                 return 1;
280         }
281         rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
282         if (size < 0 || size > rsvd_bytes) {
283                 printf("rsvd_bytes=%d vol->reserved_pebs=%d ubi->leb_size=%d\n",
284                      rsvd_bytes, vol->reserved_pebs, ubi->leb_size);
285                 printf("vol->data_pad=%d\n", vol->data_pad);
286                 printf("Size > volume size !!\n");
287                 return 1;
288         }
289
290         err = ubi_start_update(ubi, vol, size);
291         if (err < 0) {
292                 printf("Cannot start volume update\n");
293                 return err;
294         }
295
296         err = ubi_more_update_data(ubi, vol, buf, size);
297         if (err < 0) {
298                 printf("Couldnt or partially wrote data \n");
299                 return err;
300         }
301
302         if (err) {
303                 size = err;
304
305                 err = ubi_check_volume(ubi, vol->vol_id);
306                 if ( err < 0 )
307                         return err;
308
309                 if (err) {
310                         ubi_warn("volume %d on UBI device %d is corrupted",
311                                         vol->vol_id, ubi->ubi_num);
312                         vol->corrupted = 1;
313                 }
314
315                 vol->checked = 1;
316                 ubi_gluebi_updated(vol);
317         }
318
319         return 0;
320 }
321
322 static int ubi_volume_read(char *volume, char *buf, size_t size)
323 {
324         int err, lnum, off, len, tbuf_size, i = 0;
325         size_t count_save = size;
326         void *tbuf;
327         unsigned long long tmp;
328         struct ubi_volume *vol = NULL;
329         loff_t offp = 0;
330
331         for (i = 0; i < ubi->vtbl_slots; i++) {
332                 vol = ubi->volumes[i];
333                 if (vol && !strcmp(vol->name, volume)) {
334                         printf("Volume %s found at volume id %d\n",
335                                 volume, vol->vol_id);
336                         break;
337                 }
338         }
339         if (i == ubi->vtbl_slots) {
340                 printf("%s volume not found\n", volume);
341                 return 0;
342         }
343
344         printf("read %i bytes from volume %d to %x(buf address)\n",
345                (int) size, vol->vol_id, (unsigned)buf);
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("Read [%lu] bytes\n", (unsigned long) 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                 count_save = 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         printf("off=%d lnum=%d\n", off, lnum);
382         do {
383                 if (off + len >= vol->usable_leb_size)
384                         len = vol->usable_leb_size - off;
385
386                 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
387                 if (err) {
388                         printf("read err %x\n", 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                 printf("buf = %x\n", (unsigned)buf);
401                 memcpy(buf, tbuf, len);
402                 printf("buf[0] = %x\n", buf[0]);
403
404                 buf += len;
405                 len = size > tbuf_size ? tbuf_size : size;
406         } while (size);
407
408         free(tbuf);
409         return err ? err : count_save - size;
410 }
411
412 static int ubi_dev_scan(struct mtd_info *info, char *ubidev)
413 {
414         struct mtd_device *dev;
415         struct part_info *part;
416         struct mtd_partition mtd_part;
417         char buffer[32];
418         u8 pnum;
419         int err;
420
421         if (mtdparts_init() != 0)
422                 return 1;
423
424         if (find_dev_and_part(ubidev, &dev, &pnum, &part) != 0)
425                 return 1;
426
427         sprintf(buffer, "mtd=%d", pnum);
428         memset(&mtd_part, 0, sizeof(mtd_part));
429         mtd_part.name = buffer;
430         mtd_part.size = part->size;
431         mtd_part.offset = part->offset;
432         add_mtd_partitions(info, &mtd_part, 1);
433
434         err = ubi_mtd_param_parse(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         return 0;
447 }
448
449 static int do_ubi(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
450 {
451         size_t size = 0;
452         ulong addr = 0;
453         int err = 0;
454
455         if (argc < 2) {
456                 printf("Usage:\n%s\n", cmdtp->usage);
457                 return 1;
458         }
459
460         if (strcmp(argv[1], "part") == 0) {
461                 /* Print current partition */
462                 if (argc == 2) {
463                         if (ubi_dev.type == DEV_TYPE_NONE) {
464                                 printf("Error, no UBI device/partition selected!\n");
465                                 return 1;
466                         }
467
468                         printf("%s Device %d: %s, partition %s\n", ubi_dev.dev_name,
469                                ubi_dev.nr, ubi_dev.mtd_info->name, ubi_dev.part_name);
470                         return 0;
471                 }
472
473                 if (argc < 4) {
474                         printf("Usage:\n%s\n", cmdtp->usage);
475                         return 1;
476                 }
477
478                 /* todo: get dev number for NAND... */
479                 ubi_dev.nr = 0;
480
481                 /*
482                  * Check for nand|onenand selection
483                  */
484 #if defined(CONFIG_CMD_NAND)
485                 if (strcmp(argv[2], "nand") == 0) {
486                         strcpy(ubi_dev.dev_name, "NAND");
487                         ubi_dev.type = DEV_TYPE_NAND;
488                         ubi_dev.mtd_info = &nand_info[ubi_dev.nr];
489                 }
490 #endif
491 #if defined(CONFIG_CMD_ONENAND)
492                 if (strcmp(argv[2], "onenand") == 0) {
493                         strcpy(ubi_dev.dev_name, "OneNAND");
494                         ubi_dev.type = DEV_TYPE_ONENAND;
495                         ubi_dev.mtd_info = &onenand_mtd;
496                 }
497 #endif
498
499                 if (ubi_dev.type == DEV_TYPE_NONE) {
500                         printf("Error, no UBI device/partition selected!\n");
501                         return 1;
502                 }
503
504                 strcpy(ubi_dev.part_name, argv[3]);
505                 err = ubi_dev_scan(ubi_dev.mtd_info, ubi_dev.part_name);
506                 if (err) {
507                         printf("UBI init error %d\n", err);
508                         return err;
509                 }
510
511                 ubi = ubi_devices[0];
512
513                 return 0;
514         }
515
516         if ((strcmp(argv[1], "part") != 0) && (ubi_dev.type == DEV_TYPE_NONE)) {
517                 printf("Error, no UBI device/partition selected!\n");
518                 return 1;
519         }
520
521         if (strcmp(argv[1], "info") == 0) {
522                 int layout = 0;
523                 if (argc > 2 && !strncmp(argv[2], "l", 1))
524                         layout = 1;
525                 return ubi_info(layout);
526         }
527
528         if (strncmp(argv[1], "create", 6) == 0) {
529                 int dynamic = 1;        /* default: dynamic volume */
530
531                 /* Use maximum available size */
532                 size = 0;
533
534                 /* E.g., create volume size type */
535                 if (argc == 5) {
536                         if (strncmp(argv[4], "s", 1) == 0)
537                                 dynamic = 0;
538                         else if (strncmp(argv[4], "d", 1) != 0) {
539                                 printf("Incorrect type\n");
540                                 return 1;
541                         }
542                         argc--;
543                 }
544                 /* E.g., create volume size */
545                 if (argc == 4) {
546                         err = parse_num(&size, argv[3]);
547                         if (err) {
548                                 printf("Incorrect type\n");
549                                 return err;
550                         }
551                         argc--;
552                 }
553                 /* Use maximum available size */
554                 if (!size)
555                         size = ubi->avail_pebs * ubi->leb_size;
556                 /* E.g., create volume */
557                 if (argc == 3)
558                         return ubi_create_vol(argv[2], size, dynamic);
559         }
560
561         if (strncmp(argv[1], "remove", 6) == 0) {
562                 /* E.g., remove volume */
563                 if (argc == 3)
564                         return ubi_remove_vol(argv[2]);
565         }
566
567         if (strncmp(argv[1], "write", 5) == 0) {
568                 if (argc < 5) {
569                         printf("Please see usage\n");
570                         return 1;
571                 }
572
573                 addr = simple_strtoul(argv[2], NULL, 16);
574                 err = parse_num(&size, argv[4]);
575                 if (err) {
576                         printf("Please see usage\n");
577                         return err;
578                 }
579
580                 return ubi_volume_write(argv[3], (void *)addr, size);
581         }
582
583         if (strncmp(argv[1], "read", 4) == 0) {
584                 size = 0;
585
586                 /* E.g., read volume size */
587                 if (argc == 5) {
588                         err = parse_num(&size, argv[4]);
589                         if (err) {
590                                 printf("Please see usage\n");
591                                 return err;
592                         }
593                         argc--;
594                 }
595
596                 /* E.g., read volume */
597                 if (argc == 4) {
598                         addr = simple_strtoul(argv[2], NULL, 16);
599                         argc--;
600                 }
601
602                 if (argc == 3)
603                         return ubi_volume_read(argv[3], (char *)addr, size);
604         }
605
606         printf("Please see usage\n");
607         return -1;
608 }
609
610 U_BOOT_CMD(ubi, 6, 1, do_ubi,
611         "ubi      - ubi commands\n",
612         "part [nand|onenand] [part]"
613                 " - Show or set current partition\n"
614         "ubi info [l[ayout]]"
615                 " - Display volume and ubi layout information\n"
616         "ubi create[vol] volume [size] [type]"
617                 " - create volume name with size\n"
618         "ubi write[vol] address volume size"
619                 " - Write volume from address with size\n"
620         "ubi read[vol] address volume [size]"
621                 " - Read volume to address with size\n"
622         "ubi remove[vol] volume"
623                 " - Remove volume\n"
624         "[Legends]\n"
625         " volume: charater name\n"
626         " size: KiB, MiB, GiB, and bytes\n"
627         " type: s[tatic] or d[ynamic] (default=dynamic)\n"
628 );