blob: b00f2deaeceba3afdf290729bd72ffb30c2a0188 [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>
26
Dees_Troy51a0e822012-09-05 15:24:24 -040027#include <linux/input.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040028
Dees_Troy51a0e822012-09-05 15:24:24 -040029#include "bootloader.h"
30#include "common.h"
31#include "extra-functions.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040032#include "data.h"
33#include "variables.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040034
35void 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)
36{
37 ui_print("%s", str1);
Dees_Troy8170a922012-09-18 15:40:25 -040038 ui_print("%s", str2);
39 pid_t pid = fork();
40 if (pid == 0) {
41 char *args[] = { "/sbin/sh", "-c", (char*)str3, "1>&2", NULL };
42 execv("/sbin/sh", args);
43 fprintf(stderr, str4, strerror(errno));
44 _exit(-1);
45 }
46 int status;
47 while (waitpid(pid, &status, WNOHANG) == 0) {
48 ui_print(".");
49 sleep(1);
50 }
51 ui_print("\n");
52 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
53 ui_print("%s", str5);
54 } else {
55 ui_print("%s", str6);
56 }
Dees_Troy51a0e822012-09-05 15:24:24 -040057}
58
Dees_Troy51a0e822012-09-05 15:24:24 -040059void check_and_run_script(const char* script_file, const char* display_name)
60{
61 // Check for and run startup script if script exists
Dees_Troy8170a922012-09-18 15:40:25 -040062 struct stat st;
63 if (stat(script_file, &st) == 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -040064 ui_print("Running %s script...\n", display_name);
65 char command[255];
66 strcpy(command, "chmod 755 ");
67 strcat(command, script_file);
Dees_Troy8170a922012-09-18 15:40:25 -040068 system(command);
69 system(script_file);
Dees_Troy51a0e822012-09-05 15:24:24 -040070 ui_print("\nFinished running %s script.\n", display_name);
71 }
72}
73
74int check_backup_name(int show_error) {
75 // Check the backup name to ensure that it is the correct size and contains only valid characters
76 // and that a backup with that name doesn't already exist
77 char backup_name[MAX_BACKUP_NAME_LEN];
78 char backup_loc[255], tw_image_dir[255];
79 int copy_size = strlen(DataManager_GetStrValue(TW_BACKUP_NAME));
80 int index, cur_char;
Dees_Troy8170a922012-09-18 15:40:25 -040081 struct stat st;
Dees_Troy51a0e822012-09-05 15:24:24 -040082
83 // Check size
84 if (copy_size > MAX_BACKUP_NAME_LEN) {
85 if (show_error)
86 LOGE("Backup name is too long.\n");
87 return -2;
88 }
89
90 // Check characters
91 strncpy(backup_name, DataManager_GetStrValue(TW_BACKUP_NAME), copy_size);
92 if (strcmp(backup_name, "0") == 0)
93 return 0; // A "0" (zero) means to use the current timestamp for the backup name
94 for (index=0; index<copy_size; index++) {
95 cur_char = (int)backup_name[index];
96 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) {
97 // These are valid characters
98 // Numbers
99 // Upper case letters
100 // Lower case letters
101 // 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}