blob: cbc10c549474690f9adcb08c4c95d1ba479ed323 [file] [log] [blame]
Kyungmin Park694a0b32008-11-19 11:47:05 +01001/*
2 * Unsorted Block Image commands
3 *
4 * Copyright (C) 2008 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
6 *
Stefan Roese2d579e52009-04-24 20:24:19 +02007 * Copyright 2008-2009 Stefan Roese <sr@denx.de>, DENX Software Engineering
Kyungmin Park694a0b32008-11-19 11:47:05 +01008 *
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>
Heiko Schocherff94bc42014-06-24 10:10:04 +020022#include <linux/err.h>
Kyungmin Park694a0b32008-11-19 11:47:05 +010023#include <ubi_uboot.h>
24#include <asm/errno.h>
25#include <jffs2/load_kernel.h>
26
Joe Hershberger147162d2013-04-08 10:32:49 +000027#undef ubi_msg
28#define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__)
29
Kyungmin Park694a0b32008-11-19 11:47:05 +010030#define DEV_TYPE_NONE 0
31#define DEV_TYPE_NAND 1
32#define DEV_TYPE_ONENAND 2
Piotr Ziecik25ea6522008-11-17 15:58:00 +010033#define DEV_TYPE_NOR 3
Kyungmin Park694a0b32008-11-19 11:47:05 +010034
35/* Private own data */
36static struct ubi_device *ubi;
Stefan Roesea5c40672008-11-24 08:31:16 +010037static char buffer[80];
Stefan Roese2ee951b2008-11-27 14:07:09 +010038static int ubi_initialized;
Kyungmin Park694a0b32008-11-19 11:47:05 +010039
40struct selected_dev {
Kyungmin Park694a0b32008-11-19 11:47:05 +010041 char part_name[80];
Stefan Roese2d579e52009-04-24 20:24:19 +020042 int selected;
Kyungmin Park694a0b32008-11-19 11:47:05 +010043 int nr;
44 struct mtd_info *mtd_info;
45};
46
47static struct selected_dev ubi_dev;
48
Stefan Roese2f15cfd2010-11-01 17:28:22 +010049#ifdef CONFIG_CMD_UBIFS
50int ubifs_is_mounted(void);
51void cmd_ubifs_umount(void);
52#endif
53
Kyungmin Park694a0b32008-11-19 11:47:05 +010054static void display_volume_info(struct ubi_device *ubi)
55{
56 int i;
57
58 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
59 if (!ubi->volumes[i])
60 continue; /* Empty record */
61 ubi_dump_vol_info(ubi->volumes[i]);
62 }
63}
64
65static void display_ubi_info(struct ubi_device *ubi)
66{
67 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
68 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
69 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
70 ubi->peb_size, ubi->peb_size >> 10);
71 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
72 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
73 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
74 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
75 ubi_msg("VID header offset: %d (aligned %d)",
76 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
77 ubi_msg("data offset: %d", ubi->leb_start);
78 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
79 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
80 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
81 ubi_msg("number of user volumes: %d",
82 ubi->vol_count - UBI_INT_VOL_COUNT);
83 ubi_msg("available PEBs: %d", ubi->avail_pebs);
84 ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
85 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
86 ubi->beb_rsvd_pebs);
87 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
88}
89
90static int ubi_info(int layout)
91{
92 if (layout)
93 display_volume_info(ubi);
94 else
95 display_ubi_info(ubi);
96
97 return 0;
98}
99
Heiko Schocherf9f4d802014-01-25 07:27:11 +0100100static int ubi_check_volumename(const struct ubi_volume *vol, char *name)
101{
102 return strcmp(vol->name, name);
103}
104
105static int ubi_check(char *name)
106{
107 int i;
108
109 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
110 if (!ubi->volumes[i])
111 continue; /* Empty record */
112
113 if (!ubi_check_volumename(ubi->volumes[i], name))
114 return 0;
115 }
116
Stefan Agner6d0f4522015-04-10 11:25:43 +0200117 return 1;
Heiko Schocherf9f4d802014-01-25 07:27:11 +0100118}
119
120
Kyungmin Park694a0b32008-11-19 11:47:05 +0100121static int verify_mkvol_req(const struct ubi_device *ubi,
122 const struct ubi_mkvol_req *req)
123{
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100124 int n, err = EINVAL;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100125
126 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
127 req->name_len < 0)
128 goto bad;
129
130 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
131 req->vol_id != UBI_VOL_NUM_AUTO)
132 goto bad;
133
134 if (req->alignment == 0)
135 goto bad;
136
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100137 if (req->bytes == 0) {
138 printf("No space left in UBI device!\n");
139 err = ENOMEM;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100140 goto bad;
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100141 }
Kyungmin Park694a0b32008-11-19 11:47:05 +0100142
143 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
144 req->vol_type != UBI_STATIC_VOLUME)
145 goto bad;
146
147 if (req->alignment > ubi->leb_size)
148 goto bad;
149
150 n = req->alignment % ubi->min_io_size;
151 if (req->alignment != 1 && n)
152 goto bad;
153
154 if (req->name_len > UBI_VOL_NAME_MAX) {
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100155 printf("Name too long!\n");
156 err = ENAMETOOLONG;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100157 goto bad;
158 }
159
160 return 0;
161bad:
Kyungmin Park694a0b32008-11-19 11:47:05 +0100162 return err;
163}
164
Paul Burtondd7185f2013-09-04 15:16:58 +0100165static int ubi_create_vol(char *volume, int64_t size, int dynamic)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100166{
167 struct ubi_mkvol_req req;
168 int err;
169
170 if (dynamic)
171 req.vol_type = UBI_DYNAMIC_VOLUME;
172 else
173 req.vol_type = UBI_STATIC_VOLUME;
174
175 req.vol_id = UBI_VOL_NUM_AUTO;
176 req.alignment = 1;
177 req.bytes = size;
178
179 strcpy(req.name, volume);
180 req.name_len = strlen(volume);
181 req.name[req.name_len] = '\0';
182 req.padding1 = 0;
183 /* It's duplicated at drivers/mtd/ubi/cdev.c */
184 err = verify_mkvol_req(ubi, &req);
185 if (err) {
186 printf("verify_mkvol_req failed %d\n", err);
187 return err;
188 }
Paul Burtondd7185f2013-09-04 15:16:58 +0100189 printf("Creating %s volume %s of size %lld\n",
Kyungmin Park694a0b32008-11-19 11:47:05 +0100190 dynamic ? "dynamic" : "static", volume, size);
191 /* Call real ubi create volume */
192 return ubi_create_volume(ubi, &req);
193}
194
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100195static struct ubi_volume *ubi_find_volume(char *volume)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100196{
Peter Tyser3b653fd2010-04-04 22:40:50 -0500197 struct ubi_volume *vol = NULL;
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100198 int i;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100199
200 for (i = 0; i < ubi->vtbl_slots; i++) {
201 vol = ubi->volumes[i];
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100202 if (vol && !strcmp(vol->name, volume))
203 return vol;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100204 }
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100205
206 printf("Volume %s not found!\n", volume);
207 return NULL;
208}
209
210static int ubi_remove_vol(char *volume)
211{
212 int err, reserved_pebs, i;
213 struct ubi_volume *vol;
214
215 vol = ubi_find_volume(volume);
216 if (vol == NULL)
217 return ENODEV;
218
219 printf("Remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100220
221 if (ubi->ro_mode) {
222 printf("It's read-only mode\n");
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100223 err = EROFS;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100224 goto out_err;
225 }
226
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100227 err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100228 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);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100240 ubi->volumes[vol->vol_id]->eba_tbl = NULL;
241 ubi->volumes[vol->vol_id] = NULL;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100242
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;
257out_err:
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100258 ubi_err("cannot remove volume %s, error %d", volume, err);
259 if (err < 0)
260 err = -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100261 return err;
262}
263
Jeroen Hofstee0e350f82014-06-23 00:22:08 +0200264static int ubi_volume_continue_write(char *volume, void *buf, size_t size)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100265{
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100266 int err = 1;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100267 struct ubi_volume *vol;
268
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100269 vol = ubi_find_volume(volume);
270 if (vol == NULL)
271 return ENODEV;
272
Kyungmin Park694a0b32008-11-19 11:47:05 +0100273 err = ubi_more_update_data(ubi, vol, buf, size);
274 if (err < 0) {
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100275 printf("Couldnt or partially wrote data\n");
276 return -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100277 }
278
279 if (err) {
280 size = err;
281
282 err = ubi_check_volume(ubi, vol->vol_id);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100283 if (err < 0)
284 return -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100285
286 if (err) {
287 ubi_warn("volume %d on UBI device %d is corrupted",
288 vol->vol_id, ubi->ubi_num);
289 vol->corrupted = 1;
290 }
291
292 vol->checked = 1;
293 ubi_gluebi_updated(vol);
294 }
295
296 return 0;
297}
298
Paul Burtoncc734f52013-09-04 15:16:59 +0100299int ubi_volume_begin_write(char *volume, void *buf, size_t size,
300 size_t full_size)
301{
302 int err = 1;
303 int rsvd_bytes = 0;
304 struct ubi_volume *vol;
305
306 vol = ubi_find_volume(volume);
307 if (vol == NULL)
308 return ENODEV;
309
310 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
311 if (size < 0 || size > rsvd_bytes) {
312 printf("size > volume size! Aborting!\n");
313 return EINVAL;
314 }
315
316 err = ubi_start_update(ubi, vol, full_size);
317 if (err < 0) {
318 printf("Cannot start volume update\n");
319 return -err;
320 }
321
322 return ubi_volume_continue_write(volume, buf, size);
323}
324
325int ubi_volume_write(char *volume, void *buf, size_t size)
326{
327 return ubi_volume_begin_write(volume, buf, size, size);
328}
329
Joe Hershberger71829062013-04-08 10:32:47 +0000330int ubi_volume_read(char *volume, char *buf, size_t size)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100331{
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100332 int err, lnum, off, len, tbuf_size;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100333 void *tbuf;
334 unsigned long long tmp;
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100335 struct ubi_volume *vol;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100336 loff_t offp = 0;
337
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100338 vol = ubi_find_volume(volume);
339 if (vol == NULL)
340 return ENODEV;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100341
Kyungmin Park694a0b32008-11-19 11:47:05 +0100342 if (vol->updating) {
343 printf("updating");
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100344 return EBUSY;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100345 }
346 if (vol->upd_marker) {
347 printf("damaged volume, update marker is set");
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100348 return EBADF;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100349 }
350 if (offp == vol->used_bytes)
351 return 0;
352
353 if (size == 0) {
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100354 printf("No size specified -> Using max size (%lld)\n", vol->used_bytes);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100355 size = vol->used_bytes;
356 }
357
358 if (vol->corrupted)
359 printf("read from corrupted volume %d", vol->vol_id);
360 if (offp + size > vol->used_bytes)
Marek Vasut2984fd12011-09-30 12:13:25 +0200361 size = vol->used_bytes - offp;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100362
363 tbuf_size = vol->usable_leb_size;
364 if (size < tbuf_size)
365 tbuf_size = ALIGN(size, ubi->min_io_size);
366 tbuf = malloc(tbuf_size);
367 if (!tbuf) {
368 printf("NO MEM\n");
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100369 return ENOMEM;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100370 }
371 len = size > tbuf_size ? tbuf_size : size;
372
373 tmp = offp;
374 off = do_div(tmp, vol->usable_leb_size);
375 lnum = tmp;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100376 do {
377 if (off + len >= vol->usable_leb_size)
378 len = vol->usable_leb_size - off;
379
380 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
381 if (err) {
382 printf("read err %x\n", err);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100383 err = -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100384 break;
385 }
386 off += len;
387 if (off == vol->usable_leb_size) {
388 lnum += 1;
389 off -= vol->usable_leb_size;
390 }
391
392 size -= len;
393 offp += len;
394
Kyungmin Park694a0b32008-11-19 11:47:05 +0100395 memcpy(buf, tbuf, len);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100396
397 buf += len;
398 len = size > tbuf_size ? tbuf_size : size;
399 } while (size);
400
401 free(tbuf);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100402 return err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100403}
404
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200405static int ubi_dev_scan(struct mtd_info *info, char *ubidev,
406 const char *vid_header_offset)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100407{
408 struct mtd_device *dev;
409 struct part_info *part;
410 struct mtd_partition mtd_part;
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200411 char ubi_mtd_param_buffer[80];
Kyungmin Park694a0b32008-11-19 11:47:05 +0100412 u8 pnum;
413 int err;
414
Kyungmin Park694a0b32008-11-19 11:47:05 +0100415 if (find_dev_and_part(ubidev, &dev, &pnum, &part) != 0)
416 return 1;
417
418 sprintf(buffer, "mtd=%d", pnum);
419 memset(&mtd_part, 0, sizeof(mtd_part));
420 mtd_part.name = buffer;
421 mtd_part.size = part->size;
422 mtd_part.offset = part->offset;
423 add_mtd_partitions(info, &mtd_part, 1);
424
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200425 strcpy(ubi_mtd_param_buffer, buffer);
426 if (vid_header_offset)
427 sprintf(ubi_mtd_param_buffer, "mtd=%d,%s", pnum,
428 vid_header_offset);
429 err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100430 if (err) {
431 del_mtd_partitions(info);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100432 return -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100433 }
434
435 err = ubi_init();
436 if (err) {
437 del_mtd_partitions(info);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100438 return -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100439 }
440
Stefan Roese2ee951b2008-11-27 14:07:09 +0100441 ubi_initialized = 1;
442
Kyungmin Park694a0b32008-11-19 11:47:05 +0100443 return 0;
444}
445
Joe Hershberger71829062013-04-08 10:32:47 +0000446int ubi_part(char *part_name, const char *vid_header_offset)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100447{
Kyungmin Park694a0b32008-11-19 11:47:05 +0100448 int err = 0;
Joe Hershberger71829062013-04-08 10:32:47 +0000449 char mtd_dev[16];
450 struct mtd_device *dev;
451 struct part_info *part;
452 u8 pnum;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100453
Andreas Huberc203ef52009-04-02 17:15:34 +0200454 if (mtdparts_init() != 0) {
455 printf("Error initializing mtdparts!\n");
456 return 1;
457 }
458
Joe Hershberger71829062013-04-08 10:32:47 +0000459#ifdef CONFIG_CMD_UBIFS
460 /*
461 * Automatically unmount UBIFS partition when user
462 * changes the UBI device. Otherwise the following
463 * UBIFS commands will crash.
464 */
465 if (ubifs_is_mounted())
466 cmd_ubifs_umount();
467#endif
468
469 /* todo: get dev number for NAND... */
470 ubi_dev.nr = 0;
471
472 /*
473 * Call ubi_exit() before re-initializing the UBI subsystem
474 */
475 if (ubi_initialized) {
476 ubi_exit();
477 del_mtd_partitions(ubi_dev.mtd_info);
478 }
479
480 /*
481 * Search the mtd device number where this partition
482 * is located
483 */
484 if (find_dev_and_part(part_name, &dev, &pnum, &part)) {
485 printf("Partition %s not found!\n", part_name);
486 return 1;
487 }
488 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(dev->id->type), dev->id->num);
489 ubi_dev.mtd_info = get_mtd_device_nm(mtd_dev);
490 if (IS_ERR(ubi_dev.mtd_info)) {
491 printf("Partition %s not found on device %s!\n", part_name,
492 mtd_dev);
493 return 1;
494 }
495
496 ubi_dev.selected = 1;
497
498 strcpy(ubi_dev.part_name, part_name);
499 err = ubi_dev_scan(ubi_dev.mtd_info, ubi_dev.part_name,
500 vid_header_offset);
501 if (err) {
502 printf("UBI init error %d\n", err);
503 ubi_dev.selected = 0;
504 return err;
505 }
506
507 ubi = ubi_devices[0];
508
509 return 0;
510}
511
512static int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
513{
Paul Burtondd7185f2013-09-04 15:16:58 +0100514 int64_t size = 0;
Joe Hershberger71829062013-04-08 10:32:47 +0000515 ulong addr = 0;
516
517 if (argc < 2)
518 return CMD_RET_USAGE;
519
Kyungmin Park694a0b32008-11-19 11:47:05 +0100520 if (strcmp(argv[1], "part") == 0) {
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200521 const char *vid_header_offset = NULL;
Stefan Roese2d579e52009-04-24 20:24:19 +0200522
Kyungmin Park694a0b32008-11-19 11:47:05 +0100523 /* Print current partition */
524 if (argc == 2) {
Stefan Roese2d579e52009-04-24 20:24:19 +0200525 if (!ubi_dev.selected) {
Kyungmin Park694a0b32008-11-19 11:47:05 +0100526 printf("Error, no UBI device/partition selected!\n");
527 return 1;
528 }
529
Stefan Roese2d579e52009-04-24 20:24:19 +0200530 printf("Device %d: %s, partition %s\n",
Kyungmin Park694a0b32008-11-19 11:47:05 +0100531 ubi_dev.nr, ubi_dev.mtd_info->name, ubi_dev.part_name);
532 return 0;
533 }
534
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200535 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000536 return CMD_RET_USAGE;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100537
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200538 if (argc > 3)
539 vid_header_offset = argv[3];
Kyungmin Park694a0b32008-11-19 11:47:05 +0100540
Joe Hershberger71829062013-04-08 10:32:47 +0000541 return ubi_part(argv[2], vid_header_offset);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100542 }
543
Stefan Roese2d579e52009-04-24 20:24:19 +0200544 if ((strcmp(argv[1], "part") != 0) && (!ubi_dev.selected)) {
Kyungmin Park694a0b32008-11-19 11:47:05 +0100545 printf("Error, no UBI device/partition selected!\n");
546 return 1;
547 }
548
549 if (strcmp(argv[1], "info") == 0) {
550 int layout = 0;
551 if (argc > 2 && !strncmp(argv[2], "l", 1))
552 layout = 1;
553 return ubi_info(layout);
554 }
555
Heiko Schocherf9f4d802014-01-25 07:27:11 +0100556 if (strcmp(argv[1], "check") == 0) {
557 if (argc > 2)
558 return ubi_check(argv[2]);
559
560 printf("Error, no volume name passed\n");
561 return 1;
562 }
563
Kyungmin Park694a0b32008-11-19 11:47:05 +0100564 if (strncmp(argv[1], "create", 6) == 0) {
565 int dynamic = 1; /* default: dynamic volume */
566
567 /* Use maximum available size */
568 size = 0;
569
570 /* E.g., create volume size type */
571 if (argc == 5) {
572 if (strncmp(argv[4], "s", 1) == 0)
573 dynamic = 0;
574 else if (strncmp(argv[4], "d", 1) != 0) {
575 printf("Incorrect type\n");
576 return 1;
577 }
578 argc--;
579 }
580 /* E.g., create volume size */
581 if (argc == 4) {
Paul Burtondd7185f2013-09-04 15:16:58 +0100582 size = simple_strtoull(argv[3], NULL, 16);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100583 argc--;
584 }
585 /* Use maximum available size */
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100586 if (!size) {
Paul Burtondd7185f2013-09-04 15:16:58 +0100587 size = (int64_t)ubi->avail_pebs * ubi->leb_size;
588 printf("No size specified -> Using max size (%lld)\n", size);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100589 }
Kyungmin Park694a0b32008-11-19 11:47:05 +0100590 /* E.g., create volume */
591 if (argc == 3)
592 return ubi_create_vol(argv[2], size, dynamic);
593 }
594
595 if (strncmp(argv[1], "remove", 6) == 0) {
596 /* E.g., remove volume */
597 if (argc == 3)
598 return ubi_remove_vol(argv[2]);
599 }
600
601 if (strncmp(argv[1], "write", 5) == 0) {
Joe Hershberger71829062013-04-08 10:32:47 +0000602 int ret;
603
Kyungmin Park694a0b32008-11-19 11:47:05 +0100604 if (argc < 5) {
605 printf("Please see usage\n");
606 return 1;
607 }
608
609 addr = simple_strtoul(argv[2], NULL, 16);
Stefan Roesea5c40672008-11-24 08:31:16 +0100610 size = simple_strtoul(argv[4], NULL, 16);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100611
Paul Burtoncc734f52013-09-04 15:16:59 +0100612 if (strlen(argv[1]) == 10 &&
613 strncmp(argv[1] + 5, ".part", 5) == 0) {
614 if (argc < 6) {
615 ret = ubi_volume_continue_write(argv[3],
616 (void *)addr, size);
617 } else {
618 size_t full_size;
619 full_size = simple_strtoul(argv[5], NULL, 16);
620 ret = ubi_volume_begin_write(argv[3],
621 (void *)addr, size, full_size);
622 }
623 } else {
624 ret = ubi_volume_write(argv[3], (void *)addr, size);
625 }
Joe Hershberger71829062013-04-08 10:32:47 +0000626 if (!ret) {
Paul Burtondd7185f2013-09-04 15:16:58 +0100627 printf("%lld bytes written to volume %s\n", size,
Joe Hershberger71829062013-04-08 10:32:47 +0000628 argv[3]);
629 }
630
631 return ret;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100632 }
633
634 if (strncmp(argv[1], "read", 4) == 0) {
635 size = 0;
636
637 /* E.g., read volume size */
638 if (argc == 5) {
Stefan Roesea5c40672008-11-24 08:31:16 +0100639 size = simple_strtoul(argv[4], NULL, 16);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100640 argc--;
641 }
642
643 /* E.g., read volume */
644 if (argc == 4) {
645 addr = simple_strtoul(argv[2], NULL, 16);
646 argc--;
647 }
648
Joe Hershberger71829062013-04-08 10:32:47 +0000649 if (argc == 3) {
Paul Burtondd7185f2013-09-04 15:16:58 +0100650 printf("Read %lld bytes from volume %s to %lx\n", size,
Joe Hershberger71829062013-04-08 10:32:47 +0000651 argv[3], addr);
652
Kyungmin Park694a0b32008-11-19 11:47:05 +0100653 return ubi_volume_read(argv[3], (char *)addr, size);
Joe Hershberger71829062013-04-08 10:32:47 +0000654 }
Kyungmin Park694a0b32008-11-19 11:47:05 +0100655 }
656
657 printf("Please see usage\n");
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100658 return 1;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100659}
660
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200661U_BOOT_CMD(
662 ubi, 6, 1, do_ubi,
Peter Tyser2fb26042009-01-27 18:03:12 -0600663 "ubi commands",
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200664 "part [part] [offset]\n"
665 " - Show or set current partition (with optional VID"
666 " header offset)\n"
Kyungmin Park694a0b32008-11-19 11:47:05 +0100667 "ubi info [l[ayout]]"
668 " - Display volume and ubi layout information\n"
Heiko Schocherf9f4d802014-01-25 07:27:11 +0100669 "ubi check volumename"
670 " - check if volumename exists\n"
Kyungmin Park694a0b32008-11-19 11:47:05 +0100671 "ubi create[vol] volume [size] [type]"
672 " - create volume name with size\n"
673 "ubi write[vol] address volume size"
674 " - Write volume from address with size\n"
Paul Burtoncc734f52013-09-04 15:16:59 +0100675 "ubi write.part address volume size [fullsize]\n"
676 " - Write part of a volume from address\n"
Kyungmin Park694a0b32008-11-19 11:47:05 +0100677 "ubi read[vol] address volume [size]"
678 " - Read volume to address with size\n"
679 "ubi remove[vol] volume"
680 " - Remove volume\n"
681 "[Legends]\n"
Andrzej Wolskif6ca3b72009-07-17 22:26:54 +0200682 " volume: character name\n"
683 " size: specified in bytes\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200684 " type: s[tatic] or d[ynamic] (default=dynamic)"
Kyungmin Park694a0b32008-11-19 11:47:05 +0100685);