Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Unsorted Block Image commands |
| 3 | * |
| 4 | * Copyright (C) 2008 Samsung Electronics |
| 5 | * Kyungmin Park <kyungmin.park@samsung.com> |
| 6 | * |
Stefan Roese | 62eafb7 | 2009-04-24 20:24:19 +0200 | [diff] [blame] | 7 | * Copyright 2008-2009 Stefan Roese <sr@denx.de>, DENX Software Engineering |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 14 | #include <command.h> |
Simon Glass | 313112a | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 15 | #include <env.h> |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 16 | #include <exports.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 17 | #include <malloc.h> |
Simon Glass | a87fc0a | 2015-09-02 17:24:57 -0600 | [diff] [blame] | 18 | #include <memalign.h> |
Miquel Raynal | 12f1ff1 | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 19 | #include <mtd.h> |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 20 | #include <nand.h> |
| 21 | #include <onenand_uboot.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 22 | #include <dm/devres.h> |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 23 | #include <linux/mtd/mtd.h> |
| 24 | #include <linux/mtd/partitions.h> |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 25 | #include <linux/err.h> |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 26 | #include <ubi_uboot.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 27 | #include <linux/errno.h> |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 28 | #include <jffs2/load_kernel.h> |
Pali Rohár | 9395eb9 | 2022-09-12 11:38:55 +0200 | [diff] [blame] | 29 | #include <linux/log2.h> |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 30 | |
Joe Hershberger | 47550fc | 2013-04-08 10:32:49 +0000 | [diff] [blame] | 31 | #undef ubi_msg |
| 32 | #define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__) |
| 33 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 34 | /* Private own data */ |
| 35 | static struct ubi_device *ubi; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 36 | |
Stefan Roese | 408c6c4 | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 37 | #ifdef CONFIG_CMD_UBIFS |
Tien Fong Chee | dbc9d85 | 2018-07-06 16:25:12 +0800 | [diff] [blame] | 38 | #include <ubifs_uboot.h> |
Stefan Roese | 408c6c4 | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 39 | #endif |
| 40 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 41 | static 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 | |
| 52 | static 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 | |
| 77 | static 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ár | 9395eb9 | 2022-09-12 11:38:55 +0200 | [diff] [blame] | 87 | static 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 Schocher | 1762ab1 | 2014-01-25 07:27:11 +0100 | [diff] [blame] | 151 | static int ubi_check_volumename(const struct ubi_volume *vol, char *name) |
| 152 | { |
| 153 | return strcmp(vol->name, name); |
| 154 | } |
| 155 | |
| 156 | static 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 Agner | 6a889f2 | 2015-04-10 11:25:43 +0200 | [diff] [blame] | 168 | return 1; |
Heiko Schocher | 1762ab1 | 2014-01-25 07:27:11 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 171 | static int verify_mkvol_req(const struct ubi_device *ubi, |
| 172 | const struct ubi_mkvol_req *req) |
| 173 | { |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 174 | int n, err = EINVAL; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 175 | |
| 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 Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 187 | if (req->bytes == 0) { |
| 188 | printf("No space left in UBI device!\n"); |
| 189 | err = ENOMEM; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 190 | goto bad; |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 191 | } |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 192 | |
| 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 Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 205 | printf("Name too long!\n"); |
| 206 | err = ENAMETOOLONG; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 207 | goto bad; |
| 208 | } |
| 209 | |
| 210 | return 0; |
| 211 | bad: |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 212 | return err; |
| 213 | } |
| 214 | |
Quentin Schulz | a88049b | 2019-09-12 16:41:01 +0200 | [diff] [blame] | 215 | static int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id, |
| 216 | bool skipcheck) |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 217 | { |
| 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 Michl | 39370f1 | 2016-09-13 07:14:01 +0200 | [diff] [blame] | 226 | req.vol_id = vol_id; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 227 | 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 Schulz | a88049b | 2019-09-12 16:41:01 +0200 | [diff] [blame] | 233 | req.flags = 0; |
| 234 | if (skipcheck) |
| 235 | req.flags |= UBI_VOL_SKIP_CRC_CHECK_FLG; |
| 236 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 237 | /* 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 Burton | df68616 | 2013-09-04 15:16:58 +0100 | [diff] [blame] | 243 | printf("Creating %s volume %s of size %lld\n", |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 244 | dynamic ? "dynamic" : "static", volume, size); |
| 245 | /* Call real ubi create volume */ |
| 246 | return ubi_create_volume(ubi, &req); |
| 247 | } |
| 248 | |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 249 | static struct ubi_volume *ubi_find_volume(char *volume) |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 250 | { |
Martin Kurbanov | fb42f60 | 2024-06-16 16:34:17 +0300 | [diff] [blame] | 251 | struct ubi_volume *vol; |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 252 | int i; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 253 | |
| 254 | for (i = 0; i < ubi->vtbl_slots; i++) { |
| 255 | vol = ubi->volumes[i]; |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 256 | if (vol && !strcmp(vol->name, volume)) |
| 257 | return vol; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 258 | } |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 259 | |
| 260 | printf("Volume %s not found!\n", volume); |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | static 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 Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 274 | |
| 275 | if (ubi->ro_mode) { |
| 276 | printf("It's read-only mode\n"); |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 277 | err = EROFS; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 278 | goto out_err; |
| 279 | } |
| 280 | |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 281 | err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL); |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 282 | 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 Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 294 | ubi->volumes[vol->vol_id]->eba_tbl = NULL; |
| 295 | ubi->volumes[vol->vol_id] = NULL; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 296 | |
| 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; |
| 311 | out_err: |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 312 | ubi_err(ubi, "cannot remove volume %s, error %d", volume, err); |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 313 | if (err < 0) |
| 314 | err = -err; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 315 | return err; |
| 316 | } |
| 317 | |
Philippe Reynes | 10d5322 | 2020-03-23 19:20:47 +0100 | [diff] [blame] | 318 | static 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 Reynes | 0cf9f47 | 2020-12-23 15:33:07 +0100 | [diff] [blame] | 331 | if (!ubi_check(newname)) { |
| 332 | printf("%s: volume %s already exist\n", __func__, newname); |
| 333 | return EINVAL; |
| 334 | } |
| 335 | |
Philippe Reynes | 10d5322 | 2020-03-23 19:20:47 +0100 | [diff] [blame] | 336 | 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 Hofstee | f384fbf | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 356 | static int ubi_volume_continue_write(char *volume, void *buf, size_t size) |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 357 | { |
Martin Kurbanov | fb42f60 | 2024-06-16 16:34:17 +0300 | [diff] [blame] | 358 | int err; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 359 | struct ubi_volume *vol; |
| 360 | |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 361 | vol = ubi_find_volume(volume); |
| 362 | if (vol == NULL) |
| 363 | return ENODEV; |
| 364 | |
Martin Kurbanov | ff3f083 | 2024-06-16 16:34:18 +0300 | [diff] [blame] | 365 | if (!vol->updating) { |
| 366 | printf("UBI volume update was not initiated\n"); |
| 367 | return EINVAL; |
| 368 | } |
| 369 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 370 | err = ubi_more_update_data(ubi, vol, buf, size); |
| 371 | if (err < 0) { |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 372 | printf("Couldnt or partially wrote data\n"); |
| 373 | return -err; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | if (err) { |
| 377 | size = err; |
| 378 | |
| 379 | err = ubi_check_volume(ubi, vol->vol_id); |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 380 | if (err < 0) |
| 381 | return -err; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 382 | |
| 383 | if (err) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 384 | ubi_warn(ubi, "volume %d on UBI device %d is corrupt", |
| 385 | vol->vol_id, ubi->ubi_num); |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 386 | vol->corrupted = 1; |
| 387 | } |
| 388 | |
| 389 | vol->checked = 1; |
| 390 | ubi_gluebi_updated(vol); |
| 391 | } |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
Paul Burton | 6627aa3 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 396 | int ubi_volume_begin_write(char *volume, void *buf, size_t size, |
| 397 | size_t full_size) |
| 398 | { |
Martin Kurbanov | fb42f60 | 2024-06-16 16:34:17 +0300 | [diff] [blame] | 399 | int err; |
| 400 | int rsvd_bytes; |
Paul Burton | 6627aa3 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 401 | 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.de | 632a945 | 2017-04-15 16:25:25 +0200 | [diff] [blame] | 408 | if (size > rsvd_bytes) { |
Paul Burton | 6627aa3 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 409 | 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 Kurbanov | ff3f083 | 2024-06-16 16:34:18 +0300 | [diff] [blame] | 419 | /* The volume is just wiped out */ |
| 420 | if (!full_size) |
| 421 | return 0; |
| 422 | |
Paul Burton | 6627aa3 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 423 | return ubi_volume_continue_write(volume, buf, size); |
| 424 | } |
| 425 | |
Alexey Romanov | de417e9 | 2024-07-18 08:45:24 +0300 | [diff] [blame^] | 426 | static int ubi_volume_offset_write(char *volume, void *buf, loff_t offset, |
| 427 | size_t size) |
Paul Burton | 6627aa3 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 428 | { |
Alexey Romanov | de417e9 | 2024-07-18 08:45:24 +0300 | [diff] [blame^] | 429 | int len, tbuf_size, ret; |
| 430 | u64 lnum; |
| 431 | struct ubi_volume *vol; |
| 432 | loff_t off = offset; |
| 433 | void *tbuf; |
| 434 | |
| 435 | vol = ubi_find_volume(volume); |
| 436 | if (!vol) |
| 437 | return -ENODEV; |
| 438 | |
| 439 | if (size > vol->reserved_pebs * (ubi->leb_size - vol->data_pad)) |
| 440 | return -EINVAL; |
| 441 | |
| 442 | tbuf_size = vol->usable_leb_size; |
| 443 | tbuf = malloc_cache_aligned(tbuf_size); |
| 444 | if (!tbuf) |
| 445 | return -ENOMEM; |
| 446 | |
| 447 | lnum = off; |
| 448 | off = do_div(lnum, vol->usable_leb_size); |
| 449 | |
| 450 | do { |
| 451 | struct ubi_volume_desc desc = { |
| 452 | .vol = vol, |
| 453 | .mode = UBI_READWRITE, |
| 454 | }; |
| 455 | |
| 456 | len = size > tbuf_size ? tbuf_size : size; |
| 457 | if (off + len >= vol->usable_leb_size) |
| 458 | len = vol->usable_leb_size - off; |
| 459 | |
| 460 | ret = ubi_read(&desc, (int)lnum, tbuf, 0, tbuf_size); |
| 461 | if (ret) { |
| 462 | pr_err("Failed to read leb %lld (%d)\n", lnum, ret); |
| 463 | goto exit; |
| 464 | } |
| 465 | |
| 466 | memcpy(tbuf + off, buf, len); |
| 467 | |
| 468 | ret = ubi_leb_change(&desc, (int)lnum, tbuf, tbuf_size); |
| 469 | if (ret) { |
| 470 | pr_err("Failed to write leb %lld (%d)\n", lnum, ret); |
| 471 | goto exit; |
| 472 | } |
| 473 | |
| 474 | off += len; |
| 475 | if (off >= vol->usable_leb_size) { |
| 476 | lnum++; |
| 477 | off -= vol->usable_leb_size; |
| 478 | } |
| 479 | |
| 480 | buf += len; |
| 481 | size -= len; |
| 482 | } while (size); |
| 483 | |
| 484 | exit: |
| 485 | free(tbuf); |
| 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | int ubi_volume_write(char *volume, void *buf, loff_t offset, size_t size) |
| 490 | { |
| 491 | if (!offset) |
| 492 | return ubi_volume_begin_write(volume, buf, size, size); |
| 493 | |
| 494 | return ubi_volume_offset_write(volume, buf, offset, size); |
Paul Burton | 6627aa3 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 495 | } |
| 496 | |
Alexey Romanov | 0043a97 | 2024-07-18 08:45:23 +0300 | [diff] [blame] | 497 | int ubi_volume_read(char *volume, char *buf, loff_t offset, size_t size) |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 498 | { |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 499 | int err, lnum, off, len, tbuf_size; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 500 | void *tbuf; |
| 501 | unsigned long long tmp; |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 502 | struct ubi_volume *vol; |
Alexey Romanov | 0043a97 | 2024-07-18 08:45:23 +0300 | [diff] [blame] | 503 | loff_t offp = offset; |
Holger Dengler | a14912b | 2017-08-30 18:38:52 +0200 | [diff] [blame] | 504 | size_t len_read; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 505 | |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 506 | vol = ubi_find_volume(volume); |
| 507 | if (vol == NULL) |
| 508 | return ENODEV; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 509 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 510 | if (vol->updating) { |
| 511 | printf("updating"); |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 512 | return EBUSY; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 513 | } |
| 514 | if (vol->upd_marker) { |
| 515 | printf("damaged volume, update marker is set"); |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 516 | return EBADF; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 517 | } |
| 518 | if (offp == vol->used_bytes) |
| 519 | return 0; |
| 520 | |
| 521 | if (size == 0) { |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 522 | printf("No size specified -> Using max size (%lld)\n", vol->used_bytes); |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 523 | size = vol->used_bytes; |
| 524 | } |
| 525 | |
Tom Rini | 3aa6e39 | 2018-07-26 11:17:24 -0400 | [diff] [blame] | 526 | printf("Read %zu bytes from volume %s to %p\n", size, volume, buf); |
Stefan Agner | 08d4e0c | 2018-06-25 11:19:12 +0200 | [diff] [blame] | 527 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 528 | if (vol->corrupted) |
| 529 | printf("read from corrupted volume %d", vol->vol_id); |
| 530 | if (offp + size > vol->used_bytes) |
Marek Vasut | 40c0c8b | 2011-09-30 12:13:25 +0200 | [diff] [blame] | 531 | size = vol->used_bytes - offp; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 532 | |
| 533 | tbuf_size = vol->usable_leb_size; |
| 534 | if (size < tbuf_size) |
| 535 | tbuf_size = ALIGN(size, ubi->min_io_size); |
Marcel Ziswiler | abc574b | 2015-08-18 13:06:37 +0200 | [diff] [blame] | 536 | tbuf = malloc_cache_aligned(tbuf_size); |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 537 | if (!tbuf) { |
| 538 | printf("NO MEM\n"); |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 539 | return ENOMEM; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 540 | } |
| 541 | len = size > tbuf_size ? tbuf_size : size; |
| 542 | |
| 543 | tmp = offp; |
| 544 | off = do_div(tmp, vol->usable_leb_size); |
| 545 | lnum = tmp; |
Holger Dengler | a14912b | 2017-08-30 18:38:52 +0200 | [diff] [blame] | 546 | len_read = size; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 547 | do { |
| 548 | if (off + len >= vol->usable_leb_size) |
| 549 | len = vol->usable_leb_size - off; |
| 550 | |
| 551 | err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0); |
| 552 | if (err) { |
| 553 | printf("read err %x\n", err); |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 554 | err = -err; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 555 | break; |
| 556 | } |
| 557 | off += len; |
| 558 | if (off == vol->usable_leb_size) { |
| 559 | lnum += 1; |
| 560 | off -= vol->usable_leb_size; |
| 561 | } |
| 562 | |
| 563 | size -= len; |
| 564 | offp += len; |
| 565 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 566 | memcpy(buf, tbuf, len); |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 567 | |
| 568 | buf += len; |
| 569 | len = size > tbuf_size ? tbuf_size : size; |
| 570 | } while (size); |
| 571 | |
Holger Dengler | a14912b | 2017-08-30 18:38:52 +0200 | [diff] [blame] | 572 | if (!size) |
| 573 | env_set_hex("filesize", len_read); |
| 574 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 575 | free(tbuf); |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 576 | return err; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 577 | } |
| 578 | |
Miquel Raynal | 12f1ff1 | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 579 | static int ubi_dev_scan(struct mtd_info *info, const char *vid_header_offset) |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 580 | { |
Simon Kagstrom | 4a8fbfb | 2009-07-07 16:59:46 +0200 | [diff] [blame] | 581 | char ubi_mtd_param_buffer[80]; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 582 | int err; |
| 583 | |
Miquel Raynal | 12f1ff1 | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 584 | if (!vid_header_offset) |
| 585 | sprintf(ubi_mtd_param_buffer, "%s", info->name); |
| 586 | else |
| 587 | sprintf(ubi_mtd_param_buffer, "%s,%s", info->name, |
| 588 | vid_header_offset); |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 589 | |
Simon Kagstrom | 4a8fbfb | 2009-07-07 16:59:46 +0200 | [diff] [blame] | 590 | err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL); |
Miquel Raynal | 12f1ff1 | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 591 | if (err) |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 592 | return -err; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 593 | |
| 594 | err = ubi_init(); |
Miquel Raynal | 12f1ff1 | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 595 | if (err) |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 596 | return -err; |
Stefan Roese | 1b20eb8 | 2008-11-27 14:07:09 +0100 | [diff] [blame] | 597 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 598 | return 0; |
| 599 | } |
| 600 | |
Stefan Roese | 9ea7786 | 2019-09-17 09:17:53 +0200 | [diff] [blame] | 601 | static int ubi_set_skip_check(char *volume, bool skip_check) |
| 602 | { |
| 603 | struct ubi_vtbl_record vtbl_rec; |
| 604 | struct ubi_volume *vol; |
| 605 | |
| 606 | vol = ubi_find_volume(volume); |
| 607 | if (!vol) |
| 608 | return ENODEV; |
| 609 | |
| 610 | printf("%sing skip_check on volume %s\n", |
| 611 | skip_check ? "Sett" : "Clear", volume); |
| 612 | |
| 613 | vtbl_rec = ubi->vtbl[vol->vol_id]; |
| 614 | if (skip_check) { |
| 615 | vtbl_rec.flags |= UBI_VTBL_SKIP_CRC_CHECK_FLG; |
| 616 | vol->skip_check = 1; |
| 617 | } else { |
| 618 | vtbl_rec.flags &= ~UBI_VTBL_SKIP_CRC_CHECK_FLG; |
| 619 | vol->skip_check = 0; |
| 620 | } |
| 621 | |
| 622 | return ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec); |
| 623 | } |
| 624 | |
Stefan Roese | 3d8686b | 2018-10-31 12:37:20 +0100 | [diff] [blame] | 625 | static int ubi_detach(void) |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 626 | { |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 627 | #ifdef CONFIG_CMD_UBIFS |
| 628 | /* |
| 629 | * Automatically unmount UBIFS partition when user |
| 630 | * changes the UBI device. Otherwise the following |
| 631 | * UBIFS commands will crash. |
| 632 | */ |
| 633 | if (ubifs_is_mounted()) |
| 634 | cmd_ubifs_umount(); |
| 635 | #endif |
| 636 | |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 637 | /* |
| 638 | * Call ubi_exit() before re-initializing the UBI subsystem |
| 639 | */ |
Miquel Raynal | 12f1ff1 | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 640 | if (ubi) |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 641 | ubi_exit(); |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 642 | |
Miquel Raynal | 12f1ff1 | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 643 | ubi = NULL; |
| 644 | |
Heiko Schocher | 8b4b05f | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | int ubi_part(char *part_name, const char *vid_header_offset) |
| 649 | { |
Miquel Raynal | 12f1ff1 | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 650 | struct mtd_info *mtd; |
Martin Kurbanov | fb42f60 | 2024-06-16 16:34:17 +0300 | [diff] [blame] | 651 | int err; |
Heiko Schocher | 8b4b05f | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 652 | |
Alexandre Besnard | b009677 | 2022-04-04 17:50:14 +0200 | [diff] [blame] | 653 | if (ubi && ubi->mtd && !strcmp(ubi->mtd->name, part_name)) { |
| 654 | printf("UBI partition '%s' already selected\n", part_name); |
| 655 | return 0; |
| 656 | } |
| 657 | |
Heiko Schocher | 8b4b05f | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 658 | ubi_detach(); |
Miquel Raynal | 12f1ff1 | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 659 | |
| 660 | mtd_probe_devices(); |
| 661 | mtd = get_mtd_device_nm(part_name); |
| 662 | if (IS_ERR(mtd)) { |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 663 | printf("Partition %s not found!\n", part_name); |
| 664 | return 1; |
| 665 | } |
Miquel Raynal | 12f1ff1 | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 666 | put_mtd_device(mtd); |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 667 | |
Miquel Raynal | 12f1ff1 | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 668 | err = ubi_dev_scan(mtd, vid_header_offset); |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 669 | if (err) { |
| 670 | printf("UBI init error %d\n", err); |
Stefan Roese | 8a717d5 | 2018-06-26 08:12:32 +0200 | [diff] [blame] | 671 | printf("Please check, if the correct MTD partition is used (size big enough?)\n"); |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 672 | return err; |
| 673 | } |
| 674 | |
| 675 | ubi = ubi_devices[0]; |
| 676 | |
| 677 | return 0; |
| 678 | } |
| 679 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 680 | static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 681 | { |
Martin Kurbanov | fb42f60 | 2024-06-16 16:34:17 +0300 | [diff] [blame] | 682 | int64_t size; |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 683 | ulong addr = 0; |
Quentin Schulz | a88049b | 2019-09-12 16:41:01 +0200 | [diff] [blame] | 684 | bool skipcheck = false; |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 685 | |
| 686 | if (argc < 2) |
| 687 | return CMD_RET_USAGE; |
Andreas Huber | 7bf15f8 | 2009-04-02 17:15:34 +0200 | [diff] [blame] | 688 | |
Heinrich Schuchardt | e7bfcff | 2019-01-06 12:26:28 +0100 | [diff] [blame] | 689 | if (strcmp(argv[1], "detach") == 0) |
Heiko Schocher | 8b4b05f | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 690 | return ubi_detach(); |
Heiko Schocher | 8b4b05f | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 691 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 692 | if (strcmp(argv[1], "part") == 0) { |
Simon Kagstrom | 4a8fbfb | 2009-07-07 16:59:46 +0200 | [diff] [blame] | 693 | const char *vid_header_offset = NULL; |
Stefan Roese | 62eafb7 | 2009-04-24 20:24:19 +0200 | [diff] [blame] | 694 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 695 | /* Print current partition */ |
| 696 | if (argc == 2) { |
Miquel Raynal | 12f1ff1 | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 697 | if (!ubi) { |
| 698 | printf("Error, no UBI device selected!\n"); |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 699 | return 1; |
| 700 | } |
| 701 | |
Miquel Raynal | 12f1ff1 | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 702 | printf("Device %d: %s, MTD partition %s\n", |
| 703 | ubi->ubi_num, ubi->ubi_name, ubi->mtd->name); |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 704 | return 0; |
| 705 | } |
| 706 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 707 | if (argc < 3) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 708 | return CMD_RET_USAGE; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 709 | |
Simon Kagstrom | 4a8fbfb | 2009-07-07 16:59:46 +0200 | [diff] [blame] | 710 | if (argc > 3) |
| 711 | vid_header_offset = argv[3]; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 712 | |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 713 | return ubi_part(argv[2], vid_header_offset); |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 714 | } |
| 715 | |
Miquel Raynal | 12f1ff1 | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 716 | if ((strcmp(argv[1], "part") != 0) && !ubi) { |
| 717 | printf("Error, no UBI device selected!\n"); |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 718 | return 1; |
| 719 | } |
| 720 | |
| 721 | if (strcmp(argv[1], "info") == 0) { |
| 722 | int layout = 0; |
| 723 | if (argc > 2 && !strncmp(argv[2], "l", 1)) |
| 724 | layout = 1; |
| 725 | return ubi_info(layout); |
| 726 | } |
| 727 | |
Pali Rohár | 9395eb9 | 2022-09-12 11:38:55 +0200 | [diff] [blame] | 728 | if (strcmp(argv[1], "list") == 0) { |
| 729 | int numeric = 0; |
Dmitry Dunaev | 3c72eca | 2023-07-12 15:58:21 +0300 | [diff] [blame] | 730 | if (argc >= 3 && argv[2][0] == '-') { |
Pali Rohár | 9395eb9 | 2022-09-12 11:38:55 +0200 | [diff] [blame] | 731 | if (strcmp(argv[2], "-numeric") == 0) |
| 732 | numeric = 1; |
| 733 | else |
| 734 | return CMD_RET_USAGE; |
| 735 | } |
| 736 | if (!numeric && argc != 2 && argc != 3) |
| 737 | return CMD_RET_USAGE; |
| 738 | if (numeric && argc != 3 && argc != 4) |
| 739 | return CMD_RET_USAGE; |
| 740 | return ubi_list(argv[numeric ? 3 : 2], numeric); |
| 741 | } |
| 742 | |
Heiko Schocher | 1762ab1 | 2014-01-25 07:27:11 +0100 | [diff] [blame] | 743 | if (strcmp(argv[1], "check") == 0) { |
| 744 | if (argc > 2) |
| 745 | return ubi_check(argv[2]); |
| 746 | |
| 747 | printf("Error, no volume name passed\n"); |
| 748 | return 1; |
| 749 | } |
| 750 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 751 | if (strncmp(argv[1], "create", 6) == 0) { |
| 752 | int dynamic = 1; /* default: dynamic volume */ |
Ladislav Michl | 39370f1 | 2016-09-13 07:14:01 +0200 | [diff] [blame] | 753 | int id = UBI_VOL_NUM_AUTO; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 754 | |
| 755 | /* Use maximum available size */ |
| 756 | size = 0; |
| 757 | |
Quentin Schulz | a88049b | 2019-09-12 16:41:01 +0200 | [diff] [blame] | 758 | /* E.g., create volume with "skipcheck" bit set */ |
| 759 | if (argc == 7) { |
| 760 | skipcheck = strncmp(argv[6], "--skipcheck", 11) == 0; |
| 761 | argc--; |
| 762 | } |
| 763 | |
Ladislav Michl | 39370f1 | 2016-09-13 07:14:01 +0200 | [diff] [blame] | 764 | /* E.g., create volume size type vol_id */ |
| 765 | if (argc == 6) { |
| 766 | id = simple_strtoull(argv[5], NULL, 16); |
| 767 | argc--; |
| 768 | } |
| 769 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 770 | /* E.g., create volume size type */ |
| 771 | if (argc == 5) { |
| 772 | if (strncmp(argv[4], "s", 1) == 0) |
| 773 | dynamic = 0; |
| 774 | else if (strncmp(argv[4], "d", 1) != 0) { |
| 775 | printf("Incorrect type\n"); |
| 776 | return 1; |
| 777 | } |
| 778 | argc--; |
| 779 | } |
| 780 | /* E.g., create volume size */ |
| 781 | if (argc == 4) { |
Ladislav Michl | 4fb937b | 2017-01-19 11:45:35 +0100 | [diff] [blame] | 782 | if (argv[3][0] != '-') |
| 783 | size = simple_strtoull(argv[3], NULL, 16); |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 784 | argc--; |
| 785 | } |
| 786 | /* Use maximum available size */ |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 787 | if (!size) { |
Paul Burton | df68616 | 2013-09-04 15:16:58 +0100 | [diff] [blame] | 788 | size = (int64_t)ubi->avail_pebs * ubi->leb_size; |
| 789 | printf("No size specified -> Using max size (%lld)\n", size); |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 790 | } |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 791 | /* E.g., create volume */ |
Quentin Schulz | a88049b | 2019-09-12 16:41:01 +0200 | [diff] [blame] | 792 | if (argc == 3) { |
| 793 | return ubi_create_vol(argv[2], size, dynamic, id, |
| 794 | skipcheck); |
| 795 | } |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | if (strncmp(argv[1], "remove", 6) == 0) { |
| 799 | /* E.g., remove volume */ |
| 800 | if (argc == 3) |
| 801 | return ubi_remove_vol(argv[2]); |
| 802 | } |
| 803 | |
Philippe Reynes | 10d5322 | 2020-03-23 19:20:47 +0100 | [diff] [blame] | 804 | if (IS_ENABLED(CONFIG_CMD_UBI_RENAME) && !strncmp(argv[1], "rename", 6)) |
| 805 | return ubi_rename_vol(argv[2], argv[3]); |
| 806 | |
Stefan Roese | 9ea7786 | 2019-09-17 09:17:53 +0200 | [diff] [blame] | 807 | if (strncmp(argv[1], "skipcheck", 9) == 0) { |
| 808 | /* E.g., change skip_check flag */ |
| 809 | if (argc == 4) { |
| 810 | skipcheck = strncmp(argv[3], "on", 2) == 0; |
| 811 | return ubi_set_skip_check(argv[2], skipcheck); |
| 812 | } |
| 813 | } |
| 814 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 815 | if (strncmp(argv[1], "write", 5) == 0) { |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 816 | int ret; |
| 817 | |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 818 | if (argc < 5) { |
| 819 | printf("Please see usage\n"); |
| 820 | return 1; |
| 821 | } |
| 822 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 823 | addr = hextoul(argv[2], NULL); |
| 824 | size = hextoul(argv[4], NULL); |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 825 | |
Paul Burton | 6627aa3 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 826 | if (strlen(argv[1]) == 10 && |
| 827 | strncmp(argv[1] + 5, ".part", 5) == 0) { |
| 828 | if (argc < 6) { |
| 829 | ret = ubi_volume_continue_write(argv[3], |
| 830 | (void *)addr, size); |
| 831 | } else { |
| 832 | size_t full_size; |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 833 | full_size = hextoul(argv[5], NULL); |
Paul Burton | 6627aa3 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 834 | ret = ubi_volume_begin_write(argv[3], |
| 835 | (void *)addr, size, full_size); |
| 836 | } |
| 837 | } else { |
Alexey Romanov | de417e9 | 2024-07-18 08:45:24 +0300 | [diff] [blame^] | 838 | ret = ubi_volume_write(argv[3], (void *)addr, 0, size); |
Paul Burton | 6627aa3 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 839 | } |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 840 | if (!ret) { |
Paul Burton | df68616 | 2013-09-04 15:16:58 +0100 | [diff] [blame] | 841 | printf("%lld bytes written to volume %s\n", size, |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 842 | argv[3]); |
| 843 | } |
| 844 | |
| 845 | return ret; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | if (strncmp(argv[1], "read", 4) == 0) { |
| 849 | size = 0; |
| 850 | |
| 851 | /* E.g., read volume size */ |
| 852 | if (argc == 5) { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 853 | size = hextoul(argv[4], NULL); |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 854 | argc--; |
| 855 | } |
| 856 | |
| 857 | /* E.g., read volume */ |
| 858 | if (argc == 4) { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 859 | addr = hextoul(argv[2], NULL); |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 860 | argc--; |
| 861 | } |
| 862 | |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 863 | if (argc == 3) { |
Alexey Romanov | 0043a97 | 2024-07-18 08:45:23 +0300 | [diff] [blame] | 864 | return ubi_volume_read(argv[3], (char *)addr, 0, size); |
Joe Hershberger | 9219845 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 865 | } |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | printf("Please see usage\n"); |
Stefan Roese | 8cfc696 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 869 | return 1; |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 870 | } |
| 871 | |
Frans Meulenbroeks | 7675a09 | 2010-07-31 15:01:53 +0200 | [diff] [blame] | 872 | U_BOOT_CMD( |
Quentin Schulz | a88049b | 2019-09-12 16:41:01 +0200 | [diff] [blame] | 873 | ubi, 7, 1, do_ubi, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 874 | "ubi commands", |
Heiko Schocher | 8b4b05f | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 875 | "detach" |
| 876 | " - detach ubi from a mtd partition\n" |
| 877 | "ubi part [part] [offset]\n" |
Simon Kagstrom | 4a8fbfb | 2009-07-07 16:59:46 +0200 | [diff] [blame] | 878 | " - Show or set current partition (with optional VID" |
| 879 | " header offset)\n" |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 880 | "ubi info [l[ayout]]" |
| 881 | " - Display volume and ubi layout information\n" |
Pali Rohár | 9395eb9 | 2022-09-12 11:38:55 +0200 | [diff] [blame] | 882 | "ubi list [flags]" |
| 883 | " - print the list of volumes\n" |
| 884 | "ubi list [flags] <varname>" |
| 885 | " - set environment variable to the list of volumes" |
| 886 | " (flags can be -numeric)\n" |
Heiko Schocher | 1762ab1 | 2014-01-25 07:27:11 +0100 | [diff] [blame] | 887 | "ubi check volumename" |
| 888 | " - check if volumename exists\n" |
Quentin Schulz | a88049b | 2019-09-12 16:41:01 +0200 | [diff] [blame] | 889 | "ubi create[vol] volume [size] [type] [id] [--skipcheck]\n" |
Ladislav Michl | 4fb937b | 2017-01-19 11:45:35 +0100 | [diff] [blame] | 890 | " - create volume name with size ('-' for maximum" |
| 891 | " available size)\n" |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 892 | "ubi write[vol] address volume size" |
| 893 | " - Write volume from address with size\n" |
Paul Burton | 6627aa3 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 894 | "ubi write.part address volume size [fullsize]\n" |
| 895 | " - Write part of a volume from address\n" |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 896 | "ubi read[vol] address volume [size]" |
| 897 | " - Read volume to address with size\n" |
| 898 | "ubi remove[vol] volume" |
| 899 | " - Remove volume\n" |
Philippe Reynes | 10d5322 | 2020-03-23 19:20:47 +0100 | [diff] [blame] | 900 | #if IS_ENABLED(CONFIG_CMD_UBI_RENAME) |
| 901 | "ubi rename oldname newname\n" |
| 902 | #endif |
Stefan Roese | 9ea7786 | 2019-09-17 09:17:53 +0200 | [diff] [blame] | 903 | "ubi skipcheck volume on/off - Set or clear skip_check flag in volume header\n" |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 904 | "[Legends]\n" |
Andrzej Wolski | 7dcc170 | 2009-07-17 22:26:54 +0200 | [diff] [blame] | 905 | " volume: character name\n" |
| 906 | " size: specified in bytes\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 907 | " type: s[tatic] or d[ynamic] (default=dynamic)" |
Kyungmin Park | 3d2c5ff | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 908 | ); |