From 289fc24b3216c8a119bb47a5b0692cc6a74a43a8 Mon Sep 17 00:00:00 2001 From: wanggang26 Date: Mon, 5 Aug 2024 19:41:15 +0800 Subject: [PATCH] boot: fix potential bl jump to app failed issue After changing sp, following functions calling will result in unpredictable behavior in case of jumping Signed-off-by: wanggang26 --- boards/arm/imxrt/imxrt1064-evk/src/imxrt_boot_image.c | 10 ++++++---- boards/arm/nrf52/common/src/nrf52_boot_image.c | 10 ++++++---- boards/arm/nrf53/common/src/nrf53_boot_image.c | 10 ++++++---- boards/arm/nrf91/common/src/nrf91_boot_image.c | 10 ++++++---- boards/arm/samv7/common/src/sam_boot_image.c | 10 ++++++---- 5 files changed, 30 insertions(+), 20 deletions(-) diff --git a/boards/arm/imxrt/imxrt1064-evk/src/imxrt_boot_image.c b/boards/arm/imxrt/imxrt1064-evk/src/imxrt_boot_image.c index 830d1695bd..31e7e576aa 100644 --- a/boards/arm/imxrt/imxrt1064-evk/src/imxrt_boot_image.c +++ b/boards/arm/imxrt/imxrt1064-evk/src/imxrt_boot_image.c @@ -172,10 +172,12 @@ int board_boot_image(const char *path, uint32_t hdr_size) /* Set main and process stack pointers */ - __asm__ __volatile__("\tmsr msp, %0\n" : : "r" (vt.spr)); - setcontrol(0x00); - ARM_ISB(); - ((void (*)(void))vt.reset)(); + __asm__ __volatile__("\tmsr msp, %0\n" + "\tmsr control, %1\n" + "\tisb\n" + "\tmov pc, %2\n" + : + : "r" (vt.spr), "r" (0), "r" (vt.reset)); return 0; } diff --git a/boards/arm/nrf52/common/src/nrf52_boot_image.c b/boards/arm/nrf52/common/src/nrf52_boot_image.c index c102ea1039..d5fd7668c0 100644 --- a/boards/arm/nrf52/common/src/nrf52_boot_image.c +++ b/boards/arm/nrf52/common/src/nrf52_boot_image.c @@ -161,10 +161,12 @@ int board_boot_image(const char *path, uint32_t hdr_size) /* Set main and process stack pointers */ - __asm__ __volatile__("\tmsr msp, %0\n" : : "r" (vt.spr)); - setcontrol(0x00); - ARM_ISB(); - ((void (*)(void))vt.reset)(); + __asm__ __volatile__("\tmsr msp, %0\n" + "\tmsr control, %1\n" + "\tisb\n" + "\tmov pc, %2\n" + : + : "r" (vt.spr), "r" (0), "r" (vt.reset)); return 0; } diff --git a/boards/arm/nrf53/common/src/nrf53_boot_image.c b/boards/arm/nrf53/common/src/nrf53_boot_image.c index 0e9bdd30fa..6d96bc7a70 100644 --- a/boards/arm/nrf53/common/src/nrf53_boot_image.c +++ b/boards/arm/nrf53/common/src/nrf53_boot_image.c @@ -161,10 +161,12 @@ int board_boot_image(const char *path, uint32_t hdr_size) /* Set main and process stack pointers */ - __asm__ __volatile__("\tmsr msp, %0\n" : : "r" (vt.spr)); - setcontrol(0x00); - ARM_ISB(); - ((void (*)(void))vt.reset)(); + __asm__ __volatile__("\tmsr msp, %0\n" + "\tmsr control, %1\n" + "\tisb\n" + "\tmov pc, %2\n" + : + : "r" (vt.spr), "r" (0), "r" (vt.reset)); return 0; } diff --git a/boards/arm/nrf91/common/src/nrf91_boot_image.c b/boards/arm/nrf91/common/src/nrf91_boot_image.c index 203332edaf..119fc64aa7 100644 --- a/boards/arm/nrf91/common/src/nrf91_boot_image.c +++ b/boards/arm/nrf91/common/src/nrf91_boot_image.c @@ -183,10 +183,12 @@ int board_boot_image(const char *path, uint32_t hdr_size) /* Set main and process stack pointers */ - __asm__ __volatile__("\tmsr msp, %0\n" : : "r" (vt.spr)); - setcontrol(0x00); - ARM_ISB(); - ((void (*)(void))vt.reset)(); + __asm__ __volatile__("\tmsr msp, %0\n" + "\tmsr control, %1\n" + "\tisb\n" + "\tmov pc, %2\n" + : + : "r" (vt.spr), "r" (0), "r" (vt.reset)); #else /* Non-secure entry point */ diff --git a/boards/arm/samv7/common/src/sam_boot_image.c b/boards/arm/samv7/common/src/sam_boot_image.c index e6934a555c..8a3543d158 100644 --- a/boards/arm/samv7/common/src/sam_boot_image.c +++ b/boards/arm/samv7/common/src/sam_boot_image.c @@ -178,10 +178,12 @@ int board_boot_image(const char *path, uint32_t hdr_size) /* Set main and process stack pointers */ - __asm__ __volatile__("\tmsr msp, %0\n" : : "r" (vt.spr)); - setcontrol(0x00); - ARM_ISB(); - ((void (*)(void))vt.reset)(); + __asm__ __volatile__("\tmsr msp, %0\n" + "\tmsr control, %1\n" + "\tisb\n" + "\tmov pc, %2\n" + : + : "r" (vt.spr), "r" (0), "r" (vt.reset)); return 0; }