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/system_state.h" 30 31namespace { 32 33const char kLsbRelease[] = "/etc/lsb-release"; 34 35const char kLsbReleaseAppIdKey[] = "CHROMEOS_RELEASE_APPID"; 36const char kLsbReleaseAutoUpdateServerKey[] = "CHROMEOS_AUSERVER"; 37const char kLsbReleaseBoardAppIdKey[] = "CHROMEOS_BOARD_APPID"; 38const char kLsbReleaseBoardKey[] = "CHROMEOS_RELEASE_BOARD"; 39const char kLsbReleaseCanaryAppIdKey[] = "CHROMEOS_CANARY_APPID"; 40const char kLsbReleaseIsPowerwashAllowedKey[] = "CHROMEOS_IS_POWERWASH_ALLOWED"; 41const char kLsbReleaseUpdateChannelKey[] = "CHROMEOS_RELEASE_TRACK"; 42const char kLsbReleaseVersionKey[] = "CHROMEOS_RELEASE_VERSION"; 43 44const char kDefaultAppId[] = "{87efface-864d-49a5-9bb3-4b050a7c227a}"; 45 46// A prefix added to the path, used for testing. 47const char* root_prefix = nullptr; 48 49std::string GetStringWithDefault(const brillo::KeyValueStore& store, 50 const std::string& key, 51 const std::string& default_value) { 52 std::string result; 53 if (store.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 60enum class LsbReleaseSource { 61 kSystem, 62 kStateful, 63}; 64 65// Loads the lsb-release properties into the key-value |store| reading the file 66// from either the system image or the stateful partition as specified by 67// |source|. The loaded values are added to the store, possibly overriding 68// existing values. 69void LoadLsbRelease(LsbReleaseSource source, brillo::KeyValueStore* store) { 70 std::string path; 71 if (root_prefix) 72 path = root_prefix; 73 if (source == LsbReleaseSource::kStateful) 74 path += chromeos_update_engine::kStatefulPartition; 75 store->Load(base::FilePath(path + kLsbRelease)); 76} 77 78} // namespace 79 80namespace chromeos_update_engine { 81 82namespace test { 83void SetImagePropertiesRootPrefix(const char* test_root_prefix) { 84 root_prefix = test_root_prefix; 85} 86} // namespace test 87 88ImageProperties LoadImageProperties(SystemState* system_state) { 89 ImageProperties result; 90 91 brillo::KeyValueStore lsb_release; 92 LoadLsbRelease(LsbReleaseSource::kSystem, &lsb_release); 93 result.current_channel = GetStringWithDefault( 94 lsb_release, kLsbReleaseUpdateChannelKey, "stable-channel"); 95 96 // In dev-mode and unofficial build we can override the image properties set 97 // in the system image with the ones from the stateful partition, except the 98 // channel of the current image. 99 HardwareInterface* const hardware = system_state->hardware(); 100 if (!hardware->IsOfficialBuild() || !hardware->IsNormalBootMode()) 101 LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release); 102 103 // The release_app_id is used as the default appid, but can be override by 104 // the board appid in the general case or the canary appid for the canary 105 // channel only. 106 std::string release_app_id = 107 GetStringWithDefault(lsb_release, kLsbReleaseAppIdKey, kDefaultAppId); 108 109 result.product_id = GetStringWithDefault( 110 lsb_release, kLsbReleaseBoardAppIdKey, release_app_id); 111 result.canary_product_id = GetStringWithDefault( 112 lsb_release, kLsbReleaseCanaryAppIdKey, release_app_id); 113 result.board = GetStringWithDefault(lsb_release, kLsbReleaseBoardKey, ""); 114 result.version = GetStringWithDefault(lsb_release, kLsbReleaseVersionKey, ""); 115 result.omaha_url = 116 GetStringWithDefault(lsb_release, kLsbReleaseAutoUpdateServerKey, 117 constants::kOmahaDefaultProductionURL); 118 // Build fingerprint not used in Chrome OS. 119 result.build_fingerprint = ""; 120 121 return result; 122} 123 124MutableImageProperties LoadMutableImageProperties(SystemState* system_state) { 125 MutableImageProperties result; 126 brillo::KeyValueStore lsb_release; 127 LoadLsbRelease(LsbReleaseSource::kSystem, &lsb_release); 128 LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release); 129 result.target_channel = GetStringWithDefault( 130 lsb_release, kLsbReleaseUpdateChannelKey, "stable-channel"); 131 if (!lsb_release.GetBoolean(kLsbReleaseIsPowerwashAllowedKey, 132 &result.is_powerwash_allowed)) 133 result.is_powerwash_allowed = false; 134 return result; 135} 136 137bool StoreMutableImageProperties(SystemState* system_state, 138 const MutableImageProperties& properties) { 139 brillo::KeyValueStore lsb_release; 140 LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release); 141 lsb_release.SetString(kLsbReleaseUpdateChannelKey, properties.target_channel); 142 lsb_release.SetBoolean(kLsbReleaseIsPowerwashAllowedKey, 143 properties.is_powerwash_allowed); 144 145 std::string root_prefix_str = root_prefix ? root_prefix : ""; 146 base::FilePath path(root_prefix_str + kStatefulPartition + kLsbRelease); 147 if (!base::DirectoryExists(path.DirName())) 148 base::CreateDirectory(path.DirName()); 149 return lsb_release.Save(path); 150} 151 152} // namespace chromeos_update_engine 153