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 dynamic-loader example #16

Merged
merged 4 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion .github/workflows/run-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}
strategy:
matrix:
example: [hello-world, sensor-reader, arithmetic, fileio, timer, violation, sancus-step, dma]
example: [hello-world, sensor-reader, arithmetic, fileio, timer, violation, sancus-step, dma, dynamic-loader]
sancus-security: [64, 128]
include:
- example: hello-library
Expand Down
1 change: 1 addition & 0 deletions dynamic-loader/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sm1.h
30 changes: 30 additions & 0 deletions dynamic-loader/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
include ../Makefile.include

SOURCES = $(shell find *.c ! -name "sm1.c" -printf "%p ")
OBJECTS = $(SOURCES:.c=.o)
SM1_HEADER = $(shell python3 hardcode_elf.py)
jovanbulck marked this conversation as resolved.
Show resolved Hide resolved

TARGET = main.elf
TARGET_NO_WRAP = no_wrap_$(TARGET)

LDFLAGS += -lsm-control -lloader -ltools

all: $(TARGET)

$(TARGET_NO_WRAP): $(SM1_HEADER) $(OBJECTS)
$(LD) $(LDFLAGS) --prepare-for-sm-text-section-wrapping -o $@ $^
jovanbulck marked this conversation as resolved.
Show resolved Hide resolved

$(TARGET): $(TARGET_NO_WRAP)
$(SANCUS_CRYPTO) --wrap-sm-text-sections $(CRYPTOFLAGS) -o $@ $<

load: $(TARGET)
$(SANCUS_LOAD) $(LOADFLAGS) $<

sim: $(TARGET)
$(SANCUS_SIM) $(SIMFLAGS) $<

clean:
$(RM) $(TARGET) $(TARGET_NO_WRAP) $(OBJECTS)
rm -f sim-input.bin sim-output.bin
rm -f *.fst *.vcd
rm -f sm1.h sm1.o sm1.elf
20 changes: 20 additions & 0 deletions dynamic-loader/hardcode_elf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os

# build sm1.elf
os.system("sancus-cc -c -o sm1.o sm1.c")
jovanbulck marked this conversation as resolved.
Show resolved Hide resolved
os.system("sancus-ld --inline-arithmetic -o sm1.elf sm1.o")

# read ELF
with open("sm1.elf", "rb") as f:
sm1 = f.read()

# prepare string to hardcode: remove brackets
jovanbulck marked this conversation as resolved.
Show resolved Hide resolved
sm1_array = str(list(sm1))[1:-1]

# read header template
with open("header_template.h", "r") as f:
header = f.read()

# write sm1.h
with open("sm1.h", "w") as f:
f.write(header.format(sm1_array))
10 changes: 10 additions & 0 deletions dynamic-loader/header_template.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef __SM_H__
#define __SM_H__

#include <stdint.h>

const char *SM_NAME = "sm1";
const int VENDOR_ID = 0x1234;
const uint8_t SM_BINARY[] = {{ {} }};

#endif
33 changes: 33 additions & 0 deletions dynamic-loader/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <msp430.h>
#include <stdio.h>
#include <string.h>
#include <sancus_support/sm_io.h>
#include <sancus_support/sm_control.h>
#include <sancus_support/global_symtab.h>

#include "sm1.h"

int main()
{
msp430_io_init();
printf("Loading SM from buffer..\n");
int id = sm_load((void *) SM_BINARY, SM_NAME, VENDOR_ID);
printf("Loaded SM id: %d\n", id);
jovanbulck marked this conversation as resolved.
Show resolved Hide resolved

size_t current_symbol;
for (current_symbol = 0; current_symbol < symtab_get_num_symbols(); current_symbol++) {
Symbol symbol;
int is_section;
ElfModule* module;

if (!symtab_get_symbol(current_symbol,
&symbol, &is_section, &module)) {
printf("Cannot retrieve symbol at index %d\n", current_symbol);
return 1;
}

printf("%s:%d\n", symbol.name, strlen(symbol.name));
}

EXIT();
}
10 changes: 10 additions & 0 deletions dynamic-loader/sm1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <sancus/sm_support.h>
#include <stdio.h>

SM_ENTRY(sm1) void init(uint8_t* input_data, size_t len)

{
(void) input_data;
(void) len;
puts("hello from sm1");
}
15 changes: 15 additions & 0 deletions dynamic-loader/static-symtab.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <sancus_support/private/symbol.h>
#include <sancus/sm_support.h>

#include <stdlib.h>
#include <stdio.h>

STATIC_SYMBOLS_START
SYMBOL(exit),
SYMBOL(malloc),
SYMBOL(free),
SYMBOL(putchar),
SYMBOL(puts),
SYMBOL(printf),
SYMBOL(__unprotected_entry)
STATIC_SYMBOLS_END