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 | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 14 | #include "base/file_util.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 15 | #include "base/string_util.h" |
| 16 | #include "update_engine/simple_key_value_store.h" |
| 17 | #include "update_engine/utils.h" |
| 18 | |
| 19 | using std::map; |
| 20 | using std::string; |
| 21 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 22 | namespace chromeos_update_engine { |
| 23 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 24 | const char* const OmahaRequestParams::kAppId( |
| 25 | "{87efface-864d-49a5-9bb3-4b050a7c227a}"); |
| 26 | const char* const OmahaRequestParams::kOsPlatform("Chrome OS"); |
| 27 | const char* const OmahaRequestParams::kOsVersion("Indy"); |
| 28 | const char* const OmahaRequestParams::kUpdateUrl( |
| 29 | "https://tools.google.com/service/update2"); |
| 30 | |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 31 | static const char kHWIDPath[] = "/sys/devices/platform/chromeos_acpi/HWID"; |
| 32 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 33 | bool OmahaRequestDeviceParams::Init(const std::string& in_app_version, |
| 34 | const std::string& in_update_url) { |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 35 | os_platform = OmahaRequestParams::kOsPlatform; |
| 36 | os_version = OmahaRequestParams::kOsVersion; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 37 | app_version = in_app_version.empty() ? |
| 38 | GetLsbValue("CHROMEOS_RELEASE_VERSION", "") : in_app_version; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 39 | os_sp = app_version + "_" + GetMachineType(); |
| 40 | os_board = GetLsbValue("CHROMEOS_RELEASE_BOARD", ""); |
| 41 | app_id = OmahaRequestParams::kAppId; |
| 42 | app_lang = "en-US"; |
| 43 | app_track = GetLsbValue("CHROMEOS_RELEASE_TRACK", ""); |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 44 | hardware_class = GetHardwareClass(); |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 45 | struct stat stbuf; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 46 | |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 47 | // Deltas are only okay if the /.nodelta file does not exist. |
| 48 | // If we don't know (i.e. stat() returns some unexpected error), |
| 49 | // then err on the side of caution and say deltas are not okay |
| 50 | delta_okay = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) && |
| 51 | (errno == ENOENT); |
| 52 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 53 | update_url = in_update_url.empty() ? |
| 54 | GetLsbValue("CHROMEOS_AUSERVER", OmahaRequestParams::kUpdateUrl) : |
| 55 | in_update_url; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 56 | return true; |
| 57 | } |
| 58 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 59 | string OmahaRequestDeviceParams::GetLsbValue( |
| 60 | const string& key, const string& default_value) const { |
| 61 | string files[] = {string(utils::kStatefulPartition) + "/etc/lsb-release", |
| 62 | "/etc/lsb-release"}; |
| 63 | for (unsigned int i = 0; i < arraysize(files); ++i) { |
| 64 | // TODO(adlr): make sure files checked are owned as root (and all |
| 65 | // their parents are recursively, too). |
| 66 | string file_data; |
| 67 | if (!utils::ReadFileToString(root_ + files[i], &file_data)) |
| 68 | continue; |
| 69 | |
| 70 | map<string, string> data = simple_key_value_store::ParseString(file_data); |
| 71 | if (utils::MapContainsKey(data, key)) |
| 72 | return data[key]; |
| 73 | } |
| 74 | // not found |
| 75 | return default_value; |
| 76 | } |
| 77 | |
| 78 | string OmahaRequestDeviceParams::GetMachineType() const { |
| 79 | struct utsname buf; |
| 80 | string ret; |
| 81 | if (uname(&buf) == 0) |
| 82 | ret = buf.machine; |
| 83 | return ret; |
| 84 | } |
| 85 | |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 86 | string OmahaRequestDeviceParams::GetHardwareClass() const { |
| 87 | string hwid; |
| 88 | if (!file_util::ReadFileToString(FilePath(root_ + kHWIDPath), &hwid)) { |
| 89 | LOG(ERROR) << "Unable to determine the system hardware qualification ID."; |
| 90 | return ""; |
| 91 | } |
| 92 | TrimWhitespaceASCII(hwid, TRIM_ALL, &hwid); |
| 93 | return hwid; |
| 94 | } |
| 95 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 96 | } // namespace chromeos_update_engine |