Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/omaha_request_params.h" |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <sys/utsname.h> |
| 10 | |
| 11 | #include <map> |
| 12 | #include <string> |
| 13 | |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 14 | #include <base/file_util.h> |
| 15 | #include <base/string_util.h> |
| 16 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 17 | #include "update_engine/simple_key_value_store.h" |
| 18 | #include "update_engine/utils.h" |
| 19 | |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 20 | #define CALL_MEMBER_FN(object, member) ((object).*(member)) |
| 21 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 22 | using std::map; |
| 23 | using std::string; |
| 24 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 25 | namespace chromeos_update_engine { |
| 26 | |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 27 | const char OmahaRequestParams::kUpdateTrackKey[] = "CHROMEOS_RELEASE_TRACK"; |
| 28 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 29 | const char* const OmahaRequestParams::kAppId( |
| 30 | "{87efface-864d-49a5-9bb3-4b050a7c227a}"); |
| 31 | const char* const OmahaRequestParams::kOsPlatform("Chrome OS"); |
| 32 | const char* const OmahaRequestParams::kOsVersion("Indy"); |
| 33 | const char* const OmahaRequestParams::kUpdateUrl( |
| 34 | "https://tools.google.com/service/update2"); |
| 35 | |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 36 | static const char kHWIDPath[] = "/sys/devices/platform/chromeos_acpi/HWID"; |
| 37 | |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 38 | OmahaRequestDeviceParams::OmahaRequestDeviceParams() : |
| 39 | force_build_type_(false), |
| 40 | forced_official_build_(false) {} |
| 41 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 42 | bool OmahaRequestDeviceParams::Init(const std::string& in_app_version, |
| 43 | const std::string& in_update_url) { |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 44 | os_platform = OmahaRequestParams::kOsPlatform; |
| 45 | os_version = OmahaRequestParams::kOsVersion; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 46 | app_version = in_app_version.empty() ? |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 47 | GetLsbValue("CHROMEOS_RELEASE_VERSION", "", NULL) : in_app_version; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 48 | os_sp = app_version + "_" + GetMachineType(); |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 49 | os_board = GetLsbValue("CHROMEOS_RELEASE_BOARD", "", NULL); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 50 | app_id = OmahaRequestParams::kAppId; |
| 51 | app_lang = "en-US"; |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 52 | app_track = GetLsbValue( |
| 53 | kUpdateTrackKey, |
| 54 | "", |
| 55 | &chromeos_update_engine::OmahaRequestDeviceParams::IsValidTrack); |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 56 | hardware_class = GetHardwareClass(); |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 57 | struct stat stbuf; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 58 | |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 59 | // Deltas are only okay if the /.nodelta file does not exist. |
| 60 | // If we don't know (i.e. stat() returns some unexpected error), |
| 61 | // then err on the side of caution and say deltas are not okay |
| 62 | delta_okay = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) && |
| 63 | (errno == ENOENT); |
| 64 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 65 | update_url = in_update_url.empty() ? |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 66 | GetLsbValue("CHROMEOS_AUSERVER", OmahaRequestParams::kUpdateUrl, NULL) : |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 67 | in_update_url; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 68 | return true; |
| 69 | } |
| 70 | |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 71 | bool OmahaRequestDeviceParams::SetTrack(const std::string& track) { |
| 72 | TEST_AND_RETURN_FALSE(IsValidTrack(track)); |
| 73 | FilePath kFile(root_ + utils::kStatefulPartition + "/etc/lsb-release"); |
| 74 | string file_data; |
| 75 | map<string, string> data; |
| 76 | if (file_util::ReadFileToString(kFile, &file_data)) { |
| 77 | data = simple_key_value_store::ParseString(file_data); |
| 78 | } |
| 79 | data[kUpdateTrackKey] = track; |
| 80 | file_data = simple_key_value_store::AssembleString(data); |
| 81 | TEST_AND_RETURN_FALSE(file_util::CreateDirectory(kFile.DirName())); |
| 82 | TEST_AND_RETURN_FALSE( |
| 83 | file_util::WriteFile(kFile, file_data.data(), file_data.size()) == |
| 84 | static_cast<int>(file_data.size())); |
| 85 | app_track = track; |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | bool OmahaRequestDeviceParams::SetDeviceTrack(const std::string& track) { |
| 90 | OmahaRequestDeviceParams params; |
| 91 | TEST_AND_RETURN_FALSE(params.Init("", "")); |
| 92 | return params.SetTrack(track); |
| 93 | } |
| 94 | |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame^] | 95 | string OmahaRequestDeviceParams::GetDeviceTrack() { |
| 96 | OmahaRequestDeviceParams params; |
| 97 | // Note that params.app_track is an empty string if the value in |
| 98 | // lsb-release file is invalid. See Init() for details. |
| 99 | return params.Init("", "") ? params.app_track : ""; |
| 100 | } |
| 101 | |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 102 | string OmahaRequestDeviceParams::GetLsbValue(const string& key, |
| 103 | const string& default_value, |
| 104 | ValueValidator validator) const { |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 105 | string files[] = {string(utils::kStatefulPartition) + "/etc/lsb-release", |
| 106 | "/etc/lsb-release"}; |
| 107 | for (unsigned int i = 0; i < arraysize(files); ++i) { |
| 108 | // TODO(adlr): make sure files checked are owned as root (and all |
| 109 | // their parents are recursively, too). |
| 110 | string file_data; |
| 111 | if (!utils::ReadFileToString(root_ + files[i], &file_data)) |
| 112 | continue; |
| 113 | |
| 114 | map<string, string> data = simple_key_value_store::ParseString(file_data); |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 115 | if (utils::MapContainsKey(data, key)) { |
| 116 | const string& value = data[key]; |
| 117 | if (validator && !CALL_MEMBER_FN(*this, validator)(value)) { |
| 118 | continue; |
| 119 | } |
| 120 | return value; |
| 121 | } |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 122 | } |
| 123 | // not found |
| 124 | return default_value; |
| 125 | } |
| 126 | |
| 127 | string OmahaRequestDeviceParams::GetMachineType() const { |
| 128 | struct utsname buf; |
| 129 | string ret; |
| 130 | if (uname(&buf) == 0) |
| 131 | ret = buf.machine; |
| 132 | return ret; |
| 133 | } |
| 134 | |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 135 | string OmahaRequestDeviceParams::GetHardwareClass() const { |
| 136 | string hwid; |
| 137 | if (!file_util::ReadFileToString(FilePath(root_ + kHWIDPath), &hwid)) { |
| 138 | LOG(ERROR) << "Unable to determine the system hardware qualification ID."; |
| 139 | return ""; |
| 140 | } |
| 141 | TrimWhitespaceASCII(hwid, TRIM_ALL, &hwid); |
| 142 | return hwid; |
| 143 | } |
| 144 | |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 145 | bool OmahaRequestDeviceParams::IsOfficialBuild() const { |
| 146 | return force_build_type_ ? forced_official_build_ : utils::IsOfficialBuild(); |
| 147 | } |
| 148 | |
| 149 | bool OmahaRequestDeviceParams::IsValidTrack(const std::string& track) const { |
| 150 | return IsOfficialBuild() ? |
| 151 | (track == "beta-channel" || track == "dev-channel") : true; |
| 152 | } |
| 153 | |
| 154 | void OmahaRequestDeviceParams::SetBuildTypeOfficial(bool is_official) { |
| 155 | force_build_type_ = true; |
| 156 | forced_official_build_ = is_official; |
| 157 | } |
| 158 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 159 | } // namespace chromeos_update_engine |