1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2007 The Android Open Source Project
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Licensed under the Apache License, Version 2.0 (the "License");
5*4882a593Smuzhiyun * you may not use this file except in compliance with the License.
6*4882a593Smuzhiyun * You may obtain a copy of the License at
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * http://www.apache.org/licenses/LICENSE-2.0
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Unless required by applicable law or agreed to in writing, software
11*4882a593Smuzhiyun * distributed under the License is distributed on an "AS IS" BASIS,
12*4882a593Smuzhiyun * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4882a593Smuzhiyun * See the License for the specific language governing permissions and
14*4882a593Smuzhiyun * limitations under the License.
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <stdio.h>
18*4882a593Smuzhiyun #include <stdlib.h>
19*4882a593Smuzhiyun #include <string.h>
20*4882a593Smuzhiyun #include <unistd.h>
21*4882a593Smuzhiyun #include <fcntl.h>
22*4882a593Smuzhiyun #include <errno.h>
23*4882a593Smuzhiyun #include <sys/mount.h> // for _IOW, _IOR, mount()
24*4882a593Smuzhiyun #include <sys/stat.h>
25*4882a593Smuzhiyun #include <mtd/mtd-user.h>
26*4882a593Smuzhiyun #undef NDEBUG
27*4882a593Smuzhiyun #include <assert.h>
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include "mtdutils.h"
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun struct MtdReadContext {
32*4882a593Smuzhiyun const MtdPartition *partition;
33*4882a593Smuzhiyun char *buffer;
34*4882a593Smuzhiyun size_t consumed;
35*4882a593Smuzhiyun int fd;
36*4882a593Smuzhiyun };
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun struct MtdWriteContext {
39*4882a593Smuzhiyun const MtdPartition *partition;
40*4882a593Smuzhiyun char *buffer;
41*4882a593Smuzhiyun size_t stored;
42*4882a593Smuzhiyun int fd;
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun off_t* bad_block_offsets;
45*4882a593Smuzhiyun int bad_block_alloc;
46*4882a593Smuzhiyun int bad_block_count;
47*4882a593Smuzhiyun };
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun typedef struct {
50*4882a593Smuzhiyun MtdPartition *partitions;
51*4882a593Smuzhiyun int partitions_allocd;
52*4882a593Smuzhiyun int partition_count;
53*4882a593Smuzhiyun } MtdState;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun static MtdState g_mtd_state = {
56*4882a593Smuzhiyun NULL, // partitions
57*4882a593Smuzhiyun 0, // partitions_allocd
58*4882a593Smuzhiyun -1 // partition_count
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #define MTD_PROC_FILENAME "/proc/mtd"
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun int
mtd_scan_partitions()64*4882a593Smuzhiyun mtd_scan_partitions()
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun char buf[2048];
67*4882a593Smuzhiyun const char *bufp;
68*4882a593Smuzhiyun int fd;
69*4882a593Smuzhiyun int i;
70*4882a593Smuzhiyun ssize_t nbytes;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun if (g_mtd_state.partitions == NULL) {
73*4882a593Smuzhiyun const int nump = 32;
74*4882a593Smuzhiyun MtdPartition *partitions = malloc(nump * sizeof(*partitions));
75*4882a593Smuzhiyun if (partitions == NULL) {
76*4882a593Smuzhiyun errno = ENOMEM;
77*4882a593Smuzhiyun return -1;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun g_mtd_state.partitions = partitions;
80*4882a593Smuzhiyun g_mtd_state.partitions_allocd = nump;
81*4882a593Smuzhiyun memset(partitions, 0, nump * sizeof(*partitions));
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun g_mtd_state.partition_count = 0;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /* Initialize all of the entries to make things easier later.
86*4882a593Smuzhiyun * (Lets us handle sparsely-numbered partitions, which
87*4882a593Smuzhiyun * may not even be possible.)
88*4882a593Smuzhiyun */
89*4882a593Smuzhiyun for (i = 0; i < g_mtd_state.partitions_allocd; i++) {
90*4882a593Smuzhiyun MtdPartition *p = &g_mtd_state.partitions[i];
91*4882a593Smuzhiyun if (p->name != NULL) {
92*4882a593Smuzhiyun free(p->name);
93*4882a593Smuzhiyun p->name = NULL;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun p->device_index = -1;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /* Open and read the file contents.
99*4882a593Smuzhiyun */
100*4882a593Smuzhiyun fd = open(MTD_PROC_FILENAME, O_RDONLY);
101*4882a593Smuzhiyun if (fd < 0) {
102*4882a593Smuzhiyun goto bail;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun nbytes = read(fd, buf, sizeof(buf) - 1);
105*4882a593Smuzhiyun close(fd);
106*4882a593Smuzhiyun if (nbytes < 0) {
107*4882a593Smuzhiyun goto bail;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun buf[nbytes] = '\0';
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /* Parse the contents of the file, which looks like:
112*4882a593Smuzhiyun *
113*4882a593Smuzhiyun * # cat /proc/mtd
114*4882a593Smuzhiyun * dev: size erasesize name
115*4882a593Smuzhiyun * mtd0: 00080000 00020000 "bootloader"
116*4882a593Smuzhiyun * mtd1: 00400000 00020000 "mfg_and_gsm"
117*4882a593Smuzhiyun * mtd2: 00400000 00020000 "0000000c"
118*4882a593Smuzhiyun * mtd3: 00200000 00020000 "0000000d"
119*4882a593Smuzhiyun * mtd4: 04000000 00020000 "system"
120*4882a593Smuzhiyun * mtd5: 03280000 00020000 "userdata"
121*4882a593Smuzhiyun */
122*4882a593Smuzhiyun bufp = buf;
123*4882a593Smuzhiyun while (nbytes > 0) {
124*4882a593Smuzhiyun int mtdnum, mtdsize, mtderasesize;
125*4882a593Smuzhiyun int matches;
126*4882a593Smuzhiyun char mtdname[64];
127*4882a593Smuzhiyun mtdname[0] = '\0';
128*4882a593Smuzhiyun mtdnum = -1;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun matches = sscanf(bufp, "mtd%d: %x %x \"%63[^\"]",
131*4882a593Smuzhiyun &mtdnum, &mtdsize, &mtderasesize, mtdname);
132*4882a593Smuzhiyun /* This will fail on the first line, which just contains
133*4882a593Smuzhiyun * column headers.
134*4882a593Smuzhiyun */
135*4882a593Smuzhiyun if (matches == 4) {
136*4882a593Smuzhiyun MtdPartition *p = &g_mtd_state.partitions[mtdnum];
137*4882a593Smuzhiyun p->device_index = mtdnum;
138*4882a593Smuzhiyun p->size = mtdsize;
139*4882a593Smuzhiyun p->erase_size = mtderasesize;
140*4882a593Smuzhiyun p->name = strdup(mtdname);
141*4882a593Smuzhiyun if (p->name == NULL) {
142*4882a593Smuzhiyun errno = ENOMEM;
143*4882a593Smuzhiyun goto bail;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun g_mtd_state.partition_count++;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /* Eat the line.
149*4882a593Smuzhiyun */
150*4882a593Smuzhiyun while (nbytes > 0 && *bufp != '\n') {
151*4882a593Smuzhiyun bufp++;
152*4882a593Smuzhiyun nbytes--;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun if (nbytes > 0) {
155*4882a593Smuzhiyun bufp++;
156*4882a593Smuzhiyun nbytes--;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun return g_mtd_state.partition_count;
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun bail:
163*4882a593Smuzhiyun // keep "partitions" around so we can free the names on a rescan.
164*4882a593Smuzhiyun g_mtd_state.partition_count = -1;
165*4882a593Smuzhiyun return -1;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun const MtdPartition *
mtd_find_partition_by_name(const char * name)169*4882a593Smuzhiyun mtd_find_partition_by_name(const char *name)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun if (g_mtd_state.partitions != NULL) {
172*4882a593Smuzhiyun int i;
173*4882a593Smuzhiyun for (i = 0; i < g_mtd_state.partitions_allocd; i++) {
174*4882a593Smuzhiyun MtdPartition *p = &g_mtd_state.partitions[i];
175*4882a593Smuzhiyun if (p->device_index >= 0 && p->name != NULL) {
176*4882a593Smuzhiyun if (strcmp(p->name, name) == 0) {
177*4882a593Smuzhiyun return p;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun return NULL;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun int
mtd_mount_partition(const MtdPartition * partition,const char * mount_point,const char * filesystem,int read_only)186*4882a593Smuzhiyun mtd_mount_partition(const MtdPartition *partition, const char *mount_point,
187*4882a593Smuzhiyun const char *filesystem, int read_only)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
190*4882a593Smuzhiyun char devname[64];
191*4882a593Smuzhiyun int rv = -1;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun sprintf(devname, "/dev/block/mtdblock%d", partition->device_index);
194*4882a593Smuzhiyun if (!read_only) {
195*4882a593Smuzhiyun rv = mount(devname, mount_point, filesystem, flags, NULL);
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun if (read_only || rv < 0) {
198*4882a593Smuzhiyun rv = mount(devname, mount_point, filesystem, flags | MS_RDONLY, 0);
199*4882a593Smuzhiyun if (rv < 0) {
200*4882a593Smuzhiyun printf("Failed to mount %s on %s: %s\n",
201*4882a593Smuzhiyun devname, mount_point, strerror(errno));
202*4882a593Smuzhiyun } else {
203*4882a593Smuzhiyun printf("Mount %s on %s read-only\n", devname, mount_point);
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun #if 1 //TODO: figure out why this is happening; remove include of stat.h
207*4882a593Smuzhiyun if (rv >= 0) {
208*4882a593Smuzhiyun /* For some reason, the x bits sometimes aren't set on the root
209*4882a593Smuzhiyun * of mounted volumes.
210*4882a593Smuzhiyun */
211*4882a593Smuzhiyun struct stat st;
212*4882a593Smuzhiyun rv = stat(mount_point, &st);
213*4882a593Smuzhiyun if (rv < 0) {
214*4882a593Smuzhiyun return rv;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
217*4882a593Smuzhiyun if (new_mode != st.st_mode) {
218*4882a593Smuzhiyun printf("Fixing execute permissions for %s\n", mount_point);
219*4882a593Smuzhiyun rv = chmod(mount_point, new_mode);
220*4882a593Smuzhiyun if (rv < 0) {
221*4882a593Smuzhiyun printf("Couldn't fix permissions for %s: %s\n",
222*4882a593Smuzhiyun mount_point, strerror(errno));
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun #endif
227*4882a593Smuzhiyun return rv;
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun
mtd_get_flash_info(size_t * total_size,size_t * block_size,size_t * page_size)230*4882a593Smuzhiyun int mtd_get_flash_info(size_t *total_size, size_t *block_size, size_t *page_size)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun int fd = open("/dev/mtd0", O_RDONLY);
233*4882a593Smuzhiyun if (fd < 0) {
234*4882a593Smuzhiyun fprintf(stderr, "mtd: open /dev/mtd0 (%s)\n", strerror(errno));
235*4882a593Smuzhiyun return -1;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun struct mtd_info_user mtd_info;
239*4882a593Smuzhiyun int ret = ioctl(fd, MEMGETINFO, &mtd_info);
240*4882a593Smuzhiyun close(fd);
241*4882a593Smuzhiyun if (ret < 0) {
242*4882a593Smuzhiyun fprintf(stderr, "mtd: ioctl MEMGETINFO (%s)\n", strerror(errno));
243*4882a593Smuzhiyun return -1;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun printf("mtd_info.size = [%d]\n", mtd_info.size);
247*4882a593Smuzhiyun printf("mtd_info.erasesize = [%d]\n", mtd_info.erasesize);
248*4882a593Smuzhiyun printf("mtd_info.writesize = [%d]\n", mtd_info.writesize);
249*4882a593Smuzhiyun if (total_size) *total_size = mtd_info.size;
250*4882a593Smuzhiyun if (block_size) *block_size = mtd_info.erasesize;
251*4882a593Smuzhiyun if (page_size) *page_size = mtd_info.writesize;
252*4882a593Smuzhiyun return 0;
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun int
mtd_partition_info(const MtdPartition * partition,size_t * total_size,size_t * erase_size,size_t * write_size)256*4882a593Smuzhiyun mtd_partition_info(const MtdPartition *partition,
257*4882a593Smuzhiyun size_t *total_size, size_t *erase_size, size_t *write_size)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun char mtddevname[32];
260*4882a593Smuzhiyun //sprintf(mtddevname, "/dev/mtd/mtd%d", partition->device_index);
261*4882a593Smuzhiyun sprintf(mtddevname, "/dev/mtd%d", partition->device_index);
262*4882a593Smuzhiyun int fd = open(mtddevname, O_RDONLY);
263*4882a593Smuzhiyun if (fd < 0) return -1;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun struct mtd_info_user mtd_info;
266*4882a593Smuzhiyun int ret = ioctl(fd, MEMGETINFO, &mtd_info);
267*4882a593Smuzhiyun close(fd);
268*4882a593Smuzhiyun if (ret < 0) return -1;
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun if (total_size != NULL) *total_size = mtd_info.size;
271*4882a593Smuzhiyun if (erase_size != NULL) *erase_size = mtd_info.erasesize;
272*4882a593Smuzhiyun if (write_size != NULL) *write_size = mtd_info.writesize;
273*4882a593Smuzhiyun return 0;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
mtd_read_partition(const MtdPartition * partition)276*4882a593Smuzhiyun MtdReadContext *mtd_read_partition(const MtdPartition *partition)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun MtdReadContext *ctx = (MtdReadContext*) malloc(sizeof(MtdReadContext));
279*4882a593Smuzhiyun if (ctx == NULL) return NULL;
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun ctx->buffer = malloc(partition->erase_size);
282*4882a593Smuzhiyun if (ctx->buffer == NULL) {
283*4882a593Smuzhiyun free(ctx);
284*4882a593Smuzhiyun return NULL;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun char mtddevname[32];
288*4882a593Smuzhiyun sprintf(mtddevname, "/dev/mtd%d", partition->device_index);
289*4882a593Smuzhiyun //sprintf(mtddevname, "/dev/mtd/mtd%d", partition->device_index);
290*4882a593Smuzhiyun ctx->fd = open(mtddevname, O_RDONLY);
291*4882a593Smuzhiyun if (ctx->fd < 0) {
292*4882a593Smuzhiyun free(ctx);
293*4882a593Smuzhiyun free(ctx->buffer);
294*4882a593Smuzhiyun return NULL;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun ctx->partition = partition;
298*4882a593Smuzhiyun ctx->consumed = partition->erase_size;
299*4882a593Smuzhiyun return ctx;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun // Seeks to a location in the partition. Don't mix with reads of
303*4882a593Smuzhiyun // anything other than whole blocks; unpredictable things will result.
mtd_read_skip_to(const MtdReadContext * ctx,size_t offset)304*4882a593Smuzhiyun void mtd_read_skip_to(const MtdReadContext* ctx, size_t offset)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun lseek64(ctx->fd, offset, SEEK_SET);
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun
read_block(const MtdPartition * partition,int fd,char * data)309*4882a593Smuzhiyun static int read_block(const MtdPartition *partition, int fd, char *data)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun struct mtd_ecc_stats before, after;
312*4882a593Smuzhiyun if (ioctl(fd, ECCGETSTATS, &before)) {
313*4882a593Smuzhiyun fprintf(stderr, "mtd: ECCGETSTATS error (%s)\n", strerror(errno));
314*4882a593Smuzhiyun return -1;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun loff_t pos = lseek64(fd, 0, SEEK_CUR);
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun ssize_t size = partition->erase_size;
320*4882a593Smuzhiyun int mgbb;
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun while (pos + size <= (int) partition->size) {
323*4882a593Smuzhiyun if (lseek64(fd, pos, SEEK_SET) != pos || read(fd, data, size) != size) {
324*4882a593Smuzhiyun fprintf(stderr, "mtd: read error at 0x%08llx (%s)\n",
325*4882a593Smuzhiyun pos, strerror(errno));
326*4882a593Smuzhiyun } else if (ioctl(fd, ECCGETSTATS, &after)) {
327*4882a593Smuzhiyun fprintf(stderr, "mtd: ECCGETSTATS error (%s)\n", strerror(errno));
328*4882a593Smuzhiyun return -1;
329*4882a593Smuzhiyun } else if (after.failed != before.failed) {
330*4882a593Smuzhiyun fprintf(stderr, "mtd: ECC errors (%d soft, %d hard) at 0x%08llx\n",
331*4882a593Smuzhiyun after.corrected - before.corrected,
332*4882a593Smuzhiyun after.failed - before.failed, pos);
333*4882a593Smuzhiyun // copy the comparison baseline for the next read.
334*4882a593Smuzhiyun memcpy(&before, &after, sizeof(struct mtd_ecc_stats));
335*4882a593Smuzhiyun } else if ((mgbb = ioctl(fd, MEMGETBADBLOCK, &pos))) {
336*4882a593Smuzhiyun fprintf(stderr,
337*4882a593Smuzhiyun "mtd: MEMGETBADBLOCK returned %d at 0x%08llx (errno=%d)\n",
338*4882a593Smuzhiyun mgbb, pos, errno);
339*4882a593Smuzhiyun } else {
340*4882a593Smuzhiyun return 0; // Success!
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun pos += partition->erase_size;
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun errno = ENOSPC;
347*4882a593Smuzhiyun return -1;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun
mtd_read_data(MtdReadContext * ctx,char * data,size_t len)350*4882a593Smuzhiyun ssize_t mtd_read_data(MtdReadContext *ctx, char *data, size_t len)
351*4882a593Smuzhiyun {
352*4882a593Smuzhiyun ssize_t read = 0;
353*4882a593Smuzhiyun while (read < (int) len) {
354*4882a593Smuzhiyun if (ctx->consumed < ctx->partition->erase_size) {
355*4882a593Smuzhiyun size_t avail = ctx->partition->erase_size - ctx->consumed;
356*4882a593Smuzhiyun size_t copy = len - read < avail ? len - read : avail;
357*4882a593Smuzhiyun memcpy(data + read, ctx->buffer + ctx->consumed, copy);
358*4882a593Smuzhiyun ctx->consumed += copy;
359*4882a593Smuzhiyun read += copy;
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun // Read complete blocks directly into the user's buffer
363*4882a593Smuzhiyun while (ctx->consumed == ctx->partition->erase_size &&
364*4882a593Smuzhiyun len - read >= ctx->partition->erase_size) {
365*4882a593Smuzhiyun if (read_block(ctx->partition, ctx->fd, data + read)) return -1;
366*4882a593Smuzhiyun read += ctx->partition->erase_size;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun if (read >= len) {
370*4882a593Smuzhiyun return read;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun // Read the next block into the buffer
374*4882a593Smuzhiyun if (ctx->consumed == ctx->partition->erase_size && read < (int) len) {
375*4882a593Smuzhiyun if (read_block(ctx->partition, ctx->fd, ctx->buffer)) return -1;
376*4882a593Smuzhiyun ctx->consumed = 0;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun return read;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun
mtd_read_close(MtdReadContext * ctx)383*4882a593Smuzhiyun void mtd_read_close(MtdReadContext *ctx)
384*4882a593Smuzhiyun {
385*4882a593Smuzhiyun close(ctx->fd);
386*4882a593Smuzhiyun free(ctx->buffer);
387*4882a593Smuzhiyun free(ctx);
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun
mtd_write_partition(const MtdPartition * partition)390*4882a593Smuzhiyun MtdWriteContext *mtd_write_partition(const MtdPartition *partition)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun MtdWriteContext *ctx = (MtdWriteContext*) malloc(sizeof(MtdWriteContext));
393*4882a593Smuzhiyun if (ctx == NULL) return NULL;
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun ctx->bad_block_offsets = NULL;
396*4882a593Smuzhiyun ctx->bad_block_alloc = 0;
397*4882a593Smuzhiyun ctx->bad_block_count = 0;
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun ctx->buffer = malloc(partition->erase_size);
400*4882a593Smuzhiyun if (ctx->buffer == NULL) {
401*4882a593Smuzhiyun free(ctx);
402*4882a593Smuzhiyun return NULL;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun char mtddevname[32];
406*4882a593Smuzhiyun sprintf(mtddevname, "/dev/mtd%d", partition->device_index);
407*4882a593Smuzhiyun //sprintf(mtddevname, "/dev/mtd/mtd%d", partition->device_index);
408*4882a593Smuzhiyun ctx->fd = open(mtddevname, O_RDWR);
409*4882a593Smuzhiyun if (ctx->fd < 0) {
410*4882a593Smuzhiyun free(ctx->buffer);
411*4882a593Smuzhiyun free(ctx);
412*4882a593Smuzhiyun return NULL;
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun ctx->partition = partition;
416*4882a593Smuzhiyun ctx->stored = 0;
417*4882a593Smuzhiyun return ctx;
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun
add_bad_block_offset(MtdWriteContext * ctx,off_t pos)420*4882a593Smuzhiyun static void add_bad_block_offset(MtdWriteContext *ctx, off_t pos)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun if (ctx->bad_block_count + 1 > ctx->bad_block_alloc) {
423*4882a593Smuzhiyun ctx->bad_block_alloc = (ctx->bad_block_alloc * 2) + 1;
424*4882a593Smuzhiyun ctx->bad_block_offsets = realloc(ctx->bad_block_offsets,
425*4882a593Smuzhiyun ctx->bad_block_alloc * sizeof(off_t));
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun ctx->bad_block_offsets[ctx->bad_block_count++] = pos;
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun
write_block(MtdWriteContext * ctx,const char * data)430*4882a593Smuzhiyun static int write_block(MtdWriteContext *ctx, const char *data)
431*4882a593Smuzhiyun {
432*4882a593Smuzhiyun const MtdPartition *partition = ctx->partition;
433*4882a593Smuzhiyun int fd = ctx->fd;
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun off_t pos = lseek(fd, 0, SEEK_CUR);
436*4882a593Smuzhiyun if (pos == (off_t) -1) return 1;
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun ssize_t size = partition->erase_size;
439*4882a593Smuzhiyun while (pos + size <= (int) partition->size) {
440*4882a593Smuzhiyun loff_t bpos = pos;
441*4882a593Smuzhiyun int ret = ioctl(fd, MEMGETBADBLOCK, &bpos);
442*4882a593Smuzhiyun if (ret != 0 && !(ret == -1 && errno == EOPNOTSUPP)) {
443*4882a593Smuzhiyun add_bad_block_offset(ctx, pos);
444*4882a593Smuzhiyun fprintf(stderr,
445*4882a593Smuzhiyun "mtd: not writing bad block at 0x%08lx (ret %d errno %d)\n",
446*4882a593Smuzhiyun pos, ret, errno);
447*4882a593Smuzhiyun pos += partition->erase_size;
448*4882a593Smuzhiyun continue; // Don't try to erase known factory-bad blocks.
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun struct erase_info_user erase_info;
452*4882a593Smuzhiyun erase_info.start = pos;
453*4882a593Smuzhiyun erase_info.length = size;
454*4882a593Smuzhiyun int retry;
455*4882a593Smuzhiyun for (retry = 0; retry < 2; ++retry) {
456*4882a593Smuzhiyun if (ioctl(fd, MEMERASE, &erase_info) < 0) {
457*4882a593Smuzhiyun fprintf(stderr, "mtd: erase failure at 0x%08lx (%s)\n",
458*4882a593Smuzhiyun pos, strerror(errno));
459*4882a593Smuzhiyun continue;
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun if (lseek(fd, pos, SEEK_SET) != pos ||
462*4882a593Smuzhiyun write(fd, data, size) != size) {
463*4882a593Smuzhiyun fprintf(stderr, "mtd: write error at 0x%08lx (%s)\n",
464*4882a593Smuzhiyun pos, strerror(errno));
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun char verify[size];
468*4882a593Smuzhiyun if (lseek(fd, pos, SEEK_SET) != pos ||
469*4882a593Smuzhiyun read(fd, verify, size) != size) {
470*4882a593Smuzhiyun fprintf(stderr, "mtd: re-read error at 0x%08lx (%s)\n",
471*4882a593Smuzhiyun pos, strerror(errno));
472*4882a593Smuzhiyun continue;
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun if (memcmp(data, verify, size) != 0) {
475*4882a593Smuzhiyun fprintf(stderr, "mtd: verification error at 0x%08lx (%s)\n",
476*4882a593Smuzhiyun pos, strerror(errno));
477*4882a593Smuzhiyun continue;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun if (retry > 0) {
481*4882a593Smuzhiyun fprintf(stderr, "mtd: wrote block after %d retries\n", retry);
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun fprintf(stderr, "mtd: successfully wrote block at %llx\n", pos);
484*4882a593Smuzhiyun return 0; // Success!
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun // Try to erase it once more as we give up on this block
488*4882a593Smuzhiyun add_bad_block_offset(ctx, pos);
489*4882a593Smuzhiyun fprintf(stderr, "mtd: skipping write block at 0x%08lx\n", pos);
490*4882a593Smuzhiyun ioctl(fd, MEMERASE, &erase_info);
491*4882a593Smuzhiyun pos += partition->erase_size;
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun // Ran out of space on the device
495*4882a593Smuzhiyun errno = ENOSPC;
496*4882a593Smuzhiyun return -1;
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun
mtd_write_data(MtdWriteContext * ctx,const char * data,size_t len)499*4882a593Smuzhiyun ssize_t mtd_write_data(MtdWriteContext *ctx, const char *data, size_t len)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun size_t wrote = 0;
502*4882a593Smuzhiyun while (wrote < len) {
503*4882a593Smuzhiyun // Coalesce partial writes into complete blocks
504*4882a593Smuzhiyun if (ctx->stored > 0 || len - wrote < ctx->partition->erase_size) {
505*4882a593Smuzhiyun size_t avail = ctx->partition->erase_size - ctx->stored;
506*4882a593Smuzhiyun size_t copy = len - wrote < avail ? len - wrote : avail;
507*4882a593Smuzhiyun memcpy(ctx->buffer + ctx->stored, data + wrote, copy);
508*4882a593Smuzhiyun ctx->stored += copy;
509*4882a593Smuzhiyun wrote += copy;
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun // If a complete block was accumulated, write it
513*4882a593Smuzhiyun if (ctx->stored == ctx->partition->erase_size) {
514*4882a593Smuzhiyun if (write_block(ctx, ctx->buffer)) return -1;
515*4882a593Smuzhiyun ctx->stored = 0;
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun // Write complete blocks directly from the user's buffer
519*4882a593Smuzhiyun while (ctx->stored == 0 && len - wrote >= ctx->partition->erase_size) {
520*4882a593Smuzhiyun if (write_block(ctx, data + wrote)) return -1;
521*4882a593Smuzhiyun wrote += ctx->partition->erase_size;
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun return wrote;
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun
mtd_erase_blocks(MtdWriteContext * ctx,int blocks)528*4882a593Smuzhiyun off_t mtd_erase_blocks(MtdWriteContext *ctx, int blocks)
529*4882a593Smuzhiyun {
530*4882a593Smuzhiyun // Zero-pad and write any pending data to get us to a block boundary
531*4882a593Smuzhiyun if (ctx->stored > 0) {
532*4882a593Smuzhiyun size_t zero = ctx->partition->erase_size - ctx->stored;
533*4882a593Smuzhiyun memset(ctx->buffer + ctx->stored, 0, zero);
534*4882a593Smuzhiyun if (write_block(ctx, ctx->buffer)) return -1;
535*4882a593Smuzhiyun ctx->stored = 0;
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun off_t pos = lseek(ctx->fd, 0, SEEK_CUR);
539*4882a593Smuzhiyun if ((off_t) pos == (off_t) -1) return pos;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun const int total = (ctx->partition->size - pos) / ctx->partition->erase_size;
542*4882a593Smuzhiyun if (blocks < 0) blocks = total;
543*4882a593Smuzhiyun if (blocks > total) {
544*4882a593Smuzhiyun errno = ENOSPC;
545*4882a593Smuzhiyun return -1;
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun // Erase the specified number of blocks
549*4882a593Smuzhiyun while (blocks-- > 0) {
550*4882a593Smuzhiyun loff_t bpos = pos;
551*4882a593Smuzhiyun if (ioctl(ctx->fd, MEMGETBADBLOCK, &bpos) > 0) {
552*4882a593Smuzhiyun fprintf(stderr, "mtd: not erasing bad block at 0x%08lx\n", pos);
553*4882a593Smuzhiyun pos += ctx->partition->erase_size;
554*4882a593Smuzhiyun continue; // Don't try to erase known factory-bad blocks.
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun struct erase_info_user erase_info;
558*4882a593Smuzhiyun erase_info.start = pos;
559*4882a593Smuzhiyun erase_info.length = ctx->partition->erase_size;
560*4882a593Smuzhiyun if (ioctl(ctx->fd, MEMERASE, &erase_info) < 0) {
561*4882a593Smuzhiyun fprintf(stderr, "mtd: erase failure at 0x%08lx\n", pos);
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun pos += ctx->partition->erase_size;
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun return pos;
567*4882a593Smuzhiyun }
568*4882a593Smuzhiyun
mtd_write_close(MtdWriteContext * ctx)569*4882a593Smuzhiyun int mtd_write_close(MtdWriteContext *ctx)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun int r = 0;
572*4882a593Smuzhiyun // Make sure any pending data gets written
573*4882a593Smuzhiyun if (mtd_erase_blocks(ctx, 0) == (off_t) -1) r = -1;
574*4882a593Smuzhiyun if (close(ctx->fd)) r = -1;
575*4882a593Smuzhiyun free(ctx->bad_block_offsets);
576*4882a593Smuzhiyun free(ctx->buffer);
577*4882a593Smuzhiyun free(ctx);
578*4882a593Smuzhiyun return r;
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun /* Return the offset of the first good block at or after pos (which
582*4882a593Smuzhiyun * might be pos itself).
583*4882a593Smuzhiyun */
mtd_find_write_start(MtdWriteContext * ctx,off_t pos)584*4882a593Smuzhiyun off_t mtd_find_write_start(MtdWriteContext *ctx, off_t pos)
585*4882a593Smuzhiyun {
586*4882a593Smuzhiyun int i;
587*4882a593Smuzhiyun for (i = 0; i < ctx->bad_block_count; ++i) {
588*4882a593Smuzhiyun if (ctx->bad_block_offsets[i] == pos) {
589*4882a593Smuzhiyun pos += ctx->partition->erase_size;
590*4882a593Smuzhiyun } else if (ctx->bad_block_offsets[i] > pos) {
591*4882a593Smuzhiyun return pos;
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun }
594*4882a593Smuzhiyun return pos;
595*4882a593Smuzhiyun }
596