Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame] | 2 | Copyright 2012 to 2017 bigbiff/Dees_Troy TeamWin |
Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 3 | 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_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 18 | |
nkk71 | b4c3591 | 2017-10-11 23:39:10 +0300 | [diff] [blame] | 19 | |
| 20 | #ifndef _GNU_SOURCE |
| 21 | #define _GNU_SOURCE |
| 22 | #endif |
| 23 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 24 | #include <ctype.h> |
| 25 | #include <errno.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <limits.h> |
nkk71 | b4c3591 | 2017-10-11 23:39:10 +0300 | [diff] [blame] | 28 | #include <sys/mman.h> |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/wait.h> |
| 31 | #include <unistd.h> |
| 32 | |
| 33 | #include <string.h> |
| 34 | #include <stdio.h> |
Ethan Yonker | 193befe | 2019-04-02 16:01:31 -0500 | [diff] [blame] | 35 | #include <cutils/properties.h> |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 36 | |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 37 | #include <android-base/unique_fd.h> |
| 38 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 39 | #include "twcommon.h" |
Ethan Yonker | 75bf041 | 2014-11-21 13:54:27 -0600 | [diff] [blame] | 40 | #include "mtdutils/mounts.h" |
| 41 | #include "mtdutils/mtdutils.h" |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 42 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 43 | #include "otautil/sysutil.h" |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 44 | #include <ziparchive/zip_archive.h> |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 45 | #include "twinstall/install.h" |
| 46 | #include "twinstall/verifier.h" |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 47 | #include "variables.h" |
| 48 | #include "data.hpp" |
| 49 | #include "partitions.hpp" |
bigbiff bigbiff | 56cf564 | 2016-08-19 17:43:45 -0400 | [diff] [blame] | 50 | #include "twrpDigestDriver.hpp" |
| 51 | #include "twrpDigest/twrpDigest.hpp" |
| 52 | #include "twrpDigest/twrpMD5.hpp" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 53 | #include "twrp-functions.hpp" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 54 | #include "gui/gui.hpp" |
Ethan Yonker | 20eb0bc | 2016-03-22 14:23:28 -0500 | [diff] [blame] | 55 | #include "gui/pages.hpp" |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 56 | #include "twinstall.h" |
| 57 | #include "installcommand.h" |
nebrassy | ac29e69 | 2021-05-20 13:03:30 +0200 | [diff] [blame] | 58 | #include "../twrpRepacker.hpp" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 59 | extern "C" { |
| 60 | #include "gui/gui.h" |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 63 | #define AB_OTA "payload_properties.txt" |
| 64 | |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 65 | enum zip_type { |
| 66 | UNKNOWN_ZIP_TYPE = 0, |
| 67 | UPDATE_BINARY_ZIP_TYPE, |
| 68 | AB_OTA_ZIP_TYPE, |
| 69 | TWRP_THEME_ZIP_TYPE |
| 70 | }; |
| 71 | |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 72 | static int Install_Theme(const char* path, ZipArchiveHandle Zip) { |
Ethan Yonker | 20eb0bc | 2016-03-22 14:23:28 -0500 | [diff] [blame] | 73 | #ifdef TW_OEM_BUILD // We don't do custom themes in OEM builds |
Ethan Yonker | 20eb0bc | 2016-03-22 14:23:28 -0500 | [diff] [blame] | 74 | return INSTALL_CORRUPT; |
| 75 | #else |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 76 | std::string binary_name("ui.xml"); |
GarfieldHan | b3f6a26 | 2021-11-29 00:04:53 +0800 | [diff] [blame] | 77 | ZipEntry64 binary_entry; |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 78 | if (FindEntry(Zip, binary_name, &binary_entry) != 0) { |
Ethan Yonker | 20eb0bc | 2016-03-22 14:23:28 -0500 | [diff] [blame] | 79 | return INSTALL_CORRUPT; |
| 80 | } |
| 81 | if (!PartitionManager.Mount_Settings_Storage(true)) |
| 82 | return INSTALL_ERROR; |
| 83 | string theme_path = DataManager::GetSettingsStoragePath(); |
| 84 | theme_path += "/TWRP/theme"; |
| 85 | if (!TWFunc::Path_Exists(theme_path)) { |
| 86 | if (!TWFunc::Recursive_Mkdir(theme_path)) { |
| 87 | return INSTALL_ERROR; |
| 88 | } |
| 89 | } |
| 90 | theme_path += "/ui.zip"; |
| 91 | if (TWFunc::copy_file(path, theme_path, 0644) != 0) { |
| 92 | return INSTALL_ERROR; |
| 93 | } |
| 94 | LOGINFO("Installing custom theme '%s' to '%s'\n", path, theme_path.c_str()); |
| 95 | PageManager::RequestReload(); |
| 96 | return INSTALL_SUCCESS; |
| 97 | #endif |
| 98 | } |
| 99 | |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 100 | static int Prepare_Update_Binary(ZipArchiveHandle Zip) { |
Ethan Yonker | 193befe | 2019-04-02 16:01:31 -0500 | [diff] [blame] | 101 | char arches[PATH_MAX]; |
Ethan Yonker | 193befe | 2019-04-02 16:01:31 -0500 | [diff] [blame] | 102 | property_get("ro.product.cpu.abilist", arches, "error"); |
| 103 | if (strcmp(arches, "error") == 0) |
| 104 | property_get("ro.product.cpu.abi", arches, "error"); |
| 105 | vector<string> split = TWFunc::split_string(arches, ',', true); |
| 106 | std::vector<string>::iterator arch; |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 107 | std::string base_name = UPDATE_BINARY_NAME; |
Ethan Yonker | 193befe | 2019-04-02 16:01:31 -0500 | [diff] [blame] | 108 | base_name += "-"; |
GarfieldHan | b3f6a26 | 2021-11-29 00:04:53 +0800 | [diff] [blame] | 109 | ZipEntry64 binary_entry; |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 110 | std::string update_binary_string(UPDATE_BINARY_NAME); |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 111 | if (FindEntry(Zip, update_binary_string, &binary_entry) != 0) { |
| 112 | for (arch = split.begin(); arch != split.end(); arch++) { |
| 113 | std::string temp = base_name + *arch; |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 114 | std::string binary_name(temp.c_str()); |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 115 | if (FindEntry(Zip, binary_name, &binary_entry) != 0) { |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 116 | std::string binary_name(temp.c_str()); |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 117 | break; |
| 118 | } |
Ethan Yonker | 193befe | 2019-04-02 16:01:31 -0500 | [diff] [blame] | 119 | } |
| 120 | } |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 121 | LOGINFO("Extracting updater binary '%s'\n", UPDATE_BINARY_NAME); |
| 122 | unlink(TMP_UPDATER_BINARY_PATH); |
| 123 | android::base::unique_fd fd( |
| 124 | open(TMP_UPDATER_BINARY_PATH, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0755)); |
| 125 | if (fd == -1) { |
| 126 | return INSTALL_ERROR; |
| 127 | } |
| 128 | int32_t err = ExtractEntryToFile(Zip, &binary_entry, fd); |
| 129 | if (err != 0) { |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 130 | LOGERR("Could not extract '%s'\n", UPDATE_BINARY_NAME); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 131 | return INSTALL_ERROR; |
| 132 | } |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 133 | |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 134 | // If exists, extract file_contexts from the zip file |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 135 | std::string file_contexts("file_contexts"); |
GarfieldHan | b3f6a26 | 2021-11-29 00:04:53 +0800 | [diff] [blame] | 136 | ZipEntry64 file_contexts_entry; |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 137 | if (FindEntry(Zip, file_contexts, &file_contexts_entry) != 0) { |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 138 | LOGINFO("Zip does not contain SELinux file_contexts file in its root.\n"); |
| 139 | } else { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 140 | const string output_filename = "/file_contexts"; |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 141 | LOGINFO("Zip contains SELinux file_contexts file in its root. Extracting to %s\n", output_filename.c_str()); |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 142 | android::base::unique_fd fd( |
| 143 | open(output_filename.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, 0644)); |
| 144 | if (fd == -1) { |
| 145 | return INSTALL_ERROR; |
| 146 | } |
| 147 | if (ExtractEntryToFile(Zip, &file_contexts_entry, fd)) { |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 148 | LOGERR("Could not extract '%s'\n", output_filename.c_str()); |
Dees_Troy | 512376c | 2013-09-03 19:39:41 +0000 | [diff] [blame] | 149 | return INSTALL_ERROR; |
| 150 | } |
| 151 | } |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 152 | return INSTALL_SUCCESS; |
| 153 | } |
| 154 | |
nkk71 | b4c3591 | 2017-10-11 23:39:10 +0300 | [diff] [blame] | 155 | |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 156 | static int Run_Update_Binary(const char *path, int* wipe_cache, zip_type ztype) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 157 | int ret_val, pipe_fd[2], status, zip_verify; |
| 158 | char buffer[1024]; |
| 159 | FILE* child_data; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 160 | pipe(pipe_fd); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 161 | |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 162 | std::vector<std::string> args; |
| 163 | if (ztype == UPDATE_BINARY_ZIP_TYPE) { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 164 | ret_val = update_binary_command(path, 0, pipe_fd[1], &args); |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 165 | } else if (ztype == AB_OTA_ZIP_TYPE) { |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 166 | ret_val = abupdate_binary_command(path, 0, pipe_fd[1], &args); |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 167 | } else { |
| 168 | LOGERR("Unknown zip type %i\n", ztype); |
| 169 | ret_val = INSTALL_CORRUPT; |
| 170 | } |
| 171 | if (ret_val) { |
| 172 | close(pipe_fd[0]); |
| 173 | close(pipe_fd[1]); |
| 174 | return ret_val; |
| 175 | } |
| 176 | |
| 177 | // Convert the vector to a NULL-terminated char* array suitable for execv. |
| 178 | const char* chr_args[args.size() + 1]; |
| 179 | chr_args[args.size()] = NULL; |
| 180 | for (size_t i = 0; i < args.size(); i++) |
| 181 | chr_args[i] = args[i].c_str(); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 182 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 183 | pid_t pid = fork(); |
| 184 | if (pid == 0) { |
| 185 | close(pipe_fd[0]); |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 186 | execve(chr_args[0], const_cast<char**>(chr_args), environ); |
| 187 | printf("E:Can't execute '%s': %s\n", chr_args[0], strerror(errno)); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 188 | _exit(-1); |
| 189 | } |
| 190 | close(pipe_fd[1]); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 191 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 192 | *wipe_cache = 0; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 193 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 194 | DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify); |
| 195 | child_data = fdopen(pipe_fd[0], "r"); |
| 196 | while (fgets(buffer, sizeof(buffer), child_data) != NULL) { |
| 197 | char* command = strtok(buffer, " \n"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 198 | if (command == NULL) { |
| 199 | continue; |
| 200 | } else if (strcmp(command, "progress") == 0) { |
| 201 | char* fraction_char = strtok(NULL, " \n"); |
| 202 | char* seconds_char = strtok(NULL, " \n"); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 203 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 204 | float fraction_float = strtof(fraction_char, NULL); |
| 205 | int seconds_float = strtol(seconds_char, NULL, 10); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 206 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 207 | if (zip_verify) |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 208 | DataManager::ShowProgress(fraction_float * (1 - VERIFICATION_PROGRESS_FRACTION), seconds_float); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 209 | else |
| 210 | DataManager::ShowProgress(fraction_float, seconds_float); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 211 | } else if (strcmp(command, "set_progress") == 0) { |
| 212 | char* fraction_char = strtok(NULL, " \n"); |
| 213 | float fraction_float = strtof(fraction_char, NULL); |
Chaosmaster | d5364a0 | 2020-02-03 15:38:02 +0100 | [diff] [blame] | 214 | DataManager::_SetProgress(fraction_float); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 215 | } else if (strcmp(command, "ui_print") == 0) { |
| 216 | char* display_value = strtok(NULL, "\n"); |
| 217 | if (display_value) { |
| 218 | gui_print("%s", display_value); |
| 219 | } else { |
| 220 | gui_print("\n"); |
| 221 | } |
| 222 | } else if (strcmp(command, "wipe_cache") == 0) { |
| 223 | *wipe_cache = 1; |
| 224 | } else if (strcmp(command, "clear_display") == 0) { |
| 225 | // Do nothing, not supported by TWRP |
Ethan Yonker | 072c8d8 | 2016-08-26 22:22:24 -0500 | [diff] [blame] | 226 | } else if (strcmp(command, "log") == 0) { |
| 227 | printf("%s\n", strtok(NULL, "\n")); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 228 | } else { |
| 229 | LOGERR("unknown command [%s]\n", command); |
| 230 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 231 | } |
| 232 | fclose(child_data); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 233 | |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 234 | int waitrc = TWFunc::Wait_For_Child(pid, &status, "Updater"); |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 235 | if (waitrc != 0) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 236 | return INSTALL_ERROR; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 237 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 238 | return INSTALL_SUCCESS; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 239 | } |
| 240 | |
epicX | 9d80efa | 2021-03-12 18:14:23 +0530 | [diff] [blame] | 241 | int TWinstall_zip(const char* path, int* wipe_cache, bool check_for_digest) { |
nebrassy | ac29e69 | 2021-05-20 13:03:30 +0200 | [diff] [blame] | 242 | int ret_val, zip_verify = 1, unmount_system = 1, reflashtwrp = 0; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 243 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 244 | gui_msg(Msg("installing_zip=Installing zip file '{1}'")(path)); |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 245 | if (strlen(path) < 9 || strncmp(path, "/sideload", 9) != 0) { |
bigbiff bigbiff | 56cf564 | 2016-08-19 17:43:45 -0400 | [diff] [blame] | 246 | string digest_str; |
| 247 | string Full_Filename = path; |
bigbiff bigbiff | 56cf564 | 2016-08-19 17:43:45 -0400 | [diff] [blame] | 248 | |
epicX | 9d80efa | 2021-03-12 18:14:23 +0530 | [diff] [blame] | 249 | if (check_for_digest) { |
| 250 | gui_msg("check_for_digest=Checking for Digest file..."); |
| 251 | if (*path != '@' && !twrpDigestDriver::Check_File_Digest(Full_Filename)) { |
| 252 | LOGERR("Aborting zip install: Digest verification failed\n"); |
| 253 | return INSTALL_CORRUPT; |
| 254 | } |
Ethan Yonker | 2481342 | 2014-11-07 17:19:07 -0600 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Chaosmaster | ff4f958 | 2020-01-26 15:38:11 +0100 | [diff] [blame] | 258 | DataManager::GetValue(TW_UNMOUNT_SYSTEM, unmount_system); |
| 259 | |
Ethan Yonker | d5801c5 | 2014-04-14 08:59:35 -0500 | [diff] [blame] | 260 | #ifndef TW_OEM_BUILD |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 261 | DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify); |
Ethan Yonker | d5801c5 | 2014-04-14 08:59:35 -0500 | [diff] [blame] | 262 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 263 | DataManager::SetProgress(0); |
Ethan Yonker | f96087e | 2014-11-07 10:38:51 -0600 | [diff] [blame] | 264 | |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 265 | auto package = Package::CreateMemoryPackage(path); |
| 266 | if (!package) { |
| 267 | return INSTALL_CORRUPT; |
that | 5064048 | 2015-08-30 12:08:05 +0200 | [diff] [blame] | 268 | } |
Ethan Yonker | f96087e | 2014-11-07 10:38:51 -0600 | [diff] [blame] | 269 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 270 | if (zip_verify) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 271 | gui_msg("verify_zip_sig=Verifying zip signature..."); |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 272 | static constexpr const char* CERTIFICATE_ZIP_FILE = "/system/etc/security/otacerts.zip"; |
| 273 | std::vector<Certificate> loaded_keys = LoadKeysFromZipfile(CERTIFICATE_ZIP_FILE); |
| 274 | if (loaded_keys.empty()) { |
| 275 | LOGERR("Failed to load keys\n"); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 276 | return -1; |
| 277 | } |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 278 | LOGINFO("%zu key(s) loaded from %s\n", loaded_keys.size(), CERTIFICATE_ZIP_FILE); |
| 279 | |
| 280 | ret_val = verify_file(package.get(), loaded_keys, std::bind(&DataManager::SetProgress, std::placeholders::_1)); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 281 | if (ret_val != VERIFY_SUCCESS) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 282 | LOGINFO("Zip signature verification failed: %i\n", ret_val); |
| 283 | gui_err("verify_zip_fail=Zip signature verification failed!"); |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 284 | #ifdef USE_MINZIP |
Ethan Yonker | f9796a4 | 2014-11-08 07:28:03 -0600 | [diff] [blame] | 285 | sysReleaseMap(&map); |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 286 | #endif |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 287 | return -1; |
Ethan Yonker | 738be7a | 2014-12-10 11:40:43 -0600 | [diff] [blame] | 288 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 289 | gui_msg("verify_zip_done=Zip signature verified successfully."); |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 290 | } |
| 291 | } |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 292 | |
| 293 | ZipArchiveHandle Zip = package->GetZipArchiveHandle(); |
| 294 | if (!Zip) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 295 | return INSTALL_CORRUPT; |
| 296 | } |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 297 | |
Chaosmaster | ff4f958 | 2020-01-26 15:38:11 +0100 | [diff] [blame] | 298 | if (unmount_system) { |
| 299 | gui_msg("unmount_system=Unmounting System..."); |
| 300 | if(!PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), true)) { |
| 301 | gui_err("unmount_system_err=Failed unmounting System"); |
| 302 | return -1; |
| 303 | } |
Chaosmaster | 4ee7cbd | 2020-06-05 17:38:08 +0200 | [diff] [blame] | 304 | unlink("/system"); |
| 305 | mkdir("/system", 0755); |
Chaosmaster | ff4f958 | 2020-01-26 15:38:11 +0100 | [diff] [blame] | 306 | } |
| 307 | |
Ethan Yonker | 072c8d8 | 2016-08-26 22:22:24 -0500 | [diff] [blame] | 308 | time_t start, stop; |
| 309 | time(&start); |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 310 | |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 311 | std::string update_binary_name(UPDATE_BINARY_NAME); |
GarfieldHan | b3f6a26 | 2021-11-29 00:04:53 +0800 | [diff] [blame] | 312 | ZipEntry64 update_binary_entry; |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 313 | if (FindEntry(Zip, update_binary_name, &update_binary_entry) == 0) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 314 | LOGINFO("Update binary zip\n"); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 315 | // Additionally verify the compatibility of the package. |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 316 | if (!verify_package_compatibility(Zip)) { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 317 | gui_err("zip_compatible_err=Zip Treble compatibility error!"); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 318 | ret_val = INSTALL_CORRUPT; |
| 319 | } else { |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 320 | ret_val = Prepare_Update_Binary(Zip); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 321 | if (ret_val == INSTALL_SUCCESS) |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 322 | ret_val = Run_Update_Binary(path, wipe_cache, UPDATE_BINARY_ZIP_TYPE); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 323 | } |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 324 | } else { |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 325 | std::string ab_binary_name(AB_OTA); |
GarfieldHan | b3f6a26 | 2021-11-29 00:04:53 +0800 | [diff] [blame] | 326 | ZipEntry64 ab_binary_entry; |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 327 | if (FindEntry(Zip, ab_binary_name, &ab_binary_entry) == 0) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 328 | LOGINFO("AB zip\n"); |
Chaosmaster | a6da656 | 2020-02-07 19:58:10 +0100 | [diff] [blame] | 329 | gui_msg(Msg(msg::kHighlight, "flash_ab_inactive=Flashing A/B zip to inactive slot: {1}")(PartitionManager.Get_Active_Slot_Display()=="A"?"B":"A")); |
Chaosmaster | 6c1477d | 2020-02-03 15:54:32 +0100 | [diff] [blame] | 330 | // We need this so backuptool can do its magic |
| 331 | bool system_mount_state = PartitionManager.Is_Mounted_By_Path(PartitionManager.Get_Android_Root_Path()); |
| 332 | bool vendor_mount_state = PartitionManager.Is_Mounted_By_Path("/vendor"); |
bigbiff | cfa875c | 2021-06-20 16:20:27 -0400 | [diff] [blame] | 333 | PartitionManager.Mount_By_Path(PartitionManager.Get_Android_Root_Path(), false); |
| 334 | PartitionManager.Mount_By_Path("/vendor", false); |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 335 | TWFunc::copy_file("/system/bin/sh", "/tmp/sh", 0755); |
Chaosmaster | 6c1477d | 2020-02-03 15:54:32 +0100 | [diff] [blame] | 336 | mount("/tmp/sh", "/system/bin/sh", "auto", MS_BIND, NULL); |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 337 | ret_val = Run_Update_Binary(path, wipe_cache, AB_OTA_ZIP_TYPE); |
Chaosmaster | 6c1477d | 2020-02-03 15:54:32 +0100 | [diff] [blame] | 338 | umount("/system/bin/sh"); |
| 339 | unlink("/tmp/sh"); |
| 340 | if (!vendor_mount_state) |
bigbiff | cfa875c | 2021-06-20 16:20:27 -0400 | [diff] [blame] | 341 | PartitionManager.UnMount_By_Path("/vendor", false); |
Chaosmaster | 6c1477d | 2020-02-03 15:54:32 +0100 | [diff] [blame] | 342 | if (!system_mount_state) |
bigbiff | cfa875c | 2021-06-20 16:20:27 -0400 | [diff] [blame] | 343 | PartitionManager.UnMount_By_Path(PartitionManager.Get_Android_Root_Path(), false); |
bigbiff | ab76bd7 | 2021-10-11 10:17:48 -0400 | [diff] [blame] | 344 | if (android::base::GetBoolProperty("ro.virtual_ab.enabled", false)) { |
bigbiff | 77f38b5 | 2022-08-25 18:32:11 -0400 | [diff] [blame] | 345 | PartitionManager.Unlock_Block_Partitions(); |
bigbiff | cfa875c | 2021-06-20 16:20:27 -0400 | [diff] [blame] | 346 | PartitionManager.Prepare_All_Super_Volumes(); |
| 347 | gui_warn("mount_vab_partitions=Devices on super may not mount until rebooting recovery."); |
| 348 | } |
Chaosmaster | a6da656 | 2020-02-07 19:58:10 +0100 | [diff] [blame] | 349 | gui_warn("flash_ab_reboot=To flash additional zips, please reboot recovery to switch to the updated slot."); |
nebrassy | ac29e69 | 2021-05-20 13:03:30 +0200 | [diff] [blame] | 350 | DataManager::GetValue(TW_AUTO_REFLASHTWRP_VAR, reflashtwrp); |
| 351 | if (reflashtwrp) { |
| 352 | twrpRepacker repacker; |
| 353 | repacker.Flash_Current_Twrp(); |
| 354 | } |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 355 | } else { |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 356 | std::string binary_name("ui.xml"); |
GarfieldHan | b3f6a26 | 2021-11-29 00:04:53 +0800 | [diff] [blame] | 357 | ZipEntry64 binary_entry; |
sekaiacg | 826ec47 | 2021-12-04 20:10:02 +0800 | [diff] [blame] | 358 | if (FindEntry(Zip, binary_name, &binary_entry) == 0) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 359 | LOGINFO("TWRP theme zip\n"); |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 360 | ret_val = Install_Theme(path, Zip); |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 361 | } else { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 362 | ret_val = INSTALL_CORRUPT; |
| 363 | } |
| 364 | } |
| 365 | } |
Ethan Yonker | 072c8d8 | 2016-08-26 22:22:24 -0500 | [diff] [blame] | 366 | time(&stop); |
| 367 | int total_time = (int) difftime(stop, start); |
Ethan Yonker | 20eb0bc | 2016-03-22 14:23:28 -0500 | [diff] [blame] | 368 | if (ret_val == INSTALL_CORRUPT) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 369 | gui_err("invalid_zip_format=Invalid zip file format!"); |
Ethan Yonker | 072c8d8 | 2016-08-26 22:22:24 -0500 | [diff] [blame] | 370 | } else { |
| 371 | LOGINFO("Install took %i second(s).\n", total_time); |
Ethan Yonker | 20eb0bc | 2016-03-22 14:23:28 -0500 | [diff] [blame] | 372 | } |
Ethan Yonker | f9796a4 | 2014-11-08 07:28:03 -0600 | [diff] [blame] | 373 | return ret_val; |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 374 | } |