blob: 3c35b1f163161c04b1bc26469ba8c62cd99adbba [file] [log] [blame]
Tom Cherryed506f72017-05-25 15:58:59 -07001/*
2 * Copyright (C) 2007 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
Tom Cherrydcb3d152019-08-07 16:02:28 -070017#pragma once
18
19#include <pwd.h>
Tom Cherryed506f72017-05-25 15:58:59 -070020
Jooyung Han21cad322020-09-18 14:41:22 +090021#include <functional>
Tom Cherry7421fa12018-07-13 15:32:02 -070022#include <string>
23#include <vector>
24
Tom Cherrydcb3d152019-08-07 16:02:28 -070025#include "result.h"
Tom Cherryed506f72017-05-25 15:58:59 -070026#include "uevent.h"
Tom Cherry457e28f2018-08-01 13:12:20 -070027#include "uevent_handler.h"
Tom Cherryed506f72017-05-25 15:58:59 -070028
Tom Cherry81f5d3e2017-06-22 12:53:17 -070029namespace android {
30namespace init {
31
Tom Cherrydcb3d152019-08-07 16:02:28 -070032struct ExternalFirmwareHandler {
Suchang Woo22fdd0a2021-04-06 11:22:49 +090033 ExternalFirmwareHandler(std::string devpath, uid_t uid, std::string handler_path);
34
Tom Cherrydcb3d152019-08-07 16:02:28 -070035 std::string devpath;
36 uid_t uid;
37 std::string handler_path;
Suchang Woo22fdd0a2021-04-06 11:22:49 +090038
39 std::function<bool(const std::string&)> match;
Tom Cherrydcb3d152019-08-07 16:02:28 -070040};
41
Tom Cherry457e28f2018-08-01 13:12:20 -070042class FirmwareHandler : public UeventHandler {
43 public:
Tom Cherrydcb3d152019-08-07 16:02:28 -070044 FirmwareHandler(std::vector<std::string> firmware_directories,
45 std::vector<ExternalFirmwareHandler> external_firmware_handlers);
Tom Cherry457e28f2018-08-01 13:12:20 -070046 virtual ~FirmwareHandler() = default;
Tom Cherry7421fa12018-07-13 15:32:02 -070047
Tom Cherry457e28f2018-08-01 13:12:20 -070048 void HandleUevent(const Uevent& uevent) override;
49
50 private:
Tom Cherrydcb3d152019-08-07 16:02:28 -070051 friend void FirmwareTestWithExternalHandler(const std::string& test_name,
52 bool expect_new_firmware);
53
54 Result<std::string> RunExternalHandler(const std::string& handler, uid_t uid,
55 const Uevent& uevent) const;
56 std::string GetFirmwarePath(const Uevent& uevent) const;
57 void ProcessFirmwareEvent(const std::string& root, const std::string& firmware) const;
Jooyung Han21cad322020-09-18 14:41:22 +090058 bool ForEachFirmwareDirectory(std::function<bool(const std::string&)> handler) const;
Tom Cherry457e28f2018-08-01 13:12:20 -070059
60 std::vector<std::string> firmware_directories_;
Tom Cherrydcb3d152019-08-07 16:02:28 -070061 std::vector<ExternalFirmwareHandler> external_firmware_handlers_;
Tom Cherry457e28f2018-08-01 13:12:20 -070062};
Tom Cherryed506f72017-05-25 15:58:59 -070063
Tom Cherry81f5d3e2017-06-22 12:53:17 -070064} // namespace init
65} // namespace android