xref: /aosp_15_r20/system/update_engine/aosp/hardware_android.cc (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
1*5a923131SAndroid Build Coastguard Worker //
2*5a923131SAndroid Build Coastguard Worker // Copyright (C) 2015 The Android Open Source Project
3*5a923131SAndroid Build Coastguard Worker //
4*5a923131SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*5a923131SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*5a923131SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*5a923131SAndroid Build Coastguard Worker //
8*5a923131SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
9*5a923131SAndroid Build Coastguard Worker //
10*5a923131SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*5a923131SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*5a923131SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5a923131SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*5a923131SAndroid Build Coastguard Worker // limitations under the License.
15*5a923131SAndroid Build Coastguard Worker //
16*5a923131SAndroid Build Coastguard Worker 
17*5a923131SAndroid Build Coastguard Worker #include "update_engine/aosp/hardware_android.h"
18*5a923131SAndroid Build Coastguard Worker 
19*5a923131SAndroid Build Coastguard Worker #include <sys/types.h>
20*5a923131SAndroid Build Coastguard Worker 
21*5a923131SAndroid Build Coastguard Worker #include <memory>
22*5a923131SAndroid Build Coastguard Worker #include <string>
23*5a923131SAndroid Build Coastguard Worker #include <string_view>
24*5a923131SAndroid Build Coastguard Worker 
25*5a923131SAndroid Build Coastguard Worker #include <android/sysprop/GkiProperties.sysprop.h>
26*5a923131SAndroid Build Coastguard Worker #include <android-base/properties.h>
27*5a923131SAndroid Build Coastguard Worker #include <base/files/file_util.h>
28*5a923131SAndroid Build Coastguard Worker #include <base/strings/string_number_conversions.h>
29*5a923131SAndroid Build Coastguard Worker #include <bootloader_message/bootloader_message.h>
30*5a923131SAndroid Build Coastguard Worker #include <fstab/fstab.h>
31*5a923131SAndroid Build Coastguard Worker #include <libavb/libavb.h>
32*5a923131SAndroid Build Coastguard Worker #include <libavb_user/avb_ops_user.h>
33*5a923131SAndroid Build Coastguard Worker 
34*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/error_code_utils.h"
35*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/hardware.h"
36*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/platform_constants.h"
37*5a923131SAndroid Build Coastguard Worker #include "update_engine/common/utils.h"
38*5a923131SAndroid Build Coastguard Worker 
39*5a923131SAndroid Build Coastguard Worker #ifndef __ANDROID_RECOVERY__
40*5a923131SAndroid Build Coastguard Worker #include <android/sysprop/OtaProperties.sysprop.h>
41*5a923131SAndroid Build Coastguard Worker #endif
42*5a923131SAndroid Build Coastguard Worker 
43*5a923131SAndroid Build Coastguard Worker using android::base::GetBoolProperty;
44*5a923131SAndroid Build Coastguard Worker using android::base::GetIntProperty;
45*5a923131SAndroid Build Coastguard Worker using android::base::GetProperty;
46*5a923131SAndroid Build Coastguard Worker using std::string;
47*5a923131SAndroid Build Coastguard Worker 
48*5a923131SAndroid Build Coastguard Worker namespace chromeos_update_engine {
49*5a923131SAndroid Build Coastguard Worker 
50*5a923131SAndroid Build Coastguard Worker namespace {
51*5a923131SAndroid Build Coastguard Worker 
52*5a923131SAndroid Build Coastguard Worker // Android properties that identify the hardware and potentially non-updatable
53*5a923131SAndroid Build Coastguard Worker // parts of the bootloader (such as the bootloader version and the baseband
54*5a923131SAndroid Build Coastguard Worker // version).
55*5a923131SAndroid Build Coastguard Worker const char kPropProductManufacturer[] = "ro.product.manufacturer";
56*5a923131SAndroid Build Coastguard Worker const char kPropBootHardwareSKU[] = "ro.boot.hardware.sku";
57*5a923131SAndroid Build Coastguard Worker const char kPropBootRevision[] = "ro.boot.revision";
58*5a923131SAndroid Build Coastguard Worker const char kPropBuildDateUTC[] = "ro.build.date.utc";
59*5a923131SAndroid Build Coastguard Worker 
GetPartitionBuildDate(const string & partition_name)60*5a923131SAndroid Build Coastguard Worker string GetPartitionBuildDate(const string& partition_name) {
61*5a923131SAndroid Build Coastguard Worker   return android::base::GetProperty("ro." + partition_name + ".build.date.utc",
62*5a923131SAndroid Build Coastguard Worker                                     "");
63*5a923131SAndroid Build Coastguard Worker }
64*5a923131SAndroid Build Coastguard Worker 
IsTimestampNewerLogged(const std::string & partition_name,const std::string & old_version,const std::string & new_version)65*5a923131SAndroid Build Coastguard Worker ErrorCode IsTimestampNewerLogged(const std::string& partition_name,
66*5a923131SAndroid Build Coastguard Worker                                  const std::string& old_version,
67*5a923131SAndroid Build Coastguard Worker                                  const std::string& new_version) {
68*5a923131SAndroid Build Coastguard Worker   auto error_code = utils::IsTimestampNewer(old_version, new_version);
69*5a923131SAndroid Build Coastguard Worker   if (error_code != ErrorCode::kSuccess) {
70*5a923131SAndroid Build Coastguard Worker     LOG(WARNING) << "Timestamp check failed with "
71*5a923131SAndroid Build Coastguard Worker                  << utils::ErrorCodeToString(error_code) << ": "
72*5a923131SAndroid Build Coastguard Worker                  << partition_name << " Partition timestamp: " << old_version
73*5a923131SAndroid Build Coastguard Worker                  << " Update timestamp: " << new_version;
74*5a923131SAndroid Build Coastguard Worker   }
75*5a923131SAndroid Build Coastguard Worker   return error_code;
76*5a923131SAndroid Build Coastguard Worker }
77*5a923131SAndroid Build Coastguard Worker 
SetVbmetaDigestProp(const std::string & value)78*5a923131SAndroid Build Coastguard Worker void SetVbmetaDigestProp(const std::string& value) {
79*5a923131SAndroid Build Coastguard Worker #ifndef __ANDROID_RECOVERY__
80*5a923131SAndroid Build Coastguard Worker   if (!android::sysprop::OtaProperties::other_vbmeta_digest(value)) {
81*5a923131SAndroid Build Coastguard Worker     LOG(WARNING) << "Failed to set other vbmeta digest to " << value;
82*5a923131SAndroid Build Coastguard Worker   }
83*5a923131SAndroid Build Coastguard Worker #endif
84*5a923131SAndroid Build Coastguard Worker }
85*5a923131SAndroid Build Coastguard Worker 
CalculateVbmetaDigestForInactiveSlot()86*5a923131SAndroid Build Coastguard Worker std::string CalculateVbmetaDigestForInactiveSlot() {
87*5a923131SAndroid Build Coastguard Worker   AvbSlotVerifyData* avb_slot_data{};
88*5a923131SAndroid Build Coastguard Worker 
89*5a923131SAndroid Build Coastguard Worker   auto suffix = fs_mgr_get_other_slot_suffix();
90*5a923131SAndroid Build Coastguard Worker   const char* requested_partitions[] = {nullptr};
91*5a923131SAndroid Build Coastguard Worker   auto avb_ops = avb_ops_user_new();
92*5a923131SAndroid Build Coastguard Worker   auto verify_result = avb_slot_verify(avb_ops,
93*5a923131SAndroid Build Coastguard Worker                                        requested_partitions,
94*5a923131SAndroid Build Coastguard Worker                                        suffix.c_str(),
95*5a923131SAndroid Build Coastguard Worker                                        AVB_SLOT_VERIFY_FLAGS_NONE,
96*5a923131SAndroid Build Coastguard Worker                                        AVB_HASHTREE_ERROR_MODE_EIO,
97*5a923131SAndroid Build Coastguard Worker                                        &avb_slot_data);
98*5a923131SAndroid Build Coastguard Worker   if (verify_result != AVB_SLOT_VERIFY_RESULT_OK) {
99*5a923131SAndroid Build Coastguard Worker     LOG(WARNING) << "Failed to verify avb slot data: " << verify_result;
100*5a923131SAndroid Build Coastguard Worker     return "";
101*5a923131SAndroid Build Coastguard Worker   }
102*5a923131SAndroid Build Coastguard Worker 
103*5a923131SAndroid Build Coastguard Worker   uint8_t vbmeta_digest[AVB_SHA256_DIGEST_SIZE];
104*5a923131SAndroid Build Coastguard Worker   avb_slot_verify_data_calculate_vbmeta_digest(
105*5a923131SAndroid Build Coastguard Worker       avb_slot_data, AVB_DIGEST_TYPE_SHA256, vbmeta_digest);
106*5a923131SAndroid Build Coastguard Worker 
107*5a923131SAndroid Build Coastguard Worker   const std::string encoded_digest =
108*5a923131SAndroid Build Coastguard Worker       base::HexEncode(vbmeta_digest, AVB_SHA256_DIGEST_SIZE);
109*5a923131SAndroid Build Coastguard Worker   LOG(INFO) << "vbmeta digest for target slot: " << encoded_digest;
110*5a923131SAndroid Build Coastguard Worker   return ToLower(encoded_digest);
111*5a923131SAndroid Build Coastguard Worker }
112*5a923131SAndroid Build Coastguard Worker 
113*5a923131SAndroid Build Coastguard Worker }  // namespace
114*5a923131SAndroid Build Coastguard Worker 
115*5a923131SAndroid Build Coastguard Worker namespace hardware {
116*5a923131SAndroid Build Coastguard Worker 
117*5a923131SAndroid Build Coastguard Worker // Factory defined in hardware.h.
CreateHardware()118*5a923131SAndroid Build Coastguard Worker std::unique_ptr<HardwareInterface> CreateHardware() {
119*5a923131SAndroid Build Coastguard Worker   return std::make_unique<HardwareAndroid>();
120*5a923131SAndroid Build Coastguard Worker }
121*5a923131SAndroid Build Coastguard Worker 
122*5a923131SAndroid Build Coastguard Worker }  // namespace hardware
123*5a923131SAndroid Build Coastguard Worker 
124*5a923131SAndroid Build Coastguard Worker // In Android there are normally three kinds of builds: eng, userdebug and user.
125*5a923131SAndroid Build Coastguard Worker // These builds target respectively a developer build, a debuggable version of
126*5a923131SAndroid Build Coastguard Worker // the final product and the pristine final product the end user will run.
127*5a923131SAndroid Build Coastguard Worker // Apart from the ro.build.type property name, they differ in the following
128*5a923131SAndroid Build Coastguard Worker // properties that characterize the builds:
129*5a923131SAndroid Build Coastguard Worker // * eng builds: ro.secure=0 and ro.debuggable=1
130*5a923131SAndroid Build Coastguard Worker // * userdebug builds: ro.secure=1 and ro.debuggable=1
131*5a923131SAndroid Build Coastguard Worker // * user builds: ro.secure=1 and ro.debuggable=0
132*5a923131SAndroid Build Coastguard Worker //
133*5a923131SAndroid Build Coastguard Worker // See IsOfficialBuild() and IsNormalMode() for the meaning of these options in
134*5a923131SAndroid Build Coastguard Worker // Android.
135*5a923131SAndroid Build Coastguard Worker 
IsOfficialBuild() const136*5a923131SAndroid Build Coastguard Worker bool HardwareAndroid::IsOfficialBuild() const {
137*5a923131SAndroid Build Coastguard Worker   // We run an official build iff ro.secure == 1, because we expect the build to
138*5a923131SAndroid Build Coastguard Worker   // behave like the end user product and check for updates. Note that while
139*5a923131SAndroid Build Coastguard Worker   // developers are able to build "official builds" by just running "make user",
140*5a923131SAndroid Build Coastguard Worker   // that will only result in a more restrictive environment. The important part
141*5a923131SAndroid Build Coastguard Worker   // is that we don't produce and push "non-official" builds to the end user.
142*5a923131SAndroid Build Coastguard Worker   //
143*5a923131SAndroid Build Coastguard Worker   // In case of a non-bool value, we take the most restrictive option and
144*5a923131SAndroid Build Coastguard Worker   // assume we are in an official-build.
145*5a923131SAndroid Build Coastguard Worker   return GetBoolProperty("ro.secure", true);
146*5a923131SAndroid Build Coastguard Worker }
147*5a923131SAndroid Build Coastguard Worker 
IsNormalBootMode() const148*5a923131SAndroid Build Coastguard Worker bool HardwareAndroid::IsNormalBootMode() const {
149*5a923131SAndroid Build Coastguard Worker   // We are running in "dev-mode" iff ro.debuggable == 1. In dev-mode the
150*5a923131SAndroid Build Coastguard Worker   // update_engine will allow extra developers options, such as providing a
151*5a923131SAndroid Build Coastguard Worker   // different update URL. In case of error, we assume the build is in
152*5a923131SAndroid Build Coastguard Worker   // normal-mode.
153*5a923131SAndroid Build Coastguard Worker   return !GetBoolProperty("ro.debuggable", false);
154*5a923131SAndroid Build Coastguard Worker }
155*5a923131SAndroid Build Coastguard Worker 
AreDevFeaturesEnabled() const156*5a923131SAndroid Build Coastguard Worker bool HardwareAndroid::AreDevFeaturesEnabled() const {
157*5a923131SAndroid Build Coastguard Worker   return !IsNormalBootMode();
158*5a923131SAndroid Build Coastguard Worker }
159*5a923131SAndroid Build Coastguard Worker 
IsOOBEEnabled() const160*5a923131SAndroid Build Coastguard Worker bool HardwareAndroid::IsOOBEEnabled() const {
161*5a923131SAndroid Build Coastguard Worker   // No OOBE flow blocking updates for Android-based boards.
162*5a923131SAndroid Build Coastguard Worker   return false;
163*5a923131SAndroid Build Coastguard Worker }
164*5a923131SAndroid Build Coastguard Worker 
IsOOBEComplete(base::Time * out_time_of_oobe) const165*5a923131SAndroid Build Coastguard Worker bool HardwareAndroid::IsOOBEComplete(base::Time* out_time_of_oobe) const {
166*5a923131SAndroid Build Coastguard Worker   LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() called.";
167*5a923131SAndroid Build Coastguard Worker   if (out_time_of_oobe)
168*5a923131SAndroid Build Coastguard Worker     *out_time_of_oobe = base::Time();
169*5a923131SAndroid Build Coastguard Worker   return true;
170*5a923131SAndroid Build Coastguard Worker }
171*5a923131SAndroid Build Coastguard Worker 
GetHardwareClass() const172*5a923131SAndroid Build Coastguard Worker string HardwareAndroid::GetHardwareClass() const {
173*5a923131SAndroid Build Coastguard Worker   auto manufacturer = GetProperty(kPropProductManufacturer, "");
174*5a923131SAndroid Build Coastguard Worker   auto sku = GetProperty(kPropBootHardwareSKU, "");
175*5a923131SAndroid Build Coastguard Worker   auto revision = GetProperty(kPropBootRevision, "");
176*5a923131SAndroid Build Coastguard Worker 
177*5a923131SAndroid Build Coastguard Worker   return manufacturer + ":" + sku + ":" + revision;
178*5a923131SAndroid Build Coastguard Worker }
179*5a923131SAndroid Build Coastguard Worker 
GetDeviceRequisition() const180*5a923131SAndroid Build Coastguard Worker string HardwareAndroid::GetDeviceRequisition() const {
181*5a923131SAndroid Build Coastguard Worker   LOG(WARNING) << "STUB: Getting requisition is not supported.";
182*5a923131SAndroid Build Coastguard Worker   return "";
183*5a923131SAndroid Build Coastguard Worker }
184*5a923131SAndroid Build Coastguard Worker 
GetMinKernelKeyVersion() const185*5a923131SAndroid Build Coastguard Worker int HardwareAndroid::GetMinKernelKeyVersion() const {
186*5a923131SAndroid Build Coastguard Worker   LOG(WARNING) << "STUB: No Kernel key version is available.";
187*5a923131SAndroid Build Coastguard Worker   return -1;
188*5a923131SAndroid Build Coastguard Worker }
189*5a923131SAndroid Build Coastguard Worker 
GetMinFirmwareKeyVersion() const190*5a923131SAndroid Build Coastguard Worker int HardwareAndroid::GetMinFirmwareKeyVersion() const {
191*5a923131SAndroid Build Coastguard Worker   LOG(WARNING) << "STUB: No Firmware key version is available.";
192*5a923131SAndroid Build Coastguard Worker   return -1;
193*5a923131SAndroid Build Coastguard Worker }
194*5a923131SAndroid Build Coastguard Worker 
GetMaxFirmwareKeyRollforward() const195*5a923131SAndroid Build Coastguard Worker int HardwareAndroid::GetMaxFirmwareKeyRollforward() const {
196*5a923131SAndroid Build Coastguard Worker   LOG(WARNING) << "STUB: Getting firmware_max_rollforward is not supported.";
197*5a923131SAndroid Build Coastguard Worker   return -1;
198*5a923131SAndroid Build Coastguard Worker }
199*5a923131SAndroid Build Coastguard Worker 
SetMaxFirmwareKeyRollforward(int firmware_max_rollforward)200*5a923131SAndroid Build Coastguard Worker bool HardwareAndroid::SetMaxFirmwareKeyRollforward(
201*5a923131SAndroid Build Coastguard Worker     int firmware_max_rollforward) {
202*5a923131SAndroid Build Coastguard Worker   LOG(WARNING) << "STUB: Setting firmware_max_rollforward is not supported.";
203*5a923131SAndroid Build Coastguard Worker   return false;
204*5a923131SAndroid Build Coastguard Worker }
205*5a923131SAndroid Build Coastguard Worker 
SetMaxKernelKeyRollforward(int kernel_max_rollforward)206*5a923131SAndroid Build Coastguard Worker bool HardwareAndroid::SetMaxKernelKeyRollforward(int kernel_max_rollforward) {
207*5a923131SAndroid Build Coastguard Worker   LOG(WARNING) << "STUB: Setting kernel_max_rollforward is not supported.";
208*5a923131SAndroid Build Coastguard Worker   return false;
209*5a923131SAndroid Build Coastguard Worker }
210*5a923131SAndroid Build Coastguard Worker 
GetPowerwashCount() const211*5a923131SAndroid Build Coastguard Worker int HardwareAndroid::GetPowerwashCount() const {
212*5a923131SAndroid Build Coastguard Worker   LOG(WARNING) << "STUB: Assuming no factory reset was performed.";
213*5a923131SAndroid Build Coastguard Worker   return 0;
214*5a923131SAndroid Build Coastguard Worker }
215*5a923131SAndroid Build Coastguard Worker 
SchedulePowerwash()216*5a923131SAndroid Build Coastguard Worker bool HardwareAndroid::SchedulePowerwash() {
217*5a923131SAndroid Build Coastguard Worker   LOG(INFO) << "Scheduling a powerwash to BCB.";
218*5a923131SAndroid Build Coastguard Worker   string err;
219*5a923131SAndroid Build Coastguard Worker   if (!update_bootloader_message({"--wipe_data", "--reason=wipe_data_from_ota"},
220*5a923131SAndroid Build Coastguard Worker                                  &err)) {
221*5a923131SAndroid Build Coastguard Worker     LOG(ERROR) << "Failed to update bootloader message: " << err;
222*5a923131SAndroid Build Coastguard Worker     return false;
223*5a923131SAndroid Build Coastguard Worker   }
224*5a923131SAndroid Build Coastguard Worker   return true;
225*5a923131SAndroid Build Coastguard Worker }
226*5a923131SAndroid Build Coastguard Worker 
CancelPowerwash()227*5a923131SAndroid Build Coastguard Worker bool HardwareAndroid::CancelPowerwash() {
228*5a923131SAndroid Build Coastguard Worker   string err;
229*5a923131SAndroid Build Coastguard Worker   if (!clear_bootloader_message(&err)) {
230*5a923131SAndroid Build Coastguard Worker     LOG(ERROR) << "Failed to clear bootloader message: " << err;
231*5a923131SAndroid Build Coastguard Worker     return false;
232*5a923131SAndroid Build Coastguard Worker   }
233*5a923131SAndroid Build Coastguard Worker   return true;
234*5a923131SAndroid Build Coastguard Worker }
235*5a923131SAndroid Build Coastguard Worker 
GetNonVolatileDirectory(base::FilePath * path) const236*5a923131SAndroid Build Coastguard Worker bool HardwareAndroid::GetNonVolatileDirectory(base::FilePath* path) const {
237*5a923131SAndroid Build Coastguard Worker   base::FilePath local_path(constants::kNonVolatileDirectory);
238*5a923131SAndroid Build Coastguard Worker   if (!base::DirectoryExists(local_path)) {
239*5a923131SAndroid Build Coastguard Worker     LOG(ERROR) << "Non-volatile directory not found: " << local_path.value();
240*5a923131SAndroid Build Coastguard Worker     return false;
241*5a923131SAndroid Build Coastguard Worker   }
242*5a923131SAndroid Build Coastguard Worker   *path = local_path;
243*5a923131SAndroid Build Coastguard Worker   return true;
244*5a923131SAndroid Build Coastguard Worker }
245*5a923131SAndroid Build Coastguard Worker 
GetPowerwashSafeDirectory(base::FilePath * path) const246*5a923131SAndroid Build Coastguard Worker bool HardwareAndroid::GetPowerwashSafeDirectory(base::FilePath* path) const {
247*5a923131SAndroid Build Coastguard Worker   // On Android, we don't have a directory persisted across powerwash.
248*5a923131SAndroid Build Coastguard Worker   return false;
249*5a923131SAndroid Build Coastguard Worker }
250*5a923131SAndroid Build Coastguard Worker 
GetBuildTimestamp() const251*5a923131SAndroid Build Coastguard Worker int64_t HardwareAndroid::GetBuildTimestamp() const {
252*5a923131SAndroid Build Coastguard Worker   return GetIntProperty<int64_t>(kPropBuildDateUTC, 0);
253*5a923131SAndroid Build Coastguard Worker }
254*5a923131SAndroid Build Coastguard Worker 
255*5a923131SAndroid Build Coastguard Worker // Returns true if the device runs an userdebug build, and explicitly allows OTA
256*5a923131SAndroid Build Coastguard Worker // downgrade.
AllowDowngrade() const257*5a923131SAndroid Build Coastguard Worker bool HardwareAndroid::AllowDowngrade() const {
258*5a923131SAndroid Build Coastguard Worker   return GetBoolProperty("ro.ota.allow_downgrade", false) &&
259*5a923131SAndroid Build Coastguard Worker          GetBoolProperty("ro.debuggable", false);
260*5a923131SAndroid Build Coastguard Worker }
261*5a923131SAndroid Build Coastguard Worker 
GetFirstActiveOmahaPingSent() const262*5a923131SAndroid Build Coastguard Worker bool HardwareAndroid::GetFirstActiveOmahaPingSent() const {
263*5a923131SAndroid Build Coastguard Worker   LOG(WARNING) << "STUB: Assuming first active omaha was never set.";
264*5a923131SAndroid Build Coastguard Worker   return false;
265*5a923131SAndroid Build Coastguard Worker }
266*5a923131SAndroid Build Coastguard Worker 
SetFirstActiveOmahaPingSent()267*5a923131SAndroid Build Coastguard Worker bool HardwareAndroid::SetFirstActiveOmahaPingSent() {
268*5a923131SAndroid Build Coastguard Worker   LOG(WARNING) << "STUB: Assuming first active omaha is set.";
269*5a923131SAndroid Build Coastguard Worker   // We will set it true, so its failure doesn't cause escalation.
270*5a923131SAndroid Build Coastguard Worker   return true;
271*5a923131SAndroid Build Coastguard Worker }
272*5a923131SAndroid Build Coastguard Worker 
SetWarmReset(bool warm_reset)273*5a923131SAndroid Build Coastguard Worker void HardwareAndroid::SetWarmReset(bool warm_reset) {
274*5a923131SAndroid Build Coastguard Worker   if constexpr (!constants::kIsRecovery) {
275*5a923131SAndroid Build Coastguard Worker     constexpr char warm_reset_prop[] = "ota.warm_reset";
276*5a923131SAndroid Build Coastguard Worker     if (!android::base::SetProperty(warm_reset_prop, warm_reset ? "1" : "0")) {
277*5a923131SAndroid Build Coastguard Worker       LOG(WARNING) << "Failed to set prop " << warm_reset_prop;
278*5a923131SAndroid Build Coastguard Worker     }
279*5a923131SAndroid Build Coastguard Worker   }
280*5a923131SAndroid Build Coastguard Worker }
281*5a923131SAndroid Build Coastguard Worker 
SetVbmetaDigestForInactiveSlot(bool reset)282*5a923131SAndroid Build Coastguard Worker void HardwareAndroid::SetVbmetaDigestForInactiveSlot(bool reset) {
283*5a923131SAndroid Build Coastguard Worker   if constexpr (constants::kIsRecovery) {
284*5a923131SAndroid Build Coastguard Worker     return;
285*5a923131SAndroid Build Coastguard Worker   }
286*5a923131SAndroid Build Coastguard Worker 
287*5a923131SAndroid Build Coastguard Worker   if (android::base::GetProperty("ro.boot.avb_version", "").empty() &&
288*5a923131SAndroid Build Coastguard Worker       android::base::GetProperty("ro.boot.vbmeta.avb_version", "").empty()) {
289*5a923131SAndroid Build Coastguard Worker     LOG(INFO) << "Device doesn't use avb, skipping setting vbmeta digest";
290*5a923131SAndroid Build Coastguard Worker     return;
291*5a923131SAndroid Build Coastguard Worker   }
292*5a923131SAndroid Build Coastguard Worker 
293*5a923131SAndroid Build Coastguard Worker   if (reset) {
294*5a923131SAndroid Build Coastguard Worker     SetVbmetaDigestProp("");
295*5a923131SAndroid Build Coastguard Worker     return;
296*5a923131SAndroid Build Coastguard Worker   }
297*5a923131SAndroid Build Coastguard Worker 
298*5a923131SAndroid Build Coastguard Worker   std::string digest = CalculateVbmetaDigestForInactiveSlot();
299*5a923131SAndroid Build Coastguard Worker   if (digest.empty()) {
300*5a923131SAndroid Build Coastguard Worker     LOG(WARNING) << "Failed to calculate the vbmeta digest for the other slot";
301*5a923131SAndroid Build Coastguard Worker     return;
302*5a923131SAndroid Build Coastguard Worker   }
303*5a923131SAndroid Build Coastguard Worker   SetVbmetaDigestProp(digest);
304*5a923131SAndroid Build Coastguard Worker }
305*5a923131SAndroid Build Coastguard Worker 
GetVersionForLogging(const string & partition_name) const306*5a923131SAndroid Build Coastguard Worker string HardwareAndroid::GetVersionForLogging(
307*5a923131SAndroid Build Coastguard Worker     const string& partition_name) const {
308*5a923131SAndroid Build Coastguard Worker   if (partition_name == "boot") {
309*5a923131SAndroid Build Coastguard Worker     // ro.bootimage.build.date.utc
310*5a923131SAndroid Build Coastguard Worker     return GetPartitionBuildDate("bootimage");
311*5a923131SAndroid Build Coastguard Worker   }
312*5a923131SAndroid Build Coastguard Worker   return GetPartitionBuildDate(partition_name);
313*5a923131SAndroid Build Coastguard Worker }
314*5a923131SAndroid Build Coastguard Worker 
IsPartitionUpdateValid(const string & partition_name,const string & new_version) const315*5a923131SAndroid Build Coastguard Worker ErrorCode HardwareAndroid::IsPartitionUpdateValid(
316*5a923131SAndroid Build Coastguard Worker     const string& partition_name, const string& new_version) const {
317*5a923131SAndroid Build Coastguard Worker   if (partition_name == "boot") {
318*5a923131SAndroid Build Coastguard Worker     const auto old_version = GetPartitionBuildDate("bootimage");
319*5a923131SAndroid Build Coastguard Worker     auto error_code =
320*5a923131SAndroid Build Coastguard Worker         IsTimestampNewerLogged(partition_name, old_version, new_version);
321*5a923131SAndroid Build Coastguard Worker     if (error_code == ErrorCode::kPayloadTimestampError) {
322*5a923131SAndroid Build Coastguard Worker       bool prevent_downgrade =
323*5a923131SAndroid Build Coastguard Worker           android::sysprop::GkiProperties::prevent_downgrade_version().value_or(
324*5a923131SAndroid Build Coastguard Worker               false);
325*5a923131SAndroid Build Coastguard Worker       if (!prevent_downgrade) {
326*5a923131SAndroid Build Coastguard Worker         LOG(WARNING) << "Downgrade of boot image is detected, but permitting "
327*5a923131SAndroid Build Coastguard Worker                         "update because device does not prevent boot image "
328*5a923131SAndroid Build Coastguard Worker                         "downgrade";
329*5a923131SAndroid Build Coastguard Worker         // If prevent_downgrade_version sysprop is not explicitly set, permit
330*5a923131SAndroid Build Coastguard Worker         // downgrade in boot image version.
331*5a923131SAndroid Build Coastguard Worker         // Even though error_code is overridden here, always call
332*5a923131SAndroid Build Coastguard Worker         // IsTimestampNewerLogged to produce log messages.
333*5a923131SAndroid Build Coastguard Worker         error_code = ErrorCode::kSuccess;
334*5a923131SAndroid Build Coastguard Worker       }
335*5a923131SAndroid Build Coastguard Worker     }
336*5a923131SAndroid Build Coastguard Worker     return error_code;
337*5a923131SAndroid Build Coastguard Worker   }
338*5a923131SAndroid Build Coastguard Worker 
339*5a923131SAndroid Build Coastguard Worker   const auto old_version = GetPartitionBuildDate(partition_name);
340*5a923131SAndroid Build Coastguard Worker   // TODO(zhangkelvin)  for some partitions, missing a current timestamp should
341*5a923131SAndroid Build Coastguard Worker   // be an error, e.g. system, vendor, product etc.
342*5a923131SAndroid Build Coastguard Worker   auto error_code =
343*5a923131SAndroid Build Coastguard Worker       IsTimestampNewerLogged(partition_name, old_version, new_version);
344*5a923131SAndroid Build Coastguard Worker   return error_code;
345*5a923131SAndroid Build Coastguard Worker }
346*5a923131SAndroid Build Coastguard Worker 
347*5a923131SAndroid Build Coastguard Worker // Mount options for non-system partitions. This option causes selinux treat
348*5a923131SAndroid Build Coastguard Worker // every file in the mounted filesystem as having the 'postinstall_file'
349*5a923131SAndroid Build Coastguard Worker // context, regardless of what the filesystem itself records. See "SELinux
350*5a923131SAndroid Build Coastguard Worker // User's and Administrator's Guide" for more information on this option.
351*5a923131SAndroid Build Coastguard Worker constexpr const char* kDefaultPostinstallMountOptions =
352*5a923131SAndroid Build Coastguard Worker     "context=u:object_r:postinstall_file:s0";
353*5a923131SAndroid Build Coastguard Worker 
354*5a923131SAndroid Build Coastguard Worker // Mount options for system partitions. This option causes selinux to use the
355*5a923131SAndroid Build Coastguard Worker // 'postinstall_file' context as a fallback if there are no other selinux
356*5a923131SAndroid Build Coastguard Worker // contexts associated with the file in the mounted partition. See "SELinux
357*5a923131SAndroid Build Coastguard Worker // User's and Administrator's Guide" for more information on this option.
358*5a923131SAndroid Build Coastguard Worker constexpr const char* kSystemPostinstallMountOptions =
359*5a923131SAndroid Build Coastguard Worker     "defcontext=u:object_r:postinstall_file:s0";
360*5a923131SAndroid Build Coastguard Worker 
361*5a923131SAndroid Build Coastguard Worker // Name of the system-partition
362*5a923131SAndroid Build Coastguard Worker constexpr std::string_view kSystemPartitionName = "system";
363*5a923131SAndroid Build Coastguard Worker 
GetPartitionMountOptions(const std::string & partition_name) const364*5a923131SAndroid Build Coastguard Worker const char* HardwareAndroid::GetPartitionMountOptions(
365*5a923131SAndroid Build Coastguard Worker     const std::string& partition_name) const {
366*5a923131SAndroid Build Coastguard Worker   if (partition_name == kSystemPartitionName) {
367*5a923131SAndroid Build Coastguard Worker     return kSystemPostinstallMountOptions;
368*5a923131SAndroid Build Coastguard Worker   } else {
369*5a923131SAndroid Build Coastguard Worker     return kDefaultPostinstallMountOptions;
370*5a923131SAndroid Build Coastguard Worker   }
371*5a923131SAndroid Build Coastguard Worker }
372*5a923131SAndroid Build Coastguard Worker 
373*5a923131SAndroid Build Coastguard Worker }  // namespace chromeos_update_engine
374