Skip to content

Commit

Permalink
Multiple things (again)
Browse files Browse the repository at this point in the history
 * EmuNAND support (only one right now, but all I need is a menu/option for more)
   * This is both normal layout (RedNAND) and gateway layout (header at back)
 * Loader now has three different rel options for text, data, and ro
 * Screeninit.
  • Loading branch information
chaoskagami committed Jun 6, 2016
1 parent 13322ff commit 2acb102
Show file tree
Hide file tree
Showing 31 changed files with 716 additions and 67 deletions.
23 changes: 17 additions & 6 deletions external/Makefile
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
.PHONY: all
all: loader svc
all: loader svc screeninit
mkdir -p ../out/corbenik/module
mkdir -p ../out/corbenik/svc
mkdir -p ../out/corbenik/bits
cp loader/loader.cxi ../out/corbenik/module/loader.cxi
cp svc/7b.bin ../out/corbenik/svc/7b.bin
cp svc/emunand.bin ../out/corbenik/bits/emunand.bin
cp screeninit/build/screeninit.bin ../out/corbenik/bits/screeninit.bin

.PHONY: clean
clean: clean_loader clean_svc
clean: clean_loader clean_svc clean_screeninit
rm -rf ../out/corbenik/svc
rm -rf ../out/corbenik/module

.PHONY: loader
loader:
make -C loader

.PHONY: clean_loader
clean_loader:
make -C loader clean

.PHONY: svc
svc:
make -C svc

.PHONY: screeninit
screeninit:
make -C screeninit

.PHONY: clean_loader
clean_loader:
make -C loader clean

.PHONY: clean_svc
clean_svc:
make -C svc clean

.PHONY: clean_screeninit
screeninit_clean:
make -C screeninit clean
5 changes: 4 additions & 1 deletion external/loader/source/interp.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef __INTERP_H
#define __INTERP_H

int execb(uint64_t tid, uint16_t ver, uint8_t *search_mem, uint32_t search_len);
int execb(uint64_t tid, uint16_t ver,
uint8_t *text_mem, uint32_t text_len,
uint8_t *data_mem, uint32_t data_size,
uint8_t *ro_mem, uint32_t ro_size);

#endif
6 changes: 3 additions & 3 deletions external/loader/source/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ load_code(u64 progid, u16 progver, prog_addrs_t *shared, prog_addrs_t *original,
}

// Patch segments
patch_text(progid, progver, (u8 *)shared->text_addr, shared->text_size << 12, original->text_size << 12);
patch_data(progid, progver, (u8 *)shared->data_addr, shared->data_size << 12, original->data_size << 12);
patch_ro(progid, progver, (u8 *)shared->ro_addr, shared->ro_size << 12, original->ro_size << 12);
patch_exe (progid, progver, (u8 *)shared->text_addr, shared->text_size << 12, original->text_size << 12,
(u8 *)shared->data_addr, shared->data_size << 12, original->data_size << 12,
(u8 *)shared->ro_addr, shared->ro_size << 12, original->ro_size << 12);

return 0;
}
Expand Down
23 changes: 7 additions & 16 deletions external/loader/source/patcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,28 +307,19 @@ overlay_patch(u64 progId, u8 *code, u32 size)
// TODO - Implement. Needs some thought. This should allow usage of files off SD rather than RomFS.
}

// This is only for the .data segment.
void
patch_data(u64 progId, u16 progver, u8 *data, u32 size, u32 orig_size)
{
}

// This is only for the .ro segment.
void
patch_ro(u64 progId, u16 progver, u8 *ro, u32 size, u32 orig_size)
{
}

// This is only for the .code segment.
void
patch_text(u64 progId, u16 progver, u8 *text, u32 size, u32 orig_size)
patch_exe(u64 progId, u16 progver,
u8 *text, u32 text_size, u32 orig_text,
u8* data, u32 data_size, u32 orig_data,
u8* ro, u32 ro_size, u32 orig_ro)
{
if (progId == 0x0004013000008002LL)
adjust_cpu_settings(progId, text, orig_size);
adjust_cpu_settings(progId, text, orig_text);

execb(progId, progver, text, orig_size);
execb(progId, progver, text, orig_text, data, orig_data, ro, orig_ro);

language_emu(progId, text, orig_size);
language_emu(progId, text, orig_text);
}

// Gets how many bytes .text must be extended by for patches to fit.
Expand Down
7 changes: 4 additions & 3 deletions external/loader/source/patcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

#include <3ds/types.h>

void patch_text(u64 progId, u16 progver, u8 *text, u32 size, u32 orig_size);
void patch_data(u64 progId, u16 progver, u8 *data, u32 size, u32 orig_size);
void patch_ro(u64 progId, u16 progver, u8 *ro, u32 size, u32 orig_size);
void patch_exe(u64 progId, u16 progver,
u8 *text, u32 text_size, u32 orig_text,
u8* data, u32 data_size, u32 orig_data,
u8* ro, u32 ro_size, u32 orig_ro);

u32 get_text_extend(u64 progId, u16 progver, u32 size_orig);
u32 get_ro_extend(u64 progId, u16 progver, u32 size_orig);
Expand Down
47 changes: 47 additions & 0 deletions external/screeninit/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
rwildcard = $(foreach d, $(wildcard $1*), $(filter $(subst *, %, $2), $d) $(call rwildcard, $d/, $2))

ifeq ($(strip $(DEVKITARM)),)
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM")
endif

include $(DEVKITARM)/3ds_rules

CC := arm-none-eabi-gcc
AS := arm-none-eabi-as
LD := arm-none-eabi-ld
OC := arm-none-eabi-objcopy

name := $(shell basename $(CURDIR))

dir_source := source
dir_build := build

ASFLAGS := -mcpu=mpcore -mfloat-abi=hard
CFLAGS := -Wall -Wextra -MMD -MP -mthumb -mthumb-interwork $(ASFLAGS) -fno-builtin -std=c11 -Wno-main -O2 -flto -ffast-math
LDFLAGS := -nostdlib

objects = $(patsubst $(dir_source)/%.s, $(dir_build)/%.o, \
$(patsubst $(dir_source)/%.c, $(dir_build)/%.o, \
$(call rwildcard, $(dir_source), *.s *.c)))

.PHONY: all
all: $(dir_build)/$(name).bin

.PHONY: clean
clean:
@rm -rf $(dir_build)

$(dir_build)/$(name).bin: $(dir_build)/$(name).elf
$(OC) -S -O binary $< $@

$(dir_build)/$(name).elf: $(objects)
$(LINK.o) -T linker.ld $(OUTPUT_OPTION) $^

$(dir_build)/%.o: $(dir_source)/%.c
@mkdir -p "$(@D)"
$(COMPILE.c) $(OUTPUT_OPTION) $<

$(dir_build)/%.o: $(dir_source)/%.s
@mkdir -p "$(@D)"
$(COMPILE.s) $(OUTPUT_OPTION) $<
include $(call rwildcard, $(dir_build), *.d)
12 changes: 12 additions & 0 deletions external/screeninit/linker.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ENTRY(_start)
SECTIONS
{
. = 0x24FFFC00;
.text.start : { *(.text.start) }
.text : { *(.text) }
.data : { *(.data) }
.bss : { *(.bss COMMON) }
.rodata : { *(.rodata) }
. = ALIGN(4);
}

106 changes: 106 additions & 0 deletions external/screeninit/source/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include "types.h"

void main(void)
{
// FIXME - We could use some serious macros here...

u32 brightnessLevel = *(vu32 *)0x24FFFC08;
vu32 *const arm11 = (u32 *)0x1FFFFFF8;

*(vu32 *)0x10141200 = 0x1007F;
*(vu32 *)0x10202014 = 0x00000001;
*(vu32 *)0x1020200C &= 0xFFFEFFFE;
*(vu32 *)0x10202240 = brightnessLevel; // Alteration; directly read brightness.
*(vu32 *)0x10202A40 = brightnessLevel;
*(vu32 *)0x10202244 = 0x1023E;
*(vu32 *)0x10202A44 = 0x1023E;

// Top screen
*(vu32 *)0x10400400 = 0x000001c2;
*(vu32 *)0x10400404 = 0x000000d1;
*(vu32 *)0x10400408 = 0x000001c1;
*(vu32 *)0x1040040c = 0x000001c1;
*(vu32 *)0x10400410 = 0x00000000;
*(vu32 *)0x10400414 = 0x000000cf;
*(vu32 *)0x10400418 = 0x000000d1;
*(vu32 *)0x1040041c = 0x01c501c1;
*(vu32 *)0x10400420 = 0x00010000;
*(vu32 *)0x10400424 = 0x0000019d;
*(vu32 *)0x10400428 = 0x00000002;
*(vu32 *)0x1040042c = 0x00000192;
*(vu32 *)0x10400430 = 0x00000192;
*(vu32 *)0x10400434 = 0x00000192;
*(vu32 *)0x10400438 = 0x00000001;
*(vu32 *)0x1040043c = 0x00000002;
*(vu32 *)0x10400440 = 0x01960192;
*(vu32 *)0x10400444 = 0x00000000;
*(vu32 *)0x10400448 = 0x00000000;
*(vu32 *)0x1040045C = 0x00f00190;
*(vu32 *)0x10400460 = 0x01c100d1;
*(vu32 *)0x10400464 = 0x01920002;
*(vu32 *)0x10400468 = 0x18300000;
*(vu32 *)0x10400470 = 0x80341;
*(vu32 *)0x10400474 = 0x00010501;
*(vu32 *)0x10400478 = 0;
*(vu32 *)0x10400490 = 0x000002D0;
*(vu32 *)0x1040049C = 0x00000000;

// Disco register
for(u32 i = 0; i < 256; i++)
*(vu32 *)0x10400484 = 0x10101 * i;

// Bottom screen
*(vu32 *)0x10400500 = 0x000001c2;
*(vu32 *)0x10400504 = 0x000000d1;
*(vu32 *)0x10400508 = 0x000001c1;
*(vu32 *)0x1040050c = 0x000001c1;
*(vu32 *)0x10400510 = 0x000000cd;
*(vu32 *)0x10400514 = 0x000000cf;
*(vu32 *)0x10400518 = 0x000000d1;
*(vu32 *)0x1040051c = 0x01c501c1;
*(vu32 *)0x10400520 = 0x00010000;
*(vu32 *)0x10400524 = 0x0000019d;
*(vu32 *)0x10400528 = 0x00000052;
*(vu32 *)0x1040052c = 0x00000192;
*(vu32 *)0x10400530 = 0x00000192;
*(vu32 *)0x10400534 = 0x0000004f;
*(vu32 *)0x10400538 = 0x00000050;
*(vu32 *)0x1040053c = 0x00000052;
*(vu32 *)0x10400540 = 0x01980194;
*(vu32 *)0x10400544 = 0x00000000;
*(vu32 *)0x10400548 = 0x00000011;
*(vu32 *)0x1040055C = 0x00f00140;
*(vu32 *)0x10400560 = 0x01c100d1;
*(vu32 *)0x10400564 = 0x01920052;
*(vu32 *)0x10400568 = 0x18300000 + 0x46500;
*(vu32 *)0x10400570 = 0x80301;
*(vu32 *)0x10400574 = 0x00010501;
*(vu32 *)0x10400578 = 0;
*(vu32 *)0x10400590 = 0x000002D0;
*(vu32 *)0x1040059C = 0x00000000;

// Disco register
for(u32 i = 0; i < 256; i++)
*(vu32 *)0x10400584 = 0x10101 * i;

*(vu32 *)0x10400468 = 0x18300000;
*(vu32 *)0x1040046c = 0x18300000;
*(vu32 *)0x10400494 = 0x18300000;
*(vu32 *)0x10400498 = 0x18300000;
*(vu32 *)0x10400568 = 0x18346500;
*(vu32 *)0x1040056c = 0x18346500;

//Set CakeBrah framebuffers
*((vu32 *)0x23FFFE00) = 0x18300000;
*((vu32 *)0x23FFFE04) = 0x18300000;
*((vu32 *)0x23FFFE08) = 0x18346500;

//Clear ARM11 entry offset
*arm11 = 0;

//Wait for the entry to be set
while(!*arm11);

//Jump to it
((void (*)())*arm11)();
}
10 changes: 10 additions & 0 deletions external/screeninit/source/start.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.section .text.start
.align 4
.global _start
_start:
@ Disable interrupts
CPSID aif

b main

.word 0
13 changes: 13 additions & 0 deletions external/screeninit/source/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <stdint.h>

//Common data types
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef volatile u8 vu8;
typedef volatile u16 vu16;
typedef volatile u32 vu32;
typedef volatile u64 vu64;
2 changes: 1 addition & 1 deletion external/svc/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PATH := $(PATH):$(DEVKITARM)/bin

all: 7b.bin stub.bin
all: 7b.bin stub.bin emunand.bin

%.o: %.s
arm-none-eabi-as -o $@ $<
Expand Down
45 changes: 45 additions & 0 deletions external/svc/emunand.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
.section .text
.global _start
_start:
// Original code that still needs to be executed.
mov r4, r0
mov r5, r1
mov r7, r2
mov r6, r3

main:
// If we're already trying to access the SD, return.
ldr r2, [r0, #4]
ldr r1, sdmmc // In armips this instruction uses pc-releative loading
cmp r2, r1
beq nand_sd_ret

str r1, [r0, #4] // Set object to be SD
ldr r2, [r0, #8] // Get sector to read
cmp r2, #0 // Gateway compat

ldr r3, nand_offset // ^ see above
add r2, r3 // Add the offset to the NAND in the SD.

ldreq r3, ncsd_offset // ^ see above
addeq r2, r3

str r2, [r0, #8] // Store sector to read

nand_sd_ret:
// Restore registers.
mov r0, r4
mov r1, r5
mov r2, r7
mov r3, r6

// Return 4 bytes behind where we got called,
// due to the offset of this function being stored there.
mov r0, lr
add r0, #4
bx r0

sdmmc: .ascii "SDMC" // The offset of the sdmmc object.
nand_offset: .ascii "NAND" // The starting offset of the emuNAND on the SD.
ncsd_offset: .ascii "NCSD" // Location of the NCSD header relative to nand_offset

Loading

0 comments on commit 2acb102

Please sign in to comment.