blob: 933b91332497dfa80cff38ca7cb8e2ef43852284 [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"
Idries Hamadied409ea2018-01-29 16:30:36 +000031#include "utils/String16.h"
32
Elliott Hughes86ab9ff2018-09-05 12:13:11 -070033static constexpr long kRequiredAgentVersion = 0x00000001;
Idries Hamadied409ea2018-01-29 16:30:36 +000034
Elliott Hughes86ab9ff2018-09-05 12:13:11 -070035static constexpr const char* kDeviceAgentPath = "/data/local/tmp/";
Idries Hamadied409ea2018-01-29 16:30:36 +000036
Idries Hamadi4af6ee42018-09-06 18:42:39 +010037static bool g_use_localagent = false;
Idries Hamadi5b6bf942018-08-28 12:58:09 +010038
Idries Hamadied409ea2018-01-29 16:30:36 +000039long get_agent_version() {
40 std::vector<char> versionOutputBuffer;
41 std::vector<char> versionErrorBuffer;
42
Idries Hamadi7575cd92018-08-24 11:46:45 +010043 int statusCode = capture_shell_command("/data/local/tmp/deployagent version",
Idries Hamadied409ea2018-01-29 16:30:36 +000044 &versionOutputBuffer, &versionErrorBuffer);
45 long version = -1;
46
47 if (statusCode == 0 && versionOutputBuffer.size() > 0) {
48 version = strtol((char*)versionOutputBuffer.data(), NULL, 16);
49 }
50
51 return version;
52}
53
54int get_device_api_level() {
55 std::vector<char> sdkVersionOutputBuffer;
56 std::vector<char> sdkVersionErrorBuffer;
57 int api_level = -1;
58
59 int statusCode = capture_shell_command("getprop ro.build.version.sdk", &sdkVersionOutputBuffer,
60 &sdkVersionErrorBuffer);
Idries Hamadi7f51e002018-08-23 17:22:21 +010061 if (statusCode == 0 && sdkVersionOutputBuffer.size() > 0) {
Idries Hamadied409ea2018-01-29 16:30:36 +000062 api_level = strtol((char*)sdkVersionOutputBuffer.data(), NULL, 10);
63 }
64
65 return api_level;
66}
67
Idries Hamadi4af6ee42018-09-06 18:42:39 +010068void fastdeploy_set_local_agent(bool use_localagent) {
69 g_use_localagent = use_localagent;
Idries Hamadi5b6bf942018-08-28 12:58:09 +010070}
71
Idries Hamadied409ea2018-01-29 16:30:36 +000072// local_path - must start with a '/' and be relative to $ANDROID_PRODUCT_OUT
Idries Hamadi4af6ee42018-09-06 18:42:39 +010073static std::string get_agent_component_host_path(const char* local_path, const char* sdk_path) {
Idries Hamadi5b6bf942018-08-28 12:58:09 +010074 std::string adb_dir = android::base::GetExecutableDirectory();
75 if (adb_dir.empty()) {
Elliott Hughes4679a392018-10-19 13:59:44 -070076 error(1, 0, "Could not determine location of adb!");
Idries Hamadied409ea2018-01-29 16:30:36 +000077 }
78
Idries Hamadi4af6ee42018-09-06 18:42:39 +010079 if (g_use_localagent) {
Idries Hamadied409ea2018-01-29 16:30:36 +000080 const char* product_out = getenv("ANDROID_PRODUCT_OUT");
81 if (product_out == nullptr) {
Elliott Hughes4679a392018-10-19 13:59:44 -070082 error(1, 0, "Could not locate %s because $ANDROID_PRODUCT_OUT is not defined",
83 local_path);
Idries Hamadied409ea2018-01-29 16:30:36 +000084 }
Idries Hamadi4af6ee42018-09-06 18:42:39 +010085 return android::base::StringPrintf("%s%s", product_out, local_path);
Idries Hamadied409ea2018-01-29 16:30:36 +000086 } else {
Idries Hamadi4af6ee42018-09-06 18:42:39 +010087 return adb_dir + sdk_path;
Idries Hamadied409ea2018-01-29 16:30:36 +000088 }
Idries Hamadied409ea2018-01-29 16:30:36 +000089}
90
Idries Hamadi5b6bf942018-08-28 12:58:09 +010091static bool deploy_agent(bool checkTimeStamps) {
Idries Hamadied409ea2018-01-29 16:30:36 +000092 std::vector<const char*> srcs;
Idries Hamadi4af6ee42018-09-06 18:42:39 +010093 std::string jar_path =
94 get_agent_component_host_path("/system/framework/deployagent.jar", "/deployagent.jar");
95 std::string script_path =
96 get_agent_component_host_path("/system/bin/deployagent", "/deployagent");
97 srcs.push_back(jar_path.c_str());
98 srcs.push_back(script_path.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +000099
100 if (do_sync_push(srcs, kDeviceAgentPath, checkTimeStamps)) {
101 // on windows the shell script might have lost execute permission
102 // so need to set this explicitly
Idries Hamadi7575cd92018-08-24 11:46:45 +0100103 const char* kChmodCommandPattern = "chmod 777 %sdeployagent";
Idries Hamadied409ea2018-01-29 16:30:36 +0000104 std::string chmodCommand =
105 android::base::StringPrintf(kChmodCommandPattern, kDeviceAgentPath);
Idries Hamadi7f51e002018-08-23 17:22:21 +0100106 int ret = send_shell_command(chmodCommand);
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100107 if (ret != 0) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700108 error(1, 0, "Error executing %s returncode: %d", chmodCommand.c_str(), ret);
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100109 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000110 } else {
Elliott Hughes4679a392018-10-19 13:59:44 -0700111 error(1, 0, "Error pushing agent files to device");
Idries Hamadied409ea2018-01-29 16:30:36 +0000112 }
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100113
114 return true;
Idries Hamadied409ea2018-01-29 16:30:36 +0000115}
116
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100117void update_agent(FastDeploy_AgentUpdateStrategy agentUpdateStrategy) {
Idries Hamadied409ea2018-01-29 16:30:36 +0000118 long agent_version = get_agent_version();
119 switch (agentUpdateStrategy) {
120 case FastDeploy_AgentUpdateAlways:
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100121 deploy_agent(false);
Idries Hamadied409ea2018-01-29 16:30:36 +0000122 break;
123 case FastDeploy_AgentUpdateNewerTimeStamp:
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100124 deploy_agent(true);
Idries Hamadied409ea2018-01-29 16:30:36 +0000125 break;
126 case FastDeploy_AgentUpdateDifferentVersion:
127 if (agent_version != kRequiredAgentVersion) {
128 if (agent_version < 0) {
129 printf("Could not detect agent on device, deploying\n");
130 } else {
131 printf("Device agent version is (%ld), (%ld) is required, re-deploying\n",
132 agent_version, kRequiredAgentVersion);
133 }
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100134 deploy_agent(false);
Idries Hamadied409ea2018-01-29 16:30:36 +0000135 }
136 break;
137 }
138
139 agent_version = get_agent_version();
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100140 if (agent_version != kRequiredAgentVersion) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700141 error(1, 0, "After update agent version remains incorrect! Expected %ld but version is %ld",
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100142 kRequiredAgentVersion, agent_version);
143 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000144}
145
Idries Hamadi269a4b42018-09-13 18:00:25 +0100146static std::string get_string_from_utf16(const char16_t* input, int input_len) {
147 ssize_t utf8_length = utf16_to_utf8_length(input, input_len);
148 if (utf8_length <= 0) {
149 return {};
Idries Hamadied409ea2018-01-29 16:30:36 +0000150 }
Idries Hamadi269a4b42018-09-13 18:00:25 +0100151 std::string utf8;
152 utf8.resize(utf8_length);
153 utf16_to_utf8(input, input_len, &*utf8.begin(), utf8_length + 1);
154 return utf8;
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100155}
156
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100157static std::string get_packagename_from_apk(const char* apkPath) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700158#undef open
Idries Hamadi269a4b42018-09-13 18:00:25 +0100159 std::unique_ptr<android::ZipFileRO> zipFile(android::ZipFileRO::open(apkPath));
Elliott Hughes4679a392018-10-19 13:59:44 -0700160#define open ___xxx_unix_open
Idries Hamadi269a4b42018-09-13 18:00:25 +0100161 if (zipFile == nullptr) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700162 error(1, errno, "Could not open %s", apkPath);
Idries Hamadied409ea2018-01-29 16:30:36 +0000163 }
Idries Hamadi269a4b42018-09-13 18:00:25 +0100164 android::ZipEntryRO entry = zipFile->findEntryByName("AndroidManifest.xml");
165 if (entry == nullptr) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700166 error(1, 0, "Could not find AndroidManifest.xml inside %s", apkPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100167 }
168 uint32_t manifest_len = 0;
169 if (!zipFile->getEntryInfo(entry, NULL, &manifest_len, NULL, NULL, NULL, NULL)) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700170 error(1, 0, "Could not read AndroidManifest.xml inside %s", apkPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100171 }
172 std::vector<char> manifest_data(manifest_len);
173 if (!zipFile->uncompressEntry(entry, manifest_data.data(), manifest_len)) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700174 error(1, 0, "Could not uncompress AndroidManifest.xml inside %s", apkPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100175 }
176 android::ResXMLTree tree;
177 android::status_t setto_status = tree.setTo(manifest_data.data(), manifest_len, true);
178 if (setto_status != android::OK) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700179 error(1, 0, "Could not parse AndroidManifest.xml inside %s", apkPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100180 }
181 android::ResXMLParser::event_code_t code;
182 while ((code = tree.next()) != android::ResXMLParser::BAD_DOCUMENT &&
183 code != android::ResXMLParser::END_DOCUMENT) {
184 switch (code) {
185 case android::ResXMLParser::START_TAG: {
186 size_t element_name_length;
187 const char16_t* element_name = tree.getElementName(&element_name_length);
188 if (element_name == nullptr) {
189 continue;
190 }
191 std::u16string element_name_string(element_name, element_name_length);
192 if (element_name_string == u"manifest") {
193 for (size_t i = 0; i < tree.getAttributeCount(); i++) {
194 size_t attribute_name_length;
195 const char16_t* attribute_name_text =
196 tree.getAttributeName(i, &attribute_name_length);
197 if (attribute_name_text == nullptr) {
198 continue;
199 }
200 std::u16string attribute_name_string(attribute_name_text,
201 attribute_name_length);
202 if (attribute_name_string == u"package") {
203 size_t attribute_value_length;
204 const char16_t* attribute_value_text =
205 tree.getAttributeStringValue(i, &attribute_value_length);
206 if (attribute_value_text == nullptr) {
207 continue;
208 }
209 return get_string_from_utf16(attribute_value_text,
210 attribute_value_length);
211 }
212 }
213 }
214 break;
215 }
216 default:
217 break;
218 }
219 }
Elliott Hughes4679a392018-10-19 13:59:44 -0700220 error(1, 0, "Could not find package name tag in AndroidManifest.xml inside %s", apkPath);
221 __builtin_unreachable();
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;
231 int statusCode;
232 DeployAgentFileCallback cb(outputFp, &extractErrorBuffer, &statusCode);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100233 int returnCode = send_shell_command(extractCommand, false, &cb);
234 if (returnCode != 0) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700235 error(1, 0, "Executing %s returned %d", extractCommand.c_str(), returnCode);
Idries Hamadied409ea2018-01-29 16:30:36 +0000236 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000237}
238
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100239static std::string get_patch_generator_command() {
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100240 if (g_use_localagent) {
Idries Hamadied409ea2018-01-29 16:30:36 +0000241 // This should never happen on a Windows machine
Idries Hamadied409ea2018-01-29 16:30:36 +0000242 const char* host_out = getenv("ANDROID_HOST_OUT");
243 if (host_out == nullptr) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700244 error(1, 0,
245 "Could not locate deploypatchgenerator.jar because $ANDROID_HOST_OUT "
246 "is not defined");
Idries Hamadied409ea2018-01-29 16:30:36 +0000247 }
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100248 return android::base::StringPrintf("java -jar %s/framework/deploypatchgenerator.jar",
249 host_out);
Idries Hamadied409ea2018-01-29 16:30:36 +0000250 }
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100251
252 std::string adb_dir = android::base::GetExecutableDirectory();
253 if (adb_dir.empty()) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700254 error(1, 0, "Could not locate deploypatchgenerator.jar");
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100255 }
256 return android::base::StringPrintf(R"(java -jar "%s/deploypatchgenerator.jar")",
257 adb_dir.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000258}
259
Idries Hamadi269a4b42018-09-13 18:00:25 +0100260void create_patch(const char* apkPath, const char* metadataPath, const char* patchPath) {
Idries Hamadied409ea2018-01-29 16:30:36 +0000261 std::string generatePatchCommand = android::base::StringPrintf(
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100262 R"(%s "%s" "%s" > "%s")", get_patch_generator_command().c_str(), apkPath, metadataPath,
263 patchPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100264 int returnCode = system(generatePatchCommand.c_str());
265 if (returnCode != 0) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700266 error(1, 0, "Executing %s returned %d", generatePatchCommand.c_str(), returnCode);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100267 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000268}
269
270std::string get_patch_path(const char* apkPath) {
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100271 std::string packageName = get_packagename_from_apk(apkPath);
Idries Hamadied409ea2018-01-29 16:30:36 +0000272 std::string patchDevicePath =
273 android::base::StringPrintf("%s%s.patch", kDeviceAgentPath, packageName.c_str());
274 return patchDevicePath;
275}
276
Idries Hamadi269a4b42018-09-13 18:00:25 +0100277void apply_patch_on_device(const char* apkPath, const char* patchPath, const char* outputPath) {
Idries Hamadi7575cd92018-08-24 11:46:45 +0100278 const std::string kAgentApplyCommandPattern = "/data/local/tmp/deployagent apply %s %s -o %s";
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100279 std::string packageName = get_packagename_from_apk(apkPath);
Idries Hamadied409ea2018-01-29 16:30:36 +0000280 std::string patchDevicePath = get_patch_path(apkPath);
281
282 std::vector<const char*> srcs = {patchPath};
283 bool push_ok = do_sync_push(srcs, patchDevicePath.c_str(), false);
Idries Hamadied409ea2018-01-29 16:30:36 +0000284 if (!push_ok) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700285 error(1, 0, "Error pushing %s to %s returned", patchPath, patchDevicePath.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000286 }
287
288 std::string applyPatchCommand =
289 android::base::StringPrintf(kAgentApplyCommandPattern.c_str(), packageName.c_str(),
290 patchDevicePath.c_str(), outputPath);
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100291
Idries Hamadi269a4b42018-09-13 18:00:25 +0100292 int returnCode = send_shell_command(applyPatchCommand);
293 if (returnCode != 0) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700294 error(1, 0, "Executing %s returned %d", applyPatchCommand.c_str(), returnCode);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100295 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000296}
297
Idries Hamadi269a4b42018-09-13 18:00:25 +0100298void install_patch(const char* apkPath, const char* patchPath, int argc, const char** argv) {
Idries Hamadi7575cd92018-08-24 11:46:45 +0100299 const std::string kAgentApplyCommandPattern = "/data/local/tmp/deployagent apply %s %s -pm %s";
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100300 std::string packageName = get_packagename_from_apk(apkPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100301
Idries Hamadied409ea2018-01-29 16:30:36 +0000302 std::string patchDevicePath =
303 android::base::StringPrintf("%s%s.patch", kDeviceAgentPath, packageName.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000304
Idries Hamadi269a4b42018-09-13 18:00:25 +0100305 std::vector<const char*> srcs{patchPath};
306 bool push_ok = do_sync_push(srcs, patchDevicePath.c_str(), false);
Idries Hamadied409ea2018-01-29 16:30:36 +0000307 if (!push_ok) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700308 error(1, 0, "Error pushing %s to %s returned", patchPath, patchDevicePath.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000309 }
310
311 std::vector<unsigned char> applyOutputBuffer;
312 std::vector<unsigned char> applyErrorBuffer;
313 std::string argsString;
314
315 for (int i = 0; i < argc; i++) {
316 argsString.append(argv[i]);
317 argsString.append(" ");
318 }
319
320 std::string applyPatchCommand =
321 android::base::StringPrintf(kAgentApplyCommandPattern.c_str(), packageName.c_str(),
322 patchDevicePath.c_str(), argsString.c_str());
Idries Hamadi269a4b42018-09-13 18:00:25 +0100323 int returnCode = send_shell_command(applyPatchCommand);
324 if (returnCode != 0) {
Elliott Hughes4679a392018-10-19 13:59:44 -0700325 error(1, 0, "Executing %s returned %d", applyPatchCommand.c_str(), returnCode);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100326 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000327}