hardware_chromeos.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. //
  2. // Copyright (C) 2013 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/hardware_chromeos.h"
  17. #include <utility>
  18. #include <base/files/file_path.h>
  19. #include <base/files/file_util.h>
  20. #include <base/logging.h>
  21. #include <base/strings/string_number_conversions.h>
  22. #include <base/strings/string_util.h>
  23. #include <brillo/key_value_store.h>
  24. #include <debugd/dbus-constants.h>
  25. #include <vboot/crossystem.h>
  26. extern "C" {
  27. #include "vboot/vboot_host.h"
  28. }
  29. #include "update_engine/common/constants.h"
  30. #include "update_engine/common/hardware.h"
  31. #include "update_engine/common/hwid_override.h"
  32. #include "update_engine/common/platform_constants.h"
  33. #include "update_engine/common/subprocess.h"
  34. #include "update_engine/common/utils.h"
  35. #include "update_engine/dbus_connection.h"
  36. using std::string;
  37. using std::vector;
  38. namespace {
  39. const char kOOBECompletedMarker[] = "/home/chronos/.oobe_completed";
  40. // The stateful directory used by update_engine to store powerwash-safe files.
  41. // The files stored here must be whitelisted in the powerwash scripts.
  42. const char kPowerwashSafeDirectory[] =
  43. "/mnt/stateful_partition/unencrypted/preserve";
  44. // The powerwash_count marker file contains the number of times the device was
  45. // powerwashed. This value is incremented by the clobber-state script when
  46. // a powerwash is performed.
  47. const char kPowerwashCountMarker[] = "powerwash_count";
  48. // The name of the marker file used to trigger powerwash when post-install
  49. // completes successfully so that the device is powerwashed on next reboot.
  50. const char kPowerwashMarkerFile[] =
  51. "/mnt/stateful_partition/factory_install_reset";
  52. // The contents of the powerwash marker file for the non-rollback case.
  53. const char kPowerwashCommand[] = "safe fast keepimg reason=update_engine\n";
  54. // The contents of the powerwas marker file for the rollback case.
  55. const char kRollbackPowerwashCommand[] =
  56. "safe fast keepimg rollback reason=update_engine\n";
  57. // UpdateManager config path.
  58. const char* kConfigFilePath = "/etc/update_manager.conf";
  59. // UpdateManager config options:
  60. const char* kConfigOptsIsOOBEEnabled = "is_oobe_enabled";
  61. const char* kActivePingKey = "first_active_omaha_ping_sent";
  62. } // namespace
  63. namespace chromeos_update_engine {
  64. namespace hardware {
  65. // Factory defined in hardware.h.
  66. std::unique_ptr<HardwareInterface> CreateHardware() {
  67. std::unique_ptr<HardwareChromeOS> hardware(new HardwareChromeOS());
  68. hardware->Init();
  69. return std::move(hardware);
  70. }
  71. } // namespace hardware
  72. void HardwareChromeOS::Init() {
  73. LoadConfig("" /* root_prefix */, IsNormalBootMode());
  74. debugd_proxy_.reset(
  75. new org::chromium::debugdProxy(DBusConnection::Get()->GetDBus()));
  76. }
  77. bool HardwareChromeOS::IsOfficialBuild() const {
  78. return VbGetSystemPropertyInt("debug_build") == 0;
  79. }
  80. bool HardwareChromeOS::IsNormalBootMode() const {
  81. bool dev_mode = VbGetSystemPropertyInt("devsw_boot") != 0;
  82. return !dev_mode;
  83. }
  84. bool HardwareChromeOS::AreDevFeaturesEnabled() const {
  85. // Even though the debugd tools are also gated on devmode, checking here can
  86. // save us a D-Bus call so it's worth doing explicitly.
  87. if (IsNormalBootMode())
  88. return false;
  89. int32_t dev_features = debugd::DEV_FEATURES_DISABLED;
  90. brillo::ErrorPtr error;
  91. // Some boards may not include debugd so it's expected that this may fail,
  92. // in which case we treat it as disabled.
  93. if (debugd_proxy_ && debugd_proxy_->QueryDevFeatures(&dev_features, &error) &&
  94. !(dev_features & debugd::DEV_FEATURES_DISABLED)) {
  95. LOG(INFO) << "Debugd dev tools enabled.";
  96. return true;
  97. }
  98. return false;
  99. }
  100. bool HardwareChromeOS::IsOOBEEnabled() const {
  101. return is_oobe_enabled_;
  102. }
  103. bool HardwareChromeOS::IsOOBEComplete(base::Time* out_time_of_oobe) const {
  104. if (!is_oobe_enabled_) {
  105. LOG(WARNING) << "OOBE is not enabled but IsOOBEComplete() was called";
  106. }
  107. struct stat statbuf;
  108. if (stat(kOOBECompletedMarker, &statbuf) != 0) {
  109. if (errno != ENOENT) {
  110. PLOG(ERROR) << "Error getting information about " << kOOBECompletedMarker;
  111. }
  112. return false;
  113. }
  114. if (out_time_of_oobe != nullptr)
  115. *out_time_of_oobe = base::Time::FromTimeT(statbuf.st_mtime);
  116. return true;
  117. }
  118. static string ReadValueFromCrosSystem(const string& key) {
  119. char value_buffer[VB_MAX_STRING_PROPERTY];
  120. const char* rv = VbGetSystemPropertyString(
  121. key.c_str(), value_buffer, sizeof(value_buffer));
  122. if (rv != nullptr) {
  123. string return_value(value_buffer);
  124. base::TrimWhitespaceASCII(return_value, base::TRIM_ALL, &return_value);
  125. return return_value;
  126. }
  127. LOG(ERROR) << "Unable to read crossystem key " << key;
  128. return "";
  129. }
  130. string HardwareChromeOS::GetHardwareClass() const {
  131. if (USE_HWID_OVERRIDE) {
  132. return HwidOverride::Read(base::FilePath("/"));
  133. }
  134. return ReadValueFromCrosSystem("hwid");
  135. }
  136. string HardwareChromeOS::GetFirmwareVersion() const {
  137. return ReadValueFromCrosSystem("fwid");
  138. }
  139. string HardwareChromeOS::GetECVersion() const {
  140. string input_line;
  141. int exit_code = 0;
  142. vector<string> cmd = {"/usr/sbin/mosys", "-k", "ec", "info"};
  143. bool success = Subprocess::SynchronousExec(cmd, &exit_code, &input_line);
  144. if (!success || exit_code) {
  145. LOG(ERROR) << "Unable to read ec info from mosys (" << exit_code << ")";
  146. return "";
  147. }
  148. return utils::ParseECVersion(input_line);
  149. }
  150. int HardwareChromeOS::GetMinKernelKeyVersion() const {
  151. return VbGetSystemPropertyInt("tpm_kernver");
  152. }
  153. int HardwareChromeOS::GetMaxFirmwareKeyRollforward() const {
  154. return VbGetSystemPropertyInt("firmware_max_rollforward");
  155. }
  156. bool HardwareChromeOS::SetMaxFirmwareKeyRollforward(
  157. int firmware_max_rollforward) {
  158. // Not all devices have this field yet. So first try to read
  159. // it and if there is an error just fail.
  160. if (GetMaxFirmwareKeyRollforward() == -1)
  161. return false;
  162. return VbSetSystemPropertyInt("firmware_max_rollforward",
  163. firmware_max_rollforward) == 0;
  164. }
  165. int HardwareChromeOS::GetMinFirmwareKeyVersion() const {
  166. return VbGetSystemPropertyInt("tpm_fwver");
  167. }
  168. bool HardwareChromeOS::SetMaxKernelKeyRollforward(int kernel_max_rollforward) {
  169. return VbSetSystemPropertyInt("kernel_max_rollforward",
  170. kernel_max_rollforward) == 0;
  171. }
  172. int HardwareChromeOS::GetPowerwashCount() const {
  173. int powerwash_count;
  174. base::FilePath marker_path =
  175. base::FilePath(kPowerwashSafeDirectory).Append(kPowerwashCountMarker);
  176. string contents;
  177. if (!utils::ReadFile(marker_path.value(), &contents))
  178. return -1;
  179. base::TrimWhitespaceASCII(contents, base::TRIM_TRAILING, &contents);
  180. if (!base::StringToInt(contents, &powerwash_count))
  181. return -1;
  182. return powerwash_count;
  183. }
  184. bool HardwareChromeOS::SchedulePowerwash(bool is_rollback) {
  185. const char* powerwash_command =
  186. is_rollback ? kRollbackPowerwashCommand : kPowerwashCommand;
  187. bool result = utils::WriteFile(
  188. kPowerwashMarkerFile, powerwash_command, strlen(powerwash_command));
  189. if (result) {
  190. LOG(INFO) << "Created " << kPowerwashMarkerFile
  191. << " to powerwash on next reboot (is_rollback=" << is_rollback
  192. << ")";
  193. } else {
  194. PLOG(ERROR) << "Error in creating powerwash marker file: "
  195. << kPowerwashMarkerFile;
  196. }
  197. return result;
  198. }
  199. bool HardwareChromeOS::CancelPowerwash() {
  200. bool result = base::DeleteFile(base::FilePath(kPowerwashMarkerFile), false);
  201. if (result) {
  202. LOG(INFO) << "Successfully deleted the powerwash marker file : "
  203. << kPowerwashMarkerFile;
  204. } else {
  205. PLOG(ERROR) << "Could not delete the powerwash marker file : "
  206. << kPowerwashMarkerFile;
  207. }
  208. return result;
  209. }
  210. bool HardwareChromeOS::GetNonVolatileDirectory(base::FilePath* path) const {
  211. *path = base::FilePath(constants::kNonVolatileDirectory);
  212. return true;
  213. }
  214. bool HardwareChromeOS::GetPowerwashSafeDirectory(base::FilePath* path) const {
  215. *path = base::FilePath(kPowerwashSafeDirectory);
  216. return true;
  217. }
  218. int64_t HardwareChromeOS::GetBuildTimestamp() const {
  219. // TODO(senj): implement this in Chrome OS.
  220. return 0;
  221. }
  222. void HardwareChromeOS::LoadConfig(const string& root_prefix, bool normal_mode) {
  223. brillo::KeyValueStore store;
  224. if (normal_mode) {
  225. store.Load(base::FilePath(root_prefix + kConfigFilePath));
  226. } else {
  227. if (store.Load(base::FilePath(root_prefix + kStatefulPartition +
  228. kConfigFilePath))) {
  229. LOG(INFO) << "UpdateManager Config loaded from stateful partition.";
  230. } else {
  231. store.Load(base::FilePath(root_prefix + kConfigFilePath));
  232. }
  233. }
  234. if (!store.GetBoolean(kConfigOptsIsOOBEEnabled, &is_oobe_enabled_))
  235. is_oobe_enabled_ = true; // Default value.
  236. }
  237. bool HardwareChromeOS::GetFirstActiveOmahaPingSent() const {
  238. int exit_code = 0;
  239. string active_ping_str;
  240. vector<string> cmd = {"vpd_get_value", kActivePingKey};
  241. if (!Subprocess::SynchronousExec(cmd, &exit_code, &active_ping_str) ||
  242. exit_code) {
  243. LOG(ERROR) << "Failed to get vpd key for " << kActivePingKey
  244. << " with exit code: " << exit_code;
  245. return false;
  246. }
  247. base::TrimWhitespaceASCII(active_ping_str, base::TRIM_ALL, &active_ping_str);
  248. int active_ping;
  249. if (active_ping_str.empty() ||
  250. !base::StringToInt(active_ping_str, &active_ping)) {
  251. LOG(INFO) << "Failed to parse active_ping value: " << active_ping_str;
  252. return false;
  253. }
  254. return static_cast<bool>(active_ping);
  255. }
  256. bool HardwareChromeOS::SetFirstActiveOmahaPingSent() {
  257. int exit_code = 0;
  258. string output;
  259. vector<string> vpd_set_cmd = {
  260. "vpd", "-i", "RW_VPD", "-s", string(kActivePingKey) + "=1"};
  261. if (!Subprocess::SynchronousExec(vpd_set_cmd, &exit_code, &output) ||
  262. exit_code) {
  263. LOG(ERROR) << "Failed to set vpd key for " << kActivePingKey
  264. << " with exit code: " << exit_code << " with error: " << output;
  265. return false;
  266. }
  267. vector<string> vpd_dump_cmd = {"dump_vpd_log", "--force"};
  268. if (!Subprocess::SynchronousExec(vpd_dump_cmd, &exit_code, &output) ||
  269. exit_code) {
  270. LOG(ERROR) << "Failed to cache " << kActivePingKey << " using dump_vpd_log"
  271. << " with exit code: " << exit_code << " with error: " << output;
  272. return false;
  273. }
  274. return true;
  275. }
  276. } // namespace chromeos_update_engine