blob: 01bd6727821cd4c7b9531865589aed7d0c8fad39 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy32c8eb82012-09-11 15:28:06 -040018
19#include <ctype.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <limits.h>
23#include <sys/stat.h>
24#include <sys/wait.h>
25#include <unistd.h>
26
27#include <string.h>
28#include <stdio.h>
29
Dees_Troy2673cec2013-04-02 20:22:16 +000030#include "twcommon.h"
Dees_Troyb9d88ac2012-09-14 14:34:19 -040031#include "mincrypt/rsa.h"
32#include "mincrypt/sha.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040033#include "minui/minui.h"
Ethan Yonker75bf0412014-11-21 13:54:27 -060034#include "mtdutils/mounts.h"
35#include "mtdutils/mtdutils.h"
36#if (ANDROID_VERSION >= 5)
Dees_Troy32c8eb82012-09-11 15:28:06 -040037#include "minzip/SysUtil.h"
38#include "minzip/Zip.h"
Ethan Yonker75bf0412014-11-21 13:54:27 -060039#include "verifier.h"
Dees Troyb7ae0982013-09-10 20:47:35 +000040#else
Ethan Yonker75bf0412014-11-21 13:54:27 -060041#include "verifierold.h"
Dees Troyb7ae0982013-09-10 20:47:35 +000042#include "minzipold/SysUtil.h"
43#include "minzipold/Zip.h"
44#endif
Dees_Troy32c8eb82012-09-11 15:28:06 -040045#include "variables.h"
46#include "data.hpp"
47#include "partitions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050048#include "twrpDigest.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040049#include "twrp-functions.hpp"
Dees_Troy2673cec2013-04-02 20:22:16 +000050extern "C" {
51 #include "gui/gui.h"
that7e303cf2014-03-06 07:57:43 +010052 #include "legacy_property_service.h"
53}
54
55static const char* properties_path = "/dev/__properties__";
56static const char* properties_path_renamed = "/dev/__properties_kk__";
Matt Mowercdd3b332014-03-27 14:38:48 -050057static bool legacy_props_env_initd = false;
58static bool legacy_props_path_modified = false;
that7e303cf2014-03-06 07:57:43 +010059
Matt Mowercdd3b332014-03-27 14:38:48 -050060static int switch_to_legacy_properties()
that7e303cf2014-03-06 07:57:43 +010061{
Matt Mowercdd3b332014-03-27 14:38:48 -050062 if (!legacy_props_env_initd) {
63 if (legacy_properties_init() != 0)
64 return -1;
65
66 char tmp[32];
67 int propfd, propsz;
68 legacy_get_property_workspace(&propfd, &propsz);
69 sprintf(tmp, "%d,%d", dup(propfd), propsz);
70 setenv("ANDROID_PROPERTY_WORKSPACE", tmp, 1);
71 legacy_props_env_initd = true;
72 }
that7e303cf2014-03-06 07:57:43 +010073
74 if (TWFunc::Path_Exists(properties_path)) {
75 // hide real properties so that the updater uses the envvar to find the legacy format properties
Matt Mowercdd3b332014-03-27 14:38:48 -050076 if (rename(properties_path, properties_path_renamed) != 0) {
77 LOGERR("Renaming %s failed: %s\n", properties_path, strerror(errno));
78 return -1;
79 } else {
80 legacy_props_path_modified = true;
81 }
that7e303cf2014-03-06 07:57:43 +010082 }
Matt Mowercdd3b332014-03-27 14:38:48 -050083
84 return 0;
that7e303cf2014-03-06 07:57:43 +010085}
86
Matt Mowercdd3b332014-03-27 14:38:48 -050087static int switch_to_new_properties()
that7e303cf2014-03-06 07:57:43 +010088{
89 if (TWFunc::Path_Exists(properties_path_renamed)) {
Matt Mowercdd3b332014-03-27 14:38:48 -050090 if (rename(properties_path_renamed, properties_path) != 0) {
91 LOGERR("Renaming %s failed: %s\n", properties_path_renamed, strerror(errno));
92 return -1;
93 } else {
94 legacy_props_path_modified = false;
95 }
that7e303cf2014-03-06 07:57:43 +010096 }
Matt Mowercdd3b332014-03-27 14:38:48 -050097
98 return 0;
Dees_Troy2673cec2013-04-02 20:22:16 +000099}
Dees_Troy32c8eb82012-09-11 15:28:06 -0400100
Dees_Troy2673cec2013-04-02 20:22:16 +0000101static int Run_Update_Binary(const char *path, ZipArchive *Zip, int* wipe_cache) {
102 const ZipEntry* binary_location = mzFindZipEntry(Zip, ASSUMED_UPDATE_BINARY_NAME);
103 string Temp_Binary = "/tmp/updater";
104 int binary_fd, ret_val, pipe_fd[2], status, zip_verify;
105 char buffer[1024];
106 const char** args = (const char**)malloc(sizeof(char*) * 5);
107 FILE* child_data;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400108
Dees_Troy2673cec2013-04-02 20:22:16 +0000109 if (binary_location == NULL) {
110 mzCloseZipArchive(Zip);
111 return INSTALL_CORRUPT;
112 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400113
Dees_Troy2673cec2013-04-02 20:22:16 +0000114 // Delete any existing updater
115 if (TWFunc::Path_Exists(Temp_Binary) && unlink(Temp_Binary.c_str()) != 0) {
116 LOGINFO("Unable to unlink '%s'\n", Temp_Binary.c_str());
117 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400118
Dees_Troy2673cec2013-04-02 20:22:16 +0000119 binary_fd = creat(Temp_Binary.c_str(), 0755);
120 if (binary_fd < 0) {
121 mzCloseZipArchive(Zip);
122 LOGERR("Could not create file for updater extract in '%s'\n", Temp_Binary.c_str());
123 return INSTALL_ERROR;
124 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400125
Dees_Troy2673cec2013-04-02 20:22:16 +0000126 ret_val = mzExtractZipEntryToFile(Zip, binary_location, binary_fd);
127 close(binary_fd);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400128
Dees_Troy2673cec2013-04-02 20:22:16 +0000129 if (!ret_val) {
Dees_Troy512376c2013-09-03 19:39:41 +0000130 mzCloseZipArchive(Zip);
Dees_Troy2673cec2013-04-02 20:22:16 +0000131 LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME);
132 return INSTALL_ERROR;
133 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400134
Dees_Troy512376c2013-09-03 19:39:41 +0000135 // If exists, extract file_contexts from the zip file
136 const ZipEntry* selinx_contexts = mzFindZipEntry(Zip, "file_contexts");
137 if (selinx_contexts == NULL) {
138 mzCloseZipArchive(Zip);
139 LOGINFO("Zip does not contain SELinux file_contexts file in its root.\n");
140 } else {
141 string output_filename = "/file_contexts";
142 LOGINFO("Zip contains SELinux file_contexts file in its root. Extracting to %s\n", output_filename.c_str());
143 // Delete any file_contexts
144 if (TWFunc::Path_Exists(output_filename) && unlink(output_filename.c_str()) != 0) {
145 LOGINFO("Unable to unlink '%s'\n", output_filename.c_str());
146 }
147
148 int file_contexts_fd = creat(output_filename.c_str(), 0644);
149 if (file_contexts_fd < 0) {
150 mzCloseZipArchive(Zip);
151 LOGERR("Could not extract file_contexts to '%s'\n", output_filename.c_str());
152 return INSTALL_ERROR;
153 }
154
155 ret_val = mzExtractZipEntryToFile(Zip, selinx_contexts, file_contexts_fd);
156 close(file_contexts_fd);
157
158 if (!ret_val) {
159 mzCloseZipArchive(Zip);
160 LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME);
161 return INSTALL_ERROR;
162 }
163 }
164 mzCloseZipArchive(Zip);
165
Matt Mower6883d732014-03-20 17:28:13 -0500166#ifndef TW_NO_LEGACY_PROPS
Matt Mowercdd3b332014-03-27 14:38:48 -0500167 /* Set legacy properties */
168 if (switch_to_legacy_properties() != 0) {
169 LOGERR("Legacy property environment did not initialize successfully. Properties may not be detected.\n");
170 } else {
171 LOGINFO("Legacy property environment initialized.\n");
172 }
Matt Mower6883d732014-03-20 17:28:13 -0500173#endif
Matt Mowercdd3b332014-03-27 14:38:48 -0500174
Dees_Troy2673cec2013-04-02 20:22:16 +0000175 pipe(pipe_fd);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400176
Dees_Troy2673cec2013-04-02 20:22:16 +0000177 args[0] = Temp_Binary.c_str();
178 args[1] = EXPAND(RECOVERY_API_VERSION);
179 char* temp = (char*)malloc(10);
180 sprintf(temp, "%d", pipe_fd[1]);
181 args[2] = temp;
182 args[3] = (char*)path;
183 args[4] = NULL;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400184
Dees_Troy2673cec2013-04-02 20:22:16 +0000185 pid_t pid = fork();
186 if (pid == 0) {
187 close(pipe_fd[0]);
that7e303cf2014-03-06 07:57:43 +0100188 execve(Temp_Binary.c_str(), (char* const*)args, environ);
Dees_Troy2673cec2013-04-02 20:22:16 +0000189 printf("E:Can't execute '%s'\n", Temp_Binary.c_str());
190 _exit(-1);
191 }
192 close(pipe_fd[1]);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400193
Dees_Troy2673cec2013-04-02 20:22:16 +0000194 *wipe_cache = 0;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400195
Dees_Troy2673cec2013-04-02 20:22:16 +0000196 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
197 child_data = fdopen(pipe_fd[0], "r");
198 while (fgets(buffer, sizeof(buffer), child_data) != NULL) {
199 char* command = strtok(buffer, " \n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200200 if (command == NULL) {
201 continue;
202 } else if (strcmp(command, "progress") == 0) {
203 char* fraction_char = strtok(NULL, " \n");
204 char* seconds_char = strtok(NULL, " \n");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400205
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200206 float fraction_float = strtof(fraction_char, NULL);
207 int seconds_float = strtol(seconds_char, NULL, 10);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400208
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200209 if (zip_verify)
Dees_Troy2673cec2013-04-02 20:22:16 +0000210 DataManager::ShowProgress(fraction_float * (1 - VERIFICATION_PROGRESS_FRACTION), seconds_float);
211 else
212 DataManager::ShowProgress(fraction_float, seconds_float);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200213 } else if (strcmp(command, "set_progress") == 0) {
214 char* fraction_char = strtok(NULL, " \n");
215 float fraction_float = strtof(fraction_char, NULL);
216 DataManager::SetProgress(fraction_float);
217 } else if (strcmp(command, "ui_print") == 0) {
218 char* display_value = strtok(NULL, "\n");
219 if (display_value) {
220 gui_print("%s", display_value);
221 } else {
222 gui_print("\n");
223 }
224 } else if (strcmp(command, "wipe_cache") == 0) {
225 *wipe_cache = 1;
226 } else if (strcmp(command, "clear_display") == 0) {
227 // Do nothing, not supported by TWRP
228 } else {
229 LOGERR("unknown command [%s]\n", command);
230 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000231 }
232 fclose(child_data);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400233
Dees_Troy2673cec2013-04-02 20:22:16 +0000234 waitpid(pid, &status, 0);
Matt Mowercdd3b332014-03-27 14:38:48 -0500235
Matt Mower6883d732014-03-20 17:28:13 -0500236#ifndef TW_NO_LEGACY_PROPS
Matt Mowercdd3b332014-03-27 14:38:48 -0500237 /* Unset legacy properties */
238 if (legacy_props_path_modified) {
239 if (switch_to_new_properties() != 0) {
240 LOGERR("Legacy property environment did not disable successfully. Legacy properties may still be in use.\n");
241 } else {
242 LOGINFO("Legacy property environment disabled.\n");
243 }
244 }
Matt Mower6883d732014-03-20 17:28:13 -0500245#endif
Matt Mowercdd3b332014-03-27 14:38:48 -0500246
Dees_Troy2673cec2013-04-02 20:22:16 +0000247 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
248 LOGERR("Error executing updater binary in zip '%s'\n", path);
249 return INSTALL_ERROR;
250 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400251
Dees_Troy2673cec2013-04-02 20:22:16 +0000252 return INSTALL_SUCCESS;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400253}
254
Dees_Troy32c8eb82012-09-11 15:28:06 -0400255extern "C" int TWinstall_zip(const char* path, int* wipe_cache) {
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500256 int ret_val, zip_verify = 1, md5_return, key_count;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500257 twrpDigest md5sum;
258 string strpath = path;
Dees_Troy2673cec2013-04-02 20:22:16 +0000259 ZipArchive Zip;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400260
Ethan Yonker24813422014-11-07 17:19:07 -0600261 if (strcmp(path, "error") == 0) {
262 LOGERR("Failed to get adb sideload file: '%s'\n", path);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400263 return INSTALL_CORRUPT;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500264 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400265
Ethan Yonker24813422014-11-07 17:19:07 -0600266 gui_print("Installing '%s'...\n", path);
267 if (strlen(path) < 9 || strncmp(path, "/sideload", 9) != 0) {
268 gui_print("Checking for MD5 file...\n");
269 md5sum.setfn(strpath);
270 md5_return = md5sum.verify_md5digest();
271 if (md5_return == -2) { // md5 did not match
272 LOGERR("Aborting zip install\n");
273 return INSTALL_CORRUPT;
274 }
275 }
276
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500277#ifndef TW_OEM_BUILD
Dees_Troy32c8eb82012-09-11 15:28:06 -0400278 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500279#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000280 DataManager::SetProgress(0);
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600281
Ethan Yonker75bf0412014-11-21 13:54:27 -0600282#if (ANDROID_VERSION >= 5)
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600283 MemMapping map;
284 if (sysMapFile(path, &map) != 0) {
285 LOGERR("Failed to sysMapFile '%s'\n", path);
286 return -1;
287 }
Ethan Yonker75bf0412014-11-21 13:54:27 -0600288#endif
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600289
Dees_Troy32c8eb82012-09-11 15:28:06 -0400290 if (zip_verify) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000291 gui_print("Verifying zip signature...\n");
Ethan Yonker75bf0412014-11-21 13:54:27 -0600292#if (ANDROID_VERSION >= 5)
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600293 ret_val = verify_file(map.addr, map.length);
Ethan Yonker75bf0412014-11-21 13:54:27 -0600294#else
295 ret_val = verify_file(path);
296#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000297 if (ret_val != VERIFY_SUCCESS) {
298 LOGERR("Zip signature verification failed: %i\n", ret_val);
Ethan Yonker75bf0412014-11-21 13:54:27 -0600299#if (ANDROID_VERSION >= 5)
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600300 sysReleaseMap(&map);
Ethan Yonker75bf0412014-11-21 13:54:27 -0600301#endif
Dees_Troy32c8eb82012-09-11 15:28:06 -0400302 return -1;
303 }
304 }
Ethan Yonker75bf0412014-11-21 13:54:27 -0600305#if (ANDROID_VERSION >= 5)
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600306 ret_val = mzOpenZipArchive(map.addr, map.length, &Zip);
Ethan Yonker75bf0412014-11-21 13:54:27 -0600307#else
308 ret_val = mzOpenZipArchive(path, &Zip);
309#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000310 if (ret_val != 0) {
311 LOGERR("Zip file is corrupt!\n", path);
Ethan Yonker75bf0412014-11-21 13:54:27 -0600312#if (ANDROID_VERSION >= 5)
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600313 sysReleaseMap(&map);
Ethan Yonker75bf0412014-11-21 13:54:27 -0600314#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000315 return INSTALL_CORRUPT;
316 }
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600317 ret_val = Run_Update_Binary(path, &Zip, wipe_cache);
Ethan Yonker75bf0412014-11-21 13:54:27 -0600318#if (ANDROID_VERSION >= 5)
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600319 sysReleaseMap(&map);
Ethan Yonker75bf0412014-11-21 13:54:27 -0600320#endif
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600321 return ret_val;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400322}