Added a working fat driver and did some loading attempts

This commit is contained in:
Morten Delenk 2016-08-05 15:15:47 +02:00
parent 286c81791e
commit f90c6420eb
No known key found for this signature in database
GPG key ID: 3F818D0F65DCB490
11 changed files with 195 additions and 16 deletions

BIN
.do.sh.kate-swp Normal file

Binary file not shown.

18
boot/3ds/Makefile Normal file
View file

@ -0,0 +1,18 @@
include ../../kernel.settings
CC=$(PREFIX)gcc
LD=$(PREFIX)ld
SRCS = $(shell find . -name '*.[cS]')
OBJS = $(addsuffix .o,$(basename $(SRCS)))
ASFLAGS =
CFLAGS += -I. -fno-stack-protector -nostdinc -ffreestanding -std=c11 -fno-builtin -w -Werror -nostdlib -fpie
LDFLAGS += -T loader.ld
all: $(OBJS)
$(LD) $(LDFLAGS) -o loader.elf $(OBJS) -L$(HOME)/opt/lib/gcc/$(libpath)/6.1.0/ -lgcc
objcopy -O binary loader.elf loader.bin
rm loader.elf
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $^
%.o: %.S
$(CC) $(ASFLAGS) -c -o $@ $^

View file

@ -143,22 +143,22 @@ DWORD get_fattime (void) {
return 0;
}
WCHAR ff_convert ( /* Converted character, Returns zero on error */
WCHAR src, /* Character code to be converted */
UINT dir /* 0: Unicode to OEMCP, 1: OEMCP to Unicode */
/*WCHAR ff_convert ( /* Converted character, Returns zero on error *
WCHAR src, /* Character code to be converted *
UINT dir /* 0: Unicode to OEMCP, 1: OEMCP to Unicode *
)
{
WCHAR c;
if (src < 0x80) { /* ASCII */
if (src < 0x80) { /* ASCII *
c = src;
} else {
if (dir) { /* OEMCP to Unicode */
if (dir) { /* OEMCP to Unicode *
c = (src >= 0x100) ? 0 : Tbl[src - 0x80];
} else { /* Unicode to OEMCP */
} else { /* Unicode to OEMCP *
for (c = 0; c < 0x80; c++) {
if (src == Tbl[c]) break;
}
@ -177,7 +177,7 @@ WCHAR ff_wtoupper(WCHAR chr)
else
return chr;
}
*/
/*--------------------------------------------------------------------------
Module Private Definitions

11
boot/3ds/loader.ld Normal file
View file

@ -0,0 +1,11 @@
ENTRY(_start)
SECTIONS
{
. = 0x23F00000;
.text.start : { *(.text.start)}
.text : {*(.text)}
.data : {*(.data)}
.bss : {*(.bss COMMON)}
.rodata : {*(.rodata)}
. = ALIGN(4);
}

65
boot/3ds/main.c Normal file
View file

@ -0,0 +1,65 @@
#include "common.h"
#include "sdmmc.h"
#include "fatfs/ff.h"
char *lfb=(char*)0x18300000;
#define DIAGPXL(i) (lfb[6*(i)]=lfb[6*(i)+1]=lfb[6*(i)+2]=0xFF)
/**
* \struct FIRM_sect
* \brief Contains one section of the FIRM format
*/
struct FIRM_sect {
unsigned int offset; //! Offset in file (bytes)
unsigned int physical; //! Physical address, where the section is copied to
unsigned int size; //! Size of section
unsigned int arm11; //! currently unused
unsigned char SHA256[0x20]; //! Currently unused
}__attribute__((packed));
/**
* \struct FIRM_header
* \brief Contains the first sector of every FIRM file.
*/
struct FIRM_header {
char magic[4]; //! Magic "FIRM" string (not-null terminated)
int version; //! Version. Currently 1
void(*entrypoint)(void**); //! Address where the processor jumps to after loading
void * arm11entry;
unsigned int reserved[0xC];
struct FIRM_sect sections[4]; //! The four internal sections
unsigned char RSA2048[0x100]; //! Currently unused
}__attribute__((packed));
/**
* \brief Module table for the kernel (Up to 1024 mods)
*/
void *arm9modtable[64];
void *arm11modtable[64];
struct FIRM_header hdr;
void init() {
DIAGPXL(0);
FATFS fs;
FIL firm;
f_mount(&fs, "0:", 0);
if(f_open(&firm, "mtgos.firm", FA_READ | FA_OPEN_EXISTING) == FR_OK) {
DIAGPXL(1);
unsigned int br;
f_read(&firm, (void*)&hdr, sizeof(struct FIRM_header), &br);
DIAGPXL(2);
if((hdr.magic[0]!='F')||(hdr.magic[1]!='I')||(hdr.magic[2]!='R')||(hdr.magic[3]!='M'))
while(1);
DIAGPXL(3);
for(int i=0;i<4;i++) { //Load sections
if(hdr.sections[i].size==0)
continue;
f_lseek(&firm, hdr.sections[i].offset);
char oldval=*((char*)hdr.sections[i].physical);
f_read(&firm, (void*)hdr.sections[i].physical, hdr.sections[i].size, &br);
if(oldval!=*((char*)hdr.sections[i].physical))
DIAGPXL(i+4);
DIAGPXL(i+8);
}
DIAGPXL(12);
}
DIAGPXL(13);
arm9modtable[0]=0;
arm11modtable[0]=0;
for(;;);
}

63
boot/3ds/start.S Normal file
View file

@ -0,0 +1,63 @@
.section .text.start
.align 4
b _start
.word 0,0
.global _start
.extern main
_start:
mov sp, #0x28000000
//Disable caches/mpu
mrc p15, 0, r0, c1, c0, 0
bic r0, #0x1000 //Disable instruction cache
bic r0, #0x4 //Disable data cache
bic r0, #0x1 //Disable MPU
mcr p15, 0, r0, c1, c0, 0
ldr r0, =0x33333333
mcr p15, 0, r0, c5, c0, 2 //Write data access
mcr p15, 0, r0, c5, c0, 3 //Write instruction access
// Sets MPU permissions and cache settings
ldr r0, =0xFFFF001D // ffff0000 32k | bootrom (unprotected part)
ldr r1, =0x3000801B // fff00000 16k | dtcm
ldr r2, =0x01FF801D // 01ff8000 32k | itcm
ldr r3, =0x08000029 // 08000000 2M | arm9 mem (O3DS / N3DS)
ldr r4, =0x10000029 // 10000000 2M | io mem (ARM9 / first 2MB)
ldr r5, =0x20000037 // 20000000 256M | fcram (O3DS / N3DS)
ldr r6, =0x1FF00027 // 1FF00000 1M | dsp / axi wram
ldr r7, =0x1800002D // 18000000 8M | vram (+ 2MB)
mov r8, #0x29
mcr p15, 0, r0, c6, c0, 0
mcr p15, 0, r1, c6, c1, 0
mcr p15, 0, r2, c6, c2, 0
mcr p15, 0, r3, c6, c3, 0
mcr p15, 0, r4, c6, c4, 0
mcr p15, 0, r5, c6, c5, 0
mcr p15, 0, r6, c6, c6, 0
mcr p15, 0, r7, c6, c7, 0
mcr p15, 0, r8, c3, c0, 0 // Write bufferable 0, 3, 5
mcr p15, 0, r8, c2, c0, 0 // Data cacheable 0, 3, 5
mcr p15, 0, r8, c2, c0, 1 // Inst cacheable 0, 3, 5
// Enable dctm
ldr r0, =0x3000800A // set dtcm
mcr p15, 0, r0, c9, c1, 0 // set the dtcm Region Register
// Enable caches
mrc p15, 0, r0, c1, c0, 0 // read control register
orr r0, r0, #(1<<18) // - itcm enable
orr r0, r0, #(1<<16) // - dtcm enable
//orr r0, r0, #(1<<12) // - instruction cache enable
//orr r0, r0, #(1<<2) // - data cache enable
orr r0, r0, #(1<<0) // - mpu enable
mcr p15, 0, r0, c1, c0, 0 // write control register
// Flush caches
mov r0, #0
mcr p15, 0, r0, c7, c5, 0 // flush I-cache
mcr p15, 0, r0, c7, c6, 0 // flush D-cache
mcr p15, 0, r0, c7, c10, 4 // drain write buffer
// Fixes mounting of SDMC
ldr r0, =0x10000020
mov r1, #0x340
str r1, [r0]
b init

View file

@ -3,7 +3,7 @@ CC=$(PREFIX)gcc
LD=$(PREFIX)ld
SRCS = $(shell find . -name '*.[cS]')
OBJS = $(addsuffix .o,$(basename $(SRCS)))
ASFLAGS =
ASFLAGS +=
CFLAGS += -fno-stack-protector -nostdinc -ffreestanding -std=c11 -fno-builtin -w -Werror -nostdlib -fpie
LDFLAGS += -T loader.ld
all: $(OBJS)

View file

@ -3,12 +3,9 @@ SECTIONS
{
. = 0x100000;
kernel_start = .;
.bootstrap : {
*(multiboot)
boot.S (.text)
}
.text : {
*(EXCLUDE_FILE(*boot.S) .text)
*(multiboot)
*(.text)
}
.data : {
start_ctors = .;

17
do.sh Executable file
View file

@ -0,0 +1,17 @@
#!/usr/bin/env bash
gmake clean
gmake loader.bin
gmake -C kernel
gmake -C kernel clean
mv kernel/mtgos.elf .
objcopy -O binary mtgos.elf mtgos.bin
gmake -C kernel subvar=11
objcopy -O binary kernel/mtgos.elf kernel/mtgos.bin
firmlink -O 8000000 -O 1FF80000 -E $(nm mtgos.elf | egrep ' _start$' | awk '{print $1}') -e $(nm kernel/mtgos.elf | egrep ' _start$' | awk '{print $1}') -o mtgos.firm mtgos.bin kernel/mtgos.bin
rm mtgos.bin kernel/mtgos.*
sudo mount_msdosfs /dev/da3s1 mount
sudo rm mount/{arm9loaderhax.bin,mtgos.firm}
sudo mv loader.bin mount/arm9loaderhax.bin
sudo mv mtgos.firm mount/mtgos.firm
sudo umount mount
echo "Remove SD card!"

View file

@ -1,3 +1,4 @@
#!/usr/bin/env bash
echo Entry point: $(nm mtgos.elf | egrep ' _start$' | awk '{print $1}')
firmlink -O 200000 -E $(nm mtgos.elf | egrep ' _start$' | awk '{print $1}') -o $1.firm $1.bin
firmlink -O 200000 -E $(nm mtgos.elf | egrep ' _start$' | awk '{print $1}') -o $1.firm $1.bin
firmlink -O 1FF80000 -E $(nm mtgos.elf | egrep ' _start$' | awk '{print $1}') -o 3ds-$1.firm $1.bin

View file

@ -1,4 +1,5 @@
arch = x86_64
arch = 3ds
subvar = 9
MODE = debug
export PATH := $(HOME)/opt/bin:$(PATH)
LDFLAGS =
@ -16,17 +17,23 @@ else
ifeq ($(arch),3ds)
PREFIX = arm-none-eabi-
libpath = arm-none-eabi
ARCHFLAGS = -m32
ifeq ($(subvar),9)
ARCHFLAGS = -mcpu=arm946e-s -march=armv5te -mlittle-endian
else
ARCHFLAGS = -mcpu=mpcore -mlittle-endian
endif
endif
endif
endif
ifeq ($(MODE),debug)
CFLAGS := -g3 -DDEBUG
ASFLAGS := -g3 -DDEBUG
CPPFLAGS := -g3 -DDEBUG
else
CFLAGS := -O2
CPPFLAGS := -O2
endif
ASFLAGS += $(ARCHFLAGS)
CFLAGS += $(ARCHFLAGS)
CPPFLAGS += $(ARCHFLAGS)