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