blob: 41fbae734ba007670f70d81e8099f50f7982ae2f [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>
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
Piotr Ziecik25ea6522008-11-17 15:58:00 +010029#define DEV_TYPE_NOR 3
Kyungmin Park694a0b32008-11-19 11:47:05 +010030
31/* Private own data */
32static struct ubi_device *ubi;
Stefan Roesea5c40672008-11-24 08:31:16 +010033static char buffer[80];
Stefan Roese2ee951b2008-11-27 14:07:09 +010034static int ubi_initialized;
Kyungmin Park694a0b32008-11-19 11:47:05 +010035
36struct selected_dev {
Kyungmin Park694a0b32008-11-19 11:47:05 +010037 char part_name[80];
Stefan Roese2d579e52009-04-24 20:24:19 +020038 int selected;
Kyungmin Park694a0b32008-11-19 11:47:05 +010039 int nr;
40 struct mtd_info *mtd_info;
41};
42
43static struct selected_dev ubi_dev;
44
Stefan Roese2f15cfd2010-11-01 17:28:22 +010045#ifdef CONFIG_CMD_UBIFS
46int ubifs_is_mounted(void);
47void cmd_ubifs_umount(void);
48#endif
49
Kyungmin Park694a0b32008-11-19 11:47:05 +010050static 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
77static 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
88static 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
113static 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
Kyungmin Park694a0b32008-11-19 11:47:05 +0100123static int verify_mkvol_req(const struct ubi_device *ubi,
124 const struct ubi_mkvol_req *req)
125{
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100126 int n, err = EINVAL;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100127
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
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100139 if (req->bytes == 0) {
140 printf("No space left in UBI device!\n");
141 err = ENOMEM;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100142 goto bad;
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100143 }
Kyungmin Park694a0b32008-11-19 11:47:05 +0100144
145 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
146 req->vol_type != UBI_STATIC_VOLUME)
147 goto bad;
148
149 if (req->alignment > ubi->leb_size)
150 goto bad;
151
152 n = req->alignment % ubi->min_io_size;
153 if (req->alignment != 1 && n)
154 goto bad;
155
156 if (req->name_len > UBI_VOL_NAME_MAX) {
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100157 printf("Name too long!\n");
158 err = ENAMETOOLONG;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100159 goto bad;
160 }
161
162 return 0;
163bad:
Kyungmin Park694a0b32008-11-19 11:47:05 +0100164 return err;
165}
166
167static int ubi_create_vol(char *volume, int size, int dynamic)
168{
169 struct ubi_mkvol_req req;
170 int err;
171
172 if (dynamic)
173 req.vol_type = UBI_DYNAMIC_VOLUME;
174 else
175 req.vol_type = UBI_STATIC_VOLUME;
176
177 req.vol_id = UBI_VOL_NUM_AUTO;
178 req.alignment = 1;
179 req.bytes = size;
180
181 strcpy(req.name, volume);
182 req.name_len = strlen(volume);
183 req.name[req.name_len] = '\0';
184 req.padding1 = 0;
185 /* It's duplicated at drivers/mtd/ubi/cdev.c */
186 err = verify_mkvol_req(ubi, &req);
187 if (err) {
188 printf("verify_mkvol_req failed %d\n", err);
189 return err;
190 }
191 printf("Creating %s volume %s of size %d\n",
192 dynamic ? "dynamic" : "static", volume, size);
193 /* Call real ubi create volume */
194 return ubi_create_volume(ubi, &req);
195}
196
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100197static struct ubi_volume *ubi_find_volume(char *volume)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100198{
Peter Tyser3b653fd2010-04-04 22:40:50 -0500199 struct ubi_volume *vol = NULL;
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100200 int i;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100201
202 for (i = 0; i < ubi->vtbl_slots; i++) {
203 vol = ubi->volumes[i];
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100204 if (vol && !strcmp(vol->name, volume))
205 return vol;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100206 }
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100207
208 printf("Volume %s not found!\n", volume);
209 return NULL;
210}
211
212static int ubi_remove_vol(char *volume)
213{
214 int err, reserved_pebs, i;
215 struct ubi_volume *vol;
216
217 vol = ubi_find_volume(volume);
218 if (vol == NULL)
219 return ENODEV;
220
221 printf("Remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100222
223 if (ubi->ro_mode) {
224 printf("It's read-only mode\n");
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100225 err = EROFS;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100226 goto out_err;
227 }
228
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100229 err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100230 if (err) {
231 printf("Error changing Vol tabel record err=%x\n", err);
232 goto out_err;
233 }
234 reserved_pebs = vol->reserved_pebs;
235 for (i = 0; i < vol->reserved_pebs; i++) {
236 err = ubi_eba_unmap_leb(ubi, vol, i);
237 if (err)
238 goto out_err;
239 }
240
241 kfree(vol->eba_tbl);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100242 ubi->volumes[vol->vol_id]->eba_tbl = NULL;
243 ubi->volumes[vol->vol_id] = NULL;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100244
245 ubi->rsvd_pebs -= reserved_pebs;
246 ubi->avail_pebs += reserved_pebs;
247 i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
248 if (i > 0) {
249 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
250 ubi->avail_pebs -= i;
251 ubi->rsvd_pebs += i;
252 ubi->beb_rsvd_pebs += i;
253 if (i > 0)
254 ubi_msg("reserve more %d PEBs", i);
255 }
256 ubi->vol_count -= 1;
257
258 return 0;
259out_err:
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100260 ubi_err("cannot remove volume %s, error %d", volume, err);
261 if (err < 0)
262 err = -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100263 return err;
264}
265
Joe Hershberger71829062013-04-08 10:32:47 +0000266int ubi_volume_write(char *volume, void *buf, size_t size)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100267{
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100268 int err = 1;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100269 int rsvd_bytes = 0;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100270 struct ubi_volume *vol;
271
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100272 vol = ubi_find_volume(volume);
273 if (vol == NULL)
274 return ENODEV;
275
Kyungmin Park694a0b32008-11-19 11:47:05 +0100276 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
277 if (size < 0 || size > rsvd_bytes) {
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100278 printf("size > volume size! Aborting!\n");
279 return EINVAL;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100280 }
281
282 err = ubi_start_update(ubi, vol, size);
283 if (err < 0) {
284 printf("Cannot start volume update\n");
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100285 return -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100286 }
287
288 err = ubi_more_update_data(ubi, vol, buf, size);
289 if (err < 0) {
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100290 printf("Couldnt or partially wrote data\n");
291 return -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100292 }
293
294 if (err) {
295 size = err;
296
297 err = ubi_check_volume(ubi, vol->vol_id);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100298 if (err < 0)
299 return -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100300
301 if (err) {
302 ubi_warn("volume %d on UBI device %d is corrupted",
303 vol->vol_id, ubi->ubi_num);
304 vol->corrupted = 1;
305 }
306
307 vol->checked = 1;
308 ubi_gluebi_updated(vol);
309 }
310
311 return 0;
312}
313
Joe Hershberger71829062013-04-08 10:32:47 +0000314int ubi_volume_read(char *volume, char *buf, size_t size)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100315{
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100316 int err, lnum, off, len, tbuf_size;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100317 void *tbuf;
318 unsigned long long tmp;
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100319 struct ubi_volume *vol;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100320 loff_t offp = 0;
321
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100322 vol = ubi_find_volume(volume);
323 if (vol == NULL)
324 return ENODEV;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100325
Kyungmin Park694a0b32008-11-19 11:47:05 +0100326 if (vol->updating) {
327 printf("updating");
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100328 return EBUSY;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100329 }
330 if (vol->upd_marker) {
331 printf("damaged volume, update marker is set");
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100332 return EBADF;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100333 }
334 if (offp == vol->used_bytes)
335 return 0;
336
337 if (size == 0) {
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100338 printf("No size specified -> Using max size (%lld)\n", vol->used_bytes);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100339 size = vol->used_bytes;
340 }
341
342 if (vol->corrupted)
343 printf("read from corrupted volume %d", vol->vol_id);
344 if (offp + size > vol->used_bytes)
Marek Vasut2984fd12011-09-30 12:13:25 +0200345 size = vol->used_bytes - offp;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100346
347 tbuf_size = vol->usable_leb_size;
348 if (size < tbuf_size)
349 tbuf_size = ALIGN(size, ubi->min_io_size);
350 tbuf = malloc(tbuf_size);
351 if (!tbuf) {
352 printf("NO MEM\n");
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100353 return ENOMEM;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100354 }
355 len = size > tbuf_size ? tbuf_size : size;
356
357 tmp = offp;
358 off = do_div(tmp, vol->usable_leb_size);
359 lnum = tmp;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100360 do {
361 if (off + len >= vol->usable_leb_size)
362 len = vol->usable_leb_size - off;
363
364 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
365 if (err) {
366 printf("read err %x\n", err);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100367 err = -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100368 break;
369 }
370 off += len;
371 if (off == vol->usable_leb_size) {
372 lnum += 1;
373 off -= vol->usable_leb_size;
374 }
375
376 size -= len;
377 offp += len;
378
Kyungmin Park694a0b32008-11-19 11:47:05 +0100379 memcpy(buf, tbuf, len);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100380
381 buf += len;
382 len = size > tbuf_size ? tbuf_size : size;
383 } while (size);
384
385 free(tbuf);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100386 return err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100387}
388
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200389static int ubi_dev_scan(struct mtd_info *info, char *ubidev,
390 const char *vid_header_offset)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100391{
392 struct mtd_device *dev;
393 struct part_info *part;
394 struct mtd_partition mtd_part;
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200395 char ubi_mtd_param_buffer[80];
Kyungmin Park694a0b32008-11-19 11:47:05 +0100396 u8 pnum;
397 int err;
398
Kyungmin Park694a0b32008-11-19 11:47:05 +0100399 if (find_dev_and_part(ubidev, &dev, &pnum, &part) != 0)
400 return 1;
401
402 sprintf(buffer, "mtd=%d", pnum);
403 memset(&mtd_part, 0, sizeof(mtd_part));
404 mtd_part.name = buffer;
405 mtd_part.size = part->size;
406 mtd_part.offset = part->offset;
407 add_mtd_partitions(info, &mtd_part, 1);
408
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200409 strcpy(ubi_mtd_param_buffer, buffer);
410 if (vid_header_offset)
411 sprintf(ubi_mtd_param_buffer, "mtd=%d,%s", pnum,
412 vid_header_offset);
413 err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100414 if (err) {
415 del_mtd_partitions(info);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100416 return -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100417 }
418
419 err = ubi_init();
420 if (err) {
421 del_mtd_partitions(info);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100422 return -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100423 }
424
Stefan Roese2ee951b2008-11-27 14:07:09 +0100425 ubi_initialized = 1;
426
Kyungmin Park694a0b32008-11-19 11:47:05 +0100427 return 0;
428}
429
Joe Hershberger71829062013-04-08 10:32:47 +0000430int ubi_part(char *part_name, const char *vid_header_offset)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100431{
Kyungmin Park694a0b32008-11-19 11:47:05 +0100432 int err = 0;
Joe Hershberger71829062013-04-08 10:32:47 +0000433 char mtd_dev[16];
434 struct mtd_device *dev;
435 struct part_info *part;
436 u8 pnum;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100437
Andreas Huberc203ef52009-04-02 17:15:34 +0200438 if (mtdparts_init() != 0) {
439 printf("Error initializing mtdparts!\n");
440 return 1;
441 }
442
Joe Hershberger71829062013-04-08 10:32:47 +0000443#ifdef CONFIG_CMD_UBIFS
444 /*
445 * Automatically unmount UBIFS partition when user
446 * changes the UBI device. Otherwise the following
447 * UBIFS commands will crash.
448 */
449 if (ubifs_is_mounted())
450 cmd_ubifs_umount();
451#endif
452
453 /* todo: get dev number for NAND... */
454 ubi_dev.nr = 0;
455
456 /*
457 * Call ubi_exit() before re-initializing the UBI subsystem
458 */
459 if (ubi_initialized) {
460 ubi_exit();
461 del_mtd_partitions(ubi_dev.mtd_info);
462 }
463
464 /*
465 * Search the mtd device number where this partition
466 * is located
467 */
468 if (find_dev_and_part(part_name, &dev, &pnum, &part)) {
469 printf("Partition %s not found!\n", part_name);
470 return 1;
471 }
472 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(dev->id->type), dev->id->num);
473 ubi_dev.mtd_info = get_mtd_device_nm(mtd_dev);
474 if (IS_ERR(ubi_dev.mtd_info)) {
475 printf("Partition %s not found on device %s!\n", part_name,
476 mtd_dev);
477 return 1;
478 }
479
480 ubi_dev.selected = 1;
481
482 strcpy(ubi_dev.part_name, part_name);
483 err = ubi_dev_scan(ubi_dev.mtd_info, ubi_dev.part_name,
484 vid_header_offset);
485 if (err) {
486 printf("UBI init error %d\n", err);
487 ubi_dev.selected = 0;
488 return err;
489 }
490
491 ubi = ubi_devices[0];
492
493 return 0;
494}
495
496static int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
497{
498 size_t size = 0;
499 ulong addr = 0;
500
501 if (argc < 2)
502 return CMD_RET_USAGE;
503
Kyungmin Park694a0b32008-11-19 11:47:05 +0100504 if (strcmp(argv[1], "part") == 0) {
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200505 const char *vid_header_offset = NULL;
Stefan Roese2d579e52009-04-24 20:24:19 +0200506
Kyungmin Park694a0b32008-11-19 11:47:05 +0100507 /* Print current partition */
508 if (argc == 2) {
Stefan Roese2d579e52009-04-24 20:24:19 +0200509 if (!ubi_dev.selected) {
Kyungmin Park694a0b32008-11-19 11:47:05 +0100510 printf("Error, no UBI device/partition selected!\n");
511 return 1;
512 }
513
Stefan Roese2d579e52009-04-24 20:24:19 +0200514 printf("Device %d: %s, partition %s\n",
Kyungmin Park694a0b32008-11-19 11:47:05 +0100515 ubi_dev.nr, ubi_dev.mtd_info->name, ubi_dev.part_name);
516 return 0;
517 }
518
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200519 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000520 return CMD_RET_USAGE;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100521
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200522 if (argc > 3)
523 vid_header_offset = argv[3];
Kyungmin Park694a0b32008-11-19 11:47:05 +0100524
Joe Hershberger71829062013-04-08 10:32:47 +0000525 return ubi_part(argv[2], vid_header_offset);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100526 }
527
Stefan Roese2d579e52009-04-24 20:24:19 +0200528 if ((strcmp(argv[1], "part") != 0) && (!ubi_dev.selected)) {
Kyungmin Park694a0b32008-11-19 11:47:05 +0100529 printf("Error, no UBI device/partition selected!\n");
530 return 1;
531 }
532
533 if (strcmp(argv[1], "info") == 0) {
534 int layout = 0;
535 if (argc > 2 && !strncmp(argv[2], "l", 1))
536 layout = 1;
537 return ubi_info(layout);
538 }
539
540 if (strncmp(argv[1], "create", 6) == 0) {
541 int dynamic = 1; /* default: dynamic volume */
542
543 /* Use maximum available size */
544 size = 0;
545
546 /* E.g., create volume size type */
547 if (argc == 5) {
548 if (strncmp(argv[4], "s", 1) == 0)
549 dynamic = 0;
550 else if (strncmp(argv[4], "d", 1) != 0) {
551 printf("Incorrect type\n");
552 return 1;
553 }
554 argc--;
555 }
556 /* E.g., create volume size */
557 if (argc == 4) {
Stefan Roese2d2e0572008-12-02 10:53:47 +0100558 size = simple_strtoul(argv[3], NULL, 16);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100559 argc--;
560 }
561 /* Use maximum available size */
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100562 if (!size) {
Kyungmin Park694a0b32008-11-19 11:47:05 +0100563 size = ubi->avail_pebs * ubi->leb_size;
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100564 printf("No size specified -> Using max size (%u)\n", size);
565 }
Kyungmin Park694a0b32008-11-19 11:47:05 +0100566 /* E.g., create volume */
567 if (argc == 3)
568 return ubi_create_vol(argv[2], size, dynamic);
569 }
570
571 if (strncmp(argv[1], "remove", 6) == 0) {
572 /* E.g., remove volume */
573 if (argc == 3)
574 return ubi_remove_vol(argv[2]);
575 }
576
577 if (strncmp(argv[1], "write", 5) == 0) {
Joe Hershberger71829062013-04-08 10:32:47 +0000578 int ret;
579
Kyungmin Park694a0b32008-11-19 11:47:05 +0100580 if (argc < 5) {
581 printf("Please see usage\n");
582 return 1;
583 }
584
585 addr = simple_strtoul(argv[2], NULL, 16);
Stefan Roesea5c40672008-11-24 08:31:16 +0100586 size = simple_strtoul(argv[4], NULL, 16);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100587
Joe Hershberger71829062013-04-08 10:32:47 +0000588 ret = ubi_volume_write(argv[3], (void *)addr, size);
589 if (!ret) {
590 printf("%d bytes written to volume %s\n", size,
591 argv[3]);
592 }
593
594 return ret;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100595 }
596
597 if (strncmp(argv[1], "read", 4) == 0) {
598 size = 0;
599
600 /* E.g., read volume size */
601 if (argc == 5) {
Stefan Roesea5c40672008-11-24 08:31:16 +0100602 size = simple_strtoul(argv[4], NULL, 16);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100603 argc--;
604 }
605
606 /* E.g., read volume */
607 if (argc == 4) {
608 addr = simple_strtoul(argv[2], NULL, 16);
609 argc--;
610 }
611
Joe Hershberger71829062013-04-08 10:32:47 +0000612 if (argc == 3) {
613 printf("Read %d bytes from volume %s to %lx\n", size,
614 argv[3], addr);
615
Kyungmin Park694a0b32008-11-19 11:47:05 +0100616 return ubi_volume_read(argv[3], (char *)addr, size);
Joe Hershberger71829062013-04-08 10:32:47 +0000617 }
Kyungmin Park694a0b32008-11-19 11:47:05 +0100618 }
619
620 printf("Please see usage\n");
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100621 return 1;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100622}
623
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200624U_BOOT_CMD(
625 ubi, 6, 1, do_ubi,
Peter Tyser2fb26042009-01-27 18:03:12 -0600626 "ubi commands",
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200627 "part [part] [offset]\n"
628 " - Show or set current partition (with optional VID"
629 " header offset)\n"
Kyungmin Park694a0b32008-11-19 11:47:05 +0100630 "ubi info [l[ayout]]"
631 " - Display volume and ubi layout information\n"
632 "ubi create[vol] volume [size] [type]"
633 " - create volume name with size\n"
634 "ubi write[vol] address volume size"
635 " - Write volume from address with size\n"
636 "ubi read[vol] address volume [size]"
637 " - Read volume to address with size\n"
638 "ubi remove[vol] volume"
639 " - Remove volume\n"
640 "[Legends]\n"
Andrzej Wolskif6ca3b72009-07-17 22:26:54 +0200641 " volume: character name\n"
642 " size: specified in bytes\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200643 " type: s[tatic] or d[ynamic] (default=dynamic)"
Kyungmin Park694a0b32008-11-19 11:47:05 +0100644);