Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2015 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 | |
Alex Deymo | 1b03f9f | 2015-12-09 00:38:36 -0800 | [diff] [blame] | 17 | #include "update_engine/hardware_android.h" |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 18 | |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <algorithm> |
| 24 | |
| 25 | #include <bootloader.h> |
| 26 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 27 | #include <base/files/file_util.h> |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame^] | 28 | #include <base/strings/stringprintf.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 29 | #include <brillo/make_unique_ptr.h> |
Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 30 | #include <cutils/properties.h> |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 31 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 32 | #include "update_engine/common/hardware.h" |
Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 33 | #include "update_engine/common/platform_constants.h" |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 34 | #include "update_engine/common/utils.h" |
| 35 | #include "update_engine/utils_android.h" |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 36 | |
| 37 | using std::string; |
| 38 | |
| 39 | namespace chromeos_update_engine { |
| 40 | |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 41 | namespace { |
| 42 | |
| 43 | // The powerwash arguments passed to recovery. Arguments are separated by \n. |
| 44 | const char kAndroidRecoveryPowerwashCommand[] = |
| 45 | "recovery\n" |
| 46 | "--wipe_data\n" |
| 47 | "--reason=wipe_data_from_ota\n"; |
| 48 | |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame^] | 49 | // Android properties that identify the hardware and potentially non-updatable |
| 50 | // parts of the bootloader (such as the bootloader version and the baseband |
| 51 | // version). |
| 52 | const char kPropBootBootloader[] = "ro.boot.bootloader"; |
| 53 | const char kPropBootBaseband[] = "ro.boot.baseband"; |
| 54 | const char kPropProductManufacturer[] = "ro.product.manufacturer"; |
| 55 | const char kPropBootHardwareSKU[] = "ro.boot.hardware.sku"; |
| 56 | const char kPropBootRevision[] = "ro.boot.revision"; |
| 57 | |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 58 | // Write a recovery command line |message| to the BCB. The arguments to recovery |
| 59 | // must be separated by '\n'. An empty string will erase the BCB. |
| 60 | bool WriteBootloaderRecoveryMessage(const string& message) { |
| 61 | base::FilePath misc_device; |
| 62 | if (!utils::DeviceForMountPoint("/misc", &misc_device)) |
| 63 | return false; |
| 64 | |
| 65 | // Setup a bootloader_message with just the command and recovery fields set. |
| 66 | bootloader_message boot = {}; |
| 67 | if (!message.empty()) { |
| 68 | strncpy(boot.command, "boot-recovery", sizeof(boot.command) - 1); |
| 69 | memcpy(boot.recovery, |
| 70 | message.data(), |
| 71 | std::min(message.size(), sizeof(boot.recovery) - 1)); |
| 72 | } |
| 73 | |
| 74 | int fd = |
| 75 | HANDLE_EINTR(open(misc_device.value().c_str(), O_WRONLY | O_SYNC, 0600)); |
| 76 | if (fd < 0) { |
| 77 | PLOG(ERROR) << "Opening misc"; |
| 78 | return false; |
| 79 | } |
| 80 | ScopedFdCloser fd_closer(&fd); |
| 81 | // We only re-write the first part of the bootloader_message, up to and |
| 82 | // including the recovery message. |
| 83 | size_t boot_size = |
| 84 | offsetof(bootloader_message, recovery) + sizeof(boot.recovery); |
| 85 | if (!utils::WriteAll(fd, &boot, boot_size)) { |
| 86 | PLOG(ERROR) << "Writing recovery command to misc"; |
| 87 | return false; |
| 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | } // namespace |
| 93 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 94 | namespace hardware { |
| 95 | |
| 96 | // Factory defined in hardware.h. |
| 97 | std::unique_ptr<HardwareInterface> CreateHardware() { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 98 | return brillo::make_unique_ptr(new HardwareAndroid()); |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | } // namespace hardware |
| 102 | |
Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 103 | // In Android there are normally three kinds of builds: eng, userdebug and user. |
| 104 | // These builds target respectively a developer build, a debuggable version of |
| 105 | // the final product and the pristine final product the end user will run. |
| 106 | // Apart from the ro.build.type property name, they differ in the following |
| 107 | // properties that characterize the builds: |
| 108 | // * eng builds: ro.secure=0 and ro.debuggable=1 |
| 109 | // * userdebug builds: ro.secure=1 and ro.debuggable=1 |
| 110 | // * user builds: ro.secure=1 and ro.debuggable=0 |
| 111 | // |
| 112 | // See IsOfficialBuild() and IsNormalMode() for the meaning of these options in |
| 113 | // Android. |
| 114 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 115 | bool HardwareAndroid::IsOfficialBuild() const { |
Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 116 | // We run an official build iff ro.secure == 1, because we expect the build to |
| 117 | // behave like the end user product and check for updates. Note that while |
| 118 | // developers are able to build "official builds" by just running "make user", |
| 119 | // that will only result in a more restrictive environment. The important part |
| 120 | // is that we don't produce and push "non-official" builds to the end user. |
| 121 | // |
| 122 | // In case of a non-bool value, we take the most restrictive option and |
| 123 | // assume we are in an official-build. |
| 124 | return property_get_bool("ro.secure", 1) != 0; |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | bool HardwareAndroid::IsNormalBootMode() const { |
Alex Deymo | 1c4e84a | 2015-09-22 16:58:10 -0700 | [diff] [blame] | 128 | // We are running in "dev-mode" iff ro.debuggable == 1. In dev-mode the |
| 129 | // update_engine will allow extra developers options, such as providing a |
| 130 | // different update URL. In case of error, we assume the build is in |
| 131 | // normal-mode. |
| 132 | return property_get_bool("ro.debuggable", 0) != 1; |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Sen Jiang | e67bb5b | 2016-06-20 15:53:56 -0700 | [diff] [blame] | 135 | bool HardwareAndroid::AreDevFeaturesEnabled() const { |
| 136 | return !IsNormalBootMode(); |
| 137 | } |
| 138 | |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 139 | bool HardwareAndroid::IsOOBEEnabled() const { |
| 140 | // No OOBE flow blocking updates for Android-based boards. |
| 141 | return false; |
| 142 | } |
| 143 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 144 | bool HardwareAndroid::IsOOBEComplete(base::Time* out_time_of_oobe) const { |
Alex Deymo | 46a9aae | 2016-05-04 20:20:11 -0700 | [diff] [blame] | 145 | LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() called."; |
Alex Deymo | 4d2990d | 2015-09-15 12:11:26 -0700 | [diff] [blame] | 146 | if (out_time_of_oobe) |
| 147 | *out_time_of_oobe = base::Time(); |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 148 | return true; |
| 149 | } |
| 150 | |
| 151 | string HardwareAndroid::GetHardwareClass() const { |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame^] | 152 | char manufacturer[PROPERTY_VALUE_MAX]; |
| 153 | char sku[PROPERTY_VALUE_MAX]; |
| 154 | char revision[PROPERTY_VALUE_MAX]; |
| 155 | property_get(kPropBootHardwareSKU, sku, ""); |
| 156 | property_get(kPropProductManufacturer, manufacturer, ""); |
| 157 | property_get(kPropBootRevision, revision, ""); |
| 158 | |
| 159 | return base::StringPrintf("%s:%s:%s", manufacturer, sku, revision); |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | string HardwareAndroid::GetFirmwareVersion() const { |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame^] | 163 | char bootloader[PROPERTY_VALUE_MAX]; |
| 164 | property_get(kPropBootBootloader, bootloader, ""); |
| 165 | return bootloader; |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | string HardwareAndroid::GetECVersion() const { |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame^] | 169 | char baseband[PROPERTY_VALUE_MAX]; |
| 170 | property_get(kPropBootBaseband, baseband, ""); |
| 171 | return baseband; |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | int HardwareAndroid::GetPowerwashCount() const { |
| 175 | LOG(WARNING) << "STUB: Assuming no factory reset was performed."; |
| 176 | return 0; |
| 177 | } |
| 178 | |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 179 | bool HardwareAndroid::SchedulePowerwash() { |
| 180 | LOG(INFO) << "Scheduling a powerwash to BCB."; |
| 181 | return WriteBootloaderRecoveryMessage(kAndroidRecoveryPowerwashCommand); |
| 182 | } |
| 183 | |
| 184 | bool HardwareAndroid::CancelPowerwash() { |
| 185 | return WriteBootloaderRecoveryMessage(""); |
| 186 | } |
| 187 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 188 | bool HardwareAndroid::GetNonVolatileDirectory(base::FilePath* path) const { |
Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 189 | base::FilePath local_path(constants::kNonVolatileDirectory); |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 190 | if (!base::PathExists(local_path)) { |
| 191 | LOG(ERROR) << "Non-volatile directory not found: " << local_path.value(); |
| 192 | return false; |
| 193 | } |
| 194 | *path = local_path; |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | bool HardwareAndroid::GetPowerwashSafeDirectory(base::FilePath* path) const { |
| 199 | // On Android, we don't have a directory persisted across powerwash. |
| 200 | return false; |
| 201 | } |
| 202 | |
Alex Deymo | 40d86b2 | 2015-09-03 22:27:10 -0700 | [diff] [blame] | 203 | } // namespace chromeos_update_engine |