blob: f4e8664a86e376c2a68616b9d2981d0b115f47d1 [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
Henry Daitxee01c802018-12-12 10:40:57 +000019#include <string.h>
Idries Hamadied409ea2018-01-29 16:30:36 +000020#include <algorithm>
Idries Hamadi5b6bf942018-08-28 12:58:09 +010021#include <array>
Idries Hamadi269a4b42018-09-13 18:00:25 +010022#include <memory>
Idries Hamadied409ea2018-01-29 16:30:36 +000023
Idries Hamadi5b6bf942018-08-28 12:58:09 +010024#include "android-base/file.h"
25#include "android-base/strings.h"
Idries Hamadi269a4b42018-09-13 18:00:25 +010026#include "androidfw/ResourceTypes.h"
27#include "androidfw/ZipFileRO.h"
Idries Hamadied409ea2018-01-29 16:30:36 +000028#include "client/file_sync_client.h"
29#include "commandline.h"
Idries Hamadied409ea2018-01-29 16:30:36 +000030#include "fastdeploycallbacks.h"
Elliott Hughes4679a392018-10-19 13:59:44 -070031#include "sysdeps.h"
Elliott Hughesd2aaede2018-10-22 17:02:51 -070032
33#include "adb_utils.h"
Idries Hamadied409ea2018-01-29 16:30:36 +000034
Henry Daitxee01c802018-12-12 10:40:57 +000035static constexpr long kRequiredAgentVersion = 0x00000002;
Idries Hamadied409ea2018-01-29 16:30:36 +000036
Elliott Hughes86ab9ff2018-09-05 12:13:11 -070037static constexpr const char* kDeviceAgentPath = "/data/local/tmp/";
Idries Hamadied409ea2018-01-29 16:30:36 +000038
Idries Hamadi4af6ee42018-09-06 18:42:39 +010039static bool g_use_localagent = false;
Idries Hamadi5b6bf942018-08-28 12:58:09 +010040
Idries Hamadied409ea2018-01-29 16:30:36 +000041long get_agent_version() {
42 std::vector<char> versionOutputBuffer;
43 std::vector<char> versionErrorBuffer;
44
Idries Hamadi7575cd92018-08-24 11:46:45 +010045 int statusCode = capture_shell_command("/data/local/tmp/deployagent version",
Idries Hamadied409ea2018-01-29 16:30:36 +000046 &versionOutputBuffer, &versionErrorBuffer);
47 long version = -1;
48
49 if (statusCode == 0 && versionOutputBuffer.size() > 0) {
50 version = strtol((char*)versionOutputBuffer.data(), NULL, 16);
51 }
52
53 return version;
54}
55
56int get_device_api_level() {
57 std::vector<char> sdkVersionOutputBuffer;
58 std::vector<char> sdkVersionErrorBuffer;
59 int api_level = -1;
60
61 int statusCode = capture_shell_command("getprop ro.build.version.sdk", &sdkVersionOutputBuffer,
62 &sdkVersionErrorBuffer);
Idries Hamadi7f51e002018-08-23 17:22:21 +010063 if (statusCode == 0 && sdkVersionOutputBuffer.size() > 0) {
Idries Hamadied409ea2018-01-29 16:30:36 +000064 api_level = strtol((char*)sdkVersionOutputBuffer.data(), NULL, 10);
65 }
66
67 return api_level;
68}
69
Idries Hamadi4af6ee42018-09-06 18:42:39 +010070void fastdeploy_set_local_agent(bool use_localagent) {
71 g_use_localagent = use_localagent;
Idries Hamadi5b6bf942018-08-28 12:58:09 +010072}
73
Idries Hamadied409ea2018-01-29 16:30:36 +000074// local_path - must start with a '/' and be relative to $ANDROID_PRODUCT_OUT
Idries Hamadi4af6ee42018-09-06 18:42:39 +010075static std::string get_agent_component_host_path(const char* local_path, const char* sdk_path) {
Idries Hamadi5b6bf942018-08-28 12:58:09 +010076 std::string adb_dir = android::base::GetExecutableDirectory();
77 if (adb_dir.empty()) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -070078 error_exit("Could not determine location of adb!");
Idries Hamadied409ea2018-01-29 16:30:36 +000079 }
80
Idries Hamadi4af6ee42018-09-06 18:42:39 +010081 if (g_use_localagent) {
Idries Hamadied409ea2018-01-29 16:30:36 +000082 const char* product_out = getenv("ANDROID_PRODUCT_OUT");
83 if (product_out == nullptr) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -070084 error_exit("Could not locate %s because $ANDROID_PRODUCT_OUT is not defined",
85 local_path);
Idries Hamadied409ea2018-01-29 16:30:36 +000086 }
Idries Hamadi4af6ee42018-09-06 18:42:39 +010087 return android::base::StringPrintf("%s%s", product_out, local_path);
Idries Hamadied409ea2018-01-29 16:30:36 +000088 } else {
Idries Hamadi4af6ee42018-09-06 18:42:39 +010089 return adb_dir + sdk_path;
Idries Hamadied409ea2018-01-29 16:30:36 +000090 }
Idries Hamadied409ea2018-01-29 16:30:36 +000091}
92
Idries Hamadi5b6bf942018-08-28 12:58:09 +010093static bool deploy_agent(bool checkTimeStamps) {
Idries Hamadied409ea2018-01-29 16:30:36 +000094 std::vector<const char*> srcs;
Idries Hamadi4af6ee42018-09-06 18:42:39 +010095 std::string jar_path =
96 get_agent_component_host_path("/system/framework/deployagent.jar", "/deployagent.jar");
97 std::string script_path =
98 get_agent_component_host_path("/system/bin/deployagent", "/deployagent");
99 srcs.push_back(jar_path.c_str());
100 srcs.push_back(script_path.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000101
102 if (do_sync_push(srcs, kDeviceAgentPath, checkTimeStamps)) {
103 // on windows the shell script might have lost execute permission
104 // so need to set this explicitly
Idries Hamadi7575cd92018-08-24 11:46:45 +0100105 const char* kChmodCommandPattern = "chmod 777 %sdeployagent";
Idries Hamadied409ea2018-01-29 16:30:36 +0000106 std::string chmodCommand =
107 android::base::StringPrintf(kChmodCommandPattern, kDeviceAgentPath);
Idries Hamadi7f51e002018-08-23 17:22:21 +0100108 int ret = send_shell_command(chmodCommand);
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100109 if (ret != 0) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700110 error_exit("Error executing %s returncode: %d", chmodCommand.c_str(), ret);
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100111 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000112 } else {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700113 error_exit("Error pushing agent files to device");
Idries Hamadied409ea2018-01-29 16:30:36 +0000114 }
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100115
116 return true;
Idries Hamadied409ea2018-01-29 16:30:36 +0000117}
118
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100119void update_agent(FastDeploy_AgentUpdateStrategy agentUpdateStrategy) {
Idries Hamadied409ea2018-01-29 16:30:36 +0000120 long agent_version = get_agent_version();
121 switch (agentUpdateStrategy) {
122 case FastDeploy_AgentUpdateAlways:
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100123 deploy_agent(false);
Idries Hamadied409ea2018-01-29 16:30:36 +0000124 break;
125 case FastDeploy_AgentUpdateNewerTimeStamp:
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100126 deploy_agent(true);
Idries Hamadied409ea2018-01-29 16:30:36 +0000127 break;
128 case FastDeploy_AgentUpdateDifferentVersion:
129 if (agent_version != kRequiredAgentVersion) {
130 if (agent_version < 0) {
131 printf("Could not detect agent on device, deploying\n");
132 } else {
133 printf("Device agent version is (%ld), (%ld) is required, re-deploying\n",
134 agent_version, kRequiredAgentVersion);
135 }
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100136 deploy_agent(false);
Idries Hamadied409ea2018-01-29 16:30:36 +0000137 }
138 break;
139 }
140
141 agent_version = get_agent_version();
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100142 if (agent_version != kRequiredAgentVersion) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700143 error_exit("After update agent version remains incorrect! Expected %ld but version is %ld",
144 kRequiredAgentVersion, agent_version);
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100145 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000146}
147
Idries Hamadi269a4b42018-09-13 18:00:25 +0100148static std::string get_string_from_utf16(const char16_t* input, int input_len) {
149 ssize_t utf8_length = utf16_to_utf8_length(input, input_len);
150 if (utf8_length <= 0) {
151 return {};
Idries Hamadied409ea2018-01-29 16:30:36 +0000152 }
Idries Hamadi269a4b42018-09-13 18:00:25 +0100153 std::string utf8;
154 utf8.resize(utf8_length);
155 utf16_to_utf8(input, input_len, &*utf8.begin(), utf8_length + 1);
156 return utf8;
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100157}
158
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100159static std::string get_packagename_from_apk(const char* apkPath) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700160#undef open
Idries Hamadi269a4b42018-09-13 18:00:25 +0100161 std::unique_ptr<android::ZipFileRO> zipFile(android::ZipFileRO::open(apkPath));
Elliott Hughes4679a392018-10-19 13:59:44 -0700162#define open ___xxx_unix_open
Idries Hamadi269a4b42018-09-13 18:00:25 +0100163 if (zipFile == nullptr) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700164 perror_exit("Could not open %s", apkPath);
Idries Hamadied409ea2018-01-29 16:30:36 +0000165 }
Idries Hamadi269a4b42018-09-13 18:00:25 +0100166 android::ZipEntryRO entry = zipFile->findEntryByName("AndroidManifest.xml");
167 if (entry == nullptr) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700168 error_exit("Could not find AndroidManifest.xml inside %s", apkPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100169 }
170 uint32_t manifest_len = 0;
171 if (!zipFile->getEntryInfo(entry, NULL, &manifest_len, NULL, NULL, NULL, NULL)) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700172 error_exit("Could not read AndroidManifest.xml inside %s", apkPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100173 }
174 std::vector<char> manifest_data(manifest_len);
175 if (!zipFile->uncompressEntry(entry, manifest_data.data(), manifest_len)) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700176 error_exit("Could not uncompress AndroidManifest.xml inside %s", apkPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100177 }
178 android::ResXMLTree tree;
179 android::status_t setto_status = tree.setTo(manifest_data.data(), manifest_len, true);
180 if (setto_status != android::OK) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700181 error_exit("Could not parse AndroidManifest.xml inside %s", apkPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100182 }
183 android::ResXMLParser::event_code_t code;
184 while ((code = tree.next()) != android::ResXMLParser::BAD_DOCUMENT &&
185 code != android::ResXMLParser::END_DOCUMENT) {
186 switch (code) {
187 case android::ResXMLParser::START_TAG: {
188 size_t element_name_length;
189 const char16_t* element_name = tree.getElementName(&element_name_length);
190 if (element_name == nullptr) {
191 continue;
192 }
193 std::u16string element_name_string(element_name, element_name_length);
194 if (element_name_string == u"manifest") {
195 for (size_t i = 0; i < tree.getAttributeCount(); i++) {
196 size_t attribute_name_length;
197 const char16_t* attribute_name_text =
198 tree.getAttributeName(i, &attribute_name_length);
199 if (attribute_name_text == nullptr) {
200 continue;
201 }
202 std::u16string attribute_name_string(attribute_name_text,
203 attribute_name_length);
204 if (attribute_name_string == u"package") {
205 size_t attribute_value_length;
206 const char16_t* attribute_value_text =
207 tree.getAttributeStringValue(i, &attribute_value_length);
208 if (attribute_value_text == nullptr) {
209 continue;
210 }
211 return get_string_from_utf16(attribute_value_text,
212 attribute_value_length);
213 }
214 }
215 }
216 break;
217 }
218 default:
219 break;
220 }
221 }
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700222 error_exit("Could not find package name tag in AndroidManifest.xml inside %s", apkPath);
Idries Hamadied409ea2018-01-29 16:30:36 +0000223}
224
Idries Hamadi269a4b42018-09-13 18:00:25 +0100225void extract_metadata(const char* apkPath, FILE* outputFp) {
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100226 std::string packageName = get_packagename_from_apk(apkPath);
Idries Hamadi7575cd92018-08-24 11:46:45 +0100227 const char* kAgentExtractCommandPattern = "/data/local/tmp/deployagent extract %s";
Idries Hamadied409ea2018-01-29 16:30:36 +0000228 std::string extractCommand =
229 android::base::StringPrintf(kAgentExtractCommandPattern, packageName.c_str());
230
231 std::vector<char> extractErrorBuffer;
Henry Daitxf21edf32018-11-30 18:02:43 +0000232 DeployAgentFileCallback cb(outputFp, &extractErrorBuffer);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100233 int returnCode = send_shell_command(extractCommand, false, &cb);
234 if (returnCode != 0) {
Henry Daitxf21edf32018-11-30 18:02:43 +0000235 fprintf(stderr, "Executing %s returned %d\n", extractCommand.c_str(), returnCode);
236 fprintf(stderr, "%*s\n", int(extractErrorBuffer.size()), extractErrorBuffer.data());
237 error_exit("Aborting");
Idries Hamadied409ea2018-01-29 16:30:36 +0000238 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000239}
240
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100241static std::string get_patch_generator_command() {
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100242 if (g_use_localagent) {
Idries Hamadied409ea2018-01-29 16:30:36 +0000243 // This should never happen on a Windows machine
Idries Hamadied409ea2018-01-29 16:30:36 +0000244 const char* host_out = getenv("ANDROID_HOST_OUT");
245 if (host_out == nullptr) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700246 error_exit(
247 "Could not locate deploypatchgenerator.jar because $ANDROID_HOST_OUT "
248 "is not defined");
Idries Hamadied409ea2018-01-29 16:30:36 +0000249 }
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100250 return android::base::StringPrintf("java -jar %s/framework/deploypatchgenerator.jar",
251 host_out);
Idries Hamadied409ea2018-01-29 16:30:36 +0000252 }
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100253
254 std::string adb_dir = android::base::GetExecutableDirectory();
255 if (adb_dir.empty()) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700256 error_exit("Could not locate deploypatchgenerator.jar");
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100257 }
258 return android::base::StringPrintf(R"(java -jar "%s/deploypatchgenerator.jar")",
259 adb_dir.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000260}
261
Idries Hamadi269a4b42018-09-13 18:00:25 +0100262void create_patch(const char* apkPath, const char* metadataPath, const char* patchPath) {
Idries Hamadied409ea2018-01-29 16:30:36 +0000263 std::string generatePatchCommand = android::base::StringPrintf(
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100264 R"(%s "%s" "%s" > "%s")", get_patch_generator_command().c_str(), apkPath, metadataPath,
265 patchPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100266 int returnCode = system(generatePatchCommand.c_str());
267 if (returnCode != 0) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700268 error_exit("Executing %s returned %d", generatePatchCommand.c_str(), returnCode);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100269 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000270}
271
272std::string get_patch_path(const char* apkPath) {
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100273 std::string packageName = get_packagename_from_apk(apkPath);
Idries Hamadied409ea2018-01-29 16:30:36 +0000274 std::string patchDevicePath =
275 android::base::StringPrintf("%s%s.patch", kDeviceAgentPath, packageName.c_str());
276 return patchDevicePath;
277}
278
Idries Hamadi269a4b42018-09-13 18:00:25 +0100279void apply_patch_on_device(const char* apkPath, const char* patchPath, const char* outputPath) {
Idries Hamadi7575cd92018-08-24 11:46:45 +0100280 const std::string kAgentApplyCommandPattern = "/data/local/tmp/deployagent apply %s %s -o %s";
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100281 std::string packageName = get_packagename_from_apk(apkPath);
Idries Hamadied409ea2018-01-29 16:30:36 +0000282 std::string patchDevicePath = get_patch_path(apkPath);
283
284 std::vector<const char*> srcs = {patchPath};
285 bool push_ok = do_sync_push(srcs, patchDevicePath.c_str(), false);
Idries Hamadied409ea2018-01-29 16:30:36 +0000286 if (!push_ok) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700287 error_exit("Error pushing %s to %s returned", patchPath, patchDevicePath.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000288 }
289
290 std::string applyPatchCommand =
291 android::base::StringPrintf(kAgentApplyCommandPattern.c_str(), packageName.c_str(),
292 patchDevicePath.c_str(), outputPath);
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100293
Idries Hamadi269a4b42018-09-13 18:00:25 +0100294 int returnCode = send_shell_command(applyPatchCommand);
295 if (returnCode != 0) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700296 error_exit("Executing %s returned %d", applyPatchCommand.c_str(), returnCode);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100297 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000298}
299
Idries Hamadi269a4b42018-09-13 18:00:25 +0100300void install_patch(const char* apkPath, const char* patchPath, int argc, const char** argv) {
Idries Hamadi7575cd92018-08-24 11:46:45 +0100301 const std::string kAgentApplyCommandPattern = "/data/local/tmp/deployagent apply %s %s -pm %s";
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100302 std::string packageName = get_packagename_from_apk(apkPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100303
Idries Hamadied409ea2018-01-29 16:30:36 +0000304 std::string patchDevicePath =
305 android::base::StringPrintf("%s%s.patch", kDeviceAgentPath, packageName.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000306
Idries Hamadi269a4b42018-09-13 18:00:25 +0100307 std::vector<const char*> srcs{patchPath};
308 bool push_ok = do_sync_push(srcs, patchDevicePath.c_str(), false);
Idries Hamadied409ea2018-01-29 16:30:36 +0000309 if (!push_ok) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700310 error_exit("Error pushing %s to %s returned", patchPath, patchDevicePath.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000311 }
312
313 std::vector<unsigned char> applyOutputBuffer;
314 std::vector<unsigned char> applyErrorBuffer;
315 std::string argsString;
316
Henry Daitxee01c802018-12-12 10:40:57 +0000317 bool rSwitchPresent = false;
Idries Hamadied409ea2018-01-29 16:30:36 +0000318 for (int i = 0; i < argc; i++) {
319 argsString.append(argv[i]);
320 argsString.append(" ");
Henry Daitxee01c802018-12-12 10:40:57 +0000321 if (!strcmp(argv[i], "-r")) {
322 rSwitchPresent = true;
323 }
324 }
325 if (!rSwitchPresent) {
326 argsString.append("-r");
Idries Hamadied409ea2018-01-29 16:30:36 +0000327 }
328
329 std::string applyPatchCommand =
330 android::base::StringPrintf(kAgentApplyCommandPattern.c_str(), packageName.c_str(),
331 patchDevicePath.c_str(), argsString.c_str());
Idries Hamadi269a4b42018-09-13 18:00:25 +0100332 int returnCode = send_shell_command(applyPatchCommand);
333 if (returnCode != 0) {
Elliott Hughesd2aaede2018-10-22 17:02:51 -0700334 error_exit("Executing %s returned %d", applyPatchCommand.c_str(), returnCode);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100335 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000336}
Henry Daitxee01c802018-12-12 10:40:57 +0000337
338bool find_package(const char* apkPath) {
339 const std::string findCommand =
340 "/data/local/tmp/deployagent find " + get_packagename_from_apk(apkPath);
341 return !send_shell_command(findCommand);
342}