| 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 |  | 
|  | 19 | #include <string> | 
|  | 20 |  | 
| Tom Cherry | 012aa5b | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 21 | #include <android-base/properties.h> | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 22 | #include <base/logging.h> | 
|  | 23 | #include <brillo/osrelease_reader.h> | 
| Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 24 | #include <brillo/strings/string_utils.h> | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 25 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 26 | #include "update_engine/common/boot_control_interface.h" | 
|  | 27 | #include "update_engine/common/constants.h" | 
|  | 28 | #include "update_engine/common/platform_constants.h" | 
|  | 29 | #include "update_engine/common/prefs_interface.h" | 
| Sen Jiang | 684c9cd | 2017-10-17 16:26:45 -0700 | [diff] [blame] | 30 | #include "update_engine/common/utils.h" | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 31 | #include "update_engine/system_state.h" | 
|  | 32 |  | 
| Tom Cherry | 012aa5b | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 33 | using android::base::GetProperty; | 
| Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 34 | using std::string; | 
|  | 35 |  | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 36 | namespace chromeos_update_engine { | 
|  | 37 |  | 
|  | 38 | namespace { | 
|  | 39 |  | 
| Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 40 | // Build time properties name used in Android Things. | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 41 | const char kProductId[] = "product_id"; | 
|  | 42 | const char kProductVersion[] = "product_version"; | 
| Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 43 | const char kSystemId[] = "system_id"; | 
| Sen Jiang | 983f578 | 2017-02-21 15:46:02 -0800 | [diff] [blame] | 44 | const char kSystemVersion[] = "system_version"; | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 45 |  | 
| Sen Jiang | 684c9cd | 2017-10-17 16:26:45 -0700 | [diff] [blame] | 46 | // The path to the product_components file which stores the version of each | 
|  | 47 | // components in OEM partition. | 
|  | 48 | const char kProductComponentsPath[] = "/oem/os-release.d/product_components"; | 
|  | 49 |  | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 50 | // Prefs used to store the target channel and powerwash settings. | 
|  | 51 | const char kPrefsImgPropChannelName[] = "img-prop-channel-name"; | 
|  | 52 | const char kPrefsImgPropPowerwashAllowed[] = "img-prop-powerwash-allowed"; | 
|  | 53 |  | 
| Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 54 | // System properties that identifies the "board". | 
|  | 55 | const char kPropProductName[] = "ro.product.name"; | 
|  | 56 | const char kPropBuildFingerprint[] = "ro.build.fingerprint"; | 
| Sen Jiang | 1d5d95f | 2017-05-19 11:33:10 -0700 | [diff] [blame] | 57 | const char kPropBuildType[] = "ro.build.type"; | 
| Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 58 |  | 
| Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 59 | // A prefix added to the path, used for testing. | 
|  | 60 | const char* root_prefix = nullptr; | 
|  | 61 |  | 
|  | 62 | string GetStringWithDefault(const brillo::OsReleaseReader& osrelease, | 
|  | 63 | const string& key, | 
|  | 64 | const string& default_value) { | 
|  | 65 | string result; | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 66 | if (osrelease.GetString(key, &result)) | 
|  | 67 | return result; | 
|  | 68 | LOG(INFO) << "Cannot load ImageProperty " << key << ", using default value " | 
|  | 69 | << default_value; | 
|  | 70 | return default_value; | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | }  // namespace | 
|  | 74 |  | 
|  | 75 | namespace test { | 
| Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 76 | void SetImagePropertiesRootPrefix(const char* test_root_prefix) { | 
|  | 77 | root_prefix = test_root_prefix; | 
|  | 78 | } | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 79 | }  // namespace test | 
|  | 80 |  | 
|  | 81 | ImageProperties LoadImageProperties(SystemState* system_state) { | 
|  | 82 | ImageProperties result; | 
|  | 83 |  | 
|  | 84 | brillo::OsReleaseReader osrelease; | 
| Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 85 | if (root_prefix) | 
|  | 86 | osrelease.LoadTestingOnly(base::FilePath(root_prefix)); | 
|  | 87 | else | 
|  | 88 | osrelease.Load(); | 
| Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 89 | result.product_id = | 
|  | 90 | GetStringWithDefault(osrelease, kProductId, "invalid-product"); | 
|  | 91 | result.system_id = GetStringWithDefault( | 
|  | 92 | osrelease, kSystemId, "developer-boards:brillo-starter-board"); | 
| Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 93 | // Update the system id to match the prefix of product id for testing. | 
|  | 94 | string prefix, not_used, system_id; | 
|  | 95 | if (brillo::string_utils::SplitAtFirst( | 
|  | 96 | result.product_id, ":", &prefix, ¬_used, false) && | 
|  | 97 | brillo::string_utils::SplitAtFirst( | 
|  | 98 | result.system_id, ":", ¬_used, &system_id, false)) { | 
|  | 99 | result.system_id = prefix + ":" + system_id; | 
|  | 100 | } | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 101 | result.canary_product_id = result.product_id; | 
| Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 102 | result.version = GetStringWithDefault(osrelease, kProductVersion, "0.0.0.0"); | 
|  | 103 | result.system_version = | 
| Sen Jiang | 94a4dec | 2017-03-28 18:23:35 -0700 | [diff] [blame] | 104 | GetStringWithDefault(osrelease, kSystemVersion, "0.0.0.0"); | 
| Sen Jiang | 684c9cd | 2017-10-17 16:26:45 -0700 | [diff] [blame] | 105 | // Can't read it with OsReleaseReader because it has multiple lines. | 
|  | 106 | utils::ReadFile(kProductComponentsPath, &result.product_components); | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 107 |  | 
| Tom Cherry | 012aa5b | 2017-10-10 14:45:09 -0700 | [diff] [blame] | 108 | result.board = GetProperty(kPropProductName, "brillo"); | 
|  | 109 | result.build_fingerprint = GetProperty(kPropBuildFingerprint, "none"); | 
|  | 110 | result.build_type = GetProperty(kPropBuildType, ""); | 
| Sen Jiang | 1d5d95f | 2017-05-19 11:33:10 -0700 | [diff] [blame] | 111 |  | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 112 | // Brillo images don't have a channel assigned. We stored the name of the | 
|  | 113 | // channel where we got the image from in prefs at the time of the update, so | 
|  | 114 | // we use that as the current channel if available. During provisioning, there | 
|  | 115 | // is no value assigned, so we default to the "stable-channel". | 
| Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 116 | string current_channel_key = | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 117 | kPrefsChannelOnSlotPrefix + | 
|  | 118 | std::to_string(system_state->boot_control()->GetCurrentSlot()); | 
| Sen Jiang | b56fe9f | 2017-06-16 15:19:35 -0700 | [diff] [blame] | 119 | string current_channel; | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 120 | if (!system_state->prefs()->Exists(current_channel_key) || | 
|  | 121 | !system_state->prefs()->GetString(current_channel_key, ¤t_channel)) | 
|  | 122 | current_channel = "stable-channel"; | 
|  | 123 | result.current_channel = current_channel; | 
|  | 124 |  | 
|  | 125 | // Brillo only supports the official omaha URL. | 
|  | 126 | result.omaha_url = constants::kOmahaDefaultProductionURL; | 
|  | 127 |  | 
|  | 128 | return result; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | MutableImageProperties LoadMutableImageProperties(SystemState* system_state) { | 
|  | 132 | MutableImageProperties result; | 
|  | 133 | PrefsInterface* const prefs = system_state->prefs(); | 
|  | 134 | if (!prefs->GetString(kPrefsImgPropChannelName, &result.target_channel)) | 
|  | 135 | result.target_channel.clear(); | 
|  | 136 | if (!prefs->GetBoolean(kPrefsImgPropPowerwashAllowed, | 
|  | 137 | &result.is_powerwash_allowed)) { | 
|  | 138 | result.is_powerwash_allowed = false; | 
|  | 139 | } | 
|  | 140 | return result; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | bool StoreMutableImageProperties(SystemState* system_state, | 
|  | 144 | const MutableImageProperties& properties) { | 
|  | 145 | PrefsInterface* const prefs = system_state->prefs(); | 
|  | 146 | return ( | 
|  | 147 | prefs->SetString(kPrefsImgPropChannelName, properties.target_channel) && | 
|  | 148 | prefs->SetBoolean(kPrefsImgPropPowerwashAllowed, | 
|  | 149 | properties.is_powerwash_allowed)); | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | }  // namespace chromeos_update_engine |