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   kSetWristOrientationFlag,
38   kClearWristOrientationFlag,
39   kWriteTimeFormat,
40   kWriteTimeOffset,
41   kSetMaxRamSize,
42   kClearMaxRamSize,
43   kWriteTimeRtcOffset,
44   kWriteTimeMinRtc,
45   kSetSotaConfig,
46   kWriteDstTransition,
47   kWriteDstOffset,
48   kSetDisplayMode,
49   kClearDisplayMode,
50 
51   kUnset = -1,
52 };
53 
54 class MiscWriter {
55   public:
56     // sync with bootloader's abl bootloader_message.h
57     typedef struct bootloader_message_vendor {
58         char theme[32];
59         char sota[32];
60         char pkvm[32];
61         char wrist_orientation[32];
62         char timeformat[32];
63         char timeoffset[32];
64         char max_ram_size[32];
65         char sota_client_state[40];
66         char timertcoffset[32];
67         char timeminrtc[32];
68         char faceauth_eval[32];
69         char sota_schedule_shipmode[32];
70         char dsttransition[32];
71         char dstoffset[32];
72         char user_preferred_resolution[32];
73     } __attribute__((__packed__)) bootloader_message_vendor_t;
74 
75     static constexpr uint32_t kThemeFlagOffsetInVendorSpace =
76             offsetof(bootloader_message_vendor_t, theme);
77     static constexpr char kDarkThemeFlag[] = "theme-dark";
78     static constexpr uint32_t kSotaFlagOffsetInVendorSpace =
79             offsetof(bootloader_message_vendor_t, sota);
80     static constexpr char kSotaFlag[] = "enable-sota";
81     static constexpr uint32_t kPkvmFlagOffsetInVendorSpace =
82             offsetof(bootloader_message_vendor_t, pkvm);
83     static constexpr char kEnablePkvmFlag[] = "enable-pkvm";
84     static constexpr char kDisablePkvmFlag[] = "disable-pkvm";
85     static constexpr uint32_t kWristOrientationFlagOffsetInVendorSpace =
86             offsetof(bootloader_message_vendor_t, wrist_orientation);
87     static constexpr char kWristOrientationFlag[] = "wrist-orientation=";
88     static constexpr uint32_t kTimeFormatValOffsetInVendorSpace =
89             offsetof(bootloader_message_vendor_t, timeformat);
90     static constexpr char kTimeFormat[] = "timeformat=";
91     static constexpr uint32_t kTimeOffsetValOffsetInVendorSpace =
92             offsetof(bootloader_message_vendor_t, timeoffset);
93     static constexpr char kTimeOffset[] = "timeoffset=";
94     static constexpr uint32_t kMaxRamSizeOffsetInVendorSpace =
95             offsetof(bootloader_message_vendor_t, max_ram_size);
96     static constexpr char kMaxRamSize[] = "max-ram-size=";
97     static constexpr uint32_t kSotaStateOffsetInVendorSpace =
98             offsetof(bootloader_message_vendor_t, sota_client_state);
99     static constexpr uint32_t kRTimeRtcOffsetValOffsetInVendorSpace =
100             offsetof(bootloader_message_vendor_t, timertcoffset);
101     static constexpr char kTimeRtcOffset[] = "timertcoffset=";
102     static constexpr uint32_t kRTimeMinRtcValOffsetInVendorSpace =
103             offsetof(bootloader_message_vendor_t, timeminrtc);
104     static constexpr char kTimeMinRtc[] = "timeminrtc=";
105     static constexpr uint32_t kFaceauthEvalValOffsetInVendorSpace =
106             offsetof(bootloader_message_vendor_t, faceauth_eval);
107     static constexpr uint32_t kSotaScheduleShipmodeOffsetInVendorSpace =
108             offsetof(bootloader_message_vendor_t, sota_schedule_shipmode);
109     static constexpr uint32_t kDstTransitionOffsetInVendorSpace =
110             offsetof(bootloader_message_vendor_t, dsttransition);
111     static constexpr char kDstTransition[] = "dst-transition=";
112     static constexpr uint32_t kDstOffsetOffsetInVendorSpace =
113             offsetof(bootloader_message_vendor_t, dstoffset);
114     static constexpr char kDstOffset[] = "dst-offset=";
115     static constexpr uint32_t kDisplayModeOffsetInVendorSpace =
116             offsetof(bootloader_message_vendor_t, user_preferred_resolution);
117     static constexpr char kDisplayModePrefix[] = "mode=";
118 
119     // Minimum and maximum valid value for max-ram-size
120     static constexpr int32_t kRamSizeDefault = -1;
121     static constexpr uint32_t kRamSizeMin = 2048;
122     static constexpr uint32_t kRamSizeMax = 65536;
123 
124     // Minimum and maximum time zone are -12 and 14 hours from GMT
125     static constexpr int32_t kMinTimeOffset = -12 * 60 * 60 * 1000;
126     static constexpr int32_t kMaxTimeOffset = 14 * 60 * 60 * 1000;
127 
128     // Maximum display mode string length
129     static constexpr size_t kDisplayModeMaxSize = 32 - sizeof(kDisplayModePrefix);
130 
131     // Returns true of |size| bytes data starting from |offset| is fully inside the vendor space.
132     static bool OffsetAndSizeInVendorSpace(size_t offset, size_t size);
133     // Writes the given data to the vendor space in /misc partition, at the given offset. Note that
134     // offset is in relative to the start of the vendor space.
135     static bool WriteMiscPartitionVendorSpace(const void *data, size_t size, size_t offset,
136                                               std::string *err);
137 
MiscWriter(const MiscWriterActions & action)138     explicit MiscWriter(const MiscWriterActions &action) : action_(action) {}
MiscWriter(const MiscWriterActions & action,const char data)139     explicit MiscWriter(const MiscWriterActions &action, const char data)
140         : action_(action), chardata_(data) {}
MiscWriter(const MiscWriterActions & action,std::string data)141     explicit MiscWriter(const MiscWriterActions &action, std::string data)
142         : action_(action), stringdata_(data) {}
143 
144     // Performs the stored MiscWriterActions. If |override_offset| is set, writes to the input
145     // offset in the vendor space of /misc instead of the default offset.
146     bool PerformAction(std::optional<size_t> override_offset = std::nullopt);
147 
148   private:
149     MiscWriterActions action_{MiscWriterActions::kUnset};
150     char chardata_{'0'};
151     std::string stringdata_;
152 };
153 
154 }  // namespace pixel
155 }  // namespace google
156 }  // namespace hardware
157 }  // namespace android
158