b8

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.vgx.fr/b8
Log | Files | Refs

commit 68dc445f7d94e7222ba9fb4dc7f73c5c02d65436
parent c6d45b85462bad7e56528ed1dbcc414c10aafaf0
Author: Léo Villeveygoux <leo.villeveygoux@etu.u-bordeaux.fr>
Date:   Fri, 16 Jun 2017 17:39:33 +0200

cleanup: create function, remove magic value, …

Diffstat:
MMakefile | 3++-
Mb8.c | 30+++++++++++++++++++-----------
2 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,4 +1,5 @@ -LDFLAGS=-Wl,--section-start=mem_chunk=0x20000 +CFLAGS=-Wall -Wextra +LDFLAGS=-Wl,--section-start=mem_section=0x20000 PROG=b8 CARTS=$(patsubst %.asm,%.bin,$(wildcard *.asm)) diff --git a/b8.c b/b8.c @@ -12,7 +12,22 @@ }\ }while(0) -char buf[0x40000] __attribute__ ((section ("mem_chunk"))) = {0}; +#define MEM_SIZE 0x40000 +#define CART_SIZE 0x20000 +#define CART_HEADER_SIZE 0x20 + +char mem[MEM_SIZE] __attribute__ ((section ("mem_section"))) = {0}; + +void load_cart(const char *filename){ + int fd = open(filename, O_RDONLY); + CHECK_STD_ERROR(fd == -1, "open"); + + int ret = mprotect(&mem, MEM_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC); + CHECK_STD_ERROR(ret, "mprotect"); + + int rdsize = read(fd, mem, CART_SIZE); + CHECK_STD_ERROR(rdsize == -1, "read"); +} int main(int argc, char *argv[]){ void (*cart_fun)(void) = NULL; @@ -22,19 +37,12 @@ int main(int argc, char *argv[]){ return 1; } - int fd = open(argv[1], O_RDONLY); - CHECK_STD_ERROR(fd == -1, "open"); - - int ret = mprotect(&buf, 4096, PROT_READ|PROT_WRITE|PROT_EXEC); - CHECK_STD_ERROR(ret, "mprotect"); - - int rdsize = read(fd, buf, 0x20000); - CHECK_STD_ERROR(rdsize == -1, "read"); + load_cart(argv[1]); - cart_fun = (void*)buf+0x20; + cart_fun = (void*)mem+CART_HEADER_SIZE; cart_fun(); - printf("%d\n",(int)buf[0x20000]); + printf("%d\n",(int)mem[CART_SIZE]); return 0; }