blob: a003a2d65015ab6d5da6a4aa863550025fcd01e7 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
2 * Copyright (c) 2010, Code Aurora Forum. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of Code Aurora Forum, Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <stdlib.h>
31#include <stdio.h>
32#include <unistd.h>
33#include <dirent.h>
34#include <string.h>
35#include <errno.h>
36#include <sys/types.h>
37#include <sys/reboot.h>
38#include <sys/stat.h>
39#include <sys/wait.h>
40#include <sys/mount.h> // for _IOW, _IOR, mount()
41
42#include "mmcutils.h"
43
44unsigned ext3_count = 0;
45char *ext3_partitions[] = {"system", "userdata", "cache", "NONE"};
46
47unsigned vfat_count = 0;
48char *vfat_partitions[] = {"modem", "NONE"};
49
50struct MmcPartition {
51 char *device_index;
52 char *filesystem;
53 char *name;
54 unsigned dstatus;
55 unsigned dtype ;
56 unsigned dfirstsec;
57 unsigned dsize;
58};
59
60typedef struct {
61 MmcPartition *partitions;
62 int partitions_allocd;
63 int partition_count;
64} MmcState;
65
66static MmcState g_mmc_state = {
67 NULL, // partitions
68 0, // partitions_allocd
69 -1 // partition_count
70};
71
72#define MMC_DEVICENAME "/dev/block/mmcblk0"
73
74static void
75mmc_partition_name (MmcPartition *mbr, unsigned int type) {
76 switch(type)
77 {
78 char name[64];
79 case MMC_BOOT_TYPE:
80 sprintf(name,"boot");
81 mbr->name = strdup(name);
82 break;
83 case MMC_RECOVERY_TYPE:
84 sprintf(name,"recovery");
85 mbr->name = strdup(name);
86 break;
87 case MMC_EXT3_TYPE:
88 if (strcmp("NONE", ext3_partitions[ext3_count])) {
89 strcpy((char *)name,(const char *)ext3_partitions[ext3_count]);
90 mbr->name = strdup(name);
91 ext3_count++;
92 }
93 mbr->filesystem = strdup("ext3");
94 break;
95 case MMC_VFAT_TYPE:
96 if (strcmp("NONE", vfat_partitions[vfat_count])) {
97 strcpy((char *)name,(const char *)vfat_partitions[vfat_count]);
98 mbr->name = strdup(name);
99 vfat_count++;
100 }
101 mbr->filesystem = strdup("vfat");
102 break;
103 };
104}
105
106static int
107mmc_read_mbr (const char *device, MmcPartition *mbr) {
108 FILE *fd;
109 unsigned char buffer[512];
110 int idx, i;
111 unsigned mmc_partition_count = 0;
112 unsigned int dtype;
113 unsigned int dfirstsec;
114 unsigned int EBR_first_sec;
115 unsigned int EBR_current_sec;
116 int ret = -1;
117
118 fd = fopen(device, "r");
119 if(fd == NULL)
120 {
121 printf("Can't open device: \"%s\"\n", device);
122 goto ERROR2;
123 }
124 if ((fread(buffer, 512, 1, fd)) != 1)
125 {
126 printf("Can't read device: \"%s\"\n", device);
127 goto ERROR1;
128 }
129 /* Check to see if signature exists */
130 if ((buffer[TABLE_SIGNATURE] != 0x55) || \
131 (buffer[TABLE_SIGNATURE + 1] != 0xAA))
132 {
133 printf("Incorrect mbr signatures!\n");
134 goto ERROR1;
135 }
136 idx = TABLE_ENTRY_0;
137 for (i = 0; i < 4; i++)
138 {
139 char device_index[128];
140
141 mbr[mmc_partition_count].dstatus = \
142 buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_STATUS];
143 mbr[mmc_partition_count].dtype = \
144 buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_TYPE];
145 mbr[mmc_partition_count].dfirstsec = \
146 GET_LWORD_FROM_BYTE(&buffer[idx + \
147 i * TABLE_ENTRY_SIZE + \
148 OFFSET_FIRST_SEC]);
149 mbr[mmc_partition_count].dsize = \
150 GET_LWORD_FROM_BYTE(&buffer[idx + \
151 i * TABLE_ENTRY_SIZE + \
152 OFFSET_SIZE]);
153 dtype = mbr[mmc_partition_count].dtype;
154 dfirstsec = mbr[mmc_partition_count].dfirstsec;
155 mmc_partition_name(&mbr[mmc_partition_count], \
156 mbr[mmc_partition_count].dtype);
157
158 sprintf(device_index, "%sp%d", device, (mmc_partition_count+1));
159 mbr[mmc_partition_count].device_index = strdup(device_index);
160
161 mmc_partition_count++;
162 if (mmc_partition_count == MAX_PARTITIONS)
163 goto SUCCESS;
164 }
165
166 /* See if the last partition is EBR, if not, parsing is done */
167 if (dtype != 0x05)
168 {
169 goto SUCCESS;
170 }
171
172 EBR_first_sec = dfirstsec;
173 EBR_current_sec = dfirstsec;
174
175 fseek (fd, (EBR_first_sec * 512), SEEK_SET);
176 if ((fread(buffer, 512, 1, fd)) != 1)
177 goto ERROR1;
178
179 /* Loop to parse the EBR */
180 for (i = 0;; i++)
181 {
182 char device_index[128];
183
184 if ((buffer[TABLE_SIGNATURE] != 0x55) || (buffer[TABLE_SIGNATURE + 1] != 0xAA))
185 {
186 break;
187 }
188 mbr[mmc_partition_count].dstatus = \
189 buffer[TABLE_ENTRY_0 + OFFSET_STATUS];
190 mbr[mmc_partition_count].dtype = \
191 buffer[TABLE_ENTRY_0 + OFFSET_TYPE];
192 mbr[mmc_partition_count].dfirstsec = \
193 GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 + \
194 OFFSET_FIRST_SEC]) + \
195 EBR_current_sec;
196 mbr[mmc_partition_count].dsize = \
197 GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 + \
198 OFFSET_SIZE]);
199 mmc_partition_name(&mbr[mmc_partition_count], \
200 mbr[mmc_partition_count].dtype);
201
202 sprintf(device_index, "%sp%d", device, (mmc_partition_count+1));
203 mbr[mmc_partition_count].device_index = strdup(device_index);
204
205 mmc_partition_count++;
206 if (mmc_partition_count == MAX_PARTITIONS)
207 goto SUCCESS;
208
209 dfirstsec = GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_1 + OFFSET_FIRST_SEC]);
210 if(dfirstsec == 0)
211 {
212 /* Getting to the end of the EBR tables */
213 break;
214 }
215 /* More EBR to follow - read in the next EBR sector */
216 fseek (fd, ((EBR_first_sec + dfirstsec) * 512), SEEK_SET);
217 if ((fread(buffer, 512, 1, fd)) != 1)
218 goto ERROR1;
219
220 EBR_current_sec = EBR_first_sec + dfirstsec;
221 }
222
223SUCCESS:
224 ret = mmc_partition_count;
225ERROR1:
226 fclose(fd);
227ERROR2:
228 return ret;
229}
230
231int
232mmc_scan_partitions() {
233 int i;
Dees_Troy51a0e822012-09-05 15:24:24 -0400234
235 if (g_mmc_state.partitions == NULL) {
236 const int nump = MAX_PARTITIONS;
237 MmcPartition *partitions = malloc(nump * sizeof(*partitions));
238 if (partitions == NULL) {
239 errno = ENOMEM;
240 return -1;
241 }
242 g_mmc_state.partitions = partitions;
243 g_mmc_state.partitions_allocd = nump;
244 memset(partitions, 0, nump * sizeof(*partitions));
245 }
246 g_mmc_state.partition_count = 0;
247 ext3_count = 0;
248 vfat_count = 0;
249
250 /* Initialize all of the entries to make things easier later.
251 * (Lets us handle sparsely-numbered partitions, which
252 * may not even be possible.)
253 */
254 for (i = 0; i < g_mmc_state.partitions_allocd; i++) {
255 MmcPartition *p = &g_mmc_state.partitions[i];
256 if (p->device_index != NULL) {
257 free(p->device_index);
258 p->device_index = NULL;
259 }
260 if (p->name != NULL) {
261 free(p->name);
262 p->name = NULL;
263 }
264 if (p->filesystem != NULL) {
265 free(p->filesystem);
266 p->filesystem = NULL;
267 }
268 }
269
270 g_mmc_state.partition_count = mmc_read_mbr(MMC_DEVICENAME, g_mmc_state.partitions);
271 if(g_mmc_state.partition_count == -1)
272 {
273 printf("Error in reading mbr!\n");
274 // keep "partitions" around so we can free the names on a rescan.
275 g_mmc_state.partition_count = -1;
276 }
277 return g_mmc_state.partition_count;
278}
279
280static const MmcPartition *
281mmc_find_partition_by_device_index(const char *device_index)
282{
283 if (g_mmc_state.partitions != NULL) {
284 int i;
285 for (i = 0; i < g_mmc_state.partitions_allocd; i++) {
286 MmcPartition *p = &g_mmc_state.partitions[i];
287 if (p->device_index !=NULL && p->name != NULL) {
288 if (strcmp(p->device_index, device_index) == 0) {
289 return p;
290 }
291 }
292 }
293 }
294 return NULL;
295}
296
297const MmcPartition *
298mmc_find_partition_by_name(const char *name)
299{
300 if (name[0] == '/') {
301 return mmc_find_partition_by_device_index(name);
302 }
303
304 if (g_mmc_state.partitions != NULL) {
305 int i;
306 for (i = 0; i < g_mmc_state.partitions_allocd; i++) {
307 MmcPartition *p = &g_mmc_state.partitions[i];
308 if (p->device_index !=NULL && p->name != NULL) {
309 if (strcmp(p->name, name) == 0) {
310 return p;
311 }
312 }
313 }
314 }
315 return NULL;
316}
317
318#define MKE2FS_BIN "/sbin/mke2fs"
319#define TUNE2FS_BIN "/sbin/tune2fs"
320#define E2FSCK_BIN "/sbin/e2fsck"
321
322int
Ethan Yonker58f21322018-08-24 11:17:36 -0500323run_exec_process ( char *const *argv) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400324 pid_t pid;
325 int status;
326 pid = fork();
327 if (pid == 0) {
328 execv(argv[0], argv);
329 fprintf(stderr, "E:Can't run (%s)\n",strerror(errno));
330 _exit(-1);
331 }
332
333 waitpid(pid, &status, 0);
334 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
335 return 1;
336 }
337 return 0;
338}
339
340int
Ethan Yonker58f21322018-08-24 11:17:36 -0500341format_ext3_device (char *device) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400342 char *const mke2fs[] = {MKE2FS_BIN, "-j", "-q", device, NULL};
343 char *const tune2fs[] = {TUNE2FS_BIN, "-C", "1", device, NULL};
344 // Run mke2fs
345 if(run_exec_process(mke2fs)) {
346 printf("failure while running mke2fs\n");
347 return -1;
348 }
349
350 // Run tune2fs
351 if(run_exec_process(tune2fs)) {
352 printf("failure while running mke2fs\n");
353 return -1;
354 }
355
356 // Run e2fsck
357 char *const e2fsck[] = {E2FSCK_BIN, "-fy", device, NULL};
358 if(run_exec_process(e2fsck)) {
359 printf("failure while running e2fsck\n");
360 return -1;
361 }
362
363 return 0;
364}
365
366int
Ethan Yonker58f21322018-08-24 11:17:36 -0500367format_ext2_device (char *device) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400368 // Run mke2fs
369 char *const mke2fs[] = {MKE2FS_BIN, device, NULL};
370 if(run_exec_process(mke2fs))
371 return -1;
372
373 // Run tune2fs
374 char *const tune2fs[] = {TUNE2FS_BIN, "-C", "1", device, NULL};
375 if(run_exec_process(tune2fs))
376 return -1;
377
378 // Run e2fsck
379 char *const e2fsck[] = {E2FSCK_BIN, "-fy", device, NULL};
380 if(run_exec_process(e2fsck))
381 return -1;
382
383 return 0;
384}
385
386int
Ethan Yonker58f21322018-08-24 11:17:36 -0500387mmc_format_ext3 (const MmcPartition *partition) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400388 char device[128];
389 strcpy(device, partition->device_index);
390 return format_ext3_device(device);
391}
392
393int
394mmc_mount_partition(const MmcPartition *partition, const char *mount_point,
395 int read_only)
396{
397 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
398 char devname[128];
399 int rv = -1;
400 strcpy(devname, partition->device_index);
401 if (partition->filesystem == NULL) {
402 printf("Null filesystem!\n");
403 return rv;
404 }
405 if (!read_only) {
406 rv = mount(devname, mount_point, partition->filesystem, flags, NULL);
407 }
408 if (read_only || rv < 0) {
409 rv = mount(devname, mount_point, partition->filesystem, flags | MS_RDONLY, 0);
410 if (rv < 0) {
411 printf("Failed to mount %s on %s: %s\n",
412 devname, mount_point, strerror(errno));
413 } else {
414 printf("Mount %s on %s read-only\n", devname, mount_point);
415 }
416 }
417 return rv;
418}
419
420int
Ethan Yonker58f21322018-08-24 11:17:36 -0500421mmc_raw_copy (const MmcPartition *partition, const char *in_file) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400422 int ch;
423 FILE *in;
424 FILE *out;
Dees_Troy51a0e822012-09-05 15:24:24 -0400425 char buf[512];
426 unsigned sz = 0;
427 unsigned i;
428 int ret = -1;
429 char *out_file = partition->device_index;
430
431 in = fopen ( in_file, "r" );
432 if (in == NULL)
433 goto ERROR3;
434
435 out = fopen ( out_file, "w" );
436 if (out == NULL)
437 goto ERROR2;
438
439 fseek(in, 0L, SEEK_END);
440 sz = ftell(in);
441 fseek(in, 0L, SEEK_SET);
442
443 if (sz % 512)
444 {
445 while ( ( ch = fgetc ( in ) ) != EOF )
446 fputc ( ch, out );
447 }
448 else
449 {
450 for (i=0; i< (sz/512); i++)
451 {
452 if ((fread(buf, 512, 1, in)) != 1)
453 goto ERROR1;
454 if ((fwrite(buf, 512, 1, out)) != 1)
455 goto ERROR1;
456 }
457 }
458
Dan Pasanen8abeee12015-11-11 10:07:42 -0600459 fsync(fileno(out));
Dees_Troy51a0e822012-09-05 15:24:24 -0400460 ret = 0;
461ERROR1:
462 fclose ( out );
463ERROR2:
464 fclose ( in );
465ERROR3:
466 return ret;
467
468}
469
470
471int
472mmc_raw_dump_internal (const char* in_file, const char *out_file) {
473 int ch;
474 FILE *in;
475 FILE *out;
Dees_Troy51a0e822012-09-05 15:24:24 -0400476 char buf[512];
477 unsigned sz = 0;
478 unsigned i;
479 int ret = -1;
480
481 in = fopen ( in_file, "r" );
482 if (in == NULL)
483 goto ERROR3;
484
485 out = fopen ( out_file, "w" );
486 if (out == NULL)
487 goto ERROR2;
488
489 fseek(in, 0L, SEEK_END);
490 sz = ftell(in);
491 fseek(in, 0L, SEEK_SET);
492
493 if (sz % 512)
494 {
495 while ( ( ch = fgetc ( in ) ) != EOF )
496 fputc ( ch, out );
497 }
498 else
499 {
500 for (i=0; i< (sz/512); i++)
501 {
502 if ((fread(buf, 512, 1, in)) != 1)
503 goto ERROR1;
504 if ((fwrite(buf, 512, 1, out)) != 1)
505 goto ERROR1;
506 }
507 }
508
Dan Pasanen8abeee12015-11-11 10:07:42 -0600509 fsync(fileno(out));
Dees_Troy51a0e822012-09-05 15:24:24 -0400510 ret = 0;
511ERROR1:
512 fclose ( out );
513ERROR2:
514 fclose ( in );
515ERROR3:
516 return ret;
517
518}
519
520// TODO: refactor this to not be a giant copy paste mess
521int
Ethan Yonker58f21322018-08-24 11:17:36 -0500522mmc_raw_dump (const MmcPartition *partition, const char *out_file) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400523 return mmc_raw_dump_internal(partition->device_index, out_file);
524}
525
526
527int
528mmc_raw_read (const MmcPartition *partition, char *data, int data_size) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400529 FILE *in;
Dees_Troy51a0e822012-09-05 15:24:24 -0400530 unsigned sz = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400531 int ret = -1;
532 char *in_file = partition->device_index;
533
534 in = fopen ( in_file, "r" );
535 if (in == NULL)
536 goto ERROR3;
537
538 fseek(in, 0L, SEEK_END);
539 sz = ftell(in);
540 fseek(in, 0L, SEEK_SET);
541
542 fread(data, data_size, 1, in);
543
544 ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400545 fclose ( in );
546ERROR3:
547 return ret;
548
549}
550
551int
552mmc_raw_write (const MmcPartition *partition, char *data, int data_size) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400553 FILE *out;
Dees_Troy51a0e822012-09-05 15:24:24 -0400554 int ret = -1;
555 char *out_file = partition->device_index;
556
557 out = fopen ( out_file, "w" );
558 if (out == NULL)
559 goto ERROR3;
560
561 fwrite(data, data_size, 1, out);
562
563 ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400564 fclose ( out );
565ERROR3:
566 return ret;
567
568}
569
570int cmd_mmc_restore_raw_partition(const char *partition, const char *filename)
571{
572 if (partition[0] != '/') {
573 mmc_scan_partitions();
574 const MmcPartition *p;
575 p = mmc_find_partition_by_name(partition);
576 if (p == NULL)
577 return -1;
578 return mmc_raw_copy(p, filename);
579 }
580 else {
581 return mmc_raw_dump_internal(filename, partition);
582 }
583}
584
585int cmd_mmc_backup_raw_partition(const char *partition, const char *filename)
586{
587 if (partition[0] != '/') {
588 mmc_scan_partitions();
589 const MmcPartition *p;
590 p = mmc_find_partition_by_name(partition);
591 if (p == NULL)
592 return -1;
593 return mmc_raw_dump(p, filename);
594 }
595 else {
596 return mmc_raw_dump_internal(partition, filename);
597 }
598}
599
Ethan Yonker58f21322018-08-24 11:17:36 -0500600int cmd_mmc_erase_raw_partition(const char *partition __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400601{
602 return 0;
603}
604
Ethan Yonker58f21322018-08-24 11:17:36 -0500605int cmd_mmc_erase_partition(const char *partition, const char *filesystem __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400606{
607 mmc_scan_partitions();
608 const MmcPartition *p;
609 p = mmc_find_partition_by_name(partition);
610 if (p == NULL)
611 return -1;
612 return mmc_format_ext3 (p);
613}
614
Ethan Yonker58f21322018-08-24 11:17:36 -0500615int cmd_mmc_mount_partition(const char *partition, const char *mount_point, const char *filesystem __unused, int read_only)
Dees_Troy51a0e822012-09-05 15:24:24 -0400616{
617 mmc_scan_partitions();
618 const MmcPartition *p;
619 p = mmc_find_partition_by_name(partition);
620 if (p == NULL)
621 return -1;
622 return mmc_mount_partition(p, mount_point, read_only);
623}
624
625int cmd_mmc_get_partition_device(const char *partition, char *device)
626{
627 mmc_scan_partitions();
628 const MmcPartition *p;
629 p = mmc_find_partition_by_name(partition);
630 if (p == NULL)
631 return -1;
632 strcpy(device, p->device_index);
633 return 0;
634}