blob: ba7e6bd63aaba0eb234c24cab5e50695f71400ae [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>
Jooyung Han21cad322020-09-18 14:41:22 +090020#include <glob.h>
Tom Cherrydcb3d152019-08-07 16:02:28 -070021#include <pwd.h>
22#include <signal.h>
23#include <stdlib.h>
24#include <string.h>
Tom Cherryed506f72017-05-25 15:58:59 -070025#include <sys/sendfile.h>
Tom Cherryc5833052017-05-16 15:35:41 -070026#include <sys/wait.h>
Tom Cherryed506f72017-05-25 15:58:59 -070027#include <unistd.h>
28
Tom Cherryed506f72017-05-25 15:58:59 -070029#include <thread>
30
Tom Cherryede0d532017-07-06 14:20:11 -070031#include <android-base/chrono_utils.h>
Tom Cherryed506f72017-05-25 15:58:59 -070032#include <android-base/file.h>
33#include <android-base/logging.h>
Jooyung Han21cad322020-09-18 14:41:22 +090034#include <android-base/scopeguard.h>
Tom Cherrydcb3d152019-08-07 16:02:28 -070035#include <android-base/strings.h>
Tom Cherryed506f72017-05-25 15:58:59 -070036#include <android-base/unique_fd.h>
37
Tom Cherrydcb3d152019-08-07 16:02:28 -070038using android::base::ReadFdToString;
39using android::base::Socketpair;
40using android::base::Split;
Tom Cherryede0d532017-07-06 14:20:11 -070041using android::base::Timer;
Tom Cherrydcb3d152019-08-07 16:02:28 -070042using android::base::Trim;
Tom Cherry81f5d3e2017-06-22 12:53:17 -070043using android::base::unique_fd;
44using android::base::WriteFully;
45
46namespace android {
47namespace init {
48
Tom Cherrydcb3d152019-08-07 16:02:28 -070049static void LoadFirmware(const std::string& firmware, const std::string& root, int fw_fd,
50 size_t fw_size, int loading_fd, int data_fd) {
Tom Cherryed506f72017-05-25 15:58:59 -070051 // Start transfer.
Tom Cherry81f5d3e2017-06-22 12:53:17 -070052 WriteFully(loading_fd, "1", 1);
Tom Cherryed506f72017-05-25 15:58:59 -070053
54 // Copy the firmware.
55 int rc = sendfile(data_fd, fw_fd, nullptr, fw_size);
56 if (rc == -1) {
Tom Cherrydcb3d152019-08-07 16:02:28 -070057 PLOG(ERROR) << "firmware: sendfile failed { '" << root << "', '" << firmware << "' }";
Tom Cherryed506f72017-05-25 15:58:59 -070058 }
59
60 // Tell the firmware whether to abort or commit.
61 const char* response = (rc != -1) ? "0" : "-1";
Tom Cherry81f5d3e2017-06-22 12:53:17 -070062 WriteFully(loading_fd, response, strlen(response));
Tom Cherryed506f72017-05-25 15:58:59 -070063}
64
65static bool IsBooting() {
66 return access("/dev/.booting", F_OK) == 0;
67}
68
Tom Cherrydcb3d152019-08-07 16:02:28 -070069FirmwareHandler::FirmwareHandler(std::vector<std::string> firmware_directories,
70 std::vector<ExternalFirmwareHandler> external_firmware_handlers)
71 : firmware_directories_(std::move(firmware_directories)),
72 external_firmware_handlers_(std::move(external_firmware_handlers)) {}
Tom Cherry457e28f2018-08-01 13:12:20 -070073
Tom Cherrydcb3d152019-08-07 16:02:28 -070074Result<std::string> FirmwareHandler::RunExternalHandler(const std::string& handler, uid_t uid,
75 const Uevent& uevent) const {
76 unique_fd child_stdout;
77 unique_fd parent_stdout;
78 if (!Socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, &child_stdout, &parent_stdout)) {
79 return ErrnoError() << "Socketpair() for stdout failed";
80 }
Tom Cherryed506f72017-05-25 15:58:59 -070081
Tom Cherrydcb3d152019-08-07 16:02:28 -070082 unique_fd child_stderr;
83 unique_fd parent_stderr;
84 if (!Socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, &child_stderr, &parent_stderr)) {
85 return ErrnoError() << "Socketpair() for stderr failed";
86 }
87
88 signal(SIGCHLD, SIG_DFL);
89
90 auto pid = fork();
91 if (pid < 0) {
92 return ErrnoError() << "fork() failed";
93 }
94
95 if (pid == 0) {
96 setenv("FIRMWARE", uevent.firmware.c_str(), 1);
97 setenv("DEVPATH", uevent.path.c_str(), 1);
98 parent_stdout.reset();
99 parent_stderr.reset();
100 close(STDOUT_FILENO);
101 close(STDERR_FILENO);
102 dup2(child_stdout.get(), STDOUT_FILENO);
103 dup2(child_stderr.get(), STDERR_FILENO);
104
105 auto args = Split(handler, " ");
106 std::vector<char*> c_args;
107 for (auto& arg : args) {
108 c_args.emplace_back(arg.data());
109 }
110 c_args.emplace_back(nullptr);
111
112 if (setuid(uid) != 0) {
113 fprintf(stderr, "setuid() failed: %s", strerror(errno));
114 _exit(EXIT_FAILURE);
115 }
116
117 execv(c_args[0], c_args.data());
118 fprintf(stderr, "exec() failed: %s", strerror(errno));
119 _exit(EXIT_FAILURE);
120 }
121
122 child_stdout.reset();
123 child_stderr.reset();
124
125 int status;
126 pid_t waited_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
127 if (waited_pid == -1) {
128 return ErrnoError() << "waitpid() failed";
129 }
130
131 std::string stdout_content;
132 if (!ReadFdToString(parent_stdout.get(), &stdout_content)) {
133 return ErrnoError() << "ReadFdToString() for stdout failed";
134 }
135
136 std::string stderr_content;
137 if (ReadFdToString(parent_stderr.get(), &stderr_content)) {
138 auto messages = Split(stderr_content, "\n");
139 for (const auto& message : messages) {
140 if (!message.empty()) {
141 LOG(ERROR) << "External Firmware Handler: " << message;
142 }
143 }
144 } else {
145 LOG(ERROR) << "ReadFdToString() for stderr failed";
146 }
147
148 if (WIFEXITED(status)) {
149 if (WEXITSTATUS(status) == EXIT_SUCCESS) {
150 return Trim(stdout_content);
151 } else {
152 return Error() << "exited with status " << WEXITSTATUS(status);
153 }
154 } else if (WIFSIGNALED(status)) {
155 return Error() << "killed by signal " << WTERMSIG(status);
156 }
157
158 return Error() << "unexpected exit status " << status;
159}
160
161std::string FirmwareHandler::GetFirmwarePath(const Uevent& uevent) const {
162 for (const auto& external_handler : external_firmware_handlers_) {
163 if (external_handler.devpath == uevent.path) {
164 LOG(INFO) << "Launching external firmware handler '" << external_handler.handler_path
165 << "' for devpath: '" << uevent.path << "' firmware: '" << uevent.firmware
166 << "'";
167
168 auto result =
169 RunExternalHandler(external_handler.handler_path, external_handler.uid, uevent);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900170 if (!result.ok()) {
Tom Cherrydcb3d152019-08-07 16:02:28 -0700171 LOG(ERROR) << "Using default firmware; External firmware handler failed: "
172 << result.error();
173 return uevent.firmware;
174 }
175 if (result->find("..") != std::string::npos) {
176 LOG(ERROR) << "Using default firmware; External firmware handler provided an "
177 "invalid path, '"
178 << *result << "'";
179 return uevent.firmware;
180 }
181 LOG(INFO) << "Loading firmware '" << *result << "' in place of '" << uevent.firmware
182 << "'";
183 return *result;
184 }
185 }
Tom Cherryed506f72017-05-25 15:58:59 -0700186 LOG(INFO) << "firmware: loading '" << uevent.firmware << "' for '" << uevent.path << "'";
Tom Cherrydcb3d152019-08-07 16:02:28 -0700187 return uevent.firmware;
188}
Tom Cherryed506f72017-05-25 15:58:59 -0700189
Tom Cherrydcb3d152019-08-07 16:02:28 -0700190void FirmwareHandler::ProcessFirmwareEvent(const std::string& root,
191 const std::string& firmware) const {
Tom Cherryed506f72017-05-25 15:58:59 -0700192 std::string loading = root + "/loading";
193 std::string data = root + "/data";
194
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700195 unique_fd loading_fd(open(loading.c_str(), O_WRONLY | O_CLOEXEC));
Tom Cherryed506f72017-05-25 15:58:59 -0700196 if (loading_fd == -1) {
Tom Cherrydcb3d152019-08-07 16:02:28 -0700197 PLOG(ERROR) << "couldn't open firmware loading fd for " << firmware;
Tom Cherryed506f72017-05-25 15:58:59 -0700198 return;
199 }
200
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700201 unique_fd data_fd(open(data.c_str(), O_WRONLY | O_CLOEXEC));
Tom Cherryed506f72017-05-25 15:58:59 -0700202 if (data_fd == -1) {
Tom Cherrydcb3d152019-08-07 16:02:28 -0700203 PLOG(ERROR) << "couldn't open firmware data fd for " << firmware;
Tom Cherryed506f72017-05-25 15:58:59 -0700204 return;
205 }
206
Tom Cherryd38aafd2019-05-23 16:26:57 -0700207 std::vector<std::string> attempted_paths_and_errors;
Jooyung Han21cad322020-09-18 14:41:22 +0900208 auto TryLoadFirmware = [&](const std::string& firmware_directory) {
Tom Cherrydcb3d152019-08-07 16:02:28 -0700209 std::string file = firmware_directory + firmware;
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700210 unique_fd fw_fd(open(file.c_str(), O_RDONLY | O_CLOEXEC));
Tom Cherryd38aafd2019-05-23 16:26:57 -0700211 if (fw_fd == -1) {
212 attempted_paths_and_errors.emplace_back("firmware: attempted " + file +
213 ", open failed: " + strerror(errno));
Jooyung Han21cad322020-09-18 14:41:22 +0900214 return false;
Tom Cherryed506f72017-05-25 15:58:59 -0700215 }
Tom Cherryd38aafd2019-05-23 16:26:57 -0700216 struct stat sb;
217 if (fstat(fw_fd, &sb) == -1) {
218 attempted_paths_and_errors.emplace_back("firmware: attempted " + file +
219 ", fstat failed: " + strerror(errno));
Jooyung Han21cad322020-09-18 14:41:22 +0900220 return false;
Tom Cherryd38aafd2019-05-23 16:26:57 -0700221 }
Tom Cherrydcb3d152019-08-07 16:02:28 -0700222 LoadFirmware(firmware, root, fw_fd, sb.st_size, loading_fd, data_fd);
Jooyung Han21cad322020-09-18 14:41:22 +0900223 return true;
224 };
225
226 int booting = IsBooting();
227try_loading_again:
228 attempted_paths_and_errors.clear();
229 if (ForEachFirmwareDirectory(TryLoadFirmware)) {
Tom Cherryd38aafd2019-05-23 16:26:57 -0700230 return;
Tom Cherryed506f72017-05-25 15:58:59 -0700231 }
232
233 if (booting) {
234 // If we're not fully booted, we may be missing
235 // filesystems needed for firmware, wait and retry.
236 std::this_thread::sleep_for(100ms);
237 booting = IsBooting();
238 goto try_loading_again;
239 }
240
Tom Cherrydcb3d152019-08-07 16:02:28 -0700241 LOG(ERROR) << "firmware: could not find firmware for " << firmware;
Tom Cherryd38aafd2019-05-23 16:26:57 -0700242 for (const auto& message : attempted_paths_and_errors) {
243 LOG(ERROR) << message;
244 }
Tom Cherryed506f72017-05-25 15:58:59 -0700245
246 // Write "-1" as our response to the kernel's firmware request, since we have nothing for it.
247 write(loading_fd, "-1", 2);
248}
249
Jooyung Han21cad322020-09-18 14:41:22 +0900250bool FirmwareHandler::ForEachFirmwareDirectory(
251 std::function<bool(const std::string&)> handler) const {
252 for (const std::string& firmware_directory : firmware_directories_) {
253 if (std::invoke(handler, firmware_directory)) {
254 return true;
255 }
256 }
257
258 glob_t glob_result;
Jooyung Han2833e5d2020-09-21 10:56:10 +0900259 glob("/apex/*/etc/firmware/", GLOB_MARK, nullptr, &glob_result);
Jooyung Han21cad322020-09-18 14:41:22 +0900260 auto free_glob = android::base::make_scope_guard(std::bind(&globfree, &glob_result));
261 for (size_t i = 0; i < glob_result.gl_pathc; i++) {
262 char* apex_firmware_directory = glob_result.gl_pathv[i];
263 // Filter-out /apex/<name>@<ver> paths. The paths are bind-mounted to
264 // /apex/<name> paths, so unless we filter them out, we will look into the
265 // same apex twice.
266 if (strchr(apex_firmware_directory, '@')) {
267 continue;
268 }
269 if (std::invoke(handler, apex_firmware_directory)) {
270 return true;
271 }
272 }
273
274 return false;
275}
276
Tom Cherry457e28f2018-08-01 13:12:20 -0700277void FirmwareHandler::HandleUevent(const Uevent& uevent) {
Tom Cherryed506f72017-05-25 15:58:59 -0700278 if (uevent.subsystem != "firmware" || uevent.action != "add") return;
279
280 // Loading the firmware in a child means we can do that in parallel...
Tom Cherry0f296e02017-06-30 12:58:39 -0700281 auto pid = fork();
Tom Cherryc5833052017-05-16 15:35:41 -0700282 if (pid == -1) {
Tom Cherryed506f72017-05-25 15:58:59 -0700283 PLOG(ERROR) << "could not fork to process firmware event for " << uevent.firmware;
284 }
Tom Cherryc5833052017-05-16 15:35:41 -0700285 if (pid == 0) {
Tom Cherry0f296e02017-06-30 12:58:39 -0700286 Timer t;
Tom Cherrydcb3d152019-08-07 16:02:28 -0700287 auto firmware = GetFirmwarePath(uevent);
288 ProcessFirmwareEvent("/sys" + uevent.path, firmware);
Tom Cherry0f296e02017-06-30 12:58:39 -0700289 LOG(INFO) << "loading " << uevent.path << " took " << t;
Tom Cherryc5833052017-05-16 15:35:41 -0700290 _exit(EXIT_SUCCESS);
291 }
Tom Cherryed506f72017-05-25 15:58:59 -0700292}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700293
294} // namespace init
295} // namespace android