forked from Mirror/GodMode9
Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
e2b91ba0dc | ||
|
3973a24dfa | ||
|
9f278babc1 |
40
.github/ISSUE_TEMPLATE/bug_report.md
vendored
40
.github/ISSUE_TEMPLATE/bug_report.md
vendored
@ -1,40 +0,0 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Report a bug you found in GodMode9
|
||||
title: "[BUG] ..."
|
||||
labels: bug
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
---
|
||||
name: Bug report
|
||||
about: Report a bug you found
|
||||
title: "[BUG] ..."
|
||||
labels: bug
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**So you want to report a bug?**
|
||||
Hold on, there are ways you could make things easier for us:
|
||||
* Give a clear description of the bug (what happened?).
|
||||
* Give us clear steps to reproduce (when/how did it happen?).
|
||||
* Give us info about your system (where did it happen?).
|
||||
* A photograph or even a short video of the bug happening is always helpful!
|
||||
|
||||
**Info about your system**
|
||||
Include this info to make our work easier:
|
||||
* Console type (O3DS/N3DS)
|
||||
* Anything special about your console? (defects, custom modifications,...)
|
||||
* Bootloader (boot9strap/fastboot3ds)
|
||||
* Did you chainload GodMode9 via Luma?
|
||||
* Helpful hint: *if you followed the Guide, boot9strap is your bootloader and Luma is your chainloader.*
|
||||
|
||||
**Help yourself**
|
||||
*Especially for any kind of boot issue ("GodMode9 doesn't boot")*, but also in many other cases these steps make a lot of sense and we will ask you to do them anyways:
|
||||
* Check your SD card (using h2testw, f.e.) or try a different one (you wouldn't believe how common failing/fake SD cards are, and what kinds of bugs are caused by them).
|
||||
* Switch to fastboot3DS using [https://github.com/d0k3/OpenFirmInstaller](OpenFirmInstaller).
|
||||
|
||||
**Have you actually read this?**
|
||||
[] I have read the information above
|
17
.github/ISSUE_TEMPLATE/feature_request.md
vendored
17
.github/ISSUE_TEMPLATE/feature_request.md
vendored
@ -1,17 +0,0 @@
|
||||
---
|
||||
name: Feature request
|
||||
about: Suggest an idea for this GodMode9
|
||||
title: "[FEATURE REQUEST] ..."
|
||||
labels: feature request
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Got a great idea on how to improve GodMode9?**
|
||||
That's always appreciated. Please make sure you add all the required info here.
|
||||
|
||||
**Describe the feature you'd like**
|
||||
Add a clear and concise description of what you want to happen.
|
||||
|
||||
**Describe alternatives you've considered**
|
||||
Add a clear and concise description of any alternative solutions or features you've considered.
|
42
.github/workflows/ci.yml
vendored
42
.github/workflows/ci.yml
vendored
@ -1,42 +0,0 @@
|
||||
name: CI
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
container: devkitpro/devkitarm
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
|
||||
- name: Fix apt sources
|
||||
run: |
|
||||
apt-get update
|
||||
apt-get -y install dirmngr
|
||||
echo 'deb http://us.archive.ubuntu.com/ubuntu/ bionic main' >> /etc/apt/sources.list
|
||||
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
|
||||
apt-get update
|
||||
|
||||
- name: Install and update packages
|
||||
run: |
|
||||
apt-get -y install python3 python3-pip p7zip-full libarchive13
|
||||
python3 --version
|
||||
python3 -m pip install --upgrade pip setuptools
|
||||
python3 -m pip install cryptography git+https://github.com/TuxSH/firmtool.git
|
||||
|
||||
- name: Build Project
|
||||
run: make release -j$(nproc)
|
||||
|
||||
- name: Prepare build artifact
|
||||
working-directory: release
|
||||
run: |
|
||||
ZIPNAME=$(ls GodMode9-*.zip)
|
||||
rm $ZIPNAME
|
||||
echo "OUTNAME=${ZIPNAME%.zip}" >> $GITHUB_ENV
|
||||
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: ${{ env.OUTNAME }}
|
||||
path: release/*
|
||||
if-no-files-found: error
|
21
.gitignore
vendored
21
.gitignore
vendored
@ -1,11 +1,8 @@
|
||||
# Object files
|
||||
*.d
|
||||
*.o
|
||||
*.ko
|
||||
*.obj
|
||||
*.elf
|
||||
*.map
|
||||
*.dis
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
@ -34,25 +31,9 @@
|
||||
# Debug files
|
||||
*.dSYM/
|
||||
|
||||
# OS leftovers
|
||||
desktop.ini
|
||||
.DS_Store
|
||||
|
||||
# Sublime files
|
||||
*.sublime-*
|
||||
|
||||
# Visual Studio Code files
|
||||
.vscode
|
||||
|
||||
# Build directories
|
||||
/screeninit/build
|
||||
/build
|
||||
/output
|
||||
/release
|
||||
|
||||
# Build leftovers
|
||||
/data/README_internal.md
|
||||
|
||||
# User additions
|
||||
/zzz_backup
|
||||
/arm9/source/language.inl
|
||||
*.trf
|
||||
|
11
.gitmodules
vendored
Normal file
11
.gitmodules
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
[submodule "CakeHax"]
|
||||
path = CakeHax
|
||||
url = https://github.com/mid-kid/CakeHax
|
||||
[submodule "BrahmaLoader"]
|
||||
path = BrahmaLoader
|
||||
url = https://github.com/d0k3/BrahmaLoader
|
||||
ignore = dirty
|
||||
[submodule "CakesROP"]
|
||||
path = CakesROP
|
||||
url = https://github.com/mid-kid/CakesROP
|
||||
ignore = dirty
|
13
.travis.yml
Normal file
13
.travis.yml
Normal file
@ -0,0 +1,13 @@
|
||||
language: c
|
||||
|
||||
before_install:
|
||||
- wget http://sourceforge.net/projects/devkitpro/files/Automated%20Installer/devkitARMupdate.pl
|
||||
- export DEVKITPRO=/home/travis/devkitPro
|
||||
- export DEVKITARM=${DEVKITPRO}/devkitARM
|
||||
|
||||
install:
|
||||
- sudo perl devkitARMupdate.pl
|
||||
- sudo apt-get -qq install lftp p7zip-full
|
||||
|
||||
script:
|
||||
- make binary
|
1
BrahmaLoader
Submodule
1
BrahmaLoader
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 8f783782ad2a1c1f6f6cdd60ba53ca3a91a26e29
|
1
CakeHax
Submodule
1
CakeHax
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 6b8fca0b37a370a605f76b34b133da91a0b40f5e
|
1
CakesROP
Submodule
1
CakesROP
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit b14debbd349c1990aab65716086c8d3809ce90bf
|
155
HelloScript.gm9
Normal file
155
HelloScript.gm9
Normal file
@ -0,0 +1,155 @@
|
||||
# GodMode9 "Hello Script"
|
||||
# Tutorial script - read / run this to learn how it works
|
||||
# last changed: 20170706
|
||||
# author: d0k3
|
||||
|
||||
# COMMENTS / SCRIPTING BASICS / 'echo' COMMAND
|
||||
# Anything after '#' will be ignored by the scripting engine
|
||||
# Commands and arguments are separated by whitespaces, any redundant whitespaces will be ignored
|
||||
# Use '"' if an argument has to include whitespaces
|
||||
# The echo command outputs info on screen
|
||||
echo "'Hello Script'\nby d0k3\n \nThis is a sample script to\nshowcase the GM9 script engine." # comments work anywhere
|
||||
# Unknown commands lead to script abort (remove the '#' below to test)
|
||||
# iamunknown test test
|
||||
|
||||
# 'ask' COMMAND / (-o/-s) SWITCHES
|
||||
# The 'ask' command is similar to the 'echo' command, but will allow the user to abort
|
||||
# Note that normally any failed command (like a negative user response on 'ask') will result in script abort
|
||||
ask "Continue running this script?"
|
||||
|
||||
# (-o/-s) SWITCHES
|
||||
# You may use the -o / --optional and -s / --silent switches on any command
|
||||
# -o / --optional continues on failures
|
||||
# -s / --silent tries to suppress all error messages
|
||||
# This would have completely ignored the user response on the previous command:
|
||||
ask -o -s "Really continue running this script?\n(I will completely ignore your answer)"
|
||||
|
||||
# ENVIRONMENTAL VARS
|
||||
# SERIAL is the serial number of your device
|
||||
# GM9OUT is the standard output path
|
||||
# CURRDIR is the directory the script is running from
|
||||
# SYSID0 is the id0 belonging to your SysNAND
|
||||
# EMUID0 is the id0 belonging to your EmuNAND (if available)
|
||||
# Use $[VAR] to get the *content* of a variable VAR
|
||||
echo "Your serial number is $[SERIAL]\nYour std output path is $[GM9OUT]\nCurrent dir is $[CURRDIR]\n \nYour sys / emu ID0 is:\n$[SYSID0]\n$[EMUID0]"
|
||||
|
||||
# ERRORMSG and SUCCESSMSG / 'set' COMMAND
|
||||
# These two are special environment vars, allowing you to control the message on script failure or success
|
||||
set ERRORMSG "HelloScript testing script failed"
|
||||
set SUCCESSMSG "HelloScript testing script success"
|
||||
# you can also reset these to return to default script messages
|
||||
# set ERRORMSG ""
|
||||
# set SUCCESSMSG ""
|
||||
|
||||
# CREATING VARS
|
||||
# You can also create new variables using 'set'
|
||||
# Notice how you can use variables when setting variables
|
||||
set TESTPATH 0:/testdir/$[SERIAL]_tick.db
|
||||
echo "Your testpath is:\n$[TESTPATH]"
|
||||
|
||||
# 'input' COMMAND
|
||||
# The 'input' command will allow the user to input a string and store that string in a variable
|
||||
# Preset the variable to have an initial value
|
||||
set USERINPUT "Hello World"
|
||||
input "Enter something please?" USERINPUT
|
||||
echo "You entered:\n$[USERINPUT]"
|
||||
|
||||
# 'allow' COMMAND
|
||||
# typically used at the beginning of a script, prompts to allow writes to the location given in the argument
|
||||
# doing this from the beginning is preferable, so that no actions are taken unless all required write permissions are unlocked
|
||||
# (note #1: without 'allow', write permissions are still in place and the user will be asked on demand)
|
||||
# (note #2: this simple testing script requires no additional permissions, thus the command is hidden behind a '#')
|
||||
# -a / -all allows writes to the specified location and all included files and directories
|
||||
# allow -a M:/
|
||||
|
||||
# 'rm' COMMAND
|
||||
# The 'rm' command is used to remove files and directories (recursively)
|
||||
# Here, we remove the dir if it is still here from an earlier run of this script
|
||||
# If it is not here, we ignore any errors and keep silent about it
|
||||
rm -o -s 0:/testdir
|
||||
|
||||
# 'mkdir' COMMAND
|
||||
# Use this to create a directory at the specified location
|
||||
mkdir 0:/testdir
|
||||
|
||||
# 'imgmount' COMMAND
|
||||
# The 'imgmount' command is used to mount an image.
|
||||
# An image can be (among other things) FAT, NAND, NCCH, NCSD, FIRM, ticket.db...
|
||||
imgmount S:/ctrnand_full.bin
|
||||
|
||||
# 'cp' COMMAND
|
||||
# Use 'cp' to copy a file or directory (recursively)
|
||||
# -h / --hash also adds a .sha file containing the files SHA256
|
||||
# -w / --overwite forces overwrite on existing files
|
||||
# -k / --skip forces skip on existing files
|
||||
# -n / --no_cancel prevents user cancels (useful on critical operations)
|
||||
cp -h -w -n 7:/dbs/ticket.db $[TESTPATH]
|
||||
|
||||
# 'imgumount' COMMAND
|
||||
# This unmounts the current mounted image.
|
||||
imgumount
|
||||
|
||||
# 'findnot' COMMAND
|
||||
# Use 'findnot' in conjunction with '?' to find an unused filename
|
||||
findnot $[TESTPATH]_???.rn RENPATH
|
||||
|
||||
# 'mv' COMMAND
|
||||
# The 'mv command renames or moves a file or directory
|
||||
# -w / --overwite forces overwrite on existing files
|
||||
# -k / --skip forces skip on existing files
|
||||
# -n / --no_cancel prevents user cancels (useful on critical operations)
|
||||
mv -w -k $[TESTPATH] $[RENPATH]
|
||||
|
||||
# 'find' COMMAND
|
||||
# The 'find' command has two main uses, (1) checking if files / dirs exist and (2) finding files / dirs
|
||||
# Here we use it to check for RENPATH, thus we use NULL as second argument (we're not interested in the output)
|
||||
find $[RENPATH] NULL
|
||||
# Wildcards ('*' / '?') are allowed when searching for a file / directory name:
|
||||
# find S:/nand.* NANDIMAGE
|
||||
|
||||
# 'sha' COMMAND
|
||||
# Use this to check a files' SHA256
|
||||
sha $[RENPATH] $[TESTPATH].sha
|
||||
# Instead of an .sha file you can also use the SHA256 in hex as second argument
|
||||
# sha S:/sector0x96.bin 82F2730D2C2DA3F30165F987FDCCAC5CBAB24B4E5F65C981CD7BE6F438E6D9D3
|
||||
|
||||
# 'inject' COMMAND
|
||||
# This command is used to inject part of one file into another
|
||||
# The syntax is: inject origin@x:y destination@z
|
||||
# x: origin offset (in hex)
|
||||
# y: origin size, starting at x (in hex)
|
||||
# z: destination offset (in hex)
|
||||
# -n / --no_cancel prevents user cancels (useful on critical operations)
|
||||
inject S:/nand_hdr.bin@100:4 $[RENPATH]@200 # offsets and sizes are in hex
|
||||
# As we just deliberately corrupted our test file, the subsequent SHA check will fail
|
||||
set ERRORMSG "SHA check failed (this was expected)"
|
||||
sha -o $[RENPATH] $[TESTPATH].sha
|
||||
set ERRORMSG ""
|
||||
|
||||
# 'verify' COMMAND
|
||||
# Certain file formats (NAND, NCCH, NCSD, CIA, FIRM, ...) can also be verified. Use 'verify' to do so.
|
||||
# verify -o s:/firm0.bin # As drive letters are case sensitive, this would fail
|
||||
verify S:/firm1.bin
|
||||
|
||||
# 'decrypt' COMMAND
|
||||
# Certain file formats (NCCH, NCSD, CIA, FIRM, BOSS, ...) can be decrypted. Use 'decrypt' to do so.
|
||||
# Take note that all crypto operations are done INPLACE and will overwrite the file(!)
|
||||
# decrypt 0:/x.ncch
|
||||
|
||||
# 'encrypt' COMMAND
|
||||
# Certain file formats (NCCH, NCSD, CIA, BOSS, ...) can be encrypted. Use 'encrypt' to do so.
|
||||
# Take note that all crypto operations are done INPLACE and will overwrite the file(!)
|
||||
# encrypt 0:/x.ncch
|
||||
|
||||
# 'buildcia' COMMAND
|
||||
# You can build CIA files from certain file formats (TMD, NCCH, NCSD ...). Use 'buildcia' to do so.
|
||||
# CIA files will always be built to the standard output directory (0:/gm9/out)
|
||||
# -l / --legit force CIA to be legit (only works for legit system installed titles)
|
||||
# buildcia 0:/x.ncch
|
||||
|
||||
# 'reboot' / 'poweroff' COMMAND
|
||||
# These are used to reboot or power off the 3DS console
|
||||
set ERRORMSG "Test script finished,\n(without reboot)\n \nIgnore the error message."
|
||||
ask "Reboot now?"
|
||||
reboot
|
||||
poweroff # this line can never be reached
|
282
Makefile
282
Makefile
@ -1,128 +1,220 @@
|
||||
#---------------------------------------------------------------------------------
|
||||
.SUFFIXES:
|
||||
#---------------------------------------------------------------------------------
|
||||
|
||||
ifeq ($(strip $(DEVKITARM)),)
|
||||
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM")
|
||||
endif
|
||||
|
||||
include $(DEVKITARM)/base_tools
|
||||
include Makefile.common
|
||||
include $(DEVKITARM)/ds_rules
|
||||
|
||||
# Base definitions
|
||||
export VERSION := $(shell git describe --tags --abbrev=8)
|
||||
export DBUILTS := $(shell date +'%Y%m%d%H%M%S')
|
||||
export DBUILTL := $(shell date +'%Y-%m-%d %H:%M:%S')
|
||||
|
||||
export OUTDIR := output
|
||||
export RELDIR := release
|
||||
export COMMON_DIR := ../common
|
||||
|
||||
# Definitions for initial RAM disk
|
||||
VRAM_TAR := $(OUTDIR)/vram0.tar
|
||||
VRAM_DATA := data/*
|
||||
VRAM_FLAGS := --make-new --path-limit 99
|
||||
ifeq ($(NTRBOOT),1)
|
||||
VRAM_SCRIPTS := resources/gm9/scripts/*
|
||||
#---------------------------------------------------------------------------------
|
||||
# TARGET is the name of the output
|
||||
# BUILD is the directory where object files & intermediate files will be placed
|
||||
# SOURCES is a list of directories containing source code
|
||||
# DATA is a list of directories containing data files
|
||||
# INCLUDES is a list of directories containing header files
|
||||
# SPECS is the directory containing the important build and link files
|
||||
#---------------------------------------------------------------------------------
|
||||
export TARGET := GodMode9
|
||||
ifeq ($(SAFEMODE),1)
|
||||
export TARGET := SafeMode9
|
||||
endif
|
||||
BUILD := build
|
||||
SOURCES := source source/common source/filesys source/crypto source/fatfs source/nand source/virtual source/game source/gamecart source/quicklz
|
||||
DATA := data
|
||||
INCLUDES := source source/common source/font source/filesys source/crypto source/fatfs source/nand source/virtual source/game source/gamecart source/quicklz
|
||||
|
||||
# Definitions for translation files
|
||||
JSON_FOLDER := resources/languages
|
||||
TRF_FOLDER := resources/gm9/languages
|
||||
#---------------------------------------------------------------------------------
|
||||
# options for code generation
|
||||
#---------------------------------------------------------------------------------
|
||||
ARCH := -mthumb -mthumb-interwork -flto
|
||||
|
||||
SOURCE_JSON := $(JSON_FOLDER)/source.json
|
||||
LANGUAGE_INL := arm9/source/language.inl
|
||||
CFLAGS := -g -Wall -Wextra -Wpedantic -Wcast-align -Wno-main -O2\
|
||||
-march=armv5te -mtune=arm946e-s -fomit-frame-pointer -ffast-math -std=gnu11\
|
||||
$(ARCH)
|
||||
|
||||
JSON_FILES := $(filter-out $(SOURCE_JSON),$(wildcard $(JSON_FOLDER)/*.json))
|
||||
TRF_FILES := $(subst $(JSON_FOLDER),$(TRF_FOLDER),$(JSON_FILES:.json=.trf))
|
||||
CFLAGS += $(INCLUDE) -DARM9
|
||||
|
||||
ifeq ($(OS),Windows_NT)
|
||||
ifeq ($(TERM),cygwin)
|
||||
PY3 := py -3 # Windows / CMD/PowerShell
|
||||
CFLAGS += -DBUILD_NAME="\"$(TARGET) (`date +'%Y/%m/%d'`)\""
|
||||
|
||||
ifeq ($(FONT),ORIG)
|
||||
CFLAGS += -DFONT_ORIGINAL
|
||||
else ifeq ($(FONT),6X10)
|
||||
CFLAGS += -DFONT_6X10
|
||||
else ifeq ($(FONT),ACORN)
|
||||
CFLAGS += -DFONT_ACORN
|
||||
else ifeq ($(FONT),GB)
|
||||
CFLAGS += -DFONT_GB
|
||||
else
|
||||
PY3 := py # Windows / MSYS2
|
||||
CFLAGS += -DFONT_6X10
|
||||
endif
|
||||
|
||||
ifeq ($(SAFEMODE),1)
|
||||
CFLAGS += -DSAFEMODE
|
||||
endif
|
||||
|
||||
ifeq ($(SWITCH_SCREENS),1)
|
||||
CFLAGS += -DSWITCH_SCREENS
|
||||
endif
|
||||
|
||||
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
|
||||
|
||||
ASFLAGS := -g $(ARCH)
|
||||
LDFLAGS = -T../link.ld -nostartfiles -g $(ARCH) -Wl,-Map,$(TARGET).map
|
||||
|
||||
LIBS :=
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
# list of directories containing libraries, this must be the top level containing
|
||||
# include and lib
|
||||
#---------------------------------------------------------------------------------
|
||||
LIBDIRS :=
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
# no real need to edit anything past this point unless you need to add additional
|
||||
# rules for different file extensions
|
||||
#---------------------------------------------------------------------------------
|
||||
ifneq ($(BUILD),$(notdir $(CURDIR)))
|
||||
#---------------------------------------------------------------------------------
|
||||
|
||||
export OUTPUT_D := $(CURDIR)/output
|
||||
export OUTPUT := $(OUTPUT_D)/$(TARGET)
|
||||
export RELEASE := $(CURDIR)/release
|
||||
|
||||
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
|
||||
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
|
||||
|
||||
export DEPSDIR := $(CURDIR)/$(BUILD)
|
||||
|
||||
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
|
||||
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
|
||||
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
|
||||
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/gm9*.*)))
|
||||
ifeq ($(SAFEMODE),1)
|
||||
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/sm9*.*)))
|
||||
endif
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
# use CXX for linking C++ projects, CC for standard C
|
||||
#---------------------------------------------------------------------------------
|
||||
ifeq ($(strip $(CPPFILES)),)
|
||||
#---------------------------------------------------------------------------------
|
||||
export LD := $(CC)
|
||||
#---------------------------------------------------------------------------------
|
||||
else
|
||||
PY3 := python3 # Unix-like
|
||||
#---------------------------------------------------------------------------------
|
||||
export LD := $(CXX)
|
||||
#---------------------------------------------------------------------------------
|
||||
endif
|
||||
#---------------------------------------------------------------------------------
|
||||
export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES)))
|
||||
|
||||
# Definitions for ARM binaries
|
||||
export INCLUDE := -I"$(shell pwd)/common"
|
||||
export OFILES_BIN := $(addsuffix .o,$(BINFILES))
|
||||
export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
|
||||
export OFILES := $(OFILES_BIN) $(OFILES_SOURCES)
|
||||
|
||||
export ASFLAGS := -g -x assembler-with-cpp $(INCLUDE)
|
||||
export CFLAGS := -DDBUILTS="\"$(DBUILTS)\"" -DDBUILTL="\"$(DBUILTL)\"" -DVERSION="\"$(VERSION)\"" -DFLAVOR="\"$(FLAVOR)\"" \
|
||||
-g -Os -Wall -Wextra -Wcast-align -Wformat=2 -Wno-main \
|
||||
-fomit-frame-pointer -ffast-math -std=gnu11 -MMD -MP \
|
||||
-Wno-unused-function -Wno-format-truncation -Wno-format-nonliteral $(INCLUDE) -ffunction-sections -fdata-sections
|
||||
export LDFLAGS := -Tlink.ld -nostartfiles -Wl,--gc-sections,-z,max-page-size=4096
|
||||
ELF := arm9/arm9_code.elf arm9/arm9_data.elf arm11/arm11.elf
|
||||
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
|
||||
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
|
||||
-I$(CURDIR)/$(BUILD)
|
||||
|
||||
.PHONY: all firm $(VRAM_TAR) elf release clean
|
||||
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
|
||||
|
||||
.PHONY: common clean all gateway firm binary cakehax cakerop brahma release
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
all: firm
|
||||
|
||||
clean:
|
||||
@set -e; for elf in $(ELF); do \
|
||||
$(MAKE) --no-print-directory -C $$(dirname $$elf) clean; \
|
||||
done
|
||||
@rm -rf $(OUTDIR) $(RELDIR) $(FIRM) $(FIRMD) $(VRAM_TAR) $(LANGUAGE_INL) $(TRF_FILES)
|
||||
common:
|
||||
@[ -d $(OUTPUT_D) ] || mkdir -p $(OUTPUT_D)
|
||||
@[ -d $(BUILD) ] || mkdir -p $(BUILD)
|
||||
|
||||
unmarked_readme: .FORCE
|
||||
@$(PY3) utils/unmark.py -f README.md data/README_internal.md
|
||||
submodules:
|
||||
@-git submodule update --init --recursive
|
||||
|
||||
release: clean unmarked_readme
|
||||
binary: common
|
||||
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
|
||||
|
||||
firm: binary
|
||||
firmtool build $(OUTPUT).firm -D $(OUTPUT).elf -C NDMA -i
|
||||
|
||||
gateway: binary
|
||||
@cp resources/LauncherTemplate.dat $(OUTPUT_D)/Launcher.dat
|
||||
@dd if=$(OUTPUT).bin of=$(OUTPUT_D)/Launcher.dat bs=1497296 seek=1 conv=notrunc
|
||||
|
||||
cakehax: submodules binary
|
||||
@$(MAKE) dir_out=$(OUTPUT_D) name=$(TARGET).dat -C CakeHax bigpayload
|
||||
@dd if=$(OUTPUT).bin of=$(OUTPUT).dat bs=512 seek=160
|
||||
|
||||
cakerop: cakehax
|
||||
@$(MAKE) DATNAME=$(TARGET).dat DISPNAME=$(TARGET) GRAPHICS=../resources/CakesROP -C CakesROP
|
||||
@mv CakesROP/CakesROP.nds $(OUTPUT_D)/$(TARGET).nds
|
||||
|
||||
brahma: submodules binary
|
||||
@[ -d BrahmaLoader/data ] || mkdir -p BrahmaLoader/data
|
||||
@cp $(OUTPUT).bin BrahmaLoader/data/payload.bin
|
||||
@cp resources/BrahmaAppInfo BrahmaLoader/resources/AppInfo
|
||||
@cp resources/BrahmaIcon.png BrahmaLoader/resources/icon.png
|
||||
@$(MAKE) --no-print-directory -C BrahmaLoader APP_TITLE=$(TARGET)
|
||||
@mv BrahmaLoader/output/*.3dsx $(OUTPUT_D)
|
||||
@mv BrahmaLoader/output/*.smdh $(OUTPUT_D)
|
||||
|
||||
release:
|
||||
@rm -fr $(BUILD) $(OUTPUT_D) $(RELEASE)
|
||||
@$(MAKE) --no-print-directory binary
|
||||
@$(MAKE) --no-print-directory firm
|
||||
@$(MAKE) --no-print-directory firm NTRBOOT=1
|
||||
@-make --no-print-directory cakerop
|
||||
@-make --no-print-directory brahma
|
||||
@[ -d $(RELEASE) ] || mkdir -p $(RELEASE)
|
||||
@[ -d $(RELEASE)/$(TARGET) ] || mkdir -p $(RELEASE)/$(TARGET)
|
||||
@cp $(OUTPUT).bin $(RELEASE)
|
||||
@cp $(OUTPUT).firm $(RELEASE)
|
||||
@-cp $(OUTPUT).dat $(RELEASE)
|
||||
@-cp $(OUTPUT).nds $(RELEASE)
|
||||
@-cp $(OUTPUT).3dsx $(RELEASE)/$(TARGET)
|
||||
@-cp $(OUTPUT).smdh $(RELEASE)/$(TARGET)
|
||||
@cp $(CURDIR)/README.md $(RELEASE)
|
||||
@cp $(CURDIR)/HelloScript.gm9 $(RELEASE)
|
||||
@cp -R $(CURDIR)/resources/gm9 $(RELEASE)/gm9
|
||||
@-7z a $(RELEASE)/$(TARGET)-`date +'%Y%m%d-%H%M%S'`.zip $(RELEASE)/*
|
||||
|
||||
@mkdir -p $(RELDIR)
|
||||
@mkdir -p $(RELDIR)/ntrboot
|
||||
@mkdir -p $(RELDIR)/elf
|
||||
#---------------------------------------------------------------------------------
|
||||
clean:
|
||||
@echo clean ...
|
||||
@-$(MAKE) clean --no-print-directory -C CakeHax
|
||||
@-$(MAKE) clean --no-print-directory -C CakesROP
|
||||
@-$(MAKE) clean --no-print-directory -C BrahmaLoader
|
||||
@-rm -fr $(BUILD) $(OUTPUT_D) $(RELEASE)
|
||||
|
||||
@cp $(FIRM) $(RELDIR)
|
||||
@cp $(OUTDIR)/$(FLAVOR)_ntr.firm $(RELDIR)/ntrboot/
|
||||
@cp $(OUTDIR)/$(FLAVOR)_ntr.firm.sha $(RELDIR)/ntrboot/
|
||||
@cp $(OUTDIR)/$(FLAVOR)_ntr_dev.firm $(RELDIR)/ntrboot/
|
||||
@cp $(OUTDIR)/$(FLAVOR)_ntr_dev.firm.sha $(RELDIR)/ntrboot/
|
||||
@cp $(OUTDIR)/$(FLAVOR).firm $(RELDIR)/
|
||||
@cp $(OUTDIR)/$(FLAVOR).firm.sha $(RELDIR)/
|
||||
@cp $(OUTDIR)/$(FLAVOR)_dev.firm $(RELDIR)/
|
||||
@cp $(OUTDIR)/$(FLAVOR)_dev.firm.sha $(RELDIR)/
|
||||
@cp $(ELF) $(RELDIR)/elf
|
||||
@cp $(CURDIR)/README.md $(RELDIR)
|
||||
@cp $(CURDIR)/resources/lua-doc.md $(RELDIR)/lua-doc.md
|
||||
@cp -R $(CURDIR)/resources/gm9 $(RELDIR)/gm9
|
||||
@cp -R $(CURDIR)/resources/sample $(RELDIR)/sample
|
||||
|
||||
@-7za a $(RELDIR)/$(FLAVOR)-$(VERSION)-$(DBUILTS).zip ./$(RELDIR)/*
|
||||
#---------------------------------------------------------------------------------
|
||||
else
|
||||
|
||||
$(VRAM_TAR): $(SPLASH) $(OVERRIDE_FONT) $(VRAM_DATA) $(VRAM_SCRIPTS)
|
||||
@mkdir -p "$(@D)"
|
||||
@echo "Creating $@"
|
||||
@$(PY3) utils/add2tar.py $(VRAM_FLAGS) $(VRAM_TAR) $(shell ls -d -1 $^)
|
||||
DEPENDS := $(OFILES:.o=.d)
|
||||
|
||||
$(LANGUAGE_INL): $(SOURCE_JSON)
|
||||
@echo "Creating $@"
|
||||
@$(PY3) utils/transcp.py $< $@
|
||||
#---------------------------------------------------------------------------------
|
||||
# main targets
|
||||
#---------------------------------------------------------------------------------
|
||||
$(OUTPUT).bin : $(OUTPUT).elf
|
||||
$(OFILES_SOURCES) : $(HFILES)
|
||||
$(OUTPUT).elf : $(OFILES)
|
||||
|
||||
$(TRF_FOLDER)/%.trf: $(JSON_FOLDER)/%.json
|
||||
@$(PY3) utils/transriff.py $< $@
|
||||
#---------------------------------------------------------------------------------
|
||||
%.bin: %.elf
|
||||
@$(OBJCOPY) --set-section-flags .bss=alloc,load,contents -O binary $< $@
|
||||
@echo built ... $(notdir $@)
|
||||
|
||||
%.elf: .FORCE
|
||||
@echo "Building $@"
|
||||
@$(MAKE) --no-print-directory -C $(@D) $(@F)
|
||||
#---------------------------------------------------------------------------------
|
||||
# you need a rule like this for each extension you use as binary data
|
||||
#---------------------------------------------------------------------------------
|
||||
%_qlz.h %.qlz.o: %.qlz
|
||||
#---------------------------------------------------------------------------------
|
||||
@echo $(notdir $<)
|
||||
@$(bin2o)
|
||||
|
||||
# Indicate a few explicit dependencies:
|
||||
# The ARM9 data section depends on the VRAM drive
|
||||
arm9/arm9_data.elf: $(VRAM_TAR) $(LANGUAGE_INL)
|
||||
# And the code section depends on the data section being built already
|
||||
arm9/arm9_code.elf: arm9/arm9_data.elf
|
||||
-include $(DEPENDS)
|
||||
|
||||
firm: $(ELF) $(TRF_FILES)
|
||||
@mkdir -p $(call dirname,"$(FIRM)") $(call dirname,"$(FIRMD)")
|
||||
@echo "[FLAVOR] $(FLAVOR)"
|
||||
@echo "[VERSION] $(VERSION)"
|
||||
@echo "[BUILD] $(DBUILTL)"
|
||||
@echo "[FIRM] $(FIRM)"
|
||||
@$(PY3) -m firmtool build $(FIRM) $(FTFLAGS) -g -D $(ELF) -C NDMA NDMA XDMA
|
||||
@echo "[FIRM] $(FIRMD)"
|
||||
@$(PY3) -m firmtool build $(FIRMD) $(FTDFLAGS) -g -D $(ELF) -C NDMA NDMA XDMA
|
||||
|
||||
vram0: $(VRAM_TAR) .FORCE # legacy target name
|
||||
|
||||
.FORCE:
|
||||
#---------------------------------------------------------------------------------------
|
||||
endif
|
||||
#---------------------------------------------------------------------------------------
|
||||
|
@ -1,38 +0,0 @@
|
||||
|
||||
LIBS ?=
|
||||
|
||||
OBJECTS := $(patsubst $(SOURCE)/%.s, $(BUILD)/%.o, \
|
||||
$(patsubst $(SOURCE)/%.c, $(BUILD)/%.o, \
|
||||
$(call rwildcard, $(SOURCE), *.s *.c)))
|
||||
|
||||
OBJECTS_COMMON := $(patsubst $(COMMON_DIR)/%.c, $(BUILD)/%.cmn.o, \
|
||||
$(call rwildcard, $(COMMON_DIR), *.c))
|
||||
|
||||
.PHONY: all
|
||||
all: $(TARGET).elf
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
@rm -rf $(BUILD) $(TARGET).elf $(TARGET).dis $(TARGET).map
|
||||
|
||||
$(TARGET).elf: $(OBJECTS) $(OBJECTS_COMMON)
|
||||
@mkdir -p "$(@D)"
|
||||
@$(CC) $(LDFLAGS) $^ -o $@ $(LIBS)
|
||||
@$(OBJDUMP) -S -h $@ > $@.dis
|
||||
|
||||
$(BUILD)/%.cmn.o: $(COMMON_DIR)/%.c
|
||||
@mkdir -p "$(@D)"
|
||||
@echo "[$(PROCESSOR)] $<"
|
||||
@$(CC) -c $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BUILD)/%.o: $(SOURCE)/%.c
|
||||
@mkdir -p "$(@D)"
|
||||
@echo "[$(PROCESSOR)] $<"
|
||||
@$(CC) -c $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BUILD)/%.o: $(SOURCE)/%.s
|
||||
@mkdir -p "$(@D)"
|
||||
@echo "[$(PROCESSOR)] $<"
|
||||
@$(CC) -c $(ASFLAGS) -o $@ $<
|
||||
|
||||
include $(call rwildcard, $(BUILD), *.d)
|
@ -1,93 +0,0 @@
|
||||
export OBJDUMP := arm-none-eabi-objdump
|
||||
|
||||
dirname = $(shell dirname $(1))
|
||||
|
||||
rwildcard = $(foreach d, $(wildcard $1*), \
|
||||
$(filter $(subst *, %, $2), $d) \
|
||||
$(call rwildcard, $d/, $2))
|
||||
|
||||
FLAVOR ?= GodMode9
|
||||
SPLASH = resources/$(FLAVOR)_splash.png
|
||||
|
||||
ifeq ($(FLAVOR),SafeMode9)
|
||||
CFLAGS += -DSAFEMODE
|
||||
else ifeq ($(FLAVOR),GodMode64)
|
||||
OVERRIDE_FONT := resources/fonts/font_c64_8x8.pbm
|
||||
CFLAGS += -DDEFAULT_FONT=\"font_c64_8x8.pbm\"
|
||||
CFLAGS += -DCOLOR_STD_FONT="RGB(0x7B, 0x71, 0xD5)"
|
||||
CFLAGS += -DCOLOR_STD_BG="RGB(0x41, 0x30, 0xA4)"
|
||||
else ifeq ($(FLAVOR),BrickedMode9)
|
||||
OVERRIDE_FONT := resources/fonts/font_nbraille_4x6.pbm
|
||||
CFLAGS += -DDEFAULT_FONT=\"font_nbraille_4x6.pbm\"
|
||||
CFLAGS += -DCOLOR_STD_FONT="RGB(0xFF, 0xFF, 0x00)"
|
||||
CFLAGS += -DCOLOR_STD_BG="RGB(0x00, 0x00, 0xFF)"
|
||||
else ifeq ($(FLAVOR),ZuishMode9)
|
||||
OVERRIDE_FONT := resources/fonts/font_zuish_8x8.pbm
|
||||
CFLAGS += -DDEFAULT_FONT=\"font_zuish_8x8.pbm\"
|
||||
endif
|
||||
|
||||
ifeq ($(LARGEDLC),1)
|
||||
CFLAGS += -DTITLE_MAX_CONTENTS=1536
|
||||
else
|
||||
CFLAGS += -DTITLE_MAX_CONTENTS=1024
|
||||
endif
|
||||
|
||||
ifeq ($(SALTMODE),1)
|
||||
CFLAGS += -DSALTMODE
|
||||
endif
|
||||
|
||||
ifeq ($(SWITCH_SCREENS),1)
|
||||
CFLAGS += -DSWITCH_SCREENS
|
||||
endif
|
||||
|
||||
ifeq ($(SCRIPT_RUNNER),1)
|
||||
CFLAGS += -DSCRIPT_RUNNER
|
||||
endif
|
||||
|
||||
ifeq ($(AUTO_UNLOCK),1)
|
||||
CFLAGS += -DAUTO_UNLOCK
|
||||
endif
|
||||
|
||||
ifeq ($(TIMER_UNLOCK),1)
|
||||
CFLAGS += -DTIMER_UNLOCK
|
||||
endif
|
||||
|
||||
ifeq ($(HIDE_HIDDEN),1)
|
||||
CFLAGS += -DHIDE_HIDDEN
|
||||
endif
|
||||
|
||||
ifeq ($(SHOW_FREE),1)
|
||||
CFLAGS += -DSHOW_FREE
|
||||
endif
|
||||
|
||||
ifdef FIXED_BRIGHTNESS
|
||||
CFLAGS += -DFIXED_BRIGHTNESS=$(FIXED_BRIGHTNESS)
|
||||
endif
|
||||
|
||||
ifdef SD_TIMEOUT
|
||||
CFLAGS += -DSD_TIMEOUT=$(SD_TIMEOUT)
|
||||
endif
|
||||
|
||||
ifeq ($(NO_LUA),1)
|
||||
CFLAGS += -DNO_LUA
|
||||
endif
|
||||
|
||||
ifdef N_PANES
|
||||
CFLAGS += -DN_PANES=$(N_PANES)
|
||||
endif
|
||||
|
||||
ifeq ($(MONITOR_HEAP),1)
|
||||
CFLAGS += -DMONITOR_HEAP
|
||||
endif
|
||||
|
||||
ifdef NTRBOOT
|
||||
FTFLAGS = -S spi-retail
|
||||
FTDFLAGS = -S spi-dev
|
||||
FIRM = $(OUTDIR)/$(FLAVOR)_ntr.firm
|
||||
FIRMD = $(OUTDIR)/$(FLAVOR)_ntr_dev.firm
|
||||
else
|
||||
FTFLAGS = -S nand-retail
|
||||
FTDFLAGS = -S nand-dev
|
||||
FIRM = $(OUTDIR)/$(FLAVOR).firm
|
||||
FIRMD = $(OUTDIR)/$(FLAVOR)_dev.firm
|
||||
endif
|
177
README.md
177
README.md
@ -11,71 +11,50 @@ __As always, be smart, keep backups, just to be safe__.
|
||||
|
||||
|
||||
## Quick start guide
|
||||
The recommended bootloader for use with GodMode9 is [fastboot3DS](https://github.com/derrekr/fastboot3DS). There are [known issues for some users](https://github.com/d0k3/GodMode9/issues/466) when using the standard setup based on [boot9strap](https://github.com/SciresM/boot9strap) and [Luma3DS](https://github.com/AuroraWright/Luma3DS). If you insist on using that setup follow the instructions found in a [certain guide](https://3ds.hacks.guide). Here's how to set up GodMode9 (and fastboot3DS) up quickly:
|
||||
* Download [OpenFirmInstaller](https://github.com/d0k3/OpenFirmInstaller/releases/tag/v0.0.9) and follow the quick setup instructions found there.
|
||||
* Copy the `gm9` folder from the release archive to your SD card. Then, get good versions of `seeddb.bin` and `encTitleKeys.bin` from somewhere (don't ask me!) and put these two files into `sd:/gm9/support` (optional but recommended for full functionality).
|
||||
* It is also recommended you setup the RTC clock if you're running GodMode9 for the first time. Find the option via HOME button -> `More...`. Also keep in mind that you should fix your system OS clock afterwards. While you're in the `More...` menu, you may also set screen brightness to a fixed value of your choosing and manually calibrate the touch screen (*not recommended* - try the automatic configuration first).
|
||||
* Helpful hint #1: Go [here](https://3ds.hacks.guide/godmode9-usage) for step by steps on doing some common tasks in GodMode9. Especially users coming from Decrypt9WIP or Hourglass9 may find this to be helpful.
|
||||
These short instructions apply to all users who have ARM9loaderhax or SigHax and [Luma3DS](https://github.com/AuroraWright/Luma3DS) installed (Luma3DS set up with standard paths), which will be the majority of all GodMode9 users. Here's how to set it up quickly:
|
||||
* *[A9LH only]* Rename `GodMode9.bin`(from the release archive) to `X_GodMode9.bin`(change `X`to the button of your choice) and put it into `sd:/luma/payloads/`
|
||||
* *[SigHax only]* Rename `GodMode9.firm`(from the release archive) to `X_GodMode9.firm`(change `X`to the button of your choice) and put it into `sd:/luma/payloads/`
|
||||
* Copy the `gm9` folder from the release archive to your SD card. Then, get good versions of `aeskeydb.bin`, `seeddb.bin` and `encTitlekeys.bin` from somewhere (don't ask me!) and put these three files into `sd:/gm9/support` (optional but recommended for full functionality).
|
||||
* Helpful hint #1: Go [here](https://3ds.guide/godmode9-usage) for step by steps on doing some common tasks in GodMode9. Especially users coming from Decrypt9WIP or Hourglass9 may find this to be helpful.
|
||||
* Helpful hint #2: __Never unlock the red write permission level unless you know exactly what you're doing__. You will notice that prompt when it comes up, it features a completely red screen. It is recommended you stay on the yellow permission level or below at all times to be completely safe. Also read more on the write permissions system below.
|
||||
|
||||
You may now run GodMode9 via holding the X Button (or any other button you chose) at startup. See below for a list of stuff you can do with it.
|
||||
|
||||
|
||||
## Buttons in GodMode9
|
||||
GodMode9 is designed to be intuitive, buttons leading to the results you'd expect. However, some stuff may not be obvious at first glance. So, here's a quick, incomplete rundown of what each button / button combo does.
|
||||
* __\<A> button__: The \<A> button is the 'confirm' / 'choose' button. It confirms prompts and selects entries in menus. In the main file view, it pulls up a submenu for files and opens directories (use \<R+A> on directories for a submenu, also including the invaluable title search). In the hexviewer, \<A> switches into edit mode.
|
||||
* __\<B> button__: The \<B> button is the 'cancel' / 'return' button. Use it to leave menus without action, hold it on file operations to cancel said file operations.
|
||||
* __\<X> button__: In the main file view, the \<X> button deletes (marked) files. With \<R+X> files are renamed.
|
||||
* __\<Y> button__: In the main file view, the \<Y> button copies and pastes files. With \<R+Y> you can create folders and dummy files.
|
||||
* __\<L> button__: The \<L> button is the 'mark' button. Use it with \<LEFT> / \<RIGHT> to mark / unmark all files in a folder, hold it and use \<UP> / \<DOWN> to select multiple files.
|
||||
* __\<R> button__: The \<R> button is the 'switch' button. It switches buttons to their secondary function. Notable exceptions are \<R+L> for a screenshot (works almost anywhere), \<R+LEFT> / \<R+RIGHT> to switch panes and \<R+DOWN> to reload the file listing.
|
||||
* __\<START> button__: Use the \<START> button to reboot from GodMode9. Use \<R+START> to poweroff your 3DS.
|
||||
* __\<SELECT> button__: The \<SELECT> button clears or restores the clipboard (depending on if it's empty or not).
|
||||
* __\<HOME> button__: The \<HOME> button enters the HOME menu, including the scripts / payloads submenus, options for formatting the SD, setting the RTC, and more. The \<POWER> button is an alternative way of entering the HOME menu.
|
||||
* __\<R+UP> combo__: This little known keycombo, when held at startup, pauses the GodMode9 boot so that you can stare at the splash screen for a little longer.
|
||||
* __\<R+LEFT> combo__: If you have installed GodMode9 as your bootloader, this keycombo enters the bootmenu. Hold on startup! If you built GodMode9 as SALTMODE and have it as a bootloader, the keycombo is simply the \<START> button.
|
||||
## How to run this / entry points / developer info
|
||||
GodMode9 can be built to run from a number of entry points, descriptions are below. Note that you need to be on or below 3DS firmware version v11.2 (v9.2 if not using SafeHax/FastHax) or have ARM9loaderhax installed for any of these to work. All entrypoint files are included in the release archive.
|
||||
* __SigHax / Boot9Strap__: Copy `GodMode9.firm` to somewhere on your SD card (maybe refer to your CFW instructions) and run it from there. FIRM payloads can be ran from [Luma3DS](https://github.com/AuroraWright/Luma3DS) or [Boot9Strap](https://github.com/SciresM/Boot9Strap). Build this with `make firm` (requires [firmtool](https://github.com/TuxSH/firmtool) installed).
|
||||
* __A9LH, Brahma & SafeHax__: Copy `GodMode9.bin` to somewhere on your SD card and run it via either [arm9loaderhax](https://3ds.guide/), [Brahma](https://github.com/delebile/Brahma2) or [FastHax](https://github.com/nedwill/fasthax)/[SafeHax](https://github.com/TiniVi/safehax). Brahma derivatives / loaders (such as [BrahmaLoader](https://gbatemp.net/threads/release-easily-load-payloads-in-hb-launcher-via-brahma-2-mod.402857/)) and A9LH chainloaders (such as [BootCTR9](https://github.com/hartmannaf/BootCtr9)) will work with this as well. Build this with `make binary`.
|
||||
* __Homebrew Launcher__: Copy `GodMode9.3dsx` & `GodMode9.smdh` into `/3DS/GodMode9` on your SD card. Run this via [Smealums Homebrew Launcher](http://smealum.github.io/3ds/), [Mashers Grid Launcher](https://gbatemp.net/threads/release-homebrew-launcher-with-grid-layout.397527/) or any other compatible software. Build this with `make brahma`.
|
||||
* __CakeHax Browser__: Copy `GodMode9.dat` to the root of your SD card. You can then run it via http://dukesrg.github.io/?GodMode9.dat from your 3DS browser. Build this via `make cakehax`.
|
||||
* __CakeHax MSET__: Copy `GodMode9.dat` to the root of your SD card and `GodMode9.nds` to anywhere on the SD card. You can then run it either via MSET and GodMode9.nds. Build this via `make cakerop`.
|
||||
* __Gateway Browser Exploit__: Copy Launcher.dat to your SD card root and run this via http://go.gateway-3ds.com/ from your 3DS browser. Build this with `make gateway`. Please note: __this entrypoint is deprecated__. While it may still work at the present time with little to no problems, bugs will no more be fixed and it may be completely removed at a later time. Use CakeHax instead.
|
||||
|
||||
|
||||
## How to build this / developer info
|
||||
Build `GodMode9.firm` via `make firm`. This requires [firmtool](https://github.com/TuxSH/firmtool), [Python 3.5+](https://www.python.org/downloads/) and [devkitARM](https://sourceforge.net/projects/devkitpro/) installed).
|
||||
|
||||
You may run `make release` to get a nice, release-ready package of all required files. To build __SafeMode9__ (a bricksafe variant of GodMode9, with limited write permissions) instead of GodMode9, compile with `make FLAVOR=SafeMode9`. To switch screens, compile with `make SWITCH_SCREENS=1`. For additional customization, you may choose the internal font by replacing `font_default.frf` inside the `data` directory. You may also hardcode the brightness via `make FIXED_BRIGHTNESS=x`, whereas `x` is a value between 0...15.
|
||||
|
||||
Further customization is possible by hardcoding `aeskeydb.bin` (just put the file into the `data` folder when compiling). All files put into the `data` folder will turn up in the `V:` drive, but keep in mind there's a hard 223.5KiB limit for all files inside, including overhead. A standalone script runner is compiled by providing `autorun.lua` or `autorun.gm9` (again, in the `data` folder) and building with `make SCRIPT_RUNNER=1`. There's more possibility for customization, read the Makefiles to learn more.
|
||||
|
||||
To build a .firm signed with SPI boot keys (for ntrboot and the like), run `make NTRBOOT=1`. You may need to rename the output files if the ntrboot installer you use uses hardcoded filenames. Some features such as boot9 / boot11 access are not currently available from the ntrboot environment.
|
||||
|
||||
|
||||
## Bootloader mode
|
||||
Same as [boot9strap](https://github.com/SciresM/boot9strap) or [fastboot3ds](https://github.com/derrekr/fastboot3DS), GodMode9 can be installed to the system FIRM partition ('FIRM0'). When executed from a FIRM partition, GodMode9 will default to bootloader mode and try to boot, in order, `FIRM from FCRAM` (see [A9NC](https://github.com/d0k3/A9NC/releases)), `0:/bootonce.firm` (will be deleted on a successful boot), `0:/boot.firm`, `1:/boot.firm`. In bootloader mode, hold R+LEFT on boot to enter the boot menu. *Installing GodMode9 to a FIRM partition is only recommended for developers and will overwrite [boot9strap](https://github.com/SciresM/boot9strap) or any other bootloader you have installed in there*.
|
||||
If you are a developer and you are building this, you may also just run `make release` to build all files at once. To build __SafeMode9__ (a bricksafe variant of GodMode9, with limited write permissions) instead of GodMode9, compile with `make SAFEMODE=1`. To switch screens, compile with `make SWITCH_SCREENS=1`. For additional customization, you may choose the internal font via `make FONT=6X10`, `make FONT=ACORN`, `make FONT=GB` or `make FONT=ORIG`.
|
||||
|
||||
|
||||
## Write permissions system
|
||||
GodMode9 provides a write permissions system, which will protect you from accidentally damaging your system, losing data and/or modifying important system data. To unlock a write permission, an unlock sequence must be entered. This is not possible by accident. The write permission system is based on colors and the top bar on the top screen will change color according to the current write permission level. No permission above the yellow level can be unlocked on SafeMode9.
|
||||
GodMode9 provides a write permissions system, which will protect you from accidentually damaging you system, losing data and/or modifying important system data. To unlock a write permission, an unlock sequence must be entered. This is not possible by accident. The write permission system is based on colors and the top bar on the top screen will change color according to the current write permission level. No permission above the yellow level can be unlocked on SafeMode9.
|
||||
* __Green:__ Modification to system files is not possible on this permission level. You can't edit or delete savegames and installed data. However, keep in mind that any non-system related or custom stuff on your SD card is not protected.
|
||||
* __Yellow:__ You can modify system files on this permission level. Data that is unique to your console and cannot be gotten from anywhere else is still not modifiable. Any damages you introduce can be fixed in software, but loss of savegames and installed data is possible if you are not careful. __A NAND backup is highly recommended starting at this level.__
|
||||
* __Yellow:__ You can modify system files on this permission level. Data that is unique to your console and cannot be gotten from anywhere else is still not modifyable. Any damages you introduce can be fixed in software, but loss of savegames and installed data is possible if you are not careful. __A NAND backup is highly recommended starting at this level.__
|
||||
* __Orange:__ This is similar to the yellow permission level, but, in addition, allows edits to console unique data. Any damages you introduce are still fixable in software, but if you play around with this, __having a NAND backup is an absolute requirement__.
|
||||
* __Red:__ The highest regular permission level. There are no limits to system file edits, and if you are not careful enough, you can brick your console and/or remove your A9LH/B9S installation. Bricks on this level may only be fixable in hardware. __If you don't have a NAND backup at this point, you seem to have a deathwish for your console__.
|
||||
* __Red:__ The highest regular permission level. There are no limits to system file edits, and if you are not careful enough, you can brick your console and/or remove your A9LH installation. Bricks on this level may only be fixable in hardware. __If you don't have a NAND backup at this point, you seem to have a deathwish for your console__.
|
||||
* __Blue:__ This permission level is reserved for edits to system memory. While, most likely, nothing bad at all will happen, consequences of edits can be unforeseen. There is even a (albeit very small) chance of bricking your console, maybe even permanently. __Tread with caution on this level__.
|
||||
|
||||
|
||||
## Support files
|
||||
For certain functionality, GodMode9 may need 'support files'. Support files should be placed into either `0:/gm9/support` or `1:/gm9/support`. Support files contain additional information that is required in decryption operations. A list of support files, and what they do, is found below. Please don't ask for support files - find them yourself.
|
||||
* __`aeskeydb.bin`__: This should contain 0x25keyX, 0x18keyX and 0x1BkeyX to enable decryption of 7x / Secure3 / Secure4 encrypted NCCH files, 0x11key95 / 0x11key96 for FIRM decrypt support and 0x11keyOTP / 0x11keyIVOTP for 'secret' sector 0x96 crypto support. Entrypoints other than [boot9strap](https://github.com/SciresM/boot9strap) or [fastboot3ds](https://github.com/derrekr/fastboot3DS) may require a aeskeydb.bin file. This is now included in standard releases of GM9. No need to hunt down the file!
|
||||
* __`seeddb.bin`__: This file is optional and required to decrypt and mount seed-encrypted NCCHs and CIAs (if the seed in question is not installed to your NAND). Note that your seeddb.bin must also contain the seed for the specific game you need to decrypt.
|
||||
* __`encTitleKeys.bin`__ / __`decTitleKeys.bin`__: These files are optional and provide titlekeys, which are required to decrypt and install contents downloaded from CDN (for DSi and 3DS content).
|
||||
For certain functionality, GodMode9 may need 'support files'. Support files should be placed into `0:/gm9/support`. Support files contain additional information that is required in decryption operations. A list of support files, and what they do, is found below. Please don't ask for support files - find them yourself.
|
||||
* __`aeskeydb.bin`__: This should contain 0x25keyX, 0x18keyX and 0x1BkeyX to enable decryption of 7x / Secure3 / Secure4 encrypted NCCH files, 0x11key95 / 0x11key96 for FIRM decrypt support and 0x11keyOTP / 0x11keyIVOTP for 'secret' sector 0x96 crypto support. It can be created from your existing legacy `slot0x??key?.bin`files in Decrypt9 via the 'Build Key Database' feature. As an alternative (not recommended), legacy `slot0x??key?.bin` files are also supported in GodMode9. *`aeskeydb.bin` should no more be required on sighaxed systems*.
|
||||
* __`seeddb.bin`__: This file is required to decrypt and mount seed encrypted NCCHs and CIAs if the seed in question is not installed to your NAND. Note that your seeddb.bin must also contain the seed for the specific game you need to decrypt.
|
||||
* __`encTitleKeys.bin`__ / __`decTitleKeys.bin`__: These files are optional and provide titlekeys, which are required to create updatable CIAs from NCCH / NCSD files. CIAs created without these files will still work, but won't be updatable from eShop.
|
||||
|
||||
### Fonts and translations
|
||||
GodMode9 also supports custom fonts and translations as support files. These both use custom formats, fonts use FRF (Font RIFF) files which can be created using the `fontriff.py` Python script in the 'utils' folder. Translations use TRF (Translation RIFF) files from the `transriff.py` script. Examples of the inputs to these scripts can be found in the 'fonts' and 'languages' folders of the 'resources' folder respectively.
|
||||
|
||||
TRF files can be placed in `0:/gm9/languages` to show in the language menu accessible from the HOME menu and shown on first load. Official translations are provided from the community via the [GodMode9 Crowdin](https://crowdin.com/project/GodMode9). Languages can use a special font by having an FRF with the same name, for example `en.trf` and `en.frf`.
|
||||
|
||||
## Drives in GodMode9
|
||||
GodMode9 provides access to system data via drives, a listing of what each drive contains and additional info follows below. Some of these drives are removable (such as drive `7:`), some will only turn up if they are available (drive `8:` and everything associated with EmuNAND, f.e.). Information on the 3DS console file system is also found on [3Dbrew.org](https://3dbrew.org/wiki/Flash_Filesystem).
|
||||
* __`0: SDCARD`__: The SD card currently inserted into the SD card slot. The `0:/Nintendo 3DS`folder contains software installs and extdata and is specially protected via the write permission system. The SD card can be unmounted from the root directory via the R+B buttons, otherwise the SD card is always available.
|
||||
* __`1: SYSNAND CTRNAND`__: The CTRNAND partition on SysNAND. This contains your 3DS console's operating system and system software installs. Data in here is protected by the write permissions system.
|
||||
* __`2: SYSNAND TWLN`__: The TWLN partition on SysNAND. This contains your 3DS console's TWL mode operating system and system software installs. Data in here is protected by the write permissions system.
|
||||
* __`3: SYSNAND TWLP`__: The TWLP partition on SysNAND. This contains photos taken while in TWL mode.
|
||||
* __`3: SYSNAND TWLP`__: The TWLP partition on SysNAND. This contains photos taken while in TWL mode
|
||||
* __`A: SYSNAND SD`__: This drive is used for special access to data on your SD card. It actually links to a subfolder inside `0:/Nintendo 3DS` and contains software and extdata installed to SD from SysNAND. Crypto in this folder is handled only when accessed via the `A:` drive (not from `0:`). This is protected by the write permissions system.
|
||||
* __`S: SYSNAND VIRTUAL`__: This drive provides access to all partitions of the SysNAND, some of them critical for base system functionality. This is protected by the write permissions system, but, when unlocked, modifications can brick the system.
|
||||
* __`4: EMUNAND CTRNAND`__: Same as `1:`, but handles the CTRNAND on EmuNAND. For multi EmuNAND setups, the currently active EmuNAND partition can be switched via the HOME menu.
|
||||
@ -89,126 +68,88 @@ GodMode9 provides access to system data via drives, a listing of what each drive
|
||||
* __`I: IMGNAND VIRTUAL`__: When a NAND image is mounted, this provides access to the partitions inside the NAND image.
|
||||
* __`C: GAMECART`__: This is read-only and provides access to the game cartridge currently inserted into the cart slot. This can be used for dumps of CTR and TWL mode cartridges. Flash cards are supported only to a limited extent.
|
||||
* __`G: GAME IMAGE`__: CIA/NCSD/NCCH/EXEFS/ROMFS/FIRM images can be accessed via this drive when mounted. This is read-only.
|
||||
* __`K: AESKEYDB IMAGE`__: An `aeskeydb.bin` image can be mounted and accessed via this drive. The drive shows all keys inside the aeskeydb.bin. This is read-only.
|
||||
* __`T: TICKET.DB IMAGE / BDRI IMAGE`__: Ticket database files can be mounted and accessed via this drive. This provides easy and quick access to all tickets inside the `ticket.db`. This drive also provides access to other BDRI images, such as the Title database (`title.db`).
|
||||
* __`M: MEMORY VIRTUAL`__: This provides access to various memory regions. This is protected by a special write permission, and caution is advised when doing modifications inside this drive. This drive also gives access to `boot9.bin`, `boot11.bin` (boot9strap only) and `otp.mem` (sighaxed systems only).
|
||||
* __`V: VRAM VIRTUAL`__: This drive resides in part of ARM9 internal memory and contains files essential to GodMode9. The font (in FRF format), the splash logo (in PNG format) and the readme file are found there, as well as any file that is provided inside the `data` folder at build time. This is read-only.
|
||||
* __`Y: TITLE MANAGER`__: The title manager is accessed via the HOME menu and provides easy access to all installed titles.
|
||||
* __`T: TICKET.DB IMAGE`__: Ticket database files can be mounted and accessed via this drive. This provides easy and quick access to all tickets inside the `ticket.db`. This is read-only.
|
||||
* __`M: MEMORY VIRTUAL`__: This provides access to various memory regions. This is protected by a special write permission, and caution is advised when doing modifications inside this drive. This drive also gives access to `boot9.bin`, `boot11.bin` and `otp.bin` (sighaxed systems only).
|
||||
* __`X: NAND XORPADS`__: This drive contains XORpads for all NAND partitions. XORpads can be used to decrypt NAND partitions outside of the 3DS console with the help of [additional software](https://github.com/d0k3/3DSFAT16tool/releases). This is read-only.
|
||||
* __`Z: LAST SEARCH`__: After a search operation, search results are found inside this drive. The drive can be accessed at a later point to return to the former search results.
|
||||
|
||||
|
||||
## Digital preservation
|
||||
GodMode9 is one of the most important tools for digital preservation of 3DS content data. Here's some stuff you should know:
|
||||
* __Dumping game cartridges (size < 4GiB)__: Game cartridges turn up inside the `C:` drive (see above). For most carts all you need to do is copy the `.3DS` game image to some place of your choice. Game images dumped by GodMode9 contain no identifying info such as private headers or savegames. Private headers can be dumped in a separate image.
|
||||
* __Dumping game cartridges (size = 4GiB)__: Everything written above applies here as well. However, the FAT32 file system (which is what the 3DS uses) is limited to _4GiB - 1byte_. Take note that the `.3DS` game image, as provided by GodMode9 actually misses the last byte in these cases. That byte is 0xFF and unused in all known cases. It is not required for playing the image. If you need to check, we also provide split files (`.000`, `.001)`, which contain all the data. If you need a valid checksum for the `.3DS` game image, append a 0xFF byte before checking.
|
||||
* __Building CIAs (all types)__: You may convert compatible file types (game images, installed content, CDN content) to the CIA installable format using the A button menu. To get a list of installed content, press HOME, select `Title manager` and choose a drive. Take note that `standard` built CIAs are decrypted by default (decryption allows better compression by ZIP and 7Z). If you should need an encrypted CIA for some reason, apply the encryption to the CIA afterwards.
|
||||
* __Building CIAs (legit type)__: Installed content can be built as `legit` or `standard` CIA. Legit CIAs preserve more of the original data and are thus recommended for preservation purposes. When building legit CIAs, GodMode9 keeps the original crypto and tries to find a genuine, signature-valid ticket. If it doesn't find one on your system, it will use a generic ticket instead. If it only finds a personalized one, it still offers to use a generic ticket. It is not recommended to use personalized tickets - only choose this if you know what you're doing.
|
||||
* __Checking CIAs__: You may also check your CIA files with the builtin `CIA checker tool`. Legit CIAs with generic tickets are identified as `Universal Pirate Legit`, which is the recommended preservation format where `Universal Legit` is not available. Note: apart from system titles, `Universal Legit` is only available for a handful of preinstalled games from special edition 3DS consoles.
|
||||
|
||||
|
||||
## What you can do with GodMode9
|
||||
With the possibilites GodMode9 provides, not everything may be obvious at first glance. In short, __GodMode9 includes improved versions of basically everything that Decrypt9 has, and more__. Any kind of dumps and injections are handled via standard copy operations and more specific operations are found inside the A button menu. The A button menu also works for batch operations when multiple files are selected. For your convenience a (incomplete!) list of what GodMode9 can do follows below.
|
||||
|
||||
### Basic functionality
|
||||
# Basic functionality
|
||||
* __Manage files on all your data storages__: You wouldn't have expected this, right? Included are all standard file operations such as copy, delete, rename files and create folders. Use the L button to mark multiple files and apply file operations to multiple files at once.
|
||||
* __Make screenshots__: Press R+L anywhere. Screenshots are stored in PNG format.
|
||||
* __Make screenshots__: Press R+L anywhere. Screenshots are in BMP format.
|
||||
* __Use multiple panes__: Press R+left|right. This enables you to stay in one location in the first pane and open another in the second pane.
|
||||
* __Search drives and folders__: Just press R+A on the drive / folder you want to search.
|
||||
* __Compare and verify files__: Press the A button on the first file, select `Calculate SHA-256`. Do the same for the second file. If the two files are identical, you will get a message about them being identical. On the SDCARD drive (`0:`) you can also write an SHA file, so you can check for any modifications at a later point.
|
||||
* __Compare and verify files__: Press the A button on the first file, select `Calculate SHA-256`. Do the same for the second file. If the two files are identical, you will get a message about them being identical. On the SDCARD drive (`0:`) you can also write a SHA file, so you can check for any modifications at a later point.
|
||||
* __Hexview and hexedit any file__: Press the A button on a file and select `Show in Hexeditor`. A button again enables edit mode, hold the A button and press arrow buttons to edit bytes. You will get an additional confirmation prompt to take over changes. Take note that for certain files, write permissions can't be enabled.
|
||||
* __View text files in a text viewer__: Press the A button on a file and select `Show in Textviewer` (only shows up for actual text files). You can enable wordwrapped mode via R+Y, and navigate around the file via R+X and the dpad.
|
||||
* __Chainload FIRM payloads__: Press the A button on a FIRM file, select `FIRM options` -> `Boot FIRM`. Keep in mind you should not run FIRMs from dubious sources and that the write permissions system is no longer in place after booting a payload.
|
||||
* __Chainload FIRM payloads from a neat menu__: The `payloads` menu is found inside the HOME button menu. It provides any FIRM found in `0:/gm9/payloads` for quick chainloading.
|
||||
* __Inject a file to another file__: Put exactly one file (the file to be injected from) into the clipboard (via the Y button). Press A on the file to be injected to. There will be an option to inject the first file into it.
|
||||
|
||||
### Scripting functionality
|
||||
* __Run .lua scripts from anywhere on your SD card__: You can run Lua scripts via the A button menu. For an overview of usable commands have a look into the documentation and sample scripts included in the release archive. *Don't run scripts from untrusted sources.*
|
||||
* __Run Lua scripts via a neat menu__: Press the HOME button, select `More...` -> `Lua scripts...`. Any script you put into `0:/gm9/luascripts` (subdirs included) will be found here. Scripts ran via this method won't have the confirmation at the beginning either.
|
||||
* __Run legacy .gm9 scripts__: The old format of .gm9 scripts is still available, but is deprecated and will see no further development.
|
||||
# Scripting functionality
|
||||
* __Run .gm9 scripts from anywhere on your SD card__: You can run scripts in .gm9 format via the A button menu. .gm9 scripts use a shell-like language and can be edited in any text editor. For an overview of usable commands have a look into the `HelloScript.gm9` included in the release archive. *Don't run scripts from untrusted sources.*
|
||||
* __Run .gm9 scripts via a neat menu__: Press the HOME Home button, select `More...` -> `Scripts...`. Any script you put into `0:/gm9/scripts` (subdirs included) will be found here. Scripts ran via this method won't have the confirmation at the beginning either.
|
||||
|
||||
### SD card handling
|
||||
* __Format your SD card / setup an EmuNAND__: Press the HOME button, select `More...` -> `SD format menu`. This also allows to setup a RedNAND (single/multi) or GW type EmuNAND on your SD card. You will get a warning prompt and an unlock sequence before any operation starts.
|
||||
# SD card handling
|
||||
* __Format your SD card / setup an EmuNAND__: Press the HOME button, select `More...` -> `SD format menu`. This also allows to setup a RedNAND or GW type EmuNAND on your SD card. You will get a warning prompt and an unlock sequence before any operation starts.
|
||||
* __Handle multiple EmuNANDs__: Press the HOME button, select `More...` -> `Switch EmuNAND` to switch between EmuNANDs / RedNANDs. (Only available on multi EmuNAND / RedNAND systems.)
|
||||
* __Run it without an SD card / unmount the SD card__: If no SD card is found, you will be offered to run without the SD card. You can also unmount and remount your SD card from the file system root at any point.
|
||||
* __Direct access to SD installed contents__: Just take a look inside the `A:`/`B:` drives. On-the-fly-crypto is taken care for, you can access this the same as any other content.
|
||||
* __Set (and use) the RTC clock__: For correct modification / creation dates in your file system, you need to setup the RTC clock first. Press the HOME Button and select `More...` to find the option. Keep in mind that modifying the RTC clock means you should also fix system OS time afterwards.
|
||||
|
||||
### Game file handling
|
||||
* __List titles installed on your system__: Press HOME and select `Title manager`. This will also work via R+A for `CTRNAND` and `A:`/`B:` drives. This will list all titles installed in the selected location.
|
||||
* __Install titles to your system__: Just press A on any file you want installed and select `Install game image` from the submenu. Works with NCCH / NCSD / CIA / DSiWare SRLs / 3DS CDN TMDs / DSi CDN TMDs / NUS TMDs.
|
||||
* __(Batch) Uninstall titles from your system__: Most easily done via the HOME menu `Title manager`. Just select one or more titles and find the option inside the `Manage title...` submenu.
|
||||
* __Build CIAs from NCCH / NCSD (.3DS) / SRL / TMD__: Press A on the file you want converted and the option will be shown. Installed contents are found most easily via the HOME menu `Title manager`. Where applicable, you will also be able to generate legit CIAs. Note: this works also from a file search and title listing.
|
||||
# Game file handling
|
||||
* __List titles installed on your system__: Press R+A on a /title dir or a subdir below that (such dirs are found on `CTRNAND`, `TWLN` and on `A:`/`B:`). This will list all titles installed in the selected location. Works best with the below two features.
|
||||
* __Build CIAs from NCCH / NCSD (.3DS) / TMD (installed contents)__: Press A on the file you want converted, the option will be shown. Installed contents are found (among others) in `1:/titles/`(SysNAND) and `A:/titles/`(SD installed). Where applicable, you will also be able to generate legit CIAs. Note: this works also from a file search and title listing.
|
||||
* __Dump CXIs / NDS from TMD (installed contents)__: This works the same as building CIAs, but dumps decrypted CXIs or NDS rom dumps instead. Note: this works also from a file search and title listing.
|
||||
* __Decrypt, encrypt and verify NCCH / NCSD / CIA / BOSS / FIRM images__: Options are found inside the A button menu. You will be able to decrypt/encrypt to the standard output directory or (where applicable) in place.
|
||||
* __Decrypt content downloaded from CDN / NUS__: Press A on the file you want decrypted. For this to work, you need at least a TMD file (`encTitlekeys.bin` / `decTitlekeys.bin` also required, see _Support files_ below) or a CETK file. Either keep the names provided by CDN / NUS, or rename the downloaded content to `(anything).nus` or `(anything).cdn` and the CETK to `(anything).cetk`.
|
||||
* __Batch mode for the above operations__: Just select multiple files of the same type via the L button, then press the A button on one of the selected files.
|
||||
* __Access any file inside NCCH / NCSD / CIA / FIRM / NDS images__: Just mount the file via the A button menu and browse to the file you want. For CDN / NUS content, prior decryption is required for full access.
|
||||
* __Rename your NCCH / NCSD / CIA / NDS / GBA files to proper names__: Find this feature inside the A button menu. Proper names include title id, game name, product code and region.
|
||||
* __Trim NCCH / NCSD / NDS / GBA / FIRM / NAND images__: This feature is found inside the A button menu. It allows you to trim excess data from supported file types. *Warning: Excess data may not be empty, bonus drives are stored there for NAND images, NCSD card2 images store savedata there, for FIRMs parts of the A9LH exploit may be stored there*.
|
||||
* __Dump 3DS / NDS / DSi type retail game cartridges__: Insert the cartridge and take a look inside the `C:` drive. You may also dump private headers from 3DS game cartridges. The `C:` drive also gives you read/write access to the saves on the cartridges. Note: For 4GiB cartridges, the last byte is not included in the .3ds file dump. This is due to restrictrions of the FAT32 file system.
|
||||
* __Access any file inside NCCH / NCSD / CIA / FIRM images__: Just mount the file via the A button menu and browse to the file you want. For CIAs and CDN / NUS content, prior decryption may be advisable for full access.
|
||||
* __Rename your NCCH / NCSD / CIA / NDS files to proper names__: Find this feature inside the A button menu. Proper names include title id, game name, product code and region.
|
||||
* __Dump 3DS / NDS / DSi type retail game cartridges__: Insert the cartridge and take a look inside the `C:` drive. You may also dump private headers from 3DS game cartridges.
|
||||
|
||||
### NAND handling
|
||||
# NAND handling
|
||||
* __Directly mount and access NAND dumps or standard FAT images__: Just press the A button on these files to get the option. You can only mount NAND dumps from the same console.
|
||||
* __Restore NAND dumps while keeping your A9LH / sighax installation intact__: Select `Restore SysNAND (safe)` from inside the A button menu for NAND dumps.
|
||||
* __Restore NAND dumps while keeping your A9LH / sighax installation intact__: Select `Restore SysNAND (safe)` from inside the A button menu.
|
||||
* __Restore / dump NAND partitions or even full NANDs__: Just take a look into the `S:` (or `E:`/ `I:`) drive. This is done the same as any other file operation.
|
||||
* __Transfer CTRNAND images between systems__: Transfer the file located at `S:/ctrnand_full.bin` (or `E:`/ `I:`). On the receiving system, press A, select `CTRNAND Options...`, then `Transfer to NAND`.
|
||||
* __Embed an essential backup right into a NAND dump__: This is available in the A button menu for NAND dumps. Essential backups contain NAND header, `movable.sed`, `LocalFriendCodeSeed_B`, `SecureInfo_A`, NAND CID and OTP. If your local SysNAND does not contain an embedded backup, you will be asked to do one at startup. To update the essential SysNAND backup at a later point in time, press A on `S:/nand.bin` and select `NAND image options...` -> `Update embedded backup`.
|
||||
* __Install an AES key database to your NAND__: For `aeskeydb.bin` files the option is found in `aeskeydb.bin options` -> `Install aeskeydb.bin`. Only the recommended key database can be installed (see above). With an installed key database, it is possible to run the GodMode9 bootloader completely from NAND.
|
||||
* __Install FIRM files to your NAND__: Found inside the A button menu for FIRM files, select `FIRM options` -> `Install FIRM`. __Use this with caution__ - installing an incompatible FIRM file will lead to a __brick__. The FIRMs signature will automagically be replaced with a sighax signature to ensure compatibility.
|
||||
* __Embed an essential backup right into a NAND dump__: This is available in the A button menu for NAND dumps. Essential backups contain NAND header, `movable.sed`, `LocalFriendCodeSeed_B`, `SecureInfo_A`, NAND CID and OTP. If your local SysNAND does not contain an embedded backup, you will be asked to do one at startup.
|
||||
* __Actually use that extra NAND space__: You can setup a __bonus drive__ via the HOME menu, which will be available via drive letter `8:`. (Only available on systems that have the extra space.)
|
||||
* __Fix certain problems on your NANDs__: You can fix CMACs for a whole drive (works on `A:`, `B:`, `S:` and `E:`) via an entry in the R+A button menu, or even restore borked NAND headers back to a functional state (inside the A button menu of borked NANDs and available for `S:/nand_hdr.bin`). Recommended only for advanced users!
|
||||
|
||||
### System file handling
|
||||
# System file handling
|
||||
* __Check and fix CMACs (for any file that has them)__: The option will turn up in the A button menu if it is available for a given file (f.e. system savegames, `ticket.db`, etc...). This can also be done for multiple files at once if they are marked.
|
||||
* __Mount ticket.db files and dump tickets__: Mount the file via the A button menu. Tickets are sorted into `eshop` (stuff from eshop), `system` (system tickets), `unknown` (typically empty) and `hidden` (hidden tickets, found via a deeper scan) categories. All tickets displayed are legit, fake tickets are ignored
|
||||
* __Mount ticket.db files and dump tickets__: Mount the file via the A button menu. Tickets are sorted into `eshop` (stuff from eshop, probably legit), `system` (system tickets, probably legit) and `unknown`(everything else, never legit) categories.
|
||||
* __Inject any NCCH CXI file into Health & Safety__: The option is found inside the A button menu for any NCCH CXI file. NCCH CXIs are found, f.e. inside of CIAs. Keep in mind there is a (system internal) size restriction on H&S injectable apps.
|
||||
* __Inject and dump GBA VC saves__: Find the options to do this inside the A button menu for `agbsave.bin` in the `S:` drive. Keep in mind that you need to start the specific GBA game on your console before dumping / injecting the save. _To inject a save it needs to be in the clipboard_.
|
||||
* __Dump a copy of boot9, boot11 & your OTP__: This works on sighax, via boot9strap only. These files are found inside the `M:` drive and can be copied from there to any other place.
|
||||
* __Inject and dump GBA VC saves__: Look for a file called `gbavc.sav` inside the `S:` drive. Keep in mind that you need to start the specific GBA game on your console before dumping / injecting the save.
|
||||
* __Dump a copy of boot9, boot11 & your OTP__: This works on Sighax, via Boot9Strap only. These files are found inside the `M:` drive and can be copied from there to any other place.
|
||||
|
||||
### Support file handling
|
||||
* __Build `decTitleKeys.bin` / `encTitleKeys.bin` / `seeddb.bin`__: Press the HOME button, select `More...` -> `Build support files`. `decTitleKeys.bin` / `encTitleKeys.bin` can also be created / merged from tickets, `ticket.db` and merged from other `decTitleKeys.bin` / `encTitleKeys.bin` files via the A button menu.
|
||||
* __Build, mount, decrypt and encrypt `aeskeydb.bin`__: AES key databases can be merged from other `aeskeydb.bin` or build from legacy `slot??Key?.bin` files. Just select one or more files, press A on one of them and then select `Build aeskeydb.bin`. Options for mounting, decrypting and encrypting are also found in the A button menu.
|
||||
# Support file handling
|
||||
* __Build `decTitleKeys.bin` / `encTitleKeys.bin` / `seeddb.bin`__: Press the HOME button, select `More...` -> `Build support files`. `decTitleKeys.bin` / `encTitleKeys.bin` can also be created / merged from tickets, `ticket.db` and other `decTitleKeys.bin` / `encTitleKeys.bin` files via the A button menu.
|
||||
* __Build, mount, decrypt and encrypt `aeskeydb.bin`__: AES key databases can be built from other `aeskeydb.bin` or legacy `slot??Key?.bin` files. Just select one or more files, press A on one of them and then select `Build aeskeydb.bin`. Options for mounting, decrypting and encrypting are also found in the A button menu.
|
||||
* __Generate XORpads from `ncchinfo.bin` files__: Start this via the appropriate entry inside the A button menu.
|
||||
* __Generate XORpads for any NAND partition__: Take a look inside the `X:` drive. You can use these XORpads for decryption of encrypted NAND images on PC. Additional tools such as [3dsFAT16Tool](https://github.com/d0k3/3DSFAT16tool/releases) are required on PC.
|
||||
|
||||
|
||||
## License
|
||||
You may use this under the terms of the GNU General Public License GPL v2 or under the terms of any later revisions of the GPL. Refer to the provided `LICENSE.txt` file for further information.
|
||||
|
||||
## Contact info
|
||||
You can chat directly with us via IRC @ #GodMode9 on [libera.chat](https://web.libera.chat/#GodMode9) or [Discord](https://discord.gg/BRcbvtFxX4)!
|
||||
|
||||
## Credits
|
||||
This tool would not have been possible without the help of numerous people. Thanks go to (in no particular order)...
|
||||
This tool would not have been possible without the help of numerous people. Thanks go to...
|
||||
* **Archshift**, for providing the base project infrastructure
|
||||
* **Normmatt**, for sdmmc.c / sdmmc.h and gamecart code, and for being of great help on countless other occasions
|
||||
* **Cha(N)**, **Kane49**, and all other FatFS contributors for [FatFS](http://elm-chan.org/fsw/ff/00index_e.html)
|
||||
* **Wolfvak** for ARM11 code, FIRM binary launcher, exception handlers, PCX code, Makefile and for help on countless other occasions
|
||||
* **Normmatt**, for sdmmc.c / sdmmc.h and gamecart code
|
||||
* **Cha(N)**, **Kane49**, and all other FatFS contributors for FatFS
|
||||
* **SciresM** for helping me figure out RomFS and for boot9strap
|
||||
* **SciresM**, **Myria**, **Normmatt**, **TuxSH** and **hedgeberg** for figuring out sighax and giving us access to bootrom
|
||||
* **ihaveamac** for implementing Lua support, and first developing the simple CIA generation method and for being of great help in porting it
|
||||
* **DarkRTA** for linker support during the implementation of Lua
|
||||
* **luigoalma** for fixing Lua to compile without issues
|
||||
* **Gruetzig** for re-implementing the Lua os module
|
||||
* **wwylele** and **aspargas2** for documenting and implementing the DISA, DIFF, and BDRI formats
|
||||
* **dratini0** for savefile management, based on [TWLSaveTool](https://github.com/TuxSH/TWLSaveTool/)
|
||||
* **Pk11** for unicode support and her ongoing work on GodMode9 translations and translation support
|
||||
* **b1l1s** for helping me figure out A9LH compatibility
|
||||
* **Gelex** and **AuroraWright** for helping me figure out various things
|
||||
* **stuckpixel** for the new 6x10 font and help on various things
|
||||
* **Al3x_10m** for help with countless hours of testing and useful advice
|
||||
* **WinterMute** for helping me with his vast knowledge on everything gamecart related
|
||||
* **profi200** for always useful advice and helpful hints on various things
|
||||
* **windows-server-2003** for the initial implementation of if-else-goto in .gm9 scripts
|
||||
* **Kazuma77** for pushing forward scripting, for testing and for always useful advice
|
||||
* **TurdPooCharger** for being one of the most meticulous software testers around
|
||||
* **JaySea**, **YodaDaCoda**, **liomajor**, **Supster131**, **imanoob**, **Kasher_CS** and countless others from freenode #Cakey and the GBAtemp forums for testing, feedback and helpful hints
|
||||
* **dark_samus** for the new 6x10 font and help on various things
|
||||
* **Wolfvak** for the ARM9 binary launcher and other improvements
|
||||
* **WinterMute** and **YodaDaCoda** for help testing DSi cart dumping
|
||||
* **Al3x_10m**, **Supster131**, **imanoob**, **Kasher_CS** and all other fearless testers
|
||||
* **Shadowhand** for being awesome and [hosting my nightlies](https://d0k3.secretalgorithm.com/)
|
||||
* **Plailect** for putting his trust in my tools and recommending this in [The Guide](https://3ds.guide/)
|
||||
* **SirNapkin1334** for testing, bug reports and for hosting the original GodMode9 Discord server
|
||||
* **Lilith Valentine** for testing and helpful advice
|
||||
* **Project Nayuki** for [qrcodegen](https://github.com/nayuki/QR-Code-generator)
|
||||
* **Amazingmax fonts** for the Amazdoom font
|
||||
* **TakWolf** for [fusion-pixel-font](https://github.com/TakWolf/fusion-pixel-font) used for Chinese and Korean
|
||||
* The fine folks on **the official GodMode9 IRC channel and Discord server**
|
||||
* The fine folks on **freenode #Cakey**
|
||||
* All **[3dbrew.org](https://www.3dbrew.org/wiki/Main_Page) editors**
|
||||
* Everyone I possibly forgot, if you think you deserve to be mentioned, just contact us!
|
||||
* Everyone I possibly forgot, if you think you deserve to be mentioned, just contact me!
|
||||
|
@ -1,17 +0,0 @@
|
||||
PROCESSOR := ARM11
|
||||
|
||||
TARGET := $(shell basename "$(CURDIR)")
|
||||
|
||||
SOURCE := source
|
||||
BUILD := build
|
||||
|
||||
SUBARCH := -D$(PROCESSOR) -march=armv6k -mtune=mpcore -marm -mfloat-abi=hard -mfpu=vfpv2 -mtp=soft
|
||||
INCDIRS := source
|
||||
INCLUDE := $(foreach dir,$(INCDIRS),-I"$(shell pwd)/$(dir)")
|
||||
|
||||
ASFLAGS += $(SUBARCH) $(INCLUDE)
|
||||
CFLAGS += $(SUBARCH) $(INCLUDE) -flto
|
||||
LDFLAGS += $(SUBARCH) -Wl,--use-blx,-Map,$(TARGET).map -flto
|
||||
|
||||
include ../Makefile.common
|
||||
include ../Makefile.build
|
@ -1,57 +0,0 @@
|
||||
OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")
|
||||
OUTPUT_ARCH(arm)
|
||||
ENTRY(__boot)
|
||||
|
||||
MEMORY
|
||||
{
|
||||
AXIWRAM (RWX) : ORIGIN = 0x1FF80000, LENGTH = 96K
|
||||
HIGHRAM (RWX) : ORIGIN = 0xFFFF0000, LENGTH = 4K
|
||||
}
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
.text : ALIGN(4K)
|
||||
{
|
||||
__text_pa = LOADADDR(.text);
|
||||
__text_va = ABSOLUTE(.);
|
||||
*(.text*)
|
||||
. = ALIGN(4K);
|
||||
__text_va_end = .;
|
||||
} >AXIWRAM
|
||||
|
||||
.data : ALIGN(4K)
|
||||
{
|
||||
__data_pa = LOADADDR(.data);
|
||||
__data_va = ABSOLUTE(.);
|
||||
*(.data*)
|
||||
. = ALIGN(4K);
|
||||
__data_va_end = .;
|
||||
} >AXIWRAM
|
||||
|
||||
.rodata : ALIGN(4K)
|
||||
{
|
||||
__rodata_pa = LOADADDR(.rodata);
|
||||
__rodata_va = ABSOLUTE(.);
|
||||
*(.rodata*)
|
||||
. = ALIGN(4K);
|
||||
__rodata_va_end = .;
|
||||
} >AXIWRAM
|
||||
|
||||
.shared (NOLOAD) : ALIGN(4K)
|
||||
{
|
||||
__shared_pa = LOADADDR(.shared);
|
||||
__shared_va = ABSOLUTE(.);
|
||||
*(.shared*)
|
||||
. = ALIGN(4K);
|
||||
__shared_va_end = .;
|
||||
} >AXIWRAM
|
||||
|
||||
.bss (NOLOAD) : ALIGN(4K)
|
||||
{
|
||||
__bss_pa = LOADADDR(.bss);
|
||||
__bss_va = ABSOLUTE(.);
|
||||
*(.bss*)
|
||||
. = ALIGN(4K);
|
||||
__bss_va_end = .;
|
||||
} >AXIWRAM
|
||||
}
|
@ -1,300 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2017-2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#include <common.h>
|
||||
#include <arm.h>
|
||||
|
||||
#include <stdatomic.h>
|
||||
|
||||
#include "arm/gic.h"
|
||||
|
||||
#include "system/event.h"
|
||||
|
||||
/* Generic Interrupt Controller Registers */
|
||||
#define REG_GIC(cpu, o, t) REG_ARM_PMR(0x200 + ((cpu) * 0x100) + (o), t)
|
||||
|
||||
#define REG_GIC_CONTROL(c) (*REG_GIC(c, 0x00, u32))
|
||||
#define REG_GIC_PRIOMASK(c) (*REG_GIC(c, 0x04, u32))
|
||||
#define REG_GIC_POI(c) (*REG_GIC(c, 0x08, u32))
|
||||
#define REG_GIC_IRQACK(c) (*REG_GIC(c, 0x0C, u32))
|
||||
#define REG_GIC_IRQEND(c) (*REG_GIC(c, 0x10, u32))
|
||||
#define REG_GIC_LASTPRIO(c) (*REG_GIC(c, 0x14, u32))
|
||||
#define REG_GIC_PENDING(c) (*REG_GIC(c, 0x18, u32))
|
||||
|
||||
#define GIC_THIS_CPU_ALIAS (-1)
|
||||
#define GIC_IRQ_SPURIOUS (1023)
|
||||
|
||||
/* Interrupt Distributor Registers */
|
||||
#define REG_DIC(off, type) REG_ARM_PMR(0x1000 + (off), type)
|
||||
|
||||
#define REG_DIC_CONTROL (*REG_DIC(0x00, u32))
|
||||
#define REG_DIC_TYPE (*REG_DIC(0x04, u32))
|
||||
#define REG_DIC_SETENABLE REG_DIC(0x100, u32) // 32 intcfg per reg
|
||||
#define REG_DIC_CLRENABLE REG_DIC(0x180, u32)
|
||||
#define REG_DIC_SETPENDING REG_DIC(0x200, u32)
|
||||
#define REG_DIC_CLRPENDING REG_DIC(0x280, u32)
|
||||
#define REG_DIC_PRIORITY REG_DIC(0x400, u8) // 1 intcfg per reg (in upper 4 bits)
|
||||
#define REG_DIC_TARGETCPU REG_DIC(0x800, u8) // 1 intcfg per reg
|
||||
#define REG_DIC_CFGREG REG_DIC(0xC00, u32) // 16 intcfg per reg
|
||||
#define REG_DIC_SOFTINT (*REG_DIC(0xF00, u32))
|
||||
|
||||
// used only on reset routines
|
||||
#define REG_DIC_PRIORITY32 REG_DIC(0x400, u32) // 4 intcfg per reg (in upper 4 bits)
|
||||
#define REG_DIC_TARGETCPU32 REG_DIC(0x800, u32) // 4 intcfg per reg
|
||||
|
||||
#define GIC_PRIO_NEVER32 \
|
||||
(GIC_PRIO_NEVER | (GIC_PRIO_NEVER << 8) | \
|
||||
(GIC_PRIO_NEVER << 16) | (GIC_PRIO_NEVER << 24))
|
||||
#define GIC_PRIO_HIGH32 \
|
||||
(GIC_PRIO_HIGHEST | (GIC_PRIO_HIGHEST << 8) | \
|
||||
(GIC_PRIO_HIGHEST << 16) | (GIC_PRIO_HIGHEST << 24))
|
||||
|
||||
/* CPU source ID is present in Interrupt Acknowledge register? */
|
||||
#define IRQN_SRC_MASK (0x7 << 10)
|
||||
|
||||
/* Interrupt Handling */
|
||||
#define LOCAL_IRQS (32)
|
||||
#define DIC_MAX_IRQ (LOCAL_IRQS + MAX_IRQ)
|
||||
|
||||
#define COREMASK_VALID(x) (((x) > 0) && ((x) < BIT(MAX_CPU)))
|
||||
|
||||
#define IRQN_IS_VALID(n) ((n) < DIC_MAX_IRQ)
|
||||
|
||||
static gicIrqHandler gicIrqHandlers[DIC_MAX_IRQ];
|
||||
static _Atomic(u32) gicIrqPending[DIC_MAX_IRQ / 32];
|
||||
|
||||
static struct {
|
||||
u8 tgt;
|
||||
u8 prio;
|
||||
} gicIrqConfig[DIC_MAX_IRQ];
|
||||
|
||||
// gets used whenever a NULL pointer is passed to gicEnableInterrupt
|
||||
static void gicDummyHandler(u32 irqn) { (void)irqn; return; }
|
||||
|
||||
static const struct {
|
||||
u8 low, high, mode;
|
||||
} gicDefaultIrqCfg[] = {
|
||||
{ .low = 0x00, .high = 0x1F, .mode = GIC_RISINGEDGE_NN },
|
||||
{ .low = 0x20, .high = 0x23, .mode = GIC_LEVELHIGH_1N },
|
||||
{ .low = 0x24, .high = 0x24, .mode = GIC_RISINGEDGE_1N },
|
||||
{ .low = 0x25, .high = 0x27, .mode = GIC_LEVELHIGH_1N },
|
||||
{ .low = 0x28, .high = 0x2D, .mode = GIC_RISINGEDGE_1N },
|
||||
{ .low = 0x30, .high = 0x3B, .mode = GIC_LEVELHIGH_1N },
|
||||
{ .low = 0x40, .high = 0x4E, .mode = GIC_RISINGEDGE_1N },
|
||||
{ .low = 0x4F, .high = 0x4F, .mode = GIC_LEVELHIGH_1N },
|
||||
{ .low = 0x50, .high = 0x57, .mode = GIC_RISINGEDGE_1N },
|
||||
{ .low = 0x58, .high = 0x58, .mode = GIC_LEVELHIGH_1N },
|
||||
{ .low = 0x59, .high = 0x75, .mode = GIC_RISINGEDGE_1N },
|
||||
{ .low = 0x76, .high = 0x77, .mode = GIC_LEVELHIGH_1N },
|
||||
{ .low = 0x78, .high = 0x78, .mode = GIC_RISINGEDGE_1N },
|
||||
{ .low = 0x79, .high = 0x7d, .mode = GIC_LEVELHIGH_1N },
|
||||
};
|
||||
|
||||
static u8 gicGetDefaultIrqCfg(u32 irqn) {
|
||||
for (unsigned i = 0; i < countof(gicDefaultIrqCfg); i++) {
|
||||
if ((irqn >= gicDefaultIrqCfg[i].low) && (irqn <= gicDefaultIrqCfg[i].high))
|
||||
return gicDefaultIrqCfg[i].mode;
|
||||
}
|
||||
// TODO: would it be considerably faster to use bsearch?
|
||||
return GIC_RISINGEDGE_1N;
|
||||
}
|
||||
|
||||
void gicTopHandler(void)
|
||||
{
|
||||
while(1) {
|
||||
u32 irqn, irqsource, index, mask;
|
||||
|
||||
/**
|
||||
If more than one of these CPUs reads the Interrupt Acknowledge Register at the
|
||||
same time, they can all acknowledge the same interrupt. The interrupt service
|
||||
routine must ensure that only one of them tries to process the interrupt, with the
|
||||
others returning after writing the ID to the End of Interrupt Register.
|
||||
*/
|
||||
irqsource = REG_GIC_IRQACK(GIC_THIS_CPU_ALIAS);
|
||||
|
||||
if (irqsource == GIC_IRQ_SPURIOUS) // no further processing is needed
|
||||
break;
|
||||
|
||||
irqn = irqsource & ~IRQN_SRC_MASK;
|
||||
|
||||
index = irqn / 32;
|
||||
mask = BIT(irqn % 32);
|
||||
atomic_fetch_or(&gicIrqPending[index], mask);
|
||||
|
||||
(gicIrqHandlers[irqn])(irqsource);
|
||||
// if the id is < 16, the source CPU can be obtained from irqn
|
||||
// if the handler isn't set, it'll try to branch to 0 and trigger a prefetch abort
|
||||
|
||||
REG_GIC_IRQEND(GIC_THIS_CPU_ALIAS) = irqsource;
|
||||
}
|
||||
}
|
||||
|
||||
void gicGlobalReset(void)
|
||||
{
|
||||
u32 dic_type;
|
||||
unsigned gicn, intn;
|
||||
|
||||
dic_type = REG_DIC_TYPE;
|
||||
|
||||
// number of local controllers
|
||||
gicn = ((dic_type >> 5) & 3) + 1;
|
||||
|
||||
// number of interrupt lines (up to 224 external + 32 fixed internal per CPU)
|
||||
intn = ((dic_type & 7) + 1) * 32;
|
||||
|
||||
// clamp it down to the amount of CPUs designed to handle
|
||||
if (gicn > MAX_CPU)
|
||||
gicn = MAX_CPU;
|
||||
|
||||
// clear the interrupt handler and config table
|
||||
getEventIRQ()->reset();
|
||||
memset(gicIrqHandlers, 0, sizeof(gicIrqHandlers));
|
||||
memset(gicIrqConfig, 0, sizeof(gicIrqConfig));
|
||||
|
||||
// disable all MP11 GICs
|
||||
for (unsigned i = 0; i < gicn; i++)
|
||||
REG_GIC_CONTROL(i) = 0;
|
||||
|
||||
// disable the main DIC
|
||||
REG_DIC_CONTROL = 0;
|
||||
|
||||
// clear all external interrupts
|
||||
for (unsigned i = 1; i < (intn / 32); i++) {
|
||||
REG_DIC_CLRENABLE[i] = ~0;
|
||||
REG_DIC_CLRPENDING[i] = ~0;
|
||||
}
|
||||
|
||||
// reset all external priorities to highest by default
|
||||
// clear target processor regs
|
||||
for (unsigned i = 4; i < (intn / 4); i++) {
|
||||
REG_DIC_PRIORITY32[i] = GIC_PRIO_HIGH32;
|
||||
REG_DIC_TARGETCPU32[i] = 0;
|
||||
}
|
||||
|
||||
// set all interrupts to active level triggered in N-N model
|
||||
for (unsigned i = 16; i < (intn / 16); i++)
|
||||
REG_DIC_CFGREG[i] = 0;
|
||||
|
||||
// re enable the main DIC
|
||||
REG_DIC_CONTROL = 1;
|
||||
|
||||
for (unsigned i = 0; i < gicn; i++) {
|
||||
// compare all priority bits
|
||||
REG_GIC_POI(i) = 3;
|
||||
|
||||
// don't mask any interrupt with low priority
|
||||
REG_GIC_PRIOMASK(i) = 0xF0;
|
||||
|
||||
// enable all the MP11 GICs
|
||||
REG_GIC_CONTROL(i) = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void gicLocalReset(void)
|
||||
{
|
||||
u32 irq_s;
|
||||
|
||||
// disable all local interrupts
|
||||
REG_DIC_CLRENABLE[0] = ~0;
|
||||
REG_DIC_CLRPENDING[0] = ~0;
|
||||
|
||||
for (unsigned i = 0; i < 4; i++) {
|
||||
REG_DIC_PRIORITY32[i] = GIC_PRIO_HIGH32;
|
||||
// local IRQs are always unmasked by default
|
||||
|
||||
// REG_DIC_TARGETCPU[i] = 0;
|
||||
// not needed, always read as corresponding MP11 core
|
||||
}
|
||||
|
||||
// ack until it gets a spurious IRQ
|
||||
do {
|
||||
irq_s = REG_GIC_PENDING(GIC_THIS_CPU_ALIAS);
|
||||
REG_GIC_IRQEND(GIC_THIS_CPU_ALIAS) = irq_s;
|
||||
} while(irq_s != GIC_IRQ_SPURIOUS);
|
||||
}
|
||||
|
||||
static void gicSetIrqCfg(u32 irqn) {
|
||||
u32 smt, cfg;
|
||||
|
||||
smt = irqn & 15;
|
||||
cfg = REG_DIC_CFGREG[irqn / 16];
|
||||
cfg &= ~(3 << smt);
|
||||
cfg |= gicGetDefaultIrqCfg(irqn) << smt;
|
||||
REG_DIC_CFGREG[irqn / 16] = cfg;
|
||||
}
|
||||
|
||||
void gicSetInterruptConfig(u32 irqn, u32 coremask, u32 prio, gicIrqHandler handler)
|
||||
{
|
||||
if (handler == NULL) // maybe add runtime ptr checks here too?
|
||||
handler = gicDummyHandler;
|
||||
|
||||
gicIrqConfig[irqn].tgt = coremask;
|
||||
gicIrqConfig[irqn].prio = prio;
|
||||
gicIrqHandlers[irqn] = handler;
|
||||
}
|
||||
|
||||
void gicClearInterruptConfig(u32 irqn)
|
||||
{
|
||||
memset(&gicIrqConfig[irqn], 0, sizeof(gicIrqConfig[irqn]));
|
||||
gicIrqHandlers[irqn] = NULL;
|
||||
}
|
||||
|
||||
void gicEnableInterrupt(u32 irqn)
|
||||
{
|
||||
REG_DIC_PRIORITY[irqn] = gicIrqConfig[irqn].prio;
|
||||
REG_DIC_TARGETCPU[irqn] = gicIrqConfig[irqn].tgt;
|
||||
gicSetIrqCfg(irqn);
|
||||
|
||||
REG_DIC_CLRPENDING[irqn / 32] |= BIT(irqn & 0x1F);
|
||||
REG_DIC_SETENABLE[irqn / 32] |= BIT(irqn & 0x1F);
|
||||
}
|
||||
|
||||
void gicDisableInterrupt(u32 irqn)
|
||||
{
|
||||
REG_DIC_CLRENABLE[irqn / 32] |= BIT(irqn & 0x1F);
|
||||
REG_DIC_CLRPENDING[irqn / 32] |= BIT(irqn & 0x1F);
|
||||
}
|
||||
|
||||
void gicTriggerSoftInterrupt(u32 softirq)
|
||||
{
|
||||
REG_DIC_SOFTINT = softirq;
|
||||
}
|
||||
|
||||
static void irqEvReset(void) {
|
||||
memset(&gicIrqPending, 0, sizeof(gicIrqPending));
|
||||
}
|
||||
|
||||
static u32 irqEvTest(u32 param, u32 clear) {
|
||||
u32 index, tstmask, clrmask;
|
||||
|
||||
if (param >= DIC_MAX_IRQ)
|
||||
bkpt;
|
||||
index = param / 32;
|
||||
tstmask = BIT(param % 32);
|
||||
clrmask = clear ? tstmask : 0;
|
||||
return !!(atomic_fetch_and(&gicIrqPending[index], ~clrmask) & tstmask);
|
||||
}
|
||||
|
||||
static const EventInterface evIRQ = {
|
||||
.reset = irqEvReset,
|
||||
.test = irqEvTest
|
||||
};
|
||||
|
||||
const EventInterface *getEventIRQ(void) {
|
||||
return &evIRQ;
|
||||
}
|
@ -1,112 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2017-2020 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <common.h>
|
||||
#include <arm.h>
|
||||
|
||||
typedef void (*gicIrqHandler)(u32 irqn);
|
||||
|
||||
enum {
|
||||
GIC_LEVELHIGH_NN = 0, // no interrupts use level high triggers so far
|
||||
GIC_LEVELHIGH_1N = 1,
|
||||
|
||||
GIC_RISINGEDGE_NN = 2,
|
||||
GIC_RISINGEDGE_1N = 3
|
||||
|
||||
// With the 1-N model, an interrupt that is taken on any CPU clears the Pending
|
||||
// status on all CPUs.
|
||||
// With the N-N model, all CPUs receive the interrupt independently. The Pending
|
||||
// status is cleared only for the CPU that takes it, not for the other CPUs
|
||||
};
|
||||
|
||||
enum {
|
||||
GIC_PRIO0 = 0x00,
|
||||
GIC_PRIO1 = 0x10,
|
||||
GIC_PRIO2 = 0x20,
|
||||
GIC_PRIO3 = 0x30,
|
||||
GIC_PRIO4 = 0x40,
|
||||
GIC_PRIO5 = 0x50,
|
||||
GIC_PRIO6 = 0x60,
|
||||
GIC_PRIO7 = 0x70,
|
||||
GIC_PRIO14 = 0xE0,
|
||||
GIC_PRIO15 = 0xF0,
|
||||
};
|
||||
|
||||
#define GIC_PRIO_HIGHEST GIC_PRIO0
|
||||
#define GIC_PRIO_LOWEST GIC_PRIO14
|
||||
#define GIC_PRIO_NEVER GIC_PRIO15
|
||||
|
||||
void gicGlobalReset(void);
|
||||
void gicLocalReset(void);
|
||||
|
||||
/*
|
||||
Notes from https://static.docs.arm.com/ddi0360/f/DDI0360F_arm11_mpcore_r2p0_trm.pdf
|
||||
|
||||
INTERRUPT ENABLE:
|
||||
Interrupts 0-15 fields are read as one, that is, always enabled, and write to these fields
|
||||
have no effect.
|
||||
Notpresent interrupts (depending on the Interrupt Controller Type Register and
|
||||
interrupt number field) related fields are read as zero and writes to these fields have no
|
||||
effect.
|
||||
|
||||
INTERRUPT PRIORITY:
|
||||
The first four registers are aliased for each MP11 CPU, that is, the priority set for
|
||||
ID0-15 and ID29-31 can be different for each MP11 CPU. The priority of IPIs ID0-15
|
||||
depends on the receiving CPU, not the sending CPU.
|
||||
|
||||
INTERRUPT CPU TARGET:
|
||||
For MP11 CPU n, CPU targets 29, 30 and 31 are read as (1 << n). Writes are ignored.
|
||||
For IT0-IT28, these fields are read as zero and writes are ignored.
|
||||
|
||||
INTERRUPT CONFIGURATION:
|
||||
For ID0-ID15, bit 1 of the configuration pair is always read as one, that is, rising edge
|
||||
sensitive.
|
||||
For ID0-ID15, bit 0 (software model) can be configured and applies to the interrupts
|
||||
sent from the writing MP11 CPU.
|
||||
For ID29, and ID30, the configuration pair is always read as b10, that is rising edge
|
||||
sensitive and N-N software model because these IDs are allocated to timer and
|
||||
watchdog interrupts that are CPU-specific
|
||||
*/
|
||||
|
||||
#define COREMASK_ALL (BIT(MAX_CPU) - 1)
|
||||
|
||||
void gicSetInterruptConfig(u32 irqn, u32 coremask, u32 prio, gicIrqHandler handler);
|
||||
void gicClearInterruptConfig(u32 irqn);
|
||||
|
||||
void gicEnableInterrupt(u32 irqn);
|
||||
void gicDisableInterrupt(u32 irqn);
|
||||
|
||||
enum {
|
||||
GIC_SOFTIRQ_LIST = 0,
|
||||
GIC_SOFTIRQ_OTHERS = 1, // all except self
|
||||
GIC_SOFTIRQ_SELF = 2,
|
||||
};
|
||||
|
||||
#define GIC_SOFTIRQ_SOURCE(n) (((n) >> 10) & 0xF)
|
||||
#define GIC_SOFTIRQ_ID(n) ((n) & 0x3FF)
|
||||
|
||||
#define GIC_SOFTIRQ_FMT(id, filter, coremask) \
|
||||
((id) | ((coremask) << 16) | ((filter) << 24))
|
||||
// id & 0xf, coremask & 3, filter & 3
|
||||
// coremask is only used with filter == GIC_SOFTIRQ_LIST
|
||||
|
||||
#define GIC_SOFTIRQ_SRC(x) (((x) >> 10) % MAX_CPU)
|
||||
|
||||
void gicTriggerSoftInterrupt(u32 softirq);
|
@ -1,279 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2018-2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#include <types.h>
|
||||
#include <common.h>
|
||||
|
||||
#include <arm.h>
|
||||
|
||||
#include "arm/mmu.h"
|
||||
|
||||
/* Virtual Memory Mapper */
|
||||
#define L1_VA_IDX(v) (((v) >> 20) & 0xFFF)
|
||||
#define L2_VA_IDX(v) (((v) >> 12) & 0xFF)
|
||||
|
||||
#define SECT_ADDR_SHIFT (20)
|
||||
#define COARSE_ADDR_SHIFT (10)
|
||||
#define PAGE_ADDR_SHIFT (12)
|
||||
#define LPAGE_ADDR_SHIFT (16)
|
||||
|
||||
#define SECT_SIZE (BIT(SECT_ADDR_SHIFT))
|
||||
#define COARSE_SIZE (BIT(COARSE_ADDR_SHIFT))
|
||||
#define PAGE_SIZE (BIT(PAGE_ADDR_SHIFT))
|
||||
#define LPAGE_SIZE (BIT(LPAGE_ADDR_SHIFT))
|
||||
|
||||
#define SECT_MASK (~(SECT_SIZE - 1))
|
||||
#define COARSE_MASK (~(COARSE_SIZE - 1))
|
||||
#define PAGE_MASK (~(PAGE_SIZE - 1))
|
||||
#define LPAGE_MASK (~(LPAGE_SIZE - 1))
|
||||
|
||||
#define DESCRIPTOR_L1_UNMAPPED (0)
|
||||
#define DESCRIPTOR_L1_COARSE (1)
|
||||
#define DESCRIPTOR_L1_SECTION (2)
|
||||
#define DESCRIPTOR_L1_RESERVED (3)
|
||||
|
||||
#define DESCRIPTOR_L2_UNMAPPED (0)
|
||||
#define DESCRIPTOR_L2_LARGEPAGE (1)
|
||||
#define DESCRIPTOR_L2_PAGE_EXEC (2)
|
||||
#define DESCRIPTOR_L2_PAGE_NX (3)
|
||||
|
||||
#define DESCRIPTOR_TYPE_MASK (3)
|
||||
|
||||
enum {
|
||||
L1_UNMAPPED,
|
||||
L1_COARSE,
|
||||
L1_SECTION,
|
||||
L1_RESERVED,
|
||||
|
||||
L2_UNMAPPED,
|
||||
L2_LARGEPAGE,
|
||||
L2_PAGE,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
u32 desc[4096];
|
||||
} __attribute__((aligned(16384))) mmuLevel1Table;
|
||||
|
||||
typedef struct {
|
||||
u32 desc[256];
|
||||
} __attribute__((aligned(1024))) mmuLevel2Table;
|
||||
|
||||
static mmuLevel1Table mmuGlobalTT;
|
||||
|
||||
// simple watermark allocator for 2nd level page tables
|
||||
#define MAX_SECOND_LEVEL (8)
|
||||
static mmuLevel2Table mmuCoarseTables[MAX_SECOND_LEVEL];
|
||||
static u32 mmuCoarseAllocated = 0;
|
||||
static mmuLevel2Table *mmuAllocateLevel2Table(void)
|
||||
{
|
||||
return &mmuCoarseTables[mmuCoarseAllocated++];
|
||||
}
|
||||
|
||||
|
||||
// functions to convert from internal page flag format to ARM
|
||||
|
||||
// {TEX, CB} pairs
|
||||
static const u8 mmuTypeLUT[MMU_MEMORY_TYPES][2] = {
|
||||
[MMU_STRONG_ORDER] = {0, 0},
|
||||
[MMU_UNCACHEABLE] = {1, 0},
|
||||
[MMU_DEV_SHARED] = {0, 1},
|
||||
[MMU_DEV_NONSHARED] = {2, 0},
|
||||
[MMU_CACHE_WT] = {0, 2},
|
||||
[MMU_CACHE_WB] = {1, 3},
|
||||
[MMU_CACHE_WBA] = {1, 3},
|
||||
};
|
||||
|
||||
static u32 mmuGetTEX(u32 f)
|
||||
{ return mmuTypeLUT[MMU_FLAGS_TYPE(f)][0]; }
|
||||
static u32 mmuGetCB(u32 f)
|
||||
{ return mmuTypeLUT[MMU_FLAGS_TYPE(f)][1]; }
|
||||
static u32 mmuGetNX(u32 f)
|
||||
{ return MMU_FLAGS_NOEXEC(f) ? 1 : 0; }
|
||||
static u32 mmuGetShared(u32 f)
|
||||
{ return MMU_FLAGS_SHARED(f) ? 1 : 0; }
|
||||
|
||||
// access permissions
|
||||
static const u8 mmuAccessLUT[MMU_ACCESS_TYPES] = {
|
||||
[MMU_NO_ACCESS] = 0,
|
||||
[MMU_READ_ONLY] = 0x21,
|
||||
[MMU_READ_WRITE] = 0x01,
|
||||
};
|
||||
|
||||
static u32 mmuGetAP(u32 f)
|
||||
{ return mmuAccessLUT[MMU_FLAGS_ACCESS(f)]; }
|
||||
|
||||
// other misc helper functions
|
||||
static unsigned mmuWalkTT(u32 va)
|
||||
{
|
||||
mmuLevel2Table *coarsepd;
|
||||
u32 desc = mmuGlobalTT.desc[L1_VA_IDX(va)];
|
||||
|
||||
switch(desc & DESCRIPTOR_TYPE_MASK) {
|
||||
case DESCRIPTOR_L1_UNMAPPED:
|
||||
return L1_UNMAPPED;
|
||||
|
||||
case DESCRIPTOR_L1_COARSE:
|
||||
break;
|
||||
|
||||
case DESCRIPTOR_L1_SECTION:
|
||||
return L1_SECTION;
|
||||
|
||||
case DESCRIPTOR_L1_RESERVED:
|
||||
return L1_RESERVED;
|
||||
}
|
||||
|
||||
coarsepd = (mmuLevel2Table*)(desc & COARSE_MASK);
|
||||
desc = coarsepd->desc[L2_VA_IDX(va)];
|
||||
|
||||
switch(desc & DESCRIPTOR_TYPE_MASK) {
|
||||
default:
|
||||
case DESCRIPTOR_L2_UNMAPPED:
|
||||
return L2_UNMAPPED;
|
||||
|
||||
case DESCRIPTOR_L2_LARGEPAGE:
|
||||
return L2_LARGEPAGE;
|
||||
|
||||
case DESCRIPTOR_L2_PAGE_NX:
|
||||
case DESCRIPTOR_L2_PAGE_EXEC:
|
||||
return L2_PAGE;
|
||||
}
|
||||
}
|
||||
|
||||
static mmuLevel2Table *mmuCoarseFix(u32 va)
|
||||
{
|
||||
u32 type;
|
||||
mmuLevel2Table *coarsepd;
|
||||
|
||||
type = mmuWalkTT(va);
|
||||
switch(type) {
|
||||
case L1_UNMAPPED:
|
||||
coarsepd = mmuAllocateLevel2Table();
|
||||
mmuGlobalTT.desc[L1_VA_IDX(va)] = (u32)coarsepd | DESCRIPTOR_L1_COARSE;
|
||||
break;
|
||||
|
||||
case L2_UNMAPPED:
|
||||
coarsepd = (mmuLevel2Table*)(mmuGlobalTT.desc[L1_VA_IDX(va)] & COARSE_MASK);
|
||||
break;
|
||||
|
||||
default:
|
||||
coarsepd = NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
return coarsepd;
|
||||
}
|
||||
|
||||
|
||||
/* Sections */
|
||||
static u32 mmuSectionFlags(u32 f)
|
||||
{ // converts the internal format to the hardware L1 section format
|
||||
return (mmuGetShared(f) << 16) | (mmuGetTEX(f) << 12) |
|
||||
(mmuGetAP(f) << 10) | (mmuGetNX(f) << 4) |
|
||||
(mmuGetCB(f) << 2) | DESCRIPTOR_L1_SECTION;
|
||||
}
|
||||
|
||||
static void mmuMapSection(u32 va, u32 pa, u32 flags)
|
||||
{
|
||||
mmuGlobalTT.desc[L1_VA_IDX(va)] = pa | mmuSectionFlags(flags);
|
||||
}
|
||||
|
||||
|
||||
/* Pages */
|
||||
static u32 mmuPageFlags(u32 f)
|
||||
{
|
||||
return (mmuGetShared(f) << 10) | (mmuGetTEX(f) << 6) |
|
||||
(mmuGetAP(f) << 4) | (mmuGetCB(f) << 2) |
|
||||
(mmuGetNX(f) ? DESCRIPTOR_L2_PAGE_NX : DESCRIPTOR_L2_PAGE_EXEC);
|
||||
}
|
||||
|
||||
static void mmuMapPage(u32 va, u32 pa, u32 flags)
|
||||
{
|
||||
mmuLevel2Table *l2 = mmuCoarseFix(va);
|
||||
l2->desc[L2_VA_IDX(va)] = pa | mmuPageFlags(flags);
|
||||
}
|
||||
|
||||
|
||||
static bool mmuMappingFits(u32 va, u32 pa, u32 sz, u32 alignment)
|
||||
{
|
||||
return !((va | pa | sz) & (alignment));
|
||||
}
|
||||
|
||||
u32 mmuMapArea(u32 va, u32 pa, u32 size, u32 flags)
|
||||
{
|
||||
static const struct {
|
||||
u32 size;
|
||||
void (*mapfn)(u32,u32,u32);
|
||||
} VMappers[] = {
|
||||
{
|
||||
.size = BIT(SECT_ADDR_SHIFT),
|
||||
.mapfn = mmuMapSection,
|
||||
},
|
||||
{
|
||||
.size = BIT(PAGE_ADDR_SHIFT),
|
||||
.mapfn = mmuMapPage,
|
||||
},
|
||||
};
|
||||
|
||||
while(size > 0) {
|
||||
size_t i = 0;
|
||||
for (i = 0; i < countof(VMappers); i++) {
|
||||
u32 pgsize = VMappers[i].size;
|
||||
|
||||
if (mmuMappingFits(va, pa, size, pgsize-1)) {
|
||||
(VMappers[i].mapfn)(va, pa, flags);
|
||||
|
||||
va += pgsize;
|
||||
pa += pgsize;
|
||||
size -= pgsize;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* alternatively return the unmapped remaining size:
|
||||
if (i == countof(VMappers))
|
||||
return size;
|
||||
*/
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mmuInvalidate(void)
|
||||
{
|
||||
ARM_MCR(p15, 0, 0, c8, c7, 0);
|
||||
}
|
||||
|
||||
void mmuInvalidateVA(u32 addr)
|
||||
{
|
||||
ARM_MCR(p15, 0, addr, c8, c7, 2);
|
||||
}
|
||||
|
||||
void mmuInitRegisters(void)
|
||||
{
|
||||
u32 ttbr0 = (u32)(&mmuGlobalTT) | 0x12;
|
||||
|
||||
// Set up TTBR0/1 and the TTCR
|
||||
ARM_MCR(p15, 0, ttbr0, c2, c0, 0);
|
||||
ARM_MCR(p15, 0, 0, c2, c0, 1);
|
||||
ARM_MCR(p15, 0, 0, c2, c0, 2);
|
||||
|
||||
// Set up the DACR
|
||||
ARM_MCR(p15, 0, 0x55555555, c3, c0, 0);
|
||||
|
||||
// Invalidate the unified TLB
|
||||
ARM_MCR(p15, 0, 0, c8, c7, 0);
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2018-2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <types.h>
|
||||
|
||||
enum {
|
||||
MMU_STRONG_ORDER = 0,
|
||||
MMU_UNCACHEABLE,
|
||||
MMU_DEV_SHARED,
|
||||
MMU_DEV_NONSHARED,
|
||||
MMU_CACHE_WT,
|
||||
MMU_CACHE_WB,
|
||||
MMU_CACHE_WBA,
|
||||
MMU_MEMORY_TYPES,
|
||||
};
|
||||
|
||||
enum {
|
||||
MMU_NO_ACCESS = 0,
|
||||
MMU_READ_ONLY,
|
||||
MMU_READ_WRITE,
|
||||
MMU_ACCESS_TYPES,
|
||||
};
|
||||
|
||||
#define MMU_FLAGS(t, ap, nx, s) ((s) << 25 | (nx) << 24 | (ap) << 8 | (t))
|
||||
|
||||
#define MMU_FLAGS_TYPE(f) ((f) & 0xFF)
|
||||
#define MMU_FLAGS_ACCESS(f) (((f) >> 8) & 0xFF)
|
||||
|
||||
#define MMU_FLAGS_NOEXEC(f) ((f) & BIT(24))
|
||||
#define MMU_FLAGS_SHARED(f) ((f) & BIT(25))
|
||||
|
||||
u32 mmuMapArea(u32 va, u32 pa, u32 size, u32 flags);
|
||||
|
||||
void mmuInvalidate(void);
|
||||
void mmuInvalidateVA(u32 addr); // DO NOT USE
|
||||
|
||||
void mmuInitRegisters(void);
|
@ -1,32 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2018-2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#include <types.h>
|
||||
#include <arm.h>
|
||||
|
||||
#define REG_SCU_CNT (*REG_ARM_PMR(0x00, u32))
|
||||
#define REG_SCU_CFG (*REG_ARM_PMR(0x04, u32))
|
||||
#define REG_SCU_CPU (*REG_ARM_PMR(0x08, u32))
|
||||
#define REG_SCU_INV (*REG_ARM_PMR(0x0C, u32))
|
||||
|
||||
void SCU_Init(void)
|
||||
{
|
||||
REG_SCU_CNT = 0x1FFE;
|
||||
REG_SCU_INV = 0xFFFF;
|
||||
REG_SCU_CNT = 0x3FFF;
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2018-2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <types.h>
|
||||
|
||||
void SCU_Init(void);
|
@ -1,47 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#include <types.h>
|
||||
#include <arm.h>
|
||||
|
||||
#include "arm/gic.h"
|
||||
#include "arm/timer.h"
|
||||
|
||||
#define TIMER_INTERRUPT (0x1E)
|
||||
|
||||
#define REG_TIMER(c, n) REG_ARM_PMR(0x700 + ((c) * 0x100) + (n), u32)
|
||||
#define TIMER_THIS_CPU (-1)
|
||||
|
||||
#define REG_TIMER_LOAD(c) *REG_TIMER((c), 0x00)
|
||||
#define REG_TIMER_COUNT(c) *REG_TIMER((c), 0x04)
|
||||
#define REG_TIMER_CNT(c) *REG_TIMER((c), 0x08)
|
||||
#define REG_TIMER_IRQ(c) *REG_TIMER((c), 0x0C)
|
||||
|
||||
#define TIMER_CNT_SCALE(n) ((n) << 8)
|
||||
#define TIMER_CNT_INT_EN BIT(2)
|
||||
#define TIMER_CNT_RELOAD BIT(1)
|
||||
#define TIMER_CNT_ENABLE BIT(0)
|
||||
|
||||
void TIMER_WaitTicks(u32 ticks)
|
||||
{
|
||||
REG_TIMER_IRQ(TIMER_THIS_CPU) = 1;
|
||||
REG_TIMER_CNT(TIMER_THIS_CPU) = 0;
|
||||
REG_TIMER_LOAD(TIMER_THIS_CPU) = ticks;
|
||||
REG_TIMER_CNT(TIMER_THIS_CPU) = TIMER_CNT_ENABLE;
|
||||
while(REG_TIMER_COUNT(TIMER_THIS_CPU));
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <types.h>
|
||||
|
||||
// The timer interval is calculated using the following equation:
|
||||
// T = [(PRESCALER_value + 1) * (Load_value + 1) * 2] / CPU_CLK
|
||||
// therefore
|
||||
// Load_value = [(CPU_CLK / 2) * (T / (PRESCALER_value + 1))] - 1
|
||||
|
||||
#define BASE_CLKRATE (268111856 / 2)
|
||||
|
||||
#define CLK_MS_TO_TICKS(m) (((BASE_CLKRATE / 1000) * (m)) - 1)
|
||||
|
||||
void TIMER_WaitTicks(u32 ticks);
|
||||
|
||||
static inline void TIMER_WaitMS(u32 ms) {
|
||||
TIMER_WaitTicks(CLK_MS_TO_TICKS(ms));
|
||||
}
|
@ -1,219 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
// kinda hardcoded and all over the place, but it needs to stay simple
|
||||
|
||||
#include <types.h>
|
||||
#include <arm.h>
|
||||
|
||||
#include <vram.h>
|
||||
|
||||
static const u8 num_font[16*8];
|
||||
|
||||
#define SCREEN ((u16*)(VRAM_TOP_LA))
|
||||
|
||||
void draw_char(u16 *fb, int c, int x, int y)
|
||||
{
|
||||
for (int _y = 0; _y < 8; _y++) {
|
||||
for (int _x = 0; _x < 8; _x++) {
|
||||
u16 *fbpos = fb + (240 - (y + _y)) + (240 * (x + _x));
|
||||
|
||||
u8 mask = (num_font[(c * 8) + _y] >> (8 - _x)) & 1;
|
||||
|
||||
if (mask)
|
||||
*fbpos = ~0;
|
||||
else
|
||||
*fbpos = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_hex(u16 *fb, u32 num, int x, int y)
|
||||
{
|
||||
x += 7*8;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
draw_char(fb, num & 0xf, x, y);
|
||||
num >>= 4;
|
||||
x -= 8;
|
||||
}
|
||||
}
|
||||
|
||||
void do_exception(u32 type, u32 *regs)
|
||||
{
|
||||
for (int i = 0; i < 400*240; i++)
|
||||
SCREEN[i] = 0;
|
||||
|
||||
draw_hex(SCREEN, type, 8, 16);
|
||||
|
||||
for (int i = 0; i < 20; i += 2) {
|
||||
draw_hex(SCREEN, i, 8, 32 + (i * 4));
|
||||
draw_hex(SCREEN, regs[i], 80, 32 + (i * 4));
|
||||
|
||||
draw_hex(SCREEN, i + 1, 208, 32 + (i * 4));
|
||||
draw_hex(SCREEN, regs[i + 1], 280, 32 + (i * 4));
|
||||
}
|
||||
|
||||
while(1)
|
||||
ARM_WFI();
|
||||
}
|
||||
|
||||
static const u8 num_font[] = {
|
||||
0b00000000,
|
||||
0b00011000,
|
||||
0b00100100,
|
||||
0b00101100,
|
||||
0b00110100,
|
||||
0b00100100,
|
||||
0b00011000,
|
||||
0b00000000, // 0
|
||||
|
||||
0b00000000,
|
||||
0b00011000,
|
||||
0b00101000,
|
||||
0b00001000,
|
||||
0b00001000,
|
||||
0b00001000,
|
||||
0b00111100,
|
||||
0b00000000, // 1
|
||||
|
||||
0b00000000,
|
||||
0b00011000,
|
||||
0b00100100,
|
||||
0b00000100,
|
||||
0b00001000,
|
||||
0b00010000,
|
||||
0b00111100,
|
||||
0b00000000, // 2
|
||||
|
||||
0b00000000,
|
||||
0b00111000,
|
||||
0b00000100,
|
||||
0b00011000,
|
||||
0b00000100,
|
||||
0b00000100,
|
||||
0b00111000,
|
||||
0b00000000, // 3
|
||||
|
||||
0b00000000,
|
||||
0b00100100,
|
||||
0b00100100,
|
||||
0b00111100,
|
||||
0b00000100,
|
||||
0b00000100,
|
||||
0b00000100,
|
||||
0b00000000, // 4
|
||||
|
||||
0b00000000,
|
||||
0b00111100,
|
||||
0b00100000,
|
||||
0b00111000,
|
||||
0b00000100,
|
||||
0b00000100,
|
||||
0b00111000,
|
||||
0b00000000, // 5
|
||||
|
||||
0b00000000,
|
||||
0b00011100,
|
||||
0b00100000,
|
||||
0b00111000,
|
||||
0b00100100,
|
||||
0b00100100,
|
||||
0b00011000,
|
||||
0b00000000, // 6
|
||||
|
||||
0b00000000,
|
||||
0b00111100,
|
||||
0b00000100,
|
||||
0b00000100,
|
||||
0b00001000,
|
||||
0b00010000,
|
||||
0b00010000,
|
||||
0b00000000, // 7
|
||||
|
||||
0b00000000,
|
||||
0b00011000,
|
||||
0b00100100,
|
||||
0b00011000,
|
||||
0b00100100,
|
||||
0b00100100,
|
||||
0b00011000,
|
||||
0b00000000, // 8
|
||||
|
||||
0b00000000,
|
||||
0b00011000,
|
||||
0b00100100,
|
||||
0b00011100,
|
||||
0b00000100,
|
||||
0b00000100,
|
||||
0b00111000,
|
||||
0b00000000, // 9
|
||||
|
||||
0b00000000,
|
||||
0b00011000,
|
||||
0b00100100,
|
||||
0b00111100,
|
||||
0b00100100,
|
||||
0b00100100,
|
||||
0b00100100,
|
||||
0b00000000, // A
|
||||
|
||||
0b00000000,
|
||||
0b00111000,
|
||||
0b00100100,
|
||||
0b00111000,
|
||||
0b00100100,
|
||||
0b00100100,
|
||||
0b00111000,
|
||||
0b00000000, // B
|
||||
|
||||
0b00000000,
|
||||
0b00011100,
|
||||
0b00100000,
|
||||
0b00100000,
|
||||
0b00100000,
|
||||
0b00100000,
|
||||
0b00011100,
|
||||
0b00000000, // C
|
||||
|
||||
0b00000000,
|
||||
0b00110000,
|
||||
0b00101000,
|
||||
0b00100100,
|
||||
0b00100100,
|
||||
0b00101000,
|
||||
0b00110000,
|
||||
0b00000000, // C
|
||||
|
||||
0b00000000,
|
||||
0b00111100,
|
||||
0b00100000,
|
||||
0b00111100,
|
||||
0b00100000,
|
||||
0b00100000,
|
||||
0b00111100,
|
||||
0b00000000, // E
|
||||
|
||||
0b00000000,
|
||||
0b00111100,
|
||||
0b00100000,
|
||||
0b00111100,
|
||||
0b00100000,
|
||||
0b00100000,
|
||||
0b00100000,
|
||||
0b00000000, // F
|
||||
};
|
@ -1,21 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2020 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
u32 xrqInstallVectorTable(void);
|
@ -1,129 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2017-2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This is almost the same as the ARM9 exception handler,
|
||||
* but with a few extra register dumps (DFSR, IFSR and FAR)
|
||||
*/
|
||||
|
||||
#include <arm.h>
|
||||
|
||||
.arm
|
||||
.align 3
|
||||
|
||||
.macro TRAP_ENTRY xrq
|
||||
msr cpsr_f, #(\xrq << 29)
|
||||
b xrqMain
|
||||
.endm
|
||||
|
||||
xrqVectorTable:
|
||||
ldr pc, =xrqReset
|
||||
ldr pc, =xrqUndefined
|
||||
ldr pc, =xrqSVC
|
||||
ldr pc, =xrqPrefetchAbort
|
||||
ldr pc, =xrqDataAbort
|
||||
b . @ ignore the reserved exception
|
||||
ldr pc, =xrqIRQ
|
||||
ldr pc, =xrqFIQ
|
||||
.pool
|
||||
xrqVectorTableEnd:
|
||||
|
||||
xrqReset:
|
||||
TRAP_ENTRY 0
|
||||
|
||||
xrqUndefined:
|
||||
TRAP_ENTRY 1
|
||||
|
||||
xrqSVC:
|
||||
TRAP_ENTRY 2
|
||||
|
||||
xrqPrefetchAbort:
|
||||
TRAP_ENTRY 3
|
||||
|
||||
xrqDataAbort:
|
||||
TRAP_ENTRY 4
|
||||
|
||||
xrqFIQ:
|
||||
TRAP_ENTRY 7
|
||||
|
||||
xrqMain:
|
||||
clrex
|
||||
cpsid aif
|
||||
|
||||
ldr sp, =(xrqStackTop - 32*4)
|
||||
stmia sp, {r0-r7}
|
||||
|
||||
mrs r1, cpsr
|
||||
lsr r0, r1, #29
|
||||
|
||||
mrs r2, spsr
|
||||
str lr, [sp, #15*4]
|
||||
str r2, [sp, #16*4]
|
||||
|
||||
ands r2, r2, #SR_PMODE_MASK
|
||||
orreq r2, r2, #SR_SYS_MODE
|
||||
orr r2, r2, #(0x10 | SR_NOINT)
|
||||
|
||||
add r3, sp, #8*4
|
||||
msr cpsr_c, r2
|
||||
stmia r3!, {r8-r14}
|
||||
msr cpsr_c, r1
|
||||
|
||||
mrc p15, 0, r4, c5, c0, 0 @ data fault status register
|
||||
mrc p15, 0, r5, c5, c0, 1 @ instruction fault status register
|
||||
mrc p15, 0, r6, c6, c0, 0 @ data fault address
|
||||
add r3, r3, #2*4 @ skip saved PC and CPSR
|
||||
stmia r3!, {r4, r5, r6}
|
||||
|
||||
mov r1, sp
|
||||
bl do_exception
|
||||
|
||||
|
||||
xrqIRQ:
|
||||
clrex
|
||||
sub lr, lr, #4 @ Fix return address
|
||||
srsfd sp!, #SR_SVC_MODE @ Store IRQ mode LR and SPSR on the SVC stack
|
||||
cps #SR_SVC_MODE @ Switch to SVC mode
|
||||
push {r0-r4, r12, lr} @ Preserve registers
|
||||
|
||||
and r4, sp, #7 @ Fix SP to be 8byte aligned
|
||||
sub sp, sp, r4
|
||||
|
||||
bl gicTopHandler
|
||||
|
||||
add sp, sp, r4
|
||||
|
||||
pop {r0-r4, r12, lr}
|
||||
rfeia sp! @ Return from exception
|
||||
|
||||
@ u32 xrqInstallVectorTable(void)
|
||||
.global xrqInstallVectorTable
|
||||
.type xrqInstallVectorTable, %function
|
||||
xrqInstallVectorTable:
|
||||
ldr r0, =xrqPage
|
||||
ldr r1, =xrqVectorTable
|
||||
mov r2, #(xrqVectorTableEnd - xrqVectorTable)
|
||||
b memcpy
|
||||
|
||||
.section .bss.xrqPage
|
||||
.align 12
|
||||
.global xrqPage
|
||||
xrqPage:
|
||||
.space 8192 @ reserve two 4K aligned pages for vectors and abort stack
|
||||
.global xrqStackTop
|
||||
xrqStackTop:
|
@ -1,107 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2017-2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
.section .text.boot
|
||||
.align 4
|
||||
|
||||
#include <arm.h>
|
||||
|
||||
#define STACK_SZ (8192)
|
||||
|
||||
.global __boot
|
||||
__boot:
|
||||
cpsid aif, #SR_SVC_MODE
|
||||
clrex
|
||||
|
||||
@ Writeback and invalidate all DCache
|
||||
@ Invalidate all caches
|
||||
@ Data Synchronization Barrier
|
||||
mov r0, #0
|
||||
mcr p15, 0, r0, c7, c10, 0
|
||||
mcr p15, 0, r0, c7, c7, 0
|
||||
mcr p15, 0, r0, c7, c10, 4
|
||||
|
||||
@ Reset control registers
|
||||
ldr r0, =0x00054078
|
||||
ldr r1, =0x0000000F
|
||||
ldr r2, =0x00F00000
|
||||
|
||||
mcr p15, 0, r0, c1, c0, 0
|
||||
mcr p15, 0, r1, c1, c0, 1
|
||||
mcr p15, 0, r2, c1, c0, 2
|
||||
|
||||
@ VFPv2 init
|
||||
@ https://github.com/derrekr/fastboot3DS/blob/f63c967369451b1fd0078e649cf0010fe10a62fd/source/arm11/start.s#L195
|
||||
mov r0, #0
|
||||
mov r1, #0xF00000 @ Give full access to cp10/11 in user and privileged mode
|
||||
mov r2, #0x40000000 @ Clear exception bits and enable VFP11
|
||||
mov r3, #0x3C00000 @ Round towards zero (RZ) mode, flush-to-zero mode, default NaN mode
|
||||
mcr p15, 0, r1, c1, c0, 2 @ Write Coprocessor Access Control Register
|
||||
mcr p15, 0, r0, c7, c5, 4 @ Flush Prefetch Buffer
|
||||
fmxr fpexc, r2 @ Write Floating-point exception register
|
||||
fmxr fpscr, r3 @ Write Floating-Point Status and Control Register
|
||||
|
||||
|
||||
@ Get CPU ID
|
||||
mrc p15, 0, r12, c0, c0, 5
|
||||
ands r12, r12, #3
|
||||
|
||||
@ Setup stack according to CPU ID
|
||||
ldr sp, =(_stack_base + STACK_SZ)
|
||||
ldr r0, =STACK_SZ
|
||||
mla sp, r0, r12, sp
|
||||
|
||||
beq corezero_start
|
||||
|
||||
cmp r12, #MAX_CPU
|
||||
blo coresmp_start
|
||||
|
||||
1:
|
||||
wfi
|
||||
b 1b
|
||||
|
||||
corezero_start:
|
||||
@ assumes the .bss section size is 128 byte aligned (or zero)
|
||||
ldr r0, =__bss_pa
|
||||
ldr r1, =__bss_va_end @ calculate the length of .bss using the VA start and end
|
||||
ldr r2, =__bss_va
|
||||
sub r1, r1, r2
|
||||
add r1, r0, r1 @ fixup to be PA start and end
|
||||
mov r2, #0
|
||||
mov r3, #0
|
||||
mov r4, #0
|
||||
mov r5, #0
|
||||
.Lclearbss:
|
||||
cmp r0, r1
|
||||
.rept (128 / 16) @ 16 bytes copied per block
|
||||
stmloia r0!, {r2-r5}
|
||||
.endr
|
||||
blo .Lclearbss
|
||||
|
||||
bl SYS_CoreZeroInit
|
||||
|
||||
coresmp_start:
|
||||
bl SYS_CoreInit
|
||||
ldr lr, =MainLoop
|
||||
bx lr
|
||||
|
||||
.section .bss.stack
|
||||
.align 12 @ make sure stack is aligned to a page boundary
|
||||
.global _stack_base
|
||||
_stack_base:
|
||||
.space (MAX_CPU * STACK_SZ)
|
@ -1,146 +0,0 @@
|
||||
// Somewhat based on xerpi's CODEC driver for Linux
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2017 Sergi Granell, Paul LaMendola
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#include <common.h>
|
||||
#include <types.h>
|
||||
|
||||
#include <hid_map.h>
|
||||
|
||||
#include "hw/codec.h"
|
||||
#include <spi.h>
|
||||
|
||||
#define CPAD_THRESH_X (750)
|
||||
#define CPAD_THRESH_Y (150)
|
||||
|
||||
/* SPI stuff */
|
||||
static void CODEC_WriteRead(u32 *txb, u8 txl, u32 *rxb, u8 rxl)
|
||||
{
|
||||
SPI_XferInfo xfers[2];
|
||||
|
||||
xfers[0].buf = txb;
|
||||
xfers[0].len = txl;
|
||||
xfers[0].read = false;
|
||||
|
||||
xfers[1].buf = rxb;
|
||||
xfers[1].len = rxl;
|
||||
xfers[1].read = true;
|
||||
|
||||
SPI_DoXfer(SPI_DEV_CODEC, xfers, 2, true);
|
||||
}
|
||||
|
||||
static void CODEC_RegSelect(u8 reg)
|
||||
{
|
||||
SPI_XferInfo xfer;
|
||||
u32 cmd;
|
||||
|
||||
cmd = reg << 8;
|
||||
|
||||
xfer.buf = &cmd;
|
||||
xfer.len = 2;
|
||||
xfer.read = false;
|
||||
|
||||
SPI_DoXfer(SPI_DEV_CODEC, &xfer, 1, true);
|
||||
}
|
||||
|
||||
static u8 CODEC_RegRead(u8 reg)
|
||||
{
|
||||
u32 cmd, ret;
|
||||
cmd = (reg << 1) | 1;
|
||||
CODEC_WriteRead(&cmd, 1, &ret, 1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void CODEC_RegReadBuf(u8 reg, u32 *out, u8 size)
|
||||
{
|
||||
u32 cmd = (reg << 1) | 1;
|
||||
CODEC_WriteRead(&cmd, 1, out, size);
|
||||
}
|
||||
|
||||
static void CODEC_RegWrite(u8 reg, u8 val)
|
||||
{
|
||||
SPI_XferInfo xfer;
|
||||
u32 cmd;
|
||||
|
||||
cmd = (val << 8) | (reg << 1);
|
||||
|
||||
xfer.buf = &cmd;
|
||||
xfer.len = 2;
|
||||
xfer.read = false;
|
||||
|
||||
SPI_DoXfer(SPI_DEV_CODEC, &xfer, 1, true);
|
||||
}
|
||||
|
||||
static void CODEC_RegMask(u8 reg, u8 mask0, u8 mask1)
|
||||
{
|
||||
CODEC_RegWrite(reg, (CODEC_RegRead(reg) & ~mask1) | (mask0 & mask1));
|
||||
}
|
||||
|
||||
// elder god magic
|
||||
void CODEC_Init(void)
|
||||
{
|
||||
CODEC_RegSelect(0x67);
|
||||
CODEC_RegWrite(0x24, 0x98);
|
||||
CODEC_RegWrite(0x26, 0x00);
|
||||
CODEC_RegWrite(0x25, 0x43);
|
||||
CODEC_RegWrite(0x24, 0x18);
|
||||
CODEC_RegWrite(0x17, 0x43);
|
||||
CODEC_RegWrite(0x19, 0x69);
|
||||
CODEC_RegWrite(0x1B, 0x80);
|
||||
CODEC_RegWrite(0x27, 0x11);
|
||||
CODEC_RegWrite(0x26, 0xEC);
|
||||
CODEC_RegWrite(0x24, 0x18);
|
||||
CODEC_RegWrite(0x25, 0x53);
|
||||
|
||||
CODEC_RegMask(0x26, 0x80, 0x80);
|
||||
CODEC_RegMask(0x24, 0x00, 0x80);
|
||||
CODEC_RegMask(0x25, 0x10, 0x3C);
|
||||
}
|
||||
|
||||
void CODEC_GetRawData(u32 *buffer)
|
||||
{
|
||||
CODEC_RegSelect(0xFB);
|
||||
CODEC_RegReadBuf(1, buffer, 0x34);
|
||||
}
|
||||
|
||||
void CODEC_Get(CODEC_Input *input)
|
||||
{
|
||||
u32 raw_data_buf[0x34 / 4];
|
||||
u8 *raw_data = (u8*)raw_data_buf;
|
||||
s16 cpad_x, cpad_y;
|
||||
bool ts_pressed;
|
||||
|
||||
CODEC_GetRawData(raw_data_buf);
|
||||
|
||||
cpad_x = ((raw_data[0x24] << 8 | raw_data[0x25]) & 0xFFF) - 2048;
|
||||
cpad_y = ((raw_data[0x14] << 8 | raw_data[0x15]) & 0xFFF) - 2048;
|
||||
|
||||
// X axis is inverted
|
||||
input->cpad_x = (abs(cpad_x) > CPAD_THRESH_X) ? -cpad_x : 0;
|
||||
input->cpad_y = (abs(cpad_y) > CPAD_THRESH_Y) ? cpad_y : 0;
|
||||
|
||||
ts_pressed = !(raw_data[0] & BIT(4));
|
||||
if (ts_pressed) {
|
||||
input->ts_x = (raw_data[0] << 8) | raw_data[1];
|
||||
input->ts_y = (raw_data[10] << 8) | raw_data[11];
|
||||
} else {
|
||||
input->ts_x = 0xFFFF;
|
||||
input->ts_y = 0xFFFF;
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2017 Sergi Granell, Paul LaMendola
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <types.h>
|
||||
|
||||
typedef struct {
|
||||
s16 cpad_x, cpad_y;
|
||||
u16 ts_x, ts_y;
|
||||
} CODEC_Input;
|
||||
|
||||
void CODEC_Init(void);
|
||||
|
||||
void CODEC_GetRawData(u32 *buffer);
|
||||
void CODEC_Get(CODEC_Input *input);
|
@ -1,34 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2017 derrek, profi200
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <types.h>
|
||||
|
||||
#define REG_GPIO ((vu16*)(0x10100000 + 0x47000))
|
||||
|
||||
static inline void GPIO_setBit(u16 reg, u8 bitNum)
|
||||
{
|
||||
REG_GPIO[reg] |= 1u<<bitNum;
|
||||
}
|
||||
|
||||
static inline void GPIO_clearBit(u16 reg, u8 bitNum)
|
||||
{
|
||||
REG_GPIO[reg] &= ~(1u<<bitNum);
|
||||
}
|
@ -1,304 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2017-2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#include <common.h>
|
||||
#include <types.h>
|
||||
#include <vram.h>
|
||||
#include <arm.h>
|
||||
|
||||
#include "arm/timer.h"
|
||||
|
||||
#include "hw/i2c.h"
|
||||
#include "hw/mcu.h"
|
||||
#include "hw/gpulcd.h"
|
||||
|
||||
#include "system/event.h"
|
||||
|
||||
static struct
|
||||
{
|
||||
u16 lcdIds; // Bits 0-7 top screen, 8-15 bottom screen.
|
||||
bool lcdIdsRead;
|
||||
u8 lcdPower; // 1 = on. Bit 4 top light, bit 2 bottom light, bit 0 LCDs.
|
||||
u8 lcdLights[2]; // LCD backlight brightness. Top, bottom.
|
||||
u32 framebufs[2]; // For each screen
|
||||
u8 doubleBuf[2]; // Top, bottom, 1 = enable.
|
||||
u16 strides[2]; // Top, bottom
|
||||
u32 formats[2]; // Top, bottom
|
||||
} g_gfxState = {0};
|
||||
|
||||
static void setupDisplayController(u8 lcd);
|
||||
static void resetLcdsMaybe(void);
|
||||
static void waitLcdsReady(void);
|
||||
|
||||
static u32 gxModeWidth(unsigned c) {
|
||||
switch(c) {
|
||||
case 0: return 4;
|
||||
case 1: return 3;
|
||||
default: return 2;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned GFX_init(GfxFbFmt mode)
|
||||
{
|
||||
unsigned err = 0;
|
||||
|
||||
REG_CFG11_GPUPROT = 0;
|
||||
|
||||
// Reset
|
||||
REG_PDN_GPU_CNT = PDN_GPU_CNT_CLK_E;
|
||||
ARM_WaitCycles(12);
|
||||
REG_PDN_GPU_CNT = PDN_GPU_CNT_CLK_E | PDN_GPU_CNT_RST_ALL;
|
||||
REG_GX_GPU_CLK = 0x100;
|
||||
REG_GX_PSC_VRAM = 0;
|
||||
REG_GX_PSC_FILL0_CNT = 0;
|
||||
REG_GX_PSC_FILL1_CNT = 0;
|
||||
REG_GX_PPF_CNT = 0;
|
||||
|
||||
// LCD framebuffer setup.
|
||||
|
||||
g_gfxState.strides[0] = 240 * gxModeWidth(mode);
|
||||
g_gfxState.strides[1] = 240 * gxModeWidth(mode);
|
||||
|
||||
g_gfxState.framebufs[0] = VRAM_TOP_LA;
|
||||
g_gfxState.framebufs[1] = VRAM_BOT_A;
|
||||
|
||||
g_gfxState.formats[0] = mode | BIT(6) | BIT(9);
|
||||
g_gfxState.formats[1] = mode | BIT(9);
|
||||
|
||||
setupDisplayController(0);
|
||||
setupDisplayController(1);
|
||||
REG_LCD_PDC0_SWAP = 0; // Select framebuf 0.
|
||||
REG_LCD_PDC1_SWAP = 0;
|
||||
REG_LCD_PDC0_CNT = PDC_CNT_OUT_E | PDC_CNT_I_MASK_ERR | PDC_CNT_I_MASK_H | PDC_CNT_E; // Start
|
||||
REG_LCD_PDC1_CNT = PDC_CNT_OUT_E | PDC_CNT_I_MASK_ERR | PDC_CNT_I_MASK_H | PDC_CNT_E;
|
||||
|
||||
// LCD reg setup.
|
||||
REG_LCD_ABL0_FILL = 1u<<24; // Force blackscreen
|
||||
REG_LCD_ABL1_FILL = 1u<<24; // Force blackscreen
|
||||
REG_LCD_PARALLAX_CNT = 0;
|
||||
REG_LCD_PARALLAX_PWM = 0xA390A39;
|
||||
REG_LCD_RST = 0;
|
||||
REG_LCD_UNK00C = 0x10001;
|
||||
|
||||
// Clear used VRAM
|
||||
REG_GX_PSC_FILL0_S_ADDR = VRAM_TOP_LA >> 3;
|
||||
REG_GX_PSC_FILL0_E_ADDR = VRAM_END >> 3;
|
||||
REG_GX_PSC_FILL0_VAL = 0;
|
||||
REG_GX_PSC_FILL0_CNT = BIT(9) | BIT(0);
|
||||
|
||||
// Backlight and other stuff.
|
||||
REG_LCD_ABL0_LIGHT = 0;
|
||||
REG_LCD_ABL0_CNT = 0;
|
||||
REG_LCD_ABL0_LIGHT_PWM = 0;
|
||||
REG_LCD_ABL1_LIGHT = 0;
|
||||
REG_LCD_ABL1_CNT = 0;
|
||||
REG_LCD_ABL1_LIGHT_PWM = 0;
|
||||
|
||||
REG_LCD_RST = 1;
|
||||
REG_LCD_UNK00C = 0;
|
||||
TIMER_WaitMS(10);
|
||||
resetLcdsMaybe();
|
||||
MCU_controlLCDPower(2u); // Power on LCDs.
|
||||
if(eventWait(getEventMCU(), 0x3Fu<<24, 0x3Fu<<24) != 2u<<24) __builtin_trap();
|
||||
|
||||
waitLcdsReady();
|
||||
REG_LCD_ABL0_LIGHT_PWM = 0x1023E;
|
||||
REG_LCD_ABL1_LIGHT_PWM = 0x1023E;
|
||||
MCU_controlLCDPower(0x28u); // Power on backlights.
|
||||
if(eventWait(getEventMCU(), 0x3Fu<<24, 0x3Fu<<24) != 0x28u<<24) __builtin_trap();
|
||||
g_gfxState.lcdPower = 0x15; // All on.
|
||||
|
||||
// Make sure the fills finished.
|
||||
REG_LCD_ABL0_FILL = 0;
|
||||
REG_LCD_ABL1_FILL = 0;
|
||||
|
||||
// GPU stuff.
|
||||
REG_GX_GPU_CLK = 0x70100;
|
||||
*((vu32*)0x10400050) = 0x22221200;
|
||||
*((vu32*)0x10400054) = 0xFF2;
|
||||
|
||||
GFX_setBrightness(0x80, 0x80);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static u16 getLcdIds(void)
|
||||
{
|
||||
u16 ids;
|
||||
|
||||
if(!g_gfxState.lcdIdsRead)
|
||||
{
|
||||
g_gfxState.lcdIdsRead = true;
|
||||
|
||||
u16 top, bot;
|
||||
I2C_writeReg(I2C_DEV_LCD0, 0x40, 0xFF);
|
||||
I2C_readRegBuf(I2C_DEV_LCD0, 0x40, (u8*)&top, 2);
|
||||
I2C_writeReg(I2C_DEV_LCD1, 0x40, 0xFF);
|
||||
I2C_readRegBuf(I2C_DEV_LCD1, 0x40, (u8*)&bot, 2);
|
||||
|
||||
ids = top>>8;
|
||||
ids |= bot & 0xFF00u;
|
||||
g_gfxState.lcdIds = ids;
|
||||
}
|
||||
else ids = g_gfxState.lcdIds;
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
static void resetLcdsMaybe(void)
|
||||
{
|
||||
const u16 ids = getLcdIds();
|
||||
|
||||
// Top screen
|
||||
if(ids & 0xFFu) I2C_writeReg(I2C_DEV_LCD0, 0xFE, 0xAA);
|
||||
else
|
||||
{
|
||||
I2C_writeReg(I2C_DEV_LCD0, 0x11, 0x10);
|
||||
I2C_writeReg(I2C_DEV_LCD0, 0x50, 1);
|
||||
}
|
||||
|
||||
// Bottom screen
|
||||
if(ids>>8) I2C_writeReg(I2C_DEV_LCD1, 0xFE, 0xAA);
|
||||
else I2C_writeReg(I2C_DEV_LCD1, 0x11, 0x10);
|
||||
|
||||
I2C_writeReg(I2C_DEV_LCD0, 0x60, 0);
|
||||
I2C_writeReg(I2C_DEV_LCD1, 0x60, 0);
|
||||
I2C_writeReg(I2C_DEV_LCD0, 1, 0x10);
|
||||
I2C_writeReg(I2C_DEV_LCD1, 1, 0x10);
|
||||
}
|
||||
|
||||
static void waitLcdsReady(void)
|
||||
{
|
||||
const u16 ids = getLcdIds();
|
||||
|
||||
if((ids & 0xFFu) == 0 || (ids>>8) == 0) // Unknown LCD?
|
||||
{
|
||||
TIMER_WaitMS(150);
|
||||
}
|
||||
else
|
||||
{
|
||||
u32 i = 0;
|
||||
do
|
||||
{
|
||||
u16 top, bot;
|
||||
I2C_writeReg(I2C_DEV_LCD0, 0x40, 0x62);
|
||||
I2C_readRegBuf(I2C_DEV_LCD0, 0x40, (u8*)&top, 2);
|
||||
I2C_writeReg(I2C_DEV_LCD1, 0x40, 0x62);
|
||||
I2C_readRegBuf(I2C_DEV_LCD1, 0x40, (u8*)&bot, 2);
|
||||
|
||||
if((top>>8) == 1 && (bot>>8) == 1) break;
|
||||
|
||||
TIMER_WaitMS(33);
|
||||
} while(i++ < 10);
|
||||
}
|
||||
}
|
||||
|
||||
void GFX_powerOnBacklights(GfxBlight mask)
|
||||
{
|
||||
g_gfxState.lcdPower |= mask;
|
||||
|
||||
mask <<= 1;
|
||||
MCU_controlLCDPower(mask); // Power on backlights.
|
||||
eventWait(getEventMCU(), 0x3F<<24, 0x3F<<24);
|
||||
/*if(mcuEventWait(0x3Fu<<24) != (u32)mask<<24)
|
||||
__builtin_trap();*/
|
||||
}
|
||||
|
||||
void GFX_powerOffBacklights(GfxBlight mask)
|
||||
{
|
||||
g_gfxState.lcdPower &= ~mask;
|
||||
|
||||
MCU_controlLCDPower(mask); // Power off backlights.
|
||||
eventWait(getEventMCU(), 0x3F<<24, 0x3F<<24);
|
||||
/*if(mcuEventWait(0x3Fu<<24) != (u32)mask<<24)
|
||||
__builtin_trap();*/
|
||||
}
|
||||
|
||||
u8 GFX_getBrightness(void)
|
||||
{
|
||||
return REG_LCD_ABL0_LIGHT;
|
||||
}
|
||||
|
||||
void GFX_setBrightness(u8 top, u8 bot)
|
||||
{
|
||||
g_gfxState.lcdLights[0] = top;
|
||||
g_gfxState.lcdLights[1] = bot;
|
||||
REG_LCD_ABL0_LIGHT = top;
|
||||
REG_LCD_ABL1_LIGHT = bot;
|
||||
}
|
||||
|
||||
void GFX_setForceBlack(bool top, bool bot)
|
||||
{
|
||||
REG_LCD_ABL0_FILL = (u32)top<<24; // Force blackscreen
|
||||
REG_LCD_ABL1_FILL = (u32)bot<<24; // Force blackscreen
|
||||
}
|
||||
|
||||
static void setupDisplayController(u8 lcd)
|
||||
{
|
||||
if(lcd > 1) return;
|
||||
|
||||
static const u32 displayCfgs[2][24] =
|
||||
{
|
||||
{
|
||||
// PDC0 regs 0-0x4C.
|
||||
450, 209, 449, 449, 0, 207, 209, 453<<16 | 449,
|
||||
1<<16 | 0, 413, 2, 402, 402, 402, 1, 2,
|
||||
406<<16 | 402, 0, 0<<4 | 0, 0<<16 | 0xFF<<8 | 0,
|
||||
// PDC0 regs 0x5C-0x64.
|
||||
400<<16 | 240, // Width and height.
|
||||
449<<16 | 209,
|
||||
402<<16 | 2,
|
||||
// PDC0 reg 0x9C.
|
||||
0<<16 | 0
|
||||
},
|
||||
{
|
||||
// PDC1 regs 0-0x4C.
|
||||
450, 209, 449, 449, 205, 207, 209, 453<<16 | 449,
|
||||
1<<16 | 0, 413, 82, 402, 402, 79, 80, 82,
|
||||
408<<16 | 404, 0, 1<<4 | 1, 0<<16 | 0<<8 | 0xFF,
|
||||
// PDC1 regs 0x5C-0x64.
|
||||
320<<16 | 240, // Width and height.
|
||||
449<<16 | 209,
|
||||
402<<16 | 82,
|
||||
// PDC1 reg 0x9C.
|
||||
0<<16 | 0
|
||||
}
|
||||
};
|
||||
|
||||
const u32 *const cfg = displayCfgs[lcd];
|
||||
vu32 *const regs = (vu32*)(GX_REGS_BASE + 0x400 + (0x100u * lcd));
|
||||
|
||||
for (unsigned i = 0; i < 0x50/4; i++)
|
||||
regs[i] = cfg[i];
|
||||
|
||||
for (unsigned i = 0; i < 0xC/4; i++)
|
||||
regs[23 + i] = cfg[20 + i];
|
||||
|
||||
regs[36] = g_gfxState.strides[lcd]; // PDC reg 0x90 stride.
|
||||
regs[39] = cfg[23]; // PDC reg 0x9C.
|
||||
|
||||
// PDC regs 0x68, 0x6C, 0x94, 0x98 and 0x70.
|
||||
regs[26] = g_gfxState.framebufs[lcd]; // Framebuffer A first address.
|
||||
regs[27] = g_gfxState.framebufs[lcd]; // Framebuffer A second address.
|
||||
regs[37] = g_gfxState.framebufs[lcd]; // Framebuffer B first address.
|
||||
regs[38] = g_gfxState.framebufs[lcd]; // Framebuffer B second address.
|
||||
regs[28] = g_gfxState.formats[lcd]; // Format
|
||||
|
||||
regs[32] = 0; // Gamma table index 0.
|
||||
for(u32 i = 0; i < 256; i++) regs[33] = 0x10101u * i;
|
||||
}
|
@ -1,186 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2017-2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <types.h>
|
||||
|
||||
#define VBLANK_INTERRUPT (0x2A)
|
||||
|
||||
enum
|
||||
{
|
||||
PDN_GPU_CNT_RST_REGS = 1u, // And more?
|
||||
PDN_GPU_CNT_RST_PSC = 1u<<1, // ?
|
||||
PDN_GPU_CNT_RST_GEOSHADER = 1u<<2, // ?
|
||||
PDN_GPU_CNT_RST_RASTERIZER = 1u<<3, // ?
|
||||
PDN_GPU_CNT_RST_PPF = 1u<<4,
|
||||
PDN_GPU_CNT_RST_PDC = 1u<<5, // ?
|
||||
PDN_GPU_CNT_RST_PDC2 = 1u<<6, // Maybe pixel pipeline or so?
|
||||
|
||||
PDN_GPU_CNT_RST_ALL = (PDN_GPU_CNT_RST_PDC2<<1) - 1
|
||||
};
|
||||
|
||||
typedef enum
|
||||
{
|
||||
GFX_RGBA8 = 0, ///< RGBA8. (4 bytes)
|
||||
GFX_BGR8 = 1, ///< BGR8. (3 bytes)
|
||||
GFX_RGB565 = 2, ///< RGB565. (2 bytes)
|
||||
GFX_RGB5A1 = 3, ///< RGB5A1. (2 bytes)
|
||||
GFX_RGBA4 = 4 ///< RGBA4. (2 bytes)
|
||||
} GfxFbFmt;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
GFX_EVENT_PSC0 = 0u,
|
||||
GFX_EVENT_PSC1 = 1u,
|
||||
GFX_EVENT_PDC0 = 2u,
|
||||
GFX_EVENT_PDC1 = 3u,
|
||||
GFX_EVENT_PPF = 4u,
|
||||
GFX_EVENT_P3D = 5u
|
||||
} GfxEvent;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
GFX_BLIGHT_BOT = 1u<<2,
|
||||
GFX_BLIGHT_TOP = 1u<<4,
|
||||
GFX_BLIGHT_BOTH = GFX_BLIGHT_TOP | GFX_BLIGHT_BOT
|
||||
} GfxBlight;
|
||||
|
||||
#define REG_CFG11_GPUPROT *((vu16*)(0x10140140))
|
||||
#define REG_PDN_GPU_CNT *((vu32*)(0x10141200))
|
||||
#define PDN_GPU_CNT_CLK_E (1u<<16)
|
||||
#define PDN_VRAM_CNT_CLK_E (1u)
|
||||
|
||||
|
||||
|
||||
#define GX_REGS_BASE (0x10400000)
|
||||
#define REG_GX_GPU_CLK *((vu32*)(GX_REGS_BASE + 0x0004)) // ?
|
||||
|
||||
// PSC (memory fill) regs.
|
||||
#define REG_GX_PSC_FILL0_S_ADDR *((vu32*)(GX_REGS_BASE + 0x0010)) // Start address
|
||||
#define REG_GX_PSC_FILL0_E_ADDR *((vu32*)(GX_REGS_BASE + 0x0014)) // End address
|
||||
#define REG_GX_PSC_FILL0_VAL *((vu32*)(GX_REGS_BASE + 0x0018)) // Fill value
|
||||
#define REG_GX_PSC_FILL0_CNT *((vu32*)(GX_REGS_BASE + 0x001C))
|
||||
|
||||
#define REG_GX_PSC_FILL1_S_ADDR *((vu32*)(GX_REGS_BASE + 0x0020))
|
||||
#define REG_GX_PSC_FILL1_E_ADDR *((vu32*)(GX_REGS_BASE + 0x0024))
|
||||
#define REG_GX_PSC_FILL1_VAL *((vu32*)(GX_REGS_BASE + 0x0028))
|
||||
#define REG_GX_PSC_FILL1_CNT *((vu32*)(GX_REGS_BASE + 0x002C))
|
||||
|
||||
#define REG_GX_PSC_VRAM *((vu32*)(GX_REGS_BASE + 0x0030)) // gsp mudule only changes bit 8-11.
|
||||
#define REG_GX_PSC_STAT *((vu32*)(GX_REGS_BASE + 0x0034))
|
||||
|
||||
// PDC0/1 regs see lcd.h.
|
||||
|
||||
// PPF (transfer engine) regs.
|
||||
#define REG_GX_PPF_IN_ADDR *((vu32*)(GX_REGS_BASE + 0x0C00))
|
||||
#define REG_GX_PPF_OUT_ADDR *((vu32*)(GX_REGS_BASE + 0x0C04))
|
||||
#define REG_GX_PPF_DT_OUTDIM *((vu32*)(GX_REGS_BASE + 0x0C08)) // Display transfer output dimensions.
|
||||
#define REG_GX_PPF_DT_INDIM *((vu32*)(GX_REGS_BASE + 0x0C0C)) // Display transfer input dimensions.
|
||||
#define REG_GX_PPF_FlAGS *((vu32*)(GX_REGS_BASE + 0x0C10))
|
||||
#define REG_GX_PPF_UNK14 *((vu32*)(GX_REGS_BASE + 0x0C14)) // Transfer interval?
|
||||
#define REG_GX_PPF_CNT *((vu32*)(GX_REGS_BASE + 0x0C18))
|
||||
#define REG_GX_PPF_IRQ_POS *((vu32*)(GX_REGS_BASE + 0x0C1C)) // ?
|
||||
#define REG_GX_PPF_LEN *((vu32*)(GX_REGS_BASE + 0x0C20)) // Texture copy size in bytes.
|
||||
#define REG_GX_PPF_TC_INDIM *((vu32*)(GX_REGS_BASE + 0x0C24)) // Texture copy input width and gap in 16 byte units.
|
||||
#define REG_GX_PPF_TC_OUTDIM *((vu32*)(GX_REGS_BASE + 0x0C28)) // Texture copy output width and gap in 16 byte units.
|
||||
|
||||
// P3D (GPU internal) regs. See gpu_regs.h.
|
||||
#define REG_GX_P3D(reg) *((vu32*)(GX_REGS_BASE + 0x1000 + ((reg) * 4)))
|
||||
|
||||
// LCD/ABL regs.
|
||||
#define LCD_REGS_BASE (0x10202000)
|
||||
#define REG_LCD_PARALLAX_CNT *((vu32*)(LCD_REGS_BASE + 0x000)) // Controls PWM for the parallax barrier?
|
||||
#define REG_LCD_PARALLAX_PWM *((vu32*)(LCD_REGS_BASE + 0x004)) // Frequency/other PWM stuff maybe?
|
||||
#define REG_LCD_UNK00C *((vu32*)(LCD_REGS_BASE + 0x00C)) // Wtf is "FIX"?
|
||||
#define REG_LCD_RST *((vu32*)(LCD_REGS_BASE + 0x014)) // Reset active low.
|
||||
|
||||
#define REG_LCD_ABL0_CNT *((vu32*)(LCD_REGS_BASE + 0x200)) // Bit 0 enables ABL aka power saving mode.
|
||||
#define REG_LCD_ABL0_FILL *((vu32*)(LCD_REGS_BASE + 0x204))
|
||||
#define REG_LCD_ABL0_LIGHT *((vu32*)(LCD_REGS_BASE + 0x240))
|
||||
#define REG_LCD_ABL0_LIGHT_PWM *((vu32*)(LCD_REGS_BASE + 0x244))
|
||||
|
||||
#define REG_LCD_ABL1_CNT *((vu32*)(LCD_REGS_BASE + 0xA00)) // Bit 0 enables ABL aka power saving mode.
|
||||
#define REG_LCD_ABL1_FILL *((vu32*)(LCD_REGS_BASE + 0xA04))
|
||||
#define REG_LCD_ABL1_LIGHT *((vu32*)(LCD_REGS_BASE + 0xA40))
|
||||
#define REG_LCD_ABL1_LIGHT_PWM *((vu32*)(LCD_REGS_BASE + 0xA44))
|
||||
|
||||
|
||||
// Technically these regs belong in gx.h but they are used for LCD configuration so...
|
||||
// Pitfall warning: The 3DS LCDs are physically rotated 90° CCW.
|
||||
|
||||
// PDC0 (top screen display controller) regs.
|
||||
#define REG_LCD_PDC0_HTOTAL *((vu32*)(GX_REGS_BASE + 0x400))
|
||||
#define REG_LCD_PDC0_VTOTAL *((vu32*)(GX_REGS_BASE + 0x424))
|
||||
#define REG_LCD_PDC0_HPOS *((const vu32*)(GX_REGS_BASE + 0x450))
|
||||
#define REG_LCD_PDC0_VPOS *((const vu32*)(GX_REGS_BASE + 0x454))
|
||||
#define REG_LCD_PDC0_FB_A1 *((vu32*)(GX_REGS_BASE + 0x468))
|
||||
#define REG_LCD_PDC0_FB_A2 *((vu32*)(GX_REGS_BASE + 0x46C))
|
||||
#define REG_LCD_PDC0_FMT *((vu32*)(GX_REGS_BASE + 0x470))
|
||||
#define REG_LCD_PDC0_CNT *((vu32*)(GX_REGS_BASE + 0x474))
|
||||
#define REG_LCD_PDC0_SWAP *((vu32*)(GX_REGS_BASE + 0x478))
|
||||
#define REG_LCD_PDC0_STAT *((const vu32*)(GX_REGS_BASE + 0x47C))
|
||||
#define REG_LCD_PDC0_GTBL_IDX *((vu32*)(GX_REGS_BASE + 0x480)) // Gamma table index.
|
||||
#define REG_LCD_PDC0_GTBL_FIFO *((vu32*)(GX_REGS_BASE + 0x484)) // Gamma table FIFO.
|
||||
#define REG_LCD_PDC0_STRIDE *((vu32*)(GX_REGS_BASE + 0x490))
|
||||
#define REG_LCD_PDC0_FB_B1 *((vu32*)(GX_REGS_BASE + 0x494))
|
||||
#define REG_LCD_PDC0_FB_B2 *((vu32*)(GX_REGS_BASE + 0x498))
|
||||
|
||||
// PDC1 (bottom screen display controller) regs.
|
||||
#define REG_LCD_PDC1_HTOTAL *((vu32*)(GX_REGS_BASE + 0x500))
|
||||
#define REG_LCD_PDC1_VTOTAL *((vu32*)(GX_REGS_BASE + 0x524))
|
||||
#define REG_LCD_PDC1_HPOS *((const vu32*)(GX_REGS_BASE + 0x550))
|
||||
#define REG_LCD_PDC1_VPOS *((const vu32*)(GX_REGS_BASE + 0x554))
|
||||
#define REG_LCD_PDC1_FB_A1 *((vu32*)(GX_REGS_BASE + 0x568))
|
||||
#define REG_LCD_PDC1_FB_A2 *((vu32*)(GX_REGS_BASE + 0x56C))
|
||||
#define REG_LCD_PDC1_FMT *((vu32*)(GX_REGS_BASE + 0x570))
|
||||
#define REG_LCD_PDC1_CNT *((vu32*)(GX_REGS_BASE + 0x574))
|
||||
#define REG_LCD_PDC1_SWAP *((vu32*)(GX_REGS_BASE + 0x578))
|
||||
#define REG_LCD_PDC1_STAT *((const vu32*)(GX_REGS_BASE + 0x57C))
|
||||
#define REG_LCD_PDC1_GTBL_IDX *((vu32*)(GX_REGS_BASE + 0x580)) // Gamma table index.
|
||||
#define REG_LCD_PDC1_GTBL_FIFO *((vu32*)(GX_REGS_BASE + 0x584)) // Gamma table FIFO.
|
||||
#define REG_LCD_PDC1_STRIDE *((vu32*)(GX_REGS_BASE + 0x590))
|
||||
#define REG_LCD_PDC1_FB_B1 *((vu32*)(GX_REGS_BASE + 0x594))
|
||||
#define REG_LCD_PDC1_FB_B2 *((vu32*)(GX_REGS_BASE + 0x598))
|
||||
|
||||
|
||||
// REG_LCD_PDC_CNT
|
||||
#define PDC_CNT_E (1u)
|
||||
#define PDC_CNT_I_MASK_H (1u<<8) // Disables H(Blank?) IRQs.
|
||||
#define PDC_CNT_I_MASK_V (1u<<9) // Disables VBlank IRQs.
|
||||
#define PDC_CNT_I_MASK_ERR (1u<<10) // Disables error IRQs. What kind of errors?
|
||||
#define PDC_CNT_OUT_E (1u<<16) // Output enable?
|
||||
|
||||
// REG_LCD_PDC_SWAP
|
||||
// Masks
|
||||
#define PDC_SWAP_NEXT (1u) // Next framebuffer.
|
||||
#define PDC_SWAP_CUR (1u<<4) // Currently displaying framebuffer?
|
||||
// Bits
|
||||
#define PDC_SWAP_RST_FIFO (1u<<8) // Which FIFO?
|
||||
#define PDC_SWAP_I_H (1u<<16) // H(Blank?) IRQ bit.
|
||||
#define PDC_SWAP_I_V (1u<<17) // VBlank IRQ bit.
|
||||
#define PDC_SWAP_I_ERR (1u<<18) // Error IRQ bit?
|
||||
#define PDC_SWAP_I_ALL (PDC_SWAP_I_ERR | PDC_SWAP_I_V | PDC_SWAP_I_H)
|
||||
|
||||
|
||||
unsigned GFX_init(GfxFbFmt mode);
|
||||
void GFX_setForceBlack(bool top, bool bot);
|
||||
void GFX_powerOnBacklights(GfxBlight mask);
|
||||
void GFX_powerOffBacklights(GfxBlight mask);
|
||||
|
||||
u8 GFX_getBrightness(void);
|
||||
void GFX_setBrightness(u8 top, u8 bot);
|
@ -1,65 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#include <common.h>
|
||||
#include <types.h>
|
||||
#include <hid_map.h>
|
||||
|
||||
#include "hw/codec.h"
|
||||
#include "hw/hid.h"
|
||||
#include "hw/mcu.h"
|
||||
|
||||
#define REG_HID (~(*(vu16*)(0x10146000)) & BUTTON_ANY)
|
||||
|
||||
static u32 HID_ConvertCPAD(s16 cpad_x, s16 cpad_y)
|
||||
{
|
||||
u32 ret = 0;
|
||||
|
||||
if (cpad_x > 0) {
|
||||
ret |= BUTTON_RIGHT;
|
||||
} else if (cpad_x < 0) {
|
||||
ret |= BUTTON_LEFT;
|
||||
}
|
||||
|
||||
if (cpad_y > 0) {
|
||||
ret |= BUTTON_UP;
|
||||
} else if (cpad_y < 0) {
|
||||
ret |= BUTTON_DOWN;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
u64 HID_GetState(void)
|
||||
{
|
||||
CODEC_Input codec;
|
||||
u64 ret = 0;
|
||||
|
||||
CODEC_Get(&codec);
|
||||
|
||||
ret = REG_HID | mcuGetSpecialHID();
|
||||
if (!(ret & BUTTON_ARROW))
|
||||
ret |= HID_ConvertCPAD(codec.cpad_x, codec.cpad_y);
|
||||
|
||||
if (codec.ts_x <= 0xFFF)
|
||||
ret |= BUTTON_TOUCH;
|
||||
|
||||
ret |= (((u64)codec.ts_x << 16) | (u64)codec.ts_y) << 32;
|
||||
|
||||
return ret;
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <types.h>
|
||||
#include <hid_map.h>
|
||||
|
||||
u64 HID_GetState(void);
|
@ -1,214 +0,0 @@
|
||||
/*
|
||||
* This file is part of fastboot 3DS
|
||||
* Copyright (C) 2017 derrek, profi200
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <types.h>
|
||||
#include "hw/i2c.h"
|
||||
|
||||
|
||||
#define I2C1_REGS_BASE (0x10161000)
|
||||
|
||||
#define I2C2_REGS_BASE (0x10144000)
|
||||
|
||||
#define I2C3_REGS_BASE (0x10148000)
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
vu8 REG_I2C_DATA;
|
||||
vu8 REG_I2C_CNT;
|
||||
vu16 REG_I2C_CNTEX;
|
||||
vu16 REG_I2C_SCL;
|
||||
} I2cRegs;
|
||||
|
||||
static const struct
|
||||
{
|
||||
u8 busId;
|
||||
u8 devAddr;
|
||||
} i2cDevTable[] =
|
||||
{
|
||||
{0, 0x4A},
|
||||
{0, 0x7A},
|
||||
{0, 0x78},
|
||||
{1, 0x4A},
|
||||
{1, 0x78},
|
||||
{1, 0x2C},
|
||||
{1, 0x2E},
|
||||
{1, 0x40},
|
||||
{1, 0x44},
|
||||
{2, 0xD6},
|
||||
{2, 0xD0},
|
||||
{2, 0xD2},
|
||||
{2, 0xA4},
|
||||
{2, 0x9A},
|
||||
{2, 0xA0},
|
||||
{1, 0xEE},
|
||||
{0, 0x40},
|
||||
{2, 0x54}
|
||||
};
|
||||
|
||||
|
||||
|
||||
static void i2cWaitBusy(I2cRegs *const regs)
|
||||
{
|
||||
while(regs->REG_I2C_CNT & I2C_ENABLE);
|
||||
}
|
||||
|
||||
static I2cRegs* i2cGetBusRegsBase(u8 busId)
|
||||
{
|
||||
I2cRegs *base;
|
||||
switch(busId)
|
||||
{
|
||||
case 0:
|
||||
base = (I2cRegs*)I2C1_REGS_BASE;
|
||||
break;
|
||||
case 1:
|
||||
base = (I2cRegs*)I2C2_REGS_BASE;
|
||||
break;
|
||||
case 2:
|
||||
base = (I2cRegs*)I2C3_REGS_BASE;
|
||||
break;
|
||||
default:
|
||||
base = NULL;
|
||||
}
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
void I2C_init(void)
|
||||
{
|
||||
I2cRegs *regs = i2cGetBusRegsBase(0); // Bus 1
|
||||
i2cWaitBusy(regs);
|
||||
regs->REG_I2C_CNTEX = 2; // ?
|
||||
regs->REG_I2C_SCL = 1280; // ?
|
||||
|
||||
regs = i2cGetBusRegsBase(1); // Bus 2
|
||||
i2cWaitBusy(regs);
|
||||
regs->REG_I2C_CNTEX = 2; // ?
|
||||
regs->REG_I2C_SCL = 1280; // ?
|
||||
|
||||
regs = i2cGetBusRegsBase(2); // Bus 3
|
||||
i2cWaitBusy(regs);
|
||||
regs->REG_I2C_CNTEX = 2; // ?
|
||||
regs->REG_I2C_SCL = 1280; // ?
|
||||
}
|
||||
|
||||
static bool i2cStartTransfer(int devId, u8 regAddr, bool read, I2cRegs *const regs)
|
||||
{
|
||||
const u8 devAddr = i2cDevTable[devId].devAddr;
|
||||
|
||||
|
||||
u32 i = 0;
|
||||
for(; i < 8; i++)
|
||||
{
|
||||
i2cWaitBusy(regs);
|
||||
|
||||
// Select device and start.
|
||||
regs->REG_I2C_DATA = devAddr;
|
||||
regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_START;
|
||||
i2cWaitBusy(regs);
|
||||
if(!I2C_GET_ACK(regs->REG_I2C_CNT)) // If ack flag is 0 it failed.
|
||||
{
|
||||
regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_ERROR | I2C_STOP;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Select register and change direction to write.
|
||||
regs->REG_I2C_DATA = regAddr;
|
||||
regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_DIRE_WRITE;
|
||||
i2cWaitBusy(regs);
|
||||
if(!I2C_GET_ACK(regs->REG_I2C_CNT)) // If ack flag is 0 it failed.
|
||||
{
|
||||
regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_ERROR | I2C_STOP;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Select device in read mode for read transfer.
|
||||
if(read)
|
||||
{
|
||||
regs->REG_I2C_DATA = devAddr | 1u; // Set bit 0 for read.
|
||||
regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_START;
|
||||
i2cWaitBusy(regs);
|
||||
if(!I2C_GET_ACK(regs->REG_I2C_CNT)) // If ack flag is 0 it failed.
|
||||
{
|
||||
regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_ERROR | I2C_STOP;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if(i < 8) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
bool I2C_readRegBuf(int devId, u8 regAddr, u8 *out, u32 size)
|
||||
{
|
||||
const u8 busId = i2cDevTable[devId].busId;
|
||||
I2cRegs *const regs = i2cGetBusRegsBase(busId);
|
||||
|
||||
|
||||
if(!i2cStartTransfer(devId, regAddr, true, regs)) return false;
|
||||
|
||||
while(--size)
|
||||
{
|
||||
regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_DIRE_READ | I2C_ACK;
|
||||
i2cWaitBusy(regs);
|
||||
*out++ = regs->REG_I2C_DATA;
|
||||
}
|
||||
|
||||
regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_DIRE_READ | I2C_STOP;
|
||||
i2cWaitBusy(regs);
|
||||
*out = regs->REG_I2C_DATA; // Last byte
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool I2C_writeRegBuf(int devId, u8 regAddr, const u8 *in, u32 size)
|
||||
{
|
||||
const u8 busId = i2cDevTable[devId].busId;
|
||||
I2cRegs *const regs = i2cGetBusRegsBase(busId);
|
||||
|
||||
|
||||
if(!i2cStartTransfer(devId, regAddr, false, regs)) return false;
|
||||
|
||||
while(--size)
|
||||
{
|
||||
regs->REG_I2C_DATA = *in++;
|
||||
regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_DIRE_WRITE;
|
||||
i2cWaitBusy(regs);
|
||||
if(!I2C_GET_ACK(regs->REG_I2C_CNT)) // If ack flag is 0 it failed.
|
||||
{
|
||||
regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_ERROR | I2C_STOP;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
regs->REG_I2C_DATA = *in;
|
||||
regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_DIRE_WRITE | I2C_STOP;
|
||||
i2cWaitBusy(regs);
|
||||
if(!I2C_GET_ACK(regs->REG_I2C_CNT)) // If ack flag is 0 it failed.
|
||||
{
|
||||
regs->REG_I2C_CNT = I2C_ENABLE | I2C_IRQ_ENABLE | I2C_ERROR | I2C_STOP;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
@ -1,198 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#include <hid_map.h>
|
||||
#include <types.h>
|
||||
#include <arm.h>
|
||||
|
||||
#include <stdatomic.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "arm/timer.h"
|
||||
|
||||
#include "hw/gpio.h"
|
||||
#include "hw/gpulcd.h"
|
||||
#include "hw/mcu.h"
|
||||
|
||||
#include "system/event.h"
|
||||
|
||||
#define MCUEV_HID_MASK ( \
|
||||
MCUEV_HID_PWR_DOWN | MCUEV_HID_PWR_HOLD | \
|
||||
MCUEV_HID_HOME_DOWN | MCUEV_HID_HOME_UP | MCUEV_HID_WIFI_SWITCH)
|
||||
|
||||
enum {
|
||||
MCUREG_VOLUME_SLIDER = 0x09,
|
||||
|
||||
MCUREG_BATTERY_LEVEL = 0x0B,
|
||||
MCUREG_CONSOLE_STATE = 0x0F,
|
||||
|
||||
MCUREG_INT_MASK = 0x10,
|
||||
MCUREG_INT_EN = 0x18,
|
||||
|
||||
MCUREG_LCD_STATE = 0x22,
|
||||
|
||||
MCUREG_LED_WIFI = 0x2A,
|
||||
MCUREG_LED_CAMERA = 0x2B,
|
||||
MCUREG_LED_SLIDER = 0x2C,
|
||||
MCUREG_LED_STATUS = 0x2D,
|
||||
|
||||
MCUREG_RTC = 0x30,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
u8 delay;
|
||||
u8 smoothing;
|
||||
u8 loop_delay;
|
||||
u8 unk;
|
||||
u32 red[8];
|
||||
u32 green[8];
|
||||
u32 blue[8];
|
||||
} PACKED_STRUCT mcuStatusLED;
|
||||
|
||||
static u8 volumeSliderValue;
|
||||
static u32 shellState;
|
||||
static _Atomic(u32) pendingEvents;
|
||||
|
||||
static void mcuEventUpdate(void)
|
||||
{
|
||||
u32 mask;
|
||||
|
||||
// lazily update the mask on each test attempt
|
||||
if (!getEventIRQ()->test(MCU_INTERRUPT, true))
|
||||
return;
|
||||
|
||||
// reading the pending mask automagically acknowledges
|
||||
// the interrupts so all of them must be processed in one go
|
||||
mcuReadRegBuf(MCUREG_INT_MASK, (u8*)&mask, sizeof(mask));
|
||||
|
||||
if (mask & MCUEV_HID_VOLUME_SLIDER)
|
||||
volumeSliderValue = mcuReadReg(MCUREG_VOLUME_SLIDER);
|
||||
|
||||
if (mask & MCUEV_HID_SHELL_OPEN) {
|
||||
mcuResetLEDs();
|
||||
shellState = SHELL_OPEN;
|
||||
}
|
||||
|
||||
if (mask & MCUEV_HID_SHELL_CLOSE) {
|
||||
shellState = SHELL_CLOSED;
|
||||
}
|
||||
|
||||
atomic_fetch_or(&pendingEvents, mask);
|
||||
}
|
||||
|
||||
u8 mcuGetVolumeSlider(void)
|
||||
{
|
||||
mcuEventUpdate();
|
||||
return volumeSliderValue;
|
||||
}
|
||||
|
||||
u32 mcuGetSpecialHID(void)
|
||||
{
|
||||
u32 ret = 0, pend = getEventMCU()->test(MCUEV_HID_MASK, MCUEV_HID_MASK);
|
||||
|
||||
// hopefully gets unrolled
|
||||
if (pend & (MCUEV_HID_PWR_DOWN | MCUEV_HID_PWR_HOLD))
|
||||
ret |= BUTTON_POWER;
|
||||
if (pend & MCUEV_HID_HOME_DOWN)
|
||||
ret |= BUTTON_HOME;
|
||||
if (pend & MCUEV_HID_HOME_UP)
|
||||
ret &= ~BUTTON_HOME;
|
||||
return ret | shellState;
|
||||
}
|
||||
|
||||
void mcuSetStatusLED(u32 period_ms, u32 color)
|
||||
{
|
||||
u32 r, g, b, delay;
|
||||
mcuStatusLED ledState;
|
||||
|
||||
delay = clamp((period_ms * 0x200) / 1000, 1, 0xFF);
|
||||
|
||||
ledState.delay = delay;
|
||||
ledState.smoothing = delay;
|
||||
ledState.loop_delay = 0x10;
|
||||
ledState.unk = 0;
|
||||
|
||||
// all colors look like 0x00ZZ00ZZ
|
||||
// in order to alternate between
|
||||
// LED "off" and the wanted color
|
||||
r = (color >> 16) & 0xFF;
|
||||
r |= r << 16;
|
||||
for (int i = 0; i < 8; i++)
|
||||
ledState.red[i] = r;
|
||||
|
||||
g = (color >> 8) & 0xFF;
|
||||
g |= g << 16;
|
||||
for (int i = 0; i < 8; i++)
|
||||
ledState.green[i] = g;
|
||||
|
||||
b = color & 0xFF;
|
||||
b |= b << 16;
|
||||
for (int i = 0; i < 8; i++)
|
||||
ledState.blue[i] = b;
|
||||
|
||||
mcuWriteRegBuf(MCUREG_LED_STATUS, (const u8*)&ledState, sizeof(ledState));
|
||||
}
|
||||
|
||||
void mcuResetLEDs(void)
|
||||
{
|
||||
mcuWriteReg(MCUREG_LED_WIFI, 0);
|
||||
mcuWriteReg(MCUREG_LED_CAMERA, 0);
|
||||
mcuWriteReg(MCUREG_LED_SLIDER, 0);
|
||||
mcuSetStatusLED(0, 0);
|
||||
}
|
||||
|
||||
void mcuReset(void)
|
||||
{
|
||||
u32 intmask = 0;
|
||||
|
||||
atomic_init(&pendingEvents, 0);
|
||||
|
||||
// set register mask and clear any pending registers
|
||||
mcuWriteRegBuf(MCUREG_INT_EN, (const u8*)&intmask, sizeof(intmask));
|
||||
mcuReadRegBuf(MCUREG_INT_MASK, (u8*)&intmask, sizeof(intmask));
|
||||
|
||||
mcuResetLEDs();
|
||||
|
||||
volumeSliderValue = mcuReadReg(MCUREG_VOLUME_SLIDER);
|
||||
shellState = SHELL_OPEN;
|
||||
// assume the shell is always open on boot
|
||||
// knowing the average 3DS user, there will be plenty
|
||||
// of laughs when this comes back to bite us in the rear
|
||||
|
||||
GPIO_setBit(19, 9);
|
||||
}
|
||||
|
||||
static void evReset(void) {
|
||||
atomic_store(&pendingEvents, 0);
|
||||
}
|
||||
|
||||
static u32 evTest(u32 mask, u32 clear) {
|
||||
mcuEventUpdate();
|
||||
return atomic_fetch_and(&pendingEvents, ~clear) & mask;
|
||||
}
|
||||
|
||||
static const EventInterface evMCU = {
|
||||
.reset = evReset,
|
||||
.test = evTest
|
||||
};
|
||||
|
||||
const EventInterface *getEventMCU(void) {
|
||||
return &evMCU;
|
||||
}
|
||||
|
@ -1,73 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <types.h>
|
||||
|
||||
#include "arm/timer.h"
|
||||
#include "hw/i2c.h"
|
||||
|
||||
#define MCU_INTERRUPT (0x71)
|
||||
#define I2C_MCU_DEVICE (3)
|
||||
|
||||
enum {
|
||||
MCUEV_HID_PWR_DOWN = BIT(0),
|
||||
MCUEV_HID_PWR_HOLD = BIT(1),
|
||||
MCUEV_HID_HOME_DOWN = BIT(2),
|
||||
MCUEV_HID_HOME_UP = BIT(3),
|
||||
MCUEV_HID_WIFI_SWITCH = BIT(4),
|
||||
MCUEV_HID_SHELL_CLOSE = BIT(5),
|
||||
MCUEV_HID_SHELL_OPEN = BIT(6),
|
||||
MCUEV_HID_VOLUME_SLIDER = BIT(22),
|
||||
};
|
||||
|
||||
u8 mcuGetVolumeSlider(void);
|
||||
u32 mcuGetSpecialHID(void);
|
||||
|
||||
void mcuSetStatusLED(u32 period_ms, u32 color);
|
||||
void mcuResetLEDs(void);
|
||||
|
||||
void mcuReset(void);
|
||||
|
||||
static inline u8 mcuReadReg(u8 addr)
|
||||
{
|
||||
u8 val;
|
||||
I2C_readRegBuf(I2C_MCU_DEVICE, addr, &val, 1);
|
||||
return val;
|
||||
}
|
||||
|
||||
static inline bool mcuReadRegBuf(u8 addr, u8 *buf, u32 size)
|
||||
{
|
||||
return I2C_readRegBuf(I2C_MCU_DEVICE, addr, buf, size);
|
||||
}
|
||||
|
||||
static inline bool mcuWriteReg(u8 addr, u8 val)
|
||||
{
|
||||
return I2C_writeRegBuf(I2C_MCU_DEVICE, addr, &val, 1);
|
||||
}
|
||||
|
||||
static inline bool mcuWriteRegBuf(u8 addr, const u8 *buf, u32 size)
|
||||
{
|
||||
return I2C_writeRegBuf(I2C_MCU_DEVICE, addr, buf, size);
|
||||
}
|
||||
|
||||
static inline void MCU_controlLCDPower(u8 bits)
|
||||
{
|
||||
mcuWriteReg(0x22u, bits);
|
||||
}
|
@ -1,93 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#include <types.h>
|
||||
|
||||
#include <spi.h>
|
||||
#include "hw/nvram.h"
|
||||
|
||||
// returns manuf id, memory type and size
|
||||
// size = (1 << id[2]) ?
|
||||
// apparently unreliable on some Sanyo chips?
|
||||
#define CMD_RDID 0x9F
|
||||
|
||||
#define CMD_READ 0x03
|
||||
#define CMD_WREN 0x06
|
||||
#define CMD_WRDI 0x04
|
||||
|
||||
#define CMD_RDSR 0x05
|
||||
|
||||
#define CMD_DPD 0xB9 // deep power down
|
||||
#define CMD_RDP 0xAB // release from deep power down
|
||||
|
||||
static u32 NVRAM_SendStatusCommand(u32 cmd, u32 width)
|
||||
{
|
||||
u32 ret;
|
||||
SPI_XferInfo xfer[2];
|
||||
|
||||
xfer[0].buf = &cmd;
|
||||
xfer[0].len = 1;
|
||||
xfer[0].read = false;
|
||||
|
||||
xfer[1].buf = &ret;
|
||||
xfer[1].len = width;
|
||||
xfer[1].read = true;
|
||||
|
||||
ret = 0;
|
||||
SPI_DoXfer(SPI_DEV_NVRAM, xfer, 2, true);
|
||||
return ret;
|
||||
}
|
||||
|
||||
u32 NVRAM_Status(void)
|
||||
{
|
||||
return NVRAM_SendStatusCommand(CMD_RDSR, 1);
|
||||
}
|
||||
|
||||
u32 NVRAM_ReadID(void)
|
||||
{
|
||||
return NVRAM_SendStatusCommand(CMD_RDID, 3);
|
||||
}
|
||||
|
||||
void NVRAM_DeepStandby(void)
|
||||
{
|
||||
NVRAM_SendStatusCommand(CMD_DPD, 0);
|
||||
}
|
||||
|
||||
void NVRAM_Wakeup(void)
|
||||
{
|
||||
NVRAM_SendStatusCommand(CMD_RDP, 0);
|
||||
}
|
||||
|
||||
void NVRAM_Read(u32 address, u32 *buffer, u32 len)
|
||||
{
|
||||
SPI_XferInfo xfer[2];
|
||||
u32 cmd;
|
||||
|
||||
address &= BIT(24) - 1;
|
||||
cmd = __builtin_bswap32(address) | CMD_READ;
|
||||
|
||||
xfer[0].buf = &cmd;
|
||||
xfer[0].len = 4;
|
||||
xfer[0].read = false;
|
||||
|
||||
xfer[1].buf = buffer;
|
||||
xfer[1].len = len;
|
||||
xfer[1].read = true;
|
||||
|
||||
SPI_DoXfer(SPI_DEV_NVRAM, xfer, 2, true);
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <types.h>
|
||||
|
||||
#include <spi.h>
|
||||
|
||||
#define NVRAM_SR_WIP BIT(0) // work in progress / busy
|
||||
#define NVRAM_SR_WEL BIT(1) // write enable latch
|
||||
|
||||
u32 NVRAM_Status(void);
|
||||
u32 NVRAM_ReadID(void);
|
||||
|
||||
void NVRAM_Read(u32 offset, u32 *buffer, u32 len);
|
||||
|
||||
void NVRAM_DeepStandby(void);
|
||||
void NVRAM_Wakeup(void);
|
@ -1,229 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#include <common.h>
|
||||
#include <types.h>
|
||||
#include <shmem.h>
|
||||
#include <arm.h>
|
||||
#include <pxi.h>
|
||||
|
||||
#include "arm/gic.h"
|
||||
|
||||
#include "hw/hid.h"
|
||||
#include "hw/gpulcd.h"
|
||||
#include "hw/i2c.h"
|
||||
#include "hw/mcu.h"
|
||||
#include "hw/nvram.h"
|
||||
|
||||
#include "system/sys.h"
|
||||
#include "system/event.h"
|
||||
|
||||
static const u8 brightness_lvls[] = {
|
||||
0x10, 0x17, 0x1E, 0x25,
|
||||
0x2C, 0x34, 0x3C, 0x44,
|
||||
0x4D, 0x56, 0x60, 0x6B,
|
||||
0x79, 0x8C, 0xA7, 0xD2
|
||||
};
|
||||
|
||||
#ifndef FIXED_BRIGHTNESS
|
||||
static int prev_bright_lvl;
|
||||
static bool auto_brightness;
|
||||
#endif
|
||||
|
||||
static SystemSHMEM __attribute__((section(".shared"))) sharedMem;
|
||||
|
||||
static void vblankUpdate(void)
|
||||
{
|
||||
if (!getEventIRQ()->test(VBLANK_INTERRUPT, true))
|
||||
return;
|
||||
|
||||
#ifndef FIXED_BRIGHTNESS
|
||||
int cur_bright_lvl = (mcuGetVolumeSlider() >> 2) % countof(brightness_lvls);
|
||||
if ((cur_bright_lvl != prev_bright_lvl) && auto_brightness) {
|
||||
prev_bright_lvl = cur_bright_lvl;
|
||||
u8 br = brightness_lvls[cur_bright_lvl];
|
||||
GFX_setBrightness(br, br);
|
||||
}
|
||||
#endif
|
||||
|
||||
// handle shell events
|
||||
static const u32 mcuEvShell = MCUEV_HID_SHELL_OPEN | MCUEV_HID_SHELL_CLOSE;
|
||||
u32 shell = getEventMCU()->test(mcuEvShell, mcuEvShell);
|
||||
if (shell & MCUEV_HID_SHELL_CLOSE) {
|
||||
GFX_powerOffBacklights(GFX_BLIGHT_BOTH);
|
||||
} else if (shell & MCUEV_HID_SHELL_OPEN) {
|
||||
GFX_powerOnBacklights(GFX_BLIGHT_BOTH);
|
||||
}
|
||||
|
||||
sharedMem.hidState.full = HID_GetState();
|
||||
}
|
||||
|
||||
static u32 pxiRxUpdate(u32 *args)
|
||||
{
|
||||
u32 msg, lo, hi;
|
||||
|
||||
if (!getEventIRQ()->test(PXI_RX_INTERRUPT, true))
|
||||
return PXICMD_NONE;
|
||||
|
||||
msg = PXI_Recv();
|
||||
lo = msg & 0xFFFF;
|
||||
hi = msg >> 16;
|
||||
|
||||
PXI_RecvArray(args, hi);
|
||||
return lo;
|
||||
}
|
||||
|
||||
void __attribute__((noreturn)) MainLoop(void)
|
||||
{
|
||||
bool runPxiCmdProcessor = true;
|
||||
|
||||
#ifdef FIXED_BRIGHTNESS
|
||||
u8 fixed_bright_lvl = brightness_lvls[clamp(FIXED_BRIGHTNESS, 0, countof(brightness_lvls)-1)];
|
||||
GFX_setBrightness(fixed_bright_lvl, fixed_bright_lvl);
|
||||
#else
|
||||
prev_bright_lvl = -1;
|
||||
auto_brightness = true;
|
||||
#endif
|
||||
|
||||
// initialize state stuff
|
||||
getEventIRQ()->reset();
|
||||
getEventMCU()->reset();
|
||||
memset(&sharedMem, 0, sizeof(sharedMem));
|
||||
|
||||
// configure interrupts
|
||||
gicSetInterruptConfig(PXI_RX_INTERRUPT, BIT(0), GIC_PRIO0, NULL);
|
||||
gicSetInterruptConfig(VBLANK_INTERRUPT, BIT(0), GIC_PRIO0, NULL);
|
||||
gicSetInterruptConfig(MCU_INTERRUPT, BIT(0), GIC_PRIO0, NULL);
|
||||
|
||||
// enable interrupts
|
||||
gicEnableInterrupt(MCU_INTERRUPT);
|
||||
|
||||
// perform gpu init after initializing mcu but before
|
||||
// enabling the pxi system and the vblank handler
|
||||
GFX_init(GFX_RGB565);
|
||||
|
||||
gicEnableInterrupt(PXI_RX_INTERRUPT);
|
||||
gicEnableInterrupt(VBLANK_INTERRUPT);
|
||||
|
||||
// ARM9 won't try anything funny until this point
|
||||
PXI_Barrier(PXI_BOOT_BARRIER);
|
||||
|
||||
// Process commands until the ARM9 tells
|
||||
// us it's time to boot something else
|
||||
// also handles VBlank events as needed
|
||||
do {
|
||||
u32 pxiCmd, pxiReply, args[PXI_MAX_ARGS];
|
||||
|
||||
vblankUpdate();
|
||||
pxiCmd = pxiRxUpdate(args);
|
||||
|
||||
switch(pxiCmd) {
|
||||
// ignore args and just wait until the next event
|
||||
case PXICMD_NONE:
|
||||
ARM_WFI();
|
||||
break;
|
||||
|
||||
// revert to legacy boot mode
|
||||
case PXICMD_LEGACY_BOOT:
|
||||
runPxiCmdProcessor = false;
|
||||
pxiReply = 0;
|
||||
break;
|
||||
|
||||
// returns the shared memory address
|
||||
case PXICMD_GET_SHMEM_ADDRESS:
|
||||
pxiReply = (u32)&sharedMem;
|
||||
break;
|
||||
|
||||
// takes in a single argument word and performs either an
|
||||
// I2C read or write depending on the value of the top bit
|
||||
case PXICMD_I2C_OP:
|
||||
{
|
||||
u32 devId, regAddr, size;
|
||||
|
||||
devId = (args[0] & 0xff);
|
||||
regAddr = (args[0] >> 8) & 0xFF;
|
||||
size = (args[0] >> 16) % SHMEM_BUFFER_SIZE;
|
||||
|
||||
if (args[0] & BIT(31)) {
|
||||
pxiReply = I2C_writeRegBuf(devId, regAddr, sharedMem.dataBuffer.b, size);
|
||||
} else {
|
||||
pxiReply = I2C_readRegBuf(devId, regAddr, sharedMem.dataBuffer.b, size);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// checks whether the NVRAM chip is online (not doing any work)
|
||||
case PXICMD_NVRAM_ONLINE:
|
||||
pxiReply = (NVRAM_Status() & NVRAM_SR_WIP) == 0;
|
||||
break;
|
||||
|
||||
// reads data from the NVRAM chip
|
||||
case PXICMD_NVRAM_READ:
|
||||
NVRAM_Read(args[0], sharedMem.dataBuffer.w, args[1]);
|
||||
pxiReply = 0;
|
||||
break;
|
||||
|
||||
// sets the notification LED with the given color and period
|
||||
case PXICMD_SET_NOTIFY_LED:
|
||||
mcuSetStatusLED(args[0], args[1]);
|
||||
pxiReply = 0;
|
||||
break;
|
||||
|
||||
// sets the LCDs brightness (if FIXED_BRIGHTNESS is disabled)
|
||||
case PXICMD_SET_BRIGHTNESS:
|
||||
{
|
||||
pxiReply = GFX_getBrightness();
|
||||
#ifndef FIXED_BRIGHTNESS
|
||||
s32 newbrightness = (s32)args[0];
|
||||
if ((newbrightness > 0) && (newbrightness < 0x100)) {
|
||||
GFX_setBrightness(newbrightness, newbrightness);
|
||||
auto_brightness = false;
|
||||
} else {
|
||||
prev_bright_lvl = -1;
|
||||
auto_brightness = true;
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
|
||||
// replies -1 on default
|
||||
default:
|
||||
pxiReply = 0xFFFFFFFF;
|
||||
break;
|
||||
}
|
||||
|
||||
if (pxiCmd != PXICMD_NONE)
|
||||
PXI_Send(pxiReply); // was a command sent from the ARM9, send a response
|
||||
} while(runPxiCmdProcessor);
|
||||
|
||||
// perform deinit in reverse order
|
||||
gicDisableInterrupt(VBLANK_INTERRUPT);
|
||||
gicDisableInterrupt(PXI_RX_INTERRUPT);
|
||||
|
||||
// unconditionally reinitialize the screens
|
||||
// in RGB24 framebuffer mode
|
||||
GFX_init(GFX_BGR8);
|
||||
|
||||
gicDisableInterrupt(MCU_INTERRUPT);
|
||||
|
||||
// Wait for the ARM9 to do its firmlaunch setup
|
||||
PXI_Barrier(PXI_FIRMLAUNCH_BARRIER);
|
||||
|
||||
SYS_CoreZeroShutdown();
|
||||
SYS_CoreShutdown();
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <types.h>
|
||||
|
||||
typedef struct {
|
||||
void (*reset)(void);
|
||||
u32 (*test)(u32 param, u32 clear);
|
||||
} EventInterface;
|
||||
|
||||
const EventInterface *getEventIRQ(void);
|
||||
const EventInterface *getEventMCU(void);
|
||||
|
||||
static inline void eventReset(const EventInterface *ei) {
|
||||
ei->reset();
|
||||
}
|
||||
|
||||
static inline u32 eventTest(const EventInterface *ei, u32 param, u32 clear) {
|
||||
return ei->test(param, clear);
|
||||
}
|
||||
|
||||
static inline u32 eventWait(const EventInterface *ei, u32 param, u32 clear) {
|
||||
while(1) {
|
||||
u32 ret = ei->test(param, clear);
|
||||
if (ret) return ret;
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <types.h>
|
||||
|
||||
#define DEF_SECT_(n) extern u32 __##n##_pa, __##n##_va, __##n##_va_end;
|
||||
DEF_SECT_(text)
|
||||
DEF_SECT_(data)
|
||||
DEF_SECT_(rodata)
|
||||
DEF_SECT_(bss)
|
||||
DEF_SECT_(shared)
|
||||
#undef DEF_SECT_
|
||||
|
||||
#define SECTION_VA(n) ((u32)&__##n##_va)
|
||||
#define SECTION_PA(n) ((u32)&__##n##_pa)
|
||||
#define SECTION_LEN(n) (((u32)(&__##n##_va_end) - (u32)(&__##n##_va)))
|
||||
|
||||
#define SECTION_TRI(n) SECTION_VA(n), SECTION_PA(n), SECTION_LEN(n)
|
@ -1,176 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#include <types.h>
|
||||
#include <vram.h>
|
||||
#include <arm.h>
|
||||
#include <pxi.h>
|
||||
|
||||
#include "arm/gic.h"
|
||||
#include "arm/mmu.h"
|
||||
#include "arm/scu.h"
|
||||
#include "arm/xrq.h"
|
||||
|
||||
#include "hw/codec.h"
|
||||
#include "hw/gpulcd.h"
|
||||
#include "hw/i2c.h"
|
||||
#include "hw/mcu.h"
|
||||
#include <spi.h>
|
||||
|
||||
#include "system/sections.h"
|
||||
|
||||
#define CFG11_MPCORE_CLKCNT ((vu16*)(0x10141300))
|
||||
#define CFG11_SOCINFO ((vu16*)(0x10140FFC))
|
||||
|
||||
#define LEGACY_BOOT_ENTRYPOINT ((vu32*)0x1FFFFFFC)
|
||||
#define LEGACY_BOOT_ROUTINE_SMP (0x0001004C)
|
||||
|
||||
static bool SYS_IsNewConsole(void)
|
||||
{
|
||||
return (*CFG11_SOCINFO & 2) != 0;
|
||||
}
|
||||
|
||||
static bool SYS_ClkMultEnabled(void)
|
||||
{
|
||||
return (*CFG11_MPCORE_CLKCNT & 1) != 0;
|
||||
}
|
||||
|
||||
static void SYS_EnableClkMult(void)
|
||||
{
|
||||
// magic bit twiddling to enable extra FCRAM
|
||||
// only done on N3DS and when it hasn't been done yet
|
||||
// state might get a bit messed up so it has to be done
|
||||
// as early as possible in the initialization chain
|
||||
if (SYS_IsNewConsole() && !SYS_ClkMultEnabled()) {
|
||||
gicSetInterruptConfig(88, BIT(0), GIC_PRIO_HIGHEST, NULL);
|
||||
gicEnableInterrupt(88);
|
||||
*CFG11_MPCORE_CLKCNT = 0x8001;
|
||||
do {
|
||||
ARM_WFI();
|
||||
} while(!(*CFG11_MPCORE_CLKCNT & 0x8000));
|
||||
gicDisableInterrupt(88);
|
||||
gicClearInterruptConfig(88);
|
||||
}
|
||||
}
|
||||
|
||||
void SYS_CoreZeroInit(void)
|
||||
{
|
||||
gicGlobalReset();
|
||||
|
||||
*LEGACY_BOOT_ENTRYPOINT = 0;
|
||||
|
||||
SYS_EnableClkMult();
|
||||
|
||||
SCU_Init();
|
||||
|
||||
// Map all sections here
|
||||
mmuMapArea(SECTION_TRI(text), MMU_FLAGS(MMU_CACHE_WT, MMU_READ_ONLY, 0, 1));
|
||||
mmuMapArea(SECTION_TRI(data), MMU_FLAGS(MMU_CACHE_WBA, MMU_READ_WRITE, 1, 1));
|
||||
mmuMapArea(SECTION_TRI(rodata), MMU_FLAGS(MMU_CACHE_WT, MMU_READ_ONLY, 1, 1));
|
||||
mmuMapArea(SECTION_TRI(bss), MMU_FLAGS(MMU_CACHE_WBA, MMU_READ_WRITE, 1, 1));
|
||||
mmuMapArea(SECTION_TRI(shared), MMU_FLAGS(MMU_STRONG_ORDER, MMU_READ_WRITE, 1, 1));
|
||||
|
||||
// High exception vectors
|
||||
mmuMapArea(0xFFFF0000, xrqInstallVectorTable(), 4UL << 10, MMU_FLAGS(MMU_CACHE_WT, MMU_READ_ONLY, 0, 0));
|
||||
|
||||
// BootROM
|
||||
mmuMapArea(0x00010000, 0x00010000, 32UL << 10, MMU_FLAGS(MMU_CACHE_WT, MMU_READ_ONLY, 0, 1));
|
||||
|
||||
// IO Registers
|
||||
mmuMapArea(0x10100000, 0x10100000, 4UL << 20, MMU_FLAGS(MMU_DEV_SHARED, MMU_READ_WRITE, 1, 1));
|
||||
|
||||
// MPCore Private Memory Region
|
||||
mmuMapArea(0x17E00000, 0x17E00000, 8UL << 10, MMU_FLAGS(MMU_DEV_SHARED, MMU_READ_WRITE, 1, 1));
|
||||
|
||||
// VRAM
|
||||
mmuMapArea(0x18000000, 0x18000000, 6UL << 20, MMU_FLAGS(MMU_CACHE_WT, MMU_READ_WRITE, 1, 1));
|
||||
|
||||
// FCRAM
|
||||
if (SYS_IsNewConsole()) {
|
||||
mmuMapArea(0x20000000, 0x20000000, 256UL << 20, MMU_FLAGS(MMU_CACHE_WB, MMU_READ_WRITE, 1, 1));
|
||||
} else {
|
||||
mmuMapArea(0x20000000, 0x20000000, 128UL << 20, MMU_FLAGS(MMU_CACHE_WB, MMU_READ_WRITE, 1, 1));
|
||||
}
|
||||
|
||||
// screen init magicks
|
||||
TIMER_WaitMS(64);
|
||||
|
||||
// Initialize peripherals
|
||||
PXI_Reset();
|
||||
|
||||
I2C_init();
|
||||
mcuReset();
|
||||
|
||||
SPI_Init();
|
||||
CODEC_Init();
|
||||
}
|
||||
|
||||
void SYS_CoreInit(void)
|
||||
{
|
||||
// Reset local GIC registers
|
||||
gicLocalReset();
|
||||
|
||||
// Set up MMU registers
|
||||
mmuInitRegisters();
|
||||
|
||||
// Enable fancy ARM11 features
|
||||
ARM_SetACR(ARM_GetACR() |
|
||||
ACR_RETSTK | ACR_DBPRED | ACR_SBPRED | ACR_FOLDING | ACR_SMP);
|
||||
|
||||
ARM_SetCR(ARM_GetCR() |
|
||||
CR_MMU | CR_CACHES | CR_FLOWPRED | CR_HIGHVEC | CR_DSUBPAGE);
|
||||
|
||||
ARM_DSB();
|
||||
|
||||
ARM_EnableInterrupts();
|
||||
}
|
||||
|
||||
void SYS_CoreZeroShutdown(void)
|
||||
{
|
||||
ARM_DisableInterrupts();
|
||||
gicGlobalReset();
|
||||
}
|
||||
|
||||
void __attribute__((noreturn)) SYS_CoreShutdown(void)
|
||||
{
|
||||
u32 core = ARM_CoreID();
|
||||
|
||||
ARM_DisableInterrupts();
|
||||
|
||||
gicLocalReset();
|
||||
|
||||
ARM_WbInvDC();
|
||||
ARM_InvIC();
|
||||
ARM_DSB();
|
||||
|
||||
ARM_SetCR(ARM_GetCR() & ~(CR_MMU | CR_CACHES | CR_FLOWPRED));
|
||||
ARM_SetACR(ARM_GetACR() &
|
||||
~(ACR_RETSTK | ACR_DBPRED | ACR_SBPRED | ACR_FOLDING | ACR_SMP));
|
||||
|
||||
SPI_Deinit();
|
||||
|
||||
if (!core) {
|
||||
while(*LEGACY_BOOT_ENTRYPOINT == 0);
|
||||
((void (*)(void))(*LEGACY_BOOT_ENTRYPOINT))();
|
||||
} else {
|
||||
// Branch to bootrom function that does SMP reinit magic
|
||||
// (waits for IPI + branches to word @ 0x1FFFFFDC)
|
||||
((void (*)(void))LEGACY_BOOT_ROUTINE_SMP)();
|
||||
}
|
||||
__builtin_unreachable();
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <types.h>
|
||||
|
||||
/*
|
||||
how to run the SYS_Core(Zero){Init,Shutdown} functions:
|
||||
for init:
|
||||
- FIRST run CoreZeroInit ONCE
|
||||
- all cores must run CoreInit ONCE
|
||||
|
||||
for shutdown:
|
||||
- all non-zero cores must call CoreShutdown
|
||||
- core zero must call CoreZeroShutdown, then CoreShutdown
|
||||
*/
|
||||
|
||||
void SYS_CoreZeroInit(void);
|
||||
void SYS_CoreInit(void);
|
||||
|
||||
void SYS_CoreZeroShutdown(void);
|
||||
void __attribute__((noreturn)) SYS_CoreShutdown(void);
|
@ -1,24 +0,0 @@
|
||||
PROCESSOR := ARM9
|
||||
|
||||
TARGET := $(shell basename "$(CURDIR)")
|
||||
|
||||
SOURCE := source
|
||||
BUILD := build
|
||||
|
||||
SUBARCH := -D$(PROCESSOR) -march=armv5te -mtune=arm946e-s -mthumb -mfloat-abi=soft
|
||||
INCDIRS := source source/common source/filesys source/crypto source/fatfs source/nand source/virtual source/game source/gamecart source/lodepng source/lua source/qrcodegen source/system source/utils
|
||||
INCLUDE := $(foreach dir,$(INCDIRS),-I"$(shell pwd)/$(dir)")
|
||||
|
||||
ASFLAGS += $(SUBARCH) $(INCLUDE)
|
||||
CFLAGS += $(SUBARCH) $(INCLUDE) -fno-builtin-memcpy -flto
|
||||
LDFLAGS += $(SUBARCH) -Wl,--use-blx,-Map,$(TARGET).map -flto
|
||||
LIBS += -lm
|
||||
|
||||
include ../Makefile.common
|
||||
include ../Makefile.build
|
||||
|
||||
arm9_data.elf: arm9.elf
|
||||
$(OBJCOPY) -O elf32-littlearm -j .rodata* -j .data* -j .bss* $< $@
|
||||
|
||||
arm9_code.elf: arm9.elf
|
||||
$(OBJCOPY) -O elf32-littlearm -j .text* -j .vectors* $< $@
|
53
arm9/link.ld
53
arm9/link.ld
@ -1,53 +0,0 @@
|
||||
OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")
|
||||
OUTPUT_ARCH(arm)
|
||||
ENTRY(_start)
|
||||
|
||||
MEMORY
|
||||
{
|
||||
VECTORS (RX) : ORIGIN = 0x08000000, LENGTH = 64
|
||||
CODEMEM (RX) : ORIGIN = 0x08000040, LENGTH = 512K - 64
|
||||
BOOTROM (R) : ORIGIN = 0x08080000, LENGTH = 128K /* BootROM mirrors, don't touch! */
|
||||
DATAMEM (RW) : ORIGIN = 0x080A0000, LENGTH = 384K
|
||||
}
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
.vectors : ALIGN(4) {
|
||||
__vectors_lma = LOADADDR(.vectors);
|
||||
__vectors_vma = ABSOLUTE(.);
|
||||
KEEP(*(.vectors));
|
||||
. = ALIGN(4);
|
||||
__vectors_len = ABSOLUTE(.) - __vectors_vma;
|
||||
} >VECTORS AT>CODEMEM
|
||||
|
||||
.text : ALIGN(4) {
|
||||
__text_s = ABSOLUTE(.);
|
||||
*(.text.start);
|
||||
*(.text*);
|
||||
. = ALIGN(4);
|
||||
__text_e = ABSOLUTE(.);
|
||||
} >CODEMEM
|
||||
|
||||
.rodata : ALIGN(4) {
|
||||
*(.rodata*);
|
||||
. = ALIGN(4);
|
||||
__exidx_start = .;
|
||||
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
|
||||
__exidx_end = .;
|
||||
. = ALIGN(4);
|
||||
} >DATAMEM
|
||||
|
||||
.data : ALIGN(4) {
|
||||
*(.data*);
|
||||
. = ALIGN(4);
|
||||
} >DATAMEM
|
||||
|
||||
.bss (NOLOAD) : ALIGN(4) {
|
||||
__bss_start = .;
|
||||
*(.bss*);
|
||||
. = ALIGN(4);
|
||||
__bss_end = .;
|
||||
} >DATAMEM
|
||||
|
||||
__end__ = ABSOLUTE(.);
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
* This file is part of fastboot 3DS
|
||||
* Copyright (C) 2017 derrek, profi200
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#if !__ASSEMBLER__
|
||||
#error Only include this in assembly files!
|
||||
#endif
|
||||
|
||||
|
||||
.macro ASM_FUNC name
|
||||
.section .text.\name, "ax", %progbits
|
||||
.global \name
|
||||
.type \name %function
|
||||
.align 2
|
||||
\name:
|
||||
.endm
|
@ -1,69 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#define RGB(r,g,b) \
|
||||
(((u32)(r) >> 3) << 11 | \
|
||||
((u32)(g) >> 2) << 5 | \
|
||||
((u32)(b) >> 3))
|
||||
|
||||
// a base set of colors below
|
||||
#define COLOR_BLACK RGB(0x00, 0x00, 0x00)
|
||||
#define COLOR_WHITE RGB(0xFF, 0xFF, 0xFF)
|
||||
#define COLOR_GREY RGB(0x80, 0x80, 0x80)
|
||||
|
||||
#define COLOR_RED RGB(0xFF, 0x00, 0x00)
|
||||
#define COLOR_GREEN RGB(0x00, 0xFF, 0x00)
|
||||
#define COLOR_BLUE RGB(0x00, 0x00, 0xFF)
|
||||
#define COLOR_YELLOW RGB(0xFF, 0xFF, 0x00)
|
||||
#define COLOR_CYAN RGB(0xFF, 0x00, 0xFF)
|
||||
#define COLOR_ORANGE RGB(0xFF, 0xA5, 0x00)
|
||||
|
||||
#define COLOR_BRIGHTRED RGB(0xFF, 0x30, 0x30)
|
||||
#define COLOR_DARKRED RGB(0x80, 0x00, 0x00)
|
||||
#define COLOR_BRIGHTYELLOW RGB(0xFF, 0xFF, 0x30)
|
||||
#define COLOR_BRIGHTGREEN RGB(0x30, 0xFF, 0x30)
|
||||
#define COLOR_BRIGHTBLUE RGB(0x30, 0x30, 0xFF)
|
||||
|
||||
#define COLOR_TINTEDBLUE RGB(0x60, 0x60, 0x80)
|
||||
#define COLOR_TINTEDYELLOW RGB(0xD0, 0xD0, 0x60)
|
||||
#define COLOR_TINTEDGREEN RGB(0x70, 0x80, 0x70)
|
||||
#define COLOR_LIGHTGREY RGB(0xB0, 0xB0, 0xB0)
|
||||
#define COLOR_LIGHTERGREY RGB(0xD0, 0xD0, 0xD0)
|
||||
#define COLOR_DARKGREY RGB(0x50, 0x50, 0x50)
|
||||
#define COLOR_DARKESTGREY RGB(0x20, 0x20, 0x20)
|
||||
#define COLOR_SUPERFUCHSIA RGB(0xFF, 0x00, 0xEF)
|
||||
|
||||
// standard colors - used everywhere
|
||||
#ifndef COLOR_STD_BG
|
||||
#define COLOR_STD_BG COLOR_BLACK
|
||||
#endif
|
||||
#ifndef COLOR_STD_FONT
|
||||
#define COLOR_STD_FONT COLOR_WHITE
|
||||
#endif
|
||||
|
||||
// colors for GodMode9 file browser
|
||||
#define COLOR_SIDE_BAR COLOR_DARKGREY
|
||||
#define COLOR_MARKED COLOR_TINTEDYELLOW
|
||||
#define COLOR_FILE COLOR_TINTEDGREEN
|
||||
#define COLOR_DIR COLOR_TINTEDBLUE
|
||||
#define COLOR_ROOT COLOR_GREY
|
||||
#define COLOR_ENTRY(e) (((e)->marked) ? COLOR_MARKED : ((e)->type == T_DIR) ? COLOR_DIR : ((e)->type == T_FILE) ? COLOR_FILE : ((e)->type == T_ROOT) ? COLOR_ROOT : COLOR_GREY)
|
||||
|
||||
// hex viewer colors
|
||||
#define COLOR_HVOFFS RGB(0x40, 0x60, 0x50)
|
||||
#define COLOR_HVOFFSI COLOR_DARKESTGREY
|
||||
#define COLOR_HVASCII RGB(0x40, 0x80, 0x50)
|
||||
#define COLOR_HVHEX(i) ((i % 2) ? RGB(0x30, 0x90, 0x30) : RGB(0x30, 0x80, 0x30))
|
||||
|
||||
// text viewer / script viewer colors
|
||||
#define COLOR_TVOFFS RGB(0x40, 0x40, 0x40)
|
||||
#define COLOR_TVOFFSL RGB(0x30, 0x30, 0x30)
|
||||
#define COLOR_TVTEXT RGB(0xA0, 0xA0, 0xA0)
|
||||
#define COLOR_TVRUN RGB(0xC0, 0x00, 0x00)
|
||||
#define COLOR_TVCMT RGB(0x60, 0x60, 0x70)
|
||||
#define COLOR_TVCMD RGB(0xA0, 0xA0, 0xA0)
|
||||
|
||||
// battery symbol colors
|
||||
#define COLOR_BATTERY_CHARGING RGB(0x3D, 0xB7, 0xE4)
|
||||
#define COLOR_BATTERY_FULL RGB(0x0F, 0xB0, 0x1B)
|
||||
#define COLOR_BATTERY_MEDIUM RGB(0xFF, 0x88, 0x49)
|
||||
#define COLOR_BATTERY_LOW RGB(0xB4, 0x00, 0x00)
|
@ -1,213 +0,0 @@
|
||||
#include "hid.h"
|
||||
#include "i2c.h"
|
||||
#include "timer.h"
|
||||
#include "colors.h"
|
||||
#include "screenshot.h" // for screenshots
|
||||
|
||||
#include "arm.h"
|
||||
#include "fixp.h"
|
||||
#include "shmem.h"
|
||||
|
||||
#define HID_TOUCH_MAXPOINT (0x1000)
|
||||
#define HID_TOUCH_MIDPOINT (HID_TOUCH_MAXPOINT / 2)
|
||||
|
||||
|
||||
static void SetNotificationLED(u32 period_ms, u32 rgb565_color)
|
||||
{
|
||||
u32 rgb888_color =
|
||||
((rgb565_color >> 11) << (16+3) |
|
||||
(rgb565_color >> 5) << (8+2) |
|
||||
(rgb565_color << 3));
|
||||
u32 args[] = {period_ms, rgb888_color};
|
||||
PXI_DoCMD(PXICMD_SET_NOTIFY_LED, args, 2);
|
||||
}
|
||||
|
||||
// there's some weird thing going on when reading this
|
||||
// with an LDRD instruction so for now they'll be two
|
||||
// separate things - hopefully LTO won't get in the way
|
||||
u32 HID_ReadState(void)
|
||||
{
|
||||
return ARM_GetSHMEM()->hidState.keys;
|
||||
}
|
||||
|
||||
u32 HID_ReadRawTouchState(void)
|
||||
{
|
||||
return ARM_GetSHMEM()->hidState.touch;
|
||||
}
|
||||
|
||||
// ts_mult indicates a scalar for each axis
|
||||
// if |ts_mult| > 1 => point must be "scaled out"
|
||||
// if |ts_mult| < 1 => point must be "scaled in"
|
||||
// if ts_mult < 0 => axis is inverted
|
||||
static fixp_t ts_mult[2];
|
||||
|
||||
// ts_org indicates the coordinate system origin
|
||||
static int ts_org[2];
|
||||
|
||||
bool HID_TouchCalibratedTransform(u32 ts, u16 *x, u16 *y)
|
||||
{
|
||||
int xc, yc;
|
||||
int tx, ty;
|
||||
|
||||
if (ts & BIT(31))
|
||||
return false;
|
||||
|
||||
tx = HID_RAW_TX(ts) - HID_TOUCH_MIDPOINT;
|
||||
ty = HID_RAW_TY(ts) - HID_TOUCH_MIDPOINT;
|
||||
|
||||
xc = FIXP_TO_INT(fixp_round(tx * ts_mult[0])) + ts_org[0];
|
||||
yc = FIXP_TO_INT(fixp_round(ty * ts_mult[1])) + ts_org[1];
|
||||
|
||||
*x = clamp(xc, 0, (ts_org[0] * 2) - 1);
|
||||
*y = clamp(yc, 0, (ts_org[1] * 2) - 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool HID_ReadTouchState(u16 *x, u16 *y)
|
||||
{
|
||||
return HID_TouchCalibratedTransform(HID_ReadRawTouchState(), x, y);
|
||||
}
|
||||
|
||||
bool HID_SetCalibrationData(const HID_CalibrationData *calibs, u32 point_cnt, u32 screen_w, u32 screen_h)
|
||||
{
|
||||
u32 mid_x, mid_y;
|
||||
fixp_t avg_x, avg_y;
|
||||
|
||||
if (!screen_w || !screen_h || point_cnt <= 0 || point_cnt > 7)
|
||||
return false;
|
||||
|
||||
mid_x = screen_w / 2;
|
||||
mid_y = screen_h / 2;
|
||||
|
||||
avg_x = 0;
|
||||
avg_y = 0;
|
||||
for (u32 i = 0; i < point_cnt; i++) {
|
||||
const HID_CalibrationData *data = &calibs[i];
|
||||
fixp_t screen_x, screen_y, touch_x, touch_y;
|
||||
|
||||
// translate the [0, screen_w] x [0, screen_h] system
|
||||
// to [-screen_w/2, screen_w/2] x [-screen_h/2, screen_h/2]
|
||||
screen_x = INT_TO_FIXP(data->screen_x - mid_x);
|
||||
screen_y = INT_TO_FIXP(data->screen_y - mid_y);
|
||||
|
||||
// same thing for raw touchscreen data
|
||||
touch_x = INT_TO_FIXP(HID_RAW_TX(data->ts_raw) - HID_TOUCH_MIDPOINT);
|
||||
touch_y = INT_TO_FIXP(HID_RAW_TY(data->ts_raw) - HID_TOUCH_MIDPOINT);
|
||||
|
||||
// if the data retrieved came right in the middle, it's invalid
|
||||
if (!screen_x || !screen_y || !touch_x || !touch_y)
|
||||
return false;
|
||||
|
||||
// prevent integer overflows by dividing in this step
|
||||
avg_x += fixp_quotient(screen_x, touch_x * point_cnt);
|
||||
avg_y += fixp_quotient(screen_y, touch_y * point_cnt);
|
||||
}
|
||||
|
||||
// set state variables
|
||||
ts_mult[0] = avg_x;
|
||||
ts_mult[1] = avg_y;
|
||||
ts_org[0] = mid_x;
|
||||
ts_org[1] = mid_y;
|
||||
return true;
|
||||
}
|
||||
|
||||
const TouchBox* TouchBoxGet(u32* id, const u16 x, const u16 y, const TouchBox* tbs, const u32 tbn) {
|
||||
// check if inside touchbox
|
||||
for (u32 i = 0; !tbn || (i < tbn); i++) {
|
||||
const TouchBox* tb = tbs + i;
|
||||
if (tb->id == 0) break;
|
||||
if ((x >= tb->x) && (y >= tb->y) &&
|
||||
(x < tb->x + tb->w) && (y < tb->y + tb->h)) {
|
||||
if (id) *id = tb->id;
|
||||
return tb; // we know const is discarded here
|
||||
}
|
||||
}
|
||||
|
||||
if (id) *id = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
u32 InputWait(u32 timeout_sec) {
|
||||
static u64 delay = 0;
|
||||
u64 timer = timer_start();
|
||||
|
||||
u32 oldpad = HID_ReadState();
|
||||
u32 oldcart = CART_STATE;
|
||||
u32 oldsd = SD_STATE;
|
||||
|
||||
// enable notification LED if shell is closed
|
||||
// (this means we're waiting for user input)
|
||||
if (oldpad & SHELL_CLOSED) {
|
||||
SetNotificationLED(1000, COLOR_GREEN);
|
||||
while (HID_ReadState() & SHELL_CLOSED);
|
||||
}
|
||||
|
||||
delay = delay ? 144 : 256;
|
||||
|
||||
do {
|
||||
u32 newpad = HID_ReadState();
|
||||
|
||||
// handle closed shell (wait for open)
|
||||
if (newpad & SHELL_CLOSED) {
|
||||
while (HID_ReadState() & SHELL_CLOSED);
|
||||
continue;
|
||||
}
|
||||
|
||||
// no buttons pressed, check for I/O changes instead
|
||||
if (!(newpad & ~(SHELL_OPEN|SHELL_CLOSED))) {
|
||||
u32 state = CART_STATE;
|
||||
if (state != oldcart)
|
||||
return state ? CART_INSERT : CART_EJECT;
|
||||
|
||||
state = SD_STATE;
|
||||
if (state != oldsd)
|
||||
return state ? SD_INSERT : SD_EJECT;
|
||||
|
||||
oldpad = 0;
|
||||
delay = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
// special case for dpad keys
|
||||
// if any of those are held, don't wait for key changes
|
||||
// but do insert a small latency to make
|
||||
// sure any menus don't go flying off
|
||||
if ((newpad == oldpad) &&
|
||||
(!(newpad & BUTTON_ARROW) ||
|
||||
(delay && (timer_msec(timer) < delay))))
|
||||
continue;
|
||||
|
||||
// screenshot handling
|
||||
if ((newpad & BUTTON_ANY) == (BUTTON_R1 | BUTTON_L1))
|
||||
CreateScreenshot();
|
||||
|
||||
return newpad;
|
||||
} while (!timeout_sec || (timeout_sec && (timer_sec(timer) < timeout_sec)));
|
||||
|
||||
return TIMEOUT_HID;
|
||||
}
|
||||
|
||||
bool CheckButton(u32 button) {
|
||||
return (HID_ReadState() & button) == button;
|
||||
}
|
||||
|
||||
void ButtonToString(u32 button, char* str) {
|
||||
const char* strings[] = { BUTTON_STRINGS };
|
||||
|
||||
*str = '\0';
|
||||
if (button) {
|
||||
u32 b = 0;
|
||||
for (b = 0; !((button>>b)&0x1); b++);
|
||||
if (b < countof(strings)) strcpy(str, strings[b]);
|
||||
}
|
||||
}
|
||||
|
||||
u32 StringToButton(char* str) {
|
||||
const char* strings[] = { BUTTON_STRINGS };
|
||||
|
||||
u32 b = 0;
|
||||
for (b = 0; b < countof(strings); b++)
|
||||
if (strcmp(str, strings[b]) == 0) break;
|
||||
|
||||
return (b == countof(strings)) ? 0 : 1<<b;
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "hid_map.h"
|
||||
|
||||
// see: http://3dbrew.org/wiki/CONFIG9_Registers
|
||||
// see: http://3dbrew.org/wiki/EMMC_Registers
|
||||
#define CART_STATE (~(*(volatile u8*)0x10000010) & 0x1)
|
||||
#define SD_STATE ((*(volatile u16*)0x1000601C) & (0x1<<5))
|
||||
|
||||
#define HID_RAW_TX(t) ((s32)(((t) / (1 << 16)) & 0xFFF))
|
||||
#define HID_RAW_TY(t) ((s32)((t) & 0xFFF))
|
||||
|
||||
u32 HID_ReadState(void);
|
||||
|
||||
// ts_raw is the raw touchscreen value obtained when pressing down
|
||||
// the touchscreen at the screen coordinates [screen_x, screen_y]
|
||||
// note: no point can be at the center
|
||||
typedef struct {
|
||||
u32 ts_raw;
|
||||
int screen_x, screen_y;
|
||||
} HID_CalibrationData;
|
||||
|
||||
u32 HID_ReadRawTouchState(void);
|
||||
bool HID_ReadTouchState(u16 *x, u16 *y);
|
||||
bool HID_TouchCalibratedTransform(u32 ts, u16 *x, u16 *y);
|
||||
bool HID_SetCalibrationData(const HID_CalibrationData *calibs, u32 point_cnt, u32 screen_w, u32 screen_h);
|
||||
|
||||
typedef struct {
|
||||
u16 x;
|
||||
u16 y;
|
||||
u16 w;
|
||||
u16 h;
|
||||
u32 id; // shouldn't be zero
|
||||
} TouchBox;
|
||||
|
||||
// abstraction for HID_ReadTouchState, also returns touchbox id (if any)
|
||||
const TouchBox* TouchBoxGet(u32* id, const u16 x, const u16 y, const TouchBox* tbs, const u32 tbn);
|
||||
|
||||
u32 InputWait(u32 timeout_sec);
|
||||
bool CheckButton(u32 button);
|
||||
|
||||
void ButtonToString(u32 button, char* str);
|
||||
u32 StringToButton(char* str);
|
@ -1,70 +0,0 @@
|
||||
@ memcpy_arm946e-s - hand written reimplementation of memcpy to be sequential
|
||||
@ Written in 2019 by luigoalma <luigoalma at gmail dot com>
|
||||
@ To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
|
||||
@ For a copy of CC0 Public Domain Dedication, see <https://creativecommons.org/publicdomain/zero/1.0/>.
|
||||
.cpu arm946e-s
|
||||
.arch armv5te
|
||||
.arm
|
||||
.section .text.memcpy, "ax", %progbits
|
||||
.align 2
|
||||
.global memcpy
|
||||
.syntax unified
|
||||
.type memcpy, %function
|
||||
memcpy:
|
||||
@ r0 = dest
|
||||
@ r1 = src
|
||||
@ r2 = length
|
||||
@ check if length 0 and return if so
|
||||
cmp r2, #0
|
||||
bxeq lr
|
||||
push {r0,r4-r9,lr}
|
||||
@ pre-fetch data
|
||||
pld [r1]
|
||||
@ alignment check with word size
|
||||
@ if not aligned but both are in the same misalignment, fix it up
|
||||
@ otherwise jump to basic loop
|
||||
orr r12, r0, r1
|
||||
ands r12, r12, #3
|
||||
beq .L1
|
||||
mov r12, r0, LSL#30
|
||||
cmp r12, r1, LSL#30
|
||||
bne .L6
|
||||
.L0:
|
||||
ldrb r3, [r1], #1
|
||||
strb r3, [r0], #1
|
||||
subs r2, r2, #1
|
||||
popeq {r0,r4-r9,pc}
|
||||
adds r12, r12, #0x40000000
|
||||
bne .L0
|
||||
.L1:
|
||||
@ check if length higher than 32
|
||||
@ if so, do the 32 byte block copy loop,
|
||||
@ until there's nothing left or remainder to copy is less than 32
|
||||
movs r3, r2, LSR#5
|
||||
beq .L3
|
||||
.L2:
|
||||
ldm r1!, {r4-r9,r12,lr}
|
||||
stm r0!, {r4-r9,r12,lr}
|
||||
subs r3, r3, #1
|
||||
bne .L2
|
||||
ands r2, r2, #0x1F
|
||||
popeq {r0,r4-r9,pc}
|
||||
.L3:
|
||||
@ copy in word size the remaining data,
|
||||
@ and finish off with basic loop if can't copy all by word size.
|
||||
movs r3, r2, LSR#2
|
||||
beq .L6
|
||||
.L4:
|
||||
ldr r12, [r1], #4
|
||||
str r12, [r0], #4
|
||||
subs r3, r3, #1
|
||||
bne .L4
|
||||
ands r2, r2, #0x3
|
||||
.L5: @ the basic loop
|
||||
popeq {r0,r4-r9,pc}
|
||||
.L6:
|
||||
ldrb r3, [r1], #1
|
||||
strb r3, [r0], #1
|
||||
subs r2, r2, #1
|
||||
b .L5
|
||||
.size memcpy, .-memcpy
|
@ -1,26 +0,0 @@
|
||||
/*
|
||||
* This file is part of fastboot 3DS
|
||||
* Copyright (C) 2019 Aurora Wright, TuxSH, derrek, profi200
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
// Based on https://github.com/AuroraWright/Luma3DS/blob/master/arm9/source/alignedseqmemcpy.s
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
||||
|
||||
void iomemcpy(vu32 *restrict dst, const vu32 *restrict src, u32 size);
|
||||
void iomemset(vu32 *ptr, u32 value, u32 size);
|
@ -1,87 +0,0 @@
|
||||
/*
|
||||
* This file is part of fastboot 3DS
|
||||
* Copyright (C) 2019 Aurora Wright, TuxSH, derrek, profi200
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
@ Based on https://github.com/AuroraWright/Luma3DS/blob/master/arm9/source/alignedseqmemcpy.s
|
||||
|
||||
#include "asmfunc.h"
|
||||
|
||||
.arm
|
||||
.cpu arm946e-s
|
||||
.fpu softvfp
|
||||
|
||||
|
||||
|
||||
@ void iomemcpy(vu32 *restrict dst, const vu32 *restrict src, u32 size)
|
||||
ASM_FUNC iomemcpy
|
||||
bics r12, r2, #31
|
||||
beq iomemcpy_test_words
|
||||
stmfd sp!, {r4-r10}
|
||||
iomemcpy_blocks_lp:
|
||||
ldmia r1!, {r3-r10}
|
||||
stmia r0!, {r3-r10}
|
||||
subs r12, #32
|
||||
bne iomemcpy_blocks_lp
|
||||
ldmfd sp!, {r4-r10}
|
||||
iomemcpy_test_words:
|
||||
ands r12, r2, #28
|
||||
beq iomemcpy_halfword_byte
|
||||
iomemcpy_words_lp:
|
||||
ldr r3, [r1], #4
|
||||
str r3, [r0], #4
|
||||
subs r12, #4
|
||||
bne iomemcpy_words_lp
|
||||
iomemcpy_halfword_byte:
|
||||
tst r2, #2
|
||||
ldrneh r3, [r1], #2
|
||||
strneh r3, [r0], #2
|
||||
tst r2, #1
|
||||
ldrneb r3, [r1]
|
||||
strneb r3, [r0]
|
||||
bx lr
|
||||
|
||||
|
||||
@ void iomemset(vu32 *ptr, u32 value, u32 size)
|
||||
ASM_FUNC iomemset
|
||||
bics r12, r2, #31
|
||||
beq iomemset_test_words
|
||||
stmfd sp!, {r4-r9}
|
||||
mov r3, r1
|
||||
mov r4, r1
|
||||
mov r5, r1
|
||||
mov r6, r1
|
||||
mov r7, r1
|
||||
mov r8, r1
|
||||
mov r9, r1
|
||||
iomemset_blocks_lp:
|
||||
stmia r0!, {r1, r3-r9}
|
||||
subs r12, #32
|
||||
bne iomemset_blocks_lp
|
||||
ldmfd sp!, {r4-r9}
|
||||
iomemset_test_words:
|
||||
ands r12, r2, #28
|
||||
beq iomemset_halfword_byte
|
||||
iomemset_words_lp:
|
||||
str r1, [r0], #4
|
||||
subs r12, #4
|
||||
bne iomemset_words_lp
|
||||
iomemset_halfword_byte:
|
||||
tst r2, #2
|
||||
strneh r1, [r0], #2
|
||||
tst r2, #1
|
||||
strneb r1, [r0]
|
||||
bx lr
|
@ -1,45 +0,0 @@
|
||||
#include "arm.h"
|
||||
#include "power.h"
|
||||
#include "i2c.h"
|
||||
#include "pxi.h"
|
||||
|
||||
u32 SetScreenBrightness(int level) {
|
||||
u32 arg;
|
||||
|
||||
if (level != BRIGHTNESS_AUTOMATIC) {
|
||||
arg = clamp(level, BRIGHTNESS_MIN, BRIGHTNESS_MAX);
|
||||
} else {
|
||||
arg = 0;
|
||||
}
|
||||
|
||||
return PXI_DoCMD(PXICMD_SET_BRIGHTNESS, &arg, 1);
|
||||
}
|
||||
|
||||
u32 GetBatteryPercent() {
|
||||
u8 battery = 0;
|
||||
I2C_readRegBuf(I2C_DEV_MCU, 0x0B, &battery, 1);
|
||||
return battery;
|
||||
}
|
||||
|
||||
bool IsCharging() {
|
||||
u8 flags = 0;
|
||||
I2C_readRegBuf(I2C_DEV_MCU, 0x0F, &flags, 1);
|
||||
return flags & (1<<4);
|
||||
}
|
||||
|
||||
void Reboot() {
|
||||
I2C_writeReg(I2C_DEV_MCU, 0x22, 1 << 0); // poweroff LCD to prevent MCU hangs
|
||||
ARM_WbDC();
|
||||
ARM_DSB();
|
||||
I2C_writeReg(I2C_DEV_MCU, 0x20, 1 << 2);
|
||||
while(true);
|
||||
}
|
||||
|
||||
void PowerOff()
|
||||
{
|
||||
I2C_writeReg(I2C_DEV_MCU, 0x22, 1 << 0); // poweroff LCD to prevent MCU hangs
|
||||
ARM_WbDC();
|
||||
ARM_DSB();
|
||||
I2C_writeReg(I2C_DEV_MCU, 0x20, 1 << 0);
|
||||
while(true);
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#define BRIGHTNESS_AUTOMATIC (-1)
|
||||
#define BRIGHTNESS_MIN (10)
|
||||
#define BRIGHTNESS_MAX (210)
|
||||
|
||||
u32 SetScreenBrightness(int level);
|
||||
u32 GetBatteryPercent();
|
||||
bool IsCharging();
|
||||
void Reboot();
|
||||
void PowerOff();
|
@ -1,42 +0,0 @@
|
||||
#include "rtc.h"
|
||||
#include "i2c.h"
|
||||
|
||||
bool is_valid_dstime(DsTime* dstime) {
|
||||
// check the time...
|
||||
if ((DSTIMEGET(dstime, bcd_h) >= 24) ||
|
||||
(DSTIMEGET(dstime, bcd_m) >= 60) ||
|
||||
(DSTIMEGET(dstime, bcd_s) >= 60))
|
||||
return false;
|
||||
|
||||
// check the date...
|
||||
u32 year = 2000 + DSTIMEGET(dstime, bcd_Y);
|
||||
u32 month = DSTIMEGET(dstime, bcd_M);
|
||||
u32 day = DSTIMEGET(dstime, bcd_D);
|
||||
|
||||
// date: year & month
|
||||
if ((year >= 2100) || (month == 0) || (month > 12))
|
||||
return false;
|
||||
|
||||
// date: day
|
||||
// see: https://github.com/devkitPro/libnds/blob/9678bf09389cb1fcdc99dfa0357ec0cbe51dd0b7/source/arm7/clock.c#L224-L262
|
||||
u32 months_lastday[1+12] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
||||
u32 leap = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0;
|
||||
u32 days_in_month = months_lastday[month] + ((month == 2) ? leap : 0);
|
||||
if (day > days_in_month) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool get_dstime(DsTime* dstime) {
|
||||
return (I2C_readRegBuf(I2C_DEV_MCU, 0x30, (void*) dstime, sizeof(DsTime)));
|
||||
}
|
||||
|
||||
bool set_dstime(DsTime* dstime) {
|
||||
if (!is_valid_dstime(dstime)) return false;
|
||||
for (u32 i = 0; i < sizeof(DsTime); i++) {
|
||||
if ((i == 3) || (i == 7)) continue; // skip the unused bytes
|
||||
if (!I2C_writeReg(I2C_DEV_MCU, 0x30+i, ((u8*)dstime)[i]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#define BCDVALID(b) (((b)<=0x99)&&(((b)&0xF)<=0x9)&&((((b)>>4)&0xF)<=0x9))
|
||||
#define BCD2NUM(b) (BCDVALID(b) ? (((b)&0xF)+((((b)>>4)&0xF)*10)) : 0xFF)
|
||||
#define NUM2BCD(n) ((n<99) ? (((n/10)*0x10)|(n%10)) : 0x99)
|
||||
#define DSTIMEGET(bcd,n) (BCD2NUM((bcd)->n))
|
||||
|
||||
// see: http://3dbrew.org/wiki/I2C_Registers#Device_3 (register 30)
|
||||
typedef struct {
|
||||
u8 bcd_s;
|
||||
u8 bcd_m;
|
||||
u8 bcd_h;
|
||||
u8 weekday;
|
||||
u8 bcd_D;
|
||||
u8 bcd_M;
|
||||
u8 bcd_Y;
|
||||
u8 leap_count;
|
||||
} PACKED_STRUCT DsTime;
|
||||
|
||||
bool is_valid_dstime(DsTime* dstime);
|
||||
bool get_dstime(DsTime* dstime);
|
||||
bool set_dstime(DsTime* dstime);
|
@ -1,66 +0,0 @@
|
||||
#include "common.h"
|
||||
#include "ui.h"
|
||||
#include "rtc.h"
|
||||
#include "vff.h"
|
||||
#include "png.h"
|
||||
|
||||
static void Screenshot_Transpose(u16 *dest, const u16 *fb, u32 w, u32 stride)
|
||||
{
|
||||
for (u32 y = 0; y < SCREEN_HEIGHT; y++) {
|
||||
for (u32 x = 0; x < w; x++)
|
||||
*(dest++) = GetColor(fb, x, y);
|
||||
dest += stride;
|
||||
}
|
||||
}
|
||||
|
||||
void CreateScreenshot(void) {
|
||||
u8 *png;
|
||||
u16 *buffer;
|
||||
DsTime dstime;
|
||||
size_t png_size;
|
||||
char filename[64];
|
||||
u32 snapbuf_size, snap_w, snap_h, bot_offset;
|
||||
|
||||
snapbuf_size = (SCREEN_WIDTH_TOP * SCREEN_HEIGHT * BYTES_PER_PIXEL) * 2;
|
||||
snap_w = SCREEN_WIDTH_TOP;
|
||||
snap_h = SCREEN_HEIGHT * 2;
|
||||
|
||||
fvx_rmkdir(OUTPUT_PATH);
|
||||
get_dstime(&dstime);
|
||||
snprintf(filename, sizeof(filename), OUTPUT_PATH "/snap_%02X%02X%02X%02X%02X%02X.png",
|
||||
dstime.bcd_Y, dstime.bcd_M, dstime.bcd_D,
|
||||
dstime.bcd_h, dstime.bcd_m, dstime.bcd_s);
|
||||
filename[63] = '\0';
|
||||
|
||||
buffer = malloc(snapbuf_size);
|
||||
if (!buffer) return;
|
||||
|
||||
for (unsigned i = snapbuf_size/4; i < snapbuf_size/2; i++)
|
||||
buffer[i] = RGB(0x1F, 0x1F, 0x1F); // gray background
|
||||
|
||||
bot_offset = (SCREEN_WIDTH_TOP * SCREEN_HEIGHT) + 40;
|
||||
|
||||
Screenshot_Transpose(buffer, TOP_SCREEN, SCREEN_WIDTH_TOP, 0);
|
||||
Screenshot_Transpose(buffer + bot_offset, BOT_SCREEN, SCREEN_WIDTH_BOT, 80);
|
||||
|
||||
png = PNG_Compress(buffer, snap_w, snap_h, &png_size);
|
||||
|
||||
if (png && png_size) {
|
||||
u16 *buffer_top = buffer, *buffer_bottom = buffer + bot_offset;
|
||||
|
||||
// "snap effect"
|
||||
memcpy(buffer_bottom, BOT_SCREEN, SCREEN_SIZE_BOT);
|
||||
memcpy(buffer_top, TOP_SCREEN, SCREEN_SIZE_TOP);
|
||||
memset(BOT_SCREEN, 0, SCREEN_SIZE_BOT);
|
||||
memset(TOP_SCREEN, 0, SCREEN_SIZE_TOP);
|
||||
|
||||
fvx_qwrite(filename, png, 0, png_size, NULL);
|
||||
|
||||
memcpy(BOT_SCREEN, buffer_bottom, SCREEN_SIZE_BOT);
|
||||
memcpy(TOP_SCREEN, buffer_top, SCREEN_SIZE_TOP);
|
||||
}
|
||||
// what to do on error...?
|
||||
|
||||
free(buffer);
|
||||
free(png);
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
void CreateScreenshot(void);
|
@ -1,105 +0,0 @@
|
||||
#include "common.h"
|
||||
|
||||
|
||||
// see https://gist.github.com/SciresM/cdd2266efb80175d37eabbe86f9d8c52 for source
|
||||
// thanks SciresM, Myria, Normmatt, TuxSH and hedgeberg for the signatures!
|
||||
|
||||
const u8 sig_nand_firm_retail[256] = {
|
||||
0xB6, 0x72, 0x45, 0x31, 0xC4, 0x48, 0x65, 0x7A, 0x2A, 0x2E, 0xE3, 0x06, 0x45, 0x7E, 0x35, 0x0A,
|
||||
0x10, 0xD5, 0x44, 0xB4, 0x28, 0x59, 0xB0, 0xE5, 0xB0, 0xBE, 0xD2, 0x75, 0x34, 0xCC, 0xCC, 0x2A,
|
||||
0x4D, 0x47, 0xED, 0xEA, 0x60, 0xA7, 0xDD, 0x99, 0x93, 0x99, 0x50, 0xA6, 0x35, 0x7B, 0x1E, 0x35,
|
||||
0xDF, 0xC7, 0xFA, 0xC7, 0x73, 0xB7, 0xE1, 0x2E, 0x7C, 0x14, 0x81, 0x23, 0x4A, 0xF1, 0x41, 0xB3,
|
||||
0x1C, 0xF0, 0x8E, 0x9F, 0x62, 0x29, 0x3A, 0xA6, 0xBA, 0xAE, 0x24, 0x6C, 0x15, 0x09, 0x5F, 0x8B,
|
||||
0x78, 0x40, 0x2A, 0x68, 0x4D, 0x85, 0x2C, 0x68, 0x05, 0x49, 0xFA, 0x5B, 0x3F, 0x14, 0xD9, 0xE8,
|
||||
0x38, 0xA2, 0xFB, 0x9C, 0x09, 0xA1, 0x5A, 0xBB, 0x40, 0xDC, 0xA2, 0x5E, 0x40, 0xA3, 0xDD, 0xC1,
|
||||
0xF5, 0x8E, 0x79, 0xCE, 0xC9, 0x01, 0x97, 0x43, 0x63, 0xA9, 0x46, 0xE9, 0x9B, 0x43, 0x46, 0xE8,
|
||||
0xA3, 0x72, 0xB6, 0xCD, 0x55, 0xA7, 0x07, 0xE1, 0xEA, 0xB9, 0xBE, 0xC0, 0x20, 0x0B, 0x5B, 0xA0,
|
||||
0xB6, 0x61, 0x23, 0x6A, 0x87, 0x08, 0xD7, 0x04, 0x51, 0x7F, 0x43, 0xC6, 0xC3, 0x8E, 0xE9, 0x56,
|
||||
0x01, 0x11, 0xE1, 0x40, 0x5E, 0x5E, 0x8E, 0xD3, 0x56, 0xC4, 0x9C, 0x4F, 0xF6, 0x82, 0x3D, 0x12,
|
||||
0x19, 0xAF, 0xAE, 0xEB, 0x3D, 0xF3, 0xC3, 0x6B, 0x62, 0xBB, 0xA8, 0x8F, 0xC1, 0x5B, 0xA8, 0x64,
|
||||
0x8F, 0x93, 0x33, 0xFD, 0x9F, 0xC0, 0x92, 0xB8, 0x14, 0x6C, 0x3D, 0x90, 0x8F, 0x73, 0x15, 0x5D,
|
||||
0x48, 0xBE, 0x89, 0xD7, 0x26, 0x12, 0xE1, 0x8E, 0x4A, 0xA8, 0xEB, 0x9B, 0x7F, 0xD2, 0xA5, 0xF7,
|
||||
0x32, 0x8C, 0x4E, 0xCB, 0xFB, 0x00, 0x83, 0x83, 0x3C, 0xBD, 0x5C, 0x98, 0x3A, 0x25, 0xCE, 0xB8,
|
||||
0xB9, 0x41, 0xCC, 0x68, 0xEB, 0x01, 0x7C, 0xE8, 0x7F, 0x5D, 0x79, 0x3A, 0xCA, 0x09, 0xAC, 0xF7
|
||||
};
|
||||
|
||||
const u8 sig_nand_firm_dev[256] = {
|
||||
0x88, 0x69, 0x7C, 0xDC, 0xA9, 0xD1, 0xEA, 0x31, 0x82, 0x56, 0xFC, 0xD9, 0xCE, 0xD4, 0x29, 0x64,
|
||||
0xC1, 0xE9, 0x8A, 0xBC, 0x64, 0x86, 0xB2, 0xF1, 0x28, 0xEC, 0x02, 0xE7, 0x1C, 0x5A, 0xE3, 0x5D,
|
||||
0x63, 0xD3, 0xBF, 0x12, 0x46, 0x13, 0x40, 0x81, 0xAF, 0x68, 0x75, 0x47, 0x87, 0xFC, 0xB9, 0x22,
|
||||
0x57, 0x1D, 0x7F, 0x61, 0xA3, 0x0D, 0xE4, 0xFC, 0xFA, 0x82, 0x93, 0xA9, 0xDA, 0x51, 0x23, 0x96,
|
||||
0xF1, 0x31, 0x9A, 0x36, 0x49, 0x68, 0x46, 0x4C, 0xA9, 0x80, 0x6E, 0x0A, 0x52, 0x56, 0x74, 0x86,
|
||||
0x75, 0x4C, 0xDD, 0xD4, 0xC3, 0xA6, 0x2B, 0xDC, 0xE2, 0x55, 0xE0, 0xDE, 0xEC, 0x23, 0x01, 0x29,
|
||||
0xC1, 0xBA, 0xE1, 0xAE, 0x95, 0xD7, 0x86, 0x86, 0x56, 0x37, 0xC1, 0xE6, 0x5F, 0xAE, 0x83, 0xED,
|
||||
0xF8, 0xE7, 0xB0, 0x7D, 0x17, 0xC0, 0xAA, 0xDA, 0x8F, 0x05, 0x5B, 0x64, 0x0D, 0x45, 0xAB, 0x0B,
|
||||
0xAC, 0x76, 0xFF, 0x7B, 0x34, 0x39, 0xF5, 0xA4, 0xBF, 0xE8, 0xF7, 0xE0, 0xE1, 0x03, 0xBC, 0xE9,
|
||||
0x95, 0xFA, 0xD9, 0x13, 0xFB, 0x72, 0x9D, 0x3D, 0x03, 0x0B, 0x26, 0x44, 0xEC, 0x48, 0x39, 0x64,
|
||||
0x24, 0xE0, 0x56, 0x3A, 0x1B, 0x3E, 0x6A, 0x1F, 0x68, 0x0B, 0x39, 0xFC, 0x14, 0x61, 0x88, 0x6F,
|
||||
0xA7, 0xA6, 0x0B, 0x6B, 0x56, 0xC5, 0xA8, 0x46, 0x55, 0x4A, 0xE6, 0x48, 0xFC, 0x46, 0xE3, 0x0E,
|
||||
0x24, 0x67, 0x8F, 0xAF, 0x1D, 0xC3, 0xCE, 0xB1, 0x0C, 0x2A, 0x95, 0x0F, 0x4F, 0xFA, 0x20, 0x83,
|
||||
0x23, 0x4E, 0xD8, 0xDC, 0xC3, 0x58, 0x7A, 0x6D, 0x75, 0x1A, 0x7E, 0x9A, 0xFA, 0x06, 0x15, 0x69,
|
||||
0x55, 0x08, 0x4F, 0xF2, 0x72, 0x5B, 0x69, 0x8E, 0xB1, 0x74, 0x54, 0xD9, 0xB0, 0x2B, 0x6B, 0x76,
|
||||
0xBE, 0x47, 0xAB, 0xBE, 0x20, 0x62, 0x94, 0x36, 0x69, 0x87, 0xA4, 0xCA, 0xB4, 0x2C, 0xBD, 0x0B
|
||||
};
|
||||
|
||||
const u8 sig_nand_ncsd_retail[256] = {
|
||||
0x6C, 0xF5, 0x2F, 0x89, 0xF3, 0x78, 0x12, 0x0B, 0xFA, 0x4E, 0x10, 0x61, 0xD7, 0x36, 0x16, 0x34,
|
||||
0xD9, 0xA2, 0x54, 0xA4, 0xF5, 0x7A, 0xA5, 0xBD, 0x9F, 0x2C, 0x30, 0x93, 0x4F, 0x0E, 0x68, 0xCB,
|
||||
0xE6, 0x61, 0x1D, 0x90, 0xD7, 0x4C, 0xAA, 0xAC, 0xB6, 0xA9, 0x95, 0x56, 0x56, 0x47, 0x33, 0x3D,
|
||||
0xC1, 0x70, 0x92, 0xD3, 0x20, 0x13, 0x10, 0x89, 0xCC, 0xCD, 0x63, 0x31, 0xCB, 0x3A, 0x59, 0x5D,
|
||||
0x1B, 0xA2, 0x99, 0xA3, 0x2F, 0xF4, 0xD8, 0xE5, 0xDD, 0x1E, 0xB4, 0x6A, 0x2A, 0x57, 0x93, 0x5F,
|
||||
0x6F, 0xE6, 0x37, 0x32, 0x2D, 0x3B, 0xC4, 0xF6, 0x7C, 0xFE, 0xD6, 0xC2, 0x25, 0x4C, 0x08, 0x9C,
|
||||
0x62, 0xFA, 0x11, 0xD0, 0x82, 0x4A, 0x84, 0x4C, 0x79, 0xEE, 0x5A, 0x4F, 0x27, 0x3D, 0x46, 0xC2,
|
||||
0x3B, 0xBB, 0xF0, 0xA2, 0xAF, 0x6A, 0xCA, 0xDB, 0xE6, 0x46, 0xF4, 0x6B, 0x86, 0xD1, 0x28, 0x9C,
|
||||
0x7F, 0xF7, 0xE8, 0x16, 0xCF, 0xDA, 0x4B, 0xC3, 0x3D, 0xFF, 0x9D, 0x17, 0x5A, 0xC6, 0x9F, 0x72,
|
||||
0x40, 0x6C, 0x07, 0x1B, 0x51, 0xF4, 0x5A, 0x1A, 0xCB, 0x87, 0xF1, 0x68, 0xC1, 0x77, 0xCB, 0x9B,
|
||||
0xE6, 0xC3, 0x92, 0xF0, 0x34, 0x18, 0x49, 0xAE, 0x5D, 0x51, 0x0D, 0x26, 0xEE, 0xC1, 0x09, 0x7B,
|
||||
0xEB, 0xFB, 0x9D, 0x14, 0x4A, 0x16, 0x47, 0x30, 0x1B, 0xEA, 0xF9, 0x52, 0x0D, 0x22, 0xC5, 0x5A,
|
||||
0xF4, 0x6D, 0x49, 0x28, 0x4C, 0xC7, 0xF9, 0xFB, 0xBA, 0x37, 0x1A, 0x6D, 0x6E, 0x4C, 0x55, 0xF1,
|
||||
0xE5, 0x36, 0xD6, 0x23, 0x7F, 0xFF, 0x54, 0xB3, 0xE9, 0xC1, 0x1A, 0x20, 0xCF, 0xCC, 0xAC, 0x0C,
|
||||
0x6B, 0x06, 0xF6, 0x95, 0x76, 0x6A, 0xCE, 0xB1, 0x8B, 0xE3, 0x32, 0x99, 0xA9, 0x4C, 0xFC, 0xA7,
|
||||
0xE2, 0x58, 0x81, 0x86, 0x52, 0xF7, 0x52, 0x6B, 0x30, 0x6B, 0x52, 0xE0, 0xAE, 0xD0, 0x42, 0x18
|
||||
};
|
||||
|
||||
const u8 sig_nand_ncsd_dev[256] = {
|
||||
0x53, 0xCB, 0x0E, 0x4E, 0xB1, 0xA6, 0xFF, 0x84, 0x28, 0x4B, 0xE0, 0xE7, 0x38, 0x5A, 0xB4, 0xA6,
|
||||
0x86, 0xA8, 0xBB, 0xCB, 0xC1, 0x61, 0x02, 0x47, 0x92, 0x80, 0xE0, 0x58, 0x36, 0x55, 0xD2, 0x71,
|
||||
0x3F, 0xE5, 0x06, 0xFA, 0xEE, 0x74, 0xF8, 0xD1, 0x0F, 0x12, 0x20, 0x44, 0x1C, 0xC2, 0xFF, 0x5D,
|
||||
0x6D, 0xDE, 0x99, 0xBE, 0x79, 0xC1, 0x9B, 0x38, 0x6C, 0xAF, 0x68, 0xD5, 0xEB, 0x8C, 0xED, 0x1A,
|
||||
0xAB, 0x4D, 0x24, 0x3C, 0x5F, 0x39, 0x86, 0x80, 0xD3, 0x1C, 0xD2, 0xE3, 0xC9, 0xDD, 0x56, 0x70,
|
||||
0xF2, 0xA8, 0x8D, 0x56, 0x3B, 0x8F, 0x65, 0xF5, 0xB2, 0x34, 0xFD, 0x2E, 0xBB, 0x3B, 0xE4, 0x4A,
|
||||
0x3B, 0x6C, 0x30, 0x27, 0x22, 0xA2, 0xAD, 0xFB, 0x56, 0xAE, 0x3E, 0x1F, 0x64, 0x17, 0xBD, 0xEC,
|
||||
0x1E, 0x5A, 0x86, 0xAA, 0xBB, 0xAF, 0xBE, 0x94, 0x19, 0xAC, 0xA8, 0xFD, 0xCD, 0x45, 0xE2, 0xCD,
|
||||
0xF1, 0xEB, 0x69, 0x5F, 0x6E, 0xA8, 0x78, 0x16, 0x12, 0x2D, 0x7B, 0xE9, 0x8E, 0xEF, 0x92, 0xC0,
|
||||
0x81, 0x4B, 0x16, 0xB2, 0x15, 0xB3, 0x1D, 0x8C, 0x81, 0x3B, 0xB3, 0x55, 0xCE, 0xA8, 0x13, 0x8F,
|
||||
0xB3, 0xBF, 0x23, 0x74, 0x24, 0x68, 0x42, 0xCD, 0x91, 0xE1, 0xF9, 0xAA, 0xFF, 0x76, 0x87, 0x86,
|
||||
0x17, 0xCE, 0x02, 0x06, 0x47, 0x77, 0xAE, 0xA0, 0x87, 0x6A, 0x2C, 0x24, 0x5C, 0x78, 0x43, 0x41,
|
||||
0xCD, 0xEE, 0x90, 0xD6, 0x91, 0x74, 0x59, 0x08, 0xA6, 0xFF, 0x9C, 0xE7, 0x81, 0x16, 0x67, 0x96,
|
||||
0xF9, 0xF1, 0x23, 0x8F, 0x88, 0x4C, 0x84, 0xD6, 0xF1, 0xEE, 0xBB, 0x2E, 0x40, 0xB4, 0xBC, 0xA0,
|
||||
0x0A, 0x7B, 0x1E, 0x91, 0x3E, 0x09, 0x80, 0xD2, 0x9F, 0xF6, 0x06, 0x1D, 0x8A, 0xA9, 0x44, 0xC6,
|
||||
0x63, 0xF2, 0x63, 0x81, 0x27, 0xF7, 0xCC, 0xAB, 0x6F, 0xC7, 0x15, 0x38, 0x47, 0x1A, 0x51, 0x38
|
||||
};
|
||||
|
||||
|
||||
// see http://www.sighax.com/ for source of this signature
|
||||
// thanks derrek, plutoo, yellows8, smea and profi200
|
||||
|
||||
const u8 sig_nand_firm_retail_alt[256] = {
|
||||
0x6E, 0xFF, 0x20, 0x9C, 0x8F, 0x4A, 0xF6, 0x1F, 0x06, 0x24, 0x13, 0xD6, 0x02, 0xCA, 0x6B, 0x4D,
|
||||
0xA1, 0xEB, 0x5A, 0xB9, 0xB6, 0xF1, 0xA2, 0xAB, 0x22, 0x6A, 0x71, 0x1D, 0xA2, 0xCC, 0xC2, 0x7C,
|
||||
0x74, 0xDE, 0x17, 0x41, 0x14, 0x3B, 0xF6, 0x90, 0x58, 0x28, 0x4C, 0xAF, 0x44, 0x4F, 0x92, 0xA4,
|
||||
0x5A, 0xAF, 0xD5, 0xA0, 0x68, 0x04, 0x33, 0x23, 0xD4, 0x8A, 0xF1, 0xD0, 0xEC, 0x05, 0x56, 0x4E,
|
||||
0xBC, 0x79, 0xB5, 0x51, 0x34, 0xE9, 0x1A, 0x86, 0xC3, 0x78, 0x8C, 0x97, 0xBC, 0x29, 0xD5, 0xA5,
|
||||
0x8A, 0x8A, 0x45, 0x25, 0x58, 0x43, 0xB8, 0x91, 0x22, 0xC7, 0x80, 0x45, 0x42, 0xF7, 0x26, 0x77,
|
||||
0xC8, 0xDA, 0x5E, 0xB7, 0x42, 0x9B, 0xAF, 0x18, 0xF7, 0xA8, 0xB0, 0x2E, 0x8B, 0xB9, 0x40, 0xFE,
|
||||
0x99, 0x0E, 0x9D, 0xC9, 0x7E, 0xDC, 0xF4, 0x9D, 0xDB, 0x18, 0x09, 0x2C, 0x28, 0x20, 0x6E, 0x74,
|
||||
0x67, 0x53, 0xCC, 0x7C, 0x6E, 0x92, 0x36, 0x2A, 0xA8, 0xD5, 0x46, 0xB3, 0x8D, 0x9E, 0x8D, 0x43,
|
||||
0x11, 0xA6, 0xB1, 0x93, 0x0D, 0xA1, 0x48, 0x97, 0x80, 0x7E, 0x30, 0x4B, 0x5E, 0x1E, 0xC0, 0x85,
|
||||
0x6E, 0xEF, 0xD6, 0x2C, 0xEA, 0xEE, 0xF2, 0x8B, 0x08, 0xBD, 0x80, 0x39, 0x7A, 0x18, 0x15, 0x60,
|
||||
0xAE, 0x6F, 0xCE, 0x39, 0xD0, 0x9C, 0x39, 0xDC, 0x3D, 0xED, 0x8C, 0x87, 0x0A, 0xB6, 0xAB, 0xCE,
|
||||
0x28, 0x94, 0x94, 0x0C, 0x0E, 0x9C, 0x41, 0x74, 0xF0, 0x13, 0x1A, 0x0D, 0xA0, 0x74, 0x7C, 0x4A,
|
||||
0x7A, 0x42, 0xC9, 0xEC, 0x34, 0x87, 0xF1, 0x09, 0xE2, 0x52, 0xB7, 0xA9, 0xB8, 0x65, 0xAE, 0x47,
|
||||
0x78, 0x95, 0xE8, 0xD6, 0xA4, 0x2A, 0x07, 0x17, 0xC4, 0x0B, 0xCC, 0xC7, 0xA7, 0x35, 0xF3, 0x3B,
|
||||
0x1E, 0x37, 0x66, 0xAB, 0x0E, 0x4B, 0x5D, 0x68, 0x1B, 0xAB, 0x41, 0x07, 0x34, 0xAB, 0x62, 0xB0
|
||||
};
|
||||
|
@ -1,9 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
extern const u8 sig_nand_firm_retail[256];
|
||||
extern const u8 sig_nand_firm_retail_alt[256];
|
||||
extern const u8 sig_nand_firm_dev[256];
|
||||
extern const u8 sig_nand_ncsd_retail[256];
|
||||
extern const u8 sig_nand_ncsd_dev[256];
|
@ -1,388 +0,0 @@
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "language.h"
|
||||
#include "swkbd.h"
|
||||
#include "timer.h"
|
||||
#include "hid.h"
|
||||
#include "utf.h"
|
||||
|
||||
|
||||
static inline char to_uppercase(char c) {
|
||||
if ((c >= 'a') && (c <= 'z')) c += ('A'-'a');
|
||||
return c;
|
||||
}
|
||||
|
||||
static bool BuildKeyboard(TouchBox* swkbd, const char* keys, const u8* layout) {
|
||||
// count # of rows
|
||||
u32 n_rows = 0;
|
||||
for (u32 i = 0;; i++) {
|
||||
if (layout[i] == 0) {
|
||||
n_rows++;
|
||||
if (layout[i+1] == 0) break;
|
||||
}
|
||||
}
|
||||
|
||||
// set p_y start position
|
||||
u32 height = (n_rows) ? (n_rows * SWKBD_STDKEY_HEIGHT) + ((n_rows-1) * SWKDB_KEY_SPACING) : 0;
|
||||
u32 p_y = SCREEN_HEIGHT - height - SWKBD_STDKEY_HEIGHT - SWKDB_KEY_SPACING;
|
||||
|
||||
// set up the textbox
|
||||
TouchBox* txtbox = swkbd;
|
||||
txtbox->x = (SCREEN_WIDTH_BOT - SWKBD_TEXTBOX_WIDTH) / 2;
|
||||
txtbox->y = p_y - 30;
|
||||
txtbox->w = SWKBD_TEXTBOX_WIDTH;
|
||||
txtbox->h = 30;
|
||||
txtbox->id = KEY_TXTBOX;
|
||||
|
||||
// set button positions
|
||||
TouchBox* tb = swkbd + 1;
|
||||
for (u32 l = 0, k = 0; layout[l] != 0; ) {
|
||||
// calculate width of current row
|
||||
u32 n_keys = layout[l++];
|
||||
u32 width = (n_keys * SWKBD_STDKEY_WIDTH) + ((n_keys-1) * SWKDB_KEY_SPACING);
|
||||
for (u32 i = 0; layout[l+i] != 0; i++)
|
||||
width = width - SWKBD_STDKEY_WIDTH + layout[l+i];
|
||||
|
||||
// set p_x start position
|
||||
if (width > SCREEN_WIDTH_BOT) return false;
|
||||
u32 p_x = (SCREEN_WIDTH_BOT - width) / 2;
|
||||
|
||||
// set up touchboxes
|
||||
for (u32 i = 0; i < n_keys; i++) {
|
||||
tb->id = keys[k++];
|
||||
tb->x = p_x;
|
||||
tb->y = p_y;
|
||||
tb->w = ((tb->id >= 0x80) || (tb->id == (u32) ' ')) ? layout[l++] : SWKBD_STDKEY_WIDTH;
|
||||
tb->h = SWKBD_STDKEY_HEIGHT;
|
||||
|
||||
p_x += tb->w + SWKDB_KEY_SPACING;
|
||||
tb++;
|
||||
}
|
||||
|
||||
// next row
|
||||
if (layout[l++] != 0) return false;
|
||||
p_y += SWKBD_STDKEY_HEIGHT + SWKDB_KEY_SPACING;
|
||||
}
|
||||
|
||||
// set last touchbox zero (so the end can be detected)
|
||||
memset(tb, 0, sizeof(TouchBox));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void DrawKey(const TouchBox* key, const bool pressed, const u32 uppercase) {
|
||||
const char* keystrs[] = { SWKBD_KEYSTR };
|
||||
const u32 color = (pressed) ? COLOR_SWKBD_PRESSED :
|
||||
(key->id == KEY_ENTER) ? COLOR_SWKBD_ENTER :
|
||||
((key->id == KEY_CAPS) && (uppercase > 1)) ? COLOR_SWKBD_CAPS :
|
||||
COLOR_SWKBD_NORMAL;
|
||||
|
||||
// don't even try to draw the textbox
|
||||
if (key->id == KEY_TXTBOX) return;
|
||||
|
||||
char keystr[16];
|
||||
if (key->id >= 0x80) snprintf(keystr, sizeof(keystr), "%s", keystrs[key->id - 0x80]);
|
||||
else {
|
||||
keystr[0] = (uppercase) ? to_uppercase(key->id) : key->id;
|
||||
keystr[1] = 0;
|
||||
}
|
||||
|
||||
const u32 width = GetDrawStringWidth(keystr);
|
||||
const u32 f_offs_x = (key->w - width) / 2;
|
||||
const u32 f_offs_y = (key->h - FONT_HEIGHT_EXT) / 2;
|
||||
|
||||
DrawRectangle(BOT_SCREEN, key->x, key->y, key->w, key->h, color);
|
||||
DrawString(BOT_SCREEN, keystr, key->x + f_offs_x, key->y + f_offs_y, COLOR_SWKBD_CHARS, color);
|
||||
}
|
||||
|
||||
static void DrawKeyBoardBox(TouchBox* swkbd, u32 color) {
|
||||
// we need to make sure to skip the textbox here(first entry)
|
||||
|
||||
// calculate / draw keyboard box
|
||||
u16 x0 = SCREEN_WIDTH_BOT, x1 = 0;
|
||||
u16 y0 = SCREEN_HEIGHT, y1 = 0;
|
||||
for (TouchBox* tb = swkbd + 1; tb->id != 0; tb++) {
|
||||
if (tb->x < x0) x0 = tb->x;
|
||||
if (tb->y < y0) y0 = tb->y;
|
||||
if ((tb->x + tb->w) > x1) x1 = tb->x + tb->w;
|
||||
if ((tb->y + tb->h) > y1) y1 = tb->y + tb->h;
|
||||
}
|
||||
DrawRectangle(BOT_SCREEN, 0, y0-1, SCREEN_WIDTH_BOT, y1-y0+2, COLOR_STD_BG);
|
||||
DrawRectangle(BOT_SCREEN, x0-1, y0-1, x1-x0+2, y1-y0+2, color);
|
||||
}
|
||||
|
||||
static void DrawKeyBoard(TouchBox* swkbd, const u32 uppercase) {
|
||||
// we need to make sure to skip the textbox here(first entry)
|
||||
|
||||
// draw keyboard
|
||||
for (TouchBox* tb = swkbd + 1; tb->id != 0; tb++) {
|
||||
DrawKey(tb, false, uppercase);
|
||||
}
|
||||
}
|
||||
|
||||
static void DrawTextBox(const TouchBox* txtbox, const char* inputstr, const u32 cursor, u32* scroll) {
|
||||
const u32 input_shown_length = (txtbox->w / FONT_WIDTH_EXT) - 2;
|
||||
const u32 inputstr_size = strlen(inputstr); // we rely on a zero terminated string
|
||||
const u16 x = txtbox->x;
|
||||
const u16 y = txtbox->y;
|
||||
|
||||
// fix scroll
|
||||
if (cursor < *scroll) {
|
||||
*scroll = cursor;
|
||||
} else {
|
||||
int scroll_adjust = -input_shown_length;
|
||||
for (u32 i = *scroll; i < cursor; i++) {
|
||||
if (i >= inputstr_size || (inputstr[i] & 0xC0) != 0x80) scroll_adjust++;
|
||||
}
|
||||
|
||||
for (int i = 0; i < scroll_adjust; i++)
|
||||
*scroll += *scroll >= inputstr_size ? 1 : GetCharSize(inputstr + *scroll);
|
||||
}
|
||||
|
||||
u32 input_shown_size = 0;
|
||||
for (u32 i = 0; i < input_shown_length || (*scroll + input_shown_size < inputstr_size && (inputstr[*scroll + input_shown_size] & 0xC0) == 0x80); input_shown_size++) {
|
||||
if (*scroll + input_shown_size >= inputstr_size || (inputstr[*scroll + input_shown_size] & 0xC0) != 0x80) i++;
|
||||
}
|
||||
|
||||
// draw input string
|
||||
DrawStringF(BOT_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%c%-*.*s%c",
|
||||
(*scroll) ? '<' : '|',
|
||||
(int) input_shown_size,
|
||||
(int) input_shown_size,
|
||||
(*scroll > inputstr_size) ? "" : inputstr + *scroll,
|
||||
(inputstr_size - (s32) *scroll > input_shown_size) ? '>' : '|'
|
||||
);
|
||||
|
||||
// draw cursor
|
||||
u16 cpos = 0;
|
||||
for (u16 i = *scroll; i < cursor; i++) {
|
||||
if (i >= inputstr_size || (inputstr[i] & 0xC0) != 0x80) cpos++;
|
||||
}
|
||||
|
||||
DrawStringF(BOT_SCREEN, x-(FONT_WIDTH_EXT/2), y+10, COLOR_STD_FONT, COLOR_STD_BG, "%-*.*s^%-*.*s",
|
||||
(int) (1 + cpos),
|
||||
(int) (1 + cpos),
|
||||
"",
|
||||
(int) (input_shown_length - cpos),
|
||||
(int) (input_shown_length - cpos),
|
||||
""
|
||||
);
|
||||
}
|
||||
|
||||
static void MoveTextBoxCursor(const TouchBox* txtbox, const char* inputstr, const u32 max_size, u32* cursor, u32* scroll) {
|
||||
const u32 input_shown = (txtbox->w / FONT_WIDTH_EXT) - 2;
|
||||
const u64 scroll_cooldown = 144;
|
||||
u64 timer = timer_start();
|
||||
u32 id = 0;
|
||||
u16 x, y;
|
||||
|
||||
// process touch input
|
||||
while(HID_ReadTouchState(&x, &y)) {
|
||||
const TouchBox* tb = TouchBoxGet(&id, x, y, txtbox, 0);
|
||||
if (id == KEY_TXTBOX) {
|
||||
u16 x_tb = x - tb->x;
|
||||
|
||||
const u32 inputstr_size = strlen(inputstr);
|
||||
const u16 cpos_x = (x_tb < (FONT_WIDTH_EXT/2)) ? 0 : (x_tb - (FONT_WIDTH_EXT/2)) / FONT_WIDTH_EXT;
|
||||
u16 cpos_length = 0;
|
||||
u16 cpos_size = 0;
|
||||
while ((cpos_length < cpos_x && cpos_length < input_shown) || (*scroll + cpos_size < inputstr_size && (inputstr[*scroll + cpos_size] & 0xC0) == 0x80)) {
|
||||
if (*scroll + cpos_size >= inputstr_size || (inputstr[*scroll + cpos_size] & 0xC0) != 0x80) cpos_length++;
|
||||
cpos_size++;
|
||||
}
|
||||
|
||||
u32 cursor_next = *scroll + cpos_size;
|
||||
// move cursor to position pointed to
|
||||
if (*cursor != cursor_next) {
|
||||
if (cursor_next < max_size) *cursor = cursor_next;
|
||||
DrawTextBox(txtbox, inputstr, *cursor, scroll);
|
||||
timer = timer_start();
|
||||
}
|
||||
// move beyound visible field
|
||||
if (timer_msec(timer) >= scroll_cooldown) {
|
||||
if ((cpos_length == 0) && (*scroll > 0))
|
||||
*scroll -= GetPrevCharSize(inputstr + *scroll);
|
||||
else if ((cpos_length >= input_shown) && (*cursor < (max_size-1)))
|
||||
*scroll += GetCharSize(inputstr + *scroll);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static char KeyboardWait(TouchBox* swkbd, bool uppercase) {
|
||||
u32 id = 0;
|
||||
u16 x, y;
|
||||
|
||||
// wait for touch input (handle key input, too)
|
||||
while (true) {
|
||||
u32 pressed = InputWait(0);
|
||||
if (pressed & BUTTON_B) return KEY_ESCAPE;
|
||||
else if (pressed & BUTTON_A) return KEY_ENTER;
|
||||
else if (pressed & BUTTON_X) return KEY_BKSPC;
|
||||
else if (pressed & BUTTON_Y) return KEY_INSERT;
|
||||
else if (pressed & BUTTON_R1) return KEY_CAPS;
|
||||
else if (pressed & BUTTON_RIGHT) return KEY_RIGHT;
|
||||
else if (pressed & BUTTON_LEFT) return KEY_LEFT;
|
||||
else if (pressed & BUTTON_SELECT) return KEY_SWITCH;
|
||||
else if (pressed & BUTTON_TOUCH) break;
|
||||
}
|
||||
|
||||
// process touch input
|
||||
while(HID_ReadTouchState(&x, &y)) {
|
||||
const TouchBox* tb = TouchBoxGet(&id, x, y, swkbd, 0);
|
||||
if (tb) {
|
||||
if (id == KEY_TXTBOX) break; // immediately break on textbox
|
||||
DrawKey(tb, true, uppercase);
|
||||
while(HID_ReadTouchState(&x, &y) && (tb == TouchBoxGet(NULL, x, y, swkbd, 0)));
|
||||
DrawKey(tb, false, uppercase);
|
||||
}
|
||||
}
|
||||
|
||||
return (uppercase) ? to_uppercase((char) id) : (char) id;
|
||||
}
|
||||
|
||||
bool ShowKeyboard(char* inputstr, const u32 max_size, const char *format, ...) {
|
||||
static const char keys_alphabet[] = { SWKBD_KEYS_ALPHABET };
|
||||
static const char keys_special[] = { SWKBD_KEYS_SPECIAL };
|
||||
static const char keys_numpad[] = { SWKBD_KEYS_NUMPAD };
|
||||
static const u8 layout_alphabet[] = { SWKBD_LAYOUT_ALPHABET };
|
||||
static const u8 layout_special[] = { SWKBD_LAYOUT_SPECIAL };
|
||||
static const u8 layout_numpad[] = { SWKBD_LAYOUT_NUMPAD };
|
||||
TouchBox swkbd_alphabet[64];
|
||||
TouchBox swkbd_special[32];
|
||||
TouchBox swkbd_numpad[32];
|
||||
TouchBox* textbox = swkbd_alphabet; // always use this textbox
|
||||
|
||||
static bool show_instr = true;
|
||||
const char* instr = STR_KEYBOARD_CONTROLS_DETAILS;
|
||||
if (show_instr) {
|
||||
ShowPrompt(false, "%s", instr);
|
||||
show_instr = false;
|
||||
}
|
||||
|
||||
// generate keyboards
|
||||
if (!BuildKeyboard(swkbd_alphabet, keys_alphabet, layout_alphabet)) return false;
|
||||
if (!BuildKeyboard(swkbd_special, keys_special, layout_special)) return false;
|
||||
if (!BuildKeyboard(swkbd_numpad, keys_numpad, layout_numpad)) return false;
|
||||
|
||||
// (instructional) text
|
||||
char str[512]; // arbitrary limit, should be more than enough
|
||||
va_list va;
|
||||
va_start(va, format);
|
||||
vsnprintf(str, sizeof(str), format, va);
|
||||
va_end(va);
|
||||
u32 str_width = GetDrawStringWidth(str);
|
||||
if (str_width < (24 * FONT_WIDTH_EXT)) str_width = 24 * FONT_WIDTH_EXT;
|
||||
u32 str_x = (str_width >= SCREEN_WIDTH_BOT) ? 0 : (SCREEN_WIDTH_BOT - str_width) / 2;
|
||||
ClearScreen(BOT_SCREEN, COLOR_STD_BG);
|
||||
DrawStringF(BOT_SCREEN, str_x, 20, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
|
||||
|
||||
// wait for all keys released
|
||||
while (HID_ReadState() & BUTTON_ANY);
|
||||
|
||||
// handle keyboard
|
||||
u32 uppercase = 0; // 1 -> uppercase once, 2 -> uppercase always
|
||||
u32 scroll = 0;
|
||||
u32 cursor = 0;
|
||||
u32 inputstr_size = strnlen(inputstr, max_size);
|
||||
TouchBox* swkbd_prev = NULL;
|
||||
TouchBox* swkbd = swkbd_alphabet;
|
||||
bool ret = false;
|
||||
while (true) {
|
||||
// draw keyboard if required
|
||||
if (swkbd != swkbd_prev) {
|
||||
DrawKeyBoardBox(swkbd, COLOR_SWKBD_BOX);
|
||||
DrawKeyBoard(swkbd, uppercase);
|
||||
DrawTextBox(textbox, inputstr, cursor, &scroll);
|
||||
swkbd_prev = swkbd;
|
||||
}
|
||||
|
||||
// handle user input
|
||||
char key = KeyboardWait(swkbd, uppercase);
|
||||
if (key == KEY_INSERT) key = ' '; // impromptu replacement
|
||||
if (key == KEY_TXTBOX) {
|
||||
MoveTextBoxCursor(textbox, inputstr, max_size, &cursor, &scroll);
|
||||
} else if (key == KEY_CAPS) {
|
||||
uppercase = (uppercase + 1) % 3;
|
||||
DrawKeyBoard(swkbd, uppercase);
|
||||
continue;
|
||||
} else if (key == KEY_ENTER) {
|
||||
ret = true;
|
||||
break;
|
||||
} else if (key == KEY_ESCAPE) {
|
||||
break;
|
||||
} else if (key == KEY_BKSPC) {
|
||||
if (cursor) {
|
||||
int size = GetPrevCharSize(inputstr + cursor);
|
||||
if (cursor <= inputstr_size) {
|
||||
memmove(inputstr + cursor - size, inputstr + cursor, inputstr_size - cursor + size);
|
||||
inputstr_size -= size;
|
||||
}
|
||||
cursor -= size;
|
||||
}
|
||||
} else if (key == KEY_LEFT) {
|
||||
if (cursor) cursor -= GetPrevCharSize(inputstr + cursor);
|
||||
} else if (key == KEY_RIGHT) {
|
||||
int size = cursor > inputstr_size ? 1 : GetCharSize(inputstr + cursor);
|
||||
if (cursor + size < max_size) cursor += size;
|
||||
} else if (key == KEY_ALPHA) {
|
||||
swkbd = swkbd_alphabet;
|
||||
} else if (key == KEY_SPECIAL) {
|
||||
swkbd = swkbd_special;
|
||||
} else if (key == KEY_NUMPAD) {
|
||||
swkbd = swkbd_numpad;
|
||||
} else if (key == KEY_SWITCH) {
|
||||
ClearScreen(BOT_SCREEN, COLOR_STD_BG);
|
||||
return ShowStringPrompt(inputstr, max_size, "%s", str);
|
||||
} else if (key == KEY_UNICODE) {
|
||||
if (cursor > 3 && cursor <= inputstr_size) {
|
||||
u16 codepoint = 0;
|
||||
for (char *c = inputstr + cursor - 4; c < inputstr + cursor; c++) {
|
||||
if ((*c >= '0' && *c <= '9') || (*c >= 'A' && *c <= 'F') || (*c >= 'a' && *c <= 'f')) {
|
||||
codepoint <<= 4;
|
||||
codepoint |= *c - (*c <= '9' ? '0' : ((*c <= 'F' ? 'A' : 'a') - 10));
|
||||
} else {
|
||||
codepoint = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(codepoint != 0) {
|
||||
char character[5] = {0};
|
||||
u16 input[2] = {codepoint, 0};
|
||||
utf16_to_utf8((u8*)character, input, 4, 1);
|
||||
|
||||
u32 char_size = GetCharSize(character);
|
||||
memmove(inputstr + cursor - 4 + char_size, inputstr + cursor, max_size - cursor + 4 - char_size);
|
||||
memcpy(inputstr + cursor - 4, character, char_size);
|
||||
cursor -= 4 - char_size;
|
||||
}
|
||||
}
|
||||
} else if (key && (key < 0x80)) {
|
||||
if ((cursor < (max_size-1)) && (inputstr_size < max_size)) {
|
||||
// pad string (if cursor beyound string size)
|
||||
while (inputstr_size < cursor) {
|
||||
inputstr[inputstr_size++] = ' ';
|
||||
inputstr[inputstr_size] = '\0';
|
||||
}
|
||||
// make room
|
||||
if (inputstr_size < (max_size-1)) { // only if there is still room
|
||||
memmove(inputstr + cursor + 1, inputstr + cursor, max_size - cursor - 1);
|
||||
inputstr_size++;
|
||||
}
|
||||
// insert char
|
||||
inputstr[cursor++] = key;
|
||||
}
|
||||
if (uppercase == 1) {
|
||||
uppercase = 0;
|
||||
DrawKeyBoard(swkbd, uppercase);
|
||||
}
|
||||
}
|
||||
|
||||
// update text
|
||||
DrawTextBox(textbox, inputstr, cursor, &scroll);
|
||||
}
|
||||
|
||||
ClearScreen(BOT_SCREEN, COLOR_STD_BG);
|
||||
return ret;
|
||||
}
|
@ -1,89 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
#include "ui.h"
|
||||
#include "touchcal.h"
|
||||
|
||||
|
||||
// special key ids
|
||||
enum {
|
||||
KEY_DUMMY = 0x80,
|
||||
KEY_BKSPC = 0x81,
|
||||
KEY_INSERT = 0x82,
|
||||
KEY_ENTER = 0x83,
|
||||
KEY_CAPS = 0x84,
|
||||
KEY_SPECIAL = 0x85,
|
||||
KEY_NUMPAD = 0x86,
|
||||
KEY_ALPHA = 0x87,
|
||||
KEY_LEFT = 0x88,
|
||||
KEY_RIGHT = 0x89,
|
||||
KEY_ESCAPE = 0x8A,
|
||||
KEY_SWITCH = 0x8B,
|
||||
KEY_UNICODE = 0x8C,
|
||||
KEY_TXTBOX = 0xFF
|
||||
};
|
||||
|
||||
// special key strings
|
||||
#define SWKBD_KEYSTR "", "DEL", "INS", "SUBMIT", "CAPS", "#$@", "123", "ABC", "←", "→", "ESC", "SWITCH", "U+"
|
||||
|
||||
#define COLOR_SWKBD_NORMAL COLOR_GREY
|
||||
#define COLOR_SWKBD_PRESSED COLOR_LIGHTGREY
|
||||
#define COLOR_SWKBD_BOX COLOR_DARKGREY
|
||||
#define COLOR_SWKBD_CHARS COLOR_BLACK
|
||||
#define COLOR_SWKBD_ENTER COLOR_TINTEDBLUE
|
||||
#define COLOR_SWKBD_CAPS COLOR_TINTEDYELLOW
|
||||
|
||||
#define SWKBD_TEXTBOX_WIDTH 240
|
||||
#define SWKBD_STDKEY_WIDTH 18
|
||||
#define SWKBD_STDKEY_HEIGHT 20
|
||||
#define SWKDB_KEY_SPACING 1
|
||||
|
||||
#define SWKBD_KEYS_ALPHABET \
|
||||
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '+', '-', KEY_BKSPC, \
|
||||
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '&', KEY_ENTER, \
|
||||
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', '(', ')', '[', ']', \
|
||||
'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '_', '#', '!', \
|
||||
KEY_CAPS, ' ', KEY_NUMPAD, KEY_SPECIAL, KEY_LEFT, KEY_RIGHT
|
||||
|
||||
#define SWKBD_KEYS_SPECIAL \
|
||||
'(', ')', '{', '}', '[', ']', \
|
||||
'.', ',', '?', '!', '`', '\'', \
|
||||
'^', '*', '+', '-', '_', '=', \
|
||||
'@', '#', '$', '%', '&', '~', \
|
||||
KEY_ALPHA, ' ', KEY_BKSPC
|
||||
|
||||
#define SWKBD_KEYS_NUMPAD \
|
||||
'7', '8', '9', 'F', 'E', \
|
||||
'4', '5', '6', 'D', 'C', \
|
||||
'3', '2', '1', 'B', 'A', \
|
||||
'0', '.', '_', KEY_LEFT, KEY_RIGHT, \
|
||||
KEY_ALPHA, KEY_UNICODE, ' ', KEY_BKSPC
|
||||
|
||||
// offset, num of keys in row, width of special keys (...), 0
|
||||
#define SWKBD_LAYOUT_ALPHABET \
|
||||
13, 32, 0, \
|
||||
12, 51, 0, \
|
||||
13, 0, \
|
||||
12, 0, \
|
||||
6, 32, 123, 32, 32, 18, 18, 0, \
|
||||
0
|
||||
|
||||
#define SWKBD_LAYOUT_SPECIAL \
|
||||
6, 0, \
|
||||
6, 0, \
|
||||
6, 0, \
|
||||
6, 0, \
|
||||
3, 32, 46, 32, 0, \
|
||||
0
|
||||
|
||||
#define SWKBD_LAYOUT_NUMPAD \
|
||||
5, 0, \
|
||||
5, 0, \
|
||||
5, 0, \
|
||||
5, 18, 18, 0, \
|
||||
4, 20, 20, 31, 20, 0, \
|
||||
0
|
||||
|
||||
|
||||
#define ShowKeyboardOrPrompt (TouchIsCalibrated() ? ShowKeyboard : ShowStringPrompt)
|
||||
bool PRINTF_ARGS(3) ShowKeyboard(char* inputstr, u32 max_size, const char *format, ...);
|
@ -1,141 +0,0 @@
|
||||
#include "touchcal.h"
|
||||
#include "ui.h"
|
||||
#include "hid.h"
|
||||
#include "crc16.h"
|
||||
#include "language.h"
|
||||
#include "spiflash.h"
|
||||
#include "support.h"
|
||||
|
||||
|
||||
#define TOUCH_CALIB_FILENAME "gm9calib.bin"
|
||||
|
||||
|
||||
static const HID_CalibrationData default_calib = {
|
||||
.screen_x = 0,
|
||||
.screen_y = 0,
|
||||
.ts_raw = 0
|
||||
// ^ wrong: in my console it's 0x780086
|
||||
// but this is very much console dependent
|
||||
// so it's better to go with a sane default
|
||||
};
|
||||
|
||||
static bool is_calibrated = false;
|
||||
|
||||
|
||||
static bool SetCalibrationDefaults(void)
|
||||
{
|
||||
// Hardcoding this isn't ideal but it's better than
|
||||
// leaving the system without any state to work with
|
||||
is_calibrated = false; // no, this is not proper calibration
|
||||
return HID_SetCalibrationData(&default_calib, 1, SCREEN_WIDTH_BOT, SCREEN_HEIGHT);
|
||||
}
|
||||
|
||||
bool ShowTouchCalibrationDialog(void)
|
||||
{
|
||||
static const u32 dot_positions[][2] = {
|
||||
{16, 16},
|
||||
{SCREEN_WIDTH_BOT - 16, SCREEN_HEIGHT - 16},
|
||||
{16, SCREEN_HEIGHT - 16},
|
||||
{SCREEN_WIDTH_BOT - 16, 16},
|
||||
};
|
||||
|
||||
HID_CalibrationData calibrations[countof(dot_positions)];
|
||||
for (u32 i = 0; i < countof(dot_positions); i++) {
|
||||
calibrations[i].screen_x = dot_positions[i][0];
|
||||
calibrations[i].screen_y = dot_positions[i][1];
|
||||
}
|
||||
|
||||
// clear screen, draw instructions
|
||||
ClearScreen(BOT_SCREEN, COLOR_STD_BG);
|
||||
DrawStringCenter(BOT_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s",
|
||||
STR_TOUCH_CROSSHAIRS_TO_CALIBRATE_TOUCHSCREEN_USE_STYLUS);
|
||||
|
||||
// set calibration defaults
|
||||
SetCalibrationDefaults();
|
||||
|
||||
// actual calibration
|
||||
for (u32 current_dot = 0; current_dot < countof(dot_positions); current_dot++) {
|
||||
// draw four crosshairs
|
||||
for (u32 i = 0; i < countof(dot_positions); i++) {
|
||||
int color_cross = (i < current_dot) ? COLOR_BRIGHTGREEN : (i == current_dot) ? COLOR_RED : COLOR_STD_FONT;
|
||||
for (u32 r = 2; r < 8; r++) {
|
||||
DrawPixel(BOT_SCREEN, dot_positions[i][0] + 0, dot_positions[i][1] + r, color_cross);
|
||||
DrawPixel(BOT_SCREEN, dot_positions[i][0] + r, dot_positions[i][1] + 0, color_cross);
|
||||
DrawPixel(BOT_SCREEN, dot_positions[i][0] + 0, dot_positions[i][1] - r, color_cross);
|
||||
DrawPixel(BOT_SCREEN, dot_positions[i][0] - r, dot_positions[i][1] + 0, color_cross);
|
||||
}
|
||||
}
|
||||
|
||||
// wait for input, store calibration data
|
||||
while (1) {
|
||||
u32 pressed = InputWait(0);
|
||||
if (pressed & BUTTON_B) {
|
||||
return false;
|
||||
} else if (pressed & BUTTON_TOUCH) {
|
||||
calibrations[current_dot].ts_raw = HID_ReadRawTouchState();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
is_calibrated = HID_SetCalibrationData(calibrations, countof(dot_positions), SCREEN_WIDTH_BOT, SCREEN_HEIGHT);
|
||||
if (is_calibrated) { // store calibration data in a file
|
||||
SaveSupportFile(TOUCH_CALIB_FILENAME, calibrations, sizeof(calibrations));
|
||||
}
|
||||
return is_calibrated;
|
||||
}
|
||||
|
||||
bool CalibrateTouchFromSupportFile(void) {
|
||||
HID_CalibrationData calibrations[10];
|
||||
size_t size = LoadSupportFile(TOUCH_CALIB_FILENAME, calibrations, sizeof(calibrations));
|
||||
u32 n_dots = size / sizeof(HID_CalibrationData);
|
||||
|
||||
is_calibrated = (n_dots == 0) ? false :
|
||||
HID_SetCalibrationData(calibrations, n_dots, SCREEN_WIDTH_BOT, SCREEN_HEIGHT);
|
||||
return is_calibrated;
|
||||
}
|
||||
|
||||
bool CalibrateTouchFromFlash(void) {
|
||||
HID_CalibrationData data[2];
|
||||
|
||||
// set calibration defaults
|
||||
SetCalibrationDefaults();
|
||||
|
||||
// check SPIflash status
|
||||
if (!spiflash_get_status())
|
||||
return false;
|
||||
|
||||
// check NVRAM console ID
|
||||
u32 console_id = 0;
|
||||
if (!spiflash_read(0x001C, 4, (u8*)&console_id))
|
||||
return false;
|
||||
|
||||
if (((console_id >> 8) & 0xFF) != 0x57)
|
||||
return false; // not a 3DS
|
||||
|
||||
// read and check DS fw user settings
|
||||
// see: https://problemkaputt.de/gbatek.htm#dsfirmwareusersettings
|
||||
u32 fw_usercfg_buf[0x100 / 0x4];
|
||||
u8* fw_usercfg = (u8*) fw_usercfg_buf;
|
||||
if (!spiflash_read(0x1FE00, 0x100, fw_usercfg))
|
||||
return false;
|
||||
|
||||
if (getle16(fw_usercfg + 0x72) != crc16_quick(fw_usercfg, 0x70))
|
||||
return false;
|
||||
|
||||
// get touchscreen calibration data from user settings
|
||||
u8 *ts_data = fw_usercfg + 0x58;
|
||||
for (int i = 0; i < 2; i++) {
|
||||
int base = i * 6;
|
||||
data[i].ts_raw = ts_data[base + 1] << 24 | ts_data[base + 0] << 16 | ts_data[base + 3] << 8 | ts_data[base + 2];
|
||||
data[i].screen_x = (((int)ts_data[base + 4]) * SCREEN_WIDTH_BOT) / 256;
|
||||
data[i].screen_y = (((int)ts_data[base + 5]) * SCREEN_HEIGHT) / 192;
|
||||
}
|
||||
|
||||
is_calibrated = HID_SetCalibrationData(data, 2, SCREEN_WIDTH_BOT, SCREEN_HEIGHT);
|
||||
return is_calibrated;
|
||||
}
|
||||
|
||||
bool TouchIsCalibrated(void) {
|
||||
return is_calibrated;
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
bool ShowTouchCalibrationDialog(void);
|
||||
bool CalibrateTouchFromSupportFile(void);
|
||||
bool CalibrateTouchFromFlash(void);
|
||||
bool TouchIsCalibrated(void);
|
File diff suppressed because it is too large
Load Diff
@ -1,117 +0,0 @@
|
||||
// Copyright 2013 Normmatt
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vram.h>
|
||||
#include "common.h"
|
||||
#include "colors.h"
|
||||
#include "fsdir.h" // only required for ShowFileScrollPrompt
|
||||
|
||||
|
||||
#define BYTES_PER_PIXEL 2
|
||||
#define SCREEN_HEIGHT 240
|
||||
#define SCREEN_WIDTH(s) ((s == TOP_SCREEN) ? SCREEN_WIDTH_TOP : SCREEN_WIDTH_BOT)
|
||||
#define SCREEN_WIDTH_TOP 400
|
||||
#define SCREEN_WIDTH_BOT 320
|
||||
#define SCREEN_SIZE(s) ((s == TOP_SCREEN) ? SCREEN_SIZE_TOP : SCREEN_SIZE_BOT)
|
||||
#define SCREEN_SIZE_TOP (SCREEN_WIDTH_TOP * SCREEN_HEIGHT * BYTES_PER_PIXEL)
|
||||
#define SCREEN_SIZE_BOT (SCREEN_WIDTH_BOT * SCREEN_HEIGHT * BYTES_PER_PIXEL)
|
||||
#define FONT_WIDTH_EXT GetFontWidth()
|
||||
#define FONT_HEIGHT_EXT GetFontHeight()
|
||||
|
||||
#define UTF_MAX_BYTES_PER_RUNE 4
|
||||
#define UTF_BUFFER_BYTESIZE(rune_count) (((rune_count) + 1) * UTF_MAX_BYTES_PER_RUNE)
|
||||
|
||||
#define PRINTF_ARGS(n) __attribute__ ((format (printf, (n), (n) + 1)))
|
||||
|
||||
#define TOP_SCREEN ((u16*)VRAM_TOP_LA)
|
||||
#define BOT_SCREEN ((u16*)VRAM_BOT_A)
|
||||
|
||||
#ifdef SWITCH_SCREENS
|
||||
#define MAIN_SCREEN TOP_SCREEN
|
||||
#define ALT_SCREEN BOT_SCREEN
|
||||
#define SCREEN_WIDTH_MAIN SCREEN_WIDTH_TOP
|
||||
#define SCREEN_WIDTH_ALT SCREEN_WIDTH_BOT
|
||||
#else
|
||||
#define MAIN_SCREEN BOT_SCREEN
|
||||
#define ALT_SCREEN TOP_SCREEN
|
||||
#define SCREEN_WIDTH_MAIN SCREEN_WIDTH_BOT
|
||||
#define SCREEN_WIDTH_ALT SCREEN_WIDTH_TOP
|
||||
#endif
|
||||
|
||||
#define COLOR_TRANSPARENT COLOR_SUPERFUCHSIA
|
||||
|
||||
|
||||
#ifndef AUTO_UNLOCK
|
||||
bool PRINTF_ARGS(2) ShowUnlockSequence(u32 seqlvl, const char *format, ...);
|
||||
#else
|
||||
#define ShowUnlockSequence ShowPrompt
|
||||
#endif
|
||||
|
||||
const u8* GetFontFromPbm(const void* pbm, const u32 riff_size, u32* w, u32* h);
|
||||
const u8* GetFontFromRiff(const void* riff, const u32 riff_size, u32* w, u32* h, u16* count);
|
||||
bool SetFont(const void* font, const u32 font_size);
|
||||
|
||||
u16 GetColor(const u16 *screen, int x, int y);
|
||||
|
||||
void ClearScreen(u16 *screen, u32 color);
|
||||
void ClearScreenF(bool clear_main, bool clear_alt, u32 color);
|
||||
void DrawPixel(u16 *screen, int x, int y, u32 color);
|
||||
void DrawRectangle(u16 *screen, int x, int y, u32 width, u32 height, u32 color);
|
||||
void DrawBitmap(u16 *screen, int x, int y, u32 w, u32 h, const u16* bitmap);
|
||||
void DrawQrCode(u16 *screen, const u8* qrcode);
|
||||
|
||||
void DrawCharacter(u16 *screen, u32 character, int x, int y, u32 color, u32 bgcolor);
|
||||
void DrawString(u16 *screen, const char *str, int x, int y, u32 color, u32 bgcolor);
|
||||
void PRINTF_ARGS(6) DrawStringF(u16 *screen, int x, int y, u32 color, u32 bgcolor, const char *format, ...);
|
||||
void PRINTF_ARGS(4) DrawStringCenter(u16 *screen, u32 color, u32 bgcolor, const char *format, ...);
|
||||
|
||||
u32 GetCharSize(const char* str);
|
||||
u32 GetPrevCharSize(const char* str);
|
||||
|
||||
u32 GetDrawStringHeight(const char* str);
|
||||
u32 GetDrawStringWidth(const char* str);
|
||||
u32 GetFontWidth(void);
|
||||
u32 GetFontHeight(void);
|
||||
|
||||
void MultiLineString(char* dest, const char* orig, int llen, int maxl);
|
||||
void WordWrapString(char* str, int llen);
|
||||
void ResizeString(char* dest, const char* orig, int nlength, int tpos, bool align_right);
|
||||
void TruncateString(char* dest, const char* orig, int nlength, int tpos);
|
||||
void FormatNumber(char* str, u64 number);
|
||||
void FormatBytes(char* str, u64 bytes);
|
||||
|
||||
void PRINTF_ARGS(1) ShowString(const char *format, ...);
|
||||
void PRINTF_ARGS(2) ShowStringF(u16* screen, const char *format, ...);
|
||||
void PRINTF_ARGS(4) ShowIconString(u16* icon, int w, int h, const char *format, ...);
|
||||
void PRINTF_ARGS(5) ShowIconStringF(u16* screen, u16* icon, int w, int h, const char *format, ...);
|
||||
bool PRINTF_ARGS(2) ShowPrompt(bool ask, const char *format, ...);
|
||||
u32 PRINTF_ARGS(3) ShowSelectPrompt(int n, const char** options, const char *format, ...);
|
||||
u32 PRINTF_ARGS(4) ShowFileScrollPrompt(int n, const DirEntry** entries, bool hide_ext, const char *format, ...);
|
||||
u32 PRINTF_ARGS(4) ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *format, ...);
|
||||
bool PRINTF_ARGS(3) ShowStringPrompt(char* inputstr, u32 max_size, const char *format, ...);
|
||||
u64 PRINTF_ARGS(3) ShowHexPrompt(u64 start_val, u32 n_digits, const char *format, ...);
|
||||
u64 PRINTF_ARGS(2) ShowNumberPrompt(u64 start_val, const char *format, ...);
|
||||
bool PRINTF_ARGS(3) ShowDataPrompt(u8* data, u32* size, const char *format, ...);
|
||||
bool PRINTF_ARGS(2) ShowRtcSetterPrompt(void* time, const char *format, ...);
|
||||
bool ShowProgress(u64 current, u64 total, const char* opstr);
|
||||
|
||||
int ShowBrightnessConfig(int set_brightness);
|
||||
|
||||
static inline u16 rgb888_to_rgb565(u32 rgb) {
|
||||
u8 r, g, b;
|
||||
r = (rgb >> 16) & 0x1F;
|
||||
g = (rgb >> 8) & 0x3F;
|
||||
b = (rgb >> 0) & 0x1F;
|
||||
return (r << 11) | (g << 5) | b;
|
||||
}
|
||||
|
||||
static inline u16 rgb888_buf_to_rgb565(u8 *rgb) {
|
||||
u8 r, g, b;
|
||||
r = (rgb[0] >> 3);
|
||||
g = (rgb[1] >> 2);
|
||||
b = (rgb[2] >> 3);
|
||||
return (r << 11) | (g << 5) | b;
|
||||
}
|
@ -1,272 +0,0 @@
|
||||
#include "utf.h"
|
||||
|
||||
#define UTF_MAX_UNITS 256
|
||||
#define ASCII_UNKNOWN ((u8) '?')
|
||||
|
||||
// most of the code here shamelessly stolen from:
|
||||
// https://github.com/smealum/ctrulib/tree/bd34fd59dbf0691e2dba76be65f260303d8ccec7/libctru/source/util/utf
|
||||
|
||||
|
||||
int decode_utf8(u32 *out, const u8 *in)
|
||||
{
|
||||
u8 code1, code2, code3, code4;
|
||||
|
||||
code1 = *in++;
|
||||
if(code1 < 0x80)
|
||||
{
|
||||
/* 1-byte sequence */
|
||||
*out = code1;
|
||||
return 1;
|
||||
}
|
||||
else if(code1 < 0xC2)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if(code1 < 0xE0)
|
||||
{
|
||||
/* 2-byte sequence */
|
||||
code2 = *in++;
|
||||
if((code2 & 0xC0) != 0x80)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
*out = (code1 << 6) + code2 - 0x3080;
|
||||
return 2;
|
||||
}
|
||||
else if(code1 < 0xF0)
|
||||
{
|
||||
/* 3-byte sequence */
|
||||
code2 = *in++;
|
||||
if((code2 & 0xC0) != 0x80)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if(code1 == 0xE0 && code2 < 0xA0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
code3 = *in++;
|
||||
if((code3 & 0xC0) != 0x80)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
*out = (code1 << 12) + (code2 << 6) + code3 - 0xE2080;
|
||||
return 3;
|
||||
}
|
||||
else if(code1 < 0xF5)
|
||||
{
|
||||
/* 4-byte sequence */
|
||||
code2 = *in++;
|
||||
if((code2 & 0xC0) != 0x80)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if(code1 == 0xF0 && code2 < 0x90)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if(code1 == 0xF4 && code2 >= 0x90)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
code3 = *in++;
|
||||
if((code3 & 0xC0) != 0x80)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
code4 = *in++;
|
||||
if((code4 & 0xC0) != 0x80)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
*out = (code1 << 18) + (code2 << 12) + (code3 << 6) + code4 - 0x3C82080;
|
||||
return 4;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int decode_utf16(u32 *out, const u16 *in)
|
||||
{
|
||||
u16 code1, code2;
|
||||
|
||||
code1 = *in++;
|
||||
if(code1 >= 0xD800 && code1 < 0xDC00)
|
||||
{
|
||||
/* surrogate pair */
|
||||
code2 = *in++;
|
||||
if(code2 >= 0xDC00 && code2 < 0xE000)
|
||||
{
|
||||
*out = (code1 << 10) + code2 - 0x35FDC00;
|
||||
return 2;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
*out = code1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int encode_utf8(u8 *out, u32 in)
|
||||
{
|
||||
if(in < 0x80)
|
||||
{
|
||||
if(out != NULL)
|
||||
*out++ = in;
|
||||
return 1;
|
||||
}
|
||||
else if(in < 0x800)
|
||||
{
|
||||
if(out != NULL)
|
||||
{
|
||||
*out++ = (in >> 6) + 0xC0;
|
||||
*out++ = (in & 0x3F) + 0x80;
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
else if(in < 0x10000)
|
||||
{
|
||||
if(out != NULL)
|
||||
{
|
||||
*out++ = (in >> 12) + 0xE0;
|
||||
*out++ = ((in >> 6) & 0x3F) + 0x80;
|
||||
*out++ = (in & 0x3F) + 0x80;
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
else if(in < 0x110000)
|
||||
{
|
||||
if(out != NULL)
|
||||
{
|
||||
*out++ = (in >> 18) + 0xF0;
|
||||
*out++ = ((in >> 12) & 0x3F) + 0x80;
|
||||
*out++ = ((in >> 6) & 0x3F) + 0x80;
|
||||
*out++ = (in & 0x3F) + 0x80;
|
||||
}
|
||||
return 4;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int encode_utf16(u16 *out, u32 in)
|
||||
{
|
||||
if(in < 0x10000)
|
||||
{
|
||||
if(out != NULL)
|
||||
*out++ = in;
|
||||
return 1;
|
||||
}
|
||||
else if(in < 0x110000)
|
||||
{
|
||||
if(out != NULL)
|
||||
{
|
||||
*out++ = (in >> 10) + 0xD7C0;
|
||||
*out++ = (in & 0x3FF) + 0xDC00;
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int utf16_to_utf8(u8 *out, const u16 *in, int len_out, int len_in)
|
||||
{
|
||||
int rc = 0;
|
||||
int units;
|
||||
u32 code;
|
||||
u8 encoded[4];
|
||||
|
||||
do
|
||||
{
|
||||
units = decode_utf16(&code, in);
|
||||
if(units == -1)
|
||||
return -1;
|
||||
|
||||
if (len_in >= units)
|
||||
len_in -= units;
|
||||
else return -1;
|
||||
|
||||
if(code > 0)
|
||||
{
|
||||
in += units;
|
||||
|
||||
units = encode_utf8(encoded, code);
|
||||
if(units == -1)
|
||||
return -1;
|
||||
|
||||
if(out != NULL)
|
||||
{
|
||||
if(rc + units <= len_out)
|
||||
{
|
||||
*out++ = encoded[0];
|
||||
if(units > 1)
|
||||
*out++ = encoded[1];
|
||||
if(units > 2)
|
||||
*out++ = encoded[2];
|
||||
if(units > 3)
|
||||
*out++ = encoded[3];
|
||||
}
|
||||
}
|
||||
|
||||
if(UTF_MAX_UNITS - units >= rc)
|
||||
rc += units;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
} while(code > 0 && len_in > 0);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int utf8_to_utf16(u16 *out, const u8 *in, int len_out, int len_in)
|
||||
{
|
||||
int rc = 0;
|
||||
int units;
|
||||
u32 code;
|
||||
u16 encoded[2];
|
||||
|
||||
do
|
||||
{
|
||||
units = decode_utf8(&code, in);
|
||||
if(units == -1)
|
||||
return -1;
|
||||
|
||||
if (len_in >= units)
|
||||
len_in -= units;
|
||||
else return -1;
|
||||
|
||||
if(code > 0)
|
||||
{
|
||||
in += units;
|
||||
|
||||
units = encode_utf16(encoded, code);
|
||||
if(units == -1)
|
||||
return -1;
|
||||
|
||||
if(out != NULL)
|
||||
{
|
||||
if(rc + units <= len_out)
|
||||
{
|
||||
*out++ = encoded[0];
|
||||
if(units > 1)
|
||||
*out++ = encoded[1];
|
||||
}
|
||||
}
|
||||
|
||||
if(UTF_MAX_UNITS - units >= rc)
|
||||
rc += units;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
} while(code > 0 && len_in > 0);
|
||||
|
||||
return rc;
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
// most of the code here shamelessly stolen from:
|
||||
// https://github.com/smealum/ctrulib/tree/bd34fd59dbf0691e2dba76be65f260303d8ccec7/libctru/source/util/utf
|
||||
int utf16_to_utf8(u8 *out, const u16 *in, int len_out, int len_in);
|
||||
int utf8_to_utf16(u16 *out, const u8 *in, int len_out, int len_in);
|
@ -1,26 +0,0 @@
|
||||
#include "crc16.h"
|
||||
|
||||
#define CRC16_TABVAL \
|
||||
0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, \
|
||||
0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
|
||||
|
||||
|
||||
// see: https://github.com/TASVideos/desmume/blob/master/desmume/src/bios.cpp#L1070tions
|
||||
u16 crc16_quick(const void* src, u32 len) {
|
||||
static const u16 tabval[] = { CRC16_TABVAL };
|
||||
u16* data = (u16*) src;
|
||||
u16 crc = 0xFFFF;
|
||||
|
||||
for (len >>= 1; len; len--) {
|
||||
u16 curr = *(data++);
|
||||
for (u32 i = 0; i < 4; i++) {
|
||||
u16 tval = tabval[crc&0xF];
|
||||
crc >>= 4;
|
||||
crc ^= tval;
|
||||
tval = tabval[(curr >> (4*i))&0xF];
|
||||
crc ^= tval;
|
||||
}
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
u16 crc16_quick(const void* src, u32 len);
|
@ -1,90 +0,0 @@
|
||||
// C port of byuu's \nall\crc32.hpp, which was released under GPLv3
|
||||
// https://github.com/eai04191/beat/blob/master/nall/crc32.hpp
|
||||
// Ported by Hyarion for use with VirtualFatFS
|
||||
|
||||
#include "common.h"
|
||||
#include "crc32.h"
|
||||
#include "vff.h"
|
||||
|
||||
u32 crc32_adjust(u32 crc32, u8 input) {
|
||||
static const u32 crc32_table[256] = {
|
||||
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
|
||||
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
|
||||
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
|
||||
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
|
||||
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
|
||||
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
|
||||
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
|
||||
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
|
||||
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
|
||||
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
|
||||
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
|
||||
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
|
||||
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
|
||||
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
|
||||
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
|
||||
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
|
||||
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
|
||||
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
|
||||
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
|
||||
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
|
||||
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
|
||||
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
|
||||
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
|
||||
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
|
||||
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
|
||||
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
|
||||
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
|
||||
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
|
||||
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
|
||||
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
|
||||
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
|
||||
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
|
||||
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
|
||||
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
|
||||
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
|
||||
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
|
||||
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
|
||||
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
|
||||
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
|
||||
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
|
||||
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
|
||||
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
|
||||
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
|
||||
};
|
||||
return ((crc32 >> 8) & 0x00ffffff) ^ crc32_table[(crc32 ^ input) & 0xff];
|
||||
}
|
||||
|
||||
u32 crc32_calculate(u32 crc32, const u8* data, u32 length) {
|
||||
for(unsigned i = 0; i < length; i++) {
|
||||
crc32 = crc32_adjust(crc32, data[i]);
|
||||
}
|
||||
return crc32;
|
||||
}
|
||||
|
||||
u32 crc32_calculate_from_file(const char* fileName, u32 offset, u32 length) {
|
||||
FIL inputFile;
|
||||
u32 crc32 = ~0;
|
||||
u32 bufsiz = min(STD_BUFFER_SIZE, length);
|
||||
u8* buffer = (u8*) malloc(bufsiz);
|
||||
if (!buffer) return false;
|
||||
if (fvx_open(&inputFile, fileName, FA_READ) != FR_OK) {
|
||||
free(buffer);
|
||||
return crc32;
|
||||
}
|
||||
fvx_lseek(&inputFile, offset);
|
||||
|
||||
bool ret = true;
|
||||
for (u64 pos = 0; (pos < length) && ret; pos += bufsiz) {
|
||||
UINT read_bytes = min(bufsiz, length - pos);
|
||||
UINT bytes_read = read_bytes;
|
||||
if ((fvx_read(&inputFile, buffer, read_bytes, &bytes_read) != FR_OK) ||
|
||||
(read_bytes != bytes_read))
|
||||
ret = false;
|
||||
if (ret) crc32 = crc32_calculate(crc32, buffer, read_bytes);
|
||||
}
|
||||
|
||||
fvx_close(&inputFile);
|
||||
free(buffer);
|
||||
return ~crc32;
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
// C port of byuu's \nall\crc32.hpp, which was released under GPLv3
|
||||
// https://github.com/eai04191/beat/blob/master/nall/crc32.hpp
|
||||
// Ported by Hyarion for use with VirtualFatFS
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
u32 crc32_adjust(u32 crc32, u8 input);
|
||||
u32 crc32_calculate(u32 crc32, const u8* data, u32 length);
|
||||
u32 crc32_calculate_from_file(const char* fileName, u32 offset, u32 length);
|
@ -1,231 +0,0 @@
|
||||
#include "keydb.h"
|
||||
#include "aes.h"
|
||||
#include "sha.h"
|
||||
#include "vff.h"
|
||||
#include "support.h"
|
||||
|
||||
typedef struct {
|
||||
u8 slot; // keyslot, 0x00...0x39
|
||||
u8 keyUnitType; // 0 for ALL units / 1 for devkit exclusive / 2 for retail exclusive
|
||||
u8 sample[16]; // sample data, encoded with src = keyY = ctr = { 0 }
|
||||
} PACKED_STRUCT AesNcchSampleInfo;
|
||||
|
||||
static u64 keyState = 0;
|
||||
static u64 keyXState = 0;
|
||||
static u64 keyYState = 0;
|
||||
|
||||
u32 GetUnitKeysType(void)
|
||||
{
|
||||
static u32 keys_type = KEYS_UNKNOWN;
|
||||
|
||||
if (keys_type == KEYS_UNKNOWN) {
|
||||
static const u8 slot0x2CSampleRetail[16] = {
|
||||
0xBC, 0xC4, 0x16, 0x2C, 0x2A, 0x06, 0x91, 0xEE, 0x47, 0x18, 0x86, 0xB8, 0xEB, 0x2F, 0xB5, 0x48 };
|
||||
static const u8 slot0x2CSampleDevkit[16] = {
|
||||
0x29, 0xB5, 0x5D, 0x9F, 0x61, 0xAC, 0xD2, 0x28, 0x22, 0x23, 0xFB, 0x57, 0xDD, 0x50, 0x8A, 0xF5 };
|
||||
static u8 zeroes[16] = { 0 };
|
||||
u8 sample[16] = { 0 };
|
||||
setup_aeskeyY(0x2C, zeroes);
|
||||
use_aeskey(0x2C);
|
||||
set_ctr(zeroes);
|
||||
aes_decrypt(sample, sample, 1, AES_CNT_CTRNAND_MODE);
|
||||
if (memcmp(sample, slot0x2CSampleRetail, 16) == 0) {
|
||||
keys_type = KEYS_RETAIL;
|
||||
} else if (memcmp(sample, slot0x2CSampleDevkit, 16) == 0) {
|
||||
keys_type = KEYS_DEVKIT;
|
||||
}
|
||||
}
|
||||
|
||||
return keys_type;
|
||||
}
|
||||
|
||||
void CryptAesKeyInfo(AesKeyInfo* info) {
|
||||
static const u8 zeros[16] = { 0 };
|
||||
static const u8 keyY_dev[16] = {
|
||||
0xA2, 0x32, 0x4A, 0x7E, 0x7C, 0xE6, 0x1A, 0x9A, 0x45, 0x4A, 0x52, 0x19, 0xB3, 0x5B, 0xE9, 0x3B };
|
||||
const u8* aeskeyY = GetUnitKeysType() == KEYS_DEVKIT ? &keyY_dev[0] : &zeros[0];
|
||||
u8 ctr[16] = { 0 };
|
||||
memcpy(ctr, (void*) info, 12); // CTR -> slot + type + id + zeroes
|
||||
setup_aeskeyY(0x2C, aeskeyY);
|
||||
use_aeskey(0x2C);
|
||||
set_ctr(ctr);
|
||||
aes_decrypt(info->key, info->key, 1, AES_CNT_CTRNAND_MODE);
|
||||
info->isEncrypted = !info->isEncrypted;
|
||||
}
|
||||
|
||||
u32 CheckKeySlot(u32 keyslot, char type)
|
||||
{
|
||||
static const AesNcchSampleInfo keyNcchSamples[] = {
|
||||
{ 0x18, KEYS_RETAIL, // Retail NCCH Secure3
|
||||
{ 0x78, 0xBB, 0x84, 0xFA, 0xB3, 0xA2, 0x49, 0x83, 0x9E, 0x4F, 0x50, 0x7B, 0x17, 0xA0, 0xDA, 0x23 } },
|
||||
{ 0x1B, KEYS_RETAIL, // Retail NCCH Secure4
|
||||
{ 0xF3, 0x6F, 0x84, 0x7E, 0x59, 0x43, 0x6E, 0xD5, 0xA0, 0x40, 0x4C, 0x71, 0x19, 0xED, 0xF7, 0x0A } },
|
||||
{ 0x25, KEYS_RETAIL, // Retail NCCH 7x
|
||||
{ 0x34, 0x7D, 0x07, 0x48, 0xAE, 0x5D, 0xFB, 0xB0, 0xF5, 0x86, 0xD6, 0xB5, 0x14, 0x65, 0xF1, 0xFF } },
|
||||
{ 0x18, KEYS_DEVKIT, // DevKit NCCH Secure3
|
||||
{ 0x20, 0x8B, 0xB5, 0x61, 0x94, 0x18, 0x6A, 0x84, 0x91, 0xD6, 0x37, 0x27, 0x91, 0xF3, 0x53, 0xC9 } },
|
||||
{ 0x1B, KEYS_DEVKIT, // DevKit NCCH Secure4
|
||||
{ 0xB3, 0x9D, 0xC1, 0xDB, 0x5B, 0x0C, 0xE7, 0x60, 0xBE, 0xAD, 0x5A, 0xBF, 0xD0, 0x86, 0x99, 0x88 } },
|
||||
{ 0x25, KEYS_DEVKIT, // DevKit NCCH 7x
|
||||
{ 0xBC, 0x83, 0x7C, 0xC9, 0x99, 0xC8, 0x80, 0x9E, 0x8A, 0xDE, 0x4A, 0xFA, 0xAA, 0x72, 0x08, 0x28 } }
|
||||
};
|
||||
u64* state = (type == 'X') ? &keyXState : (type == 'Y') ? &keyYState : (type == 'N') ? &keyState : NULL;
|
||||
|
||||
// just to be safe...
|
||||
if (keyslot >= 0x40)
|
||||
return 1;
|
||||
|
||||
// check if key is already loaded
|
||||
if ((type != 'I') && ((*state >> keyslot) & 1))
|
||||
return 0;
|
||||
|
||||
// if is not, we may still be able to verify the currently set one (for NCCH keys)
|
||||
for (u32 p = 0; (type == 'X') && (p < sizeof(keyNcchSamples) / sizeof(AesNcchSampleInfo)); p++) {
|
||||
if (keyNcchSamples[p].slot != keyslot) // only for keyslots in the keyNcchSamples table!
|
||||
continue;
|
||||
if (keyNcchSamples[p].keyUnitType && (keyNcchSamples[p].keyUnitType != GetUnitKeysType()))
|
||||
continue;
|
||||
u8 zeroes[16] = { 0 };
|
||||
u8 sample[16] = { 0 };
|
||||
setup_aeskeyY(keyslot, zeroes);
|
||||
use_aeskey(keyslot);
|
||||
set_ctr(zeroes);
|
||||
aes_decrypt(sample, sample, 1, AES_CNT_CTRNAND_MODE);
|
||||
if (memcmp(keyNcchSamples[p].sample, sample, 16) == 0) {
|
||||
keyXState |= 1ull << keyslot;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// not set up if getting here
|
||||
return 1;
|
||||
}
|
||||
|
||||
// this function creates dependencies to "vff.h" and "support.h"
|
||||
// to get rid of these, you may replace this function with anything that works for you
|
||||
u32 LoadKeyDb(const char* path_db, AesKeyInfo* keydb, u32 bsize) {
|
||||
UINT fsize = 0;
|
||||
|
||||
if (path_db) {
|
||||
if (fvx_qread (path_db, keydb, 0, bsize, &fsize) != FR_OK) fsize = 0;
|
||||
} else if (fvx_qread ("S:/" KEYDB_NAME, keydb, 0, bsize, &fsize) != FR_OK) {
|
||||
fsize = LoadSupportFile(KEYDB_NAME, keydb, bsize); // load key database support file
|
||||
}
|
||||
|
||||
u32 nkeys = 0;
|
||||
if (fsize && !(fsize % sizeof(AesKeyInfo)))
|
||||
nkeys = fsize / sizeof(AesKeyInfo);
|
||||
return nkeys;
|
||||
}
|
||||
|
||||
u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id)
|
||||
{
|
||||
u8 keystore[16] __attribute__((aligned(32))) = {0};
|
||||
bool found = false;
|
||||
|
||||
// checking the obvious
|
||||
if ((keyslot >= 0x40) || ((type != 'X') && (type != 'Y') && (type != 'N') && (type != 'I')))
|
||||
return 1;
|
||||
|
||||
// check if already loaded
|
||||
if (!key && !id && (CheckKeySlot(keyslot, type) == 0)) return 0;
|
||||
|
||||
// use keystore if key == NULL
|
||||
if (!key) key = keystore;
|
||||
|
||||
// try to get key from 'aeskeydb.bin' file
|
||||
AesKeyInfo* keydb = (AesKeyInfo*) malloc(STD_BUFFER_SIZE);
|
||||
u32 nkeys = (keydb) ? LoadKeyDb(NULL, keydb, STD_BUFFER_SIZE) : 0;
|
||||
|
||||
for (u32 i = 0; i < nkeys; i++) {
|
||||
AesKeyInfo* info = &(keydb[i]);
|
||||
if (!((info->slot == keyslot) && (info->type == type) &&
|
||||
((!id && !(info->id[0])) || (id && (strncmp(id, info->id, 10) == 0))) &&
|
||||
(!info->keyUnitType || (info->keyUnitType == GetUnitKeysType()))))
|
||||
continue;
|
||||
found = true;
|
||||
if (info->isEncrypted)
|
||||
CryptAesKeyInfo(info);
|
||||
memcpy(key, info->key, 16);
|
||||
break;
|
||||
}
|
||||
|
||||
free(keydb);
|
||||
|
||||
// load legacy slot0x??Key?.bin file instead
|
||||
if (!found && (type != 'I')) {
|
||||
char fname[64];
|
||||
snprintf(fname, sizeof(fname), "slot0x%02lXKey%s%s.bin", keyslot,
|
||||
(type == 'X') ? "X" : (type == 'Y') ? "Y" : (type == 'I') ? "IV" : "", (id) ? id : "");
|
||||
found = (LoadSupportFile(fname, key, 16) == 16);
|
||||
}
|
||||
|
||||
// key still not found (duh)
|
||||
if (!found) return 1; // out of options here
|
||||
|
||||
// done if this is an IV
|
||||
if (type == 'I') return 0;
|
||||
|
||||
// now, setup the key
|
||||
if (type == 'X') {
|
||||
setup_aeskeyX(keyslot, key);
|
||||
keyXState |= 1ull << keyslot;
|
||||
} else if (type == 'Y') {
|
||||
setup_aeskeyY(keyslot, key);
|
||||
keyYState |= 1ull << keyslot;
|
||||
} else { // normalKey includes keyX & keyY
|
||||
setup_aeskey(keyslot, key);
|
||||
keyState |= 1ull << keyslot;
|
||||
keyXState |= 1ull << keyslot;
|
||||
keyYState |= 1ull << keyslot;
|
||||
}
|
||||
use_aeskey(keyslot);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 InitKeyDb(const char* path)
|
||||
{
|
||||
// use this to quickly initialize all applicable keys in aeskeydb.bin
|
||||
static const u64 keyslot_whitelist = (1ull<<0x02)|(1ull<<0x03)|(1ull<<0x05)|(1ull<<0x18)|(1ull<<0x19)|(1ull<<0x1A)|(1ull<<0x1B)|
|
||||
(1ull<<0x1C)|(1ull<<0x1D)|(1ull<<0x1E)|(1ull<<0x1F)|(1ull<<0x24)|(1ull<<0x25)|(1ull<<0x2F);
|
||||
|
||||
// try to load aeskeydb.bin file
|
||||
AesKeyInfo* keydb = (AesKeyInfo*) malloc(STD_BUFFER_SIZE);
|
||||
u32 nkeys = (keydb) ? LoadKeyDb(path, keydb, STD_BUFFER_SIZE) : 0;
|
||||
|
||||
// apply all applicable keys
|
||||
for (u32 i = 0; i < nkeys; i++) {
|
||||
AesKeyInfo* info = &(keydb[i]);
|
||||
if ((info->slot >= 0x40) || ((info->type != 'X') && (info->type != 'Y') && (info->type != 'N') && (info->type != 'I'))) {
|
||||
free(keydb);
|
||||
return 1; // looks faulty, better stop right here
|
||||
}
|
||||
if (!path && !((1ull<<info->slot)&keyslot_whitelist)) continue; // not in keyslot whitelist
|
||||
if ((info->type == 'I') || (*(info->id)) || (info->keyUnitType && (info->keyUnitType != GetUnitKeysType())) ||
|
||||
(CheckKeySlot(info->slot, info->type) == 0)) continue; // most likely valid, but not applicable or already set
|
||||
if (info->isEncrypted) CryptAesKeyInfo(info); // decrypt key
|
||||
|
||||
// apply key
|
||||
u8 key[16] __attribute__((aligned(32))) = {0};
|
||||
char type = info->type;
|
||||
u32 keyslot = info->slot;
|
||||
memcpy(key, info->key, 16);
|
||||
if (type == 'X') {
|
||||
setup_aeskeyX(keyslot, key);
|
||||
keyXState |= 1ull << keyslot;
|
||||
} else if (type == 'Y') {
|
||||
setup_aeskeyY(keyslot, key);
|
||||
keyYState |= 1ull << keyslot;
|
||||
} else { // normalKey includes keyX & keyY
|
||||
setup_aeskey(keyslot, key);
|
||||
keyState |= 1ull << keyslot;
|
||||
keyXState |= 1ull << keyslot;
|
||||
keyYState |= 1ull << keyslot;
|
||||
}
|
||||
use_aeskey(keyslot);
|
||||
}
|
||||
|
||||
free(keydb);
|
||||
return (nkeys) ? 0 : 1;
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#define KEYDB_NAME "aeskeydb.bin"
|
||||
|
||||
// SHA-256 and size of the recommended aeskeydb.bin file
|
||||
// equals MD5 A5B28945A7C051D7A0CD18AF0E580D1B / 1024 byte
|
||||
#define KEYDB_PERFECT_HASH \
|
||||
0x40, 0x76, 0x54, 0x3D, 0xA3, 0xFF, 0x91, 0x1C, 0xE1, 0xCC, 0x4E, 0xC7, 0x2F, 0x92, 0xE4, 0xB7, \
|
||||
0x2B, 0x24, 0x00, 0x15, 0xBE, 0x9B, 0xFC, 0xDE, 0x7F, 0xED, 0x95, 0x1D, 0xD5, 0xAB, 0x2D, 0xCB
|
||||
#define KEYDB_PERFECT_SIZE (32 * sizeof(AesKeyInfo)) // 32 keys contained
|
||||
|
||||
#define KEYS_UNKNOWN 0
|
||||
#define KEYS_DEVKIT 1
|
||||
#define KEYS_RETAIL 2
|
||||
|
||||
|
||||
typedef struct {
|
||||
u8 slot; // keyslot, 0x00...0x3F
|
||||
char type; // type 'X' / 'Y' / 'N' for normalKey / 'I' for IV
|
||||
char id[10]; // key ID for special keys, all zero for standard keys
|
||||
u8 reserved[2]; // reserved space
|
||||
u8 keyUnitType; // 0 for ALL units / 1 for devkit exclusive / 2 for retail exclusive
|
||||
u8 isEncrypted; // 0 if not / anything else if it is
|
||||
u8 key[16];
|
||||
} PACKED_STRUCT __attribute__((aligned(16))) AesKeyInfo;
|
||||
|
||||
u32 GetUnitKeysType(void);
|
||||
void CryptAesKeyInfo(AesKeyInfo* info);
|
||||
u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id);
|
||||
u32 InitKeyDb(const char* path);
|
@ -1,120 +0,0 @@
|
||||
/*
|
||||
* This file is part of fastboot 3DS
|
||||
* Copyright (C) 2017 derrek, profi200
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include "rsa.h"
|
||||
#include "sha.h"
|
||||
#include "mmio.h"
|
||||
|
||||
|
||||
|
||||
//////////////////////////////////
|
||||
// RSA //
|
||||
//////////////////////////////////
|
||||
|
||||
#define RSA_REGS_BASE (0x10000000 + 0xB000)
|
||||
#define REG_RSA_CNT *((vu32*)(RSA_REGS_BASE + 0x000))
|
||||
#define REG_RSA_UNK_F0 *((vu32*)(RSA_REGS_BASE + 0x0F0))
|
||||
#define REGs_RSA_SLOT0 ((vu32*)(RSA_REGS_BASE + 0x100))
|
||||
#define REGs_RSA_SLOT1 ((vu32*)(RSA_REGS_BASE + 0x110))
|
||||
#define REGs_RSA_SLOT2 ((vu32*)(RSA_REGS_BASE + 0x120))
|
||||
#define REGs_RSA_SLOT3 ((vu32*)(RSA_REGS_BASE + 0x130))
|
||||
#define rsaSlots ((RsaSlot*)(RSA_REGS_BASE + 0x100))
|
||||
#define REGs_RSA_EXP ((vu32*)(RSA_REGS_BASE + 0x200))
|
||||
#define REGs_RSA_MOD ((vu32*)(RSA_REGS_BASE + 0x400))
|
||||
#define REGs_RSA_TXT ((vu32*)(RSA_REGS_BASE + 0x800))
|
||||
|
||||
typedef struct
|
||||
{
|
||||
vu32 REG_RSA_SLOTCNT;
|
||||
vu32 REG_RSA_SLOTSIZE;
|
||||
vu32 REG_RSA_SLOT_UNK_0x8;
|
||||
vu32 REG_RSA_SLOT_UNK_0xC;
|
||||
} RsaSlot;
|
||||
|
||||
|
||||
void RSA_init(void)
|
||||
{
|
||||
REG_RSA_UNK_F0 = 0;
|
||||
}
|
||||
|
||||
static void rsaWaitBusy(void)
|
||||
{
|
||||
while(REG_RSA_CNT & RSA_ENABLE);
|
||||
}
|
||||
|
||||
void RSA_selectKeyslot(u8 keyslot)
|
||||
{
|
||||
rsaWaitBusy();
|
||||
REG_RSA_CNT = (REG_RSA_CNT & ~RSA_KEYSLOT(0xFu)) | RSA_KEYSLOT(keyslot);
|
||||
}
|
||||
|
||||
bool RSA_setKey2048(u8 keyslot, const u32 *const mod, u32 exp)
|
||||
{
|
||||
RsaSlot *slot = &rsaSlots[keyslot];
|
||||
rsaWaitBusy();
|
||||
if(slot->REG_RSA_SLOTCNT & RSA_KEY_WR_PROT) return false;
|
||||
// Unset key if bit 31 is not set. No idea why but boot9 does this.
|
||||
if(!(slot->REG_RSA_SLOTCNT & RSA_KEY_UNK_BIT31)) slot->REG_RSA_SLOTCNT &= ~RSA_KEY_STAT_SET;
|
||||
|
||||
REG_RSA_CNT = RSA_INPUT_NORMAL | RSA_INPUT_BIG | RSA_KEYSLOT(keyslot);
|
||||
iomemset(REGs_RSA_EXP, 0, 0x100 - 4);
|
||||
REGs_RSA_EXP[(0x100>>2) - 1] = exp;
|
||||
|
||||
if(slot->REG_RSA_SLOTSIZE != RSA_SLOTSIZE_2048) return false;
|
||||
iomemcpy(REGs_RSA_MOD, mod, 0x100);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RSA_decrypt2048(u32 *const decSig, const u32 *const encSig)
|
||||
{
|
||||
const u8 keyslot = RSA_GET_KEYSLOT;
|
||||
rsaWaitBusy();
|
||||
if(!(rsaSlots[keyslot].REG_RSA_SLOTCNT & RSA_KEY_STAT_SET)) return false;
|
||||
|
||||
REG_RSA_CNT |= RSA_INPUT_NORMAL | RSA_INPUT_BIG;
|
||||
iomemcpy(REGs_RSA_TXT, encSig, 0x100);
|
||||
|
||||
REG_RSA_CNT |= RSA_ENABLE;
|
||||
rsaWaitBusy();
|
||||
iomemcpy(decSig, REGs_RSA_TXT, 0x100);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RSA_verify2048(const u32 *const encSig, const u32 *const data, u32 size)
|
||||
{
|
||||
alignas(4) u8 decSig[0x100];
|
||||
if(!RSA_decrypt2048((u32*)(void*)decSig, encSig)) return false;
|
||||
|
||||
if(decSig[0] != 0x00 || decSig[1] != 0x01) return false;
|
||||
|
||||
u32 read = 2;
|
||||
while(read < 0x100)
|
||||
{
|
||||
if(decSig[read] != 0xFF) break;
|
||||
read++;
|
||||
}
|
||||
if(read != 0xCC || decSig[read] != 0x00) return false;
|
||||
|
||||
// ASN.1 is a clusterfuck so we skip parsing the remaining headers
|
||||
// and hardcode the hash location.
|
||||
|
||||
return sha_cmp(&(decSig[0xE0]), data, size, SHA256_MODE) == 0;
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
* This file is part of fastboot 3DS
|
||||
* Copyright (C) 2017 derrek, profi200
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
||||
|
||||
//////////////////////////////////
|
||||
// RSA //
|
||||
//////////////////////////////////
|
||||
|
||||
// REG_RSA_CNT
|
||||
#define RSA_ENABLE (1u)
|
||||
#define RSA_UNK_BIT1 (1u<<1)
|
||||
#define RSA_KEYSLOT(k) ((k)<<4)
|
||||
#define RSA_GET_KEYSLOT ((REG_RSA_CNT & RSA_KEYSLOT(0xFu))>>4)
|
||||
#define RSA_INPUT_BIG (1u<<8)
|
||||
#define RSA_INPUT_LITTLE (0u)
|
||||
#define RSA_INPUT_NORMAL (1u<<9)
|
||||
#define RSA_INPUT_REVERSED (0u)
|
||||
|
||||
// RSA_SLOTCNT
|
||||
#define RSA_KEY_STAT_SET (1u)
|
||||
#define RSA_KEY_WR_PROT (1u<<1)
|
||||
#define RSA_KEY_UNK_BIT31 (1u<<31)
|
||||
|
||||
// RSA_SLOTSIZE
|
||||
#define RSA_SLOTSIZE_2048 (0x40u)
|
||||
|
||||
|
||||
/**
|
||||
* @brief Initializes the RSA hardware.
|
||||
*/
|
||||
void RSA_init(void);
|
||||
|
||||
/**
|
||||
* @brief Selects the given keyslot for all following RSA operations.
|
||||
*
|
||||
* @param[in] keyslot The keyslot to select.
|
||||
*/
|
||||
void RSA_selectKeyslot(u8 keyslot);
|
||||
|
||||
/**
|
||||
* @brief Sets a RSA modulus + exponent in the specified keyslot.
|
||||
*
|
||||
* @param[in] keyslot The keyslot this key will be set for.
|
||||
* @param[in] mod Pointer to 2048-bit RSA modulus data.
|
||||
* @param[in] exp The exponent to set.
|
||||
*
|
||||
* @return Returns true on success, false otherwise.
|
||||
*/
|
||||
bool RSA_setKey2048(u8 keyslot, const u32 *const mod, u32 exp);
|
||||
|
||||
/**
|
||||
* @brief Decrypts a RSA 2048 signature.
|
||||
*
|
||||
* @param decSig Pointer to decrypted destination signature.
|
||||
* @param[in] encSig Pointer to encrypted source signature.
|
||||
*
|
||||
* @return Returns true on success, false otherwise.
|
||||
*/
|
||||
bool RSA_decrypt2048(u32 *const decSig, const u32 *const encSig);
|
||||
|
||||
/**
|
||||
* @brief Verifies a RSA 2048 SHA 256 signature.
|
||||
* @brief Note: This function skips the ASN.1 data and is therefore not safe.
|
||||
*
|
||||
* @param[in] encSig Pointer to encrypted source signature.
|
||||
* @param[in] data Pointer to the data to hash.
|
||||
* @param[in] size The hash data size.
|
||||
*
|
||||
* @return Returns true if the signature is valid, false otherwise.
|
||||
*/
|
||||
bool RSA_verify2048(const u32 *const encSig, const u32 *const data, u32 size);
|
@ -1,24 +0,0 @@
|
||||
FatFs License
|
||||
|
||||
FatFs has being developped as a personal project of the author, ChaN. It is free from the code anyone else wrote at current release. Following code block shows a copy of the FatFs license document that heading the source files.
|
||||
|
||||
/*----------------------------------------------------------------------------/
|
||||
/ FatFs - Generic FAT Filesystem Module Rx.xx /
|
||||
/-----------------------------------------------------------------------------/
|
||||
/
|
||||
/ Copyright (C) 20xx, 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
|
||||
/ that the following condition is met:
|
||||
/
|
||||
/ 1. Redistributions of source code must retain the above copyright notice,
|
||||
/ this condition and the following disclaimer.
|
||||
/
|
||||
/ This software is provided by the copyright holder and contributors "AS IS"
|
||||
/ and any warranties related to this software are DISCLAIMED.
|
||||
/ The copyright owner or contributors be NOT LIABLE for any damages caused
|
||||
/ by use of this software.
|
||||
/----------------------------------------------------------------------------*/
|
||||
|
||||
Therefore FatFs license is one of the BSD-style licenses but there is a significant feature. FatFs is mainly intended for embedded systems. In order to extend the usability for commercial products, the redistributions of FatFs in binary form, such as embedded code, binary library and any forms without source code, does not need to include about FatFs in the documentations. This is equivalent to the 1-clause BSD license. Of course FatFs is compatible with the most of open source software licenses including GNU GPL. When you redistribute the FatFs source code with any changes or create a fork, the license can also be changed to GNU GPL, BSD-style license or any open source software license that not conflict with FatFs license.
|
File diff suppressed because it is too large
Load Diff
@ -1,298 +0,0 @@
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ FatFs Functional Configurations
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define FFCONF_DEF 86606 /* Revision ID */
|
||||
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ Function Configurations
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define FF_FS_READONLY 0
|
||||
/* This option switches read-only configuration. (0:Read/Write or 1:Read-only)
|
||||
/ Read-only configuration removes writing API functions, f_write(), f_sync(),
|
||||
/ f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree()
|
||||
/ and optional writing functions as well. */
|
||||
|
||||
|
||||
#define FF_FS_MINIMIZE 0
|
||||
/* This option defines minimization level to remove some basic API functions.
|
||||
/
|
||||
/ 0: Basic functions are fully enabled.
|
||||
/ 1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename()
|
||||
/ are removed.
|
||||
/ 2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1.
|
||||
/ 3: f_lseek() function is removed in addition to 2. */
|
||||
|
||||
|
||||
#define FF_USE_STRFUNC 0
|
||||
/* This option switches string functions, f_gets(), f_putc(), f_puts() and f_printf().
|
||||
/
|
||||
/ 0: Disable string functions.
|
||||
/ 1: Enable without LF-CRLF conversion.
|
||||
/ 2: Enable with LF-CRLF conversion. */
|
||||
|
||||
|
||||
#define FF_USE_FIND 0
|
||||
/* This option switches filtered directory read functions, f_findfirst() and
|
||||
/ f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */
|
||||
|
||||
|
||||
#define FF_USE_MKFS 1
|
||||
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
|
||||
|
||||
|
||||
#define FF_USE_FASTSEEK 0
|
||||
/* This option switches fast seek function. (0:Disable or 1:Enable) */
|
||||
|
||||
|
||||
#define FF_USE_EXPAND 0
|
||||
/* This option switches f_expand function. (0:Disable or 1:Enable) */
|
||||
|
||||
|
||||
#define FF_USE_CHMOD 1
|
||||
/* This option switches attribute manipulation functions, f_chmod() and f_utime().
|
||||
/ (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */
|
||||
|
||||
|
||||
#define FF_USE_LABEL 1
|
||||
/* This option switches volume label functions, f_getlabel() and f_setlabel().
|
||||
/ (0:Disable or 1:Enable) */
|
||||
|
||||
|
||||
#define FF_USE_FORWARD 0
|
||||
/* This option switches f_forward() function. (0:Disable or 1:Enable) */
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ Locale and Namespace Configurations
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define FF_CODE_PAGE 437
|
||||
/* This option specifies the OEM code page to be used on the target system.
|
||||
/ Incorrect code page setting can cause a file open failure.
|
||||
/
|
||||
/ 437 - U.S.
|
||||
/ 720 - Arabic
|
||||
/ 737 - Greek
|
||||
/ 771 - KBL
|
||||
/ 775 - Baltic
|
||||
/ 850 - Latin 1
|
||||
/ 852 - Latin 2
|
||||
/ 855 - Cyrillic
|
||||
/ 857 - Turkish
|
||||
/ 860 - Portuguese
|
||||
/ 861 - Icelandic
|
||||
/ 862 - Hebrew
|
||||
/ 863 - Canadian French
|
||||
/ 864 - Arabic
|
||||
/ 865 - Nordic
|
||||
/ 866 - Russian
|
||||
/ 869 - Greek 2
|
||||
/ 932 - Japanese (DBCS)
|
||||
/ 936 - Simplified Chinese (DBCS)
|
||||
/ 949 - Korean (DBCS)
|
||||
/ 950 - Traditional Chinese (DBCS)
|
||||
/ 0 - Include all code pages above and configured by f_setcp()
|
||||
*/
|
||||
|
||||
|
||||
#define FF_USE_LFN 2
|
||||
#define FF_MAX_LFN 255
|
||||
/* The FF_USE_LFN switches the support for LFN (long file name).
|
||||
/
|
||||
/ 0: Disable LFN. FF_MAX_LFN has no effect.
|
||||
/ 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
|
||||
/ 2: Enable LFN with dynamic working buffer on the STACK.
|
||||
/ 3: Enable LFN with dynamic working buffer on the HEAP.
|
||||
/
|
||||
/ To enable the LFN, ffunicode.c needs to be added to the project. The LFN function
|
||||
/ requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and
|
||||
/ additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled.
|
||||
/ The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can
|
||||
/ be in range of 12 to 255. It is recommended to be set it 255 to fully support LFN
|
||||
/ specification.
|
||||
/ When use stack for the working buffer, take care on stack overflow. When use heap
|
||||
/ memory for the working buffer, memory management functions, ff_memalloc() and
|
||||
/ ff_memfree() exemplified in ffsystem.c, need to be added to the project. */
|
||||
|
||||
|
||||
#define FF_LFN_UNICODE 2
|
||||
/* This option switches the character encoding on the API when LFN is enabled.
|
||||
/
|
||||
/ 0: ANSI/OEM in current CP (TCHAR = char)
|
||||
/ 1: Unicode in UTF-16 (TCHAR = WCHAR)
|
||||
/ 2: Unicode in UTF-8 (TCHAR = char)
|
||||
/ 3: Unicode in UTF-32 (TCHAR = DWORD)
|
||||
/
|
||||
/ Also behavior of string I/O functions will be affected by this option.
|
||||
/ When LFN is not enabled, this option has no effect. */
|
||||
|
||||
|
||||
#define FF_LFN_BUF 255
|
||||
#define FF_SFN_BUF 12
|
||||
/* This set of options defines size of file name members in the FILINFO structure
|
||||
/ which is used to read out directory items. These values should be suffcient for
|
||||
/ the file names to read. The maximum possible length of the read file name depends
|
||||
/ on character encoding. When LFN is not enabled, these options have no effect. */
|
||||
|
||||
|
||||
#define FF_STRF_ENCODE 0
|
||||
/* When FF_LFN_UNICODE >= 1 with LFN enabled, string I/O functions, f_gets(),
|
||||
/ f_putc(), f_puts and f_printf() convert the character encoding in it.
|
||||
/ This option selects assumption of character encoding ON THE FILE to be
|
||||
/ read/written via those functions.
|
||||
/
|
||||
/ 0: ANSI/OEM in current CP
|
||||
/ 1: Unicode in UTF-16LE
|
||||
/ 2: Unicode in UTF-16BE
|
||||
/ 3: Unicode in UTF-8
|
||||
*/
|
||||
|
||||
|
||||
#define FF_FS_RPATH 0
|
||||
/* This option configures support for relative path.
|
||||
/
|
||||
/ 0: Disable relative path and remove related functions.
|
||||
/ 1: Enable relative path. f_chdir() and f_chdrive() are available.
|
||||
/ 2: f_getcwd() function is available in addition to 1.
|
||||
*/
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ Drive/Volume Configurations
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define FF_VOLUMES 10
|
||||
/* Number of volumes (logical drives) to be used. (1-10) */
|
||||
|
||||
|
||||
#define FF_STR_VOLUME_ID 0
|
||||
#define FF_VOLUME_STRS "sdcard","sysnand","systwln","systwlp","emunand","emutwln","emutwlp","imgnand","imgtwln","imgtwlp"
|
||||
/* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings.
|
||||
/ When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive
|
||||
/ number in the path name. FF_VOLUME_STRS defines the volume ID strings for each
|
||||
/ logical drives. Number of items must not be less than FF_VOLUMES. Valid
|
||||
/ characters for the volume ID strings are A-Z, a-z and 0-9, however, they are
|
||||
/ compared in case-insensitive. If FF_STR_VOLUME_ID >= 1 and FF_VOLUME_STRS is
|
||||
/ not defined, a user defined volume string table needs to be defined as:
|
||||
/
|
||||
/ const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb",...
|
||||
*/
|
||||
|
||||
|
||||
#define FF_MULTI_PARTITION 1
|
||||
/* This option switches support for multiple volumes on the physical drive.
|
||||
/ By default (0), each logical drive number is bound to the same physical drive
|
||||
/ number and only an FAT volume found on the physical drive will be mounted.
|
||||
/ When this function is enabled (1), each logical drive number can be bound to
|
||||
/ arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk()
|
||||
/ funciton will be available. */
|
||||
|
||||
|
||||
#define FF_MIN_SS 512
|
||||
#define FF_MAX_SS 512
|
||||
/* This set of options configures the range of sector size to be supported. (512,
|
||||
/ 1024, 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 FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured
|
||||
/ for variable sector size mode and disk_ioctl() function needs to implement
|
||||
/ GET_SECTOR_SIZE command. */
|
||||
|
||||
|
||||
#define FF_LBA64 0
|
||||
/* This option switches support for 64-bit LBA. (0:Disable or 1:Enable)
|
||||
/ To enable the 64-bit LBA, also exFAT needs to be enabled. (FF_FS_EXFAT == 1) */
|
||||
|
||||
|
||||
#define FF_MIN_GPT 0x100000000
|
||||
/* Minimum number of sectors to switch GPT format to create partition in f_mkfs and
|
||||
/ f_fdisk function. 0x100000000 max. This option has no effect when FF_LBA64 == 0. */
|
||||
|
||||
|
||||
#define FF_USE_TRIM 0
|
||||
/* This option switches support for ATA-TRIM. (0:Disable or 1:Enable)
|
||||
/ To enable Trim function, also CTRL_TRIM command should be implemented to the
|
||||
/ disk_ioctl() function. */
|
||||
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ System Configurations
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define FF_FS_TINY 0
|
||||
/* This option switches tiny buffer configuration. (0:Normal or 1:Tiny)
|
||||
/ At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes.
|
||||
/ Instead of private sector buffer eliminated from the file object, common sector
|
||||
/ buffer in the filesystem object (FATFS) is used for the file data transfer. */
|
||||
|
||||
|
||||
#define FF_FS_EXFAT 0
|
||||
/* This option switches support for exFAT filesystem. (0:Disable or 1:Enable)
|
||||
/ To enable exFAT, also LFN needs to be enabled. (FF_USE_LFN >= 1)
|
||||
/ Note that enabling exFAT discards ANSI C (C89) compatibility. */
|
||||
|
||||
|
||||
#define FF_FS_NORTC 0
|
||||
#define FF_NORTC_MON 1
|
||||
#define FF_NORTC_MDAY 1
|
||||
#define FF_NORTC_YEAR 2019
|
||||
/* The option FF_FS_NORTC switches timestamp functiton. If the system does not have
|
||||
/ any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable
|
||||
/ the timestamp function. Every object modified by FatFs will have a fixed timestamp
|
||||
/ defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time.
|
||||
/ To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be
|
||||
/ added to the project to read current time form real-time clock. FF_NORTC_MON,
|
||||
/ FF_NORTC_MDAY and FF_NORTC_YEAR have no effect.
|
||||
/ These options have no effect in read-only configuration (FF_FS_READONLY = 1). */
|
||||
|
||||
|
||||
#define FF_FS_NOFSINFO 0
|
||||
/* If you need to know correct free space on the FAT32 volume, set bit 0 of this
|
||||
/ option, and f_getfree() function at first time after volume mount will force
|
||||
/ a full FAT scan. Bit 1 controls the use of last allocated cluster number.
|
||||
/
|
||||
/ bit0=0: Use free cluster count in the FSINFO if available.
|
||||
/ bit0=1: Do not trust free cluster count in the FSINFO.
|
||||
/ bit1=0: Use last allocated cluster number in the FSINFO if available.
|
||||
/ bit1=1: Do not trust last allocated cluster number in the FSINFO.
|
||||
*/
|
||||
|
||||
|
||||
#define FF_FS_LOCK 32
|
||||
/* The option FF_FS_LOCK switches file lock function to control duplicated file open
|
||||
/ and illegal operation to open objects. This option must be 0 when FF_FS_READONLY
|
||||
/ is 1.
|
||||
/
|
||||
/ 0: Disable file lock function. To avoid volume corruption, application program
|
||||
/ should avoid illegal open, remove and rename to the open objects.
|
||||
/ >0: Enable file lock function. The value defines how many files/sub-directories
|
||||
/ can be opened simultaneously under file lock control. Note that the file
|
||||
/ lock control is independent of re-entrancy. */
|
||||
|
||||
|
||||
/* #include <somertos.h> // O/S definitions */
|
||||
#define FF_FS_REENTRANT 0
|
||||
#define FF_FS_TIMEOUT 1000
|
||||
#define FF_SYNC_t HANDLE
|
||||
/* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs
|
||||
/ module itself. Note that regardless of this option, file access to different
|
||||
/ volume is always re-entrant and volume control functions, f_mount(), f_mkfs()
|
||||
/ and f_fdisk() function, are always not re-entrant. Only file/directory access
|
||||
/ to the same volume is under control of this function.
|
||||
/
|
||||
/ 0: Disable re-entrancy. FF_FS_TIMEOUT and FF_SYNC_t have no effect.
|
||||
/ 1: Enable re-entrancy. Also user provided synchronization handlers,
|
||||
/ ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj()
|
||||
/ function, must be added to the project. Samples are available in
|
||||
/ option/syscall.c.
|
||||
/
|
||||
/ The FF_FS_TIMEOUT defines timeout period in unit of time tick.
|
||||
/ The FF_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
|
||||
/ included somewhere in the scope of ff.h. */
|
||||
|
||||
|
||||
|
||||
/*--- End of configuration options ---*/
|
File diff suppressed because it is too large
Load Diff
@ -1,194 +0,0 @@
|
||||
#include "filetype.h"
|
||||
#include "fsutil.h"
|
||||
#include "image.h"
|
||||
#include "fatmbr.h"
|
||||
#include "nand.h"
|
||||
#include "game.h"
|
||||
#include "disadiff.h"
|
||||
#include "keydb.h"
|
||||
#include "ctrtransfer.h"
|
||||
#include "scripting.h"
|
||||
#include "gm9lua.h"
|
||||
#include "png.h"
|
||||
#include "ui.h" // only for font file detection
|
||||
|
||||
u64 IdentifyFileType(const char* path) {
|
||||
static const u8 romfs_magic[] = { ROMFS_MAGIC };
|
||||
static const u8 diff_magic[] = { DIFF_MAGIC };
|
||||
static const u8 disa_magic[] = { DISA_MAGIC };
|
||||
static const u8 tickdb_magic[] = { TICKDB_MAGIC };
|
||||
static const u8 smdh_magic[] = { SMDH_MAGIC };
|
||||
static const u8 threedsx_magic[] = { THREEDSX_EXT_MAGIC };
|
||||
static const u8 png_magic[] = { PNG_MAGIC };
|
||||
|
||||
if (!path) return 0; // safety
|
||||
u8 ALIGN(32) header[0x2C0]; // minimum required size
|
||||
void* data = (void*) header;
|
||||
size_t fsize = FileGetSize(path);
|
||||
char* fname = strrchr(path, '/');
|
||||
char* ext = (fname) ? strrchr(++fname, '.') : NULL;
|
||||
u32 id = 0;
|
||||
|
||||
// block crappy "._" files from getting recognized as filetype
|
||||
if (!fname) return 0;
|
||||
if (strncmp(fname, "._", 2) == 0) return 0;
|
||||
|
||||
if (ext) {
|
||||
ext++;
|
||||
} else {
|
||||
ext = "";
|
||||
}
|
||||
if (FileGetData(path, header, 0x2C0, 0) < min(0x2C0, fsize)) return 0;
|
||||
if (!fsize) return 0;
|
||||
|
||||
if (fsize >= 0x200) {
|
||||
if (ValidateNandNcsdHeader((NandNcsdHeader*) data) == 0) {
|
||||
return (fsize >= GetNandNcsdMinSizeSectors((NandNcsdHeader*) data) * 0x200) ?
|
||||
IMG_NAND : (fsize == sizeof(NandNcsdHeader)) ? HDR_NAND : 0; // NAND image or just header
|
||||
} else if ((strncasecmp(path, "S:/nand.bin", 16) == 0) || (strncasecmp(path, "E:/nand.bin", 16) == 0)) {
|
||||
return NOIMG_NAND; // on NAND, but no proper NAND image
|
||||
} else if (ValidateFatHeader(header) == 0) {
|
||||
return IMG_FAT; // FAT image file
|
||||
} else if (ValidateMbrHeader((MbrHeader*) data) == 0) {
|
||||
MbrHeader* mbr = (MbrHeader*) data;
|
||||
MbrPartitionInfo* partition0 = mbr->partitions;
|
||||
bool ctr = (CheckTransferableMbr(mbr) == 0); // is this a CTRNAND MBR?
|
||||
if ((partition0->sector + partition0->count) <= (fsize / 0x200)) // size check
|
||||
return IMG_FAT | (ctr ? FLAG_CTR : 0); // possibly an MBR -> also treat as FAT image
|
||||
} else if (ValidateCiaHeader((CiaHeader*) data) == 0) {
|
||||
// this only works because these functions ignore CIA content index
|
||||
CiaInfo info;
|
||||
GetCiaInfo(&info, data);
|
||||
if (fsize >= info.size_cia)
|
||||
return GAME_CIA; // CIA file
|
||||
} else if (ValidateNcsdHeader((NcsdHeader*) data) == 0) {
|
||||
NcsdHeader* ncsd = (NcsdHeader*) data;
|
||||
if (fsize >= GetNcsdTrimmedSize(ncsd))
|
||||
return GAME_NCSD; // NCSD (".3DS") file
|
||||
} else if (ValidateNcchHeader((NcchHeader*) data) == 0) {
|
||||
NcchHeader* ncch = (NcchHeader*) data;
|
||||
u64 type = GAME_NCCH;
|
||||
if (NCCH_IS_CXI(ncch)) {
|
||||
type |= FLAG_CXI;
|
||||
/* the below is unused for now
|
||||
type |= NCCH_IS_FIRM(ncch) ? FLAG_FIRM : 0;
|
||||
NcchExtHeader exhdr;
|
||||
if ((FileGetData(path, &exhdr, 0x400, 0x200) == 0x400) && // read only what we need
|
||||
(DecryptNcch(&exhdr, 0x200, 0x400, ncch, NULL) == 0) &&
|
||||
NCCH_IS_GBAVC(&exhdr))
|
||||
type |= FLAG_GBAVC;
|
||||
*/
|
||||
}
|
||||
if (fsize >= (ncch->size * NCCH_MEDIA_UNIT))
|
||||
return type; // NCCH (".APP") file
|
||||
} else if (ValidateExeFsHeader((ExeFsHeader*) data, fsize) == 0) {
|
||||
return GAME_EXEFS; // ExeFS file (false positives possible)
|
||||
} else if (memcmp(header, romfs_magic, sizeof(romfs_magic)) == 0) {
|
||||
return GAME_ROMFS; // RomFS file (check could be better)
|
||||
} else if (ValidateTmd((TitleMetaData*) data) == 0) {
|
||||
if (fsize == TMD_SIZE_N(getbe16(header + 0x1DE)) + TMD_CDNCERT_SIZE)
|
||||
return GAME_CDNTMD; // TMD file from NUS/CDN
|
||||
else if (fsize >= TMD_SIZE_N(getbe16(header + 0x1DE)))
|
||||
return GAME_TMD; // TMD file
|
||||
} else if (ValidateTwlTmd((TitleMetaData*) data) == 0) {
|
||||
if (fsize == TMD_SIZE_TWL + TMD_CDNCERT_SIZE)
|
||||
return GAME_TWLTMD; // TMD file from NUS/CDN (TWL)
|
||||
} else if (ValidateTicket((Ticket*) data) == 0) {
|
||||
return GAME_TICKET; // Ticket file
|
||||
} else if (ValidateFirmHeader((FirmHeader*) data, fsize) == 0) {
|
||||
return SYS_FIRM; // FIRM file
|
||||
} else if ((ValidateAgbSaveHeader((AgbSaveHeader*) data) == 0) && (fsize >= AGBSAVE_MAX_SIZE)) {
|
||||
return SYS_AGBSAVE; // AGBSAVE file
|
||||
} else if (memcmp(header + 0x100, diff_magic, sizeof(diff_magic)) == 0) { // DIFF file
|
||||
if (memcmp(header + 0x100, tickdb_magic, sizeof(tickdb_magic)) == 0) // ticket.db file
|
||||
return SYS_DIFF | SYS_TICKDB; // ticket.db
|
||||
return SYS_DIFF;
|
||||
} else if (memcmp(header + 0x100, disa_magic, sizeof(disa_magic)) == 0) { // DISA file
|
||||
return SYS_DISA;
|
||||
} else if (memcmp(header, smdh_magic, sizeof(smdh_magic)) == 0) {
|
||||
return GAME_SMDH; // SMDH file
|
||||
} else if (ValidateTwlHeader((TwlHeader*) data) == 0) {
|
||||
TwlHeader* twl = (TwlHeader*) data;
|
||||
if (twl->ntr_rom_size <= fsize) { // NDS rom file
|
||||
if ((twl->unit_code == 0x03) && !twl->twl_rom_region_start)
|
||||
return GAME_NDS | FLAG_DSIW; // NDS DSiWare rom file
|
||||
return GAME_NDS; // NDS rom file
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (fsize == sizeof(TitleInfoEntry) && (strncasecmp(path, "T:/", 3) == 0)) {
|
||||
const char* mntpath = GetMountPath();
|
||||
if (mntpath && *mntpath) {
|
||||
if ((strncasecmp(mntpath, "1:/dbs/title.db", 16) == 0) ||
|
||||
(strncasecmp(mntpath, "4:/dbs/title.db", 16) == 0) ||
|
||||
(strncasecmp(mntpath, "A:/dbs/title.db", 16) == 0) ||
|
||||
(strncasecmp(mntpath, "B:/dbs/title.db", 16) == 0))
|
||||
return GAME_TIE;
|
||||
}
|
||||
} else if (GetFontFromPbm(data, fsize, NULL, NULL)) {
|
||||
return FONT_PBM;
|
||||
} else if (GetFontFromRiff(data, fsize, NULL, NULL, NULL)) {
|
||||
return FONT_RIFF;
|
||||
} else if (GetLanguage(data, fsize, NULL, NULL, NULL)) {
|
||||
return TRANSLATION;
|
||||
} else if ((fsize > sizeof(AgbHeader)) &&
|
||||
(ValidateAgbHeader((AgbHeader*) data) == 0)) {
|
||||
return GAME_GBA;
|
||||
} else if ((fsize > sizeof(BossHeader)) &&
|
||||
(ValidateBossHeader((BossHeader*) data, fsize) == 0)) {
|
||||
return GAME_BOSS; // BOSS (SpotPass) file
|
||||
} else if ((fsize > sizeof(ThreedsxHeader)) &&
|
||||
(memcmp(data, threedsx_magic, sizeof(threedsx_magic)) == 0)) {
|
||||
return GAME_3DSX; // 3DSX (executable) file
|
||||
} else if ((fsize > sizeof(CmdHeader)) &&
|
||||
(CMD_SIZE((CmdHeader*) data) == fsize)) {
|
||||
return GAME_CMD; // CMD file
|
||||
} else if ((fsize > sizeof(NcchInfoHeader)) &&
|
||||
(GetNcchInfoVersion((NcchInfoHeader*) data)) &&
|
||||
(strncasecmp(fname, NCCHINFO_NAME, 32) == 0)) {
|
||||
return BIN_NCCHNFO; // ncchinfo.bin file
|
||||
} else if ((strncasecmp(ext, "png", 4) == 0) &&
|
||||
(fsize > sizeof(png_magic)) && (memcmp(data, png_magic, sizeof(png_magic)) == 0)) {
|
||||
return GFX_PNG;
|
||||
} else if (strncasecmp(fname, TIKDB_NAME_ENC, sizeof(TIKDB_NAME_ENC)+1) == 0) {
|
||||
return BIN_TIKDB | FLAG_ENC; // titlekey database / encrypted
|
||||
} else if (strncasecmp(fname, TIKDB_NAME_DEC, sizeof(TIKDB_NAME_DEC)+1) == 0) {
|
||||
return BIN_TIKDB; // titlekey database / decrypted
|
||||
} else if (strncasecmp(fname, KEYDB_NAME, sizeof(KEYDB_NAME)+1) == 0) {
|
||||
return BIN_KEYDB; // key database
|
||||
} else if ((sscanf(fname, "slot%02lXKey", &id) == 1) && (strncasecmp(ext, "bin", 4) == 0) && (fsize = 16) && (id < 0x40)) {
|
||||
return BIN_LEGKEY; // legacy key file
|
||||
} else if ((strncmp((char*) data, CIFINISH_MAGIC, strlen(CIFINISH_MAGIC)) == 0) &&
|
||||
(fsize == CIFINISH_SIZE((void*) data)) && (fsize > sizeof(CifinishHeader))) {
|
||||
return BIN_CIFNSH;
|
||||
} else if (ValidateText((char*) data, (fsize > 0x200) ? 0x200 : fsize)) {
|
||||
u64 type = 0;
|
||||
if ((fsize < SCRIPT_MAX_SIZE) && (strcasecmp(ext, SCRIPT_EXT) == 0))
|
||||
type |= TXT_SCRIPT; // should be a script (which is also generic text)
|
||||
// this should check if it's compiled lua bytecode (done with luac), which is NOT text
|
||||
else if ((fsize < LUASCRIPT_MAX_SIZE) && (strcasecmp(ext, LUASCRIPT_EXT) == 0))
|
||||
type |= TXT_LUA;
|
||||
if (fsize < STD_BUFFER_SIZE) type |= TXT_GENERIC;
|
||||
return type;
|
||||
} else if ((strncmp(path + 2, "/Nintendo DSiWare/", 18) == 0) &&
|
||||
(sscanf(fname, "%08lx.bin", &id) == 1) && (strncasecmp(ext, "bin", 4) == 0)) {
|
||||
TadHeader hdr;
|
||||
if ((FileGetData(path, &hdr, TAD_HEADER_LEN, TAD_HEADER_OFFSET) == TAD_HEADER_LEN) &&
|
||||
(strncmp(hdr.magic, TAD_HEADER_MAGIC, strlen(TAD_HEADER_MAGIC)) == 0))
|
||||
return GAME_TAD;
|
||||
} else if ((strnlen(fname, 16) == 8) && (sscanf(fname, "%08lx", &id) == 1)) {
|
||||
char path_cdn[256];
|
||||
char* name_cdn = path_cdn + (fname - path);
|
||||
strncpy(path_cdn, path, 256);
|
||||
path_cdn[255] = '\0';
|
||||
strncpy(name_cdn, "tmd", 4); // this will not catch tmd with version
|
||||
if (FileGetSize(path_cdn) > 0)
|
||||
return GAME_NUSCDN; // NUS/CDN, recognized by TMD
|
||||
strncpy(name_cdn, "cetk", 5);
|
||||
if (FileGetSize(path_cdn) > 0)
|
||||
return GAME_NUSCDN; // NUS/CDN, recognized by CETK
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,90 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#define IMG_FAT (1ULL<<0)
|
||||
#define IMG_NAND (1ULL<<1)
|
||||
#define GAME_CIA (1ULL<<2)
|
||||
#define GAME_NCSD (1ULL<<3)
|
||||
#define GAME_NCCH (1ULL<<4)
|
||||
#define GAME_TMD (1ULL<<5)
|
||||
#define GAME_CDNTMD (1ULL<<6)
|
||||
#define GAME_TWLTMD (1ULL<<7)
|
||||
#define GAME_CMD (1ULL<<8)
|
||||
#define GAME_EXEFS (1ULL<<9)
|
||||
#define GAME_ROMFS (1ULL<<10)
|
||||
#define GAME_BOSS (1ULL<<11)
|
||||
#define GAME_NUSCDN (1ULL<<12)
|
||||
#define GAME_TICKET (1ULL<<13)
|
||||
#define GAME_TIE (1ULL<<14)
|
||||
#define GAME_SMDH (1ULL<<15)
|
||||
#define GAME_3DSX (1ULL<<16)
|
||||
#define GAME_NDS (1ULL<<17)
|
||||
#define GAME_GBA (1ULL<<18)
|
||||
#define GAME_TAD (1ULL<<19)
|
||||
#define SYS_FIRM (1ULL<<20)
|
||||
#define SYS_DIFF (1ULL<<21)
|
||||
#define SYS_DISA (1ULL<<22)
|
||||
#define SYS_AGBSAVE (1ULL<<23)
|
||||
#define SYS_TICKDB (1ULL<<24)
|
||||
#define BIN_CIFNSH (1ULL<<25)
|
||||
#define BIN_NCCHNFO (1ULL<<26)
|
||||
#define BIN_TIKDB (1ULL<<27)
|
||||
#define BIN_KEYDB (1ULL<<28)
|
||||
#define BIN_LEGKEY (1ULL<<29)
|
||||
#define TXT_SCRIPT (1ULL<<30)
|
||||
#define TXT_GENERIC (1ULL<<31)
|
||||
#define GFX_PNG (1ULL<<32)
|
||||
#define FONT_PBM (1ULL<<33)
|
||||
#define FONT_RIFF (1ULL<<34)
|
||||
#define NOIMG_NAND (1ULL<<35)
|
||||
#define HDR_NAND (1ULL<<36)
|
||||
#define TRANSLATION (1ULL<<37)
|
||||
#define TXT_LUA (1ULL<<38)
|
||||
#define TYPE_BASE 0xFFFFFFFFFFULL // 40 bit reserved for base types
|
||||
|
||||
// #define FLAG_FIRM (1ULL<<58) // <--- for CXIs containing FIRMs
|
||||
// #define FLAG_GBAVC (1ULL<<59) // <--- for GBAVC CXIs
|
||||
#define FLAG_DSIW (1ULL<<60)
|
||||
#define FLAG_ENC (1ULL<<61)
|
||||
#define FLAG_CTR (1ULL<<62)
|
||||
#define FLAG_CXI (1ULL<<63)
|
||||
|
||||
#define FTYPE_MOUNTABLE(tp) (tp&(IMG_FAT|IMG_NAND|GAME_CIA|GAME_NCSD|GAME_NCCH|GAME_EXEFS|GAME_ROMFS|GAME_NDS|GAME_TAD|SYS_FIRM|SYS_DIFF|SYS_DISA|SYS_TICKDB|BIN_KEYDB))
|
||||
#define FTYPE_VERIFICABLE(tp) (tp&(IMG_NAND|GAME_CIA|GAME_NCSD|GAME_NCCH|GAME_TMD|GAME_CDNTMD|GAME_TWLTMD|GAME_TIE|GAME_TAD|GAME_TICKET|GAME_BOSS|SYS_FIRM))
|
||||
#define FTYPE_DECRYPTABLE(tp) (tp&(GAME_CIA|GAME_NCSD|GAME_NCCH|GAME_BOSS|GAME_NUSCDN|SYS_FIRM|BIN_KEYDB))
|
||||
#define FTYPE_ENCRYPTABLE(tp) (tp&(GAME_CIA|GAME_NCSD|GAME_NCCH|GAME_BOSS|BIN_KEYDB))
|
||||
#define FTYPE_CIABUILD(tp) ((tp&(GAME_NCSD|GAME_NCCH|GAME_TMD|GAME_CDNTMD|GAME_TWLTMD|GAME_TIE|GAME_TAD)) || ((tp&GAME_NDS)&&(tp&(FLAG_DSIW))))
|
||||
#define FTYPE_CIABUILD_L(tp) (tp&(GAME_TMD|GAME_CDNTMD|GAME_TIE|GAME_TAD))
|
||||
#define FTYPE_CIAINSTALL(tp) ((tp&(GAME_NCSD|GAME_NCCH|GAME_CIA|GAME_CDNTMD|GAME_TWLTMD)) || ((tp&GAME_NDS)&&(tp&(FLAG_DSIW))))
|
||||
#define FTYPE_TIKINSTALL(tp) (tp&(GAME_TICKET))
|
||||
#define FTYPE_CIFINSTALL(tp) (tp&(BIN_CIFNSH))
|
||||
#define FTYPE_TIKDUMP(tp) (tp&(GAME_TIE))
|
||||
#define FTYPE_CXIDUMP(tp) (tp&(GAME_TMD|GAME_TIE))
|
||||
#define FTYPE_UNINSTALL(tp) (tp&(GAME_TIE))
|
||||
#define FTYPE_TIKBUILD(tp) (tp&(GAME_TICKET|SYS_TICKDB|BIN_TIKDB))
|
||||
#define FTYPE_KEYBUILD(tp) (tp&(BIN_KEYDB|BIN_LEGKEY))
|
||||
#define FTYPE_TITLEINFO(tp) (tp&(GAME_TIE|GAME_CIA|GAME_TMD|GAME_CDNTMD|GAME_TWLTMD))
|
||||
#define FTYPE_RENAMABLE(tp) (tp&(GAME_NCCH|GAME_NCSD|GAME_CIA|GAME_NDS|GAME_GBA))
|
||||
#define FTYPE_TRIMABLE(tp) (tp&(IMG_NAND|GAME_NCCH|GAME_NCSD|GAME_NDS|GAME_GBA|SYS_FIRM))
|
||||
#define FTYPE_TRANSFERABLE(tp) ((u64) (tp&(IMG_FAT|FLAG_CTR)) == (u64) (IMG_FAT|FLAG_CTR))
|
||||
#define FTYPE_NCSDFIXABLE(tp) (tp&(HDR_NAND|NOIMG_NAND))
|
||||
#define FTYPE_HASCODE(tp) (((u64) (tp&(GAME_NCCH|FLAG_CXI)) == (u64) (GAME_NCCH|FLAG_CXI))|(tp&GAME_NCSD))
|
||||
#define FTYPE_ISDISADIFF(tp) (tp&(SYS_DIFF|SYS_DISA))
|
||||
#define FTYPE_RESTORABLE(tp) (tp&(IMG_NAND))
|
||||
#define FTYPE_EBACKUP(tp) (tp&(IMG_NAND))
|
||||
// #define FTYPE_XORPAD(tp) (tp&(BIN_NCCHNFO)) // deprecated
|
||||
#define FTYPE_XORPAD(tp) 0
|
||||
#define FTYPE_KEYINIT(tp) (tp&(BIN_KEYDB))
|
||||
#define FTYPE_KEYINSTALL(tp) (tp&(BIN_KEYDB))
|
||||
#define FTYPE_SCRIPT(tp) (tp&(TXT_SCRIPT))
|
||||
#define FTYPE_LUA(tp) (tp&(TXT_LUA))
|
||||
#define FTYPE_FONT(tp) (tp&(FONT_PBM|FONT_RIFF))
|
||||
#define FTYPE_TRANSLATION(tp) (tp&(TRANSLATION))
|
||||
#define FTYPE_GFX(tp) (tp&(GFX_PNG))
|
||||
#define FTYPE_SETABLE(tp) (tp&(FONT_PBM|FONT_RIFF|TRANSLATION))
|
||||
#define FTYPE_BOOTABLE(tp) (tp&(SYS_FIRM))
|
||||
#define FTYPE_INSTALLABLE(tp) (tp&(SYS_FIRM))
|
||||
#define FTYPE_AGBSAVE(tp) (tp&(SYS_AGBSAVE))
|
||||
|
||||
u64 IdentifyFileType(const char* path);
|
@ -1,11 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "filetype.h"
|
||||
#include "fsdir.h"
|
||||
#include "fsdrive.h"
|
||||
#include "fsgame.h"
|
||||
#include "fsinit.h"
|
||||
#include "fsperm.h"
|
||||
#include "fsutil.h"
|
||||
#include "image.h"
|
||||
#include "vff.h"
|
@ -1,25 +0,0 @@
|
||||
#include "fsdir.h"
|
||||
|
||||
void DirEntryCpy(DirEntry* dest, const DirEntry* orig) {
|
||||
memcpy(dest, orig, sizeof(DirEntry));
|
||||
dest->name = dest->path + dest->p_name;
|
||||
}
|
||||
|
||||
int compDirEntry(const void* e1, const void* e2) {
|
||||
const DirEntry* entry1 = (const DirEntry*) e1;
|
||||
const DirEntry* entry2 = (const DirEntry*) e2;
|
||||
if (entry1->type == T_DOTDOT) return -1;
|
||||
if (entry2->type == T_DOTDOT) return 1;
|
||||
if (entry1->type != entry2->type)
|
||||
return entry1->type - entry2->type;
|
||||
return strncasecmp(entry1->path, entry2->path, 256);
|
||||
}
|
||||
|
||||
void SortDirStruct(DirStruct* contents) {
|
||||
qsort(contents->entry, contents->n_entries, sizeof(DirEntry), compDirEntry);
|
||||
// fix entry->names after qsort
|
||||
for (int i = 0; i < (int)contents->n_entries; i++) {
|
||||
DirEntry* entry = &(contents->entry[i]);
|
||||
entry->name = entry->path + entry->p_name;
|
||||
}
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
#include "fsdir.h"
|
||||
|
||||
#define NORM_FS 10
|
||||
#define IMGN_FS 3 // image normal filesystems
|
||||
|
||||
// primary drive types
|
||||
#define DRV_UNKNOWN (0<<0)
|
||||
#define DRV_FAT (1UL<<0)
|
||||
#define DRV_VIRTUAL (1UL<<1)
|
||||
// secondary drive types
|
||||
#define DRV_SDCARD (1UL<<2)
|
||||
#define DRV_SYSNAND (1UL<<3)
|
||||
#define DRV_EMUNAND (1UL<<4)
|
||||
#define DRV_CTRNAND (1UL<<5)
|
||||
#define DRV_TWLNAND (1UL<<6)
|
||||
#define DRV_IMAGE (1UL<<7)
|
||||
#define DRV_XORPAD (1UL<<8)
|
||||
#define DRV_RAMDRIVE (1UL<<9)
|
||||
#define DRV_MEMORY (1UL<<10)
|
||||
#define DRV_GAME (1UL<<11)
|
||||
#define DRV_CART (1UL<<12)
|
||||
#define DRV_VRAM (1UL<<13)
|
||||
#define DRV_ALIAS (1UL<<14)
|
||||
#define DRV_BONUS (1UL<<15)
|
||||
#define DRV_TITLEMAN (1UL<<16)
|
||||
#define DRV_SEARCH (1UL<<17)
|
||||
#define DRV_STDFAT (1UL<<18) // standard FAT drive without limitations
|
||||
|
||||
#define DRV_LABEL_LEN (36)
|
||||
|
||||
#define FS_DRVNAME \
|
||||
STR_LAB_SDCARD, \
|
||||
STR_LAB_SYSNAND_CTRNAND, STR_LAB_SYSNAND_TWLN, STR_LAB_SYSNAND_TWLP, STR_LAB_SYSNAND_SD, STR_LAB_SYSNAND_VIRTUAL, \
|
||||
STR_LAB_EMUNAND_CTRNAND, STR_LAB_EMUNAND_TWLN, STR_LAB_EMUNAND_TWLP, STR_LAB_EMUNAND_SD, STR_LAB_EMUNAND_VIRTUAL, \
|
||||
STR_LAB_IMGNAND_CTRNAND, STR_LAB_IMGNAND_TWLN, STR_LAB_IMGNAND_TWLP, STR_LAB_IMGNAND_VIRTUAL, \
|
||||
STR_LAB_GAMECART, \
|
||||
STR_LAB_GAME_IMAGE, STR_LAB_AESKEYDB_IMAGE, STR_LAB_BDRI_IMAGE, STR_LAB_DISA_DIFF_IMAGE, \
|
||||
STR_LAB_MEMORY_VIRTUAL, \
|
||||
STR_LAB_VRAM_VIRTUAL, \
|
||||
STR_LAB_TITLE_MANAGER, \
|
||||
STR_LAB_LAST_SEARCH
|
||||
|
||||
#define FS_DRVNUM \
|
||||
"0:", "1:", "2:", "3:", "A:", "S:", "4:", "5:", "6:", "B:", "E:", "7:", "8:", "9:", \
|
||||
"I:", "C:", "G:", "K:", "T:", "D:", "M:", "V:", "Y:", "Z:"
|
||||
|
||||
/** Function to identify the type of a drive **/
|
||||
int DriveType(const char* path);
|
||||
|
||||
/** Set search pattern / path / mode for special Z: drive **/
|
||||
void SetFSSearch(const char* pattern, const char* path);
|
||||
|
||||
/** Enable title manager for special processing of mounted title.db **/
|
||||
void SetTitleManagerMode(bool mode);
|
||||
|
||||
/** Read the FAT volume label of a partition **/
|
||||
bool GetFATVolumeLabel(const char* drv, char* label);
|
||||
|
||||
/** Get directory content under a given path **/
|
||||
void GetDirContents(DirStruct* contents, const char* path);
|
||||
|
||||
/** Gets remaining space in filesystem in bytes */
|
||||
uint64_t GetFreeSpace(const char* path);
|
||||
|
||||
/** Gets total spacein filesystem in bytes */
|
||||
uint64_t GetTotalSpace(const char* path);
|
||||
|
||||
/** Return the offset - in sectors - of the FAT partition on the drive **/
|
||||
uint64_t GetPartitionOffsetSector(const char* path);
|
@ -1,322 +0,0 @@
|
||||
#include "sddata.h"
|
||||
#include "tad.h"
|
||||
#include "aes.h"
|
||||
#include "sha.h"
|
||||
|
||||
#define DSIWARE_MAGIC "Nintendo DSiWare" // must be exactly 16 chars
|
||||
#define NUM_ALIAS_DRV 2
|
||||
#define NUM_FILCRYPTINFO 16
|
||||
|
||||
typedef struct {
|
||||
FIL* fptr;
|
||||
u8 ctr[16];
|
||||
u8 keyy[16];
|
||||
} PACKED_STRUCT FilCryptInfo;
|
||||
|
||||
static FilCryptInfo filcrypt[NUM_FILCRYPTINFO] = { 0 };
|
||||
|
||||
static char alias_drv[NUM_ALIAS_DRV]; // 1 char ASCII drive number of the alias drive / 0x00 if unused
|
||||
static char alias_path[NUM_ALIAS_DRV][128]; // full path to resolve the alias into
|
||||
|
||||
static u8 sd_keyy[NUM_ALIAS_DRV][16] __attribute__((aligned(4))); // key Y belonging to alias drive
|
||||
|
||||
int alias_num (const TCHAR* path) {
|
||||
int num = -1;
|
||||
for (u32 i = 0; i < NUM_ALIAS_DRV; i++) {
|
||||
if (!alias_drv[i]) continue;
|
||||
if ((path[0] == alias_drv[i]) && (path[1] == ':')) {
|
||||
num = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
void dealias_path (TCHAR* alias, const TCHAR* path) {
|
||||
int num = alias_num(path);
|
||||
u32 p_offs = (path[2] == '/' && ((path[3] == '/') || (path[3] == '\0'))) ? 3 : 2;
|
||||
if (num >= 0) // set alias (alias is assumed to be 256 byte!)
|
||||
snprintf(alias, 256, "%s%s", alias_path[num], path + p_offs);
|
||||
else snprintf(alias, 256, "%s", path);
|
||||
}
|
||||
|
||||
FilCryptInfo* fx_find_cryptinfo(FIL* fptr) {
|
||||
FilCryptInfo* info = NULL;
|
||||
|
||||
for (u32 i = 0; i < NUM_FILCRYPTINFO; i++) {
|
||||
if (!info && !filcrypt[i].fptr) // use first free
|
||||
info = &filcrypt[i];
|
||||
if (fptr == filcrypt[i].fptr) {
|
||||
info = &filcrypt[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
FRESULT fx_decrypt_dsiware (FIL* fp, void* buff, FSIZE_t ofs, UINT len) {
|
||||
const u32 mode = AES_CNT_TITLEKEY_DECRYPT_MODE;
|
||||
const u32 num_tbl = sizeof(TadContentTable) / sizeof(u32);
|
||||
const FSIZE_t ofs0 = f_tell(fp);
|
||||
|
||||
u8 __attribute__((aligned(16))) iv[AES_BLOCK_SIZE];
|
||||
u32 tbl[num_tbl];
|
||||
u8 hdr[TAD_HEADER_LEN];
|
||||
|
||||
FRESULT res;
|
||||
UINT br;
|
||||
|
||||
|
||||
// read and decrypt header
|
||||
if ((res = f_lseek(fp, TAD_HEADER_OFFSET)) != FR_OK) return res;
|
||||
if ((res = f_read(fp, hdr, TAD_HEADER_LEN, &br)) != FR_OK) return res;
|
||||
if (br != TAD_HEADER_LEN) return FR_DENIED;
|
||||
memcpy(iv, hdr + TAD_HEADER_LEN - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
|
||||
cbc_decrypt(hdr, hdr, sizeof(TadHeader) / AES_BLOCK_SIZE, mode, iv);
|
||||
|
||||
// setup the table
|
||||
if (BuildTadContentTable(tbl, hdr) != 0) return FR_DENIED;
|
||||
if (tbl[num_tbl-1] > f_size(fp)) return FR_DENIED; // obviously missing data
|
||||
|
||||
|
||||
// process sections
|
||||
u32 sct_start = 0;
|
||||
u32 sct_end = 0;
|
||||
for (u32 i = 0; i < num_tbl; i++, sct_start = sct_end) {
|
||||
sct_end = tbl[i];
|
||||
if (sct_start == sct_end) continue; // nothing in section
|
||||
if ((ofs + len <= sct_start) || (ofs >= sct_end)) continue; // section not in data
|
||||
|
||||
const u32 crypt_end = sct_end - (AES_BLOCK_SIZE * 2);
|
||||
const u32 data_end = min(crypt_end, ofs + len);
|
||||
u32 data_pos = max(ofs, sct_start);
|
||||
if (ofs >= crypt_end) continue; // nothing to do
|
||||
|
||||
if ((sct_start < ofs) || (sct_end > ofs + len)) { // incomplete section, ugh
|
||||
u8 __attribute__((aligned(16))) block[AES_BLOCK_SIZE];
|
||||
|
||||
// load iv0
|
||||
FSIZE_t block0_ofs = data_pos - (data_pos % AES_BLOCK_SIZE);
|
||||
FSIZE_t iv0_ofs = ((block0_ofs > sct_start) ? block0_ofs : sct_end) - AES_BLOCK_SIZE;
|
||||
if ((res = f_lseek(fp, iv0_ofs)) != FR_OK) return res;
|
||||
if ((res = f_read(fp, iv, AES_BLOCK_SIZE, &br)) != FR_OK) return res;
|
||||
|
||||
// load and decrypt block0 (if misaligned)
|
||||
if (data_pos % AES_BLOCK_SIZE) {
|
||||
if ((res = f_lseek(fp, block0_ofs)) != FR_OK) return res;
|
||||
if ((res = f_read(fp, block, AES_BLOCK_SIZE, &br)) != FR_OK) return res;
|
||||
cbc_decrypt(block, block, 1, mode, iv);
|
||||
data_pos = min(block0_ofs + AES_BLOCK_SIZE, data_end);
|
||||
memcpy(buff, block + (ofs - block0_ofs), data_pos - ofs);
|
||||
}
|
||||
|
||||
// decrypt blocks in between
|
||||
u32 num_blocks = (data_end - data_pos) / AES_BLOCK_SIZE;
|
||||
if (num_blocks) {
|
||||
u8* blocks = (u8*) buff + (data_pos - ofs);
|
||||
cbc_decrypt(blocks, blocks, num_blocks, mode, iv);
|
||||
data_pos += num_blocks * AES_BLOCK_SIZE;
|
||||
}
|
||||
|
||||
// decrypt last block
|
||||
if (data_pos < data_end) {
|
||||
u8* lbuff = (u8*) buff + (data_pos - ofs);
|
||||
// memcpy(block, lbuff, data_end - data_pos); // <--- this should work, but it doesn't
|
||||
if ((res = f_lseek(fp, data_pos)) != FR_OK) return res;
|
||||
if ((res = f_read(fp, block, AES_BLOCK_SIZE, &br)) != FR_OK) return res;
|
||||
cbc_decrypt(block, block, 1, mode, iv);
|
||||
memcpy(lbuff, block, data_end - data_pos);
|
||||
data_pos = data_end;
|
||||
}
|
||||
} else { // complete section (thank god for these!)
|
||||
u8* blocks = (u8*) buff + (sct_start - ofs);
|
||||
u8* iv0 = (u8*) buff + (sct_end - ofs) - AES_BLOCK_SIZE;
|
||||
u32 num_blocks = (crypt_end - sct_start) / AES_BLOCK_SIZE;
|
||||
memcpy(iv, iv0, AES_BLOCK_SIZE);
|
||||
cbc_decrypt(blocks, blocks, num_blocks, mode, iv);
|
||||
}
|
||||
}
|
||||
|
||||
return f_lseek(fp, ofs0);
|
||||
}
|
||||
|
||||
FRESULT fx_open (FIL* fp, const TCHAR* path, BYTE mode) {
|
||||
int num = alias_num(path);
|
||||
FilCryptInfo* info = fx_find_cryptinfo(fp);
|
||||
if (info) info->fptr = NULL;
|
||||
|
||||
if (info && (num >= 0)) {
|
||||
// DSIWare Export, mark with the magic number
|
||||
if (strncmp(path + 2, "/" DSIWARE_MAGIC, 1 + 16) == 0) {
|
||||
memcpy(info->ctr, DSIWARE_MAGIC, 16);
|
||||
} else {
|
||||
// get AES counter, see: http://www.3dbrew.org/wiki/Extdata#Encryption
|
||||
// path is the part of the full path after //Nintendo 3DS/<ID0>/<ID1>
|
||||
u8 hashstr[256] __attribute__((aligned(4)));
|
||||
u8 sha256sum[32];
|
||||
u32 plen = 0;
|
||||
// poor man's ASCII -> UTF-16 / uppercase -> lowercase
|
||||
for (plen = 0; plen < 128; plen++) {
|
||||
u8 symbol = path[2 + plen];
|
||||
if ((symbol >= 'A') && (symbol <= 'Z')) symbol += ('a' - 'A');
|
||||
hashstr[2*plen] = symbol;
|
||||
hashstr[2*plen+1] = 0;
|
||||
if (symbol == 0) break;
|
||||
}
|
||||
sha_quick(sha256sum, hashstr, (plen + 1) * 2, SHA256_MODE);
|
||||
for (u32 i = 0; i < 16; i++)
|
||||
info->ctr[i] = sha256sum[i] ^ sha256sum[i+16];
|
||||
}
|
||||
// copy over key, FIL pointer
|
||||
memcpy(info->keyy, sd_keyy[num], 16);
|
||||
info->fptr = fp;
|
||||
}
|
||||
|
||||
return fa_open(fp, path, mode);
|
||||
}
|
||||
|
||||
FRESULT fx_read (FIL* fp, void* buff, UINT btr, UINT* br) {
|
||||
FilCryptInfo* info = fx_find_cryptinfo(fp);
|
||||
FSIZE_t off = f_tell(fp);
|
||||
FRESULT res = f_read(fp, buff, btr, br);
|
||||
if (info && info->fptr) {
|
||||
setup_aeskeyY(0x34, info->keyy);
|
||||
use_aeskey(0x34);
|
||||
if (memcmp(info->ctr, DSIWARE_MAGIC, 16) == 0) fx_decrypt_dsiware(fp, buff, off, btr);
|
||||
else ctr_decrypt_byte(buff, buff, btr, off, AES_CNT_CTRNAND_MODE, info->ctr);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
FRESULT fx_write (FIL* fp, const void* buff, UINT btw, UINT* bw) {
|
||||
FilCryptInfo* info = fx_find_cryptinfo(fp);
|
||||
FSIZE_t off = f_tell(fp);
|
||||
FRESULT res = FR_OK;
|
||||
|
||||
if (info && info->fptr) {
|
||||
if (memcmp(info->ctr, DSIWARE_MAGIC, 16) == 0) return FR_DENIED;
|
||||
void* crypt_buff = (void*) malloc(min(btw, STD_BUFFER_SIZE));
|
||||
if (!crypt_buff) return FR_DENIED;
|
||||
|
||||
setup_aeskeyY(0x34, info->keyy);
|
||||
use_aeskey(0x34);
|
||||
*bw = 0;
|
||||
for (UINT p = 0; (p < btw) && (res == FR_OK); p += STD_BUFFER_SIZE) {
|
||||
UINT pcount = min(STD_BUFFER_SIZE, (btw - p));
|
||||
UINT bwl = 0;
|
||||
memcpy(crypt_buff, (u8*) buff + p, pcount);
|
||||
ctr_decrypt_byte(crypt_buff, crypt_buff, pcount, off + p, AES_CNT_CTRNAND_MODE, info->ctr);
|
||||
res = f_write(fp, (const void*) crypt_buff, pcount, &bwl);
|
||||
*bw += bwl;
|
||||
}
|
||||
|
||||
free(crypt_buff);
|
||||
} else res = f_write(fp, buff, btw, bw);
|
||||
return res;
|
||||
}
|
||||
|
||||
FRESULT fx_close (FIL* fp) {
|
||||
FilCryptInfo* info = fx_find_cryptinfo(fp);
|
||||
if (info) memset(info, 0, sizeof(FilCryptInfo));
|
||||
return f_close(fp);
|
||||
}
|
||||
|
||||
FRESULT fa_open (FIL* fp, const TCHAR* path, BYTE mode) {
|
||||
TCHAR alias[256];
|
||||
dealias_path(alias, path);
|
||||
return f_open(fp, alias, mode);
|
||||
}
|
||||
|
||||
FRESULT fa_opendir (DIR* dp, const TCHAR* path) {
|
||||
TCHAR alias[256];
|
||||
dealias_path(alias, path);
|
||||
return f_opendir(dp, alias);
|
||||
}
|
||||
|
||||
FRESULT fa_mkdir (const TCHAR* path) {
|
||||
TCHAR alias[256];
|
||||
dealias_path(alias, path);
|
||||
return f_mkdir(alias);
|
||||
}
|
||||
|
||||
FRESULT fa_stat (const TCHAR* path, FILINFO* fno) {
|
||||
TCHAR alias[256];
|
||||
dealias_path(alias, path);
|
||||
return f_stat(alias, fno);
|
||||
}
|
||||
|
||||
FRESULT fa_unlink (const TCHAR* path) {
|
||||
TCHAR alias[256];
|
||||
dealias_path(alias, path);
|
||||
return f_unlink(alias);
|
||||
}
|
||||
|
||||
// special functions for access of virtual NAND SD drives
|
||||
bool SetupNandSdDrive(const char* path, const char* sd_path, const char* movable, int num) {
|
||||
char alias[128];
|
||||
|
||||
// initial checks
|
||||
if ((num >= NUM_ALIAS_DRV) || (num < 0)) return false;
|
||||
alias_drv[num] = 0;
|
||||
if (!sd_path || !movable || !path) return true;
|
||||
|
||||
// grab the key Y from movable.sed
|
||||
UINT bytes_read = 0;
|
||||
FIL file;
|
||||
if (f_open(&file, movable, FA_READ | FA_OPEN_EXISTING) != FR_OK)
|
||||
return false;
|
||||
f_lseek(&file, 0x110);
|
||||
if ((f_read(&file, sd_keyy[num], 0x10, &bytes_read) != FR_OK) || (bytes_read != 0x10)) {
|
||||
f_close(&file);
|
||||
return false;
|
||||
}
|
||||
f_close(&file);
|
||||
|
||||
// build the alias path (id0)
|
||||
u32 sha256sum[8];
|
||||
sha_quick(sha256sum, sd_keyy[num], 0x10, SHA256_MODE);
|
||||
snprintf(alias, sizeof(alias), "%s/Nintendo 3DS/%08lX%08lX%08lX%08lX",
|
||||
sd_path, sha256sum[0], sha256sum[1], sha256sum[2], sha256sum[3]);
|
||||
|
||||
// find the alias path (id1)
|
||||
char* id1 = alias + strnlen(alias, 127);
|
||||
DIR pdir;
|
||||
FILINFO fno;
|
||||
if (f_opendir(&pdir, alias) != FR_OK)
|
||||
return false;
|
||||
(id1++)[0] = '/';
|
||||
*id1 = '\0';
|
||||
while (f_readdir(&pdir, &fno) == FR_OK) {
|
||||
if (fno.fname[0] == 0)
|
||||
break;
|
||||
if ((strnlen(fno.fname, 64) != 32) || !(fno.fattrib & AM_DIR))
|
||||
continue; // check for id1 directory
|
||||
strncpy(id1, fno.fname, 127 - (id1 - alias));
|
||||
break;
|
||||
}
|
||||
f_closedir(&pdir);
|
||||
if (!(*id1)) return false;
|
||||
|
||||
// create the alias drive
|
||||
return SetupAliasDrive(path, alias, num);
|
||||
}
|
||||
|
||||
bool SetupAliasDrive(const char* path, const char* alias, int num) {
|
||||
// initial checks
|
||||
if ((num >= NUM_ALIAS_DRV) || (num < 0)) return false;
|
||||
alias_drv[num] = 0;
|
||||
if (!alias || !path) return true;
|
||||
|
||||
// take over drive path and alias
|
||||
strncpy(alias_path[num], alias, 128);
|
||||
if (path[1] != ':') return false;
|
||||
alias_drv[num] = path[0];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CheckAliasDrive(const char* path) {
|
||||
int num = alias_num(path);
|
||||
return (num >= 0);
|
||||
}
|
@ -1,121 +0,0 @@
|
||||
#include "support.h"
|
||||
#include "fsutil.h" // only for file selector
|
||||
#include "vram0.h"
|
||||
#include "vff.h"
|
||||
|
||||
#define SUPPORT_FILE_PATHS "0:/gm9/support", "1:/gm9/support" // we also check the VRAM TAR first
|
||||
#define SUPPORT_DIR_PATHS "V:", "0:/gm9", "1:/gm9"
|
||||
|
||||
|
||||
bool CheckSupportFile(const char* fname)
|
||||
{
|
||||
// try VRAM0 first
|
||||
if (FindVTarFileInfo(fname, NULL))
|
||||
return true;
|
||||
|
||||
// try support file paths
|
||||
const char* base_paths[] = { SUPPORT_FILE_PATHS };
|
||||
for (u32 i = 0; i < countof(base_paths); i++) {
|
||||
char path[256];
|
||||
snprintf(path, sizeof(path), "%s/%s", base_paths[i], fname);
|
||||
if (fvx_stat(path, NULL) == FR_OK)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t LoadSupportFile(const char* fname, void* buffer, size_t max_len)
|
||||
{
|
||||
// try VRAM0 first
|
||||
u64 len64 = 0;
|
||||
void* data = FindVTarFileInfo(fname, &len64);
|
||||
if (data && len64 && (len64 < max_len)) {
|
||||
memcpy(buffer, data, len64);
|
||||
return (size_t) len64;
|
||||
}
|
||||
|
||||
// try support file paths
|
||||
const char* base_paths[] = { SUPPORT_FILE_PATHS };
|
||||
for (u32 i = 0; i < countof(base_paths); i++) {
|
||||
UINT len32;
|
||||
char path[256];
|
||||
snprintf(path, sizeof(path), "%s/%s", base_paths[i], fname);
|
||||
if (fvx_qread(path, buffer, 0, max_len, &len32) == FR_OK)
|
||||
return len32;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool SaveSupportFile(const char* fname, void* buffer, size_t len)
|
||||
{
|
||||
const char* base_paths[] = { SUPPORT_FILE_PATHS };
|
||||
int idx = -1;
|
||||
|
||||
// check for existing support file path
|
||||
for (u32 i = 0; (idx < 0) && (i < countof(base_paths)); i++) {
|
||||
if (fvx_stat(base_paths[i], NULL) == FR_OK)
|
||||
idx = i;
|
||||
}
|
||||
|
||||
// create path if required
|
||||
for (u32 i = 0; (idx < 0) && (i < countof(base_paths)); i++) {
|
||||
if (fvx_rmkdir(base_paths[i]) == FR_OK)
|
||||
idx = i;
|
||||
}
|
||||
|
||||
// write support file
|
||||
if (idx >= 0) {
|
||||
char path[256];
|
||||
snprintf(path, sizeof(path), "%s/%s", base_paths[idx], fname);
|
||||
fvx_unlink(path);
|
||||
if (fvx_qwrite(path, buffer, 0, len, NULL) == FR_OK)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SetAsSupportFile(const char* fname, const char* source)
|
||||
{
|
||||
u32 len = fvx_qsize(source);
|
||||
if (!len) return false;
|
||||
|
||||
void* buffer = malloc(len);
|
||||
if (!buffer) return false;
|
||||
|
||||
bool res = false;
|
||||
if (fvx_qread(source, buffer, 0, len, NULL) == FR_OK)
|
||||
res = SaveSupportFile(fname, buffer, len);
|
||||
free(buffer);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
bool GetSupportDir(char* path, const char* dname)
|
||||
{
|
||||
const char* base_paths[] = { SUPPORT_DIR_PATHS };
|
||||
for (u32 i = 0; i < countof(base_paths); i++) {
|
||||
FILINFO fno;
|
||||
snprintf(path, 256, "%s/%s", base_paths[i], dname);
|
||||
if ((fvx_stat(path, &fno) == FR_OK) && (fno.fattrib & AM_DIR))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CheckSupportDir(const char* dname)
|
||||
{
|
||||
char path[256];
|
||||
return GetSupportDir(path, dname);
|
||||
}
|
||||
|
||||
bool FileSelectorSupport(char* result, const char* text, const char* dname, const char* pattern)
|
||||
{
|
||||
char path[256];
|
||||
// result needs to be at least 256 bytes long for this to work!
|
||||
if (!GetSupportDir(path, dname)) return false;
|
||||
return FileSelector(result, text, path, pattern, HIDE_EXT, false);
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
// scripts / payloads dir names
|
||||
#define LANGUAGES_DIR "languages"
|
||||
#define SCRIPTS_DIR "scripts"
|
||||
#define LUASCRIPTS_DIR "luascripts"
|
||||
#define PAYLOADS_DIR "payloads"
|
||||
|
||||
bool CheckSupportFile(const char* fname);
|
||||
size_t LoadSupportFile(const char* fname, void* buffer, size_t max_len);
|
||||
bool SaveSupportFile(const char* fname, void* buffer, size_t len);
|
||||
bool SetAsSupportFile(const char* fname, const char* source);
|
||||
|
||||
bool GetSupportDir(char* path, const char* dname);
|
||||
bool CheckSupportDir(const char* fpath);
|
||||
bool FileSelectorSupport(char* result, const char* text, const char* dname, const char* pattern);
|
@ -1,23 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#define THREEDSX_MAGIC '3', 'D', 'S', 'X'
|
||||
#define THREEDSX_EXT_MAGIC THREEDSX_MAGIC, 0x2C, 0x00
|
||||
|
||||
|
||||
// see: http://3dbrew.org/wiki/3DSX_Format
|
||||
typedef struct {
|
||||
u8 magic[4]; // "3DSX"
|
||||
u16 size_hdr; // 0x2C with extended header
|
||||
u16 size_reloc_hdr; // 0x08 if existing
|
||||
u32 version; // should be zero
|
||||
u32 flags; // should be zero, too
|
||||
u32 size_code;
|
||||
u32 size_rodata;
|
||||
u32 size_data_bss;
|
||||
u32 size_bss;
|
||||
u32 offset_smdh;
|
||||
u32 size_smdh;
|
||||
u32 offset_romfs_lv3;
|
||||
} PACKED_STRUCT ThreedsxHeader;
|
@ -1,888 +0,0 @@
|
||||
#include "bdri.h"
|
||||
#include "vff.h"
|
||||
|
||||
#define FAT_ENTRY_SIZE 2 * sizeof(u32)
|
||||
#define REPLACE_SIZE_MISMATCH 2
|
||||
|
||||
#define getfatflag(uv) (((uv) & 0x80000000UL) != 0)
|
||||
#define getfatindex(uv) ((uv) & 0x7FFFFFFFUL)
|
||||
#define buildfatuv(index, flag) ((index) | ((flag) ? 0x80000000UL : 0))
|
||||
|
||||
typedef struct {
|
||||
char magic[4]; // "BDRI"
|
||||
u32 version; // == 0x30000
|
||||
u64 info_offset; // == 0x20
|
||||
u64 image_size; // in blocks; and including the pre-header
|
||||
u32 image_block_size;
|
||||
u8 padding1[4];
|
||||
u8 unknown[4];
|
||||
u32 data_block_size;
|
||||
u64 dht_offset;
|
||||
u32 dht_bucket_count;
|
||||
u8 padding2[4];
|
||||
u64 fht_offset;
|
||||
u32 fht_bucket_count;
|
||||
u8 padding3[4];
|
||||
u64 fat_offset;
|
||||
u32 fat_entry_count; // exculdes 0th entry
|
||||
u8 padding4[4];
|
||||
u64 data_offset;
|
||||
u32 data_block_count; // == fat_entry_count
|
||||
u8 padding5[4];
|
||||
u32 det_start_block;
|
||||
u32 det_block_count;
|
||||
u32 max_dir_count;
|
||||
u8 padding6[4];
|
||||
u32 fet_start_block;
|
||||
u32 fet_block_count;
|
||||
u32 max_file_count;
|
||||
u8 padding7[4];
|
||||
} __attribute__((packed)) BDRIFsHeader;
|
||||
|
||||
typedef struct {
|
||||
char magic[8]; // varies based on media type and importdb vs titledb
|
||||
u8 reserved[0x78];
|
||||
BDRIFsHeader fs_header;
|
||||
} __attribute__((packed)) TitleDBPreHeader;
|
||||
|
||||
typedef struct {
|
||||
char magic[4]; // "TICK"
|
||||
u32 unknown1; // usually (assuming always) == 1
|
||||
u32 unknown2;
|
||||
u32 unknown3;
|
||||
BDRIFsHeader fs_header;
|
||||
} __attribute__((packed)) TickDBPreHeader;
|
||||
|
||||
typedef struct {
|
||||
u32 parent_index;
|
||||
u8 title_id[8];
|
||||
u32 next_sibling_index;
|
||||
u8 padding1[4];
|
||||
u32 start_block_index;
|
||||
u64 size; // in bytes
|
||||
u8 padding2[8];
|
||||
u32 hash_bucket_next_index;
|
||||
} __attribute__((packed)) TdbFileEntry;
|
||||
|
||||
typedef struct {
|
||||
u32 total_entry_count;
|
||||
u32 max_entry_count; // == max_file_count + 1
|
||||
u8 padding[32];
|
||||
u32 next_dummy_index;
|
||||
} __attribute__((packed)) DummyFileEntry;
|
||||
|
||||
typedef struct {
|
||||
u32 unknown; // usually (assuming always) == 1
|
||||
u32 ticket_size; // == 0x350 == sizeof(Ticket)
|
||||
Ticket ticket;
|
||||
} __attribute__((packed, aligned(4))) TicketEntry;
|
||||
|
||||
static FIL* bdrifp;
|
||||
|
||||
static FRESULT BDRIRead(UINT ofs, UINT btr, void* buf) {
|
||||
if (bdrifp) {
|
||||
FRESULT res;
|
||||
UINT br;
|
||||
if ((fvx_tell(bdrifp) != ofs) &&
|
||||
(fvx_lseek(bdrifp, ofs) != FR_OK)) return FR_DENIED;
|
||||
res = fvx_read(bdrifp, buf, btr, &br);
|
||||
if ((res == FR_OK) && (br != btr)) res = FR_DENIED;
|
||||
return res;
|
||||
} else return FR_DENIED;
|
||||
}
|
||||
|
||||
static FRESULT BDRIWrite(UINT ofs, UINT btw, const void* buf) {
|
||||
if (bdrifp) {
|
||||
FRESULT res;
|
||||
UINT bw;
|
||||
if ((fvx_tell(bdrifp) != ofs) &&
|
||||
(fvx_lseek(bdrifp, ofs) != FR_OK)) return FR_DENIED;
|
||||
res = fvx_write(bdrifp, buf, btw, &bw);
|
||||
if ((res == FR_OK) && (bw != btw)) res = FR_DENIED;
|
||||
return res;
|
||||
} else return FR_DENIED;
|
||||
}
|
||||
|
||||
bool CheckDBMagic(const u8* pre_header, bool tickdb) {
|
||||
const TitleDBPreHeader* title = (TitleDBPreHeader*) pre_header;
|
||||
const TickDBPreHeader* tick = (TickDBPreHeader*) pre_header;
|
||||
|
||||
return (tickdb ? ((strncmp(tick->magic, "TICK", 4) == 0) && (tick->unknown1 == 1)) :
|
||||
((strcmp(title->magic, "NANDIDB") == 0) || (strcmp(title->magic, "NANDTDB") == 0) ||
|
||||
(strcmp(title->magic, "TEMPIDB") == 0) || (strcmp(title->magic, "TEMPTDB") == 0))) &&
|
||||
(strcmp((tickdb ? tick->fs_header : title->fs_header).magic, "BDRI") == 0) &&
|
||||
((tickdb ? tick->fs_header : title->fs_header).version == 0x30000);
|
||||
}
|
||||
|
||||
// This function was taken, with slight modification, from https://3dbrew.org/wiki/Inner_FAT
|
||||
static u32 GetHashBucket(const u8* tid, u32 parent_dir_index, u32 bucket_count) {
|
||||
u32 hash = parent_dir_index ^ 0x091A2B3C;
|
||||
for (u8 i = 0; i < 2; ++i) {
|
||||
hash = (hash >> 1) | (hash << 31);
|
||||
hash ^= (u32)tid[i * 4];
|
||||
hash ^= (u32)tid[i * 4 + 1] << 8;
|
||||
hash ^= (u32)tid[i * 4 + 2] << 16;
|
||||
hash ^= (u32)tid[i * 4 + 3] << 24;
|
||||
}
|
||||
return hash % bucket_count;
|
||||
}
|
||||
|
||||
static u32 GetBDRIEntrySize(const BDRIFsHeader* fs_header, const u32 fs_header_offset, const u8* title_id, u32* size) {
|
||||
if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count)) // Could be more thorough
|
||||
return 1;
|
||||
|
||||
const u32 data_offset = fs_header_offset + fs_header->data_offset;
|
||||
const u32 fet_offset = data_offset + fs_header->fet_start_block * fs_header->data_block_size;
|
||||
const u32 fht_offset = fs_header_offset + fs_header->fht_offset;
|
||||
|
||||
u32 index = 0;
|
||||
TdbFileEntry file_entry;
|
||||
u64 tid_be = getbe64(title_id);
|
||||
u8* title_id_be = (u8*) &tid_be;
|
||||
const u32 hash_bucket = GetHashBucket(title_id_be, 1, fs_header->fht_bucket_count);
|
||||
|
||||
if (BDRIRead(fht_offset + hash_bucket * sizeof(u32), sizeof(u32), &(file_entry.hash_bucket_next_index)) != FR_OK)
|
||||
return 1;
|
||||
|
||||
// Find the file entry for the tid specified, fail if it doesn't exist
|
||||
do {
|
||||
if (file_entry.hash_bucket_next_index == 0)
|
||||
return 1;
|
||||
|
||||
index = file_entry.hash_bucket_next_index;
|
||||
|
||||
if (BDRIRead(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
|
||||
return 1;
|
||||
} while (memcmp(title_id_be, file_entry.title_id, 8) != 0);
|
||||
|
||||
*size = file_entry.size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static u32 ReadBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offset, const u8* title_id, u8* entry, const u32 expected_size) {
|
||||
if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count)) // Could be more thorough
|
||||
return 1;
|
||||
|
||||
const u32 data_offset = fs_header_offset + fs_header->data_offset;
|
||||
const u32 fet_offset = data_offset + fs_header->fet_start_block * fs_header->data_block_size;
|
||||
const u32 fht_offset = fs_header_offset + fs_header->fht_offset;
|
||||
const u32 fat_offset = fs_header_offset + fs_header->fat_offset;
|
||||
|
||||
u32 index = 0;
|
||||
TdbFileEntry file_entry;
|
||||
u64 tid_be = getbe64(title_id);
|
||||
u8* title_id_be = (u8*) &tid_be;
|
||||
const u32 hash_bucket = GetHashBucket(title_id_be, 1, fs_header->fht_bucket_count);
|
||||
|
||||
if (BDRIRead(fht_offset + hash_bucket * sizeof(u32), sizeof(u32), &(file_entry.hash_bucket_next_index)) != FR_OK)
|
||||
return 1;
|
||||
|
||||
// Find the file entry for the tid specified, fail if it doesn't exist
|
||||
do {
|
||||
if (file_entry.hash_bucket_next_index == 0)
|
||||
return 1;
|
||||
|
||||
index = file_entry.hash_bucket_next_index;
|
||||
|
||||
if (BDRIRead(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
|
||||
return 1;
|
||||
} while (memcmp(title_id_be, file_entry.title_id, 8) != 0);
|
||||
|
||||
if (expected_size && (file_entry.size != expected_size))
|
||||
return 1;
|
||||
|
||||
index = file_entry.start_block_index + 1; // FAT entry index
|
||||
|
||||
u32 bytes_read = 0;
|
||||
u32 fat_entry[2];
|
||||
|
||||
while (bytes_read < file_entry.size) { // Read the full entry, walking the FAT node chain
|
||||
u32 read_start = index - 1; // Data region block index
|
||||
u32 read_count = 0;
|
||||
|
||||
if (BDRIRead(fat_offset + index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
if ((bytes_read == 0) && !getfatflag(fat_entry[0]))
|
||||
return 1;
|
||||
|
||||
u32 next_index = getfatindex(fat_entry[1]);
|
||||
|
||||
if (getfatflag(fat_entry[1])) { // Multi-entry node
|
||||
if (BDRIRead(fat_offset + (index + 1) * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
if (!getfatflag(fat_entry[0]) || getfatflag(fat_entry[1]) || (getfatindex(fat_entry[0]) != index) || (getfatindex(fat_entry[0]) >= getfatindex(fat_entry[1])))
|
||||
return 1;
|
||||
|
||||
read_count = getfatindex(fat_entry[1]) + 1 - index;
|
||||
} else { // Single-entry node
|
||||
read_count = 1;
|
||||
}
|
||||
|
||||
index = next_index;
|
||||
|
||||
u32 btr = min(file_entry.size - bytes_read, read_count * fs_header->data_block_size);
|
||||
if (entry && (BDRIRead(data_offset + read_start * fs_header->data_block_size, btr, entry + bytes_read) != FR_OK))
|
||||
return 1;
|
||||
|
||||
bytes_read += btr;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static u32 RemoveBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offset, const u8* title_id) {
|
||||
if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count)) // Could be more thorough
|
||||
return 1;
|
||||
|
||||
const u32 data_offset = fs_header_offset + fs_header->data_offset;
|
||||
const u32 det_offset = data_offset + fs_header->det_start_block * fs_header->data_block_size;
|
||||
const u32 fet_offset = data_offset + fs_header->fet_start_block * fs_header->data_block_size;
|
||||
const u32 fht_offset = fs_header_offset + fs_header->fht_offset;
|
||||
const u32 fat_offset = fs_header_offset + fs_header->fat_offset;
|
||||
|
||||
u32 index = 0, previous_index = 0;
|
||||
TdbFileEntry file_entry;
|
||||
u64 tid_be = getbe64(title_id);
|
||||
u8* title_id_be = (u8*) &tid_be;
|
||||
|
||||
// Read the index of the first file entry from the directory entry table
|
||||
if (BDRIRead(det_offset + 0x2C, sizeof(u32), &(file_entry.next_sibling_index)) != FR_OK)
|
||||
return 1;
|
||||
|
||||
// Find the file entry for the tid specified, fail if it doesn't exist
|
||||
do {
|
||||
previous_index = index;
|
||||
index = file_entry.next_sibling_index;
|
||||
|
||||
if (index == 0)
|
||||
return 1;
|
||||
|
||||
if (BDRIRead(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
|
||||
return 1;
|
||||
} while (memcmp(title_id_be, file_entry.title_id, 8) != 0);
|
||||
|
||||
DummyFileEntry dummy_entry;
|
||||
|
||||
// Read the 0th entry in the FET, which is always a dummy entry
|
||||
if (BDRIRead(fet_offset, sizeof(DummyFileEntry), &dummy_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
if (dummy_entry.max_entry_count != fs_header->max_file_count + 1)
|
||||
return 1;
|
||||
|
||||
if ((BDRIWrite(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &dummy_entry) != FR_OK) ||
|
||||
(BDRIWrite(fet_offset + 0x28, sizeof(u32), &index) != FR_OK) ||
|
||||
(BDRIWrite((previous_index == 0) ? det_offset + 0x2C : fet_offset + previous_index * sizeof(TdbFileEntry) + 0xC, sizeof(u32), &(file_entry.next_sibling_index)) != FR_OK))
|
||||
return 1;
|
||||
|
||||
const u32 hash_bucket = GetHashBucket(file_entry.title_id, file_entry.parent_index, fs_header->fht_bucket_count);
|
||||
u32 index_hash = 0;
|
||||
|
||||
if (BDRIRead(fht_offset + hash_bucket * sizeof(u32), sizeof(u32), &index_hash) != FR_OK)
|
||||
return 1;
|
||||
|
||||
if (index_hash == index) {
|
||||
if (BDRIWrite(fht_offset + hash_bucket * sizeof(u32), sizeof(u32), &(file_entry.hash_bucket_next_index)) != FR_OK)
|
||||
return 1;
|
||||
} else {
|
||||
do {
|
||||
if (index_hash == 0) // This shouldn't happen if the entry was properly added
|
||||
break;
|
||||
|
||||
if (BDRIRead(fet_offset + index_hash * sizeof(TdbFileEntry) + 0x28, sizeof(u32), &index_hash) != FR_OK)
|
||||
return 1;
|
||||
} while (index_hash != index);
|
||||
|
||||
if ((index_hash != 0) && BDRIWrite(fet_offset + index_hash * sizeof(TdbFileEntry) + 0x28, sizeof(u32), &(file_entry.hash_bucket_next_index)) != FR_OK)
|
||||
return 1;
|
||||
}
|
||||
|
||||
u32 fat_entry[2];
|
||||
|
||||
if (BDRIRead(fat_offset, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
if (getfatflag(fat_entry[1]) || (fat_entry[0] != 0))
|
||||
return 1;
|
||||
|
||||
u32 next_free_index = getfatindex(fat_entry[1]), fat_index = file_entry.start_block_index + 1;
|
||||
|
||||
if (BDRIWrite(fat_offset + sizeof(u32), sizeof(u32), &fat_index) != FR_OK)
|
||||
return 1;
|
||||
|
||||
fat_entry[1] = fat_index;
|
||||
|
||||
do {
|
||||
fat_index = getfatindex(fat_entry[1]);
|
||||
|
||||
if (BDRIRead(fat_offset + FAT_ENTRY_SIZE * fat_index, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
} while (getfatindex(fat_entry[1]) != 0);
|
||||
|
||||
fat_entry[1] |= next_free_index;
|
||||
|
||||
if ((BDRIWrite(fat_offset + fat_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK) ||
|
||||
(BDRIRead(fat_offset + next_free_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK))
|
||||
return 1;
|
||||
|
||||
fat_entry[0] = buildfatuv(fat_index, false);
|
||||
|
||||
if (BDRIWrite(fat_offset + next_free_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static u32 AddBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offset, const u8* title_id, const u8* entry, const u32 size, bool replace) {
|
||||
if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count)) // Could be more thorough
|
||||
return 1;
|
||||
|
||||
if (!entry || !size)
|
||||
return 1;
|
||||
|
||||
const u32 data_offset = fs_header_offset + fs_header->data_offset;
|
||||
const u32 det_offset = data_offset + fs_header->det_start_block * fs_header->data_block_size;
|
||||
const u32 fet_offset = data_offset + fs_header->fet_start_block * fs_header->data_block_size;
|
||||
const u32 fht_offset = fs_header_offset + fs_header->fht_offset;
|
||||
const u32 fat_offset = fs_header_offset + fs_header->fat_offset;
|
||||
const u32 size_blocks = (size / fs_header->data_block_size) + (((size % fs_header->data_block_size) == 0) ? 0 : 1);
|
||||
|
||||
u32 index = 0, max_index = 0;
|
||||
TdbFileEntry file_entry;
|
||||
u64 tid_be = getbe64(title_id);
|
||||
u8* title_id_be = (u8*) &tid_be;
|
||||
bool do_replace = false;
|
||||
|
||||
// Read the index of the first file entry from the directory entry table
|
||||
if (BDRIRead(det_offset + 0x2C, sizeof(u32), &(file_entry.next_sibling_index)) != FR_OK)
|
||||
return 1;
|
||||
|
||||
// Try to find the file entry for the tid specified
|
||||
while (file_entry.next_sibling_index != 0) {
|
||||
index = file_entry.next_sibling_index;
|
||||
max_index = max(index, max_index);
|
||||
|
||||
if (BDRIRead(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
// If an entry for the tid already existed that is already the specified size and replace was specified, just replace the existing entry
|
||||
if (memcmp(title_id_be, file_entry.title_id, 8) == 0) {
|
||||
if (!replace) return 1;
|
||||
else if (file_entry.size != size) return REPLACE_SIZE_MISMATCH;
|
||||
else {
|
||||
do_replace = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
u32 fat_entry[2];
|
||||
u32 fat_index = 0;
|
||||
|
||||
if (!do_replace) {
|
||||
if (BDRIRead(fat_offset, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
if (getfatflag(fat_entry[1]) || (fat_entry[0] != 0))
|
||||
return 1;
|
||||
|
||||
u32 next_fat_index = getfatindex(fat_entry[1]), node_size = 0;
|
||||
|
||||
// Find contiguous free space in the FAT for the entry. Technically there could be a case of enough space existing, but not in a contiguous fasion, but this would never realistically happen
|
||||
do {
|
||||
if (next_fat_index == 0)
|
||||
return 1; // Reached the end of the free node chain without finding enough contiguous free space - this should never realistically happen
|
||||
|
||||
fat_index = next_fat_index;
|
||||
|
||||
if (BDRIRead(fat_offset + fat_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
next_fat_index = getfatindex(fat_entry[1]);
|
||||
|
||||
if (getfatflag(fat_entry[1])) { // Multi-entry node
|
||||
if (BDRIRead(fat_offset + (fat_index + 1) * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
if (!getfatflag(fat_entry[0]) || getfatflag(fat_entry[1]) || (getfatindex(fat_entry[0]) != fat_index) || (getfatindex(fat_entry[0]) >= getfatindex(fat_entry[1])))
|
||||
return 1;
|
||||
|
||||
node_size = getfatindex(fat_entry[1]) + 1 - fat_index;
|
||||
} else { // Single-entry node
|
||||
node_size = 1;
|
||||
}
|
||||
} while (node_size < size_blocks);
|
||||
|
||||
const bool shrink_free_node = node_size > size_blocks;
|
||||
|
||||
if (shrink_free_node) {
|
||||
if (BDRIRead(fat_offset + fat_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
if (node_size - size_blocks == 1)
|
||||
fat_entry[1] = buildfatuv(getfatindex(fat_entry[1]), false);
|
||||
|
||||
if (BDRIWrite(fat_offset + (fat_index + size_blocks) * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
if (node_size - size_blocks > 1) {
|
||||
if (BDRIRead(fat_offset + (fat_index + node_size - 1) * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
fat_entry[0] = buildfatuv(fat_index + size_blocks, true);
|
||||
|
||||
if ((BDRIWrite(fat_offset + (fat_index + node_size - 1) * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK) ||
|
||||
(BDRIWrite(fat_offset + (fat_index + size_blocks + 1) * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK))
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (BDRIRead(fat_offset + fat_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
const u32 previous_free_index = getfatindex(fat_entry[0]), next_free_index = getfatindex(fat_entry[1]);
|
||||
|
||||
fat_entry[0] = buildfatuv(0, true);
|
||||
fat_entry[1] = buildfatuv(0, size_blocks > 1);
|
||||
|
||||
if (BDRIWrite(fat_offset + fat_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
if (size_blocks > 1) {
|
||||
fat_entry[0] = buildfatuv(fat_index, true);
|
||||
fat_entry[1] = buildfatuv(fat_index + size_blocks - 1, false);
|
||||
|
||||
if ((BDRIWrite(fat_offset + (fat_index + size_blocks - 1) * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK) ||
|
||||
(BDRIWrite(fat_offset + (fat_index + 1) * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK))
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (next_free_index != 0) {
|
||||
if (BDRIRead(fat_offset + next_free_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
fat_entry[0] = buildfatuv(shrink_free_node ? fat_index + size_blocks : previous_free_index, (!shrink_free_node && (previous_free_index == 0)));
|
||||
|
||||
if (BDRIWrite(fat_offset + next_free_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (BDRIRead(fat_offset + previous_free_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
fat_entry[1] = buildfatuv(shrink_free_node ? fat_index + size_blocks : next_free_index, getfatflag(fat_entry[1]));
|
||||
|
||||
if (BDRIWrite(fat_offset + previous_free_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
} else fat_index = file_entry.start_block_index + 1;
|
||||
|
||||
u32 bytes_written = 0, fat_index_write = fat_index;
|
||||
|
||||
while (bytes_written < size) { // Write the full entry, walking the FAT node chain
|
||||
// Can't assume contiguity here, because we might be replacing an existing entry
|
||||
u32 write_start = fat_index_write - 1; // Data region block index
|
||||
u32 write_count = 0;
|
||||
|
||||
if (BDRIRead(fat_offset + fat_index_write * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
if ((bytes_written == 0) && !getfatflag(fat_entry[0]))
|
||||
return 1;
|
||||
|
||||
u32 next_index = getfatindex(fat_entry[1]);
|
||||
|
||||
if (getfatflag(fat_entry[1])) { // Multi-entry node
|
||||
if (BDRIRead(fat_offset + (fat_index_write + 1) * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
if (!getfatflag(fat_entry[0]) || getfatflag(fat_entry[1]) || (getfatindex(fat_entry[0]) != fat_index_write) || (getfatindex(fat_entry[0]) >= getfatindex(fat_entry[1])))
|
||||
return 1;
|
||||
|
||||
write_count = getfatindex(fat_entry[1]) + 1 - fat_index_write;
|
||||
} else { // Single-entry node
|
||||
write_count = 1;
|
||||
}
|
||||
|
||||
fat_index_write = next_index;
|
||||
|
||||
u32 btw = min(size - bytes_written, write_count * fs_header->data_block_size);
|
||||
if (BDRIWrite(data_offset + write_start * fs_header->data_block_size, btw, entry + bytes_written) != FR_OK)
|
||||
return 1;
|
||||
|
||||
bytes_written += btw;
|
||||
}
|
||||
|
||||
if (!do_replace) {
|
||||
DummyFileEntry dummy_entry;
|
||||
|
||||
// Read the 0th entry in the FET, which is always a dummy entry
|
||||
if (BDRIRead(fet_offset, sizeof(DummyFileEntry), &dummy_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
if (dummy_entry.max_entry_count != fs_header->max_file_count + 1)
|
||||
return 1;
|
||||
|
||||
if (dummy_entry.next_dummy_index == 0) { // If the 0th entry is the only dummy entry, make a new entry
|
||||
file_entry.next_sibling_index = max_index + 1;
|
||||
|
||||
dummy_entry.total_entry_count++;
|
||||
if (BDRIWrite(fet_offset, sizeof(u32), &(dummy_entry.total_entry_count)) != FR_OK)
|
||||
return 1;
|
||||
} else { // If there's at least one extraneous dummy entry, replace it
|
||||
file_entry.next_sibling_index = dummy_entry.next_dummy_index;
|
||||
|
||||
if ((BDRIRead(fet_offset + dummy_entry.next_dummy_index * sizeof(DummyFileEntry), sizeof(DummyFileEntry), &dummy_entry) != FR_OK) ||
|
||||
(BDRIWrite(fet_offset, sizeof(DummyFileEntry), &dummy_entry) != FR_OK))
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (BDRIWrite((index == 0) ? det_offset + 0x2C : fet_offset + index * sizeof(TdbFileEntry) + 0xC, sizeof(u32), &(file_entry.next_sibling_index)) != FR_OK)
|
||||
return 1;
|
||||
|
||||
index = file_entry.next_sibling_index;
|
||||
|
||||
const u32 hash_bucket = GetHashBucket(title_id_be, 1, fs_header->fht_bucket_count);
|
||||
u32 index_hash = 0;
|
||||
|
||||
if ((BDRIRead(fht_offset + hash_bucket * sizeof(u32), sizeof(u32), &index_hash) != FR_OK) ||
|
||||
(BDRIWrite(fht_offset + hash_bucket * sizeof(u32), sizeof(u32), &index) != FR_OK))
|
||||
return 1;
|
||||
|
||||
memset(&file_entry, 0, sizeof(TdbFileEntry));
|
||||
file_entry.parent_index = 1;
|
||||
memcpy(file_entry.title_id, title_id_be, 8);
|
||||
file_entry.start_block_index = fat_index - 1;
|
||||
file_entry.size = (u64) size;
|
||||
file_entry.hash_bucket_next_index = index_hash;
|
||||
|
||||
if (BDRIWrite(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static u32 GetNumBDRIEntries(const BDRIFsHeader* fs_header, const u32 fs_header_offset) {
|
||||
if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count)) // Could be more thorough
|
||||
return 0;
|
||||
|
||||
const u32 data_offset = fs_header_offset + fs_header->data_offset;
|
||||
const u32 det_offset = data_offset + fs_header->det_start_block * fs_header->data_block_size;
|
||||
const u32 fet_offset = data_offset + fs_header->fet_start_block * fs_header->data_block_size;
|
||||
|
||||
u32 num_entries = 0;
|
||||
TdbFileEntry file_entry;
|
||||
|
||||
// Read the index of the first file entry from the directory entry table
|
||||
if (BDRIRead(det_offset + 0x2C, sizeof(u32), &(file_entry.next_sibling_index)) != FR_OK)
|
||||
return 0;
|
||||
|
||||
while (file_entry.next_sibling_index != 0) {
|
||||
num_entries++;
|
||||
if (BDRIRead(fet_offset + file_entry.next_sibling_index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return num_entries;
|
||||
}
|
||||
|
||||
static u32 ListBDRIEntryTitleIDs(const BDRIFsHeader* fs_header, const u32 fs_header_offset, u8* title_ids, u32 max_title_ids) {
|
||||
if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count))
|
||||
return 0;
|
||||
|
||||
const u32 data_offset = fs_header_offset + fs_header->data_offset;
|
||||
const u32 det_offset = data_offset + fs_header->det_start_block * fs_header->data_block_size;
|
||||
const u32 fet_offset = data_offset + fs_header->fet_start_block * fs_header->data_block_size;
|
||||
|
||||
u32 num_entries = 0;
|
||||
TdbFileEntry file_entry;
|
||||
|
||||
for (u32 i = 0; i < max_title_ids; i++)
|
||||
memset(title_ids, 0, max_title_ids * 8);
|
||||
|
||||
// Read the index of the first file entry from the directory entry table
|
||||
if (BDRIRead(det_offset + 0x2C, sizeof(u32), &(file_entry.next_sibling_index)) != FR_OK)
|
||||
return 1;
|
||||
|
||||
while ((file_entry.next_sibling_index != 0) && (num_entries < max_title_ids)) {
|
||||
if (BDRIRead(fet_offset + file_entry.next_sibling_index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
|
||||
return 1;
|
||||
|
||||
u64 tid_be = getbe64(file_entry.title_id);
|
||||
memcpy(title_ids + num_entries * 8, (u8*) &tid_be, 8);
|
||||
|
||||
num_entries++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 GetNumTitleInfoEntries(const char* path) {
|
||||
FIL file;
|
||||
TitleDBPreHeader pre_header;
|
||||
|
||||
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
|
||||
return 0;
|
||||
|
||||
bdrifp = &file;
|
||||
|
||||
if ((BDRIRead(0, sizeof(TitleDBPreHeader), &pre_header) != FR_OK) ||
|
||||
!CheckDBMagic((u8*) &pre_header, false)) {
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 num = GetNumBDRIEntries(&(pre_header.fs_header), sizeof(TitleDBPreHeader) - sizeof(BDRIFsHeader));
|
||||
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return num;
|
||||
}
|
||||
|
||||
u32 GetNumTickets(const char* path) {
|
||||
FIL file;
|
||||
TickDBPreHeader pre_header;
|
||||
|
||||
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
|
||||
return 0;
|
||||
|
||||
bdrifp = &file;
|
||||
|
||||
if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) ||
|
||||
!CheckDBMagic((u8*) &pre_header, true)) {
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 num = GetNumBDRIEntries(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader));
|
||||
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return num;
|
||||
}
|
||||
|
||||
u32 ListTitleInfoEntryTitleIDs(const char* path, u8* title_ids, u32 max_title_ids) {
|
||||
FIL file;
|
||||
TitleDBPreHeader pre_header;
|
||||
|
||||
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
|
||||
return 1;
|
||||
|
||||
bdrifp = &file;
|
||||
|
||||
if ((BDRIRead(0, sizeof(TitleDBPreHeader), &pre_header) != FR_OK) ||
|
||||
!CheckDBMagic((u8*) &pre_header, false) ||
|
||||
(ListBDRIEntryTitleIDs(&(pre_header.fs_header), sizeof(TitleDBPreHeader) - sizeof(BDRIFsHeader), title_ids, max_title_ids) != 0)) {
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 ListTicketTitleIDs(const char* path, u8* title_ids, u32 max_title_ids) {
|
||||
FIL file;
|
||||
TickDBPreHeader pre_header;
|
||||
|
||||
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
|
||||
return 1;
|
||||
|
||||
bdrifp = &file;
|
||||
|
||||
if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) ||
|
||||
!CheckDBMagic((u8*) &pre_header, true) ||
|
||||
(ListBDRIEntryTitleIDs(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_ids, max_title_ids) != 0)) {
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 ReadTitleInfoEntryFromDB(const char* path, const u8* title_id, TitleInfoEntry* tie) {
|
||||
FIL file;
|
||||
TitleDBPreHeader pre_header;
|
||||
|
||||
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
|
||||
return 1;
|
||||
|
||||
bdrifp = &file;
|
||||
|
||||
if ((BDRIRead(0, sizeof(TitleDBPreHeader), &pre_header) != FR_OK) ||
|
||||
!CheckDBMagic((u8*) &pre_header, false) ||
|
||||
(ReadBDRIEntry(&(pre_header.fs_header), sizeof(TitleDBPreHeader) - sizeof(BDRIFsHeader), title_id, (u8*) tie,
|
||||
sizeof(TitleInfoEntry)) != 0)) {
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 ReadTicketFromDB(const char* path, const u8* title_id, Ticket** ticket) {
|
||||
FIL file;
|
||||
TickDBPreHeader pre_header;
|
||||
TicketEntry* te = NULL;
|
||||
u32 entry_size;
|
||||
|
||||
*ticket = NULL;
|
||||
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
|
||||
return 1;
|
||||
|
||||
bdrifp = &file;
|
||||
|
||||
if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) ||
|
||||
!CheckDBMagic((u8*) &pre_header, true) ||
|
||||
(GetBDRIEntrySize(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id, &entry_size) != 0) ||
|
||||
entry_size < sizeof(TicketEntry) + 0x14 ||
|
||||
(te = (TicketEntry*)malloc(entry_size), te == NULL) ||
|
||||
(ReadBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id, (u8*) te,
|
||||
entry_size) != 0)) {
|
||||
free(te); // if allocated
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
|
||||
if (te->ticket_size != GetTicketSize(&te->ticket)) {
|
||||
free(te);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ticket) {
|
||||
u32 size = te->ticket_size;
|
||||
memmove(te, &te->ticket, size); // recycle this memory, instead of allocating another
|
||||
Ticket* tik = realloc(te, size);
|
||||
if(!tik) tik = (Ticket*)te;
|
||||
*ticket = tik;
|
||||
return 0;
|
||||
}
|
||||
|
||||
free(te);
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 RemoveTitleInfoEntryFromDB(const char* path, const u8* title_id) {
|
||||
FIL file;
|
||||
TitleDBPreHeader pre_header;
|
||||
|
||||
if (fvx_open(&file, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING) != FR_OK)
|
||||
return 1;
|
||||
|
||||
bdrifp = &file;
|
||||
|
||||
if ((BDRIRead(0, sizeof(TitleDBPreHeader), &pre_header) != FR_OK) ||
|
||||
!CheckDBMagic((u8*) &pre_header, false) ||
|
||||
(RemoveBDRIEntry(&(pre_header.fs_header), sizeof(TitleDBPreHeader) - sizeof(BDRIFsHeader), title_id) != 0)) {
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 RemoveTicketFromDB(const char* path, const u8* title_id) {
|
||||
FIL file;
|
||||
TickDBPreHeader pre_header;
|
||||
|
||||
if (fvx_open(&file, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING) != FR_OK)
|
||||
return 1;
|
||||
|
||||
bdrifp = &file;
|
||||
|
||||
if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) ||
|
||||
!CheckDBMagic((u8*) &pre_header, true) ||
|
||||
(RemoveBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id) != 0)) {
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
fvx_close(&file);
|
||||
bdrifp = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 AddTitleInfoEntryToDB(const char* path, const u8* title_id, const TitleInfoEntry* tie, bool replace) {
|
||||
FIL file;
|
||||
TitleDBPreHeader pre_header;
|
||||
|
||||
if (fvx_open(&file, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING) != FR_OK)
|
||||
return 1;
|
||||
|
||||
bdrifp = &file;
|
||||
|
||||
if ((BDRIRead(0, sizeof(TitleDBPreHeader), &pre_header) != FR_OK) ||
|
||||
!CheckDBMagic((u8*) &pre_header, false) ||
|
||||
(AddBDRIEntry(&(pre_header.fs_header), sizeof(TitleDBPreHeader) - sizeof(BDRIFsHeader), title_id,
|
||||
(const u8*) tie, sizeof(TitleInfoEntry), replace) != 0)) {
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 AddTicketToDB(const char* path, const u8* title_id, const Ticket* ticket, bool replace) {
|
||||
FIL file;
|
||||
TickDBPreHeader pre_header;
|
||||
u32 entry_size = sizeof(TicketEntry) + GetTicketContentIndexSize(ticket);
|
||||
|
||||
TicketEntry* te = (TicketEntry*)malloc(entry_size);
|
||||
if (!te) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
te->unknown = 1;
|
||||
te->ticket_size = GetTicketSize(ticket);
|
||||
memcpy(&te->ticket, ticket, te->ticket_size);
|
||||
if (fvx_open(&file, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING) != FR_OK) {
|
||||
free(te);
|
||||
return 1;
|
||||
}
|
||||
|
||||
bdrifp = &file;
|
||||
|
||||
u32 add_bdri_res = 0;
|
||||
|
||||
if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) ||
|
||||
!CheckDBMagic((u8*) &pre_header, true) ||
|
||||
((add_bdri_res = AddBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id,
|
||||
(const u8*) te, entry_size, replace)) == 1) ||
|
||||
(add_bdri_res == REPLACE_SIZE_MISMATCH && ((RemoveBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id) != 0) ||
|
||||
(AddBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id,
|
||||
(const u8*) te, entry_size, replace) != 0)))) {
|
||||
free(te);
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
free(te);
|
||||
fvx_close(bdrifp);
|
||||
bdrifp = NULL;
|
||||
return 0;
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
#include "ticket.h"
|
||||
#include "tie.h"
|
||||
|
||||
// https://www.3dbrew.org/wiki/Inner_FAT
|
||||
|
||||
u32 GetNumTitleInfoEntries(const char* path);
|
||||
u32 GetNumTickets(const char* path);
|
||||
u32 ListTitleInfoEntryTitleIDs(const char* path, u8* title_ids, u32 max_title_ids);
|
||||
u32 ListTicketTitleIDs(const char* path, u8* title_ids, u32 max_title_ids);
|
||||
u32 ReadTitleInfoEntryFromDB(const char* path, const u8* title_id, TitleInfoEntry* tie);
|
||||
u32 ReadTicketFromDB(const char* path, const u8* title_id, Ticket** ticket);
|
||||
u32 RemoveTitleInfoEntryFromDB(const char* path, const u8* title_id);
|
||||
u32 RemoveTicketFromDB(const char* path, const u8* title_id);
|
||||
u32 AddTitleInfoEntryToDB(const char* path, const u8* title_id, const TitleInfoEntry* tie, bool replace);
|
||||
u32 AddTicketToDB(const char* path, const u8* title_id, const Ticket* ticket, bool replace);
|
@ -1,686 +0,0 @@
|
||||
/*
|
||||
* This file is part of GodMode9
|
||||
* Copyright (C) 2019 Wolfvak
|
||||
*
|
||||
* 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 2 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/>.
|
||||
*/
|
||||
|
||||
#include <libgen.h>
|
||||
|
||||
#include "language.h"
|
||||
#include "common.h"
|
||||
#include "timer.h"
|
||||
#include "crc32.h"
|
||||
#include "fs.h"
|
||||
#include "ui.h"
|
||||
|
||||
#include "hid.h"
|
||||
#include "bps.h"
|
||||
|
||||
#define BEAT_VLIBUFSZ (8)
|
||||
#define BEAT_MAXPATH (256)
|
||||
#define BEAT_FILEBUFSZ (256 * 1024)
|
||||
|
||||
#define BEAT_RANGE(c, i) ((c)->ranges[1][i] - (c)->ranges[0][i])
|
||||
#define BEAT_UPDATEDELAYMS (1000 / 4)
|
||||
|
||||
#define BEAT_ABSPOS(c, i) ((c)->foff[i] + (c)->ranges[0][i])
|
||||
|
||||
#define BEAT_READONLY (FA_READ | FA_OPEN_EXISTING)
|
||||
#define BEAT_RWCREATE (FA_READ | FA_WRITE | FA_CREATE_ALWAYS)
|
||||
|
||||
static u32 progress_refcnt = 0;
|
||||
static u64 progress_timer = 0;
|
||||
|
||||
static size_t fs_size(const char *path)
|
||||
{
|
||||
FILINFO fno;
|
||||
FRESULT res = fvx_stat(path, &fno);
|
||||
if (res != FR_OK) return 0;
|
||||
return fno.fsize;
|
||||
}
|
||||
|
||||
static const char *basepath(const char *path)
|
||||
{
|
||||
const char *ret = path + strlen(path);
|
||||
while((--ret) > path) {
|
||||
if (*ret == '/') break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Possible error codes */
|
||||
enum {
|
||||
BEAT_OK = 0,
|
||||
BEAT_EOAL,
|
||||
BEAT_ABORTED,
|
||||
BEAT_IO_ERROR,
|
||||
BEAT_OVERFLOW,
|
||||
BEAT_BADPATCH,
|
||||
BEAT_BADINPUT,
|
||||
BEAT_BADOUTPUT,
|
||||
BEAT_BADCHKSUM,
|
||||
BEAT_PATCH_EXPECT,
|
||||
BEAT_OUT_OF_MEMORY,
|
||||
};
|
||||
|
||||
/* State machine actions */
|
||||
enum {
|
||||
BPS_SOURCEREAD = 0,
|
||||
BPS_TARGETREAD = 1,
|
||||
BPS_SOURCECOPY = 2,
|
||||
BPS_TARGETCOPY = 3
|
||||
};
|
||||
|
||||
enum {
|
||||
BPM_CREATEPATH = 0,
|
||||
BPM_CREATEFILE = 1,
|
||||
BPM_MODIFYFILE = 2,
|
||||
BPM_MIRRORFILE = 3
|
||||
};
|
||||
|
||||
/* File handles used within the Beat state */
|
||||
enum {
|
||||
BEAT_PF = 0, // patch file
|
||||
BEAT_IF, // input file
|
||||
BEAT_OF, // output file
|
||||
BEAT_FILENUM,
|
||||
};
|
||||
|
||||
static const u8 bps_signature[] = { 'B', 'P', 'S', '1' };
|
||||
static const u8 bps_chksumoffs[BEAT_FILENUM] = {
|
||||
[BEAT_PF] = 4, [BEAT_OF] = 8, [BEAT_IF] = 12,
|
||||
};
|
||||
static const u8 bpm_signature[] = { 'B', 'P', 'M', '1' };
|
||||
|
||||
/** BEAT STATE STORAGE */
|
||||
typedef struct {
|
||||
u8 *copybuf;
|
||||
size_t foff[BEAT_FILENUM], eoal_offset;
|
||||
size_t ranges[2][BEAT_FILENUM];
|
||||
u32 ocrc; // Output crc
|
||||
|
||||
union {
|
||||
struct { // BPS exclusive fields
|
||||
u32 xocrc; // Expected output crc
|
||||
size_t source_relative, target_relative;
|
||||
};
|
||||
struct { // BPM exclusive fields
|
||||
const char *bpm_path, *source_dir, *target_dir;
|
||||
};
|
||||
};
|
||||
char processing[BEAT_MAXPATH];
|
||||
FIL file[BEAT_FILENUM];
|
||||
} BEAT_Context;
|
||||
|
||||
typedef int (*BEAT_Action)(BEAT_Context*, u32);
|
||||
|
||||
static bool BEAT_UpdateProgress(const BEAT_Context *ctx)
|
||||
{ // only updates progress for the parent patch, so the embedded BPS wont be displayed
|
||||
u64 tmr;
|
||||
if (progress_refcnt > 2) bkpt; // nope, bug out
|
||||
|
||||
tmr = timer_msec(progress_timer);
|
||||
if (CheckButton(BUTTON_B)) return false; // check for an abort situation
|
||||
if (progress_refcnt != 1) return true; // only show the first level progress
|
||||
if (tmr < BEAT_UPDATEDELAYMS) return true; // give it some time
|
||||
|
||||
progress_timer = timer_start();
|
||||
ShowProgress(ctx->foff[BEAT_PF], BEAT_RANGE(ctx, BEAT_PF), ctx->processing);
|
||||
return true;
|
||||
}
|
||||
|
||||
static const char *BEAT_ErrString(int error)
|
||||
{ // Get an error description string
|
||||
switch(error) {
|
||||
case BEAT_OK: return STR_BEAT_NO_ERROR;
|
||||
case BEAT_EOAL: return STR_BEAT_END_OF_ACTION_LIST;
|
||||
case BEAT_ABORTED: return STR_BEAT_ABORTED_BY_USER;
|
||||
case BEAT_IO_ERROR: return STR_BEAT_FAILED_TO_READ_WRITE_FILE;
|
||||
case BEAT_OVERFLOW: return STR_BEAT_ATTEMPTED_TO_WRITE_BEYOND_EOF;
|
||||
case BEAT_BADPATCH: return STR_BEAT_INVALID_PATCH_FILE;
|
||||
case BEAT_BADINPUT: return STR_BEAT_INVALID_INPUT_FILE;
|
||||
case BEAT_BADOUTPUT: return STR_BEAT_OUTPUT_FILE_CHECKSUM_MISMATCH;
|
||||
case BEAT_BADCHKSUM: return STR_BEAT_FILE_CHECKSUM_FAILED;
|
||||
case BEAT_PATCH_EXPECT: return STR_BEAT_EXPECTED_MORE_PATCH_DATA;
|
||||
case BEAT_OUT_OF_MEMORY: return STR_BEAT_OUT_OF_MEMORY;
|
||||
default: return STR_BEAT_UNKNOWN_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
static int BEAT_Read(BEAT_Context *ctx, int id, void *out, size_t len, int fwd)
|
||||
{ // Read up to `len` bytes from the context file `id` to the `out` buffer
|
||||
UINT br;
|
||||
FRESULT res;
|
||||
if ((len + ctx->foff[id]) > BEAT_RANGE(ctx, id))
|
||||
return BEAT_OVERFLOW;
|
||||
|
||||
fvx_lseek(&ctx->file[id], BEAT_ABSPOS(ctx, id)); // ALWAYS use the state offset + start range
|
||||
ctx->foff[id] += len * fwd;
|
||||
res = fvx_read(&ctx->file[id], out, len, &br);
|
||||
return (res == FR_OK && br == len) ? BEAT_OK : BEAT_IO_ERROR;
|
||||
}
|
||||
|
||||
static int BEAT_WriteOut(BEAT_Context *ctx, const u8 *in, size_t len, int fwd)
|
||||
{ // Write `len` bytes from `in` to BEAT_OF, updates the output CRC
|
||||
UINT bw;
|
||||
FRESULT res;
|
||||
if ((len + ctx->foff[BEAT_OF]) > BEAT_RANGE(ctx, BEAT_OF))
|
||||
return BEAT_OVERFLOW;
|
||||
|
||||
// Blindly assume all writes will be done linearly
|
||||
ctx->ocrc = ~crc32_calculate(~ctx->ocrc, in, len);
|
||||
fvx_lseek(&ctx->file[BEAT_OF], BEAT_ABSPOS(ctx, BEAT_OF));
|
||||
ctx->foff[BEAT_OF] += len * fwd;
|
||||
res = fvx_write(&ctx->file[BEAT_OF], in, len, &bw);
|
||||
return (res == FR_OK && bw == len) ? BEAT_OK : BEAT_IO_ERROR;
|
||||
}
|
||||
|
||||
static void BEAT_SeekOff(BEAT_Context *ctx, int id, ssize_t offset)
|
||||
{ ctx->foff[id] += offset; } // Seek `offset` bytes forward
|
||||
static void BEAT_SeekAbs(BEAT_Context *ctx, int id, size_t pos)
|
||||
{ ctx->foff[id] = pos; } // Seek to absolute position `pos`
|
||||
|
||||
static int BEAT_NextVLI(BEAT_Context *ctx, u32 *vli)
|
||||
{ // Read the next VLI in the file, update the seek position
|
||||
int res;
|
||||
u32 ret = 0;
|
||||
u32 iter = 0;
|
||||
u8 vli_rdbuf[BEAT_VLIBUFSZ], *scan = vli_rdbuf;
|
||||
|
||||
res = BEAT_Read(ctx, BEAT_PF, vli_rdbuf, sizeof(vli_rdbuf), 0);
|
||||
if (res != BEAT_OK) return res;
|
||||
|
||||
while(scan < &vli_rdbuf[sizeof(vli_rdbuf)]) {
|
||||
u32 val = *(scan++);
|
||||
ret += (val & 0x7F) << iter;
|
||||
if (val & 0x80) break;
|
||||
iter += 7;
|
||||
ret += (u32)(1ULL << iter);
|
||||
}
|
||||
|
||||
// Seek forward only by the amount of used bytes
|
||||
BEAT_SeekOff(ctx, BEAT_PF, scan - vli_rdbuf);
|
||||
*vli = ret;
|
||||
return res;
|
||||
}
|
||||
|
||||
static s32 BEAT_DecodeSigned(u32 val) // Extract the signed number
|
||||
{ if (val&1) return -(val>>1); else return (val>>1); }
|
||||
|
||||
static int BEAT_RunActions(BEAT_Context *ctx, const BEAT_Action *acts)
|
||||
{ // Parses an action list and runs commands specified in `acts`
|
||||
u32 vli, len;
|
||||
int cmd, res = BEAT_OK;
|
||||
|
||||
while((res == BEAT_OK) &&
|
||||
(ctx->foff[BEAT_PF] < (BEAT_RANGE(ctx, BEAT_PF) - ctx->eoal_offset))) {
|
||||
res = BEAT_NextVLI(ctx, &vli); // get next action
|
||||
cmd = vli & 3;
|
||||
len = (vli >> 2) + 1;
|
||||
if (res != BEAT_OK) return res;
|
||||
|
||||
if (!BEAT_UpdateProgress(ctx)) return BEAT_ABORTED;
|
||||
|
||||
res = (acts[cmd])(ctx, len); // Execute next action
|
||||
if (res != BEAT_OK) return res; // Break on error or user abort
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static void BEAT_ReleaseCTX(BEAT_Context *ctx)
|
||||
{ // Release any resources associated to the context
|
||||
free(ctx->copybuf);
|
||||
for (int i = 0; i < BEAT_FILENUM; i++) {
|
||||
if (fvx_opened(&ctx->file[i])) fvx_close(&ctx->file[i]);
|
||||
}
|
||||
progress_refcnt--; // lol what even are atomics
|
||||
}
|
||||
|
||||
// BPS Specific functions
|
||||
/**
|
||||
Initialize the Beat File Structure
|
||||
- verifies checksums
|
||||
- performs further sanity checks
|
||||
- extracts initial info
|
||||
- leaves the file ready to begin state machine execution
|
||||
*/
|
||||
static int BPS_InitCTX_Advanced(BEAT_Context *ctx, const char *bps_path, const char *in_path, const char *out_path, size_t start, size_t end, bool do_chksum)
|
||||
{
|
||||
int res;
|
||||
u8 read_magic[4];
|
||||
u32 vli, in_sz, metaend_off;
|
||||
u32 chksum[BEAT_FILENUM], expected_chksum[BEAT_FILENUM];
|
||||
|
||||
// Clear stackbuf
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
ctx->eoal_offset = 12;
|
||||
|
||||
if (end == 0) {
|
||||
start = 0;
|
||||
end = fs_size(bps_path);
|
||||
}
|
||||
|
||||
if (do_chksum) // get BPS checksum
|
||||
chksum[BEAT_PF] = crc32_calculate_from_file(bps_path, start, end - start - 4);
|
||||
|
||||
strcpy(ctx->processing, basepath(bps_path));
|
||||
|
||||
// open all files
|
||||
fvx_open(&ctx->file[BEAT_PF], bps_path, BEAT_READONLY);
|
||||
ctx->ranges[0][BEAT_PF] = start;
|
||||
ctx->ranges[1][BEAT_PF] = end;
|
||||
|
||||
fvx_open(&ctx->file[BEAT_IF], in_path, BEAT_READONLY);
|
||||
ctx->ranges[0][BEAT_IF] = 0;
|
||||
ctx->ranges[1][BEAT_IF] = fs_size(in_path);
|
||||
|
||||
res = fvx_open(&ctx->file[BEAT_OF], out_path, BEAT_RWCREATE);
|
||||
if (res != FR_OK) return BEAT_IO_ERROR;
|
||||
|
||||
// Verify BPS1 header magic
|
||||
res = BEAT_Read(ctx, BEAT_PF, read_magic, sizeof(read_magic), 1);
|
||||
if (res != BEAT_OK) return BEAT_IO_ERROR;
|
||||
res = memcmp(read_magic, bps_signature, sizeof(bps_signature));
|
||||
if (res != 0) return BEAT_BADPATCH;
|
||||
|
||||
// Check input size
|
||||
res = BEAT_NextVLI(ctx, &in_sz);
|
||||
if (res != BEAT_OK) return res;
|
||||
if (ctx->ranges[1][BEAT_IF] != in_sz) return BEAT_BADINPUT;
|
||||
|
||||
// Get expected output size
|
||||
res = BEAT_NextVLI(ctx, &vli);
|
||||
if (res != BEAT_OK) return res;
|
||||
ctx->ranges[0][BEAT_OF] = 0;
|
||||
ctx->ranges[1][BEAT_OF] = vli;
|
||||
|
||||
// Get end of metadata offset
|
||||
res = BEAT_NextVLI(ctx, &metaend_off);
|
||||
if (res != BEAT_OK) return res;
|
||||
metaend_off += ctx->foff[BEAT_PF];
|
||||
|
||||
// Read checksums from BPS file
|
||||
for (int i = 0; i < BEAT_FILENUM; i++) {
|
||||
BEAT_SeekAbs(ctx, BEAT_PF, BEAT_RANGE(ctx, BEAT_PF) - bps_chksumoffs[i]);
|
||||
BEAT_Read(ctx, BEAT_PF, &expected_chksum[i], sizeof(u32), 0);
|
||||
}
|
||||
|
||||
if (do_chksum) { // Verify patch checksum
|
||||
if (chksum[BEAT_PF] != expected_chksum[BEAT_PF]) return BEAT_BADCHKSUM;
|
||||
}
|
||||
|
||||
// Initialize output checksums
|
||||
ctx->ocrc = 0;
|
||||
ctx->xocrc = expected_chksum[BEAT_OF];
|
||||
|
||||
// Allocate temporary block copy buffer
|
||||
ctx->copybuf = malloc(BEAT_FILEBUFSZ);
|
||||
if (ctx->copybuf == NULL) return BEAT_OUT_OF_MEMORY;
|
||||
|
||||
// Seek back to the start of action stream / end of metadata
|
||||
BEAT_SeekAbs(ctx, BEAT_PF, metaend_off);
|
||||
progress_refcnt++;
|
||||
return BEAT_OK;
|
||||
}
|
||||
|
||||
static int BPS_InitCTX(BEAT_Context *ctx, const char *bps, const char *in, const char *out)
|
||||
{ return BPS_InitCTX_Advanced(ctx, bps, in, out, 0, 0, true); }
|
||||
|
||||
/*
|
||||
Generic helper function to copy from `src_id` to BEAT_OF
|
||||
Used by SourceRead, TargetRead and CreateFile
|
||||
*/
|
||||
static int BEAT_BlkCopy(BEAT_Context *ctx, int src_id, u32 len)
|
||||
{
|
||||
while(len > 0) {
|
||||
ssize_t blksz = min(len, BEAT_FILEBUFSZ);
|
||||
int res = BEAT_Read(ctx, src_id, ctx->copybuf, blksz, 1);
|
||||
if (res != BEAT_OK) return res;
|
||||
|
||||
res = BEAT_WriteOut(ctx, ctx->copybuf, blksz, 1);
|
||||
if (res != BEAT_OK) return res;
|
||||
|
||||
if (!BEAT_UpdateProgress(ctx)) return BEAT_ABORTED;
|
||||
|
||||
len -= blksz;
|
||||
}
|
||||
return BEAT_OK;
|
||||
}
|
||||
|
||||
static int BPS_SourceRead(BEAT_Context *ctx, u32 len)
|
||||
{ // This command copies bytes from the source file to the target file
|
||||
BEAT_SeekAbs(ctx, BEAT_IF, ctx->foff[BEAT_OF]);
|
||||
return BEAT_BlkCopy(ctx, BEAT_IF, len);
|
||||
}
|
||||
|
||||
/*
|
||||
[...] the actual data is not available to the patch applier,
|
||||
so it is stored directly inside the patch.
|
||||
*/
|
||||
static int BPS_TargetRead(BEAT_Context *ctx, u32 len)
|
||||
{
|
||||
return BEAT_BlkCopy(ctx, BEAT_PF, len);
|
||||
}
|
||||
|
||||
/*
|
||||
An offset is supplied to seek the sourceRelativeOffset to the desired
|
||||
location, and then data is copied from said offset to the target file
|
||||
*/
|
||||
static int BPS_SourceCopy(BEAT_Context *ctx, u32 len)
|
||||
{
|
||||
int res;
|
||||
u32 vli;
|
||||
s32 offset;
|
||||
|
||||
res = BEAT_NextVLI(ctx, &vli);
|
||||
if (res != BEAT_OK) return res;
|
||||
|
||||
offset = BEAT_DecodeSigned(vli);
|
||||
BEAT_SeekAbs(ctx, BEAT_IF, ctx->source_relative + offset);
|
||||
ctx->source_relative += offset + len;
|
||||
|
||||
return BEAT_BlkCopy(ctx, BEAT_IF, len);
|
||||
}
|
||||
|
||||
/* This command treats all of the data that has already been written to the target file as a dictionary */
|
||||
static int BPS_TargetCopy(BEAT_Context *ctx, u32 len)
|
||||
{ // the black sheep of the family, needs special care
|
||||
int res;
|
||||
s32 offset;
|
||||
u32 out_off, rel_off, vli;
|
||||
|
||||
res = BEAT_NextVLI(ctx, &vli);
|
||||
if (res != BEAT_OK) return res;
|
||||
|
||||
offset = BEAT_DecodeSigned(vli);
|
||||
out_off = ctx->foff[BEAT_OF];
|
||||
rel_off = ctx->target_relative + offset;
|
||||
if (rel_off > out_off) return BEAT_BADPATCH; // Illegal
|
||||
|
||||
while(len != 0) {
|
||||
u8 *remfill;
|
||||
ssize_t blksz, distance, remainder;
|
||||
|
||||
blksz = min(len, BEAT_FILEBUFSZ);
|
||||
distance = min((ssize_t)(out_off - rel_off), blksz);
|
||||
|
||||
BEAT_SeekAbs(ctx, BEAT_OF, rel_off);
|
||||
res = BEAT_Read(ctx, BEAT_OF, ctx->copybuf, distance, 0);
|
||||
if (res != BEAT_OK) return res;
|
||||
|
||||
remfill = ctx->copybuf + distance;
|
||||
remainder = blksz - distance;
|
||||
while(remainder > 0) { // fill the buffer with repeats
|
||||
ssize_t remblk = min(distance, remainder);
|
||||
memcpy(remfill, ctx->copybuf, remblk);
|
||||
remfill += remblk;
|
||||
remainder -= remblk;
|
||||
}
|
||||
|
||||
BEAT_SeekAbs(ctx, BEAT_OF, out_off);
|
||||
res = BEAT_WriteOut(ctx, ctx->copybuf, blksz, 0);
|
||||
if (res != BEAT_OK) return res;
|
||||
|
||||
if (!BEAT_UpdateProgress(ctx)) return BEAT_ABORTED;
|
||||
rel_off += blksz;
|
||||
out_off += blksz;
|
||||
len -= blksz;
|
||||
}
|
||||
|
||||
BEAT_SeekAbs(ctx, BEAT_OF, out_off);
|
||||
ctx->target_relative = rel_off;
|
||||
return BEAT_OK;
|
||||
}
|
||||
|
||||
static int BPS_RunActions(BEAT_Context *ctx)
|
||||
{
|
||||
static const BEAT_Action BPS_Actions[] = { // BPS action handlers
|
||||
[BPS_SOURCEREAD] = BPS_SourceRead,
|
||||
[BPS_TARGETREAD] = BPS_TargetRead,
|
||||
[BPS_SOURCECOPY] = BPS_SourceCopy,
|
||||
[BPS_TARGETCOPY] = BPS_TargetCopy
|
||||
};
|
||||
int res = BEAT_RunActions(ctx, BPS_Actions);
|
||||
if (res == BEAT_ABORTED) return BEAT_ABORTED;
|
||||
if (res == BEAT_EOAL) // Verify hashes
|
||||
return (ctx->ocrc == ctx->xocrc) ? BEAT_OK : BEAT_BADOUTPUT;
|
||||
return res; // some kind of error
|
||||
}
|
||||
|
||||
/***********************
|
||||
BPM Specific functions
|
||||
***********************/
|
||||
static int BPM_OpenFile(BEAT_Context *ctx, int id, const char *path, size_t max_sz)
|
||||
{
|
||||
FRESULT res;
|
||||
|
||||
if (fvx_opened(&ctx->file[id])) fvx_close(&ctx->file[id]);
|
||||
res = fvx_open(&ctx->file[id], path, max_sz ? BEAT_RWCREATE : BEAT_READONLY);
|
||||
if (res != FR_OK) return BEAT_IO_ERROR;
|
||||
|
||||
ctx->ranges[0][id] = 0;
|
||||
if (max_sz > 0) {
|
||||
ctx->ranges[1][id] = max_sz;
|
||||
} else {
|
||||
ctx->ranges[1][id] = f_size(&ctx->file[id]);
|
||||
}
|
||||
|
||||
// if a new file is opened it makes no sense to keep the old CRC
|
||||
// a single outfile wont be created from more than one infile (& patch)
|
||||
ctx->ocrc = 0;
|
||||
ctx->foff[id] = 0;
|
||||
return BEAT_OK;
|
||||
}
|
||||
|
||||
static int BPM_InitCTX(BEAT_Context *ctx, const char *bpm_path, const char *src_dir, const char *dst_dir)
|
||||
{
|
||||
int res;
|
||||
u32 metaend_off;
|
||||
u8 read_magic[4];
|
||||
u32 chksum, expected_chksum;
|
||||
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
|
||||
ctx->bpm_path = bpm_path;
|
||||
ctx->source_dir = src_dir;
|
||||
ctx->target_dir = dst_dir;
|
||||
ctx->eoal_offset = 4;
|
||||
|
||||
chksum = crc32_calculate_from_file(bpm_path, 0, fs_size(bpm_path) - 4);
|
||||
res = BPM_OpenFile(ctx, BEAT_PF, bpm_path, 0);
|
||||
if (res != BEAT_OK) return res;
|
||||
res = BEAT_Read(ctx, BEAT_PF, read_magic, sizeof(read_magic), 1);
|
||||
if (res != BEAT_OK) return res;
|
||||
res = memcmp(read_magic, bpm_signature, sizeof(bpm_signature));
|
||||
if (res != 0) return BEAT_BADPATCH;
|
||||
|
||||
// Get end of metadata offset
|
||||
res = BEAT_NextVLI(ctx, &metaend_off);
|
||||
if (res != BEAT_OK) return res;
|
||||
metaend_off += ctx->foff[BEAT_PF];
|
||||
|
||||
// Read checksums from BPS file
|
||||
BEAT_SeekAbs(ctx, BEAT_PF, BEAT_RANGE(ctx, BEAT_PF) - 4);
|
||||
res = BEAT_Read(ctx, BEAT_PF, &expected_chksum, sizeof(u32), 0);
|
||||
if (res != BEAT_OK) return res;
|
||||
if (expected_chksum != chksum) return BEAT_BADCHKSUM;
|
||||
|
||||
// Allocate temporary block copy buffer
|
||||
ctx->copybuf = malloc(BEAT_FILEBUFSZ);
|
||||
if (ctx->copybuf == NULL) return BEAT_OUT_OF_MEMORY;
|
||||
|
||||
// Seek back to the start of action stream / end of metadata
|
||||
BEAT_SeekAbs(ctx, BEAT_PF, metaend_off);
|
||||
progress_refcnt++;
|
||||
return BEAT_OK;
|
||||
}
|
||||
|
||||
static int BPM_NextPath(BEAT_Context *ctx, char *out, int name_len)
|
||||
{
|
||||
if (name_len >= BEAT_MAXPATH) return BEAT_BADPATCH;
|
||||
int res = BEAT_Read(ctx, BEAT_PF, out, name_len, 1);
|
||||
out[name_len] = '\0';
|
||||
if (res == BEAT_OK) {
|
||||
out[name_len] = '\0'; // make sure the buffer ends with a zero char
|
||||
strcpy(ctx->processing, out);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static int BPM_MakeRelativePaths(BEAT_Context *ctx, char *src, char *dst, int name_len)
|
||||
{
|
||||
char name[BEAT_MAXPATH];
|
||||
int res = BPM_NextPath(ctx, name, name_len);
|
||||
if (res != BEAT_OK) return res;
|
||||
if (src != NULL)
|
||||
if (snprintf(src, BEAT_MAXPATH, "%s/%s", ctx->source_dir, name) >= BEAT_MAXPATH) return BEAT_BADPATCH;
|
||||
if (dst != NULL)
|
||||
if (snprintf(dst, BEAT_MAXPATH, "%s/%s", ctx->target_dir, name) >= BEAT_MAXPATH) return BEAT_BADPATCH;
|
||||
return res;
|
||||
}
|
||||
|
||||
static int BPM_CreatePath(BEAT_Context *ctx, u32 name_len)
|
||||
{ // Create a directory
|
||||
char path[BEAT_MAXPATH];
|
||||
int res = BPM_MakeRelativePaths(ctx, NULL, path, name_len);
|
||||
if (res != BEAT_OK) return res;
|
||||
|
||||
res = fvx_mkdir(path);
|
||||
if (res != FR_OK && res != FR_EXIST) return BEAT_IO_ERROR;
|
||||
return BEAT_OK;
|
||||
}
|
||||
|
||||
static int BPM_CreateFile(BEAT_Context *ctx, u32 name_len)
|
||||
{ // Create a file and fill it with data provided in the BPM
|
||||
u32 file_sz;
|
||||
u32 checksum;
|
||||
char path[BEAT_MAXPATH];
|
||||
|
||||
int res = BPM_MakeRelativePaths(ctx, NULL, path, name_len);
|
||||
if (res != BEAT_OK) return res;
|
||||
|
||||
res = BEAT_NextVLI(ctx, &file_sz); // get new file size
|
||||
if (res != BEAT_OK) return res;
|
||||
res = BPM_OpenFile(ctx, BEAT_OF, path, file_sz); // open file as RW
|
||||
if (res != BEAT_OK) return res;
|
||||
res = BEAT_BlkCopy(ctx, BEAT_PF, file_sz); // copy data to new file
|
||||
if (res != BEAT_OK) return res;
|
||||
|
||||
res = BEAT_Read(ctx, BEAT_PF, &checksum, sizeof(u32), 1);
|
||||
if (res != BEAT_OK) return res;
|
||||
if (ctx->ocrc != checksum) return BEAT_BADOUTPUT; // get and check CRC32
|
||||
return BEAT_OK;
|
||||
}
|
||||
|
||||
static int BPM_ModifyFile(BEAT_Context *ctx, u32 name_len)
|
||||
{ // Apply a BPS patch
|
||||
u32 origin, bps_sz;
|
||||
BEAT_Context bps_context;
|
||||
char src[BEAT_MAXPATH], dst[BEAT_MAXPATH];
|
||||
|
||||
int res = BPM_MakeRelativePaths(ctx, src, dst, name_len);
|
||||
if (res != BEAT_OK) return res;
|
||||
|
||||
res = BEAT_NextVLI(ctx, &origin); // get dummy(?) origin value
|
||||
if (res != BEAT_OK) return res;
|
||||
res = BEAT_NextVLI(ctx, &bps_sz); // get embedded BPS size
|
||||
if (res != BEAT_OK) return res;
|
||||
|
||||
res = BPS_InitCTX_Advanced(
|
||||
&bps_context, ctx->bpm_path, src, dst,
|
||||
ctx->foff[BEAT_PF], ctx->foff[BEAT_PF] + bps_sz,
|
||||
false
|
||||
); // create a BPS context using the current ranges
|
||||
if (res == BEAT_OK) res = BPS_RunActions(&bps_context); // run if OK
|
||||
BEAT_ReleaseCTX(&bps_context);
|
||||
if (res != BEAT_OK) return res; // break off if there was an error
|
||||
|
||||
BEAT_SeekOff(ctx, BEAT_PF, bps_sz); // advance beyond the BPS
|
||||
return BEAT_OK;
|
||||
}
|
||||
|
||||
static int BPM_MirrorFile(BEAT_Context *ctx, u32 name_len)
|
||||
{ // Copy a file from source to target without any modifications
|
||||
u32 origin;
|
||||
u32 checksum;
|
||||
char src[BEAT_MAXPATH], dst[BEAT_MAXPATH];
|
||||
|
||||
int res = BPM_MakeRelativePaths(ctx, src, dst, name_len);
|
||||
if (res != BEAT_OK) return res;
|
||||
|
||||
// open source and destination files, read the origin dummy
|
||||
res = BPM_OpenFile(ctx, BEAT_IF, src, 0);
|
||||
if (res != BEAT_OK) return res;
|
||||
res = BPM_OpenFile(ctx, BEAT_OF, dst, ctx->ranges[1][BEAT_IF]);
|
||||
if (res != BEAT_OK) return res;
|
||||
res = BEAT_NextVLI(ctx, &origin);
|
||||
if (res != BEAT_OK) return res;
|
||||
|
||||
// copy straight from source to destination
|
||||
res = BEAT_BlkCopy(ctx, BEAT_IF, ctx->ranges[1][BEAT_IF]);
|
||||
if (res != BEAT_OK) return res;
|
||||
|
||||
res = BEAT_Read(ctx, BEAT_PF, &checksum, sizeof(u32), 1);
|
||||
if (res != BEAT_OK) return res;
|
||||
if (ctx->ocrc != checksum) return BEAT_BADOUTPUT; // verify checksum
|
||||
return BEAT_OK;
|
||||
}
|
||||
|
||||
static int BPM_RunActions(BEAT_Context *ctx)
|
||||
{
|
||||
static const BEAT_Action BPM_Actions[] = { // BPM Action handlers
|
||||
[BPM_CREATEPATH] = BPM_CreatePath,
|
||||
[BPM_CREATEFILE] = BPM_CreateFile,
|
||||
[BPM_MODIFYFILE] = BPM_ModifyFile,
|
||||
[BPM_MIRRORFILE] = BPM_MirrorFile
|
||||
};
|
||||
int res = BEAT_RunActions(ctx, BPM_Actions);
|
||||
if (res == BEAT_ABORTED) return BEAT_ABORTED;
|
||||
if (res == BEAT_EOAL) return BEAT_OK;
|
||||
return res;
|
||||
}
|
||||
|
||||
static int BEAT_Run(const char *p, const char *s, const char *d, bool bpm)
|
||||
{
|
||||
int res;
|
||||
BEAT_Context ctx;
|
||||
|
||||
progress_timer = timer_start();
|
||||
res = (bpm ? BPM_InitCTX : BPS_InitCTX)(&ctx, p, s, d);
|
||||
if (res != BEAT_OK) {
|
||||
ShowPrompt(false, bpm ? STR_FAILED_TO_INITIALIZE_BPM_FILE : STR_FAILED_TO_INITIALIZE_BPS_FILE, BEAT_ErrString(res));
|
||||
} else {
|
||||
res = (bpm ? BPM_RunActions : BPS_RunActions)(&ctx);
|
||||
switch(res) {
|
||||
case BEAT_OK:
|
||||
ShowPrompt(false, "%s", STR_PATCH_SUCCESSFULLY_APPLIED);
|
||||
break;
|
||||
case BEAT_ABORTED:
|
||||
ShowPrompt(false, "%s", STR_PATCHING_ABORTED_BY_USER);
|
||||
break;
|
||||
default:
|
||||
ShowPrompt(false, STR_FAILED_TO_RUN_PATCH, BEAT_ErrString(res));
|
||||
break;
|
||||
}
|
||||
}
|
||||
BEAT_ReleaseCTX(&ctx);
|
||||
return (res == BEAT_OK) ? 0 : 1;
|
||||
}
|
||||
|
||||
int ApplyBPSPatch(const char* modifyName, const char* sourceName, const char* targetName)
|
||||
{ return BEAT_Run(modifyName, sourceName, targetName, false); }
|
||||
int ApplyBPMPatch(const char* patchName, const char* sourcePath, const char* targetPath)
|
||||
{ return BEAT_Run(patchName, sourcePath, targetPath, true); }
|
@ -1,9 +0,0 @@
|
||||
// C port of byuu's \nall\beat\patch.hpp and \multi.hpp, which were released under GPLv3
|
||||
// https://github.com/eai04191/beat/blob/master/nall/beat/patch.hpp
|
||||
// https://github.com/eai04191/beat/blob/master/nall/beat/multi.hpp
|
||||
// Ported by Hyarion for use with VirtualFatFS
|
||||
|
||||
#pragma once
|
||||
|
||||
int ApplyBPSPatch(const char* modifyName, const char* sourceName, const char* targetName);
|
||||
int ApplyBPMPatch(const char* patchName, const char* sourcePath, const char* targetPath);
|
@ -1,816 +0,0 @@
|
||||
#include "cert.h"
|
||||
#include "disadiff.h"
|
||||
#include "rsa.h"
|
||||
|
||||
typedef struct {
|
||||
char magic[4]; // "CERT"
|
||||
u8 unk[4]; // afaik, always 0
|
||||
u8 used_size[4]; // size used after this header
|
||||
u8 garbage[4]; // literally garbage values
|
||||
} PACKED_STRUCT CertsDbPartitionHeader;
|
||||
|
||||
static inline void GetCertDBPath(char* path, bool emunand) {
|
||||
path[0] = emunand ? '4' : '1';
|
||||
strcpy(&path[1], ":/dbs/certs.db");
|
||||
}
|
||||
|
||||
#define CERT_RETAIL_CA3_IDENT BIT(0)
|
||||
#define CERT_RETAIL_XSc_IDENT BIT(1)
|
||||
#define CERT_RETAIL_CPb_IDENT BIT(2)
|
||||
#define CERT_DEV_CA4_IDENT BIT(3)
|
||||
#define CERT_DEV_XS9_IDENT BIT(4)
|
||||
#define CERT_DEV_CPa_IDENT BIT(5)
|
||||
#define CERT_NO_STORE_SPACE (0xFF)
|
||||
|
||||
static struct {
|
||||
u32 loaded_certs_flg;
|
||||
u8 retail_CA3_raw[CERT_RSA4096_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
|
||||
u8 retail_XSc_raw[CERT_RSA2048_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
|
||||
u8 retail_CPb_raw[CERT_RSA2048_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
|
||||
u8 dev_CA4_raw[CERT_RSA4096_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
|
||||
u8 dev_XS9_raw[CERT_RSA2048_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
|
||||
u8 dev_CPa_raw[CERT_RSA2048_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
|
||||
Certificate retail_CA3;
|
||||
Certificate retail_XSc;
|
||||
Certificate retail_CPb;
|
||||
Certificate dev_CA4;
|
||||
Certificate dev_XS9;
|
||||
Certificate dev_CPa;
|
||||
} _CommonCertsStorage = {
|
||||
0, // none loaded yet, ident defines used to say what's loaded
|
||||
{0}, {0}, {0}, {0}, {0}, {0}, // no data yet
|
||||
// cert structs pre-point already to raw certs
|
||||
{
|
||||
(CertificateSignature*)&_CommonCertsStorage.retail_CA3_raw[0],
|
||||
(CertificateBody*)&_CommonCertsStorage.retail_CA3_raw[CERT_RSA4096_SIG_SIZE]
|
||||
},
|
||||
{
|
||||
(CertificateSignature*)&_CommonCertsStorage.retail_XSc_raw[0],
|
||||
(CertificateBody*)&_CommonCertsStorage.retail_XSc_raw[CERT_RSA2048_SIG_SIZE]
|
||||
},
|
||||
{
|
||||
(CertificateSignature*)&_CommonCertsStorage.retail_CPb_raw[0],
|
||||
(CertificateBody*)&_CommonCertsStorage.retail_CPb_raw[CERT_RSA2048_SIG_SIZE]
|
||||
},
|
||||
{
|
||||
(CertificateSignature*)&_CommonCertsStorage.dev_CA4_raw[0],
|
||||
(CertificateBody*)&_CommonCertsStorage.dev_CA4_raw[CERT_RSA4096_SIG_SIZE]
|
||||
},
|
||||
{
|
||||
(CertificateSignature*)&_CommonCertsStorage.dev_XS9_raw[0],
|
||||
(CertificateBody*)&_CommonCertsStorage.dev_XS9_raw[CERT_RSA2048_SIG_SIZE]
|
||||
},
|
||||
{
|
||||
(CertificateSignature*)&_CommonCertsStorage.dev_CPa_raw[0],
|
||||
(CertificateBody*)&_CommonCertsStorage.dev_CPa_raw[CERT_RSA2048_SIG_SIZE]
|
||||
}
|
||||
};
|
||||
|
||||
static inline void _Certificate_CleanupImpl(Certificate* cert);
|
||||
|
||||
bool Certificate_IsValid(const Certificate* cert) {
|
||||
if (!cert || !cert->sig || !cert->data)
|
||||
return false;
|
||||
|
||||
u32 sig_type = getbe32(cert->sig->sig_type);
|
||||
if (sig_type < 0x10000 || sig_type > 0x10005)
|
||||
return false;
|
||||
|
||||
u32 keytype = getbe32(cert->data->keytype);
|
||||
if (keytype > 2)
|
||||
return false;
|
||||
|
||||
size_t issuer_len = strnlen(cert->data->issuer, 0x40);
|
||||
size_t name_len = strnlen(cert->data->name, 0x40);
|
||||
// if >= 0x40, cert can't fit as issuer for other objects later
|
||||
// since later objects using the certificate as their issuer will have them use it as certissuer-certname
|
||||
if (!issuer_len || !name_len || (issuer_len + name_len + 1) >= 0x40)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Certificate_IsRSA(const Certificate* cert) {
|
||||
if (!Certificate_IsValid(cert)) return false;
|
||||
if (getbe32(cert->data->keytype) >= 2) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Certificate_IsECC(const Certificate* cert) {
|
||||
if (!Certificate_IsValid(cert)) return false;
|
||||
if (getbe32(cert->data->keytype) != 2) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
u32 Certificate_GetSignatureSize(const Certificate* cert, u32* size) {
|
||||
if (!size || !Certificate_IsValid(cert)) return 1;
|
||||
|
||||
u32 sig_type = getbe32(cert->sig->sig_type);
|
||||
|
||||
if (sig_type == 0x10000 || sig_type == 0x10003)
|
||||
*size = 0x200;
|
||||
else if (sig_type == 0x10001 || sig_type == 0x10004)
|
||||
*size = 0x100;
|
||||
else if (sig_type == 0x10002 || sig_type == 0x10005)
|
||||
*size = 0x3C;
|
||||
else
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 Certificate_GetModulusSize(const Certificate* cert, u32* size) {
|
||||
if (!size || !Certificate_IsRSA(cert)) return 1;
|
||||
|
||||
u32 keytype = getbe32(cert->data->keytype);
|
||||
|
||||
if (keytype == 0)
|
||||
*size = 4096 / 8;
|
||||
else if (keytype == 1)
|
||||
*size = 2048 / 8;
|
||||
else return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 Certificate_GetModulus(const Certificate* cert, void* mod) {
|
||||
u32 size;
|
||||
if (!mod || Certificate_GetModulusSize(cert, &size)) return 1;
|
||||
|
||||
memcpy(mod, cert->data->pub_key_data, size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 Certificate_GetExponent(const Certificate* cert, void* exp) {
|
||||
u32 size;
|
||||
if (!exp || Certificate_GetModulusSize(cert, &size)) return 1;
|
||||
|
||||
memcpy(exp, &cert->data->pub_key_data[size], 4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 Certificate_GetEccSingleCoordinateSize(const Certificate* cert, u32* size) {
|
||||
if (!size || !Certificate_IsECC(cert)) return 1;
|
||||
|
||||
u32 keytype = getbe32(cert->data->keytype);
|
||||
|
||||
if (keytype == 2)
|
||||
*size = 0x3C / 2;
|
||||
else return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 Certificate_GetEccXY(const Certificate* cert, void* X, void* Y) {
|
||||
u32 size;
|
||||
if (!X || !Y || Certificate_GetEccSingleCoordinateSize(cert, &size)) return 1;
|
||||
|
||||
memcpy(X, cert->data->pub_key_data, size);
|
||||
memcpy(Y, &cert->data->pub_key_data[size], size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline u32 _Certificate_GetSignatureChunkSizeFromType(u32 sig_type) {
|
||||
if (sig_type == 0x10000 || sig_type == 0x10003)
|
||||
return CERT_RSA4096_SIG_SIZE;
|
||||
else if (sig_type == 0x10001 || sig_type == 0x10004)
|
||||
return CERT_RSA2048_SIG_SIZE;
|
||||
else if (sig_type == 0x10002 || sig_type == 0x10005)
|
||||
return CERT_ECC_SIG_SIZE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 Certificate_GetSignatureChunkSize(const Certificate* cert, u32* size) {
|
||||
if (!size || !Certificate_IsValid(cert)) return 1;
|
||||
|
||||
u32 _size = _Certificate_GetSignatureChunkSizeFromType(getbe32(cert->sig->sig_type));
|
||||
|
||||
if (_size == 0) return 1;
|
||||
|
||||
*size = _size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline u32 _Certificate_GetDataChunkSizeFromType(u32 keytype) {
|
||||
if (keytype == 0)
|
||||
return CERT_RSA4096_BODY_SIZE;
|
||||
else if (keytype == 1)
|
||||
return CERT_RSA2048_BODY_SIZE;
|
||||
else if (keytype == 2)
|
||||
return CERT_ECC_BODY_SIZE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 Certificate_GetDataChunkSize(const Certificate* cert, u32* size) {
|
||||
if (!size || !Certificate_IsValid(cert)) return 1;
|
||||
|
||||
u32 _size = _Certificate_GetDataChunkSizeFromType(getbe32(cert->data->keytype));
|
||||
|
||||
if (_size == 0) return 1;
|
||||
|
||||
*size = _size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 Certificate_GetFullSize(const Certificate* cert, u32* size) {
|
||||
if (!size || !Certificate_IsValid(cert)) return 1;
|
||||
|
||||
u32 sig_size = _Certificate_GetSignatureChunkSizeFromType(getbe32(cert->sig->sig_type));
|
||||
u32 data_size = _Certificate_GetDataChunkSizeFromType(getbe32(cert->data->keytype));
|
||||
|
||||
if (sig_size == 0 || data_size == 0)
|
||||
return 1;
|
||||
|
||||
*size = sig_size + data_size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline u32 _Certificate_KeytypeSignatureSize(u32 keytype) {
|
||||
if (keytype == 0)
|
||||
return 0x200;
|
||||
else if (keytype == 1)
|
||||
return 0x100;
|
||||
else if (keytype == 2)
|
||||
return 0x3C;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline u32 _Certificate_VerifyRSA4096(const Certificate* cert, const void* sig, const void* data, u32 data_size, bool sha256) {
|
||||
(void)cert; (void)sig; (void)data; (void)data_size; (void)sha256;
|
||||
return 2; // not implemented
|
||||
}
|
||||
|
||||
// noipa, to avoid form of inlining, cloning, etc, to avoid the extra stack usage when unneeded
|
||||
static __attribute__((noipa)) bool _Certificate_SetKey2048Misaligned(const Certificate* cert) {
|
||||
u32 mod[2048/8];
|
||||
u32 exp;
|
||||
|
||||
memcpy(mod, cert->data->pub_key_data, 2048/8);
|
||||
exp = getle32(&cert->data->pub_key_data[2048/8]);
|
||||
|
||||
return RSA_setKey2048(3, mod, exp);
|
||||
}
|
||||
|
||||
static inline u32 _Certificate_VerifyRSA2048(const Certificate* cert, const void* sig, const void* data, u32 data_size, bool sha256) {
|
||||
if (!sha256)
|
||||
return 2; // not implemented
|
||||
|
||||
int ret;
|
||||
|
||||
if (((u32)&cert->data->pub_key_data[0]) & 0x3)
|
||||
ret = !_Certificate_SetKey2048Misaligned(cert);
|
||||
else
|
||||
ret = !RSA_setKey2048(3, (const u32*)(const void*)&cert->data->pub_key_data[0], getle32(&cert->data->pub_key_data[2048/8]));
|
||||
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
return !RSA_verify2048(sig, data, data_size);
|
||||
}
|
||||
|
||||
static inline u32 _Certificate_VerifyECC(const Certificate* cert, const void* sig, const void* data, u32 data_size, bool sha256) {
|
||||
(void)cert; (void)sig; (void)data; (void)data_size; (void)sha256;
|
||||
return 2; // not implemented
|
||||
}
|
||||
|
||||
u32 Certificate_VerifySignatureBlock(const Certificate* cert, const void* sig, u32 sig_size, const void* data, u32 data_size, bool sha256) {
|
||||
if (!sig || !sig_size || (!data && data_size) || !Certificate_IsValid(cert))
|
||||
return 1;
|
||||
|
||||
u32 keytype = getbe32(cert->data->keytype);
|
||||
u32 _sig_size = _Certificate_KeytypeSignatureSize(keytype);
|
||||
|
||||
if (sig_size != _sig_size)
|
||||
return 1;
|
||||
|
||||
if (keytype == 0)
|
||||
return _Certificate_VerifyRSA4096(cert, sig, data, data_size, sha256);
|
||||
if (keytype == 1)
|
||||
return _Certificate_VerifyRSA2048(cert, sig, data, data_size, sha256);
|
||||
if (keytype == 2)
|
||||
return _Certificate_VerifyECC(cert, sig, data, data_size, sha256);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static inline void* _Certificate_SafeRealloc(void* ptr, size_t size, size_t oldsize) {
|
||||
void* new_ptr;
|
||||
size_t min_size = min(oldsize, size);
|
||||
|
||||
if ((u32)ptr >= (u32)&_CommonCertsStorage && (u32)ptr < (u32)&_CommonCertsStorage + sizeof(_CommonCertsStorage)) {
|
||||
new_ptr = malloc(size);
|
||||
if (new_ptr) memcpy(new_ptr, ptr, min_size);
|
||||
} else {
|
||||
new_ptr = realloc(ptr, size);
|
||||
}
|
||||
if (!new_ptr) return NULL;
|
||||
|
||||
memset(&((u8*)new_ptr)[min_size], 0, size - min_size);
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
// will reallocate memory for certificate signature and body to fit the max possible size.
|
||||
// will also allocate an empty object if Certificate is NULL initialized.
|
||||
// if certificate points to static storage, an allocated version will be created.
|
||||
// if function fails, the Certificate, even if previously NULL initialized, still has to be passed to cleaned up after use.
|
||||
u32 Certificate_MakeEditSafe(Certificate* cert) {
|
||||
if (!cert) return 1;
|
||||
bool isvalid = Certificate_IsValid(cert);
|
||||
if ((cert->sig || cert->data) && !isvalid) return 1;
|
||||
|
||||
Certificate cert_local;
|
||||
|
||||
u32 sig_size = isvalid ? _Certificate_GetSignatureChunkSizeFromType(getbe32(cert->sig->sig_type)) : 0;
|
||||
u32 data_size = isvalid ? _Certificate_GetDataChunkSizeFromType(getbe32(cert->data->keytype)) : 0;
|
||||
|
||||
if (isvalid && (sig_size == 0 || data_size == 0)) return 1;
|
||||
|
||||
cert_local.sig = _Certificate_SafeRealloc(cert->sig, CERT_RSA4096_SIG_SIZE, sig_size);
|
||||
if (!cert_local.sig) return 1;
|
||||
cert->sig = cert_local.sig;
|
||||
cert_local.data = _Certificate_SafeRealloc(cert->data, CERT_RSA4096_BODY_SIZE, data_size);
|
||||
if (!cert_local.data) return 1;
|
||||
cert->data = cert_local.data;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static u32 _Certificate_AllocCopyOutImpl(const Certificate* cert, Certificate* out_cert) {
|
||||
u32 sig_size = _Certificate_GetSignatureChunkSizeFromType(getbe32(cert->sig->sig_type));
|
||||
u32 data_size = _Certificate_GetDataChunkSizeFromType(getbe32(cert->data->keytype));
|
||||
|
||||
if (sig_size == 0 || data_size == 0)
|
||||
return 1;
|
||||
|
||||
out_cert->sig = (CertificateSignature*)malloc(sig_size);
|
||||
out_cert->data = (CertificateBody*)malloc(data_size);
|
||||
|
||||
if (!out_cert->sig || !out_cert->data) {
|
||||
_Certificate_CleanupImpl(out_cert);
|
||||
return 1;
|
||||
}
|
||||
|
||||
memcpy(out_cert->sig, cert->sig, sig_size);
|
||||
memcpy(out_cert->data, cert->data, data_size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 Certificate_AllocCopyOut(const Certificate* cert, Certificate* out_cert) {
|
||||
if (!out_cert || !Certificate_IsValid(cert)) return 1;
|
||||
|
||||
return _Certificate_AllocCopyOutImpl(cert, out_cert);
|
||||
}
|
||||
|
||||
static u32 _Certificate_RawCopyImpl(const Certificate* cert, void* raw) {
|
||||
u32 sig_size = _Certificate_GetSignatureChunkSizeFromType(getbe32(cert->sig->sig_type));
|
||||
u32 data_size = _Certificate_GetDataChunkSizeFromType(getbe32(cert->data->keytype));
|
||||
|
||||
if (sig_size == 0 || data_size == 0)
|
||||
return 1;
|
||||
|
||||
memcpy(raw, cert->sig, sig_size);
|
||||
memcpy(&((u8*)raw)[sig_size], cert->data, data_size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u32 Certificate_RawCopy(const Certificate* cert, void* raw) {
|
||||
if (!raw || !Certificate_IsValid(cert)) return 1;
|
||||
|
||||
return _Certificate_RawCopyImpl(cert, raw);
|
||||
}
|
||||
|
||||
// ptr free check, to not free if ptr is pointing to static storage!!
|
||||
static inline void _Certificate_SafeFree(void* ptr) {
|
||||
if ((u32)ptr >= (u32)&_CommonCertsStorage && (u32)ptr < (u32)&_CommonCertsStorage + sizeof(_CommonCertsStorage))
|
||||
return;
|
||||
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
static inline void _Certificate_CleanupImpl(Certificate* cert) {
|
||||
_Certificate_SafeFree(cert->sig);
|
||||
_Certificate_SafeFree(cert->data);
|
||||
cert->sig = NULL;
|
||||
cert->data = NULL;
|
||||
}
|
||||
|
||||
u32 Certificate_Cleanup(Certificate* cert) {
|
||||
if (!cert) return 1;
|
||||
|
||||
_Certificate_CleanupImpl(cert);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static u32 _Issuer_To_StorageIdent(const char* issuer) {
|
||||
if (strncmp(issuer, "Root-CA0000000", 14) != 0)
|
||||
return CERT_NO_STORE_SPACE;
|
||||
|
||||
if (issuer[14] == '3') { // retail
|
||||
if (issuer[15] == 0)
|
||||
return CERT_RETAIL_CA3_IDENT;
|
||||
if (issuer[15] != '-')
|
||||
return CERT_NO_STORE_SPACE;
|
||||
if (!strcmp(&issuer[16], "XS0000000c"))
|
||||
return CERT_RETAIL_XSc_IDENT;
|
||||
if (!strcmp(&issuer[16], "CP0000000b"))
|
||||
return CERT_RETAIL_CPb_IDENT;
|
||||
}
|
||||
|
||||
if (issuer[14] == '4') { // dev
|
||||
if (issuer[15] == 0)
|
||||
return CERT_DEV_CA4_IDENT;
|
||||
if (issuer[15] != '-')
|
||||
return CERT_NO_STORE_SPACE;
|
||||
if (!strcmp(&issuer[16], "XS00000009"))
|
||||
return CERT_DEV_XS9_IDENT;
|
||||
if (!strcmp(&issuer[16], "CP0000000a"))
|
||||
return CERT_DEV_CPa_IDENT;
|
||||
}
|
||||
|
||||
return CERT_NO_STORE_SPACE;
|
||||
}
|
||||
|
||||
static bool _LoadFromCertStorage(Certificate* cert, u32 ident) {
|
||||
if (ident == CERT_NO_STORE_SPACE)
|
||||
return false;
|
||||
|
||||
Certificate* _cert = NULL;
|
||||
|
||||
switch (ident) {
|
||||
case CERT_RETAIL_CA3_IDENT:
|
||||
if (_CommonCertsStorage.loaded_certs_flg & CERT_RETAIL_CA3_IDENT)
|
||||
_cert = &_CommonCertsStorage.retail_CA3;
|
||||
break;
|
||||
case CERT_RETAIL_XSc_IDENT:
|
||||
if (_CommonCertsStorage.loaded_certs_flg & CERT_RETAIL_XSc_IDENT)
|
||||
_cert = &_CommonCertsStorage.retail_XSc;
|
||||
break;
|
||||
case CERT_RETAIL_CPb_IDENT:
|
||||
if (_CommonCertsStorage.loaded_certs_flg & CERT_RETAIL_CPb_IDENT)
|
||||
_cert = &_CommonCertsStorage.retail_CPb;
|
||||
break;
|
||||
case CERT_DEV_CA4_IDENT:
|
||||
if (_CommonCertsStorage.loaded_certs_flg & CERT_DEV_CA4_IDENT)
|
||||
_cert = &_CommonCertsStorage.dev_CA4;
|
||||
break;
|
||||
case CERT_DEV_XS9_IDENT:
|
||||
if (_CommonCertsStorage.loaded_certs_flg & CERT_DEV_XS9_IDENT)
|
||||
_cert = &_CommonCertsStorage.dev_XS9;
|
||||
break;
|
||||
case CERT_DEV_CPa_IDENT:
|
||||
if (_CommonCertsStorage.loaded_certs_flg & CERT_DEV_CPa_IDENT)
|
||||
_cert = &_CommonCertsStorage.dev_CPa;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (!_cert)
|
||||
return false;
|
||||
|
||||
*cert = *_cert;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void _SaveToCertStorage(const Certificate* cert, u32 ident) {
|
||||
if (ident == CERT_NO_STORE_SPACE)
|
||||
return;
|
||||
|
||||
Certificate* _cert = NULL;
|
||||
u8* raw_space = NULL;
|
||||
u32 raw_size = 0;
|
||||
|
||||
switch (ident) {
|
||||
case CERT_RETAIL_CA3_IDENT:
|
||||
if (!(_CommonCertsStorage.loaded_certs_flg & CERT_RETAIL_CA3_IDENT)) {
|
||||
_cert = &_CommonCertsStorage.retail_CA3;
|
||||
raw_space = &_CommonCertsStorage.retail_CA3_raw[0];
|
||||
raw_size = sizeof(_CommonCertsStorage.retail_CA3_raw);
|
||||
}
|
||||
break;
|
||||
case CERT_RETAIL_XSc_IDENT:
|
||||
if (!(_CommonCertsStorage.loaded_certs_flg & CERT_RETAIL_XSc_IDENT)) {
|
||||
_cert = &_CommonCertsStorage.retail_XSc;
|
||||
raw_space = &_CommonCertsStorage.retail_XSc_raw[0];
|
||||
raw_size = sizeof(_CommonCertsStorage.retail_XSc_raw);
|
||||
}
|
||||
break;
|
||||
case CERT_RETAIL_CPb_IDENT:
|
||||
if (!(_CommonCertsStorage.loaded_certs_flg & CERT_RETAIL_CPb_IDENT)) {
|
||||
_cert = &_CommonCertsStorage.retail_CPb;
|
||||
raw_space = &_CommonCertsStorage.retail_CPb_raw[0];
|
||||
raw_size = sizeof(_CommonCertsStorage.retail_CPb_raw);
|
||||
}
|
||||
break;
|
||||
case CERT_DEV_CA4_IDENT:
|
||||
if (!(_CommonCertsStorage.loaded_certs_flg & CERT_DEV_CA4_IDENT)) {
|
||||
_cert = &_CommonCertsStorage.dev_CA4;
|
||||
raw_space = &_CommonCertsStorage.dev_CA4_raw[0];
|
||||
raw_size = sizeof(_CommonCertsStorage.dev_CA4_raw);
|
||||
}
|
||||
break;
|
||||
case CERT_DEV_XS9_IDENT:
|
||||
if (!(_CommonCertsStorage.loaded_certs_flg & CERT_DEV_XS9_IDENT)) {
|
||||
_cert = &_CommonCertsStorage.dev_XS9;
|
||||
raw_space = &_CommonCertsStorage.dev_XS9_raw[0];
|
||||
raw_size = sizeof(_CommonCertsStorage.dev_XS9_raw);
|
||||
}
|
||||
break;
|
||||
case CERT_DEV_CPa_IDENT:
|
||||
if (!(_CommonCertsStorage.loaded_certs_flg & CERT_DEV_CPa_IDENT)) {
|
||||
_cert = &_CommonCertsStorage.dev_CPa;
|
||||
raw_space = &_CommonCertsStorage.dev_CPa_raw[0];
|
||||
raw_size = sizeof(_CommonCertsStorage.dev_CPa_raw);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (!_cert || !raw_space || !raw_size)
|
||||
return;
|
||||
|
||||
u32 sig_size = _Certificate_GetSignatureChunkSizeFromType(getbe32(cert->sig->sig_type));
|
||||
u32 data_size = _Certificate_GetDataChunkSizeFromType(getbe32(cert->data->keytype));
|
||||
|
||||
if (sig_size == 0 || data_size == 0)
|
||||
return;
|
||||
|
||||
if (sig_size + data_size != raw_size)
|
||||
return;
|
||||
|
||||
if (!_Certificate_RawCopyImpl(cert, raw_space)) {
|
||||
_CommonCertsStorage.loaded_certs_flg |= ident;
|
||||
}
|
||||
}
|
||||
|
||||
// grumble grumble, gotta avoid repeated code when possible or at least if significant enough
|
||||
|
||||
static u32 _DisaOpenCertDb(char (*path)[16], bool emunand, DisaDiffRWInfo* info, u8** cache, u32* offset, u32* max_offset) {
|
||||
GetCertDBPath(*path, emunand);
|
||||
|
||||
u8* _cache = NULL;
|
||||
if (GetDisaDiffRWInfo(*path, info, false) != 0) return 1;
|
||||
_cache = (u8*)malloc(info->size_dpfs_lvl2);
|
||||
if (!_cache) return 1;
|
||||
if (BuildDisaDiffDpfsLvl2Cache(*path, info, _cache, info->size_dpfs_lvl2) != 0) {
|
||||
free(_cache);
|
||||
return 1;
|
||||
}
|
||||
|
||||
CertsDbPartitionHeader header;
|
||||
|
||||
if (ReadDisaDiffIvfcLvl4(*path, info, 0, sizeof(CertsDbPartitionHeader), &header) != sizeof(CertsDbPartitionHeader)) {
|
||||
free(_cache);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (getbe32(header.magic) != 0x43455254 /* 'CERT' */ ||
|
||||
getbe32(header.unk) != 0 ||
|
||||
getle32(header.used_size) & 0xFF) {
|
||||
free(_cache);
|
||||
return 1;
|
||||
}
|
||||
|
||||
*cache = _cache;
|
||||
|
||||
*offset = sizeof(CertsDbPartitionHeader);
|
||||
*max_offset = getle32(header.used_size) + sizeof(CertsDbPartitionHeader);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static u32 _ProcessNextCertDbEntry(const char* path, DisaDiffRWInfo* info, Certificate* cert, u32 *full_size, char (*full_issuer)[0x41], u32* offset, u32 max_offset) {
|
||||
u8 sig_type_data[4];
|
||||
u8 keytype_data[4];
|
||||
|
||||
if (*offset + 4 > max_offset) return 1;
|
||||
|
||||
if (ReadDisaDiffIvfcLvl4(path, info, *offset, 4, sig_type_data) != 4)
|
||||
return 1;
|
||||
|
||||
u32 sig_type = getbe32(sig_type_data);
|
||||
|
||||
if (sig_type == 0x10002 || sig_type == 0x10005) return 1; // ECC signs not allowed on db
|
||||
|
||||
u32 sig_size = _Certificate_GetSignatureChunkSizeFromType(sig_type);
|
||||
if (sig_size == 0) return 1;
|
||||
|
||||
u32 keytype_off = *offset + sig_size + offsetof(CertificateBody, keytype);
|
||||
if (keytype_off + 4 > max_offset) return 1;
|
||||
|
||||
if (ReadDisaDiffIvfcLvl4(path, info, keytype_off, 4, keytype_data) != 4)
|
||||
return 1;
|
||||
|
||||
u32 keytype = getbe32(keytype_data);
|
||||
|
||||
if (keytype == 2) return 1; // ECC keys not allowed on db
|
||||
|
||||
u32 data_size = _Certificate_GetDataChunkSizeFromType(keytype);
|
||||
if (data_size == 0) return 1;
|
||||
|
||||
*full_size = sig_size + data_size;
|
||||
if (*offset + *full_size > max_offset) return 1;
|
||||
|
||||
cert->sig = (CertificateSignature*)malloc(sig_size);
|
||||
cert->data = (CertificateBody*)malloc(data_size);
|
||||
if (!cert->sig || !cert->data)
|
||||
return 1;
|
||||
|
||||
if (ReadDisaDiffIvfcLvl4(path, info, *offset, sig_size, cert->sig) != sig_size)
|
||||
return 1;
|
||||
|
||||
if (ReadDisaDiffIvfcLvl4(path, info, *offset + sig_size, data_size, cert->data) != data_size)
|
||||
return 1;
|
||||
|
||||
if (!Certificate_IsValid(cert))
|
||||
return 1;
|
||||
|
||||
if (snprintf(*full_issuer, 0x41, "%s-%s", cert->data->issuer, cert->data->name) > 0x40)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// certificates returned by this call are not to be deemed safe to edit, pointers or pointed data
|
||||
u32 LoadCertFromCertDb(Certificate* cert, const char* issuer) {
|
||||
if (!issuer || !cert) return 1;
|
||||
|
||||
u32 _ident = _Issuer_To_StorageIdent(issuer);
|
||||
if (_LoadFromCertStorage(cert, _ident)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ret = 1;
|
||||
|
||||
for (int i = 0; i < 2 && ret; ++i) {
|
||||
Certificate cert_local = CERTIFICATE_NULL_INIT;
|
||||
|
||||
char path[16];
|
||||
DisaDiffRWInfo info;
|
||||
u8* cache;
|
||||
|
||||
u32 offset, max_offset;
|
||||
|
||||
if (_DisaOpenCertDb(&path, i ? true : false, &info, &cache, &offset, &max_offset))
|
||||
return 1;
|
||||
|
||||
// certs.db has no filesystem.. its pretty plain, certificates after another
|
||||
// but also, certificates are not equally sized
|
||||
// so most cases of bad data, leads to giving up
|
||||
while (offset < max_offset) {
|
||||
char full_issuer[0x41];
|
||||
u32 full_size;
|
||||
|
||||
if (_ProcessNextCertDbEntry(path, &info, &cert_local, &full_size, &full_issuer, &offset, max_offset))
|
||||
break;
|
||||
|
||||
if (!strcmp(full_issuer, issuer)) {
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
_Certificate_CleanupImpl(&cert_local);
|
||||
|
||||
offset += full_size;
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
_Certificate_CleanupImpl(&cert_local);
|
||||
} else {
|
||||
*cert = cert_local;
|
||||
_SaveToCertStorage(&cert_local, _ident);
|
||||
}
|
||||
|
||||
free(cache);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// I dont expect many certs on a cert bundle, so I'll cap it to 8
|
||||
u32 BuildRawCertBundleFromCertDb(void* rawout, size_t* size, const char* const* cert_issuers, int count) {
|
||||
if (!rawout || !size || !cert_issuers || count < 0 || count > 8) return 1;
|
||||
if (!*size && count) return 1;
|
||||
if (!count) { // *shrug*
|
||||
*size = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
if (!cert_issuers[i])
|
||||
return 1;
|
||||
}
|
||||
|
||||
Certificate certs[8];
|
||||
u8 certs_loaded = 0;
|
||||
|
||||
memset(certs, 0, sizeof(certs));
|
||||
|
||||
int loaded_count = 0;
|
||||
|
||||
// search static storage first
|
||||
for (int i = 0; i < count; ++i) {
|
||||
u32 _ident = _Issuer_To_StorageIdent(cert_issuers[i]);
|
||||
if (_LoadFromCertStorage(&certs[i], _ident)) {
|
||||
certs_loaded |= BIT(i);
|
||||
++loaded_count;
|
||||
}
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
|
||||
for (int i = 0; i < 2 && loaded_count != count && !ret; ++i) {
|
||||
Certificate cert_local = CERTIFICATE_NULL_INIT;
|
||||
|
||||
char path[16];
|
||||
DisaDiffRWInfo info;
|
||||
u8* cache;
|
||||
|
||||
u32 offset, max_offset;
|
||||
|
||||
if (_DisaOpenCertDb(&path, i ? true : false, &info, &cache, &offset, &max_offset))
|
||||
continue;
|
||||
|
||||
while (offset < max_offset) {
|
||||
char full_issuer[0x41];
|
||||
u32 full_size;
|
||||
|
||||
if (_ProcessNextCertDbEntry(path, &info, &cert_local, &full_size, &full_issuer, &offset, max_offset))
|
||||
break;
|
||||
|
||||
for (int j = 0; j < count; j++) {
|
||||
if (certs_loaded & BIT(j)) continue;
|
||||
if (!strcmp(full_issuer, cert_issuers[j])) {
|
||||
ret = _Certificate_AllocCopyOutImpl(&cert_local, &certs[j]);
|
||||
if (ret) break;
|
||||
certs_loaded |= BIT(j);
|
||||
++loaded_count;
|
||||
}
|
||||
}
|
||||
|
||||
// while at it, try to save to static storage, if applicable
|
||||
u32 _ident = _Issuer_To_StorageIdent(full_issuer);
|
||||
_SaveToCertStorage(&cert_local, _ident);
|
||||
|
||||
_Certificate_CleanupImpl(&cert_local);
|
||||
|
||||
if (loaded_count == count || ret) // early exit
|
||||
break;
|
||||
|
||||
offset += full_size;
|
||||
}
|
||||
|
||||
free(cache);
|
||||
}
|
||||
|
||||
if (!ret && loaded_count == count) {
|
||||
u8* out = (u8*)rawout;
|
||||
size_t limit = *size, written = 0;
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
u32 sig_size = _Certificate_GetSignatureChunkSizeFromType(getbe32(certs[i].sig->sig_type));
|
||||
u32 data_size = _Certificate_GetDataChunkSizeFromType(getbe32(certs[i].data->keytype));
|
||||
|
||||
if (sig_size == 0 || data_size == 0) {
|
||||
ret = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
u32 full_size = sig_size + data_size;
|
||||
|
||||
if (written + full_size > limit) {
|
||||
ret = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (_Certificate_RawCopyImpl(&certs[i], out)) {
|
||||
ret = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
out += full_size;
|
||||
written += full_size;
|
||||
}
|
||||
|
||||
if (!ret)
|
||||
*size = written;
|
||||
} else {
|
||||
ret = 1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
if (certs_loaded & BIT(i))
|
||||
_Certificate_CleanupImpl(&certs[i]);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#define CERT_MAX_SIZE (sizeof(CertificateSignature) + 0x23C + sizeof(CertificateBody) + 0x238)
|
||||
|
||||
#define CERT_RSA4096_SIG_SIZE (sizeof(CertificateSignature) + 0x23C)
|
||||
#define CERT_RSA2048_SIG_SIZE (sizeof(CertificateSignature) + 0x13C)
|
||||
#define CERT_ECC_SIG_SIZE (sizeof(CertificateSignature) + 0x7C)
|
||||
#define CERT_RSA4096_BODY_SIZE (sizeof(CertificateBody) + 0x238)
|
||||
#define CERT_RSA2048_BODY_SIZE (sizeof(CertificateBody) + 0x138)
|
||||
#define CERT_ECC_BODY_SIZE (sizeof(CertificateBody) + 0x78)
|
||||
|
||||
#define CERTIFICATE_NULL_INIT ((Certificate){NULL, NULL})
|
||||
|
||||
// from: http://3dbrew.org/wiki/Certificates
|
||||
// all numbers in big endian
|
||||
typedef struct {
|
||||
u8 sig_type[4];
|
||||
u8 signature[];
|
||||
} PACKED_ALIGN(1) CertificateSignature;
|
||||
|
||||
typedef struct {
|
||||
char issuer[0x40];
|
||||
u8 keytype[4];
|
||||
char name[0x40];
|
||||
u8 expiration[4];
|
||||
u8 pub_key_data[];
|
||||
} PACKED_ALIGN(1) CertificateBody;
|
||||
|
||||
typedef struct {
|
||||
CertificateSignature* sig;
|
||||
CertificateBody* data;
|
||||
} Certificate;
|
||||
|
||||
bool Certificate_IsValid(const Certificate* cert);
|
||||
bool Certificate_IsRSA(const Certificate* cert);
|
||||
bool Certificate_IsECC(const Certificate* cert);
|
||||
u32 Certificate_GetSignatureSize(const Certificate* cert, u32* size);
|
||||
u32 Certificate_GetModulusSize(const Certificate* cert, u32* size);
|
||||
u32 Certificate_GetModulus(const Certificate* cert, void* mod);
|
||||
u32 Certificate_GetExponent(const Certificate* cert, void* exp);
|
||||
u32 Certificate_GetEccSingleCoordinateSize(const Certificate* cert, u32* size);
|
||||
u32 Certificate_GetEccXY(const Certificate* cert, void* X, void* Y);
|
||||
u32 Certificate_GetSignatureChunkSize(const Certificate* cert, u32* size);
|
||||
u32 Certificate_GetDataChunkSize(const Certificate* cert, u32* size);
|
||||
u32 Certificate_GetFullSize(const Certificate* cert, u32* size);
|
||||
u32 Certificate_VerifySignatureBlock(const Certificate* cert, const void* sig, u32 sig_size, const void* data, u32 data_size, bool sha256);
|
||||
u32 Certificate_MakeEditSafe(Certificate* cert);
|
||||
u32 Certificate_AllocCopyOut(const Certificate* cert, Certificate* out_cert);
|
||||
u32 Certificate_RawCopy(const Certificate* cert, void* raw);
|
||||
u32 Certificate_Cleanup(Certificate* cert);
|
||||
|
||||
u32 LoadCertFromCertDb(Certificate* cert, const char* issuer);
|
||||
u32 BuildRawCertBundleFromCertDb(void* rawout, size_t* size, const char* const* cert_issuers, int count);
|
@ -1,23 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#define CIFINISH_MAGIC "CIFINISH"
|
||||
#define CIFINISH_TITLE_MAGIC "TITLE"
|
||||
#define CIFINISH_SIZE(c) (sizeof(CifinishHeader) + ((((CifinishHeader*)(c))->n_entries) * sizeof(CifinishTitle)))
|
||||
|
||||
// see: https://github.com/ihaveamac/custom-install/blob/ac0be9d61d7ebef9356df23036dc53e8e862011a/custominstall.py#L163
|
||||
typedef struct {
|
||||
char magic[8];
|
||||
u32 version;
|
||||
u32 n_entries;
|
||||
} __attribute__((packed, aligned(4))) CifinishHeader;
|
||||
|
||||
typedef struct {
|
||||
char magic[5];
|
||||
u8 padding0;
|
||||
u8 has_seed; // 1 if it does, otherwise 0
|
||||
u8 padding1;
|
||||
u64 title_id;
|
||||
u8 seed[16];
|
||||
} __attribute__((packed, aligned(4))) CifinishTitle;
|
@ -1,63 +0,0 @@
|
||||
#include "cmd.h"
|
||||
|
||||
|
||||
CmdHeader* BuildAllocCmdData(TitleMetaData* tmd) {
|
||||
CmdHeader proto;
|
||||
CmdHeader* cmd = NULL;
|
||||
u32 content_count = getbe16(tmd->content_count);
|
||||
u16 max_cnt_idx = 0;
|
||||
|
||||
// sanity check
|
||||
if (!content_count)
|
||||
return NULL;
|
||||
|
||||
// find max content id
|
||||
TmdContentChunk* chunk = (TmdContentChunk*) (tmd + 1);
|
||||
for (u32 i = 0; (i < content_count) && (i < TMD_MAX_CONTENTS); i++, chunk++)
|
||||
if (getbe16(chunk->index) > max_cnt_idx) max_cnt_idx = getbe16(chunk->index);
|
||||
|
||||
// allocate memory for CMD / basic setup
|
||||
proto.cmd_id = 1;
|
||||
proto.n_entries = max_cnt_idx + 1;
|
||||
proto.n_cmacs = content_count;
|
||||
proto.unknown = 1;
|
||||
memset(proto.cmac, 0x00, 0x10);
|
||||
cmd = (CmdHeader*) malloc(CMD_SIZE(&proto));
|
||||
if (!cmd) return NULL;
|
||||
memset(cmd, 0x00, CMD_SIZE(&proto));
|
||||
memcpy(cmd, &proto, sizeof(CmdHeader));
|
||||
cmd->unknown = 0x0; // this means no CMACs, only valid for NAND
|
||||
|
||||
// copy content ids
|
||||
u32* cnt_id = (u32*) (cmd + 1);
|
||||
u32* cnt_id_2nd = cnt_id + cmd->n_entries;
|
||||
chunk = (TmdContentChunk*) (tmd + 1);
|
||||
memset(cnt_id, 0xFF, cmd->n_entries * sizeof(u32));
|
||||
for (u32 i = 0; (i < content_count) && (i < TMD_MAX_CONTENTS); i++, chunk++) {
|
||||
u32 chunk_id = getbe32(chunk->id);
|
||||
cnt_id[getbe16(chunk->index)] = chunk_id;
|
||||
*(cnt_id_2nd++) = chunk_id;
|
||||
}
|
||||
|
||||
// bubble sort the second content id list
|
||||
bool bs_finished = false;
|
||||
cnt_id_2nd = cnt_id + cmd->n_entries;
|
||||
while (!bs_finished) {
|
||||
bs_finished = true;
|
||||
for (u32 b = 1; b < cmd->n_cmacs; b++) {
|
||||
if (cnt_id_2nd[b] < cnt_id_2nd[b-1]) {
|
||||
u32 swp = cnt_id_2nd[b];
|
||||
cnt_id_2nd[b] = cnt_id_2nd[b-1];
|
||||
cnt_id_2nd[b-1] = swp;
|
||||
bs_finished = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// set CMACs to 0x00
|
||||
u8* cnt_cmac = (u8*) (cnt_id_2nd + cmd->n_cmacs);
|
||||
memset(cnt_cmac, 0x00, 0x10 * cmd->n_entries);
|
||||
|
||||
// we still need to fix / set the CMACs inside the CMD file!
|
||||
return cmd;
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
#include "tmd.h"
|
||||
|
||||
#define CMD_SIZE(cmd) (sizeof(CmdHeader) + \
|
||||
(((cmd)->n_entries) * sizeof(u32)) + \
|
||||
(((cmd)->n_cmacs) * sizeof(u32)) + \
|
||||
(((cmd)->unknown) ? (((cmd)->n_entries) * 0x10) : 0))
|
||||
|
||||
// from: http://3dbrew.org/wiki/Titles#Data_Structure
|
||||
typedef struct {
|
||||
u32 cmd_id; // same as filename id, <cmd_id>.cmd
|
||||
u32 n_entries; // matches highest content index
|
||||
u32 n_cmacs; // number of cmacs in file (excluding the one @0x10)
|
||||
u32 unknown; // usually 1
|
||||
u8 cmac[0x10]; // calculated from first 0x10 byte of data, no hashing
|
||||
// followed by u32 list of content ids (sorted by index, 0xFFFFFFFF for unavailable)
|
||||
// followed by u32 list of content ids (sorted by id?)
|
||||
// followed by <n_entries> CMACs (may contain garbage)
|
||||
} __attribute__((packed, aligned(4))) CmdHeader;
|
||||
|
||||
CmdHeader* BuildAllocCmdData(TitleMetaData* tmd);
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user