1 /*
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>
21 #include <sys/wait.h>
22 #include <unistd.h>
23
24 #include <thread>
25
26 #include <android-base/chrono_utils.h>
27 #include <android-base/file.h>
28 #include <android-base/logging.h>
29 #include <android-base/unique_fd.h>
30
31 using android::base::Timer;
32 using android::base::unique_fd;
33 using android::base::WriteFully;
34
35 namespace android {
36 namespace init {
37
LoadFirmware(const Uevent & uevent,const std::string & root,int fw_fd,size_t fw_size,int loading_fd,int data_fd)38 static 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.
41 WriteFully(loading_fd, "1", 1);
42
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";
52 WriteFully(loading_fd, response, strlen(response));
53 }
54
IsBooting()55 static bool IsBooting() {
56 return access("/dev/.booting", F_OK) == 0;
57 }
58
FirmwareHandler(std::vector<std::string> firmware_directories)59 FirmwareHandler::FirmwareHandler(std::vector<std::string> firmware_directories)
60 : firmware_directories_(std::move(firmware_directories)) {}
61
ProcessFirmwareEvent(const Uevent & uevent)62 void FirmwareHandler::ProcessFirmwareEvent(const Uevent& uevent) {
63 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
71 unique_fd loading_fd(open(loading.c_str(), O_WRONLY | O_CLOEXEC));
72 if (loading_fd == -1) {
73 PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent.firmware;
74 return;
75 }
76
77 unique_fd data_fd(open(data.c_str(), O_WRONLY | O_CLOEXEC));
78 if (data_fd == -1) {
79 PLOG(ERROR) << "couldn't open firmware data fd for " << uevent.firmware;
80 return;
81 }
82
83 try_loading_again:
84 for (const auto& firmware_directory : firmware_directories_) {
85 std::string file = firmware_directory + uevent.firmware;
86 unique_fd fw_fd(open(file.c_str(), O_RDONLY | O_CLOEXEC));
87 struct stat sb;
88 if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) {
89 LoadFirmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd);
90 return;
91 }
92 }
93
94 if (booting) {
95 // If we're not fully booted, we may be missing
96 // filesystems needed for firmware, wait and retry.
97 std::this_thread::sleep_for(100ms);
98 booting = IsBooting();
99 goto try_loading_again;
100 }
101
102 LOG(ERROR) << "firmware: could not find firmware for " << uevent.firmware;
103
104 // Write "-1" as our response to the kernel's firmware request, since we have nothing for it.
105 write(loading_fd, "-1", 2);
106 }
107
HandleUevent(const Uevent & uevent)108 void FirmwareHandler::HandleUevent(const Uevent& uevent) {
109 if (uevent.subsystem != "firmware" || uevent.action != "add") return;
110
111 // Loading the firmware in a child means we can do that in parallel...
112 auto pid = fork();
113 if (pid == -1) {
114 PLOG(ERROR) << "could not fork to process firmware event for " << uevent.firmware;
115 }
116 if (pid == 0) {
117 Timer t;
118 ProcessFirmwareEvent(uevent);
119 LOG(INFO) << "loading " << uevent.path << " took " << t;
120 _exit(EXIT_SUCCESS);
121 }
122 }
123
124 } // namespace init
125 } // namespace android
126