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