blob: 92af497d36ec5ae8162c86cb7f60da7b6c1d4f26 [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>
31#include <unistd.h>
32
33#include <string.h>
34#include <stdio.h>
Ethan Yonker193befe2019-04-02 16:01:31 -050035#include <cutils/properties.h>
Dees_Troy32c8eb82012-09-11 15:28:06 -040036
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "twcommon.h"
Ethan Yonker75bf0412014-11-21 13:54:27 -060038#include "mtdutils/mounts.h"
39#include "mtdutils/mtdutils.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050040
41#ifdef USE_MINZIP
Dees_Troy32c8eb82012-09-11 15:28:06 -040042#include "minzip/SysUtil.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050043#else
44#include "otautil/SysUtil.h"
45#include <ziparchive/zip_archive.h>
46#endif
47#include "zipwrap.hpp"
Ethan Yonkerf1179622016-08-25 15:32:21 -050048#ifdef USE_OLD_VERIFIER
Ethan Yonker4bf259f2016-08-29 11:50:34 -050049#include "verifier24/verifier.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050050#else
Ethan Yonker75bf0412014-11-21 13:54:27 -060051#include "verifier.h"
Ethan Yonkerf1179622016-08-25 15:32:21 -050052#endif
Dees_Troy32c8eb82012-09-11 15:28:06 -040053#include "variables.h"
54#include "data.hpp"
55#include "partitions.hpp"
bigbiff bigbiff56cf5642016-08-19 17:43:45 -040056#include "twrpDigestDriver.hpp"
57#include "twrpDigest/twrpDigest.hpp"
58#include "twrpDigest/twrpMD5.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040059#include "twrp-functions.hpp"
Ethan Yonker74db1572015-10-28 12:44:49 -050060#include "gui/gui.hpp"
Ethan Yonker20eb0bc2016-03-22 14:23:28 -050061#include "gui/pages.hpp"
Ethan Yonkerf1179622016-08-25 15:32:21 -050062#include "legacy_property_service.h"
Ethan Yonker941a8992016-12-05 09:04:30 -060063#include "twinstall.h"
64#include "installcommand.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000065extern "C" {
66 #include "gui/gui.h"
that7e303cf2014-03-06 07:57:43 +010067}
68
Ethan Yonker941a8992016-12-05 09:04:30 -060069#define AB_OTA "payload_properties.txt"
70
lambdadroidc4faea82018-09-26 22:56:51 +020071#ifndef TW_NO_LEGACY_PROPS
that7e303cf2014-03-06 07:57:43 +010072static const char* properties_path = "/dev/__properties__";
73static const char* properties_path_renamed = "/dev/__properties_kk__";
Matt Mowercdd3b332014-03-27 14:38:48 -050074static bool legacy_props_env_initd = false;
75static bool legacy_props_path_modified = false;
lambdadroidc4faea82018-09-26 22:56:51 +020076#endif
that7e303cf2014-03-06 07:57:43 +010077
Ethan Yonker941a8992016-12-05 09:04:30 -060078enum zip_type {
79 UNKNOWN_ZIP_TYPE = 0,
80 UPDATE_BINARY_ZIP_TYPE,
81 AB_OTA_ZIP_TYPE,
82 TWRP_THEME_ZIP_TYPE
83};
84
lambdadroidc4faea82018-09-26 22:56:51 +020085#ifndef TW_NO_LEGACY_PROPS
that50640482015-08-30 12:08:05 +020086// to support pre-KitKat update-binaries that expect properties in the legacy format
Matt Mowercdd3b332014-03-27 14:38:48 -050087static int switch_to_legacy_properties()
that7e303cf2014-03-06 07:57:43 +010088{
Matt Mowercdd3b332014-03-27 14:38:48 -050089 if (!legacy_props_env_initd) {
90 if (legacy_properties_init() != 0)
91 return -1;
92
93 char tmp[32];
94 int propfd, propsz;
95 legacy_get_property_workspace(&propfd, &propsz);
96 sprintf(tmp, "%d,%d", dup(propfd), propsz);
97 setenv("ANDROID_PROPERTY_WORKSPACE", tmp, 1);
98 legacy_props_env_initd = true;
99 }
that7e303cf2014-03-06 07:57:43 +0100100
101 if (TWFunc::Path_Exists(properties_path)) {
102 // hide real properties so that the updater uses the envvar to find the legacy format properties
Matt Mowercdd3b332014-03-27 14:38:48 -0500103 if (rename(properties_path, properties_path_renamed) != 0) {
104 LOGERR("Renaming %s failed: %s\n", properties_path, strerror(errno));
105 return -1;
106 } else {
107 legacy_props_path_modified = true;
108 }
that7e303cf2014-03-06 07:57:43 +0100109 }
Matt Mowercdd3b332014-03-27 14:38:48 -0500110
111 return 0;
that7e303cf2014-03-06 07:57:43 +0100112}
113
Matt Mowercdd3b332014-03-27 14:38:48 -0500114static int switch_to_new_properties()
that7e303cf2014-03-06 07:57:43 +0100115{
116 if (TWFunc::Path_Exists(properties_path_renamed)) {
Matt Mowercdd3b332014-03-27 14:38:48 -0500117 if (rename(properties_path_renamed, properties_path) != 0) {
118 LOGERR("Renaming %s failed: %s\n", properties_path_renamed, strerror(errno));
119 return -1;
120 } else {
121 legacy_props_path_modified = false;
122 }
that7e303cf2014-03-06 07:57:43 +0100123 }
Matt Mowercdd3b332014-03-27 14:38:48 -0500124
125 return 0;
Dees_Troy2673cec2013-04-02 20:22:16 +0000126}
lambdadroidc4faea82018-09-26 22:56:51 +0200127#endif
Dees_Troy32c8eb82012-09-11 15:28:06 -0400128
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500129static int Install_Theme(const char* path, ZipWrap *Zip) {
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500130#ifdef TW_OEM_BUILD // We don't do custom themes in OEM builds
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500131 Zip->Close();
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500132 return INSTALL_CORRUPT;
133#else
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500134 if (!Zip->EntryExists("ui.xml")) {
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500135 return INSTALL_CORRUPT;
136 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500137 Zip->Close();
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500138 if (!PartitionManager.Mount_Settings_Storage(true))
139 return INSTALL_ERROR;
140 string theme_path = DataManager::GetSettingsStoragePath();
141 theme_path += "/TWRP/theme";
142 if (!TWFunc::Path_Exists(theme_path)) {
143 if (!TWFunc::Recursive_Mkdir(theme_path)) {
144 return INSTALL_ERROR;
145 }
146 }
147 theme_path += "/ui.zip";
148 if (TWFunc::copy_file(path, theme_path, 0644) != 0) {
149 return INSTALL_ERROR;
150 }
151 LOGINFO("Installing custom theme '%s' to '%s'\n", path, theme_path.c_str());
152 PageManager::RequestReload();
153 return INSTALL_SUCCESS;
154#endif
155}
156
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500157static int Prepare_Update_Binary(const char *path, ZipWrap *Zip, int* wipe_cache) {
Ethan Yonker193befe2019-04-02 16:01:31 -0500158 char arches[PATH_MAX];
159 std::string binary_name = ASSUMED_UPDATE_BINARY_NAME;
160 property_get("ro.product.cpu.abilist", arches, "error");
161 if (strcmp(arches, "error") == 0)
162 property_get("ro.product.cpu.abi", arches, "error");
163 vector<string> split = TWFunc::split_string(arches, ',', true);
164 std::vector<string>::iterator arch;
165 std::string base_name = binary_name;
166 base_name += "-";
167 for (arch = split.begin(); arch != split.end(); arch++) {
168 std::string temp = base_name + *arch;
169 if (Zip->EntryExists(temp)) {
170 binary_name = temp;
171 break;
172 }
173 }
174 LOGINFO("Extracting updater binary '%s'\n", binary_name.c_str());
175 if (!Zip->ExtractEntry(binary_name.c_str(), TMP_UPDATER_BINARY_PATH, 0755)) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500176 Zip->Close();
Dees_Troy2673cec2013-04-02 20:22:16 +0000177 LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME);
178 return INSTALL_ERROR;
179 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400180
Dees_Troy512376c2013-09-03 19:39:41 +0000181 // If exists, extract file_contexts from the zip file
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500182 if (!Zip->EntryExists("file_contexts")) {
183 Zip->Close();
Dees_Troy512376c2013-09-03 19:39:41 +0000184 LOGINFO("Zip does not contain SELinux file_contexts file in its root.\n");
185 } else {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500186 const string output_filename = "/file_contexts";
Dees_Troy512376c2013-09-03 19:39:41 +0000187 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 -0500188 if (!Zip->ExtractEntry("file_contexts", output_filename, 0644)) {
189 Zip->Close();
that50640482015-08-30 12:08:05 +0200190 LOGERR("Could not extract '%s'\n", output_filename.c_str());
Dees_Troy512376c2013-09-03 19:39:41 +0000191 return INSTALL_ERROR;
192 }
193 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500194 Zip->Close();
Ethan Yonker941a8992016-12-05 09:04:30 -0600195 return INSTALL_SUCCESS;
196}
197
lambdadroidc4faea82018-09-26 22:56:51 +0200198#ifndef TW_NO_LEGACY_PROPS
nkk71b4c35912017-10-11 23:39:10 +0300199static bool update_binary_has_legacy_properties(const char *binary) {
200 const char str_to_match[] = "ANDROID_PROPERTY_WORKSPACE";
201 int len_to_match = sizeof(str_to_match) - 1;
202 bool found = false;
203
204 int fd = open(binary, O_RDONLY);
205 if (fd < 0) {
206 LOGINFO("has_legacy_properties: Could not open %s: %s!\n", binary, strerror(errno));
207 return false;
208 }
209
210 struct stat finfo;
211 if (fstat(fd, &finfo) < 0) {
212 LOGINFO("has_legacy_properties: Could not fstat %d: %s!\n", fd, strerror(errno));
213 close(fd);
214 return false;
215 }
216
217 void *data = mmap(NULL, finfo.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
218 if (data == MAP_FAILED) {
Simon Shi8ae2b0d2019-03-09 13:01:55 +0800219 LOGINFO("has_legacy_properties: mmap (size=%zu) failed: %s!\n", (size_t)finfo.st_size, strerror(errno));
nkk71b4c35912017-10-11 23:39:10 +0300220 } else {
221 if (memmem(data, finfo.st_size, str_to_match, len_to_match)) {
222 LOGINFO("has_legacy_properties: Found legacy property match!\n");
223 found = true;
224 }
225 munmap(data, finfo.st_size);
226 }
227 close(fd);
228
229 return found;
230}
lambdadroidc4faea82018-09-26 22:56:51 +0200231#endif
nkk71b4c35912017-10-11 23:39:10 +0300232
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500233static int Run_Update_Binary(const char *path, ZipWrap *Zip, int* wipe_cache, zip_type ztype) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600234 int ret_val, pipe_fd[2], status, zip_verify;
235 char buffer[1024];
236 FILE* child_data;
Dees_Troy512376c2013-09-03 19:39:41 +0000237
Matt Mower6883d732014-03-20 17:28:13 -0500238#ifndef TW_NO_LEGACY_PROPS
nkk71b4c35912017-10-11 23:39:10 +0300239 if (!update_binary_has_legacy_properties(TMP_UPDATER_BINARY_PATH)) {
240 LOGINFO("Legacy property environment not used in updater.\n");
241 } else if (switch_to_legacy_properties() != 0) { /* Set legacy properties */
242 LOGERR("Legacy property environment did not initialize successfully. Properties may not be detected.\n");
243 } else {
244 LOGINFO("Legacy property environment initialized.\n");
Matt Mowercdd3b332014-03-27 14:38:48 -0500245 }
Matt Mower6883d732014-03-20 17:28:13 -0500246#endif
Matt Mowercdd3b332014-03-27 14:38:48 -0500247
Dees_Troy2673cec2013-04-02 20:22:16 +0000248 pipe(pipe_fd);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400249
Ethan Yonker941a8992016-12-05 09:04:30 -0600250 std::vector<std::string> args;
251 if (ztype == UPDATE_BINARY_ZIP_TYPE) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500252 ret_val = update_binary_command(path, 0, pipe_fd[1], &args);
Ethan Yonker941a8992016-12-05 09:04:30 -0600253 } else if (ztype == AB_OTA_ZIP_TYPE) {
254 ret_val = abupdate_binary_command(path, Zip, 0, pipe_fd[1], &args);
255 } else {
256 LOGERR("Unknown zip type %i\n", ztype);
257 ret_val = INSTALL_CORRUPT;
258 }
259 if (ret_val) {
260 close(pipe_fd[0]);
261 close(pipe_fd[1]);
262 return ret_val;
263 }
264
265 // Convert the vector to a NULL-terminated char* array suitable for execv.
266 const char* chr_args[args.size() + 1];
267 chr_args[args.size()] = NULL;
268 for (size_t i = 0; i < args.size(); i++)
269 chr_args[i] = args[i].c_str();
Dees_Troy32c8eb82012-09-11 15:28:06 -0400270
Dees_Troy2673cec2013-04-02 20:22:16 +0000271 pid_t pid = fork();
272 if (pid == 0) {
273 close(pipe_fd[0]);
Ethan Yonker941a8992016-12-05 09:04:30 -0600274 execve(chr_args[0], const_cast<char**>(chr_args), environ);
275 printf("E:Can't execute '%s': %s\n", chr_args[0], strerror(errno));
Dees_Troy2673cec2013-04-02 20:22:16 +0000276 _exit(-1);
277 }
278 close(pipe_fd[1]);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400279
Dees_Troy2673cec2013-04-02 20:22:16 +0000280 *wipe_cache = 0;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400281
Dees_Troy2673cec2013-04-02 20:22:16 +0000282 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
283 child_data = fdopen(pipe_fd[0], "r");
284 while (fgets(buffer, sizeof(buffer), child_data) != NULL) {
285 char* command = strtok(buffer, " \n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200286 if (command == NULL) {
287 continue;
288 } else if (strcmp(command, "progress") == 0) {
289 char* fraction_char = strtok(NULL, " \n");
290 char* seconds_char = strtok(NULL, " \n");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400291
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200292 float fraction_float = strtof(fraction_char, NULL);
293 int seconds_float = strtol(seconds_char, NULL, 10);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400294
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200295 if (zip_verify)
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500296 DataManager::ShowProgress(fraction_float * (1 - VERIFICATION_PROGRESS_FRAC), seconds_float);
Dees_Troy2673cec2013-04-02 20:22:16 +0000297 else
298 DataManager::ShowProgress(fraction_float, seconds_float);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200299 } else if (strcmp(command, "set_progress") == 0) {
300 char* fraction_char = strtok(NULL, " \n");
301 float fraction_float = strtof(fraction_char, NULL);
302 DataManager::SetProgress(fraction_float);
303 } else if (strcmp(command, "ui_print") == 0) {
304 char* display_value = strtok(NULL, "\n");
305 if (display_value) {
306 gui_print("%s", display_value);
307 } else {
308 gui_print("\n");
309 }
310 } else if (strcmp(command, "wipe_cache") == 0) {
311 *wipe_cache = 1;
312 } else if (strcmp(command, "clear_display") == 0) {
313 // Do nothing, not supported by TWRP
Ethan Yonker072c8d82016-08-26 22:22:24 -0500314 } else if (strcmp(command, "log") == 0) {
315 printf("%s\n", strtok(NULL, "\n"));
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200316 } else {
317 LOGERR("unknown command [%s]\n", command);
318 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000319 }
320 fclose(child_data);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400321
that50640482015-08-30 12:08:05 +0200322 int waitrc = TWFunc::Wait_For_Child(pid, &status, "Updater");
Matt Mowercdd3b332014-03-27 14:38:48 -0500323
Matt Mower6883d732014-03-20 17:28:13 -0500324#ifndef TW_NO_LEGACY_PROPS
nkk71b4c35912017-10-11 23:39:10 +0300325 /* Unset legacy properties */
326 if (legacy_props_path_modified) {
327 if (switch_to_new_properties() != 0) {
328 LOGERR("Legacy property environment did not disable successfully. Legacy properties may still be in use.\n");
329 } else {
330 LOGINFO("Legacy property environment disabled.\n");
Matt Mowercdd3b332014-03-27 14:38:48 -0500331 }
332 }
Matt Mower6883d732014-03-20 17:28:13 -0500333#endif
Matt Mowercdd3b332014-03-27 14:38:48 -0500334
that50640482015-08-30 12:08:05 +0200335 if (waitrc != 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000336 return INSTALL_ERROR;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400337
Dees_Troy2673cec2013-04-02 20:22:16 +0000338 return INSTALL_SUCCESS;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400339}
340
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500341int TWinstall_zip(const char* path, int* wipe_cache) {
Chaosmasterff4f9582020-01-26 15:38:11 +0100342 int ret_val, zip_verify = 1, unmount_system = 1;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400343
Ethan Yonker24813422014-11-07 17:19:07 -0600344 if (strcmp(path, "error") == 0) {
345 LOGERR("Failed to get adb sideload file: '%s'\n", path);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400346 return INSTALL_CORRUPT;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500347 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400348
Ethan Yonker74db1572015-10-28 12:44:49 -0500349 gui_msg(Msg("installing_zip=Installing zip file '{1}'")(path));
Ethan Yonker24813422014-11-07 17:19:07 -0600350 if (strlen(path) < 9 || strncmp(path, "/sideload", 9) != 0) {
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400351 string digest_str;
352 string Full_Filename = path;
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400353
354 gui_msg("check_for_digest=Checking for Digest file...");
bigbiff bigbiff718ab392019-03-28 19:46:56 -0400355
Ethan Yonker1da568f2019-04-09 15:29:51 -0500356 if (*path != '@' && !twrpDigestDriver::Check_File_Digest(Full_Filename)) {
357 LOGERR("Aborting zip install: Digest verification failed\n");
358 return INSTALL_CORRUPT;
Ethan Yonker24813422014-11-07 17:19:07 -0600359 }
360 }
361
Chaosmasterff4f9582020-01-26 15:38:11 +0100362 DataManager::GetValue(TW_UNMOUNT_SYSTEM, unmount_system);
363
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500364#ifndef TW_OEM_BUILD
Dees_Troy32c8eb82012-09-11 15:28:06 -0400365 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500366#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000367 DataManager::SetProgress(0);
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600368
369 MemMapping map;
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600370#ifdef USE_MINZIP
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600371 if (sysMapFile(path, &map) != 0) {
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600372#else
373 if (!map.MapFile(path)) {
374#endif
Ethan Yonker74db1572015-10-28 12:44:49 -0500375 gui_msg(Msg(msg::kError, "fail_sysmap=Failed to map file '{1}'")(path));
that50640482015-08-30 12:08:05 +0200376 return -1;
377 }
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600378
Dees_Troy32c8eb82012-09-11 15:28:06 -0400379 if (zip_verify) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500380 gui_msg("verify_zip_sig=Verifying zip signature...");
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500381#ifdef USE_OLD_VERIFIER
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600382 ret_val = verify_file(map.addr, map.length);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500383#else
384 std::vector<Certificate> loadedKeys;
385 if (!load_keys("/res/keys", loadedKeys)) {
386 LOGINFO("Failed to load keys");
387 gui_err("verify_zip_fail=Zip signature verification failed!");
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600388#ifdef USE_MINZIP
389 sysReleaseMap(&map);
390#endif
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500391 return -1;
392 }
393 ret_val = verify_file(map.addr, map.length, loadedKeys, std::bind(&DataManager::SetProgress, std::placeholders::_1));
394#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000395 if (ret_val != VERIFY_SUCCESS) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500396 LOGINFO("Zip signature verification failed: %i\n", ret_val);
397 gui_err("verify_zip_fail=Zip signature verification failed!");
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600398#ifdef USE_MINZIP
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600399 sysReleaseMap(&map);
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600400#endif
Dees_Troy32c8eb82012-09-11 15:28:06 -0400401 return -1;
Ethan Yonker738be7a2014-12-10 11:40:43 -0600402 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500403 gui_msg("verify_zip_done=Zip signature verified successfully.");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400404 }
405 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500406 ZipWrap Zip;
407 if (!Zip.Open(path, &map)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500408 gui_err("zip_corrupt=Zip file is corrupt!");
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600409#ifdef USE_MINZIP
410 sysReleaseMap(&map);
411#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000412 return INSTALL_CORRUPT;
413 }
Ethan Yonker941a8992016-12-05 09:04:30 -0600414
Chaosmasterff4f9582020-01-26 15:38:11 +0100415 if (unmount_system) {
416 gui_msg("unmount_system=Unmounting System...");
417 if(!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) {
418 gui_err("unmount_system_err=Failed unmounting System");
419 return -1;
420 }
421 }
422
Ethan Yonker072c8d82016-08-26 22:22:24 -0500423 time_t start, stop;
424 time(&start);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500425 if (Zip.EntryExists(ASSUMED_UPDATE_BINARY_NAME)) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600426 LOGINFO("Update binary zip\n");
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500427 // Additionally verify the compatibility of the package.
428 if (!verify_package_compatibility(&Zip)) {
429 gui_err("zip_compatible_err=Zip Treble compatibility error!");
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500430 Zip.Close();
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600431#ifdef USE_MINZIP
432 sysReleaseMap(&map);
433#endif
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500434 ret_val = INSTALL_CORRUPT;
435 } else {
436 ret_val = Prepare_Update_Binary(path, &Zip, wipe_cache);
437 if (ret_val == INSTALL_SUCCESS)
438 ret_val = Run_Update_Binary(path, &Zip, wipe_cache, UPDATE_BINARY_ZIP_TYPE);
439 }
Ethan Yonker941a8992016-12-05 09:04:30 -0600440 } else {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500441 if (Zip.EntryExists(AB_OTA)) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600442 LOGINFO("AB zip\n");
443 ret_val = Run_Update_Binary(path, &Zip, wipe_cache, AB_OTA_ZIP_TYPE);
444 } else {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500445 if (Zip.EntryExists("ui.xml")) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600446 LOGINFO("TWRP theme zip\n");
447 ret_val = Install_Theme(path, &Zip);
448 } else {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500449 Zip.Close();
Ethan Yonker941a8992016-12-05 09:04:30 -0600450 ret_val = INSTALL_CORRUPT;
451 }
452 }
453 }
Ethan Yonker072c8d82016-08-26 22:22:24 -0500454 time(&stop);
455 int total_time = (int) difftime(stop, start);
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500456 if (ret_val == INSTALL_CORRUPT) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600457 gui_err("invalid_zip_format=Invalid zip file format!");
Ethan Yonker072c8d82016-08-26 22:22:24 -0500458 } else {
459 LOGINFO("Install took %i second(s).\n", total_time);
Ethan Yonker20eb0bc2016-03-22 14:23:28 -0500460 }
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600461#ifdef USE_MINZIP
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600462 sysReleaseMap(&map);
Ethan Yonkerecbd3e82017-12-14 14:43:59 -0600463#endif
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600464 return ret_val;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400465}