blob: 45f3cca90501d042b4be87f7d48f70a51971a0f5 [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;
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 Hughesd2aaede2018-10-22 17:02:51 -0700235 error_exit("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 Hughesd2aaede2018-10-22 17:02:51 -0700244 error_exit(
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 Hughesd2aaede2018-10-22 17:02:51 -0700254 error_exit("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 Hughesd2aaede2018-10-22 17:02:51 -0700266 error_exit("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 Hughesd2aaede2018-10-22 17:02:51 -0700285 error_exit("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 Hughesd2aaede2018-10-22 17:02:51 -0700294 error_exit("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 Hughesd2aaede2018-10-22 17:02:51 -0700308 error_exit("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 Hughesd2aaede2018-10-22 17:02:51 -0700325 error_exit("Executing %s returned %d", applyPatchCommand.c_str(), returnCode);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100326 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000327}