2016-04-07 00:52:18 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-06-25 03:22:02 +02:00
|
|
|
#include <bootloader_message/bootloader_message.h>
|
|
|
|
|
2016-04-07 00:52:18 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-08-06 23:18:33 +02:00
|
|
|
#include <optional>
|
2016-04-07 00:52:18 +02:00
|
|
|
#include <string>
|
2019-05-16 23:42:42 +02:00
|
|
|
#include <string_view>
|
2016-04-07 00:52:18 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <android-base/file.h>
|
2016-09-24 00:30:55 +02:00
|
|
|
#include <android-base/properties.h>
|
2016-04-07 00:52:18 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <android-base/unique_fd.h>
|
2018-12-04 18:37:39 +01:00
|
|
|
#include <fstab/fstab.h>
|
2016-04-07 00:52:18 +02:00
|
|
|
|
2019-05-01 22:12:57 +02:00
|
|
|
#ifndef __ANDROID__
|
|
|
|
#include <cutils/memory.h> // for strlcpy
|
|
|
|
#endif
|
|
|
|
|
2019-01-31 00:59:53 +01:00
|
|
|
using android::fs_mgr::Fstab;
|
|
|
|
using android::fs_mgr::ReadDefaultFstab;
|
|
|
|
|
2019-08-06 23:18:33 +02:00
|
|
|
static std::optional<std::string> g_misc_device_for_test;
|
2019-05-16 23:42:42 +02:00
|
|
|
|
|
|
|
// Exposed for test purpose.
|
|
|
|
void SetMiscBlockDeviceForTest(std::string_view misc_device) {
|
|
|
|
g_misc_device_for_test = misc_device;
|
|
|
|
}
|
|
|
|
|
2019-11-10 07:07:20 +01:00
|
|
|
std::string get_misc_blk_device(std::string* err) {
|
2019-08-06 23:18:33 +02:00
|
|
|
if (g_misc_device_for_test.has_value() && !g_misc_device_for_test->empty()) {
|
|
|
|
return *g_misc_device_for_test;
|
2019-05-16 23:42:42 +02:00
|
|
|
}
|
2018-12-04 18:37:39 +01:00
|
|
|
Fstab fstab;
|
|
|
|
if (!ReadDefaultFstab(&fstab)) {
|
2017-03-10 09:00:40 +01:00
|
|
|
*err = "failed to read default fstab";
|
2016-04-07 00:52:18 +02:00
|
|
|
return "";
|
|
|
|
}
|
2018-12-04 18:37:39 +01:00
|
|
|
for (const auto& entry : fstab) {
|
|
|
|
if (entry.mount_point == "/misc") {
|
|
|
|
return entry.blk_device;
|
|
|
|
}
|
2016-04-07 00:52:18 +02:00
|
|
|
}
|
2018-12-04 18:37:39 +01:00
|
|
|
|
|
|
|
*err = "failed to find /misc partition";
|
|
|
|
return "";
|
2016-04-07 00:52:18 +02:00
|
|
|
}
|
|
|
|
|
2016-06-25 03:22:02 +02:00
|
|
|
// 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) {
|
|
|
|
*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 += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str());
|
|
|
|
}
|
|
|
|
return ret == 0;
|
|
|
|
}
|
|
|
|
|
2016-11-18 21:01:26 +01:00
|
|
|
static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device,
|
|
|
|
size_t offset, std::string* err) {
|
2016-06-25 03:22:02 +02:00
|
|
|
if (!wait_for_device(misc_blk_device, err)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-06 20:47:23 +02:00
|
|
|
android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
|
2016-11-18 21:01:26 +01:00
|
|
|
if (fd == -1) {
|
2016-04-07 00:52:18 +02:00
|
|
|
*err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
|
|
|
|
strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-18 21:01:26 +01:00
|
|
|
if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
|
2016-06-25 03:22:02 +02:00
|
|
|
*err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
|
|
|
|
strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-18 21:01:26 +01:00
|
|
|
if (!android::base::ReadFully(fd, p, size)) {
|
2016-06-25 03:22:02 +02:00
|
|
|
*err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(),
|
|
|
|
strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-10 07:07:20 +01:00
|
|
|
bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
|
|
|
|
size_t offset, std::string* err) {
|
2016-11-18 21:01:26 +01:00
|
|
|
android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY));
|
|
|
|
if (fd == -1) {
|
2016-06-25 03:22:02 +02:00
|
|
|
*err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
|
|
|
|
strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-18 21:01:26 +01:00
|
|
|
if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
|
2016-06-25 03:22:02 +02:00
|
|
|
*err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
|
|
|
|
strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-18 21:01:26 +01:00
|
|
|
if (!android::base::WriteFully(fd, p, size)) {
|
2016-04-07 00:52:18 +02:00
|
|
|
*err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
|
|
|
|
strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-18 21:01:26 +01:00
|
|
|
if (fsync(fd) == -1) {
|
2016-04-07 00:52:18 +02:00
|
|
|
*err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
|
|
|
|
strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-09 00:46:07 +01:00
|
|
|
std::string get_bootloader_message_blk_device(std::string* err) {
|
|
|
|
std::string misc_blk_device = get_misc_blk_device(err);
|
|
|
|
if (misc_blk_device.empty()) return "";
|
|
|
|
if (!wait_for_device(misc_blk_device, err)) return "";
|
|
|
|
return misc_blk_device;
|
|
|
|
}
|
|
|
|
|
2016-11-18 21:01:26 +01:00
|
|
|
bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
|
|
|
|
std::string* err) {
|
|
|
|
return read_misc_partition(boot, sizeof(*boot), misc_blk_device,
|
|
|
|
BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
|
|
|
|
}
|
|
|
|
|
2016-06-25 03:22:02 +02:00
|
|
|
bool read_bootloader_message(bootloader_message* boot, std::string* err) {
|
2016-11-18 21:01:26 +01:00
|
|
|
std::string misc_blk_device = get_misc_blk_device(err);
|
|
|
|
if (misc_blk_device.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return read_bootloader_message_from(boot, misc_blk_device, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device,
|
|
|
|
std::string* err) {
|
|
|
|
return write_misc_partition(&boot, sizeof(boot), misc_blk_device,
|
|
|
|
BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
|
2016-06-25 03:22:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
|
2016-11-18 21:01:26 +01:00
|
|
|
std::string misc_blk_device = get_misc_blk_device(err);
|
|
|
|
if (misc_blk_device.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return write_bootloader_message_to(boot, misc_blk_device, err);
|
2016-06-25 03:22:02 +02:00
|
|
|
}
|
|
|
|
|
2016-04-07 00:52:18 +02:00
|
|
|
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) {
|
2016-12-13 02:06:24 +01:00
|
|
|
bootloader_message boot = {};
|
2017-10-25 22:16:54 +02:00
|
|
|
update_bootloader_message_in_struct(&boot, options);
|
|
|
|
|
2016-04-07 00:52:18 +02:00
|
|
|
return write_bootloader_message(boot, err);
|
|
|
|
}
|
|
|
|
|
2019-05-01 22:13:58 +02:00
|
|
|
bool write_bootloader_message_to(const std::vector<std::string>& options,
|
|
|
|
const std::string& misc_blk_device, std::string* err) {
|
|
|
|
bootloader_message boot = {};
|
|
|
|
update_bootloader_message_in_struct(&boot, options);
|
|
|
|
|
|
|
|
return write_bootloader_message_to(boot, misc_blk_device, err);
|
|
|
|
}
|
|
|
|
|
2016-12-14 06:53:31 +01:00
|
|
|
bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) {
|
|
|
|
bootloader_message boot;
|
|
|
|
if (!read_bootloader_message(&boot, err)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-25 22:16:54 +02:00
|
|
|
update_bootloader_message_in_struct(&boot, options);
|
2016-12-14 06:53:31 +01:00
|
|
|
|
2017-10-25 22:16:54 +02:00
|
|
|
return write_bootloader_message(boot, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool update_bootloader_message_in_struct(bootloader_message* boot,
|
|
|
|
const std::vector<std::string>& options) {
|
|
|
|
if (!boot) return false;
|
|
|
|
// Replace the command & recovery fields.
|
|
|
|
memset(boot->command, 0, sizeof(boot->command));
|
|
|
|
memset(boot->recovery, 0, sizeof(boot->recovery));
|
2016-12-14 06:53:31 +01:00
|
|
|
|
2017-10-25 22:16:54 +02:00
|
|
|
strlcpy(boot->command, "boot-recovery", sizeof(boot->command));
|
2019-05-01 22:12:57 +02:00
|
|
|
|
|
|
|
std::string recovery = "recovery\n";
|
2016-12-14 06:53:31 +01:00
|
|
|
for (const auto& s : options) {
|
2019-05-01 22:12:57 +02:00
|
|
|
recovery += s;
|
2016-12-14 06:53:31 +01:00
|
|
|
if (s.back() != '\n') {
|
2019-05-01 22:12:57 +02:00
|
|
|
recovery += '\n';
|
2016-12-14 06:53:31 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-01 22:12:57 +02:00
|
|
|
strlcpy(boot->recovery, recovery.c_str(), sizeof(boot->recovery));
|
2017-10-25 22:16:54 +02:00
|
|
|
return true;
|
2016-12-14 06:53:31 +01:00
|
|
|
}
|
|
|
|
|
2016-10-29 04:44:40 +02:00
|
|
|
bool write_reboot_bootloader(std::string* err) {
|
|
|
|
bootloader_message boot;
|
|
|
|
if (!read_bootloader_message(&boot, err)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (boot.command[0] != '\0') {
|
|
|
|
*err = "Bootloader command pending.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command));
|
|
|
|
return write_bootloader_message(boot, err);
|
|
|
|
}
|
|
|
|
|
2016-06-25 03:22:02 +02:00
|
|
|
bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
|
2016-11-18 21:01:26 +01:00
|
|
|
std::string misc_blk_device = get_misc_blk_device(err);
|
|
|
|
if (misc_blk_device.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-25 03:22:02 +02:00
|
|
|
package_data->resize(size);
|
2016-11-18 21:01:26 +01:00
|
|
|
return read_misc_partition(&(*package_data)[0], size, misc_blk_device,
|
|
|
|
WIPE_PACKAGE_OFFSET_IN_MISC, err);
|
2016-06-25 03:22:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool write_wipe_package(const std::string& package_data, std::string* err) {
|
2016-11-18 21:01:26 +01:00
|
|
|
std::string misc_blk_device = get_misc_blk_device(err);
|
|
|
|
if (misc_blk_device.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-04 22:09:38 +01:00
|
|
|
static constexpr size_t kMaximumWipePackageSize =
|
|
|
|
SYSTEM_SPACE_OFFSET_IN_MISC - WIPE_PACKAGE_OFFSET_IN_MISC;
|
|
|
|
if (package_data.size() > kMaximumWipePackageSize) {
|
|
|
|
*err = "Wipe package size " + std::to_string(package_data.size()) + " exceeds " +
|
|
|
|
std::to_string(kMaximumWipePackageSize) + " bytes";
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-18 21:01:26 +01:00
|
|
|
return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device,
|
2016-06-25 03:22:02 +02:00
|
|
|
WIPE_PACKAGE_OFFSET_IN_MISC, err);
|
|
|
|
}
|
|
|
|
|
2019-11-04 23:08:11 +01:00
|
|
|
static bool ValidateSystemSpaceRegion(size_t offset, size_t size, std::string* err) {
|
|
|
|
if (size <= SYSTEM_SPACE_SIZE_IN_MISC && offset <= (SYSTEM_SPACE_SIZE_IN_MISC - size)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
*err = android::base::StringPrintf("Out of bound access (offset %zu size %zu)", offset, size);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ReadMiscPartitionSystemSpace(void* data, size_t size, size_t offset, std::string* err) {
|
|
|
|
if (!ValidateSystemSpaceRegion(offset, size, err)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto misc_blk_device = get_misc_blk_device(err);
|
|
|
|
if (misc_blk_device.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return read_misc_partition(data, size, misc_blk_device, SYSTEM_SPACE_OFFSET_IN_MISC + offset,
|
|
|
|
err);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool WriteMiscPartitionSystemSpace(const void* data, size_t size, size_t offset,
|
|
|
|
std::string* err) {
|
|
|
|
if (!ValidateSystemSpaceRegion(offset, size, err)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto misc_blk_device = get_misc_blk_device(err);
|
|
|
|
if (misc_blk_device.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return write_misc_partition(data, size, misc_blk_device, SYSTEM_SPACE_OFFSET_IN_MISC + offset,
|
|
|
|
err);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReadMiscVirtualAbMessage(misc_virtual_ab_message* message, std::string* err) {
|
|
|
|
return ReadMiscPartitionSystemSpace(message, sizeof(*message),
|
|
|
|
offsetof(misc_system_space_layout, virtual_ab_message), err);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteMiscVirtualAbMessage(const misc_virtual_ab_message& message, std::string* err) {
|
|
|
|
return WriteMiscPartitionSystemSpace(&message, sizeof(message),
|
|
|
|
offsetof(misc_system_space_layout, virtual_ab_message), err);
|
|
|
|
}
|
|
|
|
|
2021-12-09 02:49:09 +01:00
|
|
|
bool ReadMiscMemtagMessage(misc_memtag_message* message, std::string* err) {
|
|
|
|
return ReadMiscPartitionSystemSpace(message, sizeof(*message),
|
|
|
|
offsetof(misc_system_space_layout, memtag_message), err);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteMiscMemtagMessage(const misc_memtag_message& message, std::string* err) {
|
|
|
|
return WriteMiscPartitionSystemSpace(&message, sizeof(message),
|
|
|
|
offsetof(misc_system_space_layout, memtag_message), err);
|
|
|
|
}
|
|
|
|
|
2016-10-29 04:44:40 +02:00
|
|
|
extern "C" bool write_reboot_bootloader(void) {
|
|
|
|
std::string err;
|
|
|
|
return write_reboot_bootloader(&err);
|
|
|
|
}
|
|
|
|
|
2016-04-07 00:52:18 +02:00
|
|
|
extern "C" bool write_bootloader_message(const char* options) {
|
|
|
|
std::string err;
|
|
|
|
return write_bootloader_message({options}, &err);
|
|
|
|
}
|