blob: dcfda52d6723e30bed739a56727598a90270d979 [file] [log] [blame]
Tom Cherryed506f72017-05-25 15:58:59 -07001/*
2 * Copyright (C) 2017 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
17#include "firmware_handler.h"
18
19#include <fcntl.h>
Suchang Woo22fdd0a2021-04-06 11:22:49 +090020#include <fnmatch.h>
Jooyung Han21cad322020-09-18 14:41:22 +090021#include <glob.h>
Suchang Woo10c63742021-05-13 18:56:31 +090022#include <grp.h>
Tom Cherrydcb3d152019-08-07 16:02:28 -070023#include <pwd.h>
24#include <signal.h>
25#include <stdlib.h>
26#include <string.h>
Tom Cherryed506f72017-05-25 15:58:59 -070027#include <sys/sendfile.h>
Tom Cherryc5833052017-05-16 15:35:41 -070028#include <sys/wait.h>
Tom Cherryed506f72017-05-25 15:58:59 -070029#include <unistd.h>
30
Tom Cherryed506f72017-05-25 15:58:59 -070031#include <thread>
32
Tom Cherryede0d532017-07-06 14:20:11 -070033#include <android-base/chrono_utils.h>
Tom Cherryed506f72017-05-25 15:58:59 -070034#include <android-base/file.h>
35#include <android-base/logging.h>
Suchang Woo7d8c25b2021-12-20 13:23:28 +090036#include <android-base/properties.h>
Jooyung Han21cad322020-09-18 14:41:22 +090037#include <android-base/scopeguard.h>
Tom Cherrydcb3d152019-08-07 16:02:28 -070038#include <android-base/strings.h>
Tom Cherryed506f72017-05-25 15:58:59 -070039#include <android-base/unique_fd.h>
40
Grzegorz Jaszczykac474ff2024-10-13 14:55:38 +000041#include "exthandler/exthandler.h"
42
Tom Cherrydcb3d152019-08-07 16:02:28 -070043using android::base::ReadFdToString;
44using android::base::Socketpair;
45using android::base::Split;
Tom Cherryede0d532017-07-06 14:20:11 -070046using android::base::Timer;
Tom Cherrydcb3d152019-08-07 16:02:28 -070047using android::base::Trim;
Tom Cherry81f5d3e2017-06-22 12:53:17 -070048using android::base::unique_fd;
Suchang Woo7d8c25b2021-12-20 13:23:28 +090049using android::base::WaitForProperty;
Tom Cherry81f5d3e2017-06-22 12:53:17 -070050using android::base::WriteFully;
51
52namespace android {
53namespace init {
54
Suchang Woo22fdd0a2021-04-06 11:22:49 +090055namespace {
56bool PrefixMatch(const std::string& pattern, const std::string& path) {
57 return android::base::StartsWith(path, pattern);
58}
59
60bool FnMatch(const std::string& pattern, const std::string& path) {
61 return fnmatch(pattern.c_str(), path.c_str(), 0) == 0;
62}
63
64bool EqualMatch(const std::string& pattern, const std::string& path) {
65 return pattern == path;
66}
67} // namespace
68
Tom Cherrydcb3d152019-08-07 16:02:28 -070069static void LoadFirmware(const std::string& firmware, const std::string& root, int fw_fd,
70 size_t fw_size, int loading_fd, int data_fd) {
Tom Cherryed506f72017-05-25 15:58:59 -070071 // Start transfer.
Tom Cherry81f5d3e2017-06-22 12:53:17 -070072 WriteFully(loading_fd, "1", 1);
Tom Cherryed506f72017-05-25 15:58:59 -070073
74 // Copy the firmware.
75 int rc = sendfile(data_fd, fw_fd, nullptr, fw_size);
76 if (rc == -1) {
Tom Cherrydcb3d152019-08-07 16:02:28 -070077 PLOG(ERROR) << "firmware: sendfile failed { '" << root << "', '" << firmware << "' }";
Tom Cherryed506f72017-05-25 15:58:59 -070078 }
79
80 // Tell the firmware whether to abort or commit.
81 const char* response = (rc != -1) ? "0" : "-1";
Tom Cherry81f5d3e2017-06-22 12:53:17 -070082 WriteFully(loading_fd, response, strlen(response));
Tom Cherryed506f72017-05-25 15:58:59 -070083}
84
85static bool IsBooting() {
86 return access("/dev/.booting", F_OK) == 0;
87}
88
Suchang Woo7d8c25b2021-12-20 13:23:28 +090089static bool IsApexActivated() {
90 static bool apex_activated = []() {
91 // Wait for com.android.runtime.apex activation
92 // Property name and value must be kept in sync with system/apexd/apex/apex_constants.h
93 // 60s is the default firmware sysfs fallback timeout. (/sys/class/firmware/timeout)
94 if (!WaitForProperty("apexd.status", "activated", 60s)) {
95 LOG(ERROR) << "Apexd activation wait timeout";
96 return false;
97 }
98 return true;
99 }();
100
101 return apex_activated;
102}
103
104static bool NeedsRerunExternalHandler() {
105 static bool first = true;
106
107 // Rerun external handler only on the first try and when apex is activated
108 if (first) {
109 first = false;
110 return IsApexActivated();
111 }
112
113 return first;
114}
115
Suchang Woo10c63742021-05-13 18:56:31 +0900116ExternalFirmwareHandler::ExternalFirmwareHandler(std::string devpath, uid_t uid, gid_t gid,
Suchang Woo22fdd0a2021-04-06 11:22:49 +0900117 std::string handler_path)
Suchang Woo10c63742021-05-13 18:56:31 +0900118 : devpath(std::move(devpath)), uid(uid), gid(gid), handler_path(std::move(handler_path)) {
Suchang Woo22fdd0a2021-04-06 11:22:49 +0900119 auto wildcard_position = this->devpath.find('*');
120 if (wildcard_position != std::string::npos) {
121 if (wildcard_position == this->devpath.length() - 1) {
122 this->devpath.pop_back();
123 match = std::bind(PrefixMatch, this->devpath, std::placeholders::_1);
124 } else {
125 match = std::bind(FnMatch, this->devpath, std::placeholders::_1);
126 }
127 } else {
128 match = std::bind(EqualMatch, this->devpath, std::placeholders::_1);
129 }
130}
131
Suchang Woo10c63742021-05-13 18:56:31 +0900132ExternalFirmwareHandler::ExternalFirmwareHandler(std::string devpath, uid_t uid,
133 std::string handler_path)
134 : ExternalFirmwareHandler(devpath, uid, 0, handler_path) {}
135
Tom Cherrydcb3d152019-08-07 16:02:28 -0700136FirmwareHandler::FirmwareHandler(std::vector<std::string> firmware_directories,
137 std::vector<ExternalFirmwareHandler> external_firmware_handlers)
138 : firmware_directories_(std::move(firmware_directories)),
139 external_firmware_handlers_(std::move(external_firmware_handlers)) {}
Tom Cherry457e28f2018-08-01 13:12:20 -0700140
Tom Cherrydcb3d152019-08-07 16:02:28 -0700141std::string FirmwareHandler::GetFirmwarePath(const Uevent& uevent) const {
142 for (const auto& external_handler : external_firmware_handlers_) {
Suchang Woo22fdd0a2021-04-06 11:22:49 +0900143 if (external_handler.match(uevent.path)) {
Tom Cherrydcb3d152019-08-07 16:02:28 -0700144 LOG(INFO) << "Launching external firmware handler '" << external_handler.handler_path
145 << "' for devpath: '" << uevent.path << "' firmware: '" << uevent.firmware
146 << "'";
147
Grzegorz Jaszczykac474ff2024-10-13 14:55:38 +0000148 std::unordered_map<std::string, std::string> envs_map;
149 envs_map["FIRMWARE"] = uevent.firmware;
150 envs_map["DEVPATH"] = uevent.path;
151
Suchang Woo10c63742021-05-13 18:56:31 +0900152 auto result = RunExternalHandler(external_handler.handler_path, external_handler.uid,
Grzegorz Jaszczykac474ff2024-10-13 14:55:38 +0000153 external_handler.gid, envs_map);
Suchang Woo7d8c25b2021-12-20 13:23:28 +0900154 if (!result.ok() && NeedsRerunExternalHandler()) {
155 auto res = RunExternalHandler(external_handler.handler_path, external_handler.uid,
Grzegorz Jaszczykac474ff2024-10-13 14:55:38 +0000156 external_handler.gid, envs_map);
Suchang Woo7d8c25b2021-12-20 13:23:28 +0900157 result = std::move(res);
158 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900159 if (!result.ok()) {
Tom Cherrydcb3d152019-08-07 16:02:28 -0700160 LOG(ERROR) << "Using default firmware; External firmware handler failed: "
161 << result.error();
162 return uevent.firmware;
163 }
164 if (result->find("..") != std::string::npos) {
165 LOG(ERROR) << "Using default firmware; External firmware handler provided an "
166 "invalid path, '"
167 << *result << "'";
168 return uevent.firmware;
169 }
170 LOG(INFO) << "Loading firmware '" << *result << "' in place of '" << uevent.firmware
171 << "'";
172 return *result;
173 }
174 }
Tom Cherryed506f72017-05-25 15:58:59 -0700175 LOG(INFO) << "firmware: loading '" << uevent.firmware << "' for '" << uevent.path << "'";
Tom Cherrydcb3d152019-08-07 16:02:28 -0700176 return uevent.firmware;
177}
Tom Cherryed506f72017-05-25 15:58:59 -0700178
Luca Stefani3e4159a2024-04-05 10:22:34 +0200179void FirmwareHandler::ProcessFirmwareEvent(const std::string& path,
Tom Cherrydcb3d152019-08-07 16:02:28 -0700180 const std::string& firmware) const {
Luca Stefani3e4159a2024-04-05 10:22:34 +0200181 std::string root = "/sys" + path;
Tom Cherryed506f72017-05-25 15:58:59 -0700182 std::string loading = root + "/loading";
183 std::string data = root + "/data";
184
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700185 unique_fd loading_fd(open(loading.c_str(), O_WRONLY | O_CLOEXEC));
Tom Cherryed506f72017-05-25 15:58:59 -0700186 if (loading_fd == -1) {
Tom Cherrydcb3d152019-08-07 16:02:28 -0700187 PLOG(ERROR) << "couldn't open firmware loading fd for " << firmware;
Tom Cherryed506f72017-05-25 15:58:59 -0700188 return;
189 }
190
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700191 unique_fd data_fd(open(data.c_str(), O_WRONLY | O_CLOEXEC));
Tom Cherryed506f72017-05-25 15:58:59 -0700192 if (data_fd == -1) {
Tom Cherrydcb3d152019-08-07 16:02:28 -0700193 PLOG(ERROR) << "couldn't open firmware data fd for " << firmware;
Tom Cherryed506f72017-05-25 15:58:59 -0700194 return;
195 }
196
Tom Cherryd38aafd2019-05-23 16:26:57 -0700197 std::vector<std::string> attempted_paths_and_errors;
Jooyung Han21cad322020-09-18 14:41:22 +0900198 auto TryLoadFirmware = [&](const std::string& firmware_directory) {
Tom Cherrydcb3d152019-08-07 16:02:28 -0700199 std::string file = firmware_directory + firmware;
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700200 unique_fd fw_fd(open(file.c_str(), O_RDONLY | O_CLOEXEC));
Tom Cherryd38aafd2019-05-23 16:26:57 -0700201 if (fw_fd == -1) {
202 attempted_paths_and_errors.emplace_back("firmware: attempted " + file +
203 ", open failed: " + strerror(errno));
Jooyung Han21cad322020-09-18 14:41:22 +0900204 return false;
Tom Cherryed506f72017-05-25 15:58:59 -0700205 }
Tom Cherryd38aafd2019-05-23 16:26:57 -0700206 struct stat sb;
Bart Van Asscheaee2ec82022-12-02 18:48:15 -0800207 if (fstat(fw_fd.get(), &sb) == -1) {
Tom Cherryd38aafd2019-05-23 16:26:57 -0700208 attempted_paths_and_errors.emplace_back("firmware: attempted " + file +
209 ", fstat failed: " + strerror(errno));
Jooyung Han21cad322020-09-18 14:41:22 +0900210 return false;
Tom Cherryd38aafd2019-05-23 16:26:57 -0700211 }
Luca Stefani3e4159a2024-04-05 10:22:34 +0200212 LOG(INFO) << "found " << file << " for " << path;
Bart Van Asscheaee2ec82022-12-02 18:48:15 -0800213 LoadFirmware(firmware, root, fw_fd.get(), sb.st_size, loading_fd.get(), data_fd.get());
Jooyung Han21cad322020-09-18 14:41:22 +0900214 return true;
215 };
216
217 int booting = IsBooting();
218try_loading_again:
219 attempted_paths_and_errors.clear();
220 if (ForEachFirmwareDirectory(TryLoadFirmware)) {
Tom Cherryd38aafd2019-05-23 16:26:57 -0700221 return;
Tom Cherryed506f72017-05-25 15:58:59 -0700222 }
223
224 if (booting) {
225 // If we're not fully booted, we may be missing
226 // filesystems needed for firmware, wait and retry.
227 std::this_thread::sleep_for(100ms);
228 booting = IsBooting();
229 goto try_loading_again;
230 }
231
Tom Cherrydcb3d152019-08-07 16:02:28 -0700232 LOG(ERROR) << "firmware: could not find firmware for " << firmware;
Tom Cherryd38aafd2019-05-23 16:26:57 -0700233 for (const auto& message : attempted_paths_and_errors) {
234 LOG(ERROR) << message;
235 }
Tom Cherryed506f72017-05-25 15:58:59 -0700236
237 // Write "-1" as our response to the kernel's firmware request, since we have nothing for it.
Bart Van Asscheaee2ec82022-12-02 18:48:15 -0800238 write(loading_fd.get(), "-1", 2);
Tom Cherryed506f72017-05-25 15:58:59 -0700239}
240
Jooyung Han21cad322020-09-18 14:41:22 +0900241bool FirmwareHandler::ForEachFirmwareDirectory(
242 std::function<bool(const std::string&)> handler) const {
243 for (const std::string& firmware_directory : firmware_directories_) {
244 if (std::invoke(handler, firmware_directory)) {
245 return true;
246 }
247 }
248
249 glob_t glob_result;
Jooyung Han2833e5d2020-09-21 10:56:10 +0900250 glob("/apex/*/etc/firmware/", GLOB_MARK, nullptr, &glob_result);
Jooyung Han21cad322020-09-18 14:41:22 +0900251 auto free_glob = android::base::make_scope_guard(std::bind(&globfree, &glob_result));
252 for (size_t i = 0; i < glob_result.gl_pathc; i++) {
253 char* apex_firmware_directory = glob_result.gl_pathv[i];
254 // Filter-out /apex/<name>@<ver> paths. The paths are bind-mounted to
255 // /apex/<name> paths, so unless we filter them out, we will look into the
256 // same apex twice.
257 if (strchr(apex_firmware_directory, '@')) {
258 continue;
259 }
260 if (std::invoke(handler, apex_firmware_directory)) {
261 return true;
262 }
263 }
264
265 return false;
266}
267
Tom Cherry457e28f2018-08-01 13:12:20 -0700268void FirmwareHandler::HandleUevent(const Uevent& uevent) {
Tom Cherryed506f72017-05-25 15:58:59 -0700269 if (uevent.subsystem != "firmware" || uevent.action != "add") return;
270
271 // Loading the firmware in a child means we can do that in parallel...
Tom Cherry0f296e02017-06-30 12:58:39 -0700272 auto pid = fork();
Tom Cherryc5833052017-05-16 15:35:41 -0700273 if (pid == -1) {
Tom Cherryed506f72017-05-25 15:58:59 -0700274 PLOG(ERROR) << "could not fork to process firmware event for " << uevent.firmware;
275 }
Tom Cherryc5833052017-05-16 15:35:41 -0700276 if (pid == 0) {
Tom Cherry0f296e02017-06-30 12:58:39 -0700277 Timer t;
Tom Cherrydcb3d152019-08-07 16:02:28 -0700278 auto firmware = GetFirmwarePath(uevent);
Luca Stefani3e4159a2024-04-05 10:22:34 +0200279 ProcessFirmwareEvent(uevent.path, firmware);
Tom Cherry0f296e02017-06-30 12:58:39 -0700280 LOG(INFO) << "loading " << uevent.path << " took " << t;
Tom Cherryc5833052017-05-16 15:35:41 -0700281 _exit(EXIT_SUCCESS);
282 }
Tom Cherryed506f72017-05-25 15:58:59 -0700283}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700284
285} // namespace init
286} // namespace android