From bcabd0929316fdd022ea102cc86396547ad9f070 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 22 Mar 2016 20:19:22 -0700 Subject: Switch to . Change-Id: I13ba3f40bd52b5f3e3fe9002a45a9a8630040129 --- uncrypt/uncrypt.cpp | 103 ++++++++++++++++++++++++++-------------------------- 1 file changed, 52 insertions(+), 51 deletions(-) (limited to 'uncrypt') diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index 705744eb6..abbd3e545 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -61,6 +61,7 @@ #include #include #include +#include #include #include #include @@ -69,7 +70,6 @@ #include #include "bootloader.h" -#include "unique_fd.h" #define WINDOW_SIZE 5 @@ -174,8 +174,9 @@ static int produce_block_map(const char* path, const char* map_file, const char* return -1; } std::string tmp_map_file = std::string(map_file) + ".tmp"; - unique_fd mapfd(open(tmp_map_file.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)); - if (!mapfd) { + android::base::unique_fd mapfd(open(tmp_map_file.c_str(), + O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)); + if (mapfd == -1) { ALOGE("failed to open %s: %s\n", tmp_map_file.c_str(), strerror(errno)); return -1; } @@ -201,7 +202,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* std::string s = android::base::StringPrintf("%s\n%" PRId64 " %ld\n", blk_dev, sb.st_size, static_cast(sb.st_blksize)); - if (!android::base::WriteStringToFd(s, mapfd.get())) { + if (!android::base::WriteStringToFd(s, mapfd)) { ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno)); return -1; } @@ -213,16 +214,16 @@ static int produce_block_map(const char* path, const char* map_file, const char* int head_block = 0; int head = 0, tail = 0; - unique_fd fd(open(path, O_RDONLY)); - if (!fd) { + android::base::unique_fd fd(open(path, O_RDONLY)); + if (fd == -1) { ALOGE("failed to open %s for reading: %s", path, strerror(errno)); return -1; } - unique_fd wfd(-1); + android::base::unique_fd wfd; if (encrypted) { - wfd = open(blk_dev, O_WRONLY); - if (!wfd) { + wfd.reset(open(blk_dev, O_WRONLY)); + if (wfd == -1) { ALOGE("failed to open fd for writing: %s", strerror(errno)); return -1; } @@ -241,14 +242,14 @@ static int produce_block_map(const char* path, const char* map_file, const char* if ((tail+1) % WINDOW_SIZE == head) { // write out head buffer int block = head_block; - if (ioctl(fd.get(), FIBMAP, &block) != 0) { + if (ioctl(fd, FIBMAP, &block) != 0) { ALOGE("failed to find block %d", head_block); return -1; } add_block_to_ranges(ranges, block); if (encrypted) { - if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd.get(), - static_cast(sb.st_blksize) * block) != 0) { + if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd, + static_cast(sb.st_blksize) * block) != 0) { return -1; } } @@ -260,7 +261,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* if (encrypted) { size_t to_read = static_cast( std::min(static_cast(sb.st_blksize), sb.st_size - pos)); - if (!android::base::ReadFully(fd.get(), buffers[tail].data(), to_read)) { + if (!android::base::ReadFully(fd, buffers[tail].data(), to_read)) { ALOGE("failed to read: %s", strerror(errno)); return -1; } @@ -277,14 +278,14 @@ static int produce_block_map(const char* path, const char* map_file, const char* while (head != tail) { // write out head buffer int block = head_block; - if (ioctl(fd.get(), FIBMAP, &block) != 0) { + if (ioctl(fd, FIBMAP, &block) != 0) { ALOGE("failed to find block %d", head_block); return -1; } add_block_to_ranges(ranges, block); if (encrypted) { - if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd.get(), - static_cast(sb.st_blksize) * block) != 0) { + if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd, + static_cast(sb.st_blksize) * block) != 0) { return -1; } } @@ -293,38 +294,36 @@ static int produce_block_map(const char* path, const char* map_file, const char* } if (!android::base::WriteStringToFd( - android::base::StringPrintf("%zu\n", ranges.size() / 2), mapfd.get())) { + android::base::StringPrintf("%zu\n", ranges.size() / 2), mapfd)) { ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno)); return -1; } for (size_t i = 0; i < ranges.size(); i += 2) { if (!android::base::WriteStringToFd( - android::base::StringPrintf("%d %d\n", ranges[i], ranges[i+1]), mapfd.get())) { + android::base::StringPrintf("%d %d\n", ranges[i], ranges[i+1]), mapfd)) { ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno)); return -1; } } - if (fsync(mapfd.get()) == -1) { + if (fsync(mapfd) == -1) { ALOGE("failed to fsync \"%s\": %s", tmp_map_file.c_str(), strerror(errno)); return -1; } - if (close(mapfd.get() == -1)) { + if (close(mapfd.release()) == -1) { ALOGE("failed to close %s: %s", tmp_map_file.c_str(), strerror(errno)); return -1; } - mapfd = -1; if (encrypted) { - if (fsync(wfd.get()) == -1) { + if (fsync(wfd) == -1) { ALOGE("failed to fsync \"%s\": %s", blk_dev, strerror(errno)); return -1; } - if (close(wfd.get()) == -1) { + if (close(wfd.release()) == -1) { ALOGE("failed to close %s: %s", blk_dev, strerror(errno)); return -1; } - wfd = -1; } if (rename(tmp_map_file.c_str(), map_file) == -1) { @@ -334,20 +333,19 @@ static int produce_block_map(const char* path, const char* map_file, const char* // Sync dir to make rename() result written to disk. std::string file_name = map_file; std::string dir_name = dirname(&file_name[0]); - unique_fd dfd(open(dir_name.c_str(), O_RDONLY | O_DIRECTORY)); - if (!dfd) { + android::base::unique_fd dfd(open(dir_name.c_str(), O_RDONLY | O_DIRECTORY)); + if (dfd == -1) { ALOGE("failed to open dir %s: %s", dir_name.c_str(), strerror(errno)); return -1; } - if (fsync(dfd.get()) == -1) { + if (fsync(dfd) == -1) { ALOGE("failed to fsync %s: %s", dir_name.c_str(), strerror(errno)); return -1; } - if (close(dfd.get() == -1)) { + if (close(dfd.release()) == -1) { ALOGE("failed to close %s: %s", dir_name.c_str(), strerror(errno)); return -1; } - dfd = -1; return 0; } @@ -371,12 +369,12 @@ static int read_bootloader_message(bootloader_message* out) { ALOGE("failed to find /misc partition."); return -1; } - unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY)); - if (!fd) { + android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY)); + if (fd == -1) { ALOGE("failed to open %s: %s", misc_blk_device.c_str(), strerror(errno)); return -1; } - if (!android::base::ReadFully(fd.get(), out, sizeof(*out))) { + if (!android::base::ReadFully(fd, out, sizeof(*out))) { ALOGE("failed to read %s: %s", misc_blk_device.c_str(), strerror(errno)); return -1; } @@ -389,17 +387,17 @@ static int write_bootloader_message(const bootloader_message* in) { ALOGE("failed to find /misc partition."); return -1; } - unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY | O_SYNC)); - if (!fd) { + android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY | O_SYNC)); + if (fd == -1) { ALOGE("failed to open %s: %s", misc_blk_device.c_str(), strerror(errno)); return -1; } - if (!android::base::WriteFully(fd.get(), in, sizeof(*in))) { + if (!android::base::WriteFully(fd, in, sizeof(*in))) { ALOGE("failed to write %s: %s", misc_blk_device.c_str(), strerror(errno)); return -1; } // TODO: O_SYNC and fsync() duplicates each other? - if (fsync(fd.get()) == -1) { + if (fsync(fd) == -1) { ALOGE("failed to fsync %s: %s", misc_blk_device.c_str(), strerror(errno)); return -1; } @@ -465,8 +463,9 @@ static int uncrypt(const char* input_path, const char* map_file, int status_fd) static int uncrypt_wrapper(const char* input_path, const char* map_file, const std::string& status_file) { // The pipe has been created by the system server. - unique_fd status_fd(open(status_file.c_str(), O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR)); - if (!status_fd) { + android::base::unique_fd status_fd(open(status_file.c_str(), + O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR)); + if (status_fd == -1) { ALOGE("failed to open pipe \"%s\": %s", status_file.c_str(), strerror(errno)); return 1; } @@ -474,46 +473,48 @@ static int uncrypt_wrapper(const char* input_path, const char* map_file, std::string package; if (input_path == nullptr) { if (!find_uncrypt_package(UNCRYPT_PATH_FILE, &package)) { - android::base::WriteStringToFd("-1\n", status_fd.get()); + android::base::WriteStringToFd("-1\n", status_fd); return 1; } input_path = package.c_str(); } CHECK(map_file != nullptr); - int status = uncrypt(input_path, map_file, status_fd.get()); + int status = uncrypt(input_path, map_file, status_fd); if (status != 0) { - android::base::WriteStringToFd("-1\n", status_fd.get()); + android::base::WriteStringToFd("-1\n", status_fd); return 1; } - android::base::WriteStringToFd("100\n", status_fd.get()); + android::base::WriteStringToFd("100\n", status_fd); return 0; } static int clear_bcb(const std::string& status_file) { - unique_fd status_fd(open(status_file.c_str(), O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR)); - if (!status_fd) { + android::base::unique_fd status_fd(open(status_file.c_str(), + O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR)); + if (status_fd == -1) { ALOGE("failed to open pipe \"%s\": %s", status_file.c_str(), strerror(errno)); return 1; } bootloader_message boot = {}; if (write_bootloader_message(&boot) != 0) { - android::base::WriteStringToFd("-1\n", status_fd.get()); + android::base::WriteStringToFd("-1\n", status_fd); return 1; } - android::base::WriteStringToFd("100\n", status_fd.get()); + android::base::WriteStringToFd("100\n", status_fd); return 0; } static int setup_bcb(const std::string& command_file, const std::string& status_file) { - unique_fd status_fd(open(status_file.c_str(), O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR)); - if (!status_fd) { + android::base::unique_fd status_fd(open(status_file.c_str(), + O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR)); + if (status_fd == -1) { ALOGE("failed to open pipe \"%s\": %s", status_file.c_str(), strerror(errno)); return 1; } std::string content; if (!android::base::ReadFileToString(command_file, &content)) { ALOGE("failed to read \"%s\": %s", command_file.c_str(), strerror(errno)); - android::base::WriteStringToFd("-1\n", status_fd.get()); + android::base::WriteStringToFd("-1\n", status_fd); return 1; } bootloader_message boot = {}; @@ -522,10 +523,10 @@ static int setup_bcb(const std::string& command_file, const std::string& status_ strlcat(boot.recovery, content.c_str(), sizeof(boot.recovery)); if (write_bootloader_message(&boot) != 0) { ALOGE("failed to set bootloader message"); - android::base::WriteStringToFd("-1\n", status_fd.get()); + android::base::WriteStringToFd("-1\n", status_fd); return 1; } - android::base::WriteStringToFd("100\n", status_fd.get()); + android::base::WriteStringToFd("100\n", status_fd); return 0; } -- cgit v1.2.3 From d6ac68665dcef6c12d44ee6bcd7d938ab5028150 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 29 Mar 2016 12:44:09 -0700 Subject: Fix uncrypt.cpp unique_fd build breakage. Change-Id: I4654f59463d1f3e1f4450e937cd910508b64c157 --- uncrypt/uncrypt.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'uncrypt') diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index 9e3416b03..2986606e1 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -615,20 +615,20 @@ int main(int argc, char** argv) { // c3. The socket is created by init when starting the service. uncrypt // will use the socket to communicate with its caller. - unique_fd service_socket(android_get_control_socket(UNCRYPT_SOCKET.c_str())); - if (!service_socket) { + android::base::unique_fd service_socket(android_get_control_socket(UNCRYPT_SOCKET.c_str())); + if (service_socket == -1) { ALOGE("failed to open socket \"%s\": %s", UNCRYPT_SOCKET.c_str(), strerror(errno)); return 1; } - fcntl(service_socket.get(), F_SETFD, FD_CLOEXEC); + fcntl(service_socket, F_SETFD, FD_CLOEXEC); - if (listen(service_socket.get(), 1) == -1) { + if (listen(service_socket, 1) == -1) { ALOGE("failed to listen on socket %d: %s", service_socket.get(), strerror(errno)); return 1; } - unique_fd socket_fd(accept4(service_socket.get(), nullptr, nullptr, SOCK_CLOEXEC)); - if (!socket_fd) { + android::base::unique_fd socket_fd(accept4(service_socket, nullptr, nullptr, SOCK_CLOEXEC)); + if (socket_fd == -1) { ALOGE("failed to accept on socket %d: %s", service_socket.get(), strerror(errno)); return 1; } @@ -636,13 +636,13 @@ int main(int argc, char** argv) { bool success = false; switch (action) { case UNCRYPT: - success = uncrypt_wrapper(input_path, map_file, socket_fd.get()); + success = uncrypt_wrapper(input_path, map_file, socket_fd); break; case SETUP_BCB: - success = setup_bcb(socket_fd.get()); + success = setup_bcb(socket_fd); break; case CLEAR_BCB: - success = clear_bcb(socket_fd.get()); + success = clear_bcb(socket_fd); break; default: // Should never happen. ALOGE("Invalid uncrypt action code: %d", action); @@ -653,7 +653,7 @@ int main(int argc, char** argv) { // ensure the client to receive the last status code before the socket gets // destroyed. int code; - if (android::base::ReadFully(socket_fd.get(), &code, 4)) { + if (android::base::ReadFully(socket_fd, &code, 4)) { ALOGI(" received %d, exiting now", code); } else { ALOGE("failed to read the code: %s", strerror(errno)); -- cgit v1.2.3 From 6507265906084c59ba6b80c010f06be312bd23c1 Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Tue, 29 Mar 2016 14:33:35 -0700 Subject: uncrypt: remove --read-bcb option. Bug: 27897241 Change-Id: I4f52ada58e8f204dba8c974ea0ae03876411ecf0 (cherry picked from commit 61799baba3631f55469d2754542130255ce790cf) --- uncrypt/uncrypt.cpp | 32 -------------------------------- 1 file changed, 32 deletions(-) (limited to 'uncrypt') diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index abbd3e545..43a2c2ab4 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -363,24 +363,6 @@ static std::string get_misc_blk_device() { return ""; } -static int read_bootloader_message(bootloader_message* out) { - std::string misc_blk_device = get_misc_blk_device(); - if (misc_blk_device.empty()) { - ALOGE("failed to find /misc partition."); - return -1; - } - android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY)); - if (fd == -1) { - ALOGE("failed to open %s: %s", misc_blk_device.c_str(), strerror(errno)); - return -1; - } - if (!android::base::ReadFully(fd, out, sizeof(*out))) { - ALOGE("failed to read %s: %s", misc_blk_device.c_str(), strerror(errno)); - return -1; - } - return 0; -} - static int write_bootloader_message(const bootloader_message* in) { std::string misc_blk_device = get_misc_blk_device(); if (misc_blk_device.empty()) { @@ -530,24 +512,12 @@ static int setup_bcb(const std::string& command_file, const std::string& status_ return 0; } -static int read_bcb() { - bootloader_message boot; - if (read_bootloader_message(&boot) != 0) { - ALOGE("failed to get bootloader message"); - return 1; - } - printf("bcb command: %s\n", boot.command); - printf("bcb recovery:\n%s\n", boot.recovery); - return 0; -} - static void usage(const char* exename) { fprintf(stderr, "Usage of %s:\n", exename); fprintf(stderr, "%s [ ] Uncrypt ota package.\n", exename); fprintf(stderr, "%s --reboot Clear BCB data and reboot to recovery.\n", exename); fprintf(stderr, "%s --clear-bcb Clear BCB data in misc partition.\n", exename); fprintf(stderr, "%s --setup-bcb Setup BCB data by command file.\n", exename); - fprintf(stderr, "%s --read-bcb Read BCB data from misc partition.\n", exename); } int main(int argc, char** argv) { @@ -558,8 +528,6 @@ int main(int argc, char** argv) { return clear_bcb(STATUS_FILE); } else if (strcmp(argv[1], "--setup-bcb") == 0) { return setup_bcb(COMMAND_FILE, STATUS_FILE); - } else if (strcmp(argv[1], "--read-bcb") == 0) { - return read_bcb(); } } else if (argc == 1 || argc == 3) { const char* input_path = nullptr; -- cgit v1.2.3 From 54a2747ef305c10d07d8db393125dbcbb461c428 Mon Sep 17 00:00:00 2001 From: Chih-Hung Hsieh Date: Mon, 18 Apr 2016 11:30:55 -0700 Subject: Fix google-runtime-int warnings. Bug: 28220065 Change-Id: Ida199c66692a1638be6990d583d2ed42583fb592 --- uncrypt/uncrypt.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'uncrypt') diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index 43a2c2ab4..a1de6a182 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -200,8 +200,9 @@ static int produce_block_map(const char* path, const char* map_file, const char* std::vector ranges; - std::string s = android::base::StringPrintf("%s\n%" PRId64 " %ld\n", - blk_dev, sb.st_size, static_cast(sb.st_blksize)); + std::string s = android::base::StringPrintf("%s\n%" PRId64 " %" PRId64 "\n", + blk_dev, static_cast(sb.st_size), + static_cast(sb.st_blksize)); if (!android::base::WriteStringToFd(s, mapfd)) { ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno)); return -1; -- cgit v1.2.3 From 28c1e5d3aa9610db6e141380b1435937fc7f07db Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Thu, 30 Jun 2016 15:31:51 -0700 Subject: Allow uncrypt to work without socket communication It was inconvenient to uncrypt a update package under adb shell because the uncrypt executable required a socket to start its job. Add a workaround to allow uncrypt executes without socket communication. Test: run uncrypt under adb shell, and the block map generates successfully Bug: 29906218 Change-Id: Ibc328b31636d925dc429ede8dcec7392a721dd53 --- uncrypt/uncrypt.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'uncrypt') diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index c19943fa7..ba45946aa 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -210,6 +210,11 @@ static const char* find_block_device(const char* path, bool* encryptable, bool* } static bool write_status_to_socket(int status, int socket) { + // If socket equals -1, uncrypt is in debug mode without socket communication. + // Skip writing and return success. + if (socket == -1) { + return true; + } int status_out = htonl(status); return android::base::WriteFully(socket, &status_out, sizeof(int)); } @@ -539,7 +544,7 @@ static void usage(const char* exename) { } int main(int argc, char** argv) { - enum { UNCRYPT, SETUP_BCB, CLEAR_BCB } action; + enum { UNCRYPT, SETUP_BCB, CLEAR_BCB, UNCRYPT_DEBUG } action; const char* input_path = nullptr; const char* map_file = CACHE_BLOCK_MAP.c_str(); @@ -552,7 +557,7 @@ int main(int argc, char** argv) { } else if (argc == 3) { input_path = argv[1]; map_file = argv[2]; - action = UNCRYPT; + action = UNCRYPT_DEBUG; } else { usage(argv[0]); return 2; @@ -562,6 +567,17 @@ int main(int argc, char** argv) { return 1; } + if (action == UNCRYPT_DEBUG) { + ALOGI("uncrypt called in debug mode, skip socket communication\n"); + bool success = uncrypt_wrapper(input_path, map_file, -1); + if (success) { + ALOGI("uncrypt succeeded\n"); + } else{ + ALOGI("uncrypt failed\n"); + } + return success ? 0 : 1; + } + // c3. The socket is created by init when starting the service. uncrypt // will use the socket to communicate with its caller. android::base::unique_fd service_socket(android_get_control_socket(UNCRYPT_SOCKET.c_str())); -- cgit v1.2.3 From 747781433fb01f745529c7e9dd97c5599070ad0d Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Fri, 5 Aug 2016 18:00:04 -0700 Subject: Switch recovery to libbase logging Clean up the recovery image and switch to libbase logging. Bug: 28191554 Change-Id: Icd999c3cc832f0639f204b5c36cea8afe303ad35 --- uncrypt/uncrypt.cpp | 101 +++++++++++++++++++++++++--------------------------- 1 file changed, 49 insertions(+), 52 deletions(-) (limited to 'uncrypt') diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index ba45946aa..993441a5d 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -116,9 +116,6 @@ #include #include -#define LOG_TAG "uncrypt" -#include - #define WINDOW_SIZE 5 // uncrypt provides three services: SETUP_BCB, CLEAR_BCB and UNCRYPT. @@ -139,11 +136,11 @@ static struct fstab* fstab = nullptr; static int write_at_offset(unsigned char* buffer, size_t size, int wfd, off64_t offset) { if (TEMP_FAILURE_RETRY(lseek64(wfd, offset, SEEK_SET)) == -1) { - ALOGE("error seeking to offset %" PRId64 ": %s", offset, strerror(errno)); + PLOG(ERROR) << "error seeking to offset " << offset; return -1; } if (!android::base::WriteFully(wfd, buffer, size)) { - ALOGE("error writing offset %" PRId64 ": %s", offset, strerror(errno)); + PLOG(ERROR) << "error writing offset " << offset; return -1; } return 0; @@ -167,13 +164,13 @@ static struct fstab* read_fstab() { // The fstab path is always "/fstab.${ro.hardware}". char fstab_path[PATH_MAX+1] = "/fstab."; if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) { - ALOGE("failed to get ro.hardware"); + LOG(ERROR) << "failed to get ro.hardware"; return NULL; } fstab = fs_mgr_read_fstab(fstab_path); if (!fstab) { - ALOGE("failed to read %s", fstab_path); + LOG(ERROR) << "failed to read " << fstab_path; return NULL; } @@ -224,7 +221,7 @@ static bool find_uncrypt_package(const std::string& uncrypt_path_file, std::stri CHECK(package_name != nullptr); std::string uncrypt_path; if (!android::base::ReadFileToString(uncrypt_path_file, &uncrypt_path)) { - ALOGE("failed to open \"%s\": %s", uncrypt_path_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to open \"" << uncrypt_path_file << "\""; return false; } @@ -237,33 +234,33 @@ static int produce_block_map(const char* path, const char* map_file, const char* bool encrypted, int socket) { std::string err; if (!android::base::RemoveFileIfExists(map_file, &err)) { - ALOGE("failed to remove the existing map file %s: %s", map_file, err.c_str()); + LOG(ERROR) << "failed to remove the existing map file " << map_file << ": " << err; return -1; } std::string tmp_map_file = std::string(map_file) + ".tmp"; android::base::unique_fd mapfd(open(tmp_map_file.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)); if (mapfd == -1) { - ALOGE("failed to open %s: %s\n", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to open " << tmp_map_file; return -1; } // Make sure we can write to the socket. if (!write_status_to_socket(0, socket)) { - ALOGE("failed to write to socket %d\n", socket); + LOG(ERROR) << "failed to write to socket " << socket; return -1; } struct stat sb; if (stat(path, &sb) != 0) { - ALOGE("failed to stat %s", path); + LOG(ERROR) << "failed to stat " << path; return -1; } - ALOGI(" block size: %ld bytes", static_cast(sb.st_blksize)); + LOG(INFO) << " block size: " << sb.st_blksize << " bytes"; int blocks = ((sb.st_size-1) / sb.st_blksize) + 1; - ALOGI(" file size: %" PRId64 " bytes, %d blocks", sb.st_size, blocks); + LOG(INFO) << " file size: " << sb.st_size << " bytes, " << blocks << " blocks"; std::vector ranges; @@ -271,7 +268,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* blk_dev, static_cast(sb.st_size), static_cast(sb.st_blksize)); if (!android::base::WriteStringToFd(s, mapfd)) { - ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to write " << tmp_map_file; return -1; } @@ -284,7 +281,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* android::base::unique_fd fd(open(path, O_RDONLY)); if (fd == -1) { - ALOGE("failed to open %s for reading: %s", path, strerror(errno)); + PLOG(ERROR) << "failed to open " << path << " for reading"; return -1; } @@ -292,7 +289,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* if (encrypted) { wfd.reset(open(blk_dev, O_WRONLY)); if (wfd == -1) { - ALOGE("failed to open fd for writing: %s", strerror(errno)); + PLOG(ERROR) << "failed to open " << blk_dev << " for writing"; return -1; } } @@ -311,7 +308,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* // write out head buffer int block = head_block; if (ioctl(fd, FIBMAP, &block) != 0) { - ALOGE("failed to find block %d", head_block); + LOG(ERROR) << "failed to find block " << head_block; return -1; } add_block_to_ranges(ranges, block); @@ -330,7 +327,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* size_t to_read = static_cast( std::min(static_cast(sb.st_blksize), sb.st_size - pos)); if (!android::base::ReadFully(fd, buffers[tail].data(), to_read)) { - ALOGE("failed to read: %s", strerror(errno)); + PLOG(ERROR) << "failed to read " << path; return -1; } pos += to_read; @@ -347,7 +344,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* // write out head buffer int block = head_block; if (ioctl(fd, FIBMAP, &block) != 0) { - ALOGE("failed to find block %d", head_block); + LOG(ERROR) << "failed to find block " << head_block; return -1; } add_block_to_ranges(ranges, block); @@ -363,39 +360,39 @@ static int produce_block_map(const char* path, const char* map_file, const char* if (!android::base::WriteStringToFd( android::base::StringPrintf("%zu\n", ranges.size() / 2), mapfd)) { - ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to write " << tmp_map_file; return -1; } for (size_t i = 0; i < ranges.size(); i += 2) { if (!android::base::WriteStringToFd( android::base::StringPrintf("%d %d\n", ranges[i], ranges[i+1]), mapfd)) { - ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to write " << tmp_map_file; return -1; } } if (fsync(mapfd) == -1) { - ALOGE("failed to fsync \"%s\": %s", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to fsync \"" << tmp_map_file << "\""; return -1; } if (close(mapfd.release()) == -1) { - ALOGE("failed to close %s: %s", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to close " << tmp_map_file; return -1; } if (encrypted) { if (fsync(wfd) == -1) { - ALOGE("failed to fsync \"%s\": %s", blk_dev, strerror(errno)); + PLOG(ERROR) << "failed to fsync \"" << blk_dev << "\""; return -1; } if (close(wfd.release()) == -1) { - ALOGE("failed to close %s: %s", blk_dev, strerror(errno)); + PLOG(ERROR) << "failed to close " << blk_dev; return -1; } } if (rename(tmp_map_file.c_str(), map_file) == -1) { - ALOGE("failed to rename %s to %s: %s", tmp_map_file.c_str(), map_file, strerror(errno)); + PLOG(ERROR) << "failed to rename " << tmp_map_file << " to " << map_file; return -1; } // Sync dir to make rename() result written to disk. @@ -403,28 +400,28 @@ static int produce_block_map(const char* path, const char* map_file, const char* std::string dir_name = dirname(&file_name[0]); android::base::unique_fd dfd(open(dir_name.c_str(), O_RDONLY | O_DIRECTORY)); if (dfd == -1) { - ALOGE("failed to open dir %s: %s", dir_name.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to open dir " << dir_name; return -1; } if (fsync(dfd) == -1) { - ALOGE("failed to fsync %s: %s", dir_name.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to fsync " << dir_name; return -1; } if (close(dfd.release()) == -1) { - ALOGE("failed to close %s: %s", dir_name.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to close " << dir_name; return -1; } return 0; } static int uncrypt(const char* input_path, const char* map_file, const int socket) { - ALOGI("update package is \"%s\"", input_path); + LOG(INFO) << "update package is \"" << input_path << "\""; // Turn the name of the file we're supposed to convert into an // absolute path, so we can find what filesystem it's on. char path[PATH_MAX+1]; if (realpath(input_path, path) == NULL) { - ALOGE("failed to convert \"%s\" to absolute path: %s", input_path, strerror(errno)); + PLOG(ERROR) << "failed to convert \"" << input_path << "\" to absolute path"; return 1; } @@ -432,15 +429,15 @@ static int uncrypt(const char* input_path, const char* map_file, const int socke bool encrypted; const char* blk_dev = find_block_device(path, &encryptable, &encrypted); if (blk_dev == NULL) { - ALOGE("failed to find block device for %s", path); + LOG(ERROR) << "failed to find block device for " << path; return 1; } // If the filesystem it's on isn't encrypted, we only produce the // block map, we don't rewrite the file contents (it would be // pointless to do so). - ALOGI("encryptable: %s", encryptable ? "yes" : "no"); - ALOGI(" encrypted: %s", encrypted ? "yes" : "no"); + LOG(INFO) << "encryptable: " << (encryptable ? "yes" : "no"); + LOG(INFO) << " encrypted: " << (encrypted ? "yes" : "no"); // Recovery supports installing packages from 3 paths: /cache, // /data, and /sdcard. (On a particular device, other locations @@ -450,7 +447,7 @@ static int uncrypt(const char* input_path, const char* map_file, const int socke // can read the package without mounting the partition. On /cache // and /sdcard we leave the file alone. if (strncmp(path, "/data/", 6) == 0) { - ALOGI("writing block map %s", map_file); + LOG(INFO) << "writing block map " << map_file; if (produce_block_map(path, map_file, blk_dev, encrypted, socket) != 0) { return 1; } @@ -481,7 +478,7 @@ static bool uncrypt_wrapper(const char* input_path, const char* map_file, const static bool clear_bcb(const int socket) { std::string err; if (!clear_bootloader_message(&err)) { - ALOGE("failed to clear bootloader message: %s", err.c_str()); + LOG(ERROR) << "failed to clear bootloader message: " << err; write_status_to_socket(-1, socket); return false; } @@ -493,7 +490,7 @@ static bool setup_bcb(const int socket) { // c5. receive message length int length; if (!android::base::ReadFully(socket, &length, 4)) { - ALOGE("failed to read the length: %s", strerror(errno)); + PLOG(ERROR) << "failed to read the length"; return false; } length = ntohl(length); @@ -502,17 +499,17 @@ static bool setup_bcb(const int socket) { std::string content; content.resize(length); if (!android::base::ReadFully(socket, &content[0], length)) { - ALOGE("failed to read the length: %s", strerror(errno)); + PLOG(ERROR) << "failed to read the length"; return false; } - ALOGI(" received command: [%s] (%zu)", content.c_str(), content.size()); + LOG(INFO) << " received command: [" << content << "] (" << content.size() << ")"; std::vector options = android::base::Split(content, "\n"); std::string wipe_package; for (auto& option : options) { if (android::base::StartsWith(option, "--wipe_package=")) { std::string path = option.substr(strlen("--wipe_package=")); if (!android::base::ReadFileToString(path, &wipe_package)) { - ALOGE("failed to read %s: %s", path.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to read " << path; return false; } option = android::base::StringPrintf("--wipe_package_size=%zu", wipe_package.size()); @@ -522,12 +519,12 @@ static bool setup_bcb(const int socket) { // c8. setup the bcb command std::string err; if (!write_bootloader_message(options, &err)) { - ALOGE("failed to set bootloader message: %s", err.c_str()); + LOG(ERROR) << "failed to set bootloader message: " << err; write_status_to_socket(-1, socket); return false; } if (!wipe_package.empty() && !write_wipe_package(wipe_package, &err)) { - ALOGE("failed to set wipe package: %s", err.c_str()); + PLOG(ERROR) << "failed to set wipe package: " << err; write_status_to_socket(-1, socket); return false; } @@ -568,12 +565,12 @@ int main(int argc, char** argv) { } if (action == UNCRYPT_DEBUG) { - ALOGI("uncrypt called in debug mode, skip socket communication\n"); + LOG(INFO) << "uncrypt called in debug mode, skip socket communication"; bool success = uncrypt_wrapper(input_path, map_file, -1); if (success) { - ALOGI("uncrypt succeeded\n"); + LOG(INFO) << "uncrypt succeeded"; } else{ - ALOGI("uncrypt failed\n"); + LOG(INFO) << "uncrypt failed"; } return success ? 0 : 1; } @@ -582,19 +579,19 @@ int main(int argc, char** argv) { // will use the socket to communicate with its caller. android::base::unique_fd service_socket(android_get_control_socket(UNCRYPT_SOCKET.c_str())); if (service_socket == -1) { - ALOGE("failed to open socket \"%s\": %s", UNCRYPT_SOCKET.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to open socket \"" << UNCRYPT_SOCKET << "\""; return 1; } fcntl(service_socket, F_SETFD, FD_CLOEXEC); if (listen(service_socket, 1) == -1) { - ALOGE("failed to listen on socket %d: %s", service_socket.get(), strerror(errno)); + PLOG(ERROR) << "failed to listen on socket " << service_socket.get(); return 1; } android::base::unique_fd socket_fd(accept4(service_socket, nullptr, nullptr, SOCK_CLOEXEC)); if (socket_fd == -1) { - ALOGE("failed to accept on socket %d: %s", service_socket.get(), strerror(errno)); + PLOG(ERROR) << "failed to accept on socket " << service_socket.get(); return 1; } @@ -610,7 +607,7 @@ int main(int argc, char** argv) { success = clear_bcb(socket_fd); break; default: // Should never happen. - ALOGE("Invalid uncrypt action code: %d", action); + LOG(ERROR) << "Invalid uncrypt action code: " << action; return 1; } @@ -619,9 +616,9 @@ int main(int argc, char** argv) { // destroyed. int code; if (android::base::ReadFully(socket_fd, &code, 4)) { - ALOGI(" received %d, exiting now", code); + LOG(INFO) << " received " << code << ", exiting now"; } else { - ALOGE("failed to read the code: %s", strerror(errno)); + PLOG(ERROR) << "failed to read the code"; } return success ? 0 : 1; } -- cgit v1.2.3 From 7b0ad9c638176dc364dabb65b363536055a0ea9c Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Fri, 5 Aug 2016 18:00:04 -0700 Subject: Switch recovery to libbase logging Clean up the recovery image and switch to libbase logging. Bug: 28191554 Change-Id: Icd999c3cc832f0639f204b5c36cea8afe303ad35 Merged-In: Icd999c3cc832f0639f204b5c36cea8afe303ad35 --- uncrypt/uncrypt.cpp | 91 ++++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 47 deletions(-) (limited to 'uncrypt') diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index 5697712aa..7adf88cac 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -116,9 +116,6 @@ #include #include -#define LOG_TAG "uncrypt" -#include - #define WINDOW_SIZE 5 // uncrypt provides three services: SETUP_BCB, CLEAR_BCB and UNCRYPT. @@ -139,11 +136,11 @@ static struct fstab* fstab = nullptr; static int write_at_offset(unsigned char* buffer, size_t size, int wfd, off64_t offset) { if (TEMP_FAILURE_RETRY(lseek64(wfd, offset, SEEK_SET)) == -1) { - ALOGE("error seeking to offset %" PRId64 ": %s", offset, strerror(errno)); + PLOG(ERROR) << "error seeking to offset " << offset; return -1; } if (!android::base::WriteFully(wfd, buffer, size)) { - ALOGE("error writing offset %" PRId64 ": %s", offset, strerror(errno)); + PLOG(ERROR) << "error writing offset " << offset; return -1; } return 0; @@ -167,13 +164,13 @@ static struct fstab* read_fstab() { // The fstab path is always "/fstab.${ro.hardware}". char fstab_path[PATH_MAX+1] = "/fstab."; if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) { - ALOGE("failed to get ro.hardware"); + LOG(ERROR) << "failed to get ro.hardware"; return NULL; } fstab = fs_mgr_read_fstab(fstab_path); if (!fstab) { - ALOGE("failed to read %s", fstab_path); + LOG(ERROR) << "failed to read " << fstab_path; return NULL; } @@ -219,7 +216,7 @@ static bool find_uncrypt_package(const std::string& uncrypt_path_file, std::stri CHECK(package_name != nullptr); std::string uncrypt_path; if (!android::base::ReadFileToString(uncrypt_path_file, &uncrypt_path)) { - ALOGE("failed to open \"%s\": %s", uncrypt_path_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to open \"" << uncrypt_path_file << "\""; return false; } @@ -232,33 +229,33 @@ static int produce_block_map(const char* path, const char* map_file, const char* bool encrypted, int socket) { std::string err; if (!android::base::RemoveFileIfExists(map_file, &err)) { - ALOGE("failed to remove the existing map file %s: %s", map_file, err.c_str()); + LOG(ERROR) << "failed to remove the existing map file " << map_file << ": " << err; return -1; } std::string tmp_map_file = std::string(map_file) + ".tmp"; android::base::unique_fd mapfd(open(tmp_map_file.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)); if (mapfd == -1) { - ALOGE("failed to open %s: %s\n", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to open " << tmp_map_file; return -1; } // Make sure we can write to the socket. if (!write_status_to_socket(0, socket)) { - ALOGE("failed to write to socket %d\n", socket); + LOG(ERROR) << "failed to write to socket " << socket; return -1; } struct stat sb; if (stat(path, &sb) != 0) { - ALOGE("failed to stat %s", path); + LOG(ERROR) << "failed to stat " << path; return -1; } - ALOGI(" block size: %ld bytes", static_cast(sb.st_blksize)); + LOG(INFO) << " block size: " << sb.st_blksize << " bytes"; int blocks = ((sb.st_size-1) / sb.st_blksize) + 1; - ALOGI(" file size: %" PRId64 " bytes, %d blocks", sb.st_size, blocks); + LOG(INFO) << " file size: " << sb.st_size << " bytes, " << blocks << " blocks"; std::vector ranges; @@ -266,7 +263,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* blk_dev, static_cast(sb.st_size), static_cast(sb.st_blksize)); if (!android::base::WriteStringToFd(s, mapfd)) { - ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to write " << tmp_map_file; return -1; } @@ -279,7 +276,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* android::base::unique_fd fd(open(path, O_RDONLY)); if (fd == -1) { - ALOGE("failed to open %s for reading: %s", path, strerror(errno)); + PLOG(ERROR) << "failed to open " << path << " for reading"; return -1; } @@ -287,7 +284,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* if (encrypted) { wfd.reset(open(blk_dev, O_WRONLY)); if (wfd == -1) { - ALOGE("failed to open fd for writing: %s", strerror(errno)); + PLOG(ERROR) << "failed to open " << blk_dev << " for writing"; return -1; } } @@ -306,7 +303,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* // write out head buffer int block = head_block; if (ioctl(fd, FIBMAP, &block) != 0) { - ALOGE("failed to find block %d", head_block); + LOG(ERROR) << "failed to find block " << head_block; return -1; } add_block_to_ranges(ranges, block); @@ -325,7 +322,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* size_t to_read = static_cast( std::min(static_cast(sb.st_blksize), sb.st_size - pos)); if (!android::base::ReadFully(fd, buffers[tail].data(), to_read)) { - ALOGE("failed to read: %s", strerror(errno)); + PLOG(ERROR) << "failed to read " << path; return -1; } pos += to_read; @@ -342,7 +339,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* // write out head buffer int block = head_block; if (ioctl(fd, FIBMAP, &block) != 0) { - ALOGE("failed to find block %d", head_block); + LOG(ERROR) << "failed to find block " << head_block; return -1; } add_block_to_ranges(ranges, block); @@ -358,39 +355,39 @@ static int produce_block_map(const char* path, const char* map_file, const char* if (!android::base::WriteStringToFd( android::base::StringPrintf("%zu\n", ranges.size() / 2), mapfd)) { - ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to write " << tmp_map_file; return -1; } for (size_t i = 0; i < ranges.size(); i += 2) { if (!android::base::WriteStringToFd( android::base::StringPrintf("%d %d\n", ranges[i], ranges[i+1]), mapfd)) { - ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to write " << tmp_map_file; return -1; } } if (fsync(mapfd) == -1) { - ALOGE("failed to fsync \"%s\": %s", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to fsync \"" << tmp_map_file << "\""; return -1; } if (close(mapfd.release()) == -1) { - ALOGE("failed to close %s: %s", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to close " << tmp_map_file; return -1; } if (encrypted) { if (fsync(wfd) == -1) { - ALOGE("failed to fsync \"%s\": %s", blk_dev, strerror(errno)); + PLOG(ERROR) << "failed to fsync \"" << blk_dev << "\""; return -1; } if (close(wfd.release()) == -1) { - ALOGE("failed to close %s: %s", blk_dev, strerror(errno)); + PLOG(ERROR) << "failed to close " << blk_dev; return -1; } } if (rename(tmp_map_file.c_str(), map_file) == -1) { - ALOGE("failed to rename %s to %s: %s", tmp_map_file.c_str(), map_file, strerror(errno)); + PLOG(ERROR) << "failed to rename " << tmp_map_file << " to " << map_file; return -1; } // Sync dir to make rename() result written to disk. @@ -398,28 +395,28 @@ static int produce_block_map(const char* path, const char* map_file, const char* std::string dir_name = dirname(&file_name[0]); android::base::unique_fd dfd(open(dir_name.c_str(), O_RDONLY | O_DIRECTORY)); if (dfd == -1) { - ALOGE("failed to open dir %s: %s", dir_name.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to open dir " << dir_name; return -1; } if (fsync(dfd) == -1) { - ALOGE("failed to fsync %s: %s", dir_name.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to fsync " << dir_name; return -1; } if (close(dfd.release()) == -1) { - ALOGE("failed to close %s: %s", dir_name.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to close " << dir_name; return -1; } return 0; } static int uncrypt(const char* input_path, const char* map_file, const int socket) { - ALOGI("update package is \"%s\"", input_path); + LOG(INFO) << "update package is \"" << input_path << "\""; // Turn the name of the file we're supposed to convert into an // absolute path, so we can find what filesystem it's on. char path[PATH_MAX+1]; if (realpath(input_path, path) == NULL) { - ALOGE("failed to convert \"%s\" to absolute path: %s", input_path, strerror(errno)); + PLOG(ERROR) << "failed to convert \"" << input_path << "\" to absolute path"; return 1; } @@ -427,15 +424,15 @@ static int uncrypt(const char* input_path, const char* map_file, const int socke bool encrypted; const char* blk_dev = find_block_device(path, &encryptable, &encrypted); if (blk_dev == NULL) { - ALOGE("failed to find block device for %s", path); + LOG(ERROR) << "failed to find block device for " << path; return 1; } // If the filesystem it's on isn't encrypted, we only produce the // block map, we don't rewrite the file contents (it would be // pointless to do so). - ALOGI("encryptable: %s", encryptable ? "yes" : "no"); - ALOGI(" encrypted: %s", encrypted ? "yes" : "no"); + LOG(INFO) << "encryptable: " << (encryptable ? "yes" : "no"); + LOG(INFO) << " encrypted: " << (encrypted ? "yes" : "no"); // Recovery supports installing packages from 3 paths: /cache, // /data, and /sdcard. (On a particular device, other locations @@ -445,7 +442,7 @@ static int uncrypt(const char* input_path, const char* map_file, const int socke // can read the package without mounting the partition. On /cache // and /sdcard we leave the file alone. if (strncmp(path, "/data/", 6) == 0) { - ALOGI("writing block map %s", map_file); + LOG(INFO) << "writing block map " << map_file; if (produce_block_map(path, map_file, blk_dev, encrypted, socket) != 0) { return 1; } @@ -476,7 +473,7 @@ static bool uncrypt_wrapper(const char* input_path, const char* map_file, const static bool clear_bcb(const int socket) { std::string err; if (!clear_bootloader_message(&err)) { - ALOGE("failed to clear bootloader message: %s", err.c_str()); + LOG(ERROR) << "failed to clear bootloader message: " << err; write_status_to_socket(-1, socket); return false; } @@ -488,7 +485,7 @@ static bool setup_bcb(const int socket) { // c5. receive message length int length; if (!android::base::ReadFully(socket, &length, 4)) { - ALOGE("failed to read the length: %s", strerror(errno)); + PLOG(ERROR) << "failed to read the length"; return false; } length = ntohl(length); @@ -497,15 +494,15 @@ static bool setup_bcb(const int socket) { std::string content; content.resize(length); if (!android::base::ReadFully(socket, &content[0], length)) { - ALOGE("failed to read the length: %s", strerror(errno)); + PLOG(ERROR) << "failed to read the length"; return false; } - ALOGI(" received command: [%s] (%zu)", content.c_str(), content.size()); + LOG(INFO) << " received command: [" << content << "] (" << content.size() << ")"; // c8. setup the bcb command std::string err; if (!write_bootloader_message({content}, &err)) { - ALOGE("failed to set bootloader message: %s", err.c_str()); + LOG(ERROR) << "failed to set bootloader message: " << err; write_status_to_socket(-1, socket); return false; } @@ -549,19 +546,19 @@ int main(int argc, char** argv) { // will use the socket to communicate with its caller. android::base::unique_fd service_socket(android_get_control_socket(UNCRYPT_SOCKET.c_str())); if (service_socket == -1) { - ALOGE("failed to open socket \"%s\": %s", UNCRYPT_SOCKET.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to open socket \"" << UNCRYPT_SOCKET << "\""; return 1; } fcntl(service_socket, F_SETFD, FD_CLOEXEC); if (listen(service_socket, 1) == -1) { - ALOGE("failed to listen on socket %d: %s", service_socket.get(), strerror(errno)); + PLOG(ERROR) << "failed to listen on socket " << service_socket.get(); return 1; } android::base::unique_fd socket_fd(accept4(service_socket, nullptr, nullptr, SOCK_CLOEXEC)); if (socket_fd == -1) { - ALOGE("failed to accept on socket %d: %s", service_socket.get(), strerror(errno)); + PLOG(ERROR) << "failed to accept on socket " << service_socket.get(); return 1; } @@ -577,7 +574,7 @@ int main(int argc, char** argv) { success = clear_bcb(socket_fd); break; default: // Should never happen. - ALOGE("Invalid uncrypt action code: %d", action); + LOG(ERROR) << "Invalid uncrypt action code: " << action; return 1; } @@ -586,9 +583,9 @@ int main(int argc, char** argv) { // destroyed. int code; if (android::base::ReadFully(socket_fd, &code, 4)) { - ALOGI(" received %d, exiting now", code); + LOG(INFO) << " received " << code << ", exiting now"; } else { - ALOGE("failed to read the code: %s", strerror(errno)); + PLOG(ERROR) << "failed to read the code"; } return success ? 0 : 1; } -- cgit v1.2.3 From c21edd46541a6e1c3b7de43aa6002ea45b90c64a Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Fri, 5 Aug 2016 18:00:04 -0700 Subject: Switch recovery to libbase logging Clean up the recovery image and switch to libbase logging. Bug: 28191554 Change-Id: Icd999c3cc832f0639f204b5c36cea8afe303ad35 (cherry picked from commit 747781433fb01f745529c7e9dd97c5599070ad0d) --- uncrypt/uncrypt.cpp | 95 ++++++++++++++++++++++++++--------------------------- 1 file changed, 46 insertions(+), 49 deletions(-) (limited to 'uncrypt') diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index c19943fa7..b7867edc5 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -116,9 +116,6 @@ #include #include -#define LOG_TAG "uncrypt" -#include - #define WINDOW_SIZE 5 // uncrypt provides three services: SETUP_BCB, CLEAR_BCB and UNCRYPT. @@ -139,11 +136,11 @@ static struct fstab* fstab = nullptr; static int write_at_offset(unsigned char* buffer, size_t size, int wfd, off64_t offset) { if (TEMP_FAILURE_RETRY(lseek64(wfd, offset, SEEK_SET)) == -1) { - ALOGE("error seeking to offset %" PRId64 ": %s", offset, strerror(errno)); + PLOG(ERROR) << "error seeking to offset " << offset; return -1; } if (!android::base::WriteFully(wfd, buffer, size)) { - ALOGE("error writing offset %" PRId64 ": %s", offset, strerror(errno)); + PLOG(ERROR) << "error writing offset " << offset; return -1; } return 0; @@ -167,13 +164,13 @@ static struct fstab* read_fstab() { // The fstab path is always "/fstab.${ro.hardware}". char fstab_path[PATH_MAX+1] = "/fstab."; if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) { - ALOGE("failed to get ro.hardware"); + LOG(ERROR) << "failed to get ro.hardware"; return NULL; } fstab = fs_mgr_read_fstab(fstab_path); if (!fstab) { - ALOGE("failed to read %s", fstab_path); + LOG(ERROR) << "failed to read " << fstab_path; return NULL; } @@ -219,7 +216,7 @@ static bool find_uncrypt_package(const std::string& uncrypt_path_file, std::stri CHECK(package_name != nullptr); std::string uncrypt_path; if (!android::base::ReadFileToString(uncrypt_path_file, &uncrypt_path)) { - ALOGE("failed to open \"%s\": %s", uncrypt_path_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to open \"" << uncrypt_path_file << "\""; return false; } @@ -232,33 +229,33 @@ static int produce_block_map(const char* path, const char* map_file, const char* bool encrypted, int socket) { std::string err; if (!android::base::RemoveFileIfExists(map_file, &err)) { - ALOGE("failed to remove the existing map file %s: %s", map_file, err.c_str()); + LOG(ERROR) << "failed to remove the existing map file " << map_file << ": " << err; return -1; } std::string tmp_map_file = std::string(map_file) + ".tmp"; android::base::unique_fd mapfd(open(tmp_map_file.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)); if (mapfd == -1) { - ALOGE("failed to open %s: %s\n", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to open " << tmp_map_file; return -1; } // Make sure we can write to the socket. if (!write_status_to_socket(0, socket)) { - ALOGE("failed to write to socket %d\n", socket); + LOG(ERROR) << "failed to write to socket " << socket; return -1; } struct stat sb; if (stat(path, &sb) != 0) { - ALOGE("failed to stat %s", path); + LOG(ERROR) << "failed to stat " << path; return -1; } - ALOGI(" block size: %ld bytes", static_cast(sb.st_blksize)); + LOG(INFO) << " block size: " << sb.st_blksize << " bytes"; int blocks = ((sb.st_size-1) / sb.st_blksize) + 1; - ALOGI(" file size: %" PRId64 " bytes, %d blocks", sb.st_size, blocks); + LOG(INFO) << " file size: " << sb.st_size << " bytes, " << blocks << " blocks"; std::vector ranges; @@ -266,7 +263,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* blk_dev, static_cast(sb.st_size), static_cast(sb.st_blksize)); if (!android::base::WriteStringToFd(s, mapfd)) { - ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to write " << tmp_map_file; return -1; } @@ -279,7 +276,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* android::base::unique_fd fd(open(path, O_RDONLY)); if (fd == -1) { - ALOGE("failed to open %s for reading: %s", path, strerror(errno)); + PLOG(ERROR) << "failed to open " << path << " for reading"; return -1; } @@ -287,7 +284,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* if (encrypted) { wfd.reset(open(blk_dev, O_WRONLY)); if (wfd == -1) { - ALOGE("failed to open fd for writing: %s", strerror(errno)); + PLOG(ERROR) << "failed to open " << blk_dev << " for writing"; return -1; } } @@ -306,7 +303,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* // write out head buffer int block = head_block; if (ioctl(fd, FIBMAP, &block) != 0) { - ALOGE("failed to find block %d", head_block); + LOG(ERROR) << "failed to find block " << head_block; return -1; } add_block_to_ranges(ranges, block); @@ -325,7 +322,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* size_t to_read = static_cast( std::min(static_cast(sb.st_blksize), sb.st_size - pos)); if (!android::base::ReadFully(fd, buffers[tail].data(), to_read)) { - ALOGE("failed to read: %s", strerror(errno)); + PLOG(ERROR) << "failed to read " << path; return -1; } pos += to_read; @@ -342,7 +339,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* // write out head buffer int block = head_block; if (ioctl(fd, FIBMAP, &block) != 0) { - ALOGE("failed to find block %d", head_block); + LOG(ERROR) << "failed to find block " << head_block; return -1; } add_block_to_ranges(ranges, block); @@ -358,39 +355,39 @@ static int produce_block_map(const char* path, const char* map_file, const char* if (!android::base::WriteStringToFd( android::base::StringPrintf("%zu\n", ranges.size() / 2), mapfd)) { - ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to write " << tmp_map_file; return -1; } for (size_t i = 0; i < ranges.size(); i += 2) { if (!android::base::WriteStringToFd( android::base::StringPrintf("%d %d\n", ranges[i], ranges[i+1]), mapfd)) { - ALOGE("failed to write %s: %s", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to write " << tmp_map_file; return -1; } } if (fsync(mapfd) == -1) { - ALOGE("failed to fsync \"%s\": %s", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to fsync \"" << tmp_map_file << "\""; return -1; } if (close(mapfd.release()) == -1) { - ALOGE("failed to close %s: %s", tmp_map_file.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to close " << tmp_map_file; return -1; } if (encrypted) { if (fsync(wfd) == -1) { - ALOGE("failed to fsync \"%s\": %s", blk_dev, strerror(errno)); + PLOG(ERROR) << "failed to fsync \"" << blk_dev << "\""; return -1; } if (close(wfd.release()) == -1) { - ALOGE("failed to close %s: %s", blk_dev, strerror(errno)); + PLOG(ERROR) << "failed to close " << blk_dev; return -1; } } if (rename(tmp_map_file.c_str(), map_file) == -1) { - ALOGE("failed to rename %s to %s: %s", tmp_map_file.c_str(), map_file, strerror(errno)); + PLOG(ERROR) << "failed to rename " << tmp_map_file << " to " << map_file; return -1; } // Sync dir to make rename() result written to disk. @@ -398,28 +395,28 @@ static int produce_block_map(const char* path, const char* map_file, const char* std::string dir_name = dirname(&file_name[0]); android::base::unique_fd dfd(open(dir_name.c_str(), O_RDONLY | O_DIRECTORY)); if (dfd == -1) { - ALOGE("failed to open dir %s: %s", dir_name.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to open dir " << dir_name; return -1; } if (fsync(dfd) == -1) { - ALOGE("failed to fsync %s: %s", dir_name.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to fsync " << dir_name; return -1; } if (close(dfd.release()) == -1) { - ALOGE("failed to close %s: %s", dir_name.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to close " << dir_name; return -1; } return 0; } static int uncrypt(const char* input_path, const char* map_file, const int socket) { - ALOGI("update package is \"%s\"", input_path); + LOG(INFO) << "update package is \"" << input_path << "\""; // Turn the name of the file we're supposed to convert into an // absolute path, so we can find what filesystem it's on. char path[PATH_MAX+1]; if (realpath(input_path, path) == NULL) { - ALOGE("failed to convert \"%s\" to absolute path: %s", input_path, strerror(errno)); + PLOG(ERROR) << "failed to convert \"" << input_path << "\" to absolute path"; return 1; } @@ -427,15 +424,15 @@ static int uncrypt(const char* input_path, const char* map_file, const int socke bool encrypted; const char* blk_dev = find_block_device(path, &encryptable, &encrypted); if (blk_dev == NULL) { - ALOGE("failed to find block device for %s", path); + LOG(ERROR) << "failed to find block device for " << path; return 1; } // If the filesystem it's on isn't encrypted, we only produce the // block map, we don't rewrite the file contents (it would be // pointless to do so). - ALOGI("encryptable: %s", encryptable ? "yes" : "no"); - ALOGI(" encrypted: %s", encrypted ? "yes" : "no"); + LOG(INFO) << "encryptable: " << (encryptable ? "yes" : "no"); + LOG(INFO) << " encrypted: " << (encrypted ? "yes" : "no"); // Recovery supports installing packages from 3 paths: /cache, // /data, and /sdcard. (On a particular device, other locations @@ -445,7 +442,7 @@ static int uncrypt(const char* input_path, const char* map_file, const int socke // can read the package without mounting the partition. On /cache // and /sdcard we leave the file alone. if (strncmp(path, "/data/", 6) == 0) { - ALOGI("writing block map %s", map_file); + LOG(INFO) << "writing block map " << map_file; if (produce_block_map(path, map_file, blk_dev, encrypted, socket) != 0) { return 1; } @@ -476,7 +473,7 @@ static bool uncrypt_wrapper(const char* input_path, const char* map_file, const static bool clear_bcb(const int socket) { std::string err; if (!clear_bootloader_message(&err)) { - ALOGE("failed to clear bootloader message: %s", err.c_str()); + LOG(ERROR) << "failed to clear bootloader message: " << err; write_status_to_socket(-1, socket); return false; } @@ -488,7 +485,7 @@ static bool setup_bcb(const int socket) { // c5. receive message length int length; if (!android::base::ReadFully(socket, &length, 4)) { - ALOGE("failed to read the length: %s", strerror(errno)); + PLOG(ERROR) << "failed to read the length"; return false; } length = ntohl(length); @@ -497,17 +494,17 @@ static bool setup_bcb(const int socket) { std::string content; content.resize(length); if (!android::base::ReadFully(socket, &content[0], length)) { - ALOGE("failed to read the length: %s", strerror(errno)); + PLOG(ERROR) << "failed to read the length"; return false; } - ALOGI(" received command: [%s] (%zu)", content.c_str(), content.size()); + LOG(INFO) << " received command: [" << content << "] (" << content.size() << ")"; std::vector options = android::base::Split(content, "\n"); std::string wipe_package; for (auto& option : options) { if (android::base::StartsWith(option, "--wipe_package=")) { std::string path = option.substr(strlen("--wipe_package=")); if (!android::base::ReadFileToString(path, &wipe_package)) { - ALOGE("failed to read %s: %s", path.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to read " << path; return false; } option = android::base::StringPrintf("--wipe_package_size=%zu", wipe_package.size()); @@ -517,12 +514,12 @@ static bool setup_bcb(const int socket) { // c8. setup the bcb command std::string err; if (!write_bootloader_message(options, &err)) { - ALOGE("failed to set bootloader message: %s", err.c_str()); + LOG(ERROR) << "failed to set bootloader message: " << err; write_status_to_socket(-1, socket); return false; } if (!wipe_package.empty() && !write_wipe_package(wipe_package, &err)) { - ALOGE("failed to set wipe package: %s", err.c_str()); + PLOG(ERROR) << "failed to set wipe package: " << err; write_status_to_socket(-1, socket); return false; } @@ -566,19 +563,19 @@ int main(int argc, char** argv) { // will use the socket to communicate with its caller. android::base::unique_fd service_socket(android_get_control_socket(UNCRYPT_SOCKET.c_str())); if (service_socket == -1) { - ALOGE("failed to open socket \"%s\": %s", UNCRYPT_SOCKET.c_str(), strerror(errno)); + PLOG(ERROR) << "failed to open socket \"" << UNCRYPT_SOCKET << "\""; return 1; } fcntl(service_socket, F_SETFD, FD_CLOEXEC); if (listen(service_socket, 1) == -1) { - ALOGE("failed to listen on socket %d: %s", service_socket.get(), strerror(errno)); + PLOG(ERROR) << "failed to listen on socket " << service_socket.get(); return 1; } android::base::unique_fd socket_fd(accept4(service_socket, nullptr, nullptr, SOCK_CLOEXEC)); if (socket_fd == -1) { - ALOGE("failed to accept on socket %d: %s", service_socket.get(), strerror(errno)); + PLOG(ERROR) << "failed to accept on socket " << service_socket.get(); return 1; } @@ -594,7 +591,7 @@ int main(int argc, char** argv) { success = clear_bcb(socket_fd); break; default: // Should never happen. - ALOGE("Invalid uncrypt action code: %d", action); + LOG(ERROR) << "Invalid uncrypt action code: " << action; return 1; } @@ -603,9 +600,9 @@ int main(int argc, char** argv) { // destroyed. int code; if (android::base::ReadFully(socket_fd, &code, 4)) { - ALOGI(" received %d, exiting now", code); + LOG(INFO) << " received " << code << ", exiting now"; } else { - ALOGE("failed to read the code: %s", strerror(errno)); + PLOG(ERROR) << "failed to read the code"; } return success ? 0 : 1; } -- cgit v1.2.3 From fe16b5ccaf80f6e04d5b722c37c1abd70457ad28 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Fri, 9 Sep 2016 10:55:44 -0700 Subject: save uncrypt status to last_install Save the uncrypt time cost to /cache/recovery/uncrypt_status. Recovery reads the file and saves its contents to last_install. Bug: 31383361 Test: Tested on angler and uncrypt_time reports correctly. Change-Id: I5cd3f7b6ca069d69086d09acfea8fc4f1215c833 Merged-In: I5cd3f7b6ca069d69086d09acfea8fc4f1215c833 --- uncrypt/uncrypt.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'uncrypt') diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index 7adf88cac..96edfd781 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -130,6 +130,7 @@ // devices, on which /cache partitions always exist. static const std::string CACHE_BLOCK_MAP = "/cache/recovery/block.map"; static const std::string UNCRYPT_PATH_FILE = "/cache/recovery/uncrypt_file"; +static const std::string UNCRYPT_STATUS = "/cache/recovery/uncrypt_status"; static const std::string UNCRYPT_SOCKET = "uncrypt"; static struct fstab* fstab = nullptr; @@ -461,12 +462,32 @@ static bool uncrypt_wrapper(const char* input_path, const char* map_file, const input_path = package.c_str(); } CHECK(map_file != nullptr); + +#define UNCRYPT_TIME_HOLDER 0x7FFFFFFF + // Intialize the uncrypt time cost to a huge number so that we can tell from + // the statistics if an uncrypt fails to finish. + if (!android::base::WriteStringToFile(android::base::StringPrintf( + "uncrypt_time: %d\n", UNCRYPT_TIME_HOLDER), UNCRYPT_STATUS)) { + PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; + } + + auto start = std::chrono::system_clock::now(); int status = uncrypt(input_path, map_file, socket); if (status != 0) { write_status_to_socket(-1, socket); return false; } + + std::chrono::duration duration = std::chrono::system_clock::now() - start; + int count = static_cast(duration.count()); + // Overwrite the uncrypt_time if uncrypt finishes successfully. + if (!android::base::WriteStringToFile( + android::base::StringPrintf("uncrypt_time: %d\n", count), UNCRYPT_STATUS)) { + PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; + } + write_status_to_socket(100, socket); + return true; } -- cgit v1.2.3 From 91e3aee9bdc1a503affdd925dd4da352a198abca Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 23 Sep 2016 15:30:55 -0700 Subject: Switch to . Bug: http://b/23102347 Test: boot into recovery. Change-Id: Ib2ca560f1312961c21fbaa294bb068de19cb883e --- uncrypt/uncrypt.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'uncrypt') diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index 1fba717f3..904ddad1b 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -107,12 +107,12 @@ #include #include +#include #include #include #include #include #include -#include #include #include @@ -163,13 +163,15 @@ static struct fstab* read_fstab() { fstab = NULL; // The fstab path is always "/fstab.${ro.hardware}". - char fstab_path[PATH_MAX+1] = "/fstab."; - if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) { + std::string ro_hardware = android::base::GetProperty("ro.hardware", ""); + if (ro_hardware.empty()) { LOG(ERROR) << "failed to get ro.hardware"; return NULL; } - fstab = fs_mgr_read_fstab(fstab_path); + std::string fstab_path = "/fstab." + ro_hardware; + + fstab = fs_mgr_read_fstab(fstab_path.c_str()); if (!fstab) { LOG(ERROR) << "failed to read " << fstab_path; return NULL; @@ -194,9 +196,7 @@ static const char* find_block_device(const char* path, bool* encryptable, bool* *encryptable = false; if (fs_mgr_is_encryptable(v) || fs_mgr_is_file_encrypted(v)) { *encryptable = true; - char buffer[PROPERTY_VALUE_MAX+1]; - if (property_get("ro.crypto.state", buffer, "") && - strcmp(buffer, "encrypted") == 0) { + if (android::base::GetProperty("ro.crypto.state", "") == "encrypted") { *encrypted = true; } } -- cgit v1.2.3 From cb22040c6303144a42a90f424f29a267e43bef74 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 23 Sep 2016 15:30:55 -0700 Subject: Switch to . Bug: http://b/23102347 Test: boot into recovery. Change-Id: Ib2ca560f1312961c21fbaa294bb068de19cb883e Merged-In: Ib2ca560f1312961c21fbaa294bb068de19cb883e --- uncrypt/bootloader_message_writer.cpp | 11 +++++------ uncrypt/uncrypt.cpp | 14 +++++++------- 2 files changed, 12 insertions(+), 13 deletions(-) (limited to 'uncrypt') diff --git a/uncrypt/bootloader_message_writer.cpp b/uncrypt/bootloader_message_writer.cpp index 3bb106aa0..db52121eb 100644 --- a/uncrypt/bootloader_message_writer.cpp +++ b/uncrypt/bootloader_message_writer.cpp @@ -17,12 +17,12 @@ #include #include #include -#include #include #include #include +#include #include #include #include @@ -30,14 +30,13 @@ #include "bootloader.h" static struct fstab* read_fstab(std::string* err) { - // The fstab path is always "/fstab.${ro.hardware}". - std::string fstab_path = "/fstab."; - char value[PROP_VALUE_MAX]; - if (__system_property_get("ro.hardware", value) == 0) { + std::string ro_hardware = android::base::GetProperty("ro.hardware", ""); + if (ro_hardware.empty()) { *err = "failed to get ro.hardware"; return nullptr; } - fstab_path += value; + // The fstab path is always "/fstab.${ro.hardware}". + std::string fstab_path = "/fstab." + ro_hardware; struct fstab* fstab = fs_mgr_read_fstab(fstab_path.c_str()); if (fstab == nullptr) { *err = "failed to read " + fstab_path; diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index 96edfd781..dea8445ea 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -107,12 +107,12 @@ #include #include +#include #include #include #include #include #include -#include #include #include @@ -163,13 +163,15 @@ static struct fstab* read_fstab() { fstab = NULL; // The fstab path is always "/fstab.${ro.hardware}". - char fstab_path[PATH_MAX+1] = "/fstab."; - if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) { + std::string ro_hardware = android::base::GetProperty("ro.hardware", ""); + if (ro_hardware.empty()) { LOG(ERROR) << "failed to get ro.hardware"; return NULL; } - fstab = fs_mgr_read_fstab(fstab_path); + std::string fstab_path = "/fstab." + ro_hardware; + + fstab = fs_mgr_read_fstab(fstab_path.c_str()); if (!fstab) { LOG(ERROR) << "failed to read " << fstab_path; return NULL; @@ -194,9 +196,7 @@ static const char* find_block_device(const char* path, bool* encryptable, bool* *encryptable = false; if (fs_mgr_is_encryptable(v) || fs_mgr_is_file_encrypted(v)) { *encryptable = true; - char buffer[PROPERTY_VALUE_MAX+1]; - if (property_get("ro.crypto.state", buffer, "") && - strcmp(buffer, "encrypted") == 0) { + if (android::base::GetProperty("ro.crypto.state", "") == "encrypted") { *encrypted = true; } } -- cgit v1.2.3 From da44cf18f3ce4bbffa85ad0a50bb25e9cb54a86d Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Sat, 24 Sep 2016 15:31:34 -0700 Subject: Report uncrypt errors in details Add the error codes for uncrypt and report the failure details in uncrypt_status. Test: uncrypt_error logs correctly in last_install Bug: 31603820 Change-Id: I8e0de845ce1707b6f8f5ae84564c5e93fd5f5ef5 --- uncrypt/uncrypt.cpp | 105 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 40 deletions(-) (limited to 'uncrypt') diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index dea8445ea..c77e987b5 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -116,6 +116,8 @@ #include #include +#include "error_code.h" + #define WINDOW_SIZE 5 // uncrypt provides three services: SETUP_BCB, CLEAR_BCB and UNCRYPT. @@ -231,26 +233,26 @@ static int produce_block_map(const char* path, const char* map_file, const char* std::string err; if (!android::base::RemoveFileIfExists(map_file, &err)) { LOG(ERROR) << "failed to remove the existing map file " << map_file << ": " << err; - return -1; + return kUncryptFileRemoveError; } std::string tmp_map_file = std::string(map_file) + ".tmp"; android::base::unique_fd mapfd(open(tmp_map_file.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)); if (mapfd == -1) { PLOG(ERROR) << "failed to open " << tmp_map_file; - return -1; + return kUncryptFileOpenError; } // Make sure we can write to the socket. if (!write_status_to_socket(0, socket)) { LOG(ERROR) << "failed to write to socket " << socket; - return -1; + return kUncryptSocketWriteError; } struct stat sb; if (stat(path, &sb) != 0) { LOG(ERROR) << "failed to stat " << path; - return -1; + return kUncryptFileStatError; } LOG(INFO) << " block size: " << sb.st_blksize << " bytes"; @@ -265,7 +267,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* static_cast(sb.st_blksize)); if (!android::base::WriteStringToFd(s, mapfd)) { PLOG(ERROR) << "failed to write " << tmp_map_file; - return -1; + return kUncryptWriteError; } std::vector> buffers; @@ -278,7 +280,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* android::base::unique_fd fd(open(path, O_RDONLY)); if (fd == -1) { PLOG(ERROR) << "failed to open " << path << " for reading"; - return -1; + return kUncryptFileOpenError; } android::base::unique_fd wfd; @@ -286,7 +288,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* wfd.reset(open(blk_dev, O_WRONLY)); if (wfd == -1) { PLOG(ERROR) << "failed to open " << blk_dev << " for writing"; - return -1; + return kUncryptBlockOpenError; } } @@ -304,14 +306,14 @@ static int produce_block_map(const char* path, const char* map_file, const char* // write out head buffer int block = head_block; if (ioctl(fd, FIBMAP, &block) != 0) { - LOG(ERROR) << "failed to find block " << head_block; - return -1; + PLOG(ERROR) << "failed to find block " << head_block; + return kUncryptIoctlError; } add_block_to_ranges(ranges, block); if (encrypted) { if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd, static_cast(sb.st_blksize) * block) != 0) { - return -1; + return kUncryptWriteError; } } head = (head + 1) % WINDOW_SIZE; @@ -324,7 +326,7 @@ static int produce_block_map(const char* path, const char* map_file, const char* std::min(static_cast(sb.st_blksize), sb.st_size - pos)); if (!android::base::ReadFully(fd, buffers[tail].data(), to_read)) { PLOG(ERROR) << "failed to read " << path; - return -1; + return kUncryptReadError; } pos += to_read; } else { @@ -340,14 +342,14 @@ static int produce_block_map(const char* path, const char* map_file, const char* // write out head buffer int block = head_block; if (ioctl(fd, FIBMAP, &block) != 0) { - LOG(ERROR) << "failed to find block " << head_block; - return -1; + PLOG(ERROR) << "failed to find block " << head_block; + return kUncryptIoctlError; } add_block_to_ranges(ranges, block); if (encrypted) { if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd, static_cast(sb.st_blksize) * block) != 0) { - return -1; + return kUncryptWriteError; } } head = (head + 1) % WINDOW_SIZE; @@ -357,39 +359,39 @@ static int produce_block_map(const char* path, const char* map_file, const char* if (!android::base::WriteStringToFd( android::base::StringPrintf("%zu\n", ranges.size() / 2), mapfd)) { PLOG(ERROR) << "failed to write " << tmp_map_file; - return -1; + return kUncryptWriteError; } for (size_t i = 0; i < ranges.size(); i += 2) { if (!android::base::WriteStringToFd( android::base::StringPrintf("%d %d\n", ranges[i], ranges[i+1]), mapfd)) { PLOG(ERROR) << "failed to write " << tmp_map_file; - return -1; + return kUncryptWriteError; } } if (fsync(mapfd) == -1) { PLOG(ERROR) << "failed to fsync \"" << tmp_map_file << "\""; - return -1; + return kUncryptFileSyncError; } if (close(mapfd.release()) == -1) { PLOG(ERROR) << "failed to close " << tmp_map_file; - return -1; + return kUncryptFileCloseError; } if (encrypted) { if (fsync(wfd) == -1) { PLOG(ERROR) << "failed to fsync \"" << blk_dev << "\""; - return -1; + return kUncryptFileSyncError; } if (close(wfd.release()) == -1) { PLOG(ERROR) << "failed to close " << blk_dev; - return -1; + return kUncryptFileCloseError; } } if (rename(tmp_map_file.c_str(), map_file) == -1) { PLOG(ERROR) << "failed to rename " << tmp_map_file << " to " << map_file; - return -1; + return kUncryptFileRenameError; } // Sync dir to make rename() result written to disk. std::string file_name = map_file; @@ -397,15 +399,15 @@ static int produce_block_map(const char* path, const char* map_file, const char* android::base::unique_fd dfd(open(dir_name.c_str(), O_RDONLY | O_DIRECTORY)); if (dfd == -1) { PLOG(ERROR) << "failed to open dir " << dir_name; - return -1; + return kUncryptFileOpenError; } if (fsync(dfd) == -1) { PLOG(ERROR) << "failed to fsync " << dir_name; - return -1; + return kUncryptFileSyncError; } if (close(dfd.release()) == -1) { PLOG(ERROR) << "failed to close " << dir_name; - return -1; + return kUncryptFileCloseError; } return 0; } @@ -444,45 +446,52 @@ static int uncrypt(const char* input_path, const char* map_file, const int socke // and /sdcard we leave the file alone. if (strncmp(path, "/data/", 6) == 0) { LOG(INFO) << "writing block map " << map_file; - if (produce_block_map(path, map_file, blk_dev, encrypted, socket) != 0) { - return 1; - } + return produce_block_map(path, map_file, blk_dev, encrypted, socket); } return 0; } static bool uncrypt_wrapper(const char* input_path, const char* map_file, const int socket) { + // Initialize the uncrypt error to kUncryptErrorHolder. + if (!android::base::WriteStringToFile(android::base::StringPrintf( + "uncrypt_error: %d\n", kUncryptErrorHolder), UNCRYPT_STATUS)) { + PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; + } + std::string package; if (input_path == nullptr) { if (!find_uncrypt_package(UNCRYPT_PATH_FILE, &package)) { write_status_to_socket(-1, socket); + // Overwrite the error message. + if (!android::base::WriteStringToFile(android::base::StringPrintf( + "uncrypt_error: %d\n", kUncryptPackageMissingError), UNCRYPT_STATUS)) { + PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; + } return false; } input_path = package.c_str(); } CHECK(map_file != nullptr); -#define UNCRYPT_TIME_HOLDER 0x7FFFFFFF - // Intialize the uncrypt time cost to a huge number so that we can tell from - // the statistics if an uncrypt fails to finish. - if (!android::base::WriteStringToFile(android::base::StringPrintf( - "uncrypt_time: %d\n", UNCRYPT_TIME_HOLDER), UNCRYPT_STATUS)) { - PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; - } - auto start = std::chrono::system_clock::now(); int status = uncrypt(input_path, map_file, socket); + std::chrono::duration duration = std::chrono::system_clock::now() - start; + int count = static_cast(duration.count()); + + std::string uncrypt_message = android::base::StringPrintf("uncrypt_time: %d\n", count); if (status != 0) { + // Log the time cost and error code if uncrypt fails. + uncrypt_message += android::base::StringPrintf("uncrypt_error: %d\n", status); + if (!android::base::WriteStringToFile(uncrypt_message, UNCRYPT_STATUS)) { + PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; + } + write_status_to_socket(-1, socket); return false; } - std::chrono::duration duration = std::chrono::system_clock::now() - start; - int count = static_cast(duration.count()); - // Overwrite the uncrypt_time if uncrypt finishes successfully. - if (!android::base::WriteStringToFile( - android::base::StringPrintf("uncrypt_time: %d\n", count), UNCRYPT_STATUS)) { + if (!android::base::WriteStringToFile(uncrypt_message, UNCRYPT_STATUS)) { PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; } @@ -560,6 +569,10 @@ int main(int argc, char** argv) { } if ((fstab = read_fstab()) == nullptr) { + if (!android::base::WriteStringToFile(android::base::StringPrintf( + "uncrypt_error: %d\n", kUncryptFstabReadError), UNCRYPT_STATUS)) { + PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; + } return 1; } @@ -568,18 +581,30 @@ int main(int argc, char** argv) { android::base::unique_fd service_socket(android_get_control_socket(UNCRYPT_SOCKET.c_str())); if (service_socket == -1) { PLOG(ERROR) << "failed to open socket \"" << UNCRYPT_SOCKET << "\""; + if (!android::base::WriteStringToFile(android::base::StringPrintf( + "uncrypt_error: %d\n", kUncryptSocketOpenError), UNCRYPT_STATUS)) { + PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; + } return 1; } fcntl(service_socket, F_SETFD, FD_CLOEXEC); if (listen(service_socket, 1) == -1) { PLOG(ERROR) << "failed to listen on socket " << service_socket.get(); + if (!android::base::WriteStringToFile(android::base::StringPrintf( + "uncrypt_error: %d\n", kUncryptSocketListenError), UNCRYPT_STATUS)) { + PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; + } return 1; } android::base::unique_fd socket_fd(accept4(service_socket, nullptr, nullptr, SOCK_CLOEXEC)); if (socket_fd == -1) { PLOG(ERROR) << "failed to accept on socket " << service_socket.get(); + if (!android::base::WriteStringToFile(android::base::StringPrintf( + "uncrypt_error: %d\n", kUncryptSocketAcceptError), UNCRYPT_STATUS)) { + PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; + } return 1; } -- cgit v1.2.3 From 68fc81e860ab58a5147a48093e57daab38ee84a3 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Sat, 24 Sep 2016 15:31:34 -0700 Subject: Report uncrypt errors in details Add the error codes for uncrypt and report the failure details in uncrypt_status. Test: uncrypt_error logs correctly in last_install Bug: 31603820 Change-Id: I8e0de845ce1707b6f8f5ae84564c5e93fd5f5ef5 (cherry picked from commit 0c68675f5ae80cd669e0bf014a69689b6fe08eee) --- uncrypt/uncrypt.cpp | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) (limited to 'uncrypt') diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index a5d692bbb..f31d55aa8 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -452,22 +452,23 @@ static int uncrypt(const char* input_path, const char* map_file, const int socke return 0; } -static bool uncrypt_wrapper(const char* input_path, const char* map_file, const int socket) { - // Initialize the uncrypt error to kUncryptErrorHolder. +static void log_uncrypt_error_code(UncryptErrorCode error_code) { if (!android::base::WriteStringToFile(android::base::StringPrintf( - "uncrypt_error: %d\n", kUncryptErrorHolder), UNCRYPT_STATUS)) { + "uncrypt_error: %d\n", error_code), UNCRYPT_STATUS)) { PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; } +} + +static bool uncrypt_wrapper(const char* input_path, const char* map_file, const int socket) { + // Initialize the uncrypt error to kUncryptErrorPlaceholder. + log_uncrypt_error_code(kUncryptErrorPlaceholder); std::string package; if (input_path == nullptr) { if (!find_uncrypt_package(UNCRYPT_PATH_FILE, &package)) { write_status_to_socket(-1, socket); // Overwrite the error message. - if (!android::base::WriteStringToFile(android::base::StringPrintf( - "uncrypt_error: %d\n", kUncryptPackageMissingError), UNCRYPT_STATUS)) { - PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; - } + log_uncrypt_error_code(kUncryptPackageMissingError); return false; } input_path = package.c_str(); @@ -586,10 +587,7 @@ int main(int argc, char** argv) { } if ((fstab = read_fstab()) == nullptr) { - if (!android::base::WriteStringToFile(android::base::StringPrintf( - "uncrypt_error: %d\n", kUncryptFstabReadError), UNCRYPT_STATUS)) { - PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; - } + log_uncrypt_error_code(kUncryptFstabReadError); return 1; } @@ -598,30 +596,21 @@ int main(int argc, char** argv) { android::base::unique_fd service_socket(android_get_control_socket(UNCRYPT_SOCKET.c_str())); if (service_socket == -1) { PLOG(ERROR) << "failed to open socket \"" << UNCRYPT_SOCKET << "\""; - if (!android::base::WriteStringToFile(android::base::StringPrintf( - "uncrypt_error: %d\n", kUncryptSocketOpenError), UNCRYPT_STATUS)) { - PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; - } + log_uncrypt_error_code(kUncryptSocketOpenError); return 1; } fcntl(service_socket, F_SETFD, FD_CLOEXEC); if (listen(service_socket, 1) == -1) { PLOG(ERROR) << "failed to listen on socket " << service_socket.get(); - if (!android::base::WriteStringToFile(android::base::StringPrintf( - "uncrypt_error: %d\n", kUncryptSocketListenError), UNCRYPT_STATUS)) { - PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; - } + log_uncrypt_error_code(kUncryptSocketListenError); return 1; } android::base::unique_fd socket_fd(accept4(service_socket, nullptr, nullptr, SOCK_CLOEXEC)); if (socket_fd == -1) { PLOG(ERROR) << "failed to accept on socket " << service_socket.get(); - if (!android::base::WriteStringToFile(android::base::StringPrintf( - "uncrypt_error: %d\n", kUncryptSocketAcceptError), UNCRYPT_STATUS)) { - PLOG(WARNING) << "failed to write to " << UNCRYPT_STATUS; - } + log_uncrypt_error_code(kUncryptSocketAcceptError); return 1; } -- cgit v1.2.3 From 7aa88748f6ec4e53333d1a15747bc44826ccc410 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Wed, 28 Sep 2016 11:42:17 -0700 Subject: Turn on -Werror for recovery Also remove the 0xff comparison when validating the bootloader message fields. As the fields won't be erased to 0xff after we remove the MTD support. Bug: 28202046 Test: The recovery folder compiles for aosp_x86-eng Change-Id: Ibb30ea1b2b28676fb08c7e92a1e5f7b6ef3247ab --- uncrypt/Android.mk | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'uncrypt') diff --git a/uncrypt/Android.mk b/uncrypt/Android.mk index 09cfdfca5..361379971 100644 --- a/uncrypt/Android.mk +++ b/uncrypt/Android.mk @@ -19,23 +19,24 @@ LOCAL_CLANG := true LOCAL_SRC_FILES := bootloader_message_writer.cpp LOCAL_MODULE := libbootloader_message_writer LOCAL_STATIC_LIBRARIES := libbase libfs_mgr +LOCAL_CFLAGS := -Werror LOCAL_C_INCLUDES := $(LOCAL_PATH)/.. LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include + include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_CLANG := true - LOCAL_SRC_FILES := uncrypt.cpp - LOCAL_C_INCLUDES := $(LOCAL_PATH)/.. - LOCAL_MODULE := uncrypt - -LOCAL_STATIC_LIBRARIES := libbootloader_message_writer libbase \ - liblog libfs_mgr libcutils \ - +LOCAL_STATIC_LIBRARIES := libbootloader_message_writer \ + libbase \ + liblog \ + libfs_mgr \ + libcutils +LOCAL_CFLAGS := -Werror LOCAL_INIT_RC := uncrypt.rc include $(BUILD_EXECUTABLE) -- cgit v1.2.3 From 17e316cce0047dcb892fb6dae9924cf6e0b41516 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Wed, 28 Sep 2016 11:42:17 -0700 Subject: Turn on -Werror for recovery Also remove the 0xff comparison when validating the bootloader message fields. As the fields won't be erased to 0xff after we remove the MTD support. Bug: 28202046 Test: The recovery folder compiles for aosp_x86-eng Change-Id: Ibb30ea1b2b28676fb08c7e92a1e5f7b6ef3247ab (cherry picked from commit 7aa88748f6ec4e53333d1a15747bc44826ccc410) --- uncrypt/Android.mk | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'uncrypt') diff --git a/uncrypt/Android.mk b/uncrypt/Android.mk index bb276edd5..97fc70896 100644 --- a/uncrypt/Android.mk +++ b/uncrypt/Android.mk @@ -17,16 +17,15 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_CLANG := true - LOCAL_SRC_FILES := uncrypt.cpp - LOCAL_C_INCLUDES := $(LOCAL_PATH)/.. - LOCAL_MODULE := uncrypt - -LOCAL_STATIC_LIBRARIES := libbootloader_message libbase \ - liblog libfs_mgr libcutils \ - +LOCAL_STATIC_LIBRARIES := libbootloader_message \ + libbase \ + liblog \ + libfs_mgr \ + libcutils +LOCAL_CFLAGS := -Werror LOCAL_INIT_RC := uncrypt.rc include $(BUILD_EXECUTABLE) -- cgit v1.2.3 From 8b309f6970ab3b7c53cc529c51a2cb44e1c7a7e1 Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Fri, 24 Jun 2016 18:22:02 -0700 Subject: Create bootloader_message static library. bootloader_messages merges bootloader_message_writer and bootloader.cpp, so we can use the same library to manage bootloader_message in normal boot and recovery mode. Bug: 29582118 Change-Id: I9efdf776ef8f02b53911ff43a518e035e0c29618 (cherry picked from commit 2f272c0551f984e83bc5abaf240e0dddb38a3326) --- uncrypt/Android.mk | 22 ++----- uncrypt/bootloader_message_writer.cpp | 106 ---------------------------------- uncrypt/uncrypt.cpp | 2 +- 3 files changed, 7 insertions(+), 123 deletions(-) delete mode 100644 uncrypt/bootloader_message_writer.cpp (limited to 'uncrypt') diff --git a/uncrypt/Android.mk b/uncrypt/Android.mk index 361379971..59084b0bb 100644 --- a/uncrypt/Android.mk +++ b/uncrypt/Android.mk @@ -14,28 +14,18 @@ LOCAL_PATH := $(call my-dir) -include $(CLEAR_VARS) -LOCAL_CLANG := true -LOCAL_SRC_FILES := bootloader_message_writer.cpp -LOCAL_MODULE := libbootloader_message_writer -LOCAL_STATIC_LIBRARIES := libbase libfs_mgr -LOCAL_CFLAGS := -Werror -LOCAL_C_INCLUDES := $(LOCAL_PATH)/.. -LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include - -include $(BUILD_STATIC_LIBRARY) - include $(CLEAR_VARS) LOCAL_CLANG := true LOCAL_SRC_FILES := uncrypt.cpp LOCAL_C_INCLUDES := $(LOCAL_PATH)/.. LOCAL_MODULE := uncrypt -LOCAL_STATIC_LIBRARIES := libbootloader_message_writer \ - libbase \ - liblog \ - libfs_mgr \ - libcutils +LOCAL_STATIC_LIBRARIES := \ + libbootloader_message \ + libbase \ + liblog \ + libfs_mgr \ + libcutils LOCAL_CFLAGS := -Werror LOCAL_INIT_RC := uncrypt.rc diff --git a/uncrypt/bootloader_message_writer.cpp b/uncrypt/bootloader_message_writer.cpp deleted file mode 100644 index db52121eb..000000000 --- a/uncrypt/bootloader_message_writer.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include - -#include "bootloader.h" - -static struct fstab* read_fstab(std::string* err) { - std::string ro_hardware = android::base::GetProperty("ro.hardware", ""); - if (ro_hardware.empty()) { - *err = "failed to get ro.hardware"; - return nullptr; - } - // The fstab path is always "/fstab.${ro.hardware}". - std::string fstab_path = "/fstab." + ro_hardware; - struct fstab* fstab = fs_mgr_read_fstab(fstab_path.c_str()); - if (fstab == nullptr) { - *err = "failed to read " + fstab_path; - } - return fstab; -} - -static std::string get_misc_blk_device(std::string* err) { - struct fstab* fstab = read_fstab(err); - if (fstab == nullptr) { - return ""; - } - fstab_rec* record = fs_mgr_get_entry_for_mount_point(fstab, "/misc"); - if (record == nullptr) { - *err = "failed to find /misc partition"; - return ""; - } - return record->blk_device; -} - -static bool write_bootloader_message(const bootloader_message& boot, std::string* err) { - std::string misc_blk_device = get_misc_blk_device(err); - if (misc_blk_device.empty()) { - return false; - } - android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY | O_SYNC)); - if (fd.get() == -1) { - *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), - strerror(errno)); - return false; - } - if (!android::base::WriteFully(fd.get(), &boot, sizeof(boot))) { - *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(), - strerror(errno)); - return false; - } - // TODO: O_SYNC and fsync duplicates each other? - if (fsync(fd.get()) == -1) { - *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(), - strerror(errno)); - return false; - } - return true; -} - -bool clear_bootloader_message(std::string* err) { - bootloader_message boot = {}; - return write_bootloader_message(boot, err); -} - -bool write_bootloader_message(const std::vector& options, std::string* err) { - bootloader_message boot = {}; - strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); - strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); - for (const auto& s : options) { - strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery)); - if (s.back() != '\n') { - strlcat(boot.recovery, "\n", sizeof(boot.recovery)); - } - } - return write_bootloader_message(boot, err); -} - -extern "C" bool write_bootloader_message(const char* options) { - std::string err; - return write_bootloader_message({options}, &err); -} diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index c77e987b5..8b4d8ef8c 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -111,7 +111,7 @@ #include #include #include -#include +#include #include #include #include -- cgit v1.2.3 From fd99a318fe630b49ba35f9a19a1866e8b1a42b7e Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Thu, 9 Jun 2016 14:09:39 -0700 Subject: Verify wipe package when wiping A/B device in recovery. To increase the security of wiping A/B devices, let uncrypt write wipe package in misc partition. Then recovery verifies the wipe package before wiping the device. Based on the original cherrypick, this CL also has additional changes to address the LOG statements and libziparchive changes. Bug: 29159185 Test: Build and boot into recovery. Change-Id: I186691bab1928d3dc036bc5542abd64a81bc2168 (cherry picked from commit 6faf0265c9b58db2c15b53f6d29025629d52f882) --- uncrypt/include/bootloader_message_writer.h | 35 ----------------------------- uncrypt/uncrypt.cpp | 19 +++++++++++++++- 2 files changed, 18 insertions(+), 36 deletions(-) delete mode 100644 uncrypt/include/bootloader_message_writer.h (limited to 'uncrypt') diff --git a/uncrypt/include/bootloader_message_writer.h b/uncrypt/include/bootloader_message_writer.h deleted file mode 100644 index e0ca3f44a..000000000 --- a/uncrypt/include/bootloader_message_writer.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef BOOTLOADER_MESSAGE_WRITER_H -#define BOOTLOADER_MESSAGE_WRITER_H - -#ifdef __cplusplus -#include -#include - -bool clear_bootloader_message(std::string* err); - -bool write_bootloader_message(const std::vector& options, std::string* err); - -#else -#include - -// C Interface. -bool write_bootloader_message(const char* options); -#endif - -#endif // BOOTLOADER_MESSAGE_WRITER_H diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index 8b4d8ef8c..a5d692bbb 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -528,14 +528,31 @@ static bool setup_bcb(const int socket) { return false; } LOG(INFO) << " received command: [" << content << "] (" << content.size() << ")"; + std::vector options = android::base::Split(content, "\n"); + std::string wipe_package; + for (auto& option : options) { + if (android::base::StartsWith(option, "--wipe_package=")) { + std::string path = option.substr(strlen("--wipe_package=")); + if (!android::base::ReadFileToString(path, &wipe_package)) { + PLOG(ERROR) << "failed to read " << path; + return false; + } + option = android::base::StringPrintf("--wipe_package_size=%zu", wipe_package.size()); + } + } // c8. setup the bcb command std::string err; - if (!write_bootloader_message({content}, &err)) { + if (!write_bootloader_message(options, &err)) { LOG(ERROR) << "failed to set bootloader message: " << err; write_status_to_socket(-1, socket); return false; } + if (!wipe_package.empty() && !write_wipe_package(wipe_package, &err)) { + PLOG(ERROR) << "failed to set wipe package: " << err; + write_status_to_socket(-1, socket); + return false; + } // c10. send "100" status write_status_to_socket(100, socket); return true; -- cgit v1.2.3 From 7ceff3e0030cd635f6b67153494df653c8bff3e5 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Thu, 30 Jun 2016 15:31:51 -0700 Subject: Allow uncrypt to work without socket communication It was inconvenient to uncrypt a update package under adb shell because the uncrypt executable required a socket to start its job. Add a workaround to allow uncrypt executes without socket communication. Test: run uncrypt under adb shell, and the block map generates successfully Bug: 29906218 Change-Id: Ibc328b31636d925dc429ede8dcec7392a721dd53 (cherry picked from commit 28c1e5d3aa9610db6e141380b1435937fc7f07db) --- uncrypt/uncrypt.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'uncrypt') diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index a5d692bbb..e1b6a1c20 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -210,6 +210,11 @@ static const char* find_block_device(const char* path, bool* encryptable, bool* } static bool write_status_to_socket(int status, int socket) { + // If socket equals -1, uncrypt is in debug mode without socket communication. + // Skip writing and return success. + if (socket == -1) { + return true; + } int status_out = htonl(status); return android::base::WriteFully(socket, &status_out, sizeof(int)); } @@ -566,7 +571,7 @@ static void usage(const char* exename) { } int main(int argc, char** argv) { - enum { UNCRYPT, SETUP_BCB, CLEAR_BCB } action; + enum { UNCRYPT, SETUP_BCB, CLEAR_BCB, UNCRYPT_DEBUG } action; const char* input_path = nullptr; const char* map_file = CACHE_BLOCK_MAP.c_str(); @@ -579,7 +584,7 @@ int main(int argc, char** argv) { } else if (argc == 3) { input_path = argv[1]; map_file = argv[2]; - action = UNCRYPT; + action = UNCRYPT_DEBUG; } else { usage(argv[0]); return 2; @@ -593,6 +598,17 @@ int main(int argc, char** argv) { return 1; } + if (action == UNCRYPT_DEBUG) { + LOG(INFO) << "uncrypt called in debug mode, skip socket communication\n"; + bool success = uncrypt_wrapper(input_path, map_file, -1); + if (success) { + LOG(INFO) << "uncrypt succeeded\n"; + } else{ + LOG(INFO) << "uncrypt failed\n"; + } + return success ? 0 : 1; + } + // c3. The socket is created by init when starting the service. uncrypt // will use the socket to communicate with its caller. android::base::unique_fd service_socket(android_get_control_socket(UNCRYPT_SOCKET.c_str())); -- cgit v1.2.3 From bc42603a8daae62b158d159c97ef8acfaa39d70d Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Fri, 11 Nov 2016 13:53:25 -0800 Subject: Retry ioctl in uncrypt if it returns block# 0 In some conditions, ioctl(fd, FIBMAP, &block) returns block number 0.This is a failure to locate the actual block number of the update package and will result in an invalid block.map. This CL retries ioctl a few times if it returns block number as 0. Bug: 31632090 Test: On N9, uncrypt retries ioctl and produces the correct blockmap. Change-Id: I913f98cf5c112915c2e803d0683db273c89053b6 --- uncrypt/uncrypt.cpp | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'uncrypt') diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index e1b6a1c20..b23f6be92 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -118,7 +118,8 @@ #include "error_code.h" -#define WINDOW_SIZE 5 +static constexpr int WINDOW_SIZE = 5; +static constexpr int FIBMAP_RETRY_LIMIT = 3; // uncrypt provides three services: SETUP_BCB, CLEAR_BCB and UNCRYPT. // @@ -233,6 +234,26 @@ static bool find_uncrypt_package(const std::string& uncrypt_path_file, std::stri return true; } +static int retry_fibmap(const int fd, const char* name, int* block, const int head_block) { + CHECK(block != nullptr); + for (size_t i = 0; i < FIBMAP_RETRY_LIMIT; i++) { + if (fsync(fd) == -1) { + PLOG(ERROR) << "failed to fsync \"" << name << "\""; + return kUncryptFileSyncError; + } + if (ioctl(fd, FIBMAP, block) != 0) { + PLOG(ERROR) << "failed to find block " << head_block; + return kUncryptIoctlError; + } + if (*block != 0) { + return kUncryptNoError; + } + sleep(1); + } + LOG(ERROR) << "fibmap of " << head_block << "always returns 0"; + return kUncryptIoctlError; +} + static int produce_block_map(const char* path, const char* map_file, const char* blk_dev, bool encrypted, int socket) { std::string err; @@ -314,6 +335,15 @@ static int produce_block_map(const char* path, const char* map_file, const char* PLOG(ERROR) << "failed to find block " << head_block; return kUncryptIoctlError; } + + if (block == 0) { + LOG(ERROR) << "failed to find block " << head_block << ", retrying"; + int error = retry_fibmap(fd, path, &block, head_block); + if (error != kUncryptNoError) { + return error; + } + } + add_block_to_ranges(ranges, block); if (encrypted) { if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd, @@ -350,6 +380,15 @@ static int produce_block_map(const char* path, const char* map_file, const char* PLOG(ERROR) << "failed to find block " << head_block; return kUncryptIoctlError; } + + if (block == 0) { + LOG(ERROR) << "failed to find block " << head_block << ", retrying"; + int error = retry_fibmap(fd, path, &block, head_block); + if (error != kUncryptNoError) { + return error; + } + } + add_block_to_ranges(ranges, block); if (encrypted) { if (write_at_offset(buffers[head].data(), sb.st_blksize, wfd, -- cgit v1.2.3 From 10334088017c9bfcf1b171567e7c4794876c33c9 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Mon, 12 Dec 2016 17:10:20 -0800 Subject: Add tests for setup-bcb and clear-bcb via uncrypt. Bug: http://b/33534933 Test: recovery_component_test passes (and fails on buggy build due to the CL in [1]). [1]: commit 7e31f421a514da09b90e46dbd642a5e9b16e0003 Change-Id: I120498048ec1db8f9fcbb3cf135c05d3a48cfcdf --- uncrypt/uncrypt.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'uncrypt') diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index 38b25abc7..4ac516d21 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -530,7 +530,7 @@ static bool setup_bcb(const int socket) { std::string content; content.resize(length); if (!android::base::ReadFully(socket, &content[0], length)) { - PLOG(ERROR) << "failed to read the length"; + PLOG(ERROR) << "failed to read the message"; return false; } LOG(INFO) << " received command: [" << content << "] (" << content.size() << ")"; -- cgit v1.2.3 From d13b6cf29c71412adac3b0dca5eddcd6c75f5163 Mon Sep 17 00:00:00 2001 From: Bowgo Tsai Date: Fri, 10 Mar 2017 16:00:40 +0800 Subject: recovery: replacing fs_mgr_read_fstab() with new fs_mgr APIs The fstab settings of early-mounted partitions (e.g., /vendor) will be in kernel device tree. Switch to the new API to get the whole settings with those in device tree: fs_mgr_read_fstab_with_dt("/etc/recovery.fstab") The original default /fstab.{ro.hardware} might be moved to /vendor/etc/. or /odm/etc/. Use another new API to get the default fstab instead of using the hard-coded /fstab.{ro.hardware}. This API also includes the settings from device tree: fs_mgr_read_fstab_default() Bug: 35811655 Test: boot sailfish recovery Change-Id: Iaa56ac7f7b4c4dfc7180c65f03e9a37b94f1de09 --- uncrypt/uncrypt.cpp | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) (limited to 'uncrypt') diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index a06384dd5..07d183be2 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -163,20 +163,9 @@ static void add_block_to_ranges(std::vector& ranges, int new_block) { } static struct fstab* read_fstab() { - fstab = NULL; - - // The fstab path is always "/fstab.${ro.hardware}". - std::string ro_hardware = android::base::GetProperty("ro.hardware", ""); - if (ro_hardware.empty()) { - LOG(ERROR) << "failed to get ro.hardware"; - return NULL; - } - - std::string fstab_path = "/fstab." + ro_hardware; - - fstab = fs_mgr_read_fstab(fstab_path.c_str()); + fstab = fs_mgr_read_fstab_default(); if (!fstab) { - LOG(ERROR) << "failed to read " << fstab_path; + LOG(ERROR) << "failed to read default fstab"; return NULL; } -- cgit v1.2.3