Skip to content

Commit

Permalink
vendor: Move misc_writer to here
Browse files Browse the repository at this point in the history
Bug: 131775112
Test: build
Change-Id: Idd70fd8f9843c9ec0a23281b8ea46f3aeb169ae0
  • Loading branch information
Tianjie Xu authored and jhenrique09 committed Nov 23, 2020
1 parent 72cd714 commit 0760128
Show file tree
Hide file tree
Showing 5 changed files with 508 additions and 0 deletions.
110 changes: 110 additions & 0 deletions misc/misc_writer/Android.bp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// 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.
//

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

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: [
"-Wall",
"-Werror",
],

shared_libs: [
"libbase",
],

static_libs: [
"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/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
83 changes: 83 additions & 0 deletions misc/misc_writer/misc_writer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.
*/

#include "misc_writer/misc_writer.h"

#include <string.h>

#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <bootloader_message/bootloader_message.h>

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

bool MiscWriter::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 MiscWriter::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);
}

bool MiscWriter::PerformAction(std::optional<size_t> override_offset) {
size_t offset = 0;
std::string content;
switch (action_) {
case MiscWriterActions::kSetDarkThemeFlag:
case MiscWriterActions::kClearDarkThemeFlag:
offset = override_offset.value_or(kThemeFlagOffsetInVendorSpace);
content = (action_ == MiscWriterActions::kSetDarkThemeFlag)
? kDarkThemeFlag
: std::string(strlen(kDarkThemeFlag), 0);
break;
case MiscWriterActions::kSetSotaFlag:
case MiscWriterActions::kClearSotaFlag:
offset = override_offset.value_or(kSotaFlagOffsetInVendorSpace);
content = (action_ == MiscWriterActions::kSetSotaFlag) ? kSotaFlag
: std::string(strlen(kSotaFlag), 0);
break;
case MiscWriterActions::kUnset:
LOG(ERROR) << "The misc writer action must be set";
return false;
}

if (std::string err;
!WriteMiscPartitionVendorSpace(content.data(), content.size(), offset, &err)) {
LOG(ERROR) << "Failed to write " << content << " at offset " << offset << " : " << err;
return false;
}
return true;
}

} // namespace pixel
} // namespace google
} // namespace hardware
} // namespace android
109 changes: 109 additions & 0 deletions misc/misc_writer/misc_writer_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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.
*/

#include <getopt.h>
#include <stdint.h>
#include <stdlib.h>

#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <string_view>

#include <android-base/logging.h>
#include <android-base/parseint.h>

#include "misc_writer/misc_writer.h"

using namespace std::string_literals;
using android::hardware::google::pixel::MiscWriter;
using android::hardware::google::pixel::MiscWriterActions;

static int Usage(std::string_view name) {
std::cerr << name << " usage:\n";
std::cerr << name << " [--override-vendor-space-offset <offset>] --<misc_writer_action>\n";
std::cerr << "Supported misc_writer_action is one of: \n";
std::cerr << " --set-dark-theme Write the dark theme flag\n";
std::cerr << " --clear-dark-theme Clear the dark theme flag\n";
std::cerr << " --set-sota Write the silent OTA flag\n";
std::cerr << " --clear-sota Clear the silent OTA flag\n";
std::cerr << "Writes the given hex string to the specified offset in vendor space in /misc "
"partition.\nDefault offset is used for each action unless "
"--override-vendor-space-offset is specified.\n";
return EXIT_FAILURE;
}

// misc_writer is a vendor tool that writes data to the vendor space in /misc.
int main(int argc, char** argv) {
constexpr struct option OPTIONS[] = {
{ "set-dark-theme", no_argument, nullptr, 0 },
{ "clear-dark-theme", no_argument, nullptr, 0 },
{ "set-sota", no_argument, nullptr, 0 },
{ "clear-sota", no_argument, nullptr, 0 },
{ "override-vendor-space-offset", required_argument, nullptr, 0 },
{ nullptr, 0, nullptr, 0 },
};

std::map<std::string, MiscWriterActions> action_map{
{ "set-dark-theme", MiscWriterActions::kSetDarkThemeFlag },
{ "clear-dark-theme", MiscWriterActions::kClearDarkThemeFlag },
{ "set-sota", MiscWriterActions::kSetSotaFlag },
{ "clear-sota", MiscWriterActions::kClearSotaFlag },
};

std::unique_ptr<MiscWriter> misc_writer;
std::optional<size_t> override_offset;

int arg;
int option_index = 0;
while ((arg = getopt_long(argc, argv, "", OPTIONS, &option_index)) != -1) {
if (arg != 0) {
LOG(ERROR) << "Invalid command argument";
return Usage(argv[0]);
}
auto option_name = OPTIONS[option_index].name;
if (option_name == "override-vendor-space-offset"s) {
LOG(WARNING) << "Overriding the vendor space offset in misc partition to " << optarg;
size_t offset;
if (!android::base::ParseUint(optarg, &offset)) {
LOG(ERROR) << "Failed to parse the offset: " << optarg;
return Usage(argv[0]);
}
override_offset = offset;
} else if (auto iter = action_map.find(option_name); iter != action_map.end()) {
if (misc_writer) {
LOG(ERROR) << "Misc writer action has already been set";
return Usage(argv[0]);
}
misc_writer = std::make_unique<MiscWriter>(iter->second);
} else {
LOG(FATAL) << "Unreachable path, option_name: " << option_name;
}
}

if (!misc_writer) {
LOG(ERROR) << "An action must be specified for misc writer";
return Usage(argv[0]);
}

if (!misc_writer->PerformAction(override_offset)) {
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}
Loading

0 comments on commit 0760128

Please sign in to comment.