Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code to check for valid CLR image and launch it #155

Merged
merged 1 commit into from
Mar 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions targets/CMSIS-OS/ChibiOS/Include/LaunchCLR.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define _LAUNCHCLR_H_

void LaunchCLR(uint32_t address);
bool CheckValidCLRImage(uint32_t address);

#endif //_LAUNCHCLR_H_

32 changes: 15 additions & 17 deletions targets/CMSIS-OS/ChibiOS/ST_NUCLEO144_F746ZG/nanoBooter/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ int main(void) {
// and performs the board-specific initializations.
halInit();

// the following IF is not mandatory, it's just providing a way for a user to 'force'
// the board to remain in nanoBooter and not launching nanoCLR

// if the USER button (blue one) is pressed, skip the check for a valid CLR image and remain in booter
if (palReadPad(GPIOC, GPIOC_BUTTON))
{
// check for valid CLR image
if(CheckValidCLRImage(0x08008000))
{
// there seems to be a valid CLR image
// launch nanoCLR
LaunchCLR(0x08008000);
}
}

// The kernel is initialized but not started yet, this means that
// main() is executing with absolute priority but interrupts are already enabled.
osKernelInitialize();
Expand All @@ -81,23 +96,6 @@ int main(void) {

// Normal main() thread
while (true) {

// check for button pressed
if (palReadPad(GPIOC, GPIOC_BUTTON))
{
// Start the shutdown sequence

// terminate threads
osThreadTerminate(receiverThreadId);
osThreadTerminate(blinkerThreadId);

// stop the serial-over-USB CDC driver
sduStop(&SDU1);

// launch nanoCLR
LaunchCLR(0x08008000);
}

osDelay(500);
}
}
33 changes: 15 additions & 18 deletions targets/CMSIS-OS/ChibiOS/ST_NUCLEO_F091RC/nanoBooter/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ int main(void) {
// and performs the board-specific initializations.
halInit();

// the following IF is not mandatory, it's just providing a way for a user to 'force'
// the board to remain in nanoBooter and not launching nanoCLR

// if the USER button (blue one) is pressed, skip the check for a valid CLR image and remain in booter
if (palReadPad(GPIOC, GPIOC_BUTTON))
{
// check for valid CLR image
if(CheckValidCLRImage(0x08004000))
{
// there seems to be a valid CLR image
// launch nanoCLR
LaunchCLR(0x08004000);
}
}

// The kernel is initialized but not started yet, this means that
// main() is executing with absolute priority but interrupts are already enabled.
osKernelInitialize();
Expand All @@ -60,24 +75,6 @@ int main(void) {

// Normal main() thread
while (true) {

// check for button pressed
if (!palReadPad(GPIOC, GPIOC_BUTTON))
{
// Start the shutdown sequence

// terminate threads
osThreadTerminate(receiverThreadId);
osThreadTerminate(blinkerThreadId);

// stop serial driver 2
sdStop(&SD2);

// launch nanoCLR
LaunchCLR(0x08004000);
}

osDelay(500);
}
}

32 changes: 15 additions & 17 deletions targets/CMSIS-OS/ChibiOS/ST_STM32F429I_DISCOVERY/nanoBooter/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ int main(void) {
// and performs the board-specific initializations.
halInit();

// the following IF is not mandatory, it's just providing a way for a user to 'force'
// the board to remain in nanoBooter and not launching nanoCLR

// if the USER button (blue one) is pressed, skip the check for a valid CLR image and remain in booter
if (palReadPad(GPIOA, GPIOA_BUTTON))
{
// check for valid CLR image
if(CheckValidCLRImage(0x08008000))
{
// there seems to be a valid CLR image
// launch nanoCLR
LaunchCLR(0x08008000);
}
}

// The kernel is initialized but not started yet, this means that
// main() is executing with absolute priority but interrupts are already enabled.
osKernelInitialize();
Expand All @@ -72,23 +87,6 @@ int main(void) {

// Normal main() thread
while (true) {

// check for button pressed
if (palReadPad(GPIOA, GPIOA_BUTTON))
{
// Start the shutdown sequence

// terminate threads
osThreadTerminate(receiverThreadId);
osThreadTerminate(blinkerThreadId);

// stop the serial-over-USB CDC driver
sduStop(&SDU1);

// launch nanoCLR
LaunchCLR(0x08008000);
}

osDelay(500);
}
}
Expand Down
32 changes: 15 additions & 17 deletions targets/CMSIS-OS/ChibiOS/ST_STM32F4_DISCOVERY/nanoBooter/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ int main(void) {
// and performs the board-specific initializations.
halInit();

// the following IF is not mandatory, it's just providing a way for a user to 'force'
// the board to remain in nanoBooter and not launching nanoCLR

// if the USER button (blue one) is pressed, skip the check for a valid CLR image and remain in booter
if (palReadPad(GPIOA, GPIOA_BUTTON))
{
// check for valid CLR image
if(CheckValidCLRImage(0x08008000))
{
// there seems to be a valid CLR image
// launch nanoCLR
LaunchCLR(0x08008000);
}
}

// The kernel is initialized but not started yet, this means that
// main() is executing with absolute priority but interrupts are already enabled.
osKernelInitialize();
Expand All @@ -80,23 +95,6 @@ int main(void) {

// Normal main() thread
while (true) {

// check for button pressed
if (palReadPad(GPIOA, GPIOA_BUTTON))
{
// Start the shutdown sequence

// terminate threads
osThreadTerminate(receiverThreadId);
osThreadTerminate(blinkerThreadId);

// stop the serial-over-USB CDC driver
sduStop(&SDU1);

// launch nanoCLR
LaunchCLR(0x08008000);
}

osDelay(500);
}
}
Expand Down
38 changes: 38 additions & 0 deletions targets/CMSIS-OS/ChibiOS/common/LaunchCLR.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,41 @@ void LaunchCLR(uint32_t address)
JumpToNanoCLR();
}

bool CheckValidCLRImage(uint32_t address)
{
// load nanoCLR vector table
const vectors_t* nanoCLRVectorTable = (vectors_t*) address;

// 1st check: the flash content pointed by the address can't be all 0's neither all F's
// meaning that the Flash is neither 'all burnt' or erased

// the stack pointer is at the 1st position of vectors_t
if(nanoCLRVectorTable->init_stack == 0xFFFFFFFF ||
nanoCLRVectorTable->init_stack == 0x00000000)
{
// check failed, there is no valid CLR image
//return false;
}

// volatile uint16_t* temp1 = (uint16_t*)nanoCLRVectorTable->reset_handler;
// temp1 = ((uint8_t*)temp1) - 0x1;
// volatile uint16_t temp = (uint16_t)(*temp1);


// 2nd check: the content pointed by the reset vector has to be 0xB672
// this is the opcode for 'CPSID I' which is the very 1st assembly instruction of a ChibiOS nanoCLR image
// the casts bellow are there because the opcode is a 16 bit value and we need to subtract 1 from the reset vector address pointing to it
uint16_t* opCodeAddress = (uint16_t*)nanoCLRVectorTable->reset_handler;
opCodeAddress = (uint8_t*)opCodeAddress - 0x1;

if((uint16_t)*opCodeAddress == 0xB672)
{
// check, there seems to be a valid CLR image
return true;
}
else
{
// got here, assume that there isn't a valid CLR imaged flashed
return false;
}
}