blob: 91a1a359592295dee5ddd2d2b09151380f0243f3 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
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_Troy32c8eb82012-09-11 15:28:06 -040018
19#include <ctype.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <limits.h>
23#include <sys/stat.h>
24#include <sys/wait.h>
25#include <unistd.h>
26
27#include <string.h>
28#include <stdio.h>
29
Dees_Troy2673cec2013-04-02 20:22:16 +000030#include "twcommon.h"
Dees_Troyb9d88ac2012-09-14 14:34:19 -040031#include "mincrypt/rsa.h"
32#include "mincrypt/sha.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040033#include "minui/minui.h"
Ethan Yonker75bf0412014-11-21 13:54:27 -060034#include "mtdutils/mounts.h"
35#include "mtdutils/mtdutils.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040036#include "minzip/SysUtil.h"
37#include "minzip/Zip.h"
Ethan Yonker75bf0412014-11-21 13:54:27 -060038#include "verifier.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040039#include "variables.h"
40#include "data.hpp"
41#include "partitions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050042#include "twrpDigest.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040043#include "twrp-functions.hpp"
Dees_Troy2673cec2013-04-02 20:22:16 +000044extern "C" {
45 #include "gui/gui.h"
that7e303cf2014-03-06 07:57:43 +010046 #include "legacy_property_service.h"
47}
48
49static const char* properties_path = "/dev/__properties__";
50static const char* properties_path_renamed = "/dev/__properties_kk__";
Matt Mowercdd3b332014-03-27 14:38:48 -050051static bool legacy_props_env_initd = false;
52static bool legacy_props_path_modified = false;
that7e303cf2014-03-06 07:57:43 +010053
Matt Mowercdd3b332014-03-27 14:38:48 -050054static int switch_to_legacy_properties()
that7e303cf2014-03-06 07:57:43 +010055{
Matt Mowercdd3b332014-03-27 14:38:48 -050056 if (!legacy_props_env_initd) {
57 if (legacy_properties_init() != 0)
58 return -1;
59
60 char tmp[32];
61 int propfd, propsz;
62 legacy_get_property_workspace(&propfd, &propsz);
63 sprintf(tmp, "%d,%d", dup(propfd), propsz);
64 setenv("ANDROID_PROPERTY_WORKSPACE", tmp, 1);
65 legacy_props_env_initd = true;
66 }
that7e303cf2014-03-06 07:57:43 +010067
68 if (TWFunc::Path_Exists(properties_path)) {
69 // hide real properties so that the updater uses the envvar to find the legacy format properties
Matt Mowercdd3b332014-03-27 14:38:48 -050070 if (rename(properties_path, properties_path_renamed) != 0) {
71 LOGERR("Renaming %s failed: %s\n", properties_path, strerror(errno));
72 return -1;
73 } else {
74 legacy_props_path_modified = true;
75 }
that7e303cf2014-03-06 07:57:43 +010076 }
Matt Mowercdd3b332014-03-27 14:38:48 -050077
78 return 0;
that7e303cf2014-03-06 07:57:43 +010079}
80
Matt Mowercdd3b332014-03-27 14:38:48 -050081static int switch_to_new_properties()
that7e303cf2014-03-06 07:57:43 +010082{
83 if (TWFunc::Path_Exists(properties_path_renamed)) {
Matt Mowercdd3b332014-03-27 14:38:48 -050084 if (rename(properties_path_renamed, properties_path) != 0) {
85 LOGERR("Renaming %s failed: %s\n", properties_path_renamed, strerror(errno));
86 return -1;
87 } else {
88 legacy_props_path_modified = false;
89 }
that7e303cf2014-03-06 07:57:43 +010090 }
Matt Mowercdd3b332014-03-27 14:38:48 -050091
92 return 0;
Dees_Troy2673cec2013-04-02 20:22:16 +000093}
Dees_Troy32c8eb82012-09-11 15:28:06 -040094
Dees_Troy2673cec2013-04-02 20:22:16 +000095static int Run_Update_Binary(const char *path, ZipArchive *Zip, int* wipe_cache) {
96 const ZipEntry* binary_location = mzFindZipEntry(Zip, ASSUMED_UPDATE_BINARY_NAME);
97 string Temp_Binary = "/tmp/updater";
98 int binary_fd, ret_val, pipe_fd[2], status, zip_verify;
99 char buffer[1024];
100 const char** args = (const char**)malloc(sizeof(char*) * 5);
101 FILE* child_data;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400102
Dees_Troy2673cec2013-04-02 20:22:16 +0000103 if (binary_location == NULL) {
104 mzCloseZipArchive(Zip);
105 return INSTALL_CORRUPT;
106 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400107
Dees_Troy2673cec2013-04-02 20:22:16 +0000108 // Delete any existing updater
109 if (TWFunc::Path_Exists(Temp_Binary) && unlink(Temp_Binary.c_str()) != 0) {
110 LOGINFO("Unable to unlink '%s'\n", Temp_Binary.c_str());
111 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400112
Dees_Troy2673cec2013-04-02 20:22:16 +0000113 binary_fd = creat(Temp_Binary.c_str(), 0755);
114 if (binary_fd < 0) {
115 mzCloseZipArchive(Zip);
116 LOGERR("Could not create file for updater extract in '%s'\n", Temp_Binary.c_str());
117 return INSTALL_ERROR;
118 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400119
Dees_Troy2673cec2013-04-02 20:22:16 +0000120 ret_val = mzExtractZipEntryToFile(Zip, binary_location, binary_fd);
121 close(binary_fd);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400122
Dees_Troy2673cec2013-04-02 20:22:16 +0000123 if (!ret_val) {
Dees_Troy512376c2013-09-03 19:39:41 +0000124 mzCloseZipArchive(Zip);
Dees_Troy2673cec2013-04-02 20:22:16 +0000125 LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME);
126 return INSTALL_ERROR;
127 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400128
Dees_Troy512376c2013-09-03 19:39:41 +0000129 // If exists, extract file_contexts from the zip file
130 const ZipEntry* selinx_contexts = mzFindZipEntry(Zip, "file_contexts");
131 if (selinx_contexts == NULL) {
132 mzCloseZipArchive(Zip);
133 LOGINFO("Zip does not contain SELinux file_contexts file in its root.\n");
134 } else {
135 string output_filename = "/file_contexts";
136 LOGINFO("Zip contains SELinux file_contexts file in its root. Extracting to %s\n", output_filename.c_str());
137 // Delete any file_contexts
138 if (TWFunc::Path_Exists(output_filename) && unlink(output_filename.c_str()) != 0) {
139 LOGINFO("Unable to unlink '%s'\n", output_filename.c_str());
140 }
141
142 int file_contexts_fd = creat(output_filename.c_str(), 0644);
143 if (file_contexts_fd < 0) {
144 mzCloseZipArchive(Zip);
145 LOGERR("Could not extract file_contexts to '%s'\n", output_filename.c_str());
146 return INSTALL_ERROR;
147 }
148
149 ret_val = mzExtractZipEntryToFile(Zip, selinx_contexts, file_contexts_fd);
150 close(file_contexts_fd);
151
152 if (!ret_val) {
153 mzCloseZipArchive(Zip);
154 LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME);
155 return INSTALL_ERROR;
156 }
157 }
158 mzCloseZipArchive(Zip);
159
Matt Mower6883d732014-03-20 17:28:13 -0500160#ifndef TW_NO_LEGACY_PROPS
Matt Mowercdd3b332014-03-27 14:38:48 -0500161 /* Set legacy properties */
162 if (switch_to_legacy_properties() != 0) {
163 LOGERR("Legacy property environment did not initialize successfully. Properties may not be detected.\n");
164 } else {
165 LOGINFO("Legacy property environment initialized.\n");
166 }
Matt Mower6883d732014-03-20 17:28:13 -0500167#endif
Matt Mowercdd3b332014-03-27 14:38:48 -0500168
Dees_Troy2673cec2013-04-02 20:22:16 +0000169 pipe(pipe_fd);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400170
Dees_Troy2673cec2013-04-02 20:22:16 +0000171 args[0] = Temp_Binary.c_str();
172 args[1] = EXPAND(RECOVERY_API_VERSION);
173 char* temp = (char*)malloc(10);
174 sprintf(temp, "%d", pipe_fd[1]);
175 args[2] = temp;
176 args[3] = (char*)path;
177 args[4] = NULL;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400178
Dees_Troy2673cec2013-04-02 20:22:16 +0000179 pid_t pid = fork();
180 if (pid == 0) {
181 close(pipe_fd[0]);
that7e303cf2014-03-06 07:57:43 +0100182 execve(Temp_Binary.c_str(), (char* const*)args, environ);
Dees_Troy2673cec2013-04-02 20:22:16 +0000183 printf("E:Can't execute '%s'\n", Temp_Binary.c_str());
184 _exit(-1);
185 }
186 close(pipe_fd[1]);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400187
Dees_Troy2673cec2013-04-02 20:22:16 +0000188 *wipe_cache = 0;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400189
Dees_Troy2673cec2013-04-02 20:22:16 +0000190 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
191 child_data = fdopen(pipe_fd[0], "r");
192 while (fgets(buffer, sizeof(buffer), child_data) != NULL) {
193 char* command = strtok(buffer, " \n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200194 if (command == NULL) {
195 continue;
196 } else if (strcmp(command, "progress") == 0) {
197 char* fraction_char = strtok(NULL, " \n");
198 char* seconds_char = strtok(NULL, " \n");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400199
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200200 float fraction_float = strtof(fraction_char, NULL);
201 int seconds_float = strtol(seconds_char, NULL, 10);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400202
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200203 if (zip_verify)
Dees_Troy2673cec2013-04-02 20:22:16 +0000204 DataManager::ShowProgress(fraction_float * (1 - VERIFICATION_PROGRESS_FRACTION), seconds_float);
205 else
206 DataManager::ShowProgress(fraction_float, seconds_float);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200207 } else if (strcmp(command, "set_progress") == 0) {
208 char* fraction_char = strtok(NULL, " \n");
209 float fraction_float = strtof(fraction_char, NULL);
210 DataManager::SetProgress(fraction_float);
211 } else if (strcmp(command, "ui_print") == 0) {
212 char* display_value = strtok(NULL, "\n");
213 if (display_value) {
214 gui_print("%s", display_value);
215 } else {
216 gui_print("\n");
217 }
218 } else if (strcmp(command, "wipe_cache") == 0) {
219 *wipe_cache = 1;
220 } else if (strcmp(command, "clear_display") == 0) {
221 // Do nothing, not supported by TWRP
222 } else {
223 LOGERR("unknown command [%s]\n", command);
224 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000225 }
226 fclose(child_data);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400227
Dees_Troy2673cec2013-04-02 20:22:16 +0000228 waitpid(pid, &status, 0);
Matt Mowercdd3b332014-03-27 14:38:48 -0500229
Matt Mower6883d732014-03-20 17:28:13 -0500230#ifndef TW_NO_LEGACY_PROPS
Matt Mowercdd3b332014-03-27 14:38:48 -0500231 /* Unset legacy properties */
232 if (legacy_props_path_modified) {
233 if (switch_to_new_properties() != 0) {
234 LOGERR("Legacy property environment did not disable successfully. Legacy properties may still be in use.\n");
235 } else {
236 LOGINFO("Legacy property environment disabled.\n");
237 }
238 }
Matt Mower6883d732014-03-20 17:28:13 -0500239#endif
Matt Mowercdd3b332014-03-27 14:38:48 -0500240
Dees_Troy2673cec2013-04-02 20:22:16 +0000241 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
242 LOGERR("Error executing updater binary in zip '%s'\n", path);
243 return INSTALL_ERROR;
244 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400245
Dees_Troy2673cec2013-04-02 20:22:16 +0000246 return INSTALL_SUCCESS;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400247}
248
Dees_Troy32c8eb82012-09-11 15:28:06 -0400249extern "C" int TWinstall_zip(const char* path, int* wipe_cache) {
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500250 int ret_val, zip_verify = 1, md5_return, key_count;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500251 twrpDigest md5sum;
252 string strpath = path;
Dees_Troy2673cec2013-04-02 20:22:16 +0000253 ZipArchive Zip;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400254
Ethan Yonker24813422014-11-07 17:19:07 -0600255 if (strcmp(path, "error") == 0) {
256 LOGERR("Failed to get adb sideload file: '%s'\n", path);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400257 return INSTALL_CORRUPT;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500258 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400259
Ethan Yonker24813422014-11-07 17:19:07 -0600260 gui_print("Installing '%s'...\n", path);
261 if (strlen(path) < 9 || strncmp(path, "/sideload", 9) != 0) {
262 gui_print("Checking for MD5 file...\n");
263 md5sum.setfn(strpath);
264 md5_return = md5sum.verify_md5digest();
265 if (md5_return == -2) { // md5 did not match
266 LOGERR("Aborting zip install\n");
267 return INSTALL_CORRUPT;
268 }
269 }
270
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500271#ifndef TW_OEM_BUILD
Dees_Troy32c8eb82012-09-11 15:28:06 -0400272 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500273#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000274 DataManager::SetProgress(0);
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600275
276 MemMapping map;
277 if (sysMapFile(path, &map) != 0) {
278 LOGERR("Failed to sysMapFile '%s'\n", path);
279 return -1;
280 }
281
Dees_Troy32c8eb82012-09-11 15:28:06 -0400282 if (zip_verify) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000283 gui_print("Verifying zip signature...\n");
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600284 ret_val = verify_file(map.addr, map.length);
Dees_Troy2673cec2013-04-02 20:22:16 +0000285 if (ret_val != VERIFY_SUCCESS) {
286 LOGERR("Zip signature verification failed: %i\n", ret_val);
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600287 sysReleaseMap(&map);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400288 return -1;
Ethan Yonker738be7a2014-12-10 11:40:43 -0600289 } else {
290 gui_print("Zip signature verified successfully.\n");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400291 }
292 }
Ethan Yonkerf96087e2014-11-07 10:38:51 -0600293 ret_val = mzOpenZipArchive(map.addr, map.length, &Zip);
Dees_Troy2673cec2013-04-02 20:22:16 +0000294 if (ret_val != 0) {
295 LOGERR("Zip file is corrupt!\n", path);
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600296 sysReleaseMap(&map);
Dees_Troy2673cec2013-04-02 20:22:16 +0000297 return INSTALL_CORRUPT;
298 }
Ethan Yonkerf9796a42014-11-08 07:28:03 -0600299 ret_val = Run_Update_Binary(path, &Zip, wipe_cache);
300 sysReleaseMap(&map);
301 return ret_val;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400302}