Simvik

Read a vector table

Read a Cortex-M image's initial stack pointer and reset handler, and tell a real image from noise. An easy embedded C problem (Reading a firmware dump), run…

easy · Reading a firmware dump · cortex-m · reverse-engineering · parsing

A Cortex-M image starts with its vector table, 32-bit little-endian words: the initial stack pointer, then the reset handler's address (with bit 0 set for Thumb code).

Write parse_vectors(img, n, sp, entry) for a chip with flash at FLASH_BASE (FLASH_SIZE bytes) and RAM at RAM_BASE (RAM_SIZE bytes). Read both words, store the stack pointer in *sp and the reset handler's code address (bit 0 cleared) in *entry, and return:

ReturnWhen
-1fewer than 8 bytes
-2the stack pointer is not a multiple of 4 above RAM_BASE and at most RAM_BASE + RAM_SIZE
-3the reset handler's bit 0 is clear (not Thumb), or its address is not inside the flash
0a plausible image

Store the two values whenever there are 8 bytes, even if a check then fails.

Starter code

int parse_vectors(const uint8_t *img, unsigned n, uint32_t *sp, uint32_t *entry) {
  *sp = (uint32_t)img[0] << 24 | img[1] << 16 | img[2] << 8 | img[3];
  *entry = (uint32_t)img[4] << 24 | img[5] << 16 | img[6] << 8 | img[7];
  (void)n;
  return 0;
}

Example tests

An application image

const uint8_t img[8] = { 0x00, 0x00, 0x02, 0x20, 0x99, 0x41, 0x00, 0x08 };
show(img, 8);

Expected output:

0 sp 20020000 entry 08004198

Erased flash

const uint8_t img[8] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
show(img, 8);

Expected output:

-2 sp FFFFFFFF entry FFFFFFFE

Learn it first

  • Reading a firmware dump · Vector tables, strings and disassembly, what attackers look for, and protecting the image.

More Reading a firmware dump problems