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 | 6e29518 | 2015-09-02 17:24:57 -0600 | [diff] [blame] | 18 | #include <memalign.h> |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 19 | #include <mtd.h> |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 20 | #include <nand.h> |
| 21 | #include <onenand_uboot.h> |
| 22 | #include <linux/mtd/mtd.h> |
| 23 | #include <linux/mtd/partitions.h> |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 24 | #include <linux/err.h> |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 25 | #include <ubi_uboot.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 26 | #include <linux/errno.h> |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 27 | #include <jffs2/load_kernel.h> |
| 28 | |
Joe Hershberger | 147162d | 2013-04-08 10:32:49 +0000 | [diff] [blame] | 29 | #undef ubi_msg |
| 30 | #define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__) |
| 31 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 32 | /* Private own data */ |
| 33 | static struct ubi_device *ubi; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 34 | |
Stefan Roese | 2f15cfd | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 35 | #ifdef CONFIG_CMD_UBIFS |
Tien Fong Chee | 10c2044 | 2018-07-06 16:25:12 +0800 | [diff] [blame] | 36 | #include <ubifs_uboot.h> |
Stefan Roese | 2f15cfd | 2010-11-01 17:28:22 +0100 | [diff] [blame] | 37 | #endif |
| 38 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 39 | static void display_volume_info(struct ubi_device *ubi) |
| 40 | { |
| 41 | int i; |
| 42 | |
| 43 | for (i = 0; i < (ubi->vtbl_slots + 1); i++) { |
| 44 | if (!ubi->volumes[i]) |
| 45 | continue; /* Empty record */ |
| 46 | ubi_dump_vol_info(ubi->volumes[i]); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | static void display_ubi_info(struct ubi_device *ubi) |
| 51 | { |
| 52 | ubi_msg("MTD device name: \"%s\"", ubi->mtd->name); |
| 53 | ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20); |
| 54 | ubi_msg("physical eraseblock size: %d bytes (%d KiB)", |
| 55 | ubi->peb_size, ubi->peb_size >> 10); |
| 56 | ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size); |
| 57 | ubi_msg("number of good PEBs: %d", ubi->good_peb_count); |
| 58 | ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count); |
| 59 | ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size); |
| 60 | ubi_msg("VID header offset: %d (aligned %d)", |
| 61 | ubi->vid_hdr_offset, ubi->vid_hdr_aloffset); |
| 62 | ubi_msg("data offset: %d", ubi->leb_start); |
| 63 | ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots); |
| 64 | ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD); |
| 65 | ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT); |
| 66 | ubi_msg("number of user volumes: %d", |
| 67 | ubi->vol_count - UBI_INT_VOL_COUNT); |
| 68 | ubi_msg("available PEBs: %d", ubi->avail_pebs); |
| 69 | ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs); |
| 70 | ubi_msg("number of PEBs reserved for bad PEB handling: %d", |
| 71 | ubi->beb_rsvd_pebs); |
| 72 | ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec); |
| 73 | } |
| 74 | |
| 75 | static int ubi_info(int layout) |
| 76 | { |
| 77 | if (layout) |
| 78 | display_volume_info(ubi); |
| 79 | else |
| 80 | display_ubi_info(ubi); |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
Heiko Schocher | f9f4d80 | 2014-01-25 07:27:11 +0100 | [diff] [blame] | 85 | static int ubi_check_volumename(const struct ubi_volume *vol, char *name) |
| 86 | { |
| 87 | return strcmp(vol->name, name); |
| 88 | } |
| 89 | |
| 90 | static int ubi_check(char *name) |
| 91 | { |
| 92 | int i; |
| 93 | |
| 94 | for (i = 0; i < (ubi->vtbl_slots + 1); i++) { |
| 95 | if (!ubi->volumes[i]) |
| 96 | continue; /* Empty record */ |
| 97 | |
| 98 | if (!ubi_check_volumename(ubi->volumes[i], name)) |
| 99 | return 0; |
| 100 | } |
| 101 | |
Stefan Agner | 6d0f452 | 2015-04-10 11:25:43 +0200 | [diff] [blame] | 102 | return 1; |
Heiko Schocher | f9f4d80 | 2014-01-25 07:27:11 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 105 | static int verify_mkvol_req(const struct ubi_device *ubi, |
| 106 | const struct ubi_mkvol_req *req) |
| 107 | { |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 108 | int n, err = EINVAL; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 109 | |
| 110 | if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 || |
| 111 | req->name_len < 0) |
| 112 | goto bad; |
| 113 | |
| 114 | if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) && |
| 115 | req->vol_id != UBI_VOL_NUM_AUTO) |
| 116 | goto bad; |
| 117 | |
| 118 | if (req->alignment == 0) |
| 119 | goto bad; |
| 120 | |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 121 | if (req->bytes == 0) { |
| 122 | printf("No space left in UBI device!\n"); |
| 123 | err = ENOMEM; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 124 | goto bad; |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 125 | } |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 126 | |
| 127 | if (req->vol_type != UBI_DYNAMIC_VOLUME && |
| 128 | req->vol_type != UBI_STATIC_VOLUME) |
| 129 | goto bad; |
| 130 | |
| 131 | if (req->alignment > ubi->leb_size) |
| 132 | goto bad; |
| 133 | |
| 134 | n = req->alignment % ubi->min_io_size; |
| 135 | if (req->alignment != 1 && n) |
| 136 | goto bad; |
| 137 | |
| 138 | if (req->name_len > UBI_VOL_NAME_MAX) { |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 139 | printf("Name too long!\n"); |
| 140 | err = ENAMETOOLONG; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 141 | goto bad; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | bad: |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 146 | return err; |
| 147 | } |
| 148 | |
Ladislav Michl | 0061242 | 2016-09-13 07:14:01 +0200 | [diff] [blame] | 149 | static int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id) |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 150 | { |
| 151 | struct ubi_mkvol_req req; |
| 152 | int err; |
| 153 | |
| 154 | if (dynamic) |
| 155 | req.vol_type = UBI_DYNAMIC_VOLUME; |
| 156 | else |
| 157 | req.vol_type = UBI_STATIC_VOLUME; |
| 158 | |
Ladislav Michl | 0061242 | 2016-09-13 07:14:01 +0200 | [diff] [blame] | 159 | req.vol_id = vol_id; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 160 | req.alignment = 1; |
| 161 | req.bytes = size; |
| 162 | |
| 163 | strcpy(req.name, volume); |
| 164 | req.name_len = strlen(volume); |
| 165 | req.name[req.name_len] = '\0'; |
| 166 | req.padding1 = 0; |
| 167 | /* It's duplicated at drivers/mtd/ubi/cdev.c */ |
| 168 | err = verify_mkvol_req(ubi, &req); |
| 169 | if (err) { |
| 170 | printf("verify_mkvol_req failed %d\n", err); |
| 171 | return err; |
| 172 | } |
Paul Burton | dd7185f | 2013-09-04 15:16:58 +0100 | [diff] [blame] | 173 | printf("Creating %s volume %s of size %lld\n", |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 174 | dynamic ? "dynamic" : "static", volume, size); |
| 175 | /* Call real ubi create volume */ |
| 176 | return ubi_create_volume(ubi, &req); |
| 177 | } |
| 178 | |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 179 | static struct ubi_volume *ubi_find_volume(char *volume) |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 180 | { |
Peter Tyser | 3b653fd | 2010-04-04 22:40:50 -0500 | [diff] [blame] | 181 | struct ubi_volume *vol = NULL; |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 182 | int i; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 183 | |
| 184 | for (i = 0; i < ubi->vtbl_slots; i++) { |
| 185 | vol = ubi->volumes[i]; |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 186 | if (vol && !strcmp(vol->name, volume)) |
| 187 | return vol; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 188 | } |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 189 | |
| 190 | printf("Volume %s not found!\n", volume); |
| 191 | return NULL; |
| 192 | } |
| 193 | |
| 194 | static int ubi_remove_vol(char *volume) |
| 195 | { |
| 196 | int err, reserved_pebs, i; |
| 197 | struct ubi_volume *vol; |
| 198 | |
| 199 | vol = ubi_find_volume(volume); |
| 200 | if (vol == NULL) |
| 201 | return ENODEV; |
| 202 | |
| 203 | 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] | 204 | |
| 205 | if (ubi->ro_mode) { |
| 206 | printf("It's read-only mode\n"); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 207 | err = EROFS; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 208 | goto out_err; |
| 209 | } |
| 210 | |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 211 | err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 212 | if (err) { |
| 213 | printf("Error changing Vol tabel record err=%x\n", err); |
| 214 | goto out_err; |
| 215 | } |
| 216 | reserved_pebs = vol->reserved_pebs; |
| 217 | for (i = 0; i < vol->reserved_pebs; i++) { |
| 218 | err = ubi_eba_unmap_leb(ubi, vol, i); |
| 219 | if (err) |
| 220 | goto out_err; |
| 221 | } |
| 222 | |
| 223 | kfree(vol->eba_tbl); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 224 | ubi->volumes[vol->vol_id]->eba_tbl = NULL; |
| 225 | ubi->volumes[vol->vol_id] = NULL; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 226 | |
| 227 | ubi->rsvd_pebs -= reserved_pebs; |
| 228 | ubi->avail_pebs += reserved_pebs; |
| 229 | i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs; |
| 230 | if (i > 0) { |
| 231 | i = ubi->avail_pebs >= i ? i : ubi->avail_pebs; |
| 232 | ubi->avail_pebs -= i; |
| 233 | ubi->rsvd_pebs += i; |
| 234 | ubi->beb_rsvd_pebs += i; |
| 235 | if (i > 0) |
| 236 | ubi_msg("reserve more %d PEBs", i); |
| 237 | } |
| 238 | ubi->vol_count -= 1; |
| 239 | |
| 240 | return 0; |
| 241 | out_err: |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 242 | ubi_err(ubi, "cannot remove volume %s, error %d", volume, err); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 243 | if (err < 0) |
| 244 | err = -err; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 245 | return err; |
| 246 | } |
| 247 | |
Jeroen Hofstee | 0e350f8 | 2014-06-23 00:22:08 +0200 | [diff] [blame] | 248 | 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] | 249 | { |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 250 | int err = 1; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 251 | struct ubi_volume *vol; |
| 252 | |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 253 | vol = ubi_find_volume(volume); |
| 254 | if (vol == NULL) |
| 255 | return ENODEV; |
| 256 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 257 | err = ubi_more_update_data(ubi, vol, buf, size); |
| 258 | if (err < 0) { |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 259 | printf("Couldnt or partially wrote data\n"); |
| 260 | return -err; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | if (err) { |
| 264 | size = err; |
| 265 | |
| 266 | err = ubi_check_volume(ubi, vol->vol_id); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 267 | if (err < 0) |
| 268 | return -err; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 269 | |
| 270 | if (err) { |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 271 | ubi_warn(ubi, "volume %d on UBI device %d is corrupt", |
| 272 | vol->vol_id, ubi->ubi_num); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 273 | vol->corrupted = 1; |
| 274 | } |
| 275 | |
| 276 | vol->checked = 1; |
| 277 | ubi_gluebi_updated(vol); |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
Paul Burton | cc734f5 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 283 | int ubi_volume_begin_write(char *volume, void *buf, size_t size, |
| 284 | size_t full_size) |
| 285 | { |
| 286 | int err = 1; |
| 287 | int rsvd_bytes = 0; |
| 288 | struct ubi_volume *vol; |
| 289 | |
| 290 | vol = ubi_find_volume(volume); |
| 291 | if (vol == NULL) |
| 292 | return ENODEV; |
| 293 | |
| 294 | 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] | 295 | if (size > rsvd_bytes) { |
Paul Burton | cc734f5 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 296 | printf("size > volume size! Aborting!\n"); |
| 297 | return EINVAL; |
| 298 | } |
| 299 | |
| 300 | err = ubi_start_update(ubi, vol, full_size); |
| 301 | if (err < 0) { |
| 302 | printf("Cannot start volume update\n"); |
| 303 | return -err; |
| 304 | } |
| 305 | |
| 306 | return ubi_volume_continue_write(volume, buf, size); |
| 307 | } |
| 308 | |
| 309 | int ubi_volume_write(char *volume, void *buf, size_t size) |
| 310 | { |
| 311 | return ubi_volume_begin_write(volume, buf, size, size); |
| 312 | } |
| 313 | |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 314 | int ubi_volume_read(char *volume, char *buf, size_t size) |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 315 | { |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 316 | int err, lnum, off, len, tbuf_size; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 317 | void *tbuf; |
| 318 | unsigned long long tmp; |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 319 | struct ubi_volume *vol; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 320 | loff_t offp = 0; |
Holger Dengler | 985fa93 | 2017-08-30 18:38:52 +0200 | [diff] [blame] | 321 | size_t len_read; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 322 | |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 323 | vol = ubi_find_volume(volume); |
| 324 | if (vol == NULL) |
| 325 | return ENODEV; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 326 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 327 | if (vol->updating) { |
| 328 | printf("updating"); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 329 | return EBUSY; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 330 | } |
| 331 | if (vol->upd_marker) { |
| 332 | printf("damaged volume, update marker is set"); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 333 | return EBADF; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 334 | } |
| 335 | if (offp == vol->used_bytes) |
| 336 | return 0; |
| 337 | |
| 338 | if (size == 0) { |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 339 | printf("No size specified -> Using max size (%lld)\n", vol->used_bytes); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 340 | size = vol->used_bytes; |
| 341 | } |
| 342 | |
Tom Rini | 1341533 | 2018-07-26 11:17:24 -0400 | [diff] [blame] | 343 | 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] | 344 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 345 | if (vol->corrupted) |
| 346 | printf("read from corrupted volume %d", vol->vol_id); |
| 347 | if (offp + size > vol->used_bytes) |
Marek Vasut | 2984fd1 | 2011-09-30 12:13:25 +0200 | [diff] [blame] | 348 | size = vol->used_bytes - offp; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 349 | |
| 350 | tbuf_size = vol->usable_leb_size; |
| 351 | if (size < tbuf_size) |
| 352 | tbuf_size = ALIGN(size, ubi->min_io_size); |
Marcel Ziswiler | 4519668 | 2015-08-18 13:06:37 +0200 | [diff] [blame] | 353 | tbuf = malloc_cache_aligned(tbuf_size); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 354 | if (!tbuf) { |
| 355 | printf("NO MEM\n"); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 356 | return ENOMEM; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 357 | } |
| 358 | len = size > tbuf_size ? tbuf_size : size; |
| 359 | |
| 360 | tmp = offp; |
| 361 | off = do_div(tmp, vol->usable_leb_size); |
| 362 | lnum = tmp; |
Holger Dengler | 985fa93 | 2017-08-30 18:38:52 +0200 | [diff] [blame] | 363 | len_read = size; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 364 | do { |
| 365 | if (off + len >= vol->usable_leb_size) |
| 366 | len = vol->usable_leb_size - off; |
| 367 | |
| 368 | err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0); |
| 369 | if (err) { |
| 370 | printf("read err %x\n", err); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 371 | err = -err; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 372 | break; |
| 373 | } |
| 374 | off += len; |
| 375 | if (off == vol->usable_leb_size) { |
| 376 | lnum += 1; |
| 377 | off -= vol->usable_leb_size; |
| 378 | } |
| 379 | |
| 380 | size -= len; |
| 381 | offp += len; |
| 382 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 383 | memcpy(buf, tbuf, len); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 384 | |
| 385 | buf += len; |
| 386 | len = size > tbuf_size ? tbuf_size : size; |
| 387 | } while (size); |
| 388 | |
Holger Dengler | 985fa93 | 2017-08-30 18:38:52 +0200 | [diff] [blame] | 389 | if (!size) |
| 390 | env_set_hex("filesize", len_read); |
| 391 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 392 | free(tbuf); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 393 | return err; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 394 | } |
| 395 | |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 396 | 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] | 397 | { |
Simon Kagstrom | 25c8f40 | 2009-07-07 16:59:46 +0200 | [diff] [blame] | 398 | char ubi_mtd_param_buffer[80]; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 399 | int err; |
| 400 | |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 401 | if (!vid_header_offset) |
| 402 | sprintf(ubi_mtd_param_buffer, "%s", info->name); |
| 403 | else |
| 404 | sprintf(ubi_mtd_param_buffer, "%s,%s", info->name, |
| 405 | vid_header_offset); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 406 | |
Simon Kagstrom | 25c8f40 | 2009-07-07 16:59:46 +0200 | [diff] [blame] | 407 | err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL); |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 408 | if (err) |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 409 | return -err; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 410 | |
| 411 | err = ubi_init(); |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 412 | if (err) |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 413 | return -err; |
Stefan Roese | 2ee951b | 2008-11-27 14:07:09 +0100 | [diff] [blame] | 414 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
Stefan Roese | d821e5e | 2018-10-31 12:37:20 +0100 | [diff] [blame] | 418 | static int ubi_detach(void) |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 419 | { |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 420 | #ifdef CONFIG_CMD_UBIFS |
| 421 | /* |
| 422 | * Automatically unmount UBIFS partition when user |
| 423 | * changes the UBI device. Otherwise the following |
| 424 | * UBIFS commands will crash. |
| 425 | */ |
| 426 | if (ubifs_is_mounted()) |
| 427 | cmd_ubifs_umount(); |
| 428 | #endif |
| 429 | |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 430 | /* |
| 431 | * Call ubi_exit() before re-initializing the UBI subsystem |
| 432 | */ |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 433 | if (ubi) |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 434 | ubi_exit(); |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 435 | |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 436 | ubi = NULL; |
| 437 | |
Heiko Schocher | cddfc97 | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | int ubi_part(char *part_name, const char *vid_header_offset) |
| 442 | { |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 443 | struct mtd_info *mtd; |
Heiko Schocher | cddfc97 | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 444 | int err = 0; |
Heiko Schocher | cddfc97 | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 445 | |
| 446 | ubi_detach(); |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 447 | |
| 448 | mtd_probe_devices(); |
| 449 | mtd = get_mtd_device_nm(part_name); |
| 450 | if (IS_ERR(mtd)) { |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 451 | printf("Partition %s not found!\n", part_name); |
| 452 | return 1; |
| 453 | } |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 454 | put_mtd_device(mtd); |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 455 | |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 456 | err = ubi_dev_scan(mtd, vid_header_offset); |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 457 | if (err) { |
| 458 | printf("UBI init error %d\n", err); |
Stefan Roese | 4a94e53 | 2018-06-26 08:12:32 +0200 | [diff] [blame] | 459 | 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] | 460 | return err; |
| 461 | } |
| 462 | |
| 463 | ubi = ubi_devices[0]; |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | static int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 469 | { |
Paul Burton | dd7185f | 2013-09-04 15:16:58 +0100 | [diff] [blame] | 470 | int64_t size = 0; |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 471 | ulong addr = 0; |
| 472 | |
| 473 | if (argc < 2) |
| 474 | return CMD_RET_USAGE; |
| 475 | |
Heinrich Schuchardt | 7642856 | 2019-01-06 12:26:28 +0100 | [diff] [blame] | 476 | if (strcmp(argv[1], "detach") == 0) |
Heiko Schocher | cddfc97 | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 477 | return ubi_detach(); |
Heiko Schocher | cddfc97 | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 478 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 479 | if (strcmp(argv[1], "part") == 0) { |
Simon Kagstrom | 25c8f40 | 2009-07-07 16:59:46 +0200 | [diff] [blame] | 480 | const char *vid_header_offset = NULL; |
Stefan Roese | 2d579e5 | 2009-04-24 20:24:19 +0200 | [diff] [blame] | 481 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 482 | /* Print current partition */ |
| 483 | if (argc == 2) { |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 484 | if (!ubi) { |
| 485 | printf("Error, no UBI device selected!\n"); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 486 | return 1; |
| 487 | } |
| 488 | |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 489 | printf("Device %d: %s, MTD partition %s\n", |
| 490 | ubi->ubi_num, ubi->ubi_name, ubi->mtd->name); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 491 | return 0; |
| 492 | } |
| 493 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 494 | if (argc < 3) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 495 | return CMD_RET_USAGE; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 496 | |
Simon Kagstrom | 25c8f40 | 2009-07-07 16:59:46 +0200 | [diff] [blame] | 497 | if (argc > 3) |
| 498 | vid_header_offset = argv[3]; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 499 | |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 500 | return ubi_part(argv[2], vid_header_offset); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 501 | } |
| 502 | |
Miquel Raynal | c58fb2c | 2018-09-29 12:58:29 +0200 | [diff] [blame] | 503 | if ((strcmp(argv[1], "part") != 0) && !ubi) { |
| 504 | printf("Error, no UBI device selected!\n"); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 505 | return 1; |
| 506 | } |
| 507 | |
| 508 | if (strcmp(argv[1], "info") == 0) { |
| 509 | int layout = 0; |
| 510 | if (argc > 2 && !strncmp(argv[2], "l", 1)) |
| 511 | layout = 1; |
| 512 | return ubi_info(layout); |
| 513 | } |
| 514 | |
Heiko Schocher | f9f4d80 | 2014-01-25 07:27:11 +0100 | [diff] [blame] | 515 | if (strcmp(argv[1], "check") == 0) { |
| 516 | if (argc > 2) |
| 517 | return ubi_check(argv[2]); |
| 518 | |
| 519 | printf("Error, no volume name passed\n"); |
| 520 | return 1; |
| 521 | } |
| 522 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 523 | if (strncmp(argv[1], "create", 6) == 0) { |
| 524 | int dynamic = 1; /* default: dynamic volume */ |
Ladislav Michl | 0061242 | 2016-09-13 07:14:01 +0200 | [diff] [blame] | 525 | int id = UBI_VOL_NUM_AUTO; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 526 | |
| 527 | /* Use maximum available size */ |
| 528 | size = 0; |
| 529 | |
Ladislav Michl | 0061242 | 2016-09-13 07:14:01 +0200 | [diff] [blame] | 530 | /* E.g., create volume size type vol_id */ |
| 531 | if (argc == 6) { |
| 532 | id = simple_strtoull(argv[5], NULL, 16); |
| 533 | argc--; |
| 534 | } |
| 535 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 536 | /* E.g., create volume size type */ |
| 537 | if (argc == 5) { |
| 538 | if (strncmp(argv[4], "s", 1) == 0) |
| 539 | dynamic = 0; |
| 540 | else if (strncmp(argv[4], "d", 1) != 0) { |
| 541 | printf("Incorrect type\n"); |
| 542 | return 1; |
| 543 | } |
| 544 | argc--; |
| 545 | } |
| 546 | /* E.g., create volume size */ |
| 547 | if (argc == 4) { |
Ladislav Michl | f59f07e | 2017-01-19 11:45:35 +0100 | [diff] [blame] | 548 | if (argv[3][0] != '-') |
| 549 | size = simple_strtoull(argv[3], NULL, 16); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 550 | argc--; |
| 551 | } |
| 552 | /* Use maximum available size */ |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 553 | if (!size) { |
Paul Burton | dd7185f | 2013-09-04 15:16:58 +0100 | [diff] [blame] | 554 | size = (int64_t)ubi->avail_pebs * ubi->leb_size; |
| 555 | printf("No size specified -> Using max size (%lld)\n", size); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 556 | } |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 557 | /* E.g., create volume */ |
| 558 | if (argc == 3) |
Ladislav Michl | 0061242 | 2016-09-13 07:14:01 +0200 | [diff] [blame] | 559 | return ubi_create_vol(argv[2], size, dynamic, id); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | if (strncmp(argv[1], "remove", 6) == 0) { |
| 563 | /* E.g., remove volume */ |
| 564 | if (argc == 3) |
| 565 | return ubi_remove_vol(argv[2]); |
| 566 | } |
| 567 | |
| 568 | if (strncmp(argv[1], "write", 5) == 0) { |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 569 | int ret; |
| 570 | |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 571 | if (argc < 5) { |
| 572 | printf("Please see usage\n"); |
| 573 | return 1; |
| 574 | } |
| 575 | |
| 576 | addr = simple_strtoul(argv[2], NULL, 16); |
Stefan Roese | a5c4067 | 2008-11-24 08:31:16 +0100 | [diff] [blame] | 577 | size = simple_strtoul(argv[4], NULL, 16); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 578 | |
Paul Burton | cc734f5 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 579 | if (strlen(argv[1]) == 10 && |
| 580 | strncmp(argv[1] + 5, ".part", 5) == 0) { |
| 581 | if (argc < 6) { |
| 582 | ret = ubi_volume_continue_write(argv[3], |
| 583 | (void *)addr, size); |
| 584 | } else { |
| 585 | size_t full_size; |
| 586 | full_size = simple_strtoul(argv[5], NULL, 16); |
| 587 | ret = ubi_volume_begin_write(argv[3], |
| 588 | (void *)addr, size, full_size); |
| 589 | } |
| 590 | } else { |
| 591 | ret = ubi_volume_write(argv[3], (void *)addr, size); |
| 592 | } |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 593 | if (!ret) { |
Paul Burton | dd7185f | 2013-09-04 15:16:58 +0100 | [diff] [blame] | 594 | printf("%lld bytes written to volume %s\n", size, |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 595 | argv[3]); |
| 596 | } |
| 597 | |
| 598 | return ret; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | if (strncmp(argv[1], "read", 4) == 0) { |
| 602 | size = 0; |
| 603 | |
| 604 | /* E.g., read volume size */ |
| 605 | if (argc == 5) { |
Stefan Roese | a5c4067 | 2008-11-24 08:31:16 +0100 | [diff] [blame] | 606 | size = simple_strtoul(argv[4], NULL, 16); |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 607 | argc--; |
| 608 | } |
| 609 | |
| 610 | /* E.g., read volume */ |
| 611 | if (argc == 4) { |
| 612 | addr = simple_strtoul(argv[2], NULL, 16); |
| 613 | argc--; |
| 614 | } |
| 615 | |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 616 | if (argc == 3) { |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 617 | return ubi_volume_read(argv[3], (char *)addr, size); |
Joe Hershberger | 7182906 | 2013-04-08 10:32:47 +0000 | [diff] [blame] | 618 | } |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | printf("Please see usage\n"); |
Stefan Roese | 7f5d8a4 | 2011-03-14 14:34:21 +0100 | [diff] [blame] | 622 | return 1; |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 623 | } |
| 624 | |
Frans Meulenbroeks | 388a29d | 2010-07-31 15:01:53 +0200 | [diff] [blame] | 625 | U_BOOT_CMD( |
| 626 | ubi, 6, 1, do_ubi, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 627 | "ubi commands", |
Heiko Schocher | cddfc97 | 2016-06-07 08:55:40 +0200 | [diff] [blame] | 628 | "detach" |
| 629 | " - detach ubi from a mtd partition\n" |
| 630 | "ubi part [part] [offset]\n" |
Simon Kagstrom | 25c8f40 | 2009-07-07 16:59:46 +0200 | [diff] [blame] | 631 | " - Show or set current partition (with optional VID" |
| 632 | " header offset)\n" |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 633 | "ubi info [l[ayout]]" |
| 634 | " - Display volume and ubi layout information\n" |
Heiko Schocher | f9f4d80 | 2014-01-25 07:27:11 +0100 | [diff] [blame] | 635 | "ubi check volumename" |
| 636 | " - check if volumename exists\n" |
Ladislav Michl | f59f07e | 2017-01-19 11:45:35 +0100 | [diff] [blame] | 637 | "ubi create[vol] volume [size] [type] [id]\n" |
| 638 | " - create volume name with size ('-' for maximum" |
| 639 | " available size)\n" |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 640 | "ubi write[vol] address volume size" |
| 641 | " - Write volume from address with size\n" |
Paul Burton | cc734f5 | 2013-09-04 15:16:59 +0100 | [diff] [blame] | 642 | "ubi write.part address volume size [fullsize]\n" |
| 643 | " - Write part of a volume from address\n" |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 644 | "ubi read[vol] address volume [size]" |
| 645 | " - Read volume to address with size\n" |
| 646 | "ubi remove[vol] volume" |
| 647 | " - Remove volume\n" |
| 648 | "[Legends]\n" |
Andrzej Wolski | f6ca3b7 | 2009-07-17 22:26:54 +0200 | [diff] [blame] | 649 | " volume: character name\n" |
| 650 | " size: specified in bytes\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 651 | " type: s[tatic] or d[ynamic] (default=dynamic)" |
Kyungmin Park | 694a0b3 | 2008-11-19 11:47:05 +0100 | [diff] [blame] | 652 | ); |