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