blob: 8b55147110bcd4ea70363177677ad0e27d2566ba [file] [log] [blame]
Ed Coyne7464ac92017-06-08 12:26:48 -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 "BootAction.h"
18
19#define LOG_TAG "BootAction"
20
Ed Coyne7464ac92017-06-08 12:26:48 -070021#include <dlfcn.h>
Braden Kell6dd64f32017-09-25 17:30:28 -070022
Ed Coyne7464ac92017-06-08 12:26:48 -070023#include <pio/peripheral_manager_client.h>
24#include <utils/Log.h>
25
Ed Coyne7464ac92017-06-08 12:26:48 -070026namespace android {
27
28BootAction::~BootAction() {
29 if (mLibHandle != nullptr) {
30 dlclose(mLibHandle);
31 }
32}
33
David Pursell54a8fe42017-09-29 16:05:26 -070034bool BootAction::init(const std::string& libraryPath,
Mickey Keeley47d2c022018-04-30 11:39:30 -070035 const std::unique_ptr<BootParameters>& bootParameters) {
Ed Coyne7464ac92017-06-08 12:26:48 -070036 APeripheralManagerClient* client = nullptr;
37 ALOGD("Connecting to peripheralmanager");
38 // Wait for peripheral manager to come up.
39 while (client == nullptr) {
40 client = APeripheralManagerClient_new();
41 if (client == nullptr) {
42 ALOGV("peripheralmanager is not up, sleeping before we check again.");
43 usleep(250000);
44 }
45 }
46 ALOGD("Peripheralmanager is up.");
47 APeripheralManagerClient_delete(client);
48
Braden Kell6dd64f32017-09-25 17:30:28 -070049
Ed Coyne428ed512017-08-14 15:10:06 -070050 ALOGI("Loading boot action %s", libraryPath.c_str());
51 mLibHandle = dlopen(libraryPath.c_str(), RTLD_NOW);
Ed Coyne7464ac92017-06-08 12:26:48 -070052 if (mLibHandle == nullptr) {
53 ALOGE("Unable to load library at %s :: %s",
Ed Coyne428ed512017-08-14 15:10:06 -070054 libraryPath.c_str(), dlerror());
Ed Coyne7464ac92017-06-08 12:26:48 -070055 return false;
56 }
57
58 void* loaded = nullptr;
59 if (!loadSymbol("boot_action_init", &loaded) || loaded == nullptr) {
60 return false;
61 }
62 mLibInit = reinterpret_cast<libInit>(loaded);
63
64 loaded = nullptr;
65 if (!loadSymbol("boot_action_shutdown", &loaded) || loaded == nullptr) {
66 return false;
67 }
68 mLibShutdown = reinterpret_cast<libShutdown>(loaded);
69
70 // StartPart is considered optional, if it isn't exported by the library
71 // we will still call init and shutdown.
72 loaded = nullptr;
73 if (!loadSymbol("boot_action_start_part", &loaded) || loaded == nullptr) {
74 ALOGI("No boot_action_start_part found, action will not be told when "
75 "Animation parts change.");
76 } else {
77 mLibStartPart = reinterpret_cast<libStartPart>(loaded);
78 }
79
Mickey Keeley47d2c022018-04-30 11:39:30 -070080 // SilentBoot is considered optional, if it isn't exported by the library
81 // and the boot is silent, no method is called.
82 loaded = nullptr;
83 if (!loadSymbol("boot_action_silent_boot", &loaded) || loaded == nullptr) {
84 ALOGW("No boot_action_silent_boot found, boot action will not be "
85 "executed during a silent boot.");
86 } else {
87 mLibSilentBoot = reinterpret_cast<libInit>(loaded);
88 }
89
90 bool result = true;
91 const auto& parameters = bootParameters->getParameters();
92 if (bootParameters->isSilentBoot()) {
93 if (mLibSilentBoot != nullptr) {
94 ALOGD("Entering boot_action_silent_boot");
95 result = mLibSilentBoot(parameters.data(), parameters.size());
96 ALOGD("Returned from boot_action_silent_boot");
97 } else {
98 ALOGW("Skipping missing boot_action_silent_boot");
99 }
100 } else {
101 ALOGD("Entering boot_action_init");
102 result = mLibInit(parameters.data(), parameters.size());
103 ALOGD("Returned from boot_action_init");
104 }
105
Ed Coyne7464ac92017-06-08 12:26:48 -0700106 return result;
107}
108
109void BootAction::startPart(int partNumber, int playNumber) {
110 if (mLibStartPart == nullptr) return;
111
112 ALOGD("Entering boot_action_start_part");
113 mLibStartPart(partNumber, playNumber);
114 ALOGD("Returned from boot_action_start_part");
115}
116
117void BootAction::shutdown() {
118 ALOGD("Entering boot_action_shutdown");
119 mLibShutdown();
120 ALOGD("Returned from boot_action_shutdown");
121}
122
123bool BootAction::loadSymbol(const char* symbol, void** loaded) {
124 *loaded = dlsym(mLibHandle, symbol);
Ed Coynea9dab512018-03-26 13:12:24 -0700125 if (*loaded == nullptr) {
Ed Coyne7464ac92017-06-08 12:26:48 -0700126 ALOGE("Unable to load symbol : %s :: %s", symbol, dlerror());
127 return false;
128 }
129 return true;
130}
131
Ed Coyne7464ac92017-06-08 12:26:48 -0700132} // namespace android