blob: 183a1fa8f15e813614751b1a06027242b420cca7 [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"
30#include "utils/String16.h"
31
Elliott Hughes86ab9ff2018-09-05 12:13:11 -070032static constexpr long kRequiredAgentVersion = 0x00000001;
Idries Hamadied409ea2018-01-29 16:30:36 +000033
Elliott Hughes86ab9ff2018-09-05 12:13:11 -070034static constexpr const char* kDeviceAgentPath = "/data/local/tmp/";
Idries Hamadied409ea2018-01-29 16:30:36 +000035
Idries Hamadi4af6ee42018-09-06 18:42:39 +010036static bool g_use_localagent = false;
Idries Hamadi5b6bf942018-08-28 12:58:09 +010037
Idries Hamadied409ea2018-01-29 16:30:36 +000038long get_agent_version() {
39 std::vector<char> versionOutputBuffer;
40 std::vector<char> versionErrorBuffer;
41
Idries Hamadi7575cd92018-08-24 11:46:45 +010042 int statusCode = capture_shell_command("/data/local/tmp/deployagent version",
Idries Hamadied409ea2018-01-29 16:30:36 +000043 &versionOutputBuffer, &versionErrorBuffer);
44 long version = -1;
45
46 if (statusCode == 0 && versionOutputBuffer.size() > 0) {
47 version = strtol((char*)versionOutputBuffer.data(), NULL, 16);
48 }
49
50 return version;
51}
52
53int get_device_api_level() {
54 std::vector<char> sdkVersionOutputBuffer;
55 std::vector<char> sdkVersionErrorBuffer;
56 int api_level = -1;
57
58 int statusCode = capture_shell_command("getprop ro.build.version.sdk", &sdkVersionOutputBuffer,
59 &sdkVersionErrorBuffer);
Idries Hamadi7f51e002018-08-23 17:22:21 +010060 if (statusCode == 0 && sdkVersionOutputBuffer.size() > 0) {
Idries Hamadied409ea2018-01-29 16:30:36 +000061 api_level = strtol((char*)sdkVersionOutputBuffer.data(), NULL, 10);
62 }
63
64 return api_level;
65}
66
Idries Hamadi4af6ee42018-09-06 18:42:39 +010067void fastdeploy_set_local_agent(bool use_localagent) {
68 g_use_localagent = use_localagent;
Idries Hamadi5b6bf942018-08-28 12:58:09 +010069}
70
Idries Hamadied409ea2018-01-29 16:30:36 +000071// local_path - must start with a '/' and be relative to $ANDROID_PRODUCT_OUT
Idries Hamadi4af6ee42018-09-06 18:42:39 +010072static std::string get_agent_component_host_path(const char* local_path, const char* sdk_path) {
Idries Hamadi5b6bf942018-08-28 12:58:09 +010073 std::string adb_dir = android::base::GetExecutableDirectory();
74 if (adb_dir.empty()) {
Idries Hamadi4af6ee42018-09-06 18:42:39 +010075 fatal("Could not determine location of adb!");
Idries Hamadied409ea2018-01-29 16:30:36 +000076 }
77
Idries Hamadi4af6ee42018-09-06 18:42:39 +010078 if (g_use_localagent) {
Idries Hamadied409ea2018-01-29 16:30:36 +000079 const char* product_out = getenv("ANDROID_PRODUCT_OUT");
80 if (product_out == nullptr) {
Idries Hamadi4af6ee42018-09-06 18:42:39 +010081 fatal("Could not locate %s because $ANDROID_PRODUCT_OUT is not defined", local_path);
Idries Hamadied409ea2018-01-29 16:30:36 +000082 }
Idries Hamadi4af6ee42018-09-06 18:42:39 +010083 return android::base::StringPrintf("%s%s", product_out, local_path);
Idries Hamadied409ea2018-01-29 16:30:36 +000084 } else {
Idries Hamadi4af6ee42018-09-06 18:42:39 +010085 return adb_dir + sdk_path;
Idries Hamadied409ea2018-01-29 16:30:36 +000086 }
Idries Hamadied409ea2018-01-29 16:30:36 +000087}
88
Idries Hamadi5b6bf942018-08-28 12:58:09 +010089static bool deploy_agent(bool checkTimeStamps) {
Idries Hamadied409ea2018-01-29 16:30:36 +000090 std::vector<const char*> srcs;
Idries Hamadi4af6ee42018-09-06 18:42:39 +010091 std::string jar_path =
92 get_agent_component_host_path("/system/framework/deployagent.jar", "/deployagent.jar");
93 std::string script_path =
94 get_agent_component_host_path("/system/bin/deployagent", "/deployagent");
95 srcs.push_back(jar_path.c_str());
96 srcs.push_back(script_path.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +000097
98 if (do_sync_push(srcs, kDeviceAgentPath, checkTimeStamps)) {
99 // on windows the shell script might have lost execute permission
100 // so need to set this explicitly
Idries Hamadi7575cd92018-08-24 11:46:45 +0100101 const char* kChmodCommandPattern = "chmod 777 %sdeployagent";
Idries Hamadied409ea2018-01-29 16:30:36 +0000102 std::string chmodCommand =
103 android::base::StringPrintf(kChmodCommandPattern, kDeviceAgentPath);
Idries Hamadi7f51e002018-08-23 17:22:21 +0100104 int ret = send_shell_command(chmodCommand);
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100105 if (ret != 0) {
106 fatal("Error executing %s returncode: %d", chmodCommand.c_str(), ret);
107 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000108 } else {
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100109 fatal("Error pushing agent files to device");
Idries Hamadied409ea2018-01-29 16:30:36 +0000110 }
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100111
112 return true;
Idries Hamadied409ea2018-01-29 16:30:36 +0000113}
114
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100115void update_agent(FastDeploy_AgentUpdateStrategy agentUpdateStrategy) {
Idries Hamadied409ea2018-01-29 16:30:36 +0000116 long agent_version = get_agent_version();
117 switch (agentUpdateStrategy) {
118 case FastDeploy_AgentUpdateAlways:
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100119 deploy_agent(false);
Idries Hamadied409ea2018-01-29 16:30:36 +0000120 break;
121 case FastDeploy_AgentUpdateNewerTimeStamp:
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100122 deploy_agent(true);
Idries Hamadied409ea2018-01-29 16:30:36 +0000123 break;
124 case FastDeploy_AgentUpdateDifferentVersion:
125 if (agent_version != kRequiredAgentVersion) {
126 if (agent_version < 0) {
127 printf("Could not detect agent on device, deploying\n");
128 } else {
129 printf("Device agent version is (%ld), (%ld) is required, re-deploying\n",
130 agent_version, kRequiredAgentVersion);
131 }
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100132 deploy_agent(false);
Idries Hamadied409ea2018-01-29 16:30:36 +0000133 }
134 break;
135 }
136
137 agent_version = get_agent_version();
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100138 if (agent_version != kRequiredAgentVersion) {
139 fatal("After update agent version remains incorrect! Expected %ld but version is %ld",
140 kRequiredAgentVersion, agent_version);
141 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000142}
143
Idries Hamadi269a4b42018-09-13 18:00:25 +0100144static std::string get_string_from_utf16(const char16_t* input, int input_len) {
145 ssize_t utf8_length = utf16_to_utf8_length(input, input_len);
146 if (utf8_length <= 0) {
147 return {};
Idries Hamadied409ea2018-01-29 16:30:36 +0000148 }
Idries Hamadi269a4b42018-09-13 18:00:25 +0100149 std::string utf8;
150 utf8.resize(utf8_length);
151 utf16_to_utf8(input, input_len, &*utf8.begin(), utf8_length + 1);
152 return utf8;
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100153}
154
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100155static std::string get_packagename_from_apk(const char* apkPath) {
Idries Hamadi269a4b42018-09-13 18:00:25 +0100156 std::unique_ptr<android::ZipFileRO> zipFile(android::ZipFileRO::open(apkPath));
157 if (zipFile == nullptr) {
158 fatal("Could not open %s", apkPath);
Idries Hamadied409ea2018-01-29 16:30:36 +0000159 }
Idries Hamadi269a4b42018-09-13 18:00:25 +0100160 android::ZipEntryRO entry = zipFile->findEntryByName("AndroidManifest.xml");
161 if (entry == nullptr) {
162 fatal("Could not find AndroidManifest.xml inside %s", apkPath);
163 }
164 uint32_t manifest_len = 0;
165 if (!zipFile->getEntryInfo(entry, NULL, &manifest_len, NULL, NULL, NULL, NULL)) {
166 fatal("Could not read AndroidManifest.xml inside %s", apkPath);
167 }
168 std::vector<char> manifest_data(manifest_len);
169 if (!zipFile->uncompressEntry(entry, manifest_data.data(), manifest_len)) {
170 fatal("Could not uncompress AndroidManifest.xml inside %s", apkPath);
171 }
172 android::ResXMLTree tree;
173 android::status_t setto_status = tree.setTo(manifest_data.data(), manifest_len, true);
174 if (setto_status != android::OK) {
175 fatal("Could not parse AndroidManifest.xml inside %s", apkPath);
176 }
177 android::ResXMLParser::event_code_t code;
178 while ((code = tree.next()) != android::ResXMLParser::BAD_DOCUMENT &&
179 code != android::ResXMLParser::END_DOCUMENT) {
180 switch (code) {
181 case android::ResXMLParser::START_TAG: {
182 size_t element_name_length;
183 const char16_t* element_name = tree.getElementName(&element_name_length);
184 if (element_name == nullptr) {
185 continue;
186 }
187 std::u16string element_name_string(element_name, element_name_length);
188 if (element_name_string == u"manifest") {
189 for (size_t i = 0; i < tree.getAttributeCount(); i++) {
190 size_t attribute_name_length;
191 const char16_t* attribute_name_text =
192 tree.getAttributeName(i, &attribute_name_length);
193 if (attribute_name_text == nullptr) {
194 continue;
195 }
196 std::u16string attribute_name_string(attribute_name_text,
197 attribute_name_length);
198 if (attribute_name_string == u"package") {
199 size_t attribute_value_length;
200 const char16_t* attribute_value_text =
201 tree.getAttributeStringValue(i, &attribute_value_length);
202 if (attribute_value_text == nullptr) {
203 continue;
204 }
205 return get_string_from_utf16(attribute_value_text,
206 attribute_value_length);
207 }
208 }
209 }
210 break;
211 }
212 default:
213 break;
214 }
215 }
216 fatal("Could not find package name tag in AndroidManifest.xml inside %s", apkPath);
Idries Hamadied409ea2018-01-29 16:30:36 +0000217}
218
Idries Hamadi269a4b42018-09-13 18:00:25 +0100219void extract_metadata(const char* apkPath, FILE* outputFp) {
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100220 std::string packageName = get_packagename_from_apk(apkPath);
Idries Hamadi7575cd92018-08-24 11:46:45 +0100221 const char* kAgentExtractCommandPattern = "/data/local/tmp/deployagent extract %s";
Idries Hamadied409ea2018-01-29 16:30:36 +0000222 std::string extractCommand =
223 android::base::StringPrintf(kAgentExtractCommandPattern, packageName.c_str());
224
225 std::vector<char> extractErrorBuffer;
226 int statusCode;
227 DeployAgentFileCallback cb(outputFp, &extractErrorBuffer, &statusCode);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100228 int returnCode = send_shell_command(extractCommand, false, &cb);
229 if (returnCode != 0) {
230 fatal("Executing %s returned %d\n", extractCommand.c_str(), returnCode);
Idries Hamadied409ea2018-01-29 16:30:36 +0000231 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000232}
233
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100234static std::string get_patch_generator_command() {
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100235 if (g_use_localagent) {
Idries Hamadied409ea2018-01-29 16:30:36 +0000236 // This should never happen on a Windows machine
Idries Hamadied409ea2018-01-29 16:30:36 +0000237 const char* host_out = getenv("ANDROID_HOST_OUT");
238 if (host_out == nullptr) {
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100239 fatal("Could not locate deploypatchgenerator.jar because $ANDROID_HOST_OUT is not "
240 "defined");
Idries Hamadied409ea2018-01-29 16:30:36 +0000241 }
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100242 return android::base::StringPrintf("java -jar %s/framework/deploypatchgenerator.jar",
243 host_out);
Idries Hamadied409ea2018-01-29 16:30:36 +0000244 }
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100245
246 std::string adb_dir = android::base::GetExecutableDirectory();
247 if (adb_dir.empty()) {
248 fatal("Could not locate deploypatchgenerator.jar");
249 }
250 return android::base::StringPrintf(R"(java -jar "%s/deploypatchgenerator.jar")",
251 adb_dir.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000252}
253
Idries Hamadi269a4b42018-09-13 18:00:25 +0100254void create_patch(const char* apkPath, const char* metadataPath, const char* patchPath) {
Idries Hamadied409ea2018-01-29 16:30:36 +0000255 std::string generatePatchCommand = android::base::StringPrintf(
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100256 R"(%s "%s" "%s" > "%s")", get_patch_generator_command().c_str(), apkPath, metadataPath,
257 patchPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100258 int returnCode = system(generatePatchCommand.c_str());
259 if (returnCode != 0) {
260 fatal("Executing %s returned %d\n", generatePatchCommand.c_str(), returnCode);
261 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000262}
263
264std::string get_patch_path(const char* apkPath) {
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100265 std::string packageName = get_packagename_from_apk(apkPath);
Idries Hamadied409ea2018-01-29 16:30:36 +0000266 std::string patchDevicePath =
267 android::base::StringPrintf("%s%s.patch", kDeviceAgentPath, packageName.c_str());
268 return patchDevicePath;
269}
270
Idries Hamadi269a4b42018-09-13 18:00:25 +0100271void apply_patch_on_device(const char* apkPath, const char* patchPath, const char* outputPath) {
Idries Hamadi7575cd92018-08-24 11:46:45 +0100272 const std::string kAgentApplyCommandPattern = "/data/local/tmp/deployagent apply %s %s -o %s";
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 = get_patch_path(apkPath);
275
276 std::vector<const char*> srcs = {patchPath};
277 bool push_ok = do_sync_push(srcs, patchDevicePath.c_str(), false);
Idries Hamadied409ea2018-01-29 16:30:36 +0000278 if (!push_ok) {
Idries Hamadi269a4b42018-09-13 18:00:25 +0100279 fatal("Error pushing %s to %s returned\n", patchPath, patchDevicePath.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000280 }
281
282 std::string applyPatchCommand =
283 android::base::StringPrintf(kAgentApplyCommandPattern.c_str(), packageName.c_str(),
284 patchDevicePath.c_str(), outputPath);
Idries Hamadi5b6bf942018-08-28 12:58:09 +0100285
Idries Hamadi269a4b42018-09-13 18:00:25 +0100286 int returnCode = send_shell_command(applyPatchCommand);
287 if (returnCode != 0) {
288 fatal("Executing %s returned %d\n", applyPatchCommand.c_str(), returnCode);
289 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000290}
291
Idries Hamadi269a4b42018-09-13 18:00:25 +0100292void install_patch(const char* apkPath, const char* patchPath, int argc, const char** argv) {
Idries Hamadi7575cd92018-08-24 11:46:45 +0100293 const std::string kAgentApplyCommandPattern = "/data/local/tmp/deployagent apply %s %s -pm %s";
Idries Hamadi4af6ee42018-09-06 18:42:39 +0100294 std::string packageName = get_packagename_from_apk(apkPath);
Idries Hamadi269a4b42018-09-13 18:00:25 +0100295
Idries Hamadied409ea2018-01-29 16:30:36 +0000296 std::string patchDevicePath =
297 android::base::StringPrintf("%s%s.patch", kDeviceAgentPath, packageName.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000298
Idries Hamadi269a4b42018-09-13 18:00:25 +0100299 std::vector<const char*> srcs{patchPath};
300 bool push_ok = do_sync_push(srcs, patchDevicePath.c_str(), false);
Idries Hamadied409ea2018-01-29 16:30:36 +0000301 if (!push_ok) {
Idries Hamadi269a4b42018-09-13 18:00:25 +0100302 fatal("Error pushing %s to %s returned\n", patchPath, patchDevicePath.c_str());
Idries Hamadied409ea2018-01-29 16:30:36 +0000303 }
304
305 std::vector<unsigned char> applyOutputBuffer;
306 std::vector<unsigned char> applyErrorBuffer;
307 std::string argsString;
308
309 for (int i = 0; i < argc; i++) {
310 argsString.append(argv[i]);
311 argsString.append(" ");
312 }
313
314 std::string applyPatchCommand =
315 android::base::StringPrintf(kAgentApplyCommandPattern.c_str(), packageName.c_str(),
316 patchDevicePath.c_str(), argsString.c_str());
Idries Hamadi269a4b42018-09-13 18:00:25 +0100317 int returnCode = send_shell_command(applyPatchCommand);
318 if (returnCode != 0) {
319 fatal("Executing %s returned %d\n", applyPatchCommand.c_str(), returnCode);
320 }
Idries Hamadied409ea2018-01-29 16:30:36 +0000321}