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