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 * Reserves buffer space for a nanoapp's binary. This method should be called 35 * before copyNanoappFragment is called. 36 * 37 * @param appId The unique app identifier associated with this binary 38 * @param appVersion An application-defined version number 39 * @param appBinaryLen Size of appBinary, in bytes 40 * 41 * @return true if the allocation was successful, false otherwise 42 */ 43 bool reserveBuffer(uint64_t appId, uint32_t appVersion, size_t appBinarylen); 44 45 /** 46 * Copies the (possibly fragmented) application binary data into the allocated 47 * buffer, and updates the pointer to the next address to write into. The 48 * application may be invalid - full checking and initialization happens just 49 * before invoking start() nanoapp entry point. 50 * 51 * @param buffer The pointer to the buffer 52 * @param bufferSize The size of the buffer in bytes 53 * 54 * @return true if the reserved buffer did not overflow, false otherwise 55 */ 56 bool copyNanoappFragment(const void *buffer, size_t bufferSize); 57 58 /** 59 * Associate this Nanoapp with a nanoapp included in a .so that is pre-loaded 60 * onto the filesystem. Actually loading the .so into memory is done when 61 * start() is called. 62 * 63 * @param appId The nanoapp's ID 64 * @param filename The name of the .so file in /vendor/lib/dsp that holds this 65 * nanoapp. This string is not deep-copied, so the memory must remain 66 * valid for the lifetime of this Nanoapp instance. 67 */ 68 void loadFromFile(uint64_t appId, const char *filename); 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, i.e. all 78 * binary fragments are loaded through copyNanoappFragment, or 79 * loadFromFile/loadStatic() was successful 80 */ 81 bool isLoaded() const; 82 83 /** 84 * @return true if the app runs in micro-image. 85 */ 86 bool isUimgApp() const; 87 88 protected: 89 //! The app ID we received in the metadata alongside the nanoapp binary. This 90 //! is also included in (and checked against) mAppInfo. 91 uint64_t mExpectedAppId; 92 93 //! The application-defined version number we received in the metadata 94 //! alongside the nanoapp binary. This is also included in (and checked 95 //! against) mAppInfo. 96 uint32_t mExpectedAppVersion = 0; 97 98 //! Buffer containing the complete DSO binary - only populated if 99 //! copyNanoappFragment() was used to load this nanoapp 100 void *mAppBinary = nullptr; 101 size_t mAppBinaryLen = 0; 102 103 //! If this is a pre-loaded, but non-static nanoapp (i.e. loaded from 104 //! loadFromFile), this will be set to the filename string to pass to dlopen() 105 const char *mFilename = nullptr; 106 107 //! The dynamic shared object (DSO) handle returned by dlopen[buf]() 108 void *mDsoHandle = nullptr; 109 110 //! Pointer to the app info structure within this nanoapp 111 const struct chreNslNanoappInfo *mAppInfo = nullptr; 112 113 //! Set to true if this app is built into the CHRE binary, and was loaded via 114 //! loadStatic(). In this case, the member variables above are not valid or 115 //! applicable. 116 bool mIsStatic = false; 117 118 //! True if the nanoapp runs in micro-image. 119 bool mIsUimgApp = false; 120 121 //! The number of bytes of the binary that has been loaded so far. 122 size_t mBytesLoaded = 0; 123 124 /** 125 * Calls through to openNanoappFromBuffer or openNanoappFromFile, depending on 126 * how this nanoapp was loaded. 127 */ 128 bool openNanoapp(); 129 130 /** 131 * Calls dlopenbuf on the app binary, and fetches and validates the app info 132 * pointer. This will result in execution of any on-load handlers (e.g. static 133 * global constructors) in the nanoapp. 134 * 135 * @return true if the app was opened successfully and the app info structure 136 * passed validation 137 */ 138 bool openNanoappFromBuffer(); 139 140 /** 141 * Calls dlopen on the app filename, and fetches and validates the app info 142 * pointer. This will result in execution of any on-load handlers (e.g. 143 * static global constructors) in the nanoapp. 144 * 145 * @return true if the app was opened successfully and the app info 146 * structure passed validation 147 */ 148 bool openNanoappFromFile(); 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 } // namespace chre 158 159 #endif // CHRE_PLATFORM_SLPI_PLATFORM_NANOAPP_BASE_H_ 160