1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <stddef.h> 20 #include <stdint.h> 21 22 #include <optional> 23 #include <string> 24 25 namespace android { 26 namespace hardware { 27 namespace google { 28 namespace pixel { 29 30 enum class MiscWriterActions : int32_t { 31 kSetDarkThemeFlag = 0, 32 kClearDarkThemeFlag, 33 kSetSotaFlag, 34 kClearSotaFlag, 35 kSetEnablePkvmFlag, 36 kSetDisablePkvmFlag, 37 38 kUnset = -1, 39 }; 40 41 class MiscWriter { 42 public: 43 static constexpr uint32_t kThemeFlagOffsetInVendorSpace = 0; 44 static constexpr char kDarkThemeFlag[] = "theme-dark"; 45 static constexpr uint32_t kSotaFlagOffsetInVendorSpace = 32; 46 static constexpr char kSotaFlag[] = "enable-sota"; 47 static constexpr uint32_t kPkvmFlagOffsetInVendorSpace = 64; 48 static constexpr char kEnablePkvmFlag[] = "enable-pkvm"; 49 static constexpr char kDisablePkvmFlag[] = "disable-pkvm"; 50 51 // Returns true of |size| bytes data starting from |offset| is fully inside the vendor space. 52 static bool OffsetAndSizeInVendorSpace(size_t offset, size_t size); 53 // Writes the given data to the vendor space in /misc partition, at the given offset. Note that 54 // offset is in relative to the start of the vendor space. 55 static bool WriteMiscPartitionVendorSpace(const void* data, size_t size, size_t offset, 56 std::string* err); 57 MiscWriter(const MiscWriterActions & action)58 explicit MiscWriter(const MiscWriterActions& action) : action_(action) {} 59 60 // Performs the stored MiscWriterActions. If |override_offset| is set, writes to the input offset 61 // in the vendor space of /misc instead of the default offset. 62 bool PerformAction(std::optional<size_t> override_offset = std::nullopt); 63 64 private: 65 MiscWriterActions action_{ MiscWriterActions::kUnset }; 66 }; 67 68 } // namespace pixel 69 } // namespace google 70 } // namespace hardware 71 } // namespace android 72