1 /*
2  * Copyright (C) 2016 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 "chre/platform/platform_nanoapp.h"
18 
19 #include <cinttypes>
20 #include <dlfcn.h>
21 
22 #include "chre_api/chre/version.h"
23 #include "chre/platform/assert.h"
24 #include "chre/platform/log.h"
25 #include "chre/platform/shared/nanoapp_dso_util.h"
26 
27 namespace chre {
28 
~PlatformNanoapp()29 PlatformNanoapp::~PlatformNanoapp() {
30   closeNanoapp();
31 }
32 
start()33 bool PlatformNanoapp::start() {
34   return openNanoapp() && mAppInfo->entryPoints.start();
35 }
36 
handleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)37 void PlatformNanoapp::handleEvent(uint32_t senderInstanceId,
38                                   uint16_t eventType,
39                                   const void *eventData) {
40   mAppInfo->entryPoints.handleEvent(senderInstanceId, eventType, eventData);
41 }
42 
end()43 void PlatformNanoapp::end() {
44   mAppInfo->entryPoints.end();
45   closeNanoapp();
46 }
47 
getAppId() const48 uint64_t PlatformNanoapp::getAppId() const {
49   return (mAppInfo == nullptr) ? 0 : mAppInfo->appId;
50 }
51 
getAppVersion() const52 uint32_t PlatformNanoapp::getAppVersion() const {
53   return mAppInfo->appVersion;
54 }
55 
getTargetApiVersion() const56 uint32_t PlatformNanoapp::getTargetApiVersion() const {
57   return CHRE_API_VERSION;
58 }
59 
isSystemNanoapp() const60 bool PlatformNanoapp::isSystemNanoapp() const {
61   return (mAppInfo != nullptr && mAppInfo->isSystemNanoapp);
62 }
63 
logStateToBuffer(char * buffer,size_t * bufferPos,size_t bufferSize) const64 void PlatformNanoapp::logStateToBuffer(char *buffer, size_t *bufferPos,
65                                        size_t bufferSize) const {}
66 
loadFromFile(const std::string & filename)67 void PlatformNanoappBase::loadFromFile(const std::string& filename) {
68   CHRE_ASSERT(!isLoaded());
69   mFilename = filename;
70 }
71 
loadStatic(const struct chreNslNanoappInfo * appInfo)72 void PlatformNanoappBase::loadStatic(const struct chreNslNanoappInfo *appInfo) {
73   CHRE_ASSERT(!isLoaded());
74   mIsStatic = true;
75   mAppInfo = appInfo;
76 }
77 
isLoaded() const78 bool PlatformNanoappBase::isLoaded() const {
79   return (mIsStatic || mDsoHandle != nullptr);
80 }
81 
openNanoapp()82 bool PlatformNanoappBase::openNanoapp() {
83   bool success = false;
84 
85   if (mIsStatic) {
86     success = true;
87   } else if (!mFilename.empty()) {
88     success = openNanoappFromFile();
89   } else {
90     CHRE_ASSERT(false);
91   }
92 
93   return success;
94 }
95 
openNanoappFromFile()96 bool PlatformNanoappBase::openNanoappFromFile() {
97   CHRE_ASSERT(!mFilename.empty());
98   CHRE_ASSERT_LOG(mDsoHandle == nullptr, "Re-opening nanoapp");
99   bool success = false;
100 
101   mDsoHandle = dlopen(mFilename.c_str(), RTLD_NOW | RTLD_GLOBAL);
102   if (mDsoHandle == nullptr) {
103     LOGE("Failed to load nanoapp from file %s: %s",
104          mFilename.c_str(), dlerror());
105   } else {
106     mAppInfo = static_cast<const struct chreNslNanoappInfo *>(
107         dlsym(mDsoHandle, CHRE_NSL_DSO_NANOAPP_INFO_SYMBOL_NAME));
108     if (mAppInfo == nullptr) {
109       LOGE("Failed to find app info symbol in %s: %s",
110            mFilename.c_str(), dlerror());
111     } else {
112       // TODO(b/120778991): reenable this check after adding support for passing
113       // in the .napp_header to the simulator
114       //success = validateAppInfo(0 /* skip ID validation */, 0, mAppInfo);
115       success = true;
116       if (!success) {
117         mAppInfo = nullptr;
118       } else {
119         LOGI("Successfully loaded nanoapp %s (0x%016" PRIx64 ") version 0x%"
120              PRIx32 " uimg %d system %d from file %s", mAppInfo->name,
121              mAppInfo->appId, mAppInfo->appVersion, mAppInfo->isTcmNanoapp,
122              mAppInfo->isSystemNanoapp, mFilename.c_str());
123       }
124     }
125   }
126 
127   return success;
128 }
129 
closeNanoapp()130 void PlatformNanoappBase::closeNanoapp() {
131   if (mDsoHandle != nullptr) {
132     mAppInfo = nullptr;
133     if (dlclose(mDsoHandle) != 0) {
134       LOGE("dlclose failed: %s", dlerror());
135     }
136     mDsoHandle = nullptr;
137   }
138 }
139 
140 }  // namespace chre
141