• 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 #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 
25 namespace chre {
26 
27 /**
28  * SLPI-specific nanoapp functionality.
29  */
30 class PlatformNanoappBase {
31  public:
32   /**
33    * Sets app info that will be used later when the app is loaded into the
34    * system.
35    *
36    * @param appId The unique app identifier associated with this binary
37    * @param appVersion An application-defined version number
38    * @param appFilename The filename of the app that should be loaded from disk
39    * @param targetApiVersion The target API version the nanoapp was compiled for
40    *
41    * @return true if the info was successfully stored
42    */
43   bool setAppInfo(uint64_t appId, uint32_t appVersion, const char *appFilename,
44                   uint32_t targetApiVersion);
45 
46   /**
47    * Reserves buffer space for a nanoapp's binary. This method should be called
48    * before copyNanoappFragment is called.
49    *
50    * @param appId The unique app identifier associated with this binary
51    * @param appVersion An application-defined version number
52    * @param appFlags The flags provided by the app being loaded
53    * @param appBinaryLen Size of appBinary, in bytes
54    *
55    * @return true if the allocation was successful, false otherwise
56    */
57   bool reserveBuffer(uint64_t appId, uint32_t appVersion, uint32_t appFlags,
58                      size_t appBinarylen, uint32_t targetApiVersion);
59 
60   /**
61    * Copies the (possibly fragmented) application binary data into the allocated
62    * buffer, and updates the pointer to the next address to write into. The
63    * application may be invalid - full checking and initialization happens just
64    * before invoking start() nanoapp entry point.
65    *
66    * @param buffer The pointer to the buffer
67    * @param bufferSize The size of the buffer in bytes
68    *
69    * @return true if the reserved buffer did not overflow, false otherwise
70    */
71   bool copyNanoappFragment(const void *buffer, size_t bufferSize);
72 
73   /**
74    * Associate this Nanoapp instance with a nanoapp that is statically built
75    * into the CHRE binary with the given app info structure.
76    */
77   void loadStatic(const struct chreNslNanoappInfo *appInfo);
78 
79   /**
80    * @return true if the app's binary data is resident in memory or if the app's
81    *         filename is saved, i.e. all binary fragments are loaded through
82    *         copyNanoappFragment, loadFromFile/loadStatic() was successful, or
83    *         setAppInfo was successful.
84    */
85   bool isLoaded() const;
86 
87   /**
88    * @return true if the app runs in micro-image.
89    */
90   bool isUimgApp() const;
91 
92   /**
93    * Retrieves the nanoapp's version string. This is intended to be a human
94    * readable version string to aid in debugging (ie: commit hash). This must
95    * always return a valid string so if none is available it is recommended to
96    * return "<undefined>" or similar.
97    */
98   const char *getAppVersionString() const;
99 
100  protected:
101   //! The app ID we received in the metadata alongside the nanoapp binary. This
102   //! is also included in (and checked against) mAppInfo.
103   uint64_t mExpectedAppId;
104 
105   //! The application-defined version number we received in the metadata
106   //! alongside the nanoapp binary. This is also included in (and checked
107   //! against) mAppInfo.
108   uint32_t mExpectedAppVersion = 0;
109 
110   //! The app target API version in the metadata alongside the nanoapp binary.
111   uint32_t mExpectedTargetApiVersion = 0;
112 
113   //! Buffer containing the complete DSO binary - only populated if
114   //! copyNanoappFragment() was used to load this nanoapp
115   void *mAppBinary = nullptr;
116   size_t mAppBinaryLen = 0;
117 
118   //! Null-terminated ASCII string containing the file name that contains the
119   //! app binary to be loaded. This is used over mAppBinary to load the nanoapp
120   //! if set.
121   char *mAppFilename = nullptr;
122 
123   //! The dynamic shared object (DSO) handle returned by dlopenbuf()
124   void *mDsoHandle = nullptr;
125 
126   //! Pointer to the app info structure within this nanoapp
127   const struct chreNslNanoappInfo *mAppInfo = nullptr;
128 
129   //! Set to true if this app is built into the CHRE binary, and was loaded via
130   //! loadStatic(). In this case, the member variables above are not valid or
131   //! applicable.
132   bool mIsStatic = false;
133 
134   //! True if the nanoapp runs in micro-image.
135   bool mIsUimgApp = false;
136 
137   //! The number of bytes of the binary that has been loaded so far.
138   size_t mBytesLoaded = 0;
139 
140   /**
141    * Calls through to openNanoappFromBuffer or openNanoappFromFile, depending on
142    * how this nanoapp was loaded.
143    */
144   bool openNanoapp();
145 
146   /**
147    * Calls dlopenbuf on the app binary, and fetches and validates the app info
148    * pointer. This will result in execution of any on-load handlers (e.g. static
149    * global constructors) in the nanoapp.
150    *
151    * @return true if the app was opened successfully and the app info structure
152    *         passed validation
153    */
154   bool openNanoappFromBuffer();
155 
156   /**
157    * Calls dlopen on the app file name, and fetches and validates the app info
158    * pointer. This will result in execution of any on-load handlers (e.g. static
159    * global constructors) in the nanoapp.
160    *
161    * @return true if the app was opened successfully and the app info structure
162    *         passed validation
163    */
164   bool openNanoappFromFile();
165 
166   /**
167    * Loads the nanoapp symbols from the currently loaded binary and verifies
168    * they match the expected information the nanoapp should have.
169    *
170    * @return true if the app info structure passed validation.
171    */
172   bool verifyNanoappInfo();
173 
174   /**
175    * Releases the DSO handle if it was active, by calling dlclose(). This will
176    * result in execution of any unload handlers in the nanoapp.
177    */
178   void closeNanoapp();
179 };
180 
181 }  // namespace chre
182 
183 #endif  // CHRE_PLATFORM_SLPI_PLATFORM_NANOAPP_BASE_H_
184