• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <dlfcn.h>
20 #include <cinttypes>
21 
22 #include "chre/platform/assert.h"
23 #include "chre/platform/log.h"
24 #include "chre/platform/shared/nanoapp_dso_util.h"
25 #include "chre/util/system/napp_permissions.h"
26 #include "chre_api/chre/version.h"
27 
28 namespace chre {
29 
~PlatformNanoapp()30 PlatformNanoapp::~PlatformNanoapp() {
31   closeNanoapp();
32 }
33 
start()34 bool PlatformNanoapp::start() {
35   return openNanoapp() && mAppInfo->entryPoints.start();
36 }
37 
handleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)38 void PlatformNanoapp::handleEvent(uint32_t senderInstanceId, 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 
getAppName() const60 const char *PlatformNanoapp::getAppName() const {
61   return (mAppInfo != nullptr) ? mAppInfo->name : "Unknown";
62 }
63 
supportsAppPermissions() const64 bool PlatformNanoapp::supportsAppPermissions() const {
65   return (mAppInfo != nullptr) ? (mAppInfo->structMinorVersion >=
66                                   CHRE_NSL_NANOAPP_INFO_STRUCT_MINOR_VERSION_3)
67                                : false;
68 }
69 
getAppPermissions() const70 uint32_t PlatformNanoapp::getAppPermissions() const {
71   return (supportsAppPermissions())
72              ? mAppInfo->appPermissions
73              : static_cast<uint32_t>(chre::NanoappPermissions::CHRE_PERMS_NONE);
74 }
75 
isSystemNanoapp() const76 bool PlatformNanoapp::isSystemNanoapp() const {
77   return (mAppInfo != nullptr && mAppInfo->isSystemNanoapp);
78 }
79 
logStateToBuffer(DebugDumpWrapper &) const80 void PlatformNanoapp::logStateToBuffer(
81     DebugDumpWrapper & /* debugDump */) const {}
82 
loadFromFile(const std::string & filename)83 void PlatformNanoappBase::loadFromFile(const std::string &filename) {
84   CHRE_ASSERT(!isLoaded());
85   mFilename = filename;
86 }
87 
loadStatic(const struct chreNslNanoappInfo * appInfo)88 void PlatformNanoappBase::loadStatic(const struct chreNslNanoappInfo *appInfo) {
89   CHRE_ASSERT(!isLoaded());
90   mIsStatic = true;
91   mAppInfo = appInfo;
92 }
93 
isLoaded() const94 bool PlatformNanoappBase::isLoaded() const {
95   return (mIsStatic || mDsoHandle != nullptr);
96 }
97 
openNanoapp()98 bool PlatformNanoappBase::openNanoapp() {
99   bool success = false;
100 
101   if (mIsStatic) {
102     success = true;
103   } else if (!mFilename.empty()) {
104     success = openNanoappFromFile();
105   } else {
106     CHRE_ASSERT(false);
107   }
108 
109   return success;
110 }
111 
openNanoappFromFile()112 bool PlatformNanoappBase::openNanoappFromFile() {
113   CHRE_ASSERT(!mFilename.empty());
114   CHRE_ASSERT_LOG(mDsoHandle == nullptr, "Re-opening nanoapp");
115   bool success = false;
116 
117   mDsoHandle = dlopen(mFilename.c_str(), RTLD_NOW | RTLD_GLOBAL);
118   if (mDsoHandle == nullptr) {
119     LOGE("Failed to load nanoapp from file %s: %s", mFilename.c_str(),
120          dlerror());
121   } else {
122     mAppInfo = static_cast<const struct chreNslNanoappInfo *>(
123         dlsym(mDsoHandle, CHRE_NSL_DSO_NANOAPP_INFO_SYMBOL_NAME));
124     if (mAppInfo == nullptr) {
125       LOGE("Failed to find app info symbol in %s: %s", mFilename.c_str(),
126            dlerror());
127     } else {
128       // TODO(b/120778991): reenable this check after adding support for passing
129       // in the .napp_header to the simulator
130       // success = validateAppInfo(0 /* skip ID validation */, 0, mAppInfo);
131       success = true;
132       if (!success) {
133         mAppInfo = nullptr;
134       } else {
135         LOGI("Nanoapp loaded: %s (0x%016" PRIx64 ") version 0x%" PRIx32
136              " uimg %d system %d from file %s",
137              mAppInfo->name, mAppInfo->appId, mAppInfo->appVersion,
138              mAppInfo->isTcmNanoapp, mAppInfo->isSystemNanoapp,
139              mFilename.c_str());
140         if (mAppInfo->structMinorVersion >=
141             CHRE_NSL_NANOAPP_INFO_STRUCT_MINOR_VERSION_3) {
142           LOGI("Nanoapp permissions: 0x%" PRIx32, mAppInfo->appPermissions);
143         }
144       }
145     }
146   }
147 
148   return success;
149 }
150 
closeNanoapp()151 void PlatformNanoappBase::closeNanoapp() {
152   if (mDsoHandle != nullptr) {
153     mAppInfo = nullptr;
154     if (dlclose(mDsoHandle) != 0) {
155       LOGE("dlclose failed: %s", dlerror());
156     }
157     mDsoHandle = nullptr;
158   }
159 }
160 
161 }  // namespace chre
162