blob: d59475ebaa59096b938c65db9abf907ccea8d35f [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001#include <ctype.h>
2#include <errno.h>
3#include <fcntl.h>
4#include <getopt.h>
5#include <limits.h>
6#include <linux/input.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <sys/reboot.h>
11#include <sys/types.h>
12#include <time.h>
13#include <unistd.h>
14
15#include <sys/wait.h>
16#include <sys/limits.h>
17#include <dirent.h>
18#include <sys/stat.h>
19
20#include <signal.h>
21#include <sys/wait.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040022
Conn O'Griofad9201042014-03-25 01:26:49 +000023#include <bmlutils.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040024
25#undef _PATH_BSHELL
26#define _PATH_BSHELL "/sbin/sh"
27
28int __system(const char *command)
29{
30 pid_t pid;
31 sig_t intsave, quitsave;
32 sigset_t mask, omask;
33 int pstat;
34 char *argp[] = {"sh", "-c", NULL, NULL};
35
36 if (!command) /* just checking... */
37 return(1);
38
39 argp[2] = (char *)command;
40
41 sigemptyset(&mask);
42 sigaddset(&mask, SIGCHLD);
43 sigprocmask(SIG_BLOCK, &mask, &omask);
44 switch (pid = vfork()) {
45 case -1: /* error */
46 sigprocmask(SIG_SETMASK, &omask, NULL);
47 return(-1);
48 case 0: /* child */
49 sigprocmask(SIG_SETMASK, &omask, NULL);
50 execve(_PATH_BSHELL, argp, environ);
51 _exit(127);
52 }
53
54 intsave = (sig_t) bsd_signal(SIGINT, SIG_IGN);
55 quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN);
56 pid = waitpid(pid, (int *)&pstat, 0);
57 sigprocmask(SIG_SETMASK, &omask, NULL);
58 (void)bsd_signal(SIGINT, intsave);
59 (void)bsd_signal(SIGQUIT, quitsave);
60 return (pid == -1 ? -1 : pstat);
61}
62
63static struct pid {
64 struct pid *next;
65 FILE *fp;
66 pid_t pid;
67} *pidlist;
68
69
70static int restore_internal(const char* bml, const char* filename)
71{
72 char buf[4096];
73 int dstfd, srcfd, bytes_read, bytes_written, total_read = 0;
74 if (filename == NULL)
75 srcfd = 0;
76 else {
77 srcfd = open(filename, O_RDONLY | O_LARGEFILE);
78 if (srcfd < 0)
79 return 2;
80 }
81 dstfd = open(bml, O_RDWR | O_LARGEFILE);
82 if (dstfd < 0)
83 return 3;
84 if (ioctl(dstfd, BML_UNLOCK_ALL, 0))
85 return 4;
86 do {
87 total_read += bytes_read = read(srcfd, buf, 4096);
88 if (!bytes_read)
89 break;
90 if (bytes_read < 4096)
91 memset(&buf[bytes_read], 0, 4096 - bytes_read);
92 if (write(dstfd, buf, 4096) < 4096)
93 return 5;
94 } while(bytes_read == 4096);
95
96 close(dstfd);
97 close(srcfd);
98
99 return 0;
100}
101
102int cmd_bml_restore_raw_partition(const char *partition, const char *filename)
103{
104 if (strcmp(partition, "boot") != 0 && strcmp(partition, "recovery") != 0 && strcmp(partition, "recoveryonly") != 0 && partition[0] != '/')
105 return 6;
106
107 int ret = -1;
108 if (strcmp(partition, "recoveryonly") != 0) {
109 // always restore boot, regardless of whether recovery or boot is flashed.
110 // this is because boot and recovery are the same on some samsung phones.
111 // unless of course, recoveryonly is explictly chosen (bml8)
112 ret = restore_internal(BOARD_BML_BOOT, filename);
113 if (ret != 0)
114 return ret;
115 }
116
117 if (strcmp(partition, "recovery") == 0 || strcmp(partition, "recoveryonly") == 0)
118 ret = restore_internal(BOARD_BML_RECOVERY, filename);
119
120 // support explicitly provided device paths
121 if (partition[0] == '/')
122 ret = restore_internal(partition, filename);
123 return ret;
124}
125
126int cmd_bml_backup_raw_partition(const char *partition, const char *out_file)
127{
128 char* bml;
129 if (strcmp("boot", partition) == 0)
130 bml = BOARD_BML_BOOT;
131 else if (strcmp("recovery", partition) == 0)
132 bml = BOARD_BML_RECOVERY;
133 else if (partition[0] == '/') {
134 // support explicitly provided device paths
135 bml = partition;
136 }
137 else {
138 printf("Invalid partition.\n");
139 return -1;
140 }
141
142 int ch;
143 FILE *in;
144 FILE *out;
145 int val = 0;
146 char buf[512];
147 unsigned sz = 0;
148 unsigned i;
149 int ret = -1;
150 char *in_file = bml;
151
152 in = fopen ( in_file, "r" );
153 if (in == NULL)
154 goto ERROR3;
155
156 out = fopen ( out_file, "w" );
157 if (out == NULL)
158 goto ERROR2;
159
160 fseek(in, 0L, SEEK_END);
161 sz = ftell(in);
162 fseek(in, 0L, SEEK_SET);
163
164 if (sz % 512)
165 {
166 while ( ( ch = fgetc ( in ) ) != EOF )
167 fputc ( ch, out );
168 }
169 else
170 {
171 for (i=0; i< (sz/512); i++)
172 {
173 if ((fread(buf, 512, 1, in)) != 1)
174 goto ERROR1;
175 if ((fwrite(buf, 512, 1, out)) != 1)
176 goto ERROR1;
177 }
178 }
179
180 fsync(out);
181 ret = 0;
182ERROR1:
183 fclose ( out );
184ERROR2:
185 fclose ( in );
186ERROR3:
187 return ret;
188}
189
190int cmd_bml_erase_raw_partition(const char *partition)
191{
192 // TODO: implement raw wipe
193 return 0;
194}
195
196int cmd_bml_erase_partition(const char *partition, const char *filesystem)
197{
198 return -1;
199}
200
201int cmd_bml_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
202{
203 return -1;
204}
205
206int cmd_bml_get_partition_device(const char *partition, char *device)
207{
208 return -1;
209}
210
211int format_rfs_device (const char *device, const char *path) {
212 const char *fatsize = "32";
213 const char *sectorsize = "1";
214
215 if (strcmp(path, "/datadata") == 0 || strcmp(path, "/cache") == 0) {
216 fatsize = "16";
217 }
218
219 // Just in case /data sector size needs to be altered
220 else if (strcmp(path, "/data") == 0 ) {
221 sectorsize = "1";
222 }
223
224 // dump 10KB of zeros to partition before format due to fat.format bug
225 char cmd[PATH_MAX];
226
227 sprintf(cmd, "/sbin/dd if=/dev/zero of=%s bs=4096 count=10", device);
228 if(__system(cmd)) {
229 printf("failure while zeroing rfs partition.\n");
230 return -1;
231 }
232
233 // Run fat.format
234 sprintf(cmd, "/sbin/fat.format -F %s -S 4096 -s %s %s", fatsize, sectorsize, device);
235 if(__system(cmd)) {
236 printf("failure while running fat.format\n");
237 return -1;
238 }
239
240 return 0;
241}