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 26 #include "update_engine/common/constants.h" 27 #include "update_engine/common/hardware_interface.h" 28 #include "update_engine/common/platform_constants.h" 29 #include "update_engine/common/utils.h" 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. 48 const char* root_prefix = nullptr; 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) { 71 std::string path; 72 if (root_prefix) 73 path = root_prefix; 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, 118 kLsbReleaseAutoUpdateServerKey, 119 constants::kOmahaDefaultProductionURL); 120 // Build fingerprint not used in Chrome OS. 121 result.build_fingerprint = ""; 122 result.allow_arbitrary_channels = false; 123 124 return result; 125 } 126 127 MutableImageProperties LoadMutableImageProperties(SystemState* system_state) { 128 MutableImageProperties result; 129 brillo::KeyValueStore lsb_release; 130 LoadLsbRelease(LsbReleaseSource::kSystem, &lsb_release); 131 LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release); 132 result.target_channel = GetStringWithDefault( 133 lsb_release, kLsbReleaseUpdateChannelKey, "stable-channel"); 134 if (!lsb_release.GetBoolean(kLsbReleaseIsPowerwashAllowedKey, 135 &result.is_powerwash_allowed)) 136 result.is_powerwash_allowed = false; 137 return result; 138 } 139 140 bool StoreMutableImageProperties(SystemState* system_state, 141 const MutableImageProperties& properties) { 142 brillo::KeyValueStore lsb_release; 143 LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release); 144 lsb_release.SetString(kLsbReleaseUpdateChannelKey, properties.target_channel); 145 lsb_release.SetBoolean(kLsbReleaseIsPowerwashAllowedKey, 146 properties.is_powerwash_allowed); 147 148 std::string root_prefix_str = root_prefix ? root_prefix : ""; 149 base::FilePath path(root_prefix_str + kStatefulPartition + kLsbRelease); 150 if (!base::DirectoryExists(path.DirName())) 151 base::CreateDirectory(path.DirName()); 152 return lsb_release.Save(path); 153 } 154 155 void LogImageProperties() { 156 std::string lsb_release; 157 if (utils::ReadFile(kLsbRelease, &lsb_release)) { 158 LOG(INFO) << "lsb-release inside the old rootfs:\n" << lsb_release; 159 } 160 161 std::string stateful_lsb_release; 162 if (utils::ReadFile(std::string(kStatefulPartition) + kLsbRelease, 163 &stateful_lsb_release)) { 164 LOG(INFO) << "stateful lsb-release:\n" << stateful_lsb_release; 165 } 166 } 167 168 } // namespace chromeos_update_engine 169