blob: 7fa38a9a22ddd3c39479cd4c49f12543c6d302a7 [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>
Koushik Dutta7adeadc2011-05-26 11:47:56 -07005#include <stdio.h>
Steve Kondik4123b582010-11-14 03:18:40 -05006
7#include "flashutils/flashutils.h"
8
Steve Kondik4123b582010-11-14 03:18:40 -05009int the_flash_type = UNKNOWN;
10
11int device_flash_type()
12{
13 if (the_flash_type == UNKNOWN) {
Koushik Duttaf5735102011-04-22 12:12:32 -070014 if (access("/dev/block/bml7", F_OK) == 0) {
Steve Kondik4123b582010-11-14 03:18:40 -050015 the_flash_type = BML;
16 } else if (access("/proc/emmc", F_OK) == 0) {
17 the_flash_type = MMC;
18 } else if (access("/proc/mtd", F_OK) == 0) {
19 the_flash_type = MTD;
20 } else {
21 the_flash_type = UNSUPPORTED;
22 }
23 }
24 return the_flash_type;
25}
26
27char* get_default_filesystem()
28{
29 return device_flash_type() == MMC ? "ext3" : "yaffs2";
30}
31
32// This was pulled from bionic: The default system command always looks
33// for shell in /system/bin/sh. This is bad.
34#define _PATH_BSHELL "/sbin/sh"
35
36extern char **environ;
37int
38__system(const char *command)
39{
40 pid_t pid;
41 sig_t intsave, quitsave;
42 sigset_t mask, omask;
43 int pstat;
44 char *argp[] = {"sh", "-c", NULL, NULL};
45
46 if (!command) /* just checking... */
47 return(1);
48
49 argp[2] = (char *)command;
50
51 sigemptyset(&mask);
52 sigaddset(&mask, SIGCHLD);
53 sigprocmask(SIG_BLOCK, &mask, &omask);
54 switch (pid = vfork()) {
55 case -1: /* error */
56 sigprocmask(SIG_SETMASK, &omask, NULL);
57 return(-1);
58 case 0: /* child */
59 sigprocmask(SIG_SETMASK, &omask, NULL);
60 execve(_PATH_BSHELL, argp, environ);
61 _exit(127);
62 }
63
64 intsave = (sig_t) bsd_signal(SIGINT, SIG_IGN);
65 quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN);
66 pid = waitpid(pid, (int *)&pstat, 0);
67 sigprocmask(SIG_SETMASK, &omask, NULL);
68 (void)bsd_signal(SIGINT, intsave);
69 (void)bsd_signal(SIGQUIT, quitsave);
70 return (pid == -1 ? -1 : pstat);
71}
72
Koushik Dutta41873472011-05-26 17:30:43 -070073int get_flash_type(const char* partitionType) {
74 int type = UNSUPPORTED;
75 if (strcmp(partitionType, "mtd") == 0)
76 type = MTD;
77 else if (strcmp(partitionType, "emmc") == 0)
78 type = MMC;
79 else if (strcmp(partitionType, "bml") == 0)
80 type = BML;
81 return type;
82}
83
Koushik Dutta8a6bc772011-05-26 11:14:15 -070084static int detect_partition(const char *partitionType, const char *partition)
Steve Kondik4123b582010-11-14 03:18:40 -050085{
86 int type = device_flash_type();
Koushik Dutta67fa0c32011-03-17 11:37:21 -070087 if (strstr(partition, "/dev/block/mtd") != NULL)
88 type = MTD;
89 else if (strstr(partition, "/dev/block/mmc") != NULL)
90 type = MMC;
91 else if (strstr(partition, "/dev/block/bml") != NULL)
92 type = BML;
Koushik Dutta8a6bc772011-05-26 11:14:15 -070093
94 if (partitionType != NULL) {
Koushik Dutta41873472011-05-26 17:30:43 -070095 type = get_flash_type(partitionType);
Koushik Dutta8a6bc772011-05-26 11:14:15 -070096 }
97
98 printf("partitionType: %s\n", partitionType);
99 printf("partition: %s\n", partition);
100 printf("detected type: %d\n", type);
Koushik Dutta67fa0c32011-03-17 11:37:21 -0700101 return type;
102}
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700103int restore_raw_partition(const char* partitionType, const char *partition, const char *filename)
Koushik Dutta67fa0c32011-03-17 11:37:21 -0700104{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700105 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500106 switch (type) {
107 case MTD:
108 return cmd_mtd_restore_raw_partition(partition, filename);
109 case MMC:
110 return cmd_mmc_restore_raw_partition(partition, filename);
111 case BML:
112 return cmd_bml_restore_raw_partition(partition, filename);
113 default:
114 return -1;
115 }
116}
117
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700118int backup_raw_partition(const char* partitionType, const char *partition, const char *filename)
Steve Kondik4123b582010-11-14 03:18:40 -0500119{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700120 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500121 switch (type) {
122 case MTD:
123 return cmd_mtd_backup_raw_partition(partition, filename);
124 case MMC:
125 return cmd_mmc_backup_raw_partition(partition, filename);
126 case BML:
127 return cmd_bml_backup_raw_partition(partition, filename);
128 default:
Koushik Duttaf5735102011-04-22 12:12:32 -0700129 printf("unable to detect device type");
Steve Kondik4123b582010-11-14 03:18:40 -0500130 return -1;
131 }
132}
133
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700134int erase_raw_partition(const char* partitionType, const char *partition)
Steve Kondik4123b582010-11-14 03:18:40 -0500135{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700136 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500137 switch (type) {
138 case MTD:
139 return cmd_mtd_erase_raw_partition(partition);
140 case MMC:
141 return cmd_mmc_erase_raw_partition(partition);
142 case BML:
143 return cmd_bml_erase_raw_partition(partition);
144 default:
145 return -1;
146 }
147}
148
149int erase_partition(const char *partition, const char *filesystem)
150{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700151 int type = detect_partition(NULL, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500152 switch (type) {
153 case MTD:
154 return cmd_mtd_erase_partition(partition, filesystem);
155 case MMC:
156 return cmd_mmc_erase_partition(partition, filesystem);
157 case BML:
158 return cmd_bml_erase_partition(partition, filesystem);
159 default:
160 return -1;
161 }
162}
163
164int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
165{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700166 int type = detect_partition(NULL, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500167 switch (type) {
168 case MTD:
169 return cmd_mtd_mount_partition(partition, mount_point, filesystem, read_only);
170 case MMC:
171 return cmd_mmc_mount_partition(partition, mount_point, filesystem, read_only);
172 case BML:
173 return cmd_bml_mount_partition(partition, mount_point, filesystem, read_only);
174 default:
175 return -1;
176 }
177}
178
179int get_partition_device(const char *partition, char *device)
180{
181 int type = device_flash_type();
182 switch (type) {
183 case MTD:
184 return cmd_mtd_get_partition_device(partition, device);
185 case MMC:
186 return cmd_mmc_get_partition_device(partition, device);
187 case BML:
188 return cmd_bml_get_partition_device(partition, device);
189 default:
190 return -1;
191 }
192}