Compare commits
334 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c73fec46e6 | ||
|
|
b493e2bd5f | ||
|
|
78a7d6c5af | ||
|
|
a8b2e9a50c | ||
|
|
2d7c1e42d8 | ||
|
|
64ebec3ea4 | ||
|
|
252d32aafe | ||
|
|
2349370960 | ||
|
|
6ccc0f0538 | ||
|
|
9f12a47cfc | ||
|
|
ee880802c8 | ||
|
|
ab14e77b50 | ||
|
|
1d4245e582 | ||
|
|
8636a74ba4 | ||
|
|
3b766de306 | ||
|
|
1f300e1b75 | ||
|
|
56efe6035f | ||
|
|
73e363d159 | ||
|
|
3ac134bc16 | ||
|
|
c7bb1f9655 | ||
|
|
7847646b3e | ||
|
|
ec6e9f73bb | ||
|
|
bd62f033ab | ||
|
|
ba88ec25f6 | ||
|
|
b9bc41a772 | ||
|
|
e029458b09 | ||
|
|
b3ecf1776b | ||
|
|
5fe83588eb | ||
|
|
18b8eaabae | ||
|
|
e11695c2f0 | ||
|
|
91edbf71c8 | ||
|
|
5b9b22021b | ||
|
|
033d90b866 | ||
|
|
540c0df918 | ||
|
|
6de167467f | ||
|
|
c746326eb3 | ||
|
|
0fbd384a73 | ||
|
|
e8d4a98c7b | ||
|
|
f5aa63936c | ||
|
|
62a57d0f47 | ||
|
|
958fb4c739 | ||
|
|
e231275cbe | ||
|
|
5a404c3a35 | ||
|
|
b1c07c6204 | ||
|
|
60f158d488 | ||
|
|
1887c6fae3 | ||
|
|
725a825762 | ||
|
|
b81f59d5ae | ||
|
|
39c2b8927b | ||
|
|
89dfdac7bf | ||
|
|
c087edf2ba | ||
|
|
db33c315f2 | ||
|
|
c7e7dd8248 | ||
|
|
b10d82a883 | ||
|
|
e151b3d4ef | ||
|
|
5ccf4a45c1 | ||
|
|
c5369a5cad | ||
|
|
b5eba765a5 | ||
|
|
a5ddc38477 | ||
|
|
8d102256a2 | ||
|
|
2e561f7ea9 | ||
|
|
9656fe1b6f | ||
|
|
48c23f2a43 | ||
|
|
6d82649c3c | ||
|
|
81dea35754 | ||
|
|
fdbe43421b | ||
|
|
0d71560785 | ||
|
|
108e8a0cd4 | ||
|
|
653e81c48e | ||
|
|
0dc0783094 | ||
|
|
13ef1bf6be | ||
|
|
7ea80353f6 | ||
|
|
07bbff7d11 | ||
|
|
2ff4fc3cdd | ||
|
|
c9456055ea | ||
|
|
2900f2d67b | ||
|
|
93c8c90804 | ||
|
|
36e54642d2 | ||
|
|
e04cb28711 | ||
|
|
ad1dc51e06 | ||
|
|
5b6bab7a58 | ||
|
|
9bd2fc659d | ||
|
|
f18633f65f | ||
|
|
1026bc7b4f | ||
|
|
f1dee68142 | ||
|
|
f7cc2d295c | ||
|
|
a6f254b5cf | ||
|
|
00dbbe1b28 | ||
|
|
7f33309903 | ||
|
|
c95808fa2d | ||
|
|
5b6bd048a9 | ||
|
|
28e6ad3372 | ||
|
|
9b128ebba5 | ||
|
|
71f49180c3 | ||
|
|
4a042241a8 | ||
|
|
0eab9127e3 | ||
|
|
9f11991410 | ||
|
|
fec0b95098 | ||
|
|
5081439f53 | ||
|
|
c44aebee4d | ||
|
|
706cd50f25 | ||
|
|
b1b81c87f2 | ||
|
|
bf7e30539e | ||
|
|
5f8a61201b | ||
|
|
ed8aee8b8c | ||
|
|
92ec2af587 | ||
|
|
7960c87579 | ||
|
|
700d572732 | ||
|
|
014a0d86f1 | ||
|
|
88db59405f | ||
|
|
ef2e008700 | ||
|
|
07101c053a | ||
|
|
6c5f6ac475 | ||
|
|
61ecd9a617 | ||
|
|
6f56a9bfe9 | ||
|
|
416875ee46 | ||
|
|
c875b506ea | ||
|
|
ff4517e583 | ||
|
|
92cc989dc9 | ||
|
|
6e5987e3ca | ||
|
|
f03e232b90 | ||
|
|
1eb18c1790 | ||
|
|
3076d56973 | ||
|
|
b35707edf9 | ||
|
|
dfbd0dc9e7 | ||
|
|
620ad7ba71 | ||
|
|
5e4fd53243 | ||
|
|
0ba7630354 | ||
|
|
028d0ec0d5 | ||
|
|
751fa05fcd | ||
|
|
f89845257e | ||
|
|
2f274e2f47 | ||
|
|
38088e80e1 | ||
|
|
14162828ea | ||
|
|
ab8507e09d | ||
|
|
0d25c07333 | ||
|
|
bfc8ba8447 | ||
|
|
a45f8293d9 | ||
|
|
f29b9d14d7 | ||
|
|
9c9fd2deef | ||
|
|
3bb01ffd68 | ||
|
|
bc6d9994dc | ||
|
|
e177f9e0fe | ||
|
|
c5d75d2de9 | ||
|
|
cc0cade4d2 | ||
|
|
1e3362250f | ||
|
|
db16e8d602 | ||
|
|
2a563eddd0 | ||
|
|
6b8474b7b9 | ||
|
|
b5336c81cc | ||
|
|
1fcab825bf | ||
|
|
0306556032 | ||
|
|
b093578046 | ||
|
|
9332b9eb33 | ||
|
|
141c7817a0 | ||
|
|
f155026d8f | ||
|
|
37e467ba60 | ||
|
|
3572b835b5 | ||
|
|
da4f3a72af | ||
|
|
2938bbd11f | ||
|
|
abf3017eb2 | ||
|
|
5c855ea52f | ||
|
|
c83edea7ad | ||
|
|
6d3113c8c3 | ||
|
|
48c48c7bbc | ||
|
|
e4093ed988 | ||
|
|
c79af52720 | ||
|
|
61eeaca6d5 | ||
|
|
320a79ba72 | ||
|
|
2e1b943805 | ||
|
|
bc167dde2d | ||
|
|
7d9a8b4211 | ||
|
|
0b16d88756 | ||
|
|
796cb31ed7 | ||
|
|
d7fd2f26c1 | ||
|
|
5adb8749de | ||
|
|
3474faa4a2 | ||
|
|
acd9c04ff6 | ||
|
|
833c9406b0 | ||
|
|
2f1253e27f | ||
|
|
53b847e31c | ||
|
|
7efa33dd7f | ||
|
|
4011970a57 | ||
|
|
a2cfa2be16 | ||
|
|
c4b691d688 | ||
|
|
72a7a8eee5 | ||
|
|
52d352385f | ||
|
|
c1f85650bd | ||
|
|
b830909504 | ||
|
|
4ad6b1c220 | ||
|
|
429488a4ba | ||
|
|
40c6cc11a5 | ||
|
|
594881c6ce | ||
|
|
1cc64a0fbc | ||
|
|
f492318e3c | ||
|
|
4b06bb7795 | ||
|
|
f7e570383a | ||
|
|
896a088199 | ||
|
|
f3322bd003 | ||
|
|
cef947d67d | ||
|
|
b6640d118d | ||
|
|
273d0b2ce9 | ||
|
|
9b724d776e | ||
|
|
3eaa706ccf | ||
|
|
b4c854dfe8 | ||
|
|
9cdadbe834 | ||
|
|
b3f38a8764 | ||
|
|
5d868284c6 | ||
|
|
035751980d | ||
|
|
6b80bc08d5 | ||
|
|
2089959d1b | ||
|
|
67b00ec28d | ||
|
|
127ae6b945 | ||
|
|
4f53b3ce35 | ||
|
|
c3092b482a | ||
|
|
080219f88d | ||
|
|
3fd783cd01 | ||
|
|
211cd964d7 | ||
|
|
04f42f0be4 | ||
|
|
e00ef893d0 | ||
|
|
858efa604e | ||
|
|
b63b17c54f | ||
|
|
f0e111c20e | ||
|
|
7339f57138 | ||
|
|
1e39c999f9 | ||
|
|
62f7a06192 | ||
|
|
a0531b7930 | ||
|
|
0619d04939 | ||
|
|
cd76476d26 | ||
|
|
53b6c17e33 | ||
|
|
3b7b66b272 | ||
|
|
a795a45c34 | ||
|
|
b58cbd228c | ||
|
|
fc994285f9 | ||
|
|
d5e74b91c7 | ||
|
|
c5eb2e1070 | ||
|
|
d613cb057e | ||
|
|
121792bebe | ||
|
|
e07c230106 | ||
|
|
d5ce3044c8 | ||
|
|
98ff43b4d2 | ||
|
|
1704fbcd62 | ||
|
|
014ac1cf72 | ||
|
|
b499c7ee75 | ||
|
|
2e069e326c | ||
|
|
e47d42da22 | ||
|
|
615e5dfaa7 | ||
|
|
fde2c371ef | ||
|
|
a0b4e7fd5d | ||
|
|
45c36bbcae | ||
|
|
66c041ad93 | ||
|
|
32d5c52b5f | ||
|
|
9cf5b01633 | ||
|
|
d4cf22d370 | ||
|
|
973640f023 | ||
|
|
248ea82f76 | ||
|
|
a868079a93 | ||
|
|
d270d5b9ca | ||
|
|
e9692a438b | ||
|
|
06ea123dbd | ||
|
|
85141d5eda | ||
|
|
fa13b8fbd0 | ||
|
|
5b4712644a | ||
|
|
aa422914bd | ||
|
|
22c453e297 | ||
|
|
e5f40cec5a | ||
|
|
50b24bf6c2 | ||
|
|
b575ee9e28 | ||
|
|
37030621ac | ||
|
|
f005da4d12 | ||
|
|
6295559d9c | ||
|
|
f36ff303d9 | ||
|
|
1a62e91c01 | ||
|
|
7f314dfe11 | ||
|
|
0caf9f4214 | ||
|
|
1ad600c81a | ||
|
|
bd6c7b7fdb | ||
|
|
5f93724845 | ||
|
|
ad60eac6ef | ||
|
|
b3e3a2937a | ||
|
|
d010038228 | ||
|
|
c28eada93e | ||
|
|
9d84a92b1f | ||
|
|
7884be106d | ||
|
|
5fe7c7e7e1 | ||
|
|
f244b95aad | ||
|
|
3b5a5759b8 | ||
|
|
528e7ee33b | ||
|
|
c1f55735fc | ||
|
|
185ea86284 | ||
|
|
618262f015 | ||
|
|
c0f41ac10e | ||
|
|
3010699f20 | ||
|
|
fb274538e1 | ||
|
|
1a5d953a98 | ||
|
|
2dd1baebb3 | ||
|
|
d46beac22a | ||
|
|
6ed8741006 | ||
|
|
6845e42f2c | ||
|
|
5196869634 | ||
|
|
083806bfc9 | ||
|
|
11d29368ce | ||
|
|
100c3d9e36 | ||
|
|
01fbf8c3a5 | ||
|
|
a36556d7e4 | ||
|
|
4f11162fae | ||
|
|
2cbaf39fd5 | ||
|
|
2cc46c618b | ||
|
|
a0325e91f9 | ||
|
|
e11edd5dee | ||
|
|
5e8990f571 | ||
|
|
a3fd55036a | ||
|
|
7a3d15c48b | ||
|
|
a5b52a2470 | ||
|
|
a8cd14dafd | ||
|
|
2b249bd496 | ||
|
|
c8586cfe26 | ||
|
|
f228cb241f | ||
|
|
fd33ef8496 | ||
|
|
ba1cf6473a | ||
|
|
f418dcdb7b | ||
|
|
f91c26d752 | ||
|
|
f7156f2ff2 | ||
|
|
e444b587cf | ||
|
|
a7fcc6a5cf | ||
|
|
872b1ccbb6 | ||
|
|
b7b3400296 | ||
|
|
420ccdcb82 | ||
|
|
eaa4d6323e | ||
|
|
4e7ac41a6c | ||
|
|
92f3a736a7 | ||
|
|
5a30b2b298 | ||
|
|
85aaae67bf | ||
|
|
efe66bc72e |
87
.github/ISSUE_TEMPLATE.md
vendored
Normal file
87
.github/ISSUE_TEMPLATE.md
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
<!--
|
||||
-- THIS IS NOT A SUPPORT FORUM! For support please go to:
|
||||
-- Luma3DS GBATemp thread: https://gbatemp.net/threads/luma3ds-noob-proof-3ds-custom-firmware.411110/
|
||||
-- Nintendo Hacking: https://discord.gg/MjzatM8y
|
||||
--
|
||||
-- Also check the Wiki (https://github.com/AuroraWright/Luma3DS/wiki) before making an issue.
|
||||
--
|
||||
-- For those with GBA/DSiWare/DS/AGB_FIRM/TWL_FIRM problems: https://3ds.guide/troubleshooting
|
||||
-- If you're using an emu/redNAND anything related to that must also be installed to sysNAND.
|
||||
-- Please make sure to read "Enable game patching" https://github.com/AuroraWright/Luma3DS/wiki/Options-and-usage before posting any issues about the "Enable game patching" option(s).
|
||||
--
|
||||
-- Please fill in the placeholders.
|
||||
--
|
||||
-->
|
||||
**System model:** [e.g. 2DS, New 3DS, Old 3DS]
|
||||
|
||||
**SysNAND version (+emu/redNAND version if applicable):** [e.g. 11.4.0-37U]
|
||||
|
||||
**Entrypoint (How/what you're using to boot Luma3DS):** [e.g. A9LH]
|
||||
|
||||
**Luma3DS version:** [e.g. 7.0.5 stable or if using nightly specify the commit like this https://github.com/AuroraWright/Luma3DS/commit/5fe83588eb4b8faafc7773b3739a3d3307160b04]
|
||||
<!--
|
||||
-- You can check which version you're on in the configuration menu. It will be on the top of the screen.
|
||||
-->
|
||||
|
||||
**Luma3DS configuration/options:**
|
||||
|
||||
Default EmuNAND: ( )
|
||||
|
||||
Screen brightness: ( )
|
||||
|
||||
Splash: ( )
|
||||
|
||||
PIN lock: ( )
|
||||
|
||||
New 3DS CPU: ( )
|
||||
<!--
|
||||
-- This option is only available for New 3DS/2DS.
|
||||
-->
|
||||
|
||||
|
||||
--
|
||||
|
||||
Autoboot SysNAND: ( )
|
||||
|
||||
Use SysNAND FIRM if booting with R: ( )
|
||||
|
||||
Enable loading external FIRMs and modules: ( )
|
||||
<!--
|
||||
-- Firmware (.bin) files are not required by Luma, or NTR CFW anymore.
|
||||
-- If you're having issues with this option enabled try deleting them from the luma folder on the root of the SD card and disabling this option.
|
||||
-->
|
||||
|
||||
Use custom path: ( )
|
||||
|
||||
Enable game patching: ( )
|
||||
|
||||
Show NAND or user string in System Settings: ( )
|
||||
|
||||
Show GBA boot screen in patched AGB_FIRM: ( )
|
||||
|
||||
Patch SVC/service/archive/ARM9 access: ( )
|
||||
|
||||
Set developer UNITINFO: ( )
|
||||
|
||||
Enable exception handlers: ( )
|
||||
|
||||
--
|
||||
|
||||
**Explanation of the issue:**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
**Steps to reproduce:**
|
||||
|
||||
1.
|
||||
|
||||
2.
|
||||
|
||||
|
||||
**Dump file:**
|
||||
<!-- If the issue leads to a crash you can generate a crash dump by checking the "Enable exception handlers" option.
|
||||
-- The error message will tell you where the dump is.
|
||||
Zip the dmp file and drag & drop it below. -->
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,6 +4,7 @@ loader/build
|
||||
injector/build
|
||||
exceptions/arm9/build
|
||||
exceptions/arm11/build
|
||||
haxloader/build
|
||||
*.bin
|
||||
*.3dsx
|
||||
*.smdh
|
||||
|
||||
6
.gitmodules
vendored
6
.gitmodules
vendored
@@ -1,6 +0,0 @@
|
||||
[submodule "CakeBrah"]
|
||||
path = CakeBrah
|
||||
url = https://github.com/mid-kid/CakeBrah
|
||||
[submodule "CakeHax"]
|
||||
path = CakeHax
|
||||
url = https://github.com/mid-kid/CakeHax
|
||||
1
CakeBrah
1
CakeBrah
Submodule CakeBrah deleted from 9f7cea77d4
1
CakeHax
1
CakeHax
Submodule CakeHax deleted from 5245c7b9dc
157
Makefile
157
Makefile
@@ -4,12 +4,7 @@ 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
|
||||
include $(DEVKITARM)/base_tools
|
||||
|
||||
name := Luma3DS
|
||||
revision := $(shell git describe --tags --match v[0-9]* --abbrev=8 | sed 's/-[0-9]*-g/-/i')
|
||||
@@ -22,148 +17,96 @@ dir_injector := injector
|
||||
dir_exceptions := exceptions
|
||||
dir_arm9_exceptions := $(dir_exceptions)/arm9
|
||||
dir_arm11_exceptions := $(dir_exceptions)/arm11
|
||||
dir_mset := CakeHax
|
||||
dir_ninjhax := CakeBrah
|
||||
dir_menuhax := menuhax
|
||||
dir_pathchanger := pathchanger
|
||||
dir_build := build
|
||||
dir_out := out
|
||||
|
||||
ASFLAGS := -mcpu=arm946e-s
|
||||
CFLAGS := -Wall -Wextra -MMD -MP -marm $(ASFLAGS) -fno-builtin -fshort-wchar -std=c11 -Wno-main -O2 -flto -ffast-math
|
||||
CFLAGS := -Wall -Wextra $(ASFLAGS) -fno-builtin -std=c11 -Wno-main -O2 -flto -ffast-math
|
||||
LDFLAGS := -nostartfiles
|
||||
FLAGS := name=$(name).dat dir_out=$(abspath $(dir_out)) ICON=$(abspath icon.png) APP_DESCRIPTION="Noob-friendly 3DS CFW." APP_AUTHOR="Aurora Wright/TuxSH" --no-print-directory
|
||||
|
||||
objects = $(patsubst $(dir_source)/%.s, $(dir_build)/%.o, \
|
||||
$(patsubst $(dir_source)/%.c, $(dir_build)/%.o, \
|
||||
$(call rwildcard, $(dir_source), *.s *.c)))
|
||||
|
||||
bundled = $(dir_build)/rebootpatch.h $(dir_build)/emunandpatch.h $(dir_build)/svcGetCFWInfopatch.h $(dir_build)/injector.h $(dir_build)/loader.h
|
||||
bundled = $(dir_build)/reboot.bin.o $(dir_build)/emunand.bin.o $(dir_build)/svcGetCFWInfo.bin.o $(dir_build)/k11modules.bin.o \
|
||||
$(dir_build)/injector.bin.o $(dir_build)/loader.bin.o $(dir_build)/arm9_exceptions.bin.o $(dir_build)/arm11_exceptions.bin.o
|
||||
|
||||
ifeq ($(strip $(DEV)),TRUE)
|
||||
CFLAGS += -DDEV
|
||||
bundled += $(dir_build)/k11modulespatch.h $(dir_build)/arm9_exceptions.h $(dir_build)/arm11_exceptions.h
|
||||
title := \"$(name) $(revision) (dev) configuration\"
|
||||
else
|
||||
title := \"$(name) $(revision) configuration\"
|
||||
endif
|
||||
define bin2o
|
||||
bin2s $< | $(AS) -o $(@)
|
||||
endef
|
||||
|
||||
.PHONY: all
|
||||
all: launcher a9lh ninjhax menuhax
|
||||
|
||||
.PHONY: launcher
|
||||
launcher: $(dir_out)/$(name).dat
|
||||
|
||||
.PHONY: a9lh
|
||||
a9lh: $(dir_out)/arm9loaderhax.bin
|
||||
|
||||
.PHONY: ninjhax
|
||||
ninjhax: $(dir_out)/3ds/$(name)
|
||||
|
||||
.PHONY: menuhax
|
||||
menuhax: $(dir_out)/menuhax/boot.3dsx
|
||||
all: firm
|
||||
|
||||
.PHONY: release
|
||||
ifeq ($(strip $(DEV)),TRUE)
|
||||
release: $(dir_out)/$(name)$(revision)-dev.7z
|
||||
else
|
||||
release: $(dir_out)/$(name)$(revision).7z
|
||||
endif
|
||||
|
||||
.PHONY: firm
|
||||
firm: $(dir_out)/boot.firm
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
@$(MAKE) $(FLAGS) -C $(dir_mset) clean
|
||||
@$(MAKE) $(FLAGS) -C $(dir_ninjhax) clean
|
||||
@$(MAKE) -C $(dir_loader) clean
|
||||
@$(MAKE) -C $(dir_arm9_exceptions) clean
|
||||
@$(MAKE) -C $(dir_arm11_exceptions) clean
|
||||
@$(MAKE) -C $(dir_arm11_exceptions) clean
|
||||
@$(MAKE) -C $(dir_injector) clean
|
||||
@rm -rf $(dir_out) $(dir_build)
|
||||
|
||||
$(dir_out):
|
||||
@mkdir -p "$(dir_out)"
|
||||
.PRECIOUS: $(dir_build)/%.bin
|
||||
|
||||
$(dir_out)/$(name).dat: $(dir_build)/main.bin $(dir_out)
|
||||
@$(MAKE) $(FLAGS) -C $(dir_mset) launcher
|
||||
@dd if=$(dir_build)/main.bin of=$@ bs=512 seek=144
|
||||
.PHONY: $(dir_loader)
|
||||
.PHONY: $(dir_arm9_exceptions)
|
||||
.PHONY: $(dir_arm11_exceptions)
|
||||
.PHONY: $(dir_injector)
|
||||
|
||||
$(dir_out)/menuhax/boot.3dsx: $(dir_menuhax)/menuhax.diff $(dir_out)
|
||||
$(dir_out)/$(name)$(revision).7z: all
|
||||
@mkdir -p "$(@D)"
|
||||
@cd $(dir_ninjhax); patch -p1 < ../$(dir_menuhax)/menuhax.diff; $(MAKE) $(FLAGS); git reset --hard
|
||||
@mv $(dir_out)/$(name).3dsx $@
|
||||
@rm $(dir_out)/$(name).smdh
|
||||
|
||||
$(dir_out)/arm9loaderhax.bin: $(dir_build)/main.bin $(dir_out)
|
||||
@cp -a $(dir_build)/main.bin $@
|
||||
|
||||
$(dir_out)/3ds/$(name): $(dir_out)
|
||||
@mkdir -p "$@"
|
||||
@$(MAKE) $(FLAGS) -C $(dir_ninjhax)
|
||||
@mv $(dir_out)/$(name).3dsx $(dir_out)/$(name).smdh $@
|
||||
|
||||
$(dir_out)/pathchanger: $(dir_pathchanger)/pathchanger.py $(dir_pathchanger)/prebuilt $(dir_out)
|
||||
@mkdir -p "$@"
|
||||
@cp $(dir_pathchanger)/pathchanger.py $@
|
||||
@cp -rfT $(dir_pathchanger)/prebuilt $@
|
||||
|
||||
$(dir_out)/$(name)$(revision).7z: all $(dir_out)/pathchanger
|
||||
@7z a -mx $@ ./$(@D)/*
|
||||
|
||||
$(dir_out)/$(name)$(revision)-dev.7z: all $(dir_out)/pathchanger
|
||||
@7z a -mx $@ ./$(@D)/* ./$(dir_exceptions)/exception_dump_parser.py
|
||||
|
||||
$(dir_build)/main.bin: $(dir_build)/main.elf
|
||||
$(OC) -S -O binary $< $@
|
||||
$(dir_out)/boot.firm: $(dir_build)/main.elf
|
||||
@mkdir -p "$(@D)"
|
||||
@firmtool build $@ -e 0 -D $^ -C NDMA
|
||||
|
||||
$(dir_build)/main.elf: $(objects)
|
||||
$(dir_build)/main.elf: $(bundled) $(objects)
|
||||
$(LINK.o) -T linker.ld $(OUTPUT_OPTION) $^
|
||||
|
||||
$(dir_build)/emunandpatch.h: $(dir_patches)/emunand.s
|
||||
$(dir_build)/%.bin.o: $(dir_build)/%.bin
|
||||
@$(bin2o)
|
||||
|
||||
$(dir_build)/injector.bin: $(dir_injector)
|
||||
@mkdir -p "$(@D)"
|
||||
@$(MAKE) -C $<
|
||||
|
||||
$(dir_build)/loader.bin: $(dir_loader)
|
||||
@mkdir -p "$(@D)"
|
||||
@$(MAKE) -C $<
|
||||
|
||||
$(dir_build)/arm9_exceptions.bin: $(dir_arm9_exceptions)
|
||||
@mkdir -p "$(@D)"
|
||||
@$(MAKE) -C $<
|
||||
|
||||
$(dir_build)/arm11_exceptions.bin: $(dir_arm11_exceptions)
|
||||
@mkdir -p "$(@D)"
|
||||
@$(MAKE) -C $<
|
||||
|
||||
$(dir_build)/%.bin: $(dir_patches)/%.s
|
||||
@mkdir -p "$(@D)"
|
||||
@armips $<
|
||||
@bin2c -o $@ -n emunand $(@D)/emunand.bin
|
||||
|
||||
$(dir_build)/rebootpatch.h: $(dir_patches)/reboot.s
|
||||
@mkdir -p "$(@D)"
|
||||
@armips $<
|
||||
@bin2c -o $@ -n reboot $(@D)/reboot.bin
|
||||
|
||||
$(dir_build)/svcGetCFWInfopatch.h: $(dir_patches)/svcGetCFWInfo.s
|
||||
@mkdir -p "$(@D)"
|
||||
@armips $<
|
||||
@bin2c -o $@ -n svcGetCFWInfo $(@D)/svcGetCFWInfo.bin
|
||||
|
||||
$(dir_build)/injector.h: $(dir_injector)/Makefile
|
||||
@mkdir -p "$(@D)"
|
||||
@$(MAKE) -C $(dir_injector) DEV=$(DEV)
|
||||
@bin2c -o $@ -n injector $(@D)/injector.cxi
|
||||
|
||||
$(dir_build)/loader.h: $(dir_loader)/Makefile
|
||||
@$(MAKE) -C $(dir_loader)
|
||||
@bin2c -o $@ -n loader $(@D)/loader.bin
|
||||
|
||||
$(dir_build)/k11modulespatch.h: $(dir_patches)/k11modules.s
|
||||
@mkdir -p "$(@D)"
|
||||
@armips $<
|
||||
@bin2c -o $@ -n k11modules $(@D)/k11modules.bin
|
||||
|
||||
$(dir_build)/arm9_exceptions.h: $(dir_arm9_exceptions)/Makefile
|
||||
@$(MAKE) -C $(dir_arm9_exceptions)
|
||||
@bin2c -o $@ -n arm9_exceptions $(@D)/arm9_exceptions.bin
|
||||
|
||||
$(dir_build)/arm11_exceptions.h: $(dir_arm11_exceptions)/Makefile
|
||||
@$(MAKE) -C $(dir_arm11_exceptions)
|
||||
@bin2c -o $@ -n arm11_exceptions $(@D)/arm11_exceptions.bin
|
||||
|
||||
$(dir_build)/memory.o $(dir_build)/strings.o: CFLAGS += -O3
|
||||
$(dir_build)/config.o: CFLAGS += -DCONFIG_TITLE="$(title)"
|
||||
$(dir_build)/config.o: CFLAGS += -DCONFIG_TITLE="\"$(name) $(revision) configuration\""
|
||||
$(dir_build)/patches.o: CFLAGS += -DREVISION=\"$(revision)\" -DCOMMIT_HASH="0x$(commit)"
|
||||
|
||||
$(dir_build)/%.o: $(dir_source)/%.c $(bundled)
|
||||
$(dir_build)/bundled.h: $(bundled)
|
||||
@$(foreach f, $(bundled),\
|
||||
echo "extern const u8" `(echo $(basename $(notdir $(f))) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"[];" >> $@;\
|
||||
echo "extern const u32" `(echo $(basename $(notdir $(f)))| sed -e 's/^\([0-9]\)/_\1/' | tr . _)`_size";" >> $@;\
|
||||
)
|
||||
|
||||
$(dir_build)/%.o: $(dir_source)/%.c $(dir_build)/bundled.h
|
||||
@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)
|
||||
|
||||
@@ -5,16 +5,17 @@
|
||||
|
||||
**Luma3DS** is a program to patch the system software of (New) Nintendo 3DS handheld consoles "on the fly", adding features (such as per-game language settings and debugging capabilities for developers) and removing restrictions enforced by Nintendo (such as the region lock).
|
||||
It also allows you to run unauthorized ("homebrew") content by removing signature checks.
|
||||
To use it, you will need a console capable of running homebrew software on the ARM9 processor. We recommend [Plailect's guide](https://github.com/Plailect/Guide/wiki) for details on how to get your system ready.
|
||||
To use it, you will need a console capable of running homebrew software on the ARM9 processor. We recommend [Plailect's guide](https://3ds.guide/) for details on how to get your system ready.
|
||||
|
||||
---
|
||||
|
||||
## Compiling
|
||||
|
||||
First you need to clone the repository recursively with: `git clone --recursive https://github.com/AuroraWright/Luma3DS.git`
|
||||
To compile, you'll need [armips](https://github.com/Kingcom/armips), [bin2c](https://sourceforge.net/projects/bin2c/), and a recent build of [makerom](https://github.com/profi200/Project_CTR) added to your PATH.
|
||||
To compile, you'll need [armips](https://github.com/Kingcom/armips) and a build of a recent commit of [makerom](https://github.com/profi200/Project_CTR) added to your PATH.
|
||||
For now, you'll also need to update your [libctru](https://github.com/smealum/ctrulib) install, building from the latest commit.
|
||||
For your convenience, here are [Windows](http://www91.zippyshare.com/v/ePGpjk9r/file.html) and [Linux](https://mega.nz/#!uQ1T1IAD!Q91O0e12LXKiaXh_YjXD3D5m8_W3FuMI-hEa6KVMRDQ) builds of armips (thanks to who compiled them!).
|
||||
Finally just run `make` (for the regular version) or `make DEV=TRUE` (for the dev version) and everything should work!
|
||||
Finally just run `make` and everything should work!
|
||||
You can find the compiled files in the `out` folder.
|
||||
|
||||
---
|
||||
|
||||
@@ -4,20 +4,16 @@ 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
|
||||
include $(DEVKITARM)/base_tools
|
||||
|
||||
name := arm11_exceptions
|
||||
|
||||
dir_source := source
|
||||
dir_build := build
|
||||
dir_out := ../../$(dir_build)
|
||||
|
||||
ASFLAGS := -mcpu=mpcore -mfpu=vfp
|
||||
CFLAGS := -Wall -Wextra -MMD -MP -mthumb -mthumb-interwork $(ASFLAGS) -fno-builtin -std=c11 -Wno-main -O2 -flto -ffast-math
|
||||
CFLAGS := -Wall -Wextra -MMD -MP -mthumb $(ASFLAGS) -fno-builtin -std=c11 -Wno-main -O2 -flto -ffast-math
|
||||
LDFLAGS := -nostdlib
|
||||
|
||||
objects = $(patsubst $(dir_source)/%.s, $(dir_build)/%.o, \
|
||||
@@ -25,14 +21,14 @@ objects = $(patsubst $(dir_source)/%.s, $(dir_build)/%.o, \
|
||||
$(call rwildcard, $(dir_source), *.s *.c)))
|
||||
|
||||
.PHONY: all
|
||||
all: ../../$(dir_build)/$(name).bin
|
||||
all: $(dir_out)/$(name).bin
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
@rm -rf $(dir_build)
|
||||
|
||||
../../$(dir_build)/$(name).bin: $(dir_build)/$(name).elf
|
||||
$(OC) -S -O binary $< $@
|
||||
$(dir_out)/$(name).bin: $(dir_build)/$(name).elf
|
||||
$(OBJCOPY) -S -O binary $< $@
|
||||
|
||||
$(dir_build)/$(name).elf: $(objects)
|
||||
$(CC) $(LDFLAGS) -T linker.ld $(OUTPUT_OPTION) $^
|
||||
@@ -44,4 +40,3 @@ $(dir_build)/%.o: $(dir_source)/%.c
|
||||
$(dir_build)/%.o: $(dir_source)/%.s
|
||||
@mkdir -p "$(@D)"
|
||||
$(COMPILE.s) $(OUTPUT_OPTION) $<
|
||||
include $(call rwildcard, $(dir_build), *.d)
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")
|
||||
OUTPUT_ARCH(arm)
|
||||
|
||||
ENTRY(_start)
|
||||
SECTIONS
|
||||
{
|
||||
. = 0;
|
||||
.text.start : { *(.text.start) }
|
||||
.text : { *(.text) }
|
||||
.data : { *(.data) }
|
||||
.bss : { *(.bss COMMON) }
|
||||
.rodata : { *(.rodata) }
|
||||
|
||||
.text : ALIGN(4) { *(.text.start) *(.text*); . = ALIGN(4); }
|
||||
.rodata : ALIGN(4) { *(.rodata*); . = ALIGN(4); }
|
||||
.data : ALIGN(4) { *(.data*); . = ALIGN(8); *(.bss* COMMON); . = ALIGN(8); }
|
||||
|
||||
. = ALIGN(4);
|
||||
}
|
||||
|
||||
@@ -83,7 +83,8 @@ _commonHandler:
|
||||
cps #0x13 @ switch to supervisor mode
|
||||
cmp r10, #0
|
||||
addne sp, #0x28
|
||||
ldr r2, [sp, #0x1c] @ implementation details of the official svc handler
|
||||
ldmfd sp, {r8-r11}^ @ implementation details of the official svc handler
|
||||
ldr r2, [sp, #0x1c]
|
||||
ldr r4, [sp, #0x18]
|
||||
msr cpsr_c, r3 @ restore processor mode
|
||||
tst r2, #0x20
|
||||
|
||||
@@ -22,8 +22,6 @@
|
||||
|
||||
#include "handlers.h"
|
||||
|
||||
#define FINAL_BUFFER 0xE5000000 //0x25000000
|
||||
|
||||
#define REG_DUMP_SIZE 4 * 23
|
||||
#define CODE_DUMP_SIZE 48
|
||||
|
||||
@@ -49,7 +47,8 @@ void __attribute__((noreturn)) mainHandler(u32 *regs, u32 type, u32 cpuId)
|
||||
|
||||
u32 registerDump[REG_DUMP_SIZE / 4];
|
||||
u8 codeDump[CODE_DUMP_SIZE];
|
||||
u8 *final = (u8 *)FINAL_BUFFER;
|
||||
u8 *finalBuffer = cannotAccessVA((void *)0xE5000000) ? (u8 *)0xF5000000 : (u8 *)0xE5000000; //VA for 0x25000000
|
||||
u8 *final = finalBuffer;
|
||||
|
||||
while(*(vu32 *)final == 0xDEADC0DE && *((vu32 *)final + 1) == 0xDEADCAFE);
|
||||
|
||||
@@ -68,7 +67,7 @@ void __attribute__((noreturn)) mainHandler(u32 *regs, u32 type, u32 cpuId)
|
||||
//Dump registers
|
||||
//Current order of saved regs: dfsr, ifsr, far, fpexc, fpinst, fpinst2, cpsr, pc, r8-r12, sp, lr, r0-r7
|
||||
u32 cpsr = regs[6];
|
||||
u32 pc = regs[7] - (type < 3 ? (((cpsr & 0x20) != 0 && type == 1) ? 2 : 4) : 8);
|
||||
u32 pc = regs[7] - (type < 3 ? (((cpsr & 0x20) != 0 && type == 1) ? 2 : 4) : 0);
|
||||
|
||||
registerDump[15] = pc;
|
||||
registerDump[16] = cpsr;
|
||||
@@ -80,8 +79,8 @@ void __attribute__((noreturn)) mainHandler(u32 *regs, u32 type, u32 cpuId)
|
||||
u8 *instr = (u8 *)pc + ((cpsr & 0x20) ? 2 : 4) - dumpHeader.codeDumpSize; //Doesn't work well on 32-bit Thumb instructions, but it isn't much of a problem
|
||||
dumpHeader.codeDumpSize = copyMemory(codeDump, instr, dumpHeader.codeDumpSize, ((cpsr & 0x20) != 0) ? 2 : 4);
|
||||
|
||||
//Copy register dump and code dump
|
||||
final = (u8 *)(FINAL_BUFFER + sizeof(ExceptionDumpHeader));
|
||||
//Copy register dump and code dump
|
||||
final = (u8 *)(finalBuffer + sizeof(ExceptionDumpHeader));
|
||||
final += copyMemory(final, registerDump, dumpHeader.registerDumpSize, 1);
|
||||
final += copyMemory(final, codeDump, dumpHeader.codeDumpSize, 1);
|
||||
|
||||
@@ -89,13 +88,13 @@ void __attribute__((noreturn)) mainHandler(u32 *regs, u32 type, u32 cpuId)
|
||||
dumpHeader.stackDumpSize = copyMemory(final, (const void *)registerDump[13], 0x1000 - (registerDump[13] & 0xFFF), 1);
|
||||
final += dumpHeader.stackDumpSize;
|
||||
|
||||
if(!cannotAccessVA((u8 *)0xFFFF9004))
|
||||
if(!cannotAccessVA((void *)0xFFFF9004))
|
||||
{
|
||||
vu64 *additionalData = (vu64 *)final;
|
||||
dumpHeader.additionalDataSize = 16;
|
||||
vu8 *currentKCodeSet = *(vu8 **)(*(vu8 **)0xFFFF9004 + CODESET_OFFSET); //currentKProcess + CodeSet
|
||||
|
||||
additionalData[0] = *(vu64 *)(currentKCodeSet + 0x50); //Process name
|
||||
additionalData[0] = *(vu64 *)(currentKCodeSet + 0x50); //Process name
|
||||
additionalData[1] = *(vu64 *)(currentKCodeSet + 0x5C); //Title ID
|
||||
}
|
||||
else dumpHeader.additionalDataSize = 0;
|
||||
@@ -103,8 +102,8 @@ void __attribute__((noreturn)) mainHandler(u32 *regs, u32 type, u32 cpuId)
|
||||
dumpHeader.totalSize = sizeof(ExceptionDumpHeader) + dumpHeader.registerDumpSize + dumpHeader.codeDumpSize + dumpHeader.stackDumpSize + dumpHeader.additionalDataSize;
|
||||
|
||||
//Copy header (actually optimized by the compiler)
|
||||
*(ExceptionDumpHeader *)FINAL_BUFFER = dumpHeader;
|
||||
*(ExceptionDumpHeader *)finalBuffer = dumpHeader;
|
||||
|
||||
cleanInvalidateDCacheAndDMB();
|
||||
mcuReboot(); //Also contains DCache-cleaning code
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,20 +4,16 @@ 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
|
||||
include $(DEVKITARM)/base_tools
|
||||
|
||||
name := arm9_exceptions
|
||||
|
||||
dir_source := source
|
||||
dir_build := build
|
||||
dir_out := ../../$(dir_build)
|
||||
|
||||
ASFLAGS := -mcpu=arm946e-s
|
||||
CFLAGS := -Wall -Wextra -MMD -MP -mthumb -mthumb-interwork $(ASFLAGS) -fno-builtin -std=c11 -Wno-main -O2 -flto -ffast-math
|
||||
CFLAGS := -Wall -Wextra -mthumb $(ASFLAGS) -fno-builtin -std=c11 -Wno-main -O2 -flto -ffast-math
|
||||
LDFLAGS := -nostdlib
|
||||
|
||||
objects = $(patsubst $(dir_source)/%.s, $(dir_build)/%.o, \
|
||||
@@ -25,14 +21,14 @@ objects = $(patsubst $(dir_source)/%.s, $(dir_build)/%.o, \
|
||||
$(call rwildcard, $(dir_source), *.s *.c)))
|
||||
|
||||
.PHONY: all
|
||||
all: ../../$(dir_build)/$(name).bin
|
||||
all: $(dir_out)/$(name).bin
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
@rm -rf $(dir_build)
|
||||
|
||||
../../$(dir_build)/$(name).bin: $(dir_build)/$(name).elf
|
||||
$(OC) -S -O binary $< $@
|
||||
$(dir_out)/$(name).bin: $(dir_build)/$(name).elf
|
||||
$(OBJCOPY) -S -O binary $< $@
|
||||
|
||||
$(dir_build)/$(name).elf: $(objects)
|
||||
$(CC) $(LDFLAGS) -T linker.ld $(OUTPUT_OPTION) $^
|
||||
@@ -44,4 +40,3 @@ $(dir_build)/%.o: $(dir_source)/%.c
|
||||
$(dir_build)/%.o: $(dir_source)/%.s
|
||||
@mkdir -p "$(@D)"
|
||||
$(COMPILE.s) $(OUTPUT_OPTION) $<
|
||||
include $(call rwildcard, $(dir_build), *.d)
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")
|
||||
OUTPUT_ARCH(arm)
|
||||
|
||||
ENTRY(_start)
|
||||
SECTIONS
|
||||
{
|
||||
. = 0x01FF7FE0;
|
||||
.text.start : { *(.text.start) }
|
||||
.text : { *(.text) }
|
||||
.data : { *(.data) }
|
||||
.bss : { *(.bss COMMON) }
|
||||
.rodata : { *(.rodata) }
|
||||
|
||||
.text : ALIGN(4) { *(.text.start) *(.text*); . = ALIGN(4); }
|
||||
.rodata : ALIGN(4) { *(.rodata*); . = ALIGN(4); }
|
||||
.data : ALIGN(4) { *(.data*); . = ALIGN(8); *(.bss* COMMON); . = ALIGN(8); }
|
||||
|
||||
. = ALIGN(4);
|
||||
}
|
||||
|
||||
@@ -62,6 +62,7 @@ _commonHandler:
|
||||
bic r5, r3, #0xf
|
||||
orr r5, #0x3
|
||||
msr cpsr_c, r5 @ switch to supervisor mode
|
||||
ldmfd sp, {r8-r11}^
|
||||
ldr r2, [sp, #0x1c] @ implementation details of the official svc handler
|
||||
ldr r4, [sp, #0x18]
|
||||
msr cpsr_c, r3 @ restore processor mode
|
||||
|
||||
@@ -22,9 +22,9 @@
|
||||
# Notices displayed by works containing it.
|
||||
|
||||
__author__ = "TuxSH"
|
||||
__copyright__ = "Copyright (c) 2016 TuxSH"
|
||||
__copyright__ = "Copyright (c) 2016 TuxSH"
|
||||
__license__ = "GPLv3"
|
||||
__version__ = "v1.0"
|
||||
__version__ = "v1.2"
|
||||
|
||||
"""
|
||||
Parses Luma3DS exception dumps
|
||||
@@ -33,6 +33,9 @@ Parses Luma3DS exception dumps
|
||||
import argparse
|
||||
from struct import unpack_from
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
# Source of hexdump: https://gist.github.com/ImmortalPC/c340564823f283fe530b
|
||||
# Credits for hexdump go to the original authors
|
||||
# Slightly edited by TuxSH
|
||||
@@ -76,14 +79,14 @@ def hexdump(addr, src, length=16, sep='.' ):
|
||||
text += chr(c)
|
||||
else:
|
||||
text += sep
|
||||
result.append(('%08X: %-'+str(length*(2+1)+1)+'s |%s|') % (addr + i, hexa, text))
|
||||
result.append(('%08x: %-'+str(length*(2+1)+1)+'s |%s|') % (addr + i, hexa, text))
|
||||
|
||||
return '\n'.join(result)
|
||||
|
||||
|
||||
|
||||
def makeRegisterLine(A, rA, B, rB):
|
||||
return "{0:<15}{1:<20}{2:<15}{3:<20}".format(A, "{0:08x}".format(rA), B, "{0:08x}".format(rB))
|
||||
|
||||
|
||||
handledExceptionNames = ("FIQ", "undefined instruction", "prefetch abort", "data abort")
|
||||
registerNames = tuple("r{0}".format(i) for i in range(13)) + ("sp", "lr", "pc", "cpsr") + ("dfsr", "ifsr", "far") + ("fpexc", "fpinst", "fpinst2")
|
||||
svcBreakReasons = ("(svcBreak: panic)", "(svcBreak: assertion failed)", "(svcBreak: user-related)")
|
||||
@@ -96,23 +99,24 @@ if __name__ == "__main__":
|
||||
with open(args.filename, "rb") as f: data = f.read()
|
||||
if unpack_from("<2I", data) != (0xdeadc0de, 0xdeadcafe):
|
||||
raise SystemExit("Invalid file format")
|
||||
|
||||
|
||||
version, processor, exceptionType, _, nbRegisters, codeDumpSize, stackDumpSize, additionalDataSize = unpack_from("<8I", data, 8)
|
||||
nbRegisters //= 4
|
||||
|
||||
|
||||
if version < (1 << 16) | 2:
|
||||
raise SystemExit("Incompatible format version, please use the appropriate parser.")
|
||||
|
||||
|
||||
registers = unpack_from("<{0}I".format(nbRegisters), data, 40)
|
||||
codeDump = data[40 + 4 * nbRegisters : 40 + 4 * nbRegisters + codeDumpSize]
|
||||
stackOffset = 40 + 4 * nbRegisters + codeDumpSize
|
||||
codeOffset = 40 + 4 * nbRegisters
|
||||
codeDump = data[codeOffset : codeOffset + codeDumpSize]
|
||||
stackOffset = codeOffset + codeDumpSize
|
||||
stackDump = data[stackOffset : stackOffset + stackDumpSize]
|
||||
addtionalDataOffset = stackOffset + stackDumpSize
|
||||
additionalData = data[addtionalDataOffset : addtionalDataOffset + additionalDataSize]
|
||||
|
||||
|
||||
if processor == 9: print("Processor: ARM9")
|
||||
else: print("Processor: ARM11 (core {0})".format(processor >> 16))
|
||||
|
||||
|
||||
typeDetailsStr = ""
|
||||
if exceptionType == 2:
|
||||
if (registers[16] & 0x20) == 0 and codeDumpSize >= 4:
|
||||
@@ -125,23 +129,41 @@ if __name__ == "__main__":
|
||||
instr = unpack_from("<I", codeDump[-4:])[0]
|
||||
if instr == 0xdf3c:
|
||||
typeDetailsStr = " " + (svcBreakReasons[registers[0]] if registers[0] < 3 else "(svcBreak)")
|
||||
|
||||
|
||||
elif processor != 9 and (registers[20] & 0x80000000) != 0:
|
||||
typeDetailsStr = " (VFP exception)"
|
||||
|
||||
|
||||
print("Exception type: {0}{1}".format("unknown" if exceptionType >= len(handledExceptionNames) else handledExceptionNames[exceptionType], typeDetailsStr))
|
||||
if additionalDataSize != 0:
|
||||
print("Current process: {0} ({1:016x})".format(additionalData[:8].decode("ascii"), unpack_from("<Q", additionalData, 8)[0]))
|
||||
|
||||
|
||||
print("\nRegister dump:\n")
|
||||
for i in range(0, nbRegisters - (nbRegisters % 2), 2):
|
||||
if i == 16: print("")
|
||||
print(makeRegisterLine(registerNames[i], registers[i], registerNames[i+1], registers[i+1]))
|
||||
if nbRegisters % 2 == 1: print("{0:<15}{1:<20}".format(registerNames[nbRegisters - 1], "{0:08x}".format(registers[nbRegisters - 1])))
|
||||
|
||||
|
||||
thumb = registers[16] & 0x20 != 0
|
||||
addr = registers[15] - codeDumpSize + (2 if thumb else 4)
|
||||
|
||||
print("\nCode dump:\n")
|
||||
print(hexdump(registers[15] - codeDumpSize + (4 if (registers[16] & 0x20 == 0) else 2), codeDump))
|
||||
|
||||
|
||||
objdump_res = ""
|
||||
try:
|
||||
path = os.path.join(os.environ["DEVKITARM"], "bin", "arm-none-eabi-objdump")
|
||||
if os.name == "nt":
|
||||
path = ''.join((path[1], ':', path[2:])).replace('/', '\\')
|
||||
|
||||
objdump_res = subprocess.check_output((
|
||||
path, "-marm", "-b", "binary",
|
||||
"--adjust-vma="+hex(addr - codeOffset), "--start-address="+hex(addr),
|
||||
"--stop-address="+hex(addr + codeDumpSize), "-D", "-z", "-M",
|
||||
"reg-names-std" + (",force-thumb" if thumb else ""), args.filename
|
||||
)).decode("utf-8")
|
||||
objdump_res = '\n'.join(objdump_res[objdump_res.find('<.data+'):].split('\n')[1:])
|
||||
except: objdump_res = ""
|
||||
|
||||
print(objdump_res if objdump_res != "" else hexdump(addr, codeDump))
|
||||
|
||||
print("\nStack dump:\n")
|
||||
print(hexdump(registers[13], stackDump))
|
||||
|
||||
@@ -6,15 +6,12 @@ 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_patches := patches
|
||||
dir_build := build
|
||||
dir_out := ../$(dir_build)
|
||||
|
||||
LIBS := -lctru
|
||||
LIBDIRS := $(CTRULIB)
|
||||
@@ -22,38 +19,56 @@ LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
|
||||
|
||||
INCLUDE := $(foreach dir,$(LIBDIRS),-I$(dir)/include)
|
||||
|
||||
ASFLAGS := -mcpu=mpcore -mfloat-abi=hard -mtp=soft
|
||||
CFLAGS := -Wall -Wextra -MMD -MP -marm $(ASFLAGS) -fno-builtin -std=c11 -O2 -flto -ffast-math -mword-relocations \
|
||||
-ffunction-sections -fdata-sections $(INCLUDE) -DARM11 -D_3DS
|
||||
LDFLAGS := -Xlinker --defsym="__start__=0x14000000" -specs=3dsx.specs $(ASFLAGS)
|
||||
ASFLAGS := -mcpu=mpcore -mfloat-abi=hard
|
||||
CFLAGS := -Wall -Wextra $(ASFLAGS) -fno-builtin -std=c11 -O2 -flto -ffast-math $(INCLUDE) -DARM11 -D_3DS
|
||||
LDFLAGS := -specs=3dsx.specs $(ASFLAGS) -Wl,--section-start,.text=0x14000000
|
||||
|
||||
objects = $(patsubst $(dir_source)/%.c, $(dir_build)/%.o, \
|
||||
$(call rwildcard, $(dir_source), *.s *.c))
|
||||
|
||||
ifeq ($(strip $(DEV)),TRUE)
|
||||
CFLAGS += -DDEV
|
||||
endif
|
||||
bundled = $(dir_build)/romfsredir.bin.o
|
||||
|
||||
define bin2o
|
||||
bin2s $< | $(AS) -o $(@)
|
||||
endef
|
||||
|
||||
.PHONY: all
|
||||
all: ../$(dir_build)/$(name).cxi
|
||||
all: $(dir_out)/$(name).bin
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
@rm -rf $(dir_build)
|
||||
|
||||
../$(dir_build)/$(name).cxi: $(dir_build)/$(name).elf
|
||||
.PRECIOUS: $(dir_build)/%.bin
|
||||
|
||||
$(dir_build):
|
||||
@mkdir -p "$@"
|
||||
|
||||
$(dir_out)/$(name).bin: $(dir_build)/$(name).elf
|
||||
@makerom -f ncch -rsf loader.rsf -nocodepadding -o $@ -elf $<
|
||||
|
||||
$(dir_build)/$(name).elf: $(objects)
|
||||
$(dir_build)/$(name).elf: $(bundled) $(objects)
|
||||
$(LINK.o) $(OUTPUT_OPTION) $^ $(LIBPATHS) $(LIBS)
|
||||
|
||||
$(dir_build)/%.bin.o: $(dir_build)/%.bin
|
||||
@$(bin2o)
|
||||
|
||||
$(dir_build)/%.bin: $(dir_patches)/%.s
|
||||
@mkdir -p "$(@D)"
|
||||
@armips $<
|
||||
|
||||
$(dir_build)/memory.o $(dir_build)/strings.o: CFLAGS += -O3
|
||||
|
||||
$(dir_build)/%.o: $(dir_source)/%.c
|
||||
$(dir_build)/bundled.h: $(bundled)
|
||||
@$(foreach f, $(bundled),\
|
||||
echo "extern const u8" `(echo $(basename $(notdir $(f))) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"[];" >> $@;\
|
||||
echo "extern const u32" `(echo $(basename $(notdir $(f)))| sed -e 's/^\([0-9]\)/_\1/' | tr . _)`_size";" >> $@;\
|
||||
)
|
||||
|
||||
$(dir_build)/%.o: $(dir_source)/%.c $(dir_build)/bundled.h
|
||||
@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)
|
||||
|
||||
114
injector/patches/romfsredir.s
Normal file
114
injector/patches/romfsredir.s
Normal file
@@ -0,0 +1,114 @@
|
||||
.arm.little
|
||||
.create "build/romfsredir.bin", 0
|
||||
|
||||
.macro addr, reg, func
|
||||
add reg, pc, #func-.-8
|
||||
.endmacro
|
||||
.macro load, reg, func
|
||||
ldr reg, [pc, #func-.-8]
|
||||
.endmacro
|
||||
|
||||
; Patch by delebile
|
||||
|
||||
.arm
|
||||
_start:
|
||||
|
||||
; Jumps here before the fsOpenFileDirectly call
|
||||
_mountArchive:
|
||||
b mountArchive
|
||||
.word 0xdead0000 ; Substituted opcode
|
||||
.word 0xdead0001 ; Branch to hooked function
|
||||
|
||||
; Jumps here before every iFileOpen call
|
||||
_fsRedir:
|
||||
b fsRedir
|
||||
.word 0xdead0002 ; Substituted opcode
|
||||
.word 0xdead0003 ; Branch to hooked function
|
||||
|
||||
; Mounts the archive and registers it as 'lf:'
|
||||
mountArchive:
|
||||
cmp r3, #3
|
||||
bne _mountArchive + 4
|
||||
stmfd sp!, {r0-r4, lr}
|
||||
sub sp, sp, #4
|
||||
load r1, archiveId
|
||||
mov r0, sp
|
||||
load r4, fsMountArchive
|
||||
blx r4
|
||||
mov r3, #0
|
||||
mov r2, #0
|
||||
ldr r1, [sp]
|
||||
addr r0, archiveName
|
||||
load r4, fsRegisterArchive
|
||||
blx r4
|
||||
add sp, sp, #4
|
||||
ldmfd sp!, {r0-r4, lr}
|
||||
b _mountArchive + 4
|
||||
|
||||
; Check the path passed to iFileOpen.
|
||||
; If it is trying to access a RomFS file, we try to
|
||||
; open it from the LayeredFS folder.
|
||||
; If the file cannot be opened, we just open
|
||||
; it from its original archive like nothing happened
|
||||
fsRedir:
|
||||
stmfd sp!, {r0-r12, lr}
|
||||
addr r3, romFsMount
|
||||
bl compare
|
||||
addne r3, pc, #updateRomFsMount-.-8
|
||||
blne compare
|
||||
bne endRedir
|
||||
sub sp, sp, #0x400
|
||||
pathRedir:
|
||||
stmfd sp!, {r0-r3}
|
||||
add r0, sp, #0x10
|
||||
load r3, customPath
|
||||
pathRedir_1:
|
||||
ldrb r2, [r3], #1
|
||||
strh r2, [r0], #2
|
||||
cmp r2, #0
|
||||
bne pathRedir_1
|
||||
sub r0, r0, #2
|
||||
pathRedir_2:
|
||||
ldrh r2, [r1], #2
|
||||
cmp r2, #0x3A ; ':'
|
||||
bne pathRedir_2
|
||||
pathRedir_3:
|
||||
ldrh r2, [r1], #2
|
||||
strh r2, [r0], #2
|
||||
cmp r2, #0
|
||||
bne pathRedir_3
|
||||
ldmfd sp!, {r0-r3}
|
||||
mov r1, sp
|
||||
bl _fsRedir + 4
|
||||
add sp, sp, #0x400
|
||||
cmp r0, #0
|
||||
|
||||
endRedir:
|
||||
ldmfd sp!, {r0-r12, lr}
|
||||
moveq r0, #0
|
||||
bxeq lr
|
||||
b _fsRedir + 4
|
||||
|
||||
compare:
|
||||
mov r9, r1
|
||||
add r10, r3, #4
|
||||
loop:
|
||||
ldrb r12, [r3], #1
|
||||
ldrb r11, [r9], #2
|
||||
cmp r11, r12
|
||||
bxne lr
|
||||
cmp r10, r3
|
||||
bne loop
|
||||
bx lr
|
||||
|
||||
.pool
|
||||
.align 4
|
||||
archiveName : .dcb "lf:", 0
|
||||
fsMountArchive : .word 0xdead0005
|
||||
fsRegisterArchive : .word 0xdead0006
|
||||
archiveId : .word 0xdead0007
|
||||
romFsMount : .dcb "rom:"
|
||||
updateRomFsMount : .word 0xdead0008
|
||||
customPath : .word 0xdead0004
|
||||
|
||||
.close
|
||||
@@ -9,7 +9,7 @@ typedef struct __attribute__((packed))
|
||||
u8 versionMajor;
|
||||
u8 versionMinor;
|
||||
u8 versionBuild;
|
||||
u8 flags; /* bit 0: dev branch; bit 1: is release */
|
||||
u8 flags;
|
||||
|
||||
u32 commitHash;
|
||||
|
||||
|
||||
@@ -107,3 +107,60 @@ Result FSLDR_OpenFileDirectly(Handle* out, FS_ArchiveID archiveId, FS_Path archi
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result FSLDR_OpenArchive(FS_Archive* archive, FS_ArchiveID id, FS_Path path)
|
||||
{
|
||||
if(!archive) return -2;
|
||||
|
||||
u32 *cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x80C,3,2); // 0x80C00C2
|
||||
cmdbuf[1] = id;
|
||||
cmdbuf[2] = path.type;
|
||||
cmdbuf[3] = path.size;
|
||||
cmdbuf[4] = IPC_Desc_StaticBuffer(path.size, 0);
|
||||
cmdbuf[5] = (u32) path.data;
|
||||
|
||||
Result ret = 0;
|
||||
if(R_FAILED(ret = svcSendSyncRequest(fsldrHandle))) return ret;
|
||||
|
||||
if(archive) *archive = cmdbuf[2] | ((u64) cmdbuf[3] << 32);
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result FSLDR_CloseArchive(FS_Archive archive)
|
||||
{
|
||||
if(!archive) return -2;
|
||||
|
||||
u32 *cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x80E,2,0); // 0x80E0080
|
||||
cmdbuf[1] = (u32) archive;
|
||||
cmdbuf[2] = (u32) (archive >> 32);
|
||||
|
||||
Result ret = 0;
|
||||
if(R_FAILED(ret = svcSendSyncRequest(fsldrHandle))) return ret;
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result FSLDR_OpenDirectory(Handle* out, FS_Archive archive, FS_Path path)
|
||||
{
|
||||
u32 *cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x80B,4,2); // 0x80B0102
|
||||
cmdbuf[1] = (u32) archive;
|
||||
cmdbuf[2] = (u32) (archive >> 32);
|
||||
cmdbuf[3] = path.type;
|
||||
cmdbuf[4] = path.size;
|
||||
cmdbuf[5] = IPC_Desc_StaticBuffer(path.size, 0);
|
||||
cmdbuf[6] = (u32) path.data;
|
||||
|
||||
Result ret = 0;
|
||||
if(R_FAILED(ret = svcSendSyncRequest(fsldrHandle))) return ret;
|
||||
|
||||
if(out) *out = cmdbuf[3];
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
@@ -7,3 +7,6 @@ void fsldrExit(void);
|
||||
Result FSLDR_InitializeWithSdkVersion(Handle session, u32 version);
|
||||
Result FSLDR_SetPriority(u32 priority);
|
||||
Result FSLDR_OpenFileDirectly(Handle* out, FS_ArchiveID archiveId, FS_Path archivePath, FS_Path filePath, u32 openFlags, u32 attributes);
|
||||
Result FSLDR_OpenArchive(FS_Archive* archive, FS_ArchiveID id, FS_Path path);
|
||||
Result FSLDR_CloseArchive(FS_Archive archive);
|
||||
Result FSLDR_OpenDirectory(Handle* out, FS_Archive archive, FS_Path path);
|
||||
@@ -63,4 +63,4 @@ Result IFile_Read(IFile *file, u64 *total, void *buffer, u32 len)
|
||||
|
||||
*total = cur;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,8 +155,10 @@ static Result load_code(u64 progid, prog_addrs_t *shared, u64 prog_handle, int i
|
||||
lzss_decompress((u8 *)shared->text_addr + size);
|
||||
}
|
||||
|
||||
u16 progver = g_exheader.codesetinfo.flags.remasterversion[0] | (g_exheader.codesetinfo.flags.remasterversion[1] << 8);
|
||||
|
||||
// patch
|
||||
patchCode(progid, (u8 *)shared->text_addr, shared->total_size << 12);
|
||||
patchCode(progid, progver, (u8 *)shared->text_addr, shared->total_size << 12, g_exheader.codesetinfo.text.codesize, g_exheader.codesetinfo.ro.codesize, g_exheader.codesetinfo.data.codesize, g_exheader.codesetinfo.ro.address, g_exheader.codesetinfo.data.address);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -11,13 +11,13 @@ void memcpy(void *dest, const void *src, u32 size)
|
||||
|
||||
int memcmp(const void *buf1, const void *buf2, u32 size)
|
||||
{
|
||||
const u8 *buf1c = (const u8 *)buf1;
|
||||
const u8 *buf2c = (const u8 *)buf2;
|
||||
const u8 *buf1c = (const u8 *)buf1,
|
||||
*buf2c = (const u8 *)buf2;
|
||||
|
||||
for(u32 i = 0; i < size; i++)
|
||||
{
|
||||
int cmp = buf1c[i] - buf2c[i];
|
||||
if(cmp) return cmp;
|
||||
if(cmp != 0) return cmp;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,38 +2,44 @@
|
||||
|
||||
#include <3ds/types.h>
|
||||
|
||||
#define CONFIG(a) (((info.config >> (a + 21)) & 1) != 0)
|
||||
#define MULTICONFIG(a) ((info.config >> (a * 2 + 9)) & 3)
|
||||
#define MAKE_BRANCH(src,dst) (0xEA000000 | ((u32)((((u8 *)(dst) - (u8 *)(src)) >> 2) - 2) & 0xFFFFFF))
|
||||
#define MAKE_BRANCH_LINK(src,dst) (0xEB000000 | ((u32)((((u8 *)(dst) - (u8 *)(src)) >> 2) - 2) & 0xFFFFFF))
|
||||
|
||||
#define CONFIG(a) (((info.config >> (a + 17)) & 1) != 0)
|
||||
#define MULTICONFIG(a) ((info.config >> (a * 2 + 7)) & 3)
|
||||
#define BOOTCONFIG(a, b) ((info.config >> a) & b)
|
||||
#define LOADERFLAG(a) ((info.flags >> (a + 4)) & 1) != 0
|
||||
|
||||
#define BOOTCFG_NAND BOOTCONFIG(0, 7)
|
||||
#define BOOTCFG_FIRM BOOTCONFIG(3, 7)
|
||||
#define BOOTCFG_A9LH BOOTCONFIG(6, 1)
|
||||
#define BOOTCFG_NOFORCEFLAG BOOTCONFIG(7, 1)
|
||||
#define BOOTCFG_SAFEMODE BOOTCONFIG(8, 1)
|
||||
#define BOOTCFG_NOFORCEFLAG BOOTCONFIG(6, 1)
|
||||
|
||||
enum multiOptions
|
||||
{
|
||||
DEFAULTEMU = 0,
|
||||
BRIGHTNESS,
|
||||
SPLASH,
|
||||
PIN,
|
||||
NEWCPU
|
||||
#ifdef DEV
|
||||
, DEVOPTIONS
|
||||
#endif
|
||||
};
|
||||
|
||||
enum singleOptions
|
||||
{
|
||||
AUTOBOOTSYS = 0,
|
||||
USESYSFIRM,
|
||||
USELANGEMUANDCODE,
|
||||
AUTOBOOTEMU = 0,
|
||||
USEEMUFIRM,
|
||||
LOADEXTFIRMSANDMODULES,
|
||||
PATCHGAMES,
|
||||
PATCHVERSTRING,
|
||||
SHOWGBABOOT,
|
||||
PAYLOADSPLASH
|
||||
#ifdef DEV
|
||||
, PATCHACCESS
|
||||
#endif
|
||||
PATCHACCESS,
|
||||
PATCHUNITINFO,
|
||||
ENABLEEXCEPTIONHANDLERS
|
||||
};
|
||||
|
||||
void patchCode(u64 progId, u8 *code, u32 size);
|
||||
enum flags
|
||||
{
|
||||
ISN3DS = 0,
|
||||
ISSAFEMODE
|
||||
};
|
||||
|
||||
void patchCode(u64 progId, u16 progVer, u8 *code, u32 size, u32 textSize, u32 roSize, u32 dataSize, u32 roAddress, u32 dataAddress);
|
||||
|
||||
@@ -11,7 +11,7 @@ size_t strnlen(const char *string, size_t maxlen)
|
||||
|
||||
void progIdToStr(char *strEnd, u64 progId)
|
||||
{
|
||||
while(progId)
|
||||
while(progId > 0)
|
||||
{
|
||||
static const char hexDigits[] = "0123456789ABCDEF";
|
||||
*strEnd-- = hexDigits[(u32)(progId & 0xF)];
|
||||
|
||||
14
linker.ld
14
linker.ld
@@ -1,11 +1,15 @@
|
||||
OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")
|
||||
OUTPUT_ARCH(arm)
|
||||
|
||||
ENTRY(_start)
|
||||
SECTIONS
|
||||
{
|
||||
. = 0x23F00000;
|
||||
.text.start : { *(.text.start) }
|
||||
.text : { *(.text) }
|
||||
.data : { *(.data) }
|
||||
.bss : { *(.bss COMMON) }
|
||||
.rodata : { *(.rodata) }
|
||||
|
||||
.text : ALIGN(4) { *(.text.start) *(.text*); . = ALIGN(4); }
|
||||
.rodata : ALIGN(4) { *(.rodata*); . = ALIGN(4); }
|
||||
.data : ALIGN(4) { *(.data*); . = ALIGN(4); }
|
||||
.bss : ALIGN(8) { __bss_start = .; *(.bss* COMMON); . = ALIGN(8); __bss_end = .; }
|
||||
|
||||
. = ALIGN(4);
|
||||
}
|
||||
|
||||
@@ -4,20 +4,16 @@ 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
|
||||
include $(DEVKITARM)/base_tools
|
||||
|
||||
name := $(shell basename $(CURDIR))
|
||||
|
||||
dir_source := source
|
||||
dir_build := build
|
||||
dir_out := ../$(dir_build)
|
||||
|
||||
ASFLAGS := -mcpu=arm946e-s
|
||||
CFLAGS := -Wall -Wextra -MMD -MP -mthumb -mthumb-interwork $(ASFLAGS) -fno-builtin -std=c11 -Wno-main -O2 -flto -ffast-math
|
||||
CFLAGS := -Wall -Wextra -mthumb $(ASFLAGS) -fno-builtin -std=c11 -Wno-main -O2 -flto -ffast-math
|
||||
LDFLAGS := -nostdlib
|
||||
|
||||
objects = $(patsubst $(dir_source)/%.s, $(dir_build)/%.o, \
|
||||
@@ -25,14 +21,14 @@ objects = $(patsubst $(dir_source)/%.s, $(dir_build)/%.o, \
|
||||
$(call rwildcard, $(dir_source), *.s *.c)))
|
||||
|
||||
.PHONY: all
|
||||
all: ../$(dir_build)/$(name).bin
|
||||
all: $(dir_out)/$(name).bin
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
@rm -rf $(dir_build)
|
||||
|
||||
../$(dir_build)/$(name).bin: $(dir_build)/$(name).elf
|
||||
$(OC) -S -O binary $< $@
|
||||
$(dir_out)/$(name).bin: $(dir_build)/$(name).elf
|
||||
$(OBJCOPY) -S -O binary $< $@
|
||||
|
||||
$(dir_build)/$(name).elf: $(objects)
|
||||
$(LINK.o) -T linker.ld $(OUTPUT_OPTION) $^
|
||||
@@ -46,4 +42,3 @@ $(dir_build)/%.o: $(dir_source)/%.c
|
||||
$(dir_build)/%.o: $(dir_source)/%.s
|
||||
@mkdir -p "$(@D)"
|
||||
$(COMPILE.s) $(OUTPUT_OPTION) $<
|
||||
include $(call rwildcard, $(dir_build), *.d)
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")
|
||||
OUTPUT_ARCH(arm)
|
||||
|
||||
ENTRY(_start)
|
||||
SECTIONS
|
||||
{
|
||||
. = 0x24FFFF00;
|
||||
.text.start : { *(.text.start) }
|
||||
.text : { *(.text) }
|
||||
.data : { *(.data) }
|
||||
.bss : { *(.bss COMMON) }
|
||||
.rodata : { *(.rodata) }
|
||||
. = 0x27FFE000;
|
||||
|
||||
.text : ALIGN(4) { *(.text.start) *(.text*); . = ALIGN(4); }
|
||||
.rodata : ALIGN(4) { *(.rodata*); . = ALIGN(4); }
|
||||
.data : ALIGN(4) { *(.data*); . = ALIGN(8); *(.bss* COMMON); . = ALIGN(8); }
|
||||
|
||||
. = ALIGN(4);
|
||||
}
|
||||
|
||||
@@ -18,16 +18,20 @@
|
||||
@ reasonable legal notices or author attributions in that material or in the Appropriate Legal
|
||||
@ Notices displayed by works containing it.
|
||||
|
||||
.text
|
||||
.arm
|
||||
.global flushCaches
|
||||
.type flushCaches STT_FUNC
|
||||
.align 4
|
||||
|
||||
.global flushCaches
|
||||
.type flushCaches, %function
|
||||
flushCaches:
|
||||
@ Clean and flush data cache
|
||||
@ Clean and flush both the data cache and instruction caches
|
||||
|
||||
@ Adpated from http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0155a/ch03s03s05.html ,
|
||||
@ and https://github.com/gemarcano/libctr9_io/blob/master/src/ctr_system_ARM.c#L39 as well
|
||||
@ Note: ARM's example is actually for a 8KB DCache (which is what the 3DS has)
|
||||
@ Implemented in bootROM at address 0xffff0830
|
||||
|
||||
@ Implemented in bootROM at addresses 0xffff0830 (DCache) and 0xffff0ab4 (ICache)
|
||||
|
||||
mov r1, #0 @ segment counter
|
||||
outer_loop:
|
||||
|
||||
44
loader/source/firm.c
Normal file
44
loader/source/firm.c
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* This file is part of Luma3DS
|
||||
* Copyright (C) 2017 Aurora Wright, TuxSH
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
|
||||
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
|
||||
* Notices displayed by works containing it.
|
||||
*/
|
||||
|
||||
#include "firm.h"
|
||||
#include "memory.h"
|
||||
#include "cache.h"
|
||||
|
||||
void launchFirm(Firm *firm, int argc, char **argv)
|
||||
{
|
||||
|
||||
//Copy FIRM sections to respective memory locations
|
||||
for(u32 sectionNum = 0; sectionNum < 4 && firm->section[sectionNum].size != 0; sectionNum++)
|
||||
memcpy(firm->section[sectionNum].address, (u8 *)firm + firm->section[sectionNum].offset, firm->section[sectionNum].size);
|
||||
|
||||
//Set ARM11 entrypoint
|
||||
*(vu32 *)0x1FFFFFFC = (u32)firm->arm11Entry;
|
||||
|
||||
//Ensure that all memory transfers have completed and that the caches have been flushed
|
||||
flushCaches();
|
||||
|
||||
//Jump to ARM9 entrypoint. Also give it additional arguments it can dismiss
|
||||
((void (*)(int, char**, u32))firm->arm9Entry)(argc, argv, 0x0000BEEF);
|
||||
|
||||
__builtin_unreachable();
|
||||
}
|
||||
46
loader/source/firm.h
Normal file
46
loader/source/firm.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* This file is part of Luma3DS
|
||||
* Copyright (C) 2017 Aurora Wright, TuxSH
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
|
||||
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
|
||||
* Notices displayed by works containing it.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
u32 offset;
|
||||
u8 *address;
|
||||
u32 size;
|
||||
u32 procType;
|
||||
u8 hash[0x20];
|
||||
} FirmSection;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
char magic[4];
|
||||
u32 reserved1;
|
||||
u8 *arm11Entry;
|
||||
u8 *arm9Entry;
|
||||
u8 reserved2[0x30];
|
||||
FirmSection section[4];
|
||||
} Firm;
|
||||
|
||||
void launchFirm(Firm *firm, int argc, char **argv);
|
||||
@@ -22,17 +22,20 @@
|
||||
|
||||
#include "memory.h"
|
||||
#include "cache.h"
|
||||
#include "firm.h"
|
||||
|
||||
extern u32 payloadSize; //Defined in start.s
|
||||
|
||||
void main(void)
|
||||
void main(int argc __attribute__((unused)), char **argv)
|
||||
{
|
||||
void *payloadAddress = (void *)0x23F00000;
|
||||
Firm *firm = (Firm *)0x24000000;
|
||||
char absPath[24 + 255];
|
||||
|
||||
memcpy(payloadAddress, (void *)0x24F00000, payloadSize);
|
||||
u32 i;
|
||||
for(i = 0; i < 23 + 255 && argv[0][i] != 0; i++)
|
||||
absPath[i] = argv[0][i];
|
||||
for(; i < 24 + 255; i++)
|
||||
absPath[i] = 0;
|
||||
|
||||
//Ensure that all memory transfers have completed and that the caches have been flushed
|
||||
flushCaches();
|
||||
char *argvPassed[1] = {absPath};
|
||||
|
||||
((void (*)())payloadAddress)();
|
||||
}
|
||||
launchFirm(firm, 1, argvPassed);
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
* Notices displayed by works containing it.
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* memcpy adapted from https://github.com/mid-kid/CakesForeveryWan/blob/557a8e8605ab3ee173af6497486e8f22c261d0e2/source/memfuncs.c
|
||||
*/
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
* Notices displayed by works containing it.
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* memcpy adapted from https://github.com/mid-kid/CakesForeveryWan/blob/557a8e8605ab3ee173af6497486e8f22c261d0e2/source/memfuncs.c
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
@ This file is part of Luma3DS
|
||||
@ Copyright (C) 2016 Aurora Wright, TuxSH
|
||||
@ Copyright (C) 2017 Aurora Wright, TuxSH
|
||||
@
|
||||
@ This program is free software: you can redistribute it and/or modify
|
||||
@ it under the terms of the GNU General Public License as published by
|
||||
@@ -22,6 +22,7 @@
|
||||
.align 4
|
||||
.global _start
|
||||
_start:
|
||||
ldr sp, =0x27ffe000
|
||||
b main
|
||||
|
||||
.global payloadSize
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
diff -uNr a/source/main.c b/source/main.c
|
||||
--- a/source/main.c 2016-09-11 01:04:25.665231884 +0200
|
||||
+++ b/source/main.c 2016-09-14 12:36:28.601439550 +0200
|
||||
@@ -9,6 +9,7 @@
|
||||
#endif
|
||||
|
||||
int main (void) {
|
||||
+ svcSleepThread(2500 * 1000000ULL);
|
||||
if (brahma_init()) {
|
||||
if (load_arm9_payload_offset("/" LAUNCHER_PATH, 0x12000, 0x10000) != 1)
|
||||
goto error;
|
||||
@@ -1,46 +1,48 @@
|
||||
; Code by Normmatt
|
||||
|
||||
.arm.little
|
||||
|
||||
.create "build/emunand.bin", 0
|
||||
.arm
|
||||
nand_sd:
|
||||
; Original code that still needs to be executed.
|
||||
; Original code that still needs to be executed
|
||||
mov r4, r0
|
||||
mov r5, r1
|
||||
mov r7, r2
|
||||
mov r6, r3
|
||||
; End.
|
||||
; End
|
||||
|
||||
; If we're already trying to access the SD, return.
|
||||
; If we're already trying to access the SD, return
|
||||
ldr r2, [r0, #4]
|
||||
ldr r1, [sdmmc]
|
||||
cmp r2, r1
|
||||
beq nand_sd_ret
|
||||
beq out
|
||||
|
||||
str r1, [r0, #4] ; Set object to be SD
|
||||
ldr r2, [r0, #8] ; Get sector to read
|
||||
cmp r2, #0 ; For GW compatibility, see if we're trying to read the ncsd header (sector 0)
|
||||
str r1, [r0, #4] ; Set object to be SD
|
||||
ldr r2, [r0, #8] ; Get sector to read
|
||||
cmp r2, #0 ; For GW compatibility, see if we're trying to read the ncsd header (sector 0)
|
||||
|
||||
ldr r3, [nand_offset]
|
||||
add r2, r3 ; Add the offset to the NAND in the SD.
|
||||
add r2, r3 ; Add the offset to the NAND in the SD
|
||||
|
||||
ldreq r3, [ncsd_header_offset]
|
||||
addeq r2, r3 ; If we're reading the ncsd header, add the offset of that sector.
|
||||
addeq r2, r3 ; If we're reading the ncsd header, add the offset of that sector
|
||||
|
||||
str r2, [r0, #8] ; Store sector to read
|
||||
str r2, [r0, #8] ; Store sector to read
|
||||
|
||||
nand_sd_ret:
|
||||
out:
|
||||
; Restore registers.
|
||||
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.
|
||||
; due to the offset of this function being stored there
|
||||
mov r0, lr
|
||||
add r0, #4
|
||||
bx r0
|
||||
|
||||
.pool
|
||||
sdmmc: .ascii "SDMC"
|
||||
nand_offset: .ascii "NAND" ; for rednand this should be 1
|
||||
ncsd_header_offset: .ascii "NCSD" ; depends on nand manufacturer + emunand type (GW/RED)
|
||||
.close
|
||||
nand_offset: .ascii "NAND" ; For rednand this should be 1
|
||||
ncsd_header_offset: .ascii "NCSD" ; Depends on nand manufacturer + emunand type (GW/RED)
|
||||
.close
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
; Notices displayed by works containing it.
|
||||
;
|
||||
|
||||
; This is mainly Subv's code, big thanks to him.
|
||||
; Code originally from Subv
|
||||
|
||||
.arm.little
|
||||
|
||||
@@ -35,85 +35,71 @@
|
||||
; Register contents:
|
||||
; r4: Pointer to a pointer to the exheader of the current NCCH
|
||||
; r6: Constant 0
|
||||
; SP + 0x80 - 0x7C: Pointer to the memory location where the NCCH text was loaded
|
||||
; SP + 4: Pointer to the memory location where the NCCH text was loaded
|
||||
|
||||
; Save the value of sp
|
||||
mov r0, sp
|
||||
; Save the value of all registers
|
||||
push {r0-r12}
|
||||
; Execute the instruction we overwrote in our detour
|
||||
ldr r0, [r4]
|
||||
|
||||
ldr r0, [r0, #(0x80 - 0x7C)] ; Load the .text address
|
||||
ldr r7, [r4]
|
||||
ldr r2, [r7, #0x18] ; Load the size of the .text
|
||||
ldr r8, [r7, #0x200] ; Load the low title id of the current NCCH
|
||||
mov r5, r0
|
||||
add r11, r5, r2 ; Max bounds of the memory region
|
||||
; Save the value of the register we use
|
||||
push {r0-r4}
|
||||
|
||||
ldr r9, =0x00001002 ; Low title id of the sm module
|
||||
cmp r8, r9 ; Compare the low title id to the id of the sm module
|
||||
bne fs_patch ; Skip if they're not the same
|
||||
ldr r1, [sp, #24] ; Load the .text address
|
||||
ldr r2, [r0, #0x200] ; Load the low title id of the current NCCH
|
||||
ldr r0, [r0, #0x18] ; Load the size of the .text
|
||||
add r0, r1, r0 ; Max bounds of the memory region
|
||||
|
||||
ldr r3, =0x1002 ; Low title id of the sm module
|
||||
cmp r2, r3 ; Compare the low title id to the id of the sm module
|
||||
bne fs_patch ; Skip if they're not the same
|
||||
|
||||
ldr r2, =0xE1A01006 ; mov r1, r6
|
||||
|
||||
ldr r7, =0xE1A01006 ; mov r1, r6
|
||||
ldr r8, =0xE1A00005 ; mov r0, r5
|
||||
ldr r9, =0xE3500000 ; cmp r0, #0
|
||||
ldr r10, =0xE2850004 ; add r0, r5, #4
|
||||
|
||||
loop:
|
||||
cmp r11, r5
|
||||
blo out ; Check if we didn't go past the bounds of the memory region
|
||||
ldr r6, [r5]
|
||||
cmp r6, r7
|
||||
ldreq r6, [r5, #4]
|
||||
cmpeq r6, r8
|
||||
ldreq r6, [r5, #12]
|
||||
cmpeq r6, r9
|
||||
ldreq r6, [r5, #24]
|
||||
cmpeq r6, r10
|
||||
moveq r8, r5
|
||||
addne r5, r5, #4
|
||||
cmp r0, r1
|
||||
blo die ; Check if we didn't go past the bounds of the memory region
|
||||
ldr r3, [r1]
|
||||
cmp r3, r2
|
||||
ldreqh r3, [r1, #4]
|
||||
cmpeq r3, #5
|
||||
addne r1, #4
|
||||
bne loop
|
||||
|
||||
; r8 now contains the start address of the pattern we found
|
||||
|
||||
; Write NOPs to the four instructions we want to patch
|
||||
ldr r9, =0xE320F000 ; nop
|
||||
str r9, [r8, #8] ; Patch the bl
|
||||
str r9, [r8, #12] ; Patch the cmp
|
||||
str r9, [r8, #16] ; Patch the ldreq
|
||||
str r9, [r8, #20] ; Patch the beq
|
||||
; r1 now contains the start address of the pattern we found
|
||||
ldr r0, =0xE3A00001 ; mov r0, #1
|
||||
str r0, [r1, #8] ; Patch the bl
|
||||
b out
|
||||
|
||||
fs_patch: ; patch adapted from BootNTR
|
||||
ldr r9, =0x00001102 ; Low title id of the fs module
|
||||
cmp r8, r9 ; Compare the low title id to the id of the sm module
|
||||
bne out ; Skip if they're not the same
|
||||
ldr r3, =0x1102 ; Low title id of the fs module
|
||||
cmp r2, r3 ; Compare the low title id to the id of the sm module
|
||||
bne out ; Skip if they're not the same
|
||||
|
||||
ldr r7, =0x4618 ; mov r0, r3
|
||||
ldr r8, =0x3481 ; add r4, #0x81
|
||||
ldr r2, =0x7401 ; strb r1, [r0, #16]
|
||||
ldr r3, =0x2000 ; movs r0, #0
|
||||
|
||||
loop_fs:
|
||||
cmp r11, r5
|
||||
blo out
|
||||
ldrh r6, [r5]
|
||||
cmp r6, r7
|
||||
ldreqh r6, [r5, #2]
|
||||
cmpeq r6, r8
|
||||
subeq r8, r5, #8
|
||||
addne r5, #2
|
||||
bne loop_fs
|
||||
loop_fs:
|
||||
cmp r0, r1
|
||||
blo die
|
||||
ldrh r4, [r1]
|
||||
cmp r4, r2
|
||||
ldreqh r4, [r1, #2]
|
||||
cmpeq r4, r3
|
||||
addeq r1, #8
|
||||
addne r1, #2
|
||||
bne loop_fs
|
||||
|
||||
; r1 now contains the start address of the pattern we found
|
||||
ldr r0, =0x2001 ; mov r0, #1
|
||||
ldr r2, =0x4770 ; bx lr
|
||||
strh r0, [r1]
|
||||
strh r2, [r1, #2]
|
||||
|
||||
; r8 now contains the start address of the pattern we found
|
||||
ldr r9, =0x2001 ; mov r0, #1
|
||||
ldr r10, =0x4770 ; bx lr
|
||||
strh r9, [r8]
|
||||
strh r10, [r8, #2]
|
||||
|
||||
out:
|
||||
pop {r0-r12} ; Restore the registers we used
|
||||
pop {r0-r4} ; Restore the registers we used
|
||||
bx lr ; Jump back to whoever called us
|
||||
|
||||
ldr r0, [r4] ; Execute the instruction we overwrote in our detour
|
||||
|
||||
bx lr ; Jump back to whoever called us
|
||||
die:
|
||||
b die
|
||||
|
||||
.pool
|
||||
.close
|
||||
.close
|
||||
|
||||
202
patches/reboot.s
202
patches/reboot.s
@@ -1,17 +1,26 @@
|
||||
; Code originally from delebile and mid-kid
|
||||
|
||||
.arm.little
|
||||
|
||||
payload_addr equ 0x23F00000 ; Brahma payload address.
|
||||
payload_maxsize equ 0x10000 ; Maximum size for the payload (maximum that CakeBrah supports).
|
||||
argv_addr equ 0x27FFDF00
|
||||
fname_addr equ 0x27FFDF80
|
||||
low_tid_addr equ 0x27FFDFE0
|
||||
copy_launch_stub_addr equ 0x27FFE000
|
||||
|
||||
firm_addr equ 0x24000000
|
||||
firm_maxsize equ (copy_launch_stub_addr - 0x1000 - firm_addr)
|
||||
|
||||
arm11_entrypoint_addr equ 0x1FFFFFFC
|
||||
.create "build/reboot.bin", 0
|
||||
.arm
|
||||
; Interesting registers and locations to keep in mind, set just before this code is ran:
|
||||
; - r1: FIRM path in exefs.
|
||||
; - r7: pointer to file object
|
||||
; - r7 (or r8): pointer to file object
|
||||
; - *r7: vtable
|
||||
; - *(vtable + 0x28): fread function
|
||||
; - *(r7 + 8): file handle
|
||||
|
||||
sub r7, r0, #8
|
||||
mov r8, r1
|
||||
|
||||
pxi_wait_recv:
|
||||
@@ -25,48 +34,40 @@ payload_maxsize equ 0x10000 ; Maximum size for the payload (maximum that CakeB
|
||||
cmp r0, r2
|
||||
bne pxi_wait_recv
|
||||
|
||||
mov r4, #0
|
||||
adr r1, bin_fname
|
||||
b open_payload
|
||||
; Open file
|
||||
add r0, r7, #8
|
||||
adr r1, fname
|
||||
mov r2, #1
|
||||
ldr r6, [fopen]
|
||||
orr r6, 1
|
||||
blx r6
|
||||
cmp r0, #0
|
||||
bne panic
|
||||
|
||||
fallback:
|
||||
mov r4, #1
|
||||
adr r1, dat_fname
|
||||
|
||||
open_payload:
|
||||
; Open file
|
||||
add r0, r7, #8
|
||||
mov r2, #1
|
||||
ldr r6, [fopen]
|
||||
orr r6, 1
|
||||
blx r6
|
||||
cmp r0, #0
|
||||
bne fallback ; If the .bin is not found, try the .dat.
|
||||
|
||||
read_payload:
|
||||
; Read file
|
||||
mov r0, r7
|
||||
adr r1, bytes_read
|
||||
ldr r2, =payload_addr
|
||||
cmp r4, #0
|
||||
movne r3, #0x12000 ; Skip the first 0x12000 bytes.
|
||||
moveq r3, payload_maxsize
|
||||
ldr r6, [r7]
|
||||
ldr r6, [r6, #0x28]
|
||||
blx r6
|
||||
cmp r4, #0
|
||||
movne r4, #0
|
||||
bne read_payload ; Go read the real payload.
|
||||
; Read file
|
||||
mov r0, r7
|
||||
adr r1, bytes_read
|
||||
ldr r2, =firm_addr
|
||||
ldr r3, =firm_maxsize
|
||||
ldr r6, [r7]
|
||||
ldr r6, [r6, #0x28]
|
||||
blx r6
|
||||
|
||||
; Copy the low TID (in UTF-16) of the wanted firm to the 5th byte of the payload
|
||||
add r0, r8, 0x1A
|
||||
add r1, r0, #0x10
|
||||
ldr r2, =payload_addr + 4
|
||||
copy_TID_low:
|
||||
ldrh r3, [r0], #2
|
||||
strh r3, [r2], #2
|
||||
cmp r0, r1
|
||||
blo copy_TID_low
|
||||
ldr r0, =low_tid_addr
|
||||
add r1, r8, #0x1A
|
||||
mov r2, #0x10
|
||||
bl memcpy16
|
||||
|
||||
ldr r0, =fname_addr
|
||||
adr r1, fname
|
||||
mov r2, #42
|
||||
bl memcpy16
|
||||
|
||||
ldr r0, =argv_addr
|
||||
ldr r1, =fname_addr
|
||||
ldr r2, =low_tid_addr
|
||||
stmia r0, {r1, r2}
|
||||
|
||||
; Set kernel state
|
||||
mov r0, #0
|
||||
@@ -84,30 +85,117 @@ payload_maxsize equ 0x10000 ; Maximum size for the payload (maximum that CakeB
|
||||
die:
|
||||
b die
|
||||
|
||||
memcpy16:
|
||||
cmp r2, #0
|
||||
bxeq lr
|
||||
add r2, r0, r2
|
||||
copy_loop16:
|
||||
ldrh r3, [r1], #2
|
||||
strh r3, [r0], #2
|
||||
cmp r0, r2
|
||||
blo copy_loop16
|
||||
bx lr
|
||||
|
||||
panic:
|
||||
mov r1, r0 ; unused register
|
||||
mov r0, #0
|
||||
swi 0x3C ; svcBreak(USERBREAK_PANIC)
|
||||
b die
|
||||
|
||||
bytes_read: .word 0
|
||||
fopen: .ascii "OPEN"
|
||||
.pool
|
||||
bin_fname: .dcw "sdmc:/arm9loaderhax.bin"
|
||||
.word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
dat_fname: .dcw "sdmc:/Luma3DS.dat"
|
||||
.word 0
|
||||
|
||||
.area 82, 0
|
||||
fname: .ascii "FILE"
|
||||
.endarea
|
||||
|
||||
.pool
|
||||
nand_mount: .dcw "nand"
|
||||
|
||||
.align 4
|
||||
kernelcode_start:
|
||||
|
||||
mrs r0, cpsr ; disable interrupts
|
||||
orr r0, #0xC0
|
||||
msr cpsr, r0
|
||||
|
||||
ldr sp, =0x27FFDF00
|
||||
|
||||
ldr r0, =copy_launch_stub_addr
|
||||
adr r1, copy_launch_stub
|
||||
mov r2, #(copy_launch_stub_end - copy_launch_stub)
|
||||
bl memcpy32
|
||||
|
||||
; Disable MPU
|
||||
ldr r0, =0x42078 ; alt vector select, enable itcm
|
||||
ldr r0, =0x42078 ; alt vector select, enable itcm
|
||||
mcr p15, 0, r0, c1, c0, 0
|
||||
|
||||
bl flushCaches
|
||||
|
||||
ldr r0, =copy_launch_stub_addr
|
||||
bx r0
|
||||
|
||||
copy_launch_stub:
|
||||
|
||||
ldr r4, =firm_addr
|
||||
|
||||
mov r5, #0
|
||||
load_section_loop:
|
||||
; Such checks. Very ghetto. Wow.
|
||||
add r3, r4, #0x40
|
||||
add r3, r5,lsl #5
|
||||
add r3, r5,lsl #4
|
||||
ldmia r3, {r6-r8}
|
||||
mov r0, r7
|
||||
add r1, r4, r6
|
||||
mov r2, r8
|
||||
bl memcpy32
|
||||
add r5, #1
|
||||
cmp r5, #3
|
||||
blo load_section_loop
|
||||
|
||||
ldr r0, =arm11_entrypoint_addr
|
||||
ldr r1, [r4, #0x08]
|
||||
str r1, [r0]
|
||||
|
||||
mov r0, #2 ; argc
|
||||
ldr r1, =argv_addr ; argv
|
||||
ldr r2, =0xBEEF ; magic word
|
||||
|
||||
ldr r5, =arm11_entrypoint_addr
|
||||
ldr r6, [r4, #0x08]
|
||||
str r6, [r5]
|
||||
|
||||
ldr lr, [r4, #0x0c]
|
||||
bx lr
|
||||
|
||||
memcpy32:
|
||||
cmp r2, #0
|
||||
bxeq lr
|
||||
add r2, r0, r2
|
||||
copy_loop32:
|
||||
ldr r3, [r1], #4
|
||||
str r3, [r0], #4
|
||||
cmp r0, r2
|
||||
blo copy_loop32
|
||||
bx lr
|
||||
|
||||
.pool
|
||||
|
||||
copy_launch_stub_end:
|
||||
|
||||
flushCaches:
|
||||
|
||||
; Clean and flush data cache
|
||||
mov r1, #0 ; segment counter
|
||||
mov r1, #0 ; segment counter
|
||||
outer_loop:
|
||||
mov r0, #0 ; line counter
|
||||
mov r0, #0 ; line counter
|
||||
|
||||
inner_loop:
|
||||
orr r2, r1, r0 ; generate segment and line address
|
||||
mcr p15, 0, r2, c7, c14, 2 ; clean and flush the line
|
||||
add r0, #0x20 ; increment to next line
|
||||
orr r2, r1, r0 ; generate segment and line address
|
||||
mcr p15, 0, r2, c7, c14, 2 ; clean and flush the line
|
||||
add r0, #0x20 ; increment to next line
|
||||
cmp r0, #0x400
|
||||
bne inner_loop
|
||||
|
||||
@@ -115,14 +203,12 @@ dat_fname: .dcw "sdmc:/Luma3DS.dat"
|
||||
cmp r1, #0
|
||||
bne outer_loop
|
||||
|
||||
mcr p15, 0, r1, c7, c10, 4 ; drain write buffer
|
||||
; Drain write buffer
|
||||
mcr p15, 0, r1, c7, c10, 4
|
||||
|
||||
; Flush instruction cache
|
||||
mcr p15, 0, r1, c7, c5, 0
|
||||
|
||||
; Jump to payload
|
||||
ldr r0, =payload_addr
|
||||
bx r0
|
||||
bx lr
|
||||
|
||||
.pool
|
||||
.close
|
||||
.close
|
||||
|
||||
@@ -45,4 +45,4 @@ infoStart:
|
||||
.word 0 ; truncated commit hash
|
||||
.word 0 ; config
|
||||
infoEnd:
|
||||
.close
|
||||
.close
|
||||
|
||||
@@ -1,97 +0,0 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef uint8_t u8;
|
||||
|
||||
static u8 *memsearch(u8 *startPos, const void *pattern, int size, int patternSize)
|
||||
{
|
||||
const u8 *patternc = (const u8 *)pattern;
|
||||
int table[256];
|
||||
|
||||
//Preprocessing
|
||||
int i;
|
||||
for(i = 0; i < 256; i++)
|
||||
table[i] = patternSize;
|
||||
for(i = 0; i < patternSize - 1; i++)
|
||||
table[patternc[i]] = patternSize - i - 1;
|
||||
|
||||
//Searching
|
||||
int j = 0;
|
||||
while(j <= size - patternSize)
|
||||
{
|
||||
u8 c = startPos[j + patternSize - 1];
|
||||
if(patternc[patternSize - 1] == c && memcmp(pattern, startPos + j, patternSize - 1) == 0)
|
||||
return startPos + j;
|
||||
j += table[c];
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int fsize(FILE *fp)
|
||||
{
|
||||
fseek(fp, 0, SEEK_END);
|
||||
int size = ftell(fp);
|
||||
rewind(fp);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
static void error(FILE *payload, const char *message)
|
||||
{
|
||||
fclose(payload);
|
||||
printf("%s, are you sure you're using a Luma3DS payload?\n", message);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if(argc == 1)
|
||||
{
|
||||
printf("Usage: %s <Luma3DS payload path>\n", argv[0]);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
FILE *payload;
|
||||
size_t size;
|
||||
|
||||
payload = fopen(argv[1], "rb+");
|
||||
size = fsize(payload);
|
||||
if(size > 0x20000)
|
||||
error(payload, "The input file is too large");
|
||||
|
||||
u8 *buffer = (u8 *)malloc(size);
|
||||
fread(buffer, 1, size, payload);
|
||||
|
||||
u8 pattern[] = {'s', 0, 'd', 0, 'm', 0, 'c', 0, ':', 0, '/', 0};
|
||||
|
||||
u8 *found = memsearch(buffer, pattern, size, sizeof(pattern));
|
||||
|
||||
if(found == NULL)
|
||||
{
|
||||
free(buffer);
|
||||
error(payload, "Pattern not found");
|
||||
}
|
||||
|
||||
u8 input[38] = {0};
|
||||
u8 payloadname[2 * (sizeof(input) - 1)] = {0};
|
||||
|
||||
printf("Enter the payload's path (37 characters max): ");
|
||||
scanf("%37s", input);
|
||||
|
||||
unsigned int i;
|
||||
for (i = 0; i < sizeof(input) - 1; i++)
|
||||
payloadname[2 * i] = input[i];
|
||||
|
||||
memcpy(found + 12, payloadname, sizeof(payloadname));
|
||||
|
||||
rewind(payload);
|
||||
fwrite(buffer, 1, size, payload);
|
||||
|
||||
free(buffer);
|
||||
fclose(payload);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
# Requires Python >= 3.2 or >= 2.7
|
||||
|
||||
# This is part of Luma3DS
|
||||
|
||||
__author__ = "TuxSH"
|
||||
__copyright__ = "Copyright (c) 2016 TuxSH"
|
||||
__license__ = "GPLv3"
|
||||
__version__ = "v1.0"
|
||||
|
||||
import argparse
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="Changes the path to Luma3DS for reboot patches")
|
||||
parser.add_argument("payload", help="Path to the Luma3DS payload")
|
||||
parser.add_argument("new_path", help="New Luma3DS payload path")
|
||||
args = parser.parse_args()
|
||||
data = b""
|
||||
|
||||
if len(args.new_path) > 37:
|
||||
raise SystemExit("The new payload path is too large (37 characters max.)")
|
||||
|
||||
with open(args.payload, "rb") as f: data = bytearray(f.read())
|
||||
|
||||
if len(data) == 0: raise SystemExit("Could not read {0}".format(args.payload))
|
||||
|
||||
if len(data) > 0x20000:
|
||||
raise SystemExit("The input file is too large, are you sure you're using a Luma3DS payload?")
|
||||
|
||||
found_index = data.find("sdmc:/".encode("utf-16-le"))
|
||||
|
||||
if found_index == -1:
|
||||
raise SystemExit("The pattern was not found, are you sure you're usinga a Luma3DS payload?")
|
||||
|
||||
namebuf = args.new_path.encode("utf-16-le")
|
||||
namebuf += b'\x00' * (74 - len(namebuf))
|
||||
|
||||
data[found_index + 12 : found_index + 12 + 74] = namebuf
|
||||
|
||||
with open(args.payload, "wb+") as f: f.write(data)
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
163
source/3dsheaders.h
Normal file
163
source/3dsheaders.h
Normal file
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* This file is part of Luma3DS
|
||||
* Copyright (C) 2016 Aurora Wright, TuxSH
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
|
||||
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
|
||||
* Notices displayed by works containing it.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Adapted from 3DBrew and https://github.com/mid-kid/CakesForeveryWan/blob/master/source/headers.h
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
u32 address;
|
||||
u32 phyRegionSize;
|
||||
u32 size;
|
||||
} CodeSetInfo;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
u32 saveDataSize[2];
|
||||
u32 jumpID[2];
|
||||
u8 reserved[0x30];
|
||||
} SystemInfo;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
char appTitle[8];
|
||||
u8 reserved1[5];
|
||||
u8 flag;
|
||||
u8 remasterVersion[2];
|
||||
CodeSetInfo textCodeSet;
|
||||
u32 stackSize;
|
||||
CodeSetInfo roCodeSet;
|
||||
u8 reserved2[4];
|
||||
CodeSetInfo dataCodeSet;
|
||||
u32 bssSize;
|
||||
char depends[0x180];
|
||||
SystemInfo systemInfo;
|
||||
} SystemControlInfo;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
SystemControlInfo systemControlInfo;
|
||||
u8 aci[0x200];
|
||||
u8 accessDescSig[0x100];
|
||||
u8 ncchPubKey[0x100];
|
||||
u8 aciLim[0x200];
|
||||
} ExHeader;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
u8 sig[0x100]; //RSA-2048 signature of the NCCH header, using SHA-256
|
||||
char magic[4]; //NCCH
|
||||
u32 contentSize; //Media unit
|
||||
u8 partitionId[8];
|
||||
u8 makerCode[2];
|
||||
u16 version;
|
||||
u8 reserved1[4];
|
||||
u8 programID[8];
|
||||
u8 reserved2[0x10];
|
||||
u8 logoHash[0x20]; //Logo Region SHA-256 hash
|
||||
char productCode[0x10];
|
||||
u8 exHeaderHash[0x20]; //Extended header SHA-256 hash
|
||||
u32 exHeaderSize; //Extended header size
|
||||
u32 reserved3;
|
||||
u8 flags[8];
|
||||
u32 plainOffset; //Media unit
|
||||
u32 plainSize; //Media unit
|
||||
u32 logoOffset; //Media unit
|
||||
u32 logoSize; //Media unit
|
||||
u32 exeFsOffset; //Media unit
|
||||
u32 exeFsSize; //Media unit
|
||||
u32 exeFsHashSize; //Media unit
|
||||
u32 reserved4;
|
||||
u32 romFsOffset; //Media unit
|
||||
u32 romFsSize; //Media unit
|
||||
u32 romFsHashSize; //Media unit
|
||||
u32 reserved5;
|
||||
u8 exeFsHash[0x20]; //ExeFS superblock SHA-256 hash
|
||||
u8 romFsHash[0x20]; //RomFS superblock SHA-256 hash
|
||||
} Ncch;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
Ncch ncch;
|
||||
ExHeader exHeader;
|
||||
} Cxi;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
char sigIssuer[0x40];
|
||||
u8 eccPubKey[0x3C];
|
||||
u8 version;
|
||||
u8 caCrlVersion;
|
||||
u8 signerCrlVersion;
|
||||
u8 titleKey[0x10];
|
||||
u8 reserved1;
|
||||
u8 ticketId[8];
|
||||
u8 consoleId[4];
|
||||
u8 titleId[8];
|
||||
u8 reserved2[2];
|
||||
u16 ticketTitleVersion;
|
||||
u8 reserved3[8];
|
||||
u8 licenseType;
|
||||
u8 ticketCommonKeyYIndex; //Ticket common keyY index, usually 0x1 for retail system titles.
|
||||
u8 reserved4[0x2A];
|
||||
u8 unk[4]; //eShop Account ID?
|
||||
u8 reserved5;
|
||||
u8 audit;
|
||||
u8 reserved6[0x42];
|
||||
u8 limits[0x40];
|
||||
u8 contentIndex[0xAC];
|
||||
} Ticket;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
u32 offset;
|
||||
u8 *address;
|
||||
u32 size;
|
||||
u32 procType;
|
||||
u8 hash[0x20];
|
||||
} FirmSection;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
char magic[4];
|
||||
u32 reserved1;
|
||||
u8 *arm11Entry;
|
||||
u8 *arm9Entry;
|
||||
u8 reserved2[0x30];
|
||||
FirmSection section[4];
|
||||
} Firm;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
u8 keyX[0x10];
|
||||
u8 keyY[0x10];
|
||||
u8 ctr[0x10];
|
||||
char size[8];
|
||||
u8 reserved[8];
|
||||
u8 ctlBlock[0x10];
|
||||
char magic[4];
|
||||
u8 reserved2[0xC];
|
||||
u8 slot0x16keyX[0x10];
|
||||
} Arm9Bin;
|
||||
@@ -39,9 +39,9 @@
|
||||
#define BUTTON_UP (1 << 6)
|
||||
#define BUTTON_DOWN (1 << 7)
|
||||
|
||||
#define DPAD_BUTTONS (BUTTON_LEFT | BUTTON_RIGHT | BUTTON_UP | BUTTON_DOWN)
|
||||
#define SAFE_MODE (BUTTON_R1 | BUTTON_L1 | BUTTON_A | BUTTON_UP)
|
||||
#define SINGLE_PAYLOAD_BUTTONS (BUTTON_LEFT | BUTTON_RIGHT | BUTTON_UP | BUTTON_DOWN | BUTTON_START | BUTTON_B | BUTTON_X | BUTTON_Y)
|
||||
#define L_PAYLOAD_BUTTONS (BUTTON_R1 | BUTTON_A | BUTTON_SELECT)
|
||||
#define EMUNAND_BUTTONS (BUTTON_LEFT | BUTTON_RIGHT | BUTTON_UP | BUTTON_DOWN)
|
||||
#define MENU_BUTTONS (BUTTON_LEFT | BUTTON_RIGHT | BUTTON_UP | BUTTON_DOWN | BUTTON_A | BUTTON_START)
|
||||
#define PIN_BUTTONS (BUTTON_A | BUTTON_B | BUTTON_X | BUTTON_Y | BUTTON_LEFT | BUTTON_RIGHT | BUTTON_UP | BUTTON_DOWN | BUTTON_START)
|
||||
#define SINGLE_PAYLOAD_BUTTONS (DPAD_BUTTONS | BUTTON_B | BUTTON_X | BUTTON_Y)
|
||||
#define L_PAYLOAD_BUTTONS (BUTTON_R1 | BUTTON_A | BUTTON_START | BUTTON_SELECT)
|
||||
#define MENU_BUTTONS (DPAD_BUTTONS | BUTTON_A | BUTTON_START)
|
||||
#define PIN_BUTTONS (BUTTON_A | BUTTON_B | BUTTON_X | BUTTON_Y | DPAD_BUTTONS | BUTTON_START | BUTTON_SELECT)
|
||||
395
source/config.c
395
source/config.c
@@ -29,165 +29,207 @@
|
||||
#include "buttons.h"
|
||||
#include "pin.h"
|
||||
|
||||
CfgData configData;
|
||||
ConfigurationStatus needConfig;
|
||||
static u32 oldConfig;
|
||||
|
||||
bool readConfig(void)
|
||||
{
|
||||
if(fileRead(&configData, CONFIG_PATH, sizeof(CfgData)) != sizeof(CfgData) ||
|
||||
bool ret;
|
||||
|
||||
if(fileRead(&configData, CONFIG_FILE, sizeof(CfgData)) != sizeof(CfgData) ||
|
||||
memcmp(configData.magic, "CONF", 4) != 0 ||
|
||||
configData.formatVersionMajor != CONFIG_VERSIONMAJOR ||
|
||||
configData.formatVersionMinor != CONFIG_VERSIONMINOR)
|
||||
{
|
||||
configData.config = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
ret = false;
|
||||
}
|
||||
else ret = true;
|
||||
|
||||
oldConfig = configData.config;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void writeConfig(ConfigurationStatus needConfig, u32 configTemp)
|
||||
void writeConfig(bool isPayloadLaunch)
|
||||
{
|
||||
if(isPayloadLaunch) configData.config = (configData.config & 0xFFFFFF80) | (oldConfig & 0x7F);
|
||||
|
||||
/* If the configuration is different from previously, overwrite it.
|
||||
Just the no-forcing flag being set is not enough */
|
||||
if(needConfig == CREATE_CONFIGURATION || (configTemp & 0xFFFFFF7F) != configData.config)
|
||||
if(needConfig != CREATE_CONFIGURATION && (configData.config & 0xFFFFFFBF) == oldConfig) return;
|
||||
|
||||
if(needConfig == CREATE_CONFIGURATION)
|
||||
{
|
||||
if(needConfig == CREATE_CONFIGURATION)
|
||||
{
|
||||
memcpy(configData.magic, "CONF", 4);
|
||||
configData.formatVersionMajor = CONFIG_VERSIONMAJOR;
|
||||
configData.formatVersionMinor = CONFIG_VERSIONMINOR;
|
||||
}
|
||||
|
||||
//Merge the new options and new boot configuration
|
||||
configData.config = (configData.config & 0xFFFFFE00) | (configTemp & 0x1FF);
|
||||
|
||||
if(!fileWrite(&configData, CONFIG_PATH, sizeof(CfgData)))
|
||||
error("Error writing the configuration file");
|
||||
memcpy(configData.magic, "CONF", 4);
|
||||
configData.formatVersionMajor = CONFIG_VERSIONMAJOR;
|
||||
configData.formatVersionMinor = CONFIG_VERSIONMINOR;
|
||||
}
|
||||
|
||||
if(!fileWrite(&configData, CONFIG_FILE, sizeof(CfgData)))
|
||||
error("Error writing the configuration file");
|
||||
}
|
||||
|
||||
void configMenu(bool oldPinStatus)
|
||||
void configMenu(bool isSdMode, bool oldPinStatus, u32 oldPinMode)
|
||||
{
|
||||
const char *multiOptionsText[] = { "Default EmuNAND: 1( ) 2( ) 3( ) 4( )",
|
||||
"Screen brightness: 4( ) 3( ) 2( ) 1( )",
|
||||
"Splash: Off( ) Before( ) After( ) payloads",
|
||||
"PIN lock: Off( ) 4( ) 6( ) 8( ) digits",
|
||||
"New 3DS CPU: Off( ) Clock( ) L2( ) Clock+L2( )"
|
||||
#ifdef DEV
|
||||
, "Dev. features: ErrDisp( ) UNITINFO( ) Off( )"
|
||||
#endif
|
||||
"New 3DS CPU: Off( ) Clock( ) L2( ) Clock+L2( )",
|
||||
};
|
||||
|
||||
const char *singleOptionsText[] = { "( ) Autoboot SysNAND",
|
||||
"( ) Use SysNAND FIRM if booting with R (A9LH)",
|
||||
"( ) Enable region/language emu. and ext. .code",
|
||||
const char *singleOptionsText[] = { "( ) Autoboot EmuNAND",
|
||||
"( ) Use EmuNAND FIRM if booting with R",
|
||||
"( ) Enable loading external FIRMs and modules",
|
||||
"( ) Enable game patching",
|
||||
"( ) Show NAND or user string in System Settings",
|
||||
"( ) Show GBA boot screen in patched AGB_FIRM",
|
||||
"( ) Display splash screen before payloads"
|
||||
#ifdef DEV
|
||||
, "( ) Patch SVC/service/archive/ARM9 access"
|
||||
#endif
|
||||
"( ) Patch SVC/service/archive/ARM9 access",
|
||||
"( ) Set developer UNITINFO",
|
||||
"( ) Enable exception handlers"
|
||||
};
|
||||
|
||||
const char *optionsDescription[] = { "Select the default EmuNAND.\n"
|
||||
"It will booted with no directional pad\n"
|
||||
"buttons pressed",
|
||||
const char *optionsDescription[] = { "Select the default EmuNAND.\n\n"
|
||||
"It will be booted when no\n"
|
||||
"directional pad buttons are pressed.",
|
||||
|
||||
"Select the screen brightness",
|
||||
"Select the screen brightness.",
|
||||
|
||||
"Activate a PIN lock.\n"
|
||||
"Enable splash screen support.\n\n"
|
||||
"\t* 'Before payloads' displays it\n"
|
||||
"before booting payloads\n"
|
||||
"(intended for splashes that display\n"
|
||||
"button hints).\n\n"
|
||||
"\t* 'After payloads' displays it\n"
|
||||
"afterwards.",
|
||||
|
||||
"Activate a PIN lock.\n\n"
|
||||
"The PIN will be asked each time\n"
|
||||
"Luma3DS boots.\n"
|
||||
"4, 6 or 8 digits can be selected.\n"
|
||||
"Luma3DS boots.\n\n"
|
||||
"4, 6 or 8 digits can be selected.\n\n"
|
||||
"The ABXY buttons and the directional\n"
|
||||
"pad buttons can be used as keys",
|
||||
"pad buttons can be used as keys.\n\n"
|
||||
"A message can also be displayed\n"
|
||||
"(refer to the wiki for instructions).",
|
||||
|
||||
"Select the New 3DS CPU mode.\n"
|
||||
"It will be always enabled.\n"
|
||||
"Select the New 3DS CPU mode.\n\n"
|
||||
"This won't apply to\n"
|
||||
"New 3DS exclusive/enhanced games.\n\n"
|
||||
"'Clock+L2' can cause issues with some\n"
|
||||
"games",
|
||||
#ifdef DEV
|
||||
"Select the developer features.\n"
|
||||
"'ErrDisp' displays debug information\n"
|
||||
"on the 'An error has occurred' screen.\n"
|
||||
"'UNITINFO' makes the console be always\n"
|
||||
"detected as a development unit (which\n"
|
||||
"breaks online features and allows\n"
|
||||
"booting some developer software).\n"
|
||||
"'Off' disables exception handlers\n"
|
||||
"in FIRM",
|
||||
#endif
|
||||
"If enabled SysNAND will be launched on\n"
|
||||
"boot. Otherwise, an EmuNAND will.\n"
|
||||
"Hold L on boot to switch NAND.\n"
|
||||
"games.",
|
||||
|
||||
"If enabled, an EmuNAND\n"
|
||||
"will be launched on boot.\n\n"
|
||||
"Otherwise, SysNAND will.\n\n"
|
||||
"Hold L on boot to switch NAND.\n\n"
|
||||
"To use a different EmuNAND from the\n"
|
||||
"default, hold a directional pad button\n"
|
||||
"(Up/Right/Down/Left equal EmuNANDs\n"
|
||||
"1/2/3/4)",
|
||||
"1/2/3/4).",
|
||||
|
||||
"If enabled, when holding R on boot\n"
|
||||
"EmuNAND will be booted with the\n"
|
||||
"SysNAND FIRM. Otherwise, SysNAND will\n"
|
||||
"be booted with an EmuNAND FIRM.\n"
|
||||
"SysNAND will be booted with an\n"
|
||||
"EmuNAND FIRM.\n\n"
|
||||
"Otherwise, an EmuNAND will be booted\n"
|
||||
"with the SysNAND FIRM.\n\n"
|
||||
"To use a different EmuNAND from the\n"
|
||||
"default, hold a directional pad button\n"
|
||||
"(Up/Right/Down/Left equal EmuNANDs\n"
|
||||
"1/2/3/4)",
|
||||
"1/2/3/4), also add A if you have\n"
|
||||
"a matching payload.",
|
||||
|
||||
"Enable loading external FIRMs and\n"
|
||||
"system modules.\n\n"
|
||||
"This isn't needed in most cases.\n\n"
|
||||
"Refer to the wiki for instructions.",
|
||||
|
||||
"Enable overriding the region and\n"
|
||||
"language configuration and the usage\n"
|
||||
"of patched code binaries for specific\n"
|
||||
"games.\n"
|
||||
"Also makes certain DLCs for\n"
|
||||
"out-of-region games work.\n"
|
||||
"Refer to the wiki for instructions",
|
||||
"of patched code binaries,\n"
|
||||
"IPS code patches and LayeredFS\n"
|
||||
"for specific games.\n\n"
|
||||
"Also makes certain DLCs\n"
|
||||
"for out-of-region games work.\n\n"
|
||||
"Enabling this requires the\n"
|
||||
"archive patch to be applied.\n\n"
|
||||
"Refer to the wiki for instructions.",
|
||||
|
||||
"Show the currently booted NAND\n"
|
||||
"(Sys = SysNAND, Emu = EmuNAND 1,\n"
|
||||
"EmuX = EmuNAND X,\n"
|
||||
"SysE = SysNAND with EmuNAND 1 FIRM,\n"
|
||||
"SyEX = SysNAND with EmuNAND X FIRM,\n"
|
||||
"EmXS = EmuNAND X with SysNAND FIRM)\n"
|
||||
"Enable showing the current NAND/FIRM:\n\n"
|
||||
"\t* Sys = SysNAND\n"
|
||||
"\t* Emu = EmuNAND 1\n"
|
||||
"\t* EmuX = EmuNAND X\n"
|
||||
"\t* SysE = SysNAND with EmuNAND 1 FIRM\n"
|
||||
"\t* SyEX = SysNAND with EmuNAND X FIRM\n"
|
||||
"\t* EmuS = EmuNAND 1 with SysNAND FIRM\n"
|
||||
"\t* EmXS = EmuNAND X with SysNAND FIRM\n\n"
|
||||
"or an user-defined custom string in\n"
|
||||
"System Settings.\n"
|
||||
"Refer to the wiki for instructions",
|
||||
"System Settings.\n\n"
|
||||
"Refer to the wiki for instructions.",
|
||||
|
||||
"Show the GBA boot screen when booting\n"
|
||||
"GBA games",
|
||||
"Enable showing the GBA boot screen\n"
|
||||
"when booting GBA games.",
|
||||
|
||||
"If enabled, the splash screen will be\n"
|
||||
"displayed before booting payloads,\n"
|
||||
"otherwise it will be displayed\n"
|
||||
"afterwards.\n"
|
||||
"Intended for splash screens that\n"
|
||||
"display button hints"
|
||||
#ifdef DEV
|
||||
, "Disable SVC, service, archive and ARM9\n"
|
||||
"exheader access checks"
|
||||
#endif
|
||||
"Disable SVC, service, archive and ARM9\n"
|
||||
"exheader access checks.\n\n"
|
||||
"The service and archive patches\n"
|
||||
"don't work on New 3DS FIRMs between\n"
|
||||
"9.3 and 10.4.\n\n"
|
||||
"Only select this if you know what you\n"
|
||||
"are doing!",
|
||||
|
||||
"Make the console be always detected\n"
|
||||
"as a development unit, and conversely.\n"
|
||||
"(which breaks online features, amiibo\n"
|
||||
"and retail CIAs, but allows installing\n"
|
||||
"and booting some developer software).\n\n"
|
||||
"Only select this if you know what you\n"
|
||||
"are doing!",
|
||||
|
||||
"Enable Luma3DS's ARM9/ARM11 exception\n"
|
||||
"handlers. Luma3DS should be ran as\n"
|
||||
"boot.firm\n\n"
|
||||
"Useful for debugging."
|
||||
};
|
||||
|
||||
struct multiOption {
|
||||
u32 posXs[4];
|
||||
u32 posY;
|
||||
u32 enabled;
|
||||
bool visible;
|
||||
} multiOptions[] = {
|
||||
{ .posXs = {19, 24, 29, 34} },
|
||||
{ .posXs = {21, 26, 31, 36} },
|
||||
{ .posXs = {14, 19, 24, 29} },
|
||||
{ .posXs = {17, 26, 32, 44} }
|
||||
#ifdef DEV
|
||||
, { .posXs = {23, 35, 43, 0} }
|
||||
#endif
|
||||
{ .posXs = {19, 24, 29, 34}, .visible = isSdMode },
|
||||
{ .posXs = {21, 26, 31, 36}, .visible = true },
|
||||
{ .posXs = {12, 22, 31, 0}, .visible = true },
|
||||
{ .posXs = {14, 19, 24, 29}, .visible = true },
|
||||
{ .posXs = {17, 26, 32, 44}, .visible = ISN3DS },
|
||||
};
|
||||
|
||||
//Calculate the amount of the various kinds of options and pre-select the first single one
|
||||
u32 multiOptionsAmount = sizeof(multiOptions) / sizeof(struct multiOption),
|
||||
singleOptionsAmount = sizeof(singleOptionsText) / sizeof(char *),
|
||||
totalIndexes = multiOptionsAmount + singleOptionsAmount - 1,
|
||||
selectedOption = multiOptionsAmount;
|
||||
|
||||
struct singleOption {
|
||||
u32 posY;
|
||||
bool enabled;
|
||||
} singleOptions[singleOptionsAmount];
|
||||
bool visible;
|
||||
} singleOptions[] = {
|
||||
{ .visible = isSdMode },
|
||||
{ .visible = isSdMode },
|
||||
{ .visible = true },
|
||||
{ .visible = true },
|
||||
{ .visible = true },
|
||||
{ .visible = true },
|
||||
{ .visible = true },
|
||||
{ .visible = true },
|
||||
{ .visible = true }
|
||||
};
|
||||
|
||||
//Calculate the amount of the various kinds of options and pre-select the first single one
|
||||
u32 multiOptionsAmount = sizeof(multiOptions) / sizeof(struct multiOption),
|
||||
singleOptionsAmount = sizeof(singleOptions) / sizeof(struct singleOption),
|
||||
totalIndexes = multiOptionsAmount + singleOptionsAmount - 1,
|
||||
selectedOption,
|
||||
singleSelected;
|
||||
bool isMultiOption = false;
|
||||
|
||||
//Parse the existing options
|
||||
for(u32 i = 0; i < multiOptionsAmount; i++)
|
||||
@@ -197,73 +239,100 @@ void configMenu(bool oldPinStatus)
|
||||
|
||||
initScreens();
|
||||
|
||||
drawString(CONFIG_TITLE, true, 10, 10, COLOR_TITLE);
|
||||
drawString("Press A to select, START to save", true, 10, 30, COLOR_WHITE);
|
||||
drawString(true, 10, 10, COLOR_TITLE, CONFIG_TITLE);
|
||||
drawString(true, 10, 10 + SPACING_Y, COLOR_TITLE, "Press A to select, START to save");
|
||||
|
||||
//Character to display a selected option
|
||||
char selected = 'x';
|
||||
|
||||
u32 endPos = 42;
|
||||
u32 endPos = 10 + 2 * SPACING_Y;
|
||||
|
||||
//Display all the multiple choice options in white
|
||||
for(u32 i = 0; i < multiOptionsAmount; i++)
|
||||
{
|
||||
if(!(i == NEWCPU && !isN3DS))
|
||||
{
|
||||
multiOptions[i].posY = endPos + SPACING_Y;
|
||||
endPos = drawString(multiOptionsText[i], true, 10, multiOptions[i].posY, COLOR_WHITE);
|
||||
drawCharacter(selected, true, 10 + multiOptions[i].posXs[multiOptions[i].enabled] * SPACING_X, multiOptions[i].posY, COLOR_WHITE);
|
||||
}
|
||||
if(!multiOptions[i].visible) continue;
|
||||
|
||||
multiOptions[i].posY = endPos + SPACING_Y;
|
||||
endPos = drawString(true, 10, multiOptions[i].posY, COLOR_WHITE, multiOptionsText[i]);
|
||||
drawCharacter(true, 10 + multiOptions[i].posXs[multiOptions[i].enabled] * SPACING_X, multiOptions[i].posY, COLOR_WHITE, selected);
|
||||
}
|
||||
|
||||
endPos += SPACING_Y / 2;
|
||||
u32 color = COLOR_RED;
|
||||
|
||||
//Display all the normal options in white except for the first one
|
||||
for(u32 i = 0; i < singleOptionsAmount; i++)
|
||||
for(u32 i = 0, color = COLOR_RED; i < singleOptionsAmount; i++)
|
||||
{
|
||||
if(!singleOptions[i].visible) continue;
|
||||
|
||||
singleOptions[i].posY = endPos + SPACING_Y;
|
||||
endPos = drawString(singleOptionsText[i], true, 10, singleOptions[i].posY, color);
|
||||
if(singleOptions[i].enabled) drawCharacter(selected, true, 10 + SPACING_X, singleOptions[i].posY, color);
|
||||
color = COLOR_WHITE;
|
||||
endPos = drawString(true, 10, singleOptions[i].posY, color, singleOptionsText[i]);
|
||||
if(singleOptions[i].enabled) drawCharacter(true, 10 + SPACING_X, singleOptions[i].posY, color, selected);
|
||||
|
||||
if(color == COLOR_RED)
|
||||
{
|
||||
singleSelected = i;
|
||||
selectedOption = i + multiOptionsAmount;
|
||||
color = COLOR_WHITE;
|
||||
}
|
||||
}
|
||||
|
||||
drawString(optionsDescription[selectedOption], false, 10, 10, COLOR_WHITE);
|
||||
|
||||
u32 pressed = 0;
|
||||
drawString(false, 10, 10, COLOR_WHITE, optionsDescription[selectedOption]);
|
||||
|
||||
//Boring configuration menu
|
||||
while(pressed != BUTTON_START)
|
||||
while(true)
|
||||
{
|
||||
u32 pressed;
|
||||
do
|
||||
{
|
||||
pressed = waitInput();
|
||||
pressed = waitInput(true);
|
||||
}
|
||||
while(!(pressed & MENU_BUTTONS));
|
||||
|
||||
if(pressed == BUTTON_START) break;
|
||||
|
||||
if(pressed != BUTTON_A)
|
||||
{
|
||||
//Remember the previously selected option
|
||||
u32 oldSelectedOption = selectedOption;
|
||||
|
||||
switch(pressed)
|
||||
while(true)
|
||||
{
|
||||
case BUTTON_UP:
|
||||
if(!selectedOption) selectedOption = totalIndexes;
|
||||
else selectedOption = (selectedOption == NEWCPU + 1 && !isN3DS) ? selectedOption - 2 : selectedOption - 1;
|
||||
switch(pressed)
|
||||
{
|
||||
case BUTTON_UP:
|
||||
selectedOption = !selectedOption ? totalIndexes : selectedOption - 1;
|
||||
break;
|
||||
case BUTTON_DOWN:
|
||||
selectedOption = selectedOption == totalIndexes ? 0 : selectedOption + 1;
|
||||
break;
|
||||
case BUTTON_LEFT:
|
||||
pressed = BUTTON_DOWN;
|
||||
selectedOption = 0;
|
||||
break;
|
||||
case BUTTON_RIGHT:
|
||||
pressed = BUTTON_UP;
|
||||
selectedOption = totalIndexes;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if(selectedOption < multiOptionsAmount)
|
||||
{
|
||||
if(!multiOptions[selectedOption].visible) continue;
|
||||
|
||||
isMultiOption = true;
|
||||
break;
|
||||
case BUTTON_DOWN:
|
||||
if(selectedOption == totalIndexes) selectedOption = 0;
|
||||
else selectedOption = (selectedOption == NEWCPU - 1 && !isN3DS) ? selectedOption + 2 : selectedOption + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
singleSelected = selectedOption - multiOptionsAmount;
|
||||
|
||||
if(!singleOptions[singleSelected].visible) continue;
|
||||
|
||||
isMultiOption = false;
|
||||
break;
|
||||
case BUTTON_LEFT:
|
||||
selectedOption = 0;
|
||||
break;
|
||||
case BUTTON_RIGHT:
|
||||
selectedOption = totalIndexes;
|
||||
break;
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if(selectedOption == oldSelectedOption) continue;
|
||||
@@ -271,72 +340,60 @@ void configMenu(bool oldPinStatus)
|
||||
//The user moved to a different option, print the old option in white and the new one in red. Only print 'x's if necessary
|
||||
if(oldSelectedOption < multiOptionsAmount)
|
||||
{
|
||||
drawString(multiOptionsText[oldSelectedOption], true, 10, multiOptions[oldSelectedOption].posY, COLOR_WHITE);
|
||||
drawCharacter(selected, true, 10 + multiOptions[oldSelectedOption].posXs[multiOptions[oldSelectedOption].enabled] * SPACING_X, multiOptions[oldSelectedOption].posY, COLOR_WHITE);
|
||||
drawString(true, 10, multiOptions[oldSelectedOption].posY, COLOR_WHITE, multiOptionsText[oldSelectedOption]);
|
||||
drawCharacter(true, 10 + multiOptions[oldSelectedOption].posXs[multiOptions[oldSelectedOption].enabled] * SPACING_X, multiOptions[oldSelectedOption].posY, COLOR_WHITE, selected);
|
||||
}
|
||||
else
|
||||
{
|
||||
u32 singleOldSelected = oldSelectedOption - multiOptionsAmount;
|
||||
drawString(singleOptionsText[singleOldSelected], true, 10, singleOptions[singleOldSelected].posY, COLOR_WHITE);
|
||||
if(singleOptions[singleOldSelected].enabled) drawCharacter(selected, true, 10 + SPACING_X, singleOptions[singleOldSelected].posY, COLOR_WHITE);
|
||||
drawString(true, 10, singleOptions[singleOldSelected].posY, COLOR_WHITE, singleOptionsText[singleOldSelected]);
|
||||
if(singleOptions[singleOldSelected].enabled) drawCharacter(true, 10 + SPACING_X, singleOptions[singleOldSelected].posY, COLOR_WHITE, selected);
|
||||
}
|
||||
|
||||
if(selectedOption < multiOptionsAmount)
|
||||
drawString(multiOptionsText[selectedOption], true, 10, multiOptions[selectedOption].posY, COLOR_RED);
|
||||
else
|
||||
{
|
||||
u32 singleSelected = selectedOption - multiOptionsAmount;
|
||||
drawString(singleOptionsText[singleSelected], true, 10, singleOptions[singleSelected].posY, COLOR_RED);
|
||||
}
|
||||
if(isMultiOption) drawString(true, 10, multiOptions[selectedOption].posY, COLOR_RED, multiOptionsText[selectedOption]);
|
||||
else drawString(true, 10, singleOptions[singleSelected].posY, COLOR_RED, singleOptionsText[singleSelected]);
|
||||
|
||||
clearScreens(false, true);
|
||||
drawString(optionsDescription[selectedOption], false, 10, 10, COLOR_WHITE);
|
||||
drawString(false, 10, 10, COLOR_BLACK, optionsDescription[oldSelectedOption]);
|
||||
drawString(false, 10, 10, COLOR_WHITE, optionsDescription[selectedOption]);
|
||||
}
|
||||
else
|
||||
{
|
||||
//The selected option's status changed, print the 'x's accordingly
|
||||
if(selectedOption < multiOptionsAmount)
|
||||
if(isMultiOption)
|
||||
{
|
||||
u32 oldEnabled = multiOptions[selectedOption].enabled;
|
||||
drawCharacter(selected, true, 10 + multiOptions[selectedOption].posXs[oldEnabled] * SPACING_X, multiOptions[selectedOption].posY, COLOR_BLACK);
|
||||
drawCharacter(true, 10 + multiOptions[selectedOption].posXs[oldEnabled] * SPACING_X, multiOptions[selectedOption].posY, COLOR_BLACK, selected);
|
||||
multiOptions[selectedOption].enabled = (oldEnabled == 3 || !multiOptions[selectedOption].posXs[oldEnabled + 1]) ? 0 : oldEnabled + 1;
|
||||
|
||||
if(selectedOption == BRIGHTNESS) updateBrightness(multiOptions[BRIGHTNESS].enabled);
|
||||
}
|
||||
else
|
||||
{
|
||||
bool oldEnabled = singleOptions[selectedOption - multiOptionsAmount].enabled;
|
||||
singleOptions[selectedOption - multiOptionsAmount].enabled = !oldEnabled;
|
||||
if(oldEnabled) drawCharacter(selected, true, 10 + SPACING_X, singleOptions[selectedOption - multiOptionsAmount].posY, COLOR_BLACK);
|
||||
bool oldEnabled = singleOptions[singleSelected].enabled;
|
||||
singleOptions[singleSelected].enabled = !oldEnabled;
|
||||
if(oldEnabled) drawCharacter(true, 10 + SPACING_X, singleOptions[singleSelected].posY, COLOR_BLACK, selected);
|
||||
}
|
||||
}
|
||||
|
||||
//In any case, if the current option is enabled (or a multiple choice option is selected) we must display a red 'x'
|
||||
if(selectedOption < multiOptionsAmount)
|
||||
drawCharacter(selected, true, 10 + multiOptions[selectedOption].posXs[multiOptions[selectedOption].enabled] * SPACING_X, multiOptions[selectedOption].posY, COLOR_RED);
|
||||
else
|
||||
{
|
||||
u32 singleSelected = selectedOption - multiOptionsAmount;
|
||||
if(singleOptions[singleSelected].enabled) drawCharacter(selected, true, 10 + SPACING_X, singleOptions[singleSelected].posY, COLOR_RED);
|
||||
}
|
||||
if(isMultiOption) drawCharacter(true, 10 + multiOptions[selectedOption].posXs[multiOptions[selectedOption].enabled] * SPACING_X, multiOptions[selectedOption].posY, COLOR_RED, selected);
|
||||
else if(singleOptions[singleSelected].enabled) drawCharacter(true, 10 + SPACING_X, singleOptions[singleSelected].posY, COLOR_RED, selected);
|
||||
}
|
||||
|
||||
u32 oldPinLength = MULTICONFIG(PIN);
|
||||
|
||||
//Preserve the last-used boot options (first 9 bits)
|
||||
configData.config &= 0x1FF;
|
||||
configData.config &= 0x7F;
|
||||
|
||||
//Parse and write the new configuration
|
||||
for(u32 i = 0; i < multiOptionsAmount; i++)
|
||||
configData.config |= multiOptions[i].enabled << (i * 2 + 9);
|
||||
configData.config |= multiOptions[i].enabled << (i * 2 + 7);
|
||||
for(u32 i = 0; i < singleOptionsAmount; i++)
|
||||
configData.config |= (singleOptions[i].enabled ? 1 : 0) << (i + 21);
|
||||
configData.config |= (singleOptions[i].enabled ? 1 : 0) << (i + 17);
|
||||
|
||||
if(MULTICONFIG(PIN) != 0) newPin(oldPinStatus && MULTICONFIG(PIN) == oldPinLength);
|
||||
else if(oldPinStatus) fileDelete(PIN_PATH);
|
||||
u32 newPinMode = MULTICONFIG(PIN);
|
||||
|
||||
if(newPinMode != 0) newPin(oldPinStatus && newPinMode == oldPinMode, newPinMode);
|
||||
else if(oldPinStatus) fileDelete(PIN_FILE);
|
||||
|
||||
//Wait for the pressed buttons to change
|
||||
while(HID_PAD & PIN_BUTTONS);
|
||||
|
||||
chrono(2);
|
||||
}
|
||||
wait(2000ULL);
|
||||
}
|
||||
|
||||
@@ -24,52 +24,40 @@
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#define CONFIG(a) (((configData.config >> (a + 21)) & 1) != 0)
|
||||
#define MULTICONFIG(a) ((configData.config >> (a * 2 + 9)) & 3)
|
||||
#define CONFIG(a) (((configData.config >> (a + 17)) & 1) != 0)
|
||||
#define MULTICONFIG(a) ((configData.config >> (a * 2 + 7)) & 3)
|
||||
#define BOOTCONFIG(a, b) ((configData.config >> a) & b)
|
||||
|
||||
#define CONFIG_PATH "/luma/config.bin"
|
||||
#define CONFIG_FILE "config.bin"
|
||||
#define CONFIG_VERSIONMAJOR 1
|
||||
#define CONFIG_VERSIONMINOR 4
|
||||
#define CONFIG_VERSIONMINOR 11
|
||||
|
||||
#define BOOTCFG_NAND BOOTCONFIG(0, 7)
|
||||
#define BOOTCFG_FIRM BOOTCONFIG(3, 7)
|
||||
#define BOOTCFG_A9LH BOOTCONFIG(6, 1)
|
||||
#define BOOTCFG_NOFORCEFLAG BOOTCONFIG(7, 1)
|
||||
#define BOOTCFG_SAFEMODE BOOTCONFIG(8, 1)
|
||||
#define BOOTCFG_NOFORCEFLAG BOOTCONFIG(6, 1)
|
||||
|
||||
enum multiOptions
|
||||
{
|
||||
DEFAULTEMU = 0,
|
||||
BRIGHTNESS,
|
||||
SPLASH,
|
||||
PIN,
|
||||
NEWCPU
|
||||
#ifdef DEV
|
||||
, DEVOPTIONS
|
||||
#endif
|
||||
NEWCPU,
|
||||
};
|
||||
|
||||
enum singleOptions
|
||||
{
|
||||
AUTOBOOTSYS = 0,
|
||||
USESYSFIRM,
|
||||
USELANGEMUANDCODE,
|
||||
AUTOBOOTEMU = 0,
|
||||
USEEMUFIRM,
|
||||
LOADEXTFIRMSANDMODULES,
|
||||
PATCHGAMES,
|
||||
PATCHVERSTRING,
|
||||
SHOWGBABOOT,
|
||||
PAYLOADSPLASH
|
||||
#ifdef DEV
|
||||
, PATCHACCESS
|
||||
#endif
|
||||
PATCHACCESS,
|
||||
PATCHUNITINFO,
|
||||
ENABLEEXCEPTIONHANDLERS
|
||||
};
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
char magic[4];
|
||||
u16 formatVersionMajor, formatVersionMinor;
|
||||
|
||||
u32 config;
|
||||
} CfgData;
|
||||
|
||||
typedef enum ConfigurationStatus
|
||||
{
|
||||
DONT_CONFIGURE = 0,
|
||||
@@ -77,9 +65,6 @@ typedef enum ConfigurationStatus
|
||||
CREATE_CONFIGURATION
|
||||
} ConfigurationStatus;
|
||||
|
||||
extern CfgData configData;
|
||||
extern bool isN3DS;
|
||||
|
||||
bool readConfig(void);
|
||||
void writeConfig(ConfigurationStatus needConfig, u32 configTemp);
|
||||
void configMenu(bool oldPinStatus);
|
||||
void writeConfig(bool isPayloadLaunch);
|
||||
void configMenu(bool isSdMode, bool oldPinStatus, u32 oldPinMode);
|
||||
|
||||
303
source/crypto.c
303
source/crypto.c
@@ -22,11 +22,15 @@
|
||||
|
||||
/*
|
||||
* Crypto libs from http://github.com/b1l1s/ctr
|
||||
* ARM9Loader code originally adapted from https://github.com/Reisyukaku/ReiNand/blob/228c378255ba693133dec6f3368e14d386f2cde7/source/crypto.c#L233
|
||||
* kernel9Loader code originally adapted from https://github.com/Reisyukaku/ReiNand/blob/228c378255ba693133dec6f3368e14d386f2cde7/source/crypto.c#L233
|
||||
* decryptNusFirm code adapted from https://github.com/mid-kid/CakesForeveryWan/blob/master/source/firm.c
|
||||
* ctrNandWrite logic adapted from https://github.com/d0k3/GodMode9/blob/master/source/nand/nand.c
|
||||
*/
|
||||
|
||||
#include "crypto.h"
|
||||
#include "memory.h"
|
||||
#include "strings.h"
|
||||
#include "utils.h"
|
||||
#include "fatfs/sdmmc/sdmmc.h"
|
||||
|
||||
/****************************************************************
|
||||
@@ -81,15 +85,36 @@ __asm__\
|
||||
|
||||
static void aes_setkey(u8 keyslot, const void *key, u32 keyType, u32 mode)
|
||||
{
|
||||
if(keyslot <= 0x03) return; // Ignore TWL keys for now
|
||||
u32 *key32 = (u32 *)key;
|
||||
*REG_AESCNT = (*REG_AESCNT & ~(AES_CNT_INPUT_ENDIAN | AES_CNT_INPUT_ORDER)) | mode;
|
||||
*REG_AESKEYCNT = (*REG_AESKEYCNT >> 6 << 6) | keyslot | AES_KEYCNT_WRITE;
|
||||
|
||||
REG_AESKEYFIFO[keyType] = key32[0];
|
||||
REG_AESKEYFIFO[keyType] = key32[1];
|
||||
REG_AESKEYFIFO[keyType] = key32[2];
|
||||
REG_AESKEYFIFO[keyType] = key32[3];
|
||||
if(keyslot <= 3)
|
||||
{
|
||||
if((mode & AES_CNT_INPUT_ORDER) == AES_INPUT_REVERSED)
|
||||
{
|
||||
REGs_AESTWLKEYS[keyslot][keyType][0] = key32[3];
|
||||
REGs_AESTWLKEYS[keyslot][keyType][1] = key32[2];
|
||||
REGs_AESTWLKEYS[keyslot][keyType][2] = key32[1];
|
||||
REGs_AESTWLKEYS[keyslot][keyType][3] = key32[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
REGs_AESTWLKEYS[keyslot][keyType][0] = key32[0];
|
||||
REGs_AESTWLKEYS[keyslot][keyType][1] = key32[1];
|
||||
REGs_AESTWLKEYS[keyslot][keyType][2] = key32[2];
|
||||
REGs_AESTWLKEYS[keyslot][keyType][3] = key32[3];
|
||||
}
|
||||
}
|
||||
|
||||
else if(keyslot < 0x40)
|
||||
{
|
||||
*REG_AESKEYCNT = (*REG_AESKEYCNT >> 6 << 6) | keyslot | AES_KEYCNT_WRITE;
|
||||
|
||||
REG_AESKEYFIFO[keyType] = key32[0];
|
||||
REG_AESKEYFIFO[keyType] = key32[1];
|
||||
REG_AESKEYFIFO[keyType] = key32[2];
|
||||
REG_AESKEYFIFO[keyType] = key32[3];
|
||||
}
|
||||
}
|
||||
|
||||
static void aes_use_keyslot(u8 keyslot)
|
||||
@@ -106,7 +131,7 @@ static void aes_setiv(const void *iv, u32 mode)
|
||||
const u32 *iv32 = (const u32 *)iv;
|
||||
*REG_AESCNT = (*REG_AESCNT & ~(AES_CNT_INPUT_ENDIAN | AES_CNT_INPUT_ORDER)) | mode;
|
||||
|
||||
// Word order for IV can't be changed in REG_AESCNT and always default to reversed
|
||||
//Word order for IV can't be changed in REG_AESCNT and always default to reversed
|
||||
if(mode & AES_INPUT_NORMAL)
|
||||
{
|
||||
REG_AESCTR[0] = iv32[3];
|
||||
@@ -126,14 +151,14 @@ static void aes_setiv(const void *iv, u32 mode)
|
||||
static void aes_advctr(void *ctr, u32 val, u32 mode)
|
||||
{
|
||||
u32 *ctr32 = (u32 *)ctr;
|
||||
|
||||
|
||||
int i;
|
||||
if(mode & AES_INPUT_BE)
|
||||
{
|
||||
for(i = 0; i < 4; ++i) // Endian swap
|
||||
for(i = 0; i < 4; ++i) //Endian swap
|
||||
BSWAP32(ctr32[i]);
|
||||
}
|
||||
|
||||
|
||||
if(mode & AES_INPUT_NORMAL)
|
||||
{
|
||||
ADD_u128_u32(ctr32[3], ctr32[2], ctr32[1], ctr32[0], val);
|
||||
@@ -142,10 +167,10 @@ static void aes_advctr(void *ctr, u32 val, u32 mode)
|
||||
{
|
||||
ADD_u128_u32(ctr32[0], ctr32[1], ctr32[2], ctr32[3], val);
|
||||
}
|
||||
|
||||
|
||||
if(mode & AES_INPUT_BE)
|
||||
{
|
||||
for(i = 0; i < 4; ++i) // Endian swap
|
||||
for(i = 0; i < 4; ++i) //Endian swap
|
||||
BSWAP32(ctr32[i]);
|
||||
}
|
||||
}
|
||||
@@ -176,16 +201,16 @@ static void aes_batch(void *dst, const void *src, u32 blockCount)
|
||||
{
|
||||
*REG_AESBLKCNT = blockCount << 16;
|
||||
*REG_AESCNT |= AES_CNT_START;
|
||||
|
||||
|
||||
const u32 *src32 = (const u32 *)src;
|
||||
u32 *dst32 = (u32 *)dst;
|
||||
|
||||
|
||||
u32 wbc = blockCount;
|
||||
u32 rbc = blockCount;
|
||||
|
||||
|
||||
while(rbc)
|
||||
{
|
||||
if(wbc && ((*REG_AESCNT & 0x1F) <= 0xC)) // There's space for at least 4 ints
|
||||
if(wbc && ((*REG_AESCNT & 0x1F) <= 0xC)) //There's space for at least 4 ints
|
||||
{
|
||||
*REG_AESWRFIFO = *src32++;
|
||||
*REG_AESWRFIFO = *src32++;
|
||||
@@ -193,8 +218,8 @@ static void aes_batch(void *dst, const void *src, u32 blockCount)
|
||||
*REG_AESWRFIFO = *src32++;
|
||||
wbc--;
|
||||
}
|
||||
|
||||
if(rbc && ((*REG_AESCNT & (0x1F << 0x5)) >= (0x4 << 0x5))) // At least 4 ints available for read
|
||||
|
||||
if(rbc && ((*REG_AESCNT & (0x1F << 0x5)) >= (0x4 << 0x5))) //At least 4 ints available for read
|
||||
{
|
||||
*dst32++ = *REG_AESRDFIFO;
|
||||
*dst32++ = *REG_AESRDFIFO;
|
||||
@@ -205,7 +230,7 @@ static void aes_batch(void *dst, const void *src, u32 blockCount)
|
||||
}
|
||||
}
|
||||
|
||||
static void aes(void *dst, const void *src, u32 blockCount, void *iv, u32 mode, u32 ivMode)
|
||||
void aes(void *dst, const void *src, u32 blockCount, void *iv, u32 mode, u32 ivMode)
|
||||
{
|
||||
*REG_AESCNT = mode |
|
||||
AES_CNT_INPUT_ORDER | AES_CNT_OUTPUT_ORDER |
|
||||
@@ -221,24 +246,24 @@ static void aes(void *dst, const void *src, u32 blockCount, void *iv, u32 mode,
|
||||
|
||||
blocks = (blockCount >= 0xFFFF) ? 0xFFFF : blockCount;
|
||||
|
||||
// Save the last block for the next decryption CBC batch's iv
|
||||
//Save the last block for the next decryption CBC batch's iv
|
||||
if((mode & AES_ALL_MODES) == AES_CBC_DECRYPT_MODE)
|
||||
{
|
||||
memcpy(iv, src + (blocks - 1) * AES_BLOCK_SIZE, AES_BLOCK_SIZE);
|
||||
aes_change_ctrmode(iv, AES_INPUT_BE | AES_INPUT_NORMAL, ivMode);
|
||||
}
|
||||
|
||||
// Process the current batch
|
||||
//Process the current batch
|
||||
aes_batch(dst, src, blocks);
|
||||
|
||||
// Save the last block for the next encryption CBC batch's iv
|
||||
//Save the last block for the next encryption CBC batch's iv
|
||||
if((mode & AES_ALL_MODES) == AES_CBC_ENCRYPT_MODE)
|
||||
{
|
||||
memcpy(iv, dst + (blocks - 1) * AES_BLOCK_SIZE, AES_BLOCK_SIZE);
|
||||
aes_change_ctrmode(iv, AES_INPUT_BE | AES_INPUT_NORMAL, ivMode);
|
||||
}
|
||||
|
||||
// Advance counter for CTR mode
|
||||
|
||||
//Advance counter for CTR mode
|
||||
else if((mode & AES_ALL_MODES) == AES_CTR_MODE)
|
||||
aes_advctr(iv, blocks, ivMode);
|
||||
|
||||
@@ -253,11 +278,11 @@ static void sha_wait_idle()
|
||||
while(*REG_SHA_CNT & 1);
|
||||
}
|
||||
|
||||
static void sha(void *res, const void *src, u32 size, u32 mode)
|
||||
void sha(void *res, const void *src, u32 size, u32 mode)
|
||||
{
|
||||
sha_wait_idle();
|
||||
*REG_SHA_CNT = mode | SHA_CNT_OUTPUT_ENDIAN | SHA_NORMAL_ROUND;
|
||||
|
||||
|
||||
const u32 *src32 = (const u32 *)src;
|
||||
int i;
|
||||
while(size >= 0x40)
|
||||
@@ -273,15 +298,15 @@ static void sha(void *res, const void *src, u32 size, u32 mode)
|
||||
|
||||
size -= 0x40;
|
||||
}
|
||||
|
||||
|
||||
sha_wait_idle();
|
||||
memcpy((void *)REG_SHA_INFIFO, src32, size);
|
||||
|
||||
|
||||
*REG_SHA_CNT = (*REG_SHA_CNT & ~SHA_NORMAL_ROUND) | SHA_FINAL_ROUND;
|
||||
|
||||
|
||||
while(*REG_SHA_CNT & SHA_FINAL_ROUND);
|
||||
sha_wait_idle();
|
||||
|
||||
|
||||
u32 hashSize = SHA_256_HASH_SIZE;
|
||||
if(mode == SHA_224_MODE)
|
||||
hashSize = SHA_224_HASH_SIZE;
|
||||
@@ -291,28 +316,26 @@ static void sha(void *res, const void *src, u32 size, u32 mode)
|
||||
memcpy(res, (void *)REG_SHA_HASH, hashSize);
|
||||
}
|
||||
|
||||
/****************************************************************
|
||||
* NAND/FIRM crypto
|
||||
****************************************************************/
|
||||
/*****************************************************************/
|
||||
|
||||
static u8 __attribute__((aligned(4))) nandCTR[0x10];
|
||||
__attribute__((aligned(4))) static u8 nandCtr[AES_BLOCK_SIZE];
|
||||
static u8 nandSlot;
|
||||
|
||||
static u32 fatStart;
|
||||
|
||||
//Initialize the CTRNAND crypto
|
||||
FirmwareSource firmSource;
|
||||
|
||||
void ctrNandInit(void)
|
||||
{
|
||||
u8 __attribute__((aligned(4))) cid[0x10];
|
||||
u8 __attribute__((aligned(4))) shaSum[0x20];
|
||||
__attribute__((aligned(4))) u8 cid[AES_BLOCK_SIZE],
|
||||
shaSum[SHA_256_HASH_SIZE];
|
||||
|
||||
sdmmc_get_cid(1, (u32 *)cid);
|
||||
sha(shaSum, cid, 0x10, SHA_256_MODE);
|
||||
memcpy(nandCTR, shaSum, 0x10);
|
||||
sha(shaSum, cid, sizeof(cid), SHA_256_MODE);
|
||||
memcpy(nandCtr, shaSum, sizeof(nandCtr));
|
||||
|
||||
if(isN3DS)
|
||||
if(ISN3DS)
|
||||
{
|
||||
u8 __attribute__((aligned(4))) keyY0x5[0x10] = {0x4D, 0x80, 0x4F, 0x4E, 0x99, 0x90, 0x19, 0x46, 0x13, 0xA2, 0x04, 0xAC, 0x58, 0x44, 0x60, 0xBE};
|
||||
__attribute__((aligned(4))) u8 keyY0x5[AES_BLOCK_SIZE] = {0x4D, 0x80, 0x4F, 0x4E, 0x99, 0x90, 0x19, 0x46, 0x13, 0xA2, 0x04, 0xAC, 0x58, 0x44, 0x60, 0xBE};
|
||||
aes_setkey(0x05, keyY0x5, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
|
||||
nandSlot = 0x05;
|
||||
@@ -325,15 +348,14 @@ void ctrNandInit(void)
|
||||
}
|
||||
}
|
||||
|
||||
//Read and decrypt from the selected CTRNAND
|
||||
u32 ctrNandRead(u32 sector, u32 sectorCount, u8 *outbuf)
|
||||
int ctrNandRead(u32 sector, u32 sectorCount, u8 *outbuf)
|
||||
{
|
||||
u8 __attribute__((aligned(4))) tmpCTR[0x10];
|
||||
memcpy(tmpCTR, nandCTR, 0x10);
|
||||
aes_advctr(tmpCTR, ((sector + fatStart) * 0x200) / AES_BLOCK_SIZE, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
__attribute__((aligned(4))) u8 tmpCtr[sizeof(nandCtr)];
|
||||
memcpy(tmpCtr, nandCtr, sizeof(nandCtr));
|
||||
aes_advctr(tmpCtr, ((sector + fatStart) * 0x200) / AES_BLOCK_SIZE, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
|
||||
//Read
|
||||
u32 result;
|
||||
int result;
|
||||
if(firmSource == FIRMWARE_SYSNAND)
|
||||
result = sdmmc_nand_readsectors(sector + fatStart, sectorCount, outbuf);
|
||||
else
|
||||
@@ -344,126 +366,143 @@ u32 ctrNandRead(u32 sector, u32 sectorCount, u8 *outbuf)
|
||||
|
||||
//Decrypt
|
||||
aes_use_keyslot(nandSlot);
|
||||
aes(outbuf, outbuf, sectorCount * 0x200 / AES_BLOCK_SIZE, tmpCTR, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
aes(outbuf, outbuf, sectorCount * 0x200 / AES_BLOCK_SIZE, tmpCtr, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//Sets the 7.x NCCH KeyX and the 6.x gamecard save data KeyY
|
||||
void setRSAMod0DerivedKeys(void)
|
||||
int ctrNandWrite(u32 sector, u32 sectorCount, const u8 *inbuf)
|
||||
{
|
||||
if(!isDevUnit)
|
||||
u8 *buffer = (u8 *)0x23000000;
|
||||
u32 bufferSize = 0xF00000;
|
||||
|
||||
__attribute__((aligned(4))) u8 tmpCtr[sizeof(nandCtr)];
|
||||
memcpy(tmpCtr, nandCtr, sizeof(nandCtr));
|
||||
aes_advctr(tmpCtr, ((sector + fatStart) * 0x200) / AES_BLOCK_SIZE, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
aes_use_keyslot(nandSlot);
|
||||
|
||||
int result = 0;
|
||||
for(u32 tempSector = 0; tempSector < sectorCount && !result; tempSector += bufferSize / 0x200)
|
||||
{
|
||||
const u8 __attribute__((aligned(4))) keyX0x25[0x10] = {0xCE, 0xE7, 0xD8, 0xAB, 0x30, 0xC0, 0x0D, 0xAE, 0x85, 0x0E, 0xF5, 0xE3, 0x82, 0xAC, 0x5A, 0xF3};
|
||||
const u8 __attribute__((aligned(4))) keyY0x2F[0x10] = {0xC3, 0x69, 0xBA, 0xA2, 0x1E, 0x18, 0x8A, 0x88, 0xA9, 0xAA, 0x94, 0xE5, 0x50, 0x6A, 0x9F, 0x16};
|
||||
u32 tempCount = (bufferSize / 0x200) < (sectorCount - tempSector) ? (bufferSize / 0x200) : (sectorCount - tempSector);
|
||||
|
||||
aes_setkey(0x25, keyX0x25, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
aes_setkey(0x2F, keyY0x2F, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
memcpy(buffer, inbuf + (tempSector * 0x200), tempCount * 0x200);
|
||||
|
||||
/* [3dbrew] The first 0x10-bytes are checked by the v6.0/v7.0 NATIVE_FIRM keyinit function,
|
||||
when non-zero it clears this block and continues to do the key generation.
|
||||
Otherwise when this block was already all-zero, it immediately returns. */
|
||||
memset32((void *)0x01FFCD00, 0, 0x10);
|
||||
//Encrypt
|
||||
aes(buffer, buffer, tempCount * 0x200 / AES_BLOCK_SIZE, tmpCtr, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
|
||||
//Write
|
||||
result = sdmmc_nand_writesectors(tempSector + sector + fatStart, tempCount, buffer);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//Decrypt a FIRM ExeFS
|
||||
void decryptExeFs(u8 *inbuf)
|
||||
bool decryptExeFs(Cxi *cxi)
|
||||
{
|
||||
u8 *exeFsOffset = inbuf + *(u32 *)(inbuf + 0x1A0) * 0x200;
|
||||
u32 exeFsSize = *(u32 *)(inbuf + 0x1A4) * 0x200;
|
||||
u8 ncchCTR[0x10] = {0};
|
||||
if(memcmp(cxi->ncch.magic, "NCCH", 4) != 0) return false;
|
||||
|
||||
u8 *exeFsOffset = (u8 *)cxi + (cxi->ncch.exeFsOffset + 1) * 0x200;
|
||||
u32 exeFsSize = (cxi->ncch.exeFsSize - 1) * 0x200;
|
||||
__attribute__((aligned(4))) u8 ncchCtr[AES_BLOCK_SIZE] = {0};
|
||||
|
||||
for(u32 i = 0; i < 8; i++)
|
||||
ncchCTR[7 - i] = *(inbuf + 0x108 + i);
|
||||
ncchCTR[8] = 2;
|
||||
ncchCtr[7 - i] = cxi->ncch.partitionId[i];
|
||||
ncchCtr[8] = 2;
|
||||
|
||||
aes_setkey(0x2C, inbuf, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
aes_setiv(ncchCTR, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
aes_setkey(0x2C, cxi, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
aes_advctr(ncchCtr, 0x200 / AES_BLOCK_SIZE, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
aes_use_keyslot(0x2C);
|
||||
aes(inbuf - 0x200, exeFsOffset, exeFsSize / AES_BLOCK_SIZE, ncchCTR, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
aes(cxi, exeFsOffset, exeFsSize / AES_BLOCK_SIZE, ncchCtr, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
|
||||
return memcmp(cxi, "FIRM", 4) == 0;
|
||||
}
|
||||
|
||||
//ARM9Loader replacement
|
||||
void arm9Loader(u8 *arm9Section)
|
||||
bool decryptNusFirm(const Ticket *ticket, Cxi *cxi, u32 ncchSize)
|
||||
{
|
||||
//Determine the arm9loader version
|
||||
u32 a9lVersion;
|
||||
switch(arm9Section[0x53])
|
||||
if(memcmp(ticket->sigIssuer, "Root", 4) != 0) return false;
|
||||
|
||||
__attribute__((aligned(4))) const u8 keyY0x3D[AES_BLOCK_SIZE] = {0x0C, 0x76, 0x72, 0x30, 0xF0, 0x99, 0x8F, 0x1C, 0x46, 0x82, 0x82, 0x02, 0xFA, 0xAC, 0xBE, 0x4C};
|
||||
__attribute__((aligned(4))) u8 titleKey[AES_BLOCK_SIZE],
|
||||
cetkIv[AES_BLOCK_SIZE] = {0};
|
||||
memcpy(titleKey, ticket->titleKey, sizeof(titleKey));
|
||||
memcpy(cetkIv, ticket->titleId, sizeof(ticket->titleId));
|
||||
|
||||
aes_setkey(0x3D, keyY0x3D, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
aes_use_keyslot(0x3D);
|
||||
aes(titleKey, titleKey, 1, cetkIv, AES_CBC_DECRYPT_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
|
||||
__attribute__((aligned(4))) u8 ncchIv[AES_BLOCK_SIZE] = {0};
|
||||
|
||||
aes_setkey(0x16, titleKey, AES_KEYNORMAL, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
aes_use_keyslot(0x16);
|
||||
aes(cxi, cxi, ncchSize / AES_BLOCK_SIZE, ncchIv, AES_CBC_DECRYPT_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
|
||||
return decryptExeFs(cxi);
|
||||
}
|
||||
|
||||
void kernel9Loader(Arm9Bin *arm9Section)
|
||||
{
|
||||
//Determine the kernel9loader version
|
||||
u32 k9lVersion;
|
||||
switch(arm9Section->magic[3])
|
||||
{
|
||||
case 0xFF:
|
||||
a9lVersion = 0;
|
||||
k9lVersion = 0;
|
||||
break;
|
||||
case '1':
|
||||
a9lVersion = 1;
|
||||
k9lVersion = 1;
|
||||
break;
|
||||
default:
|
||||
a9lVersion = 2;
|
||||
break;
|
||||
k9lVersion = 2;
|
||||
}
|
||||
|
||||
//Firm keys
|
||||
u8 __attribute__((aligned(4))) keyY[0x10];
|
||||
u8 __attribute__((aligned(4))) arm9BinCTR[0x10];
|
||||
u8 arm9BinSlot = a9lVersion != 0 ? 0x16 : 0x15;
|
||||
u32 *startOfArm9Bin = (u32 *)((u8 *)arm9Section + 0x800);
|
||||
if(*startOfArm9Bin == 0x47704770 || *startOfArm9Bin == 0xB0862000) return; //Already decrypted
|
||||
|
||||
//Setup keys needed for arm9bin decryption
|
||||
memcpy(keyY, arm9Section + 0x10, 0x10);
|
||||
memcpy(arm9BinCTR, arm9Section + 0x20, 0x10);
|
||||
//Set 0x11 keyslot
|
||||
__attribute__((aligned(4))) const u8 key1s[2][AES_BLOCK_SIZE] = {
|
||||
{0x07, 0x29, 0x44, 0x38, 0xF8, 0xC9, 0x75, 0x93, 0xAA, 0x0E, 0x4A, 0xB4, 0xAE, 0x84, 0xC1, 0xD8},
|
||||
{0xA2, 0xF4, 0x00, 0x3C, 0x7A, 0x95, 0x10, 0x25, 0xDF, 0x4E, 0x9E, 0x74, 0xE3, 0x0C, 0x92, 0x99}
|
||||
},
|
||||
key2s[2][AES_BLOCK_SIZE] = {
|
||||
{0x42, 0x3F, 0x81, 0x7A, 0x23, 0x52, 0x58, 0x31, 0x6E, 0x75, 0x8E, 0x3A, 0x39, 0x43, 0x2E, 0xD0},
|
||||
{0xFF, 0x77, 0xA0, 0x9A, 0x99, 0x81, 0xE9, 0x48, 0xEC, 0x51, 0xC9, 0x32, 0x5D, 0x14, 0xEC, 0x25}
|
||||
};
|
||||
|
||||
//Calculate the size of the ARM9 binary
|
||||
u32 arm9BinSize = 0;
|
||||
//http://stackoverflow.com/questions/12791077/atoi-implementation-in-c
|
||||
for(u8 *tmp = arm9Section + 0x30; *tmp != 0; tmp++)
|
||||
arm9BinSize = (arm9BinSize << 3) + (arm9BinSize << 1) + *tmp - '0';
|
||||
aes_setkey(0x11, k9lVersion == 2 ? key2s[ISDEVUNIT ? 1 : 0] : key1s[ISDEVUNIT ? 1 : 0], AES_KEYNORMAL, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
|
||||
if(a9lVersion)
|
||||
{
|
||||
u8 __attribute__((aligned(4))) keyX[0x10];
|
||||
u8 arm9BinSlot = k9lVersion == 0 ? 0x15 : 0x16;
|
||||
|
||||
if(!isDevUnit)
|
||||
{
|
||||
const u8 __attribute__((aligned(4))) key1[0x10] = {0x07, 0x29, 0x44, 0x38, 0xF8, 0xC9, 0x75, 0x93, 0xAA, 0x0E, 0x4A, 0xB4, 0xAE, 0x84, 0xC1, 0xD8};
|
||||
const u8 __attribute__((aligned(4))) key2[0x10] = {0x42, 0x3F, 0x81, 0x7A, 0x23, 0x52, 0x58, 0x31, 0x6E, 0x75, 0x8E, 0x3A, 0x39, 0x43, 0x2E, 0xD0};
|
||||
|
||||
aes_setkey(0x11, a9lVersion == 2 ? key2 : key1, AES_KEYNORMAL, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
}
|
||||
|
||||
aes_use_keyslot(0x11);
|
||||
aes(keyX, arm9Section + 0x60, 1, NULL, AES_ECB_DECRYPT_MODE, 0);
|
||||
aes_setkey(arm9BinSlot, keyX, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
}
|
||||
//Set keyX
|
||||
__attribute__((aligned(4))) u8 keyX[AES_BLOCK_SIZE];
|
||||
aes_use_keyslot(0x11);
|
||||
aes(keyX, k9lVersion == 0 ? arm9Section->keyX : arm9Section->slot0x16keyX, 1, NULL, AES_ECB_DECRYPT_MODE, 0);
|
||||
aes_setkey(arm9BinSlot, keyX, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
|
||||
//Set keyY
|
||||
__attribute__((aligned(4))) u8 keyY[AES_BLOCK_SIZE];
|
||||
memcpy(keyY, arm9Section->keyY, sizeof(keyY));
|
||||
aes_setkey(arm9BinSlot, keyY, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
aes_setiv(arm9BinCTR, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
|
||||
//Set CTR
|
||||
__attribute__((aligned(4))) u8 arm9BinCtr[AES_BLOCK_SIZE];
|
||||
memcpy(arm9BinCtr, arm9Section->ctr, sizeof(arm9BinCtr));
|
||||
|
||||
//Decrypt ARM9 binary
|
||||
aes_use_keyslot(arm9BinSlot);
|
||||
aes(startOfArm9Bin, startOfArm9Bin, decAtoi(arm9Section->size, sizeof(arm9Section->size)) / AES_BLOCK_SIZE, arm9BinCtr, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
|
||||
//Decrypt arm9bin
|
||||
aes(arm9Section + 0x800, arm9Section + 0x800, arm9BinSize / AES_BLOCK_SIZE, arm9BinCTR, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
|
||||
//Set >=9.6 KeyXs
|
||||
if(a9lVersion == 2 && !isDevUnit)
|
||||
{
|
||||
u8 __attribute__((aligned(4))) keyData[0x10] = {0xDD, 0xDA, 0xA4, 0xC6, 0x2C, 0xC4, 0x50, 0xE9, 0xDA, 0xB6, 0x9B, 0x0D, 0x9D, 0x2A, 0x21, 0x98};
|
||||
u8 __attribute__((aligned(4))) decKey[0x10];
|
||||
|
||||
//Set keys 0x19..0x1F keyXs
|
||||
aes_use_keyslot(0x11);
|
||||
for(u8 slot = 0x19; slot < 0x20; slot++, keyData[0xF]++)
|
||||
{
|
||||
aes(decKey, keyData, 1, NULL, AES_ECB_DECRYPT_MODE, 0);
|
||||
aes_setkey(slot, decKey, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
}
|
||||
}
|
||||
if(*startOfArm9Bin != 0x47704770 && *startOfArm9Bin != 0xB0862000) error("Failed to decrypt the ARM9 binary.");
|
||||
}
|
||||
|
||||
void computePinHash(u8 *out, u8 *in)
|
||||
void computePinHash(u8 *outbuf, const u8 *inbuf)
|
||||
{
|
||||
u8 __attribute__((aligned(4))) cid[0x10];
|
||||
u8 __attribute__((aligned(4))) cipherText[0x10];
|
||||
__attribute__((aligned(4))) u8 cid[AES_BLOCK_SIZE],
|
||||
cipherText[AES_BLOCK_SIZE];
|
||||
|
||||
sdmmc_get_cid(1, (u32 *)cid);
|
||||
aes_use_keyslot(4); //Console-unique keyslot whose keys are set by the ARM9 bootROM
|
||||
aes(cipherText, in, 1, cid, AES_CBC_ENCRYPT_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
sha(out, cipherText, 0x10, SHA_256_MODE);
|
||||
}
|
||||
aes_use_keyslot(0x04); //Console-unique keyslot whose keys are set by the ARM9 bootROM
|
||||
aes(cipherText, inbuf, 1, cid, AES_CBC_ENCRYPT_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
|
||||
sha(outbuf, cipherText, sizeof(cipherText), SHA_256_MODE);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,9 @@
|
||||
|
||||
/*
|
||||
* Crypto libs from http://github.com/b1l1s/ctr
|
||||
* ARM9Loader code originally adapted from https://github.com/Reisyukaku/ReiNand/blob/228c378255ba693133dec6f3368e14d386f2cde7/source/crypto.c#L233
|
||||
* kernel9Loader code originally adapted from https://github.com/Reisyukaku/ReiNand/blob/228c378255ba693133dec6f3368e14d386f2cde7/source/crypto.c#L233
|
||||
* decryptNusFirm code adapted from https://github.com/mid-kid/CakesForeveryWan/blob/master/source/firm.c
|
||||
* ctrNandWrite logic adapted from https://github.com/d0k3/GodMode9/blob/master/source/nand/nand.c
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
@@ -42,6 +44,8 @@
|
||||
#define REG_AESKEYXFIFO ((vu32 *)0x10009104)
|
||||
#define REG_AESKEYYFIFO ((vu32 *)0x10009108)
|
||||
|
||||
#define REGs_AESTWLKEYS (*((vu32 (*)[4][3][4])0x10009040))
|
||||
|
||||
#define AES_CCM_DECRYPT_MODE (0u << 27)
|
||||
#define AES_CCM_ENCRYPT_MODE (1u << 27)
|
||||
#define AES_CTR_MODE (2u << 27)
|
||||
@@ -101,12 +105,15 @@
|
||||
#define SHA_1_HASH_SIZE (160 / 8)
|
||||
|
||||
extern u32 emuOffset;
|
||||
extern bool isN3DS, isDevUnit;
|
||||
extern FirmwareSource firmSource;
|
||||
|
||||
void aes(void *dst, const void *src, u32 blockCount, void *iv, u32 mode, u32 ivMode);
|
||||
void sha(void *res, const void *src, u32 size, u32 mode);
|
||||
|
||||
void ctrNandInit(void);
|
||||
u32 ctrNandRead(u32 sector, u32 sectorCount, u8 *outbuf);
|
||||
void setRSAMod0DerivedKeys(void);
|
||||
void decryptExeFs(u8 *inbuf);
|
||||
void arm9Loader(u8 *arm9Section);
|
||||
void computePinHash(u8 *out, u8 *in);
|
||||
int ctrNandRead(u32 sector, u32 sectorCount, u8 *outbuf);
|
||||
int ctrNandWrite(u32 sector, u32 sectorCount, const u8 *inbuf);
|
||||
bool decryptExeFs(Cxi *cxi);
|
||||
bool decryptNusFirm(const Ticket *ticket, Cxi *cxi, u32 ncchSize);
|
||||
void kernel9Loader(Arm9Bin *arm9Section);
|
||||
void computePinHash(u8 *outbuf, const u8 *inbuf);
|
||||
|
||||
@@ -30,33 +30,37 @@
|
||||
#include "screen.h"
|
||||
#include "utils.h"
|
||||
#include "fs.h"
|
||||
#include "fmt.h"
|
||||
#include "font.h"
|
||||
|
||||
bool loadSplash(void)
|
||||
{
|
||||
const char topSplashPath[] = "/luma/splash.bin",
|
||||
bottomSplashPath[] = "/luma/splashbottom.bin";
|
||||
const char *topSplashFile = "splash.bin",
|
||||
*bottomSplashFile = "splashbottom.bin";
|
||||
|
||||
bool isTopSplashValid = getFileSize(topSplashPath) == SCREEN_TOP_FBSIZE,
|
||||
isBottomSplashValid = getFileSize(bottomSplashPath) == SCREEN_BOTTOM_FBSIZE;
|
||||
bool isTopSplashValid = getFileSize(topSplashFile) == SCREEN_TOP_FBSIZE,
|
||||
isBottomSplashValid = getFileSize(bottomSplashFile) == SCREEN_BOTTOM_FBSIZE;
|
||||
|
||||
//Don't delay boot nor init the screens if no splash images or invalid splash images are on the SD
|
||||
if(!isTopSplashValid && !isBottomSplashValid)
|
||||
return false;
|
||||
if(!isTopSplashValid && !isBottomSplashValid) return false;
|
||||
|
||||
initScreens();
|
||||
clearScreens(true);
|
||||
|
||||
if(isTopSplashValid) fileRead(fb->top_left, topSplashPath, 0);
|
||||
if(isBottomSplashValid) fileRead(fb->bottom, bottomSplashPath, 0);
|
||||
if(isTopSplashValid) isTopSplashValid = fileRead(fbs[1].top_left, topSplashFile, SCREEN_TOP_FBSIZE) == SCREEN_TOP_FBSIZE;
|
||||
if(isBottomSplashValid) isBottomSplashValid = fileRead(fbs[1].bottom, bottomSplashFile, SCREEN_BOTTOM_FBSIZE) == SCREEN_BOTTOM_FBSIZE;
|
||||
|
||||
chrono(3);
|
||||
if(!isTopSplashValid && !isBottomSplashValid) return false;
|
||||
|
||||
swapFramebuffers(true);
|
||||
wait(3000ULL);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void drawCharacter(char character, bool isTopScreen, u32 posX, u32 posY, u32 color)
|
||||
void drawCharacter(bool isTopScreen, u32 posX, u32 posY, u32 color, char character)
|
||||
{
|
||||
u8 *select = isTopScreen ? fb->top_left : fb->bottom;
|
||||
u8 *select = isTopScreen ? fbs[0].top_left : fbs[0].bottom;
|
||||
|
||||
for(u32 y = 0; y < 8; y++)
|
||||
{
|
||||
@@ -74,26 +78,45 @@ void drawCharacter(char character, bool isTopScreen, u32 posX, u32 posY, u32 col
|
||||
}
|
||||
}
|
||||
|
||||
u32 drawString(const char *string, bool isTopScreen, u32 posX, u32 posY, u32 color)
|
||||
u32 drawString(bool isTopScreen, u32 posX, u32 posY, u32 color, const char *string)
|
||||
{
|
||||
for(u32 i = 0, line_i = 0; i < strlen(string); i++, line_i++)
|
||||
{
|
||||
if(string[i] == '\n')
|
||||
for(u32 i = 0, line_i = 0; i < strlen(string); i++)
|
||||
switch(string[i])
|
||||
{
|
||||
posY += SPACING_Y;
|
||||
line_i = 0;
|
||||
i++;
|
||||
}
|
||||
else if(line_i >= ((isTopScreen ? SCREEN_TOP_WIDTH : SCREEN_BOTTOM_WIDTH) - posX) / SPACING_X)
|
||||
{
|
||||
//Make sure we never get out of the screen
|
||||
posY += SPACING_Y;
|
||||
line_i = 1; //Little offset so we know the same string continues
|
||||
if(string[i] == ' ') i++; //Spaces at the start look weird
|
||||
}
|
||||
case '\n':
|
||||
posY += SPACING_Y;
|
||||
line_i = 0;
|
||||
break;
|
||||
|
||||
drawCharacter(string[i], isTopScreen, posX + line_i * SPACING_X, posY, color);
|
||||
}
|
||||
case '\t':
|
||||
line_i += 2;
|
||||
break;
|
||||
|
||||
default:
|
||||
//Make sure we never get out of the screen
|
||||
if(line_i >= ((isTopScreen ? SCREEN_TOP_WIDTH : SCREEN_BOTTOM_WIDTH) - posX) / SPACING_X)
|
||||
{
|
||||
posY += SPACING_Y;
|
||||
line_i = 1; //Little offset so we know the same string continues
|
||||
if(string[i] == ' ') break; //Spaces at the start look weird
|
||||
}
|
||||
|
||||
drawCharacter(isTopScreen, posX + line_i * SPACING_X, posY, color, string[i]);
|
||||
|
||||
line_i++;
|
||||
break;
|
||||
}
|
||||
|
||||
return posY;
|
||||
}
|
||||
}
|
||||
|
||||
u32 drawFormattedString(bool isTopScreen, u32 posX, u32 posY, u32 color, const char *fmt, ...)
|
||||
{
|
||||
char buf[DRAW_MAX_FORMATTED_STRING_SIZE + 1];
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vsprintf(buf, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
return drawString(isTopScreen, posX, posY, color, buf);
|
||||
}
|
||||
|
||||
@@ -29,12 +29,6 @@
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#define SCREEN_TOP_WIDTH 400
|
||||
#define SCREEN_BOTTOM_WIDTH 320
|
||||
#define SCREEN_HEIGHT 240
|
||||
#define SCREEN_TOP_FBSIZE (3 * SCREEN_TOP_WIDTH * SCREEN_HEIGHT)
|
||||
#define SCREEN_BOTTOM_FBSIZE (3 * SCREEN_BOTTOM_WIDTH * SCREEN_HEIGHT)
|
||||
|
||||
#define SPACING_Y 10
|
||||
#define SPACING_X 8
|
||||
|
||||
@@ -44,6 +38,9 @@
|
||||
#define COLOR_BLACK 0x000000
|
||||
#define COLOR_YELLOW 0x00FFFF
|
||||
|
||||
#define DRAW_MAX_FORMATTED_STRING_SIZE 512
|
||||
|
||||
bool loadSplash(void);
|
||||
void drawCharacter(char character, bool isTopScreen, u32 posX, u32 posY, u32 color);
|
||||
u32 drawString(const char *string, bool isTopScreen, u32 posX, u32 posY, u32 color);
|
||||
void drawCharacter(bool isTopScreen, u32 posX, u32 posY, u32 color, char character);
|
||||
u32 drawString(bool isTopScreen, u32 posX, u32 posY, u32 color, const char *string);
|
||||
u32 drawFormattedString(bool isTopScreen, u32 posX, u32 posY, u32 color, const char *fmt, ...);
|
||||
|
||||
155
source/emunand.c
155
source/emunand.c
@@ -20,19 +20,34 @@
|
||||
* Notices displayed by works containing it.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Code for locating the SDMMC struct by Normmatt
|
||||
*/
|
||||
|
||||
#include "emunand.h"
|
||||
#include "memory.h"
|
||||
#include "fatfs/sdmmc/sdmmc.h"
|
||||
#include "../build/emunandpatch.h"
|
||||
#include "../build/bundled.h"
|
||||
|
||||
u32 emuOffset;
|
||||
|
||||
void locateEmuNand(u32 *emuHeader, FirmwareSource *nandType)
|
||||
{
|
||||
static u8 temp[0x200];
|
||||
const u32 nandSize = getMMCDevice(0)->total_size;
|
||||
bool found = false;
|
||||
static u8 __attribute__((aligned(4))) temp[0x200];
|
||||
static u32 nandSize = 0,
|
||||
fatStart;
|
||||
|
||||
for(u32 i = 0; i < 3 && !found; i++)
|
||||
if(!nandSize)
|
||||
{
|
||||
nandSize = getMMCDevice(0)->total_size;
|
||||
sdmmc_sdcard_readsectors(0, 1, temp);
|
||||
fatStart = *(u32 *)(temp + 0x1C6); //First sector of the FAT partition
|
||||
}
|
||||
|
||||
for(u32 i = 0; i < 3; i++)
|
||||
{
|
||||
static const u32 roundedMinsizes[] = {0x1D8000, 0x26E000};
|
||||
|
||||
u32 nandOffset;
|
||||
switch(i)
|
||||
{
|
||||
@@ -40,113 +55,141 @@ void locateEmuNand(u32 *emuHeader, FirmwareSource *nandType)
|
||||
nandOffset = ROUND_TO_4MB(nandSize + 1); //"Default" layout
|
||||
break;
|
||||
case 2:
|
||||
nandOffset = isN3DS ? 0x26E000 : 0x1D8000; //"Minsize" layout
|
||||
nandOffset = roundedMinsizes[ISN3DS ? 1 : 0]; //"Minsize" layout
|
||||
break;
|
||||
default:
|
||||
case 0:
|
||||
nandOffset = *nandType == FIRMWARE_EMUNAND ? 0 : (nandSize > 0x200000 ? 0x400000 : 0x200000); //"Legacy" layout
|
||||
break;
|
||||
}
|
||||
|
||||
if(*nandType != FIRMWARE_EMUNAND) nandOffset *= ((u32)*nandType - 1);
|
||||
|
||||
//Check for RedNAND
|
||||
if(!sdmmc_sdcard_readsectors(nandOffset + 1, 1, temp) && *(u32 *)(temp + 0x100) == NCSD_MAGIC)
|
||||
if(fatStart >= nandOffset + roundedMinsizes[ISN3DS ? 1 : 0])
|
||||
{
|
||||
emuOffset = nandOffset + 1;
|
||||
*emuHeader = nandOffset + 1;
|
||||
found = true;
|
||||
}
|
||||
//Check for RedNAND
|
||||
if(!sdmmc_sdcard_readsectors(nandOffset + 1, 1, temp) && memcmp(temp + 0x100, "NCSD", 4) == 0)
|
||||
{
|
||||
emuOffset = nandOffset + 1;
|
||||
*emuHeader = nandOffset + 1;
|
||||
return;
|
||||
}
|
||||
|
||||
//Check for Gateway EmuNAND
|
||||
else if(i != 2 && !sdmmc_sdcard_readsectors(nandOffset + nandSize, 1, temp) && *(u32 *)(temp + 0x100) == NCSD_MAGIC)
|
||||
{
|
||||
emuOffset = nandOffset;
|
||||
*emuHeader = nandOffset + nandSize;
|
||||
found = true;
|
||||
//Check for Gateway EmuNAND
|
||||
else if(i != 2 && !sdmmc_sdcard_readsectors(nandOffset + nandSize, 1, temp) && memcmp(temp + 0x100, "NCSD", 4) == 0)
|
||||
{
|
||||
emuOffset = nandOffset;
|
||||
*emuHeader = nandOffset + nandSize;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(*nandType == FIRMWARE_EMUNAND) break;
|
||||
}
|
||||
|
||||
//Fallback to the first EmuNAND if there's no second/third/fourth one, or to SysNAND if there isn't any
|
||||
if(!found)
|
||||
if(*nandType != FIRMWARE_EMUNAND)
|
||||
{
|
||||
if(*nandType != FIRMWARE_EMUNAND)
|
||||
{
|
||||
*nandType = FIRMWARE_EMUNAND;
|
||||
locateEmuNand(emuHeader, nandType);
|
||||
}
|
||||
else *nandType = FIRMWARE_SYSNAND;
|
||||
*nandType = FIRMWARE_EMUNAND;
|
||||
locateEmuNand(emuHeader, nandType);
|
||||
}
|
||||
else *nandType = FIRMWARE_SYSNAND;
|
||||
}
|
||||
|
||||
static inline u8 *getFreeK9Space(u8 *pos, u32 size)
|
||||
static inline bool getFreeK9Space(u8 *pos, u32 size, u8 **freeK9Space)
|
||||
{
|
||||
const u8 pattern[] = {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00};
|
||||
|
||||
//Looking for the last free space before Process9
|
||||
return memsearch(pos + 0x13500, pattern, size - 0x13500, sizeof(pattern)) + 0x455;
|
||||
*freeK9Space = memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(*freeK9Space == NULL) return false;
|
||||
|
||||
*freeK9Space += 0x455;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline u32 getSdmmc(u8 *pos, u32 size)
|
||||
static inline u32 getSdmmc(u8 *pos, u32 size, u32 *sdmmc)
|
||||
{
|
||||
//Look for struct code
|
||||
const u8 pattern[] = {0x21, 0x20, 0x18, 0x20};
|
||||
|
||||
const u8 *off = memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
return *(u32 *)(off + 9) + *(u32 *)(off + 0xD);
|
||||
if(off == NULL) return 1;
|
||||
|
||||
*sdmmc = *(u32 *)(off + 9) + *(u32 *)(off + 0xD);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void patchNandRw(u8 *pos, u32 size, u32 branchOffset)
|
||||
static inline u32 patchNandRw(u8 *pos, u32 size, u32 branchOffset)
|
||||
{
|
||||
const u16 nandRedir[2] = {0x4C00, 0x47A0};
|
||||
|
||||
//Look for read/write code
|
||||
const u8 pattern[] = {0x1E, 0x00, 0xC8, 0x05};
|
||||
|
||||
u16 *readOffset = (u16 *)memsearch(pos, pattern, size, sizeof(pattern)) - 3,
|
||||
*writeOffset = (u16 *)memsearch((u8 *)(readOffset + 5), pattern, 0x100, sizeof(pattern)) - 3;
|
||||
u16 *readOffset = (u16 *)memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
*readOffset = nandRedir[0];
|
||||
readOffset[1] = nandRedir[1];
|
||||
((u32 *)readOffset)[1] = branchOffset;
|
||||
*writeOffset = nandRedir[0];
|
||||
writeOffset[1] = nandRedir[1];
|
||||
((u32 *)writeOffset)[1] = branchOffset;
|
||||
if(readOffset == NULL) return 1;
|
||||
|
||||
readOffset -= 3;
|
||||
|
||||
u16 *writeOffset = (u16 *)memsearch((u8 *)(readOffset + 5), pattern, 0x100, sizeof(pattern));
|
||||
|
||||
if(writeOffset == NULL) return 1;
|
||||
|
||||
writeOffset -= 3;
|
||||
*readOffset = *writeOffset = 0x4C00;
|
||||
readOffset[1] = writeOffset[1] = 0x47A0;
|
||||
((u32 *)writeOffset)[1] = ((u32 *)readOffset)[1] = branchOffset;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void patchMpu(u8 *pos, u32 size)
|
||||
static inline u32 patchMpu(u8 *pos, u32 size)
|
||||
{
|
||||
//Look for MPU pattern
|
||||
const u8 pattern[] = {0x03, 0x00, 0x24, 0x00};
|
||||
|
||||
u32 *off = (u32 *)memsearch(pos, pattern, size, sizeof(pattern));
|
||||
u16 *off = (u16 *)memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
off[0] = 0x00360003;
|
||||
off[6] = 0x00200603;
|
||||
off[9] = 0x001C0603;
|
||||
if(off == NULL) return 1;
|
||||
|
||||
off[1] = 0x0036;
|
||||
off[0xC] = off[0x12] = 0x0603;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void patchEmuNand(u8 *arm9Section, u32 arm9SectionSize, u8 *process9Offset, u32 process9Size, u32 emuHeader, u32 branchAdditive)
|
||||
u32 patchEmuNand(u8 *arm9Section, u32 kernel9Size, u8 *process9Offset, u32 process9Size, u32 emuHeader, u8 *kernel9Address)
|
||||
{
|
||||
u8 *freeK9Space;
|
||||
|
||||
if(!getFreeK9Space(arm9Section, kernel9Size, &freeK9Space)) return 1;
|
||||
|
||||
u32 ret = 0;
|
||||
|
||||
//Copy EmuNAND code
|
||||
u8 *freeK9Space = getFreeK9Space(arm9Section, arm9SectionSize);
|
||||
memcpy(freeK9Space, emunand, emunand_size);
|
||||
memcpy(freeK9Space, emunand_bin, emunand_bin_size);
|
||||
|
||||
//Add the data of the found EmuNAND
|
||||
u32 *posOffset = (u32 *)memsearch(freeK9Space, "NAND", emunand_size, 4),
|
||||
*posHeader = (u32 *)memsearch(freeK9Space, "NCSD", emunand_size, 4);
|
||||
u32 *posOffset = (u32 *)memsearch(freeK9Space, "NAND", emunand_bin_size, 4),
|
||||
*posHeader = (u32 *)memsearch(freeK9Space, "NCSD", emunand_bin_size, 4);
|
||||
*posOffset = emuOffset;
|
||||
*posHeader = emuHeader;
|
||||
|
||||
//Find and add the SDMMC struct
|
||||
u32 *posSdmmc = (u32 *)memsearch(freeK9Space, "SDMC", emunand_size, 4);
|
||||
*posSdmmc = getSdmmc(process9Offset, process9Size);
|
||||
u32 *posSdmmc = (u32 *)memsearch(freeK9Space, "SDMC", emunand_bin_size, 4);
|
||||
u32 sdmmc;
|
||||
ret += getSdmmc(process9Offset, process9Size, &sdmmc);
|
||||
if(!ret) *posSdmmc = sdmmc;
|
||||
|
||||
//Add EmuNAND hooks
|
||||
u32 branchOffset = (u32)freeK9Space - branchAdditive;
|
||||
patchNandRw(process9Offset, process9Size, branchOffset);
|
||||
u32 branchOffset = (u32)(freeK9Space - arm9Section + kernel9Address);
|
||||
ret += patchNandRw(process9Offset, process9Size, branchOffset);
|
||||
|
||||
//Set MPU
|
||||
patchMpu(arm9Section, arm9SectionSize);
|
||||
ret += patchMpu(arm9Section, kernel9Size);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -20,15 +20,15 @@
|
||||
* Notices displayed by works containing it.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Code for locating the SDMMC struct by Normmatt
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#define NCSD_MAGIC 0x4453434E
|
||||
#define ROUND_TO_4MB(a) (((a) + 0x2000 - 1) & (~(0x2000 - 1)))
|
||||
|
||||
extern u32 emuOffset;
|
||||
extern bool isN3DS;
|
||||
|
||||
void locateEmuNand(u32 *emuHeader, FirmwareSource *nandType);
|
||||
void patchEmuNand(u8 *arm9Section, u32 arm9SectionSize, u8 *process9Offset, u32 process9Size, u32 emuHeader, u32 branchAdditive);
|
||||
u32 patchEmuNand(u8 *arm9Section, u32 kernel9Size, u8 *process9Offset, u32 process9Size, u32 emuHeader, u8 *kernel9Address);
|
||||
@@ -20,7 +20,6 @@
|
||||
* Notices displayed by works containing it.
|
||||
*/
|
||||
|
||||
#ifdef DEV
|
||||
#include "exceptions.h"
|
||||
#include "fs.h"
|
||||
#include "strings.h"
|
||||
@@ -28,174 +27,186 @@
|
||||
#include "screen.h"
|
||||
#include "draw.h"
|
||||
#include "utils.h"
|
||||
#include "../build/arm9_exceptions.h"
|
||||
#include "../build/arm11_exceptions.h"
|
||||
#include "fmt.h"
|
||||
#include "../build/bundled.h"
|
||||
|
||||
void installArm9Handlers(void)
|
||||
{
|
||||
const u32 offsets[] = {0x08, 0x18, 0x20, 0x28};
|
||||
|
||||
memcpy((void *)0x01FF8000, arm9_exceptions + 32, arm9_exceptions_size - 32);
|
||||
memcpy((void *)0x01FF8000, arm9_exceptions_bin + 32, arm9_exceptions_bin_size - 32);
|
||||
|
||||
/* IRQHandler is at 0x08000000, but we won't handle it for some reasons
|
||||
svcHandler is at 0x08000010, but we won't handle svc either */
|
||||
|
||||
const u32 offsets[] = {0x08, 0x18, 0x20, 0x28};
|
||||
|
||||
for(u32 i = 0; i < 4; i++)
|
||||
{
|
||||
*(vu32 *)(0x08000000 + offsets[i]) = 0xE51FF004;
|
||||
*(vu32 *)(0x08000000 + offsets[i] + 4) = *((u32 *)arm9_exceptions + 1 + i);
|
||||
*(vu32 *)(0x08000000 + offsets[i] + 4) = *((u32 *)arm9_exceptions_bin + 1 + i);
|
||||
}
|
||||
}
|
||||
|
||||
void installArm11Handlers(u32 *exceptionsPage, u32 stackAddress, u32 codeSetOffset)
|
||||
u32 installArm11Handlers(u32 *exceptionsPage, u32 stackAddress, u32 codeSetOffset, u32 *dAbtHandler, u32 dAbtHandlerMemAddress)
|
||||
{
|
||||
u32 *initFPU;
|
||||
for(initFPU = exceptionsPage; initFPU < (exceptionsPage + 0x400) && (initFPU[0] != 0xE59F0008 || initFPU[1] != 0xE5900000); initFPU++);
|
||||
u32 *endPos = exceptionsPage + 0x400;
|
||||
|
||||
u32 *mcuReboot;
|
||||
for(mcuReboot = exceptionsPage; mcuReboot < (exceptionsPage + 0x400) && (mcuReboot[0] != 0xE59F4104 || mcuReboot[1] != 0xE3A0A0C2); mcuReboot++);
|
||||
mcuReboot--;
|
||||
u32 *initFPU;
|
||||
for(initFPU = exceptionsPage; initFPU < endPos && *initFPU != 0xE1A0D002; initFPU++);
|
||||
|
||||
u32 *freeSpace;
|
||||
for(freeSpace = initFPU; freeSpace < (exceptionsPage + 0x400) && (freeSpace[0] != 0xFFFFFFFF || freeSpace[1] != 0xFFFFFFFF); freeSpace++);
|
||||
for(freeSpace = initFPU; freeSpace < endPos && *freeSpace != 0xFFFFFFFF; freeSpace++);
|
||||
|
||||
memcpy(freeSpace, arm11_exceptions + 32, arm11_exceptions_size - 32);
|
||||
u32 *mcuReboot;
|
||||
for(mcuReboot = exceptionsPage; mcuReboot < endPos && *mcuReboot != 0xE3A0A0C2; mcuReboot++);
|
||||
|
||||
exceptionsPage[1] = MAKE_BRANCH(exceptionsPage + 1, (u8 *)freeSpace + *(u32 *)(arm11_exceptions + 8) - 32); //Undefined Instruction
|
||||
exceptionsPage[3] = MAKE_BRANCH(exceptionsPage + 3, (u8 *)freeSpace + *(u32 *)(arm11_exceptions + 12) - 32); //Prefetch Abort
|
||||
exceptionsPage[4] = MAKE_BRANCH(exceptionsPage + 4, (u8 *)freeSpace + *(u32 *)(arm11_exceptions + 16) - 32); //Data Abort
|
||||
exceptionsPage[7] = MAKE_BRANCH(exceptionsPage + 7, (u8 *)freeSpace + *(u32 *)(arm11_exceptions + 4) - 32); //FIQ
|
||||
if(initFPU == endPos || freeSpace == endPos || mcuReboot == endPos || *(u32 *)((u8 *)freeSpace + arm11_exceptions_bin_size - 36) != 0xFFFFFFFF) return 1;
|
||||
|
||||
for(u32 *pos = freeSpace; pos < (u32 *)((u8 *)freeSpace + arm11_exceptions_size - 32); pos++)
|
||||
initFPU += 3;
|
||||
mcuReboot -= 2;
|
||||
|
||||
memcpy(freeSpace, arm11_exceptions_bin + 32, arm11_exceptions_bin_size - 32);
|
||||
|
||||
exceptionsPage[1] = MAKE_BRANCH(exceptionsPage + 1, (u8 *)freeSpace + *(u32 *)(arm11_exceptions_bin + 8) - 32); //Undefined Instruction
|
||||
exceptionsPage[3] = MAKE_BRANCH(exceptionsPage + 3, (u8 *)freeSpace + *(u32 *)(arm11_exceptions_bin + 12) - 32); //Prefetch Abort
|
||||
exceptionsPage[7] = MAKE_BRANCH(exceptionsPage + 7, (u8 *)freeSpace + *(u32 *)(arm11_exceptions_bin + 4) - 32); //FIQ
|
||||
|
||||
for(u32 *pos = dAbtHandler; *pos != stackAddress; pos++)
|
||||
{
|
||||
u32 va_dst = 0xFFFF0000 + (((u8 *)freeSpace + *(u32 *)(arm11_exceptions_bin + 4)) - (u8 *)exceptionsPage);
|
||||
u32 va_src;
|
||||
switch(*pos)
|
||||
{
|
||||
case 0xF96D0513: //srsdb sp!, 0x13
|
||||
va_src = dAbtHandlerMemAddress + ((u8 *)pos - (u8 *)dAbtHandler);
|
||||
*pos = MAKE_BRANCH((u8 *)va_src, (u8 *)va_dst);
|
||||
break;
|
||||
case 0xE29EF004: //subs pc, lr, 4
|
||||
pos++;
|
||||
*pos++ = 0xE8BD000F;// pop {r0-r3}
|
||||
va_src = dAbtHandlerMemAddress + ((u8 *)pos - (u8 *)dAbtHandler);
|
||||
*pos = MAKE_BRANCH((u8 *)va_src, (u8 *)va_dst);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for(u32 *pos = freeSpace; pos < (u32 *)((u8 *)freeSpace + arm11_exceptions_bin_size - 32); pos++)
|
||||
{
|
||||
switch(*pos) //Perform relocations
|
||||
{
|
||||
case 0xFFFF3000: *pos = stackAddress; break;
|
||||
case 0xFFFF3000: *pos = stackAddress - 0x10; break;
|
||||
case 0xEBFFFFFE: *pos = MAKE_BRANCH_LINK(pos, initFPU); break;
|
||||
case 0xEAFFFFFE: *pos = MAKE_BRANCH(pos, mcuReboot); break;
|
||||
case 0xE12FFF1C: pos[1] = 0xFFFF0000 + 4 * (u32)(freeSpace - exceptionsPage) + pos[1] - 32; break; //bx r12 (mainHandler)
|
||||
case 0xBEEFBEEF: *pos = codeSetOffset; break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void detectAndProcessExceptionDumps(void)
|
||||
{
|
||||
volatile ExceptionDumpHeader *dumpHeader = (volatile ExceptionDumpHeader *)0x25000000;
|
||||
|
||||
if(dumpHeader->magic[0] == 0xDEADC0DE && dumpHeader->magic[1] == 0xDEADCAFE && (dumpHeader->processor == 9 || dumpHeader->processor == 11))
|
||||
if(dumpHeader->magic[0] != 0xDEADC0DE || dumpHeader->magic[1] != 0xDEADCAFE || (dumpHeader->processor != 9 && dumpHeader->processor != 11)) return;
|
||||
|
||||
const vu32 *regs = (vu32 *)((vu8 *)dumpHeader + sizeof(ExceptionDumpHeader));
|
||||
const vu8 *stackDump = (vu8 *)regs + dumpHeader->registerDumpSize + dumpHeader->codeDumpSize;
|
||||
const vu8 *additionalData = stackDump + dumpHeader->stackDumpSize;
|
||||
|
||||
const char *handledExceptionNames[] = {
|
||||
"FIQ", "undefined instruction", "prefetch abort", "data abort"
|
||||
};
|
||||
|
||||
const char *specialExceptions[] = {
|
||||
"kernel panic", "svcBreak"
|
||||
};
|
||||
|
||||
const char *registerNames[] = {
|
||||
"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12",
|
||||
"SP", "LR", "PC", "CPSR", "FPEXC"
|
||||
};
|
||||
|
||||
initScreens();
|
||||
|
||||
drawString(true, 10, 10, COLOR_RED, "An exception occurred");
|
||||
u32 posY;
|
||||
if(dumpHeader->processor == 11) posY = drawFormattedString(true, 10, 30, COLOR_WHITE, "Processor: ARM11 (core %u)", dumpHeader->core);
|
||||
else posY = drawString(true, 10, 30, COLOR_WHITE, "Processor: ARM9");
|
||||
|
||||
if(dumpHeader->type == 2)
|
||||
{
|
||||
const vu32 *regs = (vu32 *)((vu8 *)dumpHeader + sizeof(ExceptionDumpHeader));
|
||||
const vu8 *stackDump = (vu8 *)regs + dumpHeader->registerDumpSize + dumpHeader->codeDumpSize;
|
||||
const vu8 *additionalData = stackDump + dumpHeader->stackDumpSize;
|
||||
|
||||
const char *handledExceptionNames[] = {
|
||||
"FIQ", "undefined instruction", "prefetch abort", "data abort"
|
||||
};
|
||||
|
||||
const char *specialExceptions[] = {
|
||||
"(kernel panic)", "(svcBreak)"
|
||||
};
|
||||
|
||||
const char *registerNames[] = {
|
||||
"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12",
|
||||
"SP", "LR", "PC", "CPSR", "FPEXC"
|
||||
};
|
||||
|
||||
char hexString[] = "00000000";
|
||||
|
||||
initScreens();
|
||||
|
||||
drawString("An exception occurred", true, 10, 10, COLOR_RED);
|
||||
u32 posY = drawString(dumpHeader->processor == 11 ? "Processor: ARM11 (core )" : "Processor: ARM9", true, 10, 30, COLOR_WHITE);
|
||||
if(dumpHeader->processor == 11) drawCharacter('0' + dumpHeader->core, true, 10 + 29 * SPACING_X, 30, COLOR_WHITE);
|
||||
|
||||
posY = drawString("Exception type: ", true, 10, posY + SPACING_Y, COLOR_WHITE);
|
||||
drawString(handledExceptionNames[dumpHeader->type], true, 10 + 17 * SPACING_X, posY, COLOR_WHITE);
|
||||
|
||||
if(dumpHeader->type == 2)
|
||||
if((regs[16] & 0x20) == 0 && dumpHeader->codeDumpSize >= 4)
|
||||
{
|
||||
if((regs[16] & 0x20) == 0 && dumpHeader->codeDumpSize >= 4)
|
||||
{
|
||||
u32 instr = *(vu32 *)(stackDump - 4);
|
||||
if(instr == 0xE12FFF7E) drawString(specialExceptions[0], true, 10 + 32 * SPACING_X, posY, COLOR_WHITE);
|
||||
else if(instr == 0xEF00003C) drawString(specialExceptions[1], true, 10 + 32 * SPACING_X, posY, COLOR_WHITE);
|
||||
}
|
||||
else if((regs[16] & 0x20) == 0 && dumpHeader->codeDumpSize >= 2)
|
||||
{
|
||||
u16 instr = *(vu16 *)(stackDump - 2);
|
||||
if(instr == 0xDF3C) drawString(specialExceptions[1], true, 10 + 32 * SPACING_X, posY, COLOR_WHITE);
|
||||
}
|
||||
u32 instr = *(vu32 *)(stackDump - 4);
|
||||
if(instr == 0xE12FFF7E)
|
||||
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Exception type: %s (%s)", handledExceptionNames[dumpHeader->type], specialExceptions[0]);
|
||||
else if(instr == 0xEF00003C)
|
||||
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Exception type: %s (%s)", handledExceptionNames[dumpHeader->type], specialExceptions[1]);
|
||||
else
|
||||
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Exception type: %s", handledExceptionNames[dumpHeader->type]);
|
||||
}
|
||||
|
||||
if(dumpHeader->processor == 11 && dumpHeader->additionalDataSize != 0)
|
||||
else if((regs[16] & 0x20) != 0 && dumpHeader->codeDumpSize >= 2)
|
||||
{
|
||||
char processName[] = "Current process: ";
|
||||
memcpy(processName + sizeof(processName) - 9, (void *)additionalData, 8);
|
||||
posY = drawString(processName, true, 10, posY + SPACING_Y, COLOR_WHITE);
|
||||
u16 instr = *(vu16 *)(stackDump - 2);
|
||||
if(instr == 0xDF3C)
|
||||
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Exception type: %s (%s)", handledExceptionNames[dumpHeader->type], specialExceptions[0]);
|
||||
else
|
||||
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Exception type: %s", handledExceptionNames[dumpHeader->type]);
|
||||
}
|
||||
|
||||
posY += SPACING_Y;
|
||||
|
||||
for(u32 i = 0; i < 17; i += 2)
|
||||
{
|
||||
posY = drawString(registerNames[i], true, 10, posY + SPACING_Y, COLOR_WHITE);
|
||||
hexItoa(regs[i], hexString, 8);
|
||||
drawString(hexString, true, 10 + 7 * SPACING_X, posY, COLOR_WHITE);
|
||||
|
||||
if(i != 16 || dumpHeader->processor != 9)
|
||||
{
|
||||
drawString(registerNames[i + 1], true, 10 + 22 * SPACING_X, posY, COLOR_WHITE);
|
||||
hexItoa(i == 16 ? regs[20] : regs[i + 1], hexString, 8);
|
||||
drawString(hexString, true, 10 + 29 * SPACING_X, posY, COLOR_WHITE);
|
||||
}
|
||||
}
|
||||
|
||||
posY += SPACING_Y;
|
||||
|
||||
u32 mode = regs[16] & 0xF;
|
||||
if(dumpHeader->type == 3 && (mode == 7 || mode == 11))
|
||||
posY = drawString("Incorrect dump: failed to dump code and/or stack", true, 10, posY + SPACING_Y, COLOR_YELLOW) + SPACING_Y;
|
||||
|
||||
u32 posYBottom = drawString("Stack dump:", false, 10, 10, COLOR_WHITE) + SPACING_Y;
|
||||
|
||||
for(u32 line = 0; line < 19 && stackDump < additionalData; line++)
|
||||
{
|
||||
hexItoa(regs[13] + 8 * line, hexString, 8);
|
||||
posYBottom = drawString(hexString, false, 10, posYBottom + SPACING_Y, COLOR_WHITE);
|
||||
drawCharacter(':', false, 10 + 8 * SPACING_X, posYBottom, COLOR_WHITE);
|
||||
|
||||
for(u32 i = 0; i < 8 && stackDump < additionalData; i++, stackDump++)
|
||||
{
|
||||
char byteString[] = "00";
|
||||
hexItoa(*stackDump, byteString, 2);
|
||||
drawString(byteString, false, 10 + 10 * SPACING_X + 3 * i * SPACING_X, posYBottom, COLOR_WHITE);
|
||||
}
|
||||
}
|
||||
|
||||
char path[42];
|
||||
char fileName[] = "crash_dump_00000000.dmp";
|
||||
const char *pathFolder = dumpHeader->processor == 9 ? "/luma/dumps/arm9" : "/luma/dumps/arm11";
|
||||
|
||||
findDumpFile(pathFolder, fileName);
|
||||
memcpy(path, pathFolder, strlen(pathFolder) + 1);
|
||||
concatenateStrings(path, "/");
|
||||
concatenateStrings(path, fileName);
|
||||
|
||||
if(fileWrite((void *)dumpHeader, path, dumpHeader->totalSize))
|
||||
{
|
||||
posY = drawString("You can find a dump in the following file:", true, 10, posY + SPACING_Y, COLOR_WHITE);
|
||||
posY = drawString(path, true, 10, posY + SPACING_Y, COLOR_WHITE) + SPACING_Y;
|
||||
}
|
||||
else posY = drawString("Error writing the dump file", true, 10, posY + SPACING_Y, COLOR_RED);
|
||||
|
||||
drawString("Press any button to shutdown", true, 10, posY + SPACING_Y, COLOR_WHITE);
|
||||
|
||||
memset32((void *)dumpHeader, 0, dumpHeader->totalSize);
|
||||
|
||||
waitInput();
|
||||
mcuPowerOff();
|
||||
}
|
||||
else
|
||||
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Exception type: %s", handledExceptionNames[dumpHeader->type]);
|
||||
|
||||
if(dumpHeader->processor == 11 && dumpHeader->additionalDataSize != 0)
|
||||
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE,
|
||||
"Current process: %.8s (%016llX)", (const char *)additionalData, *(vu64 *)(additionalData + 8));
|
||||
posY += SPACING_Y;
|
||||
|
||||
for(u32 i = 0; i < 17; i += 2)
|
||||
{
|
||||
posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, "%-7s%08X", registerNames[i], regs[i]);
|
||||
|
||||
if(i != 16 || dumpHeader->processor != 9)
|
||||
posY = drawFormattedString(true, 10 + 22 * SPACING_X, posY, COLOR_WHITE, "%-7s%08X", registerNames[i + 1], regs[i + 1]);
|
||||
}
|
||||
|
||||
posY += SPACING_Y;
|
||||
|
||||
u32 mode = regs[16] & 0xF;
|
||||
if(dumpHeader->type == 3 && (mode == 7 || mode == 11))
|
||||
posY = drawString(true, 10, posY + SPACING_Y, COLOR_YELLOW, "Incorrect dump: failed to dump code and/or stack") + SPACING_Y;
|
||||
|
||||
u32 posYBottom = drawString(false, 10, 10, COLOR_WHITE, "Stack dump:") + SPACING_Y;
|
||||
|
||||
for(u32 line = 0; line < 19 && stackDump < additionalData; line++)
|
||||
{
|
||||
posYBottom = drawFormattedString(false, 10, posYBottom + SPACING_Y, COLOR_WHITE, "%08X:", regs[13] + 8 * line);
|
||||
|
||||
for(u32 i = 0; i < 8 && stackDump < additionalData; i++, stackDump++)
|
||||
drawFormattedString(false, 10 + 10 * SPACING_X + 3 * i * SPACING_X, posYBottom, COLOR_WHITE, "%02X", *stackDump);
|
||||
}
|
||||
|
||||
char folderPath[12],
|
||||
path[36],
|
||||
fileName[24];
|
||||
|
||||
sprintf(folderPath, "dumps/arm%u", dumpHeader->processor);
|
||||
findDumpFile(folderPath, fileName);
|
||||
sprintf(path, "%s/%s", folderPath, fileName);
|
||||
|
||||
if(fileWrite((void *)dumpHeader, path, dumpHeader->totalSize))
|
||||
{
|
||||
posY = drawString(true, 10, posY + SPACING_Y, COLOR_WHITE, "You can find a dump in the following file:");
|
||||
posY = drawString(true, 10, posY + SPACING_Y, COLOR_WHITE, path) + SPACING_Y;
|
||||
}
|
||||
else posY = drawString(true, 10, posY + SPACING_Y, COLOR_RED, "Error writing the dump file");
|
||||
|
||||
drawString(true, 10, posY + SPACING_Y, COLOR_WHITE, "Press any button to shutdown");
|
||||
|
||||
memset32((void *)dumpHeader, 0, dumpHeader->totalSize);
|
||||
|
||||
waitInput(false);
|
||||
mcuPowerOff();
|
||||
}
|
||||
#endif
|
||||
@@ -20,7 +20,6 @@
|
||||
* Notices displayed by works containing it.
|
||||
*/
|
||||
|
||||
#ifdef DEV
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
@@ -28,22 +27,6 @@
|
||||
#define MAKE_BRANCH(src,dst) (0xEA000000 | ((u32)((((u8 *)(dst) - (u8 *)(src)) >> 2) - 2) & 0xFFFFFF))
|
||||
#define MAKE_BRANCH_LINK(src,dst) (0xEB000000 | ((u32)((((u8 *)(dst) - (u8 *)(src)) >> 2) - 2) & 0xFFFFFF))
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
u32 magic[2];
|
||||
u16 versionMinor, versionMajor;
|
||||
|
||||
u16 processor, core;
|
||||
u32 type;
|
||||
|
||||
u32 totalSize;
|
||||
u32 registerDumpSize;
|
||||
u32 codeDumpSize;
|
||||
u32 stackDumpSize;
|
||||
u32 additionalDataSize;
|
||||
} ExceptionDumpHeader;
|
||||
|
||||
void installArm9Handlers(void);
|
||||
void installArm11Handlers(u32 *exceptionsPage, u32 stackAddress, u32 codeSetOffset);
|
||||
u32 installArm11Handlers(u32 *exceptionsPage, u32 stackAddress, u32 codeSetOffset, u32 *dAbtHandler, u32 dAbtHandlerMemAddress);
|
||||
void detectAndProcessExceptionDumps(void);
|
||||
#endif
|
||||
13
source/fatfs/00history.txt
Executable file → Normal file
13
source/fatfs/00history.txt
Executable file → Normal file
@@ -212,7 +212,7 @@ R0.10a (January 15, 2014)
|
||||
R0.10b (May 19, 2014)
|
||||
|
||||
Fixed a hard error in the disk I/O layer can collapse the directory entry.
|
||||
Fixed LFN entry is not deleted on delete/rename an object with lossy converted SFN. (appeared at R0.07)
|
||||
Fixed LFN entry is not deleted when delete/rename an object with lossy converted SFN. (appeared at R0.07)
|
||||
|
||||
|
||||
|
||||
@@ -268,7 +268,7 @@ R0.12a (July 10, 2016)
|
||||
|
||||
R0.12b (September 04, 2016)
|
||||
|
||||
Improved f_rename() to be able to rename objects with the same name but case.
|
||||
Made f_rename() be able to rename objects with the same name but case.
|
||||
Fixed an error in the case conversion teble of code page 866. (ff.c)
|
||||
Fixed writing data is truncated at the file offset 4GiB on the exFAT volume. (appeared at R0.12)
|
||||
Fixed creating a file in the root directory of exFAT volume can fail. (appeared at R0.12)
|
||||
@@ -277,3 +277,12 @@ R0.12b (September 04, 2016)
|
||||
Fixed large file allocation/removing on the exFAT volume collapses allocation bitmap. (appeared at R0.12)
|
||||
Fixed some internal errors in f_expand() and f_lseek(). (appeared at R0.12)
|
||||
|
||||
|
||||
|
||||
R0.12c (March 04, 2017)
|
||||
|
||||
Improved write throughput at the fragmented file on the exFAT volume.
|
||||
Made memory usage for exFAT be able to be reduced as decreasing _MAX_LFN.
|
||||
Fixed successive f_getfree() can return wrong count on the FAT12/16 volume. (appeared at R0.12)
|
||||
Fixed configuration option _VOLUMES cannot be set 10. (appeared at R0.10c)
|
||||
|
||||
|
||||
26
source/fatfs/00readme.txt
Executable file → Normal file
26
source/fatfs/00readme.txt
Executable file → Normal file
@@ -1,21 +1,21 @@
|
||||
FatFs Module Source Files R0.12a
|
||||
FatFs Module Source Files R0.12c
|
||||
|
||||
|
||||
FILES
|
||||
|
||||
00readme.txt This file.
|
||||
history.txt Revision history.
|
||||
ffconf.h Configuration file for FatFs module.
|
||||
ff.h Common include file for FatFs and application module.
|
||||
ff.c FatFs module.
|
||||
diskio.h Common include file for FatFs and disk I/O module.
|
||||
diskio.c An example of glue function to attach existing disk I/O module to FatFs.
|
||||
integer.h Integer type definitions for FatFs.
|
||||
option Optional external functions.
|
||||
00readme.txt This file.
|
||||
00history.txt Revision history.
|
||||
ff.c FatFs module.
|
||||
ffconf.h Configuration file of FatFs module.
|
||||
ff.h Common include file for FatFs and application module.
|
||||
diskio.h Common include file for FatFs and disk I/O module.
|
||||
diskio.c An example of glue function to attach existing disk I/O module to FatFs.
|
||||
integer.h Integer type definitions for FatFs.
|
||||
option Optional external modules.
|
||||
|
||||
|
||||
Low level disk I/O module is not included in this archive because the FatFs
|
||||
module is only a generic file system layer and not depend on any specific
|
||||
storage device. You have to provide a low level disk I/O module that written
|
||||
to control the target storage device.
|
||||
module is only a generic file system layer and it does not depend on any specific
|
||||
storage device. You have to provide a low level disk I/O module written to
|
||||
control the storage device that attached to the target system.
|
||||
|
||||
|
||||
@@ -37,17 +37,23 @@ DSTATUS disk_initialize (
|
||||
BYTE pdrv /* Physical drive nmuber to identify the drive */
|
||||
)
|
||||
{
|
||||
switch(pdrv)
|
||||
{
|
||||
case SDCARD:
|
||||
sdmmc_sdcard_init();
|
||||
break;
|
||||
case CTRNAND:
|
||||
ctrNandInit();
|
||||
break;
|
||||
}
|
||||
DSTATUS ret;
|
||||
static u32 sdmmcInitResult = 4;
|
||||
|
||||
return RES_OK;
|
||||
if(sdmmcInitResult == 4) sdmmcInitResult = sdmmc_sdcard_init();
|
||||
|
||||
if(pdrv == CTRNAND)
|
||||
{
|
||||
if(!(sdmmcInitResult & 1))
|
||||
{
|
||||
ctrNandInit();
|
||||
ret = 0;
|
||||
}
|
||||
else ret = STA_NOINIT;
|
||||
}
|
||||
else ret = (!(sdmmcInitResult & 2)) ? 0 : STA_NOINIT;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -63,19 +69,8 @@ DRESULT disk_read (
|
||||
UINT count /* Number of sectors to read */
|
||||
)
|
||||
{
|
||||
switch(pdrv)
|
||||
{
|
||||
case SDCARD:
|
||||
if(sdmmc_sdcard_readsectors(sector, count, (BYTE *)buff))
|
||||
return RES_PARERR;
|
||||
break;
|
||||
case CTRNAND:
|
||||
if(ctrNandRead(sector, count, (BYTE *)buff))
|
||||
return RES_PARERR;
|
||||
break;
|
||||
}
|
||||
|
||||
return RES_OK;
|
||||
return ((pdrv == SDCARD && !sdmmc_sdcard_readsectors(sector, count, buff)) ||
|
||||
(pdrv == CTRNAND && !ctrNandRead(sector, count, buff))) ? RES_OK : RES_PARERR;
|
||||
}
|
||||
|
||||
|
||||
@@ -92,10 +87,8 @@ DRESULT disk_write (
|
||||
UINT count /* Number of sectors to write */
|
||||
)
|
||||
{
|
||||
if(pdrv == SDCARD && sdmmc_sdcard_writesectors(sector, count, (BYTE *)buff))
|
||||
return RES_PARERR;
|
||||
|
||||
return RES_OK;
|
||||
return ((pdrv == SDCARD && !sdmmc_sdcard_writesectors(sector, count, buff)) ||
|
||||
(pdrv == CTRNAND && !ctrNandWrite(sector, count, buff))) ? RES_OK : RES_PARERR;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
753
source/fatfs/ff.c
Executable file → Normal file
753
source/fatfs/ff.c
Executable file → Normal file
File diff suppressed because it is too large
Load Diff
25
source/fatfs/ff.h
Executable file → Normal file
25
source/fatfs/ff.h
Executable file → Normal file
@@ -1,8 +1,8 @@
|
||||
/*----------------------------------------------------------------------------/
|
||||
/ FatFs - Generic FAT file system module R0.12b /
|
||||
/ FatFs - Generic FAT file system module R0.12c /
|
||||
/-----------------------------------------------------------------------------/
|
||||
/
|
||||
/ Copyright (C) 2016, ChaN, all right reserved.
|
||||
/ Copyright (C) 2017, ChaN, all right reserved.
|
||||
/
|
||||
/ FatFs module is an open source software. Redistribution and use of FatFs in
|
||||
/ source and binary forms, with or without modification, are permitted provided
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
|
||||
#ifndef _FATFS
|
||||
#define _FATFS 68020 /* Revision ID */
|
||||
#define _FATFS 68300 /* Revision ID */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -42,13 +42,6 @@ typedef struct {
|
||||
BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */
|
||||
} PARTITION;
|
||||
extern PARTITION VolToPart[]; /* Volume - Partition resolution table */
|
||||
#define LD2PD(vol) (VolToPart[vol].pd) /* Get physical drive number */
|
||||
#define LD2PT(vol) (VolToPart[vol].pt) /* Get partition index */
|
||||
|
||||
#else /* Single partition configuration */
|
||||
#define LD2PD(vol) (BYTE)(vol) /* Each logical drive is bound to the same physical drive number */
|
||||
#define LD2PT(vol) 0 /* Find first valid partition or in SFD */
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -140,14 +133,15 @@ typedef struct {
|
||||
FATFS* fs; /* Pointer to the owner file system object */
|
||||
WORD id; /* Owner file system mount ID */
|
||||
BYTE attr; /* Object attribute */
|
||||
BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous (no data on FAT), =3:got flagmented, b2:sub-directory stretched) */
|
||||
BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous (no data on FAT), =3:flagmented in this session, b2:sub-directory stretched) */
|
||||
DWORD sclust; /* Object start cluster (0:no cluster or root directory) */
|
||||
FSIZE_t objsize; /* Object size (valid when sclust != 0) */
|
||||
#if _FS_EXFAT
|
||||
DWORD n_cont; /* Size of coutiguous part, clusters - 1 (valid when stat == 3) */
|
||||
DWORD n_cont; /* Size of first fragment, clusters - 1 (valid when stat == 3) */
|
||||
DWORD n_frag; /* Size of last fragment needs to be written (valid when not zero) */
|
||||
DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */
|
||||
DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */
|
||||
DWORD c_ofs; /* Offset in the containing directory (valid when sclust != 0) */
|
||||
DWORD c_ofs; /* Offset in the containing directory (valid when sclust != 0 and non-directory object) */
|
||||
#endif
|
||||
#if _FS_LOCK != 0
|
||||
UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */
|
||||
@@ -163,7 +157,7 @@ typedef struct {
|
||||
BYTE flag; /* File status flags */
|
||||
BYTE err; /* Abort flag (error code) */
|
||||
FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */
|
||||
DWORD clust; /* Current cluster of fpter (invalid when fprt is 0) */
|
||||
DWORD clust; /* Current cluster of fpter (invalid when fptr is 0) */
|
||||
DWORD sect; /* Sector number appearing in buf[] (0:invalid) */
|
||||
#if !_FS_READONLY
|
||||
DWORD dir_sect; /* Sector number containing the directory entry */
|
||||
@@ -185,7 +179,7 @@ typedef struct {
|
||||
_FDID obj; /* Object identifier */
|
||||
DWORD dptr; /* Current read/write offset */
|
||||
DWORD clust; /* Current cluster */
|
||||
DWORD sect; /* Current sector */
|
||||
DWORD sect; /* Current sector (0:Read operation has terminated) */
|
||||
BYTE* dir; /* Pointer to the directory item in the win[] */
|
||||
BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */
|
||||
#if _USE_LFN != 0
|
||||
@@ -285,6 +279,7 @@ TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the fil
|
||||
#define f_size(fp) ((fp)->obj.objsize)
|
||||
#define f_rewind(fp) f_lseek((fp), 0)
|
||||
#define f_rewinddir(dp) f_readdir((dp), 0)
|
||||
#define f_rmdir(path) f_unlink(path)
|
||||
|
||||
#ifndef EOF
|
||||
#define EOF (-1)
|
||||
|
||||
25
source/fatfs/ffconf.h
Executable file → Normal file
25
source/fatfs/ffconf.h
Executable file → Normal file
@@ -2,7 +2,7 @@
|
||||
/ FatFs - FAT file system module configuration file
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define _FFCONF 68020 /* Revision ID */
|
||||
#define _FFCONF 68300 /* Revision ID */
|
||||
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ Function Configurations
|
||||
@@ -73,7 +73,7 @@
|
||||
/* This option specifies the OEM code page to be used on the target system.
|
||||
/ Incorrect setting of the code page can cause a file open failure.
|
||||
/
|
||||
/ 1 - ASCII (No extended character. Non-LFN cfg. only)
|
||||
/ 1 - ASCII (No support of extended character. Non-LFN cfg. only)
|
||||
/ 437 - U.S.
|
||||
/ 720 - Arabic
|
||||
/ 737 - Greek
|
||||
@@ -134,7 +134,7 @@
|
||||
/ This option has no effect when _LFN_UNICODE == 0. */
|
||||
|
||||
|
||||
#define _FS_RPATH 0
|
||||
#define _FS_RPATH 1
|
||||
/* This option configures support of relative path.
|
||||
/
|
||||
/ 0: Disable relative path and remove related functions.
|
||||
@@ -148,7 +148,7 @@
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define _VOLUMES 2
|
||||
/* Number of volumes (logical drives) to be used. */
|
||||
/* Number of volumes (logical drives) to be used. (1-10) */
|
||||
|
||||
|
||||
#define _STR_VOLUME_ID 0
|
||||
@@ -172,11 +172,11 @@
|
||||
#define _MIN_SS 512
|
||||
#define _MAX_SS 512
|
||||
/* These options configure the range of sector size to be supported. (512, 1024,
|
||||
/ 2048 or 4096) Always set both 512 for most systems, all type of memory cards and
|
||||
/ 2048 or 4096) Always set both 512 for most systems, generic memory card and
|
||||
/ harddisk. But a larger value may be required for on-board flash memory and some
|
||||
/ type of optical media. When _MAX_SS is larger than _MIN_SS, FatFs is configured
|
||||
/ to variable sector size and GET_SECTOR_SIZE command must be implemented to the
|
||||
/ disk_ioctl() function. */
|
||||
/ to variable sector size and GET_SECTOR_SIZE command needs to be implemented to
|
||||
/ the disk_ioctl() function. */
|
||||
|
||||
|
||||
#define _USE_TRIM 0
|
||||
@@ -204,7 +204,7 @@
|
||||
|
||||
#define _FS_TINY 0
|
||||
/* This option switches tiny buffer configuration. (0:Normal or 1:Tiny)
|
||||
/ At the tiny configuration, size of file object (FIL) is reduced _MAX_SS bytes.
|
||||
/ At the tiny configuration, size of file object (FIL) is shrinked _MAX_SS bytes.
|
||||
/ Instead of private sector buffer eliminated from the file object, common sector
|
||||
/ buffer in the file system object (FATFS) is used for the file data transfer. */
|
||||
|
||||
@@ -212,20 +212,20 @@
|
||||
#define _FS_EXFAT 0
|
||||
/* This option switches support of exFAT file system. (0:Disable or 1:Enable)
|
||||
/ When enable exFAT, also LFN needs to be enabled. (_USE_LFN >= 1)
|
||||
/ Note that enabling exFAT discards C89 compatibility. */
|
||||
/ Note that enabling exFAT discards ANSI C (C89) compatibility. */
|
||||
|
||||
|
||||
#define _FS_NORTC 1
|
||||
#define _NORTC_MON 1
|
||||
#define _NORTC_MDAY 1
|
||||
#define _NORTC_YEAR 2016
|
||||
#define _NORTC_YEAR 2017
|
||||
/* The option _FS_NORTC switches timestamp functiton. If the system does not have
|
||||
/ any RTC function or valid timestamp is not needed, set _FS_NORTC = 1 to disable
|
||||
/ the timestamp function. All objects modified by FatFs will have a fixed timestamp
|
||||
/ defined by _NORTC_MON, _NORTC_MDAY and _NORTC_YEAR in local time.
|
||||
/ To enable timestamp function (_FS_NORTC = 0), get_fattime() function need to be
|
||||
/ added to the project to get current time form real-time clock. _NORTC_MON,
|
||||
/ _NORTC_MDAY and _NORTC_YEAR have no effect.
|
||||
/ _NORTC_MDAY and _NORTC_YEAR have no effect.
|
||||
/ These options have no effect at read-only configuration (_FS_READONLY = 1). */
|
||||
|
||||
|
||||
@@ -258,10 +258,11 @@
|
||||
/
|
||||
/ The _FS_TIMEOUT defines timeout period in unit of time tick.
|
||||
/ The _SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*,
|
||||
/ SemaphoreHandle_t and etc.. A header file for O/S definitions needs to be
|
||||
/ SemaphoreHandle_t and etc. A header file for O/S definitions needs to be
|
||||
/ included somewhere in the scope of ff.h. */
|
||||
|
||||
/* #include <windows.h> // O/S definitions */
|
||||
|
||||
|
||||
|
||||
/*--- End of configuration options ---*/
|
||||
|
||||
2
source/fatfs/integer.h
Executable file → Normal file
2
source/fatfs/integer.h
Executable file → Normal file
@@ -30,7 +30,7 @@ typedef unsigned short WCHAR;
|
||||
typedef long LONG;
|
||||
typedef unsigned long DWORD;
|
||||
|
||||
/* This type MUST be 64-bit (Remove this for C89 compatibility) */
|
||||
/* This type MUST be 64-bit (Remove this for ANSI C (C89) compatibility) */
|
||||
typedef unsigned long long QWORD;
|
||||
|
||||
#endif
|
||||
|
||||
0
source/fatfs/option/ccsbcs.c
Executable file → Normal file
0
source/fatfs/option/ccsbcs.c
Executable file → Normal file
@@ -1,15 +1,16 @@
|
||||
.text
|
||||
.arm
|
||||
.global waitcycles
|
||||
.type waitcycles STT_FUNC
|
||||
.align 4
|
||||
|
||||
@waitcycles ( u32 us )
|
||||
.global waitcycles
|
||||
.type waitcycles, %function
|
||||
waitcycles:
|
||||
PUSH {R0-R2,LR}
|
||||
STR R0, [SP,#4]
|
||||
waitcycles_loop:
|
||||
LDR R3, [SP,#4]
|
||||
SUBS R2, R3, #1
|
||||
STR R2, [SP,#4]
|
||||
CMP R3, #0
|
||||
BNE waitcycles_loop
|
||||
POP {R0-R2,PC}
|
||||
push {r0-r2, lr}
|
||||
str r0, [sp, #4]
|
||||
waitcycles_loop:
|
||||
ldr r3, [sp, #4]
|
||||
subs r2, r3, #1
|
||||
str r2, [sp, #4]
|
||||
cmp r3, #0
|
||||
bne waitcycles_loop
|
||||
pop {r0-r2, pc}
|
||||
|
||||
@@ -244,7 +244,6 @@ int __attribute__((noinline)) sdmmc_nand_readsectors(u32 sector_no, u32 numsecto
|
||||
return geterror(&handleNAND);
|
||||
}
|
||||
|
||||
/*
|
||||
int __attribute__((noinline)) sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, const u8 *in) //experimental
|
||||
{
|
||||
if(handleNAND.isSDHC == 0) sector_no <<= 9;
|
||||
@@ -259,7 +258,6 @@ int __attribute__((noinline)) sdmmc_nand_writesectors(u32 sector_no, u32 numsect
|
||||
inittarget(&handleSD);
|
||||
return geterror(&handleNAND);
|
||||
}
|
||||
*/
|
||||
|
||||
static u32 calcSDSize(u8 *csd, int type)
|
||||
{
|
||||
@@ -268,17 +266,17 @@ static u32 calcSDSize(u8 *csd, int type)
|
||||
switch(type)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
u32 block_len = csd[9] & 0xF;
|
||||
block_len = 1u << block_len;
|
||||
u32 mult = (u32)((csd[4] >> 7) | ((csd[5] & 3) << 1));
|
||||
mult = 1u << (mult + 2);
|
||||
result = csd[8] & 3;
|
||||
result = (result << 8) | csd[7];
|
||||
result = (result << 2) | (csd[6] >> 6);
|
||||
result = (result + 1) * mult * block_len / 512;
|
||||
}
|
||||
{
|
||||
u32 block_len = csd[9] & 0xF;
|
||||
block_len = 1u << block_len;
|
||||
u32 mult = (u32)((csd[4] >> 7) | ((csd[5] & 3) << 1));
|
||||
mult = 1u << (mult + 2);
|
||||
result = csd[8] & 3;
|
||||
result = (result << 8) | csd[7];
|
||||
result = (result << 2) | (csd[6] >> 6);
|
||||
result = (result + 1) * mult * block_len / 512;
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
result = csd[7] & 0x3F;
|
||||
result = (result << 8) | csd[6];
|
||||
@@ -472,9 +470,11 @@ void sdmmc_get_cid(bool isNand, u32 *info)
|
||||
sdmmc_send_command(device, 0x10507, device->initarg << 0x10);
|
||||
}
|
||||
|
||||
void sdmmc_sdcard_init()
|
||||
u32 sdmmc_sdcard_init()
|
||||
{
|
||||
u32 ret = 0;
|
||||
InitSD();
|
||||
Nand_Init();
|
||||
SD_Init();
|
||||
if(Nand_Init() != 0) ret &= 1;
|
||||
if(SD_Init() != 0) ret &= 2;
|
||||
return ret;
|
||||
}
|
||||
@@ -91,10 +91,10 @@ typedef struct mmcdevice {
|
||||
u32 res;
|
||||
} mmcdevice;
|
||||
|
||||
void sdmmc_sdcard_init();
|
||||
u32 sdmmc_sdcard_init();
|
||||
int sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, u8 *out);
|
||||
int sdmmc_sdcard_writesectors(u32 sector_no, u32 numsectors, const u8 *in);
|
||||
int sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, u8 *out);
|
||||
//int sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, const u8 *in);
|
||||
int sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, const u8 *in);
|
||||
void sdmmc_get_cid(bool isNand, u32 *info);
|
||||
mmcdevice *getMMCDevice(int drive);
|
||||
723
source/firm.c
723
source/firm.c
@@ -24,526 +24,330 @@
|
||||
#include "config.h"
|
||||
#include "utils.h"
|
||||
#include "fs.h"
|
||||
#include "exceptions.h"
|
||||
#include "patches.h"
|
||||
#include "memory.h"
|
||||
#include "strings.h"
|
||||
#include "cache.h"
|
||||
#include "emunand.h"
|
||||
#include "crypto.h"
|
||||
#include "draw.h"
|
||||
#include "screen.h"
|
||||
#include "buttons.h"
|
||||
#include "pin.h"
|
||||
#include "../build/injector.h"
|
||||
#include "fmt.h"
|
||||
#include "../build/bundled.h"
|
||||
|
||||
#ifdef DEV
|
||||
#include "exceptions.h"
|
||||
#endif
|
||||
|
||||
extern u16 launchedFirmTidLow[8]; //Defined in start.s
|
||||
|
||||
static firmHeader *firm = (firmHeader *)0x24000000;
|
||||
static const firmSectionHeader *section;
|
||||
|
||||
u32 emuOffset;
|
||||
bool isN3DS,
|
||||
isDevUnit,
|
||||
isFirmlaunch;
|
||||
CfgData configData;
|
||||
FirmwareSource firmSource;
|
||||
|
||||
void main(void)
|
||||
static inline bool loadFirmFromStorage(FirmwareType firmType)
|
||||
{
|
||||
bool isA9lh;
|
||||
u32 configTemp,
|
||||
emuHeader;
|
||||
FirmwareType firmType;
|
||||
FirmwareSource nandType;
|
||||
ConfigurationStatus needConfig;
|
||||
|
||||
//Detect the console being used
|
||||
isN3DS = PDN_MPCORE_CFG == 7;
|
||||
|
||||
//Detect dev units
|
||||
isDevUnit = CFG_UNITINFO != 0;
|
||||
|
||||
//Mount filesystems. CTRNAND will be mounted only if/when needed
|
||||
mountFs();
|
||||
|
||||
//Attempt to read the configuration file
|
||||
needConfig = readConfig() ? MODIFY_CONFIGURATION : CREATE_CONFIGURATION;
|
||||
|
||||
#ifdef DEV
|
||||
detectAndProcessExceptionDumps();
|
||||
#endif
|
||||
|
||||
//Determine if this is a firmlaunch boot
|
||||
if(launchedFirmTidLow[5] != 0)
|
||||
{
|
||||
isFirmlaunch = true;
|
||||
|
||||
if(needConfig == CREATE_CONFIGURATION) mcuReboot();
|
||||
|
||||
//'0' = NATIVE_FIRM, '1' = TWL_FIRM, '2' = AGB_FIRM
|
||||
firmType = launchedFirmTidLow[7] == u'3' ? SAFE_FIRM : (FirmwareType)(launchedFirmTidLow[5] - u'0');
|
||||
|
||||
nandType = (FirmwareSource)BOOTCFG_NAND;
|
||||
firmSource = (FirmwareSource)BOOTCFG_FIRM;
|
||||
isA9lh = BOOTCFG_A9LH != 0;
|
||||
|
||||
#ifdef DEV
|
||||
if(isA9lh) installArm9Handlers();
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
isFirmlaunch = false;
|
||||
firmType = NATIVE_FIRM;
|
||||
|
||||
//Determine if booting with A9LH
|
||||
isA9lh = !PDN_SPI_CNT;
|
||||
|
||||
#ifdef DEV
|
||||
if(isA9lh) installArm9Handlers();
|
||||
#endif
|
||||
|
||||
//Get pressed buttons
|
||||
u32 pressed = HID_PAD;
|
||||
|
||||
//Save old options and begin saving the new boot configuration
|
||||
configTemp = (configData.config & 0xFFFFFE00) | ((u32)isA9lh << 6);
|
||||
|
||||
//If it's a MCU reboot, try to force boot options
|
||||
if(isA9lh && CFG_BOOTENV)
|
||||
{
|
||||
//Always force a sysNAND boot when quitting AGB_FIRM
|
||||
if(CFG_BOOTENV == 7)
|
||||
{
|
||||
nandType = FIRMWARE_SYSNAND;
|
||||
firmSource = CONFIG(USESYSFIRM) ? FIRMWARE_SYSNAND : (FirmwareSource)BOOTCFG_FIRM;
|
||||
needConfig = DONT_CONFIGURE;
|
||||
|
||||
//Flag to prevent multiple boot options-forcing
|
||||
configTemp |= 1 << 7;
|
||||
}
|
||||
|
||||
/* Else, force the last used boot options unless a button is pressed
|
||||
or the no-forcing flag is set */
|
||||
else if(needConfig != CREATE_CONFIGURATION && !pressed && !BOOTCFG_NOFORCEFLAG)
|
||||
{
|
||||
nandType = (FirmwareSource)BOOTCFG_NAND;
|
||||
firmSource = (FirmwareSource)BOOTCFG_FIRM;
|
||||
needConfig = DONT_CONFIGURE;
|
||||
}
|
||||
}
|
||||
|
||||
//Boot options aren't being forced
|
||||
if(needConfig != DONT_CONFIGURE)
|
||||
{
|
||||
bool pinExists = MULTICONFIG(PIN) != 0 && verifyPin();
|
||||
|
||||
//If no configuration file exists or SELECT is held, load configuration menu
|
||||
bool shouldLoadConfigMenu = needConfig == CREATE_CONFIGURATION || ((pressed & BUTTON_SELECT) && !(pressed & BUTTON_L1));
|
||||
|
||||
if(shouldLoadConfigMenu)
|
||||
{
|
||||
configMenu(pinExists);
|
||||
|
||||
//Update pressed buttons
|
||||
pressed = HID_PAD;
|
||||
}
|
||||
|
||||
if(isA9lh && !CFG_BOOTENV && pressed == SAFE_MODE)
|
||||
{
|
||||
nandType = FIRMWARE_SYSNAND;
|
||||
firmSource = FIRMWARE_SYSNAND;
|
||||
|
||||
//Flag to tell loader to init SD
|
||||
configTemp |= 1 << 8;
|
||||
|
||||
//If the PIN has been verified, wait to make it easier to press the SAFE_MODE combo
|
||||
if(pinExists && !shouldLoadConfigMenu)
|
||||
{
|
||||
while(HID_PAD & PIN_BUTTONS);
|
||||
chrono(2);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(CONFIG(PAYLOADSPLASH) && loadSplash()) pressed = HID_PAD;
|
||||
|
||||
/* If L and R/A/Select or one of the single payload buttons are pressed,
|
||||
chainload an external payload */
|
||||
bool shouldLoadPayload = ((pressed & SINGLE_PAYLOAD_BUTTONS) && !(pressed & (BUTTON_L1 | BUTTON_R1 | BUTTON_A))) ||
|
||||
((pressed & L_PAYLOAD_BUTTONS) && (pressed & BUTTON_L1));
|
||||
|
||||
if(shouldLoadPayload) loadPayload(pressed);
|
||||
|
||||
if(!CONFIG(PAYLOADSPLASH)) loadSplash();
|
||||
|
||||
//Determine if the user chose to use the SysNAND FIRM as default for a R boot
|
||||
bool useSysAsDefault = isA9lh ? CONFIG(USESYSFIRM) : false;
|
||||
|
||||
//If R is pressed, boot the non-updated NAND with the FIRM of the opposite one
|
||||
if(pressed & BUTTON_R1)
|
||||
{
|
||||
nandType = useSysAsDefault ? FIRMWARE_EMUNAND : FIRMWARE_SYSNAND;
|
||||
firmSource = useSysAsDefault ? FIRMWARE_SYSNAND : FIRMWARE_EMUNAND;
|
||||
}
|
||||
|
||||
/* Else, boot the NAND the user set to autoboot or the opposite one, depending on L,
|
||||
with their own FIRM */
|
||||
else
|
||||
{
|
||||
nandType = (CONFIG(AUTOBOOTSYS) != !(pressed & BUTTON_L1)) ? FIRMWARE_EMUNAND : FIRMWARE_SYSNAND;
|
||||
firmSource = nandType;
|
||||
}
|
||||
|
||||
//If we're booting EmuNAND or using EmuNAND FIRM, determine which one from the directional pad buttons, or otherwise from the config
|
||||
if(nandType == FIRMWARE_EMUNAND || firmSource == FIRMWARE_EMUNAND)
|
||||
{
|
||||
FirmwareSource temp;
|
||||
switch(pressed & EMUNAND_BUTTONS)
|
||||
{
|
||||
case BUTTON_UP:
|
||||
temp = FIRMWARE_EMUNAND;
|
||||
break;
|
||||
case BUTTON_RIGHT:
|
||||
temp = FIRMWARE_EMUNAND2;
|
||||
break;
|
||||
case BUTTON_DOWN:
|
||||
temp = FIRMWARE_EMUNAND3;
|
||||
break;
|
||||
case BUTTON_LEFT:
|
||||
temp = FIRMWARE_EMUNAND4;
|
||||
break;
|
||||
default:
|
||||
temp = (FirmwareSource)(1 + MULTICONFIG(DEFAULTEMU));
|
||||
break;
|
||||
}
|
||||
|
||||
if(nandType == FIRMWARE_EMUNAND) nandType = temp;
|
||||
else firmSource = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//If we need to boot EmuNAND, make sure it exists
|
||||
if(nandType != FIRMWARE_SYSNAND)
|
||||
{
|
||||
locateEmuNand(&emuHeader, &nandType);
|
||||
if(nandType == FIRMWARE_SYSNAND) firmSource = FIRMWARE_SYSNAND;
|
||||
}
|
||||
|
||||
//Same if we're using EmuNAND as the FIRM source
|
||||
else if(firmSource != FIRMWARE_SYSNAND)
|
||||
locateEmuNand(&emuHeader, &firmSource);
|
||||
|
||||
if(!isFirmlaunch)
|
||||
{
|
||||
configTemp |= (u32)nandType | ((u32)firmSource << 3);
|
||||
writeConfig(needConfig, configTemp);
|
||||
}
|
||||
|
||||
u32 firmVersion = loadFirm(&firmType, firmSource);
|
||||
|
||||
switch(firmType)
|
||||
{
|
||||
case NATIVE_FIRM:
|
||||
patchNativeFirm(firmVersion, nandType, emuHeader, isA9lh);
|
||||
break;
|
||||
case SAFE_FIRM:
|
||||
case NATIVE_FIRM1X2X:
|
||||
if(isA9lh) patch1x2xNativeAndSafeFirm();
|
||||
break;
|
||||
default:
|
||||
//Skip patching on unsupported O3DS AGB/TWL FIRMs
|
||||
if(isN3DS || firmVersion >= (firmType == TWL_FIRM ? 0x16 : 0xB)) patchLegacyFirm(firmType);
|
||||
break;
|
||||
}
|
||||
|
||||
launchFirm(firmType);
|
||||
}
|
||||
|
||||
#ifdef DEV
|
||||
static inline u32 loadFirm(FirmwareType *firmType, FirmwareSource firmSource)
|
||||
{
|
||||
section = firm->section;
|
||||
|
||||
const char *firmwareFiles[4] = {
|
||||
"/luma/firmware.bin",
|
||||
"/luma/firmware_twl.bin",
|
||||
"/luma/firmware_agb.bin",
|
||||
"/luma/firmware_safe.bin"
|
||||
const char *firmwareFiles[] = {
|
||||
"firmware.bin",
|
||||
"firmware_twl.bin",
|
||||
"firmware_agb.bin",
|
||||
"firmware_safe.bin",
|
||||
"firmware_sysupdater.bin"
|
||||
},
|
||||
*cetkFiles[] = {
|
||||
"cetk",
|
||||
"cetk_twl",
|
||||
"cetk_agb",
|
||||
"cetk_safe",
|
||||
"cetk_sysupdater"
|
||||
};
|
||||
|
||||
u32 firmSize = fileRead(firm, firmType == NATIVE_FIRM1X2X ? firmwareFiles[0] : firmwareFiles[(u32)firmType], 0x400000 + sizeof(Cxi) + 0x200);
|
||||
|
||||
if(!firmSize) return false;
|
||||
|
||||
if(firmSize <= sizeof(Cxi) + 0x200) error("The FIRM in /luma is not valid.");
|
||||
|
||||
if(memcmp(firm, "FIRM", 4) != 0)
|
||||
{
|
||||
u8 cetk[0xA50];
|
||||
|
||||
if(fileRead(cetk, firmType == NATIVE_FIRM1X2X ? cetkFiles[0] : cetkFiles[(u32)firmType], sizeof(cetk)) != sizeof(cetk) ||
|
||||
!decryptNusFirm((Ticket *)(cetk + 0x140), (Cxi *)firm, firmSize))
|
||||
error("The FIRM in /luma is encrypted or corrupted.");
|
||||
}
|
||||
|
||||
//Check that the FIRM is right for the console from the ARM9 section address
|
||||
if((firm->section[3].offset != 0 ? firm->section[3].address : firm->section[2].address) != (ISN3DS ? (u8 *)0x8006000 : (u8 *)0x8006800))
|
||||
error("The FIRM in /luma is not for this console.");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
u32 loadFirm(FirmwareType *firmType, FirmwareSource nandType, bool loadFromStorage, bool isSafeMode)
|
||||
{
|
||||
//Load FIRM from CTRNAND
|
||||
u32 firmVersion = firmRead(firm, (u32)*firmType);
|
||||
|
||||
bool loadFromSd = false;
|
||||
if(firmVersion == 0xFFFFFFFF) error("Failed to get the CTRNAND FIRM.");
|
||||
|
||||
if(!isN3DS && *firmType == NATIVE_FIRM)
|
||||
bool mustLoadFromStorage = false;
|
||||
|
||||
if(!ISN3DS && *firmType == NATIVE_FIRM && !ISDEVUNIT)
|
||||
{
|
||||
if(firmVersion < 0x18)
|
||||
{
|
||||
//We can't boot < 3.x EmuNANDs
|
||||
if(firmSource != FIRMWARE_SYSNAND)
|
||||
error("An old unsupported EmuNAND has been detected.\nLuma3DS is unable to boot it");
|
||||
if(nandType != FIRMWARE_SYSNAND)
|
||||
error("An old unsupported EmuNAND has been detected.\nLuma3DS is unable to boot it.");
|
||||
|
||||
if(BOOTCFG_SAFEMODE != 0) error("SAFE_MODE is not supported on 1.x/2.x FIRM");
|
||||
if(isSafeMode) error("SAFE_MODE is not supported on 1.x/2.x FIRM.");
|
||||
|
||||
*firmType = NATIVE_FIRM1X2X;
|
||||
}
|
||||
|
||||
//We can't boot a 3.x/4.x NATIVE_FIRM, load one from SD
|
||||
else if(firmVersion < 0x25) loadFromSd = true;
|
||||
//We can't boot a 3.x/4.x NATIVE_FIRM, load one from SD/CTRNAND
|
||||
else if(firmVersion < 0x25) mustLoadFromStorage = true;
|
||||
}
|
||||
|
||||
//Check that the SD FIRM is right for the console from the ARM9 section address
|
||||
if(fileRead(firm, *firmType == NATIVE_FIRM1X2X ? firmwareFiles[0] : firmwareFiles[(u32)*firmType], 0x400000) &&
|
||||
((section[3].offset ? section[3].address : section[2].address) == (isN3DS ? (u8 *)0x8006000 : (u8 *)0x8006800)))
|
||||
firmVersion = 0xFFFFFFFF;
|
||||
if((loadFromStorage || mustLoadFromStorage) && loadFirmFromStorage(*firmType)) firmVersion = 0xFFFFFFFF;
|
||||
else
|
||||
{
|
||||
if(loadFromSd) error("An old unsupported FIRM has been detected.\nCopy a valid firmware.bin in /luma to boot");
|
||||
decryptExeFs((u8 *)firm);
|
||||
if(mustLoadFromStorage) error("An old unsupported FIRM has been detected.\nCopy a firmware.bin in /luma to boot.");
|
||||
if(!decryptExeFs((Cxi *)firm)) error("The CTRNAND FIRM is corrupted.");
|
||||
if(ISDEVUNIT) firmVersion = 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
return firmVersion;
|
||||
}
|
||||
#else
|
||||
static inline u32 loadFirm(FirmwareType *firmType, FirmwareSource firmSource)
|
||||
|
||||
u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, u32 emuHeader, bool isSafeMode, bool doUnitinfoPatch, bool enableExceptionHandlers)
|
||||
{
|
||||
section = firm->section;
|
||||
u8 *arm9Section = (u8 *)firm + firm->section[2].offset,
|
||||
*arm11Section1 = (u8 *)firm + firm->section[1].offset;
|
||||
|
||||
//Load FIRM from CTRNAND
|
||||
u32 firmVersion = firmRead(firm, (u32)*firmType);
|
||||
|
||||
if(!isN3DS && *firmType == NATIVE_FIRM)
|
||||
if(ISN3DS)
|
||||
{
|
||||
if(firmVersion < 0x18)
|
||||
{
|
||||
//We can't boot < 3.x EmuNANDs
|
||||
if(firmSource != FIRMWARE_SYSNAND)
|
||||
error("An old unsupported EmuNAND has been detected.\nLuma3DS is unable to boot it");
|
||||
|
||||
if(BOOTCFG_SAFEMODE != 0) error("SAFE_MODE is not supported on 1.x/2.x FIRM");
|
||||
|
||||
*firmType = NATIVE_FIRM1X2X;
|
||||
}
|
||||
|
||||
//We can't boot a 3.x/4.x NATIVE_FIRM, load one from SD
|
||||
else if(firmVersion < 0x25)
|
||||
{
|
||||
if(!fileRead(firm, "/luma/firmware.bin", 0x400000) || section[2].address != (u8 *)0x8006800)
|
||||
error("An old unsupported FIRM has been detected.\nCopy a valid firmware.bin in /luma to boot");
|
||||
|
||||
firmVersion = 0xFFFFFFFF;
|
||||
}
|
||||
}
|
||||
|
||||
if(firmVersion != 0xFFFFFFFF) decryptExeFs((u8 *)firm);
|
||||
|
||||
return firmVersion;
|
||||
}
|
||||
#endif
|
||||
|
||||
static inline void patchNativeFirm(u32 firmVersion, FirmwareSource nandType, u32 emuHeader, bool isA9lh)
|
||||
{
|
||||
u8 *arm9Section = (u8 *)firm + section[2].offset,
|
||||
*arm11Section1 = (u8 *)firm + section[1].offset;
|
||||
|
||||
if(isN3DS)
|
||||
{
|
||||
//Decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader
|
||||
arm9Loader(arm9Section);
|
||||
//Decrypt ARM9Bin and patch ARM9 entrypoint to skip kernel9loader
|
||||
kernel9Loader((Arm9Bin *)arm9Section);
|
||||
firm->arm9Entry = (u8 *)0x801B01C;
|
||||
}
|
||||
|
||||
//Find the Process9 .code location, size and memory address
|
||||
u32 process9Size,
|
||||
process9MemAddr;
|
||||
u8 *process9Offset = getProcess9Info(arm9Section, firm->section[2].size, &process9Size, &process9MemAddr);
|
||||
|
||||
//Sets the 7.x NCCH KeyX and the 6.x gamecard save data KeyY on >= 6.0 O3DS FIRMs, if not using A9LH
|
||||
else if(!isA9lh && firmVersion >= 0x29) setRSAMod0DerivedKeys();
|
||||
//Find the Kernel11 SVC table and handler, exceptions page and free space locations
|
||||
u32 baseK11VA;
|
||||
u8 *freeK11Space;
|
||||
u32 *arm11SvcHandler,
|
||||
*arm11DAbtHandler,
|
||||
*arm11ExceptionsPage,
|
||||
*arm11SvcTable = getKernel11Info(arm11Section1, firm->section[1].size, &baseK11VA, &freeK11Space, &arm11SvcHandler, &arm11DAbtHandler, &arm11ExceptionsPage);
|
||||
|
||||
u32 kernel9Size = (u32)(process9Offset - arm9Section) - sizeof(Cxi) - 0x200,
|
||||
ret = 0;
|
||||
|
||||
//Apply signature patches
|
||||
ret += patchSignatureChecks(process9Offset, process9Size);
|
||||
|
||||
//Apply EmuNAND patches
|
||||
if(nandType != FIRMWARE_SYSNAND) ret += patchEmuNand(arm9Section, kernel9Size, process9Offset, process9Size, emuHeader, firm->section[2].address);
|
||||
|
||||
//Apply FIRM0/1 writes patches on SysNAND to protect A9LH
|
||||
else ret += patchFirmWrites(process9Offset, process9Size);
|
||||
|
||||
//Apply firmlaunch patches
|
||||
ret += patchFirmlaunches(process9Offset, process9Size, process9MemAddr);
|
||||
|
||||
//Apply dev unit check patches related to NCCH encryption
|
||||
if(!ISDEVUNIT)
|
||||
{
|
||||
ret += patchZeroKeyNcchEncryptionCheck(process9Offset, process9Size);
|
||||
ret += patchNandNcchEncryptionCheck(process9Offset, process9Size);
|
||||
}
|
||||
|
||||
//11.0 FIRM patches
|
||||
if(firmVersion >= (ISN3DS ? 0x21 : 0x52))
|
||||
{
|
||||
//Apply anti-anti-DG patches
|
||||
ret += patchTitleInstallMinVersionChecks(process9Offset, process9Size, firmVersion);
|
||||
|
||||
//Restore svcBackdoor
|
||||
ret += reimplementSvcBackdoor(arm11Section1, arm11SvcTable, baseK11VA, &freeK11Space);
|
||||
}
|
||||
|
||||
//Stub svc 0x59 on 11.3+ FIRMs
|
||||
if(firmVersion >= (ISN3DS ? 0x2D : 0x5C)) ret += stubSvcRestrictGpuDma(arm11Section1, arm11SvcTable, baseK11VA);
|
||||
|
||||
ret += implementSvcGetCFWInfo(arm11Section1, arm11SvcTable, baseK11VA, &freeK11Space, isSafeMode);
|
||||
|
||||
//Apply UNITINFO patches
|
||||
if(doUnitinfoPatch)
|
||||
{
|
||||
ret += patchUnitInfoValueSet(arm9Section, kernel9Size);
|
||||
if(!ISDEVUNIT) ret += patchCheckForDevCommonKey(process9Offset, process9Size);
|
||||
}
|
||||
|
||||
if(enableExceptionHandlers)
|
||||
{
|
||||
//ARM11 exception handlers
|
||||
u32 codeSetOffset,
|
||||
stackAddress = getInfoForArm11ExceptionHandlers(arm11Section1, firm->section[1].size, &codeSetOffset);
|
||||
ret += installArm11Handlers(arm11ExceptionsPage, stackAddress, codeSetOffset, arm11DAbtHandler, baseK11VA + ((u8 *)arm11DAbtHandler - arm11Section1));
|
||||
patchSvcBreak11(arm11Section1, arm11SvcTable, baseK11VA);
|
||||
ret += patchKernel11Panic(arm11Section1, firm->section[1].size);
|
||||
|
||||
//ARM9 exception handlers
|
||||
ret += patchArm9ExceptionHandlersInstall(arm9Section, kernel9Size);
|
||||
ret += patchSvcBreak9(arm9Section, kernel9Size, (u32)firm->section[2].address);
|
||||
ret += patchKernel9Panic(arm9Section, kernel9Size);
|
||||
}
|
||||
|
||||
bool patchAccess = CONFIG(PATCHACCESS),
|
||||
patchGames = CONFIG(PATCHGAMES);
|
||||
|
||||
if(patchAccess || patchGames)
|
||||
{
|
||||
ret += patchK11ModuleChecks(arm11Section1, firm->section[1].size, &freeK11Space, patchGames);
|
||||
|
||||
if(patchAccess)
|
||||
{
|
||||
ret += patchArm11SvcAccessChecks(arm11SvcHandler, (u32 *)(arm11Section1 + firm->section[1].size));
|
||||
ret += patchP9AccessChecks(process9Offset, process9Size);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
u32 patchTwlFirm(u32 firmVersion, bool doUnitinfoPatch)
|
||||
{
|
||||
u8 *arm9Section = (u8 *)firm + firm->section[3].offset;
|
||||
|
||||
//On N3DS, decrypt ARM9Bin and patch ARM9 entrypoint to skip kernel9loader
|
||||
if(ISN3DS)
|
||||
{
|
||||
kernel9Loader((Arm9Bin *)arm9Section);
|
||||
firm->arm9Entry = (u8 *)0x801301C;
|
||||
}
|
||||
|
||||
//Find the Process9 .code location, size and memory address
|
||||
u32 process9Size,
|
||||
process9MemAddr;
|
||||
u8 *process9Offset = getProcess9(arm9Section + 0x15000, section[2].size - 0x15000, &process9Size, &process9MemAddr);
|
||||
u8 *process9Offset = getProcess9Info(arm9Section, firm->section[3].size, &process9Size, &process9MemAddr);
|
||||
|
||||
#ifdef DEV
|
||||
//Find Kernel11 SVC table and handler, exceptions page and free space locations
|
||||
u32 baseK11VA;
|
||||
u8 *freeK11Space;
|
||||
u32 *arm11SvcHandler,
|
||||
*arm11ExceptionsPage,
|
||||
*arm11SvcTable = getKernel11Info(arm11Section1, section[1].size, &baseK11VA, &freeK11Space, &arm11SvcHandler, &arm11ExceptionsPage);
|
||||
#else
|
||||
//Find Kernel11 SVC table and free space locations
|
||||
u32 baseK11VA;
|
||||
u8 *freeK11Space;
|
||||
u32 *arm11SvcTable = getKernel11Info(arm11Section1, section[1].size, &baseK11VA, &freeK11Space);
|
||||
#endif
|
||||
u32 kernel9Size = (u32)(process9Offset - arm9Section) - sizeof(Cxi) - 0x200,
|
||||
ret = 0;
|
||||
|
||||
//Apply signature patches
|
||||
patchSignatureChecks(process9Offset, process9Size);
|
||||
ret += patchLgySignatureChecks(process9Offset, process9Size);
|
||||
ret += patchTwlInvalidSignatureChecks(process9Offset, process9Size);
|
||||
ret += patchTwlNintendoLogoChecks(process9Offset, process9Size);
|
||||
ret += patchTwlWhitelistChecks(process9Offset, process9Size);
|
||||
if(ISN3DS || firmVersion > 0x11) ret += patchTwlFlashcartChecks(process9Offset, process9Size, firmVersion);
|
||||
else if(!ISN3DS && firmVersion == 0x11) ret += patchOldTwlFlashcartChecks(process9Offset, process9Size);
|
||||
ret += patchTwlShaHashChecks(process9Offset, process9Size);
|
||||
|
||||
//Apply EmuNAND patches
|
||||
if(nandType != FIRMWARE_SYSNAND)
|
||||
{
|
||||
u32 branchAdditive = (u32)firm + section[2].offset - (u32)section[2].address;
|
||||
patchEmuNand(arm9Section, section[2].size, process9Offset, process9Size, emuHeader, branchAdditive);
|
||||
}
|
||||
|
||||
//Apply FIRM0/1 writes patches on sysNAND to protect A9LH
|
||||
else if(isA9lh) patchFirmWrites(process9Offset, process9Size);
|
||||
|
||||
//Apply firmlaunch patches
|
||||
patchFirmlaunches(process9Offset, process9Size, process9MemAddr);
|
||||
|
||||
//11.0 FIRM patches
|
||||
if(firmVersion >= (isN3DS ? 0x21 : 0x52))
|
||||
{
|
||||
//Apply anti-anti-DG patches
|
||||
patchTitleInstallMinVersionCheck(process9Offset, process9Size);
|
||||
|
||||
//Restore svcBackdoor
|
||||
reimplementSvcBackdoor(arm11Section1, arm11SvcTable, baseK11VA, &freeK11Space);
|
||||
}
|
||||
|
||||
implementSvcGetCFWInfo(arm11Section1, arm11SvcTable, baseK11VA, &freeK11Space);
|
||||
|
||||
#ifdef DEV
|
||||
//Apply UNITINFO patch
|
||||
if(MULTICONFIG(DEVOPTIONS) == 1) patchUnitInfoValueSet(arm9Section, section[2].size);
|
||||
if(doUnitinfoPatch) ret += patchUnitInfoValueSet(arm9Section, kernel9Size);
|
||||
|
||||
if(isA9lh && MULTICONFIG(DEVOPTIONS) != 2)
|
||||
{
|
||||
//Install ARM11 exception handlers
|
||||
u32 codeSetOffset;
|
||||
u32 stackAddress = getInfoForArm11ExceptionHandlers(arm11Section1, section[1].size, &codeSetOffset);
|
||||
installArm11Handlers(arm11ExceptionsPage, stackAddress, codeSetOffset);
|
||||
|
||||
//Kernel9/Process9 debugging
|
||||
patchArm9ExceptionHandlersInstall(arm9Section, section[2].size);
|
||||
patchSvcBreak9(arm9Section, section[2].size, (u32)section[2].address);
|
||||
patchKernel9Panic(arm9Section, section[2].size);
|
||||
|
||||
//Stub svcBreak11 with "bkpt 65535"
|
||||
patchSvcBreak11(arm11Section1, arm11SvcTable);
|
||||
|
||||
//Stub kernel11Panic with "bkpt 65534"
|
||||
patchKernel11Panic(arm11Section1, section[1].size);
|
||||
}
|
||||
|
||||
if(CONFIG(PATCHACCESS))
|
||||
{
|
||||
patchArm11SvcAccessChecks(arm11SvcHandler);
|
||||
patchK11ModuleChecks(arm11Section1, section[1].size, &freeK11Space);
|
||||
patchP9AccessChecks(process9Offset, process9Size);
|
||||
}
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline void patchLegacyFirm(FirmwareType firmType)
|
||||
u32 patchAgbFirm(bool doUnitinfoPatch)
|
||||
{
|
||||
u8 *arm9Section = (u8 *)firm + section[3].offset;
|
||||
|
||||
//On N3DS, decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader
|
||||
if(isN3DS)
|
||||
u8 *arm9Section = (u8 *)firm + firm->section[3].offset;
|
||||
|
||||
//On N3DS, decrypt ARM9Bin and patch ARM9 entrypoint to skip kernel9loader
|
||||
if(ISN3DS)
|
||||
{
|
||||
arm9Loader(arm9Section);
|
||||
kernel9Loader((Arm9Bin *)arm9Section);
|
||||
firm->arm9Entry = (u8 *)0x801301C;
|
||||
}
|
||||
|
||||
applyLegacyFirmPatches((u8 *)firm, firmType);
|
||||
//Find the Process9 .code location, size and memory address
|
||||
u32 process9Size,
|
||||
process9MemAddr;
|
||||
u8 *process9Offset = getProcess9Info(arm9Section, firm->section[3].size, &process9Size, &process9MemAddr);
|
||||
|
||||
u32 kernel9Size = (u32)(process9Offset - arm9Section) - sizeof(Cxi) - 0x200,
|
||||
ret = 0;
|
||||
|
||||
ret += patchLgySignatureChecks(process9Offset, process9Size);
|
||||
if(CONFIG(SHOWGBABOOT)) ret += patchAgbBootSplash(process9Offset, process9Size);
|
||||
|
||||
#ifdef DEV
|
||||
//Apply UNITINFO patch
|
||||
if(MULTICONFIG(DEVOPTIONS) == 1) patchUnitInfoValueSet(arm9Section, section[3].size);
|
||||
#endif
|
||||
if(doUnitinfoPatch) ret += patchUnitInfoValueSet(arm9Section, kernel9Size);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline void patch1x2xNativeAndSafeFirm(void)
|
||||
u32 patch1x2xNativeAndSafeFirm(bool enableExceptionHandlers)
|
||||
{
|
||||
u8 *arm9Section = (u8 *)firm + section[2].offset;
|
||||
u8 *arm9Section = (u8 *)firm + firm->section[2].offset;
|
||||
|
||||
if(isN3DS)
|
||||
if(ISN3DS)
|
||||
{
|
||||
//Decrypt ARM9Bin and patch ARM9 entrypoint to skip arm9loader
|
||||
arm9Loader(arm9Section);
|
||||
//Decrypt ARM9Bin and patch ARM9 entrypoint to skip kernel9loader
|
||||
kernel9Loader((Arm9Bin *)arm9Section);
|
||||
firm->arm9Entry = (u8 *)0x801B01C;
|
||||
|
||||
patchFirmWrites(arm9Section, section[2].size);
|
||||
}
|
||||
else patchOldFirmWrites(arm9Section, section[2].size);
|
||||
|
||||
#ifdef DEV
|
||||
if(MULTICONFIG(DEVOPTIONS) != 2)
|
||||
//Find the Process9 .code location, size and memory address
|
||||
u32 process9Size,
|
||||
process9MemAddr;
|
||||
u8 *process9Offset = getProcess9Info(arm9Section, firm->section[2].size, &process9Size, &process9MemAddr);
|
||||
|
||||
u32 kernel9Size = (u32)(process9Offset - arm9Section) - sizeof(Cxi) - 0x200,
|
||||
ret = 0;
|
||||
|
||||
ret += ISN3DS ? patchFirmWrites(process9Offset, process9Size) : patchOldFirmWrites(process9Offset, process9Size);
|
||||
|
||||
ret += ISN3DS ? patchSignatureChecks(process9Offset, process9Size) : patchOldSignatureChecks(process9Offset, process9Size);
|
||||
|
||||
if(enableExceptionHandlers)
|
||||
{
|
||||
//Kernel9/Process9 debugging
|
||||
patchArm9ExceptionHandlersInstall(arm9Section, section[2].size);
|
||||
patchSvcBreak9(arm9Section, section[2].size, (u32)section[2].address);
|
||||
//ARM9 exception handlers
|
||||
ret += patchArm9ExceptionHandlersInstall(arm9Section, kernel9Size);
|
||||
ret += patchSvcBreak9(arm9Section, kernel9Size, (u32)firm->section[2].address);
|
||||
}
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef DEV
|
||||
static inline void copySection0AndInjectSystemModules(FirmwareType firmType)
|
||||
static inline void copySection0AndInjectSystemModules(FirmwareType firmType, bool loadFromStorage)
|
||||
{
|
||||
u32 srcModuleSize,
|
||||
u32 maxModuleSize = firmType == NATIVE_FIRM ? 0x80000 : 0x600000,
|
||||
srcModuleSize,
|
||||
dstModuleSize;
|
||||
const char *extModuleSizeError = "The external FIRM modules are too large.";
|
||||
|
||||
for(u8 *src = (u8 *)firm + section[0].offset, *srcEnd = src + section[0].size, *dst = section[0].address;
|
||||
src < srcEnd; src += srcModuleSize, dst += dstModuleSize)
|
||||
for(u8 *src = (u8 *)firm + firm->section[0].offset, *srcEnd = src + firm->section[0].size, *dst = firm->section[0].address;
|
||||
src < srcEnd; src += srcModuleSize, dst += dstModuleSize, maxModuleSize -= dstModuleSize)
|
||||
{
|
||||
srcModuleSize = *(u32 *)(src + 0x104) * 0x200;
|
||||
const char *moduleName = (char *)(src + 0x200);
|
||||
srcModuleSize = ((Cxi *)src)->ncch.contentSize * 0x200;
|
||||
const char *moduleName = ((Cxi *)src)->exHeader.systemControlInfo.appTitle;
|
||||
|
||||
char fileName[30] = "/luma/sysmodules/";
|
||||
const char *ext = ".cxi";
|
||||
|
||||
//Read modules from files if they exist
|
||||
concatenateStrings(fileName, moduleName);
|
||||
concatenateStrings(fileName, ext);
|
||||
|
||||
u32 fileSize = fileRead(dst, fileName, 2 * srcModuleSize);
|
||||
if(fileSize) dstModuleSize = fileSize;
|
||||
else
|
||||
if(loadFromStorage)
|
||||
{
|
||||
const void *module;
|
||||
char fileName[24];
|
||||
|
||||
if(firmType == NATIVE_FIRM && memcmp(moduleName, "loader", 6) == 0)
|
||||
{
|
||||
module = injector;
|
||||
dstModuleSize = injector_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
module = src;
|
||||
dstModuleSize = srcModuleSize;
|
||||
}
|
||||
//Read modules from files if they exist
|
||||
sprintf(fileName, "sysmodules/%.8s.cxi", moduleName);
|
||||
|
||||
memcpy(dst, module, dstModuleSize);
|
||||
dstModuleSize = getFileSize(fileName);
|
||||
|
||||
if(dstModuleSize != 0)
|
||||
{
|
||||
if(dstModuleSize > maxModuleSize) error(extModuleSizeError);
|
||||
|
||||
if(dstModuleSize <= sizeof(Cxi) + 0x200 ||
|
||||
fileRead(dst, fileName, dstModuleSize) != dstModuleSize ||
|
||||
memcmp(((Cxi *)dst)->ncch.magic, "NCCH", 4) != 0 ||
|
||||
memcmp(moduleName, ((Cxi *)dst)->exHeader.systemControlInfo.appTitle, sizeof(((Cxi *)dst)->exHeader.systemControlInfo.appTitle)) != 0)
|
||||
error("An external FIRM module is invalid or corrupted.");
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
static inline void copySection0AndInjectSystemModules(void)
|
||||
{
|
||||
u32 srcModuleSize,
|
||||
dstModuleSize;
|
||||
|
||||
for(u8 *src = (u8 *)firm + section[0].offset, *srcEnd = src + section[0].size, *dst = section[0].address;
|
||||
src < srcEnd; src += srcModuleSize, dst += dstModuleSize)
|
||||
{
|
||||
srcModuleSize = *(u32 *)(src + 0x104) * 0x200;
|
||||
const char *moduleName = (const char *)(src + 0x200);
|
||||
const u8 *module;
|
||||
|
||||
const void *module;
|
||||
|
||||
if(memcmp(moduleName, "loader", 6) == 0)
|
||||
if(firmType == NATIVE_FIRM && memcmp(moduleName, "loader", 6) == 0)
|
||||
{
|
||||
module = injector;
|
||||
dstModuleSize = injector_size;
|
||||
module = injector_bin;
|
||||
dstModuleSize = injector_bin_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -551,48 +355,31 @@ static inline void copySection0AndInjectSystemModules(void)
|
||||
dstModuleSize = srcModuleSize;
|
||||
}
|
||||
|
||||
if(dstModuleSize > maxModuleSize) error(extModuleSizeError);
|
||||
|
||||
memcpy(dst, module, dstModuleSize);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static inline void launchFirm(FirmwareType firmType)
|
||||
void launchFirm(FirmwareType firmType, bool loadFromStorage)
|
||||
{
|
||||
#ifdef DEV
|
||||
//Allow module injection and/or inject 3ds_injector on new NATIVE_FIRMs and LGY FIRMs
|
||||
u32 sectionNum;
|
||||
if(firmType != SAFE_FIRM && firmType != NATIVE_FIRM1X2X)
|
||||
if(firmType == NATIVE_FIRM || (loadFromStorage && (firmType == TWL_FIRM || firmType == AGB_FIRM)))
|
||||
{
|
||||
copySection0AndInjectSystemModules(firmType);
|
||||
copySection0AndInjectSystemModules(firmType, loadFromStorage);
|
||||
sectionNum = 1;
|
||||
}
|
||||
else sectionNum = 0;
|
||||
#else
|
||||
//If we're booting NATIVE_FIRM, section0 needs to be copied separately to inject 3ds_injector
|
||||
u32 sectionNum;
|
||||
if(firmType == NATIVE_FIRM)
|
||||
{
|
||||
copySection0AndInjectSystemModules();
|
||||
sectionNum = 1;
|
||||
}
|
||||
else sectionNum = 0;
|
||||
#endif
|
||||
|
||||
//Copy FIRM sections to respective memory locations
|
||||
for(; sectionNum < 4 && section[sectionNum].size != 0; sectionNum++)
|
||||
memcpy(section[sectionNum].address, (u8 *)firm + section[sectionNum].offset, section[sectionNum].size);
|
||||
|
||||
//Determine the ARM11 entry to use
|
||||
vu32 *arm11;
|
||||
if(isFirmlaunch) arm11 = (u32 *)0x1FFFFFFC;
|
||||
else
|
||||
{
|
||||
deinitScreens();
|
||||
arm11 = (u32 *)0x1FFFFFF8;
|
||||
}
|
||||
for(; sectionNum < 4 && firm->section[sectionNum].size != 0; sectionNum++)
|
||||
memcpy(firm->section[sectionNum].address, (u8 *)firm + firm->section[sectionNum].offset, firm->section[sectionNum].size);
|
||||
|
||||
if(!isFirmlaunch) deinitScreens();
|
||||
|
||||
//Set ARM11 kernel entrypoint
|
||||
*arm11 = (u32)firm->arm11Entry;
|
||||
ARM11_CORE0_MAILBOX_ENTRYPOINT = (u32)firm->arm11Entry;
|
||||
|
||||
//Ensure that all memory transfers have completed and that the caches have been flushed
|
||||
flushEntireDCache();
|
||||
@@ -600,4 +387,4 @@ static inline void launchFirm(FirmwareType firmType)
|
||||
|
||||
//Final jump to ARM9 kernel
|
||||
((void (*)())firm->arm9Entry)();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,39 +23,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
#include "3dsheaders.h"
|
||||
|
||||
#define CFG_BOOTENV (*(vu32 *)0x10010000)
|
||||
#define CFG_UNITINFO (*(vu8 *)0x10010010)
|
||||
#define PDN_MPCORE_CFG (*(vu32 *)0x10140FFC)
|
||||
#define PDN_SPI_CNT (*(vu32 *)0x101401C0)
|
||||
static Firm *const firm = (Firm *const)0x24000000;
|
||||
|
||||
//FIRM Header layout
|
||||
typedef struct firmSectionHeader {
|
||||
u32 offset;
|
||||
u8 *address;
|
||||
u32 size;
|
||||
u32 procType;
|
||||
u8 hash[0x20];
|
||||
} firmSectionHeader;
|
||||
|
||||
typedef struct firmHeader {
|
||||
u32 magic;
|
||||
u32 reserved1;
|
||||
u8 *arm11Entry;
|
||||
u8 *arm9Entry;
|
||||
u8 reserved2[0x30];
|
||||
firmSectionHeader section[4];
|
||||
} firmHeader;
|
||||
|
||||
static inline u32 loadFirm(FirmwareType *firmType, FirmwareSource firmSource);
|
||||
static inline void patchNativeFirm(u32 firmVersion, FirmwareSource nandType, u32 emuHeader, bool isA9lh);
|
||||
static inline void patchLegacyFirm(FirmwareType firmType);
|
||||
static inline void patch1x2xNativeAndSafeFirm(void);
|
||||
|
||||
#ifdef DEV
|
||||
static inline void copySection0AndInjectSystemModules(FirmwareType firmType);
|
||||
#else
|
||||
static inline void copySection0AndInjectSystemModules(void);
|
||||
#endif
|
||||
|
||||
static inline void launchFirm(FirmwareType firmType);
|
||||
u32 loadFirm(FirmwareType *firmType, FirmwareSource nandType, bool loadFromStorage, bool isSafeMode);
|
||||
u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, u32 emuHeader, bool isSafeMode, bool doUnitinfoPatch, bool enableExceptionHandlers);
|
||||
u32 patchTwlFirm(u32 firmVersion, bool doUnitinfoPatch);
|
||||
u32 patchAgbFirm(bool doUnitinfoPatch);
|
||||
u32 patch1x2xNativeAndSafeFirm(bool enableExceptionHandlers);
|
||||
void launchFirm(FirmwareType firmType, bool loadFromStorage);
|
||||
|
||||
293
source/fmt.c
Normal file
293
source/fmt.c
Normal file
@@ -0,0 +1,293 @@
|
||||
/* File : barebones/ee_printf.c
|
||||
This file contains an implementation of ee_printf that only requires a method to output a char to a UART without pulling in library code.
|
||||
|
||||
This code is based on a file that contains the following:
|
||||
Copyright (C) 2002 Michael Ringgaard. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. Neither the name of the project nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
//TuxSH's changes: add support for 64-bit numbers, remove floating-point code
|
||||
|
||||
#include "strings.h"
|
||||
#include "fmt.h"
|
||||
|
||||
#define ZEROPAD (1<<0) //Pad with zero
|
||||
#define SIGN (1<<1) //Unsigned/signed long
|
||||
#define PLUS (1<<2) //Show plus
|
||||
#define SPACE (1<<3) //Spacer
|
||||
#define LEFT (1<<4) //Left justified
|
||||
#define HEX_PREP (1<<5) //0x
|
||||
#define UPPERCASE (1<<6) //'ABCDEF'
|
||||
|
||||
#define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
|
||||
|
||||
static s32 skipAtoi(const char **s)
|
||||
{
|
||||
s32 i = 0;
|
||||
|
||||
while(IS_DIGIT(**s)) i = i * 10 + *((*s)++) - '0';
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
static char *processNumber(char *str, s64 num, bool isHex, s32 size, s32 precision, u32 type)
|
||||
{
|
||||
char sign = 0;
|
||||
|
||||
if(type & SIGN)
|
||||
{
|
||||
if(num < 0)
|
||||
{
|
||||
sign = '-';
|
||||
num = -num;
|
||||
size--;
|
||||
}
|
||||
else if(type & PLUS)
|
||||
{
|
||||
sign = '+';
|
||||
size--;
|
||||
}
|
||||
else if(type & SPACE)
|
||||
{
|
||||
sign = ' ';
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
static const char *lowerDigits = "0123456789abcdef",
|
||||
*upperDigits = "0123456789ABCDEF";
|
||||
|
||||
s32 i = 0;
|
||||
char tmp[20];
|
||||
const char *dig = (type & UPPERCASE) ? upperDigits : lowerDigits;
|
||||
|
||||
if(num == 0)
|
||||
{
|
||||
if(precision != 0) tmp[i++] = '0';
|
||||
type &= ~HEX_PREP;
|
||||
}
|
||||
else
|
||||
{
|
||||
while(num != 0)
|
||||
{
|
||||
u64 base = isHex ? 16ULL : 10ULL;
|
||||
tmp[i++] = dig[(u64)num % base];
|
||||
num = (s64)((u64)num / base);
|
||||
}
|
||||
}
|
||||
|
||||
if(type & LEFT || precision != -1) type &= ~ZEROPAD;
|
||||
if(type & HEX_PREP && isHex) size -= 2;
|
||||
if(i > precision) precision = i;
|
||||
size -= precision;
|
||||
if(!(type & (ZEROPAD | LEFT))) while(size-- > 0) *str++ = ' ';
|
||||
if(sign) *str++ = sign;
|
||||
|
||||
if(type & HEX_PREP && isHex)
|
||||
{
|
||||
*str++ = '0';
|
||||
*str++ = 'x';
|
||||
}
|
||||
|
||||
if(type & ZEROPAD) while(size-- > 0) *str++ = '0';
|
||||
while(i < precision--) *str++ = '0';
|
||||
while(i-- > 0) *str++ = tmp[i];
|
||||
while(size-- > 0) *str++ = ' ';
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
u32 vsprintf(char *buf, const char *fmt, va_list args)
|
||||
{
|
||||
char *str;
|
||||
|
||||
for(str = buf; *fmt; fmt++)
|
||||
{
|
||||
if(*fmt != '%')
|
||||
{
|
||||
*str++ = *fmt;
|
||||
continue;
|
||||
}
|
||||
|
||||
//Process flags
|
||||
u32 flags = 0; //Flags to number()
|
||||
bool loop = true;
|
||||
|
||||
while(loop)
|
||||
{
|
||||
switch(*++fmt)
|
||||
{
|
||||
case '-': flags |= LEFT; break;
|
||||
case '+': flags |= PLUS; break;
|
||||
case ' ': flags |= SPACE; break;
|
||||
case '#': flags |= HEX_PREP; break;
|
||||
case '0': flags |= ZEROPAD; break;
|
||||
default: loop = false; break;
|
||||
}
|
||||
}
|
||||
|
||||
//Get field width
|
||||
s32 fieldWidth = -1; //Width of output field
|
||||
if(IS_DIGIT(*fmt)) fieldWidth = skipAtoi(&fmt);
|
||||
else if(*fmt == '*')
|
||||
{
|
||||
fmt++;
|
||||
|
||||
fieldWidth = va_arg(args, s32);
|
||||
|
||||
if(fieldWidth < 0)
|
||||
{
|
||||
fieldWidth = -fieldWidth;
|
||||
flags |= LEFT;
|
||||
}
|
||||
}
|
||||
|
||||
//Get the precision
|
||||
s32 precision = -1; //Min. # of digits for integers; max number of chars for from string
|
||||
if(*fmt == '.')
|
||||
{
|
||||
fmt++;
|
||||
|
||||
if(IS_DIGIT(*fmt)) precision = skipAtoi(&fmt);
|
||||
else if(*fmt == '*')
|
||||
{
|
||||
fmt++;
|
||||
precision = va_arg(args, s32);
|
||||
}
|
||||
|
||||
if(precision < 0) precision = 0;
|
||||
}
|
||||
|
||||
//Get the conversion qualifier
|
||||
u32 integerType = 0;
|
||||
if(*fmt == 'l')
|
||||
{
|
||||
if(*++fmt == 'l')
|
||||
{
|
||||
fmt++;
|
||||
integerType = 1;
|
||||
}
|
||||
|
||||
}
|
||||
else if(*fmt == 'h')
|
||||
{
|
||||
if(*++fmt == 'h')
|
||||
{
|
||||
fmt++;
|
||||
integerType = 3;
|
||||
}
|
||||
else integerType = 2;
|
||||
}
|
||||
|
||||
bool isHex;
|
||||
|
||||
switch(*fmt)
|
||||
{
|
||||
case 'c':
|
||||
if(!(flags & LEFT)) while(--fieldWidth > 0) *str++ = ' ';
|
||||
*str++ = (u8)va_arg(args, s32);
|
||||
while(--fieldWidth > 0) *str++ = ' ';
|
||||
continue;
|
||||
|
||||
case 's':
|
||||
{
|
||||
char *s = va_arg(args, char *);
|
||||
if(!s) s = "<NULL>";
|
||||
u32 len = (precision != -1) ? strnlen(s, precision) : strlen(s);
|
||||
if(!(flags & LEFT)) while((s32)len < fieldWidth--) *str++ = ' ';
|
||||
for(u32 i = 0; i < len; i++) *str++ = *s++;
|
||||
while((s32)len < fieldWidth--) *str++ = ' ';
|
||||
continue;
|
||||
}
|
||||
|
||||
case 'p':
|
||||
if(fieldWidth == -1)
|
||||
{
|
||||
fieldWidth = 8;
|
||||
flags |= ZEROPAD;
|
||||
}
|
||||
str = processNumber(str, va_arg(args, u32), true, fieldWidth, precision, flags);
|
||||
continue;
|
||||
|
||||
//Integer number formats - set up the flags and "break"
|
||||
case 'X':
|
||||
flags |= UPPERCASE;
|
||||
|
||||
case 'x':
|
||||
isHex = true;
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
case 'i':
|
||||
flags |= SIGN;
|
||||
|
||||
case 'u':
|
||||
isHex = false;
|
||||
break;
|
||||
|
||||
default:
|
||||
if(*fmt != '%') *str++ = '%';
|
||||
if(*fmt) *str++ = *fmt;
|
||||
else fmt--;
|
||||
continue;
|
||||
}
|
||||
|
||||
s64 num;
|
||||
|
||||
if(flags & SIGN)
|
||||
{
|
||||
if(integerType == 1) num = va_arg(args, s64);
|
||||
else num = va_arg(args, s32);
|
||||
|
||||
if(integerType == 2) num = (s16)num;
|
||||
else if(integerType == 3) num = (s8)num;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(integerType == 1) num = va_arg(args, u64);
|
||||
else num = va_arg(args, u32);
|
||||
|
||||
if(integerType == 2) num = (u16)num;
|
||||
else if(integerType == 3) num = (u8)num;
|
||||
}
|
||||
|
||||
str = processNumber(str, num, isHex, fieldWidth, precision, flags);
|
||||
}
|
||||
|
||||
*str = 0;
|
||||
return str - buf;
|
||||
}
|
||||
|
||||
u32 sprintf(char *buf, const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
u32 res = vsprintf(buf, fmt, args);
|
||||
va_end(args);
|
||||
return res;
|
||||
}
|
||||
6
source/fmt.h
Normal file
6
source/fmt.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include "memory.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
u32 vsprintf(char *buf, const char *fmt, va_list args);
|
||||
u32 sprintf(char *buf, const char *fmt, ...);
|
||||
392
source/fs.c
392
source/fs.c
@@ -23,19 +23,42 @@
|
||||
#include "fs.h"
|
||||
#include "memory.h"
|
||||
#include "strings.h"
|
||||
#include "fmt.h"
|
||||
#include "crypto.h"
|
||||
#include "cache.h"
|
||||
#include "screen.h"
|
||||
#include "draw.h"
|
||||
#include "utils.h"
|
||||
#include "config.h"
|
||||
#include "fatfs/ff.h"
|
||||
#include "buttons.h"
|
||||
#include "../build/loader.h"
|
||||
#include "firm.h"
|
||||
#include "crypto.h"
|
||||
#include "../build/bundled.h"
|
||||
|
||||
static FATFS sdFs,
|
||||
nandFs;
|
||||
|
||||
void mountFs(void)
|
||||
static bool switchToMainDir(bool isSd)
|
||||
{
|
||||
f_mount(&sdFs, "0:", 1);
|
||||
f_mount(&nandFs, "1:", 0);
|
||||
const char *mainDir = isSd ? "/luma" : "/rw/luma";
|
||||
|
||||
switch(f_chdir(mainDir))
|
||||
{
|
||||
case FR_OK:
|
||||
return true;
|
||||
case FR_NO_PATH:
|
||||
f_mkdir(mainDir);
|
||||
return switchToMainDir(isSd);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool mountFs(bool isSd, bool switchToCtrNand)
|
||||
{
|
||||
return isSd ? f_mount(&sdFs, "0:", 1) == FR_OK && switchToMainDir(true) :
|
||||
f_mount(&nandFs, "1:", 1) == FR_OK && (!switchToCtrNand || (f_chdrive("1:") == FR_OK && switchToMainDir(false)));
|
||||
}
|
||||
|
||||
u32 fileRead(void *dest, const char *path, u32 maxSize)
|
||||
@@ -43,14 +66,13 @@ u32 fileRead(void *dest, const char *path, u32 maxSize)
|
||||
FIL file;
|
||||
u32 ret = 0;
|
||||
|
||||
if(f_open(&file, path, FA_READ) == FR_OK)
|
||||
{
|
||||
u32 size = f_size(&file);
|
||||
if(dest == NULL) ret = size;
|
||||
else if(!(maxSize > 0 && size > maxSize))
|
||||
f_read(&file, dest, size, (unsigned int *)&ret);
|
||||
f_close(&file);
|
||||
}
|
||||
if(f_open(&file, path, FA_READ) != FR_OK) return ret;
|
||||
|
||||
u32 size = f_size(&file);
|
||||
if(dest == NULL) ret = size;
|
||||
else if(size <= maxSize)
|
||||
f_read(&file, dest, size, (unsigned int *)&ret);
|
||||
f_close(&file);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -64,32 +86,31 @@ bool fileWrite(const void *buffer, const char *path, u32 size)
|
||||
{
|
||||
FIL file;
|
||||
|
||||
FRESULT result = f_open(&file, path, FA_WRITE | FA_OPEN_ALWAYS);
|
||||
|
||||
if(result == FR_OK)
|
||||
switch(f_open(&file, path, FA_WRITE | FA_OPEN_ALWAYS))
|
||||
{
|
||||
unsigned int written;
|
||||
f_write(&file, buffer, size, &written);
|
||||
f_close(&file);
|
||||
case FR_OK:
|
||||
{
|
||||
unsigned int written;
|
||||
f_write(&file, buffer, size, &written);
|
||||
f_truncate(&file);
|
||||
f_close(&file);
|
||||
|
||||
return true;
|
||||
return (u32)written == size;
|
||||
}
|
||||
case FR_NO_PATH:
|
||||
for(u32 i = 1; path[i] != 0; i++)
|
||||
if(path[i] == '/')
|
||||
{
|
||||
char folder[i + 1];
|
||||
memcpy(folder, path, i);
|
||||
folder[i] = 0;
|
||||
f_mkdir(folder);
|
||||
}
|
||||
|
||||
return fileWrite(buffer, path, size);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
if(result == FR_NO_PATH)
|
||||
{
|
||||
for(u32 i = 1; path[i] != 0; i++)
|
||||
if(path[i] == '/')
|
||||
{
|
||||
char folder[i + 1];
|
||||
memcpy(folder, path, i);
|
||||
folder[i] = 0;
|
||||
f_mkdir(folder);
|
||||
}
|
||||
|
||||
return fileWrite(buffer, path, size);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void fileDelete(const char *path)
|
||||
@@ -97,87 +118,253 @@ void fileDelete(const char *path)
|
||||
f_unlink(path);
|
||||
}
|
||||
|
||||
void loadPayload(u32 pressed)
|
||||
static __attribute__((noinline)) bool overlaps(u32 as, u32 ae, u32 bs, u32 be)
|
||||
{
|
||||
const char *pattern;
|
||||
if (as <= bs && bs <= ae)
|
||||
return true;
|
||||
else if (bs <= as && as <= be)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if(pressed & BUTTON_LEFT) pattern = PATTERN("left");
|
||||
else if(pressed & BUTTON_RIGHT) pattern = PATTERN("right");
|
||||
else if(pressed & BUTTON_UP) pattern = PATTERN("up");
|
||||
else if(pressed & BUTTON_DOWN) pattern = PATTERN("down");
|
||||
else if(pressed & BUTTON_START) pattern = PATTERN("start");
|
||||
else if(pressed & BUTTON_B) pattern = PATTERN("b");
|
||||
else if(pressed & BUTTON_X) pattern = PATTERN("x");
|
||||
else if(pressed & BUTTON_Y) pattern = PATTERN("y");
|
||||
else if(pressed & BUTTON_R1) pattern = PATTERN("r");
|
||||
else if(pressed & BUTTON_A) pattern = PATTERN("a");
|
||||
else pattern = PATTERN("select");
|
||||
static bool checkFirmPayload(void)
|
||||
{
|
||||
if(memcmp(firm->magic, "FIRM", 4) != 0)
|
||||
return false;
|
||||
|
||||
if(firm->arm9Entry == NULL) //allow for the arm11 entrypoint to be zero in which case nothing is done on the arm11 side
|
||||
return false;
|
||||
|
||||
u32 size = 0x200;
|
||||
for(u32 i = 0; i < 4; i++)
|
||||
size += firm->section[i].size;
|
||||
|
||||
bool arm9EpFound = false, arm11EpFound = false;
|
||||
for(u32 i = 0; i < 4; i++)
|
||||
{
|
||||
__attribute__((aligned(4))) u8 hash[0x20];
|
||||
|
||||
FirmSection *section = &firm->section[i];
|
||||
|
||||
// allow empty sections
|
||||
if (section->size == 0)
|
||||
continue;
|
||||
|
||||
if(section->offset < 0x200)
|
||||
return false;
|
||||
|
||||
if(section->address + section->size < section->address) //overflow check
|
||||
return false;
|
||||
|
||||
if(((u32)section->address & 3) || (section->offset & 0x1FF) || (section->size & 0x1FF)) //alignment check
|
||||
return false;
|
||||
|
||||
if(overlaps((u32)section->address, (u32)section->address + section->size, 0x27FFE000, 0x28000000))
|
||||
return false;
|
||||
else if(overlaps((u32)section->address, (u32)section->address + section->size, 0x27FFE000 - 0x1000, 0x27FFE000))
|
||||
return false;
|
||||
else if(overlaps((u32)section->address, (u32)section->address + section->size, (u32)firm, (u32)firm + size))
|
||||
return false;
|
||||
|
||||
sha(hash, (u8 *)firm + section->offset, section->size, SHA_256_MODE);
|
||||
if(memcmp(hash, section->hash, 0x20) != 0)
|
||||
return false;
|
||||
|
||||
if(firm->arm9Entry >= section->address && firm->arm9Entry < (section->address + section->size))
|
||||
arm9EpFound = true;
|
||||
|
||||
if(firm->arm11Entry >= section->address && firm->arm11Entry < (section->address + section->size))
|
||||
arm11EpFound = true;
|
||||
}
|
||||
|
||||
return arm9EpFound && (firm->arm11Entry == NULL || arm11EpFound);
|
||||
}
|
||||
|
||||
void loadPayload(u32 pressed, const char *payloadPath)
|
||||
{
|
||||
u32 *loaderAddress = (u32 *)0x27FFE000;
|
||||
u32 payloadSize = 0,
|
||||
maxPayloadSize = (u32)((u8 *)loaderAddress - (u8 *)firm);
|
||||
|
||||
char absPath[24 + _MAX_LFN] = {0};
|
||||
char path[10 + _MAX_LFN] = {0};
|
||||
|
||||
if(payloadPath == NULL)
|
||||
{
|
||||
const char *pattern;
|
||||
|
||||
if(pressed & BUTTON_LEFT) pattern = PATTERN("left");
|
||||
else if(pressed & BUTTON_RIGHT) pattern = PATTERN("right");
|
||||
else if(pressed & BUTTON_UP) pattern = PATTERN("up");
|
||||
else if(pressed & BUTTON_DOWN) pattern = PATTERN("down");
|
||||
else if(pressed & BUTTON_START) pattern = PATTERN("start");
|
||||
else if(pressed & BUTTON_B) pattern = PATTERN("b");
|
||||
else if(pressed & BUTTON_X) pattern = PATTERN("x");
|
||||
else if(pressed & BUTTON_Y) pattern = PATTERN("y");
|
||||
else if(pressed & BUTTON_R1) pattern = PATTERN("r");
|
||||
else if(pressed & BUTTON_A) pattern = PATTERN("a");
|
||||
else pattern = PATTERN("select");
|
||||
|
||||
DIR dir;
|
||||
FILINFO info;
|
||||
FRESULT result;
|
||||
|
||||
result = f_findfirst(&dir, &info, "payloads", pattern);
|
||||
|
||||
if(result != FR_OK) return;
|
||||
|
||||
f_closedir(&dir);
|
||||
|
||||
if(!info.fname[0]) return;
|
||||
|
||||
sprintf(path, "payloads/%s", info.fname);
|
||||
|
||||
}
|
||||
else sprintf(path, "%s", payloadPath);
|
||||
|
||||
payloadSize = fileRead(firm, path, maxPayloadSize);
|
||||
|
||||
if(!payloadSize || !checkFirmPayload()) return;
|
||||
|
||||
writeConfig(true);
|
||||
|
||||
if(memcmp(launchedPath, u"nand", 8) == 0)
|
||||
sprintf(absPath, "nand:/rw/luma/%s", path);
|
||||
else
|
||||
sprintf(absPath, "sdmc:/luma/%s", path);
|
||||
|
||||
char *argv[1] = {absPath};
|
||||
memcpy(loaderAddress, loader_bin, loader_bin_size);
|
||||
|
||||
initScreens();
|
||||
|
||||
flushDCacheRange(loaderAddress, loader_bin_size);
|
||||
flushICacheRange(loaderAddress, loader_bin_size);
|
||||
|
||||
((void (*)(int, char **, u32))loaderAddress)(1, argv, 0x0000BEEF);
|
||||
}
|
||||
|
||||
void payloadMenu(void)
|
||||
{
|
||||
DIR dir;
|
||||
FILINFO info;
|
||||
char path[28] = "/luma/payloads";
|
||||
char path[62] = "payloads";
|
||||
|
||||
FRESULT result = f_findfirst(&dir, &info, path, pattern);
|
||||
if(f_opendir(&dir, path) != FR_OK) return;
|
||||
|
||||
FILINFO info;
|
||||
u32 payloadNum = 0;
|
||||
char payloadList[20][49];
|
||||
|
||||
while(f_readdir(&dir, &info) == FR_OK && info.fname[0] != 0 && payloadNum < 20)
|
||||
{
|
||||
if(info.fname[0] == '.') continue;
|
||||
|
||||
u32 nameLength = strlen(info.fname);
|
||||
|
||||
if(nameLength < 6 || nameLength > 52) continue;
|
||||
|
||||
nameLength -= 5;
|
||||
|
||||
if(memcmp(info.fname + nameLength, ".firm", 5) != 0) continue;
|
||||
|
||||
memcpy(payloadList[payloadNum], info.fname, nameLength);
|
||||
payloadList[payloadNum][nameLength] = 0;
|
||||
payloadNum++;
|
||||
}
|
||||
|
||||
f_closedir(&dir);
|
||||
|
||||
if(result == FR_OK && info.fname[0])
|
||||
if(!payloadNum) return;
|
||||
|
||||
u32 pressed = 0,
|
||||
selectedPayload = 0;
|
||||
|
||||
if(payloadNum != 1)
|
||||
{
|
||||
u32 *loaderAddress = (u32 *)0x24FFFF00;
|
||||
u8 *payloadAddress = (u8 *)0x24F00000;
|
||||
initScreens();
|
||||
|
||||
memcpy(loaderAddress, loader, loader_size);
|
||||
drawString(true, 10, 10, COLOR_TITLE, "Luma3DS chainloader");
|
||||
drawString(true, 10, 10 + SPACING_Y, COLOR_TITLE, "Press A to select, START to quit");
|
||||
|
||||
concatenateStrings(path, "/");
|
||||
concatenateStrings(path, info.altname);
|
||||
|
||||
u32 payloadSize = fileRead(payloadAddress, path, (u8 *)loaderAddress - payloadAddress);
|
||||
|
||||
if(payloadSize > 0)
|
||||
for(u32 i = 0, posY = 10 + 3 * SPACING_Y, color = COLOR_RED; i < payloadNum; i++, posY += SPACING_Y)
|
||||
{
|
||||
loaderAddress[1] = payloadSize;
|
||||
drawString(true, 10, posY, color, payloadList[i]);
|
||||
if(color == COLOR_RED) color = COLOR_WHITE;
|
||||
}
|
||||
|
||||
initScreens();
|
||||
while(pressed != BUTTON_A && pressed != BUTTON_START)
|
||||
{
|
||||
do
|
||||
{
|
||||
pressed = waitInput(true);
|
||||
}
|
||||
while(!(pressed & MENU_BUTTONS));
|
||||
|
||||
flushDCacheRange(loaderAddress, loader_size);
|
||||
flushICacheRange(loaderAddress, loader_size);
|
||||
u32 oldSelectedPayload = selectedPayload;
|
||||
|
||||
((void (*)())loaderAddress)();
|
||||
switch(pressed)
|
||||
{
|
||||
case BUTTON_UP:
|
||||
selectedPayload = !selectedPayload ? payloadNum - 1 : selectedPayload - 1;
|
||||
break;
|
||||
case BUTTON_DOWN:
|
||||
selectedPayload = selectedPayload == payloadNum - 1 ? 0 : selectedPayload + 1;
|
||||
break;
|
||||
case BUTTON_LEFT:
|
||||
selectedPayload = 0;
|
||||
break;
|
||||
case BUTTON_RIGHT:
|
||||
selectedPayload = payloadNum - 1;
|
||||
break;
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
|
||||
if(oldSelectedPayload == selectedPayload) continue;
|
||||
|
||||
drawString(true, 10, 10 + (3 + oldSelectedPayload) * SPACING_Y, COLOR_WHITE, payloadList[oldSelectedPayload]);
|
||||
drawString(true, 10, 10 + (3 + selectedPayload) * SPACING_Y, COLOR_RED, payloadList[selectedPayload]);
|
||||
}
|
||||
}
|
||||
|
||||
if(pressed != BUTTON_START)
|
||||
{
|
||||
sprintf(path, "payloads/%s.firm", payloadList[selectedPayload]);
|
||||
loadPayload(0, path);
|
||||
error("The payload is too large or corrupted.");
|
||||
}
|
||||
|
||||
while(HID_PAD & MENU_BUTTONS);
|
||||
wait(2000ULL);
|
||||
}
|
||||
|
||||
u32 firmRead(void *dest, u32 firmType)
|
||||
{
|
||||
const char *firmFolders[4][2] = {{ "00000002", "20000002" },
|
||||
{ "00000102", "20000102" },
|
||||
{ "00000202", "20000202" },
|
||||
{ "00000003", "20000003" }};
|
||||
const char *firmFolders[][2] = {{"00000002", "20000002"},
|
||||
{"00000102", "20000102"},
|
||||
{"00000202", "20000202"},
|
||||
{"00000003", "20000003"},
|
||||
{"00000001", "20000001"}};
|
||||
|
||||
char path[48] = "1:/title/00040138/";
|
||||
concatenateStrings(path, firmFolders[firmType][isN3DS ? 1 : 0]);
|
||||
concatenateStrings(path, "/content");
|
||||
char folderPath[35],
|
||||
path[48];
|
||||
|
||||
sprintf(folderPath, "1:/title/00040138/%s/content", firmFolders[firmType][ISN3DS ? 1 : 0]);
|
||||
|
||||
DIR dir;
|
||||
FILINFO info;
|
||||
|
||||
f_opendir(&dir, path);
|
||||
|
||||
u32 firmVersion = 0xFFFFFFFF;
|
||||
|
||||
if(f_opendir(&dir, folderPath) != FR_OK) goto exit;
|
||||
|
||||
FILINFO info;
|
||||
|
||||
//Parse the target directory
|
||||
while(f_readdir(&dir, &info) == FR_OK && info.fname[0])
|
||||
while(f_readdir(&dir, &info) == FR_OK && info.fname[0] != 0)
|
||||
{
|
||||
//Not a cxi
|
||||
if(info.fname[9] != 'a') continue;
|
||||
if(info.fname[9] != 'a' || strlen(info.fname) != 12) continue;
|
||||
|
||||
//Convert the .app name to an integer
|
||||
u32 tempVersion = 0;
|
||||
for(char *tmp = info.altname; *tmp != '.'; tmp++)
|
||||
{
|
||||
tempVersion <<= 4;
|
||||
tempVersion += *tmp > '9' ? *tmp - 'A' + 10 : *tmp - '0';
|
||||
}
|
||||
u32 tempVersion = hexAtoi(info.altname, 8);
|
||||
|
||||
//Found an older cxi
|
||||
if(tempVersion < firmVersion) firmVersion = tempVersion;
|
||||
@@ -185,36 +372,31 @@ u32 firmRead(void *dest, u32 firmType)
|
||||
|
||||
f_closedir(&dir);
|
||||
|
||||
if(firmVersion == 0xFFFFFFFF) goto exit;
|
||||
|
||||
//Complete the string with the .app name
|
||||
concatenateStrings(path, "/00000000.app");
|
||||
sprintf(path, "%s/%08x.app", folderPath, firmVersion);
|
||||
|
||||
//Convert back the .app name from integer to array
|
||||
hexItoa(firmVersion, &path[35], 8);
|
||||
|
||||
fileRead(dest, path, 0);
|
||||
if(fileRead(dest, path, 0x400000 + sizeof(Cxi) + 0x200) <= sizeof(Cxi) + 0x200) firmVersion = 0xFFFFFFFF;
|
||||
|
||||
exit:
|
||||
return firmVersion;
|
||||
}
|
||||
|
||||
#ifdef DEV
|
||||
void findDumpFile(const char *path, char *fileName)
|
||||
void findDumpFile(const char *folderPath, char *fileName)
|
||||
{
|
||||
DIR dir;
|
||||
FILINFO info;
|
||||
u32 n = 0;
|
||||
FRESULT result;
|
||||
|
||||
while(f_findfirst(&dir, &info, path, fileName) == FR_OK && info.fname[0])
|
||||
for(u32 n = 0; n <= 99999999; n++)
|
||||
{
|
||||
u32 i = 18,
|
||||
tmp = ++n;
|
||||
FILINFO info;
|
||||
|
||||
while(tmp > 0)
|
||||
{
|
||||
fileName[i--] = '0' + (tmp % 10);
|
||||
tmp /= 10;
|
||||
}
|
||||
sprintf(fileName, "crash_dump_%08u.dmp", n);
|
||||
result = f_findfirst(&dir, &info, folderPath, fileName);
|
||||
|
||||
if(result != FR_OK || !info.fname[0]) break;
|
||||
}
|
||||
|
||||
f_closedir(&dir);
|
||||
if(result == FR_OK) f_closedir(&dir);
|
||||
}
|
||||
#endif
|
||||
14
source/fs.h
14
source/fs.h
@@ -24,18 +24,14 @@
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#define PATTERN(a) a "_*.bin"
|
||||
#define PATTERN(a) a "_*.firm"
|
||||
|
||||
extern bool isN3DS;
|
||||
|
||||
void mountFs(void);
|
||||
bool mountFs(bool isSd, bool switchToCtrNand);
|
||||
u32 fileRead(void *dest, const char *path, u32 maxSize);
|
||||
u32 getFileSize(const char *path);
|
||||
bool fileWrite(const void *buffer, const char *path, u32 size);
|
||||
void fileDelete(const char *path);
|
||||
void loadPayload(u32 pressed);
|
||||
void loadPayload(u32 pressed, const char *payloadPath);
|
||||
void payloadMenu(void);
|
||||
u32 firmRead(void *dest, u32 firmType);
|
||||
|
||||
#ifdef DEV
|
||||
void findDumpFile(const char *path, char *fileName);
|
||||
#endif
|
||||
void findDumpFile(const char *folderPath, char *fileName);
|
||||
|
||||
25
source/i2c.c
25
source/i2c.c
@@ -115,6 +115,31 @@ static bool i2cSelectRegister(u8 bus_id, u8 reg)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
u8 i2cReadRegister(u8 dev_id, u8 reg)
|
||||
{
|
||||
u8 bus_id = i2cGetDeviceBusId(dev_id),
|
||||
dev_addr = i2cGetDeviceRegAddr(dev_id);
|
||||
|
||||
for(u32 i = 0; i < 8; i++)
|
||||
{
|
||||
if(i2cSelectDevice(bus_id, dev_addr) && i2cSelectRegister(bus_id, reg))
|
||||
{
|
||||
if(i2cSelectDevice(bus_id, dev_addr | 1))
|
||||
{
|
||||
i2cWaitBusy(bus_id);
|
||||
i2cStop(bus_id, 1);
|
||||
i2cWaitBusy(bus_id);
|
||||
|
||||
return *i2cGetDataReg(bus_id);
|
||||
}
|
||||
}
|
||||
*i2cGetCntReg(bus_id) = 0xC5;
|
||||
i2cWaitBusy(bus_id);
|
||||
}
|
||||
|
||||
return 0xFF;
|
||||
}
|
||||
|
||||
bool i2cWriteRegister(u8 dev_id, u8 reg, u8 data)
|
||||
{
|
||||
u8 bus_id = i2cGetDeviceBusId(dev_id),
|
||||
|
||||
@@ -41,4 +41,5 @@
|
||||
#define I2C_DEV_GYRO 10
|
||||
#define I2C_DEV_IR 13
|
||||
|
||||
u8 i2cReadRegister(u8 dev_id, u8 reg);
|
||||
bool i2cWriteRegister(u8 dev_id, u8 reg, u8 data);
|
||||
323
source/main.c
Normal file
323
source/main.c
Normal file
@@ -0,0 +1,323 @@
|
||||
/*
|
||||
* This file is part of Luma3DS
|
||||
* Copyright (C) 2016 Aurora Wright, TuxSH
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
|
||||
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
|
||||
* Notices displayed by works containing it.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "emunand.h"
|
||||
#include "fs.h"
|
||||
#include "firm.h"
|
||||
#include "utils.h"
|
||||
#include "exceptions.h"
|
||||
#include "draw.h"
|
||||
#include "strings.h"
|
||||
#include "buttons.h"
|
||||
#include "pin.h"
|
||||
#include "crypto.h"
|
||||
#include "fmt.h"
|
||||
#include "memory.h"
|
||||
|
||||
extern CfgData configData;
|
||||
extern ConfigurationStatus needConfig;
|
||||
extern FirmwareSource firmSource;
|
||||
|
||||
bool isFirmlaunch;
|
||||
u16 launchedPath[41];
|
||||
|
||||
void main(int argc, char **argv)
|
||||
{
|
||||
bool isSafeMode = false,
|
||||
isNoForceFlagSet = false;
|
||||
char errbuf[46];
|
||||
u32 emuHeader;
|
||||
FirmwareType firmType;
|
||||
FirmwareSource nandType;
|
||||
|
||||
switch(argc)
|
||||
{
|
||||
case 0:
|
||||
error("Unsupported launcher (argc = 0).");
|
||||
break;
|
||||
|
||||
case 1: //Normal boot
|
||||
{
|
||||
u32 i;
|
||||
for(i = 0; i < 40 && argv[0][i] != 0; i++) //Copy and convert the path to utf16
|
||||
launchedPath[i] = argv[0][i];
|
||||
for(; i < 41; i++)
|
||||
launchedPath[i] = 0;
|
||||
|
||||
isFirmlaunch = false;
|
||||
break;
|
||||
}
|
||||
|
||||
case 2: //Firmlaunch
|
||||
{
|
||||
u32 i;
|
||||
u16 *p = (u16 *)argv[0];
|
||||
for(i = 0; i < 40 && p[i] != 0; i++)
|
||||
launchedPath[i] = p[i];
|
||||
for(; i < 41; i++)
|
||||
launchedPath[i] = 0;
|
||||
|
||||
isFirmlaunch = true;
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
sprintf(errbuf, "Unsupported launcher (argc = %d).", argc);
|
||||
error(errbuf);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//Mount SD or CTRNAND
|
||||
bool isSdMode;
|
||||
|
||||
if(memcmp(launchedPath, u"sdmc", 8) == 0)
|
||||
{
|
||||
if(!mountFs(true, false)) error("Failed to mount SD.");
|
||||
isSdMode = true;
|
||||
}
|
||||
else if(memcmp(launchedPath, u"nand", 8) == 0)
|
||||
{
|
||||
firmSource = FIRMWARE_SYSNAND;
|
||||
if(!mountFs(false, true)) error("Failed to mount SD and CTRNAND.");
|
||||
isSdMode = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
char mountPoint[5] = {0};
|
||||
for(u32 i = 0; i < 4 && launchedPath[i] != u':'; i++)
|
||||
mountPoint[i] = (char)launchedPath[i];
|
||||
sprintf(errbuf, "Launched from an unsupported location: %s.", mountPoint);
|
||||
error(errbuf);
|
||||
}
|
||||
|
||||
//Attempt to read the configuration file
|
||||
needConfig = readConfig() ? MODIFY_CONFIGURATION : CREATE_CONFIGURATION;
|
||||
|
||||
//Determine if this is a firmlaunch boot
|
||||
if(isFirmlaunch)
|
||||
{
|
||||
if(needConfig == CREATE_CONFIGURATION) mcuPowerOff();
|
||||
|
||||
switch(argv[1][14])
|
||||
{
|
||||
case '2':
|
||||
firmType = (FirmwareType)(argv[1][10] - '0');
|
||||
break;
|
||||
case '3':
|
||||
firmType = SAFE_FIRM;
|
||||
break;
|
||||
case '1':
|
||||
firmType = SYSUPDATER_FIRM;
|
||||
break;
|
||||
}
|
||||
|
||||
nandType = (FirmwareSource)BOOTCFG_NAND;
|
||||
firmSource = (FirmwareSource)BOOTCFG_FIRM;
|
||||
|
||||
goto boot;
|
||||
}
|
||||
|
||||
detectAndProcessExceptionDumps();
|
||||
installArm9Handlers();
|
||||
|
||||
firmType = NATIVE_FIRM;
|
||||
|
||||
//Get pressed buttons
|
||||
u32 pressed = HID_PAD;
|
||||
|
||||
//If it's a MCU reboot, try to force boot options
|
||||
if(CFG_BOOTENV && needConfig != CREATE_CONFIGURATION)
|
||||
{
|
||||
|
||||
//Always force a SysNAND boot when quitting AGB_FIRM
|
||||
if(CFG_BOOTENV == 7)
|
||||
{
|
||||
nandType = FIRMWARE_SYSNAND;
|
||||
firmSource = (BOOTCFG_NAND != 0) == (BOOTCFG_FIRM != 0) ? FIRMWARE_SYSNAND : (FirmwareSource)BOOTCFG_FIRM;
|
||||
|
||||
//Prevent multiple boot options-forcing
|
||||
isNoForceFlagSet = true;
|
||||
|
||||
goto boot;
|
||||
}
|
||||
|
||||
/* Else, force the last used boot options unless a button is pressed
|
||||
or the no-forcing flag is set */
|
||||
if(!pressed && !BOOTCFG_NOFORCEFLAG)
|
||||
{
|
||||
nandType = (FirmwareSource)BOOTCFG_NAND;
|
||||
firmSource = (FirmwareSource)BOOTCFG_FIRM;
|
||||
|
||||
goto boot;
|
||||
}
|
||||
}
|
||||
|
||||
u32 pinMode = MULTICONFIG(PIN);
|
||||
bool pinExists = pinMode != 0 && verifyPin(pinMode);
|
||||
|
||||
//If no configuration file exists or SELECT is held, load configuration menu
|
||||
bool shouldLoadConfigMenu = needConfig == CREATE_CONFIGURATION || ((pressed & (BUTTON_SELECT | BUTTON_L1)) == BUTTON_SELECT);
|
||||
|
||||
if(shouldLoadConfigMenu)
|
||||
{
|
||||
configMenu(isSdMode, pinExists, pinMode);
|
||||
|
||||
//Update pressed buttons
|
||||
pressed = HID_PAD;
|
||||
}
|
||||
|
||||
if(!CFG_BOOTENV && pressed == SAFE_MODE)
|
||||
{
|
||||
nandType = FIRMWARE_SYSNAND;
|
||||
firmSource = FIRMWARE_SYSNAND;
|
||||
|
||||
isSafeMode = true;
|
||||
|
||||
//If the PIN has been verified, wait to make it easier to press the SAFE_MODE combo
|
||||
if(pinExists && !shouldLoadConfigMenu)
|
||||
{
|
||||
while(HID_PAD & PIN_BUTTONS);
|
||||
wait(2000ULL);
|
||||
}
|
||||
|
||||
goto boot;
|
||||
}
|
||||
|
||||
u32 splashMode = MULTICONFIG(SPLASH);
|
||||
|
||||
if(splashMode == 1 && loadSplash()) pressed = HID_PAD;
|
||||
|
||||
if((pressed & (BUTTON_START | BUTTON_L1)) == BUTTON_START)
|
||||
{
|
||||
payloadMenu();
|
||||
pressed = HID_PAD;
|
||||
}
|
||||
else if(((pressed & SINGLE_PAYLOAD_BUTTONS) && !(pressed & (BUTTON_L1 | BUTTON_R1 | BUTTON_A))) ||
|
||||
((pressed & L_PAYLOAD_BUTTONS) && (pressed & BUTTON_L1))) loadPayload(pressed, NULL);
|
||||
|
||||
if(splashMode == 2) loadSplash();
|
||||
|
||||
//If booting from CTRNAND, always use SysNAND
|
||||
if(!isSdMode) nandType = FIRMWARE_SYSNAND;
|
||||
|
||||
//If R is pressed, boot the non-updated NAND with the FIRM of the opposite one
|
||||
else if(pressed & BUTTON_R1)
|
||||
{
|
||||
if(CONFIG(USEEMUFIRM))
|
||||
{
|
||||
nandType = FIRMWARE_SYSNAND;
|
||||
firmSource = FIRMWARE_EMUNAND;
|
||||
}
|
||||
else
|
||||
{
|
||||
nandType = FIRMWARE_EMUNAND;
|
||||
firmSource = FIRMWARE_SYSNAND;
|
||||
}
|
||||
}
|
||||
|
||||
/* Else, boot the NAND the user set to autoboot or the opposite one, depending on L,
|
||||
with their own FIRM */
|
||||
else firmSource = nandType = (CONFIG(AUTOBOOTEMU) == ((pressed & BUTTON_L1) == BUTTON_L1)) ? FIRMWARE_SYSNAND : FIRMWARE_EMUNAND;
|
||||
|
||||
//If we're booting EmuNAND or using EmuNAND FIRM, determine which one from the directional pad buttons, or otherwise from the config
|
||||
if(nandType == FIRMWARE_EMUNAND || firmSource == FIRMWARE_EMUNAND)
|
||||
{
|
||||
FirmwareSource tempNand;
|
||||
switch(pressed & DPAD_BUTTONS)
|
||||
{
|
||||
case BUTTON_UP:
|
||||
tempNand = FIRMWARE_EMUNAND;
|
||||
break;
|
||||
case BUTTON_RIGHT:
|
||||
tempNand = FIRMWARE_EMUNAND2;
|
||||
break;
|
||||
case BUTTON_DOWN:
|
||||
tempNand = FIRMWARE_EMUNAND3;
|
||||
break;
|
||||
case BUTTON_LEFT:
|
||||
tempNand = FIRMWARE_EMUNAND4;
|
||||
break;
|
||||
default:
|
||||
tempNand = (FirmwareSource)(1 + MULTICONFIG(DEFAULTEMU));
|
||||
break;
|
||||
}
|
||||
|
||||
if(nandType == FIRMWARE_EMUNAND) nandType = tempNand;
|
||||
else firmSource = tempNand;
|
||||
}
|
||||
|
||||
boot:
|
||||
|
||||
//If we need to boot EmuNAND, make sure it exists
|
||||
if(nandType != FIRMWARE_SYSNAND)
|
||||
{
|
||||
locateEmuNand(&emuHeader, &nandType);
|
||||
if(nandType == FIRMWARE_SYSNAND) firmSource = FIRMWARE_SYSNAND;
|
||||
}
|
||||
|
||||
//Same if we're using EmuNAND as the FIRM source
|
||||
else if(firmSource != FIRMWARE_SYSNAND)
|
||||
locateEmuNand(&emuHeader, &firmSource);
|
||||
|
||||
if(!isFirmlaunch)
|
||||
{
|
||||
configData.config = (configData.config & 0xFFFFFF80) | ((u32)isNoForceFlagSet << 6) | ((u32)firmSource << 3) | (u32)nandType;
|
||||
writeConfig(false);
|
||||
}
|
||||
|
||||
if(isSdMode && !mountFs(false, false)) error("Failed to mount CTRNAND.");
|
||||
|
||||
bool loadFromStorage = CONFIG(LOADEXTFIRMSANDMODULES);
|
||||
u32 firmVersion = loadFirm(&firmType, firmSource, loadFromStorage, isSafeMode);
|
||||
|
||||
bool doUnitinfoPatch = CONFIG(PATCHUNITINFO),
|
||||
enableExceptionHandlers = CONFIG(ENABLEEXCEPTIONHANDLERS);
|
||||
u32 res;
|
||||
switch(firmType)
|
||||
{
|
||||
case NATIVE_FIRM:
|
||||
res = patchNativeFirm(firmVersion, nandType, emuHeader, isSafeMode, doUnitinfoPatch, enableExceptionHandlers);
|
||||
break;
|
||||
case TWL_FIRM:
|
||||
res = patchTwlFirm(firmVersion, doUnitinfoPatch);
|
||||
break;
|
||||
case AGB_FIRM:
|
||||
res = patchAgbFirm(doUnitinfoPatch);
|
||||
break;
|
||||
case SAFE_FIRM:
|
||||
case SYSUPDATER_FIRM:
|
||||
case NATIVE_FIRM1X2X:
|
||||
res = patch1x2xNativeAndSafeFirm(enableExceptionHandlers);
|
||||
break;
|
||||
}
|
||||
|
||||
if(res != 0)
|
||||
{
|
||||
sprintf(errbuf, "Failed to apply %u FIRM patch(es).", res);
|
||||
error(errbuf);
|
||||
}
|
||||
|
||||
launchFirm(firmType, loadFromStorage);
|
||||
}
|
||||
@@ -36,6 +36,15 @@ void memcpy(void *dest, const void *src, u32 size)
|
||||
destc[i] = srcc[i];
|
||||
}
|
||||
|
||||
void *memset(void *dest, u32 value, u32 size)
|
||||
{
|
||||
u8 *destc = (u8 *)dest;
|
||||
|
||||
for(u32 i = 0; i < size; i++) destc[i] = (u8)value;
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
void memset32(void *dest, u32 filler, u32 size)
|
||||
{
|
||||
u32 *dest32 = (u32 *)dest;
|
||||
@@ -80,4 +89,4 @@ u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize)
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "types.h"
|
||||
|
||||
void memcpy(void *dest, const void *src, u32 size);
|
||||
void *memset(void *dest, u32 value, u32 size) __attribute__((used));
|
||||
void memset32(void *dest, u32 filler, u32 size);
|
||||
int memcmp(const void *buf1, const void *buf2, u32 size);
|
||||
u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize);
|
||||
u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize);
|
||||
|
||||
575
source/patches.c
575
source/patches.c
@@ -21,164 +21,288 @@
|
||||
*/
|
||||
|
||||
/*
|
||||
* Signature patches by an unknown author
|
||||
* Signature patches for old FIRMs by SciresM
|
||||
* firmlaunches patching code originally by delebile
|
||||
* FIRM partition writes patches by delebile
|
||||
* ARM11 modules patching code originally by Subv
|
||||
* Idea for svcBreak patches from yellows8 and others on #3dsdev
|
||||
* TWL_FIRM patches by Steveice10 and others
|
||||
*/
|
||||
|
||||
#include "patches.h"
|
||||
#include "fs.h"
|
||||
#include "memory.h"
|
||||
#include "config.h"
|
||||
#include "../build/rebootpatch.h"
|
||||
#include "../build/svcGetCFWInfopatch.h"
|
||||
#include "utils.h"
|
||||
#include "../build/bundled.h"
|
||||
|
||||
#ifdef DEV
|
||||
#include "../build/k11modulespatch.h"
|
||||
#endif
|
||||
|
||||
u8 *getProcess9(u8 *pos, u32 size, u32 *process9Size, u32 *process9MemAddr)
|
||||
u8 *getProcess9Info(u8 *pos, u32 size, u32 *process9Size, u32 *process9MemAddr)
|
||||
{
|
||||
u8 *off = memsearch(pos, "ess9", size, 4);
|
||||
u8 *temp = memsearch(pos, "NCCH", size, 4);
|
||||
|
||||
*process9Size = *(u32 *)(off - 0x60) * 0x200;
|
||||
*process9MemAddr = *(u32 *)(off + 0xC);
|
||||
if(temp == NULL) error("Failed to get Process9 data.");
|
||||
|
||||
//Process9 code offset (start of NCCH + ExeFS offset + ExeFS header size)
|
||||
return off - 0x204 + (*(u32 *)(off - 0x64) * 0x200) + 0x200;
|
||||
Cxi *off = (Cxi *)(temp - 0x100);
|
||||
|
||||
*process9Size = (off->ncch.exeFsSize - 1) * 0x200;
|
||||
*process9MemAddr = off->exHeader.systemControlInfo.textCodeSet.address;
|
||||
|
||||
return (u8 *)off + (off->ncch.exeFsOffset + 1) * 0x200;
|
||||
}
|
||||
|
||||
#ifdef DEV
|
||||
u32 *getKernel11Info(u8 *pos, u32 size, u32 *baseK11VA, u8 **freeK11Space, u32 **arm11SvcHandler, u32 **arm11ExceptionsPage)
|
||||
{
|
||||
const u8 pattern[] = {0x00, 0xB0, 0x9C, 0xE5};
|
||||
u32 *getKernel11Info(u8 *pos, u32 size, u32 *baseK11VA, u8 **freeK11Space, u32 **arm11SvcHandler, u32 **arm11DAbtHandler, u32 **arm11ExceptionsPage)
|
||||
{
|
||||
const u8 pattern[] = {0x00, 0xB0, 0x9C, 0xE5},
|
||||
pattern2[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
|
||||
*arm11ExceptionsPage = (u32 *)memsearch(pos, pattern, size, sizeof(pattern)) - 0xB;
|
||||
*arm11ExceptionsPage = (u32 *)memsearch(pos, pattern, size, sizeof(pattern));
|
||||
*freeK11Space = memsearch(pos, pattern2, size, sizeof(pattern2));
|
||||
|
||||
if(*arm11ExceptionsPage == NULL || *freeK11Space == NULL) error("Failed to get Kernel11 data.");
|
||||
|
||||
u32 *arm11SvcTable;
|
||||
|
||||
*arm11ExceptionsPage -= 0xB;
|
||||
u32 svcOffset = (-(((*arm11ExceptionsPage)[2] & 0xFFFFFF) << 2) & (0xFFFFFF << 2)) - 8; //Branch offset + 8 for prefetch
|
||||
u32 dabtOffset = (-(((*arm11ExceptionsPage)[4] & 0xFFFFFF) << 2) & (0xFFFFFF << 2)) - 8; //Branch offset + 8 for prefetch
|
||||
u32 pointedInstructionVA = 0xFFFF0008 - svcOffset;
|
||||
*baseK11VA = pointedInstructionVA & 0xFFFF0000; //This assumes that the pointed instruction has an offset < 0x10000, iirc that's always the case
|
||||
u32 *arm11SvcTable = (u32 *)(pos + *(u32 *)(pos + pointedInstructionVA - *baseK11VA + 8) - *baseK11VA); //SVC handler address
|
||||
*arm11SvcHandler = arm11SvcTable;
|
||||
arm11SvcTable = *arm11SvcHandler = (u32 *)(pos + *(u32 *)(pos + pointedInstructionVA - *baseK11VA + 8) - *baseK11VA); //SVC handler address
|
||||
while(*arm11SvcTable) arm11SvcTable++; //Look for SVC0 (NULL)
|
||||
|
||||
const u8 pattern2[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
|
||||
*freeK11Space = memsearch(pos, pattern2, size, sizeof(pattern2)) + 1;
|
||||
pointedInstructionVA = 0xFFFF0010 - dabtOffset;
|
||||
*arm11DAbtHandler = (u32 *)(pos + *(u32 *)(pos + pointedInstructionVA - *baseK11VA + 8) - *baseK11VA);
|
||||
(*freeK11Space)++;
|
||||
|
||||
return arm11SvcTable;
|
||||
}
|
||||
#else
|
||||
u32 *getKernel11Info(u8 *pos, u32 size, u32 *baseK11VA, u8 **freeK11Space)
|
||||
{
|
||||
const u8 pattern[] = {0x00, 0xB0, 0x9C, 0xE5};
|
||||
|
||||
u32 *arm11ExceptionsPage = (u32 *)memsearch(pos, pattern, size, sizeof(pattern)) - 0xB;
|
||||
|
||||
u32 svcOffset = (-((arm11ExceptionsPage[2] & 0xFFFFFF) << 2) & (0xFFFFFF << 2)) - 8; //Branch offset + 8 for prefetch
|
||||
u32 pointedInstructionVA = 0xFFFF0008 - svcOffset;
|
||||
*baseK11VA = pointedInstructionVA & 0xFFFF0000; //This assumes that the pointed instruction has an offset < 0x10000, iirc that's always the case
|
||||
u32 *arm11SvcTable = (u32 *)(pos + *(u32 *)(pos + pointedInstructionVA - *baseK11VA + 8) - *baseK11VA); //SVC handler address
|
||||
while(*arm11SvcTable) arm11SvcTable++; //Look for SVC0 (NULL)
|
||||
|
||||
const u8 pattern2[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
|
||||
*freeK11Space = memsearch(pos, pattern2, size, sizeof(pattern2)) + 1;
|
||||
|
||||
return arm11SvcTable;
|
||||
}
|
||||
#endif
|
||||
|
||||
void patchSignatureChecks(u8 *pos, u32 size)
|
||||
u32 patchSignatureChecks(u8 *pos, u32 size)
|
||||
{
|
||||
const u16 sigPatch[2] = {0x2000, 0x4770};
|
||||
|
||||
//Look for signature checks
|
||||
const u8 pattern[] = {0xC0, 0x1C, 0x76, 0xE7},
|
||||
pattern2[] = {0xB5, 0x22, 0x4D, 0x0C};
|
||||
|
||||
u16 *off = (u16 *)memsearch(pos, pattern, size, sizeof(pattern)),
|
||||
*off2 = (u16 *)(memsearch(pos, pattern2, size, sizeof(pattern2)) - 1);
|
||||
u16 *off = (u16 *)memsearch(pos, pattern, size, sizeof(pattern));
|
||||
u8 *temp = memsearch(pos, pattern2, size, sizeof(pattern2));
|
||||
|
||||
*off = sigPatch[0];
|
||||
off2[0] = sigPatch[0];
|
||||
off2[1] = sigPatch[1];
|
||||
if(off == NULL || temp == NULL) return 1;
|
||||
|
||||
u16 *off2 = (u16 *)(temp - 1);
|
||||
*off = off2[0] = 0x2000;
|
||||
off2[1] = 0x4770;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void patchFirmlaunches(u8 *pos, u32 size, u32 process9MemAddr)
|
||||
u32 patchOldSignatureChecks(u8 *pos, u32 size)
|
||||
{
|
||||
// Look for signature checks
|
||||
const u8 pattern[] = {0xC0, 0x1C, 0xBD, 0xE7},
|
||||
pattern2[] = {0xB5, 0x23, 0x4E, 0x0C};
|
||||
|
||||
u16 *off = (u16 *)memsearch(pos, pattern, size, sizeof(pattern));
|
||||
u8 *temp = memsearch(pos, pattern2, size, sizeof(pattern2));
|
||||
|
||||
if(off == NULL || temp == NULL) return 1;
|
||||
|
||||
u16 *off2 = (u16 *)(temp - 1);
|
||||
*off = off2[0] = 0x2000;
|
||||
off2[1] = 0x4770;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 patchFirmlaunches(u8 *pos, u32 size, u32 process9MemAddr)
|
||||
{
|
||||
//Look for firmlaunch code
|
||||
const u8 pattern[] = {0xE2, 0x20, 0x20, 0x90};
|
||||
|
||||
u8 *off = memsearch(pos, pattern, size, sizeof(pattern)) - 0x13;
|
||||
u32 pathLen;
|
||||
for(pathLen = 0; pathLen < 41 && launchedPath[pathLen] != 0; pathLen++);
|
||||
|
||||
if(launchedPath[pathLen] != 0) return 1;
|
||||
|
||||
u8 *off = memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(off == NULL) return 1;
|
||||
|
||||
off -= 0x13;
|
||||
|
||||
//Firmlaunch function offset - offset in BLX opcode (A4-16 - ARM DDI 0100E) + 1
|
||||
u32 fOpenOffset = (u32)(off + 9 - (-((*(u32 *)off & 0x00FFFFFF) << 2) & (0xFFFFFF << 2)) - pos + process9MemAddr);
|
||||
|
||||
//Copy firmlaunch code
|
||||
memcpy(off, reboot, reboot_size);
|
||||
memcpy(off, reboot_bin, reboot_bin_size);
|
||||
|
||||
//Put the fOpen offset in the right location
|
||||
u32 *pos_fopen = (u32 *)memsearch(off, "OPEN", reboot_size, 4);
|
||||
u32 *pos_fopen = (u32 *)memsearch(off, "OPEN", reboot_bin_size, 4);
|
||||
*pos_fopen = fOpenOffset;
|
||||
|
||||
u16 *fname = (u16 *)memsearch(off, "FILE", reboot_bin_size, 8);
|
||||
memcpy(fname, launchedPath, 2 * (1 + pathLen));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void patchFirmWrites(u8 *pos, u32 size)
|
||||
u32 patchFirmWrites(u8 *pos, u32 size)
|
||||
{
|
||||
//Look for FIRM writing code
|
||||
u8 *off1 = memsearch(pos, "exe:", size, 4);
|
||||
u8 *off = memsearch(pos, "exe:", size, 4);
|
||||
|
||||
if(off == NULL) return 1;
|
||||
|
||||
const u8 pattern[] = {0x00, 0x28, 0x01, 0xDA};
|
||||
|
||||
u16 *off2 = (u16 *)memsearch(off1 - 0x100, pattern, 0x100, sizeof(pattern));
|
||||
u16 *off2 = (u16 *)memsearch(off - 0x100, pattern, 0x100, sizeof(pattern));
|
||||
|
||||
if(off2 == NULL) return 1;
|
||||
|
||||
off2[0] = 0x2000;
|
||||
off2[1] = 0x46C0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void patchOldFirmWrites(u8 *pos, u32 size)
|
||||
u32 patchOldFirmWrites(u8 *pos, u32 size)
|
||||
{
|
||||
//Look for FIRM writing code
|
||||
const u8 pattern[] = {0x04, 0x1E, 0x1D, 0xDB};
|
||||
|
||||
u16 *off = (u16 *)memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(off == NULL) return 1;
|
||||
|
||||
off[0] = 0x2400;
|
||||
off[1] = 0xE01D;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void reimplementSvcBackdoor(u8 *pos, u32 *arm11SvcTable, u32 baseK11VA, u8 **freeK11Space)
|
||||
u32 patchTitleInstallMinVersionChecks(u8 *pos, u32 size, u32 firmVersion)
|
||||
{
|
||||
const u8 pattern[] = {0xFF, 0x00, 0x00, 0x02};
|
||||
|
||||
u8 *off = memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(off == NULL) return firmVersion == 0xFFFFFFFF ? 0 : 1;
|
||||
|
||||
off++;
|
||||
|
||||
//Zero out the first TitleID in the list
|
||||
memset32(off, 0, 8);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 patchZeroKeyNcchEncryptionCheck(u8 *pos, u32 size)
|
||||
{
|
||||
const u8 pattern[] = {0x28, 0x2A, 0xD0, 0x08};
|
||||
|
||||
u8 *temp = memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(temp == NULL) return 1;
|
||||
|
||||
u16 *off = (u16 *)(temp - 1);
|
||||
*off = 0x2001; //mov r0, #1
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 patchNandNcchEncryptionCheck(u8 *pos, u32 size)
|
||||
{
|
||||
const u8 pattern[] = {0x07, 0xD1, 0x28, 0x7A};
|
||||
|
||||
u16 *off = (u16 *)memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(off == NULL) return 1;
|
||||
|
||||
off--;
|
||||
*off = 0x2001; //mov r0, #1
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 patchCheckForDevCommonKey(u8 *pos, u32 size)
|
||||
{
|
||||
const u8 pattern[] = {0x03, 0x7C, 0x28, 0x00};
|
||||
|
||||
u16 *off = (u16 *)memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(off == NULL) return 1;
|
||||
|
||||
*off = 0x2301; //mov r3, #1
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 reimplementSvcBackdoor(u8 *pos, u32 *arm11SvcTable, u32 baseK11VA, u8 **freeK11Space)
|
||||
{
|
||||
if(arm11SvcTable[0x7B] != 0) return 0;
|
||||
|
||||
//Official implementation of svcBackdoor
|
||||
const u8 svcBackdoor[40] = {0xFF, 0x10, 0xCD, 0xE3, //bic r1, sp, #0xff
|
||||
0x0F, 0x1C, 0x81, 0xE3, //orr r1, r1, #0xf00
|
||||
0x28, 0x10, 0x81, 0xE2, //add r1, r1, #0x28
|
||||
0x00, 0x20, 0x91, 0xE5, //ldr r2, [r1]
|
||||
0x00, 0x60, 0x22, 0xE9, //stmdb r2!, {sp, lr}
|
||||
0x02, 0xD0, 0xA0, 0xE1, //mov sp, r2
|
||||
0x30, 0xFF, 0x2F, 0xE1, //blx r0
|
||||
0x03, 0x00, 0xBD, 0xE8, //pop {r0, r1}
|
||||
0x00, 0xD0, 0xA0, 0xE1, //mov sp, r0
|
||||
0x11, 0xFF, 0x2F, 0xE1}; //bx r1
|
||||
const u8 svcBackdoor[] = {0xFF, 0x10, 0xCD, 0xE3, //bic r1, sp, #0xff
|
||||
0x0F, 0x1C, 0x81, 0xE3, //orr r1, r1, #0xf00
|
||||
0x28, 0x10, 0x81, 0xE2, //add r1, r1, #0x28
|
||||
0x00, 0x20, 0x91, 0xE5, //ldr r2, [r1]
|
||||
0x00, 0x60, 0x22, 0xE9, //stmdb r2!, {sp, lr}
|
||||
0x02, 0xD0, 0xA0, 0xE1, //mov sp, r2
|
||||
0x30, 0xFF, 0x2F, 0xE1, //blx r0
|
||||
0x03, 0x00, 0xBD, 0xE8, //pop {r0, r1}
|
||||
0x00, 0xD0, 0xA0, 0xE1, //mov sp, r0
|
||||
0x11, 0xFF, 0x2F, 0xE1}; //bx r1
|
||||
|
||||
if(!arm11SvcTable[0x7B])
|
||||
{
|
||||
memcpy(*freeK11Space, svcBackdoor, 40);
|
||||
if(*(u32 *)(*freeK11Space + sizeof(svcBackdoor) - 4) != 0xFFFFFFFF) return 1;
|
||||
|
||||
arm11SvcTable[0x7B] = baseK11VA + *freeK11Space - pos;
|
||||
*freeK11Space += 40;
|
||||
}
|
||||
memcpy(*freeK11Space, svcBackdoor, sizeof(svcBackdoor));
|
||||
|
||||
arm11SvcTable[0x7B] = baseK11VA + *freeK11Space - pos;
|
||||
*freeK11Space += sizeof(svcBackdoor);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void implementSvcGetCFWInfo(u8 *pos, u32 *arm11SvcTable, u32 baseK11VA, u8 **freeK11Space)
|
||||
u32 stubSvcRestrictGpuDma(u8 *pos, u32 *arm11SvcTable, u32 baseK11VA)
|
||||
{
|
||||
memcpy(*freeK11Space, svcGetCFWInfo, svcGetCFWInfo_size);
|
||||
if(arm11SvcTable[0x59] != 0)
|
||||
{
|
||||
u32 *off = (u32 *)(pos + arm11SvcTable[0x59] - baseK11VA);
|
||||
off[1] = 0xE1A00000; //replace call to inner function by a NOP
|
||||
}
|
||||
|
||||
CFWInfo *info = (CFWInfo *)memsearch(*freeK11Space, "LUMA", svcGetCFWInfo_size, 4);
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 implementSvcGetCFWInfo(u8 *pos, u32 *arm11SvcTable, u32 baseK11VA, u8 **freeK11Space, bool isSafeMode)
|
||||
{
|
||||
if(*(u32 *)(*freeK11Space + svcGetCFWInfo_bin_size - 4) != 0xFFFFFFFF) return 1;
|
||||
|
||||
memcpy(*freeK11Space, svcGetCFWInfo_bin, svcGetCFWInfo_bin_size);
|
||||
|
||||
struct CfwInfo
|
||||
{
|
||||
char magic[4];
|
||||
|
||||
u8 versionMajor;
|
||||
u8 versionMinor;
|
||||
u8 versionBuild;
|
||||
u8 flags;
|
||||
|
||||
u32 commitHash;
|
||||
|
||||
u32 config;
|
||||
} __attribute__((packed)) *info = (struct CfwInfo *)memsearch(*freeK11Space, "LUMA", svcGetCFWInfo_bin_size, 4);
|
||||
|
||||
const char *rev = REVISION;
|
||||
bool isRelease;
|
||||
|
||||
info->commitHash = COMMIT_HASH;
|
||||
info->config = configData.config;
|
||||
info->versionMajor = (u8)(rev[1] - '0');
|
||||
info->versionMinor = (u8)(rev[3] - '0');
|
||||
|
||||
bool isRelease;
|
||||
|
||||
if(rev[4] == '.')
|
||||
{
|
||||
info->versionBuild = (u8)(rev[5] - '0');
|
||||
@@ -186,72 +310,25 @@ void implementSvcGetCFWInfo(u8 *pos, u32 *arm11SvcTable, u32 baseK11VA, u8 **fre
|
||||
}
|
||||
else isRelease = rev[4] == 0;
|
||||
|
||||
#ifdef DEV
|
||||
info->flags = 1 /* dev build */ | ((isRelease ? 1 : 0) << 1) /* is release */;
|
||||
#else
|
||||
info->flags = 0 /* regular build */ | ((isRelease ? 1 : 0) << 1) /* is release */;
|
||||
#endif
|
||||
if(isRelease) info->flags = 1;
|
||||
if(ISN3DS) info->flags |= 1 << 4;
|
||||
if(isSafeMode) info->flags |= 1 << 5;
|
||||
|
||||
arm11SvcTable[0x2E] = baseK11VA + *freeK11Space - pos; //Stubbed svc
|
||||
*freeK11Space += svcGetCFWInfo_size;
|
||||
*freeK11Space += svcGetCFWInfo_bin_size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void patchTitleInstallMinVersionCheck(u8 *pos, u32 size)
|
||||
u32 patchArm9ExceptionHandlersInstall(u8 *pos, u32 size)
|
||||
{
|
||||
const u8 pattern[] = {0x0A, 0x81, 0x42, 0x02};
|
||||
const u8 pattern[] = {0x80, 0xE5, 0x40, 0x1C};
|
||||
|
||||
u8 *off = memsearch(pos, pattern, size, sizeof(pattern));
|
||||
u8 *temp = memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(off != NULL) off[4] = 0xE0;
|
||||
}
|
||||
if(temp == NULL) return 1;
|
||||
|
||||
void applyLegacyFirmPatches(u8 *pos, FirmwareType firmType)
|
||||
{
|
||||
const patchData twlPatches[] = {
|
||||
{{0x1650C0, 0x165D64}, {{ 6, 0x00, 0x20, 0x4E, 0xB0, 0x70, 0xBD }}, 0},
|
||||
{{0x173A0E, 0x17474A}, { .type1 = 0x2001 }, 1},
|
||||
{{0x174802, 0x17553E}, { .type1 = 0x2000 }, 2},
|
||||
{{0x174964, 0x1756A0}, { .type1 = 0x2000 }, 2},
|
||||
{{0x174D52, 0x175A8E}, { .type1 = 0x2001 }, 2},
|
||||
{{0x174D5E, 0x175A9A}, { .type1 = 0x2001 }, 2},
|
||||
{{0x174D6A, 0x175AA6}, { .type1 = 0x2001 }, 2},
|
||||
{{0x174E56, 0x175B92}, { .type1 = 0x2001 }, 1},
|
||||
{{0x174E58, 0x175B94}, { .type1 = 0x4770 }, 1}
|
||||
},
|
||||
agbPatches[] = {
|
||||
{{0x9D2A8, 0x9DF64}, {{ 6, 0x00, 0x20, 0x4E, 0xB0, 0x70, 0xBD }}, 0},
|
||||
{{0xD7A12, 0xD8B8A}, { .type1 = 0xEF26 }, 1}
|
||||
};
|
||||
|
||||
/* Calculate the amount of patches to apply. Only count the boot screen patch for AGB_FIRM
|
||||
if the matching option was enabled (keep it as last) */
|
||||
u32 numPatches = firmType == TWL_FIRM ? (sizeof(twlPatches) / sizeof(patchData)) :
|
||||
(sizeof(agbPatches) / sizeof(patchData) - !CONFIG(SHOWGBABOOT));
|
||||
const patchData *patches = firmType == TWL_FIRM ? twlPatches : agbPatches;
|
||||
|
||||
//Patch
|
||||
for(u32 i = 0; i < numPatches; i++)
|
||||
{
|
||||
switch(patches[i].type)
|
||||
{
|
||||
case 0:
|
||||
memcpy(pos + patches[i].offset[isN3DS ? 1 : 0], patches[i].patch.type0 + 1, patches[i].patch.type0[0]);
|
||||
break;
|
||||
case 2:
|
||||
*(u16 *)(pos + patches[i].offset[isN3DS ? 1 : 0] + 2) = 0;
|
||||
case 1:
|
||||
*(u16 *)(pos + patches[i].offset[isN3DS ? 1 : 0]) = patches[i].patch.type1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef DEV
|
||||
void patchArm9ExceptionHandlersInstall(u8 *pos, u32 size)
|
||||
{
|
||||
const u8 pattern[] = {0x03, 0xA0, 0xE3, 0x18};
|
||||
|
||||
u32 *off = (u32 *)(memsearch(pos, pattern, size, sizeof(pattern)) + 0x13);
|
||||
u32 *off = (u32 *)(temp - 0xA);
|
||||
|
||||
for(u32 r0 = 0x08000000; *off != 0xE3A01040; off++) //Until mov r1, #0x40
|
||||
{
|
||||
@@ -270,107 +347,263 @@ void patchArm9ExceptionHandlersInstall(u8 *pos, u32 size)
|
||||
if(!pre) addr += offset;
|
||||
if(writeback) r0 = addr;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 getInfoForArm11ExceptionHandlers(u8 *pos, u32 size, u32 *codeSetOffset)
|
||||
{
|
||||
//This function has to succeed. Crash if it doesn't (we'll get an exception dump of it anyways)
|
||||
const u8 pattern[] = {0x1B, 0x50, 0xA0, 0xE3}, //Get TitleID from CodeSet
|
||||
pattern2[] = {0xE8, 0x13, 0x00, 0x02}; //Call exception dispatcher
|
||||
|
||||
const u8 pattern[] = {0xE3, 0xDC, 0x05, 0xC0}, //Get TitleID from CodeSet
|
||||
pattern2[] = {0xE1, 0x0F, 0x00, 0xBD}; //Call exception dispatcher
|
||||
u32 *loadCodeSet = (u32 *)memsearch(pos, pattern, size, sizeof(pattern));
|
||||
u8 *temp = memsearch(pos, pattern2, size, sizeof(pattern2));
|
||||
|
||||
u32 *loadCodeSet = (u32 *)(memsearch(pos, pattern, size, sizeof(pattern)) - 0xB);
|
||||
if(loadCodeSet == NULL || temp == NULL) error("Failed to get ARM11 exception handlers data.");
|
||||
|
||||
loadCodeSet -= 2;
|
||||
*codeSetOffset = *loadCodeSet & 0xFFF;
|
||||
|
||||
return *(u32 *)(memsearch(pos, pattern2, size, sizeof(pattern2)) + 0xD);
|
||||
return *(u32 *)(temp + 9);
|
||||
}
|
||||
|
||||
void patchSvcBreak9(u8 *pos, u32 size, u32 kernel9Address)
|
||||
u32 patchSvcBreak9(u8 *pos, u32 size, u32 kernel9Address)
|
||||
{
|
||||
/* Stub svcBreak with "bkpt 65535" so we can debug the panic.
|
||||
Thanks @yellows8 and others for mentioning this idea on #3dsdev */
|
||||
//Stub svcBreak with "bkpt 65535" so we can debug the panic
|
||||
|
||||
//Look for the svc handler
|
||||
const u8 pattern[] = {0x00, 0xE0, 0x4F, 0xE1}; //mrs lr, spsr
|
||||
|
||||
u32 *arm9SvcTable = (u32 *)memsearch(pos, pattern, size, sizeof(pattern));
|
||||
while(*arm9SvcTable) arm9SvcTable++; //Look for SVC0 (NULL)
|
||||
|
||||
if(arm9SvcTable == NULL) return 1;
|
||||
|
||||
while(*arm9SvcTable != 0) arm9SvcTable++; //Look for SVC0 (NULL)
|
||||
|
||||
u32 *addr = (u32 *)(pos + arm9SvcTable[0x3C] - kernel9Address);
|
||||
*addr = 0xE12FFF7F;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void patchSvcBreak11(u8 *pos, u32 *arm11SvcTable)
|
||||
void patchSvcBreak11(u8 *pos, u32 *arm11SvcTable, u32 baseK11VA)
|
||||
{
|
||||
//Same as above, for NATIVE_FIRM ARM11
|
||||
u32 *addr = (u32 *)(pos + arm11SvcTable[0x3C] - 0xFFF00000);
|
||||
u32 *addr = (u32 *)(pos + arm11SvcTable[0x3C] - baseK11VA);
|
||||
*addr = 0xE12FFF7F;
|
||||
}
|
||||
|
||||
void patchKernel9Panic(u8 *pos, u32 size)
|
||||
u32 patchKernel9Panic(u8 *pos, u32 size)
|
||||
{
|
||||
const u8 pattern[] = {0xFF, 0xEA, 0x04, 0xD0};
|
||||
|
||||
u32 *off = (u32 *)(memsearch(pos, pattern, size, sizeof(pattern)) - 0x12);
|
||||
u8 *temp = memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(temp == NULL) return 1;
|
||||
|
||||
u32 *off = (u32 *)(temp - 0x12);
|
||||
*off = 0xE12FFF7E;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void patchKernel11Panic(u8 *pos, u32 size)
|
||||
u32 patchKernel11Panic(u8 *pos, u32 size)
|
||||
{
|
||||
const u8 pattern[] = {0x02, 0x0B, 0x44, 0xE2};
|
||||
|
||||
u32 *off = (u32 *)memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(off == NULL) return 1;
|
||||
|
||||
*off = 0xE12FFF7E;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void patchP9AccessChecks(u8 *pos, u32 size)
|
||||
u32 patchP9AccessChecks(u8 *pos, u32 size)
|
||||
{
|
||||
const u8 pattern[] = {0xE0, 0x00, 0x40, 0x39};
|
||||
const u8 pattern[] = {0x00, 0x08, 0x49, 0x68};
|
||||
|
||||
u16 *off = (u16 *)memsearch(pos, pattern, size, sizeof(pattern)) - 7;
|
||||
u8 *temp = memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(temp == NULL) return 1;
|
||||
|
||||
u16 *off = (u16 *)(temp - 3);
|
||||
off[0] = 0x2001; //mov r0, #1
|
||||
off[1] = 0x4770; //bx lr
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void patchArm11SvcAccessChecks(u32 *arm11SvcHandler)
|
||||
u32 patchArm11SvcAccessChecks(u32 *arm11SvcHandler, u32 *endPos)
|
||||
{
|
||||
while(*arm11SvcHandler != 0xE11A0E1B) arm11SvcHandler++; //TST R10, R11,LSL LR
|
||||
while(*arm11SvcHandler != 0xE11A0E1B && arm11SvcHandler < endPos) arm11SvcHandler++; //TST R10, R11,LSL LR
|
||||
|
||||
if(arm11SvcHandler == endPos) return 1;
|
||||
|
||||
*arm11SvcHandler = 0xE3B0A001; //MOVS R10, #1
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void patchK11ModuleChecks(u8 *pos, u32 size, u8 **freeK11Space)
|
||||
u32 patchK11ModuleChecks(u8 *pos, u32 size, u8 **freeK11Space, bool patchGames)
|
||||
{
|
||||
/* We have to detour a function in the ARM11 kernel because builtin modules
|
||||
are compressed in memory and are only decompressed at runtime */
|
||||
|
||||
//Check that we have enough free space
|
||||
if(*(u32 *)(*freeK11Space + k11modules_size - 4) == 0xFFFFFFFF)
|
||||
{
|
||||
//Inject our code into the free space
|
||||
memcpy(*freeK11Space, k11modules, k11modules_size);
|
||||
if(*(u32 *)(*freeK11Space + k11modules_bin_size - 4) != 0xFFFFFFFF) return patchGames ? 1 : 0;
|
||||
|
||||
//Look for the code that decompresses the .code section of the builtin modules
|
||||
const u8 pattern[] = {0xE5, 0x48, 0x00, 0x9D};
|
||||
//Look for the code that decompresses the .code section of the builtin modules
|
||||
const u8 pattern[] = {0xE5, 0x48, 0x00, 0x9D};
|
||||
|
||||
u32 *off = (u32 *)(memsearch(pos, pattern, size, sizeof(pattern)) - 0xB);
|
||||
u8 *temp = memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
//Inject a jump (BL) instruction to our code at the offset we found
|
||||
*off = 0xEB000000 | (((((u32)*freeK11Space) - ((u32)off + 8)) >> 2) & 0xFFFFFF);
|
||||
if(temp == NULL) return 1;
|
||||
|
||||
*freeK11Space += k11modules_size;
|
||||
}
|
||||
//Inject our code into the free space
|
||||
memcpy(*freeK11Space, k11modules_bin, k11modules_bin_size);
|
||||
|
||||
u32 *off = (u32 *)(temp - 0xB);
|
||||
|
||||
//Inject a jump (BL) instruction to our code at the offset we found
|
||||
*off = 0xEB000000 | (((((u32)*freeK11Space) - ((u32)off + 8)) >> 2) & 0xFFFFFF);
|
||||
|
||||
*freeK11Space += k11modules_bin_size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void patchUnitInfoValueSet(u8 *pos, u32 size)
|
||||
u32 patchUnitInfoValueSet(u8 *pos, u32 size)
|
||||
{
|
||||
//Look for UNITINFO value being set during kernel sync
|
||||
const u8 pattern[] = {0x01, 0x10, 0xA0, 0x13};
|
||||
|
||||
u8 *off = memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
off[0] = isDevUnit ? 0 : 1;
|
||||
if(off == NULL) return 1;
|
||||
|
||||
off[0] = ISDEVUNIT ? 0 : 1;
|
||||
off[3] = 0xE3;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 patchLgySignatureChecks(u8 *pos, u32 size)
|
||||
{
|
||||
const u8 pattern[] = {0x47, 0xC1, 0x17, 0x49};
|
||||
|
||||
u8 *temp = memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(temp == NULL) return 1;
|
||||
|
||||
u16 *off = (u16 *)(temp + 1);
|
||||
off[0] = 0x2000;
|
||||
off[1] = 0xB04E;
|
||||
off[2] = 0xBD70;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 patchTwlInvalidSignatureChecks(u8 *pos, u32 size)
|
||||
{
|
||||
const u8 pattern[] = {0x20, 0xF6, 0xE7, 0x7F};
|
||||
|
||||
u8 *temp = memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(temp == NULL) return 1;
|
||||
|
||||
u16 *off = (u16 *)(temp - 1);
|
||||
*off = 0x2001; //mov r0, #1
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 patchTwlNintendoLogoChecks(u8 *pos, u32 size)
|
||||
{
|
||||
const u8 pattern[] = {0xC0, 0x30, 0x06, 0xF0};
|
||||
|
||||
u16 *off = (u16 *)memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(off == NULL) return 1;
|
||||
|
||||
off[1] = 0x2000;
|
||||
off[2] = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 patchTwlWhitelistChecks(u8 *pos, u32 size)
|
||||
{
|
||||
const u8 pattern[] = {0x22, 0x00, 0x20, 0x30};
|
||||
|
||||
u16 *off = (u16 *)memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(off == NULL) return 1;
|
||||
|
||||
off[2] = 0x2000;
|
||||
off[3] = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 patchTwlFlashcartChecks(u8 *pos, u32 size, u32 firmVersion)
|
||||
{
|
||||
const u8 pattern[] = {0x25, 0x20, 0x00, 0x0E};
|
||||
|
||||
u8 *temp = memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(temp == NULL)
|
||||
{
|
||||
if(firmVersion == 0xFFFFFFFF) return patchOldTwlFlashcartChecks(pos, size);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
u16 *off = (u16 *)(temp + 3);
|
||||
off[0] = off[6] = off[0xC] = 0x2001; //mov r0, #1
|
||||
off[1] = off[7] = off[0xD] = 0; //nop
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 patchOldTwlFlashcartChecks(u8 *pos, u32 size)
|
||||
{
|
||||
const u8 pattern[] = {0x06, 0xF0, 0xA0, 0xFD};
|
||||
|
||||
u16 *off = (u16 *)memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(off == NULL) return 1;
|
||||
|
||||
off[0] = off[6] = 0x2001; //mov r0, #1
|
||||
off[1] = off[7] = 0; //nop
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 patchTwlShaHashChecks(u8 *pos, u32 size)
|
||||
{
|
||||
const u8 pattern[] = {0x10, 0xB5, 0x14, 0x22};
|
||||
|
||||
u16 *off = (u16 *)memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(off == NULL) return 1;
|
||||
|
||||
off[0] = 0x2001; //mov r0, #1
|
||||
off[1] = 0x4770;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 patchAgbBootSplash(u8 *pos, u32 size)
|
||||
{
|
||||
const u8 pattern[] = {0x00, 0x00, 0x01, 0xEF};
|
||||
|
||||
u8 *off = memsearch(pos, pattern, size, sizeof(pattern));
|
||||
|
||||
if(off == NULL) return 1;
|
||||
|
||||
off[2] = 0x26;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
@@ -21,66 +21,50 @@
|
||||
*/
|
||||
|
||||
/*
|
||||
* Signature patches by an unknown author
|
||||
* Signature patches for old FIRMs by SciresM
|
||||
* firmlaunches patching code originally by delebile
|
||||
* FIRM partition writes patches by delebile
|
||||
* ARM11 modules patching code originally by Subv
|
||||
* Idea for svcBreak patches from yellows8 and others on #3dsdev
|
||||
* TWL_FIRM patches by Steveice10 and others
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
|
||||
typedef struct patchData {
|
||||
u32 offset[2];
|
||||
union {
|
||||
u8 type0[8];
|
||||
u16 type1;
|
||||
} patch;
|
||||
u32 type;
|
||||
} patchData;
|
||||
extern CfgData configData;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
char magic[4];
|
||||
|
||||
u8 versionMajor;
|
||||
u8 versionMinor;
|
||||
u8 versionBuild;
|
||||
u8 flags;
|
||||
|
||||
u32 commitHash;
|
||||
|
||||
u32 config;
|
||||
} CFWInfo;
|
||||
|
||||
#ifdef DEV
|
||||
extern bool isDevUnit;
|
||||
#endif
|
||||
|
||||
u8 *getProcess9(u8 *pos, u32 size, u32 *process9Size, u32 *process9MemAddr);
|
||||
|
||||
#ifdef DEV
|
||||
u32 *getKernel11Info(u8 *pos, u32 size, u32 *baseK11VA, u8 **freeK11Space, u32 **arm11SvcHandler, u32 **arm11ExceptionsPage);
|
||||
#else
|
||||
u32 *getKernel11Info(u8 *pos, u32 size, u32 *baseK11VA, u8 **freeK11Space);
|
||||
#endif
|
||||
|
||||
void patchSignatureChecks(u8 *pos, u32 size);
|
||||
void patchTitleInstallMinVersionCheck(u8 *pos, u32 size);
|
||||
void patchFirmlaunches(u8 *pos, u32 size, u32 process9MemAddr);
|
||||
void patchFirmWrites(u8 *pos, u32 size);
|
||||
void patchOldFirmWrites(u8 *pos, u32 size);
|
||||
void reimplementSvcBackdoor(u8 *pos, u32 *arm11SvcTable, u32 baseK11VA, u8 **freeK11Space);
|
||||
void implementSvcGetCFWInfo(u8 *pos, u32 *arm11SvcTable, u32 baseK11VA, u8 **freeK11Space);
|
||||
void applyLegacyFirmPatches(u8 *pos, FirmwareType firmType);
|
||||
|
||||
#ifdef DEV
|
||||
void patchArm9ExceptionHandlersInstall(u8 *pos, u32 size);
|
||||
u8 *getProcess9Info(u8 *pos, u32 size, u32 *process9Size, u32 *process9MemAddr);
|
||||
u32 *getKernel11Info(u8 *pos, u32 size, u32 *baseK11VA, u8 **freeK11Space, u32 **arm11SvcHandler, u32 **arm11DAbtHandler, u32 **arm11ExceptionsPage);
|
||||
u32 patchSignatureChecks(u8 *pos, u32 size);
|
||||
u32 patchOldSignatureChecks(u8 *pos, u32 size);
|
||||
u32 patchFirmlaunches(u8 *pos, u32 size, u32 process9MemAddr);
|
||||
u32 patchFirmWrites(u8 *pos, u32 size);
|
||||
u32 patchOldFirmWrites(u8 *pos, u32 size);
|
||||
u32 patchTitleInstallMinVersionChecks(u8 *pos, u32 size, u32 firmVersion);
|
||||
u32 patchZeroKeyNcchEncryptionCheck(u8 *pos, u32 size);
|
||||
u32 patchNandNcchEncryptionCheck(u8 *pos, u32 size);
|
||||
u32 patchCheckForDevCommonKey(u8 *pos, u32 size);
|
||||
u32 reimplementSvcBackdoor(u8 *pos, u32 *arm11SvcTable, u32 baseK11VA, u8 **freeK11Space);
|
||||
u32 stubSvcRestrictGpuDma(u8 *pos, u32 *arm11SvcTable, u32 baseK11VA);
|
||||
u32 implementSvcGetCFWInfo(u8 *pos, u32 *arm11SvcTable, u32 baseK11VA, u8 **freeK11Space, bool isSafeMode);
|
||||
u32 patchArm9ExceptionHandlersInstall(u8 *pos, u32 size);
|
||||
u32 getInfoForArm11ExceptionHandlers(u8 *pos, u32 size, u32 *codeSetOffset);
|
||||
void patchSvcBreak9(u8 *pos, u32 size, u32 kernel9Address);
|
||||
void patchSvcBreak11(u8 *pos, u32 *arm11SvcTable);
|
||||
void patchKernel9Panic(u8 *pos, u32 size);
|
||||
void patchKernel11Panic(u8 *pos, u32 size);
|
||||
void patchP9AccessChecks(u8 *pos, u32 size);
|
||||
void patchArm11SvcAccessChecks(u32 *arm11SvcHandler);
|
||||
void patchK11ModuleChecks(u8 *pos, u32 size, u8 **freeK11Space);
|
||||
void patchUnitInfoValueSet(u8 *pos, u32 size);
|
||||
#endif
|
||||
u32 patchSvcBreak9(u8 *pos, u32 size, u32 kernel9Address);
|
||||
void patchSvcBreak11(u8 *pos, u32 *arm11SvcTable, u32 baseK11VA);
|
||||
u32 patchKernel9Panic(u8 *pos, u32 size);
|
||||
u32 patchKernel11Panic(u8 *pos, u32 size);
|
||||
u32 patchP9AccessChecks(u8 *pos, u32 size);
|
||||
u32 patchArm11SvcAccessChecks(u32 *arm11SvcHandler, u32 *endPos);
|
||||
u32 patchK11ModuleChecks(u8 *pos, u32 size, u8 **freeK11Space, bool patchGames);
|
||||
u32 patchUnitInfoValueSet(u8 *pos, u32 size);
|
||||
u32 patchLgySignatureChecks(u8 *pos, u32 size);
|
||||
u32 patchTwlInvalidSignatureChecks(u8 *pos, u32 size);
|
||||
u32 patchTwlNintendoLogoChecks(u8 *pos, u32 size);
|
||||
u32 patchTwlWhitelistChecks(u8 *pos, u32 size);
|
||||
u32 patchTwlFlashcartChecks(u8 *pos, u32 size, u32 firmVersion);
|
||||
u32 patchOldTwlFlashcartChecks(u8 *pos, u32 size);
|
||||
u32 patchTwlShaHashChecks(u8 *pos, u32 size);
|
||||
u32 patchAgbBootSplash(u8 *pos, u32 size);
|
||||
|
||||
150
source/pin.c
150
source/pin.c
@@ -44,29 +44,38 @@ static char pinKeyToLetter(u32 pressed)
|
||||
return keys[31 - i];
|
||||
}
|
||||
|
||||
void newPin(bool allowSkipping)
|
||||
void newPin(bool allowSkipping, u32 pinMode)
|
||||
{
|
||||
clearScreens(true, true);
|
||||
clearScreens(false);
|
||||
|
||||
u8 length = 4 + 2 * (MULTICONFIG(PIN) - 1);
|
||||
u8 length = 4 + 2 * (pinMode - 1);
|
||||
|
||||
char *title = allowSkipping ? "Press START to skip or enter a new PIN" : "Enter a new PIN to proceed";
|
||||
drawString(title, true, 10, 10, COLOR_TITLE);
|
||||
drawString("PIN ( digits): ", true, 10, 10 + 2 * SPACING_Y, COLOR_WHITE);
|
||||
drawCharacter('0' + length, true, 10 + 5 * SPACING_X, 10 + 2 * SPACING_Y, COLOR_WHITE);
|
||||
drawString(true, 10, 10, COLOR_TITLE, "Enter a new PIN using ABXY and the DPad");
|
||||
drawString(true, 10, 10 + SPACING_Y, COLOR_TITLE, allowSkipping ? "Press START to skip, SELECT to reset" : "Press SELECT to reset");
|
||||
|
||||
drawFormattedString(true, 10, 10 + 3 * SPACING_Y, COLOR_WHITE, "PIN (%u digits): ", length);
|
||||
|
||||
//Pad to AES block length with zeroes
|
||||
u8 __attribute__((aligned(4))) enteredPassword[0x10] = {0};
|
||||
__attribute__((aligned(4))) u8 enteredPassword[AES_BLOCK_SIZE] = {0};
|
||||
|
||||
bool reset = false;
|
||||
u8 cnt = 0;
|
||||
u32 charDrawPos = 16 * SPACING_X;
|
||||
|
||||
while(cnt < length)
|
||||
{
|
||||
if(reset)
|
||||
{
|
||||
for(u32 i = 0; i < cnt; i++)
|
||||
drawCharacter(true, 10 + (16 + 2 * i) * SPACING_X, 10 + 3 * SPACING_Y, COLOR_BLACK, (char)enteredPassword[i]);
|
||||
|
||||
cnt = 0;
|
||||
reset = false;
|
||||
}
|
||||
|
||||
u32 pressed;
|
||||
do
|
||||
{
|
||||
pressed = waitInput();
|
||||
pressed = waitInput(false);
|
||||
}
|
||||
while(!(pressed & PIN_BUTTONS));
|
||||
|
||||
@@ -74,14 +83,22 @@ void newPin(bool allowSkipping)
|
||||
if(!allowSkipping) pressed &= ~BUTTON_START;
|
||||
|
||||
if(pressed & BUTTON_START) return;
|
||||
|
||||
if(pressed & BUTTON_SELECT)
|
||||
{
|
||||
reset = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!pressed) continue;
|
||||
|
||||
char key = pinKeyToLetter(pressed);
|
||||
enteredPassword[cnt++] = (u8)key; //Add character to password
|
||||
//Add character to password
|
||||
enteredPassword[cnt] = (u8)pinKeyToLetter(pressed);
|
||||
|
||||
//Visualize character on screen
|
||||
drawCharacter(key, true, 10 + charDrawPos, 10 + 2 * SPACING_Y, COLOR_WHITE);
|
||||
charDrawPos += 2 * SPACING_X;
|
||||
drawCharacter(true, 10 + (16 + 2 * cnt) * SPACING_X, 10 + 3 * SPACING_Y, COLOR_WHITE, enteredPassword[cnt]);
|
||||
|
||||
cnt++;
|
||||
}
|
||||
|
||||
PinData pin;
|
||||
@@ -89,70 +106,80 @@ void newPin(bool allowSkipping)
|
||||
memcpy(pin.magic, "PINF", 4);
|
||||
pin.formatVersionMajor = PIN_VERSIONMAJOR;
|
||||
pin.formatVersionMinor = PIN_VERSIONMINOR;
|
||||
pin.length = length;
|
||||
|
||||
u8 __attribute__((aligned(4))) tmp[0x20];
|
||||
u8 __attribute__((aligned(4))) zeroes[0x10] = {0};
|
||||
__attribute__((aligned(4))) u8 tmp[SHA_256_HASH_SIZE],
|
||||
lengthBlock[AES_BLOCK_SIZE] = {0};
|
||||
lengthBlock[0] = length;
|
||||
|
||||
computePinHash(tmp, zeroes);
|
||||
memcpy(pin.testHash, tmp, sizeof(tmp));
|
||||
computePinHash(tmp, lengthBlock);
|
||||
memcpy(pin.lengthHash, tmp, sizeof(tmp));
|
||||
|
||||
computePinHash(tmp, enteredPassword);
|
||||
memcpy(pin.hash, tmp, sizeof(tmp));
|
||||
|
||||
if(!fileWrite(&pin, PIN_PATH, sizeof(PinData)))
|
||||
if(!fileWrite(&pin, PIN_FILE, sizeof(PinData)))
|
||||
error("Error writing the PIN file");
|
||||
}
|
||||
|
||||
bool verifyPin(void)
|
||||
bool verifyPin(u32 pinMode)
|
||||
{
|
||||
PinData pin;
|
||||
|
||||
if(fileRead(&pin, PIN_PATH, sizeof(PinData)) != sizeof(PinData) ||
|
||||
if(fileRead(&pin, PIN_FILE, sizeof(PinData)) != sizeof(PinData) ||
|
||||
memcmp(pin.magic, "PINF", 4) != 0 ||
|
||||
pin.formatVersionMajor != PIN_VERSIONMAJOR ||
|
||||
pin.formatVersionMinor != PIN_VERSIONMINOR ||
|
||||
pin.length != 4 + 2 * (MULTICONFIG(PIN) - 1))
|
||||
pin.formatVersionMinor != PIN_VERSIONMINOR)
|
||||
return false;
|
||||
|
||||
u8 __attribute__((aligned(4))) zeroes[0x10] = {0};
|
||||
u8 __attribute__((aligned(4))) tmp[0x20];
|
||||
__attribute__((aligned(4))) u8 tmp[SHA_256_HASH_SIZE],
|
||||
lengthBlock[AES_BLOCK_SIZE] = {0};
|
||||
lengthBlock[0] = 4 + 2 * (pinMode - 1);
|
||||
|
||||
computePinHash(tmp, zeroes);
|
||||
computePinHash(tmp, lengthBlock);
|
||||
|
||||
//Test vector verification (SD card has, or hasn't been used on another console)
|
||||
if(memcmp(pin.testHash, tmp, sizeof(tmp)) != 0) return false;
|
||||
//Test vector verification (check if SD card has been used on another console or PIN length changed)
|
||||
if(memcmp(pin.lengthHash, tmp, sizeof(tmp)) != 0) return false;
|
||||
|
||||
initScreens();
|
||||
|
||||
//Pad to AES block length with zeroes
|
||||
u8 __attribute__((aligned(4))) enteredPassword[0x10] = {0};
|
||||
drawString(true, 10, 10, COLOR_TITLE, "Enter the PIN using ABXY and the DPad to proceed");
|
||||
drawString(true, 10, 10 + SPACING_Y, COLOR_TITLE, "Press START to shutdown, SELECT to clear");
|
||||
|
||||
bool unlock = false;
|
||||
u8 cnt = 0;
|
||||
u32 charDrawPos = 16 * SPACING_X;
|
||||
drawFormattedString(true, 10, 10 + 3 * SPACING_Y, COLOR_WHITE, "PIN (%u digits): ", lengthBlock[0]);
|
||||
|
||||
const char messagePath[] = "/luma/pinmessage.txt";
|
||||
const char *messageFile = "pinmessage.txt";
|
||||
char message[801];
|
||||
|
||||
u32 messageSize = getFileSize(messagePath);
|
||||
if(messageSize > 0 && messageSize <= 800)
|
||||
u32 messageSize = fileRead(message, messageFile, sizeof(message) - 1);
|
||||
|
||||
if(messageSize != 0)
|
||||
{
|
||||
char message[messageSize + 1];
|
||||
fileRead(message, messagePath, 0);
|
||||
message[messageSize] = 0;
|
||||
drawString(message, false, 10, 10, COLOR_WHITE);
|
||||
drawString(false, 10, 10, COLOR_WHITE, message);
|
||||
}
|
||||
|
||||
//Pad to AES block length with zeroes
|
||||
__attribute__((aligned(4))) u8 enteredPassword[AES_BLOCK_SIZE] = {0};
|
||||
|
||||
bool unlock = false,
|
||||
reset = false;
|
||||
u8 cnt = 0;
|
||||
|
||||
while(!unlock)
|
||||
{
|
||||
drawString("Press START to shutdown or enter PIN to proceed", true, 10, 10, COLOR_TITLE);
|
||||
drawString("PIN ( digits): ", true, 10, 10 + 2 * SPACING_Y, COLOR_WHITE);
|
||||
drawCharacter('0' + pin.length, true, 10 + 5 * SPACING_X, 10 + 2 * SPACING_Y, COLOR_WHITE);
|
||||
if(reset)
|
||||
{
|
||||
for(u32 i = 0; i < cnt; i++)
|
||||
drawCharacter(true, 10 + (16 + 2 * i) * SPACING_X, 10 + 3 * SPACING_Y, COLOR_BLACK, '*');
|
||||
|
||||
cnt = 0;
|
||||
reset = false;
|
||||
}
|
||||
|
||||
u32 pressed;
|
||||
do
|
||||
{
|
||||
pressed = waitInput();
|
||||
pressed = waitInput(false);
|
||||
}
|
||||
while(!(pressed & PIN_BUTTONS));
|
||||
|
||||
@@ -160,31 +187,32 @@ bool verifyPin(void)
|
||||
|
||||
pressed &= PIN_BUTTONS;
|
||||
|
||||
if(pressed & BUTTON_SELECT)
|
||||
{
|
||||
reset = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!pressed) continue;
|
||||
|
||||
char key = pinKeyToLetter(pressed);
|
||||
enteredPassword[cnt++] = (u8)key; //Add character to password
|
||||
//Add character to password
|
||||
enteredPassword[cnt] = (u8)pinKeyToLetter(pressed);
|
||||
|
||||
//Visualize character on screen
|
||||
drawCharacter(key, true, 10 + charDrawPos, 10 + 2 * SPACING_Y, COLOR_WHITE);
|
||||
charDrawPos += 2 * SPACING_X;
|
||||
drawCharacter(true, 10 + (16 + 2 * cnt) * SPACING_X, 10 + 3 * SPACING_Y, COLOR_WHITE, '*');
|
||||
|
||||
if(cnt >= pin.length)
|
||||
if(++cnt < lengthBlock[0]) continue;
|
||||
|
||||
computePinHash(tmp, enteredPassword);
|
||||
unlock = memcmp(pin.hash, tmp, sizeof(tmp)) == 0;
|
||||
|
||||
if(!unlock)
|
||||
{
|
||||
computePinHash(tmp, enteredPassword);
|
||||
unlock = memcmp(pin.hash, tmp, sizeof(tmp)) == 0;
|
||||
reset = true;
|
||||
|
||||
if(!unlock)
|
||||
{
|
||||
charDrawPos = 16 * SPACING_X;
|
||||
cnt = 0;
|
||||
|
||||
clearScreens(true, false);
|
||||
|
||||
drawString("Wrong PIN, try again", true, 10, 10 + 4 * SPACING_Y, COLOR_RED);
|
||||
}
|
||||
drawString(true, 10, 10 + 5 * SPACING_Y, COLOR_RED, "Wrong PIN, try again");
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
18
source/pin.h
18
source/pin.h
@@ -28,19 +28,9 @@
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#define PIN_PATH "/luma/pin.bin"
|
||||
#define PIN_FILE "pin.bin"
|
||||
#define PIN_VERSIONMAJOR 1
|
||||
#define PIN_VERSIONMINOR 2
|
||||
#define PIN_VERSIONMINOR 3
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
char magic[4];
|
||||
u16 formatVersionMajor, formatVersionMinor;
|
||||
|
||||
u8 length;
|
||||
u8 testHash[32];
|
||||
u8 hash[32];
|
||||
} PinData;
|
||||
|
||||
void newPin(bool allowSkipping);
|
||||
bool verifyPin(void);
|
||||
void newPin(bool allowSkipping, u32 pinMode);
|
||||
bool verifyPin(u32 pinMode);
|
||||
173
source/screen.c
173
source/screen.c
@@ -25,41 +25,46 @@
|
||||
* Screen deinit code by tiniVi
|
||||
*/
|
||||
|
||||
/*
|
||||
* About cache coherency:
|
||||
*
|
||||
* Flushing the data cache for all memory regions read from/written to by both processors is mandatory on the ARM9 processor.
|
||||
* Thus, we make sure there'll be a cache miss on the ARM9 next time it's read.
|
||||
* Otherwise the ARM9 won't see the changes made and things will break.
|
||||
*
|
||||
* On the ARM11, in the environment we're in, the MMU isn't enabled and nothing is cached.
|
||||
*/
|
||||
|
||||
#include "screen.h"
|
||||
#include "config.h"
|
||||
#include "memory.h"
|
||||
#include "cache.h"
|
||||
#include "draw.h"
|
||||
#include "i2c.h"
|
||||
#include "utils.h"
|
||||
|
||||
static vu32 *arm11Entry = (vu32 *)0x1FFFFFFC;
|
||||
|
||||
vu32 *const arm11Entry = (vu32 *)0x1FFFFFF8;
|
||||
static const u32 brightness[4] = {0x5F, 0x4C, 0x39, 0x26};
|
||||
|
||||
void __attribute__((naked)) arm11Stub(void)
|
||||
{
|
||||
//Disable interrupts
|
||||
__asm(".word 0xF10C01C0");
|
||||
|
||||
//Wait for the entry to be set
|
||||
while(*arm11Entry == ARM11_STUB_ADDRESS);
|
||||
|
||||
//Jump to it
|
||||
((void (*)())*arm11Entry)();
|
||||
WAIT_FOR_ARM9();
|
||||
}
|
||||
|
||||
static void invokeArm11Function(void (*func)())
|
||||
{
|
||||
static bool hasCopiedStub = false;
|
||||
|
||||
if(!hasCopiedStub)
|
||||
{
|
||||
memcpy((void *)ARM11_STUB_ADDRESS, arm11Stub, 0x30);
|
||||
flushDCacheRange((void *)ARM11_STUB_ADDRESS, 0x30);
|
||||
memcpy((void *)ARM11_STUB_ADDRESS, arm11Stub, 0x2C);
|
||||
hasCopiedStub = true;
|
||||
}
|
||||
|
||||
*arm11Entry = (u32)func;
|
||||
while(*arm11Entry);
|
||||
*arm11Entry = ARM11_STUB_ADDRESS;
|
||||
while(*arm11Entry);
|
||||
}
|
||||
|
||||
void deinitScreens(void)
|
||||
@@ -77,7 +82,7 @@ void deinitScreens(void)
|
||||
WAIT_FOR_ARM9();
|
||||
}
|
||||
|
||||
if(PDN_GPU_CNT != 1) invokeArm11Function(ARM11);
|
||||
if(ARESCREENSINITIALIZED) invokeArm11Function(ARM11);
|
||||
}
|
||||
|
||||
void updateBrightness(u32 brightnessIndex)
|
||||
@@ -101,12 +106,30 @@ void updateBrightness(u32 brightnessIndex)
|
||||
invokeArm11Function(ARM11);
|
||||
}
|
||||
|
||||
void clearScreens(bool clearTop, bool clearBottom)
|
||||
void swapFramebuffers(bool isAlternate)
|
||||
{
|
||||
static bool clearTopTmp,
|
||||
clearBottomTmp;
|
||||
clearTopTmp = clearTop;
|
||||
clearBottomTmp = clearBottom;
|
||||
static u32 isAlternateTmp;
|
||||
isAlternateTmp = isAlternate ? 1 : 0;
|
||||
|
||||
void __attribute__((naked)) ARM11(void)
|
||||
{
|
||||
//Disable interrupts
|
||||
__asm(".word 0xF10C01C0");
|
||||
|
||||
*(vu32 *)0x10400478 = (*(vu32 *)0x10400478 & 0xFFFFFFFE) | isAlternateTmp;
|
||||
*(vu32 *)0x10400578 = (*(vu32 *)0x10400478 & 0xFFFFFFFE) | isAlternateTmp;
|
||||
|
||||
WAIT_FOR_ARM9();
|
||||
}
|
||||
|
||||
flushDCacheRange(&isAlternateTmp, 4);
|
||||
invokeArm11Function(ARM11);
|
||||
}
|
||||
|
||||
void clearScreens(bool isAlternate)
|
||||
{
|
||||
static volatile struct fb *fbTmp;
|
||||
fbTmp = isAlternate ? &fbs[1] : &fbs[0];
|
||||
|
||||
void __attribute__((naked)) ARM11(void)
|
||||
{
|
||||
@@ -118,52 +141,35 @@ void clearScreens(bool clearTop, bool clearBottom)
|
||||
vu32 *REGs_PSC0 = (vu32 *)0x10400010,
|
||||
*REGs_PSC1 = (vu32 *)0x10400020;
|
||||
|
||||
if(clearTopTmp)
|
||||
{
|
||||
REGs_PSC0[0] = (u32)fb->top_left >> 3; //Start address
|
||||
REGs_PSC0[1] = (u32)(fb->top_left + SCREEN_TOP_FBSIZE) >> 3; //End address
|
||||
REGs_PSC0[2] = 0; //Fill value
|
||||
REGs_PSC0[3] = (2 << 8) | 1; //32-bit pattern; start
|
||||
}
|
||||
REGs_PSC0[0] = (u32)fbTmp->top_left >> 3; //Start address
|
||||
REGs_PSC0[1] = (u32)(fbTmp->top_left + SCREEN_TOP_FBSIZE) >> 3; //End address
|
||||
REGs_PSC0[2] = 0; //Fill value
|
||||
REGs_PSC0[3] = (2 << 8) | 1; //32-bit pattern; start
|
||||
|
||||
if(clearBottomTmp)
|
||||
{
|
||||
REGs_PSC1[0] = (u32)fb->bottom >> 3; //Start address
|
||||
REGs_PSC1[1] = (u32)(fb->bottom + SCREEN_BOTTOM_FBSIZE) >> 3; //End address
|
||||
REGs_PSC1[2] = 0; //Fill value
|
||||
REGs_PSC1[3] = (2 << 8) | 1; //32-bit pattern; start
|
||||
}
|
||||
REGs_PSC1[0] = (u32)fbTmp->bottom >> 3; //Start address
|
||||
REGs_PSC1[1] = (u32)(fbTmp->bottom + SCREEN_BOTTOM_FBSIZE) >> 3; //End address
|
||||
REGs_PSC1[2] = 0; //Fill value
|
||||
REGs_PSC1[3] = (2 << 8) | 1; //32-bit pattern; start
|
||||
|
||||
while(!((!clearTopTmp || (REGs_PSC0[3] & 2)) && (!clearBottomTmp || (REGs_PSC1[3] & 2))));
|
||||
|
||||
if(fb->top_right != fb->top_left && clearTopTmp)
|
||||
{
|
||||
REGs_PSC0[0] = (u32)fb->top_right >> 3; //Start address
|
||||
REGs_PSC0[1] = (u32)(fb->top_right + SCREEN_TOP_FBSIZE) >> 3; //End address
|
||||
REGs_PSC0[2] = 0; //Fill value
|
||||
REGs_PSC0[3] = (2 << 8) | 1; //32-bit pattern; start
|
||||
|
||||
while(!(REGs_PSC0[3] & 2));
|
||||
}
|
||||
while(!((REGs_PSC0[3] & 2) && (REGs_PSC1[3] & 2)));
|
||||
|
||||
WAIT_FOR_ARM9();
|
||||
}
|
||||
|
||||
flushDCacheRange(&clearTopTmp, 1);
|
||||
flushDCacheRange(&clearBottomTmp, 1);
|
||||
flushDCacheRange((void *)fb, sizeof(struct fb));
|
||||
flushDCacheRange((void *)fbTmp, sizeof(struct fb));
|
||||
flushDCacheRange(&fbTmp, 4);
|
||||
invokeArm11Function(ARM11);
|
||||
}
|
||||
|
||||
void initScreens(void)
|
||||
{
|
||||
void __attribute__((naked)) ARM11(void)
|
||||
void __attribute__((naked)) initSequence(void)
|
||||
{
|
||||
//Disable interrupts
|
||||
__asm(".word 0xF10C01C0");
|
||||
|
||||
u32 brightnessLevel = brightness[MULTICONFIG(BRIGHTNESS)];
|
||||
|
||||
|
||||
*(vu32 *)0x10141200 = 0x1007F;
|
||||
*(vu32 *)0x10202014 = 0x00000001;
|
||||
*(vu32 *)0x1020200C &= 0xFFFEFFFE;
|
||||
@@ -172,7 +178,7 @@ void initScreens(void)
|
||||
*(vu32 *)0x10202244 = 0x1023E;
|
||||
*(vu32 *)0x10202A44 = 0x1023E;
|
||||
|
||||
// Top screen
|
||||
//Top screen
|
||||
*(vu32 *)0x10400400 = 0x000001c2;
|
||||
*(vu32 *)0x10400404 = 0x000000d1;
|
||||
*(vu32 *)0x10400408 = 0x000001c1;
|
||||
@@ -202,11 +208,11 @@ void initScreens(void)
|
||||
*(vu32 *)0x10400490 = 0x000002D0;
|
||||
*(vu32 *)0x1040049C = 0x00000000;
|
||||
|
||||
// Disco register
|
||||
//Disco register
|
||||
for(u32 i = 0; i < 256; i++)
|
||||
*(vu32 *)0x10400484 = 0x10101 * i;
|
||||
|
||||
// Bottom screen
|
||||
//Bottom screen
|
||||
*(vu32 *)0x10400500 = 0x000001c2;
|
||||
*(vu32 *)0x10400504 = 0x000000d1;
|
||||
*(vu32 *)0x10400508 = 0x000001c1;
|
||||
@@ -236,39 +242,56 @@ void initScreens(void)
|
||||
*(vu32 *)0x10400590 = 0x000002D0;
|
||||
*(vu32 *)0x1040059C = 0x00000000;
|
||||
|
||||
// Disco register
|
||||
//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
|
||||
fb->top_left = (u8 *)0x18300000;
|
||||
fb->top_right = (u8 *)0x18300000;
|
||||
fb->bottom = (u8 *)0x18346500;
|
||||
|
||||
WAIT_FOR_ARM9();
|
||||
}
|
||||
|
||||
if(PDN_GPU_CNT == 1)
|
||||
//Set CakeBrah framebuffers
|
||||
void __attribute__((naked)) setupFramebuffers(void)
|
||||
{
|
||||
flushDCacheRange(&configData, sizeof(CfgData));
|
||||
flushDCacheRange((void *)fb, sizeof(struct fb));
|
||||
invokeArm11Function(ARM11);
|
||||
//Disable interrupts
|
||||
__asm(".word 0xF10C01C0");
|
||||
|
||||
clearScreens(true, true);
|
||||
fbs[0].top_left = (u8 *)0x18300000;
|
||||
fbs[1].top_left = (u8 *)0x18400000;
|
||||
fbs[0].top_right = (u8 *)0x18300000;
|
||||
fbs[1].top_right = (u8 *)0x18400000;
|
||||
fbs[0].bottom = (u8 *)0x18346500;
|
||||
fbs[1].bottom = (u8 *)0x18446500;
|
||||
|
||||
//Turn on backlight
|
||||
i2cWriteRegister(I2C_DEV_MCU, 0x22, 0x2A);
|
||||
*(vu32 *)0x10400468 = (u32)fbs[0].top_left;
|
||||
*(vu32 *)0x1040046c = (u32)fbs[1].top_left;
|
||||
*(vu32 *)0x10400494 = (u32)fbs[0].top_right;
|
||||
*(vu32 *)0x10400498 = (u32)fbs[1].top_right;
|
||||
*(vu32 *)0x10400568 = (u32)fbs[0].bottom;
|
||||
*(vu32 *)0x1040056c = (u32)fbs[1].bottom;
|
||||
|
||||
WAIT_FOR_ARM9();
|
||||
}
|
||||
else
|
||||
|
||||
static bool needToSetup = true;
|
||||
|
||||
if(needToSetup)
|
||||
{
|
||||
clearScreens(true, true);
|
||||
updateBrightness(MULTICONFIG(BRIGHTNESS));
|
||||
if(!ARESCREENSINITIALIZED)
|
||||
{
|
||||
flushDCacheRange(&configData, sizeof(CfgData));
|
||||
invokeArm11Function(initSequence);
|
||||
|
||||
//Turn on backlight
|
||||
i2cWriteRegister(I2C_DEV_MCU, 0x22, 0x2A);
|
||||
wait(3ULL);
|
||||
}
|
||||
else updateBrightness(MULTICONFIG(BRIGHTNESS));
|
||||
|
||||
flushDCacheRange((void *)fbs, 2 * sizeof(struct fb));
|
||||
invokeArm11Function(setupFramebuffers);
|
||||
needToSetup = false;
|
||||
}
|
||||
}
|
||||
|
||||
clearScreens(false);
|
||||
swapFramebuffers(false);
|
||||
}
|
||||
|
||||
@@ -31,16 +31,27 @@
|
||||
|
||||
#define PDN_GPU_CNT (*(vu8 *)0x10141200)
|
||||
|
||||
#define ARM11_STUB_ADDRESS (0x25000000 - 0x30) //It's currently only 0x28 bytes large. We're putting 0x30 just to be sure here
|
||||
#define ARESCREENSINITIALIZED (PDN_GPU_CNT != 1)
|
||||
|
||||
#define ARM11_STUB_ADDRESS 0x1FFFFF00
|
||||
#define WAIT_FOR_ARM9() *arm11Entry = 0; while(!*arm11Entry); ((void (*)())*arm11Entry)();
|
||||
|
||||
#define SCREEN_TOP_WIDTH 400
|
||||
#define SCREEN_BOTTOM_WIDTH 320
|
||||
#define SCREEN_HEIGHT 240
|
||||
#define SCREEN_TOP_FBSIZE (3 * SCREEN_TOP_WIDTH * SCREEN_HEIGHT)
|
||||
#define SCREEN_BOTTOM_FBSIZE (3 * SCREEN_BOTTOM_WIDTH * SCREEN_HEIGHT)
|
||||
|
||||
static volatile struct fb {
|
||||
u8 *top_left;
|
||||
u8 *top_right;
|
||||
u8 *bottom;
|
||||
} *const fb = (volatile struct fb *)0x23FFFE00;
|
||||
} __attribute__((packed)) *const fbs = (volatile struct fb *)0x23FFFE00;
|
||||
|
||||
extern CfgData configData;
|
||||
|
||||
void deinitScreens(void);
|
||||
void swapFramebuffers(bool isAlternate);
|
||||
void updateBrightness(u32 brightnessIndex);
|
||||
void clearScreens(bool clearTop, bool clearBottom);
|
||||
void initScreens(void);
|
||||
void clearScreens(bool isAlternate);
|
||||
void initScreens(void);
|
||||
|
||||
@@ -24,27 +24,27 @@
|
||||
.align 4
|
||||
.global _start
|
||||
_start:
|
||||
b start
|
||||
@ Disable interrupts
|
||||
mrs r4, cpsr
|
||||
orr r4, #0x1C0
|
||||
msr cpsr_cx, r4
|
||||
|
||||
.global launchedFirmTidLow
|
||||
launchedFirmTidLow:
|
||||
.hword 0, 0, 0, 0, 0, 0, 0, 0
|
||||
ldr r4, =0xBEEF
|
||||
cmp r2, r4
|
||||
movne r0, #0 @ check magic word
|
||||
|
||||
mov r9, r0
|
||||
mov r10, r1
|
||||
|
||||
start:
|
||||
@ Change the stack pointer
|
||||
mov sp, #0x27000000
|
||||
|
||||
@ Disable interrupts
|
||||
mrs r0, cpsr
|
||||
orr r0, #0x1C0
|
||||
msr cpsr_cx, r0
|
||||
|
||||
@ Disable caches / MPU
|
||||
mrc p15, 0, r0, c1, c0, 0 @ read control register
|
||||
bic r0, #(1<<12) @ - instruction cache disable
|
||||
bic r0, #(1<<2) @ - data cache disable
|
||||
bic r0, #(1<<0) @ - mpu disable
|
||||
mcr p15, 0, r0, c1, c0, 0 @ write control register
|
||||
mrc p15, 0, r4, c1, c0, 0 @ read control register
|
||||
bic r4, #(1<<12) @ - instruction cache disable
|
||||
bic r4, #(1<<2) @ - data cache disable
|
||||
bic r4, #(1<<0) @ - mpu disable
|
||||
mcr p15, 0, r4, c1, c0, 0 @ write control register
|
||||
|
||||
@ Flush caches
|
||||
bl flushEntireDCache
|
||||
@@ -58,7 +58,7 @@ start:
|
||||
@ Set MPU permissions and cache settings
|
||||
ldr r0, =0xFFFF001D @ ffff0000 32k | bootrom (unprotected part)
|
||||
ldr r1, =0x01FF801D @ 01ff8000 32k | itcm
|
||||
ldr r2, =0x08000029 @ 08000000 2M | arm9 mem (O3DS / N3DS)
|
||||
ldr r2, =0x08000029 @ 08000000 2M | arm9 mem (O3DS / N3DS)
|
||||
ldr r3, =0x10000029 @ 10000000 2M | io mem (ARM9 / first 2MB)
|
||||
ldr r4, =0x20000037 @ 20000000 256M | fcram (O3DS / N3DS)
|
||||
ldr r5, =0x1FF00027 @ 1FF00000 1M | dsp / axi wram
|
||||
@@ -91,4 +91,13 @@ start:
|
||||
mov r1, #0x340
|
||||
str r1, [r0]
|
||||
|
||||
@ Clear BSS
|
||||
ldr r0, =__bss_start
|
||||
mov r1, #0
|
||||
ldr r2, =__bss_end
|
||||
sub r2, r0
|
||||
bl memset32
|
||||
|
||||
mov r0, r9
|
||||
mov r1, r10
|
||||
b main
|
||||
|
||||
@@ -27,29 +27,38 @@ u32 strlen(const char *string)
|
||||
{
|
||||
char *stringEnd = (char *)string;
|
||||
|
||||
while(*stringEnd) stringEnd++;
|
||||
while(*stringEnd != 0) stringEnd++;
|
||||
|
||||
return stringEnd - string;
|
||||
}
|
||||
|
||||
void concatenateStrings(char *destination, const char *source)
|
||||
u32 strnlen(const char *string, u32 maxlen)
|
||||
{
|
||||
u32 i = strlen(source),
|
||||
j = strlen(destination);
|
||||
u32 size;
|
||||
|
||||
memcpy(&destination[j], source, i + 1);
|
||||
for(size = 0; size < maxlen && *string; string++, size++);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
void hexItoa(u32 number, char *out, u32 digits)
|
||||
u32 hexAtoi(const char *in, u32 digits)
|
||||
{
|
||||
const char hexDigits[] = "0123456789ABCDEF";
|
||||
u32 i = 0;
|
||||
u32 res = 0;
|
||||
char *tmp = (char *)in;
|
||||
|
||||
while(number > 0)
|
||||
{
|
||||
out[digits - 1 - i++] = hexDigits[number & 0xF];
|
||||
number >>= 4;
|
||||
}
|
||||
for(u32 i = 0; i < digits && *tmp != 0; tmp++, i++)
|
||||
res = (*tmp > '9' ? *tmp - 'A' + 10 : *tmp - '0') + (res << 4);
|
||||
|
||||
while(i < digits) out[digits - 1 - i++] = '0';
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
u32 decAtoi(const char *in, u32 digits)
|
||||
{
|
||||
u32 res = 0;
|
||||
char *tmp = (char *)in;
|
||||
|
||||
for(u32 i = 0; i < digits && *tmp != 0; tmp++, i++)
|
||||
res = *tmp - '0' + res * 10;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -25,5 +25,6 @@
|
||||
#include "types.h"
|
||||
|
||||
u32 strlen(const char *string);
|
||||
void concatenateStrings(char *destination, const char *source);
|
||||
void hexItoa(u32 number, char *out, u32 digits);
|
||||
u32 strnlen(const char *string, u32 maxlen);
|
||||
u32 hexAtoi(const char *in, u32 digits);
|
||||
u32 decAtoi(const char *in, u32 digits);
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
//Common data types
|
||||
typedef uint8_t u8;
|
||||
@@ -35,8 +36,63 @@ typedef volatile u8 vu8;
|
||||
typedef volatile u16 vu16;
|
||||
typedef volatile u32 vu32;
|
||||
typedef volatile u64 vu64;
|
||||
typedef int8_t s8;
|
||||
typedef int16_t s16;
|
||||
typedef int32_t s32;
|
||||
typedef int64_t s64;
|
||||
typedef volatile s8 vs8;
|
||||
typedef volatile s16 vs16;
|
||||
typedef volatile s32 vs32;
|
||||
typedef volatile s64 vs64;
|
||||
|
||||
#include "3dsheaders.h"
|
||||
|
||||
#define ARM11_CORE0_MAILBOX_ENTRYPOINT *(vu32 *)0x1FFFFFFC
|
||||
|
||||
#define CFG_SYSPROT9 (*(vu8 *)0x10000000)
|
||||
#define CFG_BOOTENV (*(vu32 *)0x10010000)
|
||||
#define CFG_UNITINFO (*(vu8 *)0x10010010)
|
||||
#define CFG_TWLUNITINFO (*(vu8 *)0x10010014)
|
||||
#define OTP_DEVCONSOLEID (*(vu64 *)0x10012000)
|
||||
#define OTP_TWLCONSOLEID (*(vu64 *)0x10012100)
|
||||
#define PDN_MPCORE_CFG (*(vu32 *)0x10140FFC)
|
||||
#define PDN_SPI_CNT (*(vu32 *)0x101401C0)
|
||||
|
||||
#define ISN3DS (PDN_MPCORE_CFG == 7)
|
||||
#define ISDEVUNIT (CFG_UNITINFO != 0)
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
char magic[4];
|
||||
u16 formatVersionMajor, formatVersionMinor;
|
||||
|
||||
u32 config;
|
||||
} CfgData;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
char magic[4];
|
||||
u16 formatVersionMajor, formatVersionMinor;
|
||||
|
||||
u8 lengthHash[32];
|
||||
u8 hash[32];
|
||||
} PinData;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
u32 magic[2];
|
||||
u16 versionMinor, versionMajor;
|
||||
|
||||
u16 processor, core;
|
||||
u32 type;
|
||||
|
||||
u32 totalSize;
|
||||
u32 registerDumpSize;
|
||||
u32 codeDumpSize;
|
||||
u32 stackDumpSize;
|
||||
u32 additionalDataSize;
|
||||
} ExceptionDumpHeader;
|
||||
|
||||
//Used by multiple files
|
||||
typedef enum FirmwareSource
|
||||
{
|
||||
FIRMWARE_SYSNAND = 0,
|
||||
@@ -52,5 +108,10 @@ typedef enum FirmwareType
|
||||
TWL_FIRM,
|
||||
AGB_FIRM,
|
||||
SAFE_FIRM,
|
||||
SYSUPDATER_FIRM,
|
||||
NATIVE_FIRM1X2X
|
||||
} FirmwareType;
|
||||
} FirmwareType;
|
||||
|
||||
extern u16 launchedFirmTidLow[8];
|
||||
extern u16 launchedPath[41];
|
||||
extern bool isFirmlaunch;
|
||||
|
||||
138
source/utils.c
138
source/utils.c
@@ -20,6 +20,10 @@
|
||||
* Notices displayed by works containing it.
|
||||
*/
|
||||
|
||||
/*
|
||||
* waitInput function based on code by d0k3 https://github.com/d0k3/Decrypt9WIP/blob/master/source/hid.c
|
||||
*/
|
||||
|
||||
#include "utils.h"
|
||||
#include "i2c.h"
|
||||
#include "buttons.h"
|
||||
@@ -27,97 +31,91 @@
|
||||
#include "draw.h"
|
||||
#include "cache.h"
|
||||
|
||||
u32 waitInput(void)
|
||||
{
|
||||
bool pressedKey = false;
|
||||
u32 key;
|
||||
|
||||
//Wait for no keys to be pressed
|
||||
while(HID_PAD);
|
||||
|
||||
do
|
||||
{
|
||||
//Wait for a key to be pressed
|
||||
while(!HID_PAD);
|
||||
|
||||
key = HID_PAD;
|
||||
|
||||
//Make sure it's pressed
|
||||
for(u32 i = 0x13000; i > 0; i--)
|
||||
{
|
||||
if(key != HID_PAD) break;
|
||||
if(i == 1) pressedKey = true;
|
||||
}
|
||||
}
|
||||
while(!pressedKey);
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
void mcuReboot(void)
|
||||
{
|
||||
if(!isFirmlaunch && PDN_GPU_CNT != 1) clearScreens(true, true);
|
||||
|
||||
//Ensure that all memory transfers have completed and that the data cache has been flushed
|
||||
flushEntireDCache();
|
||||
|
||||
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 2);
|
||||
while(true);
|
||||
}
|
||||
|
||||
void mcuPowerOff(void)
|
||||
{
|
||||
if(!isFirmlaunch && PDN_GPU_CNT != 1) clearScreens(true, true);
|
||||
|
||||
//Ensure that all memory transfers have completed and that the data cache has been flushed
|
||||
flushEntireDCache();
|
||||
|
||||
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 0);
|
||||
while(true);
|
||||
}
|
||||
|
||||
static inline void startChrono(u64 initialTicks)
|
||||
static void startChrono(void)
|
||||
{
|
||||
REG_TIMER_CNT(0) = 0; //67MHz
|
||||
for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 4; //Count-up
|
||||
|
||||
for(u32 i = 0; i < 4; i++) REG_TIMER_VAL(i) = (u16)(initialTicks >> (16 * i));
|
||||
for(u32 i = 0; i < 4; i++) REG_TIMER_VAL(i) = 0;
|
||||
|
||||
REG_TIMER_CNT(0) = 0x80; //67MHz; enabled
|
||||
for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 0x84; //Count-up; enabled
|
||||
}
|
||||
|
||||
static inline void stopChrono(void)
|
||||
static u64 chrono(void)
|
||||
{
|
||||
for(u32 i = 0; i < 4; i++) REG_TIMER_CNT(i) &= ~0x80;
|
||||
u64 res = 0;
|
||||
for(u32 i = 0; i < 4; i++) res |= REG_TIMER_VAL(i) << (16 * i);
|
||||
|
||||
res /= (TICKS_PER_SEC / 1000);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void chrono(u32 seconds)
|
||||
u32 waitInput(bool isMenu)
|
||||
{
|
||||
startChrono(0);
|
||||
static u64 dPadDelay = 0ULL;
|
||||
u32 key,
|
||||
oldKey = HID_PAD;
|
||||
|
||||
u64 startingTicks = 0;
|
||||
for(u32 i = 0; i < 4; i++) startingTicks |= REG_TIMER_VAL(i) << (16 * i);
|
||||
|
||||
u64 res;
|
||||
do
|
||||
if(isMenu)
|
||||
{
|
||||
res = 0;
|
||||
for(u32 i = 0; i < 4; i++) res |= REG_TIMER_VAL(i) << (16 * i);
|
||||
dPadDelay = dPadDelay > 0ULL ? 87ULL : 143ULL;
|
||||
startChrono();
|
||||
}
|
||||
while(res - startingTicks < seconds * TICKS_PER_SEC);
|
||||
|
||||
stopChrono();
|
||||
while(true)
|
||||
{
|
||||
key = HID_PAD;
|
||||
|
||||
if(!key)
|
||||
{
|
||||
if(i2cReadRegister(I2C_DEV_MCU, 0x10) == 1) mcuPowerOff();
|
||||
oldKey = 0;
|
||||
dPadDelay = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(key == oldKey && (!isMenu || (!(key & DPAD_BUTTONS) || chrono() < dPadDelay))) continue;
|
||||
|
||||
//Make sure the key is pressed
|
||||
u32 i;
|
||||
for(i = 0; i < 0x13000 && key == HID_PAD; i++);
|
||||
if(i == 0x13000) break;
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
void mcuPowerOff(void)
|
||||
{
|
||||
if(!isFirmlaunch && ARESCREENSINITIALIZED) clearScreens(false);
|
||||
|
||||
//Ensure that all memory transfers have completed and that the data cache has been flushed
|
||||
flushEntireDCache();
|
||||
|
||||
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 0);
|
||||
while(true);
|
||||
}
|
||||
|
||||
void wait(u64 amount)
|
||||
{
|
||||
startChrono();
|
||||
while(chrono() < amount);
|
||||
}
|
||||
|
||||
void error(const char *message)
|
||||
{
|
||||
initScreens();
|
||||
if(!isFirmlaunch)
|
||||
{
|
||||
initScreens();
|
||||
|
||||
drawString("An error has occurred:", true, 10, 10, COLOR_RED);
|
||||
u32 posY = drawString(message, true, 10, 30, COLOR_WHITE);
|
||||
drawString("Press any button to shutdown", true, 10, posY + 2 * SPACING_Y, COLOR_WHITE);
|
||||
drawString(true, 10, 10, COLOR_RED, "An error has occurred:");
|
||||
u32 posY = drawString(true, 10, 30, COLOR_WHITE, message);
|
||||
drawString(true, 10, posY + 2 * SPACING_Y, COLOR_WHITE, "Press any button to shutdown");
|
||||
|
||||
waitInput(false);
|
||||
}
|
||||
|
||||
waitInput();
|
||||
mcuPowerOff();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,10 @@
|
||||
* Notices displayed by works containing it.
|
||||
*/
|
||||
|
||||
/*
|
||||
* waitInput function based on code by d0k3 https://github.com/d0k3/Decrypt9WIP/blob/master/source/hid.c
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
@@ -28,10 +32,7 @@
|
||||
#define REG_TIMER_CNT(i) *(vu16 *)(0x10003002 + 4 * i)
|
||||
#define REG_TIMER_VAL(i) *(vu16 *)(0x10003000 + 4 * i)
|
||||
|
||||
extern bool isFirmlaunch;
|
||||
|
||||
u32 waitInput(void);
|
||||
void mcuReboot(void);
|
||||
u32 waitInput(bool isMenu);
|
||||
void mcuPowerOff(void);
|
||||
void chrono(u32 seconds);
|
||||
void wait(u64 amount);
|
||||
void error(const char *message);
|
||||
Reference in New Issue
Block a user