-
Notifications
You must be signed in to change notification settings - Fork 4
/
bl31_apis.c
459 lines (378 loc) · 9.53 KB
/
bl31_apis.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
* Copyright (c) 2019 Amlogic, Inc. All rights reserved.
*/
/*
* Trustzone API
*/
#include <asm/arch/io.h>
#include <asm/arch/efuse.h>
#include <asm/cache.h>
#include <asm/arch/bl31_apis.h>
#include <amlogic/cpu_id.h>
#include <asm/arch/secure_apb.h>
static long sharemem_input_base;
static long sharemem_output_base;
long get_sharemem_info(unsigned long function_id)
{
asm volatile(
__asmeq("%0", "x0")
"smc #0\n"
: "+r" (function_id));
return function_id;
}
int32_t set_boot_params(const keymaster_boot_params *boot_params)
{
const unsigned cmd = SET_BOOT_PARAMS;
if (!boot_params)
return -1;
if (!sharemem_input_base)
sharemem_input_base =
get_sharemem_info(GET_SHARE_MEM_INPUT_BASE);
memcpy((void *)sharemem_input_base,
(const void *)boot_params, sizeof(keymaster_boot_params));
asm __volatile__("" : : : "memory");
register uint64_t x0 asm("x0") = cmd;
register uint64_t x1 asm("x1") = sizeof(keymaster_boot_params);
do {
asm volatile(
__asmeq("%0", "x0")
__asmeq("%1", "x0")
__asmeq("%2", "x1")
"smc #0\n"
: "=r"(x0)
: "r"(x0), "r"(x1));
} while (0);
return (!x0)? -1: 0;
}
#ifdef CONFIG_EFUSE
int32_t meson_trustzone_efuse(struct efuse_hal_api_arg *arg)
{
int ret;
unsigned cmd, offset, size;
unsigned long *retcnt = (unsigned long *)(arg->retcnt_phy);
if (!sharemem_input_base)
sharemem_input_base =
get_sharemem_info(GET_SHARE_MEM_INPUT_BASE);
if (!sharemem_output_base)
sharemem_output_base =
get_sharemem_info(GET_SHARE_MEM_OUTPUT_BASE);
if (arg->cmd == EFUSE_HAL_API_READ)
cmd = EFUSE_READ;
else if (arg->cmd == EFUSE_HAL_API_WRITE)
cmd = EFUSE_WRITE;
else
cmd = EFUSE_WRITE_PATTERN;
offset = arg->offset;
size = arg->size;
if (arg->cmd == EFUSE_HAL_API_WRITE)
memcpy((void *)sharemem_input_base,
(const void *)arg->buffer_phy, size);
asm __volatile__("" : : : "memory");
register uint64_t x0 asm("x0") = cmd;
register uint64_t x1 asm("x1") = offset;
register uint64_t x2 asm("x2") = size;
do {
asm volatile(
__asmeq("%0", "x0")
__asmeq("%1", "x0")
__asmeq("%2", "x1")
__asmeq("%3", "x2")
"smc #0\n"
: "=r"(x0)
: "r"(x0), "r"(x1), "r"(x2));
} while (0);
ret = x0;
*retcnt = x0;
if ((arg->cmd == EFUSE_HAL_API_READ) && (ret != 0))
memcpy((void *)arg->buffer_phy,
(const void *)sharemem_output_base, ret);
if (!ret)
return -1;
else
return 0;
}
int32_t meson_trustzone_efuse_get_max(struct efuse_hal_api_arg *arg)
{
int32_t ret;
unsigned cmd = 0;
if (arg->cmd == EFUSE_HAL_API_USER_MAX)
cmd = EFUSE_USER_MAX;
asm __volatile__("" : : : "memory");
register uint64_t x0 asm("x0") = cmd;
do {
asm volatile(
__asmeq("%0", "x0")
__asmeq("%1", "x0")
"smc #0\n"
: "=r"(x0)
: "r"(x0));
} while (0);
ret = x0;
if (!ret)
return -1;
else
return ret;
}
ssize_t meson_trustzone_efuse_writepattern(const char *buf, size_t count)
{
struct efuse_hal_api_arg arg;
unsigned long retcnt;
if (count != EFUSE_BYTES)
return 0; /* Past EOF */
arg.cmd = EFUSE_HAL_API_WRITE_PATTERN;
arg.offset = 0;
arg.size = count;
arg.buffer_phy = (unsigned long)buf;
arg.retcnt_phy = (unsigned long)&retcnt;
int ret;
ret = meson_trustzone_efuse(&arg);
return ret;
}
#endif
uint64_t meson_trustzone_efuse_check(unsigned char *addr)
{
uint64_t ret = 0;
struct sram_hal_api_arg arg = {};
arg.cmd = SRAM_HAL_API_CHECK_EFUSE;
arg.req_len = 0x1000000;
arg.res_len = 0;
arg.req_phy_addr = (unsigned long)addr;
arg.res_phy_addr = (unsigned long)NULL;
asm __volatile__("" : : : "memory");
register uint64_t x0 asm("x0") = CALL_TRUSTZONE_HAL_API;
register uint64_t x1 asm("x1") = TRUSTZONE_HAL_API_SRAM;
register uint64_t x2 asm("x2") = (unsigned long)(&arg);
do {
asm volatile(
__asmeq("%0", "x0")
__asmeq("%1", "x0")
__asmeq("%2", "x1")
__asmeq("%3", "x2")
"smc #0\n"
: "=r"(x0)
: "r"(x0), "r"(x1), "r"(x2));
} while (0);
ret = x0;
return ret;
}
void debug_efuse_cmd(unsigned long cmd)
{
asm volatile(
__asmeq("%0", "x0")
"smc #0\n"
: : "r" (cmd));
}
void bl31_debug_efuse_write_pattern(const char *buf)
{
if (!sharemem_input_base)
sharemem_input_base =
get_sharemem_info(GET_SHARE_MEM_INPUT_BASE);
memcpy((void *)sharemem_input_base, (const void *)buf, 512);
debug_efuse_cmd(DEBUG_EFUSE_WRITE_PATTERN);
}
void bl31_debug_efuse_read_pattern(char *buf)
{
if (!sharemem_output_base)
sharemem_output_base =
get_sharemem_info(GET_SHARE_MEM_OUTPUT_BASE);
debug_efuse_cmd(DEBUG_EFUSE_READ_PATTERN);
memcpy((void *)buf, (const void *)sharemem_output_base, 512);
}
void aml_set_jtag_state(unsigned state, unsigned select)
{
uint64_t command;
if (state == JTAG_STATE_ON)
command = JTAG_ON;
else
command = JTAG_OFF;
asm __volatile__("" : : : "memory");
asm volatile(
__asmeq("%0", "x0")
__asmeq("%1", "x1")
"smc #0\n"
: : "r" (command), "r"(select));
}
unsigned aml_get_reboot_reason(void)
{
unsigned reason;
uint64_t ret;
register uint64_t x0 asm("x0") = GET_REBOOT_REASON;
asm volatile(
__asmeq("%0", "x0")
"smc #0\n"
:"+r"(x0));
ret = x0;
reason = (unsigned)(ret&0xffffffff);
return reason;
}
void set_viu_probe_enable(void)
{
register uint64_t x0 asm("x0") = VIU_PREOBE_EN;
asm volatile(
__asmeq("%0", "x0")
"smc #0\n"
:"+r"(x0));
}
unsigned aml_reboot(uint64_t function_id, uint64_t arg0, uint64_t arg1, uint64_t arg2)
{
register long x0 asm("x0") = function_id;
register long x1 asm("x1") = arg0;
register long x2 asm("x2") = arg1;
register long x3 asm("x3") = arg2;
asm volatile(
__asmeq("%0", "x0")
__asmeq("%1", "x1")
__asmeq("%2", "x2")
__asmeq("%3", "x3")
"smc #0\n"
: "+r" (x0)
: "r" (x1), "r" (x2), "r" (x3));
return function_id;
}
void aml_set_reboot_reason(uint64_t function_id, uint64_t arg0, uint64_t arg1, uint64_t arg2)
{
register long x0 asm("x0") = function_id;
register long x1 asm("x1") = arg0;
register long x2 asm("x2") = arg1;
register long x3 asm("x3") = arg2;
asm volatile(
__asmeq("%0", "x0")
__asmeq("%1", "x1")
__asmeq("%2", "x2")
__asmeq("%3", "x3")
"smc #0\n"
: "+r" (x0)
: "r" (x1), "r" (x2), "r" (x3));
return ;
}
unsigned long aml_sec_boot_check(unsigned long nType,
unsigned long pBuffer,
unsigned long nLength,
unsigned long nOption)
{
uint64_t ret = 1;
//#define AML_SECURE_LOG_TE
#if defined(AML_SECURE_LOG_TE)
#define AML_GET_TE(a) do{a = *((volatile unsigned int*)0xc1109988);}while(0);
unsigned nT1,nT2,nT3;
#else
#define AML_GET_TE(...)
#endif
AML_GET_TE(nT1);
asm __volatile__("" : : : "memory");
register uint64_t x0 asm("x0") = AML_DATA_PROCESS;
register uint64_t x1 asm("x1") = nType;
register uint64_t x2 asm("x2") = pBuffer;
register uint64_t x3 asm("x3") = nLength;
register uint64_t x4 asm("x4") = nOption;
do {
asm volatile(
__asmeq("%0", "x0")
__asmeq("%1", "x0")
__asmeq("%2", "x1")
__asmeq("%3", "x2")
__asmeq("%4", "x3")
__asmeq("%5", "x4")
"smc #0\n"
: "=r"(x0)
: "r"(x0), "r"(x1), "r"(x2),"r"(x3),"r"(x4));
} while (0);
ret = x0;
AML_GET_TE(nT2);;
flush_dcache_range((unsigned long )pBuffer, (unsigned long )pBuffer+nLength);
AML_GET_TE(nT3);
#if defined(AML_SECURE_LOG_TE)
printf("aml log : dec use %d(us) , flush cache used %d(us)\n",
nT2 - nT1, nT3 - nT2);
#endif
return ret;
}
void set_usb_boot_function(unsigned long command)
{
register long x0 asm("x0") = SET_USB_BOOT_FUNC;
register long x1 asm("x1") = command;
asm volatile(
__asmeq("%0", "x0")
__asmeq("%1", "x1")
"smc #0\n"
: "+r" (x0)
: "r" (x1));
}
void aml_system_off(void)
{
/* TODO: Add poweroff capability */
aml_reboot(0x82000042, 1, 0, 0);
aml_reboot(0x84000008, 0, 0, 0);
}
int __get_chip_id(unsigned char *buff, unsigned int size)
{
if (buff == NULL || size < 16)
return -1;
if (!sharemem_output_base)
sharemem_output_base =
get_sharemem_info(GET_SHARE_MEM_OUTPUT_BASE);
if (sharemem_output_base) {
register long x0 asm("x0") = GET_CHIP_ID;
register long x1 asm("x1") = 2;
asm volatile(
__asmeq("%0", "x0")
__asmeq("%1", "x1")
"smc #0\n"
: "+r" (x0)
: "r" (x1));
if (x0 == 0) {
int version = *((unsigned int *)sharemem_output_base);
if (version == 2) {
memcpy(buff, (void *)sharemem_output_base + 4, 16);
}
else {
/**
* Legacy 12-byte chip ID read out, transform data
* to expected order format.
*/
uint32_t chip_info = readl(P_AO_SEC_SD_CFG8);
uint8_t *ch;
int i;
((uint32_t *)buff)[0] =
((chip_info & 0xff000000) | // Family ID
((chip_info << 8) & 0xff0000) | // Chip Revision
((chip_info >> 8) & 0xff00)); // Package ID
((uint32_t *)buff)[0] = htonl(((uint32_t *)buff)[0]);
/* Transform into expected order for display */
ch = (uint8_t *)(sharemem_output_base + 4);
for (i = 0; i < 12; i++)
buff[i + 4] = ch[11 - i];
}
return 0;
}
}
return -1;
}
int32_t get_avbkey_from_fip(uint8_t *buf, uint32_t buflen)
{
const unsigned cmd = GET_AVBKEY_FROM_FIP;
uint32_t retlen = 0;
uint32_t ret = 0;
if (!buf)
return -1;
if (!sharemem_output_base)
sharemem_output_base =
get_sharemem_info(GET_SHARE_MEM_OUTPUT_BASE);
asm __volatile__("" : : : "memory");
register uint64_t x0 asm("x0") = cmd;
do {
asm volatile(
__asmeq("%0", "x0")
__asmeq("%1", "x0")
"smc #0\n"
: "=r"(x0)
: "r"(x0));
} while (0);
if (!x0)
ret = -1;
retlen = x0;
if (ret != -1 && buflen >= retlen)
memcpy(buf, (const void *)sharemem_output_base, retlen);
return ret;
}