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