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 #ifndef CHRE_PLATFORM_SLPI_PLATFORM_NANOAPP_BASE_H_
18 #define CHRE_PLATFORM_SLPI_PLATFORM_NANOAPP_BASE_H_
19 
20 #include <cstddef>
21 #include <cstdint>
22 
23 #include "chre/platform/shared/nanoapp_support_lib_dso.h"
24 #include "chre/util/entry_points.h"
25 
26 namespace chre {
27 
28 /**
29  * SLPI-specific nanoapp functionality.
30  */
31 class PlatformNanoappBase {
32  public:
33   /**
34    * Copies the supplied application binary data into a new buffer. The
35    * application may be invalid - full checking and initialization happens just
36    * before invoking start() nanoapp entry point.
37    *
38    * @param appId The unique app identifier associated with this binary
39    * @param appVersion An application-defined version number
40    * @param appBinary Buffer containing the complete ELF binary for this
41    *        nanoapp, without any CHRE-specific header
42    * @param appBinaryLen Size of appBinary, in bytes
43    *
44    * @return true if the allocation was successful
45    */
46   bool loadFromBuffer(uint64_t appId, uint32_t appVersion,
47                       const void *appBinary, size_t appBinaryLen);
48 
49   /**
50    * Associate this PlatformNanoapp with a nanoapp that is statically built into
51    * the CHRE binary with the given app info structure.
52    */
53   void loadStatic(const struct chreNslNanoappInfo *appInfo);
54 
55   /**
56    * @return true if the app's binary data is resident in memory, i.e. a
57    *         previous call to loadFromBuffer() or loadStatic() was successful
58    */
59   bool isLoaded() const;
60 
61  protected:
62   //! The app ID we received in the metadata alongside the nanoapp binary. This
63   //! is also included in (and checked against) mAppInfo.
64   uint64_t mExpectedAppId;
65 
66   //! The application-defined version number we received in the metadata
67   //! alongside the nanoapp binary. This is also included in (and checked
68   //! against) mAppInfo.
69   uint32_t mExpectedAppVersion;
70 
71   //! Buffer containing the complete DSO binary
72   void *mAppBinary = nullptr;
73   size_t mAppBinaryLen = 0;
74 
75   //! The dynamic shared object (DSO) handle returned by dlopen[buf]()
76   void *mDsoHandle = nullptr;
77 
78   //! Pointer to the app info structure within this nanoapp
79   const struct chreNslNanoappInfo *mAppInfo = nullptr;
80 
81   //! Set to true if this app is built into the CHRE binary, and was loaded via
82   //! loadStatic(). In this case, the member variables above are not valid or
83   //! applicable.
84   bool mIsStatic = false;
85 
86   /**
87    * Calls dlopenbuf on the app binary, and fetches and validates the app info
88    * pointer. This will result in execution of any on-load handlers (e.g. static
89    * global constructors) in the nanoapp.
90    *
91    * @return true if the app was opened successfully and the app info structure
92    *         passed validation
93    */
94   bool openNanoapp();
95 
96   /**
97    * Releases the DSO handle if it was active, by calling dlclose(). This will
98    * result in execution of any unload handlers in the nanoapp.
99    */
100   void closeNanoapp();
101 };
102 
103 }  // namespace chre
104 
105 #endif  // CHRE_PLATFORM_SLPI_PLATFORM_NANOAPP_BASE_H_
106