blob: 4247cf7d86314b220c065cbaf3e568d6d9102cce [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"
Dees Troyb7ae0982013-09-10 20:47:35 +000034#ifdef HAVE_SELINUX
Dees_Troy32c8eb82012-09-11 15:28:06 -040035#include "minzip/SysUtil.h"
36#include "minzip/Zip.h"
Dees Troyb7ae0982013-09-10 20:47:35 +000037#else
38#include "minzipold/SysUtil.h"
39#include "minzipold/Zip.h"
40#endif
Dees_Troy32c8eb82012-09-11 15:28:06 -040041#include "mtdutils/mounts.h"
42#include "mtdutils/mtdutils.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040043#include "verifier.h"
Dees_Troy32c8eb82012-09-11 15:28:06 -040044#include "variables.h"
45#include "data.hpp"
46#include "partitions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050047#include "twrpDigest.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040048#include "twrp-functions.hpp"
Dees_Troy2673cec2013-04-02 20:22:16 +000049extern "C" {
50 #include "gui/gui.h"
that7e303cf2014-03-06 07:57:43 +010051 #include "legacy_property_service.h"
52}
53
54static const char* properties_path = "/dev/__properties__";
55static const char* properties_path_renamed = "/dev/__properties_kk__";
Matt Mowercdd3b332014-03-27 14:38:48 -050056static bool legacy_props_env_initd = false;
57static bool legacy_props_path_modified = false;
that7e303cf2014-03-06 07:57:43 +010058
Matt Mowercdd3b332014-03-27 14:38:48 -050059static int switch_to_legacy_properties()
that7e303cf2014-03-06 07:57:43 +010060{
Matt Mowercdd3b332014-03-27 14:38:48 -050061 if (!legacy_props_env_initd) {
62 if (legacy_properties_init() != 0)
63 return -1;
64
65 char tmp[32];
66 int propfd, propsz;
67 legacy_get_property_workspace(&propfd, &propsz);
68 sprintf(tmp, "%d,%d", dup(propfd), propsz);
69 setenv("ANDROID_PROPERTY_WORKSPACE", tmp, 1);
70 legacy_props_env_initd = true;
71 }
that7e303cf2014-03-06 07:57:43 +010072
73 if (TWFunc::Path_Exists(properties_path)) {
74 // hide real properties so that the updater uses the envvar to find the legacy format properties
Matt Mowercdd3b332014-03-27 14:38:48 -050075 if (rename(properties_path, properties_path_renamed) != 0) {
76 LOGERR("Renaming %s failed: %s\n", properties_path, strerror(errno));
77 return -1;
78 } else {
79 legacy_props_path_modified = true;
80 }
that7e303cf2014-03-06 07:57:43 +010081 }
Matt Mowercdd3b332014-03-27 14:38:48 -050082
83 return 0;
that7e303cf2014-03-06 07:57:43 +010084}
85
Matt Mowercdd3b332014-03-27 14:38:48 -050086static int switch_to_new_properties()
that7e303cf2014-03-06 07:57:43 +010087{
88 if (TWFunc::Path_Exists(properties_path_renamed)) {
Matt Mowercdd3b332014-03-27 14:38:48 -050089 if (rename(properties_path_renamed, properties_path) != 0) {
90 LOGERR("Renaming %s failed: %s\n", properties_path_renamed, strerror(errno));
91 return -1;
92 } else {
93 legacy_props_path_modified = false;
94 }
that7e303cf2014-03-06 07:57:43 +010095 }
Matt Mowercdd3b332014-03-27 14:38:48 -050096
97 return 0;
Dees_Troy2673cec2013-04-02 20:22:16 +000098}
Dees_Troy32c8eb82012-09-11 15:28:06 -040099
Dees_Troy2673cec2013-04-02 20:22:16 +0000100static int Run_Update_Binary(const char *path, ZipArchive *Zip, int* wipe_cache) {
101 const ZipEntry* binary_location = mzFindZipEntry(Zip, ASSUMED_UPDATE_BINARY_NAME);
102 string Temp_Binary = "/tmp/updater";
103 int binary_fd, ret_val, pipe_fd[2], status, zip_verify;
104 char buffer[1024];
105 const char** args = (const char**)malloc(sizeof(char*) * 5);
106 FILE* child_data;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400107
Dees_Troy2673cec2013-04-02 20:22:16 +0000108 if (binary_location == NULL) {
109 mzCloseZipArchive(Zip);
110 return INSTALL_CORRUPT;
111 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400112
Dees_Troy2673cec2013-04-02 20:22:16 +0000113 // Delete any existing updater
114 if (TWFunc::Path_Exists(Temp_Binary) && unlink(Temp_Binary.c_str()) != 0) {
115 LOGINFO("Unable to unlink '%s'\n", Temp_Binary.c_str());
116 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400117
Dees_Troy2673cec2013-04-02 20:22:16 +0000118 binary_fd = creat(Temp_Binary.c_str(), 0755);
119 if (binary_fd < 0) {
120 mzCloseZipArchive(Zip);
121 LOGERR("Could not create file for updater extract in '%s'\n", Temp_Binary.c_str());
122 return INSTALL_ERROR;
123 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400124
Dees_Troy2673cec2013-04-02 20:22:16 +0000125 ret_val = mzExtractZipEntryToFile(Zip, binary_location, binary_fd);
126 close(binary_fd);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400127
Dees_Troy2673cec2013-04-02 20:22:16 +0000128 if (!ret_val) {
Dees_Troy512376c2013-09-03 19:39:41 +0000129 mzCloseZipArchive(Zip);
Dees_Troy2673cec2013-04-02 20:22:16 +0000130 LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME);
131 return INSTALL_ERROR;
132 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400133
Dees_Troy512376c2013-09-03 19:39:41 +0000134 // If exists, extract file_contexts from the zip file
135 const ZipEntry* selinx_contexts = mzFindZipEntry(Zip, "file_contexts");
136 if (selinx_contexts == NULL) {
137 mzCloseZipArchive(Zip);
138 LOGINFO("Zip does not contain SELinux file_contexts file in its root.\n");
139 } else {
140 string output_filename = "/file_contexts";
141 LOGINFO("Zip contains SELinux file_contexts file in its root. Extracting to %s\n", output_filename.c_str());
142 // Delete any file_contexts
143 if (TWFunc::Path_Exists(output_filename) && unlink(output_filename.c_str()) != 0) {
144 LOGINFO("Unable to unlink '%s'\n", output_filename.c_str());
145 }
146
147 int file_contexts_fd = creat(output_filename.c_str(), 0644);
148 if (file_contexts_fd < 0) {
149 mzCloseZipArchive(Zip);
150 LOGERR("Could not extract file_contexts to '%s'\n", output_filename.c_str());
151 return INSTALL_ERROR;
152 }
153
154 ret_val = mzExtractZipEntryToFile(Zip, selinx_contexts, file_contexts_fd);
155 close(file_contexts_fd);
156
157 if (!ret_val) {
158 mzCloseZipArchive(Zip);
159 LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME);
160 return INSTALL_ERROR;
161 }
162 }
163 mzCloseZipArchive(Zip);
164
Matt Mower6883d732014-03-20 17:28:13 -0500165#ifndef TW_NO_LEGACY_PROPS
Matt Mowercdd3b332014-03-27 14:38:48 -0500166 /* Set legacy properties */
167 if (switch_to_legacy_properties() != 0) {
168 LOGERR("Legacy property environment did not initialize successfully. Properties may not be detected.\n");
169 } else {
170 LOGINFO("Legacy property environment initialized.\n");
171 }
Matt Mower6883d732014-03-20 17:28:13 -0500172#endif
Matt Mowercdd3b332014-03-27 14:38:48 -0500173
Dees_Troy2673cec2013-04-02 20:22:16 +0000174 pipe(pipe_fd);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400175
Dees_Troy2673cec2013-04-02 20:22:16 +0000176 args[0] = Temp_Binary.c_str();
177 args[1] = EXPAND(RECOVERY_API_VERSION);
178 char* temp = (char*)malloc(10);
179 sprintf(temp, "%d", pipe_fd[1]);
180 args[2] = temp;
181 args[3] = (char*)path;
182 args[4] = NULL;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400183
Dees_Troy2673cec2013-04-02 20:22:16 +0000184 pid_t pid = fork();
185 if (pid == 0) {
186 close(pipe_fd[0]);
that7e303cf2014-03-06 07:57:43 +0100187 execve(Temp_Binary.c_str(), (char* const*)args, environ);
Dees_Troy2673cec2013-04-02 20:22:16 +0000188 printf("E:Can't execute '%s'\n", Temp_Binary.c_str());
189 _exit(-1);
190 }
191 close(pipe_fd[1]);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400192
Dees_Troy2673cec2013-04-02 20:22:16 +0000193 *wipe_cache = 0;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400194
Dees_Troy2673cec2013-04-02 20:22:16 +0000195 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
196 child_data = fdopen(pipe_fd[0], "r");
197 while (fgets(buffer, sizeof(buffer), child_data) != NULL) {
198 char* command = strtok(buffer, " \n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200199 if (command == NULL) {
200 continue;
201 } else if (strcmp(command, "progress") == 0) {
202 char* fraction_char = strtok(NULL, " \n");
203 char* seconds_char = strtok(NULL, " \n");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400204
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200205 float fraction_float = strtof(fraction_char, NULL);
206 int seconds_float = strtol(seconds_char, NULL, 10);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400207
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200208 if (zip_verify)
Dees_Troy2673cec2013-04-02 20:22:16 +0000209 DataManager::ShowProgress(fraction_float * (1 - VERIFICATION_PROGRESS_FRACTION), seconds_float);
210 else
211 DataManager::ShowProgress(fraction_float, seconds_float);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200212 } else if (strcmp(command, "set_progress") == 0) {
213 char* fraction_char = strtok(NULL, " \n");
214 float fraction_float = strtof(fraction_char, NULL);
215 DataManager::SetProgress(fraction_float);
216 } else if (strcmp(command, "ui_print") == 0) {
217 char* display_value = strtok(NULL, "\n");
218 if (display_value) {
219 gui_print("%s", display_value);
220 } else {
221 gui_print("\n");
222 }
223 } else if (strcmp(command, "wipe_cache") == 0) {
224 *wipe_cache = 1;
225 } else if (strcmp(command, "clear_display") == 0) {
226 // Do nothing, not supported by TWRP
227 } else {
228 LOGERR("unknown command [%s]\n", command);
229 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000230 }
231 fclose(child_data);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400232
Dees_Troy2673cec2013-04-02 20:22:16 +0000233 waitpid(pid, &status, 0);
Matt Mowercdd3b332014-03-27 14:38:48 -0500234
Matt Mower6883d732014-03-20 17:28:13 -0500235#ifndef TW_NO_LEGACY_PROPS
Matt Mowercdd3b332014-03-27 14:38:48 -0500236 /* Unset legacy properties */
237 if (legacy_props_path_modified) {
238 if (switch_to_new_properties() != 0) {
239 LOGERR("Legacy property environment did not disable successfully. Legacy properties may still be in use.\n");
240 } else {
241 LOGINFO("Legacy property environment disabled.\n");
242 }
243 }
Matt Mower6883d732014-03-20 17:28:13 -0500244#endif
Matt Mowercdd3b332014-03-27 14:38:48 -0500245
Dees_Troy2673cec2013-04-02 20:22:16 +0000246 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
247 LOGERR("Error executing updater binary in zip '%s'\n", path);
248 return INSTALL_ERROR;
249 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400250
Dees_Troy2673cec2013-04-02 20:22:16 +0000251 return INSTALL_SUCCESS;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400252}
253
Dees_Troy32c8eb82012-09-11 15:28:06 -0400254extern "C" int TWinstall_zip(const char* path, int* wipe_cache) {
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500255 int ret_val, zip_verify = 1, md5_return, key_count;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500256 twrpDigest md5sum;
257 string strpath = path;
Dees_Troy2673cec2013-04-02 20:22:16 +0000258 ZipArchive Zip;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400259
Dees_Troy2673cec2013-04-02 20:22:16 +0000260 gui_print("Installing '%s'...\nChecking for MD5 file...\n", path);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500261 md5sum.setfn(strpath);
262 md5_return = md5sum.verify_md5digest();
Matt Mowerd5c1a922014-04-15 12:50:58 -0500263 if (md5_return == -2) { // md5 did not match
264 LOGERR("Aborting zip install\n");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400265 return INSTALL_CORRUPT;
Matt Mowerd5c1a922014-04-15 12:50:58 -0500266 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400267
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500268#ifndef TW_OEM_BUILD
Dees_Troy32c8eb82012-09-11 15:28:06 -0400269 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
Ethan Yonkerd5801c52014-04-14 08:59:35 -0500270#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000271 DataManager::SetProgress(0);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400272 if (zip_verify) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000273 gui_print("Verifying zip signature...\n");
274 ret_val = verify_file(path);
275 if (ret_val != VERIFY_SUCCESS) {
276 LOGERR("Zip signature verification failed: %i\n", ret_val);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400277 return -1;
278 }
279 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000280 ret_val = mzOpenZipArchive(path, &Zip);
281 if (ret_val != 0) {
282 LOGERR("Zip file is corrupt!\n", path);
283 return INSTALL_CORRUPT;
284 }
285 return Run_Update_Binary(path, &Zip, wipe_cache);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400286}