Skip to content

Commit

Permalink
Merge "Consolidate the vendor space misc usage for Pixels"
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianjie Xu authored and Gerrit Code Review committed Nov 13, 2019
2 parents 8834b4e + 3d57c84 commit 423f0d1
Show file tree
Hide file tree
Showing 8 changed files with 452 additions and 156 deletions.
37 changes: 3 additions & 34 deletions bootloader_message/bootloader_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void SetMiscBlockDeviceForTest(std::string_view misc_device) {
g_misc_device_for_test = misc_device;
}

static std::string get_misc_blk_device(std::string* err) {
std::string get_misc_blk_device(std::string* err) {
if (g_misc_device_for_test.has_value() && !g_misc_device_for_test->empty()) {
return *g_misc_device_for_test;
}
Expand Down Expand Up @@ -111,8 +111,8 @@ static bool read_misc_partition(void* p, size_t size, const std::string& misc_bl
return true;
}

static bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
size_t offset, std::string* err) {
bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
size_t offset, std::string* err) {
android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY));
if (fd == -1) {
*err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
Expand Down Expand Up @@ -261,37 +261,6 @@ bool write_wipe_package(const std::string& package_data, std::string* err) {
WIPE_PACKAGE_OFFSET_IN_MISC, err);
}

static bool OffsetAndSizeInVendorSpace(size_t offset, size_t size) {
auto total_size = WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC;
return size <= total_size && offset <= total_size - size;
}

bool ReadMiscPartitionVendorSpace(void* data, size_t size, size_t offset, std::string* err) {
if (!OffsetAndSizeInVendorSpace(offset, size)) {
*err = android::base::StringPrintf("Out of bound read (offset %zu size %zu)", offset, size);
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, VENDOR_SPACE_OFFSET_IN_MISC + offset,
err);
}

bool WriteMiscPartitionVendorSpace(const void* data, size_t size, size_t offset, std::string* err) {
if (!OffsetAndSizeInVendorSpace(offset, size)) {
*err = android::base::StringPrintf("Out of bound write (offset %zu size %zu)", offset, size);
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, VENDOR_SPACE_OFFSET_IN_MISC + offset,
err);
}

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,18 @@ struct misc_system_space_layout {
#include <string>
#include <vector>

// Gets the block device name of /misc partition.
std::string get_misc_blk_device(std::string* err);
// Return the block device name for the bootloader message partition and waits
// for the device for up to 10 seconds. In case of error returns the empty
// string.
std::string get_bootloader_message_blk_device(std::string* err);

// Writes |size| bytes of data from buffer |p| to |misc_blk_device| at |offset|. If the write fails,
// sets the error message in |err|.
bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
size_t offset, std::string* err);

// Read bootloader message into boot. Error message will be set in err.
bool read_bootloader_message(bootloader_message* boot, std::string* err);

Expand Down Expand Up @@ -261,14 +268,6 @@ bool read_wipe_package(std::string* package_data, size_t size, std::string* err)
// Write the wipe package into BCB (to offset WIPE_PACKAGE_OFFSET_IN_MISC).
bool write_wipe_package(const std::string& package_data, std::string* err);

// Reads data from the vendor space in /misc partition, with the given offset and size. Note that
// offset is in relative to the start of vendor space.
bool ReadMiscPartitionVendorSpace(void* data, size_t size, size_t offset, std::string* err);

// Writes the given data to the vendor space in /misc partition, at the given offset. Note that
// offset is in relative to the start of the vendor space.
bool WriteMiscPartitionVendorSpace(const void* data, size_t size, size_t offset, std::string* err);

// Read or write the Virtual A/B message from system space in /misc.
bool ReadMiscVirtualAbMessage(misc_virtual_ab_message* message, std::string* err);
bool WriteMiscVirtualAbMessage(const misc_virtual_ab_message& message, std::string* err);
Expand Down
80 changes: 75 additions & 5 deletions misc_writer/Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,30 @@
// limitations under the License.
//

cc_binary {
name: "misc_writer",
cc_defaults {
name: "misc_writer_defaults",
vendor: true,
cpp_std: "experimental",

srcs: [
"misc_writer.cpp",
cflags: [
"-Wall",
"-Werror",
],

shared_libs: [
"libbase",
],

static_libs: [
"libbootloader_message_vendor",
"libfstab",
],
}

// TODO(xunchang) Remove duplicates after we convert the device specific librecovery_ui to recovery
// module. Then libmisc_writer can build as a vendor module available in recovery.
cc_library_static {
name: "libmisc_writer",
cpp_std: "experimental",

cflags: [
Expand All @@ -34,7 +50,61 @@ cc_binary {
],

static_libs: [
"libbootloader_message_vendor",
"libbootloader_message",
"libfstab",
],

srcs: [
"misc_writer.cpp",
],

export_include_dirs: [
"include",
],
}

cc_library_static {
name: "libmisc_writer_vendor",
defaults: [
"misc_writer_defaults",
],

srcs: [
"misc_writer.cpp",
],

export_include_dirs: [
"include",
],
}

cc_binary {
name: "misc_writer",
defaults: [
"misc_writer_defaults",
],

srcs: [
"misc_writer_main.cpp",
],

static_libs: [
"libmisc_writer_vendor",
]
}

cc_test {
name: "misc_writer_test",
defaults: [
"misc_writer_defaults",
],

srcs: [
"misc_writer_test.cpp",
],
test_suites: ["device-tests"],

static_libs: [
"libmisc_writer_vendor",
]
}
66 changes: 66 additions & 0 deletions misc_writer/include/misc_writer/misc_writer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2019 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.
*/

#pragma once

#include <stddef.h>
#include <stdint.h>

#include <optional>
#include <string>

namespace android {
namespace hardware {
namespace google {
namespace pixel {

enum class MiscWriterActions : int32_t {
kSetDarkThemeFlag = 0,
kClearDarkThemeFlag,
kSetSotaFlag,
kClearSotaFlag,

kUnset = -1,
};

class MiscWriter {
public:
static constexpr uint32_t kThemeFlagOffsetInVendorSpace = 0;
static constexpr char kDarkThemeFlag[] = "theme-dark";
static constexpr uint32_t kSotaFlagOffsetInVendorSpace = 32;
static constexpr char kSotaFlag[] = "enable-sota";

// Returns true of |size| bytes data starting from |offset| is fully inside the vendor space.
static bool OffsetAndSizeInVendorSpace(size_t offset, size_t size);
// Writes the given data to the vendor space in /misc partition, at the given offset. Note that
// offset is in relative to the start of the vendor space.
static bool WriteMiscPartitionVendorSpace(const void* data, size_t size, size_t offset,
std::string* err);

explicit MiscWriter(const MiscWriterActions& action) : action_(action) {}

// Performs the stored MiscWriterActions. If |override_offset| is set, writes to the input offset
// in the vendor space of /misc instead of the default offset.
bool PerformAction(std::optional<size_t> override_offset = std::nullopt);

private:
MiscWriterActions action_{ MiscWriterActions::kUnset };
};

} // namespace pixel
} // namespace google
} // namespace hardware
} // namespace android
Loading

0 comments on commit 423f0d1

Please sign in to comment.