summaryrefslogtreecommitdiffstats
path: root/bootloader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'bootloader.cpp')
-rw-r--r--bootloader.cpp101
1 files changed, 59 insertions, 42 deletions
diff --git a/bootloader.cpp b/bootloader.cpp
index 783f56ea8..6b86c17a7 100644
--- a/bootloader.cpp
+++ b/bootloader.cpp
@@ -25,13 +25,15 @@
#include <fs_mgr.h>
+#include <android-base/file.h>
+#include <android-base/unique_fd.h>
+
#include "bootloader.h"
#include "common.h"
#include "roots.h"
-#include <android-base/unique_fd.h>
-static int get_bootloader_message_block(bootloader_message* out, const Volume* v);
-static int set_bootloader_message_block(const bootloader_message* in, const Volume* v);
+static bool read_misc_partition(const Volume* v, size_t offset, size_t size, std::string* out);
+static bool write_misc_partition(const Volume* v, size_t offset, const std::string& in);
int get_bootloader_message(bootloader_message* out) {
Volume* v = volume_for_path("/misc");
@@ -40,12 +42,34 @@ int get_bootloader_message(bootloader_message* out) {
return -1;
}
if (strcmp(v->fs_type, "emmc") == 0) {
- return get_bootloader_message_block(out, v);
+ std::string s;
+ if (!read_misc_partition(v, BOOTLOADER_MESSAGE_OFFSET_IN_MISC, sizeof(bootloader_message),
+ &s)) {
+ return -1;
+ }
+ memcpy(out, s.data(), s.size());
+ return 0;
}
- LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
+ LOGE("Unknown misc partition fs_type \"%s\"\n", v->fs_type);
return -1;
}
+bool read_wipe_package(size_t size, std::string* out) {
+ Volume* v = volume_for_path("/misc");
+ if (v == nullptr) {
+ LOGE("Cannot load volume /misc!\n");
+ return false;
+ }
+ if (strcmp(v->fs_type, "mtd") == 0) {
+ LOGE("Read wipe package on mtd is not supported.\n");
+ return false;
+ } else if (strcmp(v->fs_type, "emmc") == 0) {
+ return read_misc_partition(v, WIPE_PACKAGE_OFFSET_IN_MISC, size, out);
+ }
+ LOGE("Unknown misc partition fs_type \"%s\"\n", v->fs_type);
+ return false;
+}
+
int set_bootloader_message(const bootloader_message* in) {
Volume* v = volume_for_path("/misc");
if (v == nullptr) {
@@ -53,9 +77,11 @@ int set_bootloader_message(const bootloader_message* in) {
return -1;
}
if (strcmp(v->fs_type, "emmc") == 0) {
- return set_bootloader_message_block(in, v);
+ std::string s(reinterpret_cast<const char*>(in), sizeof(*in));
+ bool success = write_misc_partition(v, BOOTLOADER_MESSAGE_OFFSET_IN_MISC, s);
+ return success ? 0 : -1;
}
- LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
+ LOGE("Unknown misc partition fs_type \"%s\"\n", v->fs_type);
return -1;
}
@@ -81,53 +107,44 @@ static void wait_for_device(const char* fn) {
}
}
-static int get_bootloader_message_block(bootloader_message* out,
- const Volume* v) {
+static bool read_misc_partition(const Volume* v, size_t offset, size_t size, std::string* out) {
wait_for_device(v->blk_device);
- FILE* f = fopen(v->blk_device, "rb");
- if (f == nullptr) {
- LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
- return -1;
+ android::base::unique_fd fd(open(v->blk_device, O_RDONLY));
+ if (fd == -1) {
+ LOGE("Failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
+ return false;
}
- bootloader_message temp;
- int count = fread(&temp, sizeof(temp), 1, f);
- if (count != 1) {
- LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno));
- return -1;
+ if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
+ LOGE("Failed to lseek \"%s\": %s\n", v->blk_device, strerror(errno));
+ return false;
}
- if (fclose(f) != 0) {
- LOGE("failed to close \"%s\": %s\n", v->blk_device, strerror(errno));
- return -1;
+ out->resize(size);
+ if (!android::base::ReadFully(fd, &(*out)[0], size)) {
+ LOGE("Failed to read \"%s\": %s\n", v->blk_device, strerror(errno));
+ return false;
}
- memcpy(out, &temp, sizeof(temp));
- return 0;
+ return true;
}
-static int set_bootloader_message_block(const bootloader_message* in,
- const Volume* v) {
+static bool write_misc_partition(const Volume* v, size_t offset, const std::string& in) {
wait_for_device(v->blk_device);
android::base::unique_fd fd(open(v->blk_device, O_WRONLY | O_SYNC));
if (fd == -1) {
- LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
- return -1;
+ LOGE("Failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
+ return false;
}
-
- size_t written = 0;
- const uint8_t* start = reinterpret_cast<const uint8_t*>(in);
- size_t total = sizeof(*in);
- while (written < total) {
- ssize_t wrote = TEMP_FAILURE_RETRY(write(fd, start + written, total - written));
- if (wrote == -1) {
- LOGE("failed to write %" PRId64 " bytes: %s\n",
- static_cast<off64_t>(written), strerror(errno));
- return -1;
- }
- written += wrote;
+ if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
+ LOGE("Failed to lseek \"%s\": %s\n", v->blk_device, strerror(errno));
+ return false;
+ }
+ if (!android::base::WriteFully(fd, in.data(), in.size())) {
+ LOGE("Failed to write \"%s\": %s\n", v->blk_device, strerror(errno));
+ return false;
}
if (fsync(fd) == -1) {
- LOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno));
- return -1;
+ LOGE("Failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno));
+ return false;
}
- return 0;
+ return true;
}