fernly: Add two-stage bootloader and get basics working
The romulator only emulates 64k of SPI ROM. That's not enough for anything interesting. Add a second-stage bootloader to send data to Fernvale over serial. This will allow us to work around needing code relocations, and allow for much larger binaries.
This commit is contained in:
parent
7eaaa0cc14
commit
dd6616b677
2 changed files with 247 additions and 0 deletions
128
fernly-loader.c
Normal file
128
fernly-loader.c
Normal file
|
@ -0,0 +1,128 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
int fernvale_reset(void) {
|
||||||
|
int fd;
|
||||||
|
uint8_t b;
|
||||||
|
|
||||||
|
fd = open("/sys/class/gpio/gpio17/value", O_WRONLY);
|
||||||
|
b = '0';
|
||||||
|
write(fd, &b, 1);
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
usleep(250000);
|
||||||
|
|
||||||
|
fd = open("/sys/class/gpio/gpio17/value", O_WRONLY);
|
||||||
|
b = '1';
|
||||||
|
write(fd, &b, 1);
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fernvale_banner(int fd) {
|
||||||
|
uint8_t b;
|
||||||
|
|
||||||
|
while (-1 != read(fd, &b, 1)) {
|
||||||
|
if (b == '>')
|
||||||
|
return 0;
|
||||||
|
printf("%c", b);
|
||||||
|
fflush(stdout);
|
||||||
|
if (b == '\r')
|
||||||
|
printf("\t");
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fernvale_print_line(int fd) {
|
||||||
|
uint8_t b;
|
||||||
|
|
||||||
|
while (-1 != read(fd, &b, 1)) {
|
||||||
|
printf("%c", b);
|
||||||
|
if (b == '\n')
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
int serfd, binfd;
|
||||||
|
struct stat stats;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (argc != 2) {
|
||||||
|
printf("Usage: %s firmware.bin\n", argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
serfd = open("/dev/ttymxc2", O_RDWR);
|
||||||
|
if (-1 == serfd) {
|
||||||
|
perror("Unable to open serial port");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
binfd = open(argv[1], O_RDONLY);
|
||||||
|
if (-1 == binfd) {
|
||||||
|
perror("Unable to open firmware file");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-1 == fstat(binfd, &stats)) {
|
||||||
|
perror("Unable to get file stats");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Resetting Fernvale... ");
|
||||||
|
fflush(stdout);
|
||||||
|
fernvale_reset();
|
||||||
|
printf("Ok.\n");
|
||||||
|
|
||||||
|
printf("Waiting for banner... ");
|
||||||
|
fflush(stdout);
|
||||||
|
fernvale_banner(serfd);
|
||||||
|
printf("Ok.\n");
|
||||||
|
|
||||||
|
printf("Writing %d bytes...", (int)stats.st_size);
|
||||||
|
{
|
||||||
|
uint8_t b;
|
||||||
|
|
||||||
|
b = stats.st_size & 0xff;
|
||||||
|
write(serfd, &b, 1);
|
||||||
|
|
||||||
|
b = stats.st_size >> 8 & 0xff;
|
||||||
|
write(serfd, &b, 1);
|
||||||
|
|
||||||
|
b = stats.st_size >> 16 & 0xff;
|
||||||
|
write(serfd, &b, 1);
|
||||||
|
|
||||||
|
b = stats.st_size >> 24 & 0xff;
|
||||||
|
write(serfd, &b, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < stats.st_size; i++) {
|
||||||
|
uint8_t b;
|
||||||
|
if (-1 == read(binfd, &b, 1)) {
|
||||||
|
perror("Error while reading binary");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
if (-1 == write(serfd, &b, 1)) {
|
||||||
|
perror("Error while writing binary to serial port");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf(" Done.\n");
|
||||||
|
|
||||||
|
printf("Result: ");
|
||||||
|
fernvale_print_line(serfd);
|
||||||
|
|
||||||
|
close(serfd);
|
||||||
|
close(binfd);
|
||||||
|
return execl("/usr/bin/screen", "screen", "/dev/ttymxc2", "115200", NULL);
|
||||||
|
return 0;
|
||||||
|
}
|
119
loader.S
Normal file
119
loader.S
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
.text
|
||||||
|
|
||||||
|
.global _start
|
||||||
|
_start:
|
||||||
|
ldr r0, =0x7000cffc // stack_start
|
||||||
|
mov sp, r0
|
||||||
|
mov r2, #0xffffffff
|
||||||
|
ldr r1, =0x7000c000 // stack_end
|
||||||
|
|
||||||
|
clear_stack:
|
||||||
|
cmp r1, r0
|
||||||
|
str r2, [r0]
|
||||||
|
sub r0, r0, #4
|
||||||
|
bcc clear_stack
|
||||||
|
|
||||||
|
bl wdt_disable
|
||||||
|
|
||||||
|
adr r0, welcome_banner
|
||||||
|
bl uart_puts
|
||||||
|
|
||||||
|
load_program:
|
||||||
|
bl uart_getc
|
||||||
|
mov r5, r0
|
||||||
|
lsls r5, r5, #0
|
||||||
|
mov r4, r5
|
||||||
|
|
||||||
|
bl uart_getc
|
||||||
|
mov r5, r0
|
||||||
|
lsls r5, r5, #8
|
||||||
|
orr r4, r5
|
||||||
|
|
||||||
|
bl uart_getc
|
||||||
|
mov r5, r0
|
||||||
|
lsls r5, r5, #16
|
||||||
|
orr r4, r5
|
||||||
|
|
||||||
|
bl uart_getc
|
||||||
|
mov r5, r0
|
||||||
|
lsls r5, r5, #24
|
||||||
|
orr r4, r5
|
||||||
|
|
||||||
|
# r4 now contains the number of bytes to load.
|
||||||
|
# r5 contains the current offset to write to.
|
||||||
|
# Load bytes from the serial port into RAM.
|
||||||
|
mov r5, #0
|
||||||
|
loader_loop:
|
||||||
|
bl uart_getc
|
||||||
|
strb r0, [r5], #1
|
||||||
|
sub r4, #1
|
||||||
|
cmp r4, #0
|
||||||
|
bne loader_loop
|
||||||
|
|
||||||
|
jump_to_new_program:
|
||||||
|
adr r0, launch_message
|
||||||
|
bl uart_puts
|
||||||
|
mov r0, #0
|
||||||
|
mov pc, r0
|
||||||
|
|
||||||
|
.align 4
|
||||||
|
welcome_banner:
|
||||||
|
.ascii "Fernvale bootloader\r\nWrite four bytes of program size, then\r\n"
|
||||||
|
.asciz "write program data...\r\n>"
|
||||||
|
launch_message:
|
||||||
|
.asciz "Launching program...\r\n"
|
||||||
|
.align 4
|
||||||
|
|
||||||
|
uart_putc:
|
||||||
|
stmfd sp!, {lr}
|
||||||
|
ldr r2, =0xa0080014 // uart offset
|
||||||
|
uart_putc_ready_wait:
|
||||||
|
ldrb r1, [r2]
|
||||||
|
tst r1, #0x20
|
||||||
|
beq uart_putc_ready_wait
|
||||||
|
|
||||||
|
sub r2, r2, #0x14
|
||||||
|
strb r0, [r2]
|
||||||
|
ldmfd sp!, {pc}
|
||||||
|
|
||||||
|
uart_puts:
|
||||||
|
stmfd sp!, {lr}
|
||||||
|
mov r3, r0
|
||||||
|
|
||||||
|
uart_puts_loop:
|
||||||
|
ldrb r0, [r3], #1
|
||||||
|
cmp r0, #0
|
||||||
|
beq uart_exit
|
||||||
|
bl uart_putc
|
||||||
|
b uart_puts_loop
|
||||||
|
uart_exit:
|
||||||
|
ldmfd sp!, {pc}
|
||||||
|
|
||||||
|
uart_getc:
|
||||||
|
stmfd sp!, {lr}
|
||||||
|
ldr r2, =0xa0080014 // uart offset
|
||||||
|
uart_getc_ready_wait:
|
||||||
|
ldrb r1, [r2]
|
||||||
|
tst r1, #0x01
|
||||||
|
beq uart_getc_ready_wait
|
||||||
|
|
||||||
|
sub r2, r2, #0x14
|
||||||
|
ldrb r0, [r2]
|
||||||
|
ldmfd sp!, {pc}
|
||||||
|
|
||||||
|
asm_memcpy:
|
||||||
|
mov r3, r1
|
||||||
|
add r3, r3, r2
|
||||||
|
|
||||||
|
asm_memcpy_loop:
|
||||||
|
cmp r1, r3
|
||||||
|
ldrcc r2, [r1], #4
|
||||||
|
strcc r2, [r0], #4
|
||||||
|
bcc asm_memcpy_loop
|
||||||
|
bx lr
|
||||||
|
|
||||||
|
wdt_disable:
|
||||||
|
ldr r1, =0xa0030000
|
||||||
|
mov r0, #0x2200
|
||||||
|
str r0, [r1]
|
||||||
|
bx lr
|
Loading…
Add table
Add a link
Reference in a new issue