blob: c2f98febc08281fbfe56c63eaf8a74f57a50040d [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
Ethan Yonker20eb0bc2016-03-22 14:23:28 -05002 Copyright 2012 to 2016 bigbiff/Dees_Troy TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 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"
Ethan Yonker75bf0412014-11-21 13:54:27 -060031#include "mtdutils/mounts.h"
32#include "mtdutils/mtdutils.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040033#include "minzip/SysUtil.h"
34#include "minzip/Zip.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050035#ifdef USE_OLD_VERIFIER
Ethan Yonker4bf259f2016-08-29 11:50:34 -050036#include "verifier24/verifier.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050037#else
Ethan Yonker75bf0412014-11-21 13:54:27 -060038#include "verifier.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050039#endif
Dees_Troy32c8eb82012-09-11 15:28:06 -040040#include "variables.h"
41#include "data.hpp"
42#include "partitions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050043#include "twrpDigest.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040044#include "twrp-functions.hpp"
Ethan Yonker74db1572015-10-28 12:44:49 -050045#include "gui/gui.hpp"
Ethan Yonker20eb0bc2016-03-22 14:23:28 -050046#include "gui/pages.hpp"
Ethan Yonkerf1179622016-08-25 15:32:21 -050047#include "legacy_property_service.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000048extern "C" {
49 #include "gui/gui.h"
that7e303cf2014-03-06 07:57:43 +010050}
51
52static const char* properties_path = "/dev/__properties__";
53static const char* properties_path_renamed = "/dev/__properties_kk__";
Matt Mowercdd3b332014-03-27 14:38:48 -050054static bool legacy_props_env_initd = false;
55static bool legacy_props_path_modified = false;
that7e303cf2014-03-06 07:57:43 +010056
that50640482015-08-30 12:08:05 +020057// to support pre-KitKat update-binaries that expect properties in the legacy format
Matt Mowercdd3b332014-03-27 14:38:48 -050058static int switch_to_legacy_properties()
that7e303cf2014-03-06 07:57:43 +010059{
Matt Mowercdd3b332014-03-27 14:38:48 -050060 if (!legacy_props_env_initd) {
61 if (legacy_properties_init() != 0)
62 return -1;
63
64 char tmp[32];
65 int propfd, propsz;
66 legacy_get_property_workspace(&propfd, &propsz);
67 sprintf(tmp, "%d,%d", dup(propfd), propsz);
68 setenv("ANDROID_PROPERTY_WORKSPACE", tmp, 1);
69 legacy_props_env_initd = true;
70 }
that7e303cf2014-03-06 07:57:43 +010071
72 if (TWFunc::Path_Exists(properties_path)) {
73 // hide real properties so that the updater uses the envvar to find the legacy format properties
Matt Mowercdd3b332014-03-27 14:38:48 -050074 if (rename(properties_path, properties_path_renamed) != 0) {
75 LOGERR("Renaming %s failed: %s\n", properties_path, strerror(errno));
76 return -1;
77 } else {
78 legacy_props_path_modified = true;
79 }
that7e303cf2014-03-06 07:57:43 +010080 }
Matt Mowercdd3b332014-03-27 14:38:48 -050081
82 return 0;
that7e303cf2014-03-06 07:57:43 +010083}
84
Matt Mowercdd3b332014-03-27 14:38:48 -050085static int switch_to_new_properties()
that7e303cf2014-03-06 07:57:43 +010086{
87 if (TWFunc::Path_Exists(properties_path_renamed)) {
Matt Mowercdd3b332014-03-27 14:38:48 -050088 if (rename(properties_path_renamed, properties_path) != 0) {
89 LOGERR("Renaming %s failed: %s\n", properties_path_renamed, strerror(errno));
90 return -1;
91 } else {
92 legacy_props_path_modified = false;
93 }
that7e303cf2014-03-06 07:57:43 +010094 }
Matt Mowercdd3b332014-03-27 14:38:48 -050095
96 return 0;
Dees_Troy2673cec2013-04-02 20:22:16 +000097}
Dees_Troy32c8eb82012-09-11 15:28:06 -040098
Ethan Yonker20eb0bc2016-03-22 14:23:28 -050099static int Install_Theme(const char* path, ZipArchive *Zip) {
100#ifdef TW_OEM_BUILD // We don't do custom themes in OEM builds
101 mzCloseZipArchive(Zip);
102 return INSTALL_CORRUPT;
103#else
104 const ZipEntry* xml_location = mzFindZipEntry(Zip, "ui.xml");
105
106 mzCloseZipArchive(Zip);
107 if (xml_location == NULL) {
108 return INSTALL_CORRUPT;
109 }
110 if (!PartitionManager.Mount_Settings_Storage(true))
111 return INSTALL_ERROR;
112 string theme_path = DataManager::GetSettingsStoragePath();
113 theme_path += "/TWRP/theme";
114 if (!TWFunc::Path_Exists(theme_path)) {
115 if (!TWFunc::Recursive_Mkdir(theme_path)) {
116 return INSTALL_ERROR;
117 }
118 }
119 theme_path += "/ui.zip";
120 if (TWFunc::copy_file(path, theme_path, 0644) != 0) {
121 return INSTALL_ERROR;
122 }
123 LOGINFO("Installing custom theme '%s' to '%s'\n", path, theme_path.c_str());
124 PageManager::RequestReload();
125 return INSTALL_SUCCESS;
126#endif
127}
128
Dees_Troy2673cec2013-04-02 20:22:16 +0000129static int Run_Update_Binary(const char *path, ZipArchive *Zip, int* wipe_cache) {
130 const ZipEntry* binary_location = mzFindZipEntry(Zip, ASSUMED_UPDATE_BINARY_NAME);
that50640482015-08-30 12:08:05 +0200131 string Temp_Binary = "/tmp/updater"; // Note: AOSP names it /tmp/update_binary (yes, with "_")
Dees_Troy2673cec2013-04-02 20:22:16 +0000132 int binary_fd, ret_val, pipe_fd[2], status, zip_verify;
133 char buffer[1024];
134 const char** args = (const char**)malloc(sizeof(char*) * 5);
135 FILE* child_data;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400136
Dees_Troy2673cec2013-04-02 20:22:16 +0000137 if (binary_location == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000138 return INSTALL_CORRUPT;
139 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400140
Dees_Troy2673cec2013-04-02 20:22:16 +0000141 // Delete any existing updater
142 if (TWFunc::Path_Exists(Temp_Binary) && unlink(Temp_Binary.c_str()) != 0) {
that50640482015-08-30 12:08:05 +0200143 LOGINFO("Unable to unlink '%s': %s\n", Temp_Binary.c_str(), strerror(errno));
Dees_Troy2673cec2013-04-02 20:22:16 +0000144 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400145
Dees_Troy2673cec2013-04-02 20:22:16 +0000146 binary_fd = creat(Temp_Binary.c_str(), 0755);
147 if (binary_fd < 0) {
that50640482015-08-30 12:08:05 +0200148 LOGERR("Could not create file for updater extract in '%s': %s\n", Temp_Binary.c_str(), strerror(errno));
Dees_Troy2673cec2013-04-02 20:22:16 +0000149 mzCloseZipArchive(Zip);
Dees_Troy2673cec2013-04-02 20:22:16 +0000150 return INSTALL_ERROR;
151 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400152
Dees_Troy2673cec2013-04-02 20:22:16 +0000153 ret_val = mzExtractZipEntryToFile(Zip, binary_location, binary_fd);
154 close(binary_fd);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400155
Dees_Troy2673cec2013-04-02 20:22:16 +0000156 if (!ret_val) {
Dees_Troy512376c2013-09-03 19:39:41 +0000157 mzCloseZipArchive(Zip);
Dees_Troy2673cec2013-04-02 20:22:16 +0000158 LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME);
159 return INSTALL_ERROR;
160 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400161
Dees_Troy512376c2013-09-03 19:39:41 +0000162 // If exists, extract file_contexts from the zip file
163 const ZipEntry* selinx_contexts = mzFindZipEntry(Zip, "file_contexts");
164 if (selinx_contexts == NULL) {
165 mzCloseZipArchive(Zip);
166 LOGINFO("Zip does not contain SELinux file_contexts file in its root.\n");
167 } else {
168 string output_filename = "/file_contexts";
169 LOGINFO("Zip contains SELinux file_contexts file in its root. Extracting to %s\n", output_filename.c_str());
170 // Delete any file_contexts
171 if (TWFunc::Path_Exists(output_filename) && unlink(output_filename.c_str()) != 0) {
that50640482015-08-30 12:08:05 +0200172 LOGINFO("Unable to unlink '%s': %s\n", output_filename.c_str(), strerror(errno));
Dees_Troy512376c2013-09-03 19:39:41 +0000173 }
174
175 int file_contexts_fd = creat(output_filename.c_str(), 0644);
176 if (file_contexts_fd < 0) {
that50640482015-08-30 12:08:05 +0200177 LOGERR("Could not extract to '%s': %s\n", output_filename.c_str(), strerror(errno));
Dees_Troy512376c2013-09-03 19:39:41 +0000178 mzCloseZipArchive(Zip);
Dees_Troy512376c2013-09-03 19:39:41 +0000179 return INSTALL_ERROR;
180 }
181
182 ret_val = mzExtractZipEntryToFile(Zip, selinx_contexts, file_contexts_fd);
183 close(file_contexts_fd);
184
185 if (!ret_val) {
186 mzCloseZipArchive(Zip);
that50640482015-08-30 12:08:05 +0200187 LOGERR("Could not extract '%s'\n", output_filename.c_str());
Dees_Troy512376c2013-09-03 19:39:41 +0000188 return INSTALL_ERROR;
189 }
190 }
191 mzCloseZipArchive(Zip);
192
Matt Mower6883d732014-03-20 17:28:13 -0500193#ifndef TW_NO_LEGACY_PROPS
Matt Mowercdd3b332014-03-27 14:38:48 -0500194 /* Set legacy properties */
195 if (switch_to_legacy_properties() != 0) {
196 LOGERR("Legacy property environment did not initialize successfully. Properties may not be detected.\n");
197 } else {
198 LOGINFO("Legacy property environment initialized.\n");
199 }
Matt Mower6883d732014-03-20 17:28:13 -0500200#endif
Matt Mowercdd3b332014-03-27 14:38:48 -0500201
Dees_Troy2673cec2013-04-02 20:22:16 +0000202 pipe(pipe_fd);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400203
Dees_Troy2673cec2013-04-02 20:22:16 +0000204 args[0] = Temp_Binary.c_str();
205 args[1] = EXPAND(RECOVERY_API_VERSION);
206 char* temp = (char*)malloc(10);
207 sprintf(temp, "%d", pipe_fd[1]);
208 args[2] = temp;
209 args[3] = (char*)path;
210 args[4] = NULL;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400211
Dees_Troy2673cec2013-04-02 20:22:16 +0000212 pid_t pid = fork();
213 if (pid == 0) {
214 close(pipe_fd[0]);
that7e303cf2014-03-06 07:57:43 +0100215 execve(Temp_Binary.c_str(), (char* const*)args, environ);
that50640482015-08-30 12:08:05 +0200216 printf("E:Can't execute '%s': %s\n", Temp_Binary.c_str(), strerror(errno));
Matt Mower13a8f0b2015-09-26 15:40:03 -0500217 free(temp);
Dees_Troy2673cec2013-04-02 20:22:16 +0000218 _exit(-1);
219 }
220 close(pipe_fd[1]);
Matt Mower13a8f0b2015-09-26 15:40:03 -0500221 free(temp);
222 temp = NULL;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400223
Dees_Troy2673cec2013-04-02 20:22:16 +0000224 *wipe_cache = 0;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400225
Dees_Troy2673cec2013-04-02 20:22:16 +0000226 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
227 child_data = fdopen(pipe_fd[0], "r");
228 while (fgets(buffer, sizeof(buffer), child_data) != NULL) {
229 char* command = strtok(buffer, " \n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200230 if (command == NULL) {
231 continue;
232 } else if (strcmp(command, "progress") == 0) {
233 char* fraction_char = strtok(NULL, " \n");
234 char* seconds_char = strtok(NULL, " \n");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400235
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200236 float fraction_float = strtof(fraction_char, NULL);
237 int seconds_float = strtol(seconds_char, NULL, 10);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400238
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 if (zip_verify)
Dees_Troy2673cec2013-04-02 20:22:16 +0000240 DataManager::ShowProgress(fraction_float * (1 - VERIFICATION_PROGRESS_FRACTION), seconds_float);
241 else
242 DataManager::ShowProgress(fraction_float, seconds_float);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200243 } else if (strcmp(command, "set_progress") == 0) {
244 char* fraction_char = strtok(NULL, " \n");
245 float fraction_float = strtof(fraction_char, NULL);
246 DataManager::SetProgress(fraction_float);
247 } else if (strcmp(command, "ui_print") == 0) {
248 char* display_value = strtok(NULL, "\n");
249 if (display_value) {
250 gui_print("%s", display_value);
251 } else {
252 gui_print("\n");
253 }
254 } else if (strcmp(command, "wipe_cache") == 0) {
255 *wipe_cache = 1;
256 } else if (strcmp(command, "clear_display") == 0) {
257 // Do nothing, not supported by TWRP
Ethan Yonker072c8d82016-08-26 22:22:24 -0500258 } else if (strcmp(command, "log") == 0) {
259 printf("%s\n", strtok(NULL, "\n"));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200260 } else {
261 LOGERR("unknown command [%s]\n", command);
262 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000263 }
264 fclose(child_data);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400265
that50640482015-08-30 12:08:05 +0200266 int waitrc = TWFunc::Wait_For_Child(pid, &status, "Updater");
Matt Mowercdd3b332014-03-27 14:38:48 -0500267
Matt Mower6883d732014-03-20 17:28:13 -0500268#ifndef TW_NO_LEGACY_PROPS
Matt Mowercdd3b332014-03-27 14:38:48 -0500269 /* Unset legacy properties */
270 if (legacy_props_path_modified) {
271 if (switch_to_new_properties() != 0) {
272 LOGERR("Legacy property environment did not disable successfully. Legacy properties may still be in use.\n");
273 } else {
274 LOGINFO("Legacy property environment disabled.\n");
275 }
276 }
Matt Mower6883d732014-03-20 17:28:13 -0500277#endif
Matt Mowercdd3b332014-03-27 14:38:48 -0500278
that50640482015-08-30 12:08:05 +0200279 if (waitrc != 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000280 return INSTALL_ERROR;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400281
Dees_Troy2673cec2013-04-02 20:22:16 +0000282 return INSTALL_SUCCESS;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400283}
284
Dees_Troy32c8eb82012-09-11 15:28:06 -0400285extern "C" int TWinstall_zip(const char* path, int* wipe_cache) {
that50640482015-08-30 12:08:05 +0200286 int ret_val, zip_verify = 1;
Dees_Troy2673cec2013-04-02 20:22:16 +0000287 ZipArchive Zip;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400288
Ethan Yonker24813422014-11-07 17:19:07 -0600289 if (strcmp(path, "error") == 0) {
290 LOGERR("Failed to get adb sideload file: '%s'\n", path);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400291 return INSTALL_CORRUPT;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500292 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400293
Ethan Yonker74db1572015-10-28 12:44:49 -0500294 gui_msg(Msg("installing_zip=Installing zip file '{1}'")(path));
Ethan Yonker24813422014-11-07 17:19:07 -0600295 if (strlen(path) < 9 || strncmp(path, "/sideload", 9) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500296 gui_msg("check_for_md5=Checking for MD5 file...");
that50640482015-08-30 12:08:05 +0200297 twrpDigest md5sum;
298 md5sum.setfn(path);
299 int md5_return = md5sum.verify_md5digest();
Ethan Yonker24813422014-11-07 17:19:07 -0600300 if (md5_return == -2) { // md5 did not match
301 LOGERR("Aborting zip install\n");
302 return INSTALL_CORRUPT;
303 }
304 }
305
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500306#ifndef TW_OEM_BUILD
Dees_Troy32c8eb82012-09-11 15:28:06 -0400307 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500308#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000309 DataManager::SetProgress(0);
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600310
311 MemMapping map;
312 if (sysMapFile(path, &map) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500313 gui_msg(Msg(msg::kError, "fail_sysmap=Failed to map file '{1}'")(path));
that50640482015-08-30 12:08:05 +0200314 return -1;
315 }
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600316
Dees_Troy32c8eb82012-09-11 15:28:06 -0400317 if (zip_verify) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500318 gui_msg("verify_zip_sig=Verifying zip signature...");
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600319 ret_val = verify_file(map.addr, map.length);
Dees_Troy2673cec2013-04-02 20:22:16 +0000320 if (ret_val != VERIFY_SUCCESS) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500321 LOGINFO("Zip signature verification failed: %i\n", ret_val);
322 gui_err("verify_zip_fail=Zip signature verification failed!");
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600323 sysReleaseMap(&map);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400324 return -1;
Ethan Yonker738be7a2014-12-10 11:40:43 -0600325 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500326 gui_msg("verify_zip_done=Zip signature verified successfully.");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400327 }
328 }
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600329 ret_val = mzOpenZipArchive(map.addr, map.length, &Zip);
Dees_Troy2673cec2013-04-02 20:22:16 +0000330 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500331 gui_err("zip_corrupt=Zip file is corrupt!");
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600332 sysReleaseMap(&map);
Dees_Troy2673cec2013-04-02 20:22:16 +0000333 return INSTALL_CORRUPT;
334 }
Ethan Yonker072c8d82016-08-26 22:22:24 -0500335 time_t start, stop;
336 time(&start);
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600337 ret_val = Run_Update_Binary(path, &Zip, wipe_cache);
Ethan Yonker072c8d82016-08-26 22:22:24 -0500338 time(&stop);
339 int total_time = (int) difftime(stop, start);
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500340 if (ret_val == INSTALL_CORRUPT) {
341 // If no updater binary is found, check for ui.xml
342 ret_val = Install_Theme(path, &Zip);
343 if (ret_val == INSTALL_CORRUPT)
344 gui_msg(Msg(msg::kError, "no_updater_binary=Could not find '{1}' in the zip file.")(ASSUMED_UPDATE_BINARY_NAME));
Ethan Yonker072c8d82016-08-26 22:22:24 -0500345 } else {
346 LOGINFO("Install took %i second(s).\n", total_time);
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500347 }
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600348 sysReleaseMap(&map);
349 return ret_val;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400350}