blob: 5ba4feb485bd40faaa9a474588006abdd0e50eb5 [file] [log] [blame]
Kyungmin Park3d2c5ff2008-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 Roese62eafb72009-04-24 20:24:19 +02007 * Copyright 2008-2009 Stefan Roese <sr@denx.de>, DENX Software Engineering
Kyungmin Park3d2c5ff2008-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
Joe Hershberger47550fc2013-04-08 10:32:49 +000026#undef ubi_msg
27#define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__)
28
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +010029#define DEV_TYPE_NONE 0
30#define DEV_TYPE_NAND 1
31#define DEV_TYPE_ONENAND 2
Piotr Ziecik88bddfd2008-11-17 15:58:00 +010032#define DEV_TYPE_NOR 3
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +010033
34/* Private own data */
35static struct ubi_device *ubi;
Stefan Roese1b714052008-11-24 08:31:16 +010036static char buffer[80];
Stefan Roese1b20eb82008-11-27 14:07:09 +010037static int ubi_initialized;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +010038
39struct selected_dev {
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +010040 char part_name[80];
Stefan Roese62eafb72009-04-24 20:24:19 +020041 int selected;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +010042 int nr;
43 struct mtd_info *mtd_info;
44};
45
46static struct selected_dev ubi_dev;
47
Stefan Roese408c6c42010-11-01 17:28:22 +010048#ifdef CONFIG_CMD_UBIFS
49int ubifs_is_mounted(void);
50void cmd_ubifs_umount(void);
51#endif
52
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +010053static void ubi_dump_vol_info(const struct ubi_volume *vol)
54{
55 ubi_msg("volume information dump:");
56 ubi_msg("vol_id %d", vol->vol_id);
57 ubi_msg("reserved_pebs %d", vol->reserved_pebs);
58 ubi_msg("alignment %d", vol->alignment);
59 ubi_msg("data_pad %d", vol->data_pad);
60 ubi_msg("vol_type %d", vol->vol_type);
61 ubi_msg("name_len %d", vol->name_len);
62 ubi_msg("usable_leb_size %d", vol->usable_leb_size);
63 ubi_msg("used_ebs %d", vol->used_ebs);
64 ubi_msg("used_bytes %lld", vol->used_bytes);
65 ubi_msg("last_eb_bytes %d", vol->last_eb_bytes);
66 ubi_msg("corrupted %d", vol->corrupted);
67 ubi_msg("upd_marker %d", vol->upd_marker);
68
69 if (vol->name_len <= UBI_VOL_NAME_MAX &&
70 strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
71 ubi_msg("name %s", vol->name);
72 } else {
73 ubi_msg("the 1st 5 characters of the name: %c%c%c%c%c",
74 vol->name[0], vol->name[1], vol->name[2],
75 vol->name[3], vol->name[4]);
76 }
77 printf("\n");
78}
79
80static void display_volume_info(struct ubi_device *ubi)
81{
82 int i;
83
84 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
85 if (!ubi->volumes[i])
86 continue; /* Empty record */
87 ubi_dump_vol_info(ubi->volumes[i]);
88 }
89}
90
91static void display_ubi_info(struct ubi_device *ubi)
92{
93 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
94 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
95 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
96 ubi->peb_size, ubi->peb_size >> 10);
97 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
98 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
99 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
100 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
101 ubi_msg("VID header offset: %d (aligned %d)",
102 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
103 ubi_msg("data offset: %d", ubi->leb_start);
104 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
105 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
106 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
107 ubi_msg("number of user volumes: %d",
108 ubi->vol_count - UBI_INT_VOL_COUNT);
109 ubi_msg("available PEBs: %d", ubi->avail_pebs);
110 ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
111 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
112 ubi->beb_rsvd_pebs);
113 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
114}
115
116static int ubi_info(int layout)
117{
118 if (layout)
119 display_volume_info(ubi);
120 else
121 display_ubi_info(ubi);
122
123 return 0;
124}
125
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100126static int verify_mkvol_req(const struct ubi_device *ubi,
127 const struct ubi_mkvol_req *req)
128{
Stefan Roese8cfc6962011-03-14 14:34:21 +0100129 int n, err = EINVAL;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100130
131 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
132 req->name_len < 0)
133 goto bad;
134
135 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
136 req->vol_id != UBI_VOL_NUM_AUTO)
137 goto bad;
138
139 if (req->alignment == 0)
140 goto bad;
141
Stefan Roese8cfc6962011-03-14 14:34:21 +0100142 if (req->bytes == 0) {
143 printf("No space left in UBI device!\n");
144 err = ENOMEM;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100145 goto bad;
Stefan Roese8cfc6962011-03-14 14:34:21 +0100146 }
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100147
148 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
149 req->vol_type != UBI_STATIC_VOLUME)
150 goto bad;
151
152 if (req->alignment > ubi->leb_size)
153 goto bad;
154
155 n = req->alignment % ubi->min_io_size;
156 if (req->alignment != 1 && n)
157 goto bad;
158
159 if (req->name_len > UBI_VOL_NAME_MAX) {
Stefan Roese8cfc6962011-03-14 14:34:21 +0100160 printf("Name too long!\n");
161 err = ENAMETOOLONG;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100162 goto bad;
163 }
164
165 return 0;
166bad:
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100167 return err;
168}
169
170static int ubi_create_vol(char *volume, int size, int dynamic)
171{
172 struct ubi_mkvol_req req;
173 int err;
174
175 if (dynamic)
176 req.vol_type = UBI_DYNAMIC_VOLUME;
177 else
178 req.vol_type = UBI_STATIC_VOLUME;
179
180 req.vol_id = UBI_VOL_NUM_AUTO;
181 req.alignment = 1;
182 req.bytes = size;
183
184 strcpy(req.name, volume);
185 req.name_len = strlen(volume);
186 req.name[req.name_len] = '\0';
187 req.padding1 = 0;
188 /* It's duplicated at drivers/mtd/ubi/cdev.c */
189 err = verify_mkvol_req(ubi, &req);
190 if (err) {
191 printf("verify_mkvol_req failed %d\n", err);
192 return err;
193 }
194 printf("Creating %s volume %s of size %d\n",
195 dynamic ? "dynamic" : "static", volume, size);
196 /* Call real ubi create volume */
197 return ubi_create_volume(ubi, &req);
198}
199
Stefan Roese8cfc6962011-03-14 14:34:21 +0100200static struct ubi_volume *ubi_find_volume(char *volume)
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100201{
Peter Tyserf2dbedc2010-04-04 22:40:50 -0500202 struct ubi_volume *vol = NULL;
Stefan Roese8cfc6962011-03-14 14:34:21 +0100203 int i;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100204
205 for (i = 0; i < ubi->vtbl_slots; i++) {
206 vol = ubi->volumes[i];
Stefan Roese8cfc6962011-03-14 14:34:21 +0100207 if (vol && !strcmp(vol->name, volume))
208 return vol;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100209 }
Stefan Roese8cfc6962011-03-14 14:34:21 +0100210
211 printf("Volume %s not found!\n", volume);
212 return NULL;
213}
214
215static int ubi_remove_vol(char *volume)
216{
217 int err, reserved_pebs, i;
218 struct ubi_volume *vol;
219
220 vol = ubi_find_volume(volume);
221 if (vol == NULL)
222 return ENODEV;
223
224 printf("Remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100225
226 if (ubi->ro_mode) {
227 printf("It's read-only mode\n");
Stefan Roese8cfc6962011-03-14 14:34:21 +0100228 err = EROFS;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100229 goto out_err;
230 }
231
Stefan Roese8cfc6962011-03-14 14:34:21 +0100232 err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL);
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100233 if (err) {
234 printf("Error changing Vol tabel record err=%x\n", err);
235 goto out_err;
236 }
237 reserved_pebs = vol->reserved_pebs;
238 for (i = 0; i < vol->reserved_pebs; i++) {
239 err = ubi_eba_unmap_leb(ubi, vol, i);
240 if (err)
241 goto out_err;
242 }
243
244 kfree(vol->eba_tbl);
Stefan Roese8cfc6962011-03-14 14:34:21 +0100245 ubi->volumes[vol->vol_id]->eba_tbl = NULL;
246 ubi->volumes[vol->vol_id] = NULL;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100247
248 ubi->rsvd_pebs -= reserved_pebs;
249 ubi->avail_pebs += reserved_pebs;
250 i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
251 if (i > 0) {
252 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
253 ubi->avail_pebs -= i;
254 ubi->rsvd_pebs += i;
255 ubi->beb_rsvd_pebs += i;
256 if (i > 0)
257 ubi_msg("reserve more %d PEBs", i);
258 }
259 ubi->vol_count -= 1;
260
261 return 0;
262out_err:
Stefan Roese8cfc6962011-03-14 14:34:21 +0100263 ubi_err("cannot remove volume %s, error %d", volume, err);
264 if (err < 0)
265 err = -err;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100266 return err;
267}
268
Joe Hershberger92198452013-04-08 10:32:47 +0000269int ubi_volume_write(char *volume, void *buf, size_t size)
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100270{
Stefan Roese8cfc6962011-03-14 14:34:21 +0100271 int err = 1;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100272 int rsvd_bytes = 0;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100273 struct ubi_volume *vol;
274
Stefan Roese8cfc6962011-03-14 14:34:21 +0100275 vol = ubi_find_volume(volume);
276 if (vol == NULL)
277 return ENODEV;
278
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100279 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
280 if (size < 0 || size > rsvd_bytes) {
Stefan Roese8cfc6962011-03-14 14:34:21 +0100281 printf("size > volume size! Aborting!\n");
282 return EINVAL;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100283 }
284
285 err = ubi_start_update(ubi, vol, size);
286 if (err < 0) {
287 printf("Cannot start volume update\n");
Stefan Roese8cfc6962011-03-14 14:34:21 +0100288 return -err;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100289 }
290
291 err = ubi_more_update_data(ubi, vol, buf, size);
292 if (err < 0) {
Stefan Roese8cfc6962011-03-14 14:34:21 +0100293 printf("Couldnt or partially wrote data\n");
294 return -err;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100295 }
296
297 if (err) {
298 size = err;
299
300 err = ubi_check_volume(ubi, vol->vol_id);
Stefan Roese8cfc6962011-03-14 14:34:21 +0100301 if (err < 0)
302 return -err;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100303
304 if (err) {
305 ubi_warn("volume %d on UBI device %d is corrupted",
306 vol->vol_id, ubi->ubi_num);
307 vol->corrupted = 1;
308 }
309
310 vol->checked = 1;
311 ubi_gluebi_updated(vol);
312 }
313
314 return 0;
315}
316
Joe Hershberger92198452013-04-08 10:32:47 +0000317int ubi_volume_read(char *volume, char *buf, size_t size)
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100318{
Stefan Roese8cfc6962011-03-14 14:34:21 +0100319 int err, lnum, off, len, tbuf_size;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100320 void *tbuf;
321 unsigned long long tmp;
Stefan Roese8cfc6962011-03-14 14:34:21 +0100322 struct ubi_volume *vol;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100323 loff_t offp = 0;
324
Stefan Roese8cfc6962011-03-14 14:34:21 +0100325 vol = ubi_find_volume(volume);
326 if (vol == NULL)
327 return ENODEV;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100328
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100329 if (vol->updating) {
330 printf("updating");
Stefan Roese8cfc6962011-03-14 14:34:21 +0100331 return EBUSY;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100332 }
333 if (vol->upd_marker) {
334 printf("damaged volume, update marker is set");
Stefan Roese8cfc6962011-03-14 14:34:21 +0100335 return EBADF;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100336 }
337 if (offp == vol->used_bytes)
338 return 0;
339
340 if (size == 0) {
Stefan Roese8cfc6962011-03-14 14:34:21 +0100341 printf("No size specified -> Using max size (%lld)\n", vol->used_bytes);
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100342 size = vol->used_bytes;
343 }
344
345 if (vol->corrupted)
346 printf("read from corrupted volume %d", vol->vol_id);
347 if (offp + size > vol->used_bytes)
Marek Vasut40c0c8b2011-09-30 12:13:25 +0200348 size = vol->used_bytes - offp;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100349
350 tbuf_size = vol->usable_leb_size;
351 if (size < tbuf_size)
352 tbuf_size = ALIGN(size, ubi->min_io_size);
353 tbuf = malloc(tbuf_size);
354 if (!tbuf) {
355 printf("NO MEM\n");
Stefan Roese8cfc6962011-03-14 14:34:21 +0100356 return ENOMEM;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100357 }
358 len = size > tbuf_size ? tbuf_size : size;
359
360 tmp = offp;
361 off = do_div(tmp, vol->usable_leb_size);
362 lnum = tmp;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100363 do {
364 if (off + len >= vol->usable_leb_size)
365 len = vol->usable_leb_size - off;
366
367 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
368 if (err) {
369 printf("read err %x\n", err);
Stefan Roese8cfc6962011-03-14 14:34:21 +0100370 err = -err;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100371 break;
372 }
373 off += len;
374 if (off == vol->usable_leb_size) {
375 lnum += 1;
376 off -= vol->usable_leb_size;
377 }
378
379 size -= len;
380 offp += len;
381
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100382 memcpy(buf, tbuf, len);
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100383
384 buf += len;
385 len = size > tbuf_size ? tbuf_size : size;
386 } while (size);
387
388 free(tbuf);
Stefan Roese8cfc6962011-03-14 14:34:21 +0100389 return err;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100390}
391
Simon Kagstrom4a8fbfb2009-07-07 16:59:46 +0200392static int ubi_dev_scan(struct mtd_info *info, char *ubidev,
393 const char *vid_header_offset)
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100394{
395 struct mtd_device *dev;
396 struct part_info *part;
397 struct mtd_partition mtd_part;
Simon Kagstrom4a8fbfb2009-07-07 16:59:46 +0200398 char ubi_mtd_param_buffer[80];
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100399 u8 pnum;
400 int err;
401
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100402 if (find_dev_and_part(ubidev, &dev, &pnum, &part) != 0)
403 return 1;
404
405 sprintf(buffer, "mtd=%d", pnum);
406 memset(&mtd_part, 0, sizeof(mtd_part));
407 mtd_part.name = buffer;
408 mtd_part.size = part->size;
409 mtd_part.offset = part->offset;
410 add_mtd_partitions(info, &mtd_part, 1);
411
Simon Kagstrom4a8fbfb2009-07-07 16:59:46 +0200412 strcpy(ubi_mtd_param_buffer, buffer);
413 if (vid_header_offset)
414 sprintf(ubi_mtd_param_buffer, "mtd=%d,%s", pnum,
415 vid_header_offset);
416 err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100417 if (err) {
418 del_mtd_partitions(info);
Stefan Roese8cfc6962011-03-14 14:34:21 +0100419 return -err;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100420 }
421
422 err = ubi_init();
423 if (err) {
424 del_mtd_partitions(info);
Stefan Roese8cfc6962011-03-14 14:34:21 +0100425 return -err;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100426 }
427
Stefan Roese1b20eb82008-11-27 14:07:09 +0100428 ubi_initialized = 1;
429
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100430 return 0;
431}
432
Joe Hershberger92198452013-04-08 10:32:47 +0000433int ubi_part(char *part_name, const char *vid_header_offset)
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100434{
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100435 int err = 0;
Joe Hershberger92198452013-04-08 10:32:47 +0000436 char mtd_dev[16];
437 struct mtd_device *dev;
438 struct part_info *part;
439 u8 pnum;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100440
Andreas Huber7bf15f82009-04-02 17:15:34 +0200441 if (mtdparts_init() != 0) {
442 printf("Error initializing mtdparts!\n");
443 return 1;
444 }
Joe Hershberger92198452013-04-08 10:32:47 +0000445
446#ifdef CONFIG_CMD_UBIFS
447 /*
448 * Automatically unmount UBIFS partition when user
449 * changes the UBI device. Otherwise the following
450 * UBIFS commands will crash.
451 */
452 if (ubifs_is_mounted())
453 cmd_ubifs_umount();
454#endif
455
456 /* todo: get dev number for NAND... */
457 ubi_dev.nr = 0;
458
459 /*
460 * Call ubi_exit() before re-initializing the UBI subsystem
461 */
462 if (ubi_initialized) {
463 ubi_exit();
464 del_mtd_partitions(ubi_dev.mtd_info);
465 }
466
467 /*
468 * Search the mtd device number where this partition
469 * is located
470 */
471 if (find_dev_and_part(part_name, &dev, &pnum, &part)) {
472 printf("Partition %s not found!\n", part_name);
473 return 1;
474 }
475 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(dev->id->type), dev->id->num);
476 ubi_dev.mtd_info = get_mtd_device_nm(mtd_dev);
477 if (IS_ERR(ubi_dev.mtd_info)) {
478 printf("Partition %s not found on device %s!\n", part_name,
479 mtd_dev);
480 return 1;
481 }
482
483 ubi_dev.selected = 1;
484
485 strcpy(ubi_dev.part_name, part_name);
486 err = ubi_dev_scan(ubi_dev.mtd_info, ubi_dev.part_name,
487 vid_header_offset);
488 if (err) {
489 printf("UBI init error %d\n", err);
490 ubi_dev.selected = 0;
491 return err;
492 }
493
494 ubi = ubi_devices[0];
495
496 return 0;
497}
498
499static int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
500{
501 size_t size = 0;
502 ulong addr = 0;
503
504 if (argc < 2)
505 return CMD_RET_USAGE;
Andreas Huber7bf15f82009-04-02 17:15:34 +0200506
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100507 if (strcmp(argv[1], "part") == 0) {
Simon Kagstrom4a8fbfb2009-07-07 16:59:46 +0200508 const char *vid_header_offset = NULL;
Stefan Roese62eafb72009-04-24 20:24:19 +0200509
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100510 /* Print current partition */
511 if (argc == 2) {
Stefan Roese62eafb72009-04-24 20:24:19 +0200512 if (!ubi_dev.selected) {
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100513 printf("Error, no UBI device/partition selected!\n");
514 return 1;
515 }
516
Stefan Roese62eafb72009-04-24 20:24:19 +0200517 printf("Device %d: %s, partition %s\n",
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100518 ubi_dev.nr, ubi_dev.mtd_info->name, ubi_dev.part_name);
519 return 0;
520 }
521
Wolfgang Denk3b683112010-07-17 01:06:04 +0200522 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000523 return CMD_RET_USAGE;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100524
Simon Kagstrom4a8fbfb2009-07-07 16:59:46 +0200525 if (argc > 3)
526 vid_header_offset = argv[3];
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100527
Joe Hershberger92198452013-04-08 10:32:47 +0000528 return ubi_part(argv[2], vid_header_offset);
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100529 }
530
Stefan Roese62eafb72009-04-24 20:24:19 +0200531 if ((strcmp(argv[1], "part") != 0) && (!ubi_dev.selected)) {
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100532 printf("Error, no UBI device/partition selected!\n");
533 return 1;
534 }
535
536 if (strcmp(argv[1], "info") == 0) {
537 int layout = 0;
538 if (argc > 2 && !strncmp(argv[2], "l", 1))
539 layout = 1;
540 return ubi_info(layout);
541 }
542
543 if (strncmp(argv[1], "create", 6) == 0) {
544 int dynamic = 1; /* default: dynamic volume */
545
546 /* Use maximum available size */
547 size = 0;
548
549 /* E.g., create volume size type */
550 if (argc == 5) {
551 if (strncmp(argv[4], "s", 1) == 0)
552 dynamic = 0;
553 else if (strncmp(argv[4], "d", 1) != 0) {
554 printf("Incorrect type\n");
555 return 1;
556 }
557 argc--;
558 }
559 /* E.g., create volume size */
560 if (argc == 4) {
Stefan Roese19d87612008-12-02 10:53:47 +0100561 size = simple_strtoul(argv[3], NULL, 16);
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100562 argc--;
563 }
564 /* Use maximum available size */
Stefan Roese8cfc6962011-03-14 14:34:21 +0100565 if (!size) {
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100566 size = ubi->avail_pebs * ubi->leb_size;
Stefan Roese8cfc6962011-03-14 14:34:21 +0100567 printf("No size specified -> Using max size (%u)\n", size);
568 }
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100569 /* E.g., create volume */
570 if (argc == 3)
571 return ubi_create_vol(argv[2], size, dynamic);
572 }
573
574 if (strncmp(argv[1], "remove", 6) == 0) {
575 /* E.g., remove volume */
576 if (argc == 3)
577 return ubi_remove_vol(argv[2]);
578 }
579
580 if (strncmp(argv[1], "write", 5) == 0) {
Joe Hershberger92198452013-04-08 10:32:47 +0000581 int ret;
582
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100583 if (argc < 5) {
584 printf("Please see usage\n");
585 return 1;
586 }
587
588 addr = simple_strtoul(argv[2], NULL, 16);
Stefan Roese1b714052008-11-24 08:31:16 +0100589 size = simple_strtoul(argv[4], NULL, 16);
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100590
Joe Hershberger92198452013-04-08 10:32:47 +0000591 ret = ubi_volume_write(argv[3], (void *)addr, size);
592 if (!ret) {
593 printf("%d bytes written to volume %s\n", size,
594 argv[3]);
595 }
596
597 return ret;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100598 }
599
600 if (strncmp(argv[1], "read", 4) == 0) {
601 size = 0;
602
603 /* E.g., read volume size */
604 if (argc == 5) {
Stefan Roese1b714052008-11-24 08:31:16 +0100605 size = simple_strtoul(argv[4], NULL, 16);
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100606 argc--;
607 }
608
609 /* E.g., read volume */
610 if (argc == 4) {
611 addr = simple_strtoul(argv[2], NULL, 16);
612 argc--;
613 }
614
Joe Hershberger92198452013-04-08 10:32:47 +0000615 if (argc == 3) {
616 printf("Read %d bytes from volume %s to %lx\n", size,
617 argv[3], addr);
618
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100619 return ubi_volume_read(argv[3], (char *)addr, size);
Joe Hershberger92198452013-04-08 10:32:47 +0000620 }
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100621 }
622
623 printf("Please see usage\n");
Stefan Roese8cfc6962011-03-14 14:34:21 +0100624 return 1;
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100625}
626
Frans Meulenbroeks7675a092010-07-31 15:01:53 +0200627U_BOOT_CMD(
628 ubi, 6, 1, do_ubi,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600629 "ubi commands",
Simon Kagstrom4a8fbfb2009-07-07 16:59:46 +0200630 "part [part] [offset]\n"
631 " - Show or set current partition (with optional VID"
632 " header offset)\n"
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100633 "ubi info [l[ayout]]"
634 " - Display volume and ubi layout information\n"
635 "ubi create[vol] volume [size] [type]"
636 " - create volume name with size\n"
637 "ubi write[vol] address volume size"
638 " - Write volume from address with size\n"
639 "ubi read[vol] address volume [size]"
640 " - Read volume to address with size\n"
641 "ubi remove[vol] volume"
642 " - Remove volume\n"
643 "[Legends]\n"
Andrzej Wolski7dcc1702009-07-17 22:26:54 +0200644 " volume: character name\n"
645 " size: specified in bytes\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200646 " type: s[tatic] or d[ynamic] (default=dynamic)"
Kyungmin Park3d2c5ff2008-11-19 11:47:05 +0100647);