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