summaryrefslogtreecommitdiffstats
path: root/bootloader_message
diff options
context:
space:
mode:
authorEthan Yonker <dees_troy@teamw.in>2016-11-20 05:24:59 +0100
committerEthan Yonker <dees_troy@teamw.in>2016-11-30 04:27:39 +0100
commitb523650c8ecb6751409120a38e52a66a3e48753f (patch)
tree895e254cec518caaefc251950df604c0db0e8381 /bootloader_message
parentFix libcrypto dependency in 6.0 trees (diff)
parentmerge in nyc-dr1-release history after reset to nyc-dr1-dev (diff)
downloadandroid_bootable_recovery-b523650c8ecb6751409120a38e52a66a3e48753f.tar
android_bootable_recovery-b523650c8ecb6751409120a38e52a66a3e48753f.tar.gz
android_bootable_recovery-b523650c8ecb6751409120a38e52a66a3e48753f.tar.bz2
android_bootable_recovery-b523650c8ecb6751409120a38e52a66a3e48753f.tar.lz
android_bootable_recovery-b523650c8ecb6751409120a38e52a66a3e48753f.tar.xz
android_bootable_recovery-b523650c8ecb6751409120a38e52a66a3e48753f.tar.zst
android_bootable_recovery-b523650c8ecb6751409120a38e52a66a3e48753f.zip
Diffstat (limited to 'bootloader_message')
-rw-r--r--bootloader_message/Android.mk41
-rw-r--r--bootloader_message/bootloader_message.cpp332
-rw-r--r--bootloader_message/include/bootloader_message/bootloader_message.h101
3 files changed, 474 insertions, 0 deletions
diff --git a/bootloader_message/Android.mk b/bootloader_message/Android.mk
new file mode 100644
index 000000000..8653fd525
--- /dev/null
+++ b/bootloader_message/Android.mk
@@ -0,0 +1,41 @@
+# 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.
+
+LOCAL_PATH := $(call my-dir)
+
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -ge 25; echo $$?),0)
+ include $(CLEAR_VARS)
+ LOCAL_CLANG := true
+ LOCAL_SRC_FILES := bootloader_message.cpp
+ LOCAL_MODULE := libbootloader_message
+ LOCAL_STATIC_LIBRARIES := libfs_mgr
+ LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+ LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+ include $(BUILD_STATIC_LIBRARY)
+endif
+
+include $(CLEAR_VARS)
+LOCAL_CLANG := true
+LOCAL_SRC_FILES := bootloader_message.cpp
+LOCAL_MODULE := libbootloader_message
+LOCAL_C_INCLUDES += bionic $(LOCAL_PATH)/include
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
+ LOCAL_C_INCLUDES += external/stlport/stlport
+ LOCAL_SHARED_LIBRARIES += libstlport
+else
+ LOCAL_SHARED_LIBRARIES += libc++
+endif
+LOCAL_CFLAGS := -DEXCLUDE_FS_MGR
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+include $(BUILD_SHARED_LIBRARY)
diff --git a/bootloader_message/bootloader_message.cpp b/bootloader_message/bootloader_message.cpp
new file mode 100644
index 000000000..eb99bdb42
--- /dev/null
+++ b/bootloader_message/bootloader_message.cpp
@@ -0,0 +1,332 @@
+/*
+ * 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 <bootloader_message/bootloader_message.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/system_properties.h>
+
+#include <string>
+#include <vector>
+
+/*
+#include <android-base/file.h>
+#include <android-base/stringprintf.h>
+#include <android-base/unique_fd.h>
+*/
+#ifndef EXCLUDE_FS_MGR
+#include <fs_mgr.h>
+#endif
+
+static std::string misc_blkdev;
+
+void set_misc_device(const char* type, const char* name) {
+ misc_blkdev = name;
+}
+
+#ifndef EXCLUDE_FS_MGR
+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) {
+ *err = "failed to get ro.hardware";
+ return nullptr;
+ }
+ fstab_path += value;
+ struct fstab* fstab = fs_mgr_read_fstab(fstab_path.c_str());
+ if (fstab == nullptr) {
+ *err = "failed to read " + fstab_path;
+ }
+ return fstab;
+}
+#endif
+
+static std::string get_misc_blk_device(std::string* err) {
+#ifdef EXCLUDE_FS_MGR
+ return misc_blkdev;
+#else
+ 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;
+#endif
+}
+
+
+// In recovery mode, recovery can get started and try to access the misc
+// device before the kernel has actually created it.
+static bool wait_for_device(const std::string& blk_device, std::string* err) {
+ int tries = 0;
+ int ret;
+ err->clear();
+ do {
+ ++tries;
+ struct stat buf;
+ ret = stat(blk_device.c_str(), &buf);
+ if (ret == -1) {
+ char buffer[2048];
+ sprintf(buffer, "failed to stat %s try %d: %s\n",
+ blk_device.c_str(), tries, strerror(errno));
+ *err += buffer;
+ /*
+ *err += android::base::StringPrintf("failed to stat %s try %d: %s\n",
+ blk_device.c_str(), tries, strerror(errno));
+ */
+ sleep(1);
+ }
+ } while (ret && tries < 10);
+
+ if (ret) {
+ *err += "failed to stat " + blk_device + "\n";
+ /*
+ *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str());
+ */
+ }
+ return ret == 0;
+}
+
+static bool read_misc_partition(void* p, size_t size, size_t offset, std::string* err) {
+ std::string misc_blk_device = get_misc_blk_device(err);
+ if (misc_blk_device.empty()) {
+ return false;
+ }
+ if (!wait_for_device(misc_blk_device, err)) {
+ return false;
+ }
+ int fd(open(misc_blk_device.c_str(), O_RDONLY));
+ if (fd < 0) {
+ *err = "failed to open " + misc_blk_device + ": ";
+ *err += strerror(errno);
+ /*
+ *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
+ strerror(errno));
+ */
+ return false;
+ }
+ if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
+ *err = "failed to lseek " + misc_blk_device + ": ";
+ *err += strerror(errno);
+ close(fd);
+ /*
+ *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
+ strerror(errno));
+ */
+ return false;
+ }
+ if (read(fd, p, size) != size) {
+ *err = "failed to read " + misc_blk_device + ": ";
+ *err += strerror(errno);
+ close(fd);
+ /*
+ *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(),
+ strerror(errno));
+ */
+ return false;
+ }
+ close(fd);
+ return true;
+}
+
+static bool write_misc_partition(const void* p, size_t size, size_t offset, std::string* err) {
+ std::string misc_blk_device = get_misc_blk_device(err);
+ if (misc_blk_device.empty()) {
+ *err = "no misc device set";
+ return false;
+ }
+ int fd = (open(misc_blk_device.c_str(), O_WRONLY | O_SYNC));
+ if (fd == -1) {
+ *err = "failed to open " + misc_blk_device + ": ";
+ *err += strerror(errno);
+ /*
+ *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
+ strerror(errno));
+ */
+ return false;
+ }
+ if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
+ *err = "failed to lseek " + misc_blk_device + ": ";
+ *err += strerror(errno);
+ close(fd);
+ /*
+ *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
+ strerror(errno));
+ */
+ return false;
+ }
+ if (write(fd, p, size) != size) {
+ *err = "failed to write " + misc_blk_device + ": ";
+ *err += strerror(errno);
+ close(fd);
+ /*
+ *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) == -1) {
+ *err = "failed to fsync " + misc_blk_device + ": ";
+ *err += strerror(errno);
+ close(fd);
+ /*
+ *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
+ strerror(errno));
+ */
+ return false;
+ }
+ close(fd);
+ return true;
+}
+
+bool read_bootloader_message(bootloader_message* boot, std::string* err) {
+ return read_misc_partition(boot, sizeof(*boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
+}
+
+bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
+ return write_misc_partition(&boot, sizeof(boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
+}
+
+bool clear_bootloader_message(std::string* err) {
+ bootloader_message boot = {};
+ return write_bootloader_message(boot, err);
+}
+
+bool write_bootloader_message(const std::vector<std::string>& 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.substr(s.size() - 1) != "\n") {
+ strlcat(boot.recovery, "\n", sizeof(boot.recovery));
+ }
+ }
+ return write_bootloader_message(boot, err);
+}
+
+bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
+ package_data->resize(size);
+ return read_misc_partition(&(*package_data)[0], size, WIPE_PACKAGE_OFFSET_IN_MISC, err);
+}
+
+bool write_wipe_package(const std::string& package_data, std::string* err) {
+ return write_misc_partition(package_data.data(), package_data.size(),
+ WIPE_PACKAGE_OFFSET_IN_MISC, err);
+}
+
+extern "C" bool write_bootloader_message(const char* options) {
+ std::string err;
+ bootloader_message boot = {};
+ memcpy(&boot, options, sizeof(boot));
+ return write_bootloader_message(boot, &err);
+}
+
+static const char *COMMAND_FILE = "/cache/recovery/command";
+static const int MAX_ARG_LENGTH = 4096;
+static const int MAX_ARGS = 100;
+
+// command line args come from, in decreasing precedence:
+// - the actual command line
+// - the bootloader control block (one per line, after "recovery")
+// - the contents of COMMAND_FILE (one per line)
+void
+get_args(int *argc, char ***argv) {
+ bootloader_message boot = {};
+ std::string err;
+ if (!read_bootloader_message(&boot, &err)) {
+ printf("%s\n", err.c_str());
+ // If fails, leave a zeroed bootloader_message.
+ memset(&boot, 0, sizeof(boot));
+ }
+ //stage = strndup(boot.stage, sizeof(boot.stage));
+
+ if (boot.command[0] != 0 && boot.command[0] != 255) {
+ printf("Boot command: %.*s\n", (int)sizeof(boot.command), boot.command);
+ }
+
+ if (boot.status[0] != 0 && boot.status[0] != 255) {
+ printf("Boot status: %.*s\n", (int)sizeof(boot.status), boot.status);
+ }
+
+ // --- if arguments weren't supplied, look in the bootloader control block
+ if (*argc <= 1) {
+ boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
+ const char *arg = strtok(boot.recovery, "\n");
+ if (arg != NULL && !strcmp(arg, "recovery")) {
+ *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
+ (*argv)[0] = strdup(arg);
+ for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
+ if ((arg = strtok(NULL, "\n")) == NULL) break;
+ (*argv)[*argc] = strdup(arg);
+ }
+ printf("Got arguments from boot message\n");
+ } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
+ printf("Bad boot message\n\"%.20s\"\n", boot.recovery);
+ }
+ }
+
+ // --- if that doesn't work, try the command file (if we have /cache).
+ if (*argc <= 1/* && has_cache*/) {
+ FILE *fp = fopen(COMMAND_FILE, "r");
+ if (fp != NULL) {
+ char *token;
+ char *argv0 = (*argv)[0];
+ *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
+ (*argv)[0] = argv0; // use the same program name
+
+ char buf[MAX_ARG_LENGTH];
+ for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
+ if (!fgets(buf, sizeof(buf), fp)) break;
+ token = strtok(buf, "\r\n");
+ if (token != NULL) {
+ (*argv)[*argc] = strdup(token); // Strip newline.
+ } else {
+ --*argc;
+ }
+ }
+
+ fclose(fp);
+ printf("Got arguments from %s\n", COMMAND_FILE);
+ }
+ }
+
+ // --> write the arguments we have back into the bootloader control block
+ // always boot into recovery after this (until finish_recovery() is called)
+ strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
+ strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
+ int i;
+ for (i = 1; i < *argc; ++i) {
+ strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
+ strlcat(boot.recovery, "\n", sizeof(boot.recovery));
+ }
+ if (!write_bootloader_message(boot, &err)) {
+ printf("%s\n", err.c_str());
+ }
+}
diff --git a/bootloader_message/include/bootloader_message/bootloader_message.h b/bootloader_message/include/bootloader_message/bootloader_message.h
new file mode 100644
index 000000000..b4d3604e7
--- /dev/null
+++ b/bootloader_message/include/bootloader_message/bootloader_message.h
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2008 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_H
+#define _BOOTLOADER_MESSAGE_H
+
+#include <stddef.h>
+
+// Spaces used by misc partition are as below:
+// 0 - 2K Bootloader Message
+// 2K - 16K Used by Vendor's bootloader
+// 16K - 64K Used by uncrypt and recovery to store wipe_package for A/B devices
+// Note that these offsets are admitted by bootloader,recovery and uncrypt, so they
+// are not configurable without changing all of them.
+static const size_t BOOTLOADER_MESSAGE_OFFSET_IN_MISC = 0;
+static const size_t WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024;
+
+/* Bootloader Message
+ *
+ * This structure describes the content of a block in flash
+ * that is used for recovery and the bootloader to talk to
+ * each other.
+ *
+ * The command field is updated by linux when it wants to
+ * reboot into recovery or to update radio or bootloader firmware.
+ * It is also updated by the bootloader when firmware update
+ * is complete (to boot into recovery for any final cleanup)
+ *
+ * The status field is written by the bootloader after the
+ * completion of an "update-radio" or "update-hboot" command.
+ *
+ * The recovery field is only written by linux and used
+ * for the system to send a message to recovery or the
+ * other way around.
+ *
+ * The stage field is written by packages which restart themselves
+ * multiple times, so that the UI can reflect which invocation of the
+ * package it is. If the value is of the format "#/#" (eg, "1/3"),
+ * the UI will add a simple indicator of that status.
+ *
+ * The slot_suffix field is used for A/B implementations where the
+ * bootloader does not set the androidboot.ro.boot.slot_suffix kernel
+ * commandline parameter. This is used by fs_mgr to mount /system and
+ * other partitions with the slotselect flag set in fstab. A/B
+ * implementations are free to use all 32 bytes and may store private
+ * data past the first NUL-byte in this field.
+ */
+struct bootloader_message {
+ char command[32];
+ char status[32];
+ char recovery[768];
+
+ // The 'recovery' field used to be 1024 bytes. It has only ever
+ // been used to store the recovery command line, so 768 bytes
+ // should be plenty. We carve off the last 256 bytes to store the
+ // stage string (for multistage packages) and possible future
+ // expansion.
+ char stage[32];
+ char slot_suffix[32];
+ char reserved[192];
+};
+
+#ifdef __cplusplus
+
+#include <string>
+#include <vector>
+
+bool read_bootloader_message(bootloader_message* boot, std::string* err);
+bool write_bootloader_message(const bootloader_message& boot, std::string* err);
+bool write_bootloader_message(const std::vector<std::string>& options, std::string* err);
+bool clear_bootloader_message(std::string* err);
+
+bool read_wipe_package(std::string* package_data, size_t size, std::string* err);
+bool write_wipe_package(const std::string& package_data, std::string* err);
+
+void set_misc_device(const char* type, const char* name);
+void get_args(int *argc, char ***argv);
+
+#else
+
+#include <stdbool.h>
+
+// C Interface.
+bool write_bootloader_message(const char* options);
+
+#endif // ifdef __cplusplus
+
+#endif // _BOOTLOADER_MESSAGE_H