blob: 8d32520596996e93511b463ff435cce65ec91734 [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;
41 size_t consumed;
42 int fd;
43};
44
45struct MtdWriteContext {
46 const MtdPartition *partition;
47 char *buffer;
48 size_t stored;
49 int fd;
50};
51
52typedef struct {
53 MtdPartition *partitions;
54 int partitions_allocd;
55 int partition_count;
56} MtdState;
57
58static MtdState g_mtd_state = {
59 NULL, // partitions
60 0, // partitions_allocd
61 -1 // partition_count
62};
63
64#define MTD_PROC_FILENAME "/proc/mtd"
65
66int
67mtd_scan_partitions()
68{
69 char buf[2048];
70 const char *bufp;
71 int fd;
72 int i;
73 ssize_t nbytes;
74
75 if (g_mtd_state.partitions == NULL) {
76 const int nump = 32;
77 MtdPartition *partitions = malloc(nump * sizeof(*partitions));
78 if (partitions == NULL) {
79 errno = ENOMEM;
80 return -1;
81 }
82 g_mtd_state.partitions = partitions;
83 g_mtd_state.partitions_allocd = nump;
84 memset(partitions, 0, nump * sizeof(*partitions));
85 }
86 g_mtd_state.partition_count = 0;
87
88 /* Initialize all of the entries to make things easier later.
89 * (Lets us handle sparsely-numbered partitions, which
90 * may not even be possible.)
91 */
92 for (i = 0; i < g_mtd_state.partitions_allocd; i++) {
93 MtdPartition *p = &g_mtd_state.partitions[i];
94 if (p->name != NULL) {
95 free(p->name);
96 p->name = NULL;
97 }
98 p->device_index = -1;
99 }
100
101 /* Open and read the file contents.
102 */
103 fd = open(MTD_PROC_FILENAME, O_RDONLY);
104 if (fd < 0) {
105 goto bail;
106 }
107 nbytes = read(fd, buf, sizeof(buf) - 1);
108 close(fd);
109 if (nbytes < 0) {
110 goto bail;
111 }
112 buf[nbytes] = '\0';
113
114 /* Parse the contents of the file, which looks like:
115 *
116 * # cat /proc/mtd
117 * dev: size erasesize name
118 * mtd0: 00080000 00020000 "bootloader"
119 * mtd1: 00400000 00020000 "mfg_and_gsm"
120 * mtd2: 00400000 00020000 "0000000c"
121 * mtd3: 00200000 00020000 "0000000d"
122 * mtd4: 04000000 00020000 "system"
123 * mtd5: 03280000 00020000 "userdata"
124 */
125 bufp = buf;
126 while (nbytes > 0) {
127 int mtdnum, mtdsize, mtderasesize;
128 int matches;
129 char mtdname[64];
130 mtdname[0] = '\0';
131 mtdnum = -1;
132
133 matches = sscanf(bufp, "mtd%d: %x %x \"%63[^\"]",
134 &mtdnum, &mtdsize, &mtderasesize, mtdname);
135 /* This will fail on the first line, which just contains
136 * column headers.
137 */
138 if (matches == 4) {
139 MtdPartition *p = &g_mtd_state.partitions[mtdnum];
140 p->device_index = mtdnum;
141 p->size = mtdsize;
142 p->erase_size = mtderasesize;
143 p->name = strdup(mtdname);
144 if (p->name == NULL) {
145 errno = ENOMEM;
146 goto bail;
147 }
148 g_mtd_state.partition_count++;
149 }
150
151 /* Eat the line.
152 */
153 while (nbytes > 0 && *bufp != '\n') {
154 bufp++;
155 nbytes--;
156 }
157 if (nbytes > 0) {
158 bufp++;
159 nbytes--;
160 }
161 }
162
163 return g_mtd_state.partition_count;
164
165bail:
166 // keep "partitions" around so we can free the names on a rescan.
167 g_mtd_state.partition_count = -1;
168 return -1;
169}
170
171const MtdPartition *
172mtd_find_partition_by_name(const char *name)
173{
174 if (g_mtd_state.partitions != NULL) {
175 int i;
176 for (i = 0; i < g_mtd_state.partitions_allocd; i++) {
177 MtdPartition *p = &g_mtd_state.partitions[i];
178 if (p->device_index >= 0 && p->name != NULL) {
179 if (strcmp(p->name, name) == 0) {
180 return p;
181 }
182 }
183 }
184 }
185 return NULL;
186}
187
188int
189mtd_mount_partition(const MtdPartition *partition, const char *mount_point,
190 const char *filesystem, int read_only)
191{
192 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
193 char devname[64];
194 int rv = -1;
195
196 sprintf(devname, "/dev/block/mtdblock%d", partition->device_index);
197 if (!read_only) {
198 rv = mount(devname, mount_point, filesystem, flags, NULL);
199 }
200 if (read_only || rv < 0) {
201 rv = mount(devname, mount_point, filesystem, flags | MS_RDONLY, 0);
202 if (rv < 0) {
203 printf("Failed to mount %s on %s: %s\n",
204 devname, mount_point, strerror(errno));
205 } else {
206 printf("Mount %s on %s read-only\n", devname, mount_point);
207 }
208 }
209#if 1 //TODO: figure out why this is happening; remove include of stat.h
210 if (rv >= 0) {
211 /* For some reason, the x bits sometimes aren't set on the root
212 * of mounted volumes.
213 */
214 struct stat st;
215 rv = stat(mount_point, &st);
216 if (rv < 0) {
217 return rv;
218 }
219 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
220 if (new_mode != st.st_mode) {
221printf("Fixing execute permissions for %s\n", mount_point);
222 rv = chmod(mount_point, new_mode);
223 if (rv < 0) {
224 printf("Couldn't fix permissions for %s: %s\n",
225 mount_point, strerror(errno));
226 }
227 }
228 }
229#endif
230 return rv;
231}
232
233int
234mtd_partition_info(const MtdPartition *partition,
235 size_t *total_size, size_t *erase_size, size_t *write_size)
236{
237 char mtddevname[32];
238 sprintf(mtddevname, "/dev/mtd/mtd%d", partition->device_index);
239 int fd = open(mtddevname, O_RDONLY);
240 if (fd < 0) return -1;
241
242 struct mtd_info_user mtd_info;
243 int ret = ioctl(fd, MEMGETINFO, &mtd_info);
244 close(fd);
245 if (ret < 0) return -1;
246
247 if (total_size != NULL) *total_size = mtd_info.size;
248 if (erase_size != NULL) *erase_size = mtd_info.erasesize;
249 if (write_size != NULL) *write_size = mtd_info.writesize;
250 return 0;
251}
252
253MtdReadContext *mtd_read_partition(const MtdPartition *partition)
254{
255 MtdReadContext *ctx = (MtdReadContext*) malloc(sizeof(MtdReadContext));
256 if (ctx == NULL) return NULL;
257
258 ctx->buffer = malloc(partition->erase_size);
259 if (ctx->buffer == NULL) {
260 free(ctx);
261 return NULL;
262 }
263
264 char mtddevname[32];
265 sprintf(mtddevname, "/dev/mtd/mtd%d", partition->device_index);
266 ctx->fd = open(mtddevname, O_RDONLY);
267 if (ctx->fd < 0) {
268 free(ctx);
269 free(ctx->buffer);
270 return NULL;
271 }
272
273 ctx->partition = partition;
274 ctx->consumed = partition->erase_size;
275 return ctx;
276}
277
278static int read_block(const MtdPartition *partition, int fd, char *data)
279{
280 struct mtd_ecc_stats before, after;
281 if (ioctl(fd, ECCGETSTATS, &before)) {
282 fprintf(stderr, "mtd: ECCGETSTATS error (%s)\n", strerror(errno));
283 return -1;
284 }
285
Doug Zongker17a47092009-12-14 18:03:27 -0800286 loff_t pos = lseek64(fd, 0, SEEK_CUR);
287
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800288 ssize_t size = partition->erase_size;
Doug Zongker17a47092009-12-14 18:03:27 -0800289 int mgbb;
290
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800291 while (pos + size <= (int) partition->size) {
Doug Zongker17a47092009-12-14 18:03:27 -0800292 if (lseek64(fd, pos, SEEK_SET) != pos || read(fd, data, size) != size) {
293 fprintf(stderr, "mtd: read error at 0x%08llx (%s)\n",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800294 pos, strerror(errno));
295 } else if (ioctl(fd, ECCGETSTATS, &after)) {
296 fprintf(stderr, "mtd: ECCGETSTATS error (%s)\n", strerror(errno));
297 return -1;
298 } else if (after.failed != before.failed) {
Doug Zongker17a47092009-12-14 18:03:27 -0800299 fprintf(stderr, "mtd: ECC errors (%d soft, %d hard) at 0x%08llx\n",
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800300 after.corrected - before.corrected,
301 after.failed - before.failed, pos);
Doug Zongker17a47092009-12-14 18:03:27 -0800302 } else if ((mgbb = ioctl(fd, MEMGETBADBLOCK, &pos))) {
303 fprintf(stderr,
304 "mtd: MEMGETBADBLOCK returned %d at 0x%08llx (errno=%d)\n",
305 mgbb, pos, errno);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800306 } else {
Doug Zongkerbec02d52009-07-01 12:09:29 -0700307 int i;
308 for (i = 0; i < size; ++i) {
309 if (data[i] != 0) {
310 return 0; // Success!
311 }
312 }
Doug Zongker17a47092009-12-14 18:03:27 -0800313 fprintf(stderr, "mtd: read all-zero block at 0x%08llx; skipping\n",
Doug Zongkerbec02d52009-07-01 12:09:29 -0700314 pos);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800315 }
316
317 pos += partition->erase_size;
318 }
319
320 errno = ENOSPC;
321 return -1;
322}
323
324ssize_t mtd_read_data(MtdReadContext *ctx, char *data, size_t len)
325{
326 ssize_t read = 0;
327 while (read < (int) len) {
328 if (ctx->consumed < ctx->partition->erase_size) {
329 size_t avail = ctx->partition->erase_size - ctx->consumed;
330 size_t copy = len - read < avail ? len - read : avail;
331 memcpy(data + read, ctx->buffer + ctx->consumed, copy);
332 ctx->consumed += copy;
333 read += copy;
334 }
335
336 // Read complete blocks directly into the user's buffer
337 while (ctx->consumed == ctx->partition->erase_size &&
338 len - read >= ctx->partition->erase_size) {
339 if (read_block(ctx->partition, ctx->fd, data + read)) return -1;
340 read += ctx->partition->erase_size;
341 }
342
Doug Zongkerbec02d52009-07-01 12:09:29 -0700343 if (read >= len) {
344 return read;
345 }
346
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800347 // Read the next block into the buffer
348 if (ctx->consumed == ctx->partition->erase_size && read < (int) len) {
349 if (read_block(ctx->partition, ctx->fd, ctx->buffer)) return -1;
350 ctx->consumed = 0;
351 }
352 }
353
354 return read;
355}
356
357void mtd_read_close(MtdReadContext *ctx)
358{
359 close(ctx->fd);
360 free(ctx->buffer);
361 free(ctx);
362}
363
364MtdWriteContext *mtd_write_partition(const MtdPartition *partition)
365{
366 MtdWriteContext *ctx = (MtdWriteContext*) malloc(sizeof(MtdWriteContext));
367 if (ctx == NULL) return NULL;
368
369 ctx->buffer = malloc(partition->erase_size);
370 if (ctx->buffer == NULL) {
371 free(ctx);
372 return NULL;
373 }
374
375 char mtddevname[32];
376 sprintf(mtddevname, "/dev/mtd/mtd%d", partition->device_index);
377 ctx->fd = open(mtddevname, O_RDWR);
378 if (ctx->fd < 0) {
379 free(ctx->buffer);
380 free(ctx);
381 return NULL;
382 }
383
384 ctx->partition = partition;
385 ctx->stored = 0;
386 return ctx;
387}
388
389static int write_block(const MtdPartition *partition, int fd, const char *data)
390{
391 off_t pos = lseek(fd, 0, SEEK_CUR);
392 if (pos == (off_t) -1) return 1;
393
394 ssize_t size = partition->erase_size;
395 while (pos + size <= (int) partition->size) {
396 loff_t bpos = pos;
397 if (ioctl(fd, MEMGETBADBLOCK, &bpos) > 0) {
398 fprintf(stderr, "mtd: not writing bad block at 0x%08lx\n", pos);
399 pos += partition->erase_size;
400 continue; // Don't try to erase known factory-bad blocks.
401 }
402
403 struct erase_info_user erase_info;
404 erase_info.start = pos;
405 erase_info.length = size;
406 int retry;
407 for (retry = 0; retry < 2; ++retry) {
408 if (ioctl(fd, MEMERASE, &erase_info) < 0) {
409 fprintf(stderr, "mtd: erase failure at 0x%08lx (%s)\n",
410 pos, strerror(errno));
411 continue;
412 }
413 if (lseek(fd, pos, SEEK_SET) != pos ||
414 write(fd, data, size) != size) {
415 fprintf(stderr, "mtd: write error at 0x%08lx (%s)\n",
416 pos, strerror(errno));
417 }
418
419 char verify[size];
420 if (lseek(fd, pos, SEEK_SET) != pos ||
421 read(fd, verify, size) != size) {
422 fprintf(stderr, "mtd: re-read error at 0x%08lx (%s)\n",
423 pos, strerror(errno));
424 continue;
425 }
426 if (memcmp(data, verify, size) != 0) {
427 fprintf(stderr, "mtd: verification error at 0x%08lx (%s)\n",
428 pos, strerror(errno));
429 continue;
430 }
431
432 if (retry > 0) {
433 fprintf(stderr, "mtd: wrote block after %d retries\n", retry);
434 }
435 return 0; // Success!
436 }
437
438 // Try to erase it once more as we give up on this block
439 fprintf(stderr, "mtd: skipping write block at 0x%08lx\n", pos);
440 ioctl(fd, MEMERASE, &erase_info);
441 pos += partition->erase_size;
442 }
443
444 // Ran out of space on the device
445 errno = ENOSPC;
446 return -1;
447}
448
449ssize_t mtd_write_data(MtdWriteContext *ctx, const char *data, size_t len)
450{
451 size_t wrote = 0;
452 while (wrote < len) {
453 // Coalesce partial writes into complete blocks
454 if (ctx->stored > 0 || len - wrote < ctx->partition->erase_size) {
455 size_t avail = ctx->partition->erase_size - ctx->stored;
456 size_t copy = len - wrote < avail ? len - wrote : avail;
457 memcpy(ctx->buffer + ctx->stored, data + wrote, copy);
458 ctx->stored += copy;
459 wrote += copy;
460 }
461
462 // If a complete block was accumulated, write it
463 if (ctx->stored == ctx->partition->erase_size) {
464 if (write_block(ctx->partition, ctx->fd, ctx->buffer)) return -1;
465 ctx->stored = 0;
466 }
467
468 // Write complete blocks directly from the user's buffer
469 while (ctx->stored == 0 && len - wrote >= ctx->partition->erase_size) {
470 if (write_block(ctx->partition, ctx->fd, data + wrote)) return -1;
471 wrote += ctx->partition->erase_size;
472 }
473 }
474
475 return wrote;
476}
477
478off_t mtd_erase_blocks(MtdWriteContext *ctx, int blocks)
479{
480 // Zero-pad and write any pending data to get us to a block boundary
481 if (ctx->stored > 0) {
482 size_t zero = ctx->partition->erase_size - ctx->stored;
483 memset(ctx->buffer + ctx->stored, 0, zero);
484 if (write_block(ctx->partition, ctx->fd, ctx->buffer)) return -1;
485 ctx->stored = 0;
486 }
487
488 off_t pos = lseek(ctx->fd, 0, SEEK_CUR);
489 if ((off_t) pos == (off_t) -1) return pos;
490
491 const int total = (ctx->partition->size - pos) / ctx->partition->erase_size;
492 if (blocks < 0) blocks = total;
493 if (blocks > total) {
494 errno = ENOSPC;
495 return -1;
496 }
497
498 // Erase the specified number of blocks
499 while (blocks-- > 0) {
500 loff_t bpos = pos;
501 if (ioctl(ctx->fd, MEMGETBADBLOCK, &bpos) > 0) {
502 fprintf(stderr, "mtd: not erasing bad block at 0x%08lx\n", pos);
503 pos += ctx->partition->erase_size;
504 continue; // Don't try to erase known factory-bad blocks.
505 }
506
507 struct erase_info_user erase_info;
508 erase_info.start = pos;
509 erase_info.length = ctx->partition->erase_size;
510 if (ioctl(ctx->fd, MEMERASE, &erase_info) < 0) {
511 fprintf(stderr, "mtd: erase failure at 0x%08lx\n", pos);
512 }
513 pos += ctx->partition->erase_size;
514 }
515
516 return pos;
517}
518
519int mtd_write_close(MtdWriteContext *ctx)
520{
521 int r = 0;
522 // Make sure any pending data gets written
523 if (mtd_erase_blocks(ctx, 0) == (off_t) -1) r = -1;
524 if (close(ctx->fd)) r = -1;
525 free(ctx->buffer);
526 free(ctx);
527 return r;
528}