blob: c067f6f2d0ff8263ed9170809ebfd0d0da2dd3e3 [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>
20#include <sys/sendfile.h>
Tom Cherryc5833052017-05-16 15:35:41 -070021#include <sys/wait.h>
Tom Cherryed506f72017-05-25 15:58:59 -070022#include <unistd.h>
23
Tom Cherryed506f72017-05-25 15:58:59 -070024#include <thread>
25
Tom Cherryede0d532017-07-06 14:20:11 -070026#include <android-base/chrono_utils.h>
Tom Cherryed506f72017-05-25 15:58:59 -070027#include <android-base/file.h>
28#include <android-base/logging.h>
29#include <android-base/unique_fd.h>
30
Tom Cherryede0d532017-07-06 14:20:11 -070031using android::base::Timer;
Tom Cherry81f5d3e2017-06-22 12:53:17 -070032using android::base::unique_fd;
33using android::base::WriteFully;
34
35namespace android {
36namespace init {
37
Tom Cherryed506f72017-05-25 15:58:59 -070038static void LoadFirmware(const Uevent& uevent, const std::string& root, int fw_fd, size_t fw_size,
39 int loading_fd, int data_fd) {
40 // Start transfer.
Tom Cherry81f5d3e2017-06-22 12:53:17 -070041 WriteFully(loading_fd, "1", 1);
Tom Cherryed506f72017-05-25 15:58:59 -070042
43 // Copy the firmware.
44 int rc = sendfile(data_fd, fw_fd, nullptr, fw_size);
45 if (rc == -1) {
46 PLOG(ERROR) << "firmware: sendfile failed { '" << root << "', '" << uevent.firmware
47 << "' }";
48 }
49
50 // Tell the firmware whether to abort or commit.
51 const char* response = (rc != -1) ? "0" : "-1";
Tom Cherry81f5d3e2017-06-22 12:53:17 -070052 WriteFully(loading_fd, response, strlen(response));
Tom Cherryed506f72017-05-25 15:58:59 -070053}
54
55static bool IsBooting() {
56 return access("/dev/.booting", F_OK) == 0;
57}
58
Tom Cherry457e28f2018-08-01 13:12:20 -070059FirmwareHandler::FirmwareHandler(std::vector<std::string> firmware_directories)
60 : firmware_directories_(std::move(firmware_directories)) {}
61
62void FirmwareHandler::ProcessFirmwareEvent(const Uevent& uevent) {
Tom Cherryed506f72017-05-25 15:58:59 -070063 int booting = IsBooting();
64
65 LOG(INFO) << "firmware: loading '" << uevent.firmware << "' for '" << uevent.path << "'";
66
67 std::string root = "/sys" + uevent.path;
68 std::string loading = root + "/loading";
69 std::string data = root + "/data";
70
Tom Cherry81f5d3e2017-06-22 12:53:17 -070071 unique_fd loading_fd(open(loading.c_str(), O_WRONLY | O_CLOEXEC));
Tom Cherryed506f72017-05-25 15:58:59 -070072 if (loading_fd == -1) {
73 PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent.firmware;
74 return;
75 }
76
Tom Cherry81f5d3e2017-06-22 12:53:17 -070077 unique_fd data_fd(open(data.c_str(), O_WRONLY | O_CLOEXEC));
Tom Cherryed506f72017-05-25 15:58:59 -070078 if (data_fd == -1) {
79 PLOG(ERROR) << "couldn't open firmware data fd for " << uevent.firmware;
80 return;
81 }
82
Tom Cherryd38aafd2019-05-23 16:26:57 -070083 std::vector<std::string> attempted_paths_and_errors;
84
Tom Cherryed506f72017-05-25 15:58:59 -070085try_loading_again:
Tom Cherryd38aafd2019-05-23 16:26:57 -070086 attempted_paths_and_errors.clear();
Tom Cherry457e28f2018-08-01 13:12:20 -070087 for (const auto& firmware_directory : firmware_directories_) {
Tom Cherry7421fa12018-07-13 15:32:02 -070088 std::string file = firmware_directory + uevent.firmware;
Tom Cherry81f5d3e2017-06-22 12:53:17 -070089 unique_fd fw_fd(open(file.c_str(), O_RDONLY | O_CLOEXEC));
Tom Cherryd38aafd2019-05-23 16:26:57 -070090 if (fw_fd == -1) {
91 attempted_paths_and_errors.emplace_back("firmware: attempted " + file +
92 ", open failed: " + strerror(errno));
93 continue;
Tom Cherryed506f72017-05-25 15:58:59 -070094 }
Tom Cherryd38aafd2019-05-23 16:26:57 -070095 struct stat sb;
96 if (fstat(fw_fd, &sb) == -1) {
97 attempted_paths_and_errors.emplace_back("firmware: attempted " + file +
98 ", fstat failed: " + strerror(errno));
99 continue;
100 }
101 LoadFirmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd);
102 return;
Tom Cherryed506f72017-05-25 15:58:59 -0700103 }
104
105 if (booting) {
106 // If we're not fully booted, we may be missing
107 // filesystems needed for firmware, wait and retry.
108 std::this_thread::sleep_for(100ms);
109 booting = IsBooting();
110 goto try_loading_again;
111 }
112
113 LOG(ERROR) << "firmware: could not find firmware for " << uevent.firmware;
Tom Cherryd38aafd2019-05-23 16:26:57 -0700114 for (const auto& message : attempted_paths_and_errors) {
115 LOG(ERROR) << message;
116 }
Tom Cherryed506f72017-05-25 15:58:59 -0700117
118 // Write "-1" as our response to the kernel's firmware request, since we have nothing for it.
119 write(loading_fd, "-1", 2);
120}
121
Tom Cherry457e28f2018-08-01 13:12:20 -0700122void FirmwareHandler::HandleUevent(const Uevent& uevent) {
Tom Cherryed506f72017-05-25 15:58:59 -0700123 if (uevent.subsystem != "firmware" || uevent.action != "add") return;
124
125 // Loading the firmware in a child means we can do that in parallel...
Tom Cherry0f296e02017-06-30 12:58:39 -0700126 auto pid = fork();
Tom Cherryc5833052017-05-16 15:35:41 -0700127 if (pid == -1) {
Tom Cherryed506f72017-05-25 15:58:59 -0700128 PLOG(ERROR) << "could not fork to process firmware event for " << uevent.firmware;
129 }
Tom Cherryc5833052017-05-16 15:35:41 -0700130 if (pid == 0) {
Tom Cherry0f296e02017-06-30 12:58:39 -0700131 Timer t;
132 ProcessFirmwareEvent(uevent);
133 LOG(INFO) << "loading " << uevent.path << " took " << t;
Tom Cherryc5833052017-05-16 15:35:41 -0700134 _exit(EXIT_SUCCESS);
135 }
Tom Cherryed506f72017-05-25 15:58:59 -0700136}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700137
138} // namespace init
139} // namespace android