AMP with Raspberry Pi: Step 2 - Compiling, Linking and Loading a Bare Metal App

Bare Metal Coding

Following Brian´s excellent bare metal tutorials I obtained a binary image for a blinking led code.
+Source files for this step from  Brian's github repository.
Compile armc-03.c with linker options to relocate the binary to upper 512Mb, that is 0x20000000

gcc -c armc-03.c -o armc-03.o
ld -Ttext 0x20000000 -nostartfiles -g -Wl,-verbose -Wl,-T,rpi.x armc-03.o -o up-metal.elf
objcopy up-metal2.elf -O binary up-metal.img

The rpi.x linker script file is included from armc-06

Check obtained img filesize, must be 152 bytes (afterall it only blinks a led).

To place (from Linux) the bare-metal executable at 0x20000000 I wrote a simple mmap() based loader, invoke it with binary filename as parameter.

loadmetal src code

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>

int main (int argc, char * argv [])
int fd_mem;
void *load_address;
unsigned long fileLen;
FILE *file;
printf ("Opening %s\n",argv[1]);

//Get file length
fseek(file, 0, SEEK_END);
fseek(file, 0, SEEK_SET);
printf ("File lenght %d\n",fileLen);

/* Map Physical address of RAM to virtual address segment with Read/Write Access */
printf ("Opening Mem %x\n",0x20000000);
fd_mem = open("/dev/mem", O_RDWR);
load_address = mmap(NULL, fileLen,PROT_READ|PROT_WRITE, MAP_SHARED, fd_mem, 0x20000000);

// Read file contents
  fread(load_address, fileLen, 1, file);

No hay comentarios:

Publicar un comentario