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