Skip to content

Commit

Permalink
Merge pull request #9 from dark-samus/master
Browse files Browse the repository at this point in the history
Screen init in stage 2
  • Loading branch information
jasondellaluce committed Feb 27, 2016
2 parents 2841057 + db13325 commit 5d7e4ca
Show file tree
Hide file tree
Showing 10 changed files with 377 additions and 29 deletions.
32 changes: 18 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY : all hax firm0 firm1 sector stage2 installer clean
.PHONY : all hax firm0 firm1 sector screen_init stage2 installer clean

TARGET = arm9loaderhax
PYTHON = python
Expand All @@ -7,29 +7,34 @@ OUTDIR = data_output

all : $(OUTDIR) hax installer

hax : $(OUTDIR) firm0 firm1 sector stage2
hax : $(OUTDIR) firm0 firm1 sector screen_init stage2

$(OUTDIR):
@[ -d $(OUTDIR) ] || mkdir -p $(OUTDIR)

firm0 :
firm0:
@cd payload_stage1 && make
@cp $(INDIR)/new3ds90.firm $(OUTDIR)/firm0.bin
@dd if=payload_stage1/payload_stage1.bin of=$(OUTDIR)/firm0.bin bs=512 seek=1922 conv=notrunc
@echo FIRM0 done!

firm1 :
firm1:
@cp $(INDIR)/new3ds10.firm $(OUTDIR)/firm1.bin
@echo FIRM1 done!

sector :
sector:
@$(PYTHON) common/sector_generator.py $(INDIR)/secret_sector.bin $(INDIR)/otp.bin $(OUTDIR)/sector.bin
@echo SECTOR done!

screen_init:
@[ -d payload_stage2/data ] || mkdir -p payload_stage2/data
$(MAKE) -C screen_init
@cp screen_init/screen_init.bin payload_stage2/data/

stage2:
@cd payload_stage2 && make
@dd if=payload_stage2/payload_stage2.bin of=$(OUTDIR)/firm1.bin bs=512 seek=1936 conv=notrunc
@cp payload_stage2/payload_stage2.bin $(OUTDIR)/stage0x5C000.bin
@cp screen_init/screen_init.bin payload_stage2/data
@$(MAKE) -C payload_stage2
@cp payload_stage2/payload_stage2.bin $(OUTDIR)/stage0x5C000.bin

installer:
@cp $(OUTDIR)/sector.bin payload_installer/brahma2/data/sector.bin
Expand All @@ -38,11 +43,10 @@ installer:
@cp $(OUTDIR)/stage0x5C000.bin payload_installer/brahma2/data/stage2.bin
@cd payload_installer && make TARGET=../$(OUTDIR)/$(TARGET)
@echo INSTALLER done!

clean:
@echo clean...
@cd payload_stage1 && make clean
@cd payload_stage2 && make clean
@cd payload_installer && make clean TARGET=../$(TARGET)
@rm -fr $(OUTDIR) payload_installer/brahma2/data/*.bin

@$(MAKE) -C payload_stage1 clean
@$(MAKE) -C screen_init clean
@$(MAKE) -C payload_stage2 clean
@$(MAKE) -C payload_installer clean TARGET=../$(TARGET)
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ sdmmc.c & sdmmc.h originally written by Normatt
Licensed under GPLv2 or any later version, refer to the license.txt file included.

* Smealum and contributors for libctru
* Normatt for sdmmc.c and .h, and also for .ld files
* Normatt for sdmmc.c and .h, and also for .ld files and the log from 3dmoo9 that provided us with some of the information needed to get screen init
* Christophe Devine for the SHA codes
* Archshift for i2c.c and .h
* Megazig for crypto.c and .h
* Patois for original BRAHMA code
* Smealum, Derrek, Plutoo for publishing the exploit
* Yellows8 and Plutoo as ideators of it
* [3dbrew community](http://3dbrew.org/)
* bilis/b1l1s for his screen init code, and work on inegrating it into stage 2
* dark_samus for work on integrating screen init into stage 2



Expand Down
9 changes: 5 additions & 4 deletions payload_stage2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ CFLAGS += $(INCLUDE)

CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions

ASFLAGS := -g $(ARCH)
ASFLAGS := -g $(ARCH)
LDFLAGS = -nostartfiles -g $(ARCH) -Wl,-Map,$(TARGET).map

LDFLAGS += --specs=../../common/payload_stage2.specs
Expand All @@ -62,6 +62,7 @@ ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------

export OUTPUT := $(CURDIR)/$(TARGET)
export DATA := data

export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
Expand Down Expand Up @@ -104,12 +105,13 @@ all: $(BUILD)
$(BUILD):
@[ -d $(OUTPUT_D) ] || mkdir -p $(OUTPUT_D)
@[ -d $(BUILD) ] || mkdir -p $(BUILD)
@[ -d $(DATA) ] || mkdir -p $(DATA)
@make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile

#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(OUTPUT).bin
@rm -fr $(DATA) $(BUILD) $(OUTPUT).elf $(OUTPUT).bin

#---------------------------------------------------------------------------------
else
Expand All @@ -126,7 +128,6 @@ $(OUTPUT).elf : $(OFILES)
%.bin: %.elf
@$(OBJCOPY) --set-section-flags .bss=alloc,load,contents -O binary $< $@
@echo built ... $(notdir $@)
@rm -f $(OUTPUT).elf

#---------------------------------------------------------------------------------
# you need a rule like this for each extension you use as binary data
Expand Down
47 changes: 37 additions & 10 deletions payload_stage2/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,57 @@
#include "sdmmc.h"
#include "i2c.h"
#include "fatfs/ff.h"
#include "screen_init.h"

#define PAYLOAD_ADDRESS 0x23F00000
#define PAYLOAD_SIZE 0x00100000
#define A11_PAYLOAD_LOC 0x1FFF4C80 //keep in mind this needs to be changed in the ld script for screen_init too
#define SCREEN_SIZE 400 * 240 * 3 / 4 //yes I know this is more than the size of the bootom screen

u8 arm11code[] = {
0x3E, 0x02, 0xE0, 0xE3, 0x1C, 0x10, 0x9F, 0xE5,
0x00, 0x10, 0x80, 0xE5, 0x7E, 0x02, 0xE0, 0xE3,
0x00, 0x10, 0xA0, 0xE3, 0x00, 0x10, 0x80, 0xE5,
0x00, 0x20, 0x90, 0xE5, 0x02, 0x00, 0x51, 0xE1,
0xFC, 0xFF, 0xFF, 0x0A, 0x12, 0xFF, 0x2F, 0xE1,
0xBE, 0xBA, 0xAD, 0xAB,
};

extern u8 screen_init_bin[];
extern u32 screen_init_bin_size;

void ownArm11()
{
memcpy((void*)0x1FFF4C80, arm11code, sizeof(arm11code));
memcpy((void*)A11_PAYLOAD_LOC, screen_init_bin, screen_init_bin_size);
*((u32*)0x1FFAED80) = 0xE51FF004;
*((u32*)0x1FFAED84) = 0x1FFF4C80;
*((u32*)0x1FFAED84) = A11_PAYLOAD_LOC;
for(int i = 0; i < 0x80000; i++)
{
*((u8*)0x1FFFFFF0) = 2;
}
for(volatile unsigned int i = 0; i < 0xF; ++i);
while(*(volatile uint32_t *)0x1FFFFFF8 != 0);
}

//fixes the snow issue
clearScreen()
{
for(int i = 0; i < (SCREEN_SIZE); i++)
{
*((unsigned int*)0x18300000 + i) = 0;
*((unsigned int*)0x18346500 + i) = 0;
}
}

int main()
{
//gateway
*(volatile uint32_t*)0x80FFFC0 = 0x18300000; // framebuffer 1 top left
*(volatile uint32_t*)0x80FFFC4 = 0x18300000; // framebuffer 2 top left
*(volatile uint32_t*)0x80FFFC8 = 0x18300000; // framebuffer 1 top right
*(volatile uint32_t*)0x80FFFCC = 0x18300000; // framebuffer 2 top right
*(volatile uint32_t*)0x80FFFD0 = 0x18346500; // framebuffer 1 bottom
*(volatile uint32_t*)0x80FFFD4 = 0x18346500; // framebuffer 2 bottom
*(volatile uint32_t*)0x80FFFD8 = 1; // framebuffer select top
*(volatile uint32_t*)0x80FFFDC = 1; // framebuffer select bottom

//cakehax
*(u32*)0x23FFFE00 = 0x18300000;
*(u32*)0x23FFFE04 = 0x18300000;
*(u32*)0x23FFFE08 = 0x18346500;

FATFS fs;
FIL payload;
u32 br;
Expand All @@ -38,6 +63,8 @@ int main()
{
f_read(&payload, PAYLOAD_ADDRESS, PAYLOAD_SIZE, &br);
ownArm11();
screenInit();
clearScreen();
((void (*)())PAYLOAD_ADDRESS)();
}
}
Expand Down
8 changes: 8 additions & 0 deletions payload_stage2/source/screen_init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "i2c.h"

void screenInit()
{

i2cWriteRegister(3, 0x22, 0x2A); // 0x2A -> boot into firm with no backlight

}
1 change: 1 addition & 0 deletions payload_stage2/source/screen_init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void screenInit();
59 changes: 59 additions & 0 deletions screen_init/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export PATH := $(DEVKITARM)/bin:$(PATH)

CC=arm-none-eabi-gcc
CP=arm-none-eabi-g++
OC=arm-none-eabi-objcopy
LD=arm-none-eabi-ld
MV=mv -f
RM=rm -rf

LIBNAME=screen_init
ELFNAME=$(LIBNAME).elf
BINNAME=$(LIBNAME).bin
DATNAME=$(LIBNAME).dat

SRC_DIR:=source/$(LIBNAME)
OBJ_DIR:=obj/$(LIBNAME)
LIB_DIR:=lib
DEP_DIR:=obj/$(LIBNAME)

LIBS=
CFLAGS=-std=gnu99 -Os -g -mword-relocations -fomit-frame-pointer -ffast-math
C9FLAGS=-mcpu=arm946e-s -march=armv5te -mlittle-endian
C11FLAGS=-mcpu=mpcore -mlittle-endian
LDFLAGS=$(LIBS)
OCFLAGS=--set-section-flags .bss=alloc,load,contents

OBJS:=$(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(wildcard $(SRC_DIR)/*.c))
OBJS+=$(patsubst $(SRC_DIR)/%.s, $(OBJ_DIR)/%.o, $(wildcard $(SRC_DIR)/*.s))
OBJS+=$(patsubst $(SRC_DIR)/%.S, $(OBJ_DIR)/%.o, $(wildcard $(SRC_DIR)/*.S))

OUT_DIR= obj/$(LIBNAME)

.PHONY: clean

all: screen_init.bin

screen_init.bin: $(OBJS)
$(CC) -nostartfiles --specs=$(LIBNAME).specs $(OBJS) $(LDFLAGS) -o $(ELFNAME)
$(OC) $(OCFLAGS) -O binary $(ELFNAME) $(BINNAME)

obj/%.o: source/%.c | dirs
@echo Compiling $<
$(CC) -c $(CFLAGS) $(C11FLAGS) $< -o $@

obj/%.o: source/%.s | dirs
@echo Compiling $<
$(CC) -c $(CFLAGS) $(C11FLAGS) $< -o $@

obj/%.o: source/%.S | dirs
@echo Compiling $<
$(CC) -c $(CFLAGS) $(C11FLAGS) $< -o $@

dirs: ${OUT_DIR}

${OUT_DIR}:
mkdir -p ${OUT_DIR}

clean:
rm -rf *.elf *.bin obj/*
21 changes: 21 additions & 0 deletions screen_init/screen_init.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
OUTPUT_ARCH(arm)
ENTRY(_entry)
SECTIONS
{
. = 0x1FFF4C80;
.text : {
*(.text.start)
*(.text)
}
.data : {
*(.data)
}
.bss : {
*(.bss COMMON)
}
.rodata : {
*(.rodata)
}
. = ALIGN(4);
_stack = . + 0x100;
}
4 changes: 4 additions & 0 deletions screen_init/screen_init.specs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%rename link old_link

*link:
%(old_link) -T screen_init.ld%s
Loading

2 comments on commit 5d7e4ca

@mariogamer2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lavanoid Good idea

Seem it is related with #13 .

@pixel-stuck
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lavanoid well there are several issues with that.... first thing's first, NATIVE_FIRM version, we have more than enough space to store some stuff in firm0 and 1 slots, but seeing that certain versions of firm aren't compatible with certain sysNAND versions, it's hard to make a solution that works for everyone, as there are already a few different schools of thought surrounding a9lh... Take for example, users (like me) who like to keep emuNAND, but keep that on 9.2 and sysNAND on the latest version, now we have to embed the 10.4 NATIVE_FIRM into the NAND, along with a payload to make it so they can load it... but what happens when they update to the next system version, and payload aren't compatible with the 10.4 firm? Well they're bricked until they can put this firmware back... then you have the users who want sysNAND to stay 9.2 and just use emuNAND for all of their needs, that problem is fairly simple to solve because they don't ever need to update the firm in NAND, but managing a good solution that supports both sides isn't exactly the easiest. That said we aren't giving up on it, however we have other uses for that extra space which will be more useful IMO than worrying about not having an SD card

Please sign in to comment.