blob: b71d4fa2374a29b889aa41d2806e945ed879ad84 [file] [log] [blame]
preludedrew38058dc2011-01-29 23:30:44 -07001#include <signal.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <sys/wait.h>
5
6#include "flashutils/flashutils.h"
7
8int 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
72int restore_raw_partition(const char *partition, const char *filename)
73{
74 int type = device_flash_type();
75 switch (type) {
76 case MTD:
77 return cmd_mtd_restore_raw_partition(partition, filename);
78 case MMC:
79 return cmd_mmc_restore_raw_partition(partition, filename);
80 case BML:
81 return cmd_bml_restore_raw_partition(partition, filename);
82 default:
83 return -1;
84 }
85}
86
87int backup_raw_partition(const char *partition, const char *filename)
88{
89 int type = device_flash_type();
90 switch (type) {
91 case MTD:
92 return cmd_mtd_backup_raw_partition(partition, filename);
93 case MMC:
94 return cmd_mmc_backup_raw_partition(partition, filename);
95 case BML:
96 return cmd_bml_backup_raw_partition(partition, filename);
97 default:
98 return -1;
99 }
100}
101
102int erase_raw_partition(const char *partition)
103{
104 int type = device_flash_type();
105 switch (type) {
106 case MTD:
107 return cmd_mtd_erase_raw_partition(partition);
108 case MMC:
109 return cmd_mmc_erase_raw_partition(partition);
110 case BML:
111 return cmd_bml_erase_raw_partition(partition);
112 default:
113 return -1;
114 }
115}
116
117int erase_partition(const char *partition, const char *filesystem)
118{
119 int type = device_flash_type();
120 switch (type) {
121 case MTD:
122 return cmd_mtd_erase_partition(partition, filesystem);
123 case MMC:
124 return cmd_mmc_erase_partition(partition, filesystem);
125 case BML:
126 return cmd_bml_erase_partition(partition, filesystem);
127 default:
128 return -1;
129 }
130}
131
132int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
133{
134 int type = device_flash_type();
135 switch (type) {
136 case MTD:
137 return cmd_mtd_mount_partition(partition, mount_point, filesystem, read_only);
138 case MMC:
139 return cmd_mmc_mount_partition(partition, mount_point, filesystem, read_only);
140 case BML:
141 return cmd_bml_mount_partition(partition, mount_point, filesystem, read_only);
142 default:
143 return -1;
144 }
145}
146
147int get_partition_device(const char *partition, char *device)
148{
149 int type = device_flash_type();
150 switch (type) {
151 case MTD:
152 return cmd_mtd_get_partition_device(partition, device);
153 case MMC:
154 return cmd_mmc_get_partition_device(partition, device);
155 case BML:
156 return cmd_bml_get_partition_device(partition, device);
157 default:
158 return -1;
159 }
160}