blob: 0b1467ea8983c2a2882c4c4f0f97651873fe7ae8 [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) {
13 if (access("/dev/block/bml1", F_OK) == 0) {
14 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 Dutta67fa0c32011-03-17 11:37:21 -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;
81
82 return type;
83}
84int restore_raw_partition(const char *partition, const char *filename)
85{
86 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
99int backup_raw_partition(const char *partition, const char *filename)
100{
Koushik Dutta67fa0c32011-03-17 11:37:21 -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:
110 return -1;
111 }
112}
113
114int erase_raw_partition(const char *partition)
115{
Koushik Dutta67fa0c32011-03-17 11:37:21 -0700116 int type = detect_partition(partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500117 switch (type) {
118 case MTD:
119 return cmd_mtd_erase_raw_partition(partition);
120 case MMC:
121 return cmd_mmc_erase_raw_partition(partition);
122 case BML:
123 return cmd_bml_erase_raw_partition(partition);
124 default:
125 return -1;
126 }
127}
128
129int erase_partition(const char *partition, const char *filesystem)
130{
Koushik Dutta67fa0c32011-03-17 11:37:21 -0700131 int type = detect_partition(partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500132 switch (type) {
133 case MTD:
134 return cmd_mtd_erase_partition(partition, filesystem);
135 case MMC:
136 return cmd_mmc_erase_partition(partition, filesystem);
137 case BML:
138 return cmd_bml_erase_partition(partition, filesystem);
139 default:
140 return -1;
141 }
142}
143
144int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
145{
Koushik Dutta67fa0c32011-03-17 11:37:21 -0700146 int type = detect_partition(partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500147 switch (type) {
148 case MTD:
149 return cmd_mtd_mount_partition(partition, mount_point, filesystem, read_only);
150 case MMC:
151 return cmd_mmc_mount_partition(partition, mount_point, filesystem, read_only);
152 case BML:
153 return cmd_bml_mount_partition(partition, mount_point, filesystem, read_only);
154 default:
155 return -1;
156 }
157}
158
159int get_partition_device(const char *partition, char *device)
160{
161 int type = device_flash_type();
162 switch (type) {
163 case MTD:
164 return cmd_mtd_get_partition_device(partition, device);
165 case MMC:
166 return cmd_mmc_get_partition_device(partition, device);
167 case BML:
168 return cmd_bml_get_partition_device(partition, device);
169 default:
170 return -1;
171 }
172}