blob: 763098da34d066a86628b385d431c65e397d3f8d [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
Dees_Troy51a0e822012-09-05 15:24:24 -040017#include <errno.h>
18#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25#include <unistd.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040026#include <linux/input.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040027
Dees_Troy51a0e822012-09-05 15:24:24 -040028#include "bootloader.h"
29#include "common.h"
30#include "extra-functions.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040031#include "data.h"
32#include "variables.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040033
34void 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)
35{
36 ui_print("%s", str1);
Dees_Troy8170a922012-09-18 15:40:25 -040037 ui_print("%s", str2);
38 pid_t pid = fork();
39 if (pid == 0) {
40 char *args[] = { "/sbin/sh", "-c", (char*)str3, "1>&2", NULL };
41 execv("/sbin/sh", args);
42 fprintf(stderr, str4, strerror(errno));
43 _exit(-1);
44 }
45 int status;
46 while (waitpid(pid, &status, WNOHANG) == 0) {
47 ui_print(".");
48 sleep(1);
49 }
50 ui_print("\n");
51 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
52 ui_print("%s", str5);
53 } else {
54 ui_print("%s", str6);
55 }
Dees_Troy51a0e822012-09-05 15:24:24 -040056}
57
Dees_Troy51a0e822012-09-05 15:24:24 -040058void check_and_run_script(const char* script_file, const char* display_name)
59{
60 // Check for and run startup script if script exists
Dees_Troy8170a922012-09-18 15:40:25 -040061 struct stat st;
62 if (stat(script_file, &st) == 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -040063 ui_print("Running %s script...\n", display_name);
64 char command[255];
65 strcpy(command, "chmod 755 ");
66 strcat(command, script_file);
Dees_Troy8170a922012-09-18 15:40:25 -040067 system(command);
68 system(script_file);
Dees_Troy51a0e822012-09-05 15:24:24 -040069 ui_print("\nFinished running %s script.\n", display_name);
70 }
71}
72
73int check_backup_name(int show_error) {
74 // Check the backup name to ensure that it is the correct size and contains only valid characters
75 // and that a backup with that name doesn't already exist
76 char backup_name[MAX_BACKUP_NAME_LEN];
77 char backup_loc[255], tw_image_dir[255];
78 int copy_size = strlen(DataManager_GetStrValue(TW_BACKUP_NAME));
79 int index, cur_char;
Dees_Troy8170a922012-09-18 15:40:25 -040080 struct stat st;
Dees_Troy51a0e822012-09-05 15:24:24 -040081
82 // Check size
83 if (copy_size > MAX_BACKUP_NAME_LEN) {
84 if (show_error)
85 LOGE("Backup name is too long.\n");
86 return -2;
87 }
88
89 // Check characters
90 strncpy(backup_name, DataManager_GetStrValue(TW_BACKUP_NAME), copy_size);
91 if (strcmp(backup_name, "0") == 0)
92 return 0; // A "0" (zero) means to use the current timestamp for the backup name
93 for (index=0; index<copy_size; index++) {
94 cur_char = (int)backup_name[index];
Dees_Troyb9d1c6d2012-09-26 10:07:14 -040095 if (cur_char == 32 || (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) {
Dees_Troy51a0e822012-09-05 15:24:24 -040096 // These are valid characters
97 // Numbers
98 // Upper case letters
99 // Lower case letters
Dees_Troyb9d1c6d2012-09-26 10:07:14 -0400100 // Space
Dees_Troy51a0e822012-09-05 15:24:24 -0400101 // and -_.{}[]
102 } else {
103 if (show_error)
104 LOGE("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
105 return -3;
106 }
107 }
108
109 // Check to make sure that a backup with this name doesn't already exist
110 strcpy(backup_loc, DataManager_GetStrValue(TW_BACKUPS_FOLDER_VAR));
111 sprintf(tw_image_dir,"%s/%s/.", backup_loc, backup_name);
Dees_Troy8170a922012-09-18 15:40:25 -0400112 if (stat(tw_image_dir, &st) == 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400113 if (show_error)
114 LOGE("A backup with this name already exists.\n");
115 return -4;
116 }
117
118 // No problems found, return 0
119 return 0;
120}
Dees_Troy7d15c252012-09-05 20:47:21 -0400121
122static const char *COMMAND_FILE = "/cache/recovery/command";
123static const char *INTENT_FILE = "/cache/recovery/intent";
124static const char *LOG_FILE = "/cache/recovery/log";
125static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
126static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
127static const char *CACHE_ROOT = "/cache";
128static const char *SDCARD_ROOT = "/sdcard";
129static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
130static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
131
132// close a file, log an error if the error indicator is set
133static void check_and_fclose(FILE *fp, const char *name) {
134 fflush(fp);
135 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
136 fclose(fp);
137}
138
139static void copy_log_file(const char* source, const char* destination, int append) {
140 FILE *log = fopen_path(destination, append ? "a" : "w");
141 if (log == NULL) {
142 LOGE("Can't open %s\n", destination);
143 } else {
144 FILE *tmplog = fopen(source, "r");
145 if (tmplog != NULL) {
146 if (append) {
147 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
148 }
149 char buf[4096];
150 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
151 if (append) {
152 tmplog_offset = ftell(tmplog);
153 }
154 check_and_fclose(tmplog, source);
155 }
156 check_and_fclose(log, destination);
157 }
158}
159
160// clear the recovery command and prepare to boot a (hopefully working) system,
161// copy our log file to cache as well (for the system to read), and
162// record any intent we were asked to communicate back to the system.
163// this function is idempotent: call it as many times as you like.
164void twfinish_recovery(const char *send_intent) {
165 // By this point, we're ready to return to the main system...
166 if (send_intent != NULL) {
167 FILE *fp = fopen_path(INTENT_FILE, "w");
168 if (fp == NULL) {
169 LOGE("Can't open %s\n", INTENT_FILE);
170 } else {
171 fputs(send_intent, fp);
172 check_and_fclose(fp, INTENT_FILE);
173 }
174 }
175
176 // Copy logs to cache so the system can find out what happened.
177 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
178 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
179 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
180 chmod(LOG_FILE, 0600);
181 chown(LOG_FILE, 1000, 1000); // system user
182 chmod(LAST_LOG_FILE, 0640);
183 chmod(LAST_INSTALL_FILE, 0644);
184
185 // Reset to normal system boot so recovery won't cycle indefinitely.
186 struct bootloader_message boot;
187 memset(&boot, 0, sizeof(boot));
188 set_bootloader_message(&boot);
189
190 // Remove the command file, so recovery won't repeat indefinitely.
Dees_Troy8170a922012-09-18 15:40:25 -0400191 if (system("mount /cache") != 0 ||
Dees_Troy7d15c252012-09-05 20:47:21 -0400192 (unlink(COMMAND_FILE) && errno != ENOENT)) {
193 LOGW("Can't unlink %s\n", COMMAND_FILE);
194 }
195
Dees_Troy8170a922012-09-18 15:40:25 -0400196 system("umount /cache");
Dees_Troy7d15c252012-09-05 20:47:21 -0400197 sync(); // For good measure.
198}