1 /*
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 
21 #include <dlfcn.h>
22 
23 #include <pio/peripheral_manager_client.h>
24 #include <utils/Log.h>
25 
26 namespace android {
27 
~BootAction()28 BootAction::~BootAction() {
29     if (mLibHandle != nullptr) {
30         dlclose(mLibHandle);
31     }
32 }
33 
init(const std::string & libraryPath,const std::unique_ptr<BootParameters> & bootParameters)34 bool BootAction::init(const std::string& libraryPath,
35                       const std::unique_ptr<BootParameters>& bootParameters) {
36     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 
49 
50     ALOGI("Loading boot action %s", libraryPath.c_str());
51     mLibHandle = dlopen(libraryPath.c_str(), RTLD_NOW);
52     if (mLibHandle == nullptr) {
53         ALOGE("Unable to load library at %s :: %s",
54               libraryPath.c_str(), dlerror());
55         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 
80     // 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 
106     return result;
107 }
108 
startPart(int partNumber,int playNumber)109 void 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 
shutdown()117 void BootAction::shutdown() {
118     ALOGD("Entering boot_action_shutdown");
119     mLibShutdown();
120     ALOGD("Returned from boot_action_shutdown");
121 }
122 
loadSymbol(const char * symbol,void ** loaded)123 bool BootAction::loadSymbol(const char* symbol, void** loaded) {
124     *loaded = dlsym(mLibHandle, symbol);
125     if (*loaded == nullptr) {
126         ALOGE("Unable to load symbol : %s :: %s", symbol, dlerror());
127         return false;
128     }
129     return true;
130 }
131 
132 }  // namespace android
133