blob: 2f8da412a7bed9955157577fd6f6af6d745922ef [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 Dutta8a6bc772011-05-26 11:14:15 -070073static int detect_partition(const char *partitionType, const char *partition)
Steve Kondik4123b582010-11-14 03:18:40 -050074{
75 int type = device_flash_type();
Koushik Dutta67fa0c32011-03-17 11:37:21 -070076 if (strstr(partition, "/dev/block/mtd") != NULL)
77 type = MTD;
78 else if (strstr(partition, "/dev/block/mmc") != NULL)
79 type = MMC;
80 else if (strstr(partition, "/dev/block/bml") != NULL)
81 type = BML;
Koushik Dutta8a6bc772011-05-26 11:14:15 -070082
83 if (partitionType != NULL) {
84 if (strstr(partitionType, "mtd") != NULL)
85 type = MTD;
86 else if (strstr(partitionType, "emmc") != NULL)
87 type = MMC;
88 else if (strstr(partitionType, "bml") != NULL)
89 type = BML;
90 }
91
92 printf("partitionType: %s\n", partitionType);
93 printf("partition: %s\n", partition);
94 printf("detected type: %d\n", type);
Koushik Dutta67fa0c32011-03-17 11:37:21 -070095 return type;
96}
Koushik Dutta8a6bc772011-05-26 11:14:15 -070097int restore_raw_partition(const char* partitionType, const char *partition, const char *filename)
Koushik Dutta67fa0c32011-03-17 11:37:21 -070098{
Koushik Dutta8a6bc772011-05-26 11:14:15 -070099 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500100 switch (type) {
101 case MTD:
102 return cmd_mtd_restore_raw_partition(partition, filename);
103 case MMC:
104 return cmd_mmc_restore_raw_partition(partition, filename);
105 case BML:
106 return cmd_bml_restore_raw_partition(partition, filename);
107 default:
108 return -1;
109 }
110}
111
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700112int backup_raw_partition(const char* partitionType, const char *partition, const char *filename)
Steve Kondik4123b582010-11-14 03:18:40 -0500113{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700114 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500115 switch (type) {
116 case MTD:
117 return cmd_mtd_backup_raw_partition(partition, filename);
118 case MMC:
119 return cmd_mmc_backup_raw_partition(partition, filename);
120 case BML:
121 return cmd_bml_backup_raw_partition(partition, filename);
122 default:
Koushik Duttaf5735102011-04-22 12:12:32 -0700123 printf("unable to detect device type");
Steve Kondik4123b582010-11-14 03:18:40 -0500124 return -1;
125 }
126}
127
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700128int erase_raw_partition(const char* partitionType, const char *partition)
Steve Kondik4123b582010-11-14 03:18:40 -0500129{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700130 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500131 switch (type) {
132 case MTD:
133 return cmd_mtd_erase_raw_partition(partition);
134 case MMC:
135 return cmd_mmc_erase_raw_partition(partition);
136 case BML:
137 return cmd_bml_erase_raw_partition(partition);
138 default:
139 return -1;
140 }
141}
142
143int erase_partition(const char *partition, const char *filesystem)
144{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700145 int type = detect_partition(NULL, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500146 switch (type) {
147 case MTD:
148 return cmd_mtd_erase_partition(partition, filesystem);
149 case MMC:
150 return cmd_mmc_erase_partition(partition, filesystem);
151 case BML:
152 return cmd_bml_erase_partition(partition, filesystem);
153 default:
154 return -1;
155 }
156}
157
158int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
159{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700160 int type = detect_partition(NULL, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500161 switch (type) {
162 case MTD:
163 return cmd_mtd_mount_partition(partition, mount_point, filesystem, read_only);
164 case MMC:
165 return cmd_mmc_mount_partition(partition, mount_point, filesystem, read_only);
166 case BML:
167 return cmd_bml_mount_partition(partition, mount_point, filesystem, read_only);
168 default:
169 return -1;
170 }
171}
172
173int get_partition_device(const char *partition, char *device)
174{
175 int type = device_flash_type();
176 switch (type) {
177 case MTD:
178 return cmd_mtd_get_partition_device(partition, device);
179 case MMC:
180 return cmd_mmc_get_partition_device(partition, device);
181 case BML:
182 return cmd_bml_get_partition_device(partition, device);
183 default:
184 return -1;
185 }
186}