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 
31 namespace {
32 
33 const char kLsbRelease[] = "/etc/lsb-release";
34 
35 const char kLsbReleaseAppIdKey[] = "CHROMEOS_RELEASE_APPID";
36 const char kLsbReleaseAutoUpdateServerKey[] = "CHROMEOS_AUSERVER";
37 const char kLsbReleaseBoardAppIdKey[] = "CHROMEOS_BOARD_APPID";
38 const char kLsbReleaseBoardKey[] = "CHROMEOS_RELEASE_BOARD";
39 const char kLsbReleaseCanaryAppIdKey[] = "CHROMEOS_CANARY_APPID";
40 const char kLsbReleaseIsPowerwashAllowedKey[] = "CHROMEOS_IS_POWERWASH_ALLOWED";
41 const char kLsbReleaseUpdateChannelKey[] = "CHROMEOS_RELEASE_TRACK";
42 const char kLsbReleaseVersionKey[] = "CHROMEOS_RELEASE_VERSION";
43 
44 const char kDefaultAppId[] = "{87efface-864d-49a5-9bb3-4b050a7c227a}";
45 
46 // A prefix added to the path, used for testing.
47 const char* root_prefix = nullptr;
48 
GetStringWithDefault(const brillo::KeyValueStore & store,const std::string & key,const std::string & default_value)49 std::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 
60 enum 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.
LoadLsbRelease(LsbReleaseSource source,brillo::KeyValueStore * store)69 void 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 
80 namespace chromeos_update_engine {
81 
82 namespace test {
SetImagePropertiesRootPrefix(const char * test_root_prefix)83 void SetImagePropertiesRootPrefix(const char* test_root_prefix) {
84   root_prefix = test_root_prefix;
85 }
86 }  // namespace test
87 
LoadImageProperties(SystemState * system_state)88 ImageProperties 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 
119   return result;
120 }
121 
LoadMutableImageProperties(SystemState * system_state)122 MutableImageProperties LoadMutableImageProperties(SystemState* system_state) {
123   MutableImageProperties result;
124   brillo::KeyValueStore lsb_release;
125   LoadLsbRelease(LsbReleaseSource::kSystem, &lsb_release);
126   LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release);
127   result.target_channel = GetStringWithDefault(
128       lsb_release, kLsbReleaseUpdateChannelKey, "stable-channel");
129   if (!lsb_release.GetBoolean(kLsbReleaseIsPowerwashAllowedKey,
130                               &result.is_powerwash_allowed))
131     result.is_powerwash_allowed = false;
132   return result;
133 }
134 
StoreMutableImageProperties(SystemState * system_state,const MutableImageProperties & properties)135 bool StoreMutableImageProperties(SystemState* system_state,
136                                  const MutableImageProperties& properties) {
137   brillo::KeyValueStore lsb_release;
138   LoadLsbRelease(LsbReleaseSource::kStateful, &lsb_release);
139   lsb_release.SetString(kLsbReleaseUpdateChannelKey, properties.target_channel);
140   lsb_release.SetBoolean(kLsbReleaseIsPowerwashAllowedKey,
141                          properties.is_powerwash_allowed);
142 
143   std::string root_prefix_str = root_prefix ? root_prefix : "";
144   base::FilePath path(root_prefix_str + kStatefulPartition + kLsbRelease);
145   if (!base::DirectoryExists(path.DirName()))
146     base::CreateDirectory(path.DirName());
147   return lsb_release.Save(path);
148 }
149 
150 }  // namespace chromeos_update_engine
151