aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTao Bao2017-04-17 18:46:05 -0500
committerTao Bao2017-04-17 22:44:37 -0500
commitbc4b1fe4c4305ebf0fbfc891b9b508c14b5c8ef8 (patch)
tree4f0f193d3f36e29485339d8d67cb24d3c4dfc3b2 /install.cpp
parentce91df82935e296b48f8e1e620e318c2564f1e8b (diff)
downloadplatform-bootable-recovery-bc4b1fe4c4305ebf0fbfc891b9b508c14b5c8ef8.tar.gz
platform-bootable-recovery-bc4b1fe4c4305ebf0fbfc891b9b508c14b5c8ef8.tar.xz
platform-bootable-recovery-bc4b1fe4c4305ebf0fbfc891b9b508c14b5c8ef8.zip
Add tests for update_binary_command().
Expose update_binary_command() through private/install.h for testing purpose. Also make minor clean-ups to install.cpp: a) adding more verbose logging on ExtractToMemory failures; b) update_binary_command() taking std::string instead of const char*; c) moving a few macro and global constants into update_binary_command(). Bug: 37300957 Test: recovery_component_test on marlin Test: Build new recovery and adb sideload on angler and sailfish. Change-Id: Ib2d9068af3fee038f01c90940ccaeb0a7da374fc
Diffstat (limited to 'install.cpp')
-rw-r--r--install.cpp162
1 files changed, 80 insertions, 82 deletions
diff --git a/install.cpp b/install.cpp
index 6dcd3565..4fb80999 100644
--- a/install.cpp
+++ b/install.cpp
@@ -57,9 +57,6 @@
57 57
58using namespace std::chrono_literals; 58using namespace std::chrono_literals;
59 59
60#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
61static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt";
62static constexpr const char* AB_OTA_PAYLOAD = "payload.bin";
63#define PUBLIC_KEYS_FILE "/res/keys" 60#define PUBLIC_KEYS_FILE "/res/keys"
64static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata"; 61static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
65static constexpr const char* UNCRYPT_STATUS = "/cache/recovery/uncrypt_status"; 62static constexpr const char* UNCRYPT_STATUS = "/cache/recovery/uncrypt_status";
@@ -136,13 +133,11 @@ static void read_source_target_build(ZipArchiveHandle zip, std::vector<std::stri
136 } 133 }
137} 134}
138 135
139// Extract the update binary from the open zip archive |zip| located at |path| 136// Extract the update binary from the open zip archive |zip| located at |path| and store into |cmd|
140// and store into |cmd| the command line that should be called. The |status_fd| 137// the command line that should be called. The |status_fd| is the file descriptor the child process
141// is the file descriptor the child process should use to report back the 138// should use to report back the progress of the update.
142// progress of the update. 139int update_binary_command(const std::string& path, ZipArchiveHandle zip, int retry_count,
143static int 140 int status_fd, std::vector<std::string>* cmd);
144update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count,
145 int status_fd, std::vector<std::string>* cmd);
146 141
147#ifdef AB_OTA_UPDATER 142#ifdef AB_OTA_UPDATER
148 143
@@ -224,87 +219,90 @@ static int check_newer_ab_build(ZipArchiveHandle zip) {
224 return 0; 219 return 0;
225} 220}
226 221
227static int 222int update_binary_command(const std::string& path, ZipArchiveHandle zip, int retry_count,
228update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count, 223 int status_fd, std::vector<std::string>* cmd) {
229 int status_fd, std::vector<std::string>* cmd) 224 CHECK(cmd != nullptr);
230{ 225 int ret = check_newer_ab_build(zip);
231 int ret = check_newer_ab_build(zip); 226 if (ret != 0) {
232 if (ret) { 227 return ret;
233 return ret; 228 }
234 }
235 229
236 // For A/B updates we extract the payload properties to a buffer and obtain 230 // For A/B updates we extract the payload properties to a buffer and obtain the RAW payload offset
237 // the RAW payload offset in the zip file. 231 // in the zip file.
238 ZipString property_name(AB_OTA_PAYLOAD_PROPERTIES); 232 static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt";
239 ZipEntry properties_entry; 233 ZipString property_name(AB_OTA_PAYLOAD_PROPERTIES);
240 if (FindEntry(zip, property_name, &properties_entry) != 0) { 234 ZipEntry properties_entry;
241 LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD_PROPERTIES; 235 if (FindEntry(zip, property_name, &properties_entry) != 0) {
242 return INSTALL_CORRUPT; 236 LOG(ERROR) << "Failed to find " << AB_OTA_PAYLOAD_PROPERTIES;
243 } 237 return INSTALL_CORRUPT;
244 std::vector<uint8_t> payload_properties( 238 }
245 properties_entry.uncompressed_length); 239 uint32_t properties_entry_length = properties_entry.uncompressed_length;
246 if (ExtractToMemory(zip, &properties_entry, payload_properties.data(), 240 std::vector<uint8_t> payload_properties(properties_entry_length);
247 properties_entry.uncompressed_length) != 0) { 241 int32_t err =
248 LOG(ERROR) << "Can't extract " << AB_OTA_PAYLOAD_PROPERTIES; 242 ExtractToMemory(zip, &properties_entry, payload_properties.data(), properties_entry_length);
249 return INSTALL_CORRUPT; 243 if (err != 0) {
250 } 244 LOG(ERROR) << "Failed to extract " << AB_OTA_PAYLOAD_PROPERTIES << ": " << ErrorCodeString(err);
245 return INSTALL_CORRUPT;
246 }
251 247
252 ZipString payload_name(AB_OTA_PAYLOAD); 248 static constexpr const char* AB_OTA_PAYLOAD = "payload.bin";
253 ZipEntry payload_entry; 249 ZipString payload_name(AB_OTA_PAYLOAD);
254 if (FindEntry(zip, payload_name, &payload_entry) != 0) { 250 ZipEntry payload_entry;
255 LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD; 251 if (FindEntry(zip, payload_name, &payload_entry) != 0) {
256 return INSTALL_CORRUPT; 252 LOG(ERROR) << "Failed to find " << AB_OTA_PAYLOAD;
257 } 253 return INSTALL_CORRUPT;
258 long payload_offset = payload_entry.offset; 254 }
259 *cmd = { 255 long payload_offset = payload_entry.offset;
260 "/sbin/update_engine_sideload", 256 *cmd = {
261 android::base::StringPrintf("--payload=file://%s", path), 257 "/sbin/update_engine_sideload",
262 android::base::StringPrintf("--offset=%ld", payload_offset), 258 "--payload=file://" + path,
263 "--headers=" + std::string(payload_properties.begin(), 259 android::base::StringPrintf("--offset=%ld", payload_offset),
264 payload_properties.end()), 260 "--headers=" + std::string(payload_properties.begin(), payload_properties.end()),
265 android::base::StringPrintf("--status_fd=%d", status_fd), 261 android::base::StringPrintf("--status_fd=%d", status_fd),
266 }; 262 };
267 return 0; 263 return 0;
268} 264}
269 265
270#else // !AB_OTA_UPDATER 266#else // !AB_OTA_UPDATER
271 267
272static int 268int update_binary_command(const std::string& path, ZipArchiveHandle zip, int retry_count,
273update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count, 269 int status_fd, std::vector<std::string>* cmd) {
274 int status_fd, std::vector<std::string>* cmd) 270 CHECK(cmd != nullptr);
275{
276 // On traditional updates we extract the update binary from the package.
277 ZipString binary_name(ASSUMED_UPDATE_BINARY_NAME);
278 ZipEntry binary_entry;
279 if (FindEntry(zip, binary_name, &binary_entry) != 0) {
280 return INSTALL_CORRUPT;
281 }
282 271
283 const char* binary = "/tmp/update_binary"; 272 // On traditional updates we extract the update binary from the package.
284 unlink(binary); 273 static constexpr const char* UPDATE_BINARY_NAME = "META-INF/com/google/android/update-binary";
285 int fd = creat(binary, 0755); 274 ZipString binary_name(UPDATE_BINARY_NAME);
286 if (fd < 0) { 275 ZipEntry binary_entry;
287 PLOG(ERROR) << "Can't make " << binary; 276 if (FindEntry(zip, binary_name, &binary_entry) != 0) {
288 return INSTALL_ERROR; 277 LOG(ERROR) << "Failed to find update binary " << UPDATE_BINARY_NAME;
289 } 278 return INSTALL_CORRUPT;
290 int error = ExtractEntryToFile(zip, &binary_entry, fd); 279 }
291 close(fd);
292 280
293 if (error != 0) { 281 const char* binary = "/tmp/update_binary";
294 LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME 282 unlink(binary);
295 << " : " << ErrorCodeString(error); 283 int fd = creat(binary, 0755);
296 return INSTALL_ERROR; 284 if (fd == -1) {
297 } 285 PLOG(ERROR) << "Failed to create " << binary;
286 return INSTALL_ERROR;
287 }
298 288
299 *cmd = { 289 int32_t error = ExtractEntryToFile(zip, &binary_entry, fd);
300 binary, 290 close(fd);
301 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk 291 if (error != 0) {
302 std::to_string(status_fd), 292 LOG(ERROR) << "Failed to extract " << UPDATE_BINARY_NAME << ": " << ErrorCodeString(error);
303 path, 293 return INSTALL_ERROR;
304 }; 294 }
305 if (retry_count > 0) 295
306 cmd->push_back("retry"); 296 *cmd = {
307 return 0; 297 binary,
298 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk
299 std::to_string(status_fd),
300 path,
301 };
302 if (retry_count > 0) {
303 cmd->push_back("retry");
304 }
305 return 0;
308} 306}
309#endif // !AB_OTA_UPDATER 307#endif // !AB_OTA_UPDATER
310 308