blob: 9f6e82b7ff5140dc05de2a03ba85e0774e68bac1 [file] [log] [blame]
Darin Petkova4a8a8c2010-07-15 22:21:12 -07001// 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 Petkovfbb40092010-07-29 17:05:50 -070014#include "base/file_util.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070015#include "base/string_util.h"
16#include "update_engine/simple_key_value_store.h"
17#include "update_engine/utils.h"
18
19using std::map;
20using std::string;
21
Darin Petkova4a8a8c2010-07-15 22:21:12 -070022namespace chromeos_update_engine {
23
Darin Petkov5a7f5652010-07-22 21:40:09 -070024const char* const OmahaRequestParams::kAppId(
25 "{87efface-864d-49a5-9bb3-4b050a7c227a}");
26const char* const OmahaRequestParams::kOsPlatform("Chrome OS");
27const char* const OmahaRequestParams::kOsVersion("Indy");
28const char* const OmahaRequestParams::kUpdateUrl(
29 "https://tools.google.com/service/update2");
30
Darin Petkovfbb40092010-07-29 17:05:50 -070031static const char kHWIDPath[] = "/sys/devices/platform/chromeos_acpi/HWID";
32
Darin Petkov5a7f5652010-07-22 21:40:09 -070033bool OmahaRequestDeviceParams::Init(const std::string& in_app_version,
34 const std::string& in_update_url) {
Darin Petkova4a8a8c2010-07-15 22:21:12 -070035 os_platform = OmahaRequestParams::kOsPlatform;
36 os_version = OmahaRequestParams::kOsVersion;
Darin Petkov5a7f5652010-07-22 21:40:09 -070037 app_version = in_app_version.empty() ?
38 GetLsbValue("CHROMEOS_RELEASE_VERSION", "") : in_app_version;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070039 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 Petkovfbb40092010-07-29 17:05:50 -070044 hardware_class = GetHardwareClass();
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070045 struct stat stbuf;
Darin Petkov5a7f5652010-07-22 21:40:09 -070046
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070047 // 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 Petkov5a7f5652010-07-22 21:40:09 -070053 update_url = in_update_url.empty() ?
54 GetLsbValue("CHROMEOS_AUSERVER", OmahaRequestParams::kUpdateUrl) :
55 in_update_url;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070056 return true;
57}
58
Darin Petkova4a8a8c2010-07-15 22:21:12 -070059string 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
78string 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 Petkovfbb40092010-07-29 17:05:50 -070086string 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 Petkova4a8a8c2010-07-15 22:21:12 -070096} // namespace chromeos_update_engine