Skip to content

Instantly share code, notes, and snippets.

@jaimejim
Created March 18, 2018 11:18
Show Gist options
  • Save jaimejim/a129979b01dbf7489b300a536938c313 to your computer and use it in GitHub Desktop.
Save jaimejim/a129979b01dbf7489b300a536938c313 to your computer and use it in GitHub Desktop.
/* SUIT pseudo bootloader
* Created at the IETF 101 Hackathon
* Receives a manifest to RAM and verifies it
* https://os.mbed.com/compiler/#nav:/uart_pseudo_bootloader/main.cpp
*/
#include "mbed.h"
#define RXBUFFERSIZE 1024 // Size of RAM buffer for receiving the manifest
#define LED_RED led1
#define LED_GREEN led2
#define LED_BLUE led3
uint8_t rxBuffer[RXBUFFERSIZE];
uint8_t *rxPtr = rxBuffer;
uint16_t manifestLen = 0;
/* Setup peripherals */
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
Serial pc(USBTX, USBRX); // tx, rx
int main() {
// Turn on all LEDs
LED_RED = 0;
LED_GREEN = 0;
LED_BLUE = 0;
wait(0.5);
// Turn off red and blue; just green is on
LED_RED = 1;
LED_BLUE = 1;
pc.printf("***Welcome to SUIT pseudo bootloader!***\n");
pc.printf("Length of your manifest in bytes?\n");
pc.printf("(Send as hex word, e.g. if the length is 1024 bytes (0x400) send 0x04, 0x00.)\n");
manifestLen = pc.getc() << 8;
manifestLen += pc.getc();
/* Catch buffer overflow */
if(manifestLen > (uint16_t)RXBUFFERSIZE) {
pc.printf("Maximum buffer size of %d bytes exceeded.\n", (uint16_t)RXBUFFERSIZE);
LED_GREEN = 1; // Turn green of
while(1) { // Trap for error state
LED_RED = !LED_RED;
wait(0.25);
}
}
/* Receive from UART to RAM buffer */
pc.printf("Waiting for %d bytes...\n", manifestLen);
uint16_t i = 0;
while(i < manifestLen) {
rxBuffer[i] = pc.getc();
i++;
LED_BLUE = !LED_BLUE; // Blink blue while receiving
LED_BLUE = !LED_BLUE;
}
// TODO: Process rxBuffer (manifest) here
LED_GREEN = 1; // Turn off green LED
for(i = 0; i < manifestLen; i++) {
pc.putc(rxBuffer[i]);
LED_BLUE = !LED_BLUE; // Blink blue while tranceiving
LED_BLUE = !LED_BLUE;
}
// Done, trap CPU
while(1) {
LED_GREEN = !LED_GREEN;
wait(0.5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment