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