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