blob: fc067669b9ab8856a46be5c9862e31581f335566 [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
286 off_t pos = lseek(fd, 0, SEEK_CUR);
287 ssize_t size = partition->erase_size;
288 while (pos + size <= (int) partition->size) {
289 if (lseek(fd, pos, SEEK_SET) != pos || read(fd, data, size) != size) {
290 fprintf(stderr, "mtd: read error at 0x%08lx (%s)\n",
291 pos, strerror(errno));
292 } else if (ioctl(fd, ECCGETSTATS, &after)) {
293 fprintf(stderr, "mtd: ECCGETSTATS error (%s)\n", strerror(errno));
294 return -1;
295 } else if (after.failed != before.failed) {
296 fprintf(stderr, "mtd: ECC errors (%d soft, %d hard) at 0x%08lx\n",
297 after.corrected - before.corrected,
298 after.failed - before.failed, pos);
299 } else {
Doug Zongkerbec02d52009-07-01 12:09:29 -0700300 int i;
301 for (i = 0; i < size; ++i) {
302 if (data[i] != 0) {
303 return 0; // Success!
304 }
305 }
306 fprintf(stderr, "mtd: read all-zero block at 0x%08lx; skipping\n",
307 pos);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800308 }
309
310 pos += partition->erase_size;
311 }
312
313 errno = ENOSPC;
314 return -1;
315}
316
317ssize_t mtd_read_data(MtdReadContext *ctx, char *data, size_t len)
318{
319 ssize_t read = 0;
320 while (read < (int) len) {
321 if (ctx->consumed < ctx->partition->erase_size) {
322 size_t avail = ctx->partition->erase_size - ctx->consumed;
323 size_t copy = len - read < avail ? len - read : avail;
324 memcpy(data + read, ctx->buffer + ctx->consumed, copy);
325 ctx->consumed += copy;
326 read += copy;
327 }
328
329 // Read complete blocks directly into the user's buffer
330 while (ctx->consumed == ctx->partition->erase_size &&
331 len - read >= ctx->partition->erase_size) {
332 if (read_block(ctx->partition, ctx->fd, data + read)) return -1;
333 read += ctx->partition->erase_size;
334 }
335
Doug Zongkerbec02d52009-07-01 12:09:29 -0700336 if (read >= len) {
337 return read;
338 }
339
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800340 // Read the next block into the buffer
341 if (ctx->consumed == ctx->partition->erase_size && read < (int) len) {
342 if (read_block(ctx->partition, ctx->fd, ctx->buffer)) return -1;
343 ctx->consumed = 0;
344 }
345 }
346
347 return read;
348}
349
350void mtd_read_close(MtdReadContext *ctx)
351{
352 close(ctx->fd);
353 free(ctx->buffer);
354 free(ctx);
355}
356
357MtdWriteContext *mtd_write_partition(const MtdPartition *partition)
358{
359 MtdWriteContext *ctx = (MtdWriteContext*) malloc(sizeof(MtdWriteContext));
360 if (ctx == NULL) return NULL;
361
362 ctx->buffer = malloc(partition->erase_size);
363 if (ctx->buffer == NULL) {
364 free(ctx);
365 return NULL;
366 }
367
368 char mtddevname[32];
369 sprintf(mtddevname, "/dev/mtd/mtd%d", partition->device_index);
370 ctx->fd = open(mtddevname, O_RDWR);
371 if (ctx->fd < 0) {
372 free(ctx->buffer);
373 free(ctx);
374 return NULL;
375 }
376
377 ctx->partition = partition;
378 ctx->stored = 0;
379 return ctx;
380}
381
382static int write_block(const MtdPartition *partition, int fd, const char *data)
383{
384 off_t pos = lseek(fd, 0, SEEK_CUR);
385 if (pos == (off_t) -1) return 1;
386
387 ssize_t size = partition->erase_size;
388 while (pos + size <= (int) partition->size) {
389 loff_t bpos = pos;
390 if (ioctl(fd, MEMGETBADBLOCK, &bpos) > 0) {
391 fprintf(stderr, "mtd: not writing bad block at 0x%08lx\n", pos);
392 pos += partition->erase_size;
393 continue; // Don't try to erase known factory-bad blocks.
394 }
395
396 struct erase_info_user erase_info;
397 erase_info.start = pos;
398 erase_info.length = size;
399 int retry;
400 for (retry = 0; retry < 2; ++retry) {
401 if (ioctl(fd, MEMERASE, &erase_info) < 0) {
402 fprintf(stderr, "mtd: erase failure at 0x%08lx (%s)\n",
403 pos, strerror(errno));
404 continue;
405 }
406 if (lseek(fd, pos, SEEK_SET) != pos ||
407 write(fd, data, size) != size) {
408 fprintf(stderr, "mtd: write error at 0x%08lx (%s)\n",
409 pos, strerror(errno));
410 }
411
412 char verify[size];
413 if (lseek(fd, pos, SEEK_SET) != pos ||
414 read(fd, verify, size) != size) {
415 fprintf(stderr, "mtd: re-read error at 0x%08lx (%s)\n",
416 pos, strerror(errno));
417 continue;
418 }
419 if (memcmp(data, verify, size) != 0) {
420 fprintf(stderr, "mtd: verification error at 0x%08lx (%s)\n",
421 pos, strerror(errno));
422 continue;
423 }
424
425 if (retry > 0) {
426 fprintf(stderr, "mtd: wrote block after %d retries\n", retry);
427 }
428 return 0; // Success!
429 }
430
431 // Try to erase it once more as we give up on this block
432 fprintf(stderr, "mtd: skipping write block at 0x%08lx\n", pos);
433 ioctl(fd, MEMERASE, &erase_info);
434 pos += partition->erase_size;
435 }
436
437 // Ran out of space on the device
438 errno = ENOSPC;
439 return -1;
440}
441
442ssize_t mtd_write_data(MtdWriteContext *ctx, const char *data, size_t len)
443{
444 size_t wrote = 0;
445 while (wrote < len) {
446 // Coalesce partial writes into complete blocks
447 if (ctx->stored > 0 || len - wrote < ctx->partition->erase_size) {
448 size_t avail = ctx->partition->erase_size - ctx->stored;
449 size_t copy = len - wrote < avail ? len - wrote : avail;
450 memcpy(ctx->buffer + ctx->stored, data + wrote, copy);
451 ctx->stored += copy;
452 wrote += copy;
453 }
454
455 // If a complete block was accumulated, write it
456 if (ctx->stored == ctx->partition->erase_size) {
457 if (write_block(ctx->partition, ctx->fd, ctx->buffer)) return -1;
458 ctx->stored = 0;
459 }
460
461 // Write complete blocks directly from the user's buffer
462 while (ctx->stored == 0 && len - wrote >= ctx->partition->erase_size) {
463 if (write_block(ctx->partition, ctx->fd, data + wrote)) return -1;
464 wrote += ctx->partition->erase_size;
465 }
466 }
467
468 return wrote;
469}
470
471off_t mtd_erase_blocks(MtdWriteContext *ctx, int blocks)
472{
473 // Zero-pad and write any pending data to get us to a block boundary
474 if (ctx->stored > 0) {
475 size_t zero = ctx->partition->erase_size - ctx->stored;
476 memset(ctx->buffer + ctx->stored, 0, zero);
477 if (write_block(ctx->partition, ctx->fd, ctx->buffer)) return -1;
478 ctx->stored = 0;
479 }
480
481 off_t pos = lseek(ctx->fd, 0, SEEK_CUR);
482 if ((off_t) pos == (off_t) -1) return pos;
483
484 const int total = (ctx->partition->size - pos) / ctx->partition->erase_size;
485 if (blocks < 0) blocks = total;
486 if (blocks > total) {
487 errno = ENOSPC;
488 return -1;
489 }
490
491 // Erase the specified number of blocks
492 while (blocks-- > 0) {
493 loff_t bpos = pos;
494 if (ioctl(ctx->fd, MEMGETBADBLOCK, &bpos) > 0) {
495 fprintf(stderr, "mtd: not erasing bad block at 0x%08lx\n", pos);
496 pos += ctx->partition->erase_size;
497 continue; // Don't try to erase known factory-bad blocks.
498 }
499
500 struct erase_info_user erase_info;
501 erase_info.start = pos;
502 erase_info.length = ctx->partition->erase_size;
503 if (ioctl(ctx->fd, MEMERASE, &erase_info) < 0) {
504 fprintf(stderr, "mtd: erase failure at 0x%08lx\n", pos);
505 }
506 pos += ctx->partition->erase_size;
507 }
508
509 return pos;
510}
511
512int mtd_write_close(MtdWriteContext *ctx)
513{
514 int r = 0;
515 // Make sure any pending data gets written
516 if (mtd_erase_blocks(ctx, 0) == (off_t) -1) r = -1;
517 if (close(ctx->fd)) r = -1;
518 free(ctx->buffer);
519 free(ctx);
520 return r;
521}