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 _SEOS_H_
18 #define _SEOS_H_
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #include <plat/taggedPtr.h>
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stdarg.h>
28 #include <stddef.h>
29 #include <eventQ.h>
30 #include <plat/app.h>
31 #include <eventnums.h>
32 #include <variant/variant.h>
33 #include "toolchain.h"
34 
35 #include <nanohub/nanohub.h>
36 
37 //#define SEGMENT_CRC_SUPPORT
38 
39 #ifndef MAX_TASKS
40 /* Default to 16 tasks, override may come from variant.h */
41 #define MAX_TASKS                        16
42 #endif
43 
44 #define MAX_EMBEDDED_EVT_SUBS             6 /* tradeoff, no wrong answer */
45 #define TASK_IDX_BITS                     8 /* should be big enough to hold MAX_TASKS, but still fit in TaskIndex */
46 
47 typedef uint8_t TaskIndex;
48 
49 struct AppFuncs { /* do not rearrange */
50     /* lifescycle */
51     bool (*init)(uint32_t yourTid);   //simple init only - no ints on at this time
52     void (*end)(void);                //die quickly please
53     /* events */
54     void (*handle)(uint32_t evtType, const void* evtData);
55 };
56 
57 /* NOTE: [TASK ID]
58  * TID is designed to be 16-bit; there is no reason for TID to become bigger than that on a system
59  * with typical RAM size of 64kB. However, in NO CASE TID values should overlap with TaggedPtr TAG mask,
60  * which is currently defined as 0x80000000.
61  */
62 
63 #define TASK_TID_BITS 16
64 
65 #define TASK_TID_MASK ((1 << TASK_TID_BITS) - 1)
66 #define TASK_TID_INCREMENT (1 << TASK_IDX_BITS)
67 #define TASK_TID_IDX_MASK ((1 << TASK_IDX_BITS) - 1)
68 #define TASK_TID_COUNTER_MASK ((1 << TASK_TID_BITS) - TASK_TID_INCREMENT)
69 
70 #if MAX_TASKS > TASK_TID_IDX_MASK
71 #error MAX_TASKS does not fit in TASK_TID_BITS
72 #endif
73 
74 #define OS_SYSTEM_TID                    0
75 #define OS_VER                           0x0000
76 
77 // FIXME: compatibility: keep key ID 1 until key update is functional
78 //#define ENCR_KEY_GOOGLE_PREPOPULATED     0x041F010000000001
79 #define ENCR_KEY_GOOGLE_PREPOPULATED     1 // our key ID is 1
80 
81 #define APP_HDR_MAGIC              NANOAPP_FW_MAGIC
82 #define APP_HDR_VER_CUR            1
83 
84 #define FL_APP_HDR_INTERNAL        0x0001 // to be able to fork behavior at run time for internal apps
85 #define FL_APP_HDR_APPLICATION     0x0002 // image has AppHdr; otherwise is has AppInfo header
86 #define FL_APP_HDR_SECURE          0x0004 // secure content, needs to be zero-filled when discarded
87 #define FL_APP_HDR_VOLATILE        0x0008 // volatile content, segment shall be deleted after operation is complete
88 #define FL_APP_HDR_CHRE            0x0010 // app is CHRE API compatible
89 #define FL_KEY_HDR_DELETE          0x8000 // key-specific flag: if set key id refers to existing key which has to be deleted
90 
91 /* app ids are split into vendor and app parts. vendor parts are assigned by google. App parts are free for each vendor to assign at will */
92 #define KEY_ID_MAKE(vendor, key)   ((((uint64_t)(vendor)) << 24) | ((key) & KEY_SEQ_ID_ANY))
93 #define HW_ID_MAKE(vendor, ver)    ((((uint64_t)(vendor)) << 24) | (PLATFORM_ID(ver) & HW_ID_ANY))
94 #define KEY_SEQ_ID_ANY             UINT64_C(0xFFFFFF)
95 #define HW_ID_ANY                  UINT64_C(0xFFFFFF)
96 #define PLATFORM_ID(ver)           ((((PLATFORM_HW_TYPE) & 0xFFFF) << 8) | (ver & 0xFF))
97 
98 #define APP_INFO_CMD_ADD_KEY 1
99 #define APP_INFO_CMD_REMOVE_KEY 2
100 #define APP_INFO_CMD_OS_UPDATE 3
101 
102 #define SEG_STATE_INVALID UINT32_C(0xFFFFFFFF)
103 #define SEG_SIZE_MAX      UINT32_C(0x00FFFFFF)
104 #define SEG_SIZE_INVALID  (-1)
105 #define SEG_ST(arg) (((arg) << 4) | (arg))
106 
107 #define SEG_ID_EMPTY    0xF
108 #define SEG_ID_RESERVED 0x7 // upload in progress
109 #define SEG_ID_VALID    0x3 // CRC-32 valid
110 #define SEG_ID_ERASED   0x0 // segment erased
111 
112 #define SEG_ST_EMPTY    SEG_ST(SEG_ID_EMPTY)
113 #define SEG_ST_RESERVED SEG_ST(SEG_ID_RESERVED)
114 #define SEG_ST_VALID    SEG_ST(SEG_ID_VALID)
115 #define SEG_ST_ERASED   SEG_ST(SEG_ID_ERASED)
116 
117 struct Segment {
118     uint8_t  state;   // 0xFF: empty; bit7=0: segment present; bit6=0: size valid; bit5=0: CRC-32 valid; bit4=0:segment erased;
119                       // bits 3-0 replicate bits7-4;
120     uint8_t  size[3]; // actual stored size in flash, initially filled with 0xFF
121                       // updated after flash operation is completed (successfully or not)
122 };
123 
124 struct AppEventFreeData { //goes with EVT_APP_FREE_EVT_DATA
125     uint32_t evtType;
126     void* evtData;
127 };
128 
129 typedef void (*OsDeferCbkF)(void *);
130 
131 typedef void (*EventFreeF)(void* event);
132 
133 SET_PACKED_STRUCT_MODE_ON
134 struct SeosEedataEncrKeyData {
135     uint64_t keyID;
136     uint8_t key[32];
137 } ATTRIBUTE_PACKED;
138 SET_PACKED_STRUCT_MODE_OFF
139 
140 /* ==== ABOUT THE "urgent" FLAG ====
141  *
142  * Do not set "urgent" unless you understand all the repercussions! What repercussions you might ask?
143  * Setting this flag will place your defer request at the front of the queue. This is useful for enqueueing work
144  * from interrupt context that needs to be done "very very soon"(tm). Doing this will delay all other work requests
145  * that have heretofore been peacefully queueing in full faith and with complete belief in fairness of our "FIFO"-ness.
146  * Please be appreciative of this fact and do not abuse this! Example: if you are setting "urgent" flag outside of interrupt
147  * context, you're very very likely wrong. That is not to say that being in interrupt context is a free pass to set this!
148  */
149 
150 // osMainInit is exposed for testing only, it must never be called for any reason at all by anyone
151 void osMainInit(void);
152 // osMainDequeueLoop is exposed for testing only, it must never be called for any reason at all by anyone
153 void osMainDequeueLoop(void);
154 void osMain(void);
155 
156 bool osEventSubscribe(uint32_t tid, uint32_t evtType); /* async */
157 bool osEventUnsubscribe(uint32_t tid, uint32_t evtType);  /* async */
158 bool osEventsSubscribe(uint32_t numEvts, ...); /* async */
159 bool osEventsUnsubscribe(uint32_t numEvts, ...); /* async */
160 
161 bool osEnqueuePrivateEvt(uint32_t evtType, void *evtData, EventFreeF evtFreeF, uint32_t toTid);
162 bool osEnqueuePrivateEvtAsApp(uint32_t evtType, void *evtData, uint32_t toTid);
163 bool osEnqueuePrivateEvtNew(uint16_t evtType, void *evtData,
164                                    void (*evtFreeCallback)(uint16_t eventType, void *eventData),
165                                    uint32_t toTid);
166 
167 bool osEnqueueEvt(uint32_t evtType, void *evtData, EventFreeF evtFreeF);
168 bool osEnqueueEvtOrFree(uint32_t evtType, void *evtData, EventFreeF evtFreeF);
169 bool osEnqueueEvtAsApp(uint32_t evtType, void *evtData, bool freeData);
170 void osRemovePendingEvents(bool (*match)(uint32_t evtType, const void *evtData, void *context), void *context);
171 
172 bool osDefer(OsDeferCbkF callback, void *cookie, bool urgent);
173 
174 bool osTidById(uint64_t *appId, uint32_t *tid);
175 bool osAppInfoById(uint64_t appId, uint32_t *appIdx, uint32_t *appVer, uint32_t *appSize);
176 bool osAppInfoByIndex(uint32_t appIdx, uint64_t *appId, uint32_t *appVer, uint32_t *appSize);
177 uint32_t osGetCurrentTid();
178 uint32_t osSetCurrentTid(uint32_t);
179 
180 struct AppHdr *osAppSegmentCreate(uint32_t size);
181 bool osAppSegmentClose(struct AppHdr *app, uint32_t segSize, uint32_t segState);
182 bool osAppSegmentSetState(const struct AppHdr *app, uint32_t segState);
183 bool osSegmentSetSize(struct Segment *seg, uint32_t size);
184 bool osAppWipeData(struct AppHdr *app);
185 struct Segment *osGetSegment(const struct AppHdr *app);
186 struct Segment *osSegmentGetEnd();
187 
osSegmentGetSize(const struct Segment * seg)188 static inline int32_t osSegmentGetSize(const struct Segment *seg)
189 {
190     return seg ? seg->size[0] | (seg->size[1] << 8) | (seg->size[2] << 16) : SEG_SIZE_INVALID;
191 }
192 
osSegmentGetState(const struct Segment * seg)193 static inline uint32_t osSegmentGetState(const struct Segment *seg)
194 {
195     return seg ? seg->state : SEG_STATE_INVALID;
196 }
197 
osSegmentGetData(const struct Segment * seg)198 static inline struct AppHdr *osSegmentGetData(const struct Segment *seg)
199 {
200     return (struct AppHdr*)(&seg[1]);
201 }
202 
203 #ifdef SEGMENT_CRC_SUPPORT
204 
205 struct SegmentFooter
206 {
207     uint32_t crc;
208 };
209 
210 #define FOOTER_SIZE sizeof(struct SegmentFooter)
211 #else
212 #define FOOTER_SIZE 0
213 #endif
214 
osSegmentSizeAlignedWithFooter(uint32_t size)215 static inline uint32_t osSegmentSizeAlignedWithFooter(uint32_t size)
216 {
217     return ((size + 3) & ~3) + FOOTER_SIZE;
218 }
219 
osSegmentSizeGetNext(const struct Segment * seg,uint32_t size)220 static inline const struct Segment *osSegmentSizeGetNext(const struct Segment *seg, uint32_t size)
221 {
222     struct Segment *next = (struct Segment *)(((uint8_t*)seg) +
223                                               osSegmentSizeAlignedWithFooter(size) +
224                                               sizeof(*seg)
225                                               );
226     return seg ? next : NULL;
227 }
228 
osSegmentGetNext(const struct Segment * seg)229 static inline const struct Segment *osSegmentGetNext(const struct Segment *seg)
230 {
231     return osSegmentSizeGetNext(seg, osSegmentGetSize(seg));
232 }
233 
osAppSegmentGetState(const struct AppHdr * app)234 static inline uint32_t osAppSegmentGetState(const struct AppHdr *app)
235 {
236     return osSegmentGetState(osGetSegment(app));
237 }
238 
239 struct SegmentIterator {
240     const struct Segment *shared;
241     const struct Segment *sharedEnd;
242     const struct Segment *seg;
243 };
244 
245 void osSegmentIteratorInit(struct SegmentIterator *it);
246 
osSegmentIteratorNext(struct SegmentIterator * it)247 static inline bool osSegmentIteratorNext(struct SegmentIterator *it)
248 {
249     const struct Segment *seg = it->shared;
250     const struct Segment *next = seg < it->sharedEnd ? osSegmentGetNext(seg) : it->sharedEnd;
251 
252     it->shared = next;
253     it->seg = seg;
254 
255     return seg < it->sharedEnd;
256 }
257 
258 bool osWriteShared(void *dest, const void *src, uint32_t len);
259 bool osEraseShared();
260 
261 //event retaining support
262 bool osRetainCurrentEvent(TaggedPtr *evtFreeingInfoP); //called from any apps' event handling to retain current event. Only valid for first app that tries. evtFreeingInfoP filled by call and used to free evt later
263 void osFreeRetainedEvent(uint32_t evtType, void *evtData, TaggedPtr *evtFreeingInfoP);
264 
265 uint32_t osExtAppStopApps(uint64_t appId);
266 uint32_t osExtAppEraseApps(uint64_t appId);
267 uint32_t osExtAppStartApps(uint64_t appId);
268 
269 bool osAppIsChre(uint16_t tid);
270 
271 /* Logging */
272 enum LogLevel {
273     LOG_ERROR = 'E',
274     LOG_WARN  = 'W',
275     LOG_INFO  = 'I',
276     LOG_DEBUG = 'D',
277 };
278 
279 void osLogv(char clevel, const char *str, va_list vl);
280 void osLog(enum LogLevel level, const char *str, ...) PRINTF_ATTRIBUTE(2, 3);
281 
282 #ifndef INTERNAL_APP_INIT
283 #define INTERNAL_APP_INIT(_id, _ver, _init, _end, _event)                               \
284 SET_INTERNAL_LOCATION(location, ".internal_app_init")static const struct AppHdr         \
285 SET_INTERNAL_LOCATION_ATTRIBUTES(used, section (".internal_app_init")) mAppHdr = {      \
286     .hdr.magic   = APP_HDR_MAGIC,                                                       \
287     .hdr.fwVer   = APP_HDR_VER_CUR,                                                     \
288     .hdr.fwFlags = FL_APP_HDR_INTERNAL | FL_APP_HDR_APPLICATION,                        \
289     .hdr.appId   = (_id),                                                               \
290     .hdr.appVer  = (_ver),                                                              \
291     .hdr.payInfoType = LAYOUT_APP,                                                      \
292     .vec.init    = (uint32_t)(_init),                                                   \
293     .vec.end     = (uint32_t)(_end),                                                    \
294     .vec.handle  = (uint32_t)(_event)                                                   \
295 }
296 #endif
297 
298 #ifndef APP_INIT
299 #define APP_INIT(_ver, _init, _end, _event)                                             \
300 extern const struct AppFuncs _mAppFuncs;                                                \
301 const struct AppFuncs SET_EXTERNAL_APP_ATTRIBUTES(used, section (".app_init"),          \
302 visibility("default")) _mAppFuncs = {                                                   \
303     .init   = (_init),                                                                  \
304     .end    = (_end),                                                                   \
305     .handle = (_event)                                                                  \
306 };                                                                                      \
307 const uint32_t SET_EXTERNAL_APP_VERSION(used, section (".app_version"),                 \
308 visibility("default")) _mAppVer = _ver
309 #endif
310 
311 
312 #ifdef __cplusplus
313 }
314 #endif
315 
316 #endif
317