Skip to content

Commit

Permalink
Fix LaunchCLR to detect valid image (#780)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Jun 29, 2018
1 parent 9180717 commit a395049
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions targets/CMSIS-OS/ChibiOS/common/LaunchCLR.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ bool CheckValidCLRImage(uint32_t address)
// 2nd check: the content pointed by the reset vector has to be 0xE002
// that's an assembly "b.n" (branch instruction) the very first one in the Reset_Handler function
// see os\common\startup\ARMCMx\compilers\GCC\vectors.S
// the linker places this startup code right after the vectors table
uint16_t* opCodeAddress = (uint16_t*)((irq_vector_t)nanoCLRVectorTable + sizeof(vectors_t) + sizeof(irq_vector_t));

// because the code in the vectors region is aligned by 16 bytes we need to check if the linker has pushed this further and adjust accordingly
uint32_t alignmentOffset = ((uint32_t)opCodeAddress % 0x10);
opCodeAddress = (uint16_t*)((uint32_t)opCodeAddress + (0x10 - alignmentOffset));
// the linker can place this anywhere on the address space because of optimizations so we better check where the reset pointer points to
uint32_t opCodeAddress = (uint32_t)((uint32_t**)nanoCLRVectorTable->reset_handler);

if((uint16_t)*opCodeAddress == 0xE002)
// real address is -1
opCodeAddress -= 1;

uint32_t opCode = *((uint32_t*)opCodeAddress);
if((uint16_t)opCode == 0xE002)
{
// check, there seems to be a valid CLR image
return true;
Expand Down

0 comments on commit a395049

Please sign in to comment.