image_properties_chromeos.cc 6.2 KB

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