-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbio_fw_updater.cc
218 lines (189 loc) · 7.74 KB
/
bio_fw_updater.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdint.h>
#include <stdlib.h>
#include <string>
#include <base/command_line.h>
#include <base/logging.h>
#include <base/time/time.h>
#include <base/timer/elapsed_timer.h>
#include <brillo/daemons/daemon.h>
#include <brillo/flag_helper.h>
#include <brillo/syslog_logging.h>
#include <base/strings/stringprintf.h>
#include <cros_config/cros_config.h>
#include "biod/biod_config.h"
#include "biod/biod_metrics.h"
#include "biod/biod_version.h"
#include "biod/cros_fp_device.h"
#include "biod/updater/cros_fp_updater.h"
#include "biod/updater/update_reason.h"
#include "biod/updater/update_status.h"
#include "biod/updater/update_utils.h"
using UpdateStatus = biod::updater::UpdateStatus;
using UpdateReason = biod::updater::UpdateReason;
using FwUpdaterStatus = biod::BiodMetrics::FwUpdaterStatus;
using FindFirmwareFileStatus = biod::updater::FindFirmwareFileStatus;
namespace {
constexpr char kHelpText[] =
"bio_fw_updater ensures the fingerprint mcu has the latest firmware\n";
void LogFWFileVersion(const biod::CrosFpFirmware& fw) {
biod::CrosFpFirmware::ImageVersion ver = fw.GetVersion();
LOG(INFO) << "FWFile RO Version: '" << ver.ro_version << "'";
LOG(INFO) << "FWFile RW Version: '" << ver.rw_version << "'";
}
void LogFPMCUVersion(const biod::CrosFpDevice::EcVersion& ver) {
LOG(INFO) << "FPMCU RO Version: '" << ver.ro_version << "'";
LOG(INFO) << "FPMCU RW Version: '" << ver.rw_version << "'";
LOG(INFO) << "FPMCU Active Image: "
<< biod::CrosFpDeviceUpdate::EcCurrentImageToString(
ver.current_image);
}
void LogSetupDirectory(const base::FilePath& log_dir_path) {
const auto log_file_path = log_dir_path.Append(base::StringPrintf(
"bio_fw_updater.%s",
brillo::GetTimeAsLogString(base::Time::Now()).c_str()));
brillo::UpdateLogSymlinks(log_dir_path.Append("bio_fw_updater.LATEST"),
log_dir_path.Append("bio_fw_updater.PREVIOUS"),
log_file_path);
logging::LoggingSettings logging_settings;
logging_settings.logging_dest = logging::LOG_TO_FILE;
logging_settings.log_file_path = log_file_path.value().c_str();
logging_settings.lock_log = logging::DONT_LOCK_LOG_FILE;
logging::InitLogging(logging_settings);
logging::SetLogItems(true, // process ID
true, // thread ID
true, // timestamp
false); // tickcount
}
class UpdaterMetrics {
public:
void SetUpdateReason(UpdateReason reason) { update_reason_ = reason; }
void Finished(FwUpdaterStatus status) {
uint64_t overall = runtime_.Elapsed().InMilliseconds();
DLOG(INFO) << "Runtime took " << overall << "ms.";
metrics_.SendFwUpdaterStatus(status, update_reason_,
static_cast<int>(overall));
}
private:
biod::BiodMetrics metrics_;
UpdateReason update_reason_ = UpdateReason::kNone;
base::ElapsedTimer runtime_;
};
} // namespace
int main(int argc, char* argv[]) {
UpdaterMetrics metrics;
DEFINE_string(log_dir, "/var/log/biod", "Directory where logs are written.");
brillo::FlagHelper::Init(argc, argv, kHelpText);
const auto log_dir_path = base::FilePath(FLAGS_log_dir);
if (base::DirectoryExists(log_dir_path)) {
LogSetupDirectory(log_dir_path);
} else {
LOG(ERROR) << "Log directory '" << log_dir_path.value()
<< "' does not exist, using syslog and stderr logging.";
brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderrIfTty |
brillo::kLogHeader);
}
biod::LogVersion();
// Check for firmware disable mechanism
if (biod::updater::UpdateDisallowed()) {
LOG(INFO) << "FPMCU update disabled, exiting.";
return EXIT_SUCCESS;
}
// Check if model supports fingerprint
brillo::CrosConfig cros_config;
if (!cros_config.Init()) {
LOG(WARNING) << "Cros config is not supported on this model, continuing "
"with legacy update.";
}
if (biod::FingerprintUnsupported(&cros_config)) {
LOG(INFO) << "Fingerprint is not supported on this model, exiting.";
return EXIT_SUCCESS;
}
// Find a firmware file that matches the firmware file pattern
base::FilePath file;
auto status = biod::updater::FindFirmwareFile(
base::FilePath(biod::updater::kFirmwareDir), &cros_config, &file);
if (status == FindFirmwareFileStatus::kNoDirectory ||
status == FindFirmwareFileStatus::kFileNotFound) {
LOG(INFO) << "No firmware "
<< ((status == FindFirmwareFileStatus::kNoDirectory) ? "directory"
: "file")
<< " on rootfs, exiting.";
return EXIT_SUCCESS;
}
if (status == FindFirmwareFileStatus::kMultipleFiles) {
LOG(ERROR) << "Found more than one firmware file, aborting.";
metrics.Finished(FwUpdaterStatus::kFailureFirmwareFileMultiple);
return EXIT_FAILURE;
}
biod::CrosFpFirmware fw(file);
if (!fw.IsValid()) {
LOG(ERROR) << "Failed to load firmware file '" << fw.GetPath().value()
<< "': " << fw.GetStatusString();
LOG(ERROR) << "We are aborting update.";
}
switch (fw.GetStatus()) {
case biod::CrosFpFirmware::Status::kUninitialized:
NOTREACHED();
return EXIT_FAILURE;
case biod::CrosFpFirmware::Status::kOk:
break;
case biod::CrosFpFirmware::Status::kNotFound:
metrics.Finished(FwUpdaterStatus::kFailureFirmwareFileNotFound);
return EXIT_FAILURE;
case biod::CrosFpFirmware::Status::kOpenError:
metrics.Finished(FwUpdaterStatus::kFailureFirmwareFileOpen);
return EXIT_FAILURE;
case biod::CrosFpFirmware::Status::kBadFmap:
metrics.Finished(FwUpdaterStatus::kFailureFirmwareFileFmap);
return EXIT_FAILURE;
}
LogFWFileVersion(fw);
biod::CrosFpDeviceUpdate ec_device;
biod::CrosFpBootUpdateCtrl boot_ctrl;
biod::CrosFpDevice::EcVersion ecver;
if (!ec_device.GetVersion(&ecver)) {
LOG(INFO) << "Failed to fetch EC version, aborting.";
metrics.Finished(FwUpdaterStatus::kFailurePreUpdateVersionCheck);
return EXIT_FAILURE;
}
LogFPMCUVersion(ecver);
auto result = biod::updater::DoUpdate(ec_device, boot_ctrl, fw);
metrics.SetUpdateReason(result.reason);
switch (result.status) {
case UpdateStatus::kUpdateFailedGetVersion:
LOG(INFO) << "Failed to fetch EC version, aborting.";
metrics.Finished(FwUpdaterStatus::kFailureUpdateVersionCheck);
return EXIT_FAILURE;
case UpdateStatus::kUpdateFailedFlashProtect:
LOG(ERROR) << "Failed to fetch flash protect status, aborting.";
metrics.Finished(FwUpdaterStatus::kFailureUpdateFlashProtect);
return EXIT_FAILURE;
case UpdateStatus::kUpdateFailedRO:
LOG(ERROR) << "Failed to update RO image, aborting.";
metrics.Finished(FwUpdaterStatus::kFailureUpdateRO);
return EXIT_FAILURE;
case UpdateStatus::kUpdateFailedRW:
LOG(ERROR) << "Failed to update RW image, aborting.";
metrics.Finished(FwUpdaterStatus::kFailureUpdateRW);
return EXIT_FAILURE;
case UpdateStatus::kUpdateSucceeded:
if (!ec_device.GetVersion(&ecver)) {
LOG(ERROR) << "Failed to fetch final EC version, update failed.";
metrics.Finished(FwUpdaterStatus::kFailurePostUpdateVersionCheck);
return EXIT_FAILURE;
}
LogFPMCUVersion(ecver);
LOG(INFO) << "The update was successful.";
metrics.Finished(FwUpdaterStatus::kSuccessful);
return EXIT_SUCCESS;
case UpdateStatus::kUpdateNotNecessary:
LOG(INFO) << "Update was not necessary.";
metrics.Finished(FwUpdaterStatus::kUnnecessary);
return EXIT_SUCCESS;
}
NOTREACHED();
return EXIT_SUCCESS;
}