blob: 952c091ee83b6a22d46942e872ce4cb76f1d49f1 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <assert.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <limits.h>
21#include <paths.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <sys/wait.h>
28#include <unistd.h>
29
30#include <ctype.h>
31#include "cutils/misc.h"
32#include "cutils/properties.h"
33#include <dirent.h>
34#include <getopt.h>
35#include <linux/input.h>
36#include <signal.h>
37#include <sys/limits.h>
38#include <termios.h>
39#include <time.h>
40#include <sys/vfs.h>
41
42#include "tw_reboot.h"
43#include "bootloader.h"
44#include "common.h"
45#include "extra-functions.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040046#include "minuitwrp/minui.h"
47#include "minzip/DirUtil.h"
48#include "minzip/Zip.h"
49#include "recovery_ui.h"
50#include "roots.h"
51#include "data.h"
52#include "variables.h"
Dees_Troy657c3092012-09-10 20:32:10 -040053#include "mincrypt/rsa.h"
54#include "verifier.h"
55#include "mincrypt/sha.h"
56
57#ifndef PUBLIC_KEYS_FILE
58#define PUBLIC_KEYS_FILE "/res/keys"
59#endif
60#ifndef ASSUMED_UPDATE_BINARY_NAME
61#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
62#endif
63enum { INSTALL_SUCCESS, INSTALL_ERROR, INSTALL_CORRUPT };
Dees_Troy51a0e822012-09-05 15:24:24 -040064
65//kang system() from bionic/libc/unistd and rename it __system() so we can be even more hackish :)
66#undef _PATH_BSHELL
67#define _PATH_BSHELL "/sbin/sh"
68
Dees_Troy51a0e822012-09-05 15:24:24 -040069extern char **environ;
70
71int __system(const char *command) {
72 pid_t pid;
73 sig_t intsave, quitsave;
74 sigset_t mask, omask;
75 int pstat;
76 char *argp[] = {"sh", "-c", NULL, NULL};
77
78 if (!command) /* just checking... */
79 return(1);
80
81 argp[2] = (char *)command;
82
83 sigemptyset(&mask);
84 sigaddset(&mask, SIGCHLD);
85 sigprocmask(SIG_BLOCK, &mask, &omask);
86 switch (pid = vfork()) {
87 case -1: /* error */
88 sigprocmask(SIG_SETMASK, &omask, NULL);
89 return(-1);
90 case 0: /* child */
91 sigprocmask(SIG_SETMASK, &omask, NULL);
92 execve(_PATH_BSHELL, argp, environ);
93 _exit(127);
94 }
95
96 intsave = (sig_t) bsd_signal(SIGINT, SIG_IGN);
97 quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN);
98 pid = waitpid(pid, (int *)&pstat, 0);
99 sigprocmask(SIG_SETMASK, &omask, NULL);
100 (void)bsd_signal(SIGINT, intsave);
101 (void)bsd_signal(SIGQUIT, quitsave);
102 return (pid == -1 ? -1 : pstat);
103}
104
105static struct pid {
106 struct pid *next;
107 FILE *fp;
108 pid_t pid;
109} *pidlist;
110
111FILE *__popen(const char *program, const char *type) {
112 struct pid * volatile cur;
113 FILE *iop;
114 int pdes[2];
115 pid_t pid;
116
117 if ((*type != 'r' && *type != 'w') || type[1] != '\0') {
118 errno = EINVAL;
119 return (NULL);
120 }
121
122 if ((cur = malloc(sizeof(struct pid))) == NULL)
123 return (NULL);
124
125 if (pipe(pdes) < 0) {
126 free(cur);
127 return (NULL);
128 }
129
130 switch (pid = vfork()) {
131 case -1: /* Error. */
132 (void)close(pdes[0]);
133 (void)close(pdes[1]);
134 free(cur);
135 return (NULL);
136 /* NOTREACHED */
137 case 0: /* Child. */
138 {
139 struct pid *pcur;
140 /*
141 * because vfork() instead of fork(), must leak FILE *,
142 * but luckily we are terminally headed for an execl()
143 */
144 for (pcur = pidlist; pcur; pcur = pcur->next)
145 close(fileno(pcur->fp));
146
147 if (*type == 'r') {
148 int tpdes1 = pdes[1];
149
150 (void) close(pdes[0]);
151 /*
152 * We must NOT modify pdes, due to the
153 * semantics of vfork.
154 */
155 if (tpdes1 != STDOUT_FILENO) {
156 (void)dup2(tpdes1, STDOUT_FILENO);
157 (void)close(tpdes1);
158 tpdes1 = STDOUT_FILENO;
159 }
160 } else {
161 (void)close(pdes[1]);
162 if (pdes[0] != STDIN_FILENO) {
163 (void)dup2(pdes[0], STDIN_FILENO);
164 (void)close(pdes[0]);
165 }
166 }
167 execl(_PATH_BSHELL, "sh", "-c", program, (char *)NULL);
168 _exit(127);
169 /* NOTREACHED */
170 }
171 }
172
173 /* Parent; assume fdopen can't fail. */
174 if (*type == 'r') {
175 iop = fdopen(pdes[0], type);
176 (void)close(pdes[1]);
177 } else {
178 iop = fdopen(pdes[1], type);
179 (void)close(pdes[0]);
180 }
181
182 /* Link into list of file descriptors. */
183 cur->fp = iop;
184 cur->pid = pid;
185 cur->next = pidlist;
186 pidlist = cur;
187
188 return (iop);
189}
190
191/*
192 * pclose --
193 * Pclose returns -1 if stream is not associated with a `popened' command,
194 * if already `pclosed', or waitpid returns an error.
195 */
196int __pclose(FILE *iop) {
197 struct pid *cur, *last;
198 int pstat;
199 pid_t pid;
200
201 /* Find the appropriate file pointer. */
202 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
203 if (cur->fp == iop)
204 break;
205
206 if (cur == NULL)
207 return (-1);
208
209 (void)fclose(iop);
210
211 do {
212 pid = waitpid(cur->pid, &pstat, 0);
213 } while (pid == -1 && errno == EINTR);
214
215 /* Remove the entry from the linked list. */
216 if (last == NULL)
217 pidlist = cur->next;
218 else
219 last->next = cur->next;
220 free(cur);
221
222 return (pid == -1 ? -1 : pstat);
223}
224
Dees_Troy51a0e822012-09-05 15:24:24 -0400225//partial kangbang from system/vold
226#ifndef CUSTOM_LUN_FILE
227#define CUSTOM_LUN_FILE "/sys/devices/platform/usb_mass_storage/lun%d/file"
228#endif
229
230int usb_storage_enable(void)
231{
232 int fd;
233 char lun_file[255];
234
235 if (DataManager_GetIntValue(TW_HAS_DUAL_STORAGE) == 1 && DataManager_GetIntValue(TW_HAS_DATA_MEDIA) == 0) {
236 Volume *vol = volume_for_path(DataManager_GetSettingsStoragePath());
237 if (!vol)
238 {
239 LOGE("Unable to locate volume information.");
240 return -1;
241 }
242
243 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
244
245 if ((fd = open(lun_file, O_WRONLY)) < 0)
246 {
247 LOGE("Unable to open ums lunfile '%s': (%s)\n", lun_file, strerror(errno));
248 return -1;
249 }
250
251 if ((write(fd, vol->device, strlen(vol->device)) < 0) &&
252 (!vol->device2 || (write(fd, vol->device, strlen(vol->device2)) < 0))) {
253 LOGE("Unable to write to ums lunfile '%s': (%s)\n", lun_file, strerror(errno));
254 close(fd);
255 return -1;
256 }
257 close(fd);
258
259 Volume *vol2 = volume_for_path(DataManager_GetStrValue(TW_EXTERNAL_PATH));
260 if (!vol)
261 {
262 LOGE("Unable to locate volume information.\n");
263 return -1;
264 }
265
266 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
267
268 if ((fd = open(lun_file, O_WRONLY)) < 0)
269 {
270 LOGE("Unable to open ums lunfile '%s': (%s)\n", lun_file, strerror(errno));
271 return -1;
272 }
273
274 if ((write(fd, vol2->device, strlen(vol2->device)) < 0) &&
275 (!vol2->device2 || (write(fd, vol2->device, strlen(vol2->device2)) < 0))) {
276 LOGE("Unable to write to ums lunfile '%s': (%s)\n", lun_file, strerror(errno));
277 close(fd);
278 return -1;
279 }
280 close(fd);
281 } else {
282 if (DataManager_GetIntValue(TW_HAS_DATA_MEDIA) == 0)
283 strcpy(lun_file, DataManager_GetCurrentStoragePath());
284 else
285 strcpy(lun_file, DataManager_GetStrValue(TW_EXTERNAL_PATH));
286
287 Volume *vol = volume_for_path(lun_file);
288 if (!vol)
289 {
290 LOGE("Unable to locate volume information.\n");
291 return -1;
292 }
293
294 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
295
296 if ((fd = open(lun_file, O_WRONLY)) < 0)
297 {
298 LOGE("Unable to open ums lunfile '%s': (%s)\n", lun_file, strerror(errno));
299 return -1;
300 }
301
302 if ((write(fd, vol->device, strlen(vol->device)) < 0) &&
303 (!vol->device2 || (write(fd, vol->device, strlen(vol->device2)) < 0))) {
304 LOGE("Unable to write to ums lunfile '%s': (%s)\n", lun_file, strerror(errno));
305 close(fd);
306 return -1;
307 }
308 close(fd);
309 }
310 return 0;
311}
312
313int usb_storage_disable(void)
314{
315 int fd, index;
316 char lun_file[255];
317
318 for (index=0; index<2; index++) {
319 sprintf(lun_file, CUSTOM_LUN_FILE, index);
320
321 if ((fd = open(lun_file, O_WRONLY)) < 0)
322 {
323 if (index == 0)
324 LOGE("Unable to open ums lunfile '%s': (%s)", lun_file, strerror(errno));
325 return -1;
326 }
327
328 char ch = 0;
329 if (write(fd, &ch, 1) < 0)
330 {
331 if (index == 0)
332 LOGE("Unable to write to ums lunfile '%s': (%s)", lun_file, strerror(errno));
333 close(fd);
334 return -1;
335 }
336
337 close(fd);
338 }
339 return 0;
340}
341
Dees_Troy51a0e822012-09-05 15:24:24 -0400342void update_tz_environment_variables() {
343 setenv("TZ", DataManager_GetStrValue(TW_TIME_ZONE_VAR), 1);
344 tzset();
345}
346
347void run_script(const char *str1, const char *str2, const char *str3, const char *str4, const char *str5, const char *str6, const char *str7, int request_confirm)
348{
349 ui_print("%s", str1);
350 //ui_clear_key_queue();
351 ui_print("\nPress Power to confirm,");
352 ui_print("\nany other key to abort.\n");
353 int confirm;
354 /*if (request_confirm) // this option is used to skip the confirmation when the gui is in use
355 confirm = ui_wait_key();
356 else*/
357 confirm = KEY_POWER;
358
359 if (confirm == BTN_MOUSE || confirm == KEY_POWER || confirm == SELECT_ITEM) {
360 ui_print("%s", str2);
361 pid_t pid = fork();
362 if (pid == 0) {
363 char *args[] = { "/sbin/sh", "-c", (char*)str3, "1>&2", NULL };
364 execv("/sbin/sh", args);
365 fprintf(stderr, str4, strerror(errno));
366 _exit(-1);
367 }
368 int status;
369 while (waitpid(pid, &status, WNOHANG) == 0) {
370 ui_print(".");
371 sleep(1);
372 }
373 ui_print("\n");
374 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
375 ui_print("%s", str5);
376 } else {
377 ui_print("%s", str6);
378 }
379 } else {
380 ui_print("%s", str7);
381 }
382 //if (!ui_text_visible()) return;
383}
384
Dees_Troy51a0e822012-09-05 15:24:24 -0400385void check_and_run_script(const char* script_file, const char* display_name)
386{
387 // Check for and run startup script if script exists
388 struct statfs st;
389 if (statfs(script_file, &st) == 0) {
390 ui_print("Running %s script...\n", display_name);
391 char command[255];
392 strcpy(command, "chmod 755 ");
393 strcat(command, script_file);
394 __system(command);
395 __system(script_file);
396 ui_print("\nFinished running %s script.\n", display_name);
397 }
398}
399
400int check_backup_name(int show_error) {
401 // Check the backup name to ensure that it is the correct size and contains only valid characters
402 // and that a backup with that name doesn't already exist
403 char backup_name[MAX_BACKUP_NAME_LEN];
404 char backup_loc[255], tw_image_dir[255];
405 int copy_size = strlen(DataManager_GetStrValue(TW_BACKUP_NAME));
406 int index, cur_char;
407 struct statfs st;
408
409 // Check size
410 if (copy_size > MAX_BACKUP_NAME_LEN) {
411 if (show_error)
412 LOGE("Backup name is too long.\n");
413 return -2;
414 }
415
416 // Check characters
417 strncpy(backup_name, DataManager_GetStrValue(TW_BACKUP_NAME), copy_size);
418 if (strcmp(backup_name, "0") == 0)
419 return 0; // A "0" (zero) means to use the current timestamp for the backup name
420 for (index=0; index<copy_size; index++) {
421 cur_char = (int)backup_name[index];
422 if ((cur_char >= 48 && cur_char <= 57) || (cur_char >= 65 && cur_char <= 91) || cur_char == 93 || cur_char == 95 || (cur_char >= 97 && cur_char <= 123) || cur_char == 125 || cur_char == 45 || cur_char == 46) {
423 // These are valid characters
424 // Numbers
425 // Upper case letters
426 // Lower case letters
427 // and -_.{}[]
428 } else {
429 if (show_error)
430 LOGE("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
431 return -3;
432 }
433 }
434
435 // Check to make sure that a backup with this name doesn't already exist
436 strcpy(backup_loc, DataManager_GetStrValue(TW_BACKUPS_FOLDER_VAR));
437 sprintf(tw_image_dir,"%s/%s/.", backup_loc, backup_name);
438 if (statfs(tw_image_dir, &st) == 0) {
439 if (show_error)
440 LOGE("A backup with this name already exists.\n");
441 return -4;
442 }
443
444 // No problems found, return 0
445 return 0;
446}
Dees_Troy7d15c252012-09-05 20:47:21 -0400447
448static const char *COMMAND_FILE = "/cache/recovery/command";
449static const char *INTENT_FILE = "/cache/recovery/intent";
450static const char *LOG_FILE = "/cache/recovery/log";
451static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
452static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
453static const char *CACHE_ROOT = "/cache";
454static const char *SDCARD_ROOT = "/sdcard";
455static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
456static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
457
458// close a file, log an error if the error indicator is set
459static void check_and_fclose(FILE *fp, const char *name) {
460 fflush(fp);
461 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
462 fclose(fp);
463}
464
465static void copy_log_file(const char* source, const char* destination, int append) {
466 FILE *log = fopen_path(destination, append ? "a" : "w");
467 if (log == NULL) {
468 LOGE("Can't open %s\n", destination);
469 } else {
470 FILE *tmplog = fopen(source, "r");
471 if (tmplog != NULL) {
472 if (append) {
473 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
474 }
475 char buf[4096];
476 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
477 if (append) {
478 tmplog_offset = ftell(tmplog);
479 }
480 check_and_fclose(tmplog, source);
481 }
482 check_and_fclose(log, destination);
483 }
484}
485
486// clear the recovery command and prepare to boot a (hopefully working) system,
487// copy our log file to cache as well (for the system to read), and
488// record any intent we were asked to communicate back to the system.
489// this function is idempotent: call it as many times as you like.
490void twfinish_recovery(const char *send_intent) {
491 // By this point, we're ready to return to the main system...
492 if (send_intent != NULL) {
493 FILE *fp = fopen_path(INTENT_FILE, "w");
494 if (fp == NULL) {
495 LOGE("Can't open %s\n", INTENT_FILE);
496 } else {
497 fputs(send_intent, fp);
498 check_and_fclose(fp, INTENT_FILE);
499 }
500 }
501
502 // Copy logs to cache so the system can find out what happened.
503 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
504 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
505 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
506 chmod(LOG_FILE, 0600);
507 chown(LOG_FILE, 1000, 1000); // system user
508 chmod(LAST_LOG_FILE, 0640);
509 chmod(LAST_INSTALL_FILE, 0644);
510
511 // Reset to normal system boot so recovery won't cycle indefinitely.
512 struct bootloader_message boot;
513 memset(&boot, 0, sizeof(boot));
514 set_bootloader_message(&boot);
515
516 // Remove the command file, so recovery won't repeat indefinitely.
517 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
518 (unlink(COMMAND_FILE) && errno != ENOENT)) {
519 LOGW("Can't unlink %s\n", COMMAND_FILE);
520 }
521
522 ensure_path_unmounted(CACHE_ROOT);
523 sync(); // For good measure.
524}
525