blob: 28bda34a252a495fb0893d009231709c9ae05b59 [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 Cherry7421fa12018-07-13 15:32:02 -070038std::vector<std::string> firmware_directories;
39
Tom Cherryed506f72017-05-25 15:58:59 -070040static void LoadFirmware(const Uevent& uevent, const std::string& root, int fw_fd, size_t fw_size,
41 int loading_fd, int data_fd) {
42 // Start transfer.
Tom Cherry81f5d3e2017-06-22 12:53:17 -070043 WriteFully(loading_fd, "1", 1);
Tom Cherryed506f72017-05-25 15:58:59 -070044
45 // Copy the firmware.
46 int rc = sendfile(data_fd, fw_fd, nullptr, fw_size);
47 if (rc == -1) {
48 PLOG(ERROR) << "firmware: sendfile failed { '" << root << "', '" << uevent.firmware
49 << "' }";
50 }
51
52 // Tell the firmware whether to abort or commit.
53 const char* response = (rc != -1) ? "0" : "-1";
Tom Cherry81f5d3e2017-06-22 12:53:17 -070054 WriteFully(loading_fd, response, strlen(response));
Tom Cherryed506f72017-05-25 15:58:59 -070055}
56
57static bool IsBooting() {
58 return access("/dev/.booting", F_OK) == 0;
59}
60
61static void ProcessFirmwareEvent(const Uevent& uevent) {
62 int booting = IsBooting();
63
64 LOG(INFO) << "firmware: loading '" << uevent.firmware << "' for '" << uevent.path << "'";
65
66 std::string root = "/sys" + uevent.path;
67 std::string loading = root + "/loading";
68 std::string data = root + "/data";
69
Tom Cherry81f5d3e2017-06-22 12:53:17 -070070 unique_fd loading_fd(open(loading.c_str(), O_WRONLY | O_CLOEXEC));
Tom Cherryed506f72017-05-25 15:58:59 -070071 if (loading_fd == -1) {
72 PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent.firmware;
73 return;
74 }
75
Tom Cherry81f5d3e2017-06-22 12:53:17 -070076 unique_fd data_fd(open(data.c_str(), O_WRONLY | O_CLOEXEC));
Tom Cherryed506f72017-05-25 15:58:59 -070077 if (data_fd == -1) {
78 PLOG(ERROR) << "couldn't open firmware data fd for " << uevent.firmware;
79 return;
80 }
81
Tom Cherryed506f72017-05-25 15:58:59 -070082try_loading_again:
Tom Cherry7421fa12018-07-13 15:32:02 -070083 for (const auto& firmware_directory : firmware_directories) {
84 std::string file = firmware_directory + uevent.firmware;
Tom Cherry81f5d3e2017-06-22 12:53:17 -070085 unique_fd fw_fd(open(file.c_str(), O_RDONLY | O_CLOEXEC));
Tom Cherryed506f72017-05-25 15:58:59 -070086 struct stat sb;
87 if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) {
88 LoadFirmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd);
89 return;
90 }
91 }
92
93 if (booting) {
94 // If we're not fully booted, we may be missing
95 // filesystems needed for firmware, wait and retry.
96 std::this_thread::sleep_for(100ms);
97 booting = IsBooting();
98 goto try_loading_again;
99 }
100
101 LOG(ERROR) << "firmware: could not find firmware for " << uevent.firmware;
102
103 // Write "-1" as our response to the kernel's firmware request, since we have nothing for it.
104 write(loading_fd, "-1", 2);
105}
106
107void HandleFirmwareEvent(const Uevent& uevent) {
108 if (uevent.subsystem != "firmware" || uevent.action != "add") return;
109
110 // Loading the firmware in a child means we can do that in parallel...
Tom Cherry0f296e02017-06-30 12:58:39 -0700111 auto pid = fork();
Tom Cherryc5833052017-05-16 15:35:41 -0700112 if (pid == -1) {
Tom Cherryed506f72017-05-25 15:58:59 -0700113 PLOG(ERROR) << "could not fork to process firmware event for " << uevent.firmware;
114 }
Tom Cherryc5833052017-05-16 15:35:41 -0700115 if (pid == 0) {
Tom Cherry0f296e02017-06-30 12:58:39 -0700116 Timer t;
117 ProcessFirmwareEvent(uevent);
118 LOG(INFO) << "loading " << uevent.path << " took " << t;
Tom Cherryc5833052017-05-16 15:35:41 -0700119 _exit(EXIT_SUCCESS);
120 }
Tom Cherryed506f72017-05-25 15:58:59 -0700121}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700122
123} // namespace init
124} // namespace android