blob: e82f15a7dedfac9370203ffe0153e2da13e2daa2 [file] [log] [blame]
Idries Hamadied409ea2018-01-29 16:30:36 +00001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Idries Hamadi269a4b42018-09-13 18:00:25 +010017#include "fastdeploy.h"
18
Idries Hamadied409ea2018-01-29 16:30:36 +000019#include <algorithm>
Idries Hamadi5b6bf942018-08-28 12:58:09 +010020#include <array>
Idries Hamadi269a4b42018-09-13 18:00:25 +010021#include <memory>
Idries Hamadied409ea2018-01-29 16:30:36 +000022
Idries Hamadi5b6bf942018-08-28 12:58:09 +010023#include "android-base/file.h"
24#include "android-base/strings.h"
Idries Hamadi269a4b42018-09-13 18:00:25 +010025#include "androidfw/ResourceTypes.h"
26#include "androidfw/ZipFileRO.h"
Idries Hamadied409ea2018-01-29 16:30:36 +000027#include "client/file_sync_client.h"
28#include "commandline.h"
Idries Hamadied409ea2018-01-29 16:30:36 +000029#include "fastdeploycallbacks.h"
Elliott Hughes4679a392018-10-19 13:59:44 -070030#include "sysdeps.h"
Elliott Hughesd2aaede2018-10-22 17:02:51 -070031
32#include "adb_utils.h"
Idries Hamadied409ea2018-01-29 16:30:36 +000033
Elliott Hughes86ab9ff2018-09-05 12:13:11 -070034static constexpr long kRequiredAgentVersion = 0x00000001;
Idries Hamadied409ea2018-01-29 16:30:36 +000035
Elliott Hughes86ab9ff2018-09-05 12:13:11 -070036static constexpr const char* kDeviceAgentPath = "/data/local/tmp/";
Idries Hamadied409ea2018-01-29 16:30:36 +000037
Idries Hamadi4af6ee42018-09-06 18:42:39 +010038static bool g_use_localagent = false;
Idries Hamadi5b6bf942018-08-28 12:58:09 +010039
Idries Hamadied409ea2018-01-29 16:30:36 +000040long get_agent_version() {
41 std::vector<char> versionOutputBuffer;
42 std::vector<char> versionErrorBuffer;
43
Idries Hamadi7575cd92018-08-24 11:46:45 +010044 int statusCode = capture_shell_command("/data/local/tmp/deployagent version",
Idries Hamadied409ea2018-01-29 16:30:36 +000045 &versionOutputBuffer, &versionErrorBuffer);
46 long version = -1;
47
48 if (statusCode == 0 && versionOutputBuffer.size() > 0) {
49 version = strtol((char*)versionOutputBuffer.data(), NULL, 16);
50 }
51
52 return version;
53}
54
55int get_device_api_level() {
56 std::vector<char> sdkVersionOutputBuffer;
57 std::vector<char> sdkVersionErrorBuffer;
58 int api_level = -1;
59
60 int statusCode = capture_shell_command("getprop ro.build.version.sdk", &sdkVersionOutputBuffer,
61 &sdkVersionErrorBuffer);
Idries Hamadi7f51e002018-08-23 17:22:21 +010062 if (statusCode == 0 && sdkVersionOutputBuffer.size() > 0) {
Idries Hamadied409ea2018-01-29 16:30:36 +000063 api_level = strtol((char*)sdkVersionOutputBuffer.data(), NULL, 10);
64 }
65
66 return api_level;
67}
68
Idries Hamadi4af6ee42018-09-06 18:42:39 +010069void fastdeploy_set_local_agent(bool use_localagent) {
70 g_use_localagent = use_localagent;
Idries Hamadi5b6bf942018-08-28 12:58:09 +010071}
72
Idries Hamadied409ea2018-01-29 16:30:36 +000073// local_path - must start with a '/' and be relative to $ANDROID_PRODUCT_OUT
Idries Hamadi4af6ee42018-09-06 18:42:39 +010074static std::string get_agent_component_host_path(const char* local_path, const char* sdk_path) {
Idries Hamadi5b6bf942018-08-28 12:58:09 +010075 std::string adb_dir = android::base::GetExecutableDirectory();
76 if (adb_dir.empty()) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -070077 error_exit("Could not determine location of adb!");
Idries Hamadied409ea2018-01-29 16:30:36 +000078 }
79
Idries Hamadi4af6ee42018-09-06 18:42:39 +010080 if (g_use_localagent) {
Idries Hamadied409ea2018-01-29 16:30:36 +000081 const char* product_out = getenv("ANDROID_PRODUCT_OUT");
82 if (product_out == nullptr) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -070083 error_exit("Could not locate %s because $ANDROID_PRODUCT_OUT is not defined",
84 local_path);
Idries Hamadied409ea2018-01-29 16:30:36 +000085 }
Idries Hamadi4af6ee42018-09-06 18:42:39 +010086 return android::base::StringPrintf("%s%s", product_out, local_path);
Idries Hamadied409ea2018-01-29 16:30:36 +000087 } else {
Idries Hamadi4af6ee42018-09-06 18:42:39 +010088 return adb_dir + sdk_path;
Idries Hamadied409ea2018-01-29 16:30:36 +000089 }
Idries Hamadied409ea2018-01-29 16:30:36 +000090}
91
Idries Hamadi5b6bf942018-08-28 12:58:09 +010092static bool deploy_agent(bool checkTimeStamps) {
Idries Hamadied409ea2018-01-29 16:30:36 +000093 std::vector<const char*> srcs;
Idries Hamadi4af6ee42018-09-06 18:42:39 +010094 std::string jar_path =
95 get_agent_component_host_path("/system/framework/deployagent.jar", "/deployagent.jar");
96 std::string script_path =
97 get_agent_component_host_path("/system/bin/deployagent", "/deployagent");
98 srcs.push_back(jar_path.c_str());
99 srcs.push_back(script_path.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000100
101 if (do_sync_push(srcs, kDeviceAgentPath, checkTimeStamps)) {
102 // on windows the shell script might have lost execute permission
103 // so need to set this explicitly
Idries Hamadi7575cd92018-08-24 11:46:45 +0100104 const char* kChmodCommandPattern = "chmod 777 %sdeployagent";
Idries Hamadied409ea2018-01-29 16:30:36 +0000105 std::string chmodCommand =
106 android::base::StringPrintf(kChmodCommandPattern, kDeviceAgentPath);
Idries Hamadi7f51e002018-08-23 17:22:21 +0100107 int ret = send_shell_command(chmodCommand);
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100108 if (ret != 0) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700109 error_exit("Error executing %s returncode: %d", chmodCommand.c_str(), ret);
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100110 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000111 } else {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700112 error_exit("Error pushing agent files to device");
Idries Hamadied409ea2018-01-29 16:30:36 +0000113 }
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100114
115 return true;
Idries Hamadied409ea2018-01-29 16:30:36 +0000116}
117
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100118void update_agent(FastDeploy_AgentUpdateStrategy agentUpdateStrategy) {
Idries Hamadied409ea2018-01-29 16:30:36 +0000119 long agent_version = get_agent_version();
120 switch (agentUpdateStrategy) {
121 case FastDeploy_AgentUpdateAlways:
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100122 deploy_agent(false);
Idries Hamadied409ea2018-01-29 16:30:36 +0000123 break;
124 case FastDeploy_AgentUpdateNewerTimeStamp:
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100125 deploy_agent(true);
Idries Hamadied409ea2018-01-29 16:30:36 +0000126 break;
127 case FastDeploy_AgentUpdateDifferentVersion:
128 if (agent_version != kRequiredAgentVersion) {
129 if (agent_version < 0) {
130 printf("Could not detect agent on device, deploying\n");
131 } else {
132 printf("Device agent version is (%ld), (%ld) is required, re-deploying\n",
133 agent_version, kRequiredAgentVersion);
134 }
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100135 deploy_agent(false);
Idries Hamadied409ea2018-01-29 16:30:36 +0000136 }
137 break;
138 }
139
140 agent_version = get_agent_version();
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100141 if (agent_version != kRequiredAgentVersion) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700142 error_exit("After update agent version remains incorrect! Expected %ld but version is %ld",
143 kRequiredAgentVersion, agent_version);
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100144 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000145}
146
Idries Hamadi269a4b42018-09-13 18:00:25 +0100147static std::string get_string_from_utf16(const char16_t* input, int input_len) {
148 ssize_t utf8_length = utf16_to_utf8_length(input, input_len);
149 if (utf8_length <= 0) {
150 return {};
Idries Hamadied409ea2018-01-29 16:30:36 +0000151 }
Idries Hamadi269a4b42018-09-13 18:00:25 +0100152 std::string utf8;
153 utf8.resize(utf8_length);
154 utf16_to_utf8(input, input_len, &*utf8.begin(), utf8_length + 1);
155 return utf8;
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100156}
157
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100158static std::string get_packagename_from_apk(const char* apkPath) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700159#undef open
Idries Hamadi269a4b42018-09-13 18:00:25 +0100160 std::unique_ptr<android::ZipFileRO> zipFile(android::ZipFileRO::open(apkPath));
Elliott Hughes4679a392018-10-19 13:59:44 -0700161#define open ___xxx_unix_open
Idries Hamadi269a4b42018-09-13 18:00:25 +0100162 if (zipFile == nullptr) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700163 perror_exit("Could not open %s", apkPath);
Idries Hamadied409ea2018-01-29 16:30:36 +0000164 }
Idries Hamadi269a4b42018-09-13 18:00:25 +0100165 android::ZipEntryRO entry = zipFile->findEntryByName("AndroidManifest.xml");
166 if (entry == nullptr) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700167 error_exit("Could not find AndroidManifest.xml inside %s", apkPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100168 }
169 uint32_t manifest_len = 0;
170 if (!zipFile->getEntryInfo(entry, NULL, &manifest_len, NULL, NULL, NULL, NULL)) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700171 error_exit("Could not read AndroidManifest.xml inside %s", apkPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100172 }
173 std::vector<char> manifest_data(manifest_len);
174 if (!zipFile->uncompressEntry(entry, manifest_data.data(), manifest_len)) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700175 error_exit("Could not uncompress AndroidManifest.xml inside %s", apkPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100176 }
177 android::ResXMLTree tree;
178 android::status_t setto_status = tree.setTo(manifest_data.data(), manifest_len, true);
179 if (setto_status != android::OK) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700180 error_exit("Could not parse AndroidManifest.xml inside %s", apkPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100181 }
182 android::ResXMLParser::event_code_t code;
183 while ((code = tree.next()) != android::ResXMLParser::BAD_DOCUMENT &&
184 code != android::ResXMLParser::END_DOCUMENT) {
185 switch (code) {
186 case android::ResXMLParser::START_TAG: {
187 size_t element_name_length;
188 const char16_t* element_name = tree.getElementName(&element_name_length);
189 if (element_name == nullptr) {
190 continue;
191 }
192 std::u16string element_name_string(element_name, element_name_length);
193 if (element_name_string == u"manifest") {
194 for (size_t i = 0; i < tree.getAttributeCount(); i++) {
195 size_t attribute_name_length;
196 const char16_t* attribute_name_text =
197 tree.getAttributeName(i, &attribute_name_length);
198 if (attribute_name_text == nullptr) {
199 continue;
200 }
201 std::u16string attribute_name_string(attribute_name_text,
202 attribute_name_length);
203 if (attribute_name_string == u"package") {
204 size_t attribute_value_length;
205 const char16_t* attribute_value_text =
206 tree.getAttributeStringValue(i, &attribute_value_length);
207 if (attribute_value_text == nullptr) {
208 continue;
209 }
210 return get_string_from_utf16(attribute_value_text,
211 attribute_value_length);
212 }
213 }
214 }
215 break;
216 }
217 default:
218 break;
219 }
220 }
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700221 error_exit("Could not find package name tag in AndroidManifest.xml inside %s", apkPath);
Idries Hamadied409ea2018-01-29 16:30:36 +0000222}
223
Idries Hamadi269a4b42018-09-13 18:00:25 +0100224void extract_metadata(const char* apkPath, FILE* outputFp) {
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100225 std::string packageName = get_packagename_from_apk(apkPath);
Idries Hamadi7575cd92018-08-24 11:46:45 +0100226 const char* kAgentExtractCommandPattern = "/data/local/tmp/deployagent extract %s";
Idries Hamadied409ea2018-01-29 16:30:36 +0000227 std::string extractCommand =
228 android::base::StringPrintf(kAgentExtractCommandPattern, packageName.c_str());
229
230 std::vector<char> extractErrorBuffer;
Henry Daitxf21edf32018-11-30 18:02:43 +0000231 DeployAgentFileCallback cb(outputFp, &extractErrorBuffer);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100232 int returnCode = send_shell_command(extractCommand, false, &cb);
233 if (returnCode != 0) {
Henry Daitxf21edf32018-11-30 18:02:43 +0000234 fprintf(stderr, "Executing %s returned %d\n", extractCommand.c_str(), returnCode);
235 fprintf(stderr, "%*s\n", int(extractErrorBuffer.size()), extractErrorBuffer.data());
236 error_exit("Aborting");
Idries Hamadied409ea2018-01-29 16:30:36 +0000237 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000238}
239
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100240static std::string get_patch_generator_command() {
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100241 if (g_use_localagent) {
Idries Hamadied409ea2018-01-29 16:30:36 +0000242 // This should never happen on a Windows machine
Idries Hamadied409ea2018-01-29 16:30:36 +0000243 const char* host_out = getenv("ANDROID_HOST_OUT");
244 if (host_out == nullptr) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700245 error_exit(
246 "Could not locate deploypatchgenerator.jar because $ANDROID_HOST_OUT "
247 "is not defined");
Idries Hamadied409ea2018-01-29 16:30:36 +0000248 }
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100249 return android::base::StringPrintf("java -jar %s/framework/deploypatchgenerator.jar",
250 host_out);
Idries Hamadied409ea2018-01-29 16:30:36 +0000251 }
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100252
253 std::string adb_dir = android::base::GetExecutableDirectory();
254 if (adb_dir.empty()) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700255 error_exit("Could not locate deploypatchgenerator.jar");
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100256 }
257 return android::base::StringPrintf(R"(java -jar "%s/deploypatchgenerator.jar")",
258 adb_dir.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000259}
260
Idries Hamadi269a4b42018-09-13 18:00:25 +0100261void create_patch(const char* apkPath, const char* metadataPath, const char* patchPath) {
Idries Hamadied409ea2018-01-29 16:30:36 +0000262 std::string generatePatchCommand = android::base::StringPrintf(
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100263 R"(%s "%s" "%s" > "%s")", get_patch_generator_command().c_str(), apkPath, metadataPath,
264 patchPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100265 int returnCode = system(generatePatchCommand.c_str());
266 if (returnCode != 0) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700267 error_exit("Executing %s returned %d", generatePatchCommand.c_str(), returnCode);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100268 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000269}
270
271std::string get_patch_path(const char* apkPath) {
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100272 std::string packageName = get_packagename_from_apk(apkPath);
Idries Hamadied409ea2018-01-29 16:30:36 +0000273 std::string patchDevicePath =
274 android::base::StringPrintf("%s%s.patch", kDeviceAgentPath, packageName.c_str());
275 return patchDevicePath;
276}
277
Idries Hamadi269a4b42018-09-13 18:00:25 +0100278void apply_patch_on_device(const char* apkPath, const char* patchPath, const char* outputPath) {
Idries Hamadi7575cd92018-08-24 11:46:45 +0100279 const std::string kAgentApplyCommandPattern = "/data/local/tmp/deployagent apply %s %s -o %s";
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100280 std::string packageName = get_packagename_from_apk(apkPath);
Idries Hamadied409ea2018-01-29 16:30:36 +0000281 std::string patchDevicePath = get_patch_path(apkPath);
282
283 std::vector<const char*> srcs = {patchPath};
284 bool push_ok = do_sync_push(srcs, patchDevicePath.c_str(), false);
Idries Hamadied409ea2018-01-29 16:30:36 +0000285 if (!push_ok) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700286 error_exit("Error pushing %s to %s returned", patchPath, patchDevicePath.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000287 }
288
289 std::string applyPatchCommand =
290 android::base::StringPrintf(kAgentApplyCommandPattern.c_str(), packageName.c_str(),
291 patchDevicePath.c_str(), outputPath);
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100292
Idries Hamadi269a4b42018-09-13 18:00:25 +0100293 int returnCode = send_shell_command(applyPatchCommand);
294 if (returnCode != 0) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700295 error_exit("Executing %s returned %d", applyPatchCommand.c_str(), returnCode);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100296 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000297}
298
Idries Hamadi269a4b42018-09-13 18:00:25 +0100299void install_patch(const char* apkPath, const char* patchPath, int argc, const char** argv) {
Idries Hamadi7575cd92018-08-24 11:46:45 +0100300 const std::string kAgentApplyCommandPattern = "/data/local/tmp/deployagent apply %s %s -pm %s";
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100301 std::string packageName = get_packagename_from_apk(apkPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100302
Idries Hamadied409ea2018-01-29 16:30:36 +0000303 std::string patchDevicePath =
304 android::base::StringPrintf("%s%s.patch", kDeviceAgentPath, packageName.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000305
Idries Hamadi269a4b42018-09-13 18:00:25 +0100306 std::vector<const char*> srcs{patchPath};
307 bool push_ok = do_sync_push(srcs, patchDevicePath.c_str(), false);
Idries Hamadied409ea2018-01-29 16:30:36 +0000308 if (!push_ok) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700309 error_exit("Error pushing %s to %s returned", patchPath, patchDevicePath.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000310 }
311
312 std::vector<unsigned char> applyOutputBuffer;
313 std::vector<unsigned char> applyErrorBuffer;
314 std::string argsString;
315
316 for (int i = 0; i < argc; i++) {
317 argsString.append(argv[i]);
318 argsString.append(" ");
319 }
320
321 std::string applyPatchCommand =
322 android::base::StringPrintf(kAgentApplyCommandPattern.c_str(), packageName.c_str(),
323 patchDevicePath.c_str(), argsString.c_str());
Idries Hamadi269a4b42018-09-13 18:00:25 +0100324 int returnCode = send_shell_command(applyPatchCommand);
325 if (returnCode != 0) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700326 error_exit("Executing %s returned %d", applyPatchCommand.c_str(), returnCode);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100327 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000328}