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