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