blob: 844c6051f69be312bc00264a087b2e196a00550e [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
24#include <string>
25#include <thread>
26
27#include <android-base/file.h>
28#include <android-base/logging.h>
29#include <android-base/unique_fd.h>
30
31#include "util.h"
32
33static void LoadFirmware(const Uevent& uevent, const std::string& root, int fw_fd, size_t fw_size,
34 int loading_fd, int data_fd) {
35 // Start transfer.
36 android::base::WriteFully(loading_fd, "1", 1);
37
38 // Copy the firmware.
39 int rc = sendfile(data_fd, fw_fd, nullptr, fw_size);
40 if (rc == -1) {
41 PLOG(ERROR) << "firmware: sendfile failed { '" << root << "', '" << uevent.firmware
42 << "' }";
43 }
44
45 // Tell the firmware whether to abort or commit.
46 const char* response = (rc != -1) ? "0" : "-1";
47 android::base::WriteFully(loading_fd, response, strlen(response));
48}
49
50static bool IsBooting() {
51 return access("/dev/.booting", F_OK) == 0;
52}
53
54static void ProcessFirmwareEvent(const Uevent& uevent) {
55 int booting = IsBooting();
56
57 LOG(INFO) << "firmware: loading '" << uevent.firmware << "' for '" << uevent.path << "'";
58
59 std::string root = "/sys" + uevent.path;
60 std::string loading = root + "/loading";
61 std::string data = root + "/data";
62
63 android::base::unique_fd loading_fd(open(loading.c_str(), O_WRONLY | O_CLOEXEC));
64 if (loading_fd == -1) {
65 PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent.firmware;
66 return;
67 }
68
69 android::base::unique_fd data_fd(open(data.c_str(), O_WRONLY | O_CLOEXEC));
70 if (data_fd == -1) {
71 PLOG(ERROR) << "couldn't open firmware data fd for " << uevent.firmware;
72 return;
73 }
74
75 static const char* firmware_dirs[] = {"/etc/firmware/", "/vendor/firmware/",
76 "/firmware/image/"};
77
78try_loading_again:
79 for (size_t i = 0; i < arraysize(firmware_dirs); i++) {
80 std::string file = firmware_dirs[i] + uevent.firmware;
81 android::base::unique_fd fw_fd(open(file.c_str(), O_RDONLY | O_CLOEXEC));
82 struct stat sb;
83 if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) {
84 LoadFirmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd);
85 return;
86 }
87 }
88
89 if (booting) {
90 // If we're not fully booted, we may be missing
91 // filesystems needed for firmware, wait and retry.
92 std::this_thread::sleep_for(100ms);
93 booting = IsBooting();
94 goto try_loading_again;
95 }
96
97 LOG(ERROR) << "firmware: could not find firmware for " << uevent.firmware;
98
99 // Write "-1" as our response to the kernel's firmware request, since we have nothing for it.
100 write(loading_fd, "-1", 2);
101}
102
103void HandleFirmwareEvent(const Uevent& uevent) {
104 if (uevent.subsystem != "firmware" || uevent.action != "add") return;
105
106 // Loading the firmware in a child means we can do that in parallel...
Tom Cherryc5833052017-05-16 15:35:41 -0700107 // We double fork instead of waiting for these processes.
Tom Cherryed506f72017-05-25 15:58:59 -0700108 pid_t pid = fork();
Tom Cherryc5833052017-05-16 15:35:41 -0700109 if (pid == -1) {
Tom Cherryed506f72017-05-25 15:58:59 -0700110 PLOG(ERROR) << "could not fork to process firmware event for " << uevent.firmware;
Tom Cherryc5833052017-05-16 15:35:41 -0700111 return;
Tom Cherryed506f72017-05-25 15:58:59 -0700112 }
Tom Cherryc5833052017-05-16 15:35:41 -0700113
114 if (pid == 0) {
115 pid = fork();
116 if (pid == -1) {
117 PLOG(ERROR) << "could not fork a sceond time to process firmware event for "
118 << uevent.firmware;
119 _exit(EXIT_FAILURE);
120 }
121 if (pid == 0) {
122 Timer t;
123 ProcessFirmwareEvent(uevent);
124 LOG(INFO) << "loading " << uevent.path << " took " << t;
125 _exit(EXIT_SUCCESS);
126 }
127
128 _exit(EXIT_SUCCESS);
129 }
130
131 waitpid(pid, nullptr, 0);
Tom Cherryed506f72017-05-25 15:58:59 -0700132}