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