The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <unistd.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <errno.h> |
| 23 | #include <sys/mount.h> // for _IOW, _IOR, mount() |
| 24 | #include <sys/stat.h> |
| 25 | #include <mtd/mtd-user.h> |
| 26 | #undef NDEBUG |
| 27 | #include <assert.h> |
| 28 | |
| 29 | #include "mtdutils.h" |
| 30 | |
Kra1o5 | 3167936 | 2014-06-20 19:06:06 +0200 | [diff] [blame] | 31 | #ifdef RK3X |
| 32 | #include "rk3xhack.h" |
Sergey 'Jin' Bostandzhyan | 80a90ed | 2013-01-04 02:29:03 +0100 | [diff] [blame] | 33 | #endif |
| 34 | |
Michael Bestas | 5f05255 | 2015-01-02 01:45:37 +0200 | [diff] [blame] | 35 | #ifdef BYNAME |
| 36 | static const char mtdprefix[] = "/dev/block/mtd/by-name/"; |
| 37 | #define MTD_BASENAME_OFFSET (sizeof(mtdprefix)-1) |
| 38 | #endif |
| 39 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 40 | struct MtdReadContext { |
| 41 | const MtdPartition *partition; |
| 42 | char *buffer; |
| 43 | size_t consumed; |
| 44 | int fd; |
| 45 | }; |
| 46 | |
| 47 | struct MtdWriteContext { |
| 48 | const MtdPartition *partition; |
| 49 | char *buffer; |
| 50 | size_t stored; |
| 51 | int fd; |
Doug Zongker | 4c5f9f3 | 2010-01-12 16:18:33 -0800 | [diff] [blame] | 52 | |
| 53 | off_t* bad_block_offsets; |
| 54 | int bad_block_alloc; |
| 55 | int bad_block_count; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | typedef struct { |
| 59 | MtdPartition *partitions; |
| 60 | int partitions_allocd; |
| 61 | int partition_count; |
| 62 | } MtdState; |
| 63 | |
| 64 | static MtdState g_mtd_state = { |
| 65 | NULL, // partitions |
| 66 | 0, // partitions_allocd |
| 67 | -1 // partition_count |
| 68 | }; |
| 69 | |
| 70 | #define MTD_PROC_FILENAME "/proc/mtd" |
| 71 | |
| 72 | int |
| 73 | mtd_scan_partitions() |
| 74 | { |
| 75 | char buf[2048]; |
| 76 | const char *bufp; |
| 77 | int fd; |
| 78 | int i; |
| 79 | ssize_t nbytes; |
| 80 | |
| 81 | if (g_mtd_state.partitions == NULL) { |
| 82 | const int nump = 32; |
| 83 | MtdPartition *partitions = malloc(nump * sizeof(*partitions)); |
| 84 | if (partitions == NULL) { |
| 85 | errno = ENOMEM; |
| 86 | return -1; |
| 87 | } |
| 88 | g_mtd_state.partitions = partitions; |
| 89 | g_mtd_state.partitions_allocd = nump; |
| 90 | memset(partitions, 0, nump * sizeof(*partitions)); |
| 91 | } |
| 92 | g_mtd_state.partition_count = 0; |
| 93 | |
| 94 | /* Initialize all of the entries to make things easier later. |
| 95 | * (Lets us handle sparsely-numbered partitions, which |
| 96 | * may not even be possible.) |
| 97 | */ |
| 98 | for (i = 0; i < g_mtd_state.partitions_allocd; i++) { |
| 99 | MtdPartition *p = &g_mtd_state.partitions[i]; |
| 100 | if (p->name != NULL) { |
| 101 | free(p->name); |
| 102 | p->name = NULL; |
| 103 | } |
| 104 | p->device_index = -1; |
| 105 | } |
| 106 | |
| 107 | /* Open and read the file contents. |
| 108 | */ |
| 109 | fd = open(MTD_PROC_FILENAME, O_RDONLY); |
| 110 | if (fd < 0) { |
| 111 | goto bail; |
| 112 | } |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 113 | nbytes = TEMP_FAILURE_RETRY(read(fd, buf, sizeof(buf) - 1)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 114 | close(fd); |
| 115 | if (nbytes < 0) { |
| 116 | goto bail; |
| 117 | } |
| 118 | buf[nbytes] = '\0'; |
| 119 | |
| 120 | /* Parse the contents of the file, which looks like: |
| 121 | * |
| 122 | * # cat /proc/mtd |
| 123 | * dev: size erasesize name |
| 124 | * mtd0: 00080000 00020000 "bootloader" |
| 125 | * mtd1: 00400000 00020000 "mfg_and_gsm" |
| 126 | * mtd2: 00400000 00020000 "0000000c" |
| 127 | * mtd3: 00200000 00020000 "0000000d" |
| 128 | * mtd4: 04000000 00020000 "system" |
| 129 | * mtd5: 03280000 00020000 "userdata" |
| 130 | */ |
| 131 | bufp = buf; |
| 132 | while (nbytes > 0) { |
| 133 | int mtdnum, mtdsize, mtderasesize; |
| 134 | int matches; |
| 135 | char mtdname[64]; |
| 136 | mtdname[0] = '\0'; |
| 137 | mtdnum = -1; |
| 138 | |
| 139 | matches = sscanf(bufp, "mtd%d: %x %x \"%63[^\"]", |
| 140 | &mtdnum, &mtdsize, &mtderasesize, mtdname); |
| 141 | /* This will fail on the first line, which just contains |
| 142 | * column headers. |
| 143 | */ |
| 144 | if (matches == 4) { |
| 145 | MtdPartition *p = &g_mtd_state.partitions[mtdnum]; |
| 146 | p->device_index = mtdnum; |
| 147 | p->size = mtdsize; |
| 148 | p->erase_size = mtderasesize; |
Michael Bestas | 5f05255 | 2015-01-02 01:45:37 +0200 | [diff] [blame] | 149 | #ifdef BYNAME |
| 150 | asprintf(&p->name, "%s%s", mtdprefix, mtdname); |
| 151 | #else |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 152 | p->name = strdup(mtdname); |
Michael Bestas | 5f05255 | 2015-01-02 01:45:37 +0200 | [diff] [blame] | 153 | #endif |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 154 | if (p->name == NULL) { |
| 155 | errno = ENOMEM; |
| 156 | goto bail; |
| 157 | } |
| 158 | g_mtd_state.partition_count++; |
| 159 | } |
| 160 | |
| 161 | /* Eat the line. |
| 162 | */ |
| 163 | while (nbytes > 0 && *bufp != '\n') { |
| 164 | bufp++; |
| 165 | nbytes--; |
| 166 | } |
| 167 | if (nbytes > 0) { |
| 168 | bufp++; |
| 169 | nbytes--; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return g_mtd_state.partition_count; |
| 174 | |
| 175 | bail: |
| 176 | // keep "partitions" around so we can free the names on a rescan. |
| 177 | g_mtd_state.partition_count = -1; |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | const MtdPartition * |
| 182 | mtd_find_partition_by_name(const char *name) |
| 183 | { |
| 184 | if (g_mtd_state.partitions != NULL) { |
| 185 | int i; |
| 186 | for (i = 0; i < g_mtd_state.partitions_allocd; i++) { |
| 187 | MtdPartition *p = &g_mtd_state.partitions[i]; |
| 188 | if (p->device_index >= 0 && p->name != NULL) { |
| 189 | if (strcmp(p->name, name) == 0) { |
| 190 | return p; |
| 191 | } |
Michael Bestas | 5f05255 | 2015-01-02 01:45:37 +0200 | [diff] [blame] | 192 | #ifdef BYNAME |
| 193 | if (strcmp(p->name+MTD_BASENAME_OFFSET, name) == 0) { |
| 194 | return p; |
| 195 | } |
| 196 | #endif |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
| 200 | return NULL; |
| 201 | } |
| 202 | |
| 203 | int |
| 204 | mtd_mount_partition(const MtdPartition *partition, const char *mount_point, |
| 205 | const char *filesystem, int read_only) |
| 206 | { |
| 207 | const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME; |
| 208 | char devname[64]; |
| 209 | int rv = -1; |
| 210 | |
| 211 | sprintf(devname, "/dev/block/mtdblock%d", partition->device_index); |
| 212 | if (!read_only) { |
| 213 | rv = mount(devname, mount_point, filesystem, flags, NULL); |
| 214 | } |
| 215 | if (read_only || rv < 0) { |
| 216 | rv = mount(devname, mount_point, filesystem, flags | MS_RDONLY, 0); |
| 217 | if (rv < 0) { |
| 218 | printf("Failed to mount %s on %s: %s\n", |
| 219 | devname, mount_point, strerror(errno)); |
| 220 | } else { |
| 221 | printf("Mount %s on %s read-only\n", devname, mount_point); |
| 222 | } |
| 223 | } |
| 224 | #if 1 //TODO: figure out why this is happening; remove include of stat.h |
| 225 | if (rv >= 0) { |
| 226 | /* For some reason, the x bits sometimes aren't set on the root |
| 227 | * of mounted volumes. |
| 228 | */ |
| 229 | struct stat st; |
| 230 | rv = stat(mount_point, &st); |
| 231 | if (rv < 0) { |
| 232 | return rv; |
| 233 | } |
| 234 | mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH; |
| 235 | if (new_mode != st.st_mode) { |
| 236 | printf("Fixing execute permissions for %s\n", mount_point); |
| 237 | rv = chmod(mount_point, new_mode); |
| 238 | if (rv < 0) { |
| 239 | printf("Couldn't fix permissions for %s: %s\n", |
| 240 | mount_point, strerror(errno)); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | #endif |
| 245 | return rv; |
| 246 | } |
| 247 | |
| 248 | int |
| 249 | mtd_partition_info(const MtdPartition *partition, |
| 250 | size_t *total_size, size_t *erase_size, size_t *write_size) |
| 251 | { |
| 252 | char mtddevname[32]; |
| 253 | sprintf(mtddevname, "/dev/mtd/mtd%d", partition->device_index); |
| 254 | int fd = open(mtddevname, O_RDONLY); |
| 255 | if (fd < 0) return -1; |
| 256 | |
| 257 | struct mtd_info_user mtd_info; |
| 258 | int ret = ioctl(fd, MEMGETINFO, &mtd_info); |
| 259 | close(fd); |
| 260 | if (ret < 0) return -1; |
| 261 | |
| 262 | if (total_size != NULL) *total_size = mtd_info.size; |
| 263 | if (erase_size != NULL) *erase_size = mtd_info.erasesize; |
| 264 | if (write_size != NULL) *write_size = mtd_info.writesize; |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | MtdReadContext *mtd_read_partition(const MtdPartition *partition) |
| 269 | { |
| 270 | MtdReadContext *ctx = (MtdReadContext*) malloc(sizeof(MtdReadContext)); |
| 271 | if (ctx == NULL) return NULL; |
| 272 | |
| 273 | ctx->buffer = malloc(partition->erase_size); |
| 274 | if (ctx->buffer == NULL) { |
| 275 | free(ctx); |
| 276 | return NULL; |
| 277 | } |
| 278 | |
| 279 | char mtddevname[32]; |
| 280 | sprintf(mtddevname, "/dev/mtd/mtd%d", partition->device_index); |
| 281 | ctx->fd = open(mtddevname, O_RDONLY); |
| 282 | if (ctx->fd < 0) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 283 | free(ctx->buffer); |
Christian Lindeberg | 862c83b | 2011-01-19 12:22:41 +0100 | [diff] [blame] | 284 | free(ctx); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | ctx->partition = partition; |
| 289 | ctx->consumed = partition->erase_size; |
| 290 | return ctx; |
| 291 | } |
| 292 | |
| 293 | static int read_block(const MtdPartition *partition, int fd, char *data) |
| 294 | { |
| 295 | struct mtd_ecc_stats before, after; |
| 296 | if (ioctl(fd, ECCGETSTATS, &before)) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 297 | printf("mtd: ECCGETSTATS error (%s)\n", strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 298 | return -1; |
| 299 | } |
| 300 | |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 301 | loff_t pos = TEMP_FAILURE_RETRY(lseek64(fd, 0, SEEK_CUR)); |
| 302 | if (pos == -1) { |
| 303 | printf("mtd: read_block: couldn't SEEK_CUR: %s\n", strerror(errno)); |
| 304 | return -1; |
| 305 | } |
Doug Zongker | 17a4709 | 2009-12-14 18:03:27 -0800 | [diff] [blame] | 306 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 307 | ssize_t size = partition->erase_size; |
Doug Zongker | 17a4709 | 2009-12-14 18:03:27 -0800 | [diff] [blame] | 308 | int mgbb; |
| 309 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 310 | while (pos + size <= (int) partition->size) { |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 311 | if (TEMP_FAILURE_RETRY(lseek64(fd, pos, SEEK_SET)) != pos || |
| 312 | TEMP_FAILURE_RETRY(read(fd, data, size)) != size) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 313 | printf("mtd: read error at 0x%08llx (%s)\n", |
Tao Bao | 5701d58 | 2015-09-24 10:56:48 -0700 | [diff] [blame] | 314 | (long long)pos, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 315 | } else if (ioctl(fd, ECCGETSTATS, &after)) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 316 | printf("mtd: ECCGETSTATS error (%s)\n", strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 317 | return -1; |
| 318 | } else if (after.failed != before.failed) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 319 | printf("mtd: ECC errors (%d soft, %d hard) at 0x%08llx\n", |
Tao Bao | 5701d58 | 2015-09-24 10:56:48 -0700 | [diff] [blame] | 320 | after.corrected - before.corrected, |
| 321 | after.failed - before.failed, (long long)pos); |
Doug Zongker | 5d6309e | 2010-11-03 14:31:01 -0700 | [diff] [blame] | 322 | // copy the comparison baseline for the next read. |
| 323 | memcpy(&before, &after, sizeof(struct mtd_ecc_stats)); |
Doug Zongker | 17a4709 | 2009-12-14 18:03:27 -0800 | [diff] [blame] | 324 | } else if ((mgbb = ioctl(fd, MEMGETBADBLOCK, &pos))) { |
| 325 | fprintf(stderr, |
Elliott Hughes | 1fdd452 | 2015-03-23 13:33:57 -0700 | [diff] [blame] | 326 | "mtd: MEMGETBADBLOCK returned %d at 0x%08llx: %s\n", |
Tao Bao | 5701d58 | 2015-09-24 10:56:48 -0700 | [diff] [blame] | 327 | mgbb, (long long)pos, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 328 | } else { |
Doug Zongker | 61ba7a8 | 2010-09-12 13:36:40 -0700 | [diff] [blame] | 329 | return 0; // Success! |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | pos += partition->erase_size; |
| 333 | } |
| 334 | |
| 335 | errno = ENOSPC; |
| 336 | return -1; |
| 337 | } |
| 338 | |
| 339 | ssize_t mtd_read_data(MtdReadContext *ctx, char *data, size_t len) |
| 340 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 341 | ssize_t read = 0; |
| 342 | while (read < (int) len) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 343 | if (ctx->consumed < ctx->partition->erase_size) { |
| 344 | size_t avail = ctx->partition->erase_size - ctx->consumed; |
| 345 | size_t copy = len - read < avail ? len - read : avail; |
| 346 | memcpy(data + read, ctx->buffer + ctx->consumed, copy); |
| 347 | ctx->consumed += copy; |
| 348 | read += copy; |
| 349 | } |
| 350 | |
| 351 | // Read complete blocks directly into the user's buffer |
| 352 | while (ctx->consumed == ctx->partition->erase_size && |
| 353 | len - read >= ctx->partition->erase_size) { |
| 354 | if (read_block(ctx->partition, ctx->fd, data + read)) return -1; |
| 355 | read += ctx->partition->erase_size; |
| 356 | } |
| 357 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 358 | if (read >= (int)len) { |
Doug Zongker | bec02d5 | 2009-07-01 12:09:29 -0700 | [diff] [blame] | 359 | return read; |
| 360 | } |
| 361 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 362 | // Read the next block into the buffer |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 363 | if (ctx->consumed == ctx->partition->erase_size && read < (int) len) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 364 | if (read_block(ctx->partition, ctx->fd, ctx->buffer)) return -1; |
| 365 | ctx->consumed = 0; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | return read; |
| 370 | } |
| 371 | |
| 372 | void mtd_read_close(MtdReadContext *ctx) |
| 373 | { |
| 374 | close(ctx->fd); |
| 375 | free(ctx->buffer); |
| 376 | free(ctx); |
| 377 | } |
| 378 | |
| 379 | MtdWriteContext *mtd_write_partition(const MtdPartition *partition) |
| 380 | { |
| 381 | MtdWriteContext *ctx = (MtdWriteContext*) malloc(sizeof(MtdWriteContext)); |
| 382 | if (ctx == NULL) return NULL; |
| 383 | |
Doug Zongker | 4c5f9f3 | 2010-01-12 16:18:33 -0800 | [diff] [blame] | 384 | ctx->bad_block_offsets = NULL; |
| 385 | ctx->bad_block_alloc = 0; |
| 386 | ctx->bad_block_count = 0; |
| 387 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 388 | ctx->buffer = malloc(partition->erase_size); |
| 389 | if (ctx->buffer == NULL) { |
| 390 | free(ctx); |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | char mtddevname[32]; |
| 395 | sprintf(mtddevname, "/dev/mtd/mtd%d", partition->device_index); |
| 396 | ctx->fd = open(mtddevname, O_RDWR); |
| 397 | if (ctx->fd < 0) { |
| 398 | free(ctx->buffer); |
| 399 | free(ctx); |
| 400 | return NULL; |
| 401 | } |
| 402 | |
| 403 | ctx->partition = partition; |
| 404 | ctx->stored = 0; |
| 405 | return ctx; |
| 406 | } |
| 407 | |
Doug Zongker | 4c5f9f3 | 2010-01-12 16:18:33 -0800 | [diff] [blame] | 408 | static void add_bad_block_offset(MtdWriteContext *ctx, off_t pos) { |
| 409 | if (ctx->bad_block_count + 1 > ctx->bad_block_alloc) { |
| 410 | ctx->bad_block_alloc = (ctx->bad_block_alloc*2) + 1; |
| 411 | ctx->bad_block_offsets = realloc(ctx->bad_block_offsets, |
| 412 | ctx->bad_block_alloc * sizeof(off_t)); |
| 413 | } |
| 414 | ctx->bad_block_offsets[ctx->bad_block_count++] = pos; |
| 415 | } |
| 416 | |
| 417 | static int write_block(MtdWriteContext *ctx, const char *data) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 418 | { |
Doug Zongker | 4c5f9f3 | 2010-01-12 16:18:33 -0800 | [diff] [blame] | 419 | const MtdPartition *partition = ctx->partition; |
| 420 | int fd = ctx->fd; |
| 421 | |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 422 | off_t pos = TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_CUR)); |
| 423 | if (pos == (off_t) -1) { |
| 424 | printf("mtd: write_block: couldn't SEEK_CUR: %s\n", strerror(errno)); |
| 425 | return -1; |
| 426 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 427 | |
| 428 | ssize_t size = partition->erase_size; |
| 429 | while (pos + size <= (int) partition->size) { |
| 430 | loff_t bpos = pos; |
Doug Zongker | aaf3f56 | 2010-09-09 16:41:49 -0700 | [diff] [blame] | 431 | int ret = ioctl(fd, MEMGETBADBLOCK, &bpos); |
| 432 | if (ret != 0 && !(ret == -1 && errno == EOPNOTSUPP)) { |
Doug Zongker | 4c5f9f3 | 2010-01-12 16:18:33 -0800 | [diff] [blame] | 433 | add_bad_block_offset(ctx, pos); |
Doug Zongker | aaf3f56 | 2010-09-09 16:41:49 -0700 | [diff] [blame] | 434 | fprintf(stderr, |
Elliott Hughes | 1fdd452 | 2015-03-23 13:33:57 -0700 | [diff] [blame] | 435 | "mtd: not writing bad block at 0x%08lx (ret %d): %s\n", |
| 436 | pos, ret, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 437 | pos += partition->erase_size; |
| 438 | continue; // Don't try to erase known factory-bad blocks. |
| 439 | } |
| 440 | |
| 441 | struct erase_info_user erase_info; |
| 442 | erase_info.start = pos; |
| 443 | erase_info.length = size; |
| 444 | int retry; |
| 445 | for (retry = 0; retry < 2; ++retry) { |
Kra1o5 | 3167936 | 2014-06-20 19:06:06 +0200 | [diff] [blame] | 446 | #ifdef RK3X |
Sergey 'Jin' Bostandzhyan | 80a90ed | 2013-01-04 02:29:03 +0100 | [diff] [blame] | 447 | if (rk30_zero_out(fd, pos, size) < 0) { |
| 448 | fprintf(stderr, "mtd: erase failure at 0x%08lx (%s)\n", |
| 449 | pos, strerror(errno)); |
| 450 | continue; |
| 451 | } |
| 452 | #else |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 453 | if (ioctl(fd, MEMERASE, &erase_info) < 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 454 | printf("mtd: erase failure at 0x%08lx (%s)\n", |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 455 | pos, strerror(errno)); |
| 456 | continue; |
| 457 | } |
Sergey 'Jin' Bostandzhyan | 80a90ed | 2013-01-04 02:29:03 +0100 | [diff] [blame] | 458 | #endif |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 459 | if (TEMP_FAILURE_RETRY(lseek(fd, pos, SEEK_SET)) != pos || |
| 460 | TEMP_FAILURE_RETRY(write(fd, data, size)) != size) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 461 | printf("mtd: write error at 0x%08lx (%s)\n", |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 462 | pos, strerror(errno)); |
| 463 | } |
| 464 | |
| 465 | char verify[size]; |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 466 | if (TEMP_FAILURE_RETRY(lseek(fd, pos, SEEK_SET)) != pos || |
| 467 | TEMP_FAILURE_RETRY(read(fd, verify, size)) != size) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 468 | printf("mtd: re-read error at 0x%08lx (%s)\n", |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 469 | pos, strerror(errno)); |
| 470 | continue; |
| 471 | } |
| 472 | if (memcmp(data, verify, size) != 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 473 | printf("mtd: verification error at 0x%08lx (%s)\n", |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 474 | pos, strerror(errno)); |
| 475 | continue; |
| 476 | } |
| 477 | |
| 478 | if (retry > 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 479 | printf("mtd: wrote block after %d retries\n", retry); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 480 | } |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 481 | printf("mtd: successfully wrote block at %lx\n", pos); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 482 | return 0; // Success! |
| 483 | } |
| 484 | |
| 485 | // Try to erase it once more as we give up on this block |
Doug Zongker | 4c5f9f3 | 2010-01-12 16:18:33 -0800 | [diff] [blame] | 486 | add_bad_block_offset(ctx, pos); |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 487 | printf("mtd: skipping write block at 0x%08lx\n", pos); |
Kra1o5 | 3167936 | 2014-06-20 19:06:06 +0200 | [diff] [blame] | 488 | #ifdef RK3X |
Sergey 'Jin' Bostandzhyan | 80a90ed | 2013-01-04 02:29:03 +0100 | [diff] [blame] | 489 | rk30_zero_out(fd, pos, size); |
| 490 | #else |
Dees Troy | bb4c0cb | 2013-11-02 20:25:14 +0000 | [diff] [blame] | 491 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 492 | ioctl(fd, MEMERASE, &erase_info); |
Sergey 'Jin' Bostandzhyan | 80a90ed | 2013-01-04 02:29:03 +0100 | [diff] [blame] | 493 | #endif |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 494 | pos += partition->erase_size; |
| 495 | } |
| 496 | |
| 497 | // Ran out of space on the device |
| 498 | errno = ENOSPC; |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | ssize_t mtd_write_data(MtdWriteContext *ctx, const char *data, size_t len) |
| 503 | { |
| 504 | size_t wrote = 0; |
| 505 | while (wrote < len) { |
| 506 | // Coalesce partial writes into complete blocks |
| 507 | if (ctx->stored > 0 || len - wrote < ctx->partition->erase_size) { |
| 508 | size_t avail = ctx->partition->erase_size - ctx->stored; |
| 509 | size_t copy = len - wrote < avail ? len - wrote : avail; |
| 510 | memcpy(ctx->buffer + ctx->stored, data + wrote, copy); |
| 511 | ctx->stored += copy; |
| 512 | wrote += copy; |
| 513 | } |
| 514 | |
| 515 | // If a complete block was accumulated, write it |
| 516 | if (ctx->stored == ctx->partition->erase_size) { |
Doug Zongker | 4c5f9f3 | 2010-01-12 16:18:33 -0800 | [diff] [blame] | 517 | if (write_block(ctx, ctx->buffer)) return -1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 518 | ctx->stored = 0; |
| 519 | } |
| 520 | |
| 521 | // Write complete blocks directly from the user's buffer |
| 522 | while (ctx->stored == 0 && len - wrote >= ctx->partition->erase_size) { |
Doug Zongker | 4c5f9f3 | 2010-01-12 16:18:33 -0800 | [diff] [blame] | 523 | if (write_block(ctx, data + wrote)) return -1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 524 | wrote += ctx->partition->erase_size; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | return wrote; |
| 529 | } |
| 530 | |
| 531 | off_t mtd_erase_blocks(MtdWriteContext *ctx, int blocks) |
| 532 | { |
| 533 | // Zero-pad and write any pending data to get us to a block boundary |
| 534 | if (ctx->stored > 0) { |
| 535 | size_t zero = ctx->partition->erase_size - ctx->stored; |
| 536 | memset(ctx->buffer + ctx->stored, 0, zero); |
Doug Zongker | 4c5f9f3 | 2010-01-12 16:18:33 -0800 | [diff] [blame] | 537 | if (write_block(ctx, ctx->buffer)) return -1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 538 | ctx->stored = 0; |
| 539 | } |
| 540 | |
Elliott Hughes | 2f5feed | 2015-04-28 17:24:24 -0700 | [diff] [blame] | 541 | off_t pos = TEMP_FAILURE_RETRY(lseek(ctx->fd, 0, SEEK_CUR)); |
| 542 | if ((off_t) pos == (off_t) -1) { |
| 543 | printf("mtd_erase_blocks: couldn't SEEK_CUR: %s\n", strerror(errno)); |
| 544 | return -1; |
| 545 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 546 | |
| 547 | const int total = (ctx->partition->size - pos) / ctx->partition->erase_size; |
| 548 | if (blocks < 0) blocks = total; |
| 549 | if (blocks > total) { |
| 550 | errno = ENOSPC; |
| 551 | return -1; |
| 552 | } |
| 553 | |
| 554 | // Erase the specified number of blocks |
| 555 | while (blocks-- > 0) { |
| 556 | loff_t bpos = pos; |
| 557 | if (ioctl(ctx->fd, MEMGETBADBLOCK, &bpos) > 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 558 | printf("mtd: not erasing bad block at 0x%08lx\n", pos); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 559 | pos += ctx->partition->erase_size; |
| 560 | continue; // Don't try to erase known factory-bad blocks. |
| 561 | } |
| 562 | |
| 563 | struct erase_info_user erase_info; |
| 564 | erase_info.start = pos; |
| 565 | erase_info.length = ctx->partition->erase_size; |
Kra1o5 | 3167936 | 2014-06-20 19:06:06 +0200 | [diff] [blame] | 566 | #ifdef RK3X |
Sergey 'Jin' Bostandzhyan | 80a90ed | 2013-01-04 02:29:03 +0100 | [diff] [blame] | 567 | if (rk30_zero_out(ctx->fd, pos, ctx->partition->erase_size) < 0) { |
| 568 | fprintf(stderr, "mtd: erase failure at 0x%08lx\n", pos); |
| 569 | } |
| 570 | #else |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 571 | if (ioctl(ctx->fd, MEMERASE, &erase_info) < 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 572 | printf("mtd: erase failure at 0x%08lx\n", pos); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 573 | } |
Sergey 'Jin' Bostandzhyan | 80a90ed | 2013-01-04 02:29:03 +0100 | [diff] [blame] | 574 | #endif |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 575 | pos += ctx->partition->erase_size; |
| 576 | } |
| 577 | |
| 578 | return pos; |
| 579 | } |
| 580 | |
| 581 | int mtd_write_close(MtdWriteContext *ctx) |
| 582 | { |
| 583 | int r = 0; |
| 584 | // Make sure any pending data gets written |
| 585 | if (mtd_erase_blocks(ctx, 0) == (off_t) -1) r = -1; |
| 586 | if (close(ctx->fd)) r = -1; |
Doug Zongker | 4c5f9f3 | 2010-01-12 16:18:33 -0800 | [diff] [blame] | 587 | free(ctx->bad_block_offsets); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 588 | free(ctx->buffer); |
| 589 | free(ctx); |
| 590 | return r; |
| 591 | } |
Doug Zongker | 4c5f9f3 | 2010-01-12 16:18:33 -0800 | [diff] [blame] | 592 | |
| 593 | /* Return the offset of the first good block at or after pos (which |
| 594 | * might be pos itself). |
| 595 | */ |
| 596 | off_t mtd_find_write_start(MtdWriteContext *ctx, off_t pos) { |
| 597 | int i; |
| 598 | for (i = 0; i < ctx->bad_block_count; ++i) { |
| 599 | if (ctx->bad_block_offsets[i] == pos) { |
| 600 | pos += ctx->partition->erase_size; |
| 601 | } else if (ctx->bad_block_offsets[i] > pos) { |
| 602 | return pos; |
| 603 | } |
| 604 | } |
| 605 | return pos; |
| 606 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 607 | |
| 608 | #define BLOCK_SIZE 2048 |
| 609 | #define SPARE_SIZE (BLOCK_SIZE >> 5) |
| 610 | #define HEADER_SIZE 2048 |
| 611 | |
| 612 | int cmd_mtd_restore_raw_partition(const char *partition_name, const char *filename) |
| 613 | { |
| 614 | const MtdPartition *ptn; |
| 615 | MtdWriteContext *write; |
| 616 | void *data; |
| 617 | |
| 618 | FILE* f = fopen(filename, "rb"); |
| 619 | if (f == NULL) { |
| 620 | fprintf(stderr, "error opening %s", filename); |
| 621 | return -1; |
| 622 | } |
| 623 | |
| 624 | if (mtd_scan_partitions() <= 0) |
| 625 | { |
| 626 | fprintf(stderr, "error scanning partitions"); |
| 627 | return -1; |
| 628 | } |
| 629 | const MtdPartition *mtd = mtd_find_partition_by_name(partition_name); |
| 630 | if (mtd == NULL) |
| 631 | { |
| 632 | fprintf(stderr, "can't find %s partition", partition_name); |
| 633 | return -1; |
| 634 | } |
| 635 | |
| 636 | int fd = open(filename, O_RDONLY); |
| 637 | if (fd < 0) |
| 638 | { |
| 639 | printf("error opening %s", filename); |
| 640 | return -1; |
| 641 | } |
| 642 | |
| 643 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 644 | if (ctx == NULL) { |
| 645 | printf("error writing %s", partition_name); |
| 646 | return -1; |
| 647 | } |
| 648 | |
| 649 | int success = 1; |
| 650 | char* buffer = malloc(BUFSIZ); |
| 651 | int read; |
| 652 | while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) { |
| 653 | int wrote = mtd_write_data(ctx, buffer, read); |
| 654 | success = success && (wrote == read); |
| 655 | } |
| 656 | free(buffer); |
| 657 | fclose(f); |
| 658 | |
| 659 | if (!success) { |
| 660 | fprintf(stderr, "error writing %s", partition_name); |
| 661 | return -1; |
| 662 | } |
| 663 | |
| 664 | if (mtd_erase_blocks(ctx, -1) == -1) { |
| 665 | fprintf(stderr, "error erasing blocks of %s\n", partition_name); |
| 666 | } |
| 667 | if (mtd_write_close(ctx) != 0) { |
| 668 | fprintf(stderr, "error closing write of %s\n", partition_name); |
| 669 | } |
| 670 | printf("%s %s partition\n", success ? "wrote" : "failed to write", partition_name); |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | |
| 675 | int cmd_mtd_backup_raw_partition(const char *partition_name, const char *filename) |
| 676 | { |
| 677 | MtdReadContext *in; |
| 678 | const MtdPartition *partition; |
| 679 | char buf[BLOCK_SIZE + SPARE_SIZE]; |
| 680 | size_t partition_size; |
| 681 | size_t read_size; |
| 682 | size_t total; |
| 683 | int fd; |
| 684 | int wrote; |
| 685 | int len; |
| 686 | |
| 687 | if (mtd_scan_partitions() <= 0) |
| 688 | { |
| 689 | printf("error scanning partitions"); |
| 690 | return -1; |
| 691 | } |
| 692 | |
| 693 | partition = mtd_find_partition_by_name(partition_name); |
| 694 | if (partition == NULL) |
| 695 | { |
| 696 | printf("can't find %s partition", partition_name); |
| 697 | return -1; |
| 698 | } |
| 699 | |
| 700 | if (mtd_partition_info(partition, &partition_size, NULL, NULL)) { |
| 701 | printf("can't get info of partition %s", partition_name); |
| 702 | return -1; |
| 703 | } |
| 704 | |
| 705 | if (!strcmp(filename, "-")) { |
| 706 | fd = fileno(stdout); |
| 707 | } |
| 708 | else { |
| 709 | fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 710 | } |
| 711 | |
| 712 | if (fd < 0) |
| 713 | { |
| 714 | printf("error opening %s", filename); |
| 715 | return -1; |
| 716 | } |
| 717 | |
| 718 | in = mtd_read_partition(partition); |
| 719 | if (in == NULL) { |
| 720 | close(fd); |
| 721 | unlink(filename); |
| 722 | printf("error opening %s: %s\n", partition_name, strerror(errno)); |
| 723 | return -1; |
| 724 | } |
| 725 | |
| 726 | total = 0; |
| 727 | while ((len = mtd_read_data(in, buf, BLOCK_SIZE)) > 0) { |
| 728 | wrote = write(fd, buf, len); |
| 729 | if (wrote != len) { |
| 730 | close(fd); |
| 731 | unlink(filename); |
| 732 | printf("error writing %s", filename); |
| 733 | return -1; |
| 734 | } |
| 735 | total += BLOCK_SIZE; |
| 736 | } |
| 737 | |
| 738 | mtd_read_close(in); |
| 739 | |
| 740 | if (close(fd)) { |
| 741 | unlink(filename); |
| 742 | printf("error closing %s", filename); |
| 743 | return -1; |
| 744 | } |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | int cmd_mtd_erase_raw_partition(const char *partition_name) |
| 749 | { |
| 750 | MtdWriteContext *out; |
| 751 | size_t erased; |
| 752 | size_t total_size; |
| 753 | size_t erase_size; |
| 754 | |
| 755 | if (mtd_scan_partitions() <= 0) |
| 756 | { |
| 757 | printf("error scanning partitions"); |
| 758 | return -1; |
| 759 | } |
| 760 | const MtdPartition *p = mtd_find_partition_by_name(partition_name); |
| 761 | if (p == NULL) |
| 762 | { |
| 763 | printf("can't find %s partition", partition_name); |
| 764 | return -1; |
| 765 | } |
| 766 | |
| 767 | out = mtd_write_partition(p); |
| 768 | if (out == NULL) |
| 769 | { |
| 770 | printf("could not estabilish write context for %s", partition_name); |
| 771 | return -1; |
| 772 | } |
| 773 | |
| 774 | // do the actual erase, -1 = full partition erase |
| 775 | erased = mtd_erase_blocks(out, -1); |
| 776 | |
| 777 | // erased = bytes erased, if zero, something borked |
| 778 | if (!erased) |
| 779 | { |
| 780 | printf("error erasing %s", partition_name); |
| 781 | return -1; |
| 782 | } |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | int cmd_mtd_erase_partition(const char *partition, const char *filesystem) |
| 788 | { |
| 789 | return cmd_mtd_erase_raw_partition(partition); |
| 790 | } |
| 791 | |
| 792 | |
| 793 | int cmd_mtd_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only) |
| 794 | { |
| 795 | mtd_scan_partitions(); |
| 796 | const MtdPartition *p; |
| 797 | p = mtd_find_partition_by_name(partition); |
| 798 | if (p == NULL) { |
| 799 | return -1; |
| 800 | } |
| 801 | return mtd_mount_partition(p, mount_point, filesystem, read_only); |
| 802 | } |
| 803 | |
| 804 | int cmd_mtd_get_partition_device(const char *partition, char *device) |
| 805 | { |
| 806 | mtd_scan_partitions(); |
| 807 | MtdPartition *p = mtd_find_partition_by_name(partition); |
| 808 | if (p == NULL) |
| 809 | return -1; |
| 810 | sprintf(device, "/dev/block/mtdblock%d", p->device_index); |
| 811 | return 0; |
Michael Bestas | 5f05255 | 2015-01-02 01:45:37 +0200 | [diff] [blame] | 812 | } |