Skip to content

Commit dd0619a

Browse files
committed
sunxi-fel: handle H616/A133 secure-FEL handoff
On secure-boot H616 and A133 devices, FEL starts in non-secure state. A direct SMC alone does not leave the BROM command loop in secure SVC, and A133 NBROM also clears the monitor vector table. Add a thunk which temporarily patches the writable SRAM SMC vector at MVBAR + 8 with "mov pc, lr". After SMC, this branches back to the thunk without restoring SPSR_mon, leaving execution in monitor mode. Keep SVC SP, LR and interrupt state in unbanked, caller-clobbered registers. Restore the original vector word, clear SCR and MVBAR, and set up the secure GIC view before switching to SVC and restoring SP/LR and the interrupt masks. No SPL workspace or private stack is needed. Require the MMU and caches to be disabled before invoking the thunk, and check the runtime probe again on return to detect failure. Gate the workaround on the secure boot status word at SID base + 0xa0. Use EFUSE_LCJS at efuse offset 0x48 as the restricted-memory probe: bit 11 is the secure-boot fuse, so the secure view is non-zero when secure boot is enabled. This avoids relying on a non-zero value in the write-protection bitmap at offset 0x40. Both probes are read-only. Apply the workaround at startup, leaving the existing SID and SPL paths unchanged. Once LCJS becomes readable, its non-zero value suppresses repeat application, including across separate sunxi-fel invocations. Normal-boot devices are excluded by the secure boot status check. Tested on a secure-boot H616 with repeated cold boots, full SID reads, separate FEL invocations, and a complete SPL/U-Boot/Linux FIT upload. U-Boot verified the FIT hashes and reached "Starting kernel ...". A133 has not been hardware-tested with this thunk. Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
1 parent 4ac3bf1 commit dd0619a

8 files changed

Lines changed: 201 additions & 24 deletions

File tree

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ SOC_INFO := soc_info.c soc_info.h
164164
FEL_LIB := fel_lib.c fel_lib.h
165165
SPI_FLASH:= fel-spiflash.c fel-spiflash.h fel-remotefunc-spi-data-transfer.h
166166

167-
sunxi-fel: fel.c fit_image.c thunks/fel-to-spl-thunk.h $(PROGRESS) $(SOC_INFO) $(FEL_LIB) $(SPI_FLASH)
167+
sunxi-fel: fel.c fit_image.c thunks/fel-to-spl-thunk.h thunks/fel-to-secure-svc-smc-thunk.h $(PROGRESS) $(SOC_INFO) $(FEL_LIB) $(SPI_FLASH)
168168
$(CC) $(HOST_CFLAGS) $(LIBUSB_CFLAGS) $(ZLIB_CFLAGS) $(LIBFDT_CFLAGS) $(LDFLAGS) -o $@ \
169169
$(filter %.c,$^) $(LIBS) $(LIBUSB_LIBS) $(ZLIB_LIBS) $(LIBFDT_LIBS)
170170

‎fel.c‎

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,10 @@ static uint32_t fel_to_spl_thunk[] = {
358358
#include "thunks/fel-to-spl-thunk.h"
359359
};
360360

361+
static uint32_t fel_to_secure_svc_smc_thunk[] = {
362+
#include "thunks/fel-to-secure-svc-smc-thunk.h"
363+
};
364+
361365
#define DRAM_BASE 0x40000000
362366
#define DRAM_SIZE 0x80000000
363367

@@ -569,38 +573,89 @@ static bool aw_fel_needs_smc_workaround(feldev_handle *dev)
569573
soc_info_t *soc_info = dev->soc_info;
570574
uint32_t val;
571575

572-
/* Return if the SoC does not need this workaround */
576+
if (soc_info->secure_boot_status_offset) {
577+
if (!soc_info->sid_base)
578+
return false;
579+
aw_fel_read(dev, soc_info->sid_base +
580+
soc_info->secure_boot_status_offset,
581+
&val, sizeof(val));
582+
if (!le32toh(val))
583+
return false;
584+
}
585+
573586
if (!soc_info->needs_smc_workaround_if_zero_word_at_addr)
574587
return false;
575-
576-
/* This has less overhead than fel_readl_n() and may be good enough */
577588
aw_fel_read(dev, soc_info->needs_smc_workaround_if_zero_word_at_addr,
578-
&val, sizeof(val));
589+
&val, sizeof(val));
579590

580-
return val == 0;
591+
return le32toh(val) == 0;
592+
}
593+
594+
static void aw_fel_execute_secure_svc_smc_thunk(feldev_handle *dev)
595+
{
596+
soc_info_t *soc_info = dev->soc_info;
597+
const secure_svc_smc_info *smc_info = soc_info->secure_svc_smc;
598+
struct {
599+
uint32_t code[ARRAY_SIZE(fel_to_secure_svc_smc_thunk)];
600+
uint32_t monitor_vector_addr;
601+
uint32_t gicc_base;
602+
uint32_t gicd_base;
603+
} thunk;
604+
size_t i;
605+
606+
if (!smc_info)
607+
pr_fatal("FEL thunk: missing secure-SVC SMC info\n");
608+
609+
if (sizeof(thunk) > soc_info->thunk_size)
610+
pr_fatal("FEL thunk: bad size (need %zu, have %u)\n",
611+
sizeof(thunk), soc_info->thunk_size);
612+
613+
if (aw_get_sctlr(dev, soc_info) & ((1 << 12) | (1 << 2) | 1))
614+
pr_fatal("Secure-SVC workaround requires MMU and caches disabled\n");
615+
616+
for (i = 0; i < ARRAY_SIZE(thunk.code); i++)
617+
thunk.code[i] = htole32(fel_to_secure_svc_smc_thunk[i]);
618+
thunk.monitor_vector_addr = htole32(smc_info->monitor_vector_addr);
619+
thunk.gicc_base = htole32(smc_info->gicc_base);
620+
thunk.gicd_base = htole32(smc_info->gicd_base);
621+
622+
aw_fel_write(dev, &thunk, soc_info->thunk_addr, sizeof(thunk));
623+
aw_fel_execute(dev, soc_info->thunk_addr);
624+
625+
if (aw_fel_needs_smc_workaround(dev))
626+
pr_fatal("SMC workaround failed to enter secure state\n");
581627
}
582628

583629
/*
584-
* Issue a "smc #0" instruction. This brings a SoC booted in "secure boot"
585-
* state from the default non-secure FEL into secure FEL.
630+
* Apply the "smc #0" workaround. This moves a secure-boot FEL session from
631+
* the default non-secure state into secure state.
586632
* This crashes on devices using "non-secure boot", as the BROM does not
587633
* provide a handler address in MVBAR. So we have a runtime check.
634+
* Some newer SoCs need to perform the SMC and return to FEL via a thunk,
635+
* which handles the monitor-to-SVC transition details.
588636
*/
589637
static void aw_apply_smc_workaround(feldev_handle *dev)
590638
{
591639
soc_info_t *soc_info = dev->soc_info;
592-
uint32_t arm_code[] = {
593-
htole32(0xe1600070), /* smc #0 */
594-
htole32(0xe12fff1e), /* bx lr */
595-
};
596640

597-
/* Return if the workaround is not needed or has been already applied */
641+
/* Return if the workaround is not needed */
598642
if (!aw_fel_needs_smc_workaround(dev))
599643
return;
600644

601-
pr_info("Applying SMC workaround... ");
602-
aw_fel_write(dev, arm_code, soc_info->scratch_addr, sizeof(arm_code));
603-
aw_fel_execute(dev, soc_info->scratch_addr);
645+
if (soc_info->smc_workaround == SMC_WORKAROUND_SECURE_SVC_SMC_THUNK) {
646+
pr_info("Applying SMC workaround via secure-SVC SMC thunk... ");
647+
aw_fel_execute_secure_svc_smc_thunk(dev);
648+
} else {
649+
uint32_t arm_code[] = {
650+
htole32(0xe1600070), /* smc #0 */
651+
htole32(0xe12fff1e), /* bx lr */
652+
};
653+
654+
pr_info("Applying SMC workaround... ");
655+
aw_fel_write(dev, arm_code, soc_info->scratch_addr,
656+
sizeof(arm_code));
657+
aw_fel_execute(dev, soc_info->scratch_addr);
658+
}
604659
pr_info(" done.\n");
605660
}
606661

@@ -1388,7 +1443,7 @@ int main(int argc, char **argv)
13881443
*/
13891444
handle = feldev_open(busnum, devnum, AW_USB_VENDOR_ID, AW_USB_PRODUCT_ID);
13901445

1391-
/* Some SoCs need the SMC workaround to enter the secure boot mode */
1446+
/* Some SoCs need the SMC workaround to enter secure state */
13921447
aw_apply_smc_workaround(handle);
13931448

13941449
/* Handle command-style arguments, in order of appearance */

‎soc_info.c‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ sram_swap_buffers a133_sram_swap_buffers[] = {
156156
{ .size = 0 } /* End of the table */
157157
};
158158

159+
static const secure_svc_smc_info ncat_gicv2_secure_svc_smc = {
160+
.monitor_vector_addr = 0x000300c0,
161+
.gicc_base = 0x03022000,
162+
.gicd_base = 0x03021000,
163+
};
164+
159165
/*
160166
* R329 has no SRAM A1, but a huge SRAM A2 at 0x100000. SPL and BROM uses
161167
* this SRAM A2's first part like how other SoCs use SRAM A1. The sp and
@@ -565,6 +571,11 @@ soc_info_t soc_info_table[] = {
565571
.rvbar_reg = 0x09010040,
566572
.rvbar_reg_alt= 0x08100040,
567573
.ver_reg = 0x03000024,
574+
/* EFUSE_LCJS: secure boot sets bit 11. */
575+
.needs_smc_workaround_if_zero_word_at_addr = 0x03006248,
576+
.secure_boot_status_offset = 0xa0,
577+
.smc_workaround = SMC_WORKAROUND_SECURE_SVC_SMC_THUNK,
578+
.secure_svc_smc = &ncat_gicv2_secure_svc_smc,
568579
.watchdog = &wd_h6_compat,
569580
},{
570581
.soc_id = 0x1851, /* Allwinner R329 */
@@ -644,8 +655,11 @@ soc_info_t soc_info_table[] = {
644655
.sid_offset = 0x200,
645656
.sid_sections = generic_2k_sid_maps,
646657
.rvbar_reg = 0x08100040,
647-
.needs_smc_workaround_if_zero_word_at_addr = 0x100004,
648-
.smc_workaround = SMC_WORKAROUND_DIRECT_SMC,
658+
/* EFUSE_LCJS: secure boot sets bit 11. */
659+
.needs_smc_workaround_if_zero_word_at_addr = 0x03006248,
660+
.secure_boot_status_offset = 0xa0,
661+
.smc_workaround = SMC_WORKAROUND_SECURE_SVC_SMC_THUNK,
662+
.secure_svc_smc = &ncat_gicv2_secure_svc_smc,
649663
.watchdog = &wd_h6_compat,
650664
},{
651665
.swap_buffers = NULL /* End of the table */

‎soc_info.h‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,30 @@ typedef struct {
108108
* be accessible from non-secure world.
109109
* - No RMR trigger on ARMv8 cores to bring the core into AArch64.
110110
* On older SoCs, a simple "smc" call returns with the NS bit cleared,
111-
* so access to all secure peripherals is suddenly possible.
111+
* so access to all secure peripherals is suddenly possible. Newer SoCs
112+
* may need a secure-SVC thunk to handle the monitor-to-SVC transition
113+
* after the SMC call before returning to FEL.
112114
* The 'needs_smc_workaround_if_zero_word_at_addr' field can be used to
113115
* have a check for this condition (reading from restricted addresses
114116
* typically returns zero) and then activate the SMC workaround if needed.
117+
* The 'secure_boot_status_offset' field can be used when the SoC has
118+
* a readable secure boot status register at sid_base + offset. It is a
119+
* gate only; a separate runtime state probe is still required to avoid
120+
* applying the workaround on every invocation.
115121
* The 'smc_workaround' field selects how to apply the workaround once the
116122
* runtime checks say that it is needed.
117123
*/
118124
typedef enum {
119125
SMC_WORKAROUND_DIRECT_SMC,
126+
SMC_WORKAROUND_SECURE_SVC_SMC_THUNK,
120127
} smc_workaround_t;
121128

129+
typedef struct {
130+
uint32_t monitor_vector_addr;
131+
uint32_t gicc_base;
132+
uint32_t gicd_base;
133+
} secure_svc_smc_info;
134+
122135
typedef struct {
123136
uint32_t soc_id; /* ID of the SoC */
124137
const char *name; /* human-readable SoC name string */
@@ -140,8 +153,11 @@ typedef struct {
140153
bool icache_fix;
141154
/* Use SMC workaround (enter secure mode) if can't read from this address */
142155
uint32_t needs_smc_workaround_if_zero_word_at_addr;
156+
/* Require non-zero secure boot status at sid_base + offset */
157+
uint32_t secure_boot_status_offset;
143158
/* How to apply the SMC workaround */
144159
smc_workaround_t smc_workaround;
160+
const secure_svc_smc_info *secure_svc_smc;
145161
uint32_t sram_size; /* Usable contiguous SRAM at spl_addr */
146162
sram_swap_buffers *swap_buffers;
147163
} soc_info_t;

‎thunks/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#
44

55
SPL_THUNK := fel-to-spl-thunk.h
6+
SPL_THUNK += fel-to-secure-svc-smc-thunk.h
67
THUNKS := clrsetbits.h
78
THUNKS += memcpy.h
89
THUNKS += readl_writel.h

‎thunks/README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ usually via `sunxi-fel`.
88

99
Normally you don't need to change or (re)build anything within this folder.
1010
Currently our main build process (via the parent directory's _Makefile_)
11-
only includes `fel-to-spl-thunk.h` directly. Other _.h_ files are provided
12-
**just for reference**. The main purpose of this folder is simply keeping
13-
track of _.S_ sources, to help with possible future maintenance of the
14-
various code snippets.
11+
includes `fel-to-spl-thunk.h` and `fel-to-secure-svc-smc-thunk.h` directly.
12+
Other _.h_ files are provided **just for reference**. The main purpose of this
13+
folder is simply keeping track of _.S_ sources, to help with possible future
14+
maintenance of the various code snippets.
1515

1616
Please note that any files lacking explicit license information are intended
1717
to be covered by the project's [overall license](../LICENSE.md) (GPLv2).
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* Enter secure SVC from FEL, with the MMU and caches disabled. */
2+
3+
.arch armv7-a
4+
.arch_extension sec
5+
.arm
6+
7+
entry_point:
8+
/* Keep CPSR and SVC LR in unbanked registers across the SMC. */
9+
mrs r12, cpsr
10+
cpsid if
11+
mov r3, lr
12+
13+
/* Return from the SMC vector without restoring SPSR_mon. */
14+
ldr r0, appended_data
15+
ldr r1, [r0, #8]
16+
ldr r2, monitor_return
17+
str r2, [r0, #8]
18+
dsb sy
19+
isb sy
20+
mov r2, sp
21+
smc #0
22+
23+
/* Now in monitor mode; restore the vector and select secure state. */
24+
str r1, [r0, #8]
25+
mov r0, #0
26+
mcr p15, 0, r0, c1, c1, 0 /* SCR */
27+
mcr p15, 0, r0, c12, c0, 1 /* MVBAR: use the ROM vectors */
28+
isb sy
29+
30+
/* Enable the secure GIC view needed by the BROM's FEL loop. */
31+
ldr r0, appended_data + 4
32+
mov r1, #0xf
33+
str r1, [r0] /* GICC_CTLR */
34+
mov r1, #0xf8
35+
str r1, [r0, #4] /* GICC_PMR */
36+
ldr r0, appended_data + 8
37+
mov r1, #3
38+
str r1, [r0] /* GICD_CTLR */
39+
dsb sy
40+
41+
/* Restore the SVC registers before restoring the interrupt masks. */
42+
cps #0x13
43+
mov sp, r2
44+
mov lr, r3
45+
msr cpsr_c, r12
46+
bx lr
47+
48+
monitor_return:
49+
mov pc, lr /* Instruction copied to MVBAR + 8 */
50+
51+
appended_data:
52+
/*
53+
* The host appends these parameters:
54+
*
55+
* uint32_t monitor_vector_addr;
56+
* uint32_t gicc_base;
57+
* uint32_t gicd_base;
58+
*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* <entry_point>: */
2+
0xe10fc000, /* 0: mrs ip, CPSR */
3+
0xf10c00c0, /* 4: cpsid if */
4+
0xe1a0300e, /* 8: mov r3, lr */
5+
0xe59f0068, /* c: ldr r0, [pc, #104] */
6+
0xe5901008, /* 10: ldr r1, [r0, #8] */
7+
0xe59f205c, /* 14: ldr r2, [pc, #92] */
8+
0xe5802008, /* 18: str r2, [r0, #8] */
9+
0xf57ff04f, /* 1c: dsb sy */
10+
0xf57ff06f, /* 20: isb sy */
11+
0xe1a0200d, /* 24: mov r2, sp */
12+
0xe1600070, /* 28: smc 0 */
13+
0xe5801008, /* 2c: str r1, [r0, #8] */
14+
0xe3a00000, /* 30: mov r0, #0 */
15+
0xee010f11, /* 34: mcr 15, 0, r0, cr1, cr1, {0} */
16+
0xee0c0f30, /* 38: mcr 15, 0, r0, cr12, cr0, {1} */
17+
0xf57ff06f, /* 3c: isb sy */
18+
0xe59f0038, /* 40: ldr r0, [pc, #56] */
19+
0xe3a0100f, /* 44: mov r1, #15 */
20+
0xe5801000, /* 48: str r1, [r0] */
21+
0xe3a010f8, /* 4c: mov r1, #248 */
22+
0xe5801004, /* 50: str r1, [r0, #4] */
23+
0xe59f0028, /* 54: ldr r0, [pc, #40] */
24+
0xe3a01003, /* 58: mov r1, #3 */
25+
0xe5801000, /* 5c: str r1, [r0] */
26+
0xf57ff04f, /* 60: dsb sy */
27+
0xf1020013, /* 64: cps #19 */
28+
0xe1a0d002, /* 68: mov sp, r2 */
29+
0xe1a0e003, /* 6c: mov lr, r3 */
30+
0xe121f00c, /* 70: msr CPSR_c, ip */
31+
0xe12fff1e, /* 74: bx lr */
32+
/* <monitor_return>: */
33+
0xe1a0f00e, /* 78: mov pc, lr */

0 commit comments

Comments
 (0)