Kyungmin Park | 694a0b3 | 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 | 2d579e5 | 2009-04-24 20:24:19 +0200 | [diff] [blame] | 7 | * Copyright 2008-2009 Stefan Roese <sr@denx.de>, DENX Software Engineering |
Kyungmin Park | 694a0b3 | 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 | |
| 14 | #include <common.h> |
| 15 | #include <command.h> |
Simon Glass | c7694dd | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 16 | #include <env.h> |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 17 | #include <exports.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 18 | #include <malloc.h> |
Simon Glass | 6e29518 | 2015-09-02 17:24:57 -0600 | [diff] [blame] | 19 | #include <memalign.h> |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 20 | #include <mtd.h> |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 21 | #include <nand.h> |
| 22 | #include <onenand_uboot.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 23 | #include <dm/devres.h> |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 24 | #include <linux/mtd/mtd.h> |
| 25 | #include <linux/mtd/partitions.h> |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 26 | #include <linux/err.h> |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 27 | #include <ubi_uboot.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 28 | #include <linux/errno.h> |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 29 | #include <jffs2/load_kernel.h> |
Pali Rohár | 6de1daf | 2022-09-12 11:38:55 +0200 | [diff] [blame] | 30 | #include <linux/log2.h> |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 31 | |
Joe Hershberger | 147162d | 2013-04-08 10:32:49 +0000 | [diff] [blame] | 32 | #undef ubi_msg |
| 33 | #define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__) |
| 34 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 35 | /* Private own data */ |
| 36 | static struct ubi_device *ubi; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 37 | |
Stefan Roese | 2f15cfd | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 38 | #ifdef CONFIG_CMD_UBIFS |
Tien Fong Chee | 10c2044 | 2018-07-06 16:25:12 +0800 | [diff] [blame] | 39 | #include <ubifs_uboot.h> |
Stefan Roese | 2f15cfd | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 40 | #endif |
| 41 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 42 | static void display_volume_info(struct ubi_device *ubi) |
| 43 | { |
| 44 | int i; |
| 45 | |
| 46 | for (i = 0; i < (ubi->vtbl_slots + 1); i++) { |
| 47 | if (!ubi->volumes[i]) |
| 48 | continue; /* Empty record */ |
| 49 | ubi_dump_vol_info(ubi->volumes[i]); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | static void display_ubi_info(struct ubi_device *ubi) |
| 54 | { |
| 55 | ubi_msg("MTD device name: \"%s\"", ubi->mtd->name); |
| 56 | ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20); |
| 57 | ubi_msg("physical eraseblock size: %d bytes (%d KiB)", |
| 58 | ubi->peb_size, ubi->peb_size >> 10); |
| 59 | ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size); |
| 60 | ubi_msg("number of good PEBs: %d", ubi->good_peb_count); |
| 61 | ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count); |
| 62 | ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size); |
| 63 | ubi_msg("VID header offset: %d (aligned %d)", |
| 64 | ubi->vid_hdr_offset, ubi->vid_hdr_aloffset); |
| 65 | ubi_msg("data offset: %d", ubi->leb_start); |
| 66 | ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots); |
| 67 | ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD); |
| 68 | ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT); |
| 69 | ubi_msg("number of user volumes: %d", |
| 70 | ubi->vol_count - UBI_INT_VOL_COUNT); |
| 71 | ubi_msg("available PEBs: %d", ubi->avail_pebs); |
| 72 | ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs); |
| 73 | ubi_msg("number of PEBs reserved for bad PEB handling: %d", |
| 74 | ubi->beb_rsvd_pebs); |
| 75 | ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec); |
| 76 | } |
| 77 | |
| 78 | static int ubi_info(int layout) |
| 79 | { |
| 80 | if (layout) |
| 81 | display_volume_info(ubi); |
| 82 | else |
| 83 | display_ubi_info(ubi); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Pali Rohár | 6de1daf | 2022-09-12 11:38:55 +0200 | [diff] [blame] | 88 | static int ubi_list(const char *var, int numeric) |
| 89 | { |
| 90 | size_t namelen, len, size; |
| 91 | char *str, *str2; |
| 92 | int i; |
| 93 | |
| 94 | if (!var) { |
| 95 | for (i = 0; i < (ubi->vtbl_slots + 1); i++) { |
| 96 | if (!ubi->volumes[i]) |
| 97 | continue; |
| 98 | if (ubi->volumes[i]->vol_id >= UBI_INTERNAL_VOL_START) |
| 99 | continue; |
| 100 | printf("%d: %s\n", |
| 101 | ubi->volumes[i]->vol_id, |
| 102 | ubi->volumes[i]->name); |
| 103 | } |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | len = 0; |
| 108 | size = 16; |
| 109 | str = malloc(size); |
| 110 | if (!str) |
| 111 | return 1; |
| 112 | |
| 113 | for (i = 0; i < (ubi->vtbl_slots + 1); i++) { |
| 114 | if (!ubi->volumes[i]) |
| 115 | continue; |
| 116 | if (ubi->volumes[i]->vol_id >= UBI_INTERNAL_VOL_START) |
| 117 | continue; |
| 118 | |
| 119 | if (numeric) |
| 120 | namelen = 10; /* strlen(stringify(INT_MAX)) */ |
| 121 | else |
| 122 | namelen = strlen(ubi->volumes[i]->name); |
| 123 | |
| 124 | if (len + namelen + 1 > size) { |
| 125 | size = roundup_pow_of_two(len + namelen + 1) * 2; |
| 126 | str2 = realloc(str, size); |
| 127 | if (!str2) { |
| 128 | free(str); |
| 129 | return 1; |
| 130 | } |
| 131 | str = str2; |
| 132 | } |
| 133 | |
| 134 | if (len) |
| 135 | str[len++] = ' '; |
| 136 | |
| 137 | if (numeric) { |
| 138 | len += sprintf(str + len, "%d", ubi->volumes[i]->vol_id) + 1; |
| 139 | } else { |
| 140 | memcpy(str + len, ubi->volumes[i]->name, namelen); |
| 141 | len += namelen; |
| 142 | str[len] = 0; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | env_set(var, str); |
| 147 | free(str); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
Heiko Schocher | f9f4d80 | 2014-01-25 07:27:11 +0100 | [diff] [blame] | 152 | static int ubi_check_volumename(const struct ubi_volume *vol, char *name) |
| 153 | { |
| 154 | return strcmp(vol->name, name); |
| 155 | } |
| 156 | |
| 157 | static int ubi_check(char *name) |
| 158 | { |
| 159 | int i; |
| 160 | |
| 161 | for (i = 0; i < (ubi->vtbl_slots + 1); i++) { |
| 162 | if (!ubi->volumes[i]) |
| 163 | continue; /* Empty record */ |
| 164 | |
| 165 | if (!ubi_check_volumename(ubi->volumes[i], name)) |
| 166 | return 0; |
| 167 | } |
| 168 | |
Stefan Agner | 6d0f452 | 2015-04-10 11:25:43 +0200 | [diff] [blame] | 169 | return 1; |
Heiko Schocher | f9f4d80 | 2014-01-25 07:27:11 +0100 | [diff] [blame] | 170 | } |
| 171 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 172 | static int verify_mkvol_req(const struct ubi_device *ubi, |
| 173 | const struct ubi_mkvol_req *req) |
| 174 | { |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 175 | int n, err = EINVAL; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 176 | |
| 177 | if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 || |
| 178 | req->name_len < 0) |
| 179 | goto bad; |
| 180 | |
| 181 | if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) && |
| 182 | req->vol_id != UBI_VOL_NUM_AUTO) |
| 183 | goto bad; |
| 184 | |
| 185 | if (req->alignment == 0) |
| 186 | goto bad; |
| 187 | |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 188 | if (req->bytes == 0) { |
| 189 | printf("No space left in UBI device!\n"); |
| 190 | err = ENOMEM; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 191 | goto bad; |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 192 | } |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 193 | |
| 194 | if (req->vol_type != UBI_DYNAMIC_VOLUME && |
| 195 | req->vol_type != UBI_STATIC_VOLUME) |
| 196 | goto bad; |
| 197 | |
| 198 | if (req->alignment > ubi->leb_size) |
| 199 | goto bad; |
| 200 | |
| 201 | n = req->alignment % ubi->min_io_size; |
| 202 | if (req->alignment != 1 && n) |
| 203 | goto bad; |
| 204 | |
| 205 | if (req->name_len > UBI_VOL_NAME_MAX) { |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 206 | printf("Name too long!\n"); |
| 207 | err = ENAMETOOLONG; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 208 | goto bad; |
| 209 | } |
| 210 | |
| 211 | return 0; |
| 212 | bad: |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 213 | return err; |
| 214 | } |
| 215 | |
Quentin Schulz | 386f20c | 2019-09-12 16:41:01 +0200 | [diff] [blame] | 216 | static int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id, |
| 217 | bool skipcheck) |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 218 | { |
| 219 | struct ubi_mkvol_req req; |
| 220 | int err; |
| 221 | |
| 222 | if (dynamic) |
| 223 | req.vol_type = UBI_DYNAMIC_VOLUME; |
| 224 | else |
| 225 | req.vol_type = UBI_STATIC_VOLUME; |
| 226 | |
Ladislav Michl | 0061242 | 2016-09-13 07:14:01 +0200 | [diff] [blame] | 227 | req.vol_id = vol_id; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 228 | req.alignment = 1; |
| 229 | req.bytes = size; |
| 230 | |
| 231 | strcpy(req.name, volume); |
| 232 | req.name_len = strlen(volume); |
| 233 | req.name[req.name_len] = '\0'; |
Quentin Schulz | 386f20c | 2019-09-12 16:41:01 +0200 | [diff] [blame] | 234 | req.flags = 0; |
| 235 | if (skipcheck) |
| 236 | req.flags |= UBI_VOL_SKIP_CRC_CHECK_FLG; |
| 237 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 238 | /* It's duplicated at drivers/mtd/ubi/cdev.c */ |
| 239 | err = verify_mkvol_req(ubi, &req); |
| 240 | if (err) { |
| 241 | printf("verify_mkvol_req failed %d\n", err); |
| 242 | return err; |
| 243 | } |
Paul Burton | dd7185f | 2013-09-04 15:16:58 +0100 | [diff] [blame] | 244 | printf("Creating %s volume %s of size %lld\n", |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 245 | dynamic ? "dynamic" : "static", volume, size); |
| 246 | /* Call real ubi create volume */ |
| 247 | return ubi_create_volume(ubi, &req); |
| 248 | } |
| 249 | |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 250 | static struct ubi_volume *ubi_find_volume(char *volume) |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 251 | { |
Peter Tyser | 3b653fd | 2010-04-04 22:40:50 -0500 | [diff] [blame] | 252 | struct ubi_volume *vol = NULL; |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 253 | int i; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 254 | |
| 255 | for (i = 0; i < ubi->vtbl_slots; i++) { |
| 256 | vol = ubi->volumes[i]; |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 257 | if (vol && !strcmp(vol->name, volume)) |
| 258 | return vol; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 259 | } |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 260 | |
| 261 | printf("Volume %s not found!\n", volume); |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | static int ubi_remove_vol(char *volume) |
| 266 | { |
| 267 | int err, reserved_pebs, i; |
| 268 | struct ubi_volume *vol; |
| 269 | |
| 270 | vol = ubi_find_volume(volume); |
| 271 | if (vol == NULL) |
| 272 | return ENODEV; |
| 273 | |
| 274 | printf("Remove UBI volume %s (id %d)\n", vol->name, vol->vol_id); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 275 | |
| 276 | if (ubi->ro_mode) { |
| 277 | printf("It's read-only mode\n"); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 278 | err = EROFS; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 279 | goto out_err; |
| 280 | } |
| 281 | |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 282 | err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 283 | if (err) { |
| 284 | printf("Error changing Vol tabel record err=%x\n", err); |
| 285 | goto out_err; |
| 286 | } |
| 287 | reserved_pebs = vol->reserved_pebs; |
| 288 | for (i = 0; i < vol->reserved_pebs; i++) { |
| 289 | err = ubi_eba_unmap_leb(ubi, vol, i); |
| 290 | if (err) |
| 291 | goto out_err; |
| 292 | } |
| 293 | |
| 294 | kfree(vol->eba_tbl); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 295 | ubi->volumes[vol->vol_id]->eba_tbl = NULL; |
| 296 | ubi->volumes[vol->vol_id] = NULL; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 297 | |
| 298 | ubi->rsvd_pebs -= reserved_pebs; |
| 299 | ubi->avail_pebs += reserved_pebs; |
| 300 | i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs; |
| 301 | if (i > 0) { |
| 302 | i = ubi->avail_pebs >= i ? i : ubi->avail_pebs; |
| 303 | ubi->avail_pebs -= i; |
| 304 | ubi->rsvd_pebs += i; |
| 305 | ubi->beb_rsvd_pebs += i; |
| 306 | if (i > 0) |
| 307 | ubi_msg("reserve more %d PEBs", i); |
| 308 | } |
| 309 | ubi->vol_count -= 1; |
| 310 | |
| 311 | return 0; |
| 312 | out_err: |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 313 | ubi_err(ubi, "cannot remove volume %s, error %d", volume, err); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 314 | if (err < 0) |
| 315 | err = -err; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 316 | return err; |
| 317 | } |
| 318 | |
Philippe Reynes | 83f7078 | 2020-03-23 19:20:47 +0100 | [diff] [blame] | 319 | static int ubi_rename_vol(char *oldname, char *newname) |
| 320 | { |
| 321 | struct ubi_volume *vol; |
| 322 | struct ubi_rename_entry rename; |
| 323 | struct ubi_volume_desc desc; |
| 324 | struct list_head list; |
| 325 | |
| 326 | vol = ubi_find_volume(oldname); |
| 327 | if (!vol) { |
| 328 | printf("%s: volume %s doesn't exist\n", __func__, oldname); |
| 329 | return ENODEV; |
| 330 | } |
| 331 | |
Philippe Reynes | 1639988 | 2020-12-23 15:33:07 +0100 | [diff] [blame] | 332 | if (!ubi_check(newname)) { |
| 333 | printf("%s: volume %s already exist\n", __func__, newname); |
| 334 | return EINVAL; |
| 335 | } |
| 336 | |
Philippe Reynes | 83f7078 | 2020-03-23 19:20:47 +0100 | [diff] [blame] | 337 | printf("Rename UBI volume %s to %s\n", oldname, newname); |
| 338 | |
| 339 | if (ubi->ro_mode) { |
| 340 | printf("%s: ubi device is in read-only mode\n", __func__); |
| 341 | return EROFS; |
| 342 | } |
| 343 | |
| 344 | rename.new_name_len = strlen(newname); |
| 345 | strcpy(rename.new_name, newname); |
| 346 | rename.remove = 0; |
| 347 | desc.vol = vol; |
| 348 | desc.mode = 0; |
| 349 | rename.desc = &desc; |
| 350 | INIT_LIST_HEAD(&rename.list); |
| 351 | INIT_LIST_HEAD(&list); |
| 352 | list_add(&rename.list, &list); |
| 353 | |
| 354 | return ubi_rename_volumes(ubi, &list); |
| 355 | } |
| 356 | |
Jeroen Hofstee | 0e350f8 | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 357 | static int ubi_volume_continue_write(char *volume, void *buf, size_t size) |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 358 | { |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 359 | int err = 1; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 360 | struct ubi_volume *vol; |
| 361 | |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 362 | vol = ubi_find_volume(volume); |
| 363 | if (vol == NULL) |
| 364 | return ENODEV; |
| 365 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 366 | err = ubi_more_update_data(ubi, vol, buf, size); |
| 367 | if (err < 0) { |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 368 | printf("Couldnt or partially wrote data\n"); |
| 369 | return -err; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | if (err) { |
| 373 | size = err; |
| 374 | |
| 375 | err = ubi_check_volume(ubi, vol->vol_id); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 376 | if (err < 0) |
| 377 | return -err; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 378 | |
| 379 | if (err) { |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 380 | ubi_warn(ubi, "volume %d on UBI device %d is corrupt", |
| 381 | vol->vol_id, ubi->ubi_num); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 382 | vol->corrupted = 1; |
| 383 | } |
| 384 | |
| 385 | vol->checked = 1; |
| 386 | ubi_gluebi_updated(vol); |
| 387 | } |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
Paul Burton | cc734f5 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 392 | int ubi_volume_begin_write(char *volume, void *buf, size_t size, |
| 393 | size_t full_size) |
| 394 | { |
| 395 | int err = 1; |
| 396 | int rsvd_bytes = 0; |
| 397 | struct ubi_volume *vol; |
| 398 | |
| 399 | vol = ubi_find_volume(volume); |
| 400 | if (vol == NULL) |
| 401 | return ENODEV; |
| 402 | |
| 403 | rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad); |
xypron.glpk@gmx.de | 18f41f2 | 2017-04-15 16:25:25 +0200 | [diff] [blame] | 404 | if (size > rsvd_bytes) { |
Paul Burton | cc734f5 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 405 | printf("size > volume size! Aborting!\n"); |
| 406 | return EINVAL; |
| 407 | } |
| 408 | |
| 409 | err = ubi_start_update(ubi, vol, full_size); |
| 410 | if (err < 0) { |
| 411 | printf("Cannot start volume update\n"); |
| 412 | return -err; |
| 413 | } |
| 414 | |
| 415 | return ubi_volume_continue_write(volume, buf, size); |
| 416 | } |
| 417 | |
| 418 | int ubi_volume_write(char *volume, void *buf, size_t size) |
| 419 | { |
| 420 | return ubi_volume_begin_write(volume, buf, size, size); |
| 421 | } |
| 422 | |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 423 | int ubi_volume_read(char *volume, char *buf, size_t size) |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 424 | { |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 425 | int err, lnum, off, len, tbuf_size; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 426 | void *tbuf; |
| 427 | unsigned long long tmp; |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 428 | struct ubi_volume *vol; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 429 | loff_t offp = 0; |
Holger Dengler | 985fa93 | 2017-08-30 18:38:52 +0200 | [diff] [blame] | 430 | size_t len_read; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 431 | |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 432 | vol = ubi_find_volume(volume); |
| 433 | if (vol == NULL) |
| 434 | return ENODEV; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 435 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 436 | if (vol->updating) { |
| 437 | printf("updating"); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 438 | return EBUSY; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 439 | } |
| 440 | if (vol->upd_marker) { |
| 441 | printf("damaged volume, update marker is set"); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 442 | return EBADF; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 443 | } |
| 444 | if (offp == vol->used_bytes) |
| 445 | return 0; |
| 446 | |
| 447 | if (size == 0) { |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 448 | printf("No size specified -> Using max size (%lld)\n", vol->used_bytes); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 449 | size = vol->used_bytes; |
| 450 | } |
| 451 | |
Tom Rini | 1341533 | 2018-07-26 11:17:24 -0400 | [diff] [blame] | 452 | printf("Read %zu bytes from volume %s to %p\n", size, volume, buf); |
Stefan Agner | 68c7025 | 2018-06-25 11:19:12 +0200 | [diff] [blame] | 453 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 454 | if (vol->corrupted) |
| 455 | printf("read from corrupted volume %d", vol->vol_id); |
| 456 | if (offp + size > vol->used_bytes) |
Marek Vasut | 2984fd1 | 2011-09-30 12:13:25 +0200 | [diff] [blame] | 457 | size = vol->used_bytes - offp; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 458 | |
| 459 | tbuf_size = vol->usable_leb_size; |
| 460 | if (size < tbuf_size) |
| 461 | tbuf_size = ALIGN(size, ubi->min_io_size); |
Marcel Ziswiler | 4519668 | 2015-08-18 13:06:37 +0200 | [diff] [blame] | 462 | tbuf = malloc_cache_aligned(tbuf_size); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 463 | if (!tbuf) { |
| 464 | printf("NO MEM\n"); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 465 | return ENOMEM; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 466 | } |
| 467 | len = size > tbuf_size ? tbuf_size : size; |
| 468 | |
| 469 | tmp = offp; |
| 470 | off = do_div(tmp, vol->usable_leb_size); |
| 471 | lnum = tmp; |
Holger Dengler | 985fa93 | 2017-08-30 18:38:52 +0200 | [diff] [blame] | 472 | len_read = size; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 473 | do { |
| 474 | if (off + len >= vol->usable_leb_size) |
| 475 | len = vol->usable_leb_size - off; |
| 476 | |
| 477 | err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0); |
| 478 | if (err) { |
| 479 | printf("read err %x\n", err); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 480 | err = -err; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 481 | break; |
| 482 | } |
| 483 | off += len; |
| 484 | if (off == vol->usable_leb_size) { |
| 485 | lnum += 1; |
| 486 | off -= vol->usable_leb_size; |
| 487 | } |
| 488 | |
| 489 | size -= len; |
| 490 | offp += len; |
| 491 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 492 | memcpy(buf, tbuf, len); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 493 | |
| 494 | buf += len; |
| 495 | len = size > tbuf_size ? tbuf_size : size; |
| 496 | } while (size); |
| 497 | |
Holger Dengler | 985fa93 | 2017-08-30 18:38:52 +0200 | [diff] [blame] | 498 | if (!size) |
| 499 | env_set_hex("filesize", len_read); |
| 500 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 501 | free(tbuf); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 502 | return err; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 503 | } |
| 504 | |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 505 | static int ubi_dev_scan(struct mtd_info *info, const char *vid_header_offset) |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 506 | { |
Simon Kagstrom | 25c8f40 | 2009-07-07 16:59:46 +0200 | [diff] [blame] | 507 | char ubi_mtd_param_buffer[80]; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 508 | int err; |
| 509 | |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 510 | if (!vid_header_offset) |
| 511 | sprintf(ubi_mtd_param_buffer, "%s", info->name); |
| 512 | else |
| 513 | sprintf(ubi_mtd_param_buffer, "%s,%s", info->name, |
| 514 | vid_header_offset); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 515 | |
Simon Kagstrom | 25c8f40 | 2009-07-07 16:59:46 +0200 | [diff] [blame] | 516 | err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL); |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 517 | if (err) |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 518 | return -err; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 519 | |
| 520 | err = ubi_init(); |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 521 | if (err) |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 522 | return -err; |
Stefan Roese | 2ee951b | 2008-11-27 14:07:09 +0100 | [diff] [blame] | 523 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 524 | return 0; |
| 525 | } |
| 526 | |
Stefan Roese | e6661cf | 2019-09-17 09:17:53 +0200 | [diff] [blame] | 527 | static int ubi_set_skip_check(char *volume, bool skip_check) |
| 528 | { |
| 529 | struct ubi_vtbl_record vtbl_rec; |
| 530 | struct ubi_volume *vol; |
| 531 | |
| 532 | vol = ubi_find_volume(volume); |
| 533 | if (!vol) |
| 534 | return ENODEV; |
| 535 | |
| 536 | printf("%sing skip_check on volume %s\n", |
| 537 | skip_check ? "Sett" : "Clear", volume); |
| 538 | |
| 539 | vtbl_rec = ubi->vtbl[vol->vol_id]; |
| 540 | if (skip_check) { |
| 541 | vtbl_rec.flags |= UBI_VTBL_SKIP_CRC_CHECK_FLG; |
| 542 | vol->skip_check = 1; |
| 543 | } else { |
| 544 | vtbl_rec.flags &= ~UBI_VTBL_SKIP_CRC_CHECK_FLG; |
| 545 | vol->skip_check = 0; |
| 546 | } |
| 547 | |
| 548 | return ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec); |
| 549 | } |
| 550 | |
Stefan Roese | d821e5e | 2018-10-31 12:37:20 +0100 | [diff] [blame] | 551 | static int ubi_detach(void) |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 552 | { |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 553 | #ifdef CONFIG_CMD_UBIFS |
| 554 | /* |
| 555 | * Automatically unmount UBIFS partition when user |
| 556 | * changes the UBI device. Otherwise the following |
| 557 | * UBIFS commands will crash. |
| 558 | */ |
| 559 | if (ubifs_is_mounted()) |
| 560 | cmd_ubifs_umount(); |
| 561 | #endif |
| 562 | |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 563 | /* |
| 564 | * Call ubi_exit() before re-initializing the UBI subsystem |
| 565 | */ |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 566 | if (ubi) |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 567 | ubi_exit(); |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 568 | |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 569 | ubi = NULL; |
| 570 | |
Heiko Schocher | cddfc97 | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | int ubi_part(char *part_name, const char *vid_header_offset) |
| 575 | { |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 576 | struct mtd_info *mtd; |
Heiko Schocher | cddfc97 | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 577 | int err = 0; |
Heiko Schocher | cddfc97 | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 578 | |
Alexandre Besnard | af298f3 | 2022-04-04 17:50:14 +0200 | [diff] [blame] | 579 | if (ubi && ubi->mtd && !strcmp(ubi->mtd->name, part_name)) { |
| 580 | printf("UBI partition '%s' already selected\n", part_name); |
| 581 | return 0; |
| 582 | } |
| 583 | |
Heiko Schocher | cddfc97 | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 584 | ubi_detach(); |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 585 | |
| 586 | mtd_probe_devices(); |
| 587 | mtd = get_mtd_device_nm(part_name); |
| 588 | if (IS_ERR(mtd)) { |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 589 | printf("Partition %s not found!\n", part_name); |
| 590 | return 1; |
| 591 | } |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 592 | put_mtd_device(mtd); |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 593 | |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 594 | err = ubi_dev_scan(mtd, vid_header_offset); |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 595 | if (err) { |
| 596 | printf("UBI init error %d\n", err); |
Stefan Roese | 4a94e53 | 2018-06-26 08:12:32 +0200 | [diff] [blame] | 597 | printf("Please check, if the correct MTD partition is used (size big enough?)\n"); |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 598 | return err; |
| 599 | } |
| 600 | |
| 601 | ubi = ubi_devices[0]; |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 606 | static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 607 | { |
Paul Burton | dd7185f | 2013-09-04 15:16:58 +0100 | [diff] [blame] | 608 | int64_t size = 0; |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 609 | ulong addr = 0; |
Quentin Schulz | 386f20c | 2019-09-12 16:41:01 +0200 | [diff] [blame] | 610 | bool skipcheck = false; |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 611 | |
| 612 | if (argc < 2) |
| 613 | return CMD_RET_USAGE; |
| 614 | |
Heinrich Schuchardt | 7642856 | 2019-01-06 12:26:28 +0100 | [diff] [blame] | 615 | if (strcmp(argv[1], "detach") == 0) |
Heiko Schocher | cddfc97 | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 616 | return ubi_detach(); |
Heiko Schocher | cddfc97 | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 617 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 618 | if (strcmp(argv[1], "part") == 0) { |
Simon Kagstrom | 25c8f40 | 2009-07-07 16:59:46 +0200 | [diff] [blame] | 619 | const char *vid_header_offset = NULL; |
Stefan Roese | 2d579e5 | 2009-04-24 20:24:19 +0200 | [diff] [blame] | 620 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 621 | /* Print current partition */ |
| 622 | if (argc == 2) { |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 623 | if (!ubi) { |
| 624 | printf("Error, no UBI device selected!\n"); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 625 | return 1; |
| 626 | } |
| 627 | |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 628 | printf("Device %d: %s, MTD partition %s\n", |
| 629 | ubi->ubi_num, ubi->ubi_name, ubi->mtd->name); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 630 | return 0; |
| 631 | } |
| 632 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 633 | if (argc < 3) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 634 | return CMD_RET_USAGE; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 635 | |
Simon Kagstrom | 25c8f40 | 2009-07-07 16:59:46 +0200 | [diff] [blame] | 636 | if (argc > 3) |
| 637 | vid_header_offset = argv[3]; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 638 | |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 639 | return ubi_part(argv[2], vid_header_offset); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 640 | } |
| 641 | |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 642 | if ((strcmp(argv[1], "part") != 0) && !ubi) { |
| 643 | printf("Error, no UBI device selected!\n"); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 644 | return 1; |
| 645 | } |
| 646 | |
| 647 | if (strcmp(argv[1], "info") == 0) { |
| 648 | int layout = 0; |
| 649 | if (argc > 2 && !strncmp(argv[2], "l", 1)) |
| 650 | layout = 1; |
| 651 | return ubi_info(layout); |
| 652 | } |
| 653 | |
Pali Rohár | 6de1daf | 2022-09-12 11:38:55 +0200 | [diff] [blame] | 654 | if (strcmp(argv[1], "list") == 0) { |
| 655 | int numeric = 0; |
Dmitry Dunaev | 34031e9 | 2023-07-12 15:58:21 +0300 | [diff] [blame] | 656 | if (argc >= 3 && argv[2][0] == '-') { |
Pali Rohár | 6de1daf | 2022-09-12 11:38:55 +0200 | [diff] [blame] | 657 | if (strcmp(argv[2], "-numeric") == 0) |
| 658 | numeric = 1; |
| 659 | else |
| 660 | return CMD_RET_USAGE; |
| 661 | } |
| 662 | if (!numeric && argc != 2 && argc != 3) |
| 663 | return CMD_RET_USAGE; |
| 664 | if (numeric && argc != 3 && argc != 4) |
| 665 | return CMD_RET_USAGE; |
| 666 | return ubi_list(argv[numeric ? 3 : 2], numeric); |
| 667 | } |
| 668 | |
Heiko Schocher | f9f4d80 | 2014-01-25 07:27:11 +0100 | [diff] [blame] | 669 | if (strcmp(argv[1], "check") == 0) { |
| 670 | if (argc > 2) |
| 671 | return ubi_check(argv[2]); |
| 672 | |
| 673 | printf("Error, no volume name passed\n"); |
| 674 | return 1; |
| 675 | } |
| 676 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 677 | if (strncmp(argv[1], "create", 6) == 0) { |
| 678 | int dynamic = 1; /* default: dynamic volume */ |
Ladislav Michl | 0061242 | 2016-09-13 07:14:01 +0200 | [diff] [blame] | 679 | int id = UBI_VOL_NUM_AUTO; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 680 | |
| 681 | /* Use maximum available size */ |
| 682 | size = 0; |
| 683 | |
Quentin Schulz | 386f20c | 2019-09-12 16:41:01 +0200 | [diff] [blame] | 684 | /* E.g., create volume with "skipcheck" bit set */ |
| 685 | if (argc == 7) { |
| 686 | skipcheck = strncmp(argv[6], "--skipcheck", 11) == 0; |
| 687 | argc--; |
| 688 | } |
| 689 | |
Ladislav Michl | 0061242 | 2016-09-13 07:14:01 +0200 | [diff] [blame] | 690 | /* E.g., create volume size type vol_id */ |
| 691 | if (argc == 6) { |
| 692 | id = simple_strtoull(argv[5], NULL, 16); |
| 693 | argc--; |
| 694 | } |
| 695 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 696 | /* E.g., create volume size type */ |
| 697 | if (argc == 5) { |
| 698 | if (strncmp(argv[4], "s", 1) == 0) |
| 699 | dynamic = 0; |
| 700 | else if (strncmp(argv[4], "d", 1) != 0) { |
| 701 | printf("Incorrect type\n"); |
| 702 | return 1; |
| 703 | } |
| 704 | argc--; |
| 705 | } |
| 706 | /* E.g., create volume size */ |
| 707 | if (argc == 4) { |
Ladislav Michl | f59f07e | 2017-01-19 11:45:35 +0100 | [diff] [blame] | 708 | if (argv[3][0] != '-') |
| 709 | size = simple_strtoull(argv[3], NULL, 16); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 710 | argc--; |
| 711 | } |
| 712 | /* Use maximum available size */ |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 713 | if (!size) { |
Paul Burton | dd7185f | 2013-09-04 15:16:58 +0100 | [diff] [blame] | 714 | size = (int64_t)ubi->avail_pebs * ubi->leb_size; |
| 715 | printf("No size specified -> Using max size (%lld)\n", size); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 716 | } |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 717 | /* E.g., create volume */ |
Quentin Schulz | 386f20c | 2019-09-12 16:41:01 +0200 | [diff] [blame] | 718 | if (argc == 3) { |
| 719 | return ubi_create_vol(argv[2], size, dynamic, id, |
| 720 | skipcheck); |
| 721 | } |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | if (strncmp(argv[1], "remove", 6) == 0) { |
| 725 | /* E.g., remove volume */ |
| 726 | if (argc == 3) |
| 727 | return ubi_remove_vol(argv[2]); |
| 728 | } |
| 729 | |
Philippe Reynes | 83f7078 | 2020-03-23 19:20:47 +0100 | [diff] [blame] | 730 | if (IS_ENABLED(CONFIG_CMD_UBI_RENAME) && !strncmp(argv[1], "rename", 6)) |
| 731 | return ubi_rename_vol(argv[2], argv[3]); |
| 732 | |
Stefan Roese | e6661cf | 2019-09-17 09:17:53 +0200 | [diff] [blame] | 733 | if (strncmp(argv[1], "skipcheck", 9) == 0) { |
| 734 | /* E.g., change skip_check flag */ |
| 735 | if (argc == 4) { |
| 736 | skipcheck = strncmp(argv[3], "on", 2) == 0; |
| 737 | return ubi_set_skip_check(argv[2], skipcheck); |
| 738 | } |
| 739 | } |
| 740 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 741 | if (strncmp(argv[1], "write", 5) == 0) { |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 742 | int ret; |
| 743 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 744 | if (argc < 5) { |
| 745 | printf("Please see usage\n"); |
| 746 | return 1; |
| 747 | } |
| 748 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 749 | addr = hextoul(argv[2], NULL); |
| 750 | size = hextoul(argv[4], NULL); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 751 | |
Paul Burton | cc734f5 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 752 | if (strlen(argv[1]) == 10 && |
| 753 | strncmp(argv[1] + 5, ".part", 5) == 0) { |
| 754 | if (argc < 6) { |
| 755 | ret = ubi_volume_continue_write(argv[3], |
| 756 | (void *)addr, size); |
| 757 | } else { |
| 758 | size_t full_size; |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 759 | full_size = hextoul(argv[5], NULL); |
Paul Burton | cc734f5 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 760 | ret = ubi_volume_begin_write(argv[3], |
| 761 | (void *)addr, size, full_size); |
| 762 | } |
| 763 | } else { |
| 764 | ret = ubi_volume_write(argv[3], (void *)addr, size); |
| 765 | } |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 766 | if (!ret) { |
Paul Burton | dd7185f | 2013-09-04 15:16:58 +0100 | [diff] [blame] | 767 | printf("%lld bytes written to volume %s\n", size, |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 768 | argv[3]); |
| 769 | } |
| 770 | |
| 771 | return ret; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | if (strncmp(argv[1], "read", 4) == 0) { |
| 775 | size = 0; |
| 776 | |
| 777 | /* E.g., read volume size */ |
| 778 | if (argc == 5) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 779 | size = hextoul(argv[4], NULL); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 780 | argc--; |
| 781 | } |
| 782 | |
| 783 | /* E.g., read volume */ |
| 784 | if (argc == 4) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 785 | addr = hextoul(argv[2], NULL); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 786 | argc--; |
| 787 | } |
| 788 | |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 789 | if (argc == 3) { |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 790 | return ubi_volume_read(argv[3], (char *)addr, size); |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 791 | } |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | printf("Please see usage\n"); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 795 | return 1; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 796 | } |
| 797 | |
Frans Meulenbroeks | 388a29d | 2010-07-31 15:01:53 +0200 | [diff] [blame] | 798 | U_BOOT_CMD( |
Quentin Schulz | 386f20c | 2019-09-12 16:41:01 +0200 | [diff] [blame] | 799 | ubi, 7, 1, do_ubi, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 800 | "ubi commands", |
Heiko Schocher | cddfc97 | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 801 | "detach" |
| 802 | " - detach ubi from a mtd partition\n" |
| 803 | "ubi part [part] [offset]\n" |
Simon Kagstrom | 25c8f40 | 2009-07-07 16:59:46 +0200 | [diff] [blame] | 804 | " - Show or set current partition (with optional VID" |
| 805 | " header offset)\n" |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 806 | "ubi info [l[ayout]]" |
| 807 | " - Display volume and ubi layout information\n" |
Pali Rohár | 6de1daf | 2022-09-12 11:38:55 +0200 | [diff] [blame] | 808 | "ubi list [flags]" |
| 809 | " - print the list of volumes\n" |
| 810 | "ubi list [flags] <varname>" |
| 811 | " - set environment variable to the list of volumes" |
| 812 | " (flags can be -numeric)\n" |
Heiko Schocher | f9f4d80 | 2014-01-25 07:27:11 +0100 | [diff] [blame] | 813 | "ubi check volumename" |
| 814 | " - check if volumename exists\n" |
Quentin Schulz | 386f20c | 2019-09-12 16:41:01 +0200 | [diff] [blame] | 815 | "ubi create[vol] volume [size] [type] [id] [--skipcheck]\n" |
Ladislav Michl | f59f07e | 2017-01-19 11:45:35 +0100 | [diff] [blame] | 816 | " - create volume name with size ('-' for maximum" |
| 817 | " available size)\n" |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 818 | "ubi write[vol] address volume size" |
| 819 | " - Write volume from address with size\n" |
Paul Burton | cc734f5 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 820 | "ubi write.part address volume size [fullsize]\n" |
| 821 | " - Write part of a volume from address\n" |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 822 | "ubi read[vol] address volume [size]" |
| 823 | " - Read volume to address with size\n" |
| 824 | "ubi remove[vol] volume" |
| 825 | " - Remove volume\n" |
Philippe Reynes | 83f7078 | 2020-03-23 19:20:47 +0100 | [diff] [blame] | 826 | #if IS_ENABLED(CONFIG_CMD_UBI_RENAME) |
| 827 | "ubi rename oldname newname\n" |
| 828 | #endif |
Stefan Roese | e6661cf | 2019-09-17 09:17:53 +0200 | [diff] [blame] | 829 | "ubi skipcheck volume on/off - Set or clear skip_check flag in volume header\n" |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 830 | "[Legends]\n" |
Andrzej Wolski | f6ca3b7 | 2009-07-17 22:26:54 +0200 | [diff] [blame] | 831 | " volume: character name\n" |
| 832 | " size: specified in bytes\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 833 | " type: s[tatic] or d[ynamic] (default=dynamic)" |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 834 | ); |