blob: 92998af2b02e4fb3d99f38228d82c826ee1e7a9a [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
Kyungmin Park694a0b32008-11-19 11:47:05 +010014#include <command.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060015#include <env.h>
Kyungmin Park694a0b32008-11-19 11:47:05 +010016#include <exports.h>
Simon Glass336d4612020-02-03 07:36:16 -070017#include <malloc.h>
Simon Glass6e295182015-09-02 17:24:57 -060018#include <memalign.h>
Miquel Raynalc58fb2c2018-09-29 12:58:29 +020019#include <mtd.h>
Kyungmin Park694a0b32008-11-19 11:47:05 +010020#include <nand.h>
21#include <onenand_uboot.h>
Simon Glass61b29b82020-02-03 07:36:15 -070022#include <dm/devres.h>
Kyungmin Park694a0b32008-11-19 11:47:05 +010023#include <linux/mtd/mtd.h>
24#include <linux/mtd/partitions.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020025#include <linux/err.h>
Kyungmin Park694a0b32008-11-19 11:47:05 +010026#include <ubi_uboot.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090027#include <linux/errno.h>
Kyungmin Park694a0b32008-11-19 11:47:05 +010028#include <jffs2/load_kernel.h>
Pali Rohár6de1daf2022-09-12 11:38:55 +020029#include <linux/log2.h>
Kyungmin Park694a0b32008-11-19 11:47:05 +010030
Joe Hershberger147162d2013-04-08 10:32:49 +000031#undef ubi_msg
32#define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__)
33
Kyungmin Park694a0b32008-11-19 11:47:05 +010034/* Private own data */
35static struct ubi_device *ubi;
Kyungmin Park694a0b32008-11-19 11:47:05 +010036
Stefan Roese2f15cfd2010-11-01 17:28:22 +010037#ifdef CONFIG_CMD_UBIFS
Tien Fong Chee10c20442018-07-06 16:25:12 +080038#include <ubifs_uboot.h>
Stefan Roese2f15cfd2010-11-01 17:28:22 +010039#endif
40
Kyungmin Park694a0b32008-11-19 11:47:05 +010041static void display_volume_info(struct ubi_device *ubi)
42{
43 int i;
44
45 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
46 if (!ubi->volumes[i])
47 continue; /* Empty record */
48 ubi_dump_vol_info(ubi->volumes[i]);
49 }
50}
51
52static void display_ubi_info(struct ubi_device *ubi)
53{
54 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
55 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
56 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
57 ubi->peb_size, ubi->peb_size >> 10);
58 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
59 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
60 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
61 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
62 ubi_msg("VID header offset: %d (aligned %d)",
63 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
64 ubi_msg("data offset: %d", ubi->leb_start);
65 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
66 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
67 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
68 ubi_msg("number of user volumes: %d",
69 ubi->vol_count - UBI_INT_VOL_COUNT);
70 ubi_msg("available PEBs: %d", ubi->avail_pebs);
71 ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
72 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
73 ubi->beb_rsvd_pebs);
74 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
75}
76
77static int ubi_info(int layout)
78{
79 if (layout)
80 display_volume_info(ubi);
81 else
82 display_ubi_info(ubi);
83
84 return 0;
85}
86
Pali Rohár6de1daf2022-09-12 11:38:55 +020087static int ubi_list(const char *var, int numeric)
88{
89 size_t namelen, len, size;
90 char *str, *str2;
91 int i;
92
93 if (!var) {
94 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
95 if (!ubi->volumes[i])
96 continue;
97 if (ubi->volumes[i]->vol_id >= UBI_INTERNAL_VOL_START)
98 continue;
99 printf("%d: %s\n",
100 ubi->volumes[i]->vol_id,
101 ubi->volumes[i]->name);
102 }
103 return 0;
104 }
105
106 len = 0;
107 size = 16;
108 str = malloc(size);
109 if (!str)
110 return 1;
111
112 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
113 if (!ubi->volumes[i])
114 continue;
115 if (ubi->volumes[i]->vol_id >= UBI_INTERNAL_VOL_START)
116 continue;
117
118 if (numeric)
119 namelen = 10; /* strlen(stringify(INT_MAX)) */
120 else
121 namelen = strlen(ubi->volumes[i]->name);
122
123 if (len + namelen + 1 > size) {
124 size = roundup_pow_of_two(len + namelen + 1) * 2;
125 str2 = realloc(str, size);
126 if (!str2) {
127 free(str);
128 return 1;
129 }
130 str = str2;
131 }
132
133 if (len)
134 str[len++] = ' ';
135
136 if (numeric) {
137 len += sprintf(str + len, "%d", ubi->volumes[i]->vol_id) + 1;
138 } else {
139 memcpy(str + len, ubi->volumes[i]->name, namelen);
140 len += namelen;
141 str[len] = 0;
142 }
143 }
144
145 env_set(var, str);
146 free(str);
147
148 return 0;
149}
150
Heiko Schocherf9f4d802014-01-25 07:27:11 +0100151static int ubi_check_volumename(const struct ubi_volume *vol, char *name)
152{
153 return strcmp(vol->name, name);
154}
155
156static int ubi_check(char *name)
157{
158 int i;
159
160 for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
161 if (!ubi->volumes[i])
162 continue; /* Empty record */
163
164 if (!ubi_check_volumename(ubi->volumes[i], name))
165 return 0;
166 }
167
Stefan Agner6d0f4522015-04-10 11:25:43 +0200168 return 1;
Heiko Schocherf9f4d802014-01-25 07:27:11 +0100169}
170
Kyungmin Park694a0b32008-11-19 11:47:05 +0100171static int verify_mkvol_req(const struct ubi_device *ubi,
172 const struct ubi_mkvol_req *req)
173{
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100174 int n, err = EINVAL;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100175
176 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
177 req->name_len < 0)
178 goto bad;
179
180 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
181 req->vol_id != UBI_VOL_NUM_AUTO)
182 goto bad;
183
184 if (req->alignment == 0)
185 goto bad;
186
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100187 if (req->bytes == 0) {
188 printf("No space left in UBI device!\n");
189 err = ENOMEM;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100190 goto bad;
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100191 }
Kyungmin Park694a0b32008-11-19 11:47:05 +0100192
193 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
194 req->vol_type != UBI_STATIC_VOLUME)
195 goto bad;
196
197 if (req->alignment > ubi->leb_size)
198 goto bad;
199
200 n = req->alignment % ubi->min_io_size;
201 if (req->alignment != 1 && n)
202 goto bad;
203
204 if (req->name_len > UBI_VOL_NAME_MAX) {
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100205 printf("Name too long!\n");
206 err = ENAMETOOLONG;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100207 goto bad;
208 }
209
210 return 0;
211bad:
Kyungmin Park694a0b32008-11-19 11:47:05 +0100212 return err;
213}
214
Quentin Schulz386f20c2019-09-12 16:41:01 +0200215static int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id,
216 bool skipcheck)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100217{
218 struct ubi_mkvol_req req;
219 int err;
220
221 if (dynamic)
222 req.vol_type = UBI_DYNAMIC_VOLUME;
223 else
224 req.vol_type = UBI_STATIC_VOLUME;
225
Ladislav Michl00612422016-09-13 07:14:01 +0200226 req.vol_id = vol_id;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100227 req.alignment = 1;
228 req.bytes = size;
229
230 strcpy(req.name, volume);
231 req.name_len = strlen(volume);
232 req.name[req.name_len] = '\0';
Quentin Schulz386f20c2019-09-12 16:41:01 +0200233 req.flags = 0;
234 if (skipcheck)
235 req.flags |= UBI_VOL_SKIP_CRC_CHECK_FLG;
236
Kyungmin Park694a0b32008-11-19 11:47:05 +0100237 /* It's duplicated at drivers/mtd/ubi/cdev.c */
238 err = verify_mkvol_req(ubi, &req);
239 if (err) {
240 printf("verify_mkvol_req failed %d\n", err);
241 return err;
242 }
Paul Burtondd7185f2013-09-04 15:16:58 +0100243 printf("Creating %s volume %s of size %lld\n",
Kyungmin Park694a0b32008-11-19 11:47:05 +0100244 dynamic ? "dynamic" : "static", volume, size);
245 /* Call real ubi create volume */
246 return ubi_create_volume(ubi, &req);
247}
248
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100249static struct ubi_volume *ubi_find_volume(char *volume)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100250{
Martin Kurbanovabae0b72024-06-16 16:34:17 +0300251 struct ubi_volume *vol;
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100252 int i;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100253
254 for (i = 0; i < ubi->vtbl_slots; i++) {
255 vol = ubi->volumes[i];
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100256 if (vol && !strcmp(vol->name, volume))
257 return vol;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100258 }
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100259
260 printf("Volume %s not found!\n", volume);
261 return NULL;
262}
263
264static int ubi_remove_vol(char *volume)
265{
266 int err, reserved_pebs, i;
267 struct ubi_volume *vol;
268
269 vol = ubi_find_volume(volume);
270 if (vol == NULL)
271 return ENODEV;
272
273 printf("Remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100274
275 if (ubi->ro_mode) {
276 printf("It's read-only mode\n");
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100277 err = EROFS;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100278 goto out_err;
279 }
280
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100281 err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100282 if (err) {
283 printf("Error changing Vol tabel record err=%x\n", err);
284 goto out_err;
285 }
286 reserved_pebs = vol->reserved_pebs;
287 for (i = 0; i < vol->reserved_pebs; i++) {
288 err = ubi_eba_unmap_leb(ubi, vol, i);
289 if (err)
290 goto out_err;
291 }
292
293 kfree(vol->eba_tbl);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100294 ubi->volumes[vol->vol_id]->eba_tbl = NULL;
295 ubi->volumes[vol->vol_id] = NULL;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100296
297 ubi->rsvd_pebs -= reserved_pebs;
298 ubi->avail_pebs += reserved_pebs;
299 i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
300 if (i > 0) {
301 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
302 ubi->avail_pebs -= i;
303 ubi->rsvd_pebs += i;
304 ubi->beb_rsvd_pebs += i;
305 if (i > 0)
306 ubi_msg("reserve more %d PEBs", i);
307 }
308 ubi->vol_count -= 1;
309
310 return 0;
311out_err:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200312 ubi_err(ubi, "cannot remove volume %s, error %d", volume, err);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100313 if (err < 0)
314 err = -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100315 return err;
316}
317
Philippe Reynes83f70782020-03-23 19:20:47 +0100318static int ubi_rename_vol(char *oldname, char *newname)
319{
320 struct ubi_volume *vol;
321 struct ubi_rename_entry rename;
322 struct ubi_volume_desc desc;
323 struct list_head list;
324
325 vol = ubi_find_volume(oldname);
326 if (!vol) {
327 printf("%s: volume %s doesn't exist\n", __func__, oldname);
328 return ENODEV;
329 }
330
Philippe Reynes16399882020-12-23 15:33:07 +0100331 if (!ubi_check(newname)) {
332 printf("%s: volume %s already exist\n", __func__, newname);
333 return EINVAL;
334 }
335
Philippe Reynes83f70782020-03-23 19:20:47 +0100336 printf("Rename UBI volume %s to %s\n", oldname, newname);
337
338 if (ubi->ro_mode) {
339 printf("%s: ubi device is in read-only mode\n", __func__);
340 return EROFS;
341 }
342
343 rename.new_name_len = strlen(newname);
344 strcpy(rename.new_name, newname);
345 rename.remove = 0;
346 desc.vol = vol;
347 desc.mode = 0;
348 rename.desc = &desc;
349 INIT_LIST_HEAD(&rename.list);
350 INIT_LIST_HEAD(&list);
351 list_add(&rename.list, &list);
352
353 return ubi_rename_volumes(ubi, &list);
354}
355
Jeroen Hofstee0e350f82014-06-23 00:22:08 +0200356static int ubi_volume_continue_write(char *volume, void *buf, size_t size)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100357{
Martin Kurbanovabae0b72024-06-16 16:34:17 +0300358 int err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100359 struct ubi_volume *vol;
360
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100361 vol = ubi_find_volume(volume);
362 if (vol == NULL)
363 return ENODEV;
364
Martin Kurbanov27b169f2024-06-16 16:34:18 +0300365 if (!vol->updating) {
366 printf("UBI volume update was not initiated\n");
367 return EINVAL;
368 }
369
Kyungmin Park694a0b32008-11-19 11:47:05 +0100370 err = ubi_more_update_data(ubi, vol, buf, size);
371 if (err < 0) {
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100372 printf("Couldnt or partially wrote data\n");
373 return -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100374 }
375
376 if (err) {
377 size = err;
378
379 err = ubi_check_volume(ubi, vol->vol_id);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100380 if (err < 0)
381 return -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100382
383 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200384 ubi_warn(ubi, "volume %d on UBI device %d is corrupt",
385 vol->vol_id, ubi->ubi_num);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100386 vol->corrupted = 1;
387 }
388
389 vol->checked = 1;
390 ubi_gluebi_updated(vol);
391 }
392
393 return 0;
394}
395
Paul Burtoncc734f52013-09-04 15:16:59 +0100396int ubi_volume_begin_write(char *volume, void *buf, size_t size,
397 size_t full_size)
398{
Martin Kurbanovabae0b72024-06-16 16:34:17 +0300399 int err;
400 int rsvd_bytes;
Paul Burtoncc734f52013-09-04 15:16:59 +0100401 struct ubi_volume *vol;
402
403 vol = ubi_find_volume(volume);
404 if (vol == NULL)
405 return ENODEV;
406
407 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
xypron.glpk@gmx.de18f41f22017-04-15 16:25:25 +0200408 if (size > rsvd_bytes) {
Paul Burtoncc734f52013-09-04 15:16:59 +0100409 printf("size > volume size! Aborting!\n");
410 return EINVAL;
411 }
412
413 err = ubi_start_update(ubi, vol, full_size);
414 if (err < 0) {
415 printf("Cannot start volume update\n");
416 return -err;
417 }
418
Martin Kurbanov27b169f2024-06-16 16:34:18 +0300419 /* The volume is just wiped out */
420 if (!full_size)
421 return 0;
422
Paul Burtoncc734f52013-09-04 15:16:59 +0100423 return ubi_volume_continue_write(volume, buf, size);
424}
425
426int ubi_volume_write(char *volume, void *buf, size_t size)
427{
428 return ubi_volume_begin_write(volume, buf, size, size);
429}
430
Joe Hershberger71829062013-04-08 10:32:47 +0000431int ubi_volume_read(char *volume, char *buf, size_t size)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100432{
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100433 int err, lnum, off, len, tbuf_size;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100434 void *tbuf;
435 unsigned long long tmp;
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100436 struct ubi_volume *vol;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100437 loff_t offp = 0;
Holger Dengler985fa932017-08-30 18:38:52 +0200438 size_t len_read;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100439
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100440 vol = ubi_find_volume(volume);
441 if (vol == NULL)
442 return ENODEV;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100443
Kyungmin Park694a0b32008-11-19 11:47:05 +0100444 if (vol->updating) {
445 printf("updating");
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100446 return EBUSY;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100447 }
448 if (vol->upd_marker) {
449 printf("damaged volume, update marker is set");
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100450 return EBADF;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100451 }
452 if (offp == vol->used_bytes)
453 return 0;
454
455 if (size == 0) {
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100456 printf("No size specified -> Using max size (%lld)\n", vol->used_bytes);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100457 size = vol->used_bytes;
458 }
459
Tom Rini13415332018-07-26 11:17:24 -0400460 printf("Read %zu bytes from volume %s to %p\n", size, volume, buf);
Stefan Agner68c70252018-06-25 11:19:12 +0200461
Kyungmin Park694a0b32008-11-19 11:47:05 +0100462 if (vol->corrupted)
463 printf("read from corrupted volume %d", vol->vol_id);
464 if (offp + size > vol->used_bytes)
Marek Vasut2984fd12011-09-30 12:13:25 +0200465 size = vol->used_bytes - offp;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100466
467 tbuf_size = vol->usable_leb_size;
468 if (size < tbuf_size)
469 tbuf_size = ALIGN(size, ubi->min_io_size);
Marcel Ziswiler45196682015-08-18 13:06:37 +0200470 tbuf = malloc_cache_aligned(tbuf_size);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100471 if (!tbuf) {
472 printf("NO MEM\n");
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100473 return ENOMEM;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100474 }
475 len = size > tbuf_size ? tbuf_size : size;
476
477 tmp = offp;
478 off = do_div(tmp, vol->usable_leb_size);
479 lnum = tmp;
Holger Dengler985fa932017-08-30 18:38:52 +0200480 len_read = size;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100481 do {
482 if (off + len >= vol->usable_leb_size)
483 len = vol->usable_leb_size - off;
484
485 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
486 if (err) {
487 printf("read err %x\n", err);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100488 err = -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100489 break;
490 }
491 off += len;
492 if (off == vol->usable_leb_size) {
493 lnum += 1;
494 off -= vol->usable_leb_size;
495 }
496
497 size -= len;
498 offp += len;
499
Kyungmin Park694a0b32008-11-19 11:47:05 +0100500 memcpy(buf, tbuf, len);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100501
502 buf += len;
503 len = size > tbuf_size ? tbuf_size : size;
504 } while (size);
505
Holger Dengler985fa932017-08-30 18:38:52 +0200506 if (!size)
507 env_set_hex("filesize", len_read);
508
Kyungmin Park694a0b32008-11-19 11:47:05 +0100509 free(tbuf);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100510 return err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100511}
512
Miquel Raynalc58fb2c2018-09-29 12:58:29 +0200513static int ubi_dev_scan(struct mtd_info *info, const char *vid_header_offset)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100514{
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200515 char ubi_mtd_param_buffer[80];
Kyungmin Park694a0b32008-11-19 11:47:05 +0100516 int err;
517
Miquel Raynalc58fb2c2018-09-29 12:58:29 +0200518 if (!vid_header_offset)
519 sprintf(ubi_mtd_param_buffer, "%s", info->name);
520 else
521 sprintf(ubi_mtd_param_buffer, "%s,%s", info->name,
522 vid_header_offset);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100523
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200524 err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
Miquel Raynalc58fb2c2018-09-29 12:58:29 +0200525 if (err)
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100526 return -err;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100527
528 err = ubi_init();
Miquel Raynalc58fb2c2018-09-29 12:58:29 +0200529 if (err)
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100530 return -err;
Stefan Roese2ee951b2008-11-27 14:07:09 +0100531
Kyungmin Park694a0b32008-11-19 11:47:05 +0100532 return 0;
533}
534
Stefan Roesee6661cf2019-09-17 09:17:53 +0200535static int ubi_set_skip_check(char *volume, bool skip_check)
536{
537 struct ubi_vtbl_record vtbl_rec;
538 struct ubi_volume *vol;
539
540 vol = ubi_find_volume(volume);
541 if (!vol)
542 return ENODEV;
543
544 printf("%sing skip_check on volume %s\n",
545 skip_check ? "Sett" : "Clear", volume);
546
547 vtbl_rec = ubi->vtbl[vol->vol_id];
548 if (skip_check) {
549 vtbl_rec.flags |= UBI_VTBL_SKIP_CRC_CHECK_FLG;
550 vol->skip_check = 1;
551 } else {
552 vtbl_rec.flags &= ~UBI_VTBL_SKIP_CRC_CHECK_FLG;
553 vol->skip_check = 0;
554 }
555
556 return ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
557}
558
Stefan Roesed821e5e2018-10-31 12:37:20 +0100559static int ubi_detach(void)
Kyungmin Park694a0b32008-11-19 11:47:05 +0100560{
Joe Hershberger71829062013-04-08 10:32:47 +0000561#ifdef CONFIG_CMD_UBIFS
562 /*
563 * Automatically unmount UBIFS partition when user
564 * changes the UBI device. Otherwise the following
565 * UBIFS commands will crash.
566 */
567 if (ubifs_is_mounted())
568 cmd_ubifs_umount();
569#endif
570
Joe Hershberger71829062013-04-08 10:32:47 +0000571 /*
572 * Call ubi_exit() before re-initializing the UBI subsystem
573 */
Miquel Raynalc58fb2c2018-09-29 12:58:29 +0200574 if (ubi)
Joe Hershberger71829062013-04-08 10:32:47 +0000575 ubi_exit();
Joe Hershberger71829062013-04-08 10:32:47 +0000576
Miquel Raynalc58fb2c2018-09-29 12:58:29 +0200577 ubi = NULL;
578
Heiko Schochercddfc972016-06-07 08:55:40 +0200579 return 0;
580}
581
582int ubi_part(char *part_name, const char *vid_header_offset)
583{
Miquel Raynalc58fb2c2018-09-29 12:58:29 +0200584 struct mtd_info *mtd;
Martin Kurbanovabae0b72024-06-16 16:34:17 +0300585 int err;
Heiko Schochercddfc972016-06-07 08:55:40 +0200586
Alexandre Besnardaf298f32022-04-04 17:50:14 +0200587 if (ubi && ubi->mtd && !strcmp(ubi->mtd->name, part_name)) {
588 printf("UBI partition '%s' already selected\n", part_name);
589 return 0;
590 }
591
Heiko Schochercddfc972016-06-07 08:55:40 +0200592 ubi_detach();
Miquel Raynalc58fb2c2018-09-29 12:58:29 +0200593
594 mtd_probe_devices();
595 mtd = get_mtd_device_nm(part_name);
596 if (IS_ERR(mtd)) {
Joe Hershberger71829062013-04-08 10:32:47 +0000597 printf("Partition %s not found!\n", part_name);
598 return 1;
599 }
Miquel Raynalc58fb2c2018-09-29 12:58:29 +0200600 put_mtd_device(mtd);
Joe Hershberger71829062013-04-08 10:32:47 +0000601
Miquel Raynalc58fb2c2018-09-29 12:58:29 +0200602 err = ubi_dev_scan(mtd, vid_header_offset);
Joe Hershberger71829062013-04-08 10:32:47 +0000603 if (err) {
604 printf("UBI init error %d\n", err);
Stefan Roese4a94e532018-06-26 08:12:32 +0200605 printf("Please check, if the correct MTD partition is used (size big enough?)\n");
Joe Hershberger71829062013-04-08 10:32:47 +0000606 return err;
607 }
608
609 ubi = ubi_devices[0];
610
611 return 0;
612}
613
Simon Glass09140112020-05-10 11:40:03 -0600614static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Joe Hershberger71829062013-04-08 10:32:47 +0000615{
Martin Kurbanovabae0b72024-06-16 16:34:17 +0300616 int64_t size;
Joe Hershberger71829062013-04-08 10:32:47 +0000617 ulong addr = 0;
Quentin Schulz386f20c2019-09-12 16:41:01 +0200618 bool skipcheck = false;
Joe Hershberger71829062013-04-08 10:32:47 +0000619
620 if (argc < 2)
621 return CMD_RET_USAGE;
622
Heinrich Schuchardt76428562019-01-06 12:26:28 +0100623 if (strcmp(argv[1], "detach") == 0)
Heiko Schochercddfc972016-06-07 08:55:40 +0200624 return ubi_detach();
Heiko Schochercddfc972016-06-07 08:55:40 +0200625
Kyungmin Park694a0b32008-11-19 11:47:05 +0100626 if (strcmp(argv[1], "part") == 0) {
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200627 const char *vid_header_offset = NULL;
Stefan Roese2d579e52009-04-24 20:24:19 +0200628
Kyungmin Park694a0b32008-11-19 11:47:05 +0100629 /* Print current partition */
630 if (argc == 2) {
Miquel Raynalc58fb2c2018-09-29 12:58:29 +0200631 if (!ubi) {
632 printf("Error, no UBI device selected!\n");
Kyungmin Park694a0b32008-11-19 11:47:05 +0100633 return 1;
634 }
635
Miquel Raynalc58fb2c2018-09-29 12:58:29 +0200636 printf("Device %d: %s, MTD partition %s\n",
637 ubi->ubi_num, ubi->ubi_name, ubi->mtd->name);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100638 return 0;
639 }
640
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200641 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000642 return CMD_RET_USAGE;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100643
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200644 if (argc > 3)
645 vid_header_offset = argv[3];
Kyungmin Park694a0b32008-11-19 11:47:05 +0100646
Joe Hershberger71829062013-04-08 10:32:47 +0000647 return ubi_part(argv[2], vid_header_offset);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100648 }
649
Miquel Raynalc58fb2c2018-09-29 12:58:29 +0200650 if ((strcmp(argv[1], "part") != 0) && !ubi) {
651 printf("Error, no UBI device selected!\n");
Kyungmin Park694a0b32008-11-19 11:47:05 +0100652 return 1;
653 }
654
655 if (strcmp(argv[1], "info") == 0) {
656 int layout = 0;
657 if (argc > 2 && !strncmp(argv[2], "l", 1))
658 layout = 1;
659 return ubi_info(layout);
660 }
661
Pali Rohár6de1daf2022-09-12 11:38:55 +0200662 if (strcmp(argv[1], "list") == 0) {
663 int numeric = 0;
Dmitry Dunaev34031e92023-07-12 15:58:21 +0300664 if (argc >= 3 && argv[2][0] == '-') {
Pali Rohár6de1daf2022-09-12 11:38:55 +0200665 if (strcmp(argv[2], "-numeric") == 0)
666 numeric = 1;
667 else
668 return CMD_RET_USAGE;
669 }
670 if (!numeric && argc != 2 && argc != 3)
671 return CMD_RET_USAGE;
672 if (numeric && argc != 3 && argc != 4)
673 return CMD_RET_USAGE;
674 return ubi_list(argv[numeric ? 3 : 2], numeric);
675 }
676
Heiko Schocherf9f4d802014-01-25 07:27:11 +0100677 if (strcmp(argv[1], "check") == 0) {
678 if (argc > 2)
679 return ubi_check(argv[2]);
680
681 printf("Error, no volume name passed\n");
682 return 1;
683 }
684
Kyungmin Park694a0b32008-11-19 11:47:05 +0100685 if (strncmp(argv[1], "create", 6) == 0) {
686 int dynamic = 1; /* default: dynamic volume */
Ladislav Michl00612422016-09-13 07:14:01 +0200687 int id = UBI_VOL_NUM_AUTO;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100688
689 /* Use maximum available size */
690 size = 0;
691
Quentin Schulz386f20c2019-09-12 16:41:01 +0200692 /* E.g., create volume with "skipcheck" bit set */
693 if (argc == 7) {
694 skipcheck = strncmp(argv[6], "--skipcheck", 11) == 0;
695 argc--;
696 }
697
Ladislav Michl00612422016-09-13 07:14:01 +0200698 /* E.g., create volume size type vol_id */
699 if (argc == 6) {
700 id = simple_strtoull(argv[5], NULL, 16);
701 argc--;
702 }
703
Kyungmin Park694a0b32008-11-19 11:47:05 +0100704 /* E.g., create volume size type */
705 if (argc == 5) {
706 if (strncmp(argv[4], "s", 1) == 0)
707 dynamic = 0;
708 else if (strncmp(argv[4], "d", 1) != 0) {
709 printf("Incorrect type\n");
710 return 1;
711 }
712 argc--;
713 }
714 /* E.g., create volume size */
715 if (argc == 4) {
Ladislav Michlf59f07e2017-01-19 11:45:35 +0100716 if (argv[3][0] != '-')
717 size = simple_strtoull(argv[3], NULL, 16);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100718 argc--;
719 }
720 /* Use maximum available size */
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100721 if (!size) {
Paul Burtondd7185f2013-09-04 15:16:58 +0100722 size = (int64_t)ubi->avail_pebs * ubi->leb_size;
723 printf("No size specified -> Using max size (%lld)\n", size);
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100724 }
Kyungmin Park694a0b32008-11-19 11:47:05 +0100725 /* E.g., create volume */
Quentin Schulz386f20c2019-09-12 16:41:01 +0200726 if (argc == 3) {
727 return ubi_create_vol(argv[2], size, dynamic, id,
728 skipcheck);
729 }
Kyungmin Park694a0b32008-11-19 11:47:05 +0100730 }
731
732 if (strncmp(argv[1], "remove", 6) == 0) {
733 /* E.g., remove volume */
734 if (argc == 3)
735 return ubi_remove_vol(argv[2]);
736 }
737
Philippe Reynes83f70782020-03-23 19:20:47 +0100738 if (IS_ENABLED(CONFIG_CMD_UBI_RENAME) && !strncmp(argv[1], "rename", 6))
739 return ubi_rename_vol(argv[2], argv[3]);
740
Stefan Roesee6661cf2019-09-17 09:17:53 +0200741 if (strncmp(argv[1], "skipcheck", 9) == 0) {
742 /* E.g., change skip_check flag */
743 if (argc == 4) {
744 skipcheck = strncmp(argv[3], "on", 2) == 0;
745 return ubi_set_skip_check(argv[2], skipcheck);
746 }
747 }
748
Kyungmin Park694a0b32008-11-19 11:47:05 +0100749 if (strncmp(argv[1], "write", 5) == 0) {
Joe Hershberger71829062013-04-08 10:32:47 +0000750 int ret;
751
Kyungmin Park694a0b32008-11-19 11:47:05 +0100752 if (argc < 5) {
753 printf("Please see usage\n");
754 return 1;
755 }
756
Simon Glass7e5f4602021-07-24 09:03:29 -0600757 addr = hextoul(argv[2], NULL);
758 size = hextoul(argv[4], NULL);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100759
Paul Burtoncc734f52013-09-04 15:16:59 +0100760 if (strlen(argv[1]) == 10 &&
761 strncmp(argv[1] + 5, ".part", 5) == 0) {
762 if (argc < 6) {
763 ret = ubi_volume_continue_write(argv[3],
764 (void *)addr, size);
765 } else {
766 size_t full_size;
Simon Glass7e5f4602021-07-24 09:03:29 -0600767 full_size = hextoul(argv[5], NULL);
Paul Burtoncc734f52013-09-04 15:16:59 +0100768 ret = ubi_volume_begin_write(argv[3],
769 (void *)addr, size, full_size);
770 }
771 } else {
772 ret = ubi_volume_write(argv[3], (void *)addr, size);
773 }
Joe Hershberger71829062013-04-08 10:32:47 +0000774 if (!ret) {
Paul Burtondd7185f2013-09-04 15:16:58 +0100775 printf("%lld bytes written to volume %s\n", size,
Joe Hershberger71829062013-04-08 10:32:47 +0000776 argv[3]);
777 }
778
779 return ret;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100780 }
781
782 if (strncmp(argv[1], "read", 4) == 0) {
783 size = 0;
784
785 /* E.g., read volume size */
786 if (argc == 5) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600787 size = hextoul(argv[4], NULL);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100788 argc--;
789 }
790
791 /* E.g., read volume */
792 if (argc == 4) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600793 addr = hextoul(argv[2], NULL);
Kyungmin Park694a0b32008-11-19 11:47:05 +0100794 argc--;
795 }
796
Joe Hershberger71829062013-04-08 10:32:47 +0000797 if (argc == 3) {
Kyungmin Park694a0b32008-11-19 11:47:05 +0100798 return ubi_volume_read(argv[3], (char *)addr, size);
Joe Hershberger71829062013-04-08 10:32:47 +0000799 }
Kyungmin Park694a0b32008-11-19 11:47:05 +0100800 }
801
802 printf("Please see usage\n");
Stefan Roese7f5d8a42011-03-14 14:34:21 +0100803 return 1;
Kyungmin Park694a0b32008-11-19 11:47:05 +0100804}
805
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200806U_BOOT_CMD(
Quentin Schulz386f20c2019-09-12 16:41:01 +0200807 ubi, 7, 1, do_ubi,
Peter Tyser2fb26042009-01-27 18:03:12 -0600808 "ubi commands",
Heiko Schochercddfc972016-06-07 08:55:40 +0200809 "detach"
810 " - detach ubi from a mtd partition\n"
811 "ubi part [part] [offset]\n"
Simon Kagstrom25c8f402009-07-07 16:59:46 +0200812 " - Show or set current partition (with optional VID"
813 " header offset)\n"
Kyungmin Park694a0b32008-11-19 11:47:05 +0100814 "ubi info [l[ayout]]"
815 " - Display volume and ubi layout information\n"
Pali Rohár6de1daf2022-09-12 11:38:55 +0200816 "ubi list [flags]"
817 " - print the list of volumes\n"
818 "ubi list [flags] <varname>"
819 " - set environment variable to the list of volumes"
820 " (flags can be -numeric)\n"
Heiko Schocherf9f4d802014-01-25 07:27:11 +0100821 "ubi check volumename"
822 " - check if volumename exists\n"
Quentin Schulz386f20c2019-09-12 16:41:01 +0200823 "ubi create[vol] volume [size] [type] [id] [--skipcheck]\n"
Ladislav Michlf59f07e2017-01-19 11:45:35 +0100824 " - create volume name with size ('-' for maximum"
825 " available size)\n"
Kyungmin Park694a0b32008-11-19 11:47:05 +0100826 "ubi write[vol] address volume size"
827 " - Write volume from address with size\n"
Paul Burtoncc734f52013-09-04 15:16:59 +0100828 "ubi write.part address volume size [fullsize]\n"
829 " - Write part of a volume from address\n"
Kyungmin Park694a0b32008-11-19 11:47:05 +0100830 "ubi read[vol] address volume [size]"
831 " - Read volume to address with size\n"
832 "ubi remove[vol] volume"
833 " - Remove volume\n"
Philippe Reynes83f70782020-03-23 19:20:47 +0100834#if IS_ENABLED(CONFIG_CMD_UBI_RENAME)
835 "ubi rename oldname newname\n"
836#endif
Stefan Roesee6661cf2019-09-17 09:17:53 +0200837 "ubi skipcheck volume on/off - Set or clear skip_check flag in volume header\n"
Kyungmin Park694a0b32008-11-19 11:47:05 +0100838 "[Legends]\n"
Andrzej Wolskif6ca3b72009-07-17 22:26:54 +0200839 " volume: character name\n"
840 " size: specified in bytes\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200841 " type: s[tatic] or d[ynamic] (default=dynamic)"
Kyungmin Park694a0b32008-11-19 11:47:05 +0100842);