blob: af12ce36f0d4ffadadfe26f0ec539637357d1e0e [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"
51}
Dees_Troy32c8eb82012-09-11 15:28:06 -040052
Dees_Troy2673cec2013-04-02 20:22:16 +000053static int Run_Update_Binary(const char *path, ZipArchive *Zip, int* wipe_cache) {
54 const ZipEntry* binary_location = mzFindZipEntry(Zip, ASSUMED_UPDATE_BINARY_NAME);
55 string Temp_Binary = "/tmp/updater";
56 int binary_fd, ret_val, pipe_fd[2], status, zip_verify;
57 char buffer[1024];
58 const char** args = (const char**)malloc(sizeof(char*) * 5);
59 FILE* child_data;
Dees_Troy32c8eb82012-09-11 15:28:06 -040060
Dees_Troy2673cec2013-04-02 20:22:16 +000061 if (binary_location == NULL) {
62 mzCloseZipArchive(Zip);
63 return INSTALL_CORRUPT;
64 }
Dees_Troy32c8eb82012-09-11 15:28:06 -040065
Dees_Troy2673cec2013-04-02 20:22:16 +000066 // Delete any existing updater
67 if (TWFunc::Path_Exists(Temp_Binary) && unlink(Temp_Binary.c_str()) != 0) {
68 LOGINFO("Unable to unlink '%s'\n", Temp_Binary.c_str());
69 }
Dees_Troy32c8eb82012-09-11 15:28:06 -040070
Dees_Troy2673cec2013-04-02 20:22:16 +000071 binary_fd = creat(Temp_Binary.c_str(), 0755);
72 if (binary_fd < 0) {
73 mzCloseZipArchive(Zip);
74 LOGERR("Could not create file for updater extract in '%s'\n", Temp_Binary.c_str());
75 return INSTALL_ERROR;
76 }
Dees_Troy32c8eb82012-09-11 15:28:06 -040077
Dees_Troy2673cec2013-04-02 20:22:16 +000078 ret_val = mzExtractZipEntryToFile(Zip, binary_location, binary_fd);
79 close(binary_fd);
Dees_Troy32c8eb82012-09-11 15:28:06 -040080
Dees_Troy2673cec2013-04-02 20:22:16 +000081 if (!ret_val) {
Dees_Troy512376c2013-09-03 19:39:41 +000082 mzCloseZipArchive(Zip);
Dees_Troy2673cec2013-04-02 20:22:16 +000083 LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME);
84 return INSTALL_ERROR;
85 }
Dees_Troy32c8eb82012-09-11 15:28:06 -040086
Dees_Troy512376c2013-09-03 19:39:41 +000087 // If exists, extract file_contexts from the zip file
88 const ZipEntry* selinx_contexts = mzFindZipEntry(Zip, "file_contexts");
89 if (selinx_contexts == NULL) {
90 mzCloseZipArchive(Zip);
91 LOGINFO("Zip does not contain SELinux file_contexts file in its root.\n");
92 } else {
93 string output_filename = "/file_contexts";
94 LOGINFO("Zip contains SELinux file_contexts file in its root. Extracting to %s\n", output_filename.c_str());
95 // Delete any file_contexts
96 if (TWFunc::Path_Exists(output_filename) && unlink(output_filename.c_str()) != 0) {
97 LOGINFO("Unable to unlink '%s'\n", output_filename.c_str());
98 }
99
100 int file_contexts_fd = creat(output_filename.c_str(), 0644);
101 if (file_contexts_fd < 0) {
102 mzCloseZipArchive(Zip);
103 LOGERR("Could not extract file_contexts to '%s'\n", output_filename.c_str());
104 return INSTALL_ERROR;
105 }
106
107 ret_val = mzExtractZipEntryToFile(Zip, selinx_contexts, file_contexts_fd);
108 close(file_contexts_fd);
109
110 if (!ret_val) {
111 mzCloseZipArchive(Zip);
112 LOGERR("Could not extract '%s'\n", ASSUMED_UPDATE_BINARY_NAME);
113 return INSTALL_ERROR;
114 }
115 }
116 mzCloseZipArchive(Zip);
117
Dees_Troy2673cec2013-04-02 20:22:16 +0000118 pipe(pipe_fd);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400119
Dees_Troy2673cec2013-04-02 20:22:16 +0000120 args[0] = Temp_Binary.c_str();
121 args[1] = EXPAND(RECOVERY_API_VERSION);
122 char* temp = (char*)malloc(10);
123 sprintf(temp, "%d", pipe_fd[1]);
124 args[2] = temp;
125 args[3] = (char*)path;
126 args[4] = NULL;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400127
Dees_Troy2673cec2013-04-02 20:22:16 +0000128 pid_t pid = fork();
129 if (pid == 0) {
130 close(pipe_fd[0]);
131 execv(Temp_Binary.c_str(), (char* const*)args);
132 printf("E:Can't execute '%s'\n", Temp_Binary.c_str());
133 _exit(-1);
134 }
135 close(pipe_fd[1]);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400136
Dees_Troy2673cec2013-04-02 20:22:16 +0000137 *wipe_cache = 0;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400138
Dees_Troy2673cec2013-04-02 20:22:16 +0000139 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
140 child_data = fdopen(pipe_fd[0], "r");
141 while (fgets(buffer, sizeof(buffer), child_data) != NULL) {
142 char* command = strtok(buffer, " \n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200143 if (command == NULL) {
144 continue;
145 } else if (strcmp(command, "progress") == 0) {
146 char* fraction_char = strtok(NULL, " \n");
147 char* seconds_char = strtok(NULL, " \n");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400148
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200149 float fraction_float = strtof(fraction_char, NULL);
150 int seconds_float = strtol(seconds_char, NULL, 10);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400151
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200152 if (zip_verify)
Dees_Troy2673cec2013-04-02 20:22:16 +0000153 DataManager::ShowProgress(fraction_float * (1 - VERIFICATION_PROGRESS_FRACTION), seconds_float);
154 else
155 DataManager::ShowProgress(fraction_float, seconds_float);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 } else if (strcmp(command, "set_progress") == 0) {
157 char* fraction_char = strtok(NULL, " \n");
158 float fraction_float = strtof(fraction_char, NULL);
159 DataManager::SetProgress(fraction_float);
160 } else if (strcmp(command, "ui_print") == 0) {
161 char* display_value = strtok(NULL, "\n");
162 if (display_value) {
163 gui_print("%s", display_value);
164 } else {
165 gui_print("\n");
166 }
167 } else if (strcmp(command, "wipe_cache") == 0) {
168 *wipe_cache = 1;
169 } else if (strcmp(command, "clear_display") == 0) {
170 // Do nothing, not supported by TWRP
171 } else {
172 LOGERR("unknown command [%s]\n", command);
173 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000174 }
175 fclose(child_data);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400176
Dees_Troy2673cec2013-04-02 20:22:16 +0000177 waitpid(pid, &status, 0);
178 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
179 LOGERR("Error executing updater binary in zip '%s'\n", path);
180 return INSTALL_ERROR;
181 }
Dees_Troy32c8eb82012-09-11 15:28:06 -0400182
Dees_Troy2673cec2013-04-02 20:22:16 +0000183 return INSTALL_SUCCESS;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400184}
185
Dees_Troy32c8eb82012-09-11 15:28:06 -0400186extern "C" int TWinstall_zip(const char* path, int* wipe_cache) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000187 int ret_val, zip_verify, md5_return, key_count;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500188 twrpDigest md5sum;
189 string strpath = path;
Dees_Troy2673cec2013-04-02 20:22:16 +0000190 ZipArchive Zip;
Dees_Troy32c8eb82012-09-11 15:28:06 -0400191
Dees_Troy2673cec2013-04-02 20:22:16 +0000192 gui_print("Installing '%s'...\nChecking for MD5 file...\n", path);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500193 md5sum.setfn(strpath);
194 md5_return = md5sum.verify_md5digest();
195 if (md5_return == -2) {
Dees_Troy32c8eb82012-09-11 15:28:06 -0400196 // MD5 did not match.
Dees_Troy2673cec2013-04-02 20:22:16 +0000197 LOGERR("Zip MD5 does not match.\nUnable to install zip.\n");
Dees_Troy32c8eb82012-09-11 15:28:06 -0400198 return INSTALL_CORRUPT;
199 } else if (md5_return == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000200 gui_print("Skipping MD5 check: no MD5 file found.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500201 } else if (md5_return == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000202 gui_print("Zip MD5 matched.\n"); // MD5 found and matched.
Dees_Troy32c8eb82012-09-11 15:28:06 -0400203
204 DataManager::GetValue(TW_SIGNED_ZIP_VERIFY_VAR, zip_verify);
Dees_Troy2673cec2013-04-02 20:22:16 +0000205 DataManager::SetProgress(0);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400206 if (zip_verify) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000207 gui_print("Verifying zip signature...\n");
208 ret_val = verify_file(path);
209 if (ret_val != VERIFY_SUCCESS) {
210 LOGERR("Zip signature verification failed: %i\n", ret_val);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400211 return -1;
212 }
213 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000214 ret_val = mzOpenZipArchive(path, &Zip);
215 if (ret_val != 0) {
216 LOGERR("Zip file is corrupt!\n", path);
217 return INSTALL_CORRUPT;
218 }
219 return Run_Update_Binary(path, &Zip, wipe_cache);
Dees_Troy32c8eb82012-09-11 15:28:06 -0400220}