blob: 27f0c663b222a2a9c3fd33af7410a8d4fbafbf2e [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
Koushik Dutta01143a52011-05-30 15:22:04 -070098 return type;
Koushik Dutta67fa0c32011-03-17 11:37:21 -070099}
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700100int restore_raw_partition(const char* partitionType, const char *partition, const char *filename)
Koushik Dutta67fa0c32011-03-17 11:37:21 -0700101{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700102 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500103 switch (type) {
104 case MTD:
105 return cmd_mtd_restore_raw_partition(partition, filename);
106 case MMC:
107 return cmd_mmc_restore_raw_partition(partition, filename);
108 case BML:
109 return cmd_bml_restore_raw_partition(partition, filename);
110 default:
111 return -1;
112 }
113}
114
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700115int backup_raw_partition(const char* partitionType, const char *partition, const char *filename)
Steve Kondik4123b582010-11-14 03:18:40 -0500116{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700117 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500118 switch (type) {
119 case MTD:
120 return cmd_mtd_backup_raw_partition(partition, filename);
121 case MMC:
122 return cmd_mmc_backup_raw_partition(partition, filename);
123 case BML:
124 return cmd_bml_backup_raw_partition(partition, filename);
125 default:
Koushik Duttaf5735102011-04-22 12:12:32 -0700126 printf("unable to detect device type");
Steve Kondik4123b582010-11-14 03:18:40 -0500127 return -1;
128 }
129}
130
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700131int erase_raw_partition(const char* partitionType, const char *partition)
Steve Kondik4123b582010-11-14 03:18:40 -0500132{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700133 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500134 switch (type) {
135 case MTD:
136 return cmd_mtd_erase_raw_partition(partition);
137 case MMC:
138 return cmd_mmc_erase_raw_partition(partition);
139 case BML:
140 return cmd_bml_erase_raw_partition(partition);
141 default:
142 return -1;
143 }
144}
145
146int erase_partition(const char *partition, const char *filesystem)
147{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700148 int type = detect_partition(NULL, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500149 switch (type) {
150 case MTD:
151 return cmd_mtd_erase_partition(partition, filesystem);
152 case MMC:
153 return cmd_mmc_erase_partition(partition, filesystem);
154 case BML:
155 return cmd_bml_erase_partition(partition, filesystem);
156 default:
157 return -1;
158 }
159}
160
161int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
162{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700163 int type = detect_partition(NULL, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500164 switch (type) {
165 case MTD:
166 return cmd_mtd_mount_partition(partition, mount_point, filesystem, read_only);
167 case MMC:
168 return cmd_mmc_mount_partition(partition, mount_point, filesystem, read_only);
169 case BML:
170 return cmd_bml_mount_partition(partition, mount_point, filesystem, read_only);
171 default:
172 return -1;
173 }
174}
175
176int get_partition_device(const char *partition, char *device)
177{
178 int type = device_flash_type();
179 switch (type) {
180 case MTD:
181 return cmd_mtd_get_partition_device(partition, device);
182 case MMC:
183 return cmd_mmc_get_partition_device(partition, device);
184 case BML:
185 return cmd_bml_get_partition_device(partition, device);
186 default:
187 return -1;
188 }
189}