blob: fd338288a0dd211a8b1eeb856c766be0cc46686d [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>
Chaosmaster6c1477d2020-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
bigbiffd58ba182020-03-23 10:02:29 -040045#include "otautil/sysutil.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050046#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"
bigbiffd58ba182020-03-23 10:02:29 -040051#elif USE_28_VERIFIER
52#include "verifier28/verifier.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050053#else
bigbiffd58ba182020-03-23 10:02:29 -040054#include "install/verifier.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050055#endif
Dees_Troy32c8eb82012-09-11 15:28:06 -040056#include "variables.h"
57#include "data.hpp"
58#include "partitions.hpp"
bigbiff bigbiff56cf5642016-08-19 17:43:45 -040059#include "twrpDigestDriver.hpp"
60#include "twrpDigest/twrpDigest.hpp"
61#include "twrpDigest/twrpMD5.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040062#include "twrp-functions.hpp"
Ethan Yonker74db1572015-10-28 12:44:49 -050063#include "gui/gui.hpp"
Ethan Yonker20eb0bc2016-03-22 14:23:28 -050064#include "gui/pages.hpp"
Ethan Yonkerf1179622016-08-25 15:32:21 -050065#include "legacy_property_service.h"
Ethan Yonker941a8992016-12-05 09:04:30 -060066#include "twinstall.h"
67#include "installcommand.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000068extern "C" {
69 #include "gui/gui.h"
that7e303cf2014-03-06 07:57:43 +010070}
71
Ethan Yonker941a8992016-12-05 09:04:30 -060072#define AB_OTA "payload_properties.txt"
73
lambdadroidc4faea82018-09-26 22:56:51 +020074#ifndef TW_NO_LEGACY_PROPS
that7e303cf2014-03-06 07:57:43 +010075static const char* properties_path = "/dev/__properties__";
76static const char* properties_path_renamed = "/dev/__properties_kk__";
Matt Mowercdd3b332014-03-27 14:38:48 -050077static bool legacy_props_env_initd = false;
78static bool legacy_props_path_modified = false;
lambdadroidc4faea82018-09-26 22:56:51 +020079#endif
that7e303cf2014-03-06 07:57:43 +010080
Ethan Yonker941a8992016-12-05 09:04:30 -060081enum zip_type {
82 UNKNOWN_ZIP_TYPE = 0,
83 UPDATE_BINARY_ZIP_TYPE,
84 AB_OTA_ZIP_TYPE,
85 TWRP_THEME_ZIP_TYPE
86};
87
lambdadroidc4faea82018-09-26 22:56:51 +020088#ifndef TW_NO_LEGACY_PROPS
that50640482015-08-30 12:08:05 +020089// to support pre-KitKat update-binaries that expect properties in the legacy format
Matt Mowercdd3b332014-03-27 14:38:48 -050090static int switch_to_legacy_properties()
that7e303cf2014-03-06 07:57:43 +010091{
Matt Mowercdd3b332014-03-27 14:38:48 -050092 if (!legacy_props_env_initd) {
93 if (legacy_properties_init() != 0)
94 return -1;
95
96 char tmp[32];
97 int propfd, propsz;
98 legacy_get_property_workspace(&propfd, &propsz);
99 sprintf(tmp, "%d,%d", dup(propfd), propsz);
100 setenv("ANDROID_PROPERTY_WORKSPACE", tmp, 1);
101 legacy_props_env_initd = true;
102 }
that7e303cf2014-03-06 07:57:43 +0100103
104 if (TWFunc::Path_Exists(properties_path)) {
105 // hide real properties so that the updater uses the envvar to find the legacy format properties
Matt Mowercdd3b332014-03-27 14:38:48 -0500106 if (rename(properties_path, properties_path_renamed) != 0) {
107 LOGERR("Renaming %s failed: %s\n", properties_path, strerror(errno));
108 return -1;
109 } else {
110 legacy_props_path_modified = true;
111 }
that7e303cf2014-03-06 07:57:43 +0100112 }
Matt Mowercdd3b332014-03-27 14:38:48 -0500113
114 return 0;
that7e303cf2014-03-06 07:57:43 +0100115}
116
Matt Mowercdd3b332014-03-27 14:38:48 -0500117static int switch_to_new_properties()
that7e303cf2014-03-06 07:57:43 +0100118{
119 if (TWFunc::Path_Exists(properties_path_renamed)) {
Matt Mowercdd3b332014-03-27 14:38:48 -0500120 if (rename(properties_path_renamed, properties_path) != 0) {
121 LOGERR("Renaming %s failed: %s\n", properties_path_renamed, strerror(errno));
122 return -1;
123 } else {
124 legacy_props_path_modified = false;
125 }
that7e303cf2014-03-06 07:57:43 +0100126 }
Matt Mowercdd3b332014-03-27 14:38:48 -0500127
128 return 0;
Dees_Troy2673cec2013-04-02 20:22:16 +0000129}
lambdadroidc4faea82018-09-26 22:56:51 +0200130#endif
Dees_Troy32c8eb82012-09-11 15:28:06 -0400131
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500132static int Install_Theme(const char* path, ZipWrap *Zip) {
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500133#ifdef TW_OEM_BUILD // We don't do custom themes in OEM builds
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500134 Zip->Close();
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500135 return INSTALL_CORRUPT;
136#else
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500137 if (!Zip->EntryExists("ui.xml")) {
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500138 return INSTALL_CORRUPT;
139 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500140 Zip->Close();
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500141 if (!PartitionManager.Mount_Settings_Storage(true))
142 return INSTALL_ERROR;
143 string theme_path = DataManager::GetSettingsStoragePath();
144 theme_path += "/TWRP/theme";
145 if (!TWFunc::Path_Exists(theme_path)) {
146 if (!TWFunc::Recursive_Mkdir(theme_path)) {
147 return INSTALL_ERROR;
148 }
149 }
150 theme_path += "/ui.zip";
151 if (TWFunc::copy_file(path, theme_path, 0644) != 0) {
152 return INSTALL_ERROR;
153 }
154 LOGINFO("Installing custom theme '%s' to '%s'\n", path, theme_path.c_str());
155 PageManager::RequestReload();
156 return INSTALL_SUCCESS;
157#endif
158}
159
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500160static int Prepare_Update_Binary(const char *path, ZipWrap *Zip, int* wipe_cache) {
Ethan Yonker193befe2019-04-02 16:01:31 -0500161 char arches[PATH_MAX];
162 std::string binary_name = ASSUMED_UPDATE_BINARY_NAME;
163 property_get("ro.product.cpu.abilist", arches, "error");
164 if (strcmp(arches, "error") == 0)
165 property_get("ro.product.cpu.abi", arches, "error");
166 vector<string> split = TWFunc::split_string(arches, ',', true);
167 std::vector<string>::iterator arch;
168 std::string base_name = binary_name;
169 base_name += "-";
170 for (arch = split.begin(); arch != split.end(); arch++) {
171 std::string temp = base_name + *arch;
172 if (Zip->EntryExists(temp)) {
173 binary_name = temp;
174 break;
175 }
176 }
177 LOGINFO("Extracting updater binary '%s'\n", binary_name.c_str());
178 if (!Zip->ExtractEntry(binary_name.c_str(), TMP_UPDATER_BINARY_PATH, 0755)) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500179 Zip->Close();
Dees_Troy2673cec2013-04-02 20:22:16 +0000180 LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME);
181 return INSTALL_ERROR;
182 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400183
Dees_Troy512376c2013-09-03 19:39:41 +0000184 // If exists, extract file_contexts from the zip file
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500185 if (!Zip->EntryExists("file_contexts")) {
186 Zip->Close();
Dees_Troy512376c2013-09-03 19:39:41 +0000187 LOGINFO("Zip does not contain SELinux file_contexts file in its root.\n");
188 } else {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500189 const string output_filename = "/file_contexts";
Dees_Troy512376c2013-09-03 19:39:41 +0000190 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 -0500191 if (!Zip->ExtractEntry("file_contexts", output_filename, 0644)) {
192 Zip->Close();
that50640482015-08-30 12:08:05 +0200193 LOGERR("Could not extract '%s'\n", output_filename.c_str());
Dees_Troy512376c2013-09-03 19:39:41 +0000194 return INSTALL_ERROR;
195 }
196 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500197 Zip->Close();
Ethan Yonker941a8992016-12-05 09:04:30 -0600198 return INSTALL_SUCCESS;
199}
200
lambdadroidc4faea82018-09-26 22:56:51 +0200201#ifndef TW_NO_LEGACY_PROPS
nkk71b4c35912017-10-11 23:39:10 +0300202static bool update_binary_has_legacy_properties(const char *binary) {
203 const char str_to_match[] = "ANDROID_PROPERTY_WORKSPACE";
204 int len_to_match = sizeof(str_to_match) - 1;
205 bool found = false;
206
207 int fd = open(binary, O_RDONLY);
208 if (fd < 0) {
209 LOGINFO("has_legacy_properties: Could not open %s: %s!\n", binary, strerror(errno));
210 return false;
211 }
212
213 struct stat finfo;
214 if (fstat(fd, &finfo) < 0) {
215 LOGINFO("has_legacy_properties: Could not fstat %d: %s!\n", fd, strerror(errno));
216 close(fd);
217 return false;
218 }
219
220 void *data = mmap(NULL, finfo.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
221 if (data == MAP_FAILED) {
Simon Shi8ae2b0d2019-03-09 13:01:55 +0800222 LOGINFO("has_legacy_properties: mmap (size=%zu) failed: %s!\n", (size_t)finfo.st_size, strerror(errno));
nkk71b4c35912017-10-11 23:39:10 +0300223 } else {
224 if (memmem(data, finfo.st_size, str_to_match, len_to_match)) {
225 LOGINFO("has_legacy_properties: Found legacy property match!\n");
226 found = true;
227 }
228 munmap(data, finfo.st_size);
229 }
230 close(fd);
231
232 return found;
233}
lambdadroidc4faea82018-09-26 22:56:51 +0200234#endif
nkk71b4c35912017-10-11 23:39:10 +0300235
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500236static int Run_Update_Binary(const char *path, ZipWrap *Zip, int* wipe_cache, zip_type ztype) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600237 int ret_val, pipe_fd[2], status, zip_verify;
238 char buffer[1024];
239 FILE* child_data;
Dees_Troy512376c2013-09-03 19:39:41 +0000240
Matt Mower6883d732014-03-20 17:28:13 -0500241#ifndef TW_NO_LEGACY_PROPS
nkk71b4c35912017-10-11 23:39:10 +0300242 if (!update_binary_has_legacy_properties(TMP_UPDATER_BINARY_PATH)) {
243 LOGINFO("Legacy property environment not used in updater.\n");
244 } else if (switch_to_legacy_properties() != 0) { /* Set legacy properties */
245 LOGERR("Legacy property environment did not initialize successfully. Properties may not be detected.\n");
246 } else {
247 LOGINFO("Legacy property environment initialized.\n");
Matt Mowercdd3b332014-03-27 14:38:48 -0500248 }
Matt Mower6883d732014-03-20 17:28:13 -0500249#endif
Matt Mowercdd3b332014-03-27 14:38:48 -0500250
Dees_Troy2673cec2013-04-02 20:22:16 +0000251 pipe(pipe_fd);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400252
Ethan Yonker941a8992016-12-05 09:04:30 -0600253 std::vector<std::string> args;
254 if (ztype == UPDATE_BINARY_ZIP_TYPE) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500255 ret_val = update_binary_command(path, 0, pipe_fd[1], &args);
Ethan Yonker941a8992016-12-05 09:04:30 -0600256 } else if (ztype == AB_OTA_ZIP_TYPE) {
257 ret_val = abupdate_binary_command(path, Zip, 0, pipe_fd[1], &args);
258 } else {
259 LOGERR("Unknown zip type %i\n", ztype);
260 ret_val = INSTALL_CORRUPT;
261 }
262 if (ret_val) {
263 close(pipe_fd[0]);
264 close(pipe_fd[1]);
265 return ret_val;
266 }
267
268 // Convert the vector to a NULL-terminated char* array suitable for execv.
269 const char* chr_args[args.size() + 1];
270 chr_args[args.size()] = NULL;
271 for (size_t i = 0; i < args.size(); i++)
272 chr_args[i] = args[i].c_str();
Dees_Troy32c8eb82012-09-11 15:28:06 -0400273
Dees_Troy2673cec2013-04-02 20:22:16 +0000274 pid_t pid = fork();
275 if (pid == 0) {
276 close(pipe_fd[0]);
Ethan Yonker941a8992016-12-05 09:04:30 -0600277 execve(chr_args[0], const_cast<char**>(chr_args), environ);
278 printf("E:Can't execute '%s': %s\n", chr_args[0], strerror(errno));
Dees_Troy2673cec2013-04-02 20:22:16 +0000279 _exit(-1);
280 }
281 close(pipe_fd[1]);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400282
Dees_Troy2673cec2013-04-02 20:22:16 +0000283 *wipe_cache = 0;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400284
Dees_Troy2673cec2013-04-02 20:22:16 +0000285 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
286 child_data = fdopen(pipe_fd[0], "r");
287 while (fgets(buffer, sizeof(buffer), child_data) != NULL) {
288 char* command = strtok(buffer, " \n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200289 if (command == NULL) {
290 continue;
291 } else if (strcmp(command, "progress") == 0) {
292 char* fraction_char = strtok(NULL, " \n");
293 char* seconds_char = strtok(NULL, " \n");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400294
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200295 float fraction_float = strtof(fraction_char, NULL);
296 int seconds_float = strtol(seconds_char, NULL, 10);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400297
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200298 if (zip_verify)
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500299 DataManager::ShowProgress(fraction_float * (1 - VERIFICATION_PROGRESS_FRAC), seconds_float);
Dees_Troy2673cec2013-04-02 20:22:16 +0000300 else
301 DataManager::ShowProgress(fraction_float, seconds_float);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200302 } else if (strcmp(command, "set_progress") == 0) {
303 char* fraction_char = strtok(NULL, " \n");
304 float fraction_float = strtof(fraction_char, NULL);
Chaosmasterd5364a02020-02-03 15:38:02 +0100305 DataManager::_SetProgress(fraction_float);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200306 } else if (strcmp(command, "ui_print") == 0) {
307 char* display_value = strtok(NULL, "\n");
308 if (display_value) {
309 gui_print("%s", display_value);
310 } else {
311 gui_print("\n");
312 }
313 } else if (strcmp(command, "wipe_cache") == 0) {
314 *wipe_cache = 1;
315 } else if (strcmp(command, "clear_display") == 0) {
316 // Do nothing, not supported by TWRP
Ethan Yonker072c8d82016-08-26 22:22:24 -0500317 } else if (strcmp(command, "log") == 0) {
318 printf("%s\n", strtok(NULL, "\n"));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200319 } else {
320 LOGERR("unknown command [%s]\n", command);
321 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000322 }
323 fclose(child_data);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400324
that50640482015-08-30 12:08:05 +0200325 int waitrc = TWFunc::Wait_For_Child(pid, &status, "Updater");
Matt Mowercdd3b332014-03-27 14:38:48 -0500326
Matt Mower6883d732014-03-20 17:28:13 -0500327#ifndef TW_NO_LEGACY_PROPS
nkk71b4c35912017-10-11 23:39:10 +0300328 /* Unset legacy properties */
329 if (legacy_props_path_modified) {
330 if (switch_to_new_properties() != 0) {
331 LOGERR("Legacy property environment did not disable successfully. Legacy properties may still be in use.\n");
332 } else {
333 LOGINFO("Legacy property environment disabled.\n");
Matt Mowercdd3b332014-03-27 14:38:48 -0500334 }
335 }
Matt Mower6883d732014-03-20 17:28:13 -0500336#endif
Matt Mowercdd3b332014-03-27 14:38:48 -0500337
that50640482015-08-30 12:08:05 +0200338 if (waitrc != 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000339 return INSTALL_ERROR;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400340
Dees_Troy2673cec2013-04-02 20:22:16 +0000341 return INSTALL_SUCCESS;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400342}
343
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500344int TWinstall_zip(const char* path, int* wipe_cache) {
Chaosmasterff4f9582020-01-26 15:38:11 +0100345 int ret_val, zip_verify = 1, unmount_system = 1;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400346
Ethan Yonker24813422014-11-07 17:19:07 -0600347 if (strcmp(path, "error") == 0) {
348 LOGERR("Failed to get adb sideload file: '%s'\n", path);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400349 return INSTALL_CORRUPT;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500350 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400351
Ethan Yonker74db1572015-10-28 12:44:49 -0500352 gui_msg(Msg("installing_zip=Installing zip file '{1}'")(path));
Ethan Yonker24813422014-11-07 17:19:07 -0600353 if (strlen(path) < 9 || strncmp(path, "/sideload", 9) != 0) {
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400354 string digest_str;
355 string Full_Filename = path;
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400356
357 gui_msg("check_for_digest=Checking for Digest file...");
bigbiff bigbiff718ab392019-03-28 19:46:56 -0400358
Ethan Yonker1da568f2019-04-09 15:29:51 -0500359 if (*path != '@' && !twrpDigestDriver::Check_File_Digest(Full_Filename)) {
360 LOGERR("Aborting zip install: Digest verification failed\n");
361 return INSTALL_CORRUPT;
Ethan Yonker24813422014-11-07 17:19:07 -0600362 }
363 }
364
Chaosmasterff4f9582020-01-26 15:38:11 +0100365 DataManager::GetValue(TW_UNMOUNT_SYSTEM, unmount_system);
366
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500367#ifndef TW_OEM_BUILD
Dees_Troy32c8eb82012-09-11 15:28:06 -0400368 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500369#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000370 DataManager::SetProgress(0);
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600371
372 MemMapping map;
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600373#ifdef USE_MINZIP
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600374 if (sysMapFile(path, &map) != 0) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600375#else
376 if (!map.MapFile(path)) {
377#endif
Ethan Yonker74db1572015-10-28 12:44:49 -0500378 gui_msg(Msg(msg::kError, "fail_sysmap=Failed to map file '{1}'")(path));
that50640482015-08-30 12:08:05 +0200379 return -1;
380 }
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600381
Dees_Troy32c8eb82012-09-11 15:28:06 -0400382 if (zip_verify) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500383 gui_msg("verify_zip_sig=Verifying zip signature...");
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500384#ifdef USE_OLD_VERIFIER
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600385 ret_val = verify_file(map.addr, map.length);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500386#else
387 std::vector<Certificate> loadedKeys;
388 if (!load_keys("/res/keys", loadedKeys)) {
389 LOGINFO("Failed to load keys");
390 gui_err("verify_zip_fail=Zip signature verification failed!");
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600391#ifdef USE_MINZIP
392 sysReleaseMap(&map);
393#endif
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500394 return -1;
395 }
396 ret_val = verify_file(map.addr, map.length, loadedKeys, std::bind(&DataManager::SetProgress, std::placeholders::_1));
397#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000398 if (ret_val != VERIFY_SUCCESS) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500399 LOGINFO("Zip signature verification failed: %i\n", ret_val);
400 gui_err("verify_zip_fail=Zip signature verification failed!");
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600401#ifdef USE_MINZIP
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600402 sysReleaseMap(&map);
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600403#endif
Dees_Troy32c8eb82012-09-11 15:28:06 -0400404 return -1;
Ethan Yonker738be7a2014-12-10 11:40:43 -0600405 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500406 gui_msg("verify_zip_done=Zip signature verified successfully.");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400407 }
408 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500409 ZipWrap Zip;
410 if (!Zip.Open(path, &map)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500411 gui_err("zip_corrupt=Zip file is corrupt!");
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600412#ifdef USE_MINZIP
413 sysReleaseMap(&map);
414#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000415 return INSTALL_CORRUPT;
416 }
Ethan Yonker941a8992016-12-05 09:04:30 -0600417
Chaosmasterff4f9582020-01-26 15:38:11 +0100418 if (unmount_system) {
419 gui_msg("unmount_system=Unmounting System...");
420 if(!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
421 gui_err("unmount_system_err=Failed unmounting System");
422 return -1;
423 }
Chaosmaster4ee7cbd2020-06-05 17:38:08 +0200424 unlink("/system");
425 mkdir("/system", 0755);
Chaosmasterff4f9582020-01-26 15:38:11 +0100426 }
427
Ethan Yonker072c8d82016-08-26 22:22:24 -0500428 time_t start, stop;
429 time(&start);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500430 if (Zip.EntryExists(ASSUMED_UPDATE_BINARY_NAME)) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600431 LOGINFO("Update binary zip\n");
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500432 // Additionally verify the compatibility of the package.
433 if (!verify_package_compatibility(&Zip)) {
434 gui_err("zip_compatible_err=Zip Treble compatibility error!");
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500435 Zip.Close();
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600436#ifdef USE_MINZIP
437 sysReleaseMap(&map);
438#endif
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500439 ret_val = INSTALL_CORRUPT;
440 } else {
441 ret_val = Prepare_Update_Binary(path, &Zip, wipe_cache);
442 if (ret_val == INSTALL_SUCCESS)
443 ret_val = Run_Update_Binary(path, &Zip, wipe_cache, UPDATE_BINARY_ZIP_TYPE);
444 }
Ethan Yonker941a8992016-12-05 09:04:30 -0600445 } else {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500446 if (Zip.EntryExists(AB_OTA)) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600447 LOGINFO("AB zip\n");
Chaosmastera6da6562020-02-07 19:58:10 +0100448 gui_msg(Msg(msg::kHighlight, "flash_ab_inactive=Flashing A/B zip to inactive slot: {1}")(PartitionManager.Get_Active_Slot_Display()=="A"?"B":"A"));
Chaosmaster6c1477d2020-02-03 15:54:32 +0100449 // We need this so backuptool can do its magic
450 bool system_mount_state = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path());
451 bool vendor_mount_state = PartitionManager.Is_Mounted_By_Path("/vendor");
452 PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
453 PartitionManager.Mount_By_Path("/vendor", true);
bigbiffad58e1b2020-07-06 20:24:34 -0400454 TWFunc::Exec_Cmd("cp -f /system/bin/sh /tmp/sh");
Chaosmaster6c1477d2020-02-03 15:54:32 +0100455 mount("/tmp/sh", "/system/bin/sh", "auto", MS_BIND, NULL);
Ethan Yonker941a8992016-12-05 09:04:30 -0600456 ret_val = Run_Update_Binary(path, &Zip, wipe_cache, AB_OTA_ZIP_TYPE);
Chaosmaster6c1477d2020-02-03 15:54:32 +0100457 umount("/system/bin/sh");
458 unlink("/tmp/sh");
459 if (!vendor_mount_state)
460 PartitionManager.UnMount_By_Path("/vendor", true);
461 if (!system_mount_state)
462 PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true);
Chaosmastera6da6562020-02-07 19:58:10 +0100463 gui_warn("flash_ab_reboot=To flash additional zips, please reboot recovery to switch to the updated slot.");
Chaosmaster6c1477d2020-02-03 15:54:32 +0100464
Ethan Yonker941a8992016-12-05 09:04:30 -0600465 } else {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500466 if (Zip.EntryExists("ui.xml")) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600467 LOGINFO("TWRP theme zip\n");
468 ret_val = Install_Theme(path, &Zip);
469 } else {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500470 Zip.Close();
Ethan Yonker941a8992016-12-05 09:04:30 -0600471 ret_val = INSTALL_CORRUPT;
472 }
473 }
474 }
Ethan Yonker072c8d82016-08-26 22:22:24 -0500475 time(&stop);
476 int total_time = (int) difftime(stop, start);
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500477 if (ret_val == INSTALL_CORRUPT) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600478 gui_err("invalid_zip_format=Invalid zip file format!");
Ethan Yonker072c8d82016-08-26 22:22:24 -0500479 } else {
480 LOGINFO("Install took %i second(s).\n", total_time);
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500481 }
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600482#ifdef USE_MINZIP
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600483 sysReleaseMap(&map);
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600484#endif
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600485 return ret_val;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400486}