| 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 | #include <vector> | 
 | 21 |  | 
 | 22 | #include <base/files/file_util.h> | 
 | 23 | #include <base/logging.h> | 
 | 24 | #include <brillo/key_value_store.h> | 
 | 25 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 26 | #include "update_engine/common/constants.h" | 
 | 27 | #include "update_engine/common/hardware_interface.h" | 
 | 28 | #include "update_engine/common/platform_constants.h" | 
| Amin Hassani | afd8cea | 2017-12-04 14:20:00 -0800 | [diff] [blame] | 29 | #include "update_engine/common/utils.h" | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 30 | #include "update_engine/system_state.h" | 
 | 31 |  | 
 | 32 | namespace { | 
 | 33 |  | 
 | 34 | const char kLsbRelease[] = "/etc/lsb-release"; | 
 | 35 |  | 
 | 36 | const char kLsbReleaseAppIdKey[] = "CHROMEOS_RELEASE_APPID"; | 
 | 37 | const char kLsbReleaseAutoUpdateServerKey[] = "CHROMEOS_AUSERVER"; | 
 | 38 | const char kLsbReleaseBoardAppIdKey[] = "CHROMEOS_BOARD_APPID"; | 
 | 39 | const char kLsbReleaseBoardKey[] = "CHROMEOS_RELEASE_BOARD"; | 
 | 40 | const char kLsbReleaseCanaryAppIdKey[] = "CHROMEOS_CANARY_APPID"; | 
 | 41 | const char kLsbReleaseIsPowerwashAllowedKey[] = "CHROMEOS_IS_POWERWASH_ALLOWED"; | 
 | 42 | const char kLsbReleaseUpdateChannelKey[] = "CHROMEOS_RELEASE_TRACK"; | 
 | 43 | const char kLsbReleaseVersionKey[] = "CHROMEOS_RELEASE_VERSION"; | 
 | 44 |  | 
 | 45 | const char kDefaultAppId[] = "{87efface-864d-49a5-9bb3-4b050a7c227a}"; | 
 | 46 |  | 
 | 47 | // A prefix added to the path, used for testing. | 
| Alex Deymo | 3be05c8 | 2015-10-23 11:29:11 -0700 | [diff] [blame] | 48 | const char* root_prefix = nullptr; | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 49 |  | 
 | 50 | std::string GetStringWithDefault(const brillo::KeyValueStore& store, | 
 | 51 |                                  const std::string& key, | 
 | 52 |                                  const std::string& default_value) { | 
 | 53 |   std::string result; | 
 | 54 |   if (store.GetString(key, &result)) | 
 | 55 |     return result; | 
 | 56 |   LOG(INFO) << "Cannot load ImageProperty " << key << ", using default value " | 
 | 57 |             << default_value; | 
 | 58 |   return default_value; | 
 | 59 | } | 
 | 60 |  | 
 | 61 | enum class LsbReleaseSource { | 
 | 62 |   kSystem, | 
 | 63 |   kStateful, | 
 | 64 | }; | 
 | 65 |  | 
 | 66 | // Loads the lsb-release properties into the key-value |store| reading the file | 
 | 67 | // from either the system image or the stateful partition as specified by | 
 | 68 | // |source|. The loaded values are added to the store, possibly overriding | 
 | 69 | // existing values. | 
 | 70 | void LoadLsbRelease(LsbReleaseSource source, brillo::KeyValueStore* store) { | 
| Alex Deymo | 3be05c8 | 2015-10-23 11:29:11 -0700 | [diff] [blame] | 71 |   std::string path; | 
 | 72 |   if (root_prefix) | 
 | 73 |     path = root_prefix; | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 74 |   if (source == LsbReleaseSource::kStateful) | 
 | 75 |     path += chromeos_update_engine::kStatefulPartition; | 
 | 76 |   store->Load(base::FilePath(path + kLsbRelease)); | 
 | 77 | } | 
 | 78 |  | 
 | 79 | }  // namespace | 
 | 80 |  | 
 | 81 | namespace chromeos_update_engine { | 
 | 82 |  | 
 | 83 | namespace test { | 
 | 84 | void SetImagePropertiesRootPrefix(const char* test_root_prefix) { | 
 | 85 |   root_prefix = test_root_prefix; | 
 | 86 | } | 
 | 87 | }  // namespace test | 
 | 88 |  | 
 | 89 | ImageProperties LoadImageProperties(SystemState* system_state) { | 
 | 90 |   ImageProperties result; | 
 | 91 |  | 
 | 92 |   brillo::KeyValueStore lsb_release; | 
 | 93 |   LoadLsbRelease(LsbReleaseSource::kSystem, &lsb_release); | 
 | 94 |   result.current_channel = GetStringWithDefault( | 
 | 95 |       lsb_release, kLsbReleaseUpdateChannelKey, "stable-channel"); | 
 | 96 |  | 
 | 97 |   // In dev-mode and unofficial build we can override the image properties set | 
 | 98 |   // in the system image with the ones from the stateful partition, except the | 
 | 99 |   // channel of the current image. | 
 | 100 |   HardwareInterface* const hardware = system_state->hardware(); | 
 | 101 |   if (!hardware->IsOfficialBuild() || !hardware->IsNormalBootMode()) | 
 | 102 |     LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release); | 
 | 103 |  | 
 | 104 |   // The release_app_id is used as the default appid, but can be override by | 
 | 105 |   // the board appid in the general case or the canary appid for the canary | 
 | 106 |   // channel only. | 
 | 107 |   std::string release_app_id = | 
 | 108 |       GetStringWithDefault(lsb_release, kLsbReleaseAppIdKey, kDefaultAppId); | 
 | 109 |  | 
 | 110 |   result.product_id = GetStringWithDefault( | 
 | 111 |       lsb_release, kLsbReleaseBoardAppIdKey, release_app_id); | 
 | 112 |   result.canary_product_id = GetStringWithDefault( | 
 | 113 |       lsb_release, kLsbReleaseCanaryAppIdKey, release_app_id); | 
 | 114 |   result.board = GetStringWithDefault(lsb_release, kLsbReleaseBoardKey, ""); | 
 | 115 |   result.version = GetStringWithDefault(lsb_release, kLsbReleaseVersionKey, ""); | 
 | 116 |   result.omaha_url = | 
 | 117 |       GetStringWithDefault(lsb_release, kLsbReleaseAutoUpdateServerKey, | 
 | 118 |                            constants::kOmahaDefaultProductionURL); | 
| Alex Deymo | ebf6e12 | 2017-03-10 16:12:01 -0800 | [diff] [blame] | 119 |   // Build fingerprint not used in Chrome OS. | 
| Alex Deymo | 558fe6a | 2017-05-19 13:16:20 -0700 | [diff] [blame] | 120 |   result.build_fingerprint = ""; | 
| Sen Jiang | 7f785f5 | 2018-01-24 13:31:56 -0800 | [diff] [blame] | 121 |   result.allow_arbitrary_channels = false; | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 122 |  | 
 | 123 |   return result; | 
 | 124 | } | 
 | 125 |  | 
 | 126 | MutableImageProperties LoadMutableImageProperties(SystemState* system_state) { | 
 | 127 |   MutableImageProperties result; | 
 | 128 |   brillo::KeyValueStore lsb_release; | 
 | 129 |   LoadLsbRelease(LsbReleaseSource::kSystem, &lsb_release); | 
 | 130 |   LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release); | 
 | 131 |   result.target_channel = GetStringWithDefault( | 
 | 132 |       lsb_release, kLsbReleaseUpdateChannelKey, "stable-channel"); | 
 | 133 |   if (!lsb_release.GetBoolean(kLsbReleaseIsPowerwashAllowedKey, | 
 | 134 |                               &result.is_powerwash_allowed)) | 
 | 135 |     result.is_powerwash_allowed = false; | 
 | 136 |   return result; | 
 | 137 | } | 
 | 138 |  | 
 | 139 | bool StoreMutableImageProperties(SystemState* system_state, | 
 | 140 |                                  const MutableImageProperties& properties) { | 
 | 141 |   brillo::KeyValueStore lsb_release; | 
 | 142 |   LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release); | 
 | 143 |   lsb_release.SetString(kLsbReleaseUpdateChannelKey, properties.target_channel); | 
 | 144 |   lsb_release.SetBoolean(kLsbReleaseIsPowerwashAllowedKey, | 
 | 145 |                          properties.is_powerwash_allowed); | 
 | 146 |  | 
| Alex Deymo | 5378f5e | 2015-11-10 15:02:50 -0800 | [diff] [blame] | 147 |   std::string root_prefix_str = root_prefix ? root_prefix : ""; | 
 | 148 |   base::FilePath path(root_prefix_str + kStatefulPartition + kLsbRelease); | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 149 |   if (!base::DirectoryExists(path.DirName())) | 
 | 150 |     base::CreateDirectory(path.DirName()); | 
 | 151 |   return lsb_release.Save(path); | 
 | 152 | } | 
 | 153 |  | 
| Amin Hassani | afd8cea | 2017-12-04 14:20:00 -0800 | [diff] [blame] | 154 | void LogImageProperties() { | 
 | 155 |   std::string lsb_release; | 
 | 156 |   if (utils::ReadFile(kLsbRelease, &lsb_release)) { | 
 | 157 |     LOG(INFO) << "lsb-release inside the old rootfs:\n" << lsb_release; | 
 | 158 |   } | 
 | 159 |  | 
 | 160 |   std::string stateful_lsb_release; | 
 | 161 |   if (utils::ReadFile(std::string(kStatefulPartition) + kLsbRelease, | 
 | 162 |                       &stateful_lsb_release)) { | 
 | 163 |     LOG(INFO) << "stateful lsb-release:\n" << stateful_lsb_release; | 
 | 164 |   } | 
 | 165 | } | 
 | 166 |  | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 167 | }  // namespace chromeos_update_engine |