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 
36   kUnset = -1,
37 };
38 
39 class MiscWriter {
40  public:
41   static constexpr uint32_t kThemeFlagOffsetInVendorSpace = 0;
42   static constexpr char kDarkThemeFlag[] = "theme-dark";
43   static constexpr uint32_t kSotaFlagOffsetInVendorSpace = 32;
44   static constexpr char kSotaFlag[] = "enable-sota";
45 
46   // Returns true of |size| bytes data starting from |offset| is fully inside the vendor space.
47   static bool OffsetAndSizeInVendorSpace(size_t offset, size_t size);
48   // Writes the given data to the vendor space in /misc partition, at the given offset. Note that
49   // offset is in relative to the start of the vendor space.
50   static bool WriteMiscPartitionVendorSpace(const void* data, size_t size, size_t offset,
51                                             std::string* err);
52 
MiscWriter(const MiscWriterActions & action)53   explicit MiscWriter(const MiscWriterActions& action) : action_(action) {}
54 
55   // Performs the stored MiscWriterActions. If |override_offset| is set, writes to the input offset
56   // in the vendor space of /misc instead of the default offset.
57   bool PerformAction(std::optional<size_t> override_offset = std::nullopt);
58 
59  private:
60   MiscWriterActions action_{ MiscWriterActions::kUnset };
61 };
62 
63 }  // namespace pixel
64 }  // namespace google
65 }  // namespace hardware
66 }  // namespace android
67