blob: 7504e4a5894a6600c7a692693e0b7efbeac081bb [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 Duttafdd47662011-04-17 00:49:15 -070072static int detect_partition(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 Duttaf5735102011-04-22 12:12:32 -070081
Koushik Dutta67fa0c32011-03-17 11:37:21 -070082 return type;
83}
Koushik Duttafdd47662011-04-17 00:49:15 -070084int restore_raw_partition(const char *partition, const char *filename)
Koushik Dutta67fa0c32011-03-17 11:37:21 -070085{
Koushik Duttafdd47662011-04-17 00:49:15 -070086 int type = detect_partition(partition);
Steve Kondik4123b582010-11-14 03:18:40 -050087 switch (type) {
88 case MTD:
89 return cmd_mtd_restore_raw_partition(partition, filename);
90 case MMC:
91 return cmd_mmc_restore_raw_partition(partition, filename);
92 case BML:
93 return cmd_bml_restore_raw_partition(partition, filename);
94 default:
95 return -1;
96 }
97}
98
Koushik Duttafdd47662011-04-17 00:49:15 -070099int backup_raw_partition(const char *partition, const char *filename)
Steve Kondik4123b582010-11-14 03:18:40 -0500100{
Koushik Duttafdd47662011-04-17 00:49:15 -0700101 int type = detect_partition(partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500102 switch (type) {
103 case MTD:
104 return cmd_mtd_backup_raw_partition(partition, filename);
105 case MMC:
106 return cmd_mmc_backup_raw_partition(partition, filename);
107 case BML:
108 return cmd_bml_backup_raw_partition(partition, filename);
109 default:
Koushik Duttaf5735102011-04-22 12:12:32 -0700110 printf("unable to detect device type");
Steve Kondik4123b582010-11-14 03:18:40 -0500111 return -1;
112 }
113}
114
Koushik Duttafdd47662011-04-17 00:49:15 -0700115int erase_raw_partition(const char *partition)
Steve Kondik4123b582010-11-14 03:18:40 -0500116{
Koushik Duttafdd47662011-04-17 00:49:15 -0700117 int type = detect_partition(partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500118 switch (type) {
119 case MTD:
120 return cmd_mtd_erase_raw_partition(partition);
121 case MMC:
122 return cmd_mmc_erase_raw_partition(partition);
123 case BML:
124 return cmd_bml_erase_raw_partition(partition);
125 default:
126 return -1;
127 }
128}
129
130int erase_partition(const char *partition, const char *filesystem)
131{
Koushik Duttafdd47662011-04-17 00:49:15 -0700132 int type = detect_partition(partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500133 switch (type) {
134 case MTD:
135 return cmd_mtd_erase_partition(partition, filesystem);
136 case MMC:
137 return cmd_mmc_erase_partition(partition, filesystem);
138 case BML:
139 return cmd_bml_erase_partition(partition, filesystem);
140 default:
141 return -1;
142 }
143}
144
145int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
146{
Koushik Duttafdd47662011-04-17 00:49:15 -0700147 int type = detect_partition(partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500148 switch (type) {
149 case MTD:
150 return cmd_mtd_mount_partition(partition, mount_point, filesystem, read_only);
151 case MMC:
152 return cmd_mmc_mount_partition(partition, mount_point, filesystem, read_only);
153 case BML:
154 return cmd_bml_mount_partition(partition, mount_point, filesystem, read_only);
155 default:
156 return -1;
157 }
158}
159
160int get_partition_device(const char *partition, char *device)
161{
162 int type = device_flash_type();
163 switch (type) {
164 case MTD:
165 return cmd_mtd_get_partition_device(partition, device);
166 case MMC:
167 return cmd_mmc_get_partition_device(partition, device);
168 case BML:
169 return cmd_bml_get_partition_device(partition, device);
170 default:
171 return -1;
172 }
173}