blob: ae1abc5c4728d30d14bfa01dd5448984020f0840 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002 Copyright 2012 to 2017 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
nkk71b4c35912017-10-11 23:39:10 +030019
20#ifndef _GNU_SOURCE
21#define _GNU_SOURCE
22#endif
23
Dees_Troy32c8eb82012-09-11 15:28:06 -040024#include <ctype.h>
25#include <errno.h>
26#include <fcntl.h>
27#include <limits.h>
nkk71b4c35912017-10-11 23:39:10 +030028#include <sys/mman.h>
Dees_Troy32c8eb82012-09-11 15:28:06 -040029#include <sys/stat.h>
30#include <sys/wait.h>
Chaosmaster7bf8df52020-02-03 15:54:32 +010031#include <sys/mount.h>
Dees_Troy32c8eb82012-09-11 15:28:06 -040032#include <unistd.h>
33
34#include <string.h>
35#include <stdio.h>
Ethan Yonker193befe2019-04-02 16:01:31 -050036#include <cutils/properties.h>
Dees_Troy32c8eb82012-09-11 15:28:06 -040037
Dees_Troy2673cec2013-04-02 20:22:16 +000038#include "twcommon.h"
Ethan Yonker75bf0412014-11-21 13:54:27 -060039#include "mtdutils/mounts.h"
40#include "mtdutils/mtdutils.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050041
42#ifdef USE_MINZIP
Dees_Troy32c8eb82012-09-11 15:28:06 -040043#include "minzip/SysUtil.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050044#else
45#include "otautil/SysUtil.h"
46#include <ziparchive/zip_archive.h>
47#endif
48#include "zipwrap.hpp"
Ethan Yonkerf1179622016-08-25 15:32:21 -050049#ifdef USE_OLD_VERIFIER
Ethan Yonker4bf259f2016-08-29 11:50:34 -050050#include "verifier24/verifier.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050051#else
Ethan Yonker75bf0412014-11-21 13:54:27 -060052#include "verifier.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050053#endif
Dees_Troy32c8eb82012-09-11 15:28:06 -040054#include "variables.h"
55#include "data.hpp"
56#include "partitions.hpp"
bigbiff bigbiff56cf5642016-08-19 17:43:45 -040057#include "twrpDigestDriver.hpp"
58#include "twrpDigest/twrpDigest.hpp"
59#include "twrpDigest/twrpMD5.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040060#include "twrp-functions.hpp"
Ethan Yonker74db1572015-10-28 12:44:49 -050061#include "gui/gui.hpp"
Ethan Yonker20eb0bc2016-03-22 14:23:28 -050062#include "gui/pages.hpp"
Ethan Yonkerf1179622016-08-25 15:32:21 -050063#include "legacy_property_service.h"
Ethan Yonker941a8992016-12-05 09:04:30 -060064#include "twinstall.h"
65#include "installcommand.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000066extern "C" {
67 #include "gui/gui.h"
that7e303cf2014-03-06 07:57:43 +010068}
69
Ethan Yonker941a8992016-12-05 09:04:30 -060070#define AB_OTA "payload_properties.txt"
71
lambdadroidc4faea82018-09-26 22:56:51 +020072#ifndef TW_NO_LEGACY_PROPS
that7e303cf2014-03-06 07:57:43 +010073static const char* properties_path = "/dev/__properties__";
74static const char* properties_path_renamed = "/dev/__properties_kk__";
Matt Mowercdd3b332014-03-27 14:38:48 -050075static bool legacy_props_env_initd = false;
76static bool legacy_props_path_modified = false;
lambdadroidc4faea82018-09-26 22:56:51 +020077#endif
that7e303cf2014-03-06 07:57:43 +010078
Ethan Yonker941a8992016-12-05 09:04:30 -060079enum zip_type {
80 UNKNOWN_ZIP_TYPE = 0,
81 UPDATE_BINARY_ZIP_TYPE,
82 AB_OTA_ZIP_TYPE,
83 TWRP_THEME_ZIP_TYPE
84};
85
lambdadroidc4faea82018-09-26 22:56:51 +020086#ifndef TW_NO_LEGACY_PROPS
that50640482015-08-30 12:08:05 +020087// to support pre-KitKat update-binaries that expect properties in the legacy format
Matt Mowercdd3b332014-03-27 14:38:48 -050088static int switch_to_legacy_properties()
that7e303cf2014-03-06 07:57:43 +010089{
Matt Mowercdd3b332014-03-27 14:38:48 -050090 if (!legacy_props_env_initd) {
91 if (legacy_properties_init() != 0)
92 return -1;
93
94 char tmp[32];
95 int propfd, propsz;
96 legacy_get_property_workspace(&propfd, &propsz);
97 sprintf(tmp, "%d,%d", dup(propfd), propsz);
98 setenv("ANDROID_PROPERTY_WORKSPACE", tmp, 1);
99 legacy_props_env_initd = true;
100 }
that7e303cf2014-03-06 07:57:43 +0100101
102 if (TWFunc::Path_Exists(properties_path)) {
103 // hide real properties so that the updater uses the envvar to find the legacy format properties
Matt Mowercdd3b332014-03-27 14:38:48 -0500104 if (rename(properties_path, properties_path_renamed) != 0) {
105 LOGERR("Renaming %s failed: %s\n", properties_path, strerror(errno));
106 return -1;
107 } else {
108 legacy_props_path_modified = true;
109 }
that7e303cf2014-03-06 07:57:43 +0100110 }
Matt Mowercdd3b332014-03-27 14:38:48 -0500111
112 return 0;
that7e303cf2014-03-06 07:57:43 +0100113}
114
Matt Mowercdd3b332014-03-27 14:38:48 -0500115static int switch_to_new_properties()
that7e303cf2014-03-06 07:57:43 +0100116{
117 if (TWFunc::Path_Exists(properties_path_renamed)) {
Matt Mowercdd3b332014-03-27 14:38:48 -0500118 if (rename(properties_path_renamed, properties_path) != 0) {
119 LOGERR("Renaming %s failed: %s\n", properties_path_renamed, strerror(errno));
120 return -1;
121 } else {
122 legacy_props_path_modified = false;
123 }
that7e303cf2014-03-06 07:57:43 +0100124 }
Matt Mowercdd3b332014-03-27 14:38:48 -0500125
126 return 0;
Dees_Troy2673cec2013-04-02 20:22:16 +0000127}
lambdadroidc4faea82018-09-26 22:56:51 +0200128#endif
Dees_Troy32c8eb82012-09-11 15:28:06 -0400129
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500130static int Install_Theme(const char* path, ZipWrap *Zip) {
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500131#ifdef TW_OEM_BUILD // We don't do custom themes in OEM builds
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500132 Zip->Close();
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500133 return INSTALL_CORRUPT;
134#else
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500135 if (!Zip->EntryExists("ui.xml")) {
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500136 return INSTALL_CORRUPT;
137 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500138 Zip->Close();
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500139 if (!PartitionManager.Mount_Settings_Storage(true))
140 return INSTALL_ERROR;
141 string theme_path = DataManager::GetSettingsStoragePath();
142 theme_path += "/TWRP/theme";
143 if (!TWFunc::Path_Exists(theme_path)) {
144 if (!TWFunc::Recursive_Mkdir(theme_path)) {
145 return INSTALL_ERROR;
146 }
147 }
148 theme_path += "/ui.zip";
149 if (TWFunc::copy_file(path, theme_path, 0644) != 0) {
150 return INSTALL_ERROR;
151 }
152 LOGINFO("Installing custom theme '%s' to '%s'\n", path, theme_path.c_str());
153 PageManager::RequestReload();
154 return INSTALL_SUCCESS;
155#endif
156}
157
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500158static int Prepare_Update_Binary(const char *path, ZipWrap *Zip, int* wipe_cache) {
Ethan Yonker193befe2019-04-02 16:01:31 -0500159 char arches[PATH_MAX];
160 std::string binary_name = ASSUMED_UPDATE_BINARY_NAME;
161 property_get("ro.product.cpu.abilist", arches, "error");
162 if (strcmp(arches, "error") == 0)
163 property_get("ro.product.cpu.abi", arches, "error");
164 vector<string> split = TWFunc::split_string(arches, ',', true);
165 std::vector<string>::iterator arch;
166 std::string base_name = binary_name;
167 base_name += "-";
168 for (arch = split.begin(); arch != split.end(); arch++) {
169 std::string temp = base_name + *arch;
170 if (Zip->EntryExists(temp)) {
171 binary_name = temp;
172 break;
173 }
174 }
175 LOGINFO("Extracting updater binary '%s'\n", binary_name.c_str());
176 if (!Zip->ExtractEntry(binary_name.c_str(), TMP_UPDATER_BINARY_PATH, 0755)) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500177 Zip->Close();
Dees_Troy2673cec2013-04-02 20:22:16 +0000178 LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME);
179 return INSTALL_ERROR;
180 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400181
Dees_Troy512376c2013-09-03 19:39:41 +0000182 // If exists, extract file_contexts from the zip file
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500183 if (!Zip->EntryExists("file_contexts")) {
184 Zip->Close();
Dees_Troy512376c2013-09-03 19:39:41 +0000185 LOGINFO("Zip does not contain SELinux file_contexts file in its root.\n");
186 } else {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500187 const string output_filename = "/file_contexts";
Dees_Troy512376c2013-09-03 19:39:41 +0000188 LOGINFO("Zip contains SELinux file_contexts file in its root. Extracting to %s\n", output_filename.c_str());
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500189 if (!Zip->ExtractEntry("file_contexts", output_filename, 0644)) {
190 Zip->Close();
that50640482015-08-30 12:08:05 +0200191 LOGERR("Could not extract '%s'\n", output_filename.c_str());
Dees_Troy512376c2013-09-03 19:39:41 +0000192 return INSTALL_ERROR;
193 }
194 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500195 Zip->Close();
Ethan Yonker941a8992016-12-05 09:04:30 -0600196 return INSTALL_SUCCESS;
197}
198
lambdadroidc4faea82018-09-26 22:56:51 +0200199#ifndef TW_NO_LEGACY_PROPS
nkk71b4c35912017-10-11 23:39:10 +0300200static bool update_binary_has_legacy_properties(const char *binary) {
201 const char str_to_match[] = "ANDROID_PROPERTY_WORKSPACE";
202 int len_to_match = sizeof(str_to_match) - 1;
203 bool found = false;
204
205 int fd = open(binary, O_RDONLY);
206 if (fd < 0) {
207 LOGINFO("has_legacy_properties: Could not open %s: %s!\n", binary, strerror(errno));
208 return false;
209 }
210
211 struct stat finfo;
212 if (fstat(fd, &finfo) < 0) {
213 LOGINFO("has_legacy_properties: Could not fstat %d: %s!\n", fd, strerror(errno));
214 close(fd);
215 return false;
216 }
217
218 void *data = mmap(NULL, finfo.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
219 if (data == MAP_FAILED) {
Simon Shi8ae2b0d2019-03-09 13:01:55 +0800220 LOGINFO("has_legacy_properties: mmap (size=%zu) failed: %s!\n", (size_t)finfo.st_size, strerror(errno));
nkk71b4c35912017-10-11 23:39:10 +0300221 } else {
222 if (memmem(data, finfo.st_size, str_to_match, len_to_match)) {
223 LOGINFO("has_legacy_properties: Found legacy property match!\n");
224 found = true;
225 }
226 munmap(data, finfo.st_size);
227 }
228 close(fd);
229
230 return found;
231}
lambdadroidc4faea82018-09-26 22:56:51 +0200232#endif
nkk71b4c35912017-10-11 23:39:10 +0300233
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500234static int Run_Update_Binary(const char *path, ZipWrap *Zip, int* wipe_cache, zip_type ztype) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600235 int ret_val, pipe_fd[2], status, zip_verify;
236 char buffer[1024];
237 FILE* child_data;
Dees_Troy512376c2013-09-03 19:39:41 +0000238
Matt Mower6883d732014-03-20 17:28:13 -0500239#ifndef TW_NO_LEGACY_PROPS
nkk71b4c35912017-10-11 23:39:10 +0300240 if (!update_binary_has_legacy_properties(TMP_UPDATER_BINARY_PATH)) {
241 LOGINFO("Legacy property environment not used in updater.\n");
242 } else if (switch_to_legacy_properties() != 0) { /* Set legacy properties */
243 LOGERR("Legacy property environment did not initialize successfully. Properties may not be detected.\n");
244 } else {
245 LOGINFO("Legacy property environment initialized.\n");
Matt Mowercdd3b332014-03-27 14:38:48 -0500246 }
Matt Mower6883d732014-03-20 17:28:13 -0500247#endif
Matt Mowercdd3b332014-03-27 14:38:48 -0500248
Dees_Troy2673cec2013-04-02 20:22:16 +0000249 pipe(pipe_fd);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400250
Ethan Yonker941a8992016-12-05 09:04:30 -0600251 std::vector<std::string> args;
252 if (ztype == UPDATE_BINARY_ZIP_TYPE) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500253 ret_val = update_binary_command(path, 0, pipe_fd[1], &args);
Ethan Yonker941a8992016-12-05 09:04:30 -0600254 } else if (ztype == AB_OTA_ZIP_TYPE) {
255 ret_val = abupdate_binary_command(path, Zip, 0, pipe_fd[1], &args);
256 } else {
257 LOGERR("Unknown zip type %i\n", ztype);
258 ret_val = INSTALL_CORRUPT;
259 }
260 if (ret_val) {
261 close(pipe_fd[0]);
262 close(pipe_fd[1]);
263 return ret_val;
264 }
265
266 // Convert the vector to a NULL-terminated char* array suitable for execv.
267 const char* chr_args[args.size() + 1];
268 chr_args[args.size()] = NULL;
269 for (size_t i = 0; i < args.size(); i++)
270 chr_args[i] = args[i].c_str();
Dees_Troy32c8eb82012-09-11 15:28:06 -0400271
Dees_Troy2673cec2013-04-02 20:22:16 +0000272 pid_t pid = fork();
273 if (pid == 0) {
274 close(pipe_fd[0]);
Ethan Yonker941a8992016-12-05 09:04:30 -0600275 execve(chr_args[0], const_cast<char**>(chr_args), environ);
276 printf("E:Can't execute '%s': %s\n", chr_args[0], strerror(errno));
Dees_Troy2673cec2013-04-02 20:22:16 +0000277 _exit(-1);
278 }
279 close(pipe_fd[1]);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400280
Dees_Troy2673cec2013-04-02 20:22:16 +0000281 *wipe_cache = 0;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400282
Dees_Troy2673cec2013-04-02 20:22:16 +0000283 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
284 child_data = fdopen(pipe_fd[0], "r");
285 while (fgets(buffer, sizeof(buffer), child_data) != NULL) {
286 char* command = strtok(buffer, " \n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200287 if (command == NULL) {
288 continue;
289 } else if (strcmp(command, "progress") == 0) {
290 char* fraction_char = strtok(NULL, " \n");
291 char* seconds_char = strtok(NULL, " \n");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400292
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200293 float fraction_float = strtof(fraction_char, NULL);
294 int seconds_float = strtol(seconds_char, NULL, 10);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400295
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200296 if (zip_verify)
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500297 DataManager::ShowProgress(fraction_float * (1 - VERIFICATION_PROGRESS_FRAC), seconds_float);
Dees_Troy2673cec2013-04-02 20:22:16 +0000298 else
299 DataManager::ShowProgress(fraction_float, seconds_float);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200300 } else if (strcmp(command, "set_progress") == 0) {
301 char* fraction_char = strtok(NULL, " \n");
302 float fraction_float = strtof(fraction_char, NULL);
Chaosmasterab164372020-02-03 15:38:02 +0100303 DataManager::_SetProgress(fraction_float);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200304 } else if (strcmp(command, "ui_print") == 0) {
305 char* display_value = strtok(NULL, "\n");
306 if (display_value) {
307 gui_print("%s", display_value);
308 } else {
309 gui_print("\n");
310 }
311 } else if (strcmp(command, "wipe_cache") == 0) {
312 *wipe_cache = 1;
313 } else if (strcmp(command, "clear_display") == 0) {
314 // Do nothing, not supported by TWRP
Ethan Yonker072c8d82016-08-26 22:22:24 -0500315 } else if (strcmp(command, "log") == 0) {
316 printf("%s\n", strtok(NULL, "\n"));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200317 } else {
318 LOGERR("unknown command [%s]\n", command);
319 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000320 }
321 fclose(child_data);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400322
that50640482015-08-30 12:08:05 +0200323 int waitrc = TWFunc::Wait_For_Child(pid, &status, "Updater");
Matt Mowercdd3b332014-03-27 14:38:48 -0500324
Matt Mower6883d732014-03-20 17:28:13 -0500325#ifndef TW_NO_LEGACY_PROPS
nkk71b4c35912017-10-11 23:39:10 +0300326 /* Unset legacy properties */
327 if (legacy_props_path_modified) {
328 if (switch_to_new_properties() != 0) {
329 LOGERR("Legacy property environment did not disable successfully. Legacy properties may still be in use.\n");
330 } else {
331 LOGINFO("Legacy property environment disabled.\n");
Matt Mowercdd3b332014-03-27 14:38:48 -0500332 }
333 }
Matt Mower6883d732014-03-20 17:28:13 -0500334#endif
Matt Mowercdd3b332014-03-27 14:38:48 -0500335
that50640482015-08-30 12:08:05 +0200336 if (waitrc != 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000337 return INSTALL_ERROR;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400338
Dees_Troy2673cec2013-04-02 20:22:16 +0000339 return INSTALL_SUCCESS;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400340}
341
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500342int TWinstall_zip(const char* path, int* wipe_cache) {
Chaosmasterff4f9582020-01-26 15:38:11 +0100343 int ret_val, zip_verify = 1, unmount_system = 1;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400344
Ethan Yonker24813422014-11-07 17:19:07 -0600345 if (strcmp(path, "error") == 0) {
346 LOGERR("Failed to get adb sideload file: '%s'\n", path);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400347 return INSTALL_CORRUPT;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500348 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400349
Ethan Yonker74db1572015-10-28 12:44:49 -0500350 gui_msg(Msg("installing_zip=Installing zip file '{1}'")(path));
Ethan Yonker24813422014-11-07 17:19:07 -0600351 if (strlen(path) < 9 || strncmp(path, "/sideload", 9) != 0) {
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400352 string digest_str;
353 string Full_Filename = path;
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400354
355 gui_msg("check_for_digest=Checking for Digest file...");
bigbiff bigbiff718ab392019-03-28 19:46:56 -0400356
Ethan Yonker1da568f2019-04-09 15:29:51 -0500357 if (*path != '@' && !twrpDigestDriver::Check_File_Digest(Full_Filename)) {
358 LOGERR("Aborting zip install: Digest verification failed\n");
359 return INSTALL_CORRUPT;
Ethan Yonker24813422014-11-07 17:19:07 -0600360 }
361 }
362
Chaosmasterff4f9582020-01-26 15:38:11 +0100363 DataManager::GetValue(TW_UNMOUNT_SYSTEM, unmount_system);
364
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500365#ifndef TW_OEM_BUILD
Dees_Troy32c8eb82012-09-11 15:28:06 -0400366 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500367#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000368 DataManager::SetProgress(0);
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600369
370 MemMapping map;
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600371#ifdef USE_MINZIP
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600372 if (sysMapFile(path, &map) != 0) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600373#else
374 if (!map.MapFile(path)) {
375#endif
Ethan Yonker74db1572015-10-28 12:44:49 -0500376 gui_msg(Msg(msg::kError, "fail_sysmap=Failed to map file '{1}'")(path));
that50640482015-08-30 12:08:05 +0200377 return -1;
378 }
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600379
Dees_Troy32c8eb82012-09-11 15:28:06 -0400380 if (zip_verify) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500381 gui_msg("verify_zip_sig=Verifying zip signature...");
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500382#ifdef USE_OLD_VERIFIER
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600383 ret_val = verify_file(map.addr, map.length);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500384#else
385 std::vector<Certificate> loadedKeys;
386 if (!load_keys("/res/keys", loadedKeys)) {
387 LOGINFO("Failed to load keys");
388 gui_err("verify_zip_fail=Zip signature verification failed!");
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600389#ifdef USE_MINZIP
390 sysReleaseMap(&map);
391#endif
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500392 return -1;
393 }
394 ret_val = verify_file(map.addr, map.length, loadedKeys, std::bind(&DataManager::SetProgress, std::placeholders::_1));
395#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000396 if (ret_val != VERIFY_SUCCESS) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500397 LOGINFO("Zip signature verification failed: %i\n", ret_val);
398 gui_err("verify_zip_fail=Zip signature verification failed!");
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600399#ifdef USE_MINZIP
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600400 sysReleaseMap(&map);
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600401#endif
Dees_Troy32c8eb82012-09-11 15:28:06 -0400402 return -1;
Ethan Yonker738be7a2014-12-10 11:40:43 -0600403 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500404 gui_msg("verify_zip_done=Zip signature verified successfully.");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400405 }
406 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500407 ZipWrap Zip;
408 if (!Zip.Open(path, &map)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500409 gui_err("zip_corrupt=Zip file is corrupt!");
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600410#ifdef USE_MINZIP
411 sysReleaseMap(&map);
412#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000413 return INSTALL_CORRUPT;
414 }
Ethan Yonker941a8992016-12-05 09:04:30 -0600415
Chaosmasterff4f9582020-01-26 15:38:11 +0100416 if (unmount_system) {
417 gui_msg("unmount_system=Unmounting System...");
418 if(!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
419 gui_err("unmount_system_err=Failed unmounting System");
420 return -1;
421 }
422 }
423
Ethan Yonker072c8d82016-08-26 22:22:24 -0500424 time_t start, stop;
425 time(&start);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500426 if (Zip.EntryExists(ASSUMED_UPDATE_BINARY_NAME)) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600427 LOGINFO("Update binary zip\n");
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500428 // Additionally verify the compatibility of the package.
429 if (!verify_package_compatibility(&Zip)) {
430 gui_err("zip_compatible_err=Zip Treble compatibility error!");
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500431 Zip.Close();
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600432#ifdef USE_MINZIP
433 sysReleaseMap(&map);
434#endif
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500435 ret_val = INSTALL_CORRUPT;
436 } else {
437 ret_val = Prepare_Update_Binary(path, &Zip, wipe_cache);
438 if (ret_val == INSTALL_SUCCESS)
439 ret_val = Run_Update_Binary(path, &Zip, wipe_cache, UPDATE_BINARY_ZIP_TYPE);
440 }
Ethan Yonker941a8992016-12-05 09:04:30 -0600441 } else {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500442 if (Zip.EntryExists(AB_OTA)) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600443 LOGINFO("AB zip\n");
Chaosmaster5343a3c2020-02-07 19:58:10 +0100444 gui_msg(Msg(msg::kHighlight, "flash_ab_inactive=Flashing A/B zip to inactive slot: {1}")(PartitionManager.Get_Active_Slot_Display()=="A"?"B":"A"));
Chaosmaster7bf8df52020-02-03 15:54:32 +0100445 // We need this so backuptool can do its magic
446 bool system_mount_state = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
447 bool vendor_mount_state = PartitionManager.Is_Mounted_By_Path("/vendor");
448 PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
449 PartitionManager.Mount_By_Path("/vendor", true);
450 TWFunc::Exec_Cmd("cp -f /sbin/sh /tmp/sh");
451 mount("/tmp/sh", "/system/bin/sh", "auto", MS_BIND, NULL);
Ethan Yonker941a8992016-12-05 09:04:30 -0600452 ret_val = Run_Update_Binary(path, &Zip, wipe_cache, AB_OTA_ZIP_TYPE);
Chaosmaster7bf8df52020-02-03 15:54:32 +0100453 umount("/system/bin/sh");
454 unlink("/tmp/sh");
455 if (!vendor_mount_state)
456 PartitionManager.UnMount_By_Path("/vendor", true);
457 if (!system_mount_state)
458 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Chaosmaster5343a3c2020-02-07 19:58:10 +0100459 gui_warn("flash_ab_reboot=To flash additional zips, please reboot recovery to switch to the updated slot.");
Chaosmaster7bf8df52020-02-03 15:54:32 +0100460
Ethan Yonker941a8992016-12-05 09:04:30 -0600461 } else {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500462 if (Zip.EntryExists("ui.xml")) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600463 LOGINFO("TWRP theme zip\n");
464 ret_val = Install_Theme(path, &Zip);
465 } else {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500466 Zip.Close();
Ethan Yonker941a8992016-12-05 09:04:30 -0600467 ret_val = INSTALL_CORRUPT;
468 }
469 }
470 }
Ethan Yonker072c8d82016-08-26 22:22:24 -0500471 time(&stop);
472 int total_time = (int) difftime(stop, start);
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500473 if (ret_val == INSTALL_CORRUPT) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600474 gui_err("invalid_zip_format=Invalid zip file format!");
Ethan Yonker072c8d82016-08-26 22:22:24 -0500475 } else {
476 LOGINFO("Install took %i second(s).\n", total_time);
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500477 }
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600478#ifdef USE_MINZIP
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600479 sysReleaseMap(&map);
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600480#endif
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600481 return ret_val;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400482}