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