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 _NANOHUB_NANOHUB_H_
18 #define _NANOHUB_NANOHUB_H_
19 
20 #include <inttypes.h>
21 #include <nanohub/aes.h>
22 
23 /* this file is collection of nanohub-related definitions shared between multiple parties,
24  * including but not limited to: HAL, Kernel, utilities, nanohub FW
25  * it provides minimum details on nanohub implementation, necessary to reliably identify it, and
26  * generate/parse compatible images
27  */
28 
29 #define NANOHUB_OS_PATCH_LEVEL  0x0000
30 
31 #define NANOHUB_VENDOR_GOOGLE      UINT64_C(0x476F6F676C) // "Googl"
32 #define NANOHUB_VENDOR_STMICRO     UINT64_C(0x53544d6963) // "STMic"
33 #define NANOHUB_VENDOR_INVENSENSE  UINT64_C(0x496E76656E) // "Inven"
34 
35 #define NANOAPP_SIGNED_FLAG    0x1  // contents is signed with one or more signature block(s)
36 #define NANOAPP_ENCRYPTED_FLAG 0x2  // contents is encrypted with exactly one encryption key
37 
38 #define NANOAPP_AOSP_MAGIC (((uint32_t)'N' <<  0) | ((uint32_t)'A' <<  8) | ((uint32_t)'N' << 16) | ((uint32_t)'O' << 24))
39 #define NANOAPP_FW_MAGIC (((uint32_t)'N' <<  0) | ((uint32_t)'B' <<  8) | ((uint32_t)'I' << 16) | ((uint32_t)'N' << 24))
40 #define GOOGLE_LAYOUT_MAGIC (((uint32_t)'G' <<  0) | ((uint32_t)'o' <<  8) | ((uint32_t)'o' << 16) | ((uint32_t)'g' << 24))
41 
42 #define APP_ID_ANY                 UINT64_C(0xFFFFFFFFFFFFFFFF)
43 #define APP_VENDOR_ANY             UINT64_C(0xFFFFFFFFFF)
44 #define APP_VENDOR_SHF             (24)
45 #define APP_SEQ_ID_ANY             UINT32_C(0xFFFFFF)
46 #define APP_ID_GET_VENDOR(appid)   ((appid) >> APP_VENDOR_SHF)
47 #define APP_ID_GET_SEQ_ID(appid)   ((appid) & APP_SEQ_ID_ANY)
48 #define APP_ID_MAKE(vendor, app)   ((((uint64_t)(vendor)) << APP_VENDOR_SHF) | ((app) & APP_SEQ_ID_ANY))
49 
50 #ifndef CONTEXT_HUB_H
51 // The binary format below is in little endian format; borrowed from CONTEXT_HUB_H
52 struct nano_app_binary_t {
53     uint32_t header_version;       // 0x1 for this version
54     uint32_t magic;                // "NANO"
55     uint64_t app_id;               // App Id contains vendor id
56     uint32_t app_version;          // Version of the app
57     uint32_t flags;                // Signed, encrypted
58     uint64_t hw_hub_type;          // which hub type is this compiled for
59     uint8_t  chre_api_major;       // Which CHRE API version this is compiled for
60     uint8_t  chre_api_minor;
61     uint8_t  reserved[6];          // Should be all zeroes
62     uint8_t  custom_binary[0];     // start of custom binary data
63 };
64 
65 #endif
66 
67 struct HostMsgHdr {
68     uint32_t eventId;
69     uint64_t appId;
70     uint8_t len;
71 } __attribute__((packed));
72 
73 struct HostMsgHdrChreV10 {
74     uint32_t eventId;
75     uint64_t appId;
76     uint8_t len;
77     uint32_t appEventId;
78 } __attribute__((packed));
79 
80 struct HostMsgHdrChre {
81     uint32_t eventId;
82     uint64_t appId;
83     uint8_t len;
84     uint32_t appEventId;
85     uint16_t endpoint;
86 } __attribute__((packed));
87 
88 // we translate AOSP header into FW header: this header is in LE format
89 // please maintain natural alignment for every field (matters to Intel; otherwise is has to be declared as packed)
90 struct FwCommonHdr {
91     uint32_t magic;         // external & internal: NANOAPP_FW_MAGIC
92     uint16_t fwVer;         // external & internal: set to 1; header version
93     uint16_t fwFlags;       // external & internal: class : EXTERNAL/INTERNAL, EXEC/NOEXEC, APP/KERNEL/EEDATA/...
94     uint64_t appId;         // external: copy from AOSP header; internal: defined locally
95     uint32_t appVer;        // external: copy from AOSP header; internal: defined locally
96     uint8_t  payInfoType;   // external: copy ImageLayout::payload; internal: LAYOUT_APP
97     uint8_t  payInfoSize;   // sizeof(PayloadInfo) for this payload type
98     uint8_t  chreApiMajor;  // Chre Api Major Version (or 0xFF for non-chre nanoapps)
99     uint8_t  chreApiMinor;  // Chre Api Minor Version (or 0xFF for non-chre nanoapps)
100 };
101 
102 struct SectInfo {
103     uint32_t data_start;
104     uint32_t data_end;
105     uint32_t data_data;
106 
107     uint32_t bss_start;
108     uint32_t bss_end;
109 
110     uint32_t got_start;
111     uint32_t got_end;
112     uint32_t rel_start;
113     uint32_t rel_end;
114 };
115 
116 // this is platform-invariant version of struct TaskFuncs (from seos.h)
117 struct AppVectors {
118     uint32_t init;
119     uint32_t end;
120     uint32_t handle;
121 };
122 
123 #define FLASH_RELOC_OFFSET offsetof(struct AppHdr, sect)        // used by appSupport.c at run time
124 #define BINARY_RELOC_OFFSET offsetof(struct BinHdr, sect)       // used by postprocess at build time
125 
126 struct BinCommonHdr {
127     uint32_t magic;
128     uint32_t appVer;
129 };
130 
131 // binary nanoapp image (.bin) produced by objcopy starts with this binary header (LE)
132 struct BinHdr {
133     struct BinCommonHdr hdr;
134     struct SectInfo     sect;
135     struct AppVectors   vec;
136 };
137 
138 // FW nanoapp image starts with this binary header (LE) in flash
139 struct AppHdr {
140     struct FwCommonHdr hdr;
141     struct SectInfo    sect;
142     struct AppVectors  vec;
143 };
144 
145 struct AppSecSignHdr {
146     uint32_t appDataLen;
147 };
148 
149 struct AppSecEncrHdr {
150     uint64_t keyID;
151     uint32_t dataLen;
152     uint32_t IV[AES_BLOCK_WORDS];
153 };
154 
155 #define LAYOUT_APP  1
156 #define LAYOUT_KEY  2
157 #define LAYOUT_OS   3
158 #define LAYOUT_DATA 4
159 
160 struct ImageLayout {
161     uint32_t magic;     // Layout ID: (GOOGLE_LAYOUT_MAGIC for this implementation)
162     uint8_t  version;   // layout version
163     uint8_t  payload;   // type of payload: APP, SECRET KEY, OS IMAGE, USER DATA, ...
164     uint16_t flags;     // layout flags: extra options for certain payload types; payload-specific
165 };
166 
167 // .napp image starts with this binary header (LE)
168 // it is optionally followed by AppSecSignHdr and/or AppSecEncrHdr
169 // all of the above are included in signing hash, but never encrypted
170 // encryption (if enabled) starts immediately after those
171 struct ImageHeader {
172     struct nano_app_binary_t aosp;
173     struct ImageLayout   layout;
174 };
175 
176 #define CKK_RSA 0x00
177 #define CKK_AES 0x1F
178 
179 #define CKO_PUBLIC_KEY  0x02
180 #define CKO_PRIVATE_KEY 0x03
181 #define CKO_SECRET_KEY  0x04
182 
183 // flags
184 #define FL_KI_ENFORCE_ID 0x0001  // if set, size, key_type, obj_type must be valid
185 
186 // payload header format: LAYOUT_KEY
187 struct KeyInfo {
188     union {
189         struct {
190             uint16_t id;        // arbitrary number, != 0, equivalent of PKCS#11 name
191             uint16_t flags;     // key flags (additional PKCS#11 attrs, unused for now; must be 0)
192             uint16_t size;      // key size in bits
193             uint8_t  key_type;  // 8 LSB of PKCS-11 CKK_<KEY TYPE>
194             uint8_t  obj_type;  // 8 LSB of PKCS-11 CKO_<OBJ TYPE>
195         };
196         uint64_t data;          // complete 64-bit key-id, unique within this APP namespace (complete id is <APP_ID | KEY_INFO> 128 bits)
197     };
198 };
199 
200 #define AES_KEY_ID(_id) (((struct KeyInfo){ .key_type = CKK_AES, .obj_type = CKO_SECRET_KEY, .size = 256, .id = (_id) }).data)
201 
202 // payload header format: LAYOUT_APP
203 struct AppInfo {
204     struct SectInfo   sect;
205     struct AppVectors vec;
206 };
207 
208 #define OS_UPDT_MARKER_INPROGRESS     0xFF
209 #define OS_UPDT_MARKER_DOWNLOADED     0xFE
210 #define OS_UPDT_MARKER_VERIFIED       0xF0
211 #define OS_UPDT_MARKER_INVALID        0x00
212 #define OS_UPDT_MAGIC                 "Nanohub OS" //11 bytes incl terminator
213 
214 // payload header format: LAYOUT_OS
215 struct OsUpdateHdr {
216     char magic[11];
217     uint8_t marker; //OS_UPDT_MARKER_INPROGRESS -> OS_UPDT_MARKER_DOWNLOADED -> OS_UPDT_MARKER_VERIFIED / OS_UPDT_INVALID
218     uint32_t size;  //does not include the mandatory signature (using device key) that follows
219 };
220 
221 // payload header format: LAYOUT_DATA
222 struct DataInfo {
223     uint32_t id;
224     uint32_t size;
225 };
226 
227 #endif // _NANOHUB_NANOHUB_H_
228