Merge branch 'master' of ssh://gemini/home/wd/git/u-boot/master
[kernel/u-boot.git] / common / cmd_ubi.c
1 /*
2  * Unsorted Block Image commands
3  *
4  *  Copyright (C) 2008 Samsung Electronics
5  *  Kyungmin Park <kyungmin.park@samsung.com>
6  *
7  * Copyright 2008 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 (mtdparts_init() != 0)
408                 return 1;
409
410         if (find_dev_and_part(ubidev, &dev, &pnum, &part) != 0)
411                 return 1;
412
413         sprintf(buffer, "mtd=%d", pnum);
414         memset(&mtd_part, 0, sizeof(mtd_part));
415         mtd_part.name = buffer;
416         mtd_part.size = part->size;
417         mtd_part.offset = part->offset;
418         add_mtd_partitions(info, &mtd_part, 1);
419
420         err = ubi_mtd_param_parse(buffer, NULL);
421         if (err) {
422                 del_mtd_partitions(info);
423                 return err;
424         }
425
426         err = ubi_init();
427         if (err) {
428                 del_mtd_partitions(info);
429                 return err;
430         }
431
432         ubi_initialized = 1;
433
434         return 0;
435 }
436
437 static int do_ubi(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
438 {
439         size_t size = 0;
440         ulong addr = 0;
441         int err = 0;
442
443         if (argc < 2) {
444                 printf("Usage:\n%s\n", cmdtp->usage);
445                 return 1;
446         }
447
448         if (strcmp(argv[1], "part") == 0) {
449                 /* Print current partition */
450                 if (argc == 2) {
451                         if (ubi_dev.type == DEV_TYPE_NONE) {
452                                 printf("Error, no UBI device/partition selected!\n");
453                                 return 1;
454                         }
455
456                         printf("%s Device %d: %s, partition %s\n", ubi_dev.dev_name,
457                                ubi_dev.nr, ubi_dev.mtd_info->name, ubi_dev.part_name);
458                         return 0;
459                 }
460
461                 if (argc < 4) {
462                         printf("Usage:\n%s\n", cmdtp->usage);
463                         return 1;
464                 }
465
466                 /* todo: get dev number for NAND... */
467                 ubi_dev.nr = 0;
468
469                 /*
470                  * Call ubi_exit() before re-initializing the UBI subsystem
471                  */
472                 if (ubi_initialized) {
473                         ubi_exit();
474                         del_mtd_partitions(ubi_dev.mtd_info);
475                 }
476
477                 /*
478                  * Check for nand|onenand selection
479                  */
480 #if defined(CONFIG_CMD_NAND)
481                 if (strcmp(argv[2], "nand") == 0) {
482                         strcpy(ubi_dev.dev_name, "NAND");
483                         ubi_dev.type = DEV_TYPE_NAND;
484                         ubi_dev.mtd_info = &nand_info[ubi_dev.nr];
485                 }
486 #endif
487 #if defined(CONFIG_FLASH_CFI_MTD)
488                 if (strcmp(argv[2], "nor") == 0) {
489                         strcpy(ubi_dev.dev_name, "NOR");
490                         ubi_dev.type = DEV_TYPE_NOR;
491                         ubi_dev.mtd_info = get_mtd_device_nm(CFI_MTD_DEV_NAME);
492                 }
493 #endif
494 #if defined(CONFIG_CMD_ONENAND)
495                 if (strcmp(argv[2], "onenand") == 0) {
496                         strcpy(ubi_dev.dev_name, "OneNAND");
497                         ubi_dev.type = DEV_TYPE_ONENAND;
498                         ubi_dev.mtd_info = &onenand_mtd;
499                 }
500 #endif
501
502                 if (ubi_dev.type == DEV_TYPE_NONE) {
503                         printf("Error, no UBI device/partition selected!\n");
504                         return 1;
505                 }
506
507                 strcpy(ubi_dev.part_name, argv[3]);
508                 err = ubi_dev_scan(ubi_dev.mtd_info, ubi_dev.part_name);
509                 if (err) {
510                         printf("UBI init error %d\n", err);
511                         ubi_dev.type = DEV_TYPE_NONE;
512                         return err;
513                 }
514
515                 ubi = ubi_devices[0];
516
517                 return 0;
518         }
519
520         if ((strcmp(argv[1], "part") != 0) && (ubi_dev.type == DEV_TYPE_NONE)) {
521                 printf("Error, no UBI device/partition selected!\n");
522                 return 1;
523         }
524
525         if (strcmp(argv[1], "info") == 0) {
526                 int layout = 0;
527                 if (argc > 2 && !strncmp(argv[2], "l", 1))
528                         layout = 1;
529                 return ubi_info(layout);
530         }
531
532         if (strncmp(argv[1], "create", 6) == 0) {
533                 int dynamic = 1;        /* default: dynamic volume */
534
535                 /* Use maximum available size */
536                 size = 0;
537
538                 /* E.g., create volume size type */
539                 if (argc == 5) {
540                         if (strncmp(argv[4], "s", 1) == 0)
541                                 dynamic = 0;
542                         else if (strncmp(argv[4], "d", 1) != 0) {
543                                 printf("Incorrect type\n");
544                                 return 1;
545                         }
546                         argc--;
547                 }
548                 /* E.g., create volume size */
549                 if (argc == 4) {
550                         size = simple_strtoul(argv[3], NULL, 16);
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                 size = simple_strtoul(argv[4], NULL, 16);
575
576                 return ubi_volume_write(argv[3], (void *)addr, size);
577         }
578
579         if (strncmp(argv[1], "read", 4) == 0) {
580                 size = 0;
581
582                 /* E.g., read volume size */
583                 if (argc == 5) {
584                         size = simple_strtoul(argv[4], NULL, 16);
585                         argc--;
586                 }
587
588                 /* E.g., read volume */
589                 if (argc == 4) {
590                         addr = simple_strtoul(argv[2], NULL, 16);
591                         argc--;
592                 }
593
594                 if (argc == 3)
595                         return ubi_volume_read(argv[3], (char *)addr, size);
596         }
597
598         printf("Please see usage\n");
599         return -1;
600 }
601
602 U_BOOT_CMD(ubi, 6, 1, do_ubi,
603         "ubi      - ubi commands\n",
604         "part [nand|nor|onenand] [part]"
605                 " - Show or set current partition\n"
606         "ubi info [l[ayout]]"
607                 " - Display volume and ubi layout information\n"
608         "ubi create[vol] volume [size] [type]"
609                 " - create volume name with size\n"
610         "ubi write[vol] address volume size"
611                 " - Write volume from address with size\n"
612         "ubi read[vol] address volume [size]"
613                 " - Read volume to address with size\n"
614         "ubi remove[vol] volume"
615                 " - Remove volume\n"
616         "[Legends]\n"
617         " volume: charater name\n"
618         " size: KiB, MiB, GiB, and bytes\n"
619         " type: s[tatic] or d[ynamic] (default=dynamic)\n"
620 );