Alex Deymo | 8561665 | 2015-10-15 18:48:31 -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 | |
| 17 | #include "update_engine/image_properties.h" |
| 18 | |
Sen Jiang | 740c2fc | 2018-01-22 17:10:41 -0800 | [diff] [blame^] | 19 | #include <fcntl.h> |
| 20 | |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
Tom Cherry | 012aa5b | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 23 | #include <android-base/properties.h> |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 24 | #include <base/logging.h> |
Sen Jiang | 740c2fc | 2018-01-22 17:10:41 -0800 | [diff] [blame^] | 25 | #include <bootloader.h> |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 26 | #include <brillo/osrelease_reader.h> |
Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 27 | #include <brillo/strings/string_utils.h> |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 28 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 29 | #include "update_engine/common/boot_control_interface.h" |
| 30 | #include "update_engine/common/constants.h" |
| 31 | #include "update_engine/common/platform_constants.h" |
| 32 | #include "update_engine/common/prefs_interface.h" |
Sen Jiang | 684c9cd | 2017-10-17 16:26:45 -0700 | [diff] [blame] | 33 | #include "update_engine/common/utils.h" |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 34 | #include "update_engine/system_state.h" |
Sen Jiang | 740c2fc | 2018-01-22 17:10:41 -0800 | [diff] [blame^] | 35 | #include "update_engine/utils_android.h" |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 36 | |
Tom Cherry | 012aa5b | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 37 | using android::base::GetProperty; |
Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 38 | using std::string; |
| 39 | |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 40 | namespace chromeos_update_engine { |
| 41 | |
| 42 | namespace { |
| 43 | |
Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 44 | // Build time properties name used in Android Things. |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 45 | const char kProductId[] = "product_id"; |
| 46 | const char kProductVersion[] = "product_version"; |
Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 47 | const char kSystemId[] = "system_id"; |
Sen Jiang | 983f578 | 2017-02-21 15:46:02 -0800 | [diff] [blame] | 48 | const char kSystemVersion[] = "system_version"; |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 49 | |
Sen Jiang | 684c9cd | 2017-10-17 16:26:45 -0700 | [diff] [blame] | 50 | // The path to the product_components file which stores the version of each |
| 51 | // components in OEM partition. |
| 52 | const char kProductComponentsPath[] = "/oem/os-release.d/product_components"; |
| 53 | |
Sen Jiang | 740c2fc | 2018-01-22 17:10:41 -0800 | [diff] [blame^] | 54 | // Prefs used to store the powerwash settings. |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 55 | const char kPrefsImgPropPowerwashAllowed[] = "img-prop-powerwash-allowed"; |
| 56 | |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 57 | // System properties that identifies the "board". |
| 58 | const char kPropProductName[] = "ro.product.name"; |
| 59 | const char kPropBuildFingerprint[] = "ro.build.fingerprint"; |
Sen Jiang | 1d5d95f | 2017-05-19 11:33:10 -0700 | [diff] [blame] | 60 | const char kPropBuildType[] = "ro.build.type"; |
Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 61 | |
Sen Jiang | 740c2fc | 2018-01-22 17:10:41 -0800 | [diff] [blame^] | 62 | // Default channel from factory.prop |
| 63 | const char kPropDefaultChannel[] = "ro.update.default_channel"; |
| 64 | |
Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 65 | // A prefix added to the path, used for testing. |
| 66 | const char* root_prefix = nullptr; |
| 67 | |
| 68 | string GetStringWithDefault(const brillo::OsReleaseReader& osrelease, |
| 69 | const string& key, |
| 70 | const string& default_value) { |
| 71 | string result; |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 72 | if (osrelease.GetString(key, &result)) |
| 73 | return result; |
| 74 | LOG(INFO) << "Cannot load ImageProperty " << key << ", using default value " |
| 75 | << default_value; |
| 76 | return default_value; |
| 77 | } |
| 78 | |
Sen Jiang | 740c2fc | 2018-01-22 17:10:41 -0800 | [diff] [blame^] | 79 | // Open misc partition for read or write and output the fd in |out_fd|. |
| 80 | bool OpenMisc(bool write, int* out_fd) { |
| 81 | base::FilePath misc_device; |
| 82 | int flags = write ? O_WRONLY | O_SYNC : O_RDONLY; |
| 83 | if (root_prefix) { |
| 84 | // Use a file for unittest and create one if doesn't exist. |
| 85 | misc_device = base::FilePath(root_prefix).Append("misc"); |
| 86 | if (write) |
| 87 | flags |= O_CREAT; |
| 88 | } else if (!utils::DeviceForMountPoint("/misc", &misc_device)) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | int fd = HANDLE_EINTR(open(misc_device.value().c_str(), flags, 0600)); |
| 93 | if (fd < 0) { |
| 94 | PLOG(ERROR) << "Opening misc failed"; |
| 95 | return false; |
| 96 | } |
| 97 | *out_fd = fd; |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | // The offset and size of the channel field in misc partition. |
| 102 | constexpr size_t kChannelOffset = |
| 103 | BOOTLOADER_MESSAGE_OFFSET_IN_MISC + |
| 104 | offsetof(bootloader_message_ab, update_channel); |
| 105 | constexpr size_t kChannelSize = sizeof(bootloader_message_ab::update_channel); |
| 106 | |
| 107 | // Read channel from misc partition to |out_channel|, return false if unable to |
| 108 | // read misc or no channel is set in misc. |
| 109 | bool ReadChannelFromMisc(string* out_channel) { |
| 110 | int fd; |
| 111 | TEST_AND_RETURN_FALSE(OpenMisc(false, &fd)); |
| 112 | ScopedFdCloser fd_closer(&fd); |
| 113 | char channel[kChannelSize] = {0}; |
| 114 | ssize_t bytes_read = 0; |
| 115 | if (!utils::PReadAll( |
| 116 | fd, channel, kChannelSize - 1, kChannelOffset, &bytes_read) || |
| 117 | bytes_read != kChannelSize - 1) { |
| 118 | PLOG(ERROR) << "Reading update channel from misc failed"; |
| 119 | return false; |
| 120 | } |
| 121 | if (channel[0] == '\0') { |
| 122 | LOG(INFO) << "No channel set in misc."; |
| 123 | return false; |
| 124 | } |
| 125 | out_channel->assign(channel); |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | // Write |in_channel| to misc partition, return false if failed to write. |
| 130 | bool WriteChannelToMisc(const string& in_channel) { |
| 131 | int fd; |
| 132 | TEST_AND_RETURN_FALSE(OpenMisc(true, &fd)); |
| 133 | ScopedFdCloser fd_closer(&fd); |
| 134 | if (in_channel.size() >= kChannelSize) { |
| 135 | LOG(ERROR) << "Channel name is too long: " << in_channel |
| 136 | << ", the maximum length is " << kChannelSize - 1; |
| 137 | return false; |
| 138 | } |
| 139 | char channel[kChannelSize] = {0}; |
| 140 | memcpy(channel, in_channel.data(), in_channel.size()); |
| 141 | if (!utils::PWriteAll(fd, channel, kChannelSize, kChannelOffset)) { |
| 142 | PLOG(ERROR) << "Writing update channel to misc failed"; |
| 143 | return false; |
| 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | string GetTargetChannel() { |
| 149 | string channel; |
| 150 | if (!ReadChannelFromMisc(&channel)) |
| 151 | channel = GetProperty(kPropDefaultChannel, "stable-channel"); |
| 152 | return channel; |
| 153 | } |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 154 | } // namespace |
| 155 | |
| 156 | namespace test { |
Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 157 | void SetImagePropertiesRootPrefix(const char* test_root_prefix) { |
| 158 | root_prefix = test_root_prefix; |
| 159 | } |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 160 | } // namespace test |
| 161 | |
| 162 | ImageProperties LoadImageProperties(SystemState* system_state) { |
| 163 | ImageProperties result; |
| 164 | |
| 165 | brillo::OsReleaseReader osrelease; |
Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 166 | if (root_prefix) |
| 167 | osrelease.LoadTestingOnly(base::FilePath(root_prefix)); |
| 168 | else |
| 169 | osrelease.Load(); |
Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 170 | result.product_id = |
| 171 | GetStringWithDefault(osrelease, kProductId, "invalid-product"); |
| 172 | result.system_id = GetStringWithDefault( |
| 173 | osrelease, kSystemId, "developer-boards:brillo-starter-board"); |
Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 174 | // Update the system id to match the prefix of product id for testing. |
| 175 | string prefix, not_used, system_id; |
| 176 | if (brillo::string_utils::SplitAtFirst( |
| 177 | result.product_id, ":", &prefix, ¬_used, false) && |
| 178 | brillo::string_utils::SplitAtFirst( |
| 179 | result.system_id, ":", ¬_used, &system_id, false)) { |
| 180 | result.system_id = prefix + ":" + system_id; |
| 181 | } |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 182 | result.canary_product_id = result.product_id; |
Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 183 | result.version = GetStringWithDefault(osrelease, kProductVersion, "0.0.0.0"); |
| 184 | result.system_version = |
Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 185 | GetStringWithDefault(osrelease, kSystemVersion, "0.0.0.0"); |
Sen Jiang | 684c9cd | 2017-10-17 16:26:45 -0700 | [diff] [blame] | 186 | // Can't read it with OsReleaseReader because it has multiple lines. |
| 187 | utils::ReadFile(kProductComponentsPath, &result.product_components); |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 188 | |
Tom Cherry | 012aa5b | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 189 | result.board = GetProperty(kPropProductName, "brillo"); |
| 190 | result.build_fingerprint = GetProperty(kPropBuildFingerprint, "none"); |
| 191 | result.build_type = GetProperty(kPropBuildType, ""); |
Sen Jiang | 1d5d95f | 2017-05-19 11:33:10 -0700 | [diff] [blame] | 192 | |
Sen Jiang | 740c2fc | 2018-01-22 17:10:41 -0800 | [diff] [blame^] | 193 | // Android doesn't have channel information in system image, we try to read |
| 194 | // the channel of current slot from prefs and then fallback to use the |
| 195 | // persisted target channel as current channel. |
Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 196 | string current_channel_key = |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 197 | kPrefsChannelOnSlotPrefix + |
| 198 | std::to_string(system_state->boot_control()->GetCurrentSlot()); |
Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 199 | string current_channel; |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 200 | if (!system_state->prefs()->Exists(current_channel_key) || |
| 201 | !system_state->prefs()->GetString(current_channel_key, ¤t_channel)) |
Sen Jiang | 740c2fc | 2018-01-22 17:10:41 -0800 | [diff] [blame^] | 202 | current_channel = GetTargetChannel(); |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 203 | result.current_channel = current_channel; |
| 204 | |
| 205 | // Brillo only supports the official omaha URL. |
| 206 | result.omaha_url = constants::kOmahaDefaultProductionURL; |
| 207 | |
| 208 | return result; |
| 209 | } |
| 210 | |
| 211 | MutableImageProperties LoadMutableImageProperties(SystemState* system_state) { |
| 212 | MutableImageProperties result; |
Sen Jiang | 740c2fc | 2018-01-22 17:10:41 -0800 | [diff] [blame^] | 213 | result.target_channel = GetTargetChannel(); |
| 214 | if (!system_state->prefs()->GetBoolean(kPrefsImgPropPowerwashAllowed, |
| 215 | &result.is_powerwash_allowed)) { |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 216 | result.is_powerwash_allowed = false; |
| 217 | } |
| 218 | return result; |
| 219 | } |
| 220 | |
| 221 | bool StoreMutableImageProperties(SystemState* system_state, |
| 222 | const MutableImageProperties& properties) { |
Sen Jiang | 740c2fc | 2018-01-22 17:10:41 -0800 | [diff] [blame^] | 223 | bool ret = true; |
| 224 | if (!WriteChannelToMisc(properties.target_channel)) |
| 225 | ret = false; |
| 226 | if (!system_state->prefs()->SetBoolean(kPrefsImgPropPowerwashAllowed, |
| 227 | properties.is_powerwash_allowed)) |
| 228 | ret = false; |
| 229 | return ret; |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Amin Hassani | afd8cea | 2017-12-04 14:20:00 -0800 | [diff] [blame] | 232 | void LogImageProperties() { |
| 233 | // TODO(*): Implement this. |
| 234 | } |
| 235 | |
Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 236 | } // namespace chromeos_update_engine |