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