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