blob: 25feabd69dbe6652e1441579a633b3f0b133915d [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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
31struct MtdPartition {
32 int device_index;
33 unsigned int size;
34 unsigned int erase_size;
35 char *name;
36};
37
38struct MtdReadContext {
39 const MtdPartition *partition;
40 char *buffer;
Koushik K. Dutta8ce0be42010-02-20 15:59:06 -080041 size_t read_size;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042 size_t consumed;
43 int fd;
44};
45
46struct MtdWriteContext {
47 const MtdPartition *partition;
48 char *buffer;
49 size_t stored;
50 int fd;
Doug Zongker22d79a52010-01-12 16:18:33 -080051
52 off_t* bad_block_offsets;
53 int bad_block_alloc;
54 int bad_block_count;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055};
56
57typedef struct {
58 MtdPartition *partitions;
59 int partitions_allocd;
60 int partition_count;
61} MtdState;
62
63static MtdState g_mtd_state = {
64 NULL, // partitions
65 0, // partitions_allocd
66 -1 // partition_count
67};
68
69#define MTD_PROC_FILENAME "/proc/mtd"
70
71int
72mtd_scan_partitions()
73{
74 char buf[2048];
75 const char *bufp;
76 int fd;
77 int i;
78 ssize_t nbytes;
79
80 if (g_mtd_state.partitions == NULL) {
81 const int nump = 32;
82 MtdPartition *partitions = malloc(nump * sizeof(*partitions));
83 if (partitions == NULL) {
84 errno = ENOMEM;
85 return -1;
86 }
87 g_mtd_state.partitions = partitions;
88 g_mtd_state.partitions_allocd = nump;
89 memset(partitions, 0, nump * sizeof(*partitions));
90 }
91 g_mtd_state.partition_count = 0;
92
93 /* Initialize all of the entries to make things easier later.
94 * (Lets us handle sparsely-numbered partitions, which
95 * may not even be possible.)
96 */
97 for (i = 0; i < g_mtd_state.partitions_allocd; i++) {
98 MtdPartition *p = &g_mtd_state.partitions[i];
99 if (p->name != NULL) {
100 free(p->name);
101 p->name = NULL;
102 }
103 p->device_index = -1;
104 }
105
106 /* Open and read the file contents.
107 */
108 fd = open(MTD_PROC_FILENAME, O_RDONLY);
109 if (fd < 0) {
110 goto bail;
111 }
112 nbytes = read(fd, buf, sizeof(buf) - 1);
113 close(fd);
114 if (nbytes < 0) {
115 goto bail;
116 }
117 buf[nbytes] = '\0';
118
119 /* Parse the contents of the file, which looks like:
120 *
121 * # cat /proc/mtd
122 * dev: size erasesize name
123 * mtd0: 00080000 00020000 "bootloader"
124 * mtd1: 00400000 00020000 "mfg_and_gsm"
125 * mtd2: 00400000 00020000 "0000000c"
126 * mtd3: 00200000 00020000 "0000000d"
127 * mtd4: 04000000 00020000 "system"
128 * mtd5: 03280000 00020000 "userdata"
129 */
130 bufp = buf;
131 while (nbytes > 0) {
132 int mtdnum, mtdsize, mtderasesize;
133 int matches;
134 char mtdname[64];
135 mtdname[0] = '\0';
136 mtdnum = -1;
137
138 matches = sscanf(bufp, "mtd%d: %x %x \"%63[^\"]",
139 &mtdnum, &mtdsize, &mtderasesize, mtdname);
140 /* This will fail on the first line, which just contains
141 * column headers.
142 */
143 if (matches == 4) {
144 MtdPartition *p = &g_mtd_state.partitions[mtdnum];
145 p->device_index = mtdnum;
146 p->size = mtdsize;
147 p->erase_size = mtderasesize;
148 p->name = strdup(mtdname);
149 if (p->name == NULL) {
150 errno = ENOMEM;
151 goto bail;
152 }
153 g_mtd_state.partition_count++;
154 }
155
156 /* Eat the line.
157 */
158 while (nbytes > 0 && *bufp != '\n') {
159 bufp++;
160 nbytes--;
161 }
162 if (nbytes > 0) {
163 bufp++;
164 nbytes--;
165 }
166 }
167
168 return g_mtd_state.partition_count;
169
170bail:
171 // keep "partitions" around so we can free the names on a rescan.
172 g_mtd_state.partition_count = -1;
173 return -1;
174}
175
176const MtdPartition *
177mtd_find_partition_by_name(const char *name)
178{
179 if (g_mtd_state.partitions != NULL) {
180 int i;
181 for (i = 0; i < g_mtd_state.partitions_allocd; i++) {
182 MtdPartition *p = &g_mtd_state.partitions[i];
183 if (p->device_index >= 0 && p->name != NULL) {
184 if (strcmp(p->name, name) == 0) {
185 return p;
186 }
187 }
188 }
189 }
190 return NULL;
191}
192
193int
194mtd_mount_partition(const MtdPartition *partition, const char *mount_point,
195 const char *filesystem, int read_only)
196{
197 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
198 char devname[64];
199 int rv = -1;
200
201 sprintf(devname, "/dev/block/mtdblock%d", partition->device_index);
202 if (!read_only) {
203 rv = mount(devname, mount_point, filesystem, flags, NULL);
204 }
205 if (read_only || rv < 0) {
206 rv = mount(devname, mount_point, filesystem, flags | MS_RDONLY, 0);
207 if (rv < 0) {
208 printf("Failed to mount %s on %s: %s\n",
209 devname, mount_point, strerror(errno));
210 } else {
211 printf("Mount %s on %s read-only\n", devname, mount_point);
212 }
213 }
214#if 1 //TODO: figure out why this is happening; remove include of stat.h
215 if (rv >= 0) {
216 /* For some reason, the x bits sometimes aren't set on the root
217 * of mounted volumes.
218 */
219 struct stat st;
220 rv = stat(mount_point, &st);
221 if (rv < 0) {
222 return rv;
223 }
224 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
225 if (new_mode != st.st_mode) {
226printf("Fixing execute permissions for %s\n", mount_point);
227 rv = chmod(mount_point, new_mode);
228 if (rv < 0) {
229 printf("Couldn't fix permissions for %s: %s\n",
230 mount_point, strerror(errno));
231 }
232 }
233 }
234#endif
235 return rv;
236}
237
238int
239mtd_partition_info(const MtdPartition *partition,
240 size_t *total_size, size_t *erase_size, size_t *write_size)
241{
242 char mtddevname[32];
243 sprintf(mtddevname, "/dev/mtd/mtd%d", partition->device_index);
244 int fd = open(mtddevname, O_RDONLY);
245 if (fd < 0) return -1;
246
247 struct mtd_info_user mtd_info;
248 int ret = ioctl(fd, MEMGETINFO, &mtd_info);
249 close(fd);
250 if (ret < 0) return -1;
251
252 if (total_size != NULL) *total_size = mtd_info.size;
253 if (erase_size != NULL) *erase_size = mtd_info.erasesize;
254 if (write_size != NULL) *write_size = mtd_info.writesize;
255 return 0;
256}
257
258MtdReadContext *mtd_read_partition(const MtdPartition *partition)
259{
260 MtdReadContext *ctx = (MtdReadContext*) malloc(sizeof(MtdReadContext));
261 if (ctx == NULL) return NULL;
262
263 ctx->buffer = malloc(partition->erase_size);
264 if (ctx->buffer == NULL) {
265 free(ctx);
266 return NULL;
267 }
268
269 char mtddevname[32];
270 sprintf(mtddevname, "/dev/mtd/mtd%d", partition->device_index);
271 ctx->fd = open(mtddevname, O_RDONLY);
272 if (ctx->fd < 0) {
273 free(ctx);
274 free(ctx->buffer);
275 return NULL;
276 }
277
278 ctx->partition = partition;
279 ctx->consumed = partition->erase_size;
280 return ctx;
281}
282
283static int read_block(const MtdPartition *partition, int fd, char *data)
284{
285 struct mtd_ecc_stats before, after;
286 if (ioctl(fd, ECCGETSTATS, &before)) {
287 fprintf(stderr, "mtd: ECCGETSTATS error (%s)\n", strerror(errno));
288 return -1;
289 }
290
Doug Zongker17a47092009-12-14 18:03:27 -0800291 loff_t pos = lseek64(fd, 0, SEEK_CUR);
292
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800293 ssize_t size = partition->erase_size;
Doug Zongker17a47092009-12-14 18:03:27 -0800294 int mgbb;
295
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800296 while (pos + size <= (int) partition->size) {
Doug Zongker17a47092009-12-14 18:03:27 -0800297 if (lseek64(fd, pos, SEEK_SET) != pos || read(fd, data, size) != size) {
298 fprintf(stderr, "mtd: read error at 0x%08llx (%s)\n",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800299 pos, strerror(errno));
300 } else if (ioctl(fd, ECCGETSTATS, &after)) {
301 fprintf(stderr, "mtd: ECCGETSTATS error (%s)\n", strerror(errno));
302 return -1;
303 } else if (after.failed != before.failed) {
Doug Zongker17a47092009-12-14 18:03:27 -0800304 fprintf(stderr, "mtd: ECC errors (%d soft, %d hard) at 0x%08llx\n",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800305 after.corrected - before.corrected,
306 after.failed - before.failed, pos);
Doug Zongker17a47092009-12-14 18:03:27 -0800307 } else if ((mgbb = ioctl(fd, MEMGETBADBLOCK, &pos))) {
308 fprintf(stderr,
309 "mtd: MEMGETBADBLOCK returned %d at 0x%08llx (errno=%d)\n",
310 mgbb, pos, errno);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800311 } else {
Doug Zongkerbec02d52009-07-01 12:09:29 -0700312 int i;
313 for (i = 0; i < size; ++i) {
314 if (data[i] != 0) {
315 return 0; // Success!
316 }
317 }
Doug Zongker17a47092009-12-14 18:03:27 -0800318 fprintf(stderr, "mtd: read all-zero block at 0x%08llx; skipping\n",
Doug Zongkerbec02d52009-07-01 12:09:29 -0700319 pos);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800320 }
321
322 pos += partition->erase_size;
323 }
324
325 errno = ENOSPC;
326 return -1;
327}
328
329ssize_t mtd_read_data(MtdReadContext *ctx, char *data, size_t len)
330{
331 ssize_t read = 0;
332 while (read < (int) len) {
333 if (ctx->consumed < ctx->partition->erase_size) {
334 size_t avail = ctx->partition->erase_size - ctx->consumed;
335 size_t copy = len - read < avail ? len - read : avail;
336 memcpy(data + read, ctx->buffer + ctx->consumed, copy);
337 ctx->consumed += copy;
338 read += copy;
339 }
340
341 // Read complete blocks directly into the user's buffer
342 while (ctx->consumed == ctx->partition->erase_size &&
343 len - read >= ctx->partition->erase_size) {
344 if (read_block(ctx->partition, ctx->fd, data + read)) return -1;
345 read += ctx->partition->erase_size;
346 }
347
Doug Zongkerbec02d52009-07-01 12:09:29 -0700348 if (read >= len) {
349 return read;
350 }
351
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800352 // Read the next block into the buffer
353 if (ctx->consumed == ctx->partition->erase_size && read < (int) len) {
354 if (read_block(ctx->partition, ctx->fd, ctx->buffer)) return -1;
355 ctx->consumed = 0;
356 }
357 }
358
359 return read;
360}
361
362void mtd_read_close(MtdReadContext *ctx)
363{
364 close(ctx->fd);
365 free(ctx->buffer);
366 free(ctx);
367}
368
369MtdWriteContext *mtd_write_partition(const MtdPartition *partition)
370{
371 MtdWriteContext *ctx = (MtdWriteContext*) malloc(sizeof(MtdWriteContext));
372 if (ctx == NULL) return NULL;
373
Doug Zongker22d79a52010-01-12 16:18:33 -0800374 ctx->bad_block_offsets = NULL;
375 ctx->bad_block_alloc = 0;
376 ctx->bad_block_count = 0;
377
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800378 ctx->buffer = malloc(partition->erase_size);
379 if (ctx->buffer == NULL) {
380 free(ctx);
381 return NULL;
382 }
383
384 char mtddevname[32];
385 sprintf(mtddevname, "/dev/mtd/mtd%d", partition->device_index);
386 ctx->fd = open(mtddevname, O_RDWR);
387 if (ctx->fd < 0) {
388 free(ctx->buffer);
389 free(ctx);
390 return NULL;
391 }
392
393 ctx->partition = partition;
394 ctx->stored = 0;
395 return ctx;
396}
397
Doug Zongker22d79a52010-01-12 16:18:33 -0800398static void add_bad_block_offset(MtdWriteContext *ctx, off_t pos) {
399 if (ctx->bad_block_count + 1 > ctx->bad_block_alloc) {
400 ctx->bad_block_alloc = (ctx->bad_block_alloc*2) + 1;
401 ctx->bad_block_offsets = realloc(ctx->bad_block_offsets,
402 ctx->bad_block_alloc * sizeof(off_t));
403 }
404 ctx->bad_block_offsets[ctx->bad_block_count++] = pos;
405}
406
407static int write_block(MtdWriteContext *ctx, const char *data)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800408{
Doug Zongker22d79a52010-01-12 16:18:33 -0800409 const MtdPartition *partition = ctx->partition;
410 int fd = ctx->fd;
411
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800412 off_t pos = lseek(fd, 0, SEEK_CUR);
413 if (pos == (off_t) -1) return 1;
414
415 ssize_t size = partition->erase_size;
416 while (pos + size <= (int) partition->size) {
417 loff_t bpos = pos;
418 if (ioctl(fd, MEMGETBADBLOCK, &bpos) > 0) {
Doug Zongker22d79a52010-01-12 16:18:33 -0800419 add_bad_block_offset(ctx, pos);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800420 fprintf(stderr, "mtd: not writing bad block at 0x%08lx\n", pos);
421 pos += partition->erase_size;
422 continue; // Don't try to erase known factory-bad blocks.
423 }
424
425 struct erase_info_user erase_info;
426 erase_info.start = pos;
427 erase_info.length = size;
428 int retry;
429 for (retry = 0; retry < 2; ++retry) {
430 if (ioctl(fd, MEMERASE, &erase_info) < 0) {
431 fprintf(stderr, "mtd: erase failure at 0x%08lx (%s)\n",
432 pos, strerror(errno));
433 continue;
434 }
435 if (lseek(fd, pos, SEEK_SET) != pos ||
436 write(fd, data, size) != size) {
437 fprintf(stderr, "mtd: write error at 0x%08lx (%s)\n",
438 pos, strerror(errno));
439 }
440
441 char verify[size];
442 if (lseek(fd, pos, SEEK_SET) != pos ||
443 read(fd, verify, size) != size) {
444 fprintf(stderr, "mtd: re-read error at 0x%08lx (%s)\n",
445 pos, strerror(errno));
446 continue;
447 }
448 if (memcmp(data, verify, size) != 0) {
449 fprintf(stderr, "mtd: verification error at 0x%08lx (%s)\n",
450 pos, strerror(errno));
451 continue;
452 }
453
454 if (retry > 0) {
455 fprintf(stderr, "mtd: wrote block after %d retries\n", retry);
456 }
457 return 0; // Success!
458 }
459
460 // Try to erase it once more as we give up on this block
Doug Zongker22d79a52010-01-12 16:18:33 -0800461 add_bad_block_offset(ctx, pos);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800462 fprintf(stderr, "mtd: skipping write block at 0x%08lx\n", pos);
463 ioctl(fd, MEMERASE, &erase_info);
464 pos += partition->erase_size;
465 }
466
467 // Ran out of space on the device
468 errno = ENOSPC;
469 return -1;
470}
471
472ssize_t mtd_write_data(MtdWriteContext *ctx, const char *data, size_t len)
473{
474 size_t wrote = 0;
475 while (wrote < len) {
476 // Coalesce partial writes into complete blocks
477 if (ctx->stored > 0 || len - wrote < ctx->partition->erase_size) {
478 size_t avail = ctx->partition->erase_size - ctx->stored;
479 size_t copy = len - wrote < avail ? len - wrote : avail;
480 memcpy(ctx->buffer + ctx->stored, data + wrote, copy);
481 ctx->stored += copy;
482 wrote += copy;
483 }
484
485 // If a complete block was accumulated, write it
486 if (ctx->stored == ctx->partition->erase_size) {
Doug Zongker22d79a52010-01-12 16:18:33 -0800487 if (write_block(ctx, ctx->buffer)) return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800488 ctx->stored = 0;
489 }
490
491 // Write complete blocks directly from the user's buffer
492 while (ctx->stored == 0 && len - wrote >= ctx->partition->erase_size) {
Doug Zongker22d79a52010-01-12 16:18:33 -0800493 if (write_block(ctx, data + wrote)) return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800494 wrote += ctx->partition->erase_size;
495 }
496 }
497
498 return wrote;
499}
500
501off_t mtd_erase_blocks(MtdWriteContext *ctx, int blocks)
502{
503 // Zero-pad and write any pending data to get us to a block boundary
504 if (ctx->stored > 0) {
505 size_t zero = ctx->partition->erase_size - ctx->stored;
506 memset(ctx->buffer + ctx->stored, 0, zero);
Doug Zongker22d79a52010-01-12 16:18:33 -0800507 if (write_block(ctx, ctx->buffer)) return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800508 ctx->stored = 0;
509 }
510
511 off_t pos = lseek(ctx->fd, 0, SEEK_CUR);
512 if ((off_t) pos == (off_t) -1) return pos;
513
514 const int total = (ctx->partition->size - pos) / ctx->partition->erase_size;
515 if (blocks < 0) blocks = total;
516 if (blocks > total) {
517 errno = ENOSPC;
518 return -1;
519 }
520
521 // Erase the specified number of blocks
522 while (blocks-- > 0) {
523 loff_t bpos = pos;
524 if (ioctl(ctx->fd, MEMGETBADBLOCK, &bpos) > 0) {
525 fprintf(stderr, "mtd: not erasing bad block at 0x%08lx\n", pos);
526 pos += ctx->partition->erase_size;
527 continue; // Don't try to erase known factory-bad blocks.
528 }
529
530 struct erase_info_user erase_info;
531 erase_info.start = pos;
532 erase_info.length = ctx->partition->erase_size;
533 if (ioctl(ctx->fd, MEMERASE, &erase_info) < 0) {
534 fprintf(stderr, "mtd: erase failure at 0x%08lx\n", pos);
535 }
536 pos += ctx->partition->erase_size;
537 }
538
539 return pos;
540}
541
542int mtd_write_close(MtdWriteContext *ctx)
543{
544 int r = 0;
545 // Make sure any pending data gets written
546 if (mtd_erase_blocks(ctx, 0) == (off_t) -1) r = -1;
547 if (close(ctx->fd)) r = -1;
Doug Zongker22d79a52010-01-12 16:18:33 -0800548 free(ctx->bad_block_offsets);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800549 free(ctx->buffer);
550 free(ctx);
551 return r;
552}
Doug Zongker22d79a52010-01-12 16:18:33 -0800553
554/* Return the offset of the first good block at or after pos (which
555 * might be pos itself).
556 */
557off_t mtd_find_write_start(MtdWriteContext *ctx, off_t pos) {
558 int i;
559 for (i = 0; i < ctx->bad_block_count; ++i) {
560 if (ctx->bad_block_offsets[i] == pos) {
561 pos += ctx->partition->erase_size;
562 } else if (ctx->bad_block_offsets[i] > pos) {
563 return pos;
564 }
565 }
566 return pos;
567}
Koushik K. Dutta8ce0be42010-02-20 15:59:06 -0800568
569
570ssize_t mtd_read_raw(MtdReadContext *ctx, char *data, size_t len)
571{
572 static const int SPARE_SIZE = (2048 >> 5);
573 struct mtd_info_user mtd_info;
574 struct mtd_oob_buf oob_buf;
575 struct nand_ecclayout ecc_layout;
576 struct nand_oobfree *fp;
577 unsigned char ecc[SPARE_SIZE];
578 char *src, *dst;
579 int i, n, ret;
580
581/*
582 * FIXME: These two ioctls should be cached in MtdReadContext.
583 */
584 ret = ioctl(ctx->fd, MEMGETINFO, &mtd_info);
585 if (ret < 0)
586 return -1;
587
588 ret = ioctl(ctx->fd, ECCGETLAYOUT, &ecc_layout);
589 if (ret < 0)
590 return -1;
591
592 ctx->read_size = mtd_info.writesize;
593 ctx->consumed = ctx->read_size;
594
595/*
596 * Read next good data block.
597 */
598 ret = read_block(ctx->partition, ctx->fd, data);
599 if (ret < 0)
600 return -1;
601
602 dst = src = data + ctx->read_size;
603
604/*
605 * Read OOB data for last block read in read_block().
606 */
607 oob_buf.start = lseek(ctx->fd, 0, SEEK_CUR) - ctx->read_size;
608 oob_buf.length = mtd_info.oobsize;
609 oob_buf.ptr = (unsigned char *) src;
610
611 ret = ioctl(ctx->fd, MEMREADOOB, &oob_buf);
612 if (ret < 0)
613 return -1;
614
615/*
616 * As yaffs and yaffs2 use mode MEM_OOB_AUTO, but mtdchar uses
617 * MEM_OOB_PLACE, copy the spare data down the hard way.
618 *
619 * Safe away ECC data:
620 */
621 for (i = 0; i < ecc_layout.eccbytes; i++) {
622 ecc[i] = src[ecc_layout.eccpos[i]];
623 }
624 for ( ; i < SPARE_SIZE; i++) {
625 ecc[i] = 0;
626 }
627
628/*
629 * Copy yaffs2 spare data down.
630 */
631 n = ecc_layout.oobavail;
632 fp = &ecc_layout.oobfree[0];
633 while (n) {
634 if (fp->offset) {
635 memmove(dst, src + fp->offset, fp->length);
636 }
637 dst += fp->length;
638 n -= fp->length;
639 ++fp;
640 }
641
642/*
643 * Restore ECC data behind spare data.
644 */
645 memcpy(dst, ecc, (ctx->read_size >> 5) - ecc_layout.oobavail);
646
647 return ctx->read_size + (ctx->read_size >> 5);
648}