blob: 7e283efbaebd29b8acead2168ca28f62f77576b0 [file] [log] [blame]
Shashank Mittal815ca5d2010-07-27 11:09:19 -07001/*
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
Koushik Dutta19447c02010-11-10 10:40:44 -080050struct 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
Shashank Mittal815ca5d2010-07-27 11:09:19 -070060typedef 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;
Koushik Duttafef77c02010-11-09 20:03:42 -080083 case MMC_RECOVERY_TYPE:
84 sprintf(name,"recovery");
85 mbr->name = strdup(name);
86 break;
Shashank Mittal815ca5d2010-07-27 11:09:19 -070087 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;
234 ssize_t nbytes;
235
236 if (g_mmc_state.partitions == NULL) {
237 const int nump = MAX_PARTITIONS;
238 MmcPartition *partitions = malloc(nump * sizeof(*partitions));
239 if (partitions == NULL) {
240 errno = ENOMEM;
241 return -1;
242 }
243 g_mmc_state.partitions = partitions;
244 g_mmc_state.partitions_allocd = nump;
245 memset(partitions, 0, nump * sizeof(*partitions));
246 }
247 g_mmc_state.partition_count = 0;
248 ext3_count = 0;
249 vfat_count = 0;
250
251 /* Initialize all of the entries to make things easier later.
252 * (Lets us handle sparsely-numbered partitions, which
253 * may not even be possible.)
254 */
255 for (i = 0; i < g_mmc_state.partitions_allocd; i++) {
256 MmcPartition *p = &g_mmc_state.partitions[i];
257 if (p->device_index != NULL) {
258 free(p->device_index);
259 p->device_index = NULL;
260 }
261 if (p->name != NULL) {
262 free(p->name);
263 p->name = NULL;
264 }
265 if (p->filesystem != NULL) {
266 free(p->filesystem);
267 p->filesystem = NULL;
268 }
269 }
270
271 g_mmc_state.partition_count = mmc_read_mbr(MMC_DEVICENAME, g_mmc_state.partitions);
272 if(g_mmc_state.partition_count == -1)
273 {
274 printf("Error in reading mbr!\n");
275 // keep "partitions" around so we can free the names on a rescan.
276 g_mmc_state.partition_count = -1;
277 }
278 return g_mmc_state.partition_count;
279}
280
281const MmcPartition *
282mmc_find_partition_by_name(const char *name)
283{
284 if (g_mmc_state.partitions != NULL) {
285 int i;
286 for (i = 0; i < g_mmc_state.partitions_allocd; i++) {
287 MmcPartition *p = &g_mmc_state.partitions[i];
288 if (p->device_index !=NULL && p->name != NULL) {
289 if (strcmp(p->name, name) == 0) {
290 return p;
291 }
292 }
293 }
294 }
295 return NULL;
296}
297
Koushik Duttafef77c02010-11-09 20:03:42 -0800298#define MKE2FS_BIN "/sbin/mke2fs"
299#define TUNE2FS_BIN "/sbin/tune2fs"
300#define E2FSCK_BIN "/sbin/e2fsck"
Shashank Mittal815ca5d2010-07-27 11:09:19 -0700301
Koushik Duttada32b542011-01-01 17:55:22 -0800302int
Shashank Mittal815ca5d2010-07-27 11:09:19 -0700303run_exec_process ( char **argv) {
304 pid_t pid;
305 int status;
306 pid = fork();
307 if (pid == 0) {
308 execv(argv[0], argv);
309 fprintf(stderr, "E:Can't run (%s)\n",strerror(errno));
310 _exit(-1);
311 }
312
313 waitpid(pid, &status, 0);
314 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
315 return 1;
316 }
317 return 0;
318}
319
320int
Koushik Duttab4c5fd62011-01-02 22:54:31 -0800321format_ext3_device (const char *device) {
Shashank Mittal815ca5d2010-07-27 11:09:19 -0700322 // Run mke2fs
323 char *const mke2fs[] = {MKE2FS_BIN, "-j", device, NULL};
324 if(run_exec_process(mke2fs))
325 return -1;
326
327 // Run tune2fs
Koushik Duttafef77c02010-11-09 20:03:42 -0800328 char *const tune2fs[] = {TUNE2FS_BIN, "-j", "-C", "1", device, NULL};
Shashank Mittal815ca5d2010-07-27 11:09:19 -0700329 if(run_exec_process(tune2fs))
330 return -1;
331
332 // Run e2fsck
333 char *const e2fsck[] = {E2FSCK_BIN, "-fy", device, NULL};
334 if(run_exec_process(e2fsck))
335 return -1;
336
337 return 0;
338}
339
340int
Koushik Duttab4c5fd62011-01-02 22:54:31 -0800341format_ext2_device (const char *device) {
342 // Run mke2fs
343 char *const mke2fs[] = {MKE2FS_BIN, device, NULL};
344 if(run_exec_process(mke2fs))
345 return -1;
346
347 // Run tune2fs
348 char *const tune2fs[] = {TUNE2FS_BIN, "-C", "1", device, NULL};
349 if(run_exec_process(tune2fs))
350 return -1;
351
352 // Run e2fsck
353 char *const e2fsck[] = {E2FSCK_BIN, "-fy", device, NULL};
354 if(run_exec_process(e2fsck))
355 return -1;
356
357 return 0;
358}
359
360int
361mmc_format_ext3 (MmcPartition *partition) {
362 char device[128];
363 strcpy(device, partition->device_index);
364 return format_ext3_device(device);
365}
366
367int
Shashank Mittal815ca5d2010-07-27 11:09:19 -0700368mmc_mount_partition(const MmcPartition *partition, const char *mount_point,
369 int read_only)
370{
371 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
372 char devname[128];
373 int rv = -1;
374 strcpy(devname, partition->device_index);
375 if (partition->filesystem == NULL) {
376 printf("Null filesystem!\n");
377 return rv;
378 }
379 if (!read_only) {
380 rv = mount(devname, mount_point, partition->filesystem, flags, NULL);
381 }
382 if (read_only || rv < 0) {
383 rv = mount(devname, mount_point, partition->filesystem, flags | MS_RDONLY, 0);
384 if (rv < 0) {
385 printf("Failed to mount %s on %s: %s\n",
386 devname, mount_point, strerror(errno));
387 } else {
388 printf("Mount %s on %s read-only\n", devname, mount_point);
389 }
390 }
391 return rv;
392}
393
394int
395mmc_raw_copy (const MmcPartition *partition, char *in_file) {
396 int ch;
397 FILE *in;
398 FILE *out;
399 int val = 0;
400 char buf[512];
401 unsigned sz = 0;
402 unsigned i;
403 int ret = -1;
404 char *out_file = partition->device_index;
405
406 in = fopen ( in_file, "r" );
407 if (in == NULL)
408 goto ERROR3;
409
410 out = fopen ( out_file, "w" );
411 if (out == NULL)
412 goto ERROR2;
413
414 fseek(in, 0L, SEEK_END);
415 sz = ftell(in);
416 fseek(in, 0L, SEEK_SET);
417
418 if (sz % 512)
419 {
420 while ( ( ch = fgetc ( in ) ) != EOF )
421 fputc ( ch, out );
422 }
423 else
424 {
425 for (i=0; i< (sz/512); i++)
426 {
427 if ((fread(buf, 512, 1, in)) != 1)
428 goto ERROR1;
429 if ((fwrite(buf, 512, 1, out)) != 1)
430 goto ERROR1;
431 }
432 }
433
434 fsync(out);
435 ret = 0;
436ERROR1:
437 fclose ( out );
438ERROR2:
439 fclose ( in );
440ERROR3:
441 return ret;
442
443}
444
Koushik Duttafef77c02010-11-09 20:03:42 -0800445
Koushik Dutta19447c02010-11-10 10:40:44 -0800446// TODO: refactor this to not be a giant copy paste mess
Koushik Duttafef77c02010-11-09 20:03:42 -0800447int
448mmc_raw_dump (const MmcPartition *partition, char *out_file) {
449 int ch;
450 FILE *in;
451 FILE *out;
452 int val = 0;
453 char buf[512];
454 unsigned sz = 0;
455 unsigned i;
456 int ret = -1;
457 char *in_file = partition->device_index;
458
459 in = fopen ( in_file, "r" );
460 if (in == NULL)
461 goto ERROR3;
462
463 out = fopen ( out_file, "w" );
464 if (out == NULL)
465 goto ERROR2;
466
467 fseek(in, 0L, SEEK_END);
468 sz = ftell(in);
469 fseek(in, 0L, SEEK_SET);
470
471 if (sz % 512)
472 {
473 while ( ( ch = fgetc ( in ) ) != EOF )
474 fputc ( ch, out );
475 }
476 else
477 {
478 for (i=0; i< (sz/512); i++)
479 {
480 if ((fread(buf, 512, 1, in)) != 1)
481 goto ERROR1;
482 if ((fwrite(buf, 512, 1, out)) != 1)
483 goto ERROR1;
484 }
485 }
486
487 fsync(out);
488 ret = 0;
489ERROR1:
490 fclose ( out );
491ERROR2:
492 fclose ( in );
493ERROR3:
494 return ret;
495
496}
497
Koushik Dutta19447c02010-11-10 10:40:44 -0800498
499int
500mmc_raw_read (const MmcPartition *partition, char *data, int data_size) {
501 int ch;
502 FILE *in;
503 int val = 0;
504 char buf[512];
505 unsigned sz = 0;
506 unsigned i;
507 int ret = -1;
508 char *in_file = partition->device_index;
509
510 in = fopen ( in_file, "r" );
511 if (in == NULL)
512 goto ERROR3;
513
514 fseek(in, 0L, SEEK_END);
515 sz = ftell(in);
516 fseek(in, 0L, SEEK_SET);
517
518 fread(data, data_size, 1, in);
519
520 ret = 0;
521ERROR1:
522ERROR2:
523 fclose ( in );
524ERROR3:
525 return ret;
526
527}
528
529int
530mmc_raw_write (const MmcPartition *partition, char *data, int data_size) {
531 int ch;
532 FILE *out;
533 int val = 0;
534 char buf[512];
535 unsigned sz = 0;
536 unsigned i;
537 int ret = -1;
538 char *out_file = partition->device_index;
539
540 out = fopen ( out_file, "w" );
541 if (out == NULL)
542 goto ERROR3;
543
544 fwrite(data, data_size, 1, out);
545
546 ret = 0;
547ERROR1:
548ERROR2:
549 fclose ( out );
550ERROR3:
551 return ret;
552
553}
554
Steve Kondik4123b582010-11-14 03:18:40 -0500555int cmd_mmc_restore_raw_partition(const char *partition, const char *filename)
Koushik Dutta19447c02010-11-10 10:40:44 -0800556{
557 mmc_scan_partitions();
558 const MmcPartition *p;
559 p = mmc_find_partition_by_name(partition);
560 if (p == NULL)
561 return -1;
562 return mmc_raw_copy(p, filename);
563}
564
Steve Kondik4123b582010-11-14 03:18:40 -0500565int cmd_mmc_backup_raw_partition(const char *partition, const char *filename)
Koushik Dutta19447c02010-11-10 10:40:44 -0800566{
567 mmc_scan_partitions();
568 const MmcPartition *p;
569 p = mmc_find_partition_by_name(partition);
570 if (p == NULL)
571 return -1;
572 return mmc_raw_dump(p, filename);
573}
574
Steve Kondik4123b582010-11-14 03:18:40 -0500575int cmd_mmc_erase_raw_partition(const char *partition)
Koushik Dutta19447c02010-11-10 10:40:44 -0800576{
577 mmc_scan_partitions();
578 const MmcPartition *p;
579 p = mmc_find_partition_by_name(partition);
580 if (p == NULL)
581 return -1;
Steve Kondik4123b582010-11-14 03:18:40 -0500582
Koushik Dutta19447c02010-11-10 10:40:44 -0800583 // TODO: implement raw wipe
584 return 0;
585}
586
Steve Kondik4123b582010-11-14 03:18:40 -0500587int cmd_mmc_erase_partition(const char *partition, const char *filesystem)
Koushik Dutta19447c02010-11-10 10:40:44 -0800588{
589 mmc_scan_partitions();
590 const MmcPartition *p;
591 p = mmc_find_partition_by_name(partition);
592 if (p == NULL)
593 return -1;
594 return mmc_format_ext3 (p);
595}
596
Steve Kondik4123b582010-11-14 03:18:40 -0500597int cmd_mmc_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
Koushik Dutta19447c02010-11-10 10:40:44 -0800598{
599 mmc_scan_partitions();
600 const MmcPartition *p;
601 p = mmc_find_partition_by_name(partition);
602 if (p == NULL)
603 return -1;
604 return mmc_mount_partition(p, mount_point, read_only);
605}
606
Steve Kondik4123b582010-11-14 03:18:40 -0500607int cmd_mmc_get_partition_device(const char *partition, char *device)
Koushik Dutta19447c02010-11-10 10:40:44 -0800608{
609 mmc_scan_partitions();
610 const MmcPartition *p;
611 p = mmc_find_partition_by_name(partition);
612 if (p == NULL)
Koushik Dutta12154202010-11-11 01:16:14 -0800613 return -1;
614 strcpy(device, p->device_index);
615 return 0;
Koushik Dutta19447c02010-11-10 10:40:44 -0800616}