blob: 7dc750cf31e009ec29ac1ac069c60dca36f57de9 [file] [log] [blame]
Steve Kondik4123b582010-11-14 03:18:40 -05001#include <signal.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <sys/wait.h>
5
6#include "flashutils/flashutils.h"
7
Steve Kondik4123b582010-11-14 03:18:40 -05008int the_flash_type = UNKNOWN;
9
10int device_flash_type()
11{
12 if (the_flash_type == UNKNOWN) {
Koushik Duttaf5735102011-04-22 12:12:32 -070013 if (access("/dev/block/bml7", F_OK) == 0) {
Steve Kondik4123b582010-11-14 03:18:40 -050014 the_flash_type = BML;
15 } else if (access("/proc/emmc", F_OK) == 0) {
16 the_flash_type = MMC;
17 } else if (access("/proc/mtd", F_OK) == 0) {
18 the_flash_type = MTD;
19 } else {
20 the_flash_type = UNSUPPORTED;
21 }
22 }
23 return the_flash_type;
24}
25
26char* get_default_filesystem()
27{
28 return device_flash_type() == MMC ? "ext3" : "yaffs2";
29}
30
31// This was pulled from bionic: The default system command always looks
32// for shell in /system/bin/sh. This is bad.
33#define _PATH_BSHELL "/sbin/sh"
34
35extern char **environ;
36int
37__system(const char *command)
38{
39 pid_t pid;
40 sig_t intsave, quitsave;
41 sigset_t mask, omask;
42 int pstat;
43 char *argp[] = {"sh", "-c", NULL, NULL};
44
45 if (!command) /* just checking... */
46 return(1);
47
48 argp[2] = (char *)command;
49
50 sigemptyset(&mask);
51 sigaddset(&mask, SIGCHLD);
52 sigprocmask(SIG_BLOCK, &mask, &omask);
53 switch (pid = vfork()) {
54 case -1: /* error */
55 sigprocmask(SIG_SETMASK, &omask, NULL);
56 return(-1);
57 case 0: /* child */
58 sigprocmask(SIG_SETMASK, &omask, NULL);
59 execve(_PATH_BSHELL, argp, environ);
60 _exit(127);
61 }
62
63 intsave = (sig_t) bsd_signal(SIGINT, SIG_IGN);
64 quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN);
65 pid = waitpid(pid, (int *)&pstat, 0);
66 sigprocmask(SIG_SETMASK, &omask, NULL);
67 (void)bsd_signal(SIGINT, intsave);
68 (void)bsd_signal(SIGQUIT, quitsave);
69 return (pid == -1 ? -1 : pstat);
70}
71
Koushik Dutta8a6bc772011-05-26 11:14:15 -070072static int detect_partition(const char *partitionType, const char *partition)
Steve Kondik4123b582010-11-14 03:18:40 -050073{
74 int type = device_flash_type();
Koushik Dutta67fa0c32011-03-17 11:37:21 -070075 if (strstr(partition, "/dev/block/mtd") != NULL)
76 type = MTD;
77 else if (strstr(partition, "/dev/block/mmc") != NULL)
78 type = MMC;
79 else if (strstr(partition, "/dev/block/bml") != NULL)
80 type = BML;
Koushik Dutta8a6bc772011-05-26 11:14:15 -070081
82 if (partitionType != NULL) {
83 if (strstr(partitionType, "mtd") != NULL)
84 type = MTD;
85 else if (strstr(partitionType, "emmc") != NULL)
86 type = MMC;
87 else if (strstr(partitionType, "bml") != NULL)
88 type = BML;
89 }
90
91 printf("partitionType: %s\n", partitionType);
92 printf("partition: %s\n", partition);
93 printf("detected type: %d\n", type);
Koushik Dutta67fa0c32011-03-17 11:37:21 -070094 return type;
95}
Koushik Dutta8a6bc772011-05-26 11:14:15 -070096int restore_raw_partition(const char* partitionType, const char *partition, const char *filename)
Koushik Dutta67fa0c32011-03-17 11:37:21 -070097{
Koushik Dutta8a6bc772011-05-26 11:14:15 -070098 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -050099 switch (type) {
100 case MTD:
101 return cmd_mtd_restore_raw_partition(partition, filename);
102 case MMC:
103 return cmd_mmc_restore_raw_partition(partition, filename);
104 case BML:
105 return cmd_bml_restore_raw_partition(partition, filename);
106 default:
107 return -1;
108 }
109}
110
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700111int backup_raw_partition(const char* partitionType, const char *partition, const char *filename)
Steve Kondik4123b582010-11-14 03:18:40 -0500112{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700113 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500114 switch (type) {
115 case MTD:
116 return cmd_mtd_backup_raw_partition(partition, filename);
117 case MMC:
118 return cmd_mmc_backup_raw_partition(partition, filename);
119 case BML:
120 return cmd_bml_backup_raw_partition(partition, filename);
121 default:
Koushik Duttaf5735102011-04-22 12:12:32 -0700122 printf("unable to detect device type");
Steve Kondik4123b582010-11-14 03:18:40 -0500123 return -1;
124 }
125}
126
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700127int erase_raw_partition(const char* partitionType, const char *partition)
Steve Kondik4123b582010-11-14 03:18:40 -0500128{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700129 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500130 switch (type) {
131 case MTD:
132 return cmd_mtd_erase_raw_partition(partition);
133 case MMC:
134 return cmd_mmc_erase_raw_partition(partition);
135 case BML:
136 return cmd_bml_erase_raw_partition(partition);
137 default:
138 return -1;
139 }
140}
141
142int erase_partition(const char *partition, const char *filesystem)
143{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700144 int type = detect_partition(NULL, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500145 switch (type) {
146 case MTD:
147 return cmd_mtd_erase_partition(partition, filesystem);
148 case MMC:
149 return cmd_mmc_erase_partition(partition, filesystem);
150 case BML:
151 return cmd_bml_erase_partition(partition, filesystem);
152 default:
153 return -1;
154 }
155}
156
157int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
158{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700159 int type = detect_partition(NULL, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500160 switch (type) {
161 case MTD:
162 return cmd_mtd_mount_partition(partition, mount_point, filesystem, read_only);
163 case MMC:
164 return cmd_mmc_mount_partition(partition, mount_point, filesystem, read_only);
165 case BML:
166 return cmd_bml_mount_partition(partition, mount_point, filesystem, read_only);
167 default:
168 return -1;
169 }
170}
171
172int get_partition_device(const char *partition, char *device)
173{
174 int type = device_flash_type();
175 switch (type) {
176 case MTD:
177 return cmd_mtd_get_partition_device(partition, device);
178 case MMC:
179 return cmd_mmc_get_partition_device(partition, device);
180 case BML:
181 return cmd_bml_get_partition_device(partition, device);
182 default:
183 return -1;
184 }
185}