blob: 0162c767bb5c655eb6c3776cb063646f75a1f30d [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 Yonker75bf0412014-11-21 13:54:27 -060035#include "verifier.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040036#include "variables.h"
37#include "data.hpp"
38#include "partitions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050039#include "twrpDigest.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "twrp-functions.hpp"
Ethan Yonker74db1572015-10-28 12:44:49 -050041#include "gui/gui.hpp"
Ethan Yonker20eb0bc2016-03-22 14:23:28 -050042#include "gui/pages.hpp"
Dees_Troy2673cec2013-04-02 20:22:16 +000043extern "C" {
44 #include "gui/gui.h"
that7e303cf2014-03-06 07:57:43 +010045 #include "legacy_property_service.h"
46}
47
48static const char* properties_path = "/dev/__properties__";
49static const char* properties_path_renamed = "/dev/__properties_kk__";
Matt Mowercdd3b332014-03-27 14:38:48 -050050static bool legacy_props_env_initd = false;
51static bool legacy_props_path_modified = false;
that7e303cf2014-03-06 07:57:43 +010052
that50640482015-08-30 12:08:05 +020053// to support pre-KitKat update-binaries that expect properties in the legacy format
Matt Mowercdd3b332014-03-27 14:38:48 -050054static int switch_to_legacy_properties()
that7e303cf2014-03-06 07:57:43 +010055{
Matt Mowercdd3b332014-03-27 14:38:48 -050056 if (!legacy_props_env_initd) {
57 if (legacy_properties_init() != 0)
58 return -1;
59
60 char tmp[32];
61 int propfd, propsz;
62 legacy_get_property_workspace(&propfd, &propsz);
63 sprintf(tmp, "%d,%d", dup(propfd), propsz);
64 setenv("ANDROID_PROPERTY_WORKSPACE", tmp, 1);
65 legacy_props_env_initd = true;
66 }
that7e303cf2014-03-06 07:57:43 +010067
68 if (TWFunc::Path_Exists(properties_path)) {
69 // hide real properties so that the updater uses the envvar to find the legacy format properties
Matt Mowercdd3b332014-03-27 14:38:48 -050070 if (rename(properties_path, properties_path_renamed) != 0) {
71 LOGERR("Renaming %s failed: %s\n", properties_path, strerror(errno));
72 return -1;
73 } else {
74 legacy_props_path_modified = true;
75 }
that7e303cf2014-03-06 07:57:43 +010076 }
Matt Mowercdd3b332014-03-27 14:38:48 -050077
78 return 0;
that7e303cf2014-03-06 07:57:43 +010079}
80
Matt Mowercdd3b332014-03-27 14:38:48 -050081static int switch_to_new_properties()
that7e303cf2014-03-06 07:57:43 +010082{
83 if (TWFunc::Path_Exists(properties_path_renamed)) {
Matt Mowercdd3b332014-03-27 14:38:48 -050084 if (rename(properties_path_renamed, properties_path) != 0) {
85 LOGERR("Renaming %s failed: %s\n", properties_path_renamed, strerror(errno));
86 return -1;
87 } else {
88 legacy_props_path_modified = false;
89 }
that7e303cf2014-03-06 07:57:43 +010090 }
Matt Mowercdd3b332014-03-27 14:38:48 -050091
92 return 0;
Dees_Troy2673cec2013-04-02 20:22:16 +000093}
Dees_Troy32c8eb82012-09-11 15:28:06 -040094
Ethan Yonker20eb0bc2016-03-22 14:23:28 -050095static int Install_Theme(const char* path, ZipArchive *Zip) {
96#ifdef TW_OEM_BUILD // We don't do custom themes in OEM builds
97 mzCloseZipArchive(Zip);
98 return INSTALL_CORRUPT;
99#else
100 const ZipEntry* xml_location = mzFindZipEntry(Zip, "ui.xml");
101
102 mzCloseZipArchive(Zip);
103 if (xml_location == NULL) {
104 return INSTALL_CORRUPT;
105 }
106 if (!PartitionManager.Mount_Settings_Storage(true))
107 return INSTALL_ERROR;
108 string theme_path = DataManager::GetSettingsStoragePath();
109 theme_path += "/TWRP/theme";
110 if (!TWFunc::Path_Exists(theme_path)) {
111 if (!TWFunc::Recursive_Mkdir(theme_path)) {
112 return INSTALL_ERROR;
113 }
114 }
115 theme_path += "/ui.zip";
116 if (TWFunc::copy_file(path, theme_path, 0644) != 0) {
117 return INSTALL_ERROR;
118 }
119 LOGINFO("Installing custom theme '%s' to '%s'\n", path, theme_path.c_str());
120 PageManager::RequestReload();
121 return INSTALL_SUCCESS;
122#endif
123}
124
Dees_Troy2673cec2013-04-02 20:22:16 +0000125static int Run_Update_Binary(const char *path, ZipArchive *Zip, int* wipe_cache) {
126 const ZipEntry* binary_location = mzFindZipEntry(Zip, ASSUMED_UPDATE_BINARY_NAME);
that50640482015-08-30 12:08:05 +0200127 string Temp_Binary = "/tmp/updater"; // Note: AOSP names it /tmp/update_binary (yes, with "_")
Dees_Troy2673cec2013-04-02 20:22:16 +0000128 int binary_fd, ret_val, pipe_fd[2], status, zip_verify;
129 char buffer[1024];
130 const char** args = (const char**)malloc(sizeof(char*) * 5);
131 FILE* child_data;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400132
Dees_Troy2673cec2013-04-02 20:22:16 +0000133 if (binary_location == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000134 return INSTALL_CORRUPT;
135 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400136
Dees_Troy2673cec2013-04-02 20:22:16 +0000137 // Delete any existing updater
138 if (TWFunc::Path_Exists(Temp_Binary) && unlink(Temp_Binary.c_str()) != 0) {
that50640482015-08-30 12:08:05 +0200139 LOGINFO("Unable to unlink '%s': %s\n", Temp_Binary.c_str(), strerror(errno));
Dees_Troy2673cec2013-04-02 20:22:16 +0000140 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400141
Dees_Troy2673cec2013-04-02 20:22:16 +0000142 binary_fd = creat(Temp_Binary.c_str(), 0755);
143 if (binary_fd < 0) {
that50640482015-08-30 12:08:05 +0200144 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 +0000145 mzCloseZipArchive(Zip);
Dees_Troy2673cec2013-04-02 20:22:16 +0000146 return INSTALL_ERROR;
147 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400148
Dees_Troy2673cec2013-04-02 20:22:16 +0000149 ret_val = mzExtractZipEntryToFile(Zip, binary_location, binary_fd);
150 close(binary_fd);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400151
Dees_Troy2673cec2013-04-02 20:22:16 +0000152 if (!ret_val) {
Dees_Troy512376c2013-09-03 19:39:41 +0000153 mzCloseZipArchive(Zip);
Dees_Troy2673cec2013-04-02 20:22:16 +0000154 LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME);
155 return INSTALL_ERROR;
156 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400157
Dees_Troy512376c2013-09-03 19:39:41 +0000158 // If exists, extract file_contexts from the zip file
159 const ZipEntry* selinx_contexts = mzFindZipEntry(Zip, "file_contexts");
160 if (selinx_contexts == NULL) {
161 mzCloseZipArchive(Zip);
162 LOGINFO("Zip does not contain SELinux file_contexts file in its root.\n");
163 } else {
164 string output_filename = "/file_contexts";
165 LOGINFO("Zip contains SELinux file_contexts file in its root. Extracting to %s\n", output_filename.c_str());
166 // Delete any file_contexts
167 if (TWFunc::Path_Exists(output_filename) && unlink(output_filename.c_str()) != 0) {
that50640482015-08-30 12:08:05 +0200168 LOGINFO("Unable to unlink '%s': %s\n", output_filename.c_str(), strerror(errno));
Dees_Troy512376c2013-09-03 19:39:41 +0000169 }
170
171 int file_contexts_fd = creat(output_filename.c_str(), 0644);
172 if (file_contexts_fd < 0) {
that50640482015-08-30 12:08:05 +0200173 LOGERR("Could not extract to '%s': %s\n", output_filename.c_str(), strerror(errno));
Dees_Troy512376c2013-09-03 19:39:41 +0000174 mzCloseZipArchive(Zip);
Dees_Troy512376c2013-09-03 19:39:41 +0000175 return INSTALL_ERROR;
176 }
177
178 ret_val = mzExtractZipEntryToFile(Zip, selinx_contexts, file_contexts_fd);
179 close(file_contexts_fd);
180
181 if (!ret_val) {
182 mzCloseZipArchive(Zip);
that50640482015-08-30 12:08:05 +0200183 LOGERR("Could not extract '%s'\n", output_filename.c_str());
Dees_Troy512376c2013-09-03 19:39:41 +0000184 return INSTALL_ERROR;
185 }
186 }
187 mzCloseZipArchive(Zip);
188
Matt Mower6883d732014-03-20 17:28:13 -0500189#ifndef TW_NO_LEGACY_PROPS
Matt Mowercdd3b332014-03-27 14:38:48 -0500190 /* Set legacy properties */
191 if (switch_to_legacy_properties() != 0) {
192 LOGERR("Legacy property environment did not initialize successfully. Properties may not be detected.\n");
193 } else {
194 LOGINFO("Legacy property environment initialized.\n");
195 }
Matt Mower6883d732014-03-20 17:28:13 -0500196#endif
Matt Mowercdd3b332014-03-27 14:38:48 -0500197
Dees_Troy2673cec2013-04-02 20:22:16 +0000198 pipe(pipe_fd);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400199
Dees_Troy2673cec2013-04-02 20:22:16 +0000200 args[0] = Temp_Binary.c_str();
201 args[1] = EXPAND(RECOVERY_API_VERSION);
202 char* temp = (char*)malloc(10);
203 sprintf(temp, "%d", pipe_fd[1]);
204 args[2] = temp;
205 args[3] = (char*)path;
206 args[4] = NULL;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400207
Dees_Troy2673cec2013-04-02 20:22:16 +0000208 pid_t pid = fork();
209 if (pid == 0) {
210 close(pipe_fd[0]);
that7e303cf2014-03-06 07:57:43 +0100211 execve(Temp_Binary.c_str(), (char* const*)args, environ);
that50640482015-08-30 12:08:05 +0200212 printf("E:Can't execute '%s': %s\n", Temp_Binary.c_str(), strerror(errno));
Matt Mower13a8f0b2015-09-26 15:40:03 -0500213 free(temp);
Dees_Troy2673cec2013-04-02 20:22:16 +0000214 _exit(-1);
215 }
216 close(pipe_fd[1]);
Matt Mower13a8f0b2015-09-26 15:40:03 -0500217 free(temp);
218 temp = NULL;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400219
Dees_Troy2673cec2013-04-02 20:22:16 +0000220 *wipe_cache = 0;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400221
Dees_Troy2673cec2013-04-02 20:22:16 +0000222 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
223 child_data = fdopen(pipe_fd[0], "r");
224 while (fgets(buffer, sizeof(buffer), child_data) != NULL) {
225 char* command = strtok(buffer, " \n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200226 if (command == NULL) {
227 continue;
228 } else if (strcmp(command, "progress") == 0) {
229 char* fraction_char = strtok(NULL, " \n");
230 char* seconds_char = strtok(NULL, " \n");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400231
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200232 float fraction_float = strtof(fraction_char, NULL);
233 int seconds_float = strtol(seconds_char, NULL, 10);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400234
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200235 if (zip_verify)
Dees_Troy2673cec2013-04-02 20:22:16 +0000236 DataManager::ShowProgress(fraction_float * (1 - VERIFICATION_PROGRESS_FRACTION), seconds_float);
237 else
238 DataManager::ShowProgress(fraction_float, seconds_float);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200239 } else if (strcmp(command, "set_progress") == 0) {
240 char* fraction_char = strtok(NULL, " \n");
241 float fraction_float = strtof(fraction_char, NULL);
242 DataManager::SetProgress(fraction_float);
243 } else if (strcmp(command, "ui_print") == 0) {
244 char* display_value = strtok(NULL, "\n");
245 if (display_value) {
246 gui_print("%s", display_value);
247 } else {
248 gui_print("\n");
249 }
250 } else if (strcmp(command, "wipe_cache") == 0) {
251 *wipe_cache = 1;
252 } else if (strcmp(command, "clear_display") == 0) {
253 // Do nothing, not supported by TWRP
254 } else {
255 LOGERR("unknown command [%s]\n", command);
256 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000257 }
258 fclose(child_data);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400259
that50640482015-08-30 12:08:05 +0200260 int waitrc = TWFunc::Wait_For_Child(pid, &status, "Updater");
Matt Mowercdd3b332014-03-27 14:38:48 -0500261
Matt Mower6883d732014-03-20 17:28:13 -0500262#ifndef TW_NO_LEGACY_PROPS
Matt Mowercdd3b332014-03-27 14:38:48 -0500263 /* Unset legacy properties */
264 if (legacy_props_path_modified) {
265 if (switch_to_new_properties() != 0) {
266 LOGERR("Legacy property environment did not disable successfully. Legacy properties may still be in use.\n");
267 } else {
268 LOGINFO("Legacy property environment disabled.\n");
269 }
270 }
Matt Mower6883d732014-03-20 17:28:13 -0500271#endif
Matt Mowercdd3b332014-03-27 14:38:48 -0500272
that50640482015-08-30 12:08:05 +0200273 if (waitrc != 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000274 return INSTALL_ERROR;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400275
Dees_Troy2673cec2013-04-02 20:22:16 +0000276 return INSTALL_SUCCESS;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400277}
278
Dees_Troy32c8eb82012-09-11 15:28:06 -0400279extern "C" int TWinstall_zip(const char* path, int* wipe_cache) {
that50640482015-08-30 12:08:05 +0200280 int ret_val, zip_verify = 1;
Dees_Troy2673cec2013-04-02 20:22:16 +0000281 ZipArchive Zip;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400282
Ethan Yonker24813422014-11-07 17:19:07 -0600283 if (strcmp(path, "error") == 0) {
284 LOGERR("Failed to get adb sideload file: '%s'\n", path);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400285 return INSTALL_CORRUPT;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500286 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400287
Ethan Yonker74db1572015-10-28 12:44:49 -0500288 gui_msg(Msg("installing_zip=Installing zip file '{1}'")(path));
Ethan Yonker24813422014-11-07 17:19:07 -0600289 if (strlen(path) < 9 || strncmp(path, "/sideload", 9) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500290 gui_msg("check_for_md5=Checking for MD5 file...");
that50640482015-08-30 12:08:05 +0200291 twrpDigest md5sum;
292 md5sum.setfn(path);
293 int md5_return = md5sum.verify_md5digest();
Ethan Yonker24813422014-11-07 17:19:07 -0600294 if (md5_return == -2) { // md5 did not match
295 LOGERR("Aborting zip install\n");
296 return INSTALL_CORRUPT;
297 }
298 }
299
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500300#ifndef TW_OEM_BUILD
Dees_Troy32c8eb82012-09-11 15:28:06 -0400301 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500302#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000303 DataManager::SetProgress(0);
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600304
305 MemMapping map;
306 if (sysMapFile(path, &map) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500307 gui_msg(Msg(msg::kError, "fail_sysmap=Failed to map file '{1}'")(path));
that50640482015-08-30 12:08:05 +0200308 return -1;
309 }
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600310
Dees_Troy32c8eb82012-09-11 15:28:06 -0400311 if (zip_verify) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500312 gui_msg("verify_zip_sig=Verifying zip signature...");
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600313 ret_val = verify_file(map.addr, map.length);
Dees_Troy2673cec2013-04-02 20:22:16 +0000314 if (ret_val != VERIFY_SUCCESS) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500315 LOGINFO("Zip signature verification failed: %i\n", ret_val);
316 gui_err("verify_zip_fail=Zip signature verification failed!");
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600317 sysReleaseMap(&map);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400318 return -1;
Ethan Yonker738be7a2014-12-10 11:40:43 -0600319 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500320 gui_msg("verify_zip_done=Zip signature verified successfully.");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400321 }
322 }
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600323 ret_val = mzOpenZipArchive(map.addr, map.length, &Zip);
Dees_Troy2673cec2013-04-02 20:22:16 +0000324 if (ret_val != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500325 gui_err("zip_corrupt=Zip file is corrupt!");
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600326 sysReleaseMap(&map);
Dees_Troy2673cec2013-04-02 20:22:16 +0000327 return INSTALL_CORRUPT;
328 }
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600329 ret_val = Run_Update_Binary(path, &Zip, wipe_cache);
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500330 if (ret_val == INSTALL_CORRUPT) {
331 // If no updater binary is found, check for ui.xml
332 ret_val = Install_Theme(path, &Zip);
333 if (ret_val == INSTALL_CORRUPT)
334 gui_msg(Msg(msg::kError, "no_updater_binary=Could not find '{1}' in the zip file.")(ASSUMED_UPDATE_BINARY_NAME));
335 }
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600336 sysReleaseMap(&map);
337 return ret_val;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400338}