forked from Mirror/GodMode9
Compare commits
177 Commits
spi_unalig
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
cf1cb1cfc5 | ||
|
9ae4351bce | ||
|
99f1abd7a4 | ||
|
15eb3b1ebe | ||
|
4424c37a89 | ||
|
bc84780036 | ||
|
712df196f1 | ||
|
a20c955e2a | ||
|
7b20581fce | ||
|
ba0272ff5d | ||
|
50270a820c | ||
|
61c79e3e3f | ||
|
ab222de6b1 | ||
|
143fcf0d6b | ||
|
157848c770 | ||
|
8cde50e091 | ||
|
105f4ae5f7 | ||
|
9e7df4c52d | ||
|
208f12bde7 | ||
|
7b6b478582 | ||
|
eee63dd155 | ||
|
dab90a9162 | ||
|
26990ca23a | ||
|
338a2aa98a | ||
|
399740b50e | ||
|
ad8b5e0a8c | ||
|
14b390a943 | ||
|
031762a1fe | ||
|
8b362c977a | ||
|
11b05d7a3d | ||
|
3008bfed61 | ||
|
f7a9b3eec8 | ||
|
e1fa23a031 | ||
|
8006fbd5db | ||
|
b4f04d3620 | ||
|
eb37a21354 | ||
|
a68d7d0cb7 | ||
|
87f9b82cd3 | ||
|
a293c008d5 | ||
|
780016933c | ||
|
041ff61d41 | ||
|
1fd15657d8 | ||
|
0971b3d9fa | ||
|
b7c97af144 | ||
|
723529e2d8 | ||
|
b5fca3bc7e | ||
|
620e5061c5 | ||
|
5aaac66eef | ||
|
439e06334b | ||
|
8303440c19 | ||
|
93ee590cad | ||
|
cae3d272d3 | ||
|
64414e12ab | ||
|
9514755989 | ||
|
ccd21984b2 | ||
|
a23ba0e14b | ||
|
3710ed975b | ||
|
9416ec5ac0 | ||
|
c9d792cb27 | ||
|
096e6c3cb7 | ||
|
b11194397a | ||
|
658c9b491c | ||
|
f611b31c0c | ||
|
c13bba4cfe | ||
|
d95a606ec2 | ||
|
586d30fafa | ||
|
682b570ef7 | ||
|
8d1557191f | ||
|
cb11db6f1b | ||
|
1554aac4e1 | ||
|
fdbca10773 | ||
|
1f8de2af99 | ||
|
1b8bd121b6 | ||
|
ba10ce96c3 | ||
|
25bf8b3f93 | ||
|
07cb94d99a | ||
|
1ffbca7d46 | ||
|
942e67e507 | ||
|
830479f50c | ||
|
0275a85121 | ||
|
3eb92754bc | ||
|
77fc7af2f2 | ||
|
b366200d4b | ||
|
13eb4f8869 | ||
|
55385a5502 | ||
|
0dbe70928f | ||
|
7feeb51a65 | ||
|
c966acc851 | ||
|
e042886db4 | ||
|
ddf577b88c | ||
|
3124d944a6 | ||
|
27e316571d | ||
|
0e46d4fca8 | ||
|
33d59f6d3a | ||
|
d85023b173 | ||
|
4dc96d37e8 | ||
|
c9b6a335f7 | ||
|
33a115b75c | ||
|
ef161bce42 | ||
|
d8d43c14f3 | ||
|
37c8c50097 | ||
|
c2d96c0d9c | ||
|
41d36c620f | ||
|
4b5ac1a8e0 | ||
|
be289b4c55 | ||
|
3bfb9ef6ec | ||
|
1f96b5e9e6 | ||
|
8427e0776c | ||
|
61c17e491f | ||
|
236d2dc09c | ||
|
8680358aa1 | ||
|
7e01954e48 | ||
|
0825139cb2 | ||
|
9f431a5fde | ||
|
cc99734fac | ||
|
6799b24730 | ||
|
adb8ad260f | ||
|
5c2ab6958c | ||
|
7af76b91bb | ||
|
ce50bd63a8 | ||
|
1a27dcb1e8 | ||
|
c20911047a | ||
|
fd8c5d1897 | ||
|
0bbbc7c324 | ||
|
9f52deedad | ||
|
60f2c5192d | ||
|
f2e52bd1c7 | ||
|
dfb2dff352 | ||
|
1cb72a87e1 | ||
|
3952de3a1e | ||
|
294890057f | ||
|
4e00b8b7b6 | ||
|
ddfdf81cf7 | ||
|
8b5af2c22f | ||
|
445688c5fb | ||
|
8fa85437dd | ||
|
2cd6acb31e | ||
|
c70a7db0f3 | ||
|
7bdd01738a | ||
|
152c6c4d5a | ||
|
32936345a6 | ||
|
30c5e1fd67 | ||
|
3f7eb872b8 | ||
|
bea16124a4 | ||
|
71e3b6f73c | ||
|
ee43fe328f | ||
|
31389687ab | ||
|
eadc1ab6b9 | ||
|
9ecb90a2ba | ||
|
a6e20c641a | ||
|
c4b3b582a7 | ||
|
c31737c257 | ||
|
2f61722aa4 | ||
|
ab4316fd4e | ||
|
24195c319a | ||
|
f9408a9c10 | ||
|
8114a0bd26 | ||
|
7620310b73 | ||
|
1e9fb36582 | ||
|
9191a3244f | ||
|
af5e1a218e | ||
|
667a1bf2c0 | ||
|
48347c947a | ||
|
e41b098843 | ||
|
58fb9913d5 | ||
|
899c8a8816 | ||
|
203cf7f9e3 | ||
|
8ebb74b0bc | ||
|
af14376c84 | ||
|
647d5722aa | ||
|
25a22d30d0 | ||
|
2f2b7faeb4 | ||
|
efcfed31b3 | ||
|
7f6f6db410 | ||
|
e9599aad1c | ||
|
46a9a5819a | ||
|
65f6748dc1 |
15
.github/workflows/ci.yml
vendored
15
.github/workflows/ci.yml
vendored
@ -4,8 +4,8 @@ on: [push, pull_request]
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
runs-on: ubuntu-18.04
|
runs-on: ubuntu-latest
|
||||||
container: devkitpro/devkitarm:latest
|
container: devkitpro/devkitarm
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v1
|
||||||
@ -23,21 +23,20 @@ jobs:
|
|||||||
apt-get -y install python3 python3-pip p7zip-full libarchive13
|
apt-get -y install python3 python3-pip p7zip-full libarchive13
|
||||||
python3 --version
|
python3 --version
|
||||||
python3 -m pip install --upgrade pip setuptools
|
python3 -m pip install --upgrade pip setuptools
|
||||||
python3 -m pip install cryptography
|
python3 -m pip install cryptography git+https://github.com/TuxSH/firmtool.git
|
||||||
python3 -m pip install git+https://github.com/TuxSH/firmtool.git
|
|
||||||
|
|
||||||
- name: Build Project
|
- name: Build Project
|
||||||
run: make release
|
run: make release -j$(nproc)
|
||||||
|
|
||||||
- name: Prepare build artifact
|
- name: Prepare build artifact
|
||||||
|
working-directory: release
|
||||||
run: |
|
run: |
|
||||||
cd release
|
|
||||||
ZIPNAME=$(ls GodMode9-*.zip)
|
ZIPNAME=$(ls GodMode9-*.zip)
|
||||||
rm $ZIPNAME
|
rm $ZIPNAME
|
||||||
echo "OUTNAME=${ZIPNAME%.zip}" >> $GITHUB_ENV
|
echo "OUTNAME=${ZIPNAME%.zip}" >> $GITHUB_ENV
|
||||||
cd ..
|
|
||||||
|
|
||||||
- uses: actions/upload-artifact@v2
|
- uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: ${{ env.OUTNAME }}
|
name: ${{ env.OUTNAME }}
|
||||||
path: release/*
|
path: release/*
|
||||||
|
if-no-files-found: error
|
||||||
|
9
.gitignore
vendored
9
.gitignore
vendored
@ -5,6 +5,7 @@
|
|||||||
*.obj
|
*.obj
|
||||||
*.elf
|
*.elf
|
||||||
*.map
|
*.map
|
||||||
|
*.dis
|
||||||
|
|
||||||
# Precompiled Headers
|
# Precompiled Headers
|
||||||
*.gch
|
*.gch
|
||||||
@ -35,10 +36,14 @@
|
|||||||
|
|
||||||
# OS leftovers
|
# OS leftovers
|
||||||
desktop.ini
|
desktop.ini
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
# Sublime files
|
# Sublime files
|
||||||
*.sublime-*
|
*.sublime-*
|
||||||
|
|
||||||
|
# Visual Studio Code files
|
||||||
|
.vscode
|
||||||
|
|
||||||
# Build directories
|
# Build directories
|
||||||
/build
|
/build
|
||||||
/output
|
/output
|
||||||
@ -48,6 +53,6 @@ desktop.ini
|
|||||||
/data/README_internal.md
|
/data/README_internal.md
|
||||||
|
|
||||||
# User additions
|
# User additions
|
||||||
/data/aeskeydb.bin
|
|
||||||
/data/aeskeydb_.bin
|
|
||||||
/zzz_backup
|
/zzz_backup
|
||||||
|
/arm9/source/language.inl
|
||||||
|
*.trf
|
||||||
|
62
Makefile
62
Makefile
@ -16,15 +16,28 @@ export RELDIR := release
|
|||||||
export COMMON_DIR := ../common
|
export COMMON_DIR := ../common
|
||||||
|
|
||||||
# Definitions for initial RAM disk
|
# Definitions for initial RAM disk
|
||||||
VRAM_OUT := $(OUTDIR)/vram0.tar
|
VRAM_TAR := $(OUTDIR)/vram0.tar
|
||||||
VRAM_DATA := data
|
VRAM_DATA := data/*
|
||||||
VRAM_FLAGS := --make-new --path-limit 99 --size-limit 262144
|
VRAM_FLAGS := --make-new --path-limit 99
|
||||||
|
ifeq ($(NTRBOOT),1)
|
||||||
|
VRAM_SCRIPTS := resources/gm9/scripts/*
|
||||||
|
endif
|
||||||
|
|
||||||
|
# Definitions for translation files
|
||||||
|
JSON_FOLDER := resources/languages
|
||||||
|
TRF_FOLDER := resources/gm9/languages
|
||||||
|
|
||||||
|
SOURCE_JSON := $(JSON_FOLDER)/source.json
|
||||||
|
LANGUAGE_INL := arm9/source/language.inl
|
||||||
|
|
||||||
|
JSON_FILES := $(filter-out $(SOURCE_JSON),$(wildcard $(JSON_FOLDER)/*.json))
|
||||||
|
TRF_FILES := $(subst $(JSON_FOLDER),$(TRF_FOLDER),$(JSON_FILES:.json=.trf))
|
||||||
|
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
ifeq ($(TERM),cygwin)
|
ifeq ($(TERM),cygwin)
|
||||||
PY3 := py -3 # Windows / CMD/PowerShell
|
PY3 := py -3 # Windows / CMD/PowerShell
|
||||||
else
|
else
|
||||||
PY3 := python3 # Windows / MSYS2
|
PY3 := py # Windows / MSYS2
|
||||||
endif
|
endif
|
||||||
else
|
else
|
||||||
PY3 := python3 # Unix-like
|
PY3 := python3 # Unix-like
|
||||||
@ -37,18 +50,18 @@ export ASFLAGS := -g -x assembler-with-cpp $(INCLUDE)
|
|||||||
export CFLAGS := -DDBUILTS="\"$(DBUILTS)\"" -DDBUILTL="\"$(DBUILTL)\"" -DVERSION="\"$(VERSION)\"" -DFLAVOR="\"$(FLAVOR)\"" \
|
export CFLAGS := -DDBUILTS="\"$(DBUILTS)\"" -DDBUILTL="\"$(DBUILTL)\"" -DVERSION="\"$(VERSION)\"" -DFLAVOR="\"$(FLAVOR)\"" \
|
||||||
-g -Os -Wall -Wextra -Wcast-align -Wformat=2 -Wno-main \
|
-g -Os -Wall -Wextra -Wcast-align -Wformat=2 -Wno-main \
|
||||||
-fomit-frame-pointer -ffast-math -std=gnu11 -MMD -MP \
|
-fomit-frame-pointer -ffast-math -std=gnu11 -MMD -MP \
|
||||||
-Wno-unused-function -Wno-format-truncation $(INCLUDE) -ffunction-sections -fdata-sections
|
-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
|
export LDFLAGS := -Tlink.ld -nostartfiles -Wl,--gc-sections,-z,max-page-size=4096
|
||||||
ELF := arm9/arm9.elf arm11/arm11.elf
|
ELF := arm9/arm9_code.elf arm9/arm9_data.elf arm11/arm11.elf
|
||||||
|
|
||||||
.PHONY: all firm vram0 elf release clean
|
.PHONY: all firm $(VRAM_TAR) elf release clean
|
||||||
all: firm
|
all: firm
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@set -e; for elf in $(ELF); do \
|
@set -e; for elf in $(ELF); do \
|
||||||
$(MAKE) --no-print-directory -C $$(dirname $$elf) clean; \
|
$(MAKE) --no-print-directory -C $$(dirname $$elf) clean; \
|
||||||
done
|
done
|
||||||
@rm -rf $(OUTDIR) $(RELDIR) $(FIRM) $(FIRMD) $(VRAM_OUT)
|
@rm -rf $(OUTDIR) $(RELDIR) $(FIRM) $(FIRMD) $(VRAM_TAR) $(LANGUAGE_INL) $(TRF_FILES)
|
||||||
|
|
||||||
unmarked_readme: .FORCE
|
unmarked_readme: .FORCE
|
||||||
@$(PY3) utils/unmark.py -f README.md data/README_internal.md
|
@$(PY3) utils/unmark.py -f README.md data/README_internal.md
|
||||||
@ -72,29 +85,44 @@ release: clean unmarked_readme
|
|||||||
@cp $(OUTDIR)/$(FLAVOR)_dev.firm.sha $(RELDIR)/
|
@cp $(OUTDIR)/$(FLAVOR)_dev.firm.sha $(RELDIR)/
|
||||||
@cp $(ELF) $(RELDIR)/elf
|
@cp $(ELF) $(RELDIR)/elf
|
||||||
@cp $(CURDIR)/README.md $(RELDIR)
|
@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/gm9 $(RELDIR)/gm9
|
||||||
@cp -R $(CURDIR)/resources/sample $(RELDIR)/sample
|
@cp -R $(CURDIR)/resources/sample $(RELDIR)/sample
|
||||||
|
|
||||||
@-7za a $(RELDIR)/$(FLAVOR)-$(VERSION)-$(DBUILTS).zip ./$(RELDIR)/*
|
@-7za a $(RELDIR)/$(FLAVOR)-$(VERSION)-$(DBUILTS).zip ./$(RELDIR)/*
|
||||||
|
|
||||||
vram0:
|
$(VRAM_TAR): $(SPLASH) $(OVERRIDE_FONT) $(VRAM_DATA) $(VRAM_SCRIPTS)
|
||||||
@mkdir -p "$(OUTDIR)"
|
@mkdir -p "$(@D)"
|
||||||
@echo "Creating $(VRAM_OUT)"
|
@echo "Creating $@"
|
||||||
@$(PY3) utils/add2tar.py $(VRAM_FLAGS) $(VRAM_OUT) $(shell ls -d $(SPLASH) $(OVERRIDE_FONT) $(VRAM_DATA)/*)
|
@$(PY3) utils/add2tar.py $(VRAM_FLAGS) $(VRAM_TAR) $(shell ls -d -1 $^)
|
||||||
|
|
||||||
|
$(LANGUAGE_INL): $(SOURCE_JSON)
|
||||||
|
@echo "Creating $@"
|
||||||
|
@$(PY3) utils/transcp.py $< $@
|
||||||
|
|
||||||
|
$(TRF_FOLDER)/%.trf: $(JSON_FOLDER)/%.json
|
||||||
|
@$(PY3) utils/transriff.py $< $@
|
||||||
|
|
||||||
%.elf: .FORCE
|
%.elf: .FORCE
|
||||||
@echo "Building $@"
|
@echo "Building $@"
|
||||||
@$(MAKE) --no-print-directory -C $(@D)
|
@$(MAKE) --no-print-directory -C $(@D) $(@F)
|
||||||
|
|
||||||
firm: $(ELF) vram0
|
# Indicate a few explicit dependencies:
|
||||||
@test `wc -c <$(VRAM_OUT)` -le 262144
|
# 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
|
||||||
|
|
||||||
|
firm: $(ELF) $(TRF_FILES)
|
||||||
@mkdir -p $(call dirname,"$(FIRM)") $(call dirname,"$(FIRMD)")
|
@mkdir -p $(call dirname,"$(FIRM)") $(call dirname,"$(FIRMD)")
|
||||||
@echo "[FLAVOR] $(FLAVOR)"
|
@echo "[FLAVOR] $(FLAVOR)"
|
||||||
@echo "[VERSION] $(VERSION)"
|
@echo "[VERSION] $(VERSION)"
|
||||||
@echo "[BUILD] $(DBUILTL)"
|
@echo "[BUILD] $(DBUILTL)"
|
||||||
@echo "[FIRM] $(FIRM)"
|
@echo "[FIRM] $(FIRM)"
|
||||||
@$(PY3) -m firmtool build $(FIRM) $(FTFLAGS) -g -A 0x80C0000 -D $(ELF) $(VRAM_OUT) -C NDMA XDMA memcpy
|
@$(PY3) -m firmtool build $(FIRM) $(FTFLAGS) -g -D $(ELF) -C NDMA NDMA XDMA
|
||||||
@echo "[FIRM] $(FIRMD)"
|
@echo "[FIRM] $(FIRMD)"
|
||||||
@$(PY3) -m firmtool build $(FIRMD) $(FTDFLAGS) -g -A 0x80C0000 -D $(ELF) $(VRAM_OUT) -C NDMA XDMA memcpy
|
@$(PY3) -m firmtool build $(FIRMD) $(FTDFLAGS) -g -D $(ELF) -C NDMA NDMA XDMA
|
||||||
|
|
||||||
|
vram0: $(VRAM_TAR) .FORCE # legacy target name
|
||||||
|
|
||||||
.FORCE:
|
.FORCE:
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
LIBS ?=
|
||||||
|
|
||||||
OBJECTS := $(patsubst $(SOURCE)/%.s, $(BUILD)/%.o, \
|
OBJECTS := $(patsubst $(SOURCE)/%.s, $(BUILD)/%.o, \
|
||||||
$(patsubst $(SOURCE)/%.c, $(BUILD)/%.o, \
|
$(patsubst $(SOURCE)/%.c, $(BUILD)/%.o, \
|
||||||
$(call rwildcard, $(SOURCE), *.s *.c)))
|
$(call rwildcard, $(SOURCE), *.s *.c)))
|
||||||
@ -11,11 +13,12 @@ all: $(TARGET).elf
|
|||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
@rm -rf $(BUILD) $(TARGET).elf $(TARGET).map
|
@rm -rf $(BUILD) $(TARGET).elf $(TARGET).dis $(TARGET).map
|
||||||
|
|
||||||
$(TARGET).elf: $(OBJECTS) $(OBJECTS_COMMON)
|
$(TARGET).elf: $(OBJECTS) $(OBJECTS_COMMON)
|
||||||
@mkdir -p "$(@D)"
|
@mkdir -p "$(@D)"
|
||||||
@$(CC) $(LDFLAGS) $^ -o $@
|
@$(CC) $(LDFLAGS) $^ -o $@ $(LIBS)
|
||||||
|
@$(OBJDUMP) -S -h $@ > $@.dis
|
||||||
|
|
||||||
$(BUILD)/%.cmn.o: $(COMMON_DIR)/%.c
|
$(BUILD)/%.cmn.o: $(COMMON_DIR)/%.c
|
||||||
@mkdir -p "$(@D)"
|
@mkdir -p "$(@D)"
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
export OBJDUMP := arm-none-eabi-objdump
|
||||||
|
|
||||||
dirname = $(shell dirname $(1))
|
dirname = $(shell dirname $(1))
|
||||||
|
|
||||||
@ -25,6 +26,12 @@ else ifeq ($(FLAVOR),ZuishMode9)
|
|||||||
CFLAGS += -DDEFAULT_FONT=\"font_zuish_8x8.pbm\"
|
CFLAGS += -DDEFAULT_FONT=\"font_zuish_8x8.pbm\"
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(LARGEDLC),1)
|
||||||
|
CFLAGS += -DTITLE_MAX_CONTENTS=1536
|
||||||
|
else
|
||||||
|
CFLAGS += -DTITLE_MAX_CONTENTS=1024
|
||||||
|
endif
|
||||||
|
|
||||||
ifeq ($(SALTMODE),1)
|
ifeq ($(SALTMODE),1)
|
||||||
CFLAGS += -DSALTMODE
|
CFLAGS += -DSALTMODE
|
||||||
endif
|
endif
|
||||||
@ -61,6 +68,10 @@ ifdef SD_TIMEOUT
|
|||||||
CFLAGS += -DSD_TIMEOUT=$(SD_TIMEOUT)
|
CFLAGS += -DSD_TIMEOUT=$(SD_TIMEOUT)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(NO_LUA),1)
|
||||||
|
CFLAGS += -DNO_LUA
|
||||||
|
endif
|
||||||
|
|
||||||
ifdef N_PANES
|
ifdef N_PANES
|
||||||
CFLAGS += -DN_PANES=$(N_PANES)
|
CFLAGS += -DN_PANES=$(N_PANES)
|
||||||
endif
|
endif
|
||||||
|
54
README.md
54
README.md
@ -39,9 +39,9 @@ GodMode9 is designed to be intuitive, buttons leading to the results you'd expec
|
|||||||
## How to build this / developer info
|
## 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).
|
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.pbm` inside the `data` directory. You may also hardcode the brightness via `make FIXED_BRIGHTNESS=x`, whereas `x` is a value between 0...15.
|
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 3MB limit for all files inside, including overhead. A standalone script runner is compiled by providing `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.
|
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.
|
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.
|
||||||
|
|
||||||
@ -61,10 +61,14 @@ GodMode9 provides a write permissions system, which will protect you from accide
|
|||||||
|
|
||||||
## Support files
|
## 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.
|
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. A known perfect `aeskeydb.bin` can be found somewhere on the net, is exactly 1024 byte big and has an MD5 of A5B28945A7C051D7A0CD18AF0E580D1B. Have fun hunting!
|
* __`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 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.
|
* __`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 create updatable CIAs from NCCH / NCSD files. CIAs created without these files will still work, but won't be updatable from eShop.
|
* __`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).
|
||||||
|
|
||||||
|
### 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
|
## 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).
|
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).
|
||||||
@ -86,9 +90,10 @@ GodMode9 provides access to system data via drives, a listing of what each drive
|
|||||||
* __`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.
|
* __`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.
|
* __`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.
|
* __`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`__: 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.
|
* __`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).
|
* __`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 the first VRAM bank and contains files essential to GodMode9. The font (in PBM 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.
|
* __`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.
|
||||||
* __`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.
|
* __`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.
|
||||||
|
|
||||||
|
|
||||||
@ -96,8 +101,8 @@ GodMode9 provides access to system data via drives, a listing of what each drive
|
|||||||
GodMode9 is one of the most important tools for digital preservation of 3DS content data. Here's some stuff you should know:
|
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)__: 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.
|
* __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) to the CIA installable format using the A button menu. To get a list of installed content, press R+A on one of the compatible drives (`1:`, `2:`, `A:`, ...) and select `Search for titles`. 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 (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 or if it only finds a personalized one, it offers to use a generic ticket instead. It is not recommended to use personalized tickets - only choose this if you know what you're doing.
|
* __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.
|
* __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.
|
||||||
|
|
||||||
|
|
||||||
@ -117,8 +122,9 @@ With the possibilites GodMode9 provides, not everything may be obvious at first
|
|||||||
* __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.
|
* __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
|
### 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 sample scripts included in the release archive. *Don't run scripts from untrusted sources.*
|
* __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 .gm9 scripts via a neat menu__: Press the 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.
|
* __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.
|
||||||
|
|
||||||
### SD card handling
|
### 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.
|
* __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.
|
||||||
@ -128,15 +134,17 @@ With the possibilites GodMode9 provides, not everything may be obvious at first
|
|||||||
* __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.
|
* __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
|
### Game file handling
|
||||||
* __List titles installed on your system__: Press R+A on a /title dir or a subdir below that. This will also work directly for `CTRNAND`, `TWLN` and `A:`/`B:` drives. This will list all titles installed in the selected location. Works best with the below two features.
|
* __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.
|
||||||
* __Build CIAs from NCCH / NCSD (.3DS) / TMD (installed contents)__: Press A on the file you want converted and 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.
|
* __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.
|
||||||
* __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.
|
* __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, 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`.
|
* __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.
|
* __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.
|
* __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.
|
* __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 / 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*.
|
* __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.
|
* __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.
|
||||||
|
|
||||||
### NAND handling
|
### NAND handling
|
||||||
@ -154,7 +162,7 @@ With the possibilites GodMode9 provides, not everything may be obvious at first
|
|||||||
* __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.
|
* __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), `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
|
||||||
* __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 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.
|
* __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.
|
* __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
|
### Support file handling
|
||||||
@ -165,6 +173,8 @@ With the possibilites GodMode9 provides, not everything may be obvious at first
|
|||||||
## License
|
## 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.
|
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
|
## 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 (in no particular order)...
|
||||||
@ -174,9 +184,13 @@ This tool would not have been possible without the help of numerous people. Than
|
|||||||
* **Wolfvak** for ARM11 code, FIRM binary launcher, exception handlers, PCX code, Makefile and for help on countless other occasions
|
* **Wolfvak** for ARM11 code, FIRM binary launcher, exception handlers, PCX code, Makefile and for help on countless other occasions
|
||||||
* **SciresM** for helping me figure out RomFS and for boot9strap
|
* **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
|
* **SciresM**, **Myria**, **Normmatt**, **TuxSH** and **hedgeberg** for figuring out sighax and giving us access to bootrom
|
||||||
* **ihaveamac** for first developing the simple CIA generation method and for being of great help in porting it
|
* **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
|
* **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/)
|
* **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
|
* **b1l1s** for helping me figure out A9LH compatibility
|
||||||
* **Gelex** and **AuroraWright** for helping me figure out various things
|
* **Gelex** and **AuroraWright** for helping me figure out various things
|
||||||
* **stuckpixel** for the new 6x10 font and help on various things
|
* **stuckpixel** for the new 6x10 font and help on various things
|
||||||
@ -185,14 +199,16 @@ This tool would not have been possible without the help of numerous people. Than
|
|||||||
* **profi200** for always useful advice and helpful hints on various things
|
* **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
|
* **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
|
* **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
|
* **JaySea**, **YodaDaCoda**, **liomajor**, **Supster131**, **imanoob**, **Kasher_CS** and countless others from freenode #Cakey and the GBAtemp forums for testing, feedback and helpful hints
|
||||||
* **Shadowhand** for being awesome and [hosting my nightlies](https://d0k3.secretalgorithm.com/)
|
* **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/)
|
* **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 official [GodMode9 Discord channel](https://discord.gg/EGu6Qxw)
|
* **SirNapkin1334** for testing, bug reports and for hosting the original GodMode9 Discord server
|
||||||
* **Lilith Valentine** for testing and helpful advice
|
* **Lilith Valentine** for testing and helpful advice
|
||||||
* **Project Nayuki** for [qrcodegen](https://github.com/nayuki/QR-Code-generator)
|
* **Project Nayuki** for [qrcodegen](https://github.com/nayuki/QR-Code-generator)
|
||||||
* **Amazingmax fonts** for the Amazdoom font
|
* **Amazingmax fonts** for the Amazdoom font
|
||||||
* The fine folks on **the official GodMode9 Discord**
|
* **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**
|
* The fine folks on **freenode #Cakey**
|
||||||
* All **[3dbrew.org](https://www.3dbrew.org/wiki/Main_Page) editors**
|
* 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 me!
|
* Everyone I possibly forgot, if you think you deserve to be mentioned, just contact us!
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
PROCESSOR := ARM11
|
PROCESSOR := ARM11
|
||||||
|
|
||||||
TARGET := $(shell basename $(CURDIR))
|
TARGET := $(shell basename "$(CURDIR)")
|
||||||
|
|
||||||
SOURCE := source
|
SOURCE := source
|
||||||
BUILD := build
|
BUILD := build
|
||||||
@ -11,7 +11,7 @@ INCLUDE := $(foreach dir,$(INCDIRS),-I"$(shell pwd)/$(dir)")
|
|||||||
|
|
||||||
ASFLAGS += $(SUBARCH) $(INCLUDE)
|
ASFLAGS += $(SUBARCH) $(INCLUDE)
|
||||||
CFLAGS += $(SUBARCH) $(INCLUDE) -flto
|
CFLAGS += $(SUBARCH) $(INCLUDE) -flto
|
||||||
LDFLAGS += $(SUBARCH) -Wl,-Map,$(TARGET).map -flto
|
LDFLAGS += $(SUBARCH) -Wl,--use-blx,-Map,$(TARGET).map -flto
|
||||||
|
|
||||||
include ../Makefile.common
|
include ../Makefile.common
|
||||||
include ../Makefile.build
|
include ../Makefile.build
|
||||||
|
@ -16,7 +16,7 @@ SECTIONS
|
|||||||
__text_va = ABSOLUTE(.);
|
__text_va = ABSOLUTE(.);
|
||||||
*(.text*)
|
*(.text*)
|
||||||
. = ALIGN(4K);
|
. = ALIGN(4K);
|
||||||
__text_len = . - __text_va;
|
__text_va_end = .;
|
||||||
} >AXIWRAM
|
} >AXIWRAM
|
||||||
|
|
||||||
.data : ALIGN(4K)
|
.data : ALIGN(4K)
|
||||||
@ -25,7 +25,7 @@ SECTIONS
|
|||||||
__data_va = ABSOLUTE(.);
|
__data_va = ABSOLUTE(.);
|
||||||
*(.data*)
|
*(.data*)
|
||||||
. = ALIGN(4K);
|
. = ALIGN(4K);
|
||||||
__data_len = . - __data_va;
|
__data_va_end = .;
|
||||||
} >AXIWRAM
|
} >AXIWRAM
|
||||||
|
|
||||||
.rodata : ALIGN(4K)
|
.rodata : ALIGN(4K)
|
||||||
@ -34,7 +34,7 @@ SECTIONS
|
|||||||
__rodata_va = ABSOLUTE(.);
|
__rodata_va = ABSOLUTE(.);
|
||||||
*(.rodata*)
|
*(.rodata*)
|
||||||
. = ALIGN(4K);
|
. = ALIGN(4K);
|
||||||
__rodata_len = . - __rodata_va;
|
__rodata_va_end = .;
|
||||||
} >AXIWRAM
|
} >AXIWRAM
|
||||||
|
|
||||||
.shared (NOLOAD) : ALIGN(4K)
|
.shared (NOLOAD) : ALIGN(4K)
|
||||||
@ -43,7 +43,7 @@ SECTIONS
|
|||||||
__shared_va = ABSOLUTE(.);
|
__shared_va = ABSOLUTE(.);
|
||||||
*(.shared*)
|
*(.shared*)
|
||||||
. = ALIGN(4K);
|
. = ALIGN(4K);
|
||||||
__shared_len = . - __shared_va;
|
__shared_va_end = .;
|
||||||
} >AXIWRAM
|
} >AXIWRAM
|
||||||
|
|
||||||
.bss (NOLOAD) : ALIGN(4K)
|
.bss (NOLOAD) : ALIGN(4K)
|
||||||
@ -52,6 +52,6 @@ SECTIONS
|
|||||||
__bss_va = ABSOLUTE(.);
|
__bss_va = ABSOLUTE(.);
|
||||||
*(.bss*)
|
*(.bss*)
|
||||||
. = ALIGN(4K);
|
. = ALIGN(4K);
|
||||||
__bss_len = . - __bss_va;
|
__bss_va_end = .;
|
||||||
} >AXIWRAM
|
} >AXIWRAM
|
||||||
}
|
}
|
||||||
|
@ -19,8 +19,12 @@
|
|||||||
#include <common.h>
|
#include <common.h>
|
||||||
#include <arm.h>
|
#include <arm.h>
|
||||||
|
|
||||||
|
#include <stdatomic.h>
|
||||||
|
|
||||||
#include "arm/gic.h"
|
#include "arm/gic.h"
|
||||||
|
|
||||||
|
#include "system/event.h"
|
||||||
|
|
||||||
/* Generic Interrupt Controller Registers */
|
/* Generic Interrupt Controller Registers */
|
||||||
#define REG_GIC(cpu, o, t) REG_ARM_PMR(0x200 + ((cpu) * 0x100) + (o), t)
|
#define REG_GIC(cpu, o, t) REG_ARM_PMR(0x200 + ((cpu) * 0x100) + (o), t)
|
||||||
|
|
||||||
@ -72,6 +76,7 @@
|
|||||||
#define IRQN_IS_VALID(n) ((n) < DIC_MAX_IRQ)
|
#define IRQN_IS_VALID(n) ((n) < DIC_MAX_IRQ)
|
||||||
|
|
||||||
static gicIrqHandler gicIrqHandlers[DIC_MAX_IRQ];
|
static gicIrqHandler gicIrqHandlers[DIC_MAX_IRQ];
|
||||||
|
static _Atomic(u32) gicIrqPending[DIC_MAX_IRQ / 32];
|
||||||
|
|
||||||
static struct {
|
static struct {
|
||||||
u8 tgt;
|
u8 tgt;
|
||||||
@ -106,14 +111,13 @@ static u8 gicGetDefaultIrqCfg(u32 irqn) {
|
|||||||
return gicDefaultIrqCfg[i].mode;
|
return gicDefaultIrqCfg[i].mode;
|
||||||
}
|
}
|
||||||
// TODO: would it be considerably faster to use bsearch?
|
// TODO: would it be considerably faster to use bsearch?
|
||||||
|
|
||||||
return GIC_RISINGEDGE_1N;
|
return GIC_RISINGEDGE_1N;
|
||||||
}
|
}
|
||||||
|
|
||||||
void gicTopHandler(void)
|
void gicTopHandler(void)
|
||||||
{
|
{
|
||||||
while(1) {
|
while(1) {
|
||||||
u32 irqn;
|
u32 irqn, irqsource, index, mask;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
If more than one of these CPUs reads the Interrupt Acknowledge Register at the
|
If more than one of these CPUs reads the Interrupt Acknowledge Register at the
|
||||||
@ -121,16 +125,22 @@ void gicTopHandler(void)
|
|||||||
routine must ensure that only one of them tries to process the interrupt, with the
|
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.
|
others returning after writing the ID to the End of Interrupt Register.
|
||||||
*/
|
*/
|
||||||
irqn = REG_GIC_IRQACK(GIC_THIS_CPU_ALIAS);
|
irqsource = REG_GIC_IRQACK(GIC_THIS_CPU_ALIAS);
|
||||||
|
|
||||||
if (irqn == GIC_IRQ_SPURIOUS) // no further processing is needed
|
if (irqsource == GIC_IRQ_SPURIOUS) // no further processing is needed
|
||||||
break;
|
break;
|
||||||
|
|
||||||
(gicIrqHandlers[irqn & ~IRQN_SRC_MASK])(irqn);
|
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 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
|
// 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) = irqn;
|
REG_GIC_IRQEND(GIC_THIS_CPU_ALIAS) = irqsource;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,6 +162,7 @@ void gicGlobalReset(void)
|
|||||||
gicn = MAX_CPU;
|
gicn = MAX_CPU;
|
||||||
|
|
||||||
// clear the interrupt handler and config table
|
// clear the interrupt handler and config table
|
||||||
|
getEventIRQ()->reset();
|
||||||
memset(gicIrqHandlers, 0, sizeof(gicIrqHandlers));
|
memset(gicIrqHandlers, 0, sizeof(gicIrqHandlers));
|
||||||
memset(gicIrqConfig, 0, sizeof(gicIrqConfig));
|
memset(gicIrqConfig, 0, sizeof(gicIrqConfig));
|
||||||
|
|
||||||
@ -263,3 +274,27 @@ void gicTriggerSoftInterrupt(u32 softirq)
|
|||||||
{
|
{
|
||||||
REG_DIC_SOFTINT = 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;
|
||||||
|
}
|
||||||
|
@ -76,10 +76,12 @@ __boot:
|
|||||||
b 1b
|
b 1b
|
||||||
|
|
||||||
corezero_start:
|
corezero_start:
|
||||||
@ assume __bss_len is 128 byte aligned
|
@ assumes the .bss section size is 128 byte aligned (or zero)
|
||||||
ldr r0, =__bss_pa
|
ldr r0, =__bss_pa
|
||||||
ldr r1, =__bss_len
|
ldr r1, =__bss_va_end @ calculate the length of .bss using the VA start and end
|
||||||
add r1, r0, r1
|
ldr r2, =__bss_va
|
||||||
|
sub r1, r1, r2
|
||||||
|
add r1, r0, r1 @ fixup to be PA start and end
|
||||||
mov r2, #0
|
mov r2, #0
|
||||||
mov r3, #0
|
mov r3, #0
|
||||||
mov r4, #0
|
mov r4, #0
|
||||||
|
@ -27,6 +27,8 @@
|
|||||||
#include "hw/mcu.h"
|
#include "hw/mcu.h"
|
||||||
#include "hw/gpulcd.h"
|
#include "hw/gpulcd.h"
|
||||||
|
|
||||||
|
#include "system/event.h"
|
||||||
|
|
||||||
static struct
|
static struct
|
||||||
{
|
{
|
||||||
u16 lcdIds; // Bits 0-7 top screen, 8-15 bottom screen.
|
u16 lcdIds; // Bits 0-7 top screen, 8-15 bottom screen.
|
||||||
@ -112,13 +114,13 @@ unsigned GFX_init(GfxFbFmt mode)
|
|||||||
TIMER_WaitMS(10);
|
TIMER_WaitMS(10);
|
||||||
resetLcdsMaybe();
|
resetLcdsMaybe();
|
||||||
MCU_controlLCDPower(2u); // Power on LCDs.
|
MCU_controlLCDPower(2u); // Power on LCDs.
|
||||||
if(MCU_waitEvents(0x3Fu<<24) != 2u<<24) __builtin_trap();
|
if(eventWait(getEventMCU(), 0x3Fu<<24, 0x3Fu<<24) != 2u<<24) __builtin_trap();
|
||||||
|
|
||||||
waitLcdsReady();
|
waitLcdsReady();
|
||||||
REG_LCD_ABL0_LIGHT_PWM = 0x1023E;
|
REG_LCD_ABL0_LIGHT_PWM = 0x1023E;
|
||||||
REG_LCD_ABL1_LIGHT_PWM = 0x1023E;
|
REG_LCD_ABL1_LIGHT_PWM = 0x1023E;
|
||||||
MCU_controlLCDPower(0x28u); // Power on backlights.
|
MCU_controlLCDPower(0x28u); // Power on backlights.
|
||||||
if(MCU_waitEvents(0x3Fu<<24) != 0x28u<<24) __builtin_trap();
|
if(eventWait(getEventMCU(), 0x3Fu<<24, 0x3Fu<<24) != 0x28u<<24) __builtin_trap();
|
||||||
g_gfxState.lcdPower = 0x15; // All on.
|
g_gfxState.lcdPower = 0x15; // All on.
|
||||||
|
|
||||||
// Make sure the fills finished.
|
// Make sure the fills finished.
|
||||||
@ -212,8 +214,9 @@ void GFX_powerOnBacklights(GfxBlight mask)
|
|||||||
|
|
||||||
mask <<= 1;
|
mask <<= 1;
|
||||||
MCU_controlLCDPower(mask); // Power on backlights.
|
MCU_controlLCDPower(mask); // Power on backlights.
|
||||||
if(MCU_waitEvents(0x3Fu<<24) != (u32)mask<<24)
|
eventWait(getEventMCU(), 0x3F<<24, 0x3F<<24);
|
||||||
__builtin_trap();
|
/*if(mcuEventWait(0x3Fu<<24) != (u32)mask<<24)
|
||||||
|
__builtin_trap();*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void GFX_powerOffBacklights(GfxBlight mask)
|
void GFX_powerOffBacklights(GfxBlight mask)
|
||||||
@ -221,8 +224,9 @@ void GFX_powerOffBacklights(GfxBlight mask)
|
|||||||
g_gfxState.lcdPower &= ~mask;
|
g_gfxState.lcdPower &= ~mask;
|
||||||
|
|
||||||
MCU_controlLCDPower(mask); // Power off backlights.
|
MCU_controlLCDPower(mask); // Power off backlights.
|
||||||
if(MCU_waitEvents(0x3Fu<<24) != (u32)mask<<24)
|
eventWait(getEventMCU(), 0x3F<<24, 0x3F<<24);
|
||||||
__builtin_trap();
|
/*if(mcuEventWait(0x3Fu<<24) != (u32)mask<<24)
|
||||||
|
__builtin_trap();*/
|
||||||
}
|
}
|
||||||
|
|
||||||
u8 GFX_getBrightness(void)
|
u8 GFX_getBrightness(void)
|
||||||
|
@ -52,7 +52,7 @@ u64 HID_GetState(void)
|
|||||||
|
|
||||||
CODEC_Get(&codec);
|
CODEC_Get(&codec);
|
||||||
|
|
||||||
ret = REG_HID | MCU_GetSpecialHID();
|
ret = REG_HID | mcuGetSpecialHID();
|
||||||
if (!(ret & BUTTON_ARROW))
|
if (!(ret & BUTTON_ARROW))
|
||||||
ret |= HID_ConvertCPAD(codec.cpad_x, codec.cpad_y);
|
ret |= HID_ConvertCPAD(codec.cpad_x, codec.cpad_y);
|
||||||
|
|
||||||
|
@ -20,40 +20,39 @@
|
|||||||
#include <types.h>
|
#include <types.h>
|
||||||
#include <arm.h>
|
#include <arm.h>
|
||||||
|
|
||||||
|
#include <stdatomic.h>
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
#include "arm/timer.h"
|
#include "arm/timer.h"
|
||||||
|
|
||||||
#include "hw/gpio.h"
|
#include "hw/gpio.h"
|
||||||
#include "hw/gpulcd.h"
|
#include "hw/gpulcd.h"
|
||||||
#include "hw/mcu.h"
|
#include "hw/mcu.h"
|
||||||
|
|
||||||
enum {
|
#include "system/event.h"
|
||||||
MCU_PWR_BTN = 0,
|
|
||||||
MCU_PWR_HOLD = 1,
|
#define MCUEV_HID_MASK ( \
|
||||||
MCU_HOME_BTN = 2,
|
MCUEV_HID_PWR_DOWN | MCUEV_HID_PWR_HOLD | \
|
||||||
MCU_HOME_LIFT = 3,
|
MCUEV_HID_HOME_DOWN | MCUEV_HID_HOME_UP | MCUEV_HID_WIFI_SWITCH)
|
||||||
MCU_WIFI_SWITCH = 4,
|
|
||||||
MCU_SHELL_CLOSE = 5,
|
|
||||||
MCU_SHELL_OPEN = 6,
|
|
||||||
MCU_VOL_SLIDER = 22,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
REG_VOL_SLIDER = 0x09,
|
MCUREG_VOLUME_SLIDER = 0x09,
|
||||||
|
|
||||||
REG_BATTERY_LEVEL = 0x0B,
|
MCUREG_BATTERY_LEVEL = 0x0B,
|
||||||
REG_CONSOLE_STATE = 0x0F,
|
MCUREG_CONSOLE_STATE = 0x0F,
|
||||||
|
|
||||||
REG_INT_MASK = 0x10,
|
MCUREG_INT_MASK = 0x10,
|
||||||
REG_INT_EN = 0x18,
|
MCUREG_INT_EN = 0x18,
|
||||||
|
|
||||||
REG_LCD_STATE = 0x22,
|
MCUREG_LCD_STATE = 0x22,
|
||||||
|
|
||||||
REG_LED_WIFI = 0x2A,
|
MCUREG_LED_WIFI = 0x2A,
|
||||||
REG_LED_CAMERA = 0x2B,
|
MCUREG_LED_CAMERA = 0x2B,
|
||||||
REG_LED_SLIDER = 0x2C,
|
MCUREG_LED_SLIDER = 0x2C,
|
||||||
REG_LED_NOTIF = 0x2D,
|
MCUREG_LED_STATUS = 0x2D,
|
||||||
|
|
||||||
REG_RTC = 0x30,
|
MCUREG_RTC = 0x30,
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -64,47 +63,70 @@ typedef struct {
|
|||||||
u32 red[8];
|
u32 red[8];
|
||||||
u32 green[8];
|
u32 green[8];
|
||||||
u32 blue[8];
|
u32 blue[8];
|
||||||
} PACKED_STRUCT MCU_NotificationLED;
|
} PACKED_STRUCT mcuStatusLED;
|
||||||
|
|
||||||
static u8 cached_volume_slider = 0;
|
static u8 volumeSliderValue;
|
||||||
static u32 spec_hid = 0, shell_state = 0;
|
static u32 shellState;
|
||||||
|
static _Atomic(u32) pendingEvents;
|
||||||
|
|
||||||
static void MCU_UpdateVolumeSlider(void)
|
static void mcuEventUpdate(void)
|
||||||
{
|
{
|
||||||
cached_volume_slider = MCU_ReadReg(REG_VOL_SLIDER);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void MCU_UpdateShellState(bool open)
|
u8 mcuGetVolumeSlider(void)
|
||||||
{
|
{
|
||||||
shell_state = open ? SHELL_OPEN : SHELL_CLOSED;
|
mcuEventUpdate();
|
||||||
|
return volumeSliderValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
u8 MCU_GetVolumeSlider(void)
|
u32 mcuGetSpecialHID(void)
|
||||||
{
|
{
|
||||||
return cached_volume_slider;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 MCU_GetSpecialHID(void)
|
void mcuSetStatusLED(u32 period_ms, u32 color)
|
||||||
{
|
{
|
||||||
u32 ret = spec_hid | shell_state;
|
u32 r, g, b, delay;
|
||||||
spec_hid = 0;
|
mcuStatusLED ledState;
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MCU_SetNotificationLED(u32 period_ms, u32 color)
|
delay = clamp((period_ms * 0x200) / 1000, 1, 0xFF);
|
||||||
{
|
|
||||||
u32 r, g, b;
|
|
||||||
MCU_NotificationLED led_state;
|
|
||||||
|
|
||||||
// handle proper non-zero periods
|
ledState.delay = delay;
|
||||||
// so small the hardware can't handle it
|
ledState.smoothing = delay;
|
||||||
if (period_ms != 0 && period_ms < 63)
|
ledState.loop_delay = 0x10;
|
||||||
period_ms = 63;
|
ledState.unk = 0;
|
||||||
|
|
||||||
led_state.delay = (period_ms * 0x10) / 1000;
|
|
||||||
led_state.smoothing = 0x40;
|
|
||||||
led_state.loop_delay = 0x10;
|
|
||||||
led_state.unk = 0;
|
|
||||||
|
|
||||||
// all colors look like 0x00ZZ00ZZ
|
// all colors look like 0x00ZZ00ZZ
|
||||||
// in order to alternate between
|
// in order to alternate between
|
||||||
@ -112,93 +134,65 @@ void MCU_SetNotificationLED(u32 period_ms, u32 color)
|
|||||||
r = (color >> 16) & 0xFF;
|
r = (color >> 16) & 0xFF;
|
||||||
r |= r << 16;
|
r |= r << 16;
|
||||||
for (int i = 0; i < 8; i++)
|
for (int i = 0; i < 8; i++)
|
||||||
led_state.red[i] = r;
|
ledState.red[i] = r;
|
||||||
|
|
||||||
g = (color >> 8) & 0xFF;
|
g = (color >> 8) & 0xFF;
|
||||||
g |= g << 16;
|
g |= g << 16;
|
||||||
for (int i = 0; i < 8; i++)
|
for (int i = 0; i < 8; i++)
|
||||||
led_state.green[i] = g;
|
ledState.green[i] = g;
|
||||||
|
|
||||||
b = color & 0xFF;
|
b = color & 0xFF;
|
||||||
b |= b << 16;
|
b |= b << 16;
|
||||||
for (int i = 0; i < 8; i++)
|
for (int i = 0; i < 8; i++)
|
||||||
led_state.blue[i] = b;
|
ledState.blue[i] = b;
|
||||||
|
|
||||||
MCU_WriteRegBuf(REG_LED_NOTIF, (const u8*)&led_state, sizeof(led_state));
|
mcuWriteRegBuf(MCUREG_LED_STATUS, (const u8*)&ledState, sizeof(ledState));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MCU_ResetLED(void)
|
void mcuResetLEDs(void)
|
||||||
{
|
{
|
||||||
MCU_WriteReg(REG_LED_WIFI, 0);
|
mcuWriteReg(MCUREG_LED_WIFI, 0);
|
||||||
MCU_WriteReg(REG_LED_CAMERA, 0);
|
mcuWriteReg(MCUREG_LED_CAMERA, 0);
|
||||||
MCU_WriteReg(REG_LED_SLIDER, 0);
|
mcuWriteReg(MCUREG_LED_SLIDER, 0);
|
||||||
MCU_SetNotificationLED(0, 0);
|
mcuSetStatusLED(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MCU_HandleInterrupts(u32 __attribute__((unused)) irqn)
|
void mcuReset(void)
|
||||||
{
|
{
|
||||||
u32 ints;
|
u32 intmask = 0;
|
||||||
|
|
||||||
// Reading the pending mask automagically acknowledges
|
atomic_init(&pendingEvents, 0);
|
||||||
// the interrupts so all of them must be processed in one go
|
|
||||||
MCU_ReadRegBuf(REG_INT_MASK, (u8*)&ints, sizeof(ints));
|
|
||||||
|
|
||||||
while(ints != 0) {
|
// set register mask and clear any pending registers
|
||||||
u32 mcu_int_id = 31 - __builtin_clz(ints);
|
mcuWriteRegBuf(MCUREG_INT_EN, (const u8*)&intmask, sizeof(intmask));
|
||||||
|
mcuReadRegBuf(MCUREG_INT_MASK, (u8*)&intmask, sizeof(intmask));
|
||||||
|
|
||||||
switch(mcu_int_id) {
|
mcuResetLEDs();
|
||||||
case MCU_PWR_BTN:
|
|
||||||
case MCU_PWR_HOLD:
|
|
||||||
spec_hid |= BUTTON_POWER;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MCU_HOME_BTN:
|
volumeSliderValue = mcuReadReg(MCUREG_VOLUME_SLIDER);
|
||||||
spec_hid |= BUTTON_HOME;
|
shellState = SHELL_OPEN;
|
||||||
break;
|
// assume the shell is always open on boot
|
||||||
|
// knowing the average 3DS user, there will be plenty
|
||||||
case MCU_HOME_LIFT:
|
// of laughs when this comes back to bite us in the rear
|
||||||
spec_hid &= ~BUTTON_HOME;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MCU_WIFI_SWITCH:
|
|
||||||
spec_hid |= BUTTON_WIFI;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MCU_SHELL_OPEN:
|
|
||||||
MCU_UpdateShellState(true);
|
|
||||||
MCU_ResetLED();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MCU_SHELL_CLOSE:
|
|
||||||
MCU_UpdateShellState(false);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MCU_VOL_SLIDER:
|
|
||||||
MCU_UpdateVolumeSlider();
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ints &= ~BIT(mcu_int_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MCU_Init(void)
|
|
||||||
{
|
|
||||||
u32 clrpend, mask = 0;
|
|
||||||
|
|
||||||
shell_state = SHELL_OPEN;
|
|
||||||
|
|
||||||
/* set register mask and clear any pending registers */
|
|
||||||
MCU_WriteRegBuf(REG_INT_EN, (const u8*)&mask, sizeof(mask));
|
|
||||||
MCU_ReadRegBuf(REG_INT_MASK, (u8*)&clrpend, sizeof(clrpend));
|
|
||||||
|
|
||||||
MCU_ResetLED();
|
|
||||||
|
|
||||||
MCU_UpdateVolumeSlider();
|
|
||||||
MCU_UpdateShellState(MCU_ReadReg(REG_CONSOLE_STATE) & BIT(1));
|
|
||||||
|
|
||||||
GPIO_setBit(19, 9);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -26,51 +26,48 @@
|
|||||||
#define MCU_INTERRUPT (0x71)
|
#define MCU_INTERRUPT (0x71)
|
||||||
#define I2C_MCU_DEVICE (3)
|
#define I2C_MCU_DEVICE (3)
|
||||||
|
|
||||||
u8 MCU_GetVolumeSlider(void);
|
enum {
|
||||||
u32 MCU_GetSpecialHID(void);
|
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),
|
||||||
|
};
|
||||||
|
|
||||||
void MCU_SetNotificationLED(u32 period_ms, u32 color);
|
u8 mcuGetVolumeSlider(void);
|
||||||
void MCU_ResetLED(void);
|
u32 mcuGetSpecialHID(void);
|
||||||
|
|
||||||
void MCU_HandleInterrupts(u32 irqn);
|
void mcuSetStatusLED(u32 period_ms, u32 color);
|
||||||
|
void mcuResetLEDs(void);
|
||||||
|
|
||||||
void MCU_Init(void);
|
void mcuReset(void);
|
||||||
|
|
||||||
static inline u8 MCU_ReadReg(u8 addr)
|
static inline u8 mcuReadReg(u8 addr)
|
||||||
{
|
{
|
||||||
u8 val;
|
u8 val;
|
||||||
I2C_readRegBuf(I2C_MCU_DEVICE, addr, &val, 1);
|
I2C_readRegBuf(I2C_MCU_DEVICE, addr, &val, 1);
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool MCU_ReadRegBuf(u8 addr, u8 *buf, u32 size)
|
static inline bool mcuReadRegBuf(u8 addr, u8 *buf, u32 size)
|
||||||
{
|
{
|
||||||
return I2C_readRegBuf(I2C_MCU_DEVICE, addr, buf, size);
|
return I2C_readRegBuf(I2C_MCU_DEVICE, addr, buf, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool MCU_WriteReg(u8 addr, u8 val)
|
static inline bool mcuWriteReg(u8 addr, u8 val)
|
||||||
{
|
{
|
||||||
return I2C_writeRegBuf(I2C_MCU_DEVICE, addr, &val, 1);
|
return I2C_writeRegBuf(I2C_MCU_DEVICE, addr, &val, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool MCU_WriteRegBuf(u8 addr, const u8 *buf, u32 size)
|
static inline bool mcuWriteRegBuf(u8 addr, const u8 *buf, u32 size)
|
||||||
{
|
{
|
||||||
return I2C_writeRegBuf(I2C_MCU_DEVICE, addr, buf, size);
|
return I2C_writeRegBuf(I2C_MCU_DEVICE, addr, buf, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline u32 MCU_waitEvents(u32 mask) {
|
|
||||||
u32 v;
|
|
||||||
while(1) {
|
|
||||||
TIMER_WaitMS(10);
|
|
||||||
MCU_ReadRegBuf(0x10, (u8*)&v, 4);
|
|
||||||
v &= mask;
|
|
||||||
if (v)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void MCU_controlLCDPower(u8 bits)
|
static inline void MCU_controlLCDPower(u8 bits)
|
||||||
{
|
{
|
||||||
MCU_WriteReg(0x22u, bits);
|
mcuWriteReg(0x22u, bits);
|
||||||
}
|
}
|
||||||
|
@ -31,8 +31,9 @@
|
|||||||
#include "hw/nvram.h"
|
#include "hw/nvram.h"
|
||||||
|
|
||||||
#include "system/sys.h"
|
#include "system/sys.h"
|
||||||
|
#include "system/event.h"
|
||||||
|
|
||||||
static const u8 brLvlTbl[] = {
|
static const u8 brightness_lvls[] = {
|
||||||
0x10, 0x17, 0x1E, 0x25,
|
0x10, 0x17, 0x1E, 0x25,
|
||||||
0x2C, 0x34, 0x3C, 0x44,
|
0x2C, 0x34, 0x3C, 0x44,
|
||||||
0x4D, 0x56, 0x60, 0x6B,
|
0x4D, 0x56, 0x60, 0x6B,
|
||||||
@ -40,170 +41,185 @@ static const u8 brLvlTbl[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
#ifndef FIXED_BRIGHTNESS
|
#ifndef FIXED_BRIGHTNESS
|
||||||
static int oldBrLvl;
|
static int prev_bright_lvl;
|
||||||
static bool autoBr;
|
static bool auto_brightness;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static SystemSHMEM __attribute__((section(".shared"))) SharedMemoryState;
|
static SystemSHMEM __attribute__((section(".shared"))) sharedMem;
|
||||||
|
|
||||||
void VBlank_Handler(u32 __attribute__((unused)) irqn)
|
static void vblankUpdate(void)
|
||||||
{
|
{
|
||||||
|
if (!getEventIRQ()->test(VBLANK_INTERRUPT, true))
|
||||||
|
return;
|
||||||
|
|
||||||
#ifndef FIXED_BRIGHTNESS
|
#ifndef FIXED_BRIGHTNESS
|
||||||
int newBrLvl = (MCU_GetVolumeSlider() >> 2) % countof(brLvlTbl);
|
int cur_bright_lvl = (mcuGetVolumeSlider() >> 2) % countof(brightness_lvls);
|
||||||
if ((newBrLvl != oldBrLvl) && autoBr) {
|
if ((cur_bright_lvl != prev_bright_lvl) && auto_brightness) {
|
||||||
oldBrLvl = newBrLvl;
|
prev_bright_lvl = cur_bright_lvl;
|
||||||
u8 br = brLvlTbl[newBrLvl];
|
u8 br = brightness_lvls[cur_bright_lvl];
|
||||||
GFX_setBrightness(br, br);
|
GFX_setBrightness(br, br);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
SharedMemoryState.hidState.full = HID_GetState();
|
// 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 bool legacy_boot = false;
|
static u32 pxiRxUpdate(u32 *args)
|
||||||
|
|
||||||
void PXI_RX_Handler(u32 __attribute__((unused)) irqn)
|
|
||||||
{
|
{
|
||||||
u32 ret, msg, cmd, argc, args[PXI_MAX_ARGS];
|
u32 msg, lo, hi;
|
||||||
|
|
||||||
|
if (!getEventIRQ()->test(PXI_RX_INTERRUPT, true))
|
||||||
|
return PXICMD_NONE;
|
||||||
|
|
||||||
msg = PXI_Recv();
|
msg = PXI_Recv();
|
||||||
cmd = msg & 0xFFFF;
|
lo = msg & 0xFFFF;
|
||||||
argc = msg >> 16;
|
hi = msg >> 16;
|
||||||
|
|
||||||
if (argc >= PXI_MAX_ARGS) {
|
PXI_RecvArray(args, hi);
|
||||||
PXI_Send(0xFFFFFFFF);
|
return lo;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
PXI_RecvArray(args, argc);
|
|
||||||
|
|
||||||
switch (cmd) {
|
|
||||||
case PXI_LEGACY_MODE:
|
|
||||||
{
|
|
||||||
// TODO: If SMP is enabled, an IPI should be sent here (with a DSB)
|
|
||||||
legacy_boot = true;
|
|
||||||
ret = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case PXI_GET_SHMEM:
|
|
||||||
{
|
|
||||||
ret = (u32)&SharedMemoryState;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case PXI_SET_VMODE:
|
|
||||||
{
|
|
||||||
GFX_init(args[0] ? GFX_BGR8 : GFX_RGB565);
|
|
||||||
ret = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case PXI_I2C_READ:
|
|
||||||
{
|
|
||||||
u32 devId, regAddr, size;
|
|
||||||
|
|
||||||
devId = (args[0] & 0xff);
|
|
||||||
regAddr = (args[0] >> 8) & 0xff;
|
|
||||||
size = (args[0] >> 16) % I2C_SHARED_BUFSZ;
|
|
||||||
|
|
||||||
ret = I2C_readRegBuf(devId, regAddr, SharedMemoryState.i2cBuffer, size);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case PXI_I2C_WRITE:
|
|
||||||
{
|
|
||||||
u32 devId, regAddr, size;
|
|
||||||
|
|
||||||
devId = (args[0] & 0xff);
|
|
||||||
regAddr = (args[0] >> 8) & 0xff;
|
|
||||||
size = (args[0] >> 16) % I2C_SHARED_BUFSZ;
|
|
||||||
|
|
||||||
ret = I2C_writeRegBuf(devId, regAddr, SharedMemoryState.i2cBuffer, size);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case PXI_NVRAM_ONLINE:
|
|
||||||
{
|
|
||||||
ret = (NVRAM_Status() & NVRAM_SR_WIP) == 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case PXI_NVRAM_READ:
|
|
||||||
{
|
|
||||||
NVRAM_Read(args[0], (u32*)SharedMemoryState.spiBuffer, args[1]);
|
|
||||||
ret = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case PXI_NOTIFY_LED:
|
|
||||||
{
|
|
||||||
MCU_SetNotificationLED(args[0], args[1]);
|
|
||||||
ret = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case PXI_BRIGHTNESS:
|
|
||||||
{
|
|
||||||
ret = GFX_getBrightness();
|
|
||||||
#ifndef FIXED_BRIGHTNESS
|
|
||||||
s32 newbrightness = (s32)args[0];
|
|
||||||
if ((newbrightness > 0) && (newbrightness < 0x100)) {
|
|
||||||
GFX_setBrightness(newbrightness, newbrightness);
|
|
||||||
autoBr = false;
|
|
||||||
} else {
|
|
||||||
oldBrLvl = -1;
|
|
||||||
autoBr = true;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* New CMD template:
|
|
||||||
case CMD_ID:
|
|
||||||
{
|
|
||||||
<var declarations/assignments>
|
|
||||||
<execute the command>
|
|
||||||
<set the return value>
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
default:
|
|
||||||
ret = 0xFFFFFFFF;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
PXI_Send(ret);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void __attribute__((noreturn)) MainLoop(void)
|
void __attribute__((noreturn)) MainLoop(void)
|
||||||
{
|
{
|
||||||
|
bool runPxiCmdProcessor = true;
|
||||||
|
|
||||||
#ifdef FIXED_BRIGHTNESS
|
#ifdef FIXED_BRIGHTNESS
|
||||||
u8 fixBrLvl = brLvlTbl[clamp(FIXED_BRIGHTNESS, 0, countof(brLvlTbl)-1)];
|
u8 fixed_bright_lvl = brightness_lvls[clamp(FIXED_BRIGHTNESS, 0, countof(brightness_lvls)-1)];
|
||||||
GFX_setBrightness(fixBrLvl, fixBrLvl);
|
GFX_setBrightness(fixed_bright_lvl, fixed_bright_lvl);
|
||||||
#else
|
#else
|
||||||
oldBrLvl = -1;
|
prev_bright_lvl = -1;
|
||||||
autoBr = true;
|
auto_brightness = true;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// initialize state stuff
|
||||||
|
getEventIRQ()->reset();
|
||||||
|
getEventMCU()->reset();
|
||||||
|
memset(&sharedMem, 0, sizeof(sharedMem));
|
||||||
|
|
||||||
// configure interrupts
|
// configure interrupts
|
||||||
gicSetInterruptConfig(PXI_RX_INTERRUPT, BIT(0), GIC_PRIO2, PXI_RX_Handler);
|
gicSetInterruptConfig(PXI_RX_INTERRUPT, BIT(0), GIC_PRIO0, NULL);
|
||||||
gicSetInterruptConfig(MCU_INTERRUPT, BIT(0), GIC_PRIO1, MCU_HandleInterrupts);
|
gicSetInterruptConfig(VBLANK_INTERRUPT, BIT(0), GIC_PRIO0, NULL);
|
||||||
gicSetInterruptConfig(VBLANK_INTERRUPT, BIT(0), GIC_PRIO0, VBlank_Handler);
|
gicSetInterruptConfig(MCU_INTERRUPT, BIT(0), GIC_PRIO0, NULL);
|
||||||
|
|
||||||
// enable interrupts
|
// enable interrupts
|
||||||
gicEnableInterrupt(PXI_RX_INTERRUPT);
|
|
||||||
gicEnableInterrupt(MCU_INTERRUPT);
|
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);
|
gicEnableInterrupt(VBLANK_INTERRUPT);
|
||||||
|
|
||||||
// ARM9 won't try anything funny until this point
|
// ARM9 won't try anything funny until this point
|
||||||
PXI_Barrier(PXI_BOOT_BARRIER);
|
PXI_Barrier(PXI_BOOT_BARRIER);
|
||||||
|
|
||||||
// Process IRQs until the ARM9 tells us it's time to boot something else
|
// Process commands until the ARM9 tells
|
||||||
|
// us it's time to boot something else
|
||||||
|
// also handles VBlank events as needed
|
||||||
do {
|
do {
|
||||||
ARM_WFI();
|
u32 pxiCmd, pxiReply, args[PXI_MAX_ARGS];
|
||||||
} while(!legacy_boot);
|
|
||||||
|
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
|
// Wait for the ARM9 to do its firmlaunch setup
|
||||||
PXI_Barrier(PXI_FIRMLAUNCH_BARRIER);
|
PXI_Barrier(PXI_FIRMLAUNCH_BARRIER);
|
||||||
|
26
arm11/source/system/event.h
Normal file
26
arm11/source/system/event.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
|
}
|
@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
#include <types.h>
|
#include <types.h>
|
||||||
|
|
||||||
#define DEF_SECT_(n) extern u32 __##n##_pa, __##n##_va, __##n##_len;
|
#define DEF_SECT_(n) extern u32 __##n##_pa, __##n##_va, __##n##_va_end;
|
||||||
DEF_SECT_(text)
|
DEF_SECT_(text)
|
||||||
DEF_SECT_(data)
|
DEF_SECT_(data)
|
||||||
DEF_SECT_(rodata)
|
DEF_SECT_(rodata)
|
||||||
@ -30,6 +30,6 @@ DEF_SECT_(shared)
|
|||||||
|
|
||||||
#define SECTION_VA(n) ((u32)&__##n##_va)
|
#define SECTION_VA(n) ((u32)&__##n##_va)
|
||||||
#define SECTION_PA(n) ((u32)&__##n##_pa)
|
#define SECTION_PA(n) ((u32)&__##n##_pa)
|
||||||
#define SECTION_LEN(n) ((u32)&__##n##_len)
|
#define SECTION_LEN(n) (((u32)(&__##n##_va_end) - (u32)(&__##n##_va)))
|
||||||
|
|
||||||
#define SECTION_TRI(n) SECTION_VA(n), SECTION_PA(n), SECTION_LEN(n)
|
#define SECTION_TRI(n) SECTION_VA(n), SECTION_PA(n), SECTION_LEN(n)
|
||||||
|
@ -107,20 +107,17 @@ void SYS_CoreZeroInit(void)
|
|||||||
mmuMapArea(0x20000000, 0x20000000, 128UL << 20, MMU_FLAGS(MMU_CACHE_WB, MMU_READ_WRITE, 1, 1));
|
mmuMapArea(0x20000000, 0x20000000, 128UL << 20, MMU_FLAGS(MMU_CACHE_WB, MMU_READ_WRITE, 1, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SYS_IsNewConsole()) {
|
// screen init magicks
|
||||||
TIMER_WaitMS(150);
|
TIMER_WaitMS(64);
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize peripherals
|
// Initialize peripherals
|
||||||
PXI_Reset();
|
PXI_Reset();
|
||||||
|
|
||||||
I2C_init();
|
I2C_init();
|
||||||
MCU_Init();
|
mcuReset();
|
||||||
|
|
||||||
SPI_Init();
|
SPI_Init();
|
||||||
CODEC_Init();
|
CODEC_Init();
|
||||||
|
|
||||||
GFX_init(GFX_RGB565);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SYS_CoreInit(void)
|
void SYS_CoreInit(void)
|
||||||
|
@ -1,17 +1,24 @@
|
|||||||
PROCESSOR := ARM9
|
PROCESSOR := ARM9
|
||||||
|
|
||||||
TARGET := $(shell basename $(CURDIR))
|
TARGET := $(shell basename "$(CURDIR)")
|
||||||
|
|
||||||
SOURCE := source
|
SOURCE := source
|
||||||
BUILD := build
|
BUILD := build
|
||||||
|
|
||||||
SUBARCH := -D$(PROCESSOR) -march=armv5te -mtune=arm946e-s -mthumb -mfloat-abi=soft
|
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/qrcodegen source/system source/utils
|
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)")
|
INCLUDE := $(foreach dir,$(INCDIRS),-I"$(shell pwd)/$(dir)")
|
||||||
|
|
||||||
ASFLAGS += $(SUBARCH) $(INCLUDE)
|
ASFLAGS += $(SUBARCH) $(INCLUDE)
|
||||||
CFLAGS += $(SUBARCH) $(INCLUDE) -fno-builtin-memcpy -flto
|
CFLAGS += $(SUBARCH) $(INCLUDE) -fno-builtin-memcpy -flto
|
||||||
LDFLAGS += $(SUBARCH) -Wl,-Map,$(TARGET).map -flto
|
LDFLAGS += $(SUBARCH) -Wl,--use-blx,-Map,$(TARGET).map -flto
|
||||||
|
LIBS += -lm
|
||||||
|
|
||||||
include ../Makefile.common
|
include ../Makefile.common
|
||||||
include ../Makefile.build
|
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* $< $@
|
||||||
|
20
arm9/link.ld
20
arm9/link.ld
@ -4,8 +4,10 @@ ENTRY(_start)
|
|||||||
|
|
||||||
MEMORY
|
MEMORY
|
||||||
{
|
{
|
||||||
AHBWRAM (RWX) : ORIGIN = 0x08006000, LENGTH = 512K
|
|
||||||
VECTORS (RX) : ORIGIN = 0x08000000, LENGTH = 64
|
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
|
SECTIONS
|
||||||
@ -16,7 +18,7 @@ SECTIONS
|
|||||||
KEEP(*(.vectors));
|
KEEP(*(.vectors));
|
||||||
. = ALIGN(4);
|
. = ALIGN(4);
|
||||||
__vectors_len = ABSOLUTE(.) - __vectors_vma;
|
__vectors_len = ABSOLUTE(.) - __vectors_vma;
|
||||||
} >VECTORS AT>AHBWRAM
|
} >VECTORS AT>CODEMEM
|
||||||
|
|
||||||
.text : ALIGN(4) {
|
.text : ALIGN(4) {
|
||||||
__text_s = ABSOLUTE(.);
|
__text_s = ABSOLUTE(.);
|
||||||
@ -24,24 +26,28 @@ SECTIONS
|
|||||||
*(.text*);
|
*(.text*);
|
||||||
. = ALIGN(4);
|
. = ALIGN(4);
|
||||||
__text_e = ABSOLUTE(.);
|
__text_e = ABSOLUTE(.);
|
||||||
} >AHBWRAM
|
} >CODEMEM
|
||||||
|
|
||||||
.rodata : ALIGN(4) {
|
.rodata : ALIGN(4) {
|
||||||
*(.rodata*);
|
*(.rodata*);
|
||||||
. = ALIGN(4);
|
. = ALIGN(4);
|
||||||
} >AHBWRAM
|
__exidx_start = .;
|
||||||
|
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
|
||||||
|
__exidx_end = .;
|
||||||
|
. = ALIGN(4);
|
||||||
|
} >DATAMEM
|
||||||
|
|
||||||
.data : ALIGN(4) {
|
.data : ALIGN(4) {
|
||||||
*(.data*);
|
*(.data*);
|
||||||
. = ALIGN(4);
|
. = ALIGN(4);
|
||||||
} >AHBWRAM
|
} >DATAMEM
|
||||||
|
|
||||||
.bss : ALIGN(4) {
|
.bss (NOLOAD) : ALIGN(4) {
|
||||||
__bss_start = .;
|
__bss_start = .;
|
||||||
*(.bss*);
|
*(.bss*);
|
||||||
. = ALIGN(4);
|
. = ALIGN(4);
|
||||||
__bss_end = .;
|
__bss_end = .;
|
||||||
} >AHBWRAM
|
} >DATAMEM
|
||||||
|
|
||||||
__end__ = ABSOLUTE(.);
|
__end__ = ABSOLUTE(.);
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ static void SetNotificationLED(u32 period_ms, u32 rgb565_color)
|
|||||||
(rgb565_color >> 5) << (8+2) |
|
(rgb565_color >> 5) << (8+2) |
|
||||||
(rgb565_color << 3));
|
(rgb565_color << 3));
|
||||||
u32 args[] = {period_ms, rgb888_color};
|
u32 args[] = {period_ms, rgb888_color};
|
||||||
PXI_DoCMD(PXI_NOTIFY_LED, args, 2);
|
PXI_DoCMD(PXICMD_SET_NOTIFY_LED, args, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// there's some weird thing going on when reading this
|
// there's some weird thing going on when reading this
|
||||||
|
@ -12,7 +12,7 @@ u32 SetScreenBrightness(int level) {
|
|||||||
arg = 0;
|
arg = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return PXI_DoCMD(PXI_BRIGHTNESS, &arg, 1);
|
return PXI_DoCMD(PXICMD_SET_BRIGHTNESS, &arg, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 GetBatteryPercent() {
|
u32 GetBatteryPercent() {
|
||||||
|
@ -27,7 +27,7 @@ void CreateScreenshot(void) {
|
|||||||
|
|
||||||
fvx_rmkdir(OUTPUT_PATH);
|
fvx_rmkdir(OUTPUT_PATH);
|
||||||
get_dstime(&dstime);
|
get_dstime(&dstime);
|
||||||
snprintf(filename, 64, OUTPUT_PATH "/snap_%02X%02X%02X%02X%02X%02X.png",
|
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_Y, dstime.bcd_M, dstime.bcd_D,
|
||||||
dstime.bcd_h, dstime.bcd_m, dstime.bcd_s);
|
dstime.bcd_h, dstime.bcd_m, dstime.bcd_s);
|
||||||
filename[63] = '\0';
|
filename[63] = '\0';
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
#include "language.h"
|
||||||
#include "swkbd.h"
|
#include "swkbd.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
#include "hid.h"
|
#include "hid.h"
|
||||||
|
#include "utf.h"
|
||||||
|
|
||||||
|
|
||||||
static inline char to_uppercase(char c) {
|
static inline char to_uppercase(char c) {
|
||||||
@ -79,7 +81,7 @@ static void DrawKey(const TouchBox* key, const bool pressed, const u32 uppercase
|
|||||||
if (key->id == KEY_TXTBOX) return;
|
if (key->id == KEY_TXTBOX) return;
|
||||||
|
|
||||||
char keystr[16];
|
char keystr[16];
|
||||||
if (key->id >= 0x80) snprintf(keystr, 16, "%s", keystrs[key->id - 0x80]);
|
if (key->id >= 0x80) snprintf(keystr, sizeof(keystr), "%s", keystrs[key->id - 0x80]);
|
||||||
else {
|
else {
|
||||||
keystr[0] = (uppercase) ? to_uppercase(key->id) : key->id;
|
keystr[0] = (uppercase) ? to_uppercase(key->id) : key->id;
|
||||||
keystr[1] = 0;
|
keystr[1] = 0;
|
||||||
@ -90,7 +92,7 @@ static void DrawKey(const TouchBox* key, const bool pressed, const u32 uppercase
|
|||||||
const u32 f_offs_y = (key->h - FONT_HEIGHT_EXT) / 2;
|
const u32 f_offs_y = (key->h - FONT_HEIGHT_EXT) / 2;
|
||||||
|
|
||||||
DrawRectangle(BOT_SCREEN, key->x, key->y, key->w, key->h, color);
|
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, false);
|
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) {
|
static void DrawKeyBoardBox(TouchBox* swkbd, u32 color) {
|
||||||
@ -119,34 +121,50 @@ static void DrawKeyBoard(TouchBox* swkbd, const u32 uppercase) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void DrawTextBox(const TouchBox* txtbox, const char* inputstr, const u32 cursor, u32* scroll) {
|
static void DrawTextBox(const TouchBox* txtbox, const char* inputstr, const u32 cursor, u32* scroll) {
|
||||||
const u32 input_shown = (txtbox->w / FONT_WIDTH_EXT) - 2;
|
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 u32 inputstr_size = strlen(inputstr); // we rely on a zero terminated string
|
||||||
const u16 x = txtbox->x;
|
const u16 x = txtbox->x;
|
||||||
const u16 y = txtbox->y;
|
const u16 y = txtbox->y;
|
||||||
|
|
||||||
// fix scroll
|
// fix scroll
|
||||||
if (cursor < *scroll) *scroll = cursor;
|
if (cursor < *scroll) {
|
||||||
else if (cursor - *scroll > input_shown) *scroll = cursor - input_shown;
|
*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
|
// draw input string
|
||||||
DrawStringF(BOT_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%c%-*.*s%-*.*s%c",
|
DrawStringF(BOT_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%c%-*.*s%c",
|
||||||
(*scroll) ? '<' : '|',
|
(*scroll) ? '<' : '|',
|
||||||
(inputstr_size > input_shown) ? input_shown : inputstr_size,
|
(int) input_shown_size,
|
||||||
(inputstr_size > input_shown) ? input_shown : inputstr_size,
|
(int) input_shown_size,
|
||||||
(*scroll > inputstr_size) ? "" : inputstr + *scroll,
|
(*scroll > inputstr_size) ? "" : inputstr + *scroll,
|
||||||
(inputstr_size > input_shown) ? 0 : input_shown - inputstr_size,
|
(inputstr_size - (s32) *scroll > input_shown_size) ? '>' : '|'
|
||||||
(inputstr_size > input_shown) ? 0 : input_shown - inputstr_size,
|
|
||||||
"",
|
|
||||||
(inputstr_size - (s32) *scroll > input_shown) ? '>' : '|'
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// draw cursor
|
// 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",
|
DrawStringF(BOT_SCREEN, x-(FONT_WIDTH_EXT/2), y+10, COLOR_STD_FONT, COLOR_STD_BG, "%-*.*s^%-*.*s",
|
||||||
1 + cursor - *scroll,
|
(int) (1 + cpos),
|
||||||
1 + cursor - *scroll,
|
(int) (1 + cpos),
|
||||||
"",
|
"",
|
||||||
input_shown - (cursor - *scroll),
|
(int) (input_shown_length - cpos),
|
||||||
input_shown - (cursor - *scroll),
|
(int) (input_shown_length - cpos),
|
||||||
""
|
""
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -163,8 +181,17 @@ static void MoveTextBoxCursor(const TouchBox* txtbox, const char* inputstr, cons
|
|||||||
const TouchBox* tb = TouchBoxGet(&id, x, y, txtbox, 0);
|
const TouchBox* tb = TouchBoxGet(&id, x, y, txtbox, 0);
|
||||||
if (id == KEY_TXTBOX) {
|
if (id == KEY_TXTBOX) {
|
||||||
u16 x_tb = x - tb->x;
|
u16 x_tb = x - tb->x;
|
||||||
u16 cpos = (x_tb < (FONT_WIDTH_EXT/2)) ? 0 : (x_tb - (FONT_WIDTH_EXT/2)) / FONT_WIDTH_EXT;
|
|
||||||
u32 cursor_next = *scroll + ((cpos <= input_shown) ? cpos : input_shown);
|
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
|
// move cursor to position pointed to
|
||||||
if (*cursor != cursor_next) {
|
if (*cursor != cursor_next) {
|
||||||
if (cursor_next < max_size) *cursor = cursor_next;
|
if (cursor_next < max_size) *cursor = cursor_next;
|
||||||
@ -173,10 +200,10 @@ static void MoveTextBoxCursor(const TouchBox* txtbox, const char* inputstr, cons
|
|||||||
}
|
}
|
||||||
// move beyound visible field
|
// move beyound visible field
|
||||||
if (timer_msec(timer) >= scroll_cooldown) {
|
if (timer_msec(timer) >= scroll_cooldown) {
|
||||||
if ((cpos == 0) && (*scroll > 0))
|
if ((cpos_length == 0) && (*scroll > 0))
|
||||||
(*scroll)--;
|
*scroll -= GetPrevCharSize(inputstr + *scroll);
|
||||||
else if ((cpos >= input_shown) && (*cursor < (max_size-1)))
|
else if ((cpos_length >= input_shown) && (*cursor < (max_size-1)))
|
||||||
(*scroll)++;
|
*scroll += GetCharSize(inputstr + *scroll);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -226,6 +253,13 @@ bool ShowKeyboard(char* inputstr, const u32 max_size, const char *format, ...) {
|
|||||||
TouchBox swkbd_numpad[32];
|
TouchBox swkbd_numpad[32];
|
||||||
TouchBox* textbox = swkbd_alphabet; // always use this textbox
|
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
|
// generate keyboards
|
||||||
if (!BuildKeyboard(swkbd_alphabet, keys_alphabet, layout_alphabet)) return false;
|
if (!BuildKeyboard(swkbd_alphabet, keys_alphabet, layout_alphabet)) return false;
|
||||||
if (!BuildKeyboard(swkbd_special, keys_special, layout_special)) return false;
|
if (!BuildKeyboard(swkbd_special, keys_special, layout_special)) return false;
|
||||||
@ -235,7 +269,7 @@ bool ShowKeyboard(char* inputstr, const u32 max_size, const char *format, ...) {
|
|||||||
char str[512]; // arbitrary limit, should be more than enough
|
char str[512]; // arbitrary limit, should be more than enough
|
||||||
va_list va;
|
va_list va;
|
||||||
va_start(va, format);
|
va_start(va, format);
|
||||||
vsnprintf(str, 512, format, va);
|
vsnprintf(str, sizeof(str), format, va);
|
||||||
va_end(va);
|
va_end(va);
|
||||||
u32 str_width = GetDrawStringWidth(str);
|
u32 str_width = GetDrawStringWidth(str);
|
||||||
if (str_width < (24 * FONT_WIDTH_EXT)) str_width = 24 * FONT_WIDTH_EXT;
|
if (str_width < (24 * FONT_WIDTH_EXT)) str_width = 24 * FONT_WIDTH_EXT;
|
||||||
@ -279,16 +313,18 @@ bool ShowKeyboard(char* inputstr, const u32 max_size, const char *format, ...) {
|
|||||||
break;
|
break;
|
||||||
} else if (key == KEY_BKSPC) {
|
} else if (key == KEY_BKSPC) {
|
||||||
if (cursor) {
|
if (cursor) {
|
||||||
|
int size = GetPrevCharSize(inputstr + cursor);
|
||||||
if (cursor <= inputstr_size) {
|
if (cursor <= inputstr_size) {
|
||||||
memmove(inputstr + cursor - 1, inputstr + cursor, inputstr_size - cursor + 1);
|
memmove(inputstr + cursor - size, inputstr + cursor, inputstr_size - cursor + size);
|
||||||
inputstr_size--;
|
inputstr_size -= size;
|
||||||
}
|
}
|
||||||
cursor--;
|
cursor -= size;
|
||||||
}
|
}
|
||||||
} else if (key == KEY_LEFT) {
|
} else if (key == KEY_LEFT) {
|
||||||
if (cursor) cursor--;
|
if (cursor) cursor -= GetPrevCharSize(inputstr + cursor);
|
||||||
} else if (key == KEY_RIGHT) {
|
} else if (key == KEY_RIGHT) {
|
||||||
if (cursor < (max_size-1)) cursor++;
|
int size = cursor > inputstr_size ? 1 : GetCharSize(inputstr + cursor);
|
||||||
|
if (cursor + size < max_size) cursor += size;
|
||||||
} else if (key == KEY_ALPHA) {
|
} else if (key == KEY_ALPHA) {
|
||||||
swkbd = swkbd_alphabet;
|
swkbd = swkbd_alphabet;
|
||||||
} else if (key == KEY_SPECIAL) {
|
} else if (key == KEY_SPECIAL) {
|
||||||
@ -297,7 +333,31 @@ bool ShowKeyboard(char* inputstr, const u32 max_size, const char *format, ...) {
|
|||||||
swkbd = swkbd_numpad;
|
swkbd = swkbd_numpad;
|
||||||
} else if (key == KEY_SWITCH) {
|
} else if (key == KEY_SWITCH) {
|
||||||
ClearScreen(BOT_SCREEN, COLOR_STD_BG);
|
ClearScreen(BOT_SCREEN, COLOR_STD_BG);
|
||||||
return ShowStringPrompt(inputstr, max_size, str);
|
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)) {
|
} else if (key && (key < 0x80)) {
|
||||||
if ((cursor < (max_size-1)) && (inputstr_size < max_size)) {
|
if ((cursor < (max_size-1)) && (inputstr_size < max_size)) {
|
||||||
// pad string (if cursor beyound string size)
|
// pad string (if cursor beyound string size)
|
||||||
|
@ -19,11 +19,12 @@ enum {
|
|||||||
KEY_RIGHT = 0x89,
|
KEY_RIGHT = 0x89,
|
||||||
KEY_ESCAPE = 0x8A,
|
KEY_ESCAPE = 0x8A,
|
||||||
KEY_SWITCH = 0x8B,
|
KEY_SWITCH = 0x8B,
|
||||||
|
KEY_UNICODE = 0x8C,
|
||||||
KEY_TXTBOX = 0xFF
|
KEY_TXTBOX = 0xFF
|
||||||
};
|
};
|
||||||
|
|
||||||
// special key strings
|
// special key strings
|
||||||
#define SWKBD_KEYSTR "", "DEL", "INS", "SUBMIT", "CAPS", "#$@", "123", "ABC", "\x1b", "\x1a", "ESC", "SWITCH"
|
#define SWKBD_KEYSTR "", "DEL", "INS", "SUBMIT", "CAPS", "#$@", "123", "ABC", "←", "→", "ESC", "SWITCH", "U+"
|
||||||
|
|
||||||
#define COLOR_SWKBD_NORMAL COLOR_GREY
|
#define COLOR_SWKBD_NORMAL COLOR_GREY
|
||||||
#define COLOR_SWKBD_PRESSED COLOR_LIGHTGREY
|
#define COLOR_SWKBD_PRESSED COLOR_LIGHTGREY
|
||||||
@ -56,7 +57,7 @@ enum {
|
|||||||
'4', '5', '6', 'D', 'C', \
|
'4', '5', '6', 'D', 'C', \
|
||||||
'3', '2', '1', 'B', 'A', \
|
'3', '2', '1', 'B', 'A', \
|
||||||
'0', '.', '_', KEY_LEFT, KEY_RIGHT, \
|
'0', '.', '_', KEY_LEFT, KEY_RIGHT, \
|
||||||
KEY_ALPHA, ' ', KEY_BKSPC
|
KEY_ALPHA, KEY_UNICODE, ' ', KEY_BKSPC
|
||||||
|
|
||||||
// offset, num of keys in row, width of special keys (...), 0
|
// offset, num of keys in row, width of special keys (...), 0
|
||||||
#define SWKBD_LAYOUT_ALPHABET \
|
#define SWKBD_LAYOUT_ALPHABET \
|
||||||
@ -80,9 +81,9 @@ enum {
|
|||||||
5, 0, \
|
5, 0, \
|
||||||
5, 0, \
|
5, 0, \
|
||||||
5, 18, 18, 0, \
|
5, 18, 18, 0, \
|
||||||
3, 30, 34, 30, 0, \
|
4, 20, 20, 31, 20, 0, \
|
||||||
0
|
0
|
||||||
|
|
||||||
|
|
||||||
#define ShowKeyboardOrPrompt (TouchIsCalibrated() ? ShowKeyboard : ShowStringPrompt)
|
#define ShowKeyboardOrPrompt (TouchIsCalibrated() ? ShowKeyboard : ShowStringPrompt)
|
||||||
bool ShowKeyboard(char* inputstr, u32 max_size, const char *format, ...);
|
bool PRINTF_ARGS(3) ShowKeyboard(char* inputstr, u32 max_size, const char *format, ...);
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
#include "hid.h"
|
#include "hid.h"
|
||||||
#include "crc16.h"
|
#include "crc16.h"
|
||||||
|
#include "language.h"
|
||||||
#include "spiflash.h"
|
#include "spiflash.h"
|
||||||
#include "support.h"
|
#include "support.h"
|
||||||
|
|
||||||
@ -46,8 +47,8 @@ bool ShowTouchCalibrationDialog(void)
|
|||||||
|
|
||||||
// clear screen, draw instructions
|
// clear screen, draw instructions
|
||||||
ClearScreen(BOT_SCREEN, COLOR_STD_BG);
|
ClearScreen(BOT_SCREEN, COLOR_STD_BG);
|
||||||
DrawStringCenter(BOT_SCREEN, COLOR_STD_FONT, COLOR_STD_BG,
|
DrawStringCenter(BOT_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s",
|
||||||
"Touch the red crosshairs to\ncalibrate your touchscreen.\n \nUse the stylus for best\nresults!");
|
STR_TOUCH_CROSSHAIRS_TO_CALIBRATE_TOUCHSCREEN_USE_STYLUS);
|
||||||
|
|
||||||
// set calibration defaults
|
// set calibration defaults
|
||||||
SetCalibrationDefaults();
|
SetCalibrationDefaults();
|
||||||
|
@ -15,21 +15,134 @@
|
|||||||
#include "power.h"
|
#include "power.h"
|
||||||
#include "hid.h"
|
#include "hid.h"
|
||||||
#include "fixp.h"
|
#include "fixp.h"
|
||||||
|
#include "language.h"
|
||||||
|
|
||||||
#define STRBUF_SIZE 512 // maximum size of the string buffer
|
#define STRBUF_SIZE 512 // maximum size of the string buffer
|
||||||
#define FONT_MAX_WIDTH 8
|
#define FONT_MAX_WIDTH 8
|
||||||
#define FONT_MAX_HEIGHT 10
|
#define FONT_MAX_HEIGHT 10
|
||||||
#define PROGRESS_REFRESH_RATE 30 // the progress bar is only allowed to draw to screen every X milliseconds
|
#define PROGRESS_REFRESH_RATE 30 // the progress bar is only allowed to draw to screen every X milliseconds
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char chunk_id[4]; // NOT null terminated
|
||||||
|
u32 size;
|
||||||
|
} RiffChunkHeader;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
u8 width;
|
||||||
|
u8 height;
|
||||||
|
u16 count;
|
||||||
|
} FontMeta;
|
||||||
|
|
||||||
static u32 font_width = 0;
|
static u32 font_width = 0;
|
||||||
static u32 font_height = 0;
|
static u32 font_height = 0;
|
||||||
|
static u32 font_count = 0;
|
||||||
static u32 line_height = 0;
|
static u32 line_height = 0;
|
||||||
static u8 font_bin[FONT_MAX_HEIGHT * 256];
|
static u8* font_bin = NULL;
|
||||||
|
static u16* font_map = NULL;
|
||||||
|
static u16 ascii_lut[0x60];
|
||||||
|
|
||||||
|
// lookup table to sort CP-437 so it can be binary searched with Unicode codepoints
|
||||||
|
static const u8 cp437_sorted[0x100] = {
|
||||||
|
0x00, 0xF5, 0xF6, 0xFC, 0xFD, 0xFB, 0xFA, 0xA4, 0xF3, 0xF2, 0xF4, 0xF9, 0xF8, 0xFE, 0xFF, 0xF7,
|
||||||
|
0xEF, 0xF1, 0xAD, 0xA5, 0x6D, 0x65, 0xED, 0xAE, 0xA9, 0xAB, 0xAA, 0xA8, 0xB2, 0xAC, 0xEE, 0xF0,
|
||||||
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
|
||||||
|
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
|
||||||
|
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
|
||||||
|
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40,
|
||||||
|
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
|
||||||
|
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0xB8,
|
||||||
|
0x77, 0x95, 0x85, 0x7F, 0x80, 0x7D, 0x81, 0x83, 0x86, 0x87, 0x84, 0x8B, 0x8A, 0x88, 0x74, 0x75,
|
||||||
|
0x78, 0x82, 0x76, 0x8F, 0x90, 0x8D, 0x94, 0x92, 0x96, 0x7A, 0x7B, 0x62, 0x63, 0x64, 0xA7, 0x97,
|
||||||
|
0x7E, 0x89, 0x8E, 0x93, 0x8C, 0x79, 0x66, 0x6F, 0x73, 0xB9, 0x68, 0x72, 0x71, 0x61, 0x67, 0x70,
|
||||||
|
0xE9, 0xEA, 0xEB, 0xBD, 0xC3, 0xD8, 0xD9, 0xCD, 0xCC, 0xDA, 0xC8, 0xCE, 0xD4, 0xD3, 0xD2, 0xBF,
|
||||||
|
0xC0, 0xC5, 0xC4, 0xC2, 0xBC, 0xC6, 0xD5, 0xD6, 0xD1, 0xCB, 0xE0, 0xDD, 0xD7, 0xC7, 0xE3, 0xDE,
|
||||||
|
0xDF, 0xDB, 0xDC, 0xD0, 0xCF, 0xC9, 0xCA, 0xE2, 0xE1, 0xC1, 0xBE, 0xE6, 0xE5, 0xE7, 0xE8, 0xE4,
|
||||||
|
0x9D, 0x7C, 0x98, 0xA0, 0x9A, 0xA1, 0x6C, 0xA2, 0x9B, 0x99, 0x9C, 0x9E, 0xB1, 0xA3, 0x9F, 0xB3,
|
||||||
|
0xB5, 0x6A, 0xB7, 0xB6, 0xBA, 0xBB, 0x91, 0xB4, 0x69, 0xAF, 0x6E, 0xB0, 0xA6, 0x6B, 0xEC, 0x60
|
||||||
|
};
|
||||||
|
|
||||||
|
// Unicode font mapping for sorted CP-437
|
||||||
|
static const u16 cp437_sorted_map[0x100] = {
|
||||||
|
0x0000, 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E,
|
||||||
|
0x002F, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E,
|
||||||
|
0x003F, 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E,
|
||||||
|
0x004F, 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E,
|
||||||
|
0x005F, 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E,
|
||||||
|
0x006F, 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E,
|
||||||
|
0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A5, 0x00A7, 0x00AA, 0x00AB, 0x00AC, 0x00B0, 0x00B1, 0x00B2, 0x00B5, 0x00B6, 0x00B7, 0x00BA,
|
||||||
|
0x00BB, 0x00BC, 0x00BD, 0x00BF, 0x00C4, 0x00C5, 0x00C6, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00DF, 0x00E0, 0x00E1, 0x00E2,
|
||||||
|
0x00E4, 0x00E5, 0x00E6, 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, 0x00F1, 0x00F2, 0x00F3, 0x00F4,
|
||||||
|
0x00F6, 0x00F7, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FF, 0x0192, 0x0393, 0x0398, 0x03A3, 0x03A6, 0x03A9, 0x03B1, 0x03B4, 0x03B5,
|
||||||
|
0x03C0, 0x03C3, 0x03C4, 0x03C6, 0x2022, 0x203C, 0x207F, 0x20A7, 0x2190, 0x2191, 0x2192, 0x2193, 0x2194, 0x2195, 0x21A8, 0x2219,
|
||||||
|
0x221A, 0x221E, 0x221F, 0x2229, 0x2248, 0x2261, 0x2264, 0x2265, 0x2302, 0x2310, 0x2320, 0x2321, 0x2500, 0x2502, 0x250C, 0x2510,
|
||||||
|
0x2514, 0x2518, 0x251C, 0x2524, 0x252C, 0x2534, 0x253C, 0x2550, 0x2551, 0x2552, 0x2553, 0x2554, 0x2555, 0x2556, 0x2557, 0x2558,
|
||||||
|
0x2559, 0x255A, 0x255B, 0x255C, 0x255D, 0x255E, 0x255F, 0x2560, 0x2561, 0x2562, 0x2563, 0x2564, 0x2565, 0x2566, 0x2567, 0x2568,
|
||||||
|
0x2569, 0x256A, 0x256B, 0x256C, 0x2580, 0x2584, 0x2588, 0x258C, 0x2590, 0x2591, 0x2592, 0x2593, 0x25A0, 0x25AC, 0x25B2, 0x25BA,
|
||||||
|
0x25BC, 0x25C4, 0x25CB, 0x25D8, 0x25D9, 0x263A, 0x263B, 0x263C, 0x2640, 0x2642, 0x2660, 0x2663, 0x2665, 0x2666, 0x266A, 0x266B
|
||||||
|
};
|
||||||
|
|
||||||
|
// lookup table to convert the old CP-437 escapes to Unicode
|
||||||
|
static const u16 cp437_escapes[0x20] = {
|
||||||
|
0x0000, 0x263A, 0x263B, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022, 0x25D8, 0x25CB, 0x25D9, 0x2642, 0x2640, 0x266A, 0x266B, 0x263C,
|
||||||
|
0x25BA, 0x25C4, 0x2195, 0x203C, 0x00B6, 0x00A7, 0x25AC, 0x21A8, 0x2191, 0x2193, 0x2192, 0x2190, 0x221F, 0x2194, 0x25B2, 0x25BC
|
||||||
|
};
|
||||||
|
|
||||||
#define PIXEL_OFFSET(x, y) (((x) * SCREEN_HEIGHT) + (SCREEN_HEIGHT - (y) - 1))
|
#define PIXEL_OFFSET(x, y) (((x) * SCREEN_HEIGHT) + (SCREEN_HEIGHT - (y) - 1))
|
||||||
|
|
||||||
u8* GetFontFromPbm(const void* pbm, const u32 pbm_size, u32* w, u32* h) {
|
|
||||||
char* hdr = (char*) pbm;
|
u16 GetFontIndex(u16 c, bool use_ascii_lut)
|
||||||
|
{
|
||||||
|
if (c < 0x20) return GetFontIndex(cp437_escapes[c], use_ascii_lut);
|
||||||
|
else if (use_ascii_lut && c < 0x80) return ascii_lut[c - 0x20];
|
||||||
|
|
||||||
|
int left = 0;
|
||||||
|
int right = font_count;
|
||||||
|
|
||||||
|
while (left <= right) {
|
||||||
|
int mid = left + ((right - left) / 2);
|
||||||
|
if (font_map[mid] == c)
|
||||||
|
return mid;
|
||||||
|
|
||||||
|
if (font_map[mid] < c)
|
||||||
|
left = mid + 1;
|
||||||
|
else
|
||||||
|
right = mid - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if not found in font, return a '?'
|
||||||
|
return ascii_lut['?' - 0x20];
|
||||||
|
}
|
||||||
|
|
||||||
|
// gets a u32 codepoint from a UTF-8 string and moves the pointer to the next character
|
||||||
|
u32 GetCharacter(const char** str)
|
||||||
|
{
|
||||||
|
u32 c;
|
||||||
|
|
||||||
|
if ((**str & 0x80) == 0) {
|
||||||
|
c = *(*str)++;
|
||||||
|
} else if ((**str & 0xE0) == 0xC0) {
|
||||||
|
c = (*(*str)++ & 0x1F) << 6;
|
||||||
|
c |= *(*str)++ & 0x3F;
|
||||||
|
} else if ((**str & 0xF0) == 0xE0) {
|
||||||
|
c = (*(*str)++ & 0x0F) << 12;
|
||||||
|
c |= (*(*str)++ & 0x3F) << 6;
|
||||||
|
c |= *(*str)++ & 0x3F;
|
||||||
|
} else if ((**str & 0xF8) == 0xF0) {
|
||||||
|
c = (*(*str)++ & 0x07) << 18;
|
||||||
|
c |= (*(*str)++ & 0x3F) << 12;
|
||||||
|
c |= (*(*str)++ & 0x3F) << 6;
|
||||||
|
c |= *(*str)++ & 0x3F;
|
||||||
|
} else {
|
||||||
|
// invalid UTF-8, skip to next character
|
||||||
|
(*str)++;
|
||||||
|
c = '?';
|
||||||
|
}
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
const u8* GetFontFromPbm(const void* pbm, const u32 pbm_size, u32* w, u32* h) {
|
||||||
|
const char* hdr = (const char*) pbm;
|
||||||
u32 hdr_max_size = min(512, pbm_size);
|
u32 hdr_max_size = min(512, pbm_size);
|
||||||
u32 pbm_w = 0;
|
u32 pbm_w = 0;
|
||||||
u32 pbm_h = 0;
|
u32 pbm_h = 0;
|
||||||
@ -90,43 +203,134 @@ u8* GetFontFromPbm(const void* pbm, const u32 pbm_size, u32* w, u32* h) {
|
|||||||
return (u8*) pbm + p;
|
return (u8*) pbm + p;
|
||||||
}
|
}
|
||||||
|
|
||||||
// sets the font from a given PBM
|
const u8* GetFontFromRiff(const void* riff, const u32 riff_size, u32* w, u32* h, u16* count) {
|
||||||
// if no PBM is given, the PBM is fetched from the default VRAM0 location
|
const void* ptr = riff;
|
||||||
bool SetFontFromPbm(const void* pbm, u32 pbm_size) {
|
const RiffChunkHeader* riff_header;
|
||||||
u32 w, h;
|
const RiffChunkHeader* chunk_header;
|
||||||
u8* ptr = NULL;
|
|
||||||
|
|
||||||
if (!pbm) {
|
// check header magic and load size
|
||||||
u64 pbm_size64 = 0;
|
if (!ptr) return NULL;
|
||||||
pbm = FindVTarFileInfo(VRAM0_FONT_PBM, &pbm_size64);
|
riff_header = ptr;
|
||||||
pbm_size = (u32) pbm_size64;
|
if (memcmp(riff_header->chunk_id, "RIFF", 4) != 0) return NULL;
|
||||||
|
|
||||||
|
// ensure enough space is allocated
|
||||||
|
if (riff_header->size > riff_size) return NULL;
|
||||||
|
|
||||||
|
ptr += sizeof(RiffChunkHeader);
|
||||||
|
|
||||||
|
while ((u32)(ptr - riff) < riff_header->size + sizeof(RiffChunkHeader)) {
|
||||||
|
chunk_header = ptr;
|
||||||
|
|
||||||
|
// check for and load META section
|
||||||
|
if (memcmp(chunk_header->chunk_id, "META", 4) == 0) {
|
||||||
|
|
||||||
|
if (chunk_header->size != 4) return NULL;
|
||||||
|
|
||||||
|
const FontMeta *meta = ptr + sizeof(RiffChunkHeader);
|
||||||
|
if (meta->width > FONT_MAX_WIDTH || meta->height > FONT_MAX_HEIGHT) return NULL;
|
||||||
|
|
||||||
|
// all good
|
||||||
|
if (w) *w = meta->width;
|
||||||
|
if (h) *h = meta->height;
|
||||||
|
if (count) *count = meta->count;
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr += sizeof(RiffChunkHeader) + chunk_header->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pbm)
|
return NULL;
|
||||||
ptr = GetFontFromPbm(pbm, pbm_size, &w, &h);
|
}
|
||||||
|
|
||||||
if (!ptr) {
|
// sets the font from a given RIFF or PBM
|
||||||
|
// if no font is given, the font is fetched from the default VRAM0 location
|
||||||
|
bool SetFont(const void* font, u32 font_size) {
|
||||||
|
u32 w, h;
|
||||||
|
u16 count;
|
||||||
|
const u8* ptr = NULL;
|
||||||
|
|
||||||
|
if (!font) {
|
||||||
|
u64 font_size64 = 0;
|
||||||
|
font = FindVTarFileInfo(VRAM0_FONT, &font_size64);
|
||||||
|
font_size = (u32) font_size64;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!font)
|
||||||
return false;
|
return false;
|
||||||
} else if (w > 8) {
|
|
||||||
font_width = w / 16;
|
|
||||||
font_height = h / 16;
|
|
||||||
memset(font_bin, 0x00, w * h / 8);
|
|
||||||
|
|
||||||
for (u32 cy = 0; cy < 16; cy++) {
|
if ((ptr = GetFontFromRiff(font, font_size, &w, &h, &count))) { // RIFF font
|
||||||
for (u32 row = 0; row < font_height; row++) {
|
|
||||||
for (u32 cx = 0; cx < 16; cx++) {
|
|
||||||
u32 bp0 = (cx * font_width) >> 3;
|
|
||||||
u32 bm0 = (cx * font_width) % 8;
|
|
||||||
u8 byte = ((ptr[bp0] << bm0) | (ptr[bp0+1] >> (8 - bm0))) & (0xFF << (8 - font_width));
|
|
||||||
font_bin[(((cy << 4) + cx) * font_height) + row] = byte;
|
|
||||||
}
|
|
||||||
ptr += font_width << 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
font_width = w;
|
font_width = w;
|
||||||
font_height = h / 256;
|
font_height = h;
|
||||||
memcpy(font_bin, ptr, h);
|
font_count = count;
|
||||||
|
|
||||||
|
const RiffChunkHeader* riff_header;
|
||||||
|
const RiffChunkHeader* chunk_header;
|
||||||
|
|
||||||
|
// load total size
|
||||||
|
riff_header = font;
|
||||||
|
|
||||||
|
while (((u32)ptr - (u32)font) < riff_header->size + sizeof(RiffChunkHeader)) {
|
||||||
|
chunk_header = (const void *)ptr;
|
||||||
|
|
||||||
|
if (memcmp(chunk_header->chunk_id, "CDAT", 4) == 0) { // character data
|
||||||
|
if (font_bin) free(font_bin);
|
||||||
|
font_bin = malloc(font_height * font_count);
|
||||||
|
if (!font_bin) return NULL;
|
||||||
|
|
||||||
|
memcpy(font_bin, ptr + sizeof(RiffChunkHeader), font_height * font_count);
|
||||||
|
} else if (memcmp(chunk_header->chunk_id, "CMAP", 4) == 0) { // character map
|
||||||
|
if (font_map) free(font_map);
|
||||||
|
font_map = malloc(sizeof(u16) * font_count);
|
||||||
|
if (!font_map) return NULL;
|
||||||
|
|
||||||
|
memcpy(font_map, ptr + sizeof(RiffChunkHeader), sizeof(u16) * font_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr += sizeof(RiffChunkHeader) + chunk_header->size;
|
||||||
|
}
|
||||||
|
} else if ((ptr = GetFontFromPbm(font, font_size, &w, &h))) {
|
||||||
|
font_count = 0x100;
|
||||||
|
|
||||||
|
if (w > 8) {
|
||||||
|
font_width = w / 16;
|
||||||
|
font_height = h / 16;
|
||||||
|
|
||||||
|
if (font_bin) free(font_bin);
|
||||||
|
font_bin = malloc(font_height * font_count);
|
||||||
|
if (!font_bin) return NULL;
|
||||||
|
|
||||||
|
for (u32 cy = 0; cy < 16; cy++) {
|
||||||
|
for (u32 row = 0; row < font_height; row++) {
|
||||||
|
for (u32 cx = 0; cx < 16; cx++) {
|
||||||
|
u32 bp0 = (cx * font_width) >> 3;
|
||||||
|
u32 bm0 = (cx * font_width) % 8;
|
||||||
|
u8 byte = ((ptr[bp0] << bm0) | (ptr[bp0+1] >> (8 - bm0))) & (0xFF << (8 - font_width));
|
||||||
|
font_bin[(cp437_sorted[(cy << 4) + cx] * font_height) + row] = byte;
|
||||||
|
}
|
||||||
|
ptr += font_width << 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
font_width = w;
|
||||||
|
font_height = h / 256;
|
||||||
|
for (u32 i = 0; i < font_count; i++)
|
||||||
|
memcpy(font_bin + cp437_sorted[i] * font_height, ptr + i * font_height, font_height);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (font_map) free(font_map);
|
||||||
|
font_map = malloc(sizeof(u16) * font_count);
|
||||||
|
if (!font_map) return NULL;
|
||||||
|
|
||||||
|
memcpy(font_map, cp437_sorted_map, sizeof(cp437_sorted_map));
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// set up ASCII lookup table
|
||||||
|
ascii_lut['?' - 0x20] = GetFontIndex('?', false);
|
||||||
|
for (int i = 0; i < 0x60; i++) {
|
||||||
|
ascii_lut[i] = GetFontIndex(i + 0x20, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
line_height = min(10, font_height + 2);
|
line_height = min(10, font_height + 2);
|
||||||
@ -222,14 +426,14 @@ void DrawQrCode(u16 *screen, const u8* qrcode)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawCharacter(u16 *screen, int character, int x, int y, u32 color, u32 bgcolor)
|
void DrawCharacter(u16 *screen, u32 character, int x, int y, u32 color, u32 bgcolor)
|
||||||
{
|
{
|
||||||
for (int yy = 0; yy < (int) font_height; yy++) {
|
for (int yy = 0; yy < (int) font_height; yy++) {
|
||||||
int xDisplacement = x * SCREEN_HEIGHT;
|
int xDisplacement = x * SCREEN_HEIGHT;
|
||||||
int yDisplacement = SCREEN_HEIGHT - (y + yy) - 1;
|
int yDisplacement = SCREEN_HEIGHT - (y + yy) - 1;
|
||||||
u16* screenPos = screen + xDisplacement + yDisplacement;
|
u16* screenPos = screen + xDisplacement + yDisplacement;
|
||||||
|
|
||||||
u8 charPos = font_bin[character * font_height + yy];
|
u8 charPos = font_bin[GetFontIndex(character, true) * font_height + yy];
|
||||||
for (int xx = 7; xx >= (8 - (int) font_width); xx--) {
|
for (int xx = 7; xx >= (8 - (int) font_width); xx--) {
|
||||||
if ((charPos >> xx) & 1) {
|
if ((charPos >> xx) & 1) {
|
||||||
*screenPos = color;
|
*screenPos = color;
|
||||||
@ -241,14 +445,12 @@ void DrawCharacter(u16 *screen, int character, int x, int y, u32 color, u32 bgco
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawString(u16 *screen, const char *str, int x, int y, u32 color, u32 bgcolor, bool fix_utf8)
|
void DrawString(u16 *screen, const char *str, int x, int y, u32 color, u32 bgcolor)
|
||||||
{
|
{
|
||||||
size_t max_len = (((screen == TOP_SCREEN) ? SCREEN_WIDTH_TOP : SCREEN_WIDTH_BOT) - x) / font_width;
|
size_t max_len = (((screen == TOP_SCREEN) ? SCREEN_WIDTH_TOP : SCREEN_WIDTH_BOT) - x) / font_width;
|
||||||
size_t len = (strlen(str) > max_len) ? max_len : strlen(str);
|
|
||||||
|
|
||||||
for (size_t i = 0; i < len; i++) {
|
for (size_t i = 0; i < max_len && *str; i++) {
|
||||||
char c = (char) (fix_utf8 && str[i] >= 0x80) ? '?' : str[i];
|
DrawCharacter(screen, GetCharacter(&str), x + i * font_width, y, color, bgcolor);
|
||||||
DrawCharacter(screen, c, x + i * font_width, y, color, bgcolor);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -261,7 +463,7 @@ void DrawStringF(u16 *screen, int x, int y, u32 color, u32 bgcolor, const char *
|
|||||||
va_end(va);
|
va_end(va);
|
||||||
|
|
||||||
for (char* text = strtok(str, "\n"); text != NULL; text = strtok(NULL, "\n"), y += line_height)
|
for (char* text = strtok(str, "\n"); text != NULL; text = strtok(NULL, "\n"), y += line_height)
|
||||||
DrawString(screen, text, x, y, color, bgcolor, true);
|
DrawString(screen, text, x, y, color, bgcolor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawStringCenter(u16 *screen, u32 color, u32 bgcolor, const char *format, ...)
|
void DrawStringCenter(u16 *screen, u32 color, u32 bgcolor, const char *format, ...)
|
||||||
@ -287,16 +489,44 @@ u32 GetDrawStringHeight(const char* str) {
|
|||||||
return height;
|
return height;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u32 GetCharSize(const char* str) {
|
||||||
|
const char *start = str;
|
||||||
|
do {
|
||||||
|
str++;
|
||||||
|
} while ((*str & 0xC0) == 0x80);
|
||||||
|
|
||||||
|
return str - start;
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 GetPrevCharSize(const char* str) {
|
||||||
|
const char *start = str;
|
||||||
|
do {
|
||||||
|
str--;
|
||||||
|
} while ((*str & 0xC0) == 0x80);
|
||||||
|
|
||||||
|
return start - str;
|
||||||
|
}
|
||||||
|
|
||||||
u32 GetDrawStringWidth(const char* str) {
|
u32 GetDrawStringWidth(const char* str) {
|
||||||
u32 width = 0;
|
u32 width = 0;
|
||||||
char* old_lf = (char*) str;
|
char* old_lf = (char*) str;
|
||||||
char* str_end = (char*) str + strnlen(str, STRBUF_SIZE);
|
char* str_end = (char*) str + strnlen(str, STRBUF_SIZE);
|
||||||
for (char* lf = strchr(str, '\n'); lf != NULL; lf = strchr(lf + 1, '\n')) {
|
for (char* lf = strchr(str, '\n'); lf != NULL; lf = strchr(lf + 1, '\n')) {
|
||||||
if ((u32) (lf - old_lf) > width) width = lf - old_lf;
|
u32 length = 0;
|
||||||
|
for (char* c = old_lf; c != lf; c++) {
|
||||||
|
if ((*c & 0xC0) != 0x80) length++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (length > width) width = length;
|
||||||
old_lf = lf;
|
old_lf = lf;
|
||||||
}
|
}
|
||||||
if ((u32) (str_end - old_lf) > width)
|
|
||||||
width = str_end - old_lf;
|
u32 length = 0;
|
||||||
|
for (char* c = old_lf; c != str_end; c++) {
|
||||||
|
if ((*c & 0xC0) != 0x80) length++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (length > width) width = length;
|
||||||
width *= font_width;
|
width *= font_width;
|
||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
@ -309,6 +539,26 @@ u32 GetFontHeight(void) {
|
|||||||
return font_height;
|
return font_height;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MultiLineString(char* dest, const char* orig, int llen, int maxl) {
|
||||||
|
char* ptr_o = (char*) orig;
|
||||||
|
char* ptr_d = (char*) dest;
|
||||||
|
for (int l = 0; l < maxl; l++) {
|
||||||
|
int len = strnlen(ptr_o, llen+1);
|
||||||
|
snprintf(ptr_d, llen+1, "%.*s", llen, ptr_o);
|
||||||
|
ptr_o += min(len, llen);
|
||||||
|
ptr_d += min(len, llen);
|
||||||
|
if (len <= llen) break;
|
||||||
|
*(ptr_d++) = '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
// string too long?
|
||||||
|
if (!maxl) *dest = '\0';
|
||||||
|
else if (*ptr_o) {
|
||||||
|
if (llen >= 3) snprintf(ptr_d - 4, 4, "...");
|
||||||
|
else *(ptr_d-1) = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void WordWrapString(char* str, int llen) {
|
void WordWrapString(char* str, int llen) {
|
||||||
char* last_brk = str - 1;
|
char* last_brk = str - 1;
|
||||||
char* last_spc = str - 1;
|
char* last_spc = str - 1;
|
||||||
@ -330,26 +580,50 @@ void WordWrapString(char* str, int llen) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ResizeString(char* dest, const char* orig, int nsize, int tpos, bool align_right) {
|
// dest must be at least 4x the size of nlength to account for UTF-8
|
||||||
int osize = strnlen(orig, 256);
|
void ResizeString(char* dest, const char* orig, int nlength, int tpos, bool align_right) {
|
||||||
if (nsize < osize) {
|
int olength = 0;
|
||||||
TruncateString(dest, orig, nsize, tpos);
|
for (int i = 0; i < 256 && orig[i]; i++) {
|
||||||
} else if (!align_right) {
|
if ((orig[i] & 0xC0) != 0x80) olength++;
|
||||||
snprintf(dest, nsize + 1, "%-*.*s", nsize, nsize, orig);
|
}
|
||||||
|
|
||||||
|
if (nlength < olength) {
|
||||||
|
TruncateString(dest, orig, nlength, tpos);
|
||||||
} else {
|
} else {
|
||||||
snprintf(dest, nsize + 1, "%*.*s", nsize, nsize, orig);
|
int nsize = 0;
|
||||||
|
int osize = strnlen(orig, 256);
|
||||||
|
for (int i = 0; i < nlength || (nsize <= osize && (orig[nsize] & 0xC0) == 0x80); nsize++) {
|
||||||
|
if (nsize > osize || (orig[nsize] & 0xC0) != 0x80) i++;
|
||||||
|
}
|
||||||
|
snprintf(dest, UTF_BUFFER_BYTESIZE(nlength), align_right ? "%*.*s" : "%-*.*s", nsize, nsize, orig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TruncateString(char* dest, const char* orig, int nsize, int tpos) {
|
// dest must be at least 4x the size of nlength to account for UTF-8
|
||||||
int osize = strnlen(orig, 256);
|
void TruncateString(char* dest, const char* orig, int nlength, int tpos) {
|
||||||
if (nsize < 0) {
|
int osize = strnlen(orig, 256), olength = 0;
|
||||||
|
for (int i = 0; i < 256 && orig[i]; i++) {
|
||||||
|
if ((orig[i] & 0xC0) != 0x80) olength++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nlength < 0) {
|
||||||
return;
|
return;
|
||||||
} else if ((nsize <= 3) || (nsize >= osize)) {
|
} else if ((nlength <= 3) || (nlength >= olength)) {
|
||||||
snprintf(dest, nsize + 1, "%s", orig);
|
strcpy(dest, orig);
|
||||||
} else {
|
} else {
|
||||||
if (tpos + 3 > nsize) tpos = nsize - 3;
|
if (tpos + 3 > nlength) tpos = nlength - 3;
|
||||||
snprintf(dest, nsize + 1, "%-.*s...%-.*s", tpos, orig, nsize - (3 + tpos), orig + osize - (nsize - (3 + tpos)));
|
|
||||||
|
int tposStart = 0;
|
||||||
|
for (int i = 0; i < tpos || (orig[tposStart] & 0xC0) == 0x80; tposStart++) {
|
||||||
|
if ((orig[tposStart] & 0xC0) != 0x80) i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
int tposEnd = 0;
|
||||||
|
for (int i = 0; i < nlength - tpos - 3; tposEnd++) {
|
||||||
|
if ((orig[osize - 1 - tposEnd] & 0xC0) != 0x80) i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
snprintf(dest, UTF_BUFFER_BYTESIZE(nlength), "%-.*s...%-.*s", tposStart, orig, tposEnd, orig + osize - tposEnd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -359,20 +633,20 @@ void FormatNumber(char* str, u64 number) { // str should be 32 byte in size
|
|||||||
for (; number / (mag1000 * 1000) > 0; mag1000 *= 1000);
|
for (; number / (mag1000 * 1000) > 0; mag1000 *= 1000);
|
||||||
for (; mag1000 > 0; mag1000 /= 1000) {
|
for (; mag1000 > 0; mag1000 /= 1000) {
|
||||||
u32 pos = strnlen(str, 31);
|
u32 pos = strnlen(str, 31);
|
||||||
snprintf(str + pos, 31 - pos, "%0*llu%c", (pos) ? 3 : 1, (number / mag1000) % 1000, (mag1000 > 1) ? ',' : '\0');
|
snprintf(str + pos, 31 - pos, "%0*llu%s", (pos) ? 3 : 1, (number / mag1000) % 1000, (mag1000 > 1) ? STR_THOUSAND_SEPARATOR : "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FormatBytes(char* str, u64 bytes) { // str should be 32 byte in size, just to be safe
|
void FormatBytes(char* str, u64 bytes) { // str should be 32 byte in size, just to be safe
|
||||||
const char* units[] = {" Byte", " kB", " MB", " GB"};
|
const char* units[] = {STR_BYTE, STR_KB, STR_MB, STR_GB};
|
||||||
|
|
||||||
if (bytes == (u64) -1) snprintf(str, 32, "INVALID");
|
if (bytes == (u64) -1) snprintf(str, 32, "%s", STR_INVALID);
|
||||||
else if (bytes < 1024) snprintf(str, 32, "%llu%s", bytes, units[0]);
|
else if (bytes < 1024) snprintf(str, 32, "%llu%s", bytes, units[0]);
|
||||||
else {
|
else {
|
||||||
u32 scale = 1;
|
u32 scale = 1;
|
||||||
u64 bytes100 = (bytes * 100) >> 10;
|
u64 bytes100 = (bytes * 100) >> 10;
|
||||||
for(; (bytes100 >= 1024*100) && (scale < 3); scale++, bytes100 >>= 10);
|
for(; (bytes100 >= 1024*100) && (scale < 3); scale++, bytes100 >>= 10);
|
||||||
snprintf(str, 32, "%llu.%llu%s", bytes100 / 100, (bytes100 % 100) / 10, units[scale]);
|
snprintf(str, 32, "%llu%s%llu%s", bytes100 / 100, STR_DECIMAL_SEPARATOR, (bytes100 % 100) / 10, units[scale]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -439,7 +713,7 @@ void ShowIconString(u16* icon, int w, int h, const char *format, ...)
|
|||||||
vsnprintf(str, STRBUF_SIZE, format, va);
|
vsnprintf(str, STRBUF_SIZE, format, va);
|
||||||
va_end(va);
|
va_end(va);
|
||||||
|
|
||||||
ShowIconStringF(MAIN_SCREEN, icon, w, h, str);
|
ShowIconStringF(MAIN_SCREEN, icon, w, h, "%s", str);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ShowPrompt(bool ask, const char *format, ...)
|
bool ShowPrompt(bool ask, const char *format, ...)
|
||||||
@ -454,7 +728,7 @@ bool ShowPrompt(bool ask, const char *format, ...)
|
|||||||
|
|
||||||
ClearScreenF(true, false, COLOR_STD_BG);
|
ClearScreenF(true, false, COLOR_STD_BG);
|
||||||
DrawStringCenter(MAIN_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s\n \n%s", str,
|
DrawStringCenter(MAIN_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s\n \n%s", str,
|
||||||
(ask) ? "(<A> yes, <B> no)" : "(<A> to continue)");
|
(ask) ? STR_A_YES_B_NO : STR_A_TO_CONTINUE);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
u32 pad_state = InputWait(0);
|
u32 pad_state = InputWait(0);
|
||||||
@ -474,7 +748,7 @@ bool ShowPrompt(bool ask, const char *format, ...)
|
|||||||
#define PRNG (*(volatile u32*)0x10011000)
|
#define PRNG (*(volatile u32*)0x10011000)
|
||||||
bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
|
bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
|
||||||
static const int seqcolors[7] = { COLOR_STD_FONT, COLOR_BRIGHTGREEN,
|
static const int seqcolors[7] = { COLOR_STD_FONT, COLOR_BRIGHTGREEN,
|
||||||
COLOR_BRIGHTYELLOW, COLOR_ORANGE, COLOR_BRIGHTBLUE, COLOR_RED, COLOR_DARKRED };
|
COLOR_BRIGHTYELLOW, COLOR_ORANGE, COLOR_BRIGHTBLUE, COLOR_BRIGHTBLUE, COLOR_DARKRED };
|
||||||
const u32 seqlen_max = 7;
|
const u32 seqlen_max = 7;
|
||||||
const u32 seqlen = seqlen_max - ((seqlvl < 3) ? 2 : (seqlvl < 4) ? 1 : 0);
|
const u32 seqlen = seqlen_max - ((seqlvl < 3) ? 2 : (seqlvl < 4) ? 1 : 0);
|
||||||
|
|
||||||
@ -499,7 +773,7 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
|
|||||||
x = (str_width >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - str_width) / 2;
|
x = (str_width >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - str_width) / 2;
|
||||||
y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2;
|
y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2;
|
||||||
|
|
||||||
if (seqlvl >= 6) { // special handling
|
if (seqlvl >= 5) { // special handling
|
||||||
color_bg = seqcolors[seqlvl];
|
color_bg = seqcolors[seqlvl];
|
||||||
color_font = COLOR_BLACK;
|
color_font = COLOR_BLACK;
|
||||||
color_off = COLOR_BLACK;
|
color_off = COLOR_BLACK;
|
||||||
@ -509,13 +783,13 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
|
|||||||
ClearScreenF(true, false, color_bg);
|
ClearScreenF(true, false, color_bg);
|
||||||
DrawStringF(MAIN_SCREEN, x, y, color_font, color_bg, "%s", str);
|
DrawStringF(MAIN_SCREEN, x, y, color_font, color_bg, "%s", str);
|
||||||
#ifndef TIMER_UNLOCK
|
#ifndef TIMER_UNLOCK
|
||||||
DrawStringF(MAIN_SCREEN, x, y + str_height - 28, color_font, color_bg, "To proceed, enter this:");
|
DrawStringF(MAIN_SCREEN, x, y + str_height - 28, color_font, color_bg, "%s", STR_TO_PROCEED_ENTER_THIS);
|
||||||
|
|
||||||
// generate sequence
|
// generate sequence
|
||||||
const char dpad_symbols[] = { '\x1A', '\x1B', '\x18', '\x19' }; // R L U D
|
const char *dpad_symbols[] = { "→", "←", "↑", "↓" }; // R L U D
|
||||||
|
|
||||||
u32 sequence[seqlen_max];
|
u32 sequence[seqlen_max];
|
||||||
char seqsymbols[seqlen_max];
|
const char *seqsymbols[seqlen_max];
|
||||||
u32 lastlsh = (u32) -1;
|
u32 lastlsh = (u32) -1;
|
||||||
for (u32 n = 0; n < (seqlen-1); n++) {
|
for (u32 n = 0; n < (seqlen-1); n++) {
|
||||||
u32 lsh = lastlsh;
|
u32 lsh = lastlsh;
|
||||||
@ -525,13 +799,13 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
|
|||||||
seqsymbols[n] = dpad_symbols[lsh];
|
seqsymbols[n] = dpad_symbols[lsh];
|
||||||
}
|
}
|
||||||
sequence[seqlen-1] = BUTTON_A;
|
sequence[seqlen-1] = BUTTON_A;
|
||||||
seqsymbols[seqlen-1] = 'A';
|
seqsymbols[seqlen-1] = "A";
|
||||||
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
for (u32 n = 0; n < seqlen; n++) {
|
for (u32 n = 0; n < seqlen; n++) {
|
||||||
DrawStringF(MAIN_SCREEN, x + (n*4*FONT_WIDTH_EXT), y + str_height - 28 + line_height,
|
DrawStringF(MAIN_SCREEN, x + (n*4*FONT_WIDTH_EXT), y + str_height - 28 + line_height,
|
||||||
(lvl > n) ? color_on : color_off, color_bg, "<%c>", seqsymbols[n]);
|
(lvl > n) ? color_on : color_off, color_bg, "<%s>", seqsymbols[n]);
|
||||||
}
|
}
|
||||||
if (lvl == seqlen)
|
if (lvl == seqlen)
|
||||||
break;
|
break;
|
||||||
@ -546,7 +820,7 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
|
|||||||
lvl = 0;
|
lvl = 0;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
DrawStringF(MAIN_SCREEN, x, y + str_height - 28, color_font, color_bg, "To proceed, hold <X>:");
|
DrawStringF(MAIN_SCREEN, x, y + str_height - 28, color_font, color_bg, STR_TO_PROCEED_HOLD_X);
|
||||||
|
|
||||||
while (!CheckButton(BUTTON_B)) {
|
while (!CheckButton(BUTTON_B)) {
|
||||||
for (u32 n = 0; n < seqlen; n++) {
|
for (u32 n = 0; n < seqlen; n++) {
|
||||||
@ -573,10 +847,11 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
u32 ShowSelectPrompt(u32 n, const char** options, const char *format, ...) {
|
u32 ShowSelectPrompt(int n, const char** options, const char *format, ...) {
|
||||||
u32 str_width, str_height;
|
u32 str_width, str_height;
|
||||||
u32 x, y, yopt;
|
u32 x, y, yopt;
|
||||||
u32 sel = 0;
|
int sel = 0, scroll = 0;
|
||||||
|
int n_show = min(n, 10);
|
||||||
|
|
||||||
char str[STRBUF_SIZE];
|
char str[STRBUF_SIZE];
|
||||||
va_list va;
|
va_list va;
|
||||||
@ -588,30 +863,64 @@ u32 ShowSelectPrompt(u32 n, const char** options, const char *format, ...) {
|
|||||||
// else if (n == 1) return ShowPrompt(true, "%s\n%s?", str, options[0]) ? 1 : 0;
|
// else if (n == 1) return ShowPrompt(true, "%s\n%s?", str, options[0]) ? 1 : 0;
|
||||||
|
|
||||||
str_width = GetDrawStringWidth(str);
|
str_width = GetDrawStringWidth(str);
|
||||||
str_height = GetDrawStringHeight(str) + (n * (line_height + 2)) + (3 * line_height);
|
str_height = GetDrawStringHeight(str) + (n_show * (line_height + 2)) + (3 * line_height);
|
||||||
if (str_width < 24 * font_width) str_width = 24 * font_width;
|
if (str_width < 24 * font_width) str_width = 24 * font_width;
|
||||||
for (u32 i = 0; i < n; i++) if (str_width < GetDrawStringWidth(options[i]))
|
for (int i = 0; i < n; i++) if (str_width < GetDrawStringWidth(options[i]) + (3 * font_width))
|
||||||
str_width = GetDrawStringWidth(options[i]);
|
str_width = GetDrawStringWidth(options[i]) + (3 * font_width);
|
||||||
x = (str_width >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - str_width) / 2;
|
x = (str_width >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - str_width) / 2;
|
||||||
y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2;
|
y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2;
|
||||||
yopt = y + GetDrawStringHeight(str) + 8;
|
yopt = y + GetDrawStringHeight(str) + 8;
|
||||||
|
|
||||||
ClearScreenF(true, false, COLOR_STD_BG);
|
ClearScreenF(true, false, COLOR_STD_BG);
|
||||||
DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
|
DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
|
||||||
DrawStringF(MAIN_SCREEN, x, yopt + (n*(line_height+2)) + line_height, COLOR_STD_FONT, COLOR_STD_BG, "(<A> select, <B> cancel)");
|
DrawStringF(MAIN_SCREEN, x, yopt + (n_show*(line_height+2)) + line_height, COLOR_STD_FONT, COLOR_STD_BG, "%s", STR_A_SELECT_B_CANCEL);
|
||||||
while (true) {
|
while (true) {
|
||||||
for (u32 i = 0; i < n; i++) {
|
for (int i = scroll; i < scroll+n_show; i++) {
|
||||||
DrawStringF(MAIN_SCREEN, x, yopt + ((line_height+2)*i), (sel == i) ? COLOR_STD_FONT : COLOR_LIGHTGREY, COLOR_STD_BG, "%2.2s %s",
|
DrawStringF(MAIN_SCREEN, x, yopt + ((line_height+2)*(i-scroll)), (sel == i) ? COLOR_STD_FONT : COLOR_LIGHTGREY, COLOR_STD_BG, "%2.2s %s",
|
||||||
(sel == i) ? "->" : "", options[i]);
|
(sel == i) ? "->" : "", options[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// show [n more]
|
||||||
|
if (n - n_show - scroll > 0) {
|
||||||
|
char more_str[UTF_BUFFER_BYTESIZE(str_width / font_width)], temp_str[UTF_BUFFER_BYTESIZE(64)];
|
||||||
|
snprintf(temp_str, sizeof(temp_str), STR_N_MORE, (n - (n_show-1) - scroll));
|
||||||
|
ResizeString(more_str, temp_str, str_width / font_width, 8, false);
|
||||||
|
DrawString(MAIN_SCREEN, more_str, x, yopt + (line_height+2)*(n_show-1), COLOR_LIGHTGREY, COLOR_STD_BG);
|
||||||
|
}
|
||||||
|
// show scroll bar
|
||||||
|
u32 bar_x = x + str_width + 2;
|
||||||
|
const u32 flist_height = (n_show * (line_height + 2));
|
||||||
|
const u32 bar_width = 2;
|
||||||
|
if (n > n_show) { // draw position bar at the right
|
||||||
|
const u32 bar_height_min = 32;
|
||||||
|
u32 bar_height = (n_show * flist_height) / n;
|
||||||
|
if (bar_height < bar_height_min) bar_height = bar_height_min;
|
||||||
|
const u32 bar_y = ((u64) scroll * (flist_height - bar_height)) / (n - n_show) + yopt;
|
||||||
|
|
||||||
|
DrawRectangle(MAIN_SCREEN, bar_x, bar_y, bar_width, bar_height, COLOR_SIDE_BAR);
|
||||||
|
}
|
||||||
|
|
||||||
u32 pad_state = InputWait(0);
|
u32 pad_state = InputWait(0);
|
||||||
if (pad_state & BUTTON_DOWN) sel = (sel+1) % n;
|
if (pad_state & BUTTON_DOWN) sel = (sel+1) % n;
|
||||||
else if (pad_state & BUTTON_UP) sel = (sel+n-1) % n;
|
else if (pad_state & BUTTON_UP) sel = (sel+n-1) % n;
|
||||||
|
else if (pad_state & BUTTON_RIGHT) sel += n_show;
|
||||||
|
else if (pad_state & BUTTON_LEFT) sel -= n_show;
|
||||||
else if (pad_state & BUTTON_A) break;
|
else if (pad_state & BUTTON_A) break;
|
||||||
else if (pad_state & BUTTON_B) {
|
else if (pad_state & BUTTON_B) {
|
||||||
sel = n;
|
sel = n;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (sel < 0) sel = 0;
|
||||||
|
else if (sel >= n) sel = n-1;
|
||||||
|
|
||||||
|
int prev_scroll = scroll;
|
||||||
|
if (sel < scroll) scroll = sel;
|
||||||
|
else if (sel == n-1 && sel >= (scroll + n_show - 1)) scroll = sel - n_show + 1;
|
||||||
|
else if (sel >= (scroll + (n_show-1) - 1)) scroll = sel - (n_show-1) + 1;
|
||||||
|
|
||||||
|
if (scroll != prev_scroll) {
|
||||||
|
DrawRectangle(MAIN_SCREEN, x + font_width * 3, yopt, str_width + 4, (n_show * (line_height + 2)), COLOR_STD_BG);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ClearScreenF(true, false, COLOR_STD_BG);
|
ClearScreenF(true, false, COLOR_STD_BG);
|
||||||
@ -619,12 +928,12 @@ u32 ShowSelectPrompt(u32 n, const char** options, const char *format, ...) {
|
|||||||
return (sel >= n) ? 0 : sel + 1;
|
return (sel >= n) ? 0 : sel + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 ShowFileScrollPrompt(u32 n, const DirEntry** options, bool hide_ext, const char *format, ...) {
|
u32 ShowFileScrollPrompt(int n, const DirEntry** options, bool hide_ext, const char *format, ...) {
|
||||||
u32 str_height, fname_len;
|
u32 str_height, fname_len;
|
||||||
u32 x, y, yopt;
|
u32 x, y, yopt;
|
||||||
const u32 item_width = SCREEN_WIDTH(MAIN_SCREEN) - 40;
|
const u32 item_width = SCREEN_WIDTH(MAIN_SCREEN) - 40;
|
||||||
int sel = 0, scroll = 0;
|
int sel = 0, scroll = 0;
|
||||||
u32 n_show = min(n, 10);
|
int n_show = min(n, 10);
|
||||||
|
|
||||||
char str[STRBUF_SIZE];
|
char str[STRBUF_SIZE];
|
||||||
va_list va;
|
va_list va;
|
||||||
@ -643,15 +952,15 @@ u32 ShowFileScrollPrompt(u32 n, const DirEntry** options, bool hide_ext, const c
|
|||||||
|
|
||||||
ClearScreenF(true, false, COLOR_STD_BG);
|
ClearScreenF(true, false, COLOR_STD_BG);
|
||||||
DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
|
DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
|
||||||
DrawStringF(MAIN_SCREEN, x, yopt + (n_show*(line_height+2)) + line_height, COLOR_STD_FONT, COLOR_STD_BG, "(<A> select, <B> cancel)");
|
DrawStringF(MAIN_SCREEN, x, yopt + (n_show*(line_height+2)) + line_height, COLOR_STD_FONT, COLOR_STD_BG, "%s", STR_A_SELECT_B_CANCEL);
|
||||||
while (true) {
|
while (true) {
|
||||||
for (u32 i = scroll; i < scroll+n_show; i++) {
|
for (int i = scroll; i < scroll+n_show; i++) {
|
||||||
char bytestr[16];
|
char bytestr[16];
|
||||||
FormatBytes(bytestr, options[i]->size);
|
FormatBytes(bytestr, options[i]->size);
|
||||||
|
|
||||||
char content_str[64 + 1];
|
char content_str[UTF_BUFFER_BYTESIZE(fname_len)];
|
||||||
char temp_str[256];
|
char temp_str[256];
|
||||||
strncpy(temp_str, options[i]->name, 255);
|
strncpy(temp_str, options[i]->name, 256);
|
||||||
|
|
||||||
char* dot = strrchr(temp_str, '.');
|
char* dot = strrchr(temp_str, '.');
|
||||||
if (hide_ext && dot) *dot = '\0';
|
if (hide_ext && dot) *dot = '\0';
|
||||||
@ -659,18 +968,19 @@ u32 ShowFileScrollPrompt(u32 n, const DirEntry** options, bool hide_ext, const c
|
|||||||
ResizeString(content_str, temp_str, fname_len, 8, false);
|
ResizeString(content_str, temp_str, fname_len, 8, false);
|
||||||
|
|
||||||
DrawStringF(MAIN_SCREEN, x, yopt + ((line_height+2)*(i-scroll)),
|
DrawStringF(MAIN_SCREEN, x, yopt + ((line_height+2)*(i-scroll)),
|
||||||
(sel == (int)i) ? COLOR_STD_FONT : COLOR_ENTRY(options[i]), COLOR_STD_BG, "%2.2s %s",
|
(sel == i) ? COLOR_STD_FONT : COLOR_ENTRY(options[i]), COLOR_STD_BG, "%2.2s %s",
|
||||||
(sel == (int)i) ? "->" : "", content_str);
|
(sel == i) ? "->" : "", content_str);
|
||||||
|
|
||||||
DrawStringF(MAIN_SCREEN, x + item_width - font_width * 11, yopt + ((line_height+2)*(i-scroll)),
|
DrawStringF(MAIN_SCREEN, x + item_width - font_width * 11, yopt + ((line_height+2)*(i-scroll)),
|
||||||
(sel == (int)i) ? COLOR_STD_FONT : COLOR_ENTRY(options[i]), COLOR_STD_BG, "%10.10s",
|
(sel == i) ? COLOR_STD_FONT : COLOR_ENTRY(options[i]), COLOR_STD_BG, "%10.10s",
|
||||||
(options[i]->type == T_DIR) ? "(dir)" : (options[i]->type == T_DOTDOT) ? "(..)" : bytestr);
|
(options[i]->type == T_DIR) ? STR_DIR : (options[i]->type == T_DOTDOT) ? "(..)" : bytestr);
|
||||||
}
|
}
|
||||||
// show [n more]
|
// show [n more]
|
||||||
if (n - n_show - scroll) {
|
if (n - n_show - scroll > 0) {
|
||||||
char more_str[64 + 1];
|
char more_str[UTF_BUFFER_BYTESIZE(item_width / font_width)], temp_str[UTF_BUFFER_BYTESIZE(64)];
|
||||||
snprintf(more_str, 64, " [%d more]", (int)(n - (n_show-1) - scroll));
|
snprintf(temp_str, sizeof(temp_str), STR_N_MORE, (n - (n_show-1) - scroll));
|
||||||
DrawStringF(MAIN_SCREEN, x, yopt + (line_height+2)*(n_show-1), COLOR_LIGHTGREY, COLOR_STD_BG, "%-*s", item_width / font_width, more_str);
|
ResizeString(more_str, temp_str, item_width / font_width, 8, false);
|
||||||
|
DrawString(MAIN_SCREEN, more_str, x, yopt + (line_height+2)*(n_show-1), COLOR_LIGHTGREY, COLOR_STD_BG);
|
||||||
}
|
}
|
||||||
// show scroll bar
|
// show scroll bar
|
||||||
u32 bar_x = x + item_width + 2;
|
u32 bar_x = x + item_width + 2;
|
||||||
@ -688,8 +998,8 @@ u32 ShowFileScrollPrompt(u32 n, const DirEntry** options, bool hide_ext, const c
|
|||||||
} else DrawRectangle(MAIN_SCREEN, bar_x, yopt, bar_width, flist_height, COLOR_STD_BG);
|
} else DrawRectangle(MAIN_SCREEN, bar_x, yopt, bar_width, flist_height, COLOR_STD_BG);
|
||||||
|
|
||||||
u32 pad_state = InputWait(0);
|
u32 pad_state = InputWait(0);
|
||||||
if (pad_state & BUTTON_DOWN) sel++;
|
if (pad_state & BUTTON_DOWN) sel = (sel+1) % n;
|
||||||
else if (pad_state & BUTTON_UP) sel--;
|
else if (pad_state & BUTTON_UP) sel = (sel+n-1) % n;
|
||||||
else if (pad_state & BUTTON_RIGHT) sel += n_show;
|
else if (pad_state & BUTTON_RIGHT) sel += n_show;
|
||||||
else if (pad_state & BUTTON_LEFT) sel -= n_show;
|
else if (pad_state & BUTTON_LEFT) sel -= n_show;
|
||||||
else if (pad_state & BUTTON_A) break;
|
else if (pad_state & BUTTON_A) break;
|
||||||
@ -698,15 +1008,15 @@ u32 ShowFileScrollPrompt(u32 n, const DirEntry** options, bool hide_ext, const c
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (sel < 0) sel = 0;
|
if (sel < 0) sel = 0;
|
||||||
else if (sel >= (int)n) sel = n-1;
|
else if (sel >= n) sel = n-1;
|
||||||
if (sel < scroll) scroll = sel;
|
if (sel < scroll) scroll = sel;
|
||||||
else if (sel == (int) n-1 && sel >= (int)(scroll + n_show - 1)) scroll = sel - n_show + 1;
|
else if (sel == n-1 && sel >= (scroll + n_show - 1)) scroll = sel - n_show + 1;
|
||||||
else if (sel >= (int)(scroll + (n_show-1) - 1)) scroll = sel - (n_show-1) + 1;
|
else if (sel >= (scroll + (n_show-1) - 1)) scroll = sel - (n_show-1) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ClearScreenF(true, false, COLOR_STD_BG);
|
ClearScreenF(true, false, COLOR_STD_BG);
|
||||||
|
|
||||||
return (sel >= (int)n) ? 0 : sel + 1;
|
return (sel >= n) ? 0 : sel + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *format, ...) {
|
u32 ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *format, ...) {
|
||||||
@ -723,7 +1033,7 @@ u32 ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *f
|
|||||||
ButtonToString(keys[i], buttonstr);
|
ButtonToString(keys[i], buttonstr);
|
||||||
ptr += snprintf(ptr, STRBUF_SIZE - (ptr-str), "\n<%s> %s", buttonstr, options[i]);
|
ptr += snprintf(ptr, STRBUF_SIZE - (ptr-str), "\n<%s> %s", buttonstr, options[i]);
|
||||||
}
|
}
|
||||||
ptr += snprintf(ptr, STRBUF_SIZE - (ptr-str), "\n \n<%s> %s", "B", "cancel");
|
ptr += snprintf(ptr, STRBUF_SIZE - (ptr-str), "\n \n<%s> %s", "B", STR_CANCEL);
|
||||||
|
|
||||||
ClearScreenF(true, false, COLOR_STD_BG);
|
ClearScreenF(true, false, COLOR_STD_BG);
|
||||||
DrawStringCenter(MAIN_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
|
DrawStringCenter(MAIN_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
|
||||||
@ -746,7 +1056,7 @@ u32 ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *f
|
|||||||
|
|
||||||
bool ShowInputPrompt(char* inputstr, u32 max_size, u32 resize, const char* alphabet, const char *format, va_list va) {
|
bool ShowInputPrompt(char* inputstr, u32 max_size, u32 resize, const char* alphabet, const char *format, va_list va) {
|
||||||
const u32 alphabet_size = strnlen(alphabet, 256);
|
const u32 alphabet_size = strnlen(alphabet, 256);
|
||||||
const u32 input_shown = 22;
|
const u32 input_shown_length = 22;
|
||||||
const u32 fast_scroll = 4;
|
const u32 fast_scroll = 4;
|
||||||
const u64 aprv_delay = 128;
|
const u64 aprv_delay = 128;
|
||||||
|
|
||||||
@ -772,7 +1082,7 @@ bool ShowInputPrompt(char* inputstr, u32 max_size, u32 resize, const char* alpha
|
|||||||
ClearScreenF(true, false, COLOR_STD_BG);
|
ClearScreenF(true, false, COLOR_STD_BG);
|
||||||
DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
|
DrawStringF(MAIN_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%s", str);
|
||||||
DrawStringF(MAIN_SCREEN, x + 8, y + str_height - 40, COLOR_STD_FONT, COLOR_STD_BG,
|
DrawStringF(MAIN_SCREEN, x + 8, y + str_height - 40, COLOR_STD_FONT, COLOR_STD_BG,
|
||||||
"R - (\x18\x19) fast scroll\nL - clear data%s", resize ? "\nX - remove char\nY - insert char" : "");
|
"%s\n%s", STR_R_FAST_SCROLL_L_CLEAR_DATA, resize ? STR_X_REMOVE_CHAR_Y_INSERT_CHAR : "");
|
||||||
|
|
||||||
// wait for all keys released
|
// wait for all keys released
|
||||||
while (HID_ReadState() & BUTTON_ANY);
|
while (HID_ReadState() & BUTTON_ANY);
|
||||||
@ -785,25 +1095,43 @@ bool ShowInputPrompt(char* inputstr, u32 max_size, u32 resize, const char* alpha
|
|||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
u32 inputstr_size = strnlen(inputstr, max_size - 1);
|
u32 inputstr_size = strnlen(inputstr, max_size - 1);
|
||||||
if (cursor_s < scroll) scroll = cursor_s;
|
|
||||||
else if (cursor_s - scroll >= input_shown) scroll = cursor_s - input_shown + 1;
|
if (cursor_s < scroll) {
|
||||||
while (scroll && (inputstr_size - scroll < input_shown)) scroll--;
|
scroll = cursor_s;
|
||||||
DrawStringF(MAIN_SCREEN, x, y + str_height - 76, COLOR_STD_FONT, COLOR_STD_BG, "%c%-*.*s%c%-*.*s\n%-*.*s^%-*.*s",
|
} else {
|
||||||
|
int scroll_adjust = -input_shown_length;
|
||||||
|
for (u32 i = scroll; i < cursor_s; 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++;
|
||||||
|
}
|
||||||
|
|
||||||
|
u16 cpos = 0;
|
||||||
|
for (u16 i = scroll; i < cursor_s; i++) {
|
||||||
|
if (i >= inputstr_size || (inputstr[i] & 0xC0) != 0x80) cpos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
DrawStringF(MAIN_SCREEN, x, y + str_height - 76, COLOR_STD_FONT, COLOR_STD_BG, "%c%-*.*s%c\n%-*.*s^%-*.*s",
|
||||||
(scroll) ? '<' : '|',
|
(scroll) ? '<' : '|',
|
||||||
(inputstr_size > input_shown) ? input_shown : inputstr_size,
|
(int) input_shown_size,
|
||||||
(inputstr_size > input_shown) ? input_shown : inputstr_size,
|
(int) input_shown_size,
|
||||||
inputstr + scroll,
|
(scroll > inputstr_size) ? "" : inputstr + scroll,
|
||||||
(inputstr_size - scroll > input_shown) ? '>' : '|',
|
(inputstr_size - (s32) scroll > input_shown_size) ? '>' : '|',
|
||||||
(inputstr_size > input_shown) ? 0 : input_shown - inputstr_size,
|
(int) 1 + cpos,
|
||||||
(inputstr_size > input_shown) ? 0 : input_shown - inputstr_size,
|
(int) 1 + cpos,
|
||||||
"",
|
"",
|
||||||
1 + cursor_s - scroll,
|
(int) input_shown_length - cpos,
|
||||||
1 + cursor_s - scroll,
|
(int) input_shown_length - cpos,
|
||||||
"",
|
|
||||||
input_shown - (cursor_s - scroll),
|
|
||||||
input_shown - (cursor_s - scroll),
|
|
||||||
""
|
""
|
||||||
);
|
);
|
||||||
|
|
||||||
if (cursor_a < 0) {
|
if (cursor_a < 0) {
|
||||||
for (cursor_a = alphabet_size - 1; (cursor_a > 0) && (alphabet[cursor_a] != inputstr[cursor_s]); cursor_a--);
|
for (cursor_a = alphabet_size - 1; (cursor_a > 0) && (alphabet[cursor_a] != inputstr[cursor_s]); cursor_a--);
|
||||||
}
|
}
|
||||||
@ -843,11 +1171,26 @@ bool ShowInputPrompt(char* inputstr, u32 max_size, u32 resize, const char* alpha
|
|||||||
}
|
}
|
||||||
} else if (pad_state & BUTTON_X) {
|
} else if (pad_state & BUTTON_X) {
|
||||||
if (resize && (inputstr_size > resize)) {
|
if (resize && (inputstr_size > resize)) {
|
||||||
char* inputfrom = inputstr + cursor_s - (cursor_s % resize) + resize;
|
u32 char_index = 0;
|
||||||
char* inputto = inputstr + cursor_s - (cursor_s % resize);
|
for(u32 i = 0; i < cursor_s; i++) {
|
||||||
|
if (i >= inputstr_size || (inputstr[i] & 0xC0) != 0x80) char_index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 to_index = char_index - (char_index % resize);
|
||||||
|
u32 from_index = to_index + resize;
|
||||||
|
|
||||||
|
char* inputto = inputstr + cursor_s;
|
||||||
|
for (u32 i = 0; i < char_index - to_index; i++) {
|
||||||
|
inputto -= GetPrevCharSize(inputto);
|
||||||
|
}
|
||||||
|
char* inputfrom = inputto;
|
||||||
|
for (u32 i = 0; i < from_index - to_index; i++) {
|
||||||
|
inputfrom += GetCharSize(inputfrom);
|
||||||
|
}
|
||||||
|
|
||||||
memmove(inputto, inputfrom, max_size - (inputfrom - inputstr));
|
memmove(inputto, inputfrom, max_size - (inputfrom - inputstr));
|
||||||
inputstr_size -= resize;
|
inputstr_size -= inputfrom - inputto;
|
||||||
while (cursor_s >= inputstr_size)
|
while (cursor_s >= inputstr_size || (inputstr[cursor_s] & 0xC0) == 0x80)
|
||||||
cursor_s--;
|
cursor_s--;
|
||||||
cursor_a = -1;
|
cursor_a = -1;
|
||||||
} else if (resize == 1) {
|
} else if (resize == 1) {
|
||||||
@ -864,18 +1207,29 @@ bool ShowInputPrompt(char* inputstr, u32 max_size, u32 resize, const char* alpha
|
|||||||
cursor_a = 0;
|
cursor_a = 0;
|
||||||
}
|
}
|
||||||
} else if (pad_state & BUTTON_UP) {
|
} else if (pad_state & BUTTON_UP) {
|
||||||
|
int size = GetCharSize(inputstr + cursor_s);
|
||||||
|
if(size > 1) {
|
||||||
|
memmove(inputstr + cursor_s, inputstr + cursor_s + size - 1, inputstr_size - cursor_s + size - 1);
|
||||||
|
}
|
||||||
|
|
||||||
cursor_a += (pad_state & BUTTON_R1) ? fast_scroll : 1;
|
cursor_a += (pad_state & BUTTON_R1) ? fast_scroll : 1;
|
||||||
cursor_a = cursor_a % alphabet_size;
|
cursor_a = cursor_a % alphabet_size;
|
||||||
inputstr[cursor_s] = alphabet[cursor_a];
|
inputstr[cursor_s] = alphabet[cursor_a];
|
||||||
} else if (pad_state & BUTTON_DOWN) {
|
} else if (pad_state & BUTTON_DOWN) {
|
||||||
|
int size = GetCharSize(inputstr + cursor_s);
|
||||||
|
if(size > 1) {
|
||||||
|
memmove(inputstr + cursor_s, inputstr + cursor_s + size - 1, inputstr_size - cursor_s + size - 1);
|
||||||
|
}
|
||||||
|
|
||||||
cursor_a -= (pad_state & BUTTON_R1) ? fast_scroll : 1;
|
cursor_a -= (pad_state & BUTTON_R1) ? fast_scroll : 1;
|
||||||
if (cursor_a < 0) cursor_a = alphabet_size + cursor_a;
|
if (cursor_a < 0) cursor_a = alphabet_size + cursor_a;
|
||||||
inputstr[cursor_s] = alphabet[cursor_a];
|
inputstr[cursor_s] = alphabet[cursor_a];
|
||||||
} else if (pad_state & BUTTON_LEFT) {
|
} else if (pad_state & BUTTON_LEFT) {
|
||||||
if (cursor_s > 0) cursor_s--;
|
if (cursor_s > 0) cursor_s -= GetPrevCharSize(inputstr + cursor_s);
|
||||||
cursor_a = -1;
|
cursor_a = -1;
|
||||||
} else if (pad_state & BUTTON_RIGHT) {
|
} else if (pad_state & BUTTON_RIGHT) {
|
||||||
if (cursor_s < max_size - 2) cursor_s++;
|
int size = cursor_s > inputstr_size ? 1 : GetCharSize(inputstr + cursor_s);
|
||||||
|
if (cursor_s + size < max_size - 1) cursor_s += size;
|
||||||
if (cursor_s >= inputstr_size) {
|
if (cursor_s >= inputstr_size) {
|
||||||
memset(inputstr + cursor_s, alphabet[0], resize);
|
memset(inputstr + cursor_s, alphabet[0], resize);
|
||||||
inputstr[cursor_s + resize] = '\0';
|
inputstr[cursor_s + resize] = '\0';
|
||||||
@ -911,7 +1265,7 @@ u64 ShowHexPrompt(u64 start_val, u32 n_digits, const char *format, ...) {
|
|||||||
va_list va;
|
va_list va;
|
||||||
|
|
||||||
if (n_digits > 16) n_digits = 16;
|
if (n_digits > 16) n_digits = 16;
|
||||||
snprintf(inputstr, 16 + 1, "%0*llX", (int) n_digits, start_val);
|
snprintf(inputstr, sizeof(inputstr), "%0*llX", (int) n_digits, start_val);
|
||||||
|
|
||||||
va_start(va, format);
|
va_start(va, format);
|
||||||
if (ShowInputPrompt(inputstr, n_digits + 1, 0, alphabet, format, va)) {
|
if (ShowInputPrompt(inputstr, n_digits + 1, 0, alphabet, format, va)) {
|
||||||
@ -928,7 +1282,7 @@ u64 ShowNumberPrompt(u64 start_val, const char *format, ...) {
|
|||||||
u64 ret = 0;
|
u64 ret = 0;
|
||||||
va_list va;
|
va_list va;
|
||||||
|
|
||||||
snprintf(inputstr, 20 + 1, "%llu", start_val);
|
snprintf(inputstr, sizeof(inputstr), "%llu", start_val);
|
||||||
|
|
||||||
va_start(va, format);
|
va_start(va, format);
|
||||||
if (ShowInputPrompt(inputstr, 20 + 1, 1, alphabet, format, va)) {
|
if (ShowInputPrompt(inputstr, 20 + 1, 1, alphabet, format, va)) {
|
||||||
@ -1048,8 +1402,8 @@ bool ShowProgress(u64 current, u64 total, const char* opstr)
|
|||||||
const u32 text_pos_y = bar_pos_y + bar_height + 2;
|
const u32 text_pos_y = bar_pos_y + bar_height + 2;
|
||||||
u32 prog_width = ((total > 0) && (current <= total)) ? (current * (bar_width-4)) / total : 0;
|
u32 prog_width = ((total > 0) && (current <= total)) ? (current * (bar_width-4)) / total : 0;
|
||||||
u32 prog_percent = ((total > 0) && (current <= total)) ? (current * 100) / total : 0;
|
u32 prog_percent = ((total > 0) && (current <= total)) ? (current * 100) / total : 0;
|
||||||
char tempstr[64];
|
char tempstr[UTF_BUFFER_BYTESIZE(64)];
|
||||||
char progstr[64];
|
char progstr[UTF_BUFFER_BYTESIZE(64)];
|
||||||
|
|
||||||
static u64 last_msec_elapsed = 0;
|
static u64 last_msec_elapsed = 0;
|
||||||
static u64 last_sec_remain = 0;
|
static u64 last_sec_remain = 0;
|
||||||
@ -1073,16 +1427,20 @@ bool ShowProgress(u64 current, u64 total, const char* opstr)
|
|||||||
DrawRectangle(MAIN_SCREEN, bar_pos_x + 2 + prog_width, bar_pos_y + 2, (bar_width-4) - prog_width, bar_height - 4, COLOR_STD_BG);
|
DrawRectangle(MAIN_SCREEN, bar_pos_x + 2 + prog_width, bar_pos_y + 2, (bar_width-4) - prog_width, bar_height - 4, COLOR_STD_BG);
|
||||||
|
|
||||||
TruncateString(progstr, opstr, min(63, (bar_width / FONT_WIDTH_EXT) - 7), 8);
|
TruncateString(progstr, opstr, min(63, (bar_width / FONT_WIDTH_EXT) - 7), 8);
|
||||||
snprintf(tempstr, 64, "%s (%lu%%)", progstr, prog_percent);
|
snprintf(tempstr, sizeof(tempstr), "%s (%lu%%)", progstr, prog_percent);
|
||||||
ResizeString(progstr, tempstr, bar_width / FONT_WIDTH_EXT, 8, false);
|
ResizeString(progstr, tempstr, bar_width / FONT_WIDTH_EXT, 8, false);
|
||||||
DrawString(MAIN_SCREEN, progstr, bar_pos_x, text_pos_y, COLOR_STD_FONT, COLOR_STD_BG, true);
|
DrawString(MAIN_SCREEN, progstr, bar_pos_x, text_pos_y, COLOR_STD_FONT, COLOR_STD_BG);
|
||||||
if (sec_elapsed >= 1) {
|
if (sec_elapsed >= 1) {
|
||||||
snprintf(tempstr, 16, "ETA %02llum%02llus", sec_remain / 60, sec_remain % 60);
|
if (sec_remain >= 3600) {
|
||||||
|
snprintf(tempstr, sizeof(tempstr), STR_ETA_N_HOUR_N_MIN_N_SEC, (sec_remain / 3600), (sec_remain / 60) % 60, sec_remain % 60);
|
||||||
|
} else {
|
||||||
|
snprintf(tempstr, sizeof(tempstr), STR_ETA_N_MIN_N_SEC, sec_remain / 60, sec_remain % 60);
|
||||||
|
}
|
||||||
ResizeString(progstr, tempstr, 16, 8, true);
|
ResizeString(progstr, tempstr, 16, 8, true);
|
||||||
DrawString(MAIN_SCREEN, progstr, bar_pos_x + bar_width - 1 - (FONT_WIDTH_EXT * 16),
|
DrawString(MAIN_SCREEN, progstr, bar_pos_x + bar_width - 1 - (FONT_WIDTH_EXT * 16),
|
||||||
bar_pos_y - line_height - 1, COLOR_STD_FONT, COLOR_STD_BG, true);
|
bar_pos_y - line_height - 1, COLOR_STD_FONT, COLOR_STD_BG);
|
||||||
}
|
}
|
||||||
DrawString(MAIN_SCREEN, "(hold B to cancel)", bar_pos_x + 2, text_pos_y + 14, COLOR_STD_FONT, COLOR_STD_BG, false);
|
DrawString(MAIN_SCREEN, STR_HOLD_B_TO_CANCEL, bar_pos_x + 2, text_pos_y + 14, COLOR_STD_FONT, COLOR_STD_BG);
|
||||||
|
|
||||||
last_prog_width = prog_width;
|
last_prog_width = prog_width;
|
||||||
|
|
||||||
@ -1095,13 +1453,7 @@ int ShowBrightnessConfig(int set_brightness)
|
|||||||
u32 btn_input, bar_count;
|
u32 btn_input, bar_count;
|
||||||
int bar_x_pos, bar_y_pos, bar_width, bar_height;
|
int bar_x_pos, bar_y_pos, bar_width, bar_height;
|
||||||
|
|
||||||
const char *brightness_str =
|
const char *brightness_str = STR_BRIGHTNESS_CONTROLS;
|
||||||
"[\x1B] Decrease brightness\n"
|
|
||||||
"[\x1A] Increase brightness\n"
|
|
||||||
" \n"
|
|
||||||
"[X] Use volume slider control\n"
|
|
||||||
"[A] Set current brightness\n"
|
|
||||||
"[B] Cancel";
|
|
||||||
static const u16 brightness_slider_colmasks[] = {
|
static const u16 brightness_slider_colmasks[] = {
|
||||||
COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE
|
COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE
|
||||||
};
|
};
|
||||||
@ -1122,7 +1474,7 @@ int ShowBrightnessConfig(int set_brightness)
|
|||||||
// draw initial UI stuff
|
// draw initial UI stuff
|
||||||
DrawStringF(MAIN_SCREEN,
|
DrawStringF(MAIN_SCREEN,
|
||||||
(SCREEN_WIDTH_MAIN - GetDrawStringWidth(brightness_str)) / 2,
|
(SCREEN_WIDTH_MAIN - GetDrawStringWidth(brightness_str)) / 2,
|
||||||
(SCREEN_HEIGHT / 4) * 2, COLOR_STD_FONT, COLOR_STD_BG, brightness_str);
|
(SCREEN_HEIGHT / 4) * 2, COLOR_STD_FONT, COLOR_STD_BG, "%s", brightness_str);
|
||||||
|
|
||||||
// draw all color gradient bars
|
// draw all color gradient bars
|
||||||
for (int x = 0; x < bar_width; x++) {
|
for (int x = 0; x < bar_width; x++) {
|
||||||
|
@ -21,6 +21,11 @@
|
|||||||
#define FONT_WIDTH_EXT GetFontWidth()
|
#define FONT_WIDTH_EXT GetFontWidth()
|
||||||
#define FONT_HEIGHT_EXT GetFontHeight()
|
#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 TOP_SCREEN ((u16*)VRAM_TOP_LA)
|
||||||
#define BOT_SCREEN ((u16*)VRAM_BOT_A)
|
#define BOT_SCREEN ((u16*)VRAM_BOT_A)
|
||||||
|
|
||||||
@ -40,13 +45,14 @@
|
|||||||
|
|
||||||
|
|
||||||
#ifndef AUTO_UNLOCK
|
#ifndef AUTO_UNLOCK
|
||||||
bool ShowUnlockSequence(u32 seqlvl, const char *format, ...);
|
bool PRINTF_ARGS(2) ShowUnlockSequence(u32 seqlvl, const char *format, ...);
|
||||||
#else
|
#else
|
||||||
#define ShowUnlockSequence ShowPrompt
|
#define ShowUnlockSequence ShowPrompt
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
u8* GetFontFromPbm(const void* pbm, const u32 pbm_size, u32* w, u32* h);
|
const u8* GetFontFromPbm(const void* pbm, const u32 riff_size, u32* w, u32* h);
|
||||||
bool SetFontFromPbm(const void* pbm, const u32 pbm_size);
|
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);
|
u16 GetColor(const u16 *screen, int x, int y);
|
||||||
|
|
||||||
@ -57,35 +63,39 @@ 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 DrawBitmap(u16 *screen, int x, int y, u32 w, u32 h, const u16* bitmap);
|
||||||
void DrawQrCode(u16 *screen, const u8* qrcode);
|
void DrawQrCode(u16 *screen, const u8* qrcode);
|
||||||
|
|
||||||
void DrawCharacter(u16 *screen, int character, int x, int y, u32 color, u32 bgcolor);
|
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, bool fix_utf8);
|
void DrawString(u16 *screen, const char *str, int x, int y, u32 color, u32 bgcolor);
|
||||||
void DrawStringF(u16 *screen, int x, int y, u32 color, u32 bgcolor, const char *format, ...);
|
void PRINTF_ARGS(6) DrawStringF(u16 *screen, int x, int y, u32 color, u32 bgcolor, const char *format, ...);
|
||||||
void DrawStringCenter(u16 *screen, 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 GetDrawStringHeight(const char* str);
|
||||||
u32 GetDrawStringWidth(const char* str);
|
u32 GetDrawStringWidth(const char* str);
|
||||||
u32 GetFontWidth(void);
|
u32 GetFontWidth(void);
|
||||||
u32 GetFontHeight(void);
|
u32 GetFontHeight(void);
|
||||||
|
|
||||||
|
void MultiLineString(char* dest, const char* orig, int llen, int maxl);
|
||||||
void WordWrapString(char* str, int llen);
|
void WordWrapString(char* str, int llen);
|
||||||
void ResizeString(char* dest, const char* orig, int nsize, int tpos, bool align_right);
|
void ResizeString(char* dest, const char* orig, int nlength, int tpos, bool align_right);
|
||||||
void TruncateString(char* dest, const char* orig, int nsize, int tpos);
|
void TruncateString(char* dest, const char* orig, int nlength, int tpos);
|
||||||
void FormatNumber(char* str, u64 number);
|
void FormatNumber(char* str, u64 number);
|
||||||
void FormatBytes(char* str, u64 bytes);
|
void FormatBytes(char* str, u64 bytes);
|
||||||
|
|
||||||
void ShowString(const char *format, ...);
|
void PRINTF_ARGS(1) ShowString(const char *format, ...);
|
||||||
void ShowStringF(u16* screen, const char *format, ...);
|
void PRINTF_ARGS(2) ShowStringF(u16* screen, const char *format, ...);
|
||||||
void ShowIconString(u16* icon, int w, int h, const char *format, ...);
|
void PRINTF_ARGS(4) ShowIconString(u16* icon, int w, int h, const char *format, ...);
|
||||||
void ShowIconStringF(u16* screen, 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 ShowPrompt(bool ask, const char *format, ...);
|
bool PRINTF_ARGS(2) ShowPrompt(bool ask, const char *format, ...);
|
||||||
u32 ShowSelectPrompt(u32 n, const char** options, const char *format, ...);
|
u32 PRINTF_ARGS(3) ShowSelectPrompt(int n, const char** options, const char *format, ...);
|
||||||
u32 ShowFileScrollPrompt(u32 n, const DirEntry** entries, bool hide_ext, const char *format, ...);
|
u32 PRINTF_ARGS(4) ShowFileScrollPrompt(int n, const DirEntry** entries, bool hide_ext, const char *format, ...);
|
||||||
u32 ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *format, ...);
|
u32 PRINTF_ARGS(4) ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *format, ...);
|
||||||
bool ShowStringPrompt(char* inputstr, u32 max_size, const char *format, ...);
|
bool PRINTF_ARGS(3) ShowStringPrompt(char* inputstr, u32 max_size, const char *format, ...);
|
||||||
u64 ShowHexPrompt(u64 start_val, u32 n_digits, const char *format, ...);
|
u64 PRINTF_ARGS(3) ShowHexPrompt(u64 start_val, u32 n_digits, const char *format, ...);
|
||||||
u64 ShowNumberPrompt(u64 start_val, const char *format, ...);
|
u64 PRINTF_ARGS(2) ShowNumberPrompt(u64 start_val, const char *format, ...);
|
||||||
bool ShowDataPrompt(u8* data, u32* size, const char *format, ...);
|
bool PRINTF_ARGS(3) ShowDataPrompt(u8* data, u32* size, const char *format, ...);
|
||||||
bool ShowRtcSetterPrompt(void* time, const char *format, ...);
|
bool PRINTF_ARGS(2) ShowRtcSetterPrompt(void* time, const char *format, ...);
|
||||||
bool ShowProgress(u64 current, u64 total, const char* opstr);
|
bool ShowProgress(u64 current, u64 total, const char* opstr);
|
||||||
|
|
||||||
int ShowBrightnessConfig(int set_brightness);
|
int ShowBrightnessConfig(int set_brightness);
|
||||||
|
@ -40,10 +40,13 @@ u32 GetUnitKeysType(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void CryptAesKeyInfo(AesKeyInfo* info) {
|
void CryptAesKeyInfo(AesKeyInfo* info) {
|
||||||
static u8 zeroes[16] = { 0 };
|
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 };
|
u8 ctr[16] = { 0 };
|
||||||
memcpy(ctr, (void*) info, 12); // CTR -> slot + type + id + zeroes
|
memcpy(ctr, (void*) info, 12); // CTR -> slot + type + id + zeroes
|
||||||
setup_aeskeyY(0x2C, zeroes);
|
setup_aeskeyY(0x2C, aeskeyY);
|
||||||
use_aeskey(0x2C);
|
use_aeskey(0x2C);
|
||||||
set_ctr(ctr);
|
set_ctr(ctr);
|
||||||
aes_decrypt(info->key, info->key, 1, AES_CNT_CTRNAND_MODE);
|
aes_decrypt(info->key, info->key, 1, AES_CNT_CTRNAND_MODE);
|
||||||
@ -152,7 +155,7 @@ u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id)
|
|||||||
// load legacy slot0x??Key?.bin file instead
|
// load legacy slot0x??Key?.bin file instead
|
||||||
if (!found && (type != 'I')) {
|
if (!found && (type != 'I')) {
|
||||||
char fname[64];
|
char fname[64];
|
||||||
snprintf(fname, 64, "slot0x%02lXKey%s%s.bin", keyslot,
|
snprintf(fname, sizeof(fname), "slot0x%02lXKey%s%s.bin", keyslot,
|
||||||
(type == 'X') ? "X" : (type == 'Y') ? "Y" : (type == 'I') ? "IV" : "", (id) ? id : "");
|
(type == 'X') ? "X" : (type == 'Y') ? "Y" : (type == 'I') ? "IV" : "", (id) ? id : "");
|
||||||
found = (LoadSupportFile(fname, key, 16) == 16);
|
found = (LoadSupportFile(fname, key, 16) == 16);
|
||||||
}
|
}
|
||||||
@ -226,25 +229,3 @@ u32 InitKeyDb(const char* path)
|
|||||||
free(keydb);
|
free(keydb);
|
||||||
return (nkeys) ? 0 : 1;
|
return (nkeys) ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// creates dependency to "sha.h", not required for base keydb functions
|
|
||||||
u32 CheckRecommendedKeyDb(const char* path)
|
|
||||||
{
|
|
||||||
// SHA-256 of the recommended aeskeydb.bin file
|
|
||||||
// equals MD5 A5B28945A7C051D7A0CD18AF0E580D1B
|
|
||||||
static const u8 recommended_sha[0x20] = {
|
|
||||||
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
|
|
||||||
};
|
|
||||||
|
|
||||||
// try to load aeskeydb.bin file
|
|
||||||
AesKeyInfo* keydb = (AesKeyInfo*) malloc(STD_BUFFER_SIZE);
|
|
||||||
if (!keydb) return 1;
|
|
||||||
u32 nkeys = LoadKeyDb(path, keydb, STD_BUFFER_SIZE);
|
|
||||||
|
|
||||||
// compare with recommended SHA
|
|
||||||
bool res = (nkeys && (sha_cmp(recommended_sha, keydb, nkeys * sizeof(AesKeyInfo), SHA256_MODE) == 0));
|
|
||||||
|
|
||||||
free(keydb);
|
|
||||||
return res ? 0 : 1;
|
|
||||||
}
|
|
||||||
|
@ -30,4 +30,3 @@ u32 GetUnitKeysType(void);
|
|||||||
void CryptAesKeyInfo(AesKeyInfo* info);
|
void CryptAesKeyInfo(AesKeyInfo* info);
|
||||||
u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id);
|
u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id);
|
||||||
u32 InitKeyDb(const char* path);
|
u32 InitKeyDb(const char* path);
|
||||||
u32 CheckRecommendedKeyDb(const char* path);
|
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
#include "keydb.h"
|
#include "keydb.h"
|
||||||
#include "ctrtransfer.h"
|
#include "ctrtransfer.h"
|
||||||
#include "scripting.h"
|
#include "scripting.h"
|
||||||
|
#include "gm9lua.h"
|
||||||
#include "png.h"
|
#include "png.h"
|
||||||
#include "ui.h" // only for font file detection
|
#include "ui.h" // only for font file detection
|
||||||
|
|
||||||
@ -86,11 +87,14 @@ u64 IdentifyFileType(const char* path) {
|
|||||||
return GAME_ROMFS; // RomFS file (check could be better)
|
return GAME_ROMFS; // RomFS file (check could be better)
|
||||||
} else if (ValidateTmd((TitleMetaData*) data) == 0) {
|
} else if (ValidateTmd((TitleMetaData*) data) == 0) {
|
||||||
if (fsize == TMD_SIZE_N(getbe16(header + 0x1DE)) + TMD_CDNCERT_SIZE)
|
if (fsize == TMD_SIZE_N(getbe16(header + 0x1DE)) + TMD_CDNCERT_SIZE)
|
||||||
return GAME_TMD | FLAG_NUSCDN; // TMD file from NUS/CDN
|
return GAME_CDNTMD; // TMD file from NUS/CDN
|
||||||
else if (fsize >= TMD_SIZE_N(getbe16(header + 0x1DE)))
|
else if (fsize >= TMD_SIZE_N(getbe16(header + 0x1DE)))
|
||||||
return GAME_TMD; // TMD file
|
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) {
|
} else if (ValidateTicket((Ticket*) data) == 0) {
|
||||||
return GAME_TICKET; // Ticket file (not used for anything right now)
|
return GAME_TICKET; // Ticket file
|
||||||
} else if (ValidateFirmHeader((FirmHeader*) data, fsize) == 0) {
|
} else if (ValidateFirmHeader((FirmHeader*) data, fsize) == 0) {
|
||||||
return SYS_FIRM; // FIRM file
|
return SYS_FIRM; // FIRM file
|
||||||
} else if ((ValidateAgbSaveHeader((AgbSaveHeader*) data) == 0) && (fsize >= AGBSAVE_MAX_SIZE)) {
|
} else if ((ValidateAgbSaveHeader((AgbSaveHeader*) data) == 0) && (fsize >= AGBSAVE_MAX_SIZE)) {
|
||||||
@ -124,6 +128,10 @@ u64 IdentifyFileType(const char* path) {
|
|||||||
}
|
}
|
||||||
} else if (GetFontFromPbm(data, fsize, NULL, NULL)) {
|
} else if (GetFontFromPbm(data, fsize, NULL, NULL)) {
|
||||||
return FONT_PBM;
|
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)) &&
|
} else if ((fsize > sizeof(AgbHeader)) &&
|
||||||
(ValidateAgbHeader((AgbHeader*) data) == 0)) {
|
(ValidateAgbHeader((AgbHeader*) data) == 0)) {
|
||||||
return GAME_GBA;
|
return GAME_GBA;
|
||||||
@ -143,14 +151,6 @@ u64 IdentifyFileType(const char* path) {
|
|||||||
} else if ((strncasecmp(ext, "png", 4) == 0) &&
|
} else if ((strncasecmp(ext, "png", 4) == 0) &&
|
||||||
(fsize > sizeof(png_magic)) && (memcmp(data, png_magic, sizeof(png_magic)) == 0)) {
|
(fsize > sizeof(png_magic)) && (memcmp(data, png_magic, sizeof(png_magic)) == 0)) {
|
||||||
return GFX_PNG;
|
return GFX_PNG;
|
||||||
} else if ((strncasecmp(ext, "cdn", 4) == 0) || (strncasecmp(ext, "nus", 4) == 0)) {
|
|
||||||
char path_cetk[256];
|
|
||||||
char* ext_cetk = path_cetk + (ext - path);
|
|
||||||
strncpy(path_cetk, path, 256);
|
|
||||||
path_cetk[255] = '\0';
|
|
||||||
strncpy(ext_cetk, "cetk", 5);
|
|
||||||
if (FileGetSize(path_cetk) > 0)
|
|
||||||
return GAME_NUSCDN; // NUS/CDN type 2
|
|
||||||
} else if (strncasecmp(fname, TIKDB_NAME_ENC, sizeof(TIKDB_NAME_ENC)+1) == 0) {
|
} else if (strncasecmp(fname, TIKDB_NAME_ENC, sizeof(TIKDB_NAME_ENC)+1) == 0) {
|
||||||
return BIN_TIKDB | FLAG_ENC; // titlekey database / encrypted
|
return BIN_TIKDB | FLAG_ENC; // titlekey database / encrypted
|
||||||
} else if (strncasecmp(fname, TIKDB_NAME_DEC, sizeof(TIKDB_NAME_DEC)+1) == 0) {
|
} else if (strncasecmp(fname, TIKDB_NAME_DEC, sizeof(TIKDB_NAME_DEC)+1) == 0) {
|
||||||
@ -159,10 +159,16 @@ u64 IdentifyFileType(const char* path) {
|
|||||||
return BIN_KEYDB; // key database
|
return BIN_KEYDB; // key database
|
||||||
} else if ((sscanf(fname, "slot%02lXKey", &id) == 1) && (strncasecmp(ext, "bin", 4) == 0) && (fsize = 16) && (id < 0x40)) {
|
} else if ((sscanf(fname, "slot%02lXKey", &id) == 1) && (strncasecmp(ext, "bin", 4) == 0) && (fsize = 16) && (id < 0x40)) {
|
||||||
return BIN_LEGKEY; // legacy key file
|
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)) {
|
} else if (ValidateText((char*) data, (fsize > 0x200) ? 0x200 : fsize)) {
|
||||||
u64 type = 0;
|
u64 type = 0;
|
||||||
if ((fsize < SCRIPT_MAX_SIZE) && (strncasecmp(ext, SCRIPT_EXT, strnlen(SCRIPT_EXT, 16) + 1) == 0))
|
if ((fsize < SCRIPT_MAX_SIZE) && (strcasecmp(ext, SCRIPT_EXT) == 0))
|
||||||
type |= TXT_SCRIPT; // should be a script (which is also generic text)
|
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;
|
if (fsize < STD_BUFFER_SIZE) type |= TXT_GENERIC;
|
||||||
return type;
|
return type;
|
||||||
} else if ((strncmp(path + 2, "/Nintendo DSiWare/", 18) == 0) &&
|
} else if ((strncmp(path + 2, "/Nintendo DSiWare/", 18) == 0) &&
|
||||||
@ -176,12 +182,12 @@ u64 IdentifyFileType(const char* path) {
|
|||||||
char* name_cdn = path_cdn + (fname - path);
|
char* name_cdn = path_cdn + (fname - path);
|
||||||
strncpy(path_cdn, path, 256);
|
strncpy(path_cdn, path, 256);
|
||||||
path_cdn[255] = '\0';
|
path_cdn[255] = '\0';
|
||||||
strncpy(name_cdn, "tmd", 4);
|
strncpy(name_cdn, "tmd", 4); // this will not catch tmd with version
|
||||||
if (FileGetSize(path_cdn) > 0)
|
if (FileGetSize(path_cdn) > 0)
|
||||||
return GAME_NUSCDN; // NUS/CDN type 1
|
return GAME_NUSCDN; // NUS/CDN, recognized by TMD
|
||||||
strncpy(name_cdn, "cetk", 5);
|
strncpy(name_cdn, "cetk", 5);
|
||||||
if (FileGetSize(path_cdn) > 0)
|
if (FileGetSize(path_cdn) > 0)
|
||||||
return GAME_NUSCDN; // NUS/CDN type 1
|
return GAME_NUSCDN; // NUS/CDN, recognized by CETK
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -8,58 +8,65 @@
|
|||||||
#define GAME_NCSD (1ULL<<3)
|
#define GAME_NCSD (1ULL<<3)
|
||||||
#define GAME_NCCH (1ULL<<4)
|
#define GAME_NCCH (1ULL<<4)
|
||||||
#define GAME_TMD (1ULL<<5)
|
#define GAME_TMD (1ULL<<5)
|
||||||
#define GAME_CMD (1ULL<<6)
|
#define GAME_CDNTMD (1ULL<<6)
|
||||||
#define GAME_EXEFS (1ULL<<7)
|
#define GAME_TWLTMD (1ULL<<7)
|
||||||
#define GAME_ROMFS (1ULL<<8)
|
#define GAME_CMD (1ULL<<8)
|
||||||
#define GAME_BOSS (1ULL<<9)
|
#define GAME_EXEFS (1ULL<<9)
|
||||||
#define GAME_NUSCDN (1ULL<<10)
|
#define GAME_ROMFS (1ULL<<10)
|
||||||
#define GAME_TICKET (1ULL<<11)
|
#define GAME_BOSS (1ULL<<11)
|
||||||
#define GAME_TIE (1ULL<<12)
|
#define GAME_NUSCDN (1ULL<<12)
|
||||||
#define GAME_SMDH (1ULL<<13)
|
#define GAME_TICKET (1ULL<<13)
|
||||||
#define GAME_3DSX (1ULL<<14)
|
#define GAME_TIE (1ULL<<14)
|
||||||
#define GAME_NDS (1ULL<<15)
|
#define GAME_SMDH (1ULL<<15)
|
||||||
#define GAME_GBA (1ULL<<16)
|
#define GAME_3DSX (1ULL<<16)
|
||||||
#define GAME_TAD (1ULL<<17)
|
#define GAME_NDS (1ULL<<17)
|
||||||
#define SYS_FIRM (1ULL<<18)
|
#define GAME_GBA (1ULL<<18)
|
||||||
#define SYS_DIFF (1ULL<<19)
|
#define GAME_TAD (1ULL<<19)
|
||||||
#define SYS_DISA (1ULL<<20)
|
#define SYS_FIRM (1ULL<<20)
|
||||||
#define SYS_AGBSAVE (1ULL<<21)
|
#define SYS_DIFF (1ULL<<21)
|
||||||
#define SYS_TICKDB (1ULL<<22)
|
#define SYS_DISA (1ULL<<22)
|
||||||
#define BIN_NCCHNFO (1ULL<<23)
|
#define SYS_AGBSAVE (1ULL<<23)
|
||||||
#define BIN_TIKDB (1ULL<<24)
|
#define SYS_TICKDB (1ULL<<24)
|
||||||
#define BIN_KEYDB (1ULL<<25)
|
#define BIN_CIFNSH (1ULL<<25)
|
||||||
#define BIN_LEGKEY (1ULL<<26)
|
#define BIN_NCCHNFO (1ULL<<26)
|
||||||
#define TXT_SCRIPT (1ULL<<27)
|
#define BIN_TIKDB (1ULL<<27)
|
||||||
#define TXT_GENERIC (1ULL<<28)
|
#define BIN_KEYDB (1ULL<<28)
|
||||||
#define GFX_PNG (1ULL<<29)
|
#define BIN_LEGKEY (1ULL<<29)
|
||||||
#define FONT_PBM (1ULL<<30)
|
#define TXT_SCRIPT (1ULL<<30)
|
||||||
#define NOIMG_NAND (1ULL<<31)
|
#define TXT_GENERIC (1ULL<<31)
|
||||||
#define HDR_NAND (1ULL<<32)
|
#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 TYPE_BASE 0xFFFFFFFFFFULL // 40 bit reserved for base types
|
||||||
|
|
||||||
// #define FLAG_FIRM (1ULL<<57) // <--- for CXIs containing FIRMs
|
// #define FLAG_FIRM (1ULL<<58) // <--- for CXIs containing FIRMs
|
||||||
// #define FLAG_GBAVC (1ULL<<58) // <--- for GBAVC CXIs
|
// #define FLAG_GBAVC (1ULL<<59) // <--- for GBAVC CXIs
|
||||||
#define FLAG_DSIW (1ULL<<59)
|
#define FLAG_DSIW (1ULL<<60)
|
||||||
#define FLAG_ENC (1ULL<<60)
|
#define FLAG_ENC (1ULL<<61)
|
||||||
#define FLAG_CTR (1ULL<<61)
|
#define FLAG_CTR (1ULL<<62)
|
||||||
#define FLAG_NUSCDN (1ULL<<62)
|
|
||||||
#define FLAG_CXI (1ULL<<63)
|
#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_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_TIE|GAME_BOSS|SYS_FIRM))
|
#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_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_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_TIE)) || ((tp&GAME_NDS)&&(tp&(FLAG_DSIW))))
|
#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) (FTYPE_CIABUILD(tp) && (tp&(GAME_TMD|GAME_TIE)))
|
#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)) || ((tp&GAME_NDS)&&(tp&(FLAG_DSIW))) || (tp&(GAME_TMD)&&(tp&(FLAG_NUSCDN))))
|
#define FTYPE_CIAINSTALL(tp) ((tp&(GAME_NCSD|GAME_NCCH|GAME_CIA|GAME_CDNTMD|GAME_TWLTMD)) || ((tp&GAME_NDS)&&(tp&(FLAG_DSIW))))
|
||||||
#define FTYPE_CXIDUMP(tp) (tp&(GAME_TMD))
|
#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_UNINSTALL(tp) (tp&(GAME_TIE))
|
||||||
#define FTYPE_TIKBUILD(tp) (tp&(GAME_TICKET|SYS_TICKDB|BIN_TIKDB))
|
#define FTYPE_TIKBUILD(tp) (tp&(GAME_TICKET|SYS_TICKDB|BIN_TIKDB))
|
||||||
#define FTYPE_KEYBUILD(tp) (tp&(BIN_KEYDB|BIN_LEGKEY))
|
#define FTYPE_KEYBUILD(tp) (tp&(BIN_KEYDB|BIN_LEGKEY))
|
||||||
#define FTYPE_TITLEINFO(tp) (tp&(GAME_TIE|GAME_SMDH|GAME_NCCH|GAME_NCSD|GAME_CIA|GAME_TMD|GAME_NDS|GAME_GBA|GAME_TAD|GAME_3DSX))
|
#define FTYPE_TITLEINFO(tp) (tp&(GAME_TIE|GAME_CIA|GAME_TMD|GAME_CDNTMD|GAME_TWLTMD))
|
||||||
#define FTYPE_CIACHECK(tp) (tp&GAME_CIA)
|
|
||||||
#define FTYPE_RENAMABLE(tp) (tp&(GAME_NCCH|GAME_NCSD|GAME_CIA|GAME_NDS|GAME_GBA))
|
#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|SYS_FIRM))
|
#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_TRANSFERABLE(tp) ((u64) (tp&(IMG_FAT|FLAG_CTR)) == (u64) (IMG_FAT|FLAG_CTR))
|
||||||
#define FTYPE_NCSDFIXABLE(tp) (tp&(HDR_NAND|NOIMG_NAND))
|
#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_HASCODE(tp) (((u64) (tp&(GAME_NCCH|FLAG_CXI)) == (u64) (GAME_NCCH|FLAG_CXI))|(tp&GAME_NCSD))
|
||||||
@ -71,9 +78,11 @@
|
|||||||
#define FTYPE_KEYINIT(tp) (tp&(BIN_KEYDB))
|
#define FTYPE_KEYINIT(tp) (tp&(BIN_KEYDB))
|
||||||
#define FTYPE_KEYINSTALL(tp) (tp&(BIN_KEYDB))
|
#define FTYPE_KEYINSTALL(tp) (tp&(BIN_KEYDB))
|
||||||
#define FTYPE_SCRIPT(tp) (tp&(TXT_SCRIPT))
|
#define FTYPE_SCRIPT(tp) (tp&(TXT_SCRIPT))
|
||||||
#define FTYPE_FONT(tp) (tp&(FONT_PBM))
|
#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_GFX(tp) (tp&(GFX_PNG))
|
||||||
#define FTYPE_SETABLE(tp) (tp&(FONT_PBM))
|
#define FTYPE_SETABLE(tp) (tp&(FONT_PBM|FONT_RIFF|TRANSLATION))
|
||||||
#define FTYPE_BOOTABLE(tp) (tp&(SYS_FIRM))
|
#define FTYPE_BOOTABLE(tp) (tp&(SYS_FIRM))
|
||||||
#define FTYPE_INSTALLABLE(tp) (tp&(SYS_FIRM))
|
#define FTYPE_INSTALLABLE(tp) (tp&(SYS_FIRM))
|
||||||
#define FTYPE_AGBSAVE(tp) (tp&(SYS_AGBSAVE))
|
#define FTYPE_AGBSAVE(tp) (tp&(SYS_AGBSAVE))
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "fsdrive.h"
|
#include "fsdrive.h"
|
||||||
#include "fsgame.h"
|
#include "fsgame.h"
|
||||||
#include "fsinit.h"
|
#include "fsinit.h"
|
||||||
|
#include "language.h"
|
||||||
#include "virtual.h"
|
#include "virtual.h"
|
||||||
#include "vcart.h"
|
#include "vcart.h"
|
||||||
#include "sddata.h"
|
#include "sddata.h"
|
||||||
@ -11,7 +12,7 @@
|
|||||||
// last search pattern, path & mode
|
// last search pattern, path & mode
|
||||||
static char search_pattern[256] = { 0 };
|
static char search_pattern[256] = { 0 };
|
||||||
static char search_path[256] = { 0 };
|
static char search_path[256] = { 0 };
|
||||||
static bool search_title_mode = false;
|
static bool title_manager_mode = false;
|
||||||
|
|
||||||
int DriveType(const char* path) {
|
int DriveType(const char* path) {
|
||||||
int type = DRV_UNKNOWN;
|
int type = DRV_UNKNOWN;
|
||||||
@ -21,6 +22,8 @@ int DriveType(const char* path) {
|
|||||||
type = DRV_FAT | DRV_ALIAS | ((*path == 'A') ? DRV_SYSNAND : DRV_EMUNAND);
|
type = DRV_FAT | DRV_ALIAS | ((*path == 'A') ? DRV_SYSNAND : DRV_EMUNAND);
|
||||||
} else if (*search_pattern && *search_path && (strncmp(path, "Z:", 3) == 0)) {
|
} else if (*search_pattern && *search_path && (strncmp(path, "Z:", 3) == 0)) {
|
||||||
type = DRV_SEARCH;
|
type = DRV_SEARCH;
|
||||||
|
} else if (title_manager_mode && (strncmp(path, "Y:", 3) == 0)) {
|
||||||
|
type = DRV_VIRTUAL | DRV_TITLEMAN;
|
||||||
} else if ((pdrv >= 0) && (pdrv < NORM_FS)) {
|
} else if ((pdrv >= 0) && (pdrv < NORM_FS)) {
|
||||||
if (pdrv == 0) {
|
if (pdrv == 0) {
|
||||||
type = DRV_FAT | DRV_SDCARD | DRV_STDFAT;
|
type = DRV_FAT | DRV_SDCARD | DRV_STDFAT;
|
||||||
@ -64,28 +67,31 @@ int DriveType(const char* path) {
|
|||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetFSSearch(const char* pattern, const char* path, bool mode) {
|
void SetFSSearch(const char* pattern, const char* path) {
|
||||||
if (pattern && path) {
|
if (pattern && path) {
|
||||||
strncpy(search_pattern, pattern, 256);
|
strncpy(search_pattern, pattern, 256);
|
||||||
search_pattern[255] = '\0';
|
search_pattern[255] = '\0';
|
||||||
strncpy(search_path, path, 256);
|
strncpy(search_path, path, 256);
|
||||||
search_path[255] = '\0';
|
search_path[255] = '\0';
|
||||||
search_title_mode = mode;
|
|
||||||
} else *search_pattern = *search_path = '\0';
|
} else *search_pattern = *search_path = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SetTitleManagerMode(bool mode) {
|
||||||
|
title_manager_mode = mode;
|
||||||
|
}
|
||||||
|
|
||||||
bool GetFATVolumeLabel(const char* drv, char* label) {
|
bool GetFATVolumeLabel(const char* drv, char* label) {
|
||||||
return (f_getlabel(drv, label, NULL) == FR_OK);
|
return (f_getlabel(drv, label, NULL) == FR_OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GetRootDirContentsWorker(DirStruct* contents) {
|
bool GetRootDirContentsWorker(DirStruct* contents) {
|
||||||
static const char* drvname[] = { FS_DRVNAME };
|
const char* drvname[] = { FS_DRVNAME };
|
||||||
static const char* drvnum[] = { FS_DRVNUM };
|
static const char* drvnum[] = { FS_DRVNUM };
|
||||||
u32 n_entries = 0;
|
u32 n_entries = 0;
|
||||||
|
|
||||||
char sdlabel[DRV_LABEL_LEN];
|
char sdlabel[DRV_LABEL_LEN];
|
||||||
if (!GetFATVolumeLabel("0:", sdlabel) || !(*sdlabel))
|
if (!GetFATVolumeLabel("0:", sdlabel) || !(*sdlabel))
|
||||||
strcpy(sdlabel, "NOLABEL");
|
strcpy(sdlabel, STR_LAB_NOLABEL);
|
||||||
|
|
||||||
char carttype[16];
|
char carttype[16];
|
||||||
GetVCartTypeString(carttype);
|
GetVCartTypeString(carttype);
|
||||||
@ -96,15 +102,15 @@ bool GetRootDirContentsWorker(DirStruct* contents) {
|
|||||||
if (!DriveType(drvnum[i])) continue; // drive not available
|
if (!DriveType(drvnum[i])) continue; // drive not available
|
||||||
entry->p_name = 4;
|
entry->p_name = 4;
|
||||||
entry->name = entry->path + entry->p_name;
|
entry->name = entry->path + entry->p_name;
|
||||||
memset(entry->path, 0x00, 64);
|
memset(entry->path, 0x00, 256);
|
||||||
snprintf(entry->path, 4, "%s", drvnum[i]);
|
snprintf(entry->path, 4, "%s", drvnum[i]);
|
||||||
if ((*(drvnum[i]) >= '7') && (*(drvnum[i]) <= '9') && !(GetMountState() & IMG_NAND)) // Drive 7...9 handling
|
if ((*(drvnum[i]) >= '7') && (*(drvnum[i]) <= '9') && !(GetMountState() & IMG_NAND)) // Drive 7...9 handling
|
||||||
snprintf(entry->name, 32, "[%s] %s", drvnum[i],
|
snprintf(entry->name, 252, "[%s] %s", drvnum[i],
|
||||||
(*(drvnum[i]) == '7') ? "FAT IMAGE" :
|
(*(drvnum[i]) == '7') ? STR_LAB_FAT_IMAGE :
|
||||||
(*(drvnum[i]) == '8') ? "BONUS DRIVE" :
|
(*(drvnum[i]) == '8') ? STR_LAB_BONUS_DRIVE :
|
||||||
(*(drvnum[i]) == '9') ? "RAMDRIVE" : "UNK");
|
(*(drvnum[i]) == '9') ? STR_LAB_RAMDRIVE : "UNK");
|
||||||
else if (*(drvnum[i]) == 'G') // Game drive special handling
|
else if (*(drvnum[i]) == 'G') // Game drive special handling
|
||||||
snprintf(entry->name, 32, "[%s] %s %s", drvnum[i],
|
snprintf(entry->name, 252, "[%s] %s %s", drvnum[i],
|
||||||
(GetMountState() & GAME_CIA ) ? "CIA" :
|
(GetMountState() & GAME_CIA ) ? "CIA" :
|
||||||
(GetMountState() & GAME_NCSD ) ? "NCSD" :
|
(GetMountState() & GAME_NCSD ) ? "NCSD" :
|
||||||
(GetMountState() & GAME_NCCH ) ? "NCCH" :
|
(GetMountState() & GAME_NCCH ) ? "NCCH" :
|
||||||
@ -114,10 +120,10 @@ bool GetRootDirContentsWorker(DirStruct* contents) {
|
|||||||
(GetMountState() & SYS_FIRM ) ? "FIRM" :
|
(GetMountState() & SYS_FIRM ) ? "FIRM" :
|
||||||
(GetMountState() & GAME_TAD ) ? "DSIWARE" : "UNK", drvname[i]);
|
(GetMountState() & GAME_TAD ) ? "DSIWARE" : "UNK", drvname[i]);
|
||||||
else if (*(drvnum[i]) == 'C') // Game cart handling
|
else if (*(drvnum[i]) == 'C') // Game cart handling
|
||||||
snprintf(entry->name, 32, "[%s] %s (%s)", drvnum[i], drvname[i], carttype);
|
snprintf(entry->name, 252, "[%s] %s (%s)", drvnum[i], drvname[i], carttype);
|
||||||
else if (*(drvnum[i]) == '0') // SD card handling
|
else if (*(drvnum[i]) == '0') // SD card handling
|
||||||
snprintf(entry->name, 32, "[%s] %s (%s)", drvnum[i], drvname[i], sdlabel);
|
snprintf(entry->name, 252, "[%s] %s (%s)", drvnum[i], drvname[i], sdlabel);
|
||||||
else snprintf(entry->name, 32, "[%s] %s", drvnum[i], drvname[i]);
|
else snprintf(entry->name, 252, "[%s] %s", drvnum[i], drvname[i]);
|
||||||
entry->size = GetTotalSpace(entry->path);
|
entry->size = GetTotalSpace(entry->path);
|
||||||
entry->type = T_ROOT;
|
entry->type = T_ROOT;
|
||||||
entry->marked = 0;
|
entry->marked = 0;
|
||||||
@ -206,9 +212,12 @@ void SearchDirContents(DirStruct* contents, const char* path, const char* patter
|
|||||||
|
|
||||||
void GetDirContents(DirStruct* contents, const char* path) {
|
void GetDirContents(DirStruct* contents, const char* path) {
|
||||||
if (*search_path && (DriveType(path) & DRV_SEARCH)) {
|
if (*search_path && (DriveType(path) & DRV_SEARCH)) {
|
||||||
ShowString("Searching, please wait...");
|
ShowString("%s", STR_SEARCHING_PLEASE_WAIT);
|
||||||
SearchDirContents(contents, search_path, search_pattern, true);
|
SearchDirContents(contents, search_path, search_pattern, true);
|
||||||
if (search_title_mode) SetDirGoodNames(contents);
|
ClearScreenF(true, false, COLOR_STD_BG);
|
||||||
|
} else if (title_manager_mode && (DriveType(path) & DRV_TITLEMAN)) {
|
||||||
|
SearchDirContents(contents, "T:", "*", false);
|
||||||
|
SetupTitleManager(contents);
|
||||||
ClearScreenF(true, false, COLOR_STD_BG);
|
ClearScreenF(true, false, COLOR_STD_BG);
|
||||||
} else SearchDirContents(contents, path, NULL, false);
|
} else SearchDirContents(contents, path, NULL, false);
|
||||||
if (*path) SortDirStruct(contents);
|
if (*path) SortDirStruct(contents);
|
||||||
@ -223,7 +232,7 @@ uint64_t GetFreeSpace(const char* path)
|
|||||||
FATFS* fsobj = GetMountedFSObject(path);
|
FATFS* fsobj = GetMountedFSObject(path);
|
||||||
if ((pdrv < 0) || !fsobj) return 0;
|
if ((pdrv < 0) || !fsobj) return 0;
|
||||||
|
|
||||||
snprintf(fsname, 3, "%i:", pdrv);
|
snprintf(fsname, sizeof(fsname), "%i:", pdrv);
|
||||||
if (f_getfree(fsname, &free_clusters, &fsptr) != FR_OK)
|
if (f_getfree(fsname, &free_clusters, &fsptr) != FR_OK)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
@ -25,30 +25,36 @@
|
|||||||
#define DRV_VRAM (1UL<<13)
|
#define DRV_VRAM (1UL<<13)
|
||||||
#define DRV_ALIAS (1UL<<14)
|
#define DRV_ALIAS (1UL<<14)
|
||||||
#define DRV_BONUS (1UL<<15)
|
#define DRV_BONUS (1UL<<15)
|
||||||
#define DRV_SEARCH (1UL<<16)
|
#define DRV_TITLEMAN (1UL<<16)
|
||||||
#define DRV_STDFAT (1UL<<17) // standard FAT drive without limitations
|
#define DRV_SEARCH (1UL<<17)
|
||||||
|
#define DRV_STDFAT (1UL<<18) // standard FAT drive without limitations
|
||||||
|
|
||||||
#define DRV_LABEL_LEN (36)
|
#define DRV_LABEL_LEN (36)
|
||||||
|
|
||||||
#define FS_DRVNAME \
|
#define FS_DRVNAME \
|
||||||
"SDCARD", \
|
STR_LAB_SDCARD, \
|
||||||
"SYSNAND CTRNAND", "SYSNAND TWLN", "SYSNAND TWLP", "SYSNAND SD", "SYSNAND VIRTUAL", \
|
STR_LAB_SYSNAND_CTRNAND, STR_LAB_SYSNAND_TWLN, STR_LAB_SYSNAND_TWLP, STR_LAB_SYSNAND_SD, STR_LAB_SYSNAND_VIRTUAL, \
|
||||||
"EMUNAND CTRNAND", "EMUNAND TWLN", "EMUNAND TWLP", "EMUNAND SD", "EMUNAND VIRTUAL", \
|
STR_LAB_EMUNAND_CTRNAND, STR_LAB_EMUNAND_TWLN, STR_LAB_EMUNAND_TWLP, STR_LAB_EMUNAND_SD, STR_LAB_EMUNAND_VIRTUAL, \
|
||||||
"IMGNAND CTRNAND", "IMGNAND TWLN", "IMGNAND TWLP", "IMGNAND VIRTUAL", \
|
STR_LAB_IMGNAND_CTRNAND, STR_LAB_IMGNAND_TWLN, STR_LAB_IMGNAND_TWLP, STR_LAB_IMGNAND_VIRTUAL, \
|
||||||
"GAMECART", \
|
STR_LAB_GAMECART, \
|
||||||
"GAME IMAGE", "AESKEYDB IMAGE", "BDRI IMAGE", "DISA/DIFF IMAGE", \
|
STR_LAB_GAME_IMAGE, STR_LAB_AESKEYDB_IMAGE, STR_LAB_BDRI_IMAGE, STR_LAB_DISA_DIFF_IMAGE, \
|
||||||
"MEMORY VIRTUAL", \
|
STR_LAB_MEMORY_VIRTUAL, \
|
||||||
"VRAM VIRTUAL", \
|
STR_LAB_VRAM_VIRTUAL, \
|
||||||
"LAST SEARCH" \
|
STR_LAB_TITLE_MANAGER, \
|
||||||
|
STR_LAB_LAST_SEARCH
|
||||||
|
|
||||||
#define FS_DRVNUM \
|
#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:", "Z:"
|
"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 **/
|
/** Function to identify the type of a drive **/
|
||||||
int DriveType(const char* path);
|
int DriveType(const char* path);
|
||||||
|
|
||||||
/** Set search pattern / path / mode for special Z: drive **/
|
/** Set search pattern / path / mode for special Z: drive **/
|
||||||
void SetFSSearch(const char* pattern, const char* path, bool mode);
|
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 **/
|
/** Read the FAT volume label of a partition **/
|
||||||
bool GetFATVolumeLabel(const char* drv, char* label);
|
bool GetFATVolumeLabel(const char* drv, char* label);
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
#include "fsgame.h"
|
#include "fsgame.h"
|
||||||
#include "fsperm.h"
|
#include "fsperm.h"
|
||||||
#include "gameutil.h"
|
#include "gameutil.h"
|
||||||
|
#include "language.h"
|
||||||
|
#include "tie.h"
|
||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
#include "ff.h"
|
#include "vff.h"
|
||||||
|
|
||||||
void SetDirGoodNames(DirStruct* contents) {
|
void SetupTitleManager(DirStruct* contents) {
|
||||||
char goodname[256];
|
char goodname[256];
|
||||||
ShowProgress(0, 0, "");
|
ShowProgress(0, 0, "");
|
||||||
for (u32 s = 0; s < contents->n_entries; s++) {
|
for (u32 s = 0; s < contents->n_entries; s++) {
|
||||||
DirEntry* entry = &(contents->entry[s]);
|
DirEntry* entry = &(contents->entry[s]);
|
||||||
|
// set good name for entry
|
||||||
u32 plen = strnlen(entry->path, 256);
|
u32 plen = strnlen(entry->path, 256);
|
||||||
if (!ShowProgress(s+1, contents->n_entries, entry->path)) break;
|
if (!ShowProgress(s+1, contents->n_entries, entry->path)) break;
|
||||||
if ((GetGoodName(goodname, entry->path, false) != 0) ||
|
if ((GetGoodName(goodname, entry->path, false) != 0) ||
|
||||||
@ -17,6 +20,11 @@ void SetDirGoodNames(DirStruct* contents) {
|
|||||||
entry->p_name = plen + 1;
|
entry->p_name = plen + 1;
|
||||||
entry->name = entry->path + entry->p_name;
|
entry->name = entry->path + entry->p_name;
|
||||||
snprintf(entry->name, 256 - entry->p_name, "%s", goodname);
|
snprintf(entry->name, 256 - entry->p_name, "%s", goodname);
|
||||||
|
// grab title size from tie
|
||||||
|
TitleInfoEntry tie;
|
||||||
|
if (fvx_qread(entry->path, &tie, 0, sizeof(TitleInfoEntry), NULL) != FR_OK)
|
||||||
|
continue;
|
||||||
|
entry->size = tie.title_size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,12 +35,12 @@ bool GoodRenamer(DirEntry* entry, bool ask) {
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (ask) { // ask for confirmatiom
|
if (ask) { // ask for confirmatiom
|
||||||
char oldname_tr[32+1];
|
char oldname_tr[UTF_BUFFER_BYTESIZE(32)];
|
||||||
char newname_ww[256];
|
char newname_ww[256];
|
||||||
TruncateString(oldname_tr, entry->name, 32, 8);
|
TruncateString(oldname_tr, entry->name, 32, 8);
|
||||||
strncpy(newname_ww, goodname, 256);
|
strncpy(newname_ww, goodname, 256);
|
||||||
WordWrapString(newname_ww, 32);
|
WordWrapString(newname_ww, 32);
|
||||||
if (!ShowPrompt(true, "%s\nRename to good name?\n \n%s", oldname_tr, newname_ww))
|
if (!ShowPrompt(true, "%s\n%s\n \n%s", oldname_tr, STR_RENAME_TO_GOOD_NAME, newname_ww))
|
||||||
return true; // call it a success because user choice
|
return true; // call it a success because user choice
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,5 +3,5 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "fsdir.h"
|
#include "fsdir.h"
|
||||||
|
|
||||||
void SetDirGoodNames(DirStruct* contents);
|
void SetupTitleManager(DirStruct* contents);
|
||||||
bool GoodRenamer(DirEntry* entry, bool ask);
|
bool GoodRenamer(DirEntry* entry, bool ask);
|
||||||
|
@ -21,7 +21,7 @@ bool InitExtFS() {
|
|||||||
|
|
||||||
for (u32 i = 1; i < NORM_FS; i++) {
|
for (u32 i = 1; i < NORM_FS; i++) {
|
||||||
char fsname[8];
|
char fsname[8];
|
||||||
snprintf(fsname, 7, "%lu:", i);
|
snprintf(fsname, sizeof(fsname), "%lu:", i);
|
||||||
if (fs_mounted[i]) continue;
|
if (fs_mounted[i]) continue;
|
||||||
fs_mounted[i] = (f_mount(fs + i, fsname, 1) == FR_OK);
|
fs_mounted[i] = (f_mount(fs + i, fsname, 1) == FR_OK);
|
||||||
if ((!fs_mounted[i] || !ramdrv_ready) && (i == NORM_FS - 1) && !(GetMountState() & IMG_NAND)) {
|
if ((!fs_mounted[i] || !ramdrv_ready) && (i == NORM_FS - 1) && !(GetMountState() & IMG_NAND)) {
|
||||||
@ -44,7 +44,7 @@ bool InitImgFS(const char* path) {
|
|||||||
u32 drv_i = NORM_FS - IMGN_FS;
|
u32 drv_i = NORM_FS - IMGN_FS;
|
||||||
char fsname[8];
|
char fsname[8];
|
||||||
for (; drv_i < NORM_FS; drv_i++) {
|
for (; drv_i < NORM_FS; drv_i++) {
|
||||||
snprintf(fsname, 7, "%lu:", drv_i);
|
snprintf(fsname, sizeof(fsname), "%lu:", drv_i);
|
||||||
if (!(DriveType(fsname)&DRV_IMAGE)) break;
|
if (!(DriveType(fsname)&DRV_IMAGE)) break;
|
||||||
}
|
}
|
||||||
// deinit virtual filesystem
|
// deinit virtual filesystem
|
||||||
@ -58,7 +58,7 @@ bool InitImgFS(const char* path) {
|
|||||||
else if ((type&IMG_FAT) && (drv_i < NORM_FS - IMGN_FS + 1)) drv_i = NORM_FS - IMGN_FS + 1;
|
else if ((type&IMG_FAT) && (drv_i < NORM_FS - IMGN_FS + 1)) drv_i = NORM_FS - IMGN_FS + 1;
|
||||||
// reinit image filesystem
|
// reinit image filesystem
|
||||||
for (u32 i = NORM_FS - IMGN_FS; i < drv_i; i++) {
|
for (u32 i = NORM_FS - IMGN_FS; i < drv_i; i++) {
|
||||||
snprintf(fsname, 7, "%lu:", i);
|
snprintf(fsname, sizeof(fsname), "%lu:", i);
|
||||||
fs_mounted[i] = (f_mount(fs + i, fsname, 1) == FR_OK);
|
fs_mounted[i] = (f_mount(fs + i, fsname, 1) == FR_OK);
|
||||||
}
|
}
|
||||||
return GetMountState();
|
return GetMountState();
|
||||||
@ -71,7 +71,7 @@ void DeinitExtFS() {
|
|||||||
for (u32 i = NORM_FS - 1; i > 0; i--) {
|
for (u32 i = NORM_FS - 1; i > 0; i--) {
|
||||||
if (fs_mounted[i]) {
|
if (fs_mounted[i]) {
|
||||||
char fsname[8];
|
char fsname[8];
|
||||||
snprintf(fsname, 7, "%lu:", i);
|
snprintf(fsname, sizeof(fsname), "%lu:", i);
|
||||||
f_mount(NULL, fsname, 1);
|
f_mount(NULL, fsname, 1);
|
||||||
fs_mounted[i] = false;
|
fs_mounted[i] = false;
|
||||||
}
|
}
|
||||||
@ -91,7 +91,7 @@ void DismountDriveType(u32 type) { // careful with this - no safety checks
|
|||||||
}
|
}
|
||||||
for (u32 i = 0; i < NORM_FS; i++) {
|
for (u32 i = 0; i < NORM_FS; i++) {
|
||||||
char fsname[8];
|
char fsname[8];
|
||||||
snprintf(fsname, 7, "%lu:", i);
|
snprintf(fsname, sizeof(fsname), "%lu:", i);
|
||||||
if (!fs_mounted[i] || !(type & DriveType(fsname)))
|
if (!fs_mounted[i] || !(type & DriveType(fsname)))
|
||||||
continue;
|
continue;
|
||||||
f_mount(NULL, fsname, 1);
|
f_mount(NULL, fsname, 1);
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "image.h"
|
#include "image.h"
|
||||||
#include "unittype.h"
|
#include "unittype.h"
|
||||||
#include "essentials.h"
|
#include "essentials.h"
|
||||||
|
#include "language.h"
|
||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
#include "sdmmc.h"
|
#include "sdmmc.h"
|
||||||
|
|
||||||
@ -20,7 +21,7 @@
|
|||||||
static u32 write_permissions = PERM_BASE;
|
static u32 write_permissions = PERM_BASE;
|
||||||
|
|
||||||
bool CheckWritePermissions(const char* path) {
|
bool CheckWritePermissions(const char* path) {
|
||||||
char area_name[16];
|
char area_name[UTF_BUFFER_BYTESIZE(16)];
|
||||||
int drvtype = DriveType(path);
|
int drvtype = DriveType(path);
|
||||||
u32 perm;
|
u32 perm;
|
||||||
|
|
||||||
@ -40,7 +41,7 @@ bool CheckWritePermissions(const char* path) {
|
|||||||
|
|
||||||
// SD card write protection check
|
// SD card write protection check
|
||||||
if ((drvtype & (DRV_SDCARD | DRV_EMUNAND | DRV_ALIAS)) && SD_WRITE_PROTECTED) {
|
if ((drvtype & (DRV_SDCARD | DRV_EMUNAND | DRV_ALIAS)) && SD_WRITE_PROTECTED) {
|
||||||
ShowPrompt(false, "SD card is write protected!\nCan't continue.");
|
ShowPrompt(false, "%s", STR_SD_WRITE_PROTECTED_CANT_CONTINUE);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,7 +64,7 @@ bool CheckWritePermissions(const char* path) {
|
|||||||
if ((drvtype & DRV_CTRNAND) || (lvl == 2)) lvl = 3;
|
if ((drvtype & DRV_CTRNAND) || (lvl == 2)) lvl = 3;
|
||||||
}
|
}
|
||||||
perm = perms[lvl];
|
perm = perms[lvl];
|
||||||
snprintf(area_name, 16, "SysNAND (lvl%lu)", lvl);
|
snprintf(area_name, sizeof(area_name), STR_SYSNAND_LVL_N, lvl);
|
||||||
} else if (drvtype & DRV_EMUNAND) {
|
} else if (drvtype & DRV_EMUNAND) {
|
||||||
static const u32 perms[] = { PERM_EMU_LVL0, PERM_EMU_LVL1 };
|
static const u32 perms[] = { PERM_EMU_LVL0, PERM_EMU_LVL1 };
|
||||||
u32 lvl = (drvtype & (DRV_ALIAS|DRV_CTRNAND)) ? 1 : 0;
|
u32 lvl = (drvtype & (DRV_ALIAS|DRV_CTRNAND)) ? 1 : 0;
|
||||||
@ -73,34 +74,34 @@ bool CheckWritePermissions(const char* path) {
|
|||||||
if (strncasecmp(path_f, path_lvl1[i], 256) == 0) lvl = 1;
|
if (strncasecmp(path_f, path_lvl1[i], 256) == 0) lvl = 1;
|
||||||
}
|
}
|
||||||
perm = perms[lvl];
|
perm = perms[lvl];
|
||||||
snprintf(area_name, 16, "EmuNAND (lvl%lu)", lvl);
|
snprintf(area_name, sizeof(area_name), STR_EMUNAND_LVL_N, lvl);
|
||||||
} else if (drvtype & DRV_GAME) {
|
} else if (drvtype & DRV_GAME) {
|
||||||
perm = PERM_GAME;
|
perm = PERM_GAME;
|
||||||
snprintf(area_name, 16, "game images");
|
snprintf(area_name, sizeof(area_name), "%s", STR_GAME_IMAGES);
|
||||||
} else if (drvtype & DRV_CART) {
|
} else if (drvtype & DRV_CART) {
|
||||||
perm = PERM_CART;
|
perm = PERM_CART;
|
||||||
snprintf(area_name, 16, "gamecart saves");
|
snprintf(area_name, sizeof(area_name), "%s", STR_GAMECART_SAVES);
|
||||||
} else if (drvtype & DRV_VRAM) {
|
} else if (drvtype & DRV_VRAM) {
|
||||||
perm = PERM_VRAM;
|
perm = PERM_VRAM;
|
||||||
snprintf(area_name, 16, "vram0");
|
snprintf(area_name, sizeof(area_name), "vram0");
|
||||||
} else if (drvtype & DRV_XORPAD) {
|
} else if (drvtype & DRV_XORPAD) {
|
||||||
perm = PERM_XORPAD;
|
perm = PERM_XORPAD;
|
||||||
snprintf(area_name, 16, "XORpads");
|
snprintf(area_name, sizeof(area_name), "XORpads");
|
||||||
} else if (drvtype & DRV_IMAGE) {
|
} else if (drvtype & DRV_IMAGE) {
|
||||||
perm = PERM_IMAGE;
|
perm = PERM_IMAGE;
|
||||||
snprintf(area_name, 16, "images");
|
snprintf(area_name, sizeof(area_name), "%s", STR_IMAGES);
|
||||||
} else if (drvtype & DRV_MEMORY) {
|
} else if (drvtype & DRV_MEMORY) {
|
||||||
perm = PERM_MEMORY;
|
perm = PERM_MEMORY;
|
||||||
snprintf(area_name, 16, "memory areas");
|
snprintf(area_name, sizeof(area_name), "%s", STR_MEMORY_AREAS);
|
||||||
} else if (strncasecmp(path_f, "0:/Nintendo 3DS", 15) == 0) { // this check could be better
|
} else if (strncasecmp(path_f, "0:/Nintendo 3DS", 15) == 0) { // this check could be better
|
||||||
perm = PERM_SDDATA;
|
perm = PERM_SDDATA;
|
||||||
snprintf(area_name, 16, "SD system data");
|
snprintf(area_name, sizeof(area_name), "%s", STR_SD_SYSTEM_DATA);
|
||||||
} else if (drvtype & DRV_SDCARD) {
|
} else if (drvtype & DRV_SDCARD) {
|
||||||
perm = PERM_SDCARD;
|
perm = PERM_SDCARD;
|
||||||
snprintf(area_name, 16, "SD card");
|
snprintf(area_name, sizeof(area_name), "%s", STR_SD_CARD);
|
||||||
} else if (drvtype & DRV_RAMDRIVE) {
|
} else if (drvtype & DRV_RAMDRIVE) {
|
||||||
perm = PERM_RAMDRIVE;
|
perm = PERM_RAMDRIVE;
|
||||||
snprintf(area_name, 16, "RAM drive");
|
snprintf(area_name, sizeof(area_name), "%s", STR_RAM_DRIVE);
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -112,14 +113,14 @@ bool CheckWritePermissions(const char* path) {
|
|||||||
// offer unlock if possible
|
// offer unlock if possible
|
||||||
if (!(perm & (PERM_VRAM|PERM_GAME|PERM_XORPAD))) {
|
if (!(perm & (PERM_VRAM|PERM_GAME|PERM_XORPAD))) {
|
||||||
// ask the user
|
// ask the user
|
||||||
if (!ShowPrompt(true, "Writing to %s is locked!\nUnlock it now?", area_name))
|
if (!ShowPrompt(true, STR_WRITING_TO_DRIVE_IS_LOCKED_UNLOCK_NOW, area_name))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return SetWritePermissions(perm, true);
|
return SetWritePermissions(perm, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// unlock not possible
|
// unlock not possible
|
||||||
ShowPrompt(false, "Unlock write permission for\n%s is not allowed.", area_name);
|
ShowPrompt(false, STR_UNLOCK_WRITE_FOR_DRIVE_NOT_ALLOWED, area_name);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,65 +145,65 @@ bool SetWritePermissions(u32 perm, bool add_perm) {
|
|||||||
|
|
||||||
switch (perm) {
|
switch (perm) {
|
||||||
case PERM_BASE:
|
case PERM_BASE:
|
||||||
if (!ShowUnlockSequence(1, "You want to enable base\nwriting permissions."))
|
if (!ShowUnlockSequence(1, "%s", STR_ENABLE_BASE_WRITE))
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
case PERM_SDCARD:
|
case PERM_SDCARD:
|
||||||
if (!ShowUnlockSequence(1, "You want to enable SD card\nwriting permissions."))
|
if (!ShowUnlockSequence(1, "%s", STR_ENABLE_SD_WRITE))
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
case PERM_IMAGE:
|
case PERM_IMAGE:
|
||||||
if (!ShowUnlockSequence(1, "You want to enable image\nwriting permissions."))
|
if (!ShowUnlockSequence(1, "%s", STR_ENABLE_IMAGE_WRITE))
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
case PERM_RAMDRIVE:
|
case PERM_RAMDRIVE:
|
||||||
if (!ShowUnlockSequence(1, "You want to enable RAM drive\nwriting permissions."))
|
if (!ShowUnlockSequence(1, "%s", STR_ENABLE_RAM_DRIVE_WRITE))
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
case PERM_EMU_LVL0:
|
case PERM_EMU_LVL0:
|
||||||
if (!ShowUnlockSequence(1, "You want to enable EmuNAND\nlvl0 writing permissions."))
|
if (!ShowUnlockSequence(1, "%s", STR_ENABLE_EMUNAND_0_WRITE))
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
case PERM_SYS_LVL0:
|
case PERM_SYS_LVL0:
|
||||||
if (!ShowUnlockSequence(1, "You want to enable SysNAND\nlvl0 writing permissions."))
|
if (!ShowUnlockSequence(1, "%s", STR_ENABLE_SYSNAND_0_WRITE))
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
case PERM_EMU_LVL1:
|
case PERM_EMU_LVL1:
|
||||||
if (!ShowUnlockSequence(2, "You want to enable EmuNAND\nlvl1 writing permissions.\n \nThis enables you to modify\nrecoverable system data,\nuser data & savegames."))
|
if (!ShowUnlockSequence(2, "%s", STR_ENABLE_EMUNAND_1_WRITE))
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
case PERM_SYS_LVL1:
|
case PERM_SYS_LVL1:
|
||||||
if (!ShowUnlockSequence(2, "You want to enable SysNAND\nlvl1 writing permissions.\n \nThis enables you to modify\nsystem data, installations,\nuser data & savegames."))
|
if (!ShowUnlockSequence(2, "%s", STR_ENABLE_SYSNAND_1_WRITE))
|
||||||
return false;
|
|
||||||
break;
|
|
||||||
case PERM_SDDATA:
|
|
||||||
if (!ShowUnlockSequence(2, "You want to enable SD data\nwriting permissions.\n \nThis enables you to modify\ninstallations, user data &\nsavegames."))
|
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
case PERM_CART:
|
case PERM_CART:
|
||||||
if (!ShowUnlockSequence(2, "You want to enable gamecart\nsave writing permissions."))
|
if (!ShowUnlockSequence(2, "%s", STR_ENABLE_GAMECART_SAVE_WRITE))
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
#ifndef SAFEMODE
|
#ifndef SAFEMODE
|
||||||
case PERM_SYS_LVL2:
|
case PERM_SYS_LVL2:
|
||||||
if (!ShowUnlockSequence(3, "!Better be careful!\n \nYou want to enable SysNAND\nlvl2 writing permissions.\n \nThis enables you to modify\nirrecoverable system data!"))
|
if (!ShowUnlockSequence(3, "%s", STR_ENABLE_SYSNAND_2_WRITE))
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
case PERM_MEMORY:
|
case PERM_MEMORY:
|
||||||
if (!ShowUnlockSequence(4, "!Better be careful!\n \nYou want to enable memory\nwriting permissions.\n \nWriting to certain areas may\nlead to unexpected results."))
|
if (!ShowUnlockSequence(4, "%s", STR_ENABLE_MEMORY_WRITE))
|
||||||
|
return false;
|
||||||
|
break;
|
||||||
|
case PERM_SDDATA:
|
||||||
|
if (!ShowUnlockSequence(5, "%s", STR_ENABLE_SD_DATA_WRITE))
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
case PERM_SYS_LVL3:
|
case PERM_SYS_LVL3:
|
||||||
if (!ShowUnlockSequence(6, "!THIS IS YOUR ONLY WARNING!\n \nYou want to enable SysNAND\nlvl3 writing permissions.\n \nThis enables you to OVERWRITE\nyour bootloader installation,\nessential system files and/or\nBRICK your console!"))
|
if (!ShowUnlockSequence(6, "%s", STR_ENABLE_SYSNAND_3_WRITE))
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
ShowPrompt(false, "Unlock write permission is not allowed.");
|
ShowPrompt(false, "%s", STR_UNLOCK_WRITE_NOT_ALLOWED);
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
#else
|
#else
|
||||||
default:
|
default:
|
||||||
ShowPrompt(false, "Can't unlock write permission.\nTry GodMode9 instead!");
|
ShowPrompt(false, "%s", STR_CANT_UNLOCK_WRITE_TRY_GODMODE9);
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
#define PERM_BASE (PERM_SDCARD | PERM_IMAGE | PERM_RAMDRIVE | PERM_EMU_LVL0 | PERM_SYS_LVL0)
|
#define PERM_BASE (PERM_SDCARD | PERM_IMAGE | PERM_RAMDRIVE | PERM_EMU_LVL0 | PERM_SYS_LVL0)
|
||||||
|
|
||||||
// permission levels / colors
|
// permission levels / colors
|
||||||
#define PERM_BLUE (GetWritePermissions()&PERM_MEMORY)
|
#define PERM_BLUE (GetWritePermissions()&(PERM_MEMORY|(PERM_SDDATA&~PERM_SDCARD)))
|
||||||
#define PERM_RED (GetWritePermissions()&(PERM_SYS_LVL3&~PERM_SYS_LVL2))
|
#define PERM_RED (GetWritePermissions()&(PERM_SYS_LVL3&~PERM_SYS_LVL2))
|
||||||
#define PERM_ORANGE (GetWritePermissions()&(PERM_SYS_LVL2&~PERM_SYS_LVL1))
|
#define PERM_ORANGE (GetWritePermissions()&(PERM_SYS_LVL2&~PERM_SYS_LVL1))
|
||||||
#define PERM_YELLOW (GetWritePermissions()&((PERM_SYS_LVL1&~PERM_SYS_LVL0)|(PERM_EMU_LVL1&~PERM_EMU_LVL0)|(PERM_SDDATA&~PERM_SDCARD)|PERM_CART))
|
#define PERM_YELLOW (GetWritePermissions()&((PERM_SYS_LVL1&~PERM_SYS_LVL0)|(PERM_EMU_LVL1&~PERM_EMU_LVL0)|(PERM_SDDATA&~PERM_SDCARD)|PERM_CART))
|
||||||
|
@ -11,9 +11,10 @@
|
|||||||
#include "ff.h"
|
#include "ff.h"
|
||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
#include "swkbd.h"
|
#include "swkbd.h"
|
||||||
|
#include "language.h"
|
||||||
|
|
||||||
#define SKIP_CUR (1UL<<10)
|
#define SKIP_CUR (1UL<<11)
|
||||||
#define OVERWRITE_CUR (1UL<<11)
|
#define OVERWRITE_CUR (1UL<<12)
|
||||||
|
|
||||||
#define _MAX_FS_OPT 8 // max file selector options
|
#define _MAX_FS_OPT 8 // max file selector options
|
||||||
|
|
||||||
@ -46,13 +47,13 @@ bool FormatSDCard(u64 hidden_mb, u32 cluster_size, const char* label) {
|
|||||||
|
|
||||||
// FAT size check
|
// FAT size check
|
||||||
if (fat_size < 0x80000) { // minimum free space: 256MB
|
if (fat_size < 0x80000) { // minimum free space: 256MB
|
||||||
ShowPrompt(false, "Error: SD card is too small");
|
ShowPrompt(false, "%s", STR_ERROR_SD_TOO_SMALL);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write protection check
|
// Write protection check
|
||||||
if (SD_WRITE_PROTECTED) {
|
if (SD_WRITE_PROTECTED) {
|
||||||
ShowPrompt(false, "SD card is write protected!\nCan't continue.");
|
ShowPrompt(false, "%s", STR_SD_WRITE_PROTECTED_CANT_CONTINUE);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,15 +68,15 @@ bool FormatSDCard(u64 hidden_mb, u32 cluster_size, const char* label) {
|
|||||||
|
|
||||||
// one last warning....
|
// one last warning....
|
||||||
// 0:/Nintendo 3DS/ write permission is ignored here, this warning is enough
|
// 0:/Nintendo 3DS/ write permission is ignored here, this warning is enough
|
||||||
if (!ShowUnlockSequence(5, "!WARNING!\n \nProceeding will format this SD.\nThis will irreversibly delete\nALL data on it."))
|
if (!ShowUnlockSequence(5, "%s", STR_WARNING_PROCEEDING_WILL_FORMAT_SD_DELETE_ALL_DATA))
|
||||||
return false;
|
return false;
|
||||||
ShowString("Formatting SD, please wait...");
|
ShowString("%s", STR_FORMATTING_SD_PLEASE_WAIT);
|
||||||
|
|
||||||
// write the MBR to disk
|
// write the MBR to disk
|
||||||
// !this assumes a fully deinitialized file system!
|
// !this assumes a fully deinitialized file system!
|
||||||
if ((sdmmc_sdcard_init() != 0) || (sdmmc_sdcard_writesectors(0, 1, mbr) != 0) ||
|
if ((sdmmc_sdcard_init() != 0) || (sdmmc_sdcard_writesectors(0, 1, mbr) != 0) ||
|
||||||
(emu_size && ((sdmmc_nand_readsectors(0, 1, ncsd) != 0) || (sdmmc_sdcard_writesectors(1, 1, ncsd) != 0)))) {
|
(emu_size && ((sdmmc_nand_readsectors(0, 1, ncsd) != 0) || (sdmmc_sdcard_writesectors(1, 1, ncsd) != 0)))) {
|
||||||
ShowPrompt(false, "Error: SD card i/o failure");
|
ShowPrompt(false, "%s", STR_ERROR_SD_CARD_IO_FAILURE);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,9 +105,9 @@ bool FormatSDCard(u64 hidden_mb, u32 cluster_size, const char* label) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool SetupBonusDrive(void) {
|
bool SetupBonusDrive(void) {
|
||||||
if (!ShowUnlockSequence(3, "Format the bonus drive?\nThis will irreversibly delete\nALL data on it."))
|
if (!ShowUnlockSequence(3, "%s", STR_FORMAT_BONUS_DRIVE_DELETE_ALL_DATA))
|
||||||
return false;
|
return false;
|
||||||
ShowString("Formatting drive, please wait...");
|
ShowString("%s", STR_FORMATTING_DRIVE_PLEASE_WAIT);
|
||||||
if (GetMountState() & IMG_NAND) InitImgFS(NULL);
|
if (GetMountState() & IMG_NAND) InitImgFS(NULL);
|
||||||
|
|
||||||
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
|
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
|
||||||
@ -127,10 +128,10 @@ bool FileUnlock(const char* path) {
|
|||||||
|
|
||||||
if (!(DriveType(path) & DRV_FAT)) return true; // can't really check this
|
if (!(DriveType(path) & DRV_FAT)) return true; // can't really check this
|
||||||
if ((res = fx_open(&file, path, FA_READ | FA_OPEN_EXISTING)) != FR_OK) {
|
if ((res = fx_open(&file, path, FA_READ | FA_OPEN_EXISTING)) != FR_OK) {
|
||||||
char pathstr[32 + 1];
|
char pathstr[UTF_BUFFER_BYTESIZE(32)];
|
||||||
TruncateString(pathstr, path, 32, 8);
|
TruncateString(pathstr, path, 32, 8);
|
||||||
if (GetMountState() && (res == FR_LOCKED) &&
|
if (GetMountState() && (res == FR_LOCKED) &&
|
||||||
(ShowPrompt(true, "%s\nFile is currently mounted.\nUnmount to unlock?", pathstr))) {
|
(ShowPrompt(true, "%s\n%s", pathstr, STR_FILE_IS_MOUNTED_UNMOUNT_TO_UNLOCK))) {
|
||||||
InitImgFS(NULL);
|
InitImgFS(NULL);
|
||||||
if (fx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
|
if (fx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
|
||||||
return false;
|
return false;
|
||||||
@ -161,7 +162,7 @@ size_t FileGetSize(const char* path) {
|
|||||||
return fno.fsize;
|
return fno.fsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileGetSha256(const char* path, u8* sha256, u64 offset, u64 size) {
|
bool FileGetSha(const char* path, u8* hash, u64 offset, u64 size, bool sha1) {
|
||||||
bool ret = true;
|
bool ret = true;
|
||||||
FIL file;
|
FIL file;
|
||||||
u64 fsize;
|
u64 fsize;
|
||||||
@ -179,7 +180,7 @@ bool FileGetSha256(const char* path, u8* sha256, u64 offset, u64 size) {
|
|||||||
if (!buffer) return false;
|
if (!buffer) return false;
|
||||||
|
|
||||||
ShowProgress(0, 0, path);
|
ShowProgress(0, 0, path);
|
||||||
sha_init(SHA256_MODE);
|
sha_init(sha1 ? SHA1_MODE : SHA256_MODE);
|
||||||
for (u64 pos = 0; (pos < size) && ret; pos += bufsiz) {
|
for (u64 pos = 0; (pos < size) && ret; pos += bufsiz) {
|
||||||
UINT read_bytes = min(bufsiz, size - pos);
|
UINT read_bytes = min(bufsiz, size - pos);
|
||||||
UINT bytes_read = 0;
|
UINT bytes_read = 0;
|
||||||
@ -190,7 +191,7 @@ bool FileGetSha256(const char* path, u8* sha256, u64 offset, u64 size) {
|
|||||||
sha_update(buffer, bytes_read);
|
sha_update(buffer, bytes_read);
|
||||||
}
|
}
|
||||||
|
|
||||||
sha_get(sha256);
|
sha_get(hash);
|
||||||
fvx_close(&file);
|
fvx_close(&file);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
|
|
||||||
@ -250,7 +251,7 @@ bool FileInjectFile(const char* dest, const char* orig, u64 off_dest, u64 off_or
|
|||||||
|
|
||||||
if (!CheckWritePermissions(dest)) return false;
|
if (!CheckWritePermissions(dest)) return false;
|
||||||
if (strncasecmp(dest, orig, 256) == 0) {
|
if (strncasecmp(dest, orig, 256) == 0) {
|
||||||
ShowPrompt(false, "Error: Can't inject file into itself");
|
ShowPrompt(false, "%s", STR_ERROR_CANT_INJECT_FILE_INTO_ITSELF);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -269,12 +270,12 @@ bool FileInjectFile(const char* dest, const char* orig, u64 off_dest, u64 off_or
|
|||||||
|
|
||||||
// check file limits
|
// check file limits
|
||||||
if (!allow_expand && (off_dest + size > fvx_size(&dfile))) {
|
if (!allow_expand && (off_dest + size > fvx_size(&dfile))) {
|
||||||
ShowPrompt(false, "Operation would write beyond end of file");
|
ShowPrompt(false, "%s", STR_OPERATION_WOULD_WRITE_BEYOND_EOF);
|
||||||
fvx_close(&dfile);
|
fvx_close(&dfile);
|
||||||
fvx_close(&ofile);
|
fvx_close(&ofile);
|
||||||
return false;
|
return false;
|
||||||
} else if (off_orig + size > fvx_size(&ofile)) {
|
} else if (off_orig + size > fvx_size(&ofile)) {
|
||||||
ShowPrompt(false, "Not enough data in file");
|
ShowPrompt(false, "%s", STR_NOT_ENOUGH_DATA_IN_FILE);
|
||||||
fvx_close(&dfile);
|
fvx_close(&dfile);
|
||||||
fvx_close(&ofile);
|
fvx_close(&ofile);
|
||||||
return false;
|
return false;
|
||||||
@ -295,8 +296,8 @@ bool FileInjectFile(const char* dest, const char* orig, u64 off_dest, u64 off_or
|
|||||||
ret = false;
|
ret = false;
|
||||||
if (ret && !ShowProgress(pos + bytes_read, size, orig)) {
|
if (ret && !ShowProgress(pos + bytes_read, size, orig)) {
|
||||||
if (flags && (*flags & NO_CANCEL)) {
|
if (flags && (*flags & NO_CANCEL)) {
|
||||||
ShowPrompt(false, "Cancel is not allowed here");
|
ShowPrompt(false, "%s", STR_CANCEL_IS_NOT_ALLOWED_HERE);
|
||||||
} else ret = !ShowPrompt(true, "B button detected. Cancel?");
|
} else ret = !ShowPrompt(true, "%s", STR_B_DETECTED_CANCEL);
|
||||||
ShowProgress(0, 0, orig);
|
ShowProgress(0, 0, orig);
|
||||||
ShowProgress(pos + bytes_read, size, orig);
|
ShowProgress(pos + bytes_read, size, orig);
|
||||||
}
|
}
|
||||||
@ -325,7 +326,7 @@ bool FileSetByte(const char* dest, u64 offset, u64 size, u8 fillbyte, u32* flags
|
|||||||
|
|
||||||
// check file limits
|
// check file limits
|
||||||
if (!allow_expand && (offset + size > fvx_size(&dfile))) {
|
if (!allow_expand && (offset + size > fvx_size(&dfile))) {
|
||||||
ShowPrompt(false, "Operation would write beyond end of file");
|
ShowPrompt(false, "%s", STR_OPERATION_WOULD_WRITE_BEYOND_EOF);
|
||||||
fvx_close(&dfile);
|
fvx_close(&dfile);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -345,8 +346,8 @@ bool FileSetByte(const char* dest, u64 offset, u64 size, u8 fillbyte, u32* flags
|
|||||||
ret = false;
|
ret = false;
|
||||||
if (ret && !ShowProgress(pos + bytes_written, size, dest)) {
|
if (ret && !ShowProgress(pos + bytes_written, size, dest)) {
|
||||||
if (flags && (*flags & NO_CANCEL)) {
|
if (flags && (*flags & NO_CANCEL)) {
|
||||||
ShowPrompt(false, "Cancel is not allowed here");
|
ShowPrompt(false, "%s", STR_CANCEL_IS_NOT_ALLOWED_HERE);
|
||||||
} else ret = !ShowPrompt(true, "B button detected. Cancel?");
|
} else ret = !ShowPrompt(true, "%s", STR_B_DETECTED_CANCEL);
|
||||||
ShowProgress(0, 0, dest);
|
ShowProgress(0, 0, dest);
|
||||||
ShowProgress(pos + bytes_written, size, dest);
|
ShowProgress(pos + bytes_written, size, dest);
|
||||||
}
|
}
|
||||||
@ -362,8 +363,8 @@ bool FileSetByte(const char* dest, u64 offset, u64 size, u8 fillbyte, u32* flags
|
|||||||
bool FileCreateDummy(const char* cpath, const char* filename, u64 size) {
|
bool FileCreateDummy(const char* cpath, const char* filename, u64 size) {
|
||||||
char npath[256]; // 256 is the maximum length of a full path
|
char npath[256]; // 256 is the maximum length of a full path
|
||||||
if (!CheckWritePermissions(cpath)) return false;
|
if (!CheckWritePermissions(cpath)) return false;
|
||||||
if (filename) snprintf(npath, 255, "%s/%s", cpath, filename);
|
if (filename) snprintf(npath, sizeof(npath), "%s/%s", cpath, filename);
|
||||||
else snprintf(npath, 255, "%s", cpath);
|
else snprintf(npath, sizeof(npath), "%s", cpath);
|
||||||
|
|
||||||
// create dummy file (fail if already existing)
|
// create dummy file (fail if already existing)
|
||||||
// then, expand the file size via cluster preallocation
|
// then, expand the file size via cluster preallocation
|
||||||
@ -380,7 +381,7 @@ bool FileCreateDummy(const char* cpath, const char* filename, u64 size) {
|
|||||||
bool DirCreate(const char* cpath, const char* dirname) {
|
bool DirCreate(const char* cpath, const char* dirname) {
|
||||||
char npath[256]; // 256 is the maximum length of a full path
|
char npath[256]; // 256 is the maximum length of a full path
|
||||||
if (!CheckWritePermissions(cpath)) return false;
|
if (!CheckWritePermissions(cpath)) return false;
|
||||||
snprintf(npath, 255, "%s/%s", cpath, dirname);
|
snprintf(npath, sizeof(npath), "%s/%s", cpath, dirname);
|
||||||
if (fa_mkdir(npath) != FR_OK) return false;
|
if (fa_mkdir(npath) != FR_OK) return false;
|
||||||
return (fa_stat(npath, NULL) == FR_OK);
|
return (fa_stat(npath, NULL) == FR_OK);
|
||||||
}
|
}
|
||||||
@ -448,6 +449,7 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
|
|||||||
bool silent = (flags && (*flags & SILENT));
|
bool silent = (flags && (*flags & SILENT));
|
||||||
bool append = (flags && (*flags & APPEND_ALL));
|
bool append = (flags && (*flags & APPEND_ALL));
|
||||||
bool calcsha = (flags && (*flags & CALC_SHA) && !append);
|
bool calcsha = (flags && (*flags & CALC_SHA) && !append);
|
||||||
|
bool sha1 = (flags && (*flags & USE_SHA1));
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
|
||||||
// check destination write permission (special paths only)
|
// check destination write permission (special paths only)
|
||||||
@ -460,12 +462,12 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
|
|||||||
if (move && (to_virtual || fno.fattrib & AM_VRT)) return false; // trying to move a virtual file
|
if (move && (to_virtual || fno.fattrib & AM_VRT)) return false; // trying to move a virtual file
|
||||||
|
|
||||||
// path string (for output)
|
// path string (for output)
|
||||||
char deststr[36 + 1];
|
char deststr[UTF_BUFFER_BYTESIZE(36)];
|
||||||
TruncateString(deststr, dest, 36, 8);
|
TruncateString(deststr, dest, 36, 8);
|
||||||
|
|
||||||
// the copy process takes place here
|
// the copy process takes place here
|
||||||
if (!ShowProgress(0, 0, orig) && !(flags && (*flags & NO_CANCEL))) {
|
if (!ShowProgress(0, 0, orig) && !(flags && (*flags & NO_CANCEL))) {
|
||||||
if (ShowPrompt(true, "%s\nB button detected. Cancel?", deststr)) return false;
|
if (ShowPrompt(true, "%s\n%s", deststr, STR_B_DETECTED_CANCEL)) return false;
|
||||||
ShowProgress(0, 0, orig);
|
ShowProgress(0, 0, orig);
|
||||||
}
|
}
|
||||||
if (move && fvx_stat(dest, NULL) != FR_OK) { // moving if dest not existing
|
if (move && fvx_stat(dest, NULL) != FR_OK) { // moving if dest not existing
|
||||||
@ -475,14 +477,14 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
|
|||||||
char* fname = orig + strnlen(orig, 256);
|
char* fname = orig + strnlen(orig, 256);
|
||||||
|
|
||||||
if (append) {
|
if (append) {
|
||||||
if (!silent) ShowPrompt(false, "%s\nError: Cannot append a folder", deststr);
|
if (!silent) ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_CANNOT_APPEND_FOLDER);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create the destination folder if it does not already exist
|
// create the destination folder if it does not already exist
|
||||||
if (fvx_opendir(&pdir, dest) != FR_OK) {
|
if (fvx_opendir(&pdir, dest) != FR_OK) {
|
||||||
if (fvx_mkdir(dest) != FR_OK) {
|
if (fvx_mkdir(dest) != FR_OK) {
|
||||||
if (!silent) ShowPrompt(false, "%s\nError: Overwriting file with dir", deststr);
|
if (!silent) ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_OVERWRITING_FILE_WITH_DIR);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else fvx_closedir(&pdir);
|
} else fvx_closedir(&pdir);
|
||||||
@ -515,7 +517,7 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
|
|||||||
} else if (move) { // moving if destination exists
|
} else if (move) { // moving if destination exists
|
||||||
if (fvx_stat(dest, &fno) != FR_OK) return false;
|
if (fvx_stat(dest, &fno) != FR_OK) return false;
|
||||||
if (fno.fattrib & AM_DIR) {
|
if (fno.fattrib & AM_DIR) {
|
||||||
if (!silent) ShowPrompt(false, "%s\nError: Overwriting dir with file", deststr);
|
if (!silent) ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_OVERWRITING_DIR_WITH_FILE);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (fvx_unlink(dest) != FR_OK) return false;
|
if (fvx_unlink(dest) != FR_OK) return false;
|
||||||
@ -534,7 +536,7 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
|
|||||||
|
|
||||||
if ((!append || (fvx_open(&dfile, dest, FA_WRITE | FA_OPEN_EXISTING) != FR_OK)) &&
|
if ((!append || (fvx_open(&dfile, dest, FA_WRITE | FA_OPEN_EXISTING) != FR_OK)) &&
|
||||||
(fvx_open(&dfile, dest, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK)) {
|
(fvx_open(&dfile, dest, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK)) {
|
||||||
if (!silent) ShowPrompt(false, "%s\nError: Cannot open destination file", deststr);
|
if (!silent) ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_CANNOT_OPEN_DESTINATION_FILE);
|
||||||
fvx_close(&ofile);
|
fvx_close(&ofile);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -543,7 +545,7 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
|
|||||||
osize = fvx_size(&ofile);
|
osize = fvx_size(&ofile);
|
||||||
dsize = append ? fvx_size(&dfile) : 0; // always 0 if not appending to file
|
dsize = append ? fvx_size(&dfile) : 0; // always 0 if not appending to file
|
||||||
if ((fvx_lseek(&dfile, (osize + dsize)) != FR_OK) || (fvx_sync(&dfile) != FR_OK) || (fvx_tell(&dfile) != (osize + dsize))) { // check space via cluster preallocation
|
if ((fvx_lseek(&dfile, (osize + dsize)) != FR_OK) || (fvx_sync(&dfile) != FR_OK) || (fvx_tell(&dfile) != (osize + dsize))) { // check space via cluster preallocation
|
||||||
if (!silent) ShowPrompt(false, "%s\nError: Not enough space available", deststr);
|
if (!silent) ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_NOT_ENOUGH_SPACE_AVAILABLE);
|
||||||
ret = false;
|
ret = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -552,7 +554,7 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
|
|||||||
fvx_lseek(&ofile, 0);
|
fvx_lseek(&ofile, 0);
|
||||||
fvx_sync(&ofile);
|
fvx_sync(&ofile);
|
||||||
|
|
||||||
if (calcsha) sha_init(SHA256_MODE);
|
if (calcsha) sha_init(sha1 ? SHA1_MODE : SHA256_MODE);
|
||||||
for (u64 pos = 0; (pos < osize) && ret; pos += bufsiz) {
|
for (u64 pos = 0; (pos < osize) && ret; pos += bufsiz) {
|
||||||
UINT bytes_read = 0;
|
UINT bytes_read = 0;
|
||||||
UINT bytes_written = 0;
|
UINT bytes_written = 0;
|
||||||
@ -565,8 +567,8 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
|
|||||||
u64 total = osize;
|
u64 total = osize;
|
||||||
if (ret && !ShowProgress(current, total, orig)) {
|
if (ret && !ShowProgress(current, total, orig)) {
|
||||||
if (flags && (*flags & NO_CANCEL)) {
|
if (flags && (*flags & NO_CANCEL)) {
|
||||||
ShowPrompt(false, "%s\nCancel is not allowed here", deststr);
|
ShowPrompt(false, "%s\n%s", deststr, STR_CANCEL_IS_NOT_ALLOWED_HERE);
|
||||||
} else ret = !ShowPrompt(true, "%s\nB button detected. Cancel?", deststr);
|
} else ret = !ShowPrompt(true, "%s\n%s", deststr, STR_B_DETECTED_CANCEL);
|
||||||
ShowProgress(0, 0, orig);
|
ShowProgress(0, 0, orig);
|
||||||
ShowProgress(current, total, orig);
|
ShowProgress(current, total, orig);
|
||||||
}
|
}
|
||||||
@ -580,11 +582,11 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
|
|||||||
if (!ret && ((dsize == 0) || (fvx_lseek(&dfile, dsize) != FR_OK) || (f_truncate(&dfile) != FR_OK))) {
|
if (!ret && ((dsize == 0) || (fvx_lseek(&dfile, dsize) != FR_OK) || (f_truncate(&dfile) != FR_OK))) {
|
||||||
fvx_unlink(dest);
|
fvx_unlink(dest);
|
||||||
} else if (!to_virtual && calcsha) {
|
} else if (!to_virtual && calcsha) {
|
||||||
u8 sha256[0x20];
|
u8 hash[0x20];
|
||||||
char* ext_sha = dest + strnlen(dest, 256);
|
char* ext_sha = dest + strnlen(dest, 256);
|
||||||
strncpy(ext_sha, ".sha", 256 - (ext_sha - dest));
|
snprintf(ext_sha, 256 - (ext_sha - dest), ".sha%c", sha1 ? '1' : '\0');
|
||||||
sha_get(sha256);
|
sha_get(hash);
|
||||||
FileSetData(dest, sha256, 0x20, 0, true);
|
FileSetData(dest, hash, sha1 ? 20 : 32, 0, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -606,21 +608,21 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
|
|||||||
int odrvtype = DriveType(orig);
|
int odrvtype = DriveType(orig);
|
||||||
char ldest[256]; // 256 is the maximum length of a full path
|
char ldest[256]; // 256 is the maximum length of a full path
|
||||||
char lorig[256];
|
char lorig[256];
|
||||||
strncpy(ldest, dest, 255);
|
strncpy(ldest, dest, 256);
|
||||||
strncpy(lorig, orig, 255);
|
strncpy(lorig, orig, 256);
|
||||||
char deststr[36 + 1];
|
char deststr[UTF_BUFFER_BYTESIZE(36)];
|
||||||
TruncateString(deststr, ldest, 36, 8);
|
TruncateString(deststr, ldest, 36, 8);
|
||||||
|
|
||||||
// moving only for regular FAT drives (= not alias drives)
|
// moving only for regular FAT drives (= not alias drives)
|
||||||
if (move && !(ddrvtype & odrvtype & DRV_STDFAT)) {
|
if (move && !(ddrvtype & odrvtype & DRV_STDFAT)) {
|
||||||
ShowPrompt(false, "Error: Only FAT files can be moved");
|
ShowPrompt(false, "%s", STR_ERROR_ONLY_FAT_FILES_CAN_BE_MOVED);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// is destination part of origin?
|
// is destination part of origin?
|
||||||
u32 olen = strnlen(lorig, 255);
|
u32 olen = strnlen(lorig, 255);
|
||||||
if ((strncasecmp(ldest, lorig, olen) == 0) && (ldest[olen] == '/')) {
|
if ((strncasecmp(ldest, lorig, olen) == 0) && (ldest[olen] == '/')) {
|
||||||
ShowPrompt(false, "%s\nError: Destination is part of origin", deststr);
|
ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_DESTINATION_IS_PART_OF_ORIGIN);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -632,7 +634,7 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
|
|||||||
|
|
||||||
// check & fix destination == origin
|
// check & fix destination == origin
|
||||||
while (strncasecmp(ldest, lorig, 255) == 0) {
|
while (strncasecmp(ldest, lorig, 255) == 0) {
|
||||||
if (!ShowKeyboardOrPrompt(dname, 255 - (dname - ldest), "%s\nDestination equals origin\nChoose another name?", deststr))
|
if (!ShowKeyboardOrPrompt(dname, 255 - (dname - ldest), "%s\n%s", deststr, STR_ERROR_DESTINATION_EQUALS_ORIGIN_CHOOSE_ANOTHER_NAME))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -643,12 +645,11 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
const char* optionstr[5] =
|
const char* optionstr[5] =
|
||||||
{"Choose new name", "Overwrite file(s)", "Skip file(s)", "Overwrite all", "Skip all"};
|
{STR_CHOOSE_NEW_NAME, STR_OVERWRITE_FILES, STR_SKIP_FILES, STR_OVERWRITE_ALL, STR_SKIP_ALL};
|
||||||
u32 user_select = ShowSelectPrompt((*flags & ASK_ALL) ? 5 : 3, optionstr,
|
u32 user_select = ShowSelectPrompt((*flags & ASK_ALL) ? 5 : 3, optionstr, STR_DESTINATION_ALREADY_EXISTS, deststr);
|
||||||
"Destination already exists:\n%s", deststr);
|
|
||||||
if (user_select == 1) {
|
if (user_select == 1) {
|
||||||
do {
|
do {
|
||||||
if (!ShowKeyboardOrPrompt(dname, 255 - (dname - ldest), "Choose new destination name"))
|
if (!ShowKeyboardOrPrompt(dname, 255 - (dname - ldest), "%s", STR_CHOOSE_NEW_DESTINATION_NAME))
|
||||||
return false;
|
return false;
|
||||||
} while (fa_stat(ldest, NULL) == FR_OK);
|
} while (fa_stat(ldest, NULL) == FR_OK);
|
||||||
} else if (user_select == 2) {
|
} else if (user_select == 2) {
|
||||||
@ -672,7 +673,7 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
|
|||||||
// setup buffer
|
// setup buffer
|
||||||
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
|
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
|
||||||
if (!buffer) {
|
if (!buffer) {
|
||||||
ShowPrompt(false, "Out of memory.");
|
ShowPrompt(false, "%s", STR_OUT_OF_MEMORY);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -698,20 +699,20 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
|
|||||||
|
|
||||||
// prevent illegal operations
|
// prevent illegal operations
|
||||||
if (force_unmount && (odrvtype & ddrvtype & (DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE))) {
|
if (force_unmount && (odrvtype & ddrvtype & (DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE))) {
|
||||||
ShowPrompt(false, "Copy operation is not allowed");
|
ShowPrompt(false, "%s", STR_COPY_OPERATION_IS_NOT_ALLOWED);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check destination == origin
|
// check destination == origin
|
||||||
if (strncasecmp(ldest, lorig, 255) == 0) {
|
if (strncasecmp(ldest, lorig, 255) == 0) {
|
||||||
ShowPrompt(false, "%s\nDestination equals origin", deststr);
|
ShowPrompt(false, "%s\n%s", deststr, STR_DESTINATION_EQUALS_ORIGIN);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup buffer
|
// setup buffer
|
||||||
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
|
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
|
||||||
if (!buffer) {
|
if (!buffer) {
|
||||||
ShowPrompt(false, "Out of memory.");
|
ShowPrompt(false, "%s", STR_OUT_OF_MEMORY);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -730,7 +731,7 @@ bool PathCopy(const char* destdir, const char* orig, u32* flags) {
|
|||||||
char dest[256]; // maximum path name length in FAT
|
char dest[256]; // maximum path name length in FAT
|
||||||
char* oname = strrchr(orig, '/');
|
char* oname = strrchr(orig, '/');
|
||||||
if (oname == NULL) return false; // not a proper origin path
|
if (oname == NULL) return false; // not a proper origin path
|
||||||
snprintf(dest, 255, "%s/%s", destdir, (++oname));
|
snprintf(dest, sizeof(dest), "%s/%s", destdir, (++oname));
|
||||||
|
|
||||||
// virtual destination special handling
|
// virtual destination special handling
|
||||||
if (GetVirtualSource(destdir) & ~VRT_BDRI) {
|
if (GetVirtualSource(destdir) & ~VRT_BDRI) {
|
||||||
@ -744,12 +745,12 @@ bool PathCopy(const char* destdir, const char* orig, u32* flags) {
|
|||||||
if (!ReadVirtualDir(&dvfile, &vdir)) return false;
|
if (!ReadVirtualDir(&dvfile, &vdir)) return false;
|
||||||
if (dvfile.size == osize) break; // file found
|
if (dvfile.size == osize) break; // file found
|
||||||
}
|
}
|
||||||
if (!ShowPrompt(true, "Entry not found: %s\nInject into %s instead?", dest, dvfile.name))
|
if (!ShowPrompt(true, STR_ENTRY_NOT_FOUND_PATH_INJECT_INTO_PATH_INSTEAD, dest, dvfile.name))
|
||||||
return false;
|
return false;
|
||||||
snprintf(dest, 255, "%s/%s", destdir, dvfile.name);
|
snprintf(dest, sizeof(dest), "%s/%s", destdir, dvfile.name);
|
||||||
} else if (osize < dvfile.size) { // if origin is smaller than destination...
|
} else if (osize < dvfile.size) { // if origin is smaller than destination...
|
||||||
char deststr[36 + 1];
|
char deststr[UTF_BUFFER_BYTESIZE(36)];
|
||||||
char origstr[36 + 1];
|
char origstr[UTF_BUFFER_BYTESIZE(36)];
|
||||||
char osizestr[32];
|
char osizestr[32];
|
||||||
char dsizestr[32];
|
char dsizestr[32];
|
||||||
TruncateString(deststr, dest, 36, 8);
|
TruncateString(deststr, dest, 36, 8);
|
||||||
@ -757,7 +758,7 @@ bool PathCopy(const char* destdir, const char* orig, u32* flags) {
|
|||||||
FormatBytes(osizestr, osize);
|
FormatBytes(osizestr, osize);
|
||||||
FormatBytes(dsizestr, dvfile.size);
|
FormatBytes(dsizestr, dvfile.size);
|
||||||
if (dvfile.size > osize) {
|
if (dvfile.size > osize) {
|
||||||
if (!ShowPrompt(true, "File smaller than available space:\n%s (%s)\n%s (%s)\nContinue?", origstr, osizestr, deststr, dsizestr))
|
if (!ShowPrompt(true, STR_FILE_SMALLER_THAN_SPACE_SIZES_CONTINUE, origstr, osizestr, deststr, dsizestr))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -771,7 +772,7 @@ bool PathMove(const char* destdir, const char* orig, u32* flags) {
|
|||||||
char dest[256]; // maximum path name length in FAT
|
char dest[256]; // maximum path name length in FAT
|
||||||
char* oname = strrchr(orig, '/');
|
char* oname = strrchr(orig, '/');
|
||||||
if (oname == NULL) return false; // not a proper origin path
|
if (oname == NULL) return false; // not a proper origin path
|
||||||
snprintf(dest, 255, "%s/%s", destdir, (++oname));
|
snprintf(dest, sizeof(dest), "%s/%s", destdir, (++oname));
|
||||||
|
|
||||||
return PathMoveCopy(dest, orig, flags, true);
|
return PathMoveCopy(dest, orig, flags, true);
|
||||||
}
|
}
|
||||||
@ -821,7 +822,7 @@ bool FileSelectorWorker(char* result, const char* text, const char* path, const
|
|||||||
GetDirContents(contents, path_local);
|
GetDirContents(contents, path_local);
|
||||||
|
|
||||||
while (pos < contents->n_entries) {
|
while (pos < contents->n_entries) {
|
||||||
char opt_names[_MAX_FS_OPT+1][32+1];
|
char opt_names[_MAX_FS_OPT+1][UTF_BUFFER_BYTESIZE(32)];
|
||||||
DirEntry* res_entry[MAX_DIR_ENTRIES+1] = { NULL };
|
DirEntry* res_entry[MAX_DIR_ENTRIES+1] = { NULL };
|
||||||
u32 n_opt = 0;
|
u32 n_opt = 0;
|
||||||
for (; pos < contents->n_entries; pos++) {
|
for (; pos < contents->n_entries; pos++) {
|
||||||
@ -831,13 +832,13 @@ bool FileSelectorWorker(char* result, const char* text, const char* path, const
|
|||||||
(entry->type == T_DOTDOT) || (strncmp(entry->name, "._", 2) == 0))
|
(entry->type == T_DOTDOT) || (strncmp(entry->name, "._", 2) == 0))
|
||||||
continue;
|
continue;
|
||||||
if (!new_style && n_opt == _MAX_FS_OPT) {
|
if (!new_style && n_opt == _MAX_FS_OPT) {
|
||||||
snprintf(opt_names[n_opt++], 32, "[more...]");
|
snprintf(opt_names[n_opt++], 32, "%s", STR_BRACKET_MORE);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!new_style) {
|
if (!new_style) {
|
||||||
char temp_str[256];
|
char temp_str[256];
|
||||||
snprintf(temp_str, 256, "%s", entry->name);
|
snprintf(temp_str, sizeof(temp_str), "%s", entry->name);
|
||||||
if (hide_ext && (entry->type == T_FILE)) {
|
if (hide_ext && (entry->type == T_FILE)) {
|
||||||
char* dot = strrchr(temp_str, '.');
|
char* dot = strrchr(temp_str, '.');
|
||||||
if (dot) *dot = '\0';
|
if (dot) *dot = '\0';
|
||||||
@ -848,7 +849,7 @@ bool FileSelectorWorker(char* result, const char* text, const char* path, const
|
|||||||
n_found++;
|
n_found++;
|
||||||
}
|
}
|
||||||
if ((pos >= contents->n_entries) && (n_opt < n_found) && !new_style)
|
if ((pos >= contents->n_entries) && (n_opt < n_found) && !new_style)
|
||||||
snprintf(opt_names[n_opt++], 32, "[more...]");
|
snprintf(opt_names[n_opt++], 32, "%s", STR_BRACKET_MORE);
|
||||||
if (!n_opt) break;
|
if (!n_opt) break;
|
||||||
|
|
||||||
const char* optionstr[_MAX_FS_OPT+1] = { NULL };
|
const char* optionstr[_MAX_FS_OPT+1] = { NULL };
|
||||||
@ -871,9 +872,9 @@ bool FileSelectorWorker(char* result, const char* text, const char* path, const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!n_found) { // not a single matching entry found
|
if (!n_found) { // not a single matching entry found
|
||||||
char pathstr[32+1];
|
char pathstr[UTF_BUFFER_BYTESIZE(32)];
|
||||||
TruncateString(pathstr, path_local, 32, 8);
|
TruncateString(pathstr, path_local, 32, 8);
|
||||||
ShowPrompt(false, "%s\nNo usable entries found.", pathstr);
|
ShowPrompt(false, "%s\n%s", pathstr, STR_NO_USABLE_ENTRIES_FOUND);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -883,6 +884,7 @@ bool FileSelector(char* result, const char* text, const char* path, const char*
|
|||||||
void* buffer = (void*) malloc(sizeof(DirStruct));
|
void* buffer = (void*) malloc(sizeof(DirStruct));
|
||||||
if (!buffer) return false;
|
if (!buffer) return false;
|
||||||
|
|
||||||
|
// for this to work, result needs to be at least 256 bytes in size
|
||||||
bool ret = FileSelectorWorker(result, text, path, pattern, flags, buffer, new_style);
|
bool ret = FileSelectorWorker(result, text, path, pattern, flags, buffer, new_style);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -7,12 +7,13 @@
|
|||||||
#define NO_CANCEL (1UL<<1)
|
#define NO_CANCEL (1UL<<1)
|
||||||
#define SILENT (1UL<<2)
|
#define SILENT (1UL<<2)
|
||||||
#define CALC_SHA (1UL<<3)
|
#define CALC_SHA (1UL<<3)
|
||||||
#define BUILD_PATH (1UL<<4)
|
#define USE_SHA1 (1UL<<4)
|
||||||
#define ALLOW_EXPAND (1UL<<5)
|
#define BUILD_PATH (1UL<<5)
|
||||||
#define ASK_ALL (1UL<<6)
|
#define ALLOW_EXPAND (1UL<<6)
|
||||||
#define SKIP_ALL (1UL<<7)
|
#define ASK_ALL (1UL<<7)
|
||||||
#define OVERWRITE_ALL (1UL<<8)
|
#define SKIP_ALL (1UL<<8)
|
||||||
#define APPEND_ALL (1UL<<9)
|
#define OVERWRITE_ALL (1UL<<9)
|
||||||
|
#define APPEND_ALL (1UL<<10)
|
||||||
|
|
||||||
// file selector flags
|
// file selector flags
|
||||||
#define NO_DIRS (1UL<<0)
|
#define NO_DIRS (1UL<<0)
|
||||||
@ -43,7 +44,7 @@ size_t FileGetData(const char* path, void* data, size_t size, size_t foffset);
|
|||||||
size_t FileGetSize(const char* path);
|
size_t FileGetSize(const char* path);
|
||||||
|
|
||||||
/** Get SHA-256 of file **/
|
/** Get SHA-256 of file **/
|
||||||
bool FileGetSha256(const char* path, u8* sha256, u64 offset, u64 size);
|
bool FileGetSha(const char* path, u8* hash, u64 offset, u64 size, bool sha1);
|
||||||
|
|
||||||
/** Find data in file **/
|
/** Find data in file **/
|
||||||
u32 FileFindData(const char* path, u8* data, u32 size_data, u32 offset_file);
|
u32 FileFindData(const char* path, u8* data, u32 size_data, u32 offset_file);
|
||||||
|
@ -74,6 +74,6 @@ u64 MountImage(const char* path) {
|
|||||||
return 0;
|
return 0;
|
||||||
fvx_lseek(&mount_file, 0);
|
fvx_lseek(&mount_file, 0);
|
||||||
fvx_sync(&mount_file);
|
fvx_sync(&mount_file);
|
||||||
strncpy(mount_path, path, 255);
|
strncpy(mount_path, path, 256);
|
||||||
return (mount_state = type);
|
return (mount_state = type);
|
||||||
}
|
}
|
||||||
|
@ -35,9 +35,9 @@ int alias_num (const TCHAR* path) {
|
|||||||
void dealias_path (TCHAR* alias, const TCHAR* path) {
|
void dealias_path (TCHAR* alias, const TCHAR* path) {
|
||||||
int num = alias_num(path);
|
int num = alias_num(path);
|
||||||
u32 p_offs = (path[2] == '/' && ((path[3] == '/') || (path[3] == '\0'))) ? 3 : 2;
|
u32 p_offs = (path[2] == '/' && ((path[3] == '/') || (path[3] == '\0'))) ? 3 : 2;
|
||||||
if (num >= 0) // set alias (alias is assumed to be 256 byte)
|
if (num >= 0) // set alias (alias is assumed to be 256 byte!)
|
||||||
snprintf(alias, 256, "%s%s", alias_path[num], path + p_offs);
|
snprintf(alias, 256, "%s%s", alias_path[num], path + p_offs);
|
||||||
else strncpy(alias, path, 256);
|
else snprintf(alias, 256, "%s", path);
|
||||||
}
|
}
|
||||||
|
|
||||||
FilCryptInfo* fx_find_cryptinfo(FIL* fptr) {
|
FilCryptInfo* fx_find_cryptinfo(FIL* fptr) {
|
||||||
@ -276,7 +276,7 @@ bool SetupNandSdDrive(const char* path, const char* sd_path, const char* movable
|
|||||||
// build the alias path (id0)
|
// build the alias path (id0)
|
||||||
u32 sha256sum[8];
|
u32 sha256sum[8];
|
||||||
sha_quick(sha256sum, sd_keyy[num], 0x10, SHA256_MODE);
|
sha_quick(sha256sum, sd_keyy[num], 0x10, SHA256_MODE);
|
||||||
snprintf(alias, 127, "%s/Nintendo 3DS/%08lX%08lX%08lX%08lX",
|
snprintf(alias, sizeof(alias), "%s/Nintendo 3DS/%08lX%08lX%08lX%08lX",
|
||||||
sd_path, sha256sum[0], sha256sum[1], sha256sum[2], sha256sum[3]);
|
sd_path, sha256sum[0], sha256sum[1], sha256sum[2], sha256sum[3]);
|
||||||
|
|
||||||
// find the alias path (id1)
|
// find the alias path (id1)
|
||||||
|
@ -17,7 +17,7 @@ bool CheckSupportFile(const char* fname)
|
|||||||
const char* base_paths[] = { SUPPORT_FILE_PATHS };
|
const char* base_paths[] = { SUPPORT_FILE_PATHS };
|
||||||
for (u32 i = 0; i < countof(base_paths); i++) {
|
for (u32 i = 0; i < countof(base_paths); i++) {
|
||||||
char path[256];
|
char path[256];
|
||||||
snprintf(path, 256, "%s/%s", base_paths[i], fname);
|
snprintf(path, sizeof(path), "%s/%s", base_paths[i], fname);
|
||||||
if (fvx_stat(path, NULL) == FR_OK)
|
if (fvx_stat(path, NULL) == FR_OK)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -40,7 +40,7 @@ size_t LoadSupportFile(const char* fname, void* buffer, size_t max_len)
|
|||||||
for (u32 i = 0; i < countof(base_paths); i++) {
|
for (u32 i = 0; i < countof(base_paths); i++) {
|
||||||
UINT len32;
|
UINT len32;
|
||||||
char path[256];
|
char path[256];
|
||||||
snprintf(path, 256, "%s/%s", base_paths[i], fname);
|
snprintf(path, sizeof(path), "%s/%s", base_paths[i], fname);
|
||||||
if (fvx_qread(path, buffer, 0, max_len, &len32) == FR_OK)
|
if (fvx_qread(path, buffer, 0, max_len, &len32) == FR_OK)
|
||||||
return len32;
|
return len32;
|
||||||
}
|
}
|
||||||
@ -68,7 +68,7 @@ bool SaveSupportFile(const char* fname, void* buffer, size_t len)
|
|||||||
// write support file
|
// write support file
|
||||||
if (idx >= 0) {
|
if (idx >= 0) {
|
||||||
char path[256];
|
char path[256];
|
||||||
snprintf(path, 256, "%s/%s", base_paths[idx], fname);
|
snprintf(path, sizeof(path), "%s/%s", base_paths[idx], fname);
|
||||||
fvx_unlink(path);
|
fvx_unlink(path);
|
||||||
if (fvx_qwrite(path, buffer, 0, len, NULL) == FR_OK)
|
if (fvx_qwrite(path, buffer, 0, len, NULL) == FR_OK)
|
||||||
return true;
|
return true;
|
||||||
@ -115,6 +115,7 @@ bool CheckSupportDir(const char* dname)
|
|||||||
bool FileSelectorSupport(char* result, const char* text, const char* dname, const char* pattern)
|
bool FileSelectorSupport(char* result, const char* text, const char* dname, const char* pattern)
|
||||||
{
|
{
|
||||||
char path[256];
|
char path[256];
|
||||||
|
// result needs to be at least 256 bytes long for this to work!
|
||||||
if (!GetSupportDir(path, dname)) return false;
|
if (!GetSupportDir(path, dname)) return false;
|
||||||
return FileSelector(result, text, path, pattern, HIDE_EXT, false);
|
return FileSelector(result, text, path, pattern, HIDE_EXT, false);
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,9 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
// scripts / payloads dir names
|
// scripts / payloads dir names
|
||||||
|
#define LANGUAGES_DIR "languages"
|
||||||
#define SCRIPTS_DIR "scripts"
|
#define SCRIPTS_DIR "scripts"
|
||||||
|
#define LUASCRIPTS_DIR "luascripts"
|
||||||
#define PAYLOADS_DIR "payloads"
|
#define PAYLOADS_DIR "payloads"
|
||||||
|
|
||||||
bool CheckSupportFile(const char* fname);
|
bool CheckSupportFile(const char* fname);
|
||||||
@ -11,5 +13,6 @@ size_t LoadSupportFile(const char* fname, void* buffer, size_t max_len);
|
|||||||
bool SaveSupportFile(const char* fname, void* buffer, size_t len);
|
bool SaveSupportFile(const char* fname, void* buffer, size_t len);
|
||||||
bool SetAsSupportFile(const char* fname, const char* source);
|
bool SetAsSupportFile(const char* fname, const char* source);
|
||||||
|
|
||||||
|
bool GetSupportDir(char* path, const char* dname);
|
||||||
bool CheckSupportDir(const char* fpath);
|
bool CheckSupportDir(const char* fpath);
|
||||||
bool FileSelectorSupport(char* result, const char* text, const char* dname, const char* pattern);
|
bool FileSelectorSupport(char* result, const char* text, const char* dname, const char* pattern);
|
||||||
|
@ -257,7 +257,8 @@ FRESULT worker_fvx_rmkdir (TCHAR* tpath) {
|
|||||||
FRESULT fvx_rmkdir (const TCHAR* path) {
|
FRESULT fvx_rmkdir (const TCHAR* path) {
|
||||||
#if !_LFN_UNICODE // this will not work for unicode
|
#if !_LFN_UNICODE // this will not work for unicode
|
||||||
TCHAR tpath[_MAX_FN_LEN+1];
|
TCHAR tpath[_MAX_FN_LEN+1];
|
||||||
strncpy(tpath, path, _MAX_FN_LEN);
|
if (strlen(path) > _MAX_FN_LEN) return FR_INVALID_NAME;
|
||||||
|
strcpy(tpath, path);
|
||||||
return worker_fvx_rmkdir( tpath );
|
return worker_fvx_rmkdir( tpath );
|
||||||
#else
|
#else
|
||||||
return FR_DENIED;
|
return FR_DENIED;
|
||||||
@ -267,7 +268,8 @@ FRESULT fvx_rmkdir (const TCHAR* path) {
|
|||||||
FRESULT fvx_rmkpath (const TCHAR* path) {
|
FRESULT fvx_rmkpath (const TCHAR* path) {
|
||||||
#if !_LFN_UNICODE // this will not work for unicode
|
#if !_LFN_UNICODE // this will not work for unicode
|
||||||
TCHAR tpath[_MAX_FN_LEN+1];
|
TCHAR tpath[_MAX_FN_LEN+1];
|
||||||
strncpy(tpath, path, _MAX_FN_LEN);
|
if (strlen(path) > _MAX_FN_LEN) return FR_INVALID_NAME;
|
||||||
|
strcpy(tpath, path);
|
||||||
TCHAR* slash = strrchr(tpath, '/');
|
TCHAR* slash = strrchr(tpath, '/');
|
||||||
if (!slash) return FR_DENIED;
|
if (!slash) return FR_DENIED;
|
||||||
*slash = '\0';
|
*slash = '\0';
|
||||||
@ -295,7 +297,7 @@ FRESULT worker_fvx_runlink (TCHAR* tpath) {
|
|||||||
while (fvx_readdir(&pdir, &fno) == FR_OK) {
|
while (fvx_readdir(&pdir, &fno) == FR_OK) {
|
||||||
if ((strncmp(fno.fname, ".", 2) == 0) || (strncmp(fno.fname, "..", 3) == 0))
|
if ((strncmp(fno.fname, ".", 2) == 0) || (strncmp(fno.fname, "..", 3) == 0))
|
||||||
continue; // filter out virtual entries
|
continue; // filter out virtual entries
|
||||||
strncpy(fname, fno.fname, tpath + 255 - fname);
|
strcpy(fname, fno.fname);
|
||||||
if (fno.fname[0] == 0) {
|
if (fno.fname[0] == 0) {
|
||||||
break;
|
break;
|
||||||
} else { // return value won't matter
|
} else { // return value won't matter
|
||||||
@ -313,7 +315,8 @@ FRESULT worker_fvx_runlink (TCHAR* tpath) {
|
|||||||
FRESULT fvx_runlink (const TCHAR* path) {
|
FRESULT fvx_runlink (const TCHAR* path) {
|
||||||
#if !_LFN_UNICODE // this will not work for unicode
|
#if !_LFN_UNICODE // this will not work for unicode
|
||||||
TCHAR tpath[_MAX_FN_LEN+1];
|
TCHAR tpath[_MAX_FN_LEN+1];
|
||||||
strncpy(tpath, path, _MAX_FN_LEN);
|
if (strlen(path) > _MAX_FN_LEN) return FR_INVALID_NAME;
|
||||||
|
strcpy(tpath, path);
|
||||||
return worker_fvx_runlink( tpath );
|
return worker_fvx_runlink( tpath );
|
||||||
#else
|
#else
|
||||||
return FR_DENIED;
|
return FR_DENIED;
|
||||||
@ -356,7 +359,8 @@ FRESULT fvx_preaddir (DIR* dp, FILINFO* fno, const TCHAR* pattern) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
FRESULT fvx_findpath (TCHAR* path, const TCHAR* pattern, BYTE mode) {
|
FRESULT fvx_findpath (TCHAR* path, const TCHAR* pattern, BYTE mode) {
|
||||||
strncpy(path, pattern, _MAX_FN_LEN);
|
if (strlen(pattern) > _MAX_FN_LEN) return FR_INVALID_NAME;
|
||||||
|
strcpy(path, pattern);
|
||||||
TCHAR* fname = strrchr(path, '/');
|
TCHAR* fname = strrchr(path, '/');
|
||||||
if (!fname) return FR_DENIED;
|
if (!fname) return FR_DENIED;
|
||||||
*fname = '\0';
|
*fname = '\0';
|
||||||
@ -376,7 +380,7 @@ FRESULT fvx_findpath (TCHAR* path, const TCHAR* pattern, BYTE mode) {
|
|||||||
while ((fvx_preaddir(&pdir, &fno, npattern) == FR_OK) && *(fno.fname)) {
|
while ((fvx_preaddir(&pdir, &fno, npattern) == FR_OK) && *(fno.fname)) {
|
||||||
int cmp = strncmp(fno.fname, fname, _MAX_FN_LEN);
|
int cmp = strncmp(fno.fname, fname, _MAX_FN_LEN);
|
||||||
if (((mode & FN_HIGHEST) && (cmp > 0)) || ((mode & FN_LOWEST) && (cmp < 0)) || !(*fname))
|
if (((mode & FN_HIGHEST) && (cmp > 0)) || ((mode & FN_LOWEST) && (cmp < 0)) || !(*fname))
|
||||||
strncpy(fname, fno.fname, _MAX_FN_LEN - (fname - path));
|
strcpy(fname, fno.fname);
|
||||||
if (!(mode & (FN_HIGHEST|FN_LOWEST))) break;
|
if (!(mode & (FN_HIGHEST|FN_LOWEST))) break;
|
||||||
}
|
}
|
||||||
fvx_closedir( &pdir );
|
fvx_closedir( &pdir );
|
||||||
@ -385,7 +389,8 @@ FRESULT fvx_findpath (TCHAR* path, const TCHAR* pattern, BYTE mode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
FRESULT fvx_findnopath (TCHAR* path, const TCHAR* pattern) {
|
FRESULT fvx_findnopath (TCHAR* path, const TCHAR* pattern) {
|
||||||
strncpy(path, pattern, _MAX_FN_LEN);
|
if (strlen(pattern) > _MAX_FN_LEN) return FR_INVALID_NAME;
|
||||||
|
strcpy(path, pattern);
|
||||||
TCHAR* fname = strrchr(path, '/');
|
TCHAR* fname = strrchr(path, '/');
|
||||||
if (!fname) return FR_DENIED;
|
if (!fname) return FR_DENIED;
|
||||||
fname++;
|
fname++;
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#define fvx_tell(fp) ((fp)->fptr)
|
#define fvx_tell(fp) ((fp)->fptr)
|
||||||
#define fvx_size(fp) ((fp)->obj.objsize)
|
#define fvx_size(fp) ((fp)->obj.objsize)
|
||||||
|
#define fvx_eof(fp) (fvx_tell(fp) == fvx_size(fp))
|
||||||
|
|
||||||
#define FN_ANY 0x00
|
#define FN_ANY 0x00
|
||||||
#define FN_HIGHEST 0x01
|
#define FN_HIGHEST 0x01
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include "vff.h"
|
#include "vff.h"
|
||||||
|
|
||||||
#define FAT_ENTRY_SIZE 2 * sizeof(u32)
|
#define FAT_ENTRY_SIZE 2 * sizeof(u32)
|
||||||
|
#define REPLACE_SIZE_MISMATCH 2
|
||||||
|
|
||||||
#define getfatflag(uv) (((uv) & 0x80000000UL) != 0)
|
#define getfatflag(uv) (((uv) & 0x80000000UL) != 0)
|
||||||
#define getfatindex(uv) ((uv) & 0x7FFFFFFFUL)
|
#define getfatindex(uv) ((uv) & 0x7FFFFFFFUL)
|
||||||
@ -369,7 +370,8 @@ static u32 AddBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offse
|
|||||||
|
|
||||||
// If an entry for the tid already existed that is already the specified size and replace was specified, just replace the existing entry
|
// 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 (memcmp(title_id_be, file_entry.title_id, 8) == 0) {
|
||||||
if (!replace || (file_entry.size != size)) return 1;
|
if (!replace) return 1;
|
||||||
|
else if (file_entry.size != size) return REPLACE_SIZE_MISMATCH;
|
||||||
else {
|
else {
|
||||||
do_replace = true;
|
do_replace = true;
|
||||||
break;
|
break;
|
||||||
@ -864,10 +866,15 @@ u32 AddTicketToDB(const char* path, const u8* title_id, const Ticket* ticket, bo
|
|||||||
|
|
||||||
bdrifp = &file;
|
bdrifp = &file;
|
||||||
|
|
||||||
|
u32 add_bdri_res = 0;
|
||||||
|
|
||||||
if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) ||
|
if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) ||
|
||||||
!CheckDBMagic((u8*) &pre_header, true) ||
|
!CheckDBMagic((u8*) &pre_header, true) ||
|
||||||
(AddBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id,
|
((add_bdri_res = AddBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id,
|
||||||
(const u8*) te, entry_size, replace) != 0)) {
|
(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);
|
free(te);
|
||||||
fvx_close(bdrifp);
|
fvx_close(bdrifp);
|
||||||
bdrifp = NULL;
|
bdrifp = NULL;
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
|
|
||||||
|
#include "language.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
#include "crc32.h"
|
#include "crc32.h"
|
||||||
@ -143,18 +144,18 @@ static bool BEAT_UpdateProgress(const BEAT_Context *ctx)
|
|||||||
static const char *BEAT_ErrString(int error)
|
static const char *BEAT_ErrString(int error)
|
||||||
{ // Get an error description string
|
{ // Get an error description string
|
||||||
switch(error) {
|
switch(error) {
|
||||||
case BEAT_OK: return "No error";
|
case BEAT_OK: return STR_BEAT_NO_ERROR;
|
||||||
case BEAT_EOAL: return "End of action list";
|
case BEAT_EOAL: return STR_BEAT_END_OF_ACTION_LIST;
|
||||||
case BEAT_ABORTED: return "Aborted by user";
|
case BEAT_ABORTED: return STR_BEAT_ABORTED_BY_USER;
|
||||||
case BEAT_IO_ERROR: return "Failed to read/write file";
|
case BEAT_IO_ERROR: return STR_BEAT_FAILED_TO_READ_WRITE_FILE;
|
||||||
case BEAT_OVERFLOW: return "Attempted to write beyond end of file";
|
case BEAT_OVERFLOW: return STR_BEAT_ATTEMPTED_TO_WRITE_BEYOND_EOF;
|
||||||
case BEAT_BADPATCH: return "Invalid patch file";
|
case BEAT_BADPATCH: return STR_BEAT_INVALID_PATCH_FILE;
|
||||||
case BEAT_BADINPUT: return "Invalid input file";
|
case BEAT_BADINPUT: return STR_BEAT_INVALID_INPUT_FILE;
|
||||||
case BEAT_BADOUTPUT: return "Output file checksum mismatch";
|
case BEAT_BADOUTPUT: return STR_BEAT_OUTPUT_FILE_CHECKSUM_MISMATCH;
|
||||||
case BEAT_BADCHKSUM: return "File checksum failed";
|
case BEAT_BADCHKSUM: return STR_BEAT_FILE_CHECKSUM_FAILED;
|
||||||
case BEAT_PATCH_EXPECT: return "Expected more patch data";
|
case BEAT_PATCH_EXPECT: return STR_BEAT_EXPECTED_MORE_PATCH_DATA;
|
||||||
case BEAT_OUT_OF_MEMORY: return "Out of memory";
|
case BEAT_OUT_OF_MEMORY: return STR_BEAT_OUT_OF_MEMORY;
|
||||||
default: return "Unknown error";
|
default: return STR_BEAT_UNKNOWN_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -221,7 +222,7 @@ static s32 BEAT_DecodeSigned(u32 val) // Extract the signed number
|
|||||||
static int BEAT_RunActions(BEAT_Context *ctx, const BEAT_Action *acts)
|
static int BEAT_RunActions(BEAT_Context *ctx, const BEAT_Action *acts)
|
||||||
{ // Parses an action list and runs commands specified in `acts`
|
{ // Parses an action list and runs commands specified in `acts`
|
||||||
u32 vli, len;
|
u32 vli, len;
|
||||||
int cmd, res;
|
int cmd, res = BEAT_OK;
|
||||||
|
|
||||||
while((res == BEAT_OK) &&
|
while((res == BEAT_OK) &&
|
||||||
(ctx->foff[BEAT_PF] < (BEAT_RANGE(ctx, BEAT_PF) - ctx->eoal_offset))) {
|
(ctx->foff[BEAT_PF] < (BEAT_RANGE(ctx, BEAT_PF) - ctx->eoal_offset))) {
|
||||||
@ -660,19 +661,18 @@ static int BEAT_Run(const char *p, const char *s, const char *d, bool bpm)
|
|||||||
progress_timer = timer_start();
|
progress_timer = timer_start();
|
||||||
res = (bpm ? BPM_InitCTX : BPS_InitCTX)(&ctx, p, s, d);
|
res = (bpm ? BPM_InitCTX : BPS_InitCTX)(&ctx, p, s, d);
|
||||||
if (res != BEAT_OK) {
|
if (res != BEAT_OK) {
|
||||||
ShowPrompt(false, "Failed to initialize %s file:\n%s",
|
ShowPrompt(false, bpm ? STR_FAILED_TO_INITIALIZE_BPM_FILE : STR_FAILED_TO_INITIALIZE_BPS_FILE, BEAT_ErrString(res));
|
||||||
bpm ? "BPM" : "BPS", BEAT_ErrString(res));
|
|
||||||
} else {
|
} else {
|
||||||
res = (bpm ? BPM_RunActions : BPS_RunActions)(&ctx);
|
res = (bpm ? BPM_RunActions : BPS_RunActions)(&ctx);
|
||||||
switch(res) {
|
switch(res) {
|
||||||
case BEAT_OK:
|
case BEAT_OK:
|
||||||
ShowPrompt(false, "Patch successfully applied");
|
ShowPrompt(false, "%s", STR_PATCH_SUCCESSFULLY_APPLIED);
|
||||||
break;
|
break;
|
||||||
case BEAT_ABORTED:
|
case BEAT_ABORTED:
|
||||||
ShowPrompt(false, "Patching aborted by user");
|
ShowPrompt(false, "%s", STR_PATCHING_ABORTED_BY_USER);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
ShowPrompt(false, "Failed to run patch:\n%s", BEAT_ErrString(res));
|
ShowPrompt(false, STR_FAILED_TO_RUN_PATCH, BEAT_ErrString(res));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,816 @@
|
|||||||
#include "cert.h"
|
#include "cert.h"
|
||||||
#include "ff.h"
|
#include "disadiff.h"
|
||||||
|
#include "rsa.h"
|
||||||
|
|
||||||
u32 LoadCertFromCertDb(u64 offset, Certificate* cert, u32* mod, u32* exp) {
|
typedef struct {
|
||||||
Certificate cert_local;
|
char magic[4]; // "CERT"
|
||||||
FIL db;
|
u8 unk[4]; // afaik, always 0
|
||||||
UINT bytes_read;
|
u8 used_size[4]; // size used after this header
|
||||||
|
u8 garbage[4]; // literally garbage values
|
||||||
|
} PACKED_STRUCT CertsDbPartitionHeader;
|
||||||
|
|
||||||
// not much in terms of error checking here
|
static inline void GetCertDBPath(char* path, bool emunand) {
|
||||||
if (f_open(&db, "1:/dbs/certs.db", FA_READ | FA_OPEN_EXISTING) != FR_OK)
|
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 1;
|
||||||
f_lseek(&db, offset);
|
|
||||||
if (!cert) cert = &cert_local;
|
|
||||||
f_read(&db, cert, CERT_SIZE, &bytes_read);
|
|
||||||
f_close(&db);
|
|
||||||
|
|
||||||
if (mod) memcpy(mod, cert->mod, 0x100);
|
|
||||||
if (exp) *exp = getle32(cert->exp);
|
|
||||||
|
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
@ -2,21 +2,54 @@
|
|||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
#define CERT_SIZE sizeof(Certificate)
|
#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
|
// from: http://3dbrew.org/wiki/Certificates
|
||||||
// all numbers in big endian
|
// all numbers in big endian
|
||||||
typedef struct {
|
typedef struct {
|
||||||
u8 sig_type[4]; // expected: 0x010004 / RSA_2048 SHA256
|
u8 sig_type[4];
|
||||||
u8 signature[0x100];
|
u8 signature[];
|
||||||
u8 padding0[0x3C];
|
} PACKED_ALIGN(1) CertificateSignature;
|
||||||
u8 issuer[0x40];
|
|
||||||
u8 keytype[4]; // expected: 0x01 / RSA_2048
|
|
||||||
u8 name[0x40];
|
|
||||||
u8 unknown[4];
|
|
||||||
u8 mod[0x100];
|
|
||||||
u8 exp[0x04];
|
|
||||||
u8 padding1[0x34];
|
|
||||||
} PACKED_STRUCT Certificate;
|
|
||||||
|
|
||||||
u32 LoadCertFromCertDb(u64 offset, Certificate* cert, u32* mod, u32* exp);
|
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);
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "ff.h"
|
#include "ff.h"
|
||||||
#include "aes.h"
|
#include "aes.h"
|
||||||
#include "sha.h"
|
#include "sha.h"
|
||||||
|
#include "cert.h"
|
||||||
|
|
||||||
u32 ValidateCiaHeader(CiaHeader* header) {
|
u32 ValidateCiaHeader(CiaHeader* header) {
|
||||||
if ((header->size_header != CIA_HEADER_SIZE) ||
|
if ((header->size_header != CIA_HEADER_SIZE) ||
|
||||||
@ -59,21 +60,14 @@ u32 BuildCiaCert(u8* ciacert) {
|
|||||||
0x18, 0x83, 0xAF, 0xE0, 0xF4, 0xE5, 0x62, 0xBA, 0x69, 0xEE, 0x72, 0x2A, 0xC2, 0x4E, 0x95, 0xB3
|
0x18, 0x83, 0xAF, 0xE0, 0xF4, 0xE5, 0x62, 0xBA, 0x69, 0xEE, 0x72, 0x2A, 0xC2, 0x4E, 0x95, 0xB3
|
||||||
};
|
};
|
||||||
|
|
||||||
// open certs.db file on SysNAND
|
static const char* const retail_issuers[] = {"Root-CA00000003", "Root-CA00000003-XS0000000c", "Root-CA00000003-CP0000000b"};
|
||||||
FIL db;
|
static const char* const dev_issuers[] = {"Root-CA00000004", "Root-CA00000004-XS00000009", "Root-CA00000004-CP0000000a"};
|
||||||
UINT bytes_read;
|
|
||||||
if (f_open(&db, "1:/dbs/certs.db", FA_READ | FA_OPEN_EXISTING) != FR_OK)
|
size_t size = CIA_CERT_SIZE;
|
||||||
|
if (BuildRawCertBundleFromCertDb(ciacert, &size, !IS_DEVKIT ? retail_issuers : dev_issuers, 3) ||
|
||||||
|
size != CIA_CERT_SIZE) {
|
||||||
return 1;
|
return 1;
|
||||||
// grab CIA cert from 4 offsets
|
}
|
||||||
f_lseek(&db, 0x0C10);
|
|
||||||
f_read(&db, ciacert + 0x000, 0x1F0, &bytes_read);
|
|
||||||
f_lseek(&db, 0x3A00);
|
|
||||||
f_read(&db, ciacert + 0x1F0, 0x210, &bytes_read);
|
|
||||||
f_lseek(&db, 0x3F10);
|
|
||||||
f_read(&db, ciacert + 0x400, 0x300, &bytes_read);
|
|
||||||
f_lseek(&db, 0x3C10);
|
|
||||||
f_read(&db, ciacert + 0x700, 0x300, &bytes_read);
|
|
||||||
f_close(&db);
|
|
||||||
|
|
||||||
// check the certificate hash
|
// check the certificate hash
|
||||||
u8 cert_hash[0x20];
|
u8 cert_hash[0x20];
|
||||||
|
23
arm9/source/game/cifinish.h
Normal file
23
arm9/source/game/cifinish.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#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,4 +1,5 @@
|
|||||||
#include "codelzss.h"
|
#include "codelzss.h"
|
||||||
|
#include "language.h"
|
||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
|
|
||||||
#define CODE_COMP_SIZE(f) ((f)->off_size_comp & 0xFFFFFF)
|
#define CODE_COMP_SIZE(f) ((f)->off_size_comp & 0xFFFFFF)
|
||||||
@ -45,10 +46,10 @@ u32 DecompressCodeLzss(u8* code, u32* code_size, u32 max_size) {
|
|||||||
|
|
||||||
// main decompression loop
|
// main decompression loop
|
||||||
while ((ptr_in > comp_start) && (ptr_out > comp_start)) {
|
while ((ptr_in > comp_start) && (ptr_out > comp_start)) {
|
||||||
if (!ShowProgress(data_end - ptr_out, data_end - data_start, "Decompressing .code...")) {
|
if (!ShowProgress(data_end - ptr_out, data_end - data_start, STR_DECOMPRESSING_DOT_CODE)) {
|
||||||
if (ShowPrompt(true, "Decompressing .code...\nB button detected. Cancel?")) return 1;
|
if (ShowPrompt(true, "%s", STR_DECOMPRESSING_DOT_CODE_B_DETECTED_CANCEL)) return 1;
|
||||||
ShowProgress(0, data_end - data_start, "Decompressing .code...");
|
ShowProgress(0, data_end - data_start, STR_DECOMPRESSING_DOT_CODE);
|
||||||
ShowProgress(data_end - ptr_out, data_end - data_start, "Decompressing .code...");
|
ShowProgress(data_end - ptr_out, data_end - data_start, STR_DECOMPRESSING_DOT_CODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// sanity check
|
// sanity check
|
||||||
@ -242,13 +243,13 @@ bool CompressCodeLzss(const u8* a_pUncompressed, u32 a_uUncompressedSize, u8* a_
|
|||||||
u8* pDest = a_pCompressed + a_uUncompressedSize;
|
u8* pDest = a_pCompressed + a_uUncompressedSize;
|
||||||
|
|
||||||
while (pSrc - a_pUncompressed > 0 && pDest - a_pCompressed > 0) {
|
while (pSrc - a_pUncompressed > 0 && pDest - a_pCompressed > 0) {
|
||||||
if (!ShowProgress((u32)(a_pUncompressed + a_uUncompressedSize - pSrc), a_uUncompressedSize, "Compressing .code...")) {
|
if (!ShowProgress((u32)(a_pUncompressed + a_uUncompressedSize - pSrc), a_uUncompressedSize, STR_COMPRESSING_DOT_CODE)) {
|
||||||
if (ShowPrompt(true, "Compressing .code...\nB button detected. Cancel?")) {
|
if (ShowPrompt(true, "%s", STR_COMPRESSING_DOT_CODE_B_DETECTED_CANCEL)) {
|
||||||
bResult = false;
|
bResult = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ShowProgress(0, a_uUncompressedSize, "Compressing .code...");
|
ShowProgress(0, a_uUncompressedSize, STR_COMPRESSING_DOT_CODE);
|
||||||
ShowProgress((u32)(a_pUncompressed + a_uUncompressedSize - pSrc), a_uUncompressedSize, "Compressing .code...");
|
ShowProgress((u32)(a_pUncompressed + a_uUncompressedSize - pSrc), a_uUncompressedSize, STR_COMPRESSING_DOT_CODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
u8* pFlag = --pDest;
|
u8* pFlag = --pDest;
|
||||||
|
@ -12,18 +12,19 @@
|
|||||||
// valid addresses for FIRM section loading
|
// valid addresses for FIRM section loading
|
||||||
// pairs of start / end address, provided by Wolfvak
|
// pairs of start / end address, provided by Wolfvak
|
||||||
#define FIRM_VALID_ADDRESS \
|
#define FIRM_VALID_ADDRESS \
|
||||||
0x08000040, 0x08100000, \
|
|
||||||
0x18000000, 0x18600000, \
|
0x18000000, 0x18600000, \
|
||||||
0x1FF00000, 0x1FFFFC00
|
0x1FF00000, 0x1FFFFC00
|
||||||
|
|
||||||
// valid addresses (installable) for FIRM section loading
|
// valid addresses (installable) for FIRM section loading
|
||||||
#define FIRM_VALID_ADDRESS_INSTALL \
|
#define FIRM_VALID_ADDRESS_INSTALL \
|
||||||
FIRM_VALID_ADDRESS, \
|
FIRM_VALID_ADDRESS, \
|
||||||
|
0x08000040, 0x080F7FFF, \
|
||||||
0x10000000, 0x10200000
|
0x10000000, 0x10200000
|
||||||
|
|
||||||
// valid addresses (bootable) for FIRM section loading
|
// valid addresses (bootable) for FIRM section loading
|
||||||
#define FIRM_VALID_ADDRESS_BOOT \
|
#define FIRM_VALID_ADDRESS_BOOT \
|
||||||
FIRM_VALID_ADDRESS, \
|
FIRM_VALID_ADDRESS, \
|
||||||
|
0x08000040, 0x08100000, \
|
||||||
0x20000000, 0x27FFFA00
|
0x20000000, 0x27FFFA00
|
||||||
|
|
||||||
static const u32 whitelist_boot[] = { FIRM_VALID_ADDRESS_BOOT };
|
static const u32 whitelist_boot[] = { FIRM_VALID_ADDRESS_BOOT };
|
||||||
|
@ -20,3 +20,4 @@
|
|||||||
#include "bdri.h"
|
#include "bdri.h"
|
||||||
#include "ticketdb.h"
|
#include "ticketdb.h"
|
||||||
#include "ncchinfo.h"
|
#include "ncchinfo.h"
|
||||||
|
#include "cifinish.h"
|
||||||
|
@ -87,3 +87,17 @@ u32 ValidateAgbHeader(AgbHeader* agb) {
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
} */
|
} */
|
||||||
|
|
||||||
|
// see: http://problemkaputt.de/gbatek.htm#gbacartridgeheader
|
||||||
|
const char* AgbDestStr(const char* code) {
|
||||||
|
switch(code[3]) {
|
||||||
|
case 'J': return STR_REGION_JAPAN;
|
||||||
|
case 'E': return STR_REGION_AMERICAS;
|
||||||
|
case 'P': return STR_REGION_EUROPE;
|
||||||
|
case 'D': return STR_REGION_GERMANY;
|
||||||
|
case 'F': return STR_REGION_FRANCE;
|
||||||
|
case 'I': return STR_REGION_ITALY;
|
||||||
|
case 'S': return STR_REGION_SPAIN;
|
||||||
|
default: return STR_REGION_UNKNOWN;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include "language.h"
|
||||||
|
|
||||||
#define GBAVC_MAGIC '.', 'C', 'A', 'A'
|
#define GBAVC_MAGIC '.', 'C', 'A', 'A'
|
||||||
#define AGBSAVE_MAGIC '.', 'S', 'A', 'V'
|
#define AGBSAVE_MAGIC '.', 'S', 'A', 'V'
|
||||||
@ -28,16 +29,6 @@
|
|||||||
((size) == GBASAVE_FLASH_64K) || \
|
((size) == GBASAVE_FLASH_64K) || \
|
||||||
((size) == GBASAVE_FLASH_128K))
|
((size) == GBASAVE_FLASH_128K))
|
||||||
|
|
||||||
// see: http://problemkaputt.de/gbatek.htm#gbacartridgeheader
|
|
||||||
#define AGB_DESTSTR(code) \
|
|
||||||
(((code)[3] == 'J') ? "Japan" : \
|
|
||||||
((code)[3] == 'E') ? "USA/English" : \
|
|
||||||
((code)[3] == 'P') ? "Europe/Elsewhere" : \
|
|
||||||
((code)[3] == 'D') ? "German" : \
|
|
||||||
((code)[3] == 'F') ? "French" : \
|
|
||||||
((code)[3] == 'I') ? "Italian" : \
|
|
||||||
((code)[3] == 'S') ? "Spanish" : "Unknown")
|
|
||||||
|
|
||||||
|
|
||||||
// see: http://3dbrew.org/wiki/3DS_Virtual_Console#Footer
|
// see: http://3dbrew.org/wiki/3DS_Virtual_Console#Footer
|
||||||
// still a lot of unknowns in here, also redundant stuff left out
|
// still a lot of unknowns in here, also redundant stuff left out
|
||||||
@ -89,5 +80,8 @@ typedef struct {
|
|||||||
} __attribute__((packed, aligned(16))) AgbHeader;
|
} __attribute__((packed, aligned(16))) AgbHeader;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
u32 ValidateAgbSaveHeader(AgbSaveHeader* header);
|
u32 ValidateAgbSaveHeader(AgbSaveHeader* header);
|
||||||
u32 ValidateAgbHeader(AgbHeader* agb);
|
u32 ValidateAgbHeader(AgbHeader* agb);
|
||||||
|
|
||||||
|
const char* AgbDestStr(const char* code);
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include "ips.h"
|
#include "ips.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "fsperm.h"
|
#include "fsperm.h"
|
||||||
|
#include "language.h"
|
||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
#include "vff.h"
|
#include "vff.h"
|
||||||
|
|
||||||
@ -30,21 +31,21 @@ char errName[256];
|
|||||||
int displayError(int errcode) {
|
int displayError(int errcode) {
|
||||||
switch(errcode) {
|
switch(errcode) {
|
||||||
case IPS_NOTTHIS:
|
case IPS_NOTTHIS:
|
||||||
ShowPrompt(false, "%s\nThe patch is most likely not intended for this file.", errName); break;
|
ShowPrompt(false, "%s\n%s", errName, STR_PATCH_MOST_LIKELY_NOT_FOR_THIS_FILE); break;
|
||||||
case IPS_THISOUT:
|
case IPS_THISOUT:
|
||||||
ShowPrompt(false, "%s\nYou most likely applied the patch on the output file.", errName); break;
|
ShowPrompt(false, "%s\n%s", errName, STR_YOU_MOST_LIKELY_APPLIED_PATCH_ON_OUTPUT); break;
|
||||||
case IPS_SCRAMBLED:
|
case IPS_SCRAMBLED:
|
||||||
ShowPrompt(false, "%s\nThe patch is technically valid,\nbut seems scrambled or malformed.", errName); break;
|
ShowPrompt(false, "%s\n%s", errName, STR_PATCH_TECHNICALLY_VALID_BUT_SEEMS_SCRAMBLED); break;
|
||||||
case IPS_INVALID:
|
case IPS_INVALID:
|
||||||
ShowPrompt(false, "%s\nThe patch is invalid.", errName); break;
|
ShowPrompt(false, "%s\n%s", errName, STR_PATCH_IS_INVALID); break;
|
||||||
case IPS_16MB:
|
case IPS_16MB:
|
||||||
ShowPrompt(false, "%s\nOne or both files is bigger than 16MB.\nThe IPS format doesn't support that.", errName); break;
|
ShowPrompt(false, "%s\n%s", errName, STR_FILES_BIGGER_THAN_16MB_IPS_DOESNT_SUPPORT_THAT); break;
|
||||||
case IPS_INVALID_FILE_PATH:
|
case IPS_INVALID_FILE_PATH:
|
||||||
ShowPrompt(false, "%s\nThe requested file path was invalid.", errName); break;
|
ShowPrompt(false, "%s\n%s", errName, STR_REQUESTED_FILE_PATH_WAS_INVALID); break;
|
||||||
case IPS_CANCELED:
|
case IPS_CANCELED:
|
||||||
ShowPrompt(false, "%s\nPatching canceled.", errName); break;
|
ShowPrompt(false, "%s\n%s", errName, STR_PATCHING_CANCELED); break;
|
||||||
case IPS_MEMORY:
|
case IPS_MEMORY:
|
||||||
ShowPrompt(false, "%s\nNot enough memory.", errName); break;
|
ShowPrompt(false, "%s\n%s", errName, STR_NOT_ENOUGH_MEMORY); break;
|
||||||
}
|
}
|
||||||
fvx_close(&patchFile);
|
fvx_close(&patchFile);
|
||||||
fvx_close(&inFile);
|
fvx_close(&inFile);
|
||||||
@ -112,7 +113,7 @@ UINT read24() {
|
|||||||
int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName) {
|
int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName) {
|
||||||
int error = IPS_INVALID;
|
int error = IPS_INVALID;
|
||||||
UINT outlen_min, outlen_max, outlen_min_mem;
|
UINT outlen_min, outlen_max, outlen_min_mem;
|
||||||
snprintf(errName, 256, "%s", patchName);
|
snprintf(errName, sizeof(errName), "%s", patchName);
|
||||||
|
|
||||||
if (fvx_open(&patchFile, patchName, FA_READ) != FR_OK) return displayError(IPS_INVALID_FILE_PATH);
|
if (fvx_open(&patchFile, patchName, FA_READ) != FR_OK) return displayError(IPS_INVALID_FILE_PATH);
|
||||||
patchSize = fvx_size(&patchFile);
|
patchSize = fvx_size(&patchFile);
|
||||||
@ -140,7 +141,7 @@ int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName
|
|||||||
while (offset != 0x454F46) // 454F46=EOF
|
while (offset != 0x454F46) // 454F46=EOF
|
||||||
{
|
{
|
||||||
if (!ShowProgress(patchOffset, patchSize, patchName)) {
|
if (!ShowProgress(patchOffset, patchSize, patchName)) {
|
||||||
if (ShowPrompt(true, "%s\nB button detected. Cancel?", patchName)) return displayError(IPS_CANCELED);
|
if (ShowPrompt(true, "%s\n%s", patchName, STR_B_DETECTED_CANCEL)) return displayError(IPS_CANCELED);
|
||||||
ShowProgress(0, patchSize, patchName);
|
ShowProgress(0, patchSize, patchName);
|
||||||
ShowProgress(patchOffset, patchSize, patchName);
|
ShowProgress(patchOffset, patchSize, patchName);
|
||||||
}
|
}
|
||||||
@ -211,7 +212,7 @@ int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName
|
|||||||
while (offset != 0x454F46)
|
while (offset != 0x454F46)
|
||||||
{
|
{
|
||||||
if (!ShowProgress(offset, outSize, outName)) {
|
if (!ShowProgress(offset, outSize, outName)) {
|
||||||
if (ShowPrompt(true, "%s\nB button detected. Cancel?", outName)) return displayError(IPS_CANCELED);
|
if (ShowPrompt(true, "%s\n%s", outName, STR_B_DETECTED_CANCEL)) return displayError(IPS_CANCELED);
|
||||||
ShowProgress(0, outSize, outName);
|
ShowProgress(0, outSize, outName);
|
||||||
ShowProgress(offset, outSize, outName);
|
ShowProgress(offset, outSize, outName);
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,23 @@ u32 ValidateTwlHeader(TwlHeader* twl) {
|
|||||||
return (crc16_quick(twl->logo, sizeof(twl->logo)) == NDS_LOGO_CRC16) ? 0 : 1;
|
return (crc16_quick(twl->logo, sizeof(twl->logo)) == NDS_LOGO_CRC16) ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u32 VerifyTwlIconData(TwlIconData* icon, u32 version) {
|
||||||
|
u32 tsize = TWLICON_SIZE_DATA(version);
|
||||||
|
u32 isize = TWLICON_SIZE_DATA(icon->version);
|
||||||
|
u8* icn = (u8*) icon;
|
||||||
|
|
||||||
|
if (!isize) return 1;
|
||||||
|
if (version && (!tsize || tsize > isize)) return 1;
|
||||||
|
|
||||||
|
u32 size = version ? tsize : isize;
|
||||||
|
if ((size >= 0x0840) && (crc16_quick(icn + 0x0020, 0x0840 - 0x0020) != icon->crc_0x0020_0x0840)) return 1;
|
||||||
|
if ((size >= 0x0940) && (crc16_quick(icn + 0x0020, 0x0940 - 0x0020) != icon->crc_0x0020_0x0940)) return 1;
|
||||||
|
if ((size >= 0x1240) && (crc16_quick(icn + 0x0020, 0x0A40 - 0x0020) != icon->crc_0x0020_0x0A40)) return 1;
|
||||||
|
if ((size >= 0x23C0) && (crc16_quick(icn + 0x1240, 0x23C0 - 0x1240) != icon->crc_0x1240_0x23C0)) return 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
u32 BuildTwlSaveHeader(void* sav, u32 size) {
|
u32 BuildTwlSaveHeader(void* sav, u32 size) {
|
||||||
const u16 sct_size = 0x200;
|
const u16 sct_size = 0x200;
|
||||||
if (size / (u32) sct_size > 0xFFFF)
|
if (size / (u32) sct_size > 0xFFFF)
|
||||||
@ -38,21 +55,25 @@ u32 BuildTwlSaveHeader(void* sav, u32 size) {
|
|||||||
u16 n_sct = 1;
|
u16 n_sct = 1;
|
||||||
u16 sct_track = 1;
|
u16 sct_track = 1;
|
||||||
u16 sct_heads = 1;
|
u16 sct_heads = 1;
|
||||||
while (true) {
|
u16 n_sct_next = 0;
|
||||||
if (sct_heads < sct_track) {
|
while (n_sct_next <= n_sct_max) {
|
||||||
u16 n_sct_next = sct_track * (sct_heads+1) * (sct_heads+1);
|
n_sct_next = sct_track * (sct_heads + 1) * (sct_heads + 1);
|
||||||
if (n_sct_next < n_sct_max) {
|
if (n_sct_next <= n_sct_max) {
|
||||||
sct_heads++;
|
sct_heads++;
|
||||||
|
n_sct = n_sct_next;
|
||||||
|
|
||||||
|
sct_track++;
|
||||||
|
n_sct_next = sct_track * sct_heads * sct_heads;
|
||||||
|
if (n_sct_next <= n_sct_max) {
|
||||||
n_sct = n_sct_next;
|
n_sct = n_sct_next;
|
||||||
} else break;
|
}
|
||||||
} else {
|
|
||||||
u16 n_sct_next = (sct_track+1) * sct_heads * sct_heads;
|
|
||||||
if (n_sct_next < n_sct_max) {
|
|
||||||
sct_track++;
|
|
||||||
n_sct = n_sct_next;
|
|
||||||
} else break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
n_sct_next = (sct_track + 1) * sct_heads * sct_heads;
|
||||||
|
if (n_sct_next <= n_sct_max) {
|
||||||
|
sct_track++;
|
||||||
|
n_sct = n_sct_next;
|
||||||
|
}
|
||||||
|
|
||||||
// sectors per cluster (should be identical to Nintendo)
|
// sectors per cluster (should be identical to Nintendo)
|
||||||
u8 clr_size = (n_sct > 8 * 1024) ? 8 : (n_sct > 1024) ? 4 : 1;
|
u8 clr_size = (n_sct > 8 * 1024) ? 8 : (n_sct > 1024) ? 4 : 1;
|
||||||
@ -126,11 +147,19 @@ u32 GetTwlIcon(u16* icon, const TwlIconData* twl_icon) {
|
|||||||
u32 ix = x + (i & 0x7);
|
u32 ix = x + (i & 0x7);
|
||||||
u32 iy = y + (i >> 3);
|
u32 iy = y + (i >> 3);
|
||||||
|
|
||||||
pix555 = palette[((i%2) ? (*pix4 >> 4) : *pix4) & 0xF];
|
int palette_index = ((i%2) ? (*pix4 >> 4) : *pix4) & 0xF;
|
||||||
r = pix555 & 0x1F;
|
if (palette_index) {
|
||||||
g = ((pix555 >> 5) & 0x1F) << 1;
|
pix555 = palette[palette_index];
|
||||||
g |= (g >> 1) & 1;
|
r = pix555 & 0x1F;
|
||||||
b = (pix555 >> 10) & 0x1F;
|
g = ((pix555 >> 5) & 0x1F) << 1;
|
||||||
|
g |= (g >> 1) & 1;
|
||||||
|
b = (pix555 >> 10) & 0x1F;
|
||||||
|
} else {
|
||||||
|
// Set transparent pixels to white
|
||||||
|
r = 31;
|
||||||
|
g = 63;
|
||||||
|
b = 31;
|
||||||
|
}
|
||||||
icon[(iy * w) + ix] = (r << 11) | (g << 5) | b;
|
icon[(iy * w) + ix] = (r << 11) | (g << 5) | b;
|
||||||
if (i % 2) pix4++;
|
if (i % 2) pix4++;
|
||||||
}
|
}
|
||||||
|
@ -127,6 +127,7 @@ typedef struct {
|
|||||||
} PACKED_STRUCT TwlHeader;
|
} PACKED_STRUCT TwlHeader;
|
||||||
|
|
||||||
u32 ValidateTwlHeader(TwlHeader* twl);
|
u32 ValidateTwlHeader(TwlHeader* twl);
|
||||||
|
u32 VerifyTwlIconData(TwlIconData* icon, u32 size);
|
||||||
u32 BuildTwlSaveHeader(void* sav, u32 size);
|
u32 BuildTwlSaveHeader(void* sav, u32 size);
|
||||||
u32 LoadTwlMetaData(const char* path, TwlHeader* hdr, TwlIconData* icon);
|
u32 LoadTwlMetaData(const char* path, TwlHeader* hdr, TwlIconData* icon);
|
||||||
u32 GetTwlTitle(char* desc, const TwlIconData* twl_icon);
|
u32 GetTwlTitle(char* desc, const TwlIconData* twl_icon);
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include "language.h"
|
||||||
#include "region.h"
|
#include "region.h"
|
||||||
|
|
||||||
// Names of system regions, short form.
|
// Names of system regions, short form.
|
||||||
@ -12,13 +13,16 @@ const char* const g_regionNamesShort[SMDH_NUM_REGIONS] = {
|
|||||||
"TWN",
|
"TWN",
|
||||||
};
|
};
|
||||||
|
|
||||||
// Names of system regions, long form.
|
// Names of system regions, long form and translatable.
|
||||||
const char* const g_regionNamesLong[SMDH_NUM_REGIONS] = {
|
const char* regionNameLong(int region) {
|
||||||
"Japan",
|
switch(region) {
|
||||||
"Americas",
|
case REGION_JPN: return STR_REGION_JAPAN;
|
||||||
"Europe",
|
case REGION_USA: return STR_REGION_AMERICAS;
|
||||||
"Australia",
|
case REGION_EUR: return STR_REGION_EUROPE;
|
||||||
"China",
|
case REGION_AUS: return STR_REGION_AUSTRALIA;
|
||||||
"Korea",
|
case REGION_CHN: return STR_REGION_CHINA;
|
||||||
"Taiwan",
|
case REGION_KOR: return STR_REGION_KOREA;
|
||||||
|
case REGION_TWN: return STR_REGION_TAIWAN;
|
||||||
|
default: return STR_REGION_UNKNOWN;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
@ -27,5 +27,5 @@
|
|||||||
|
|
||||||
// Names of system regions, short form.
|
// Names of system regions, short form.
|
||||||
extern const char* const g_regionNamesShort[SMDH_NUM_REGIONS];
|
extern const char* const g_regionNamesShort[SMDH_NUM_REGIONS];
|
||||||
// Names of system regions, long form.
|
// Names of system regions, long form and translatable.
|
||||||
extern const char* const g_regionNamesLong[SMDH_NUM_REGIONS];
|
const char* regionNameLong(int region);
|
||||||
|
@ -9,28 +9,28 @@
|
|||||||
|
|
||||||
// this structure is 0x80 bytes, thanks @luigoalma
|
// this structure is 0x80 bytes, thanks @luigoalma
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char magic[4]; // "PREP" for prepurchase install. NIM excepts "PREP" to do seed downloads on the background.
|
char magic[4]; // "PREP" for prepurchase install. NIM excepts "PREP" to do seed downloads on the background.
|
||||||
// playable date parameters
|
// playable date parameters
|
||||||
// 2000-01-01 is a safe bet for a stub entry
|
// 2000-01-01 is a safe bet for a stub entry
|
||||||
s32 year;
|
s32 year;
|
||||||
u8 month;
|
u8 month;
|
||||||
u8 day;
|
u8 day;
|
||||||
u16 country_code; // enum list of values, this will affect seed downloading, just requires at least one valid enum value. 1 == Japan, it's enough.
|
u16 country_code; // enum list of values, this will affect seed downloading, just requires at least one valid enum value. 1 == Japan, it's enough.
|
||||||
// everything after this point can be 0 padded
|
// everything after this point can be 0 padded
|
||||||
u32 seed_status; // 0 == not tried, 1 == last attempt failed, 2 == seed downloaded successfully
|
u32 seed_status; // 0 == not tried, 1 == last attempt failed, 2 == seed downloaded successfully
|
||||||
s32 seed_result; // result related to last download attempt
|
s32 seed_result; // result related to last download attempt
|
||||||
s32 seed_support_error_code; // support code derived from the result code
|
s32 seed_support_error_code; // support code derived from the result code
|
||||||
// after this point, all is unused or padding. NIM wont use or access this at all.
|
// after this point, all is unused or padding. NIM wont use or access this at all.
|
||||||
// It's memset to 0 by NIM
|
// It's memset to 0 by NIM
|
||||||
u8 unknown[0x68];
|
u8 unknown[0x68];
|
||||||
} PACKED_STRUCT TitleTagEntry;
|
} PACKED_STRUCT TitleTagEntry;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
u32 unknown0;
|
u32 unknown0;
|
||||||
u32 n_entries;
|
u32 n_entries;
|
||||||
u8 unknown1[0x1000 - 0x8];
|
u8 unknown1[0x1000 - 0x8];
|
||||||
u64 titleId[TITLETAG_MAX_ENTRIES];
|
u64 titleId[TITLETAG_MAX_ENTRIES];
|
||||||
TitleTagEntry tag[TITLETAG_MAX_ENTRIES];
|
TitleTagEntry tag[TITLETAG_MAX_ENTRIES];
|
||||||
} PACKED_STRUCT TitleTag;
|
} PACKED_STRUCT TitleTag;
|
||||||
|
|
||||||
u32 GetSeedPath(char* path, const char* drv) {
|
u32 GetSeedPath(char* path, const char* drv) {
|
||||||
@ -82,7 +82,7 @@ u32 FindSeed(u8* seed, u64 titleId, u32 hash_seed) {
|
|||||||
// read SEEDDB from file
|
// read SEEDDB from file
|
||||||
if (GetSeedPath(path, nand_drv[i]) != 0) continue;
|
if (GetSeedPath(path, nand_drv[i]) != 0) continue;
|
||||||
if ((ReadDisaDiffIvfcLvl4(path, NULL, SEEDSAVE_AREA_OFFSET, sizeof(SeedDb), seeddb) != sizeof(SeedDb)) ||
|
if ((ReadDisaDiffIvfcLvl4(path, NULL, SEEDSAVE_AREA_OFFSET, sizeof(SeedDb), seeddb) != sizeof(SeedDb)) ||
|
||||||
(seeddb->n_entries > SEEDSAVE_MAX_ENTRIES))
|
(seeddb->n_entries > SEEDSAVE_MAX_ENTRIES))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// search for the seed
|
// search for the seed
|
||||||
@ -98,7 +98,7 @@ u32 FindSeed(u8* seed, u64 titleId, u32 hash_seed) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// not found -> try seeddb.bin
|
// not found -> try seeddb.bin
|
||||||
SeedInfo* seeddb = (SeedInfo*) (void*) buffer;
|
SeedInfo* seeddb = (SeedInfo*) (void*) buffer;
|
||||||
size_t len = LoadSupportFile(SEEDINFO_NAME, seeddb, STD_BUFFER_SIZE);
|
size_t len = LoadSupportFile(SEEDINFO_NAME, seeddb, STD_BUFFER_SIZE);
|
||||||
if (len && (seeddb->n_entries <= (len - 16) / 32)) { // check filesize / seeddb size
|
if (len && (seeddb->n_entries <= (len - 16) / 32)) { // check filesize / seeddb size
|
||||||
@ -169,49 +169,49 @@ u32 InstallSeedDbToSystem(SeedInfo* seed_info, bool to_emunand) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
u32 SetupSeedPrePurchase(u64 titleId, bool to_emunand) {
|
u32 SetupSeedPrePurchase(u64 titleId, bool to_emunand) {
|
||||||
// here, we ask the system to install the seed for us
|
// here, we ask the system to install the seed for us
|
||||||
TitleTag* titletag = (TitleTag*) malloc(sizeof(TitleTag));
|
TitleTag* titletag = (TitleTag*) malloc(sizeof(TitleTag));
|
||||||
if (!titletag) return 1;
|
if (!titletag) return 1;
|
||||||
|
|
||||||
char path[128];
|
char path[128];
|
||||||
if ((GetSeedPath(path, to_emunand ? "4:" : "1:") != 0) ||
|
if ((GetSeedPath(path, to_emunand ? "4:" : "1:") != 0) ||
|
||||||
(ReadDisaDiffIvfcLvl4(path, NULL, TITLETAG_AREA_OFFSET, sizeof(TitleTag), titletag) != sizeof(TitleTag)) ||
|
(ReadDisaDiffIvfcLvl4(path, NULL, TITLETAG_AREA_OFFSET, sizeof(TitleTag), titletag) != sizeof(TitleTag)) ||
|
||||||
(titletag->n_entries >= TITLETAG_MAX_ENTRIES)) {
|
(titletag->n_entries >= TITLETAG_MAX_ENTRIES)) {
|
||||||
free (titletag);
|
free (titletag);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// pointers for TITLETAG title IDs and seeds
|
// pointers for TITLETAG title IDs and seeds
|
||||||
// find a free slot, insert titletag
|
// find a free slot, insert titletag
|
||||||
u32 slot = 0;
|
u32 slot = 0;
|
||||||
for (; slot < titletag->n_entries; slot++)
|
for (; slot < titletag->n_entries; slot++)
|
||||||
if (titletag->titleId[slot] == titleId) break;
|
if (titletag->titleId[slot] == titleId) break;
|
||||||
if (slot >= titletag->n_entries)
|
if (slot >= titletag->n_entries)
|
||||||
titletag->n_entries = slot + 1;
|
titletag->n_entries = slot + 1;
|
||||||
|
|
||||||
TitleTagEntry* ttag = &(titletag->tag[slot]);
|
TitleTagEntry* ttag = &(titletag->tag[slot]);
|
||||||
titletag->titleId[slot] = titleId;
|
titletag->titleId[slot] = titleId;
|
||||||
memset(ttag, 0, sizeof(TitleTagEntry));
|
memset(ttag, 0, sizeof(TitleTagEntry));
|
||||||
memcpy(ttag->magic, "PREP", 4);
|
memcpy(ttag->magic, "PREP", 4);
|
||||||
ttag->year = 2000;
|
ttag->year = 2000;
|
||||||
ttag->month = 1;
|
ttag->month = 1;
|
||||||
ttag->day = 1;
|
ttag->day = 1;
|
||||||
ttag->country_code = 1;
|
ttag->country_code = 1;
|
||||||
|
|
||||||
// write back to system (warning: no write protection checks here)
|
// write back to system (warning: no write protection checks here)
|
||||||
u32 size = WriteDisaDiffIvfcLvl4(path, NULL, TITLETAG_AREA_OFFSET, sizeof(TitleTag), titletag);
|
u32 size = WriteDisaDiffIvfcLvl4(path, NULL, TITLETAG_AREA_OFFSET, sizeof(TitleTag), titletag);
|
||||||
FixFileCmac(path, false);
|
FixFileCmac(path, false);
|
||||||
|
|
||||||
free(titletag);
|
free(titletag);
|
||||||
return (size == sizeof(TitleTag)) ? 0 : 1;
|
return (size == sizeof(TitleTag)) ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 SetupSeedSystemCrypto(u64 titleId, u32 hash_seed, bool to_emunand) {
|
u32 SetupSeedSystemCrypto(u64 titleId, u32 hash_seed, bool to_emunand) {
|
||||||
// attempt to find the seed inside the seeddb.bin support file
|
// attempt to find the seed inside the seeddb.bin support file
|
||||||
SeedInfo* seeddb = (SeedInfo*) malloc(STD_BUFFER_SIZE);
|
SeedInfo* seeddb = (SeedInfo*) malloc(STD_BUFFER_SIZE);
|
||||||
if (!seeddb) return 1;
|
if (!seeddb) return 1;
|
||||||
|
|
||||||
size_t len = LoadSupportFile(SEEDINFO_NAME, seeddb, STD_BUFFER_SIZE);
|
size_t len = LoadSupportFile(SEEDINFO_NAME, seeddb, STD_BUFFER_SIZE);
|
||||||
if (len && (seeddb->n_entries <= (len - 16) / 32)) { // check filesize / seeddb size
|
if (len && (seeddb->n_entries <= (len - 16) / 32)) { // check filesize / seeddb size
|
||||||
for (u32 s = 0; s < seeddb->n_entries; s++) {
|
for (u32 s = 0; s < seeddb->n_entries; s++) {
|
||||||
if (titleId != seeddb->entries[s].titleId)
|
if (titleId != seeddb->entries[s].titleId)
|
||||||
@ -224,7 +224,7 @@ u32 SetupSeedSystemCrypto(u64 titleId, u32 hash_seed, bool to_emunand) {
|
|||||||
sha_quick(sha256sum, lseed, 16 + 8, SHA256_MODE);
|
sha_quick(sha256sum, lseed, 16 + 8, SHA256_MODE);
|
||||||
u32 res = 0; // assuming the installed seed to be correct
|
u32 res = 0; // assuming the installed seed to be correct
|
||||||
if (hash_seed == sha256sum[0]) {
|
if (hash_seed == sha256sum[0]) {
|
||||||
// found, install it
|
// found, install it
|
||||||
seeddb->n_entries = 1;
|
seeddb->n_entries = 1;
|
||||||
seeddb->entries[0].titleId = titleId;
|
seeddb->entries[0].titleId = titleId;
|
||||||
memcpy(&(seeddb->entries[0].seed), lseed, sizeof(Seed));
|
memcpy(&(seeddb->entries[0].seed), lseed, sizeof(Seed));
|
||||||
@ -236,5 +236,5 @@ u32 SetupSeedSystemCrypto(u64 titleId, u32 hash_seed, bool to_emunand) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
free(seeddb);
|
free(seeddb);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
#define SEEDSAVE_AREA_OFFSET 0x3000
|
#define SEEDSAVE_AREA_OFFSET 0x3000
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
u8 byte[16];
|
u8 byte[16];
|
||||||
} PACKED_STRUCT Seed;
|
} PACKED_STRUCT Seed;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -26,11 +26,11 @@ typedef struct {
|
|||||||
} PACKED_STRUCT SeedInfo;
|
} PACKED_STRUCT SeedInfo;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
u32 unknown0;
|
u32 unknown0;
|
||||||
u32 n_entries;
|
u32 n_entries;
|
||||||
u8 unknown1[0x1000 - 0x8];
|
u8 unknown1[0x1000 - 0x8];
|
||||||
u64 titleId[SEEDSAVE_MAX_ENTRIES];
|
u64 titleId[SEEDSAVE_MAX_ENTRIES];
|
||||||
Seed seed[SEEDSAVE_MAX_ENTRIES];
|
Seed seed[SEEDSAVE_MAX_ENTRIES];
|
||||||
} PACKED_STRUCT SeedDb;
|
} PACKED_STRUCT SeedDb;
|
||||||
|
|
||||||
u32 GetSeedPath(char* path, const char* drv);
|
u32 GetSeedPath(char* path, const char* drv);
|
||||||
|
@ -1,6 +1,20 @@
|
|||||||
#include "tad.h"
|
#include "tad.h"
|
||||||
|
#include "sha.h"
|
||||||
|
|
||||||
|
|
||||||
|
u32 VerifyTadStub(TadStub* tad) {
|
||||||
|
TadFooter* ftr = &(tad->footer);
|
||||||
|
TadHeader* hdr = &(tad->header);
|
||||||
|
TadBanner* bnr = &(tad->banner);
|
||||||
|
|
||||||
|
if ((strncmp(hdr->magic, TAD_HEADER_MAGIC, strlen(TAD_HEADER_MAGIC)) != 0) ||
|
||||||
|
(sha_cmp(ftr->banner_sha256, bnr, sizeof(TadBanner), SHA256_MODE) != 0) ||
|
||||||
|
(sha_cmp(ftr->header_sha256, hdr, sizeof(TadHeader), SHA256_MODE) != 0))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
u32 BuildTadContentTable(void* table, void* header) {
|
u32 BuildTadContentTable(void* table, void* header) {
|
||||||
TadHeader* hdr = (TadHeader*) header;
|
TadHeader* hdr = (TadHeader*) header;
|
||||||
TadContentTable* tbl = (TadContentTable*) table;
|
TadContentTable* tbl = (TadContentTable*) table;
|
||||||
|
@ -56,4 +56,14 @@ typedef struct {
|
|||||||
u8 padding[0x4];
|
u8 padding[0x4];
|
||||||
} PACKED_STRUCT TadFooter;
|
} PACKED_STRUCT TadFooter;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
TadBanner banner;
|
||||||
|
TadBlockMetaData banner_bmd;
|
||||||
|
TadHeader header;
|
||||||
|
TadBlockMetaData header_bmd;
|
||||||
|
TadFooter footer;
|
||||||
|
TadBlockMetaData footer_bmd;
|
||||||
|
} PACKED_STRUCT TadStub;
|
||||||
|
|
||||||
|
u32 VerifyTadStub(TadStub* tad);
|
||||||
u32 BuildTadContentTable(void* table, void* header);
|
u32 BuildTadContentTable(void* table, void* header);
|
||||||
|
@ -19,47 +19,113 @@ u32 ValidateTicket(Ticket* ticket) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 ValidateTicketSignature(Ticket* ticket) {
|
u32 ValidateTwlTicket(Ticket* ticket) {
|
||||||
static bool got_modexp = false;
|
static const u8 magic[] = { TICKET_SIG_TYPE_TWL };
|
||||||
static u32 mod[0x100 / 0x4] = { 0 };
|
if ((memcmp(ticket->sig_type, magic, sizeof(magic)) != 0) ||
|
||||||
static u32 exp = 0;
|
(strncmp((char*) ticket->issuer, TICKET_ISSUER_TWL, 0x40) != 0))
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (!got_modexp) {
|
u32 ValidateTicketSignature(Ticket* ticket) {
|
||||||
// grab mod/exp from cert from cert.db
|
Certificate cert;
|
||||||
if (LoadCertFromCertDb(0x3F10, NULL, mod, &exp) == 0)
|
|
||||||
got_modexp = true;
|
// grab cert from certs.db
|
||||||
else return 1;
|
if (LoadCertFromCertDb(&cert, (char*)(ticket->issuer)) != 0)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
int ret = Certificate_VerifySignatureBlock(&cert, &(ticket->signature), 0x100, (void*)&(ticket->issuer), GetTicketSize(ticket) - 0x140, true);
|
||||||
|
|
||||||
|
Certificate_Cleanup(&cert);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 BuildVariableFakeTicket(Ticket** ticket, u32* ticket_size, const u8* title_id, u32 index_max) {
|
||||||
|
if (!ticket || !ticket_size)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
static const u8 sig_type[4] = { TICKET_SIG_TYPE }; // RSA_2048 SHA256
|
||||||
|
|
||||||
|
// calculate sizes and determine pointers to use
|
||||||
|
u32 rights_field_count = (min(index_max, 0x10000) + 1023) >> 10; // round up to 1024 and cap at 65536, and div by 1024
|
||||||
|
u32 content_index_size = sizeof(TicketContentIndexMainHeader) + sizeof(TicketContentIndexDataHeader) + sizeof(TicketRightsField) * rights_field_count;
|
||||||
|
u32 _ticket_size = sizeof(Ticket) + content_index_size;
|
||||||
|
Ticket *_ticket;
|
||||||
|
|
||||||
|
if (*ticket) { // if a pointer was pregiven
|
||||||
|
if (*ticket_size < _ticket_size) { // then check given boundary size
|
||||||
|
*ticket_size = _ticket_size; // if not enough, inform the actual needed size
|
||||||
|
return 2; // indicate a size error
|
||||||
|
}
|
||||||
|
_ticket = *ticket; // get the pointer if we good to go
|
||||||
|
} else // if not pregiven, allocate one
|
||||||
|
_ticket = (Ticket*)malloc(_ticket_size);
|
||||||
|
|
||||||
|
if (!_ticket)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
// set ticket all zero for a clean start
|
||||||
|
memset(_ticket, 0x00, _ticket_size);
|
||||||
|
// fill ticket values
|
||||||
|
memcpy(_ticket->sig_type, sig_type, 4);
|
||||||
|
memset(_ticket->signature, 0xFF, 0x100);
|
||||||
|
snprintf((char*) _ticket->issuer, 0x40, IS_DEVKIT ? TICKET_ISSUER_DEV : TICKET_ISSUER);
|
||||||
|
memset(_ticket->ecdsa, 0xFF, 0x3C);
|
||||||
|
_ticket->version = 0x01;
|
||||||
|
memset(_ticket->titlekey, 0xFF, 16);
|
||||||
|
if (title_id) memcpy(_ticket->title_id, title_id, 8);
|
||||||
|
_ticket->commonkey_idx = 0x00; // eshop
|
||||||
|
_ticket->audit = 0x01; // whatever
|
||||||
|
|
||||||
|
// fill in rights
|
||||||
|
TicketContentIndexMainHeader* mheader = (TicketContentIndexMainHeader*)&_ticket->content_index[0];
|
||||||
|
TicketContentIndexDataHeader* dheader = (TicketContentIndexDataHeader*)&_ticket->content_index[0x14];
|
||||||
|
TicketRightsField* rights = (TicketRightsField*)&_ticket->content_index[0x28];
|
||||||
|
|
||||||
|
// first main data header
|
||||||
|
mheader->unk1[1] = 0x1; mheader->unk2[1] = 0x14;
|
||||||
|
mheader->content_index_size[3] = (u8)(content_index_size >> 0);
|
||||||
|
mheader->content_index_size[2] = (u8)(content_index_size >> 8);
|
||||||
|
mheader->content_index_size[1] = (u8)(content_index_size >> 16);
|
||||||
|
mheader->content_index_size[0] = (u8)(content_index_size >> 24);
|
||||||
|
mheader->data_header_relative_offset[3] = 0x14; // relative offset for TicketContentIndexDataHeader
|
||||||
|
mheader->unk3[1] = 0x1; mheader->unk4[1] = 0x14;
|
||||||
|
|
||||||
|
// then the data header
|
||||||
|
dheader->data_relative_offset[3] = 0x28; // relative offset for TicketRightsField
|
||||||
|
dheader->max_entry_count[3] = (u8)(rights_field_count >> 0);
|
||||||
|
dheader->max_entry_count[2] = (u8)(rights_field_count >> 8);
|
||||||
|
dheader->max_entry_count[1] = (u8)(rights_field_count >> 16);
|
||||||
|
dheader->max_entry_count[0] = (u8)(rights_field_count >> 24);
|
||||||
|
dheader->size_per_entry[3] = (u8)sizeof(TicketRightsField); // sizeof should be 0x84
|
||||||
|
dheader->total_size_used[3] = (u8)((sizeof(TicketRightsField) * rights_field_count) >> 0);
|
||||||
|
dheader->total_size_used[2] = (u8)((sizeof(TicketRightsField) * rights_field_count) >> 8);
|
||||||
|
dheader->total_size_used[1] = (u8)((sizeof(TicketRightsField) * rights_field_count) >> 16);
|
||||||
|
dheader->total_size_used[0] = (u8)((sizeof(TicketRightsField) * rights_field_count) >> 24);
|
||||||
|
dheader->data_type[1] = 3; // right fields
|
||||||
|
|
||||||
|
// now the right fields
|
||||||
|
// indexoffets must be in accending order to have the desired effect
|
||||||
|
for (u32 i = 0; i < rights_field_count; ++i) {
|
||||||
|
rights[i].indexoffset[1] = (u8)((1024 * i) >> 0);
|
||||||
|
rights[i].indexoffset[0] = (u8)((1024 * i) >> 8);
|
||||||
|
memset(&rights[i].rightsbitfield[0], 0xFF, sizeof(rights[0].rightsbitfield));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!RSA_setKey2048(3, mod, exp) ||
|
*ticket = _ticket;
|
||||||
!RSA_verify2048((void*) &(ticket->signature), (void*) &(ticket->issuer), GetTicketSize(ticket) - 0x140))
|
*ticket_size = _ticket_size;
|
||||||
return 1;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 BuildFakeTicket(Ticket* ticket, u8* title_id) {
|
u32 BuildFakeTicket(Ticket* ticket, const u8* title_id) {
|
||||||
static const u8 sig_type[4] = { TICKET_SIG_TYPE }; // RSA_2048 SHA256
|
Ticket* tik = NULL;
|
||||||
static const u8 ticket_cnt_index[] = { // whatever this is
|
u32 ticket_size = sizeof(TicketCommon);
|
||||||
0x00, 0x01, 0x00, 0x14, 0x00, 0x00, 0x00, 0xAC, 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x00, 0x14,
|
u32 res = BuildVariableFakeTicket(&tik, &ticket_size, title_id, TICKET_MAX_CONTENTS);
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x84,
|
if (res != 0) return res;
|
||||||
0x00, 0x00, 0x00, 0x84, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
memcpy(ticket, tik, ticket_size);
|
||||||
};
|
free(tik);
|
||||||
// set ticket all zero for a clean start
|
|
||||||
memset(ticket, 0x00, TICKET_COMMON_SIZE); // 0xAC being size of this fake ticket's content index
|
|
||||||
// fill ticket values
|
|
||||||
memcpy(ticket->sig_type, sig_type, 4);
|
|
||||||
memset(ticket->signature, 0xFF, 0x100);
|
|
||||||
snprintf((char*) ticket->issuer, 0x40, IS_DEVKIT ? TICKET_ISSUER_DEV : TICKET_ISSUER);
|
|
||||||
memset(ticket->ecdsa, 0xFF, 0x3C);
|
|
||||||
ticket->version = 0x01;
|
|
||||||
memset(ticket->titlekey, 0xFF, 16);
|
|
||||||
memcpy(ticket->title_id, title_id, 8);
|
|
||||||
ticket->commonkey_idx = 0x00; // eshop
|
|
||||||
ticket->audit = 0x01; // whatever
|
|
||||||
memcpy(ticket->content_index, ticket_cnt_index, sizeof(ticket_cnt_index));
|
|
||||||
memset(&ticket->content_index[sizeof(ticket_cnt_index)], 0xFF, 0x80); // 1024 content indexes
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,19 +147,14 @@ u32 BuildTicketCert(u8* tickcert) {
|
|||||||
0xC6, 0x4B, 0xD4, 0x8F, 0xDF, 0x13, 0x21, 0x3D, 0xFC, 0x72, 0xFC, 0x8D, 0x9F, 0xDD, 0x01, 0x0E
|
0xC6, 0x4B, 0xD4, 0x8F, 0xDF, 0x13, 0x21, 0x3D, 0xFC, 0x72, 0xFC, 0x8D, 0x9F, 0xDD, 0x01, 0x0E
|
||||||
};
|
};
|
||||||
|
|
||||||
// open certs.db file on SysNAND
|
static const char* const retail_issuers[] = {"Root-CA00000003-XS0000000c", "Root-CA00000003"};
|
||||||
FIL db;
|
static const char* const dev_issuers[] = {"Root-CA00000004-XS00000009", "Root-CA00000004"};
|
||||||
UINT bytes_read;
|
|
||||||
if (f_open(&db, "1:/dbs/certs.db", FA_READ | FA_OPEN_EXISTING) != FR_OK)
|
size_t size = TICKET_CDNCERT_SIZE;
|
||||||
|
if (BuildRawCertBundleFromCertDb(tickcert, &size, !IS_DEVKIT ? retail_issuers : dev_issuers, 2) ||
|
||||||
|
size != TICKET_CDNCERT_SIZE) {
|
||||||
return 1;
|
return 1;
|
||||||
// grab ticket cert from 3 offsets
|
}
|
||||||
f_lseek(&db, 0x3F10);
|
|
||||||
f_read(&db, tickcert + 0x000, 0x300, &bytes_read);
|
|
||||||
f_lseek(&db, 0x0C10);
|
|
||||||
f_read(&db, tickcert + 0x300, 0x1F0, &bytes_read);
|
|
||||||
f_lseek(&db, 0x3A00);
|
|
||||||
f_read(&db, tickcert + 0x4F0, 0x210, &bytes_read);
|
|
||||||
f_close(&db);
|
|
||||||
|
|
||||||
// check the certificate hash
|
// check the certificate hash
|
||||||
u8 cert_hash[0x20];
|
u8 cert_hash[0x20];
|
||||||
|
@ -1,15 +1,22 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include "tmd.h"
|
||||||
|
|
||||||
#define TICKET_COMMON_SIZE sizeof(TicketCommon)
|
#define TICKET_COMMON_SIZE sizeof(TicketCommon)
|
||||||
#define TICKET_MINIMUM_SIZE sizeof(TicketMinimum)
|
#define TICKET_MINIMUM_SIZE sizeof(TicketMinimum)
|
||||||
|
#define TICKET_TWL_SIZE sizeof(Ticket)
|
||||||
#define TICKET_CDNCERT_SIZE 0x700
|
#define TICKET_CDNCERT_SIZE 0x700
|
||||||
|
#define TICKET_MAX_CONTENTS TITLE_MAX_CONTENTS // should be TMD_MAX_CONTENTS
|
||||||
|
#define TICKET_COMMON_CNT_INDEX_SIZE (0x28 + (((TICKET_MAX_CONTENTS + 1023) >> 10) * 0x84))
|
||||||
|
|
||||||
#define TICKET_ISSUER "Root-CA00000003-XS0000000c"
|
#define TICKET_ISSUER "Root-CA00000003-XS0000000c"
|
||||||
#define TICKET_ISSUER_DEV "Root-CA00000004-XS00000009"
|
#define TICKET_ISSUER_DEV "Root-CA00000004-XS00000009"
|
||||||
#define TICKET_SIG_TYPE 0x00, 0x01, 0x00, 0x04 // RSA_2048 SHA256
|
#define TICKET_SIG_TYPE 0x00, 0x01, 0x00, 0x04 // RSA_2048 SHA256
|
||||||
|
|
||||||
|
#define TICKET_ISSUER_TWL "Root-CA00000001-XS00000006"
|
||||||
|
#define TICKET_SIG_TYPE_TWL 0x00, 0x01, 0x00, 0x01 // RSA_2048 SHA1
|
||||||
|
|
||||||
#define TICKET_DEVKIT(tick) (strncmp((char*)tick->issuer, TICKET_ISSUER_DEV, 0x40) == 0)
|
#define TICKET_DEVKIT(tick) (strncmp((char*)tick->issuer, TICKET_ISSUER_DEV, 0x40) == 0)
|
||||||
|
|
||||||
// from: https://github.com/profi200/Project_CTR/blob/02159e17ee225de3f7c46ca195ff0f9ba3b3d3e4/ctrtool/tik.h#L15-L39
|
// from: https://github.com/profi200/Project_CTR/blob/02159e17ee225de3f7c46ca195ff0f9ba3b3d3e4/ctrtool/tik.h#L15-L39
|
||||||
@ -46,18 +53,18 @@
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
TICKETBASE;
|
TICKETBASE;
|
||||||
u8 content_index[];
|
u8 content_index[];
|
||||||
} __attribute__((packed, aligned(4))) Ticket;
|
} PACKED_STRUCT Ticket;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
TICKETBASE;
|
TICKETBASE;
|
||||||
u8 content_index[0xAC];
|
u8 content_index[TICKET_COMMON_CNT_INDEX_SIZE];
|
||||||
} __attribute__((packed, aligned(4))) TicketCommon;
|
} PACKED_STRUCT TicketCommon;
|
||||||
|
|
||||||
// minimum allowed content_index is 0x14
|
// minimum allowed content_index is 0x14
|
||||||
typedef struct {
|
typedef struct {
|
||||||
TICKETBASE;
|
TICKETBASE;
|
||||||
u8 content_index[0x14];
|
u8 content_index[0x14];
|
||||||
} __attribute__((packed, aligned(4))) TicketMinimum;
|
} PACKED_STRUCT TicketMinimum;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
u8 unk1[2];
|
u8 unk1[2];
|
||||||
@ -67,7 +74,7 @@ typedef struct {
|
|||||||
u8 unk3[2];
|
u8 unk3[2];
|
||||||
u8 unk4[2];
|
u8 unk4[2];
|
||||||
u8 unk5[4];
|
u8 unk5[4];
|
||||||
} __attribute__((packed)) TicketContentIndexMainHeader;
|
} PACKED_ALIGN(1) TicketContentIndexMainHeader;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
u8 data_relative_offset[4]; // relative to content index start
|
u8 data_relative_offset[4]; // relative to content index start
|
||||||
@ -76,14 +83,14 @@ typedef struct {
|
|||||||
u8 total_size_used[4]; // also no effect
|
u8 total_size_used[4]; // also no effect
|
||||||
u8 data_type[2]; // perhaps, does have effect and change with different data like on 0004000D tickets
|
u8 data_type[2]; // perhaps, does have effect and change with different data like on 0004000D tickets
|
||||||
u8 unknown[2]; // or padding
|
u8 unknown[2]; // or padding
|
||||||
} __attribute__((packed)) TicketContentIndexDataHeader;
|
} PACKED_ALIGN(1) TicketContentIndexDataHeader;
|
||||||
|
|
||||||
// data type == 3
|
// data type == 3
|
||||||
typedef struct {
|
typedef struct {
|
||||||
u8 unk[2]; // seemly has no meaning
|
u8 unk[2]; // seemly has no meaning
|
||||||
u8 indexoffset[2];
|
u8 indexoffset[2];
|
||||||
u8 rightsbitfield[0x80];
|
u8 rightsbitfield[0x80];
|
||||||
} __attribute__((packed)) TicketRightsField;
|
} PACKED_ALIGN(1) TicketRightsField;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
size_t count;
|
size_t count;
|
||||||
@ -91,8 +98,9 @@ typedef struct {
|
|||||||
} TicketRightsCheck;
|
} TicketRightsCheck;
|
||||||
|
|
||||||
u32 ValidateTicket(Ticket* ticket);
|
u32 ValidateTicket(Ticket* ticket);
|
||||||
|
u32 ValidateTwlTicket(Ticket* ticket);
|
||||||
u32 ValidateTicketSignature(Ticket* ticket);
|
u32 ValidateTicketSignature(Ticket* ticket);
|
||||||
u32 BuildFakeTicket(Ticket* ticket, u8* title_id);
|
u32 BuildFakeTicket(Ticket* ticket, const u8* title_id);
|
||||||
u32 GetTicketContentIndexSize(const Ticket* ticket);
|
u32 GetTicketContentIndexSize(const Ticket* ticket);
|
||||||
u32 GetTicketSize(const Ticket* ticket);
|
u32 GetTicketSize(const Ticket* ticket);
|
||||||
u32 BuildTicketCert(u8* tickcert);
|
u32 BuildTicketCert(u8* tickcert);
|
||||||
|
@ -26,15 +26,21 @@ u32 CryptTitleKey(TitleKeyEntry* tik, bool encrypt, bool devkit) {
|
|||||||
{0x75, 0x05, 0x52, 0xBF, 0xAA, 0x1C, 0x04, 0x07, 0x55, 0xC8, 0xD5, 0x9A, 0x55, 0xF9, 0xAD, 0x1F} , // 4
|
{0x75, 0x05, 0x52, 0xBF, 0xAA, 0x1C, 0x04, 0x07, 0x55, 0xC8, 0xD5, 0x9A, 0x55, 0xF9, 0xAD, 0x1F} , // 4
|
||||||
{0xAA, 0xDA, 0x4C, 0xA8, 0xF6, 0xE5, 0xA9, 0x77, 0xE0, 0xA0, 0xF9, 0xE4, 0x76, 0xCF, 0x0D, 0x63} , // 5
|
{0xAA, 0xDA, 0x4C, 0xA8, 0xF6, 0xE5, 0xA9, 0x77, 0xE0, 0xA0, 0xF9, 0xE4, 0x76, 0xCF, 0x0D, 0x63} , // 5
|
||||||
};
|
};
|
||||||
|
// From unknown source
|
||||||
|
static const u8 common_key_twl[16] __attribute__((aligned(16))) =
|
||||||
|
{0xAF, 0x1B, 0xF5, 0x16, 0xA8, 0x07, 0xD2, 0x1A, 0xEA, 0x45, 0x98, 0x4F, 0x04, 0x74, 0x28, 0x61}; // TWL
|
||||||
|
|
||||||
u32 mode = (encrypt) ? AES_CNT_TITLEKEY_ENCRYPT_MODE : AES_CNT_TITLEKEY_DECRYPT_MODE;
|
u32 mode = (encrypt) ? AES_CNT_TITLEKEY_ENCRYPT_MODE : AES_CNT_TITLEKEY_DECRYPT_MODE;
|
||||||
u8 ctr[16] = { 0 };
|
u8 ctr[16] = { 0 };
|
||||||
|
|
||||||
// setup key 0x3D // ctr
|
if (getbe16(tik->title_id) == 0x3) { // setup TWL key
|
||||||
if (tik->commonkey_idx >= 6) return 1;
|
setup_aeskey(0x11, (void*) common_key_twl);
|
||||||
if (!devkit) setup_aeskeyY(0x3D, (void*) common_keyy[tik->commonkey_idx]);
|
use_aeskey(0x11);
|
||||||
else setup_aeskey(0x3D, (void*) common_key_dev[tik->commonkey_idx]);
|
} else { // setup key 0x3D // ctr
|
||||||
use_aeskey(0x3D);
|
if (!devkit) setup_aeskeyY(0x3D, (void*) common_keyy[tik->commonkey_idx]);
|
||||||
|
else setup_aeskey(0x3D, (void*) common_key_dev[tik->commonkey_idx]);
|
||||||
|
use_aeskey(0x3D);
|
||||||
|
}
|
||||||
memcpy(ctr, tik->title_id, 8);
|
memcpy(ctr, tik->title_id, 8);
|
||||||
set_ctr(ctr);
|
set_ctr(ctr);
|
||||||
|
|
||||||
@ -54,6 +60,17 @@ u32 GetTitleKey(u8* titlekey, Ticket* ticket) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u32 SetTitleKey(const u8* titlekey, Ticket* ticket) {
|
||||||
|
TitleKeyEntry tik = { 0 };
|
||||||
|
memcpy(tik.title_id, ticket->title_id, 8);
|
||||||
|
memcpy(tik.titlekey, titlekey, 16);
|
||||||
|
tik.commonkey_idx = ticket->commonkey_idx;
|
||||||
|
|
||||||
|
if (CryptTitleKey(&tik, true, TICKET_DEVKIT(ticket)) != 0) return 1;
|
||||||
|
memcpy(ticket->titlekey, tik.titlekey, 16);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
u32 FindTicket(Ticket** ticket, u8* title_id, bool force_legit, bool emunand) {
|
u32 FindTicket(Ticket** ticket, u8* title_id, bool force_legit, bool emunand) {
|
||||||
const char* path_db = TICKDB_PATH(emunand); // EmuNAND / SysNAND
|
const char* path_db = TICKDB_PATH(emunand); // EmuNAND / SysNAND
|
||||||
char path_store[256] = { 0 };
|
char path_store[256] = { 0 };
|
||||||
@ -88,11 +105,11 @@ u32 FindTicket(Ticket** ticket, u8* title_id, bool force_legit, bool emunand) {
|
|||||||
|
|
||||||
u32 FindTitleKey(Ticket* ticket, u8* title_id) {
|
u32 FindTitleKey(Ticket* ticket, u8* title_id) {
|
||||||
bool found = false;
|
bool found = false;
|
||||||
TitleKeysInfo* tikdb = (TitleKeysInfo*) malloc(STD_BUFFER_SIZE); // more than enough
|
|
||||||
if (!tikdb) return 1;
|
|
||||||
|
|
||||||
// search for a titlekey inside encTitleKeys.bin / decTitleKeys.bin
|
// search for a titlekey inside encTitleKeys.bin / decTitleKeys.bin
|
||||||
// when found, add it to the ticket
|
// when found, add it to the ticket
|
||||||
|
TitleKeysInfo* tikdb = (TitleKeysInfo*) malloc(STD_BUFFER_SIZE); // more than enough
|
||||||
|
if (!tikdb) return 1;
|
||||||
for (u32 enc = 0; (enc <= 1) && !found; enc++) {
|
for (u32 enc = 0; (enc <= 1) && !found; enc++) {
|
||||||
u32 len = LoadSupportFile((enc) ? TIKDB_NAME_ENC : TIKDB_NAME_DEC, tikdb, STD_BUFFER_SIZE);
|
u32 len = LoadSupportFile((enc) ? TIKDB_NAME_ENC : TIKDB_NAME_DEC, tikdb, STD_BUFFER_SIZE);
|
||||||
|
|
||||||
@ -111,11 +128,31 @@ u32 FindTitleKey(Ticket* ticket, u8* title_id) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
free(tikdb);
|
free(tikdb);
|
||||||
|
|
||||||
|
// desperate measures - search in the internal ticket database
|
||||||
|
Ticket* ticket_tmp = NULL;
|
||||||
|
if (FindTicket(&ticket_tmp, title_id, false, false) == 0) {
|
||||||
|
memcpy(ticket->titlekey, ticket_tmp->titlekey, 16);
|
||||||
|
ticket->commonkey_idx = ticket_tmp->commonkey_idx;
|
||||||
|
free(ticket_tmp);
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
|
||||||
return (found) ? 0 : 1;
|
return (found) ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u32 FindTitleKeyForId(u8* titlekey, u8* title_id) {
|
||||||
|
TicketCommon tik;
|
||||||
|
|
||||||
|
if ((BuildFakeTicket((Ticket*) &tik, title_id) != 0) ||
|
||||||
|
(FindTitleKey((Ticket*) &tik, title_id) != 0) ||
|
||||||
|
(GetTitleKey(titlekey, (Ticket*) &tik) != 0))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
u32 AddTitleKeyToInfo(TitleKeysInfo* tik_info, TitleKeyEntry* tik_entry, bool decrypted_in, bool decrypted_out, bool devkit) {
|
u32 AddTitleKeyToInfo(TitleKeysInfo* tik_info, TitleKeyEntry* tik_entry, bool decrypted_in, bool decrypted_out, bool devkit) {
|
||||||
if (!tik_entry) { // no titlekey entry -> reset database
|
if (!tik_entry) { // no titlekey entry -> reset database
|
||||||
memset(tik_info, 0, 16);
|
memset(tik_info, 0, 16);
|
||||||
@ -141,3 +178,9 @@ u32 AddTicketToInfo(TitleKeysInfo* tik_info, Ticket* ticket, bool decrypt) { //
|
|||||||
tik.commonkey_idx = ticket->commonkey_idx;
|
tik.commonkey_idx = ticket->commonkey_idx;
|
||||||
return AddTitleKeyToInfo(tik_info, &tik, false, decrypt, TICKET_DEVKIT(ticket));
|
return AddTitleKeyToInfo(tik_info, &tik, false, decrypt, TICKET_DEVKIT(ticket));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u32 CryptTitleKeyInfo(TitleKeysInfo* tik_info, bool encrypt) {
|
||||||
|
for (u32 t = 0; t < tik_info->n_entries; t++)
|
||||||
|
CryptTitleKey(tik_info->entries + t, encrypt, false);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
@ -35,7 +35,10 @@ typedef struct {
|
|||||||
|
|
||||||
|
|
||||||
u32 GetTitleKey(u8* titlekey, Ticket* ticket);
|
u32 GetTitleKey(u8* titlekey, Ticket* ticket);
|
||||||
|
u32 SetTitleKey(const u8* titlekey, Ticket* ticket);
|
||||||
u32 FindTicket(Ticket** ticket, u8* title_id, bool force_legit, bool emunand);
|
u32 FindTicket(Ticket** ticket, u8* title_id, bool force_legit, bool emunand);
|
||||||
u32 FindTitleKey(Ticket* ticket, u8* title_id);
|
u32 FindTitleKey(Ticket* ticket, u8* title_id);
|
||||||
|
u32 FindTitleKeyForId(u8* titlekey, u8* title_id);
|
||||||
u32 AddTitleKeyToInfo(TitleKeysInfo* tik_info, TitleKeyEntry* tik_entry, bool decrypted_in, bool decrypted_out, bool devkit);
|
u32 AddTitleKeyToInfo(TitleKeysInfo* tik_info, TitleKeyEntry* tik_entry, bool decrypted_in, bool decrypted_out, bool devkit);
|
||||||
u32 AddTicketToInfo(TitleKeysInfo* tik_info, Ticket* ticket, bool decrypt);
|
u32 AddTicketToInfo(TitleKeysInfo* tik_info, Ticket* ticket, bool decrypt);
|
||||||
|
u32 CryptTitleKeyInfo(TitleKeysInfo* tik_info, bool encrypt);
|
||||||
|
@ -44,7 +44,9 @@ u32 BuildTitleInfoEntryTmd(TitleInfoEntry* tie, TitleMetaData* tmd, bool sd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// manual? dlp? save? (we need to properly check this later)
|
// manual? dlp? save? (we need to properly check this later)
|
||||||
if (((title_id >> 32) == 0x00040000) || ((title_id >> 32) == 0x00040010)) {
|
if (((title_id >> 32) == 0x00040000) ||
|
||||||
|
((title_id >> 32) == 0x0004000E) ||
|
||||||
|
((title_id >> 32) == 0x00040010)) {
|
||||||
if (has_idx1) tie->flags_0[0] = 0x1; // this may have a manual
|
if (has_idx1) tie->flags_0[0] = 0x1; // this may have a manual
|
||||||
if (has_idx2) tie->title_version |= (0xFFFF << 16); // this may have a dlp
|
if (has_idx2) tie->title_version |= (0xFFFF << 16); // this may have a dlp
|
||||||
if (getle32(tmd->save_size)) tie->flags_1[0] = 0x01; // this may have an sd save
|
if (getle32(tmd->save_size)) tie->flags_1[0] = 0x01; // this may have an sd save
|
||||||
|
@ -14,23 +14,27 @@ u32 ValidateTmd(TitleMetaData* tmd) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u32 ValidateTwlTmd(TitleMetaData* tmd) {
|
||||||
|
static const u8 magic[] = { TMD_SIG_TYPE_TWL };
|
||||||
|
if ((memcmp(tmd->sig_type, magic, sizeof(magic)) != 0) ||
|
||||||
|
(strncmp((char*) tmd->issuer, TMD_ISSUER_TWL, 0x40) != 0) ||
|
||||||
|
(getbe16(tmd->content_count) != 1))
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
u32 ValidateTmdSignature(TitleMetaData* tmd) {
|
u32 ValidateTmdSignature(TitleMetaData* tmd) {
|
||||||
static bool got_modexp = false;
|
Certificate cert;
|
||||||
static u32 mod[0x100 / 4] = { 0 };
|
|
||||||
static u32 exp = 0;
|
|
||||||
|
|
||||||
if (!got_modexp) {
|
// grab cert from certs.db
|
||||||
// grab mod/exp from cert from cert.db
|
if (LoadCertFromCertDb(&cert, (char*)(tmd->issuer)) != 0)
|
||||||
if (LoadCertFromCertDb(0x3C10, NULL, mod, &exp) == 0)
|
|
||||||
got_modexp = true;
|
|
||||||
else return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!RSA_setKey2048(3, mod, exp) ||
|
|
||||||
!RSA_verify2048((void*) &(tmd->signature), (void*) &(tmd->issuer), 0xC4))
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
return 0;
|
int ret = Certificate_VerifySignatureBlock(&cert, &(tmd->signature), 0x100, (void*)&(tmd->issuer), 0xC4, true);
|
||||||
|
|
||||||
|
Certificate_Cleanup(&cert);
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 VerifyTmd(TitleMetaData* tmd) {
|
u32 VerifyTmd(TitleMetaData* tmd) {
|
||||||
@ -112,19 +116,14 @@ u32 BuildTmdCert(u8* tmdcert) {
|
|||||||
0xC2, 0xE9, 0xCA, 0x93, 0x94, 0xF4, 0x29, 0xA0, 0x38, 0x54, 0x75, 0xFF, 0xAB, 0x6E, 0x8E, 0x71
|
0xC2, 0xE9, 0xCA, 0x93, 0x94, 0xF4, 0x29, 0xA0, 0x38, 0x54, 0x75, 0xFF, 0xAB, 0x6E, 0x8E, 0x71
|
||||||
};
|
};
|
||||||
|
|
||||||
// open certs.db file on SysNAND
|
static const char* const retail_issuers[] = {"Root-CA00000003-CP0000000b", "Root-CA00000003"};
|
||||||
FIL db;
|
static const char* const dev_issuers[] = {"Root-CA00000004-CP0000000a", "Root-CA00000004"};
|
||||||
UINT bytes_read;
|
|
||||||
if (f_open(&db, "1:/dbs/certs.db", FA_READ | FA_OPEN_EXISTING) != FR_OK)
|
size_t size = TMD_CDNCERT_SIZE;
|
||||||
|
if (BuildRawCertBundleFromCertDb(tmdcert, &size, !IS_DEVKIT ? retail_issuers : dev_issuers, 2) ||
|
||||||
|
size != TMD_CDNCERT_SIZE) {
|
||||||
return 1;
|
return 1;
|
||||||
// grab TMD cert from 3 offsets
|
}
|
||||||
f_lseek(&db, 0x3C10);
|
|
||||||
f_read(&db, tmdcert + 0x000, 0x300, &bytes_read);
|
|
||||||
f_lseek(&db, 0x0C10);
|
|
||||||
f_read(&db, tmdcert + 0x300, 0x1F0, &bytes_read);
|
|
||||||
f_lseek(&db, 0x3A00);
|
|
||||||
f_read(&db, tmdcert + 0x4F0, 0x210, &bytes_read);
|
|
||||||
f_close(&db);
|
|
||||||
|
|
||||||
// check the certificate hash
|
// check the certificate hash
|
||||||
u8 cert_hash[0x20];
|
u8 cert_hash[0x20];
|
||||||
|
@ -2,17 +2,22 @@
|
|||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
#define TMD_MAX_CONTENTS 1000 // 383 // theme CIAs contain maximum 100 themes + 1 index content
|
#define TMD_MAX_CONTENTS TITLE_MAX_CONTENTS // 1024 // 383 // theme CIAs contain maximum 100 themes + 1 index content
|
||||||
|
|
||||||
#define TMD_SIZE_MIN sizeof(TitleMetaData)
|
#define TMD_SIZE_MIN sizeof(TitleMetaData)
|
||||||
#define TMD_SIZE_MAX (sizeof(TitleMetaData) + (TMD_MAX_CONTENTS*sizeof(TmdContentChunk)))
|
#define TMD_SIZE_MAX (sizeof(TitleMetaData) + (TMD_MAX_CONTENTS*sizeof(TmdContentChunk)))
|
||||||
#define TMD_SIZE_N(n) (sizeof(TitleMetaData) + (n*sizeof(TmdContentChunk)))
|
#define TMD_SIZE_N(n) (sizeof(TitleMetaData) + (n*sizeof(TmdContentChunk)))
|
||||||
|
#define TMD_SIZE_STUB (TMD_SIZE_MIN - (0x20 + (64 * sizeof(TmdContentInfo))))
|
||||||
|
#define TMD_SIZE_TWL (TMD_SIZE_STUB + 0x24)
|
||||||
#define TMD_CDNCERT_SIZE 0x700
|
#define TMD_CDNCERT_SIZE 0x700
|
||||||
|
|
||||||
#define TMD_ISSUER "Root-CA00000003-CP0000000b"
|
#define TMD_ISSUER "Root-CA00000003-CP0000000b"
|
||||||
#define TMD_ISSUER_DEV "Root-CA00000004-CP0000000a"
|
#define TMD_ISSUER_DEV "Root-CA00000004-CP0000000a"
|
||||||
#define TMD_SIG_TYPE 0x00, 0x01, 0x00, 0x04 // RSA_2048 SHA256
|
#define TMD_SIG_TYPE 0x00, 0x01, 0x00, 0x04 // RSA_2048 SHA256
|
||||||
|
|
||||||
|
#define TMD_ISSUER_TWL "Root-CA00000001-CP00000007"
|
||||||
|
#define TMD_SIG_TYPE_TWL 0x00, 0x01, 0x00, 0x01 // RSA_2048 SHA1
|
||||||
|
|
||||||
#define DLC_TID_HIGH 0x00, 0x04, 0x00, 0x8C // title id high for DLC
|
#define DLC_TID_HIGH 0x00, 0x04, 0x00, 0x8C // title id high for DLC
|
||||||
|
|
||||||
// from: https://github.com/profi200/Project_CTR/blob/02159e17ee225de3f7c46ca195ff0f9ba3b3d3e4/ctrtool/tmd.h#L18-L59;
|
// from: https://github.com/profi200/Project_CTR/blob/02159e17ee225de3f7c46ca195ff0f9ba3b3d3e4/ctrtool/tmd.h#L18-L59;
|
||||||
@ -58,6 +63,7 @@ typedef struct {
|
|||||||
} __attribute__((packed, aligned(4))) TitleMetaData;
|
} __attribute__((packed, aligned(4))) TitleMetaData;
|
||||||
|
|
||||||
u32 ValidateTmd(TitleMetaData* tmd);
|
u32 ValidateTmd(TitleMetaData* tmd);
|
||||||
|
u32 ValidateTwlTmd(TitleMetaData* tmd);
|
||||||
u32 ValidateTmdSignature(TitleMetaData* tmd);
|
u32 ValidateTmdSignature(TitleMetaData* tmd);
|
||||||
u32 VerifyTmd(TitleMetaData* tmd);
|
u32 VerifyTmd(TitleMetaData* tmd);
|
||||||
u32 GetTmdCtr(u8* ctr, TmdContentChunk* chunk);
|
u32 GetTmdCtr(u8* ctr, TmdContentChunk* chunk);
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <arm.h>
|
||||||
#include "card_spi.h"
|
#include "card_spi.h"
|
||||||
#include <spi.h>
|
#include <spi.h>
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
@ -57,58 +58,56 @@ int CardSPIWriteSaveData_24bit_erase_program(CardSPIType type, u32 offset, const
|
|||||||
int CardSPIEraseSector_emulated(CardSPIType type, u32 offset);
|
int CardSPIEraseSector_emulated(CardSPIType type, u32 offset);
|
||||||
int CardSPIEraseSector_real(CardSPIType type, u32 offset);
|
int CardSPIEraseSector_real(CardSPIType type, u32 offset);
|
||||||
|
|
||||||
const CardSPITypeData EEPROM_512B_ = { CardSPIEnableWriting_512B, CardSPIReadSaveData_9bit, CardSPIWriteSaveData_9bit, CardSPIEraseSector_emulated, 0xffffff, 1 << 9, 16, 16, 16, 0, 0, 0 };
|
const CardSPITypeData EEPROM_512B = { CardSPIEnableWriting_512B, CardSPIReadSaveData_9bit, CardSPIWriteSaveData_9bit, CardSPIEraseSector_emulated, 0xffff, 1 << 9, 16, 16, 16, 0, 0, 0 };
|
||||||
|
|
||||||
const CardSPITypeData EEPROM_DUMMY = { CardSPIEnableWriting_regular, CardSPIReadSaveData_16bit, CardSPIWriteSaveData_16bit, CardSPIEraseSector_emulated, 0xffffff, UINT32_MAX, 1, 1, 1, SPI_EEPROM_CMD_WRITE, 0, 0 };
|
const CardSPITypeData EEPROM_DUMMY = { CardSPIEnableWriting_regular, CardSPIReadSaveData_16bit, CardSPIWriteSaveData_16bit, CardSPIEraseSector_emulated, 0xffff, UINT32_MAX, 1, 1, 1, SPI_EEPROM_CMD_WRITE, 0, 0 };
|
||||||
const CardSPITypeData EEPROMTypes[] = {
|
const CardSPITypeData EEPROMTypes[] = {
|
||||||
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_16bit, CardSPIWriteSaveData_16bit, CardSPIEraseSector_emulated, 0xffffff, 1 << 13, 32, 32, 32, SPI_EEPROM_CMD_WRITE, 0, 0}, // EEPROM 8 KB
|
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_16bit, CardSPIWriteSaveData_16bit, CardSPIEraseSector_emulated, 0xffff, 1 << 13, 32, 32, 32, SPI_EEPROM_CMD_WRITE, 0, 0}, // EEPROM 8 KB
|
||||||
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_16bit, CardSPIWriteSaveData_16bit, CardSPIEraseSector_emulated, 0xffffff, 1 << 16, 128, 128, 128, SPI_EEPROM_CMD_WRITE, 0, 0}, // EEPROM 64 KB
|
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_16bit, CardSPIWriteSaveData_16bit, CardSPIEraseSector_emulated, 0xffff, 1 << 16, 128, 128, 128, SPI_EEPROM_CMD_WRITE, 0, 0}, // EEPROM 64 KB
|
||||||
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_write, CardSPIEraseSector_emulated, 0xffffff, 1 << 17, 256, 256, 256, SPI_EEPROM_CMD_WRITE, 0, 0}, // EEPROM 128 KB
|
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_write, CardSPIEraseSector_emulated, 0xffff, 1 << 17, 256, 256, 256, SPI_EEPROM_CMD_WRITE, 0, 0}, // EEPROM 128 KB
|
||||||
};
|
};
|
||||||
|
|
||||||
const CardSPITypeData FLASH_DUMMY = { NULL, CardSPIReadSaveData_24bit, NULL, NULL, 0x0, 0, 0, 0, 0, 0, 0, 0 };
|
const CardSPITypeData FLASH_DUMMY = { NULL, CardSPIReadSaveData_24bit, NULL, NULL, 0x0, 0, 0, 0, 0, 0, 0, 0 };
|
||||||
const CardSPITypeData flashTypes[] = {
|
const CardSPITypeData flashTypes[] = {
|
||||||
// NTR/TWL
|
// NTR/TWL
|
||||||
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_write, CardSPIEraseSector_real, 0x204012, 1 << 18, 65536, 256, 256, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
|
|
||||||
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0x621600, 1 << 18, 65536, 256, 65536, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
|
// Most common flash chip in DS games, in 3 different sizes (256kB: ST M45PE20, 512kB: ST M45PE40, 1MB: ST M45PE80)
|
||||||
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_write, CardSPIEraseSector_real, 0x204013, 1 << 19, 65536, 256, 256, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
|
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_write, CardSPIEraseSector_real, 0x2040, 0, 65536, 256, 256, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
|
||||||
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_write, CardSPIEraseSector_real, 0x621100, 1 << 19, 65536, 256, 256, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
|
|
||||||
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_write, CardSPIEraseSector_real, 0x204014, 1 << 20, 65536, 256, 256, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
|
// Found in Mario Kart, for example (always 256kB, Sanyo LE25FW203T)
|
||||||
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0x202017, 1 << 23, 65536, 256, 65536, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
|
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0x6216, 1 << 18, 65536, 256, 65536, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
|
||||||
// CTR
|
|
||||||
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0xC22211, 1 << 17, 4096, 32, 4096, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_MXIC_SE },
|
// Found in some copies of Pokemon Diamond/Pearl (always 512kB, Sanyo ??)
|
||||||
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0xC22213, 1 << 19, 4096, 32, 4096, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_MXIC_SE },
|
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_write, CardSPIEraseSector_real, 0x6211, 1 << 19, 65536, 256, 256, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
|
||||||
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0xC22214, 1 << 20, 4096, 32, 4096, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_MXIC_SE },
|
|
||||||
|
// Found in Art Academy, and maybe some other ones (8MB: ST M25P64)
|
||||||
|
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0x2020, 0, 65536, 256, 65536, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
|
||||||
|
|
||||||
|
// Found in some DS bootlegs (ST M25PE40VP in my case)
|
||||||
|
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0x2080, 0, 65536, 256, 65536, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
|
||||||
|
|
||||||
|
// This is the most common type of flash 3DS cartridges. Not normally found in DS ones, but check anyway. (Custom MXIC chips)
|
||||||
|
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0xC222, 0, 4096, 32, 4096, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_MXIC_SE },
|
||||||
|
|
||||||
|
// Found this in some copies of ArtAcademy, I think it's something from MXIC (Thank you for the help @FerozElMejor on Discord)
|
||||||
|
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0xC220, 0, 4096, 32, 4096, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_MXIC_SE },
|
||||||
};
|
};
|
||||||
|
|
||||||
const CardSPITypeData * const EEPROM_512B = &EEPROM_512B_;
|
const CardSPITypeData * const FLASH_CTR_GENERIC = flashTypes + 5;
|
||||||
|
|
||||||
const CardSPITypeData * const EEPROM_8KB = EEPROMTypes + 0;
|
|
||||||
const CardSPITypeData * const EEPROM_64KB = EEPROMTypes + 1;
|
|
||||||
const CardSPITypeData * const EEPROM_128KB = EEPROMTypes + 2;
|
|
||||||
|
|
||||||
const CardSPITypeData * const FLASH_256KB_1 = flashTypes + 0;
|
|
||||||
const CardSPITypeData * const FLASH_256KB_2 = flashTypes + 1;
|
|
||||||
const CardSPITypeData * const FLASH_512KB_1 = flashTypes + 2;
|
|
||||||
const CardSPITypeData * const FLASH_512KB_2 = flashTypes + 3;
|
|
||||||
const CardSPITypeData * const FLASH_1MB = flashTypes + 4;
|
|
||||||
const CardSPITypeData * const FLASH_8MB = flashTypes + 5;
|
|
||||||
|
|
||||||
const CardSPITypeData * const FLASH_128KB_CTR = flashTypes + 6;
|
|
||||||
const CardSPITypeData * const FLASH_512KB_CTR = flashTypes + 7;
|
|
||||||
const CardSPITypeData * const FLASH_1MB_CTR = flashTypes + 8;
|
|
||||||
|
|
||||||
#define REG_CFG9_CARDCTL *((vu16*)0x1000000C)
|
#define REG_CFG9_CARDCTL *((vu16*)0x1000000C)
|
||||||
#define CARDCTL_SPICARD (1u<<8)
|
#define CARDCTL_SPICARD (1u<<8)
|
||||||
|
|
||||||
int CardSPIWriteRead(CardSPIType type, const void* cmd, u32 cmdSize, void* answer, u32 answerSize, const void* data, u32 dataSize) {
|
int CardSPIWriteRead(bool infrared, const void* cmd, u32 cmdSize, void* answer, u32 answerSize, const void* data, u32 dataSize) {
|
||||||
u32 headerFooterVal = 0;
|
u32 headerFooterVal = 0;
|
||||||
|
|
||||||
REG_CFG9_CARDCTL |= CARDCTL_SPICARD;
|
REG_CFG9_CARDCTL |= CARDCTL_SPICARD;
|
||||||
|
|
||||||
if (type.infrared) {
|
if (infrared) {
|
||||||
SPI_XferInfo irXfer = { &headerFooterVal, 1, false };
|
SPI_XferInfo irXfer = { &headerFooterVal, 1, false };
|
||||||
SPI_DoXfer(SPI_DEV_CART_IR, &irXfer, 1, false);
|
SPI_DoXfer(SPI_DEV_CART_IR, &irXfer, 1, false);
|
||||||
|
// Wait as specified by GBATEK (0x800 cycles at 33 MHz)
|
||||||
|
ARM_WaitCycles(0x800 * 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPI_XferInfo transfers[3] = {
|
SPI_XferInfo transfers[3] = {
|
||||||
@ -120,16 +119,21 @@ int CardSPIWriteRead(CardSPIType type, const void* cmd, u32 cmdSize, void* answe
|
|||||||
|
|
||||||
REG_CFG9_CARDCTL &= ~CARDCTL_SPICARD;
|
REG_CFG9_CARDCTL &= ~CARDCTL_SPICARD;
|
||||||
|
|
||||||
|
if (infrared) {
|
||||||
|
// Wait as specified by GBATEK (0x800 cycles at 33 MHz)
|
||||||
|
ARM_WaitCycles(0x800 * 4);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CardSPIWaitWriteEnd(CardSPIType type, u32 timeout) {
|
int CardSPIWaitWriteEnd(bool infrared, u32 timeout) {
|
||||||
u8 cmd = SPI_CMD_RDSR, statusReg = 0;
|
u8 cmd = SPI_CMD_RDSR, statusReg = 0;
|
||||||
int res = 0;
|
int res = 0;
|
||||||
u64 time_start = timer_start();
|
u64 time_start = timer_start();
|
||||||
|
|
||||||
do {
|
do {
|
||||||
res = CardSPIWriteRead(type, &cmd, 1, &statusReg, 1, 0, 0);
|
res = CardSPIWriteRead(infrared, &cmd, 1, &statusReg, 1, 0, 0);
|
||||||
if (res) return res;
|
if (res) return res;
|
||||||
if (timer_msec(time_start) > timeout) return 1;
|
if (timer_msec(time_start) > timeout) return 1;
|
||||||
} while(statusReg & SPI_FLG_WIP);
|
} while(statusReg & SPI_FLG_WIP);
|
||||||
@ -139,18 +143,18 @@ int CardSPIWaitWriteEnd(CardSPIType type, u32 timeout) {
|
|||||||
|
|
||||||
int CardSPIEnableWriting_512B(CardSPIType type) {
|
int CardSPIEnableWriting_512B(CardSPIType type) {
|
||||||
u8 cmd = SPI_CMD_WREN;
|
u8 cmd = SPI_CMD_WREN;
|
||||||
return CardSPIWriteRead(type, &cmd, 1, NULL, 0, 0, 0);
|
return CardSPIWriteRead(type.infrared, &cmd, 1, NULL, 0, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int CardSPIEnableWriting_regular(CardSPIType type) {
|
int CardSPIEnableWriting_regular(CardSPIType type) {
|
||||||
u8 cmd = SPI_CMD_WREN, statusReg = 0;
|
u8 cmd = SPI_CMD_WREN, statusReg = 0;
|
||||||
int res = CardSPIWriteRead(type, &cmd, 1, NULL, 0, 0, 0);
|
int res = CardSPIWriteRead(type.infrared, &cmd, 1, NULL, 0, 0, 0);
|
||||||
|
|
||||||
if (res) return res;
|
if (res) return res;
|
||||||
cmd = SPI_CMD_RDSR;
|
cmd = SPI_CMD_RDSR;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
res = CardSPIWriteRead(type, &cmd, 1, &statusReg, 1, 0, 0);
|
res = CardSPIWriteRead(type.infrared, &cmd, 1, &statusReg, 1, 0, 0);
|
||||||
if (res) return res;
|
if (res) return res;
|
||||||
} while(statusReg & ~SPI_FLG_WEL);
|
} while(statusReg & ~SPI_FLG_WEL);
|
||||||
|
|
||||||
@ -165,24 +169,24 @@ int CardSPIEnableWriting(CardSPIType type) {
|
|||||||
int _SPIWriteTransaction(CardSPIType type, void* cmd, u32 cmdSize, const void* data, u32 dataSize) {
|
int _SPIWriteTransaction(CardSPIType type, void* cmd, u32 cmdSize, const void* data, u32 dataSize) {
|
||||||
int res;
|
int res;
|
||||||
if ((res = CardSPIEnableWriting(type))) return res;
|
if ((res = CardSPIEnableWriting(type))) return res;
|
||||||
if ((res = CardSPIWriteRead(type, cmd, cmdSize, NULL, 0, (void*) ((u8*) data), dataSize))) return res;
|
if ((res = CardSPIWriteRead(type.infrared, cmd, cmdSize, NULL, 0, (void*) ((u8*) data), dataSize))) return res;
|
||||||
return CardSPIWaitWriteEnd(type, 1000);
|
return CardSPIWaitWriteEnd(type.infrared, 10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
int CardSPIReadJEDECIDAndStatusReg(CardSPIType type, u32* id, u8* statusReg) {
|
int CardSPIReadJEDECIDAndStatusReg(bool infrared, u32* id, u8* statusReg) {
|
||||||
u8 cmd = SPI_FLASH_CMD_RDID;
|
u8 cmd = SPI_FLASH_CMD_RDID;
|
||||||
u8 reg = 0;
|
u8 reg = 0;
|
||||||
u8 idbuf[3] = { 0 };
|
u8 idbuf[3] = { 0 };
|
||||||
u32 id_ = 0;
|
u32 id_ = 0;
|
||||||
int res = CardSPIWaitWriteEnd(type, 0);
|
int res = CardSPIWaitWriteEnd(infrared, 10);
|
||||||
if (res) return res;
|
if (res) return res;
|
||||||
|
|
||||||
if ((res = CardSPIWriteRead(type, &cmd, 1, idbuf, 3, 0, 0))) return res;
|
if ((res = CardSPIWriteRead(infrared, &cmd, 1, idbuf, 3, 0, 0))) return res;
|
||||||
|
|
||||||
id_ = (idbuf[0] << 16) | (idbuf[1] << 8) | idbuf[2];
|
id_ = (idbuf[0] << 16) | (idbuf[1] << 8) | idbuf[2];
|
||||||
cmd = SPI_CMD_RDSR;
|
cmd = SPI_CMD_RDSR;
|
||||||
|
|
||||||
if ((res = CardSPIWriteRead(type, &cmd, 1, ®, 1, 0, 0))) return res;
|
if ((res = CardSPIWriteRead(infrared, &cmd, 1, ®, 1, 0, 0))) return res;
|
||||||
|
|
||||||
if (id) *id = id_;
|
if (id) *id = id_;
|
||||||
if (statusReg) *statusReg = reg;
|
if (statusReg) *statusReg = reg;
|
||||||
@ -202,7 +206,19 @@ u32 CardSPIGetEraseSize(CardSPIType type) {
|
|||||||
|
|
||||||
u32 CardSPIGetCapacity(CardSPIType type) {
|
u32 CardSPIGetCapacity(CardSPIType type) {
|
||||||
if (type.chip == NO_CHIP) return 0;
|
if (type.chip == NO_CHIP) return 0;
|
||||||
return type.chip->capacity;
|
if (type.chip->capacity != 0) {
|
||||||
|
return type.chip->capacity;
|
||||||
|
} else {
|
||||||
|
u32 jedecid;
|
||||||
|
CardSPIReadJEDECIDAndStatusReg(type.infrared, &jedecid, NULL);
|
||||||
|
u8 size_exponent = jedecid & 0xff;
|
||||||
|
if ((size_exponent >= 8) && (size_exponent <= 24)) {
|
||||||
|
// Thanks to @wwylele for pointing this out
|
||||||
|
return 1 << size_exponent;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int CardSPIWriteSaveData_9bit(CardSPIType type, u32 offset, const void* data, u32 size) {
|
int CardSPIWriteSaveData_9bit(CardSPIType type, u32 offset, const void* data, u32 size) {
|
||||||
@ -256,7 +272,7 @@ int CardSPIWriteSaveData_24bit_erase_program(CardSPIType type, u32 offset, const
|
|||||||
if (!(res = _SPIWriteTransaction(type, cmd, 4, (void*) ((u8*) data - offset + pos), pageSize))) {
|
if (!(res = _SPIWriteTransaction(type, cmd, 4, (void*) ((u8*) data - offset + pos), pageSize))) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
CardSPIWriteRead(type, "\x04", 1, NULL, 0, NULL, 0);
|
CardSPIWriteRead(type.infrared, "\x04", 1, NULL, 0, NULL, 0);
|
||||||
}
|
}
|
||||||
if(res) {
|
if(res) {
|
||||||
free(newData);
|
free(newData);
|
||||||
@ -272,13 +288,12 @@ int CardSPIWriteSaveData(CardSPIType type, u32 offset, const void* data, u32 siz
|
|||||||
if (type.chip == NO_CHIP) return 1;
|
if (type.chip == NO_CHIP) return 1;
|
||||||
|
|
||||||
if (size == 0) return 0;
|
if (size == 0) return 0;
|
||||||
size = min(size, CardSPIGetCapacity(type) - offset);
|
|
||||||
u32 end = offset + size;
|
u32 end = offset + size;
|
||||||
u32 pos = offset;
|
u32 pos = offset;
|
||||||
u32 writeSize = type.chip->writeSize;
|
u32 writeSize = type.chip->writeSize;
|
||||||
if (writeSize == 0) return 0xC8E13404;
|
if (writeSize == 0) return 0xC8E13404;
|
||||||
|
|
||||||
int res = CardSPIWaitWriteEnd(type, 1000);
|
int res = CardSPIWaitWriteEnd(type.infrared, 1000);
|
||||||
if (res) return res;
|
if (res) return res;
|
||||||
|
|
||||||
while(pos < end) {
|
while(pos < end) {
|
||||||
@ -303,23 +318,23 @@ int CardSPIReadSaveData_9bit(CardSPIType type, u32 pos, void* data, u32 size) {
|
|||||||
|
|
||||||
u32 read = 0;
|
u32 read = 0;
|
||||||
if (pos < 0x100) {
|
if (pos < 0x100) {
|
||||||
u32 len = 0x100 - pos;
|
u32 len = min(0x100 - pos, size);
|
||||||
cmd[0] = SPI_512B_EEPROM_CMD_RDLO;
|
cmd[0] = SPI_512B_EEPROM_CMD_RDLO;
|
||||||
cmd[1] = (u8) pos;
|
cmd[1] = (u8) pos;
|
||||||
|
|
||||||
int res = CardSPIWriteRead(type, cmd, cmdSize, data, len, NULL, 0);
|
int res = CardSPIWriteRead(type.infrared, cmd, cmdSize, data, len, NULL, 0);
|
||||||
if (res) return res;
|
if (res) return res;
|
||||||
|
|
||||||
read += len;
|
read += len;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (end >= 0x100) {
|
if (end >= 0x100) {
|
||||||
u32 len = end - 0x100;
|
u32 len = min(end - 0x100, size);
|
||||||
|
|
||||||
cmd[0] = SPI_512B_EEPROM_CMD_RDHI;
|
cmd[0] = SPI_512B_EEPROM_CMD_RDHI;
|
||||||
cmd[1] = (u8)(pos + read);
|
cmd[1] = (u8)(pos + read);
|
||||||
|
|
||||||
int res = CardSPIWriteRead(type, cmd, cmdSize, (void*)((u8*)data + read), len, NULL, 0);
|
int res = CardSPIWriteRead(type.infrared, cmd, cmdSize, (void*)((u8*)data + read), len, NULL, 0);
|
||||||
|
|
||||||
if (res) return res;
|
if (res) return res;
|
||||||
}
|
}
|
||||||
@ -330,13 +345,13 @@ int CardSPIReadSaveData_9bit(CardSPIType type, u32 pos, void* data, u32 size) {
|
|||||||
int CardSPIReadSaveData_16bit(CardSPIType type, u32 offset, void* data, u32 size) {
|
int CardSPIReadSaveData_16bit(CardSPIType type, u32 offset, void* data, u32 size) {
|
||||||
u8 cmd[3] = { SPI_CMD_READ, (u8)(offset >> 8), (u8) offset };
|
u8 cmd[3] = { SPI_CMD_READ, (u8)(offset >> 8), (u8) offset };
|
||||||
|
|
||||||
return CardSPIWriteRead(type, cmd, 3, data, size, NULL, 0);
|
return CardSPIWriteRead(type.infrared, cmd, 3, data, size, NULL, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int CardSPIReadSaveData_24bit(CardSPIType type, u32 offset, void* data, u32 size) {
|
int CardSPIReadSaveData_24bit(CardSPIType type, u32 offset, void* data, u32 size) {
|
||||||
u8 cmd[4] = { SPI_CMD_READ, (u8)(offset >> 16), (u8)(offset >> 8), (u8) offset };
|
u8 cmd[4] = { SPI_CMD_READ, (u8)(offset >> 16), (u8)(offset >> 8), (u8) offset };
|
||||||
|
|
||||||
return CardSPIWriteRead(type, cmd, 4, data, size, NULL, 0);
|
return CardSPIWriteRead(type.infrared, cmd, 4, data, size, NULL, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int CardSPIReadSaveData(CardSPIType type, u32 offset, void* data, u32 size) {
|
int CardSPIReadSaveData(CardSPIType type, u32 offset, void* data, u32 size) {
|
||||||
@ -344,11 +359,9 @@ int CardSPIReadSaveData(CardSPIType type, u32 offset, void* data, u32 size) {
|
|||||||
|
|
||||||
if (size == 0) return 0;
|
if (size == 0) return 0;
|
||||||
|
|
||||||
int res = CardSPIWaitWriteEnd(type, 1000);
|
int res = CardSPIWaitWriteEnd(type.infrared, 1000);
|
||||||
if (res) return res;
|
if (res) return res;
|
||||||
|
|
||||||
size = (size <= CardSPIGetCapacity(type) - offset) ? size : CardSPIGetCapacity(type) - offset;
|
|
||||||
|
|
||||||
return type.chip->readSaveData(type, offset, data, size);
|
return type.chip->readSaveData(type, offset, data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -367,7 +380,7 @@ int CardSPIEraseSector_emulated(CardSPIType type, u32 offset) {
|
|||||||
int CardSPIEraseSector_real(CardSPIType type, u32 offset) {
|
int CardSPIEraseSector_real(CardSPIType type, u32 offset) {
|
||||||
u8 cmd[4] = { type.chip->eraseCommand, (u8)(offset >> 16), (u8)(offset >> 8), (u8) offset };
|
u8 cmd[4] = { type.chip->eraseCommand, (u8)(offset >> 16), (u8)(offset >> 8), (u8) offset };
|
||||||
|
|
||||||
int res = CardSPIWaitWriteEnd(type, 10000);
|
int res = CardSPIWaitWriteEnd(type.infrared, 10000);
|
||||||
if (res) return res;
|
if (res) return res;
|
||||||
|
|
||||||
return _SPIWriteTransaction(type, cmd, 4, NULL, 0);
|
return _SPIWriteTransaction(type, cmd, 4, NULL, 0);
|
||||||
@ -438,28 +451,21 @@ int _SPIIsDataMirrored(CardSPIType type, int size, bool* mirrored) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CardSPIGetCardSPIType(CardSPIType* type, bool infrared) {
|
CardSPIType CardSPIGetCardSPIType(bool infrared) {
|
||||||
u8 sr = 0;
|
u8 sr = 0;
|
||||||
u32 jedec = 0;
|
u32 jedec = 0;
|
||||||
CardSPIType t = {NO_CHIP, infrared};
|
CardSPIType t = {NO_CHIP, infrared};
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
if(infrared) {
|
res = CardSPIReadJEDECIDAndStatusReg(infrared, &jedec, &sr);
|
||||||
// Infrared carts currently not supported, need additional handling!
|
if (res) return (CardSPIType) {NO_CHIP, false};
|
||||||
*type = (CardSPIType) {NO_CHIP, true};
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
res = CardSPIReadJEDECIDAndStatusReg(t, &jedec, &sr);
|
|
||||||
if (res) return res;
|
|
||||||
|
|
||||||
if ((sr & 0xfd) == 0x00 && (jedec != 0x00ffffff)) { t.chip = &FLASH_DUMMY; }
|
if ((sr & 0xfd) == 0x00 && (jedec != 0x00ffffff)) { t.chip = &FLASH_DUMMY; }
|
||||||
if ((sr & 0xfd) == 0xF0 && (jedec == 0x00ffffff)) { *type = (CardSPIType) { EEPROM_512B, false }; return 0; }
|
if ((sr & 0xfd) == 0xF0 && (jedec == 0x00ffffff)) { return (CardSPIType) { &EEPROM_512B, false }; }
|
||||||
if ((sr & 0xfd) == 0x00 && (jedec == 0x00ffffff)) { t = (CardSPIType) { &EEPROM_DUMMY, false }; }
|
if ((sr & 0xfd) == 0x00 && (jedec == 0x00ffffff)) { t = (CardSPIType) { &EEPROM_DUMMY, false }; }
|
||||||
|
|
||||||
if(t.chip == NO_CHIP) {
|
if(t.chip == NO_CHIP) {
|
||||||
*type = (CardSPIType) {NO_CHIP, false};
|
return (CardSPIType) {NO_CHIP, false};
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (t.chip == &EEPROM_DUMMY) {
|
if (t.chip == &EEPROM_DUMMY) {
|
||||||
@ -467,24 +473,20 @@ int CardSPIGetCardSPIType(CardSPIType* type, bool infrared) {
|
|||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
for(i = 0; i < sizeof(EEPROMTypes) / sizeof(CardSPITypeData) - 1; i++) {
|
for(i = 0; i < sizeof(EEPROMTypes) / sizeof(CardSPITypeData) - 1; i++) {
|
||||||
if ((res = _SPIIsDataMirrored(t, CardSPIGetCapacity((CardSPIType) {EEPROMTypes + i, false}), &mirrored))) return res;
|
if ((res = _SPIIsDataMirrored(t, CardSPIGetCapacity((CardSPIType) {EEPROMTypes + i, false}), &mirrored))) return (CardSPIType) {NO_CHIP, false};
|
||||||
if (mirrored) {
|
if (mirrored) {
|
||||||
*type = (CardSPIType) {EEPROMTypes + i, false};
|
return (CardSPIType) {EEPROMTypes + i, false};
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*type = (CardSPIType) { EEPROMTypes + i, false };
|
return (CardSPIType) { EEPROMTypes + i, false };
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for(size_t i = 0; i < sizeof(flashTypes) / sizeof(CardSPITypeData); i++) {
|
for(size_t i = 0; i < sizeof(flashTypes) / sizeof(CardSPITypeData); i++) {
|
||||||
if (flashTypes[i].jedecId == jedec) {
|
if (flashTypes[i].jedecId == (jedec >> 8)) {
|
||||||
*type = (CardSPIType) { flashTypes + i, infrared };
|
return (CardSPIType) { flashTypes + i, infrared };
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*type = (CardSPIType) { NO_CHIP, infrared };
|
return (CardSPIType) { NO_CHIP, infrared };
|
||||||
return 0;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ struct CardSPITypeData {
|
|||||||
int (*readSaveData) (CardSPIType type, u32 offset, void* data, u32 size);
|
int (*readSaveData) (CardSPIType type, u32 offset, void* data, u32 size);
|
||||||
int (*writeSaveData) (CardSPIType type, u32 offset, const void* data, u32 size);
|
int (*writeSaveData) (CardSPIType type, u32 offset, const void* data, u32 size);
|
||||||
int (*eraseSector) (CardSPIType type, u32 offset);
|
int (*eraseSector) (CardSPIType type, u32 offset);
|
||||||
u32 jedecId;
|
u16 jedecId;
|
||||||
u32 capacity;
|
u32 capacity;
|
||||||
u32 eraseSize;
|
u32 eraseSize;
|
||||||
u32 pageSize;
|
u32 pageSize;
|
||||||
@ -49,28 +49,13 @@ struct CardSPITypeData {
|
|||||||
|
|
||||||
#define NO_CHIP NULL
|
#define NO_CHIP NULL
|
||||||
|
|
||||||
extern const CardSPITypeData * const EEPROM_512B;
|
extern const CardSPITypeData * const FLASH_CTR_GENERIC; // Handles each 3ds cartridge the exact same
|
||||||
|
|
||||||
extern const CardSPITypeData * const EEPROM_8KB;
|
int CardSPIWriteRead(bool infrared, const void* cmd, u32 cmdSize, void* answer, u32 answerSize, const void* data, u32 dataSize);
|
||||||
extern const CardSPITypeData * const EEPROM_64KB;
|
int CardSPIWaitWriteEnd(bool infrared, u32 timeout);
|
||||||
extern const CardSPITypeData * const EEPROM_128KB;
|
|
||||||
|
|
||||||
extern const CardSPITypeData * const FLASH_256KB_1;
|
|
||||||
extern const CardSPITypeData * const FLASH_256KB_2;
|
|
||||||
extern const CardSPITypeData * const FLASH_512KB_1;
|
|
||||||
extern const CardSPITypeData * const FLASH_512KB_2;
|
|
||||||
extern const CardSPITypeData * const FLASH_1MB;
|
|
||||||
extern const CardSPITypeData * const FLASH_8MB;
|
|
||||||
|
|
||||||
extern const CardSPITypeData * const FLASH_128KB_CTR; // Most common, including Ocarina of time 3D
|
|
||||||
extern const CardSPITypeData * const FLASH_512KB_CTR; // Also common, including Detective Pikachu
|
|
||||||
extern const CardSPITypeData * const FLASH_1MB_CTR; // For example Pokemon Ultra Sun
|
|
||||||
|
|
||||||
int CardSPIWriteRead(CardSPIType type, const void* cmd, u32 cmdSize, void* answer, u32 answerSize, const void* data, u32 dataSize);
|
|
||||||
int CardSPIWaitWriteEnd(CardSPIType type, u32 timeout);
|
|
||||||
int CardSPIEnableWriting(CardSPIType type);
|
int CardSPIEnableWriting(CardSPIType type);
|
||||||
int CardSPIReadJEDECIDAndStatusReg(CardSPIType type, u32* id, u8* statusReg);
|
int CardSPIReadJEDECIDAndStatusReg(bool infrared, u32* id, u8* statusReg);
|
||||||
int CardSPIGetCardSPIType(CardSPIType* type, bool infrared);
|
CardSPIType CardSPIGetCardSPIType(bool infrared);
|
||||||
u32 CardSPIGetPageSize(CardSPIType type);
|
u32 CardSPIGetPageSize(CardSPIType type);
|
||||||
u32 CardSPIGetCapacity(CardSPIType type);
|
u32 CardSPIGetCapacity(CardSPIType type);
|
||||||
u32 CardSPIGetEraseSize(CardSPIType type);
|
u32 CardSPIGetEraseSize(CardSPIType type);
|
||||||
|
@ -27,7 +27,7 @@ void CTR_CmdReadData(u32 sector, u32 length, u32 blocks, void* buffer)
|
|||||||
(u32)((sector << 9) & 0xFFFFFFFF),
|
(u32)((sector << 9) & 0xFFFFFFFF),
|
||||||
0x00000000, 0x00000000
|
0x00000000, 0x00000000
|
||||||
};
|
};
|
||||||
CTR_SendCommand(read_cmd, length, blocks, 0x704822C, buffer);
|
CTR_SendCommand(read_cmd, length, blocks, 0x104822C, buffer); // Clock divider 5 (13.4 MHz). Same as Process9.
|
||||||
}
|
}
|
||||||
|
|
||||||
void CTR_CmdReadHeader(void* buffer)
|
void CTR_CmdReadHeader(void* buffer)
|
||||||
|
@ -15,5 +15,5 @@ void NTR_CmdEnter16ByteMode(void);
|
|||||||
void NTR_CmdReadHeader (u8* buffer);
|
void NTR_CmdReadHeader (u8* buffer);
|
||||||
void NTR_CmdReadData (u32 offset, void* buffer);
|
void NTR_CmdReadData (u32 offset, void* buffer);
|
||||||
|
|
||||||
bool NTR_Secure_Init (u8* buffer, u32 CartID, int iCardDevice);
|
bool NTR_Secure_Init (u8* buffer, u8* sa_copy, u32 CartID, int iCardDevice);
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include "nds.h"
|
#include "nds.h"
|
||||||
#include "ncch.h"
|
#include "ncch.h"
|
||||||
#include "ncsd.h"
|
#include "ncsd.h"
|
||||||
|
#include "rtc.h"
|
||||||
|
|
||||||
#define CART_INSERTED (!(REG_CARDSTATUS & 0x1))
|
#define CART_INSERTED (!(REG_CARDSTATUS & 0x1))
|
||||||
|
|
||||||
@ -32,8 +33,7 @@ typedef struct {
|
|||||||
TwlHeader ntr_header;
|
TwlHeader ntr_header;
|
||||||
u8 ntr_padding[0x3000]; // 0x00
|
u8 ntr_padding[0x3000]; // 0x00
|
||||||
u8 secure_area[0x4000];
|
u8 secure_area[0x4000];
|
||||||
TwlHeader twl_header;
|
u8 secure_area_enc[0x4000];
|
||||||
u8 twl_padding[0x3000]; // 0x00
|
|
||||||
u8 modcrypt_area[0x4000];
|
u8 modcrypt_area[0x4000];
|
||||||
u32 cart_type;
|
u32 cart_type;
|
||||||
u32 cart_id;
|
u32 cart_id;
|
||||||
@ -44,6 +44,9 @@ typedef struct {
|
|||||||
u32 arm9i_rom_offset;
|
u32 arm9i_rom_offset;
|
||||||
} PACKED_ALIGN(16) CartDataNtrTwl;
|
} PACKED_ALIGN(16) CartDataNtrTwl;
|
||||||
|
|
||||||
|
static DsTime init_time;
|
||||||
|
static bool encrypted_sa = false;
|
||||||
|
|
||||||
u32 GetCartName(char* name, CartData* cdata) {
|
u32 GetCartName(char* name, CartData* cdata) {
|
||||||
if (cdata->cart_type & CART_CTR) {
|
if (cdata->cart_type & CART_CTR) {
|
||||||
CartDataCtr* cdata_i = (CartDataCtr*)cdata;
|
CartDataCtr* cdata_i = (CartDataCtr*)cdata;
|
||||||
@ -59,7 +62,105 @@ u32 GetCartName(char* name, CartData* cdata) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u32 GetCartInfoString(char* info, size_t info_size, CartData* cdata) {
|
||||||
|
size_t info_index = 0;
|
||||||
|
u8 padding;
|
||||||
|
|
||||||
|
// read the last byte of the cart storage, but ignore the result
|
||||||
|
ReadCartBytes(&padding, cdata->cart_size - 1, 1, cdata, false);
|
||||||
|
|
||||||
|
if (cdata->cart_type & CART_CTR) {
|
||||||
|
CartDataCtr* cdata_i = (CartDataCtr*)cdata;
|
||||||
|
NcsdHeader* ncsd = &(cdata_i->ncsd);
|
||||||
|
NcchHeader* ncch = &(cdata_i->ncch);
|
||||||
|
info_index += snprintf(info + info_index, info_size - info_index,
|
||||||
|
"Title ID : %016llX\n"
|
||||||
|
"Product Code : %.10s\n"
|
||||||
|
"Revision : %lu\n"
|
||||||
|
"Cart ID : %08lX\n"
|
||||||
|
"Platform : %s\n",
|
||||||
|
ncsd->mediaId, ncch->productcode, cdata_i->rom_version, cdata_i->cart_id,
|
||||||
|
(ncch->flags[4] == 0x2) ? "N3DS" : "O3DS");
|
||||||
|
} else if (cdata->cart_type & CART_NTR) {
|
||||||
|
CartDataNtrTwl* cdata_i = (CartDataNtrTwl*)cdata;
|
||||||
|
TwlHeader* nds = &(cdata_i->ntr_header);
|
||||||
|
info_index += snprintf(info + info_index, info_size - info_index,
|
||||||
|
"Title String : %.12s\n"
|
||||||
|
"Product Code : %.6s\n"
|
||||||
|
"Revision : %u\n"
|
||||||
|
"Cart ID : %08lX\n"
|
||||||
|
"Platform : %s\n",
|
||||||
|
nds->game_title, nds->game_code, nds->rom_version, cdata_i->cart_id,
|
||||||
|
(nds->unit_code == 0x2) ? "DSi Enhanced" : (nds->unit_code == 0x3) ? "DSi Exclusive" : "DS");
|
||||||
|
} else return 1;
|
||||||
|
|
||||||
|
info_index += snprintf(info + info_index, info_size - info_index,
|
||||||
|
"Save Type : %s\n",
|
||||||
|
(cdata->save_type == CARD_SAVE_NONE) ? "NONE" :
|
||||||
|
(cdata->save_type == CARD_SAVE_SPI) ? "SPI" :
|
||||||
|
(cdata->save_type == CARD_SAVE_CARD2) ? "CARD2" :
|
||||||
|
(cdata->save_type == CARD_SAVE_RETAIL_NAND) ? "RETAIL_NAND" : "UNK");
|
||||||
|
|
||||||
|
if (cdata->save_type == CARD_SAVE_SPI) {
|
||||||
|
u32 jedecid = 0;
|
||||||
|
if (CardSPIReadJEDECIDAndStatusReg(cdata->spi_save_type.infrared, &jedecid, NULL) == 0) {
|
||||||
|
info_index += snprintf(info + info_index, info_size - info_index,
|
||||||
|
"Save chip ID : %06lX\n",
|
||||||
|
jedecid);
|
||||||
|
}
|
||||||
|
} else info_index += snprintf(info + info_index, info_size - info_index,
|
||||||
|
"Save chip ID : <none>\n");
|
||||||
|
|
||||||
|
info_index += snprintf(info + info_index, info_size - info_index,
|
||||||
|
"Padding Byte : %02X\n"
|
||||||
|
"Timestamp : 20%02X-%02X-%02X %02X:%02X:%02X\n"
|
||||||
|
"GM9 Version : %s\n",
|
||||||
|
padding,
|
||||||
|
init_time.bcd_Y, init_time.bcd_M, init_time.bcd_D,
|
||||||
|
init_time.bcd_h, init_time.bcd_m, init_time.bcd_s,
|
||||||
|
VERSION);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 SetSecureAreaEncryption(bool encrypted) {
|
||||||
|
encrypted_sa = encrypted;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static u32 GetCtrCartSaveSize(CartData* cdata) {
|
||||||
|
NcsdHeader* ncsd = (NcsdHeader*) (void*) cdata->header;
|
||||||
|
u32 ncch_sector = ncsd->partitions[0].offset;
|
||||||
|
|
||||||
|
// Load header and ExHeader for first partition
|
||||||
|
u8 buffer[0x400];
|
||||||
|
CTR_CmdReadData(ncch_sector, 0x200, 2, buffer);
|
||||||
|
NcchHeader* ncch = (NcchHeader*) (void*) buffer;
|
||||||
|
if (ValidateNcchHeader(ncch) != 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure first partition has ExHeader
|
||||||
|
if (ncch->size_exthdr < 0x200) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrypt ExHeader
|
||||||
|
if ((NCCH_ENCRYPTED(ncch)) && (SetupNcchCrypto(ncch, NCCH_NOCRYPTO) == 0)) {
|
||||||
|
DecryptNcch(buffer + NCCH_EXTHDR_OFFSET, NCCH_EXTHDR_OFFSET, sizeof(buffer) - NCCH_EXTHDR_OFFSET, ncch, NULL);
|
||||||
|
}
|
||||||
|
u64 savesize = getle64(buffer + NCCH_EXTHDR_OFFSET + 0x1C0);
|
||||||
|
|
||||||
|
// check our work
|
||||||
|
if (savesize <= UINT32_MAX) {
|
||||||
|
return (u32) savesize;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
u32 InitCartRead(CartData* cdata) {
|
u32 InitCartRead(CartData* cdata) {
|
||||||
|
get_dstime(&init_time);
|
||||||
|
encrypted_sa = false;
|
||||||
memset(cdata, 0x00, sizeof(CartData));
|
memset(cdata, 0x00, sizeof(CartData));
|
||||||
cdata->cart_type = CART_NONE;
|
cdata->cart_type = CART_NONE;
|
||||||
if (!CART_INSERTED) return 1;
|
if (!CART_INSERTED) return 1;
|
||||||
@ -118,16 +219,36 @@ u32 InitCartRead(CartData* cdata) {
|
|||||||
|
|
||||||
// save data
|
// save data
|
||||||
u32 card2_offset = getle32(cdata->header + 0x200);
|
u32 card2_offset = getle32(cdata->header + 0x200);
|
||||||
if ((card2_offset != 0xFFFFFFFF) || (CardSPIGetCardSPIType(&(cdata->save_type), 0) != 0)) {
|
if (card2_offset != 0xFFFFFFFF) {
|
||||||
cdata->save_type = (CardSPIType) { NO_CHIP, false };
|
cdata->save_type = CARD_SAVE_CARD2;
|
||||||
|
cdata->save_size = GetCtrCartSaveSize(cdata);
|
||||||
|
// Sanity checks
|
||||||
|
if ((cdata->save_size == 0) ||
|
||||||
|
(card2_offset * NCSD_MEDIA_UNIT >= cdata->cart_size) ||
|
||||||
|
(card2_offset * NCSD_MEDIA_UNIT + cdata->save_size > cdata->cart_size)) {
|
||||||
|
cdata->save_type = CARD_SAVE_NONE;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cdata->spi_save_type = (CardSPIType) { FLASH_CTR_GENERIC, false };
|
||||||
|
cdata->save_size = CardSPIGetCapacity(cdata->spi_save_type);
|
||||||
|
if (cdata->save_size == 0) {
|
||||||
|
cdata->spi_save_type = (CardSPIType) { NO_CHIP, false };
|
||||||
|
}
|
||||||
|
if (cdata->spi_save_type.chip == NO_CHIP) {
|
||||||
|
cdata->save_type = CARD_SAVE_NONE;
|
||||||
|
} else {
|
||||||
|
cdata->save_type = CARD_SAVE_SPI;
|
||||||
|
cdata->save_size = CardSPIGetCapacity(cdata->spi_save_type);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
cdata->save_size = CardSPIGetCapacity(cdata->save_type);
|
|
||||||
} else { // NTR/TWL cartridges
|
} else { // NTR/TWL cartridges
|
||||||
// NTR header
|
// NTR header
|
||||||
TwlHeader* nds_header = (void*)cdata->header;
|
TwlHeader* nds_header = (void*)cdata->header;
|
||||||
|
u8 secure_area_enc[0x4000];
|
||||||
NTR_CmdReadHeader(cdata->header);
|
NTR_CmdReadHeader(cdata->header);
|
||||||
if (!(*(cdata->header))) return 1; // error reading the header
|
if (!(*(cdata->header))) return 1; // error reading the header
|
||||||
if (!NTR_Secure_Init(cdata->header, Cart_GetID(), 0)) return 1;
|
if (!NTR_Secure_Init(cdata->header, secure_area_enc, Cart_GetID(), 0)) return 1;
|
||||||
|
|
||||||
|
|
||||||
// cartridge size, trimmed size, twl presets
|
// cartridge size, trimmed size, twl presets
|
||||||
if (nds_header->device_capacity >= 15) return 1; // too big, not valid
|
if (nds_header->device_capacity >= 15) return 1; // too big, not valid
|
||||||
@ -149,29 +270,44 @@ u32 InitCartRead(CartData* cdata) {
|
|||||||
// We'll only want to use TWL secure init if this is a TWL cartridge.
|
// We'll only want to use TWL secure init if this is a TWL cartridge.
|
||||||
if (cdata->cart_id & 0x40000000U) { // TWL cartridge
|
if (cdata->cart_id & 0x40000000U) { // TWL cartridge
|
||||||
Cart_Init();
|
Cart_Init();
|
||||||
NTR_CmdReadHeader(cdata->twl_header);
|
NTR_CmdReadHeader(cdata->storage);
|
||||||
if (!NTR_Secure_Init(cdata->twl_header, Cart_GetID(), 1)) return 1;
|
if (!NTR_Secure_Init(cdata->storage, NULL, Cart_GetID(), 1)) return 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Check if immediately after the reported cart size
|
||||||
|
// is the magic number string 'ac' (auth code).
|
||||||
|
// If found, add 0x88 bytes for the download play RSA key.
|
||||||
|
u16 rsaMagic;
|
||||||
|
ReadCartBytes(&rsaMagic, cdata->data_size, 2, cdata, false);
|
||||||
|
if(rsaMagic == 0x6361) {
|
||||||
|
cdata->data_size += 0x88;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// store encrypted secure area
|
||||||
|
memcpy(cdata->storage, secure_area_enc, 0x4000);
|
||||||
|
|
||||||
// last safety check
|
// last safety check
|
||||||
if (cdata->data_size > cdata->cart_size) return 1;
|
if (cdata->data_size > cdata->cart_size) return 1;
|
||||||
|
|
||||||
// save data
|
// save data
|
||||||
bool infrared = *(nds_header->game_code) == 'I';
|
bool infrared = *(nds_header->game_code) == 'I';
|
||||||
if (CardSPIGetCardSPIType(&(cdata->save_type), infrared) != 0) {
|
cdata->spi_save_type = CardSPIGetCardSPIType(infrared);
|
||||||
cdata->save_type = (CardSPIType) { NO_CHIP, false };
|
if (cdata->spi_save_type.chip == NO_CHIP) {
|
||||||
|
cdata->save_type = CARD_SAVE_NONE;
|
||||||
|
} else {
|
||||||
|
cdata->save_type = CARD_SAVE_SPI;
|
||||||
|
cdata->save_size = CardSPIGetCapacity(cdata->spi_save_type);
|
||||||
}
|
}
|
||||||
cdata->save_size = CardSPIGetCapacity(cdata->save_type);
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 ReadCartSectors(void* buffer, u32 sector, u32 count, CartData* cdata) {
|
u32 ReadCartSectors(void* buffer, u32 sector, u32 count, CartData* cdata, bool card2_blanking) {
|
||||||
u8* buffer8 = (u8*) buffer;
|
u8* buffer8 = (u8*) buffer;
|
||||||
if (!CART_INSERTED) return 1;
|
if (!CART_INSERTED) return 1;
|
||||||
// header
|
// header
|
||||||
u32 header_sectors = (cdata->cart_type & CART_CTR) ? 0x4000/0x200 : 0x8000/0x200;
|
const u32 header_sectors = 0x4000/0x200;
|
||||||
if (sector < header_sectors) {
|
if (sector < header_sectors) {
|
||||||
u32 header_count = (sector + count > header_sectors) ? header_sectors - sector : count;
|
u32 header_count = (sector + count > header_sectors) ? header_sectors - sector : count;
|
||||||
memcpy(buffer8, cdata->header + (sector * 0x200), header_count * 0x200);
|
memcpy(buffer8, cdata->header + (sector * 0x200), header_count * 0x200);
|
||||||
@ -191,21 +327,50 @@ u32 ReadCartSectors(void* buffer, u32 sector, u32 count, CartData* cdata) {
|
|||||||
CTR_CmdReadData(sector + i, 0x200, min(max_read, count - i), buff);
|
CTR_CmdReadData(sector + i, 0x200, min(max_read, count - i), buff);
|
||||||
buff += max_read * 0x200;
|
buff += max_read * 0x200;
|
||||||
}
|
}
|
||||||
|
|
||||||
// overwrite the card2 savegame with 0xFF
|
// overwrite the card2 savegame with 0xFF
|
||||||
u32 card2_offset = getle32(cdata->header + 0x200);
|
u32 card2_offset = getle32(cdata->header + 0x200);
|
||||||
if ((card2_offset != 0xFFFFFFFF) &&
|
u32 save_sectors = cdata->save_size / 0x200;
|
||||||
|
if (card2_blanking &&
|
||||||
|
(cdata->save_type == CARD_SAVE_CARD2) &&
|
||||||
((card2_offset * 0x200) >= cdata->data_size) &&
|
((card2_offset * 0x200) >= cdata->data_size) &&
|
||||||
(sector + count > card2_offset)) {
|
(sector + count > card2_offset) && // requested area ends after the save starts
|
||||||
if (sector > card2_offset)
|
(sector < card2_offset + save_sectors)) { // requested area starts before the save ends
|
||||||
memset(buffer8, 0xFF, (count * 0x200));
|
u32 blank_start_sector, blank_end_sector;
|
||||||
else memset(buffer8 + (card2_offset - sector) * 0x200, 0xFF,
|
if (sector > card2_offset) {
|
||||||
(count - (card2_offset - sector)) * 0x200);
|
blank_start_sector = sector;
|
||||||
|
} else {
|
||||||
|
blank_start_sector = card2_offset;
|
||||||
|
}
|
||||||
|
if (sector + count < card2_offset + save_sectors) {
|
||||||
|
blank_end_sector = sector + count;
|
||||||
|
} else {
|
||||||
|
blank_end_sector = card2_offset + save_sectors;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(buffer8 + (blank_start_sector - sector) * 0x200, 0xFF,
|
||||||
|
(blank_end_sector - blank_start_sector) * 0x200);
|
||||||
}
|
}
|
||||||
} else if (cdata->cart_type & CART_NTR) {
|
} else if (cdata->cart_type & CART_NTR) {
|
||||||
u8* buff = buffer8;
|
u8* buff = buffer8;
|
||||||
|
|
||||||
|
// secure area handling
|
||||||
|
const u32 sa_sector_end = 0x8000/0x200;
|
||||||
|
if (sector < sa_sector_end) {
|
||||||
|
CartDataNtrTwl* cdata_twl = (CartDataNtrTwl*) cdata;
|
||||||
|
u8* sa = encrypted_sa ? cdata_twl->secure_area_enc : cdata_twl->secure_area;
|
||||||
|
u32 count_sa = ((sector + count) > sa_sector_end) ? sa_sector_end - sector : count;
|
||||||
|
memcpy(buff, sa + ((sector - header_sectors) * 0x200), count_sa * 0x200);
|
||||||
|
buff += count_sa * 0x200;
|
||||||
|
sector += count_sa;
|
||||||
|
count -= count_sa;
|
||||||
|
}
|
||||||
|
|
||||||
|
// regular cart data
|
||||||
u32 off = sector * 0x200;
|
u32 off = sector * 0x200;
|
||||||
for (u32 i = 0; i < count; i++, off += 0x200, buff += 0x200)
|
for (u32 i = 0; i < count; i++, off += 0x200, buff += 0x200)
|
||||||
NTR_CmdReadData(off, buff);
|
NTR_CmdReadData(off, buff);
|
||||||
|
|
||||||
// modcrypt area handling
|
// modcrypt area handling
|
||||||
if ((cdata->cart_type & CART_TWL) &&
|
if ((cdata->cart_type & CART_TWL) &&
|
||||||
((sector+count) * 0x200 > cdata->arm9i_rom_offset) &&
|
((sector+count) * 0x200 > cdata->arm9i_rom_offset) &&
|
||||||
@ -220,22 +385,22 @@ u32 ReadCartSectors(void* buffer, u32 sector, u32 count, CartData* cdata) {
|
|||||||
size_i = MODC_AREA_SIZE - offset_i;
|
size_i = MODC_AREA_SIZE - offset_i;
|
||||||
if (size_i > (count * 0x200) - (buffer_arm9i - buffer8))
|
if (size_i > (count * 0x200) - (buffer_arm9i - buffer8))
|
||||||
size_i = (count * 0x200) - (buffer_arm9i - buffer8);
|
size_i = (count * 0x200) - (buffer_arm9i - buffer8);
|
||||||
if (size_i) memcpy(buffer_arm9i, cdata->twl_header + 0x4000 + offset_i, size_i);
|
if (size_i) memcpy(buffer_arm9i, cdata->storage + 0x4000 + offset_i, size_i);
|
||||||
}
|
}
|
||||||
} else return 1;
|
} else return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 ReadCartBytes(void* buffer, u64 offset, u64 count, CartData* cdata) {
|
u32 ReadCartBytes(void* buffer, u64 offset, u64 count, CartData* cdata, bool card2_blanking) {
|
||||||
if (!(offset % 0x200) && !(count % 0x200)) { // aligned data -> simple case
|
if (!(offset % 0x200) && !(count % 0x200)) { // aligned data -> simple case
|
||||||
// simple wrapper function for ReadCartSectors(...)
|
// simple wrapper function for ReadCartSectors(...)
|
||||||
return ReadCartSectors(buffer, offset / 0x200, count / 0x200, cdata);
|
return ReadCartSectors(buffer, offset / 0x200, count / 0x200, cdata, card2_blanking);
|
||||||
} else { // misaligned data -> -___-
|
} else { // misaligned data -> -___-
|
||||||
u8* buffer8 = (u8*) buffer;
|
u8* buffer8 = (u8*) buffer;
|
||||||
u8 l_buffer[0x200];
|
u8 l_buffer[0x200];
|
||||||
if (offset % 0x200) { // handle misaligned offset
|
if (offset % 0x200) { // handle misaligned offset
|
||||||
u32 offset_fix = 0x200 - (offset % 0x200);
|
u32 offset_fix = 0x200 - (offset % 0x200);
|
||||||
if (ReadCartSectors(l_buffer, offset / 0x200, 1, cdata) != 0) return 1;
|
if (ReadCartSectors(l_buffer, offset / 0x200, 1, cdata, card2_blanking) != 0) return 1;
|
||||||
memcpy(buffer8, l_buffer + 0x200 - offset_fix, min(offset_fix, count));
|
memcpy(buffer8, l_buffer + 0x200 - offset_fix, min(offset_fix, count));
|
||||||
if (count <= offset_fix) return 0;
|
if (count <= offset_fix) return 0;
|
||||||
offset += offset_fix;
|
offset += offset_fix;
|
||||||
@ -243,11 +408,11 @@ u32 ReadCartBytes(void* buffer, u64 offset, u64 count, CartData* cdata) {
|
|||||||
count -= offset_fix;
|
count -= offset_fix;
|
||||||
} // offset is now aligned and part of the data is read
|
} // offset is now aligned and part of the data is read
|
||||||
if (count >= 0x200) { // otherwise this is misaligned and will be handled below
|
if (count >= 0x200) { // otherwise this is misaligned and will be handled below
|
||||||
if (ReadCartSectors(buffer8, offset / 0x200, count / 0x200, cdata) != 0) return 1;
|
if (ReadCartSectors(buffer8, offset / 0x200, count / 0x200, cdata, card2_blanking) != 0) return 1;
|
||||||
}
|
}
|
||||||
if (count % 0x200) { // handle misaligned count
|
if (count % 0x200) { // handle misaligned count
|
||||||
u32 count_fix = count % 0x200;
|
u32 count_fix = count % 0x200;
|
||||||
if (ReadCartSectors(l_buffer, (offset + count) / 0x200, 1, cdata) != 0) return 1;
|
if (ReadCartSectors(l_buffer, (offset + count) / 0x200, 1, cdata, card2_blanking) != 0) return 1;
|
||||||
memcpy(buffer8 + count - count_fix, l_buffer, count_fix);
|
memcpy(buffer8 + count - count_fix, l_buffer, count_fix);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -264,30 +429,43 @@ u32 ReadCartPrivateHeader(void* buffer, u64 offset, u64 count, CartData* cdata)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u32 ReadCartInfo(u8* buffer, u64 offset, u64 count, CartData* cdata) {
|
||||||
|
char info[256];
|
||||||
|
u32 len;
|
||||||
|
|
||||||
|
GetCartInfoString(info, sizeof(info), cdata);
|
||||||
|
len = strnlen(info, 255);
|
||||||
|
|
||||||
|
if (offset >= len) return 0;
|
||||||
|
if (offset + count > len) count = len - offset;
|
||||||
|
memcpy(buffer, info + offset, count);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
u32 ReadCartSave(u8* buffer, u64 offset, u64 count, CartData* cdata) {
|
u32 ReadCartSave(u8* buffer, u64 offset, u64 count, CartData* cdata) {
|
||||||
if (offset >= cdata->save_size) return 1;
|
if (offset >= cdata->save_size) return 1;
|
||||||
if (offset + count > cdata->save_size) count = cdata->save_size - offset;
|
if (offset + count > cdata->save_size) count = cdata->save_size - offset;
|
||||||
return (CardSPIReadSaveData(cdata->save_type, offset, buffer, count) == 0) ? 0 : 1;
|
switch (cdata->save_type) {
|
||||||
|
case CARD_SAVE_SPI:
|
||||||
|
return (CardSPIReadSaveData(cdata->spi_save_type, offset, buffer, count) == 0) ? 0 : 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CARD_SAVE_CARD2:
|
||||||
|
{
|
||||||
|
u32 card2_offset = getle32(cdata->header + 0x200);
|
||||||
|
return ReadCartBytes(buffer, card2_offset * NCSD_MEDIA_UNIT + offset, count, cdata, false);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 WriteCartSave(const u8* buffer, u64 offset, u64 count, CartData* cdata) {
|
u32 WriteCartSave(const u8* buffer, u64 offset, u64 count, CartData* cdata) {
|
||||||
if (offset >= cdata->save_size) return 1;
|
if (offset >= cdata->save_size) return 1;
|
||||||
if (offset + count > cdata->save_size) count = cdata->save_size - offset;
|
if (offset + count > cdata->save_size) count = cdata->save_size - offset;
|
||||||
return (CardSPIWriteSaveData(cdata->save_type, offset, buffer, count) == 0) ? 0 : 1;
|
return (CardSPIWriteSaveData(cdata->spi_save_type, offset, buffer, count) == 0) ? 0 : 1;
|
||||||
}
|
|
||||||
|
|
||||||
u32 ReadCartSaveJedecId(u8* buffer, u64 offset, u64 count, CartData* cdata) {
|
|
||||||
u8 ownBuf[JEDECID_AND_SREG_SIZE] = { 0 };
|
|
||||||
u32 id;
|
|
||||||
u8 sReg;
|
|
||||||
if (offset >= JEDECID_AND_SREG_SIZE) return 1;
|
|
||||||
if (offset + count > JEDECID_AND_SREG_SIZE) count = JEDECID_AND_SREG_SIZE - offset;
|
|
||||||
int res = CardSPIReadJEDECIDAndStatusReg(cdata->save_type, &id, &sReg);
|
|
||||||
if (res) return res;
|
|
||||||
ownBuf[0] = (id >> 16) & 0xff;
|
|
||||||
ownBuf[1] = (id >> 8) & 0xff;
|
|
||||||
ownBuf[2] = id & 0xff;
|
|
||||||
ownBuf[JEDECID_AND_SREG_SIZE - 1] = sReg;
|
|
||||||
memcpy(buffer, ownBuf + offset, count);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
@ -10,25 +10,34 @@
|
|||||||
|
|
||||||
#define MODC_AREA_SIZE 0x4000
|
#define MODC_AREA_SIZE 0x4000
|
||||||
#define PRIV_HDR_SIZE 0x50
|
#define PRIV_HDR_SIZE 0x50
|
||||||
#define JEDECID_AND_SREG_SIZE 0x4
|
|
||||||
|
typedef enum CardSaveType {
|
||||||
|
CARD_SAVE_NONE,
|
||||||
|
CARD_SAVE_SPI,
|
||||||
|
CARD_SAVE_CARD2,
|
||||||
|
CARD_SAVE_RETAIL_NAND,
|
||||||
|
} CardSaveType;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
u8 header[0x8000]; // NTR header + secure area / CTR header + private header
|
u8 header[0x8000]; // NTR header + secure area / CTR header + private header
|
||||||
u8 twl_header[0x8000]; // TWL header + modcrypt area / unused
|
u8 storage[0x8000]; // encrypted secure area + modcrypt area / unused
|
||||||
u32 cart_type;
|
u32 cart_type;
|
||||||
u32 cart_id;
|
u32 cart_id;
|
||||||
u64 cart_size;
|
u64 cart_size;
|
||||||
u64 data_size;
|
u64 data_size;
|
||||||
u32 save_size;
|
u32 save_size;
|
||||||
CardSPIType save_type;
|
CardSaveType save_type;
|
||||||
|
CardSPIType spi_save_type; // Specific data for SPI save
|
||||||
u32 arm9i_rom_offset; // TWL specific
|
u32 arm9i_rom_offset; // TWL specific
|
||||||
} PACKED_ALIGN(16) CartData;
|
} PACKED_ALIGN(16) CartData;
|
||||||
|
|
||||||
u32 GetCartName(char* name, CartData* cdata);
|
u32 GetCartName(char* name, CartData* cdata);
|
||||||
|
u32 GetCartInfoString(char* info, size_t info_size, CartData* cdata);
|
||||||
|
u32 SetSecureAreaEncryption(bool encrypted);
|
||||||
u32 InitCartRead(CartData* cdata);
|
u32 InitCartRead(CartData* cdata);
|
||||||
u32 ReadCartSectors(void* buffer, u32 sector, u32 count, CartData* cdata);
|
u32 ReadCartSectors(void* buffer, u32 sector, u32 count, CartData* cdata, bool card2_blanking);
|
||||||
u32 ReadCartBytes(void* buffer, u64 offset, u64 count, CartData* cdata);
|
u32 ReadCartBytes(void* buffer, u64 offset, u64 count, CartData* cdata, bool card2_blanking);
|
||||||
u32 ReadCartPrivateHeader(void* buffer, u64 offset, u64 count, CartData* cdata);
|
u32 ReadCartPrivateHeader(void* buffer, u64 offset, u64 count, CartData* cdata);
|
||||||
|
u32 ReadCartInfo(u8* buffer, u64 offset, u64 count, CartData* cdata);
|
||||||
u32 ReadCartSave(u8* buffer, u64 offset, u64 count, CartData* cdata);
|
u32 ReadCartSave(u8* buffer, u64 offset, u64 count, CartData* cdata);
|
||||||
u32 WriteCartSave(const u8* buffer, u64 offset, u64 count, CartData* cdata);
|
u32 WriteCartSave(const u8* buffer, u64 offset, u64 count, CartData* cdata);
|
||||||
u32 ReadCartSaveJedecId(u8* buffer, u64 offset, u64 count, CartData* cdata);
|
|
||||||
|
@ -231,7 +231,7 @@ void NTR_CmdSecure (u32 flags, void* buffer, u32 length, u8* pcmd)
|
|||||||
cardPolledTransfer (flags, buffer, length, pcmd);
|
cardPolledTransfer (flags, buffer, length, pcmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NTR_Secure_Init (u8* header, u32 CartID, int iCardDevice)
|
bool NTR_Secure_Init (u8* header, u8* sa_copy, u32 CartID, int iCardDevice)
|
||||||
{
|
{
|
||||||
u32 iGameCode;
|
u32 iGameCode;
|
||||||
u32 iCardHash[0x412] = {0};
|
u32 iCardHash[0x412] = {0};
|
||||||
@ -329,6 +329,7 @@ bool NTR_Secure_Init (u8* header, u32 CartID, int iCardDevice)
|
|||||||
NTR_CmdSecure (flagsKey1, NULL, 0, cmdData);
|
NTR_CmdSecure (flagsKey1, NULL, 0, cmdData);
|
||||||
|
|
||||||
//CycloDS doesn't like the dsi secure area being decrypted
|
//CycloDS doesn't like the dsi secure area being decrypted
|
||||||
|
if (sa_copy) memcpy(sa_copy, secureArea, 0x4000);
|
||||||
if(!iCardDevice && ((nds9Offset != 0x4000) || secureArea[0] || secureArea[1]))
|
if(!iCardDevice && ((nds9Offset != 0x4000) || secureArea[0] || secureArea[1]))
|
||||||
{
|
{
|
||||||
NTR_DecryptSecureArea (iGameCode, iCardHash, nCardHash, iKeyCode, secureArea, iCardDevice);
|
NTR_DecryptSecureArea (iGameCode, iCardHash, nCardHash, iKeyCode, secureArea, iCardDevice);
|
||||||
|
File diff suppressed because it is too large
Load Diff
188
arm9/source/language.c
Normal file
188
arm9/source/language.c
Normal file
@ -0,0 +1,188 @@
|
|||||||
|
#include "language.h"
|
||||||
|
#include "fsdrive.h"
|
||||||
|
#include "fsutil.h"
|
||||||
|
#include "support.h"
|
||||||
|
#include "ui.h"
|
||||||
|
|
||||||
|
#define STRING(what, def) const char* STR_##what = NULL;
|
||||||
|
#include "language.inl"
|
||||||
|
#undef STRING
|
||||||
|
|
||||||
|
static const char** translation_ptrs[] = {
|
||||||
|
#define STRING(what, def) &STR_##what,
|
||||||
|
#include "language.inl"
|
||||||
|
#undef STRING
|
||||||
|
};
|
||||||
|
|
||||||
|
static const char* translation_fallbacks[] = {
|
||||||
|
#define STRING(what, def) def,
|
||||||
|
#include "language.inl"
|
||||||
|
#undef STRING
|
||||||
|
};
|
||||||
|
|
||||||
|
static char* translation_data = NULL;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char name[32];
|
||||||
|
char path[256];
|
||||||
|
} Language;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char chunk_id[4]; // NOT null terminated
|
||||||
|
u32 size;
|
||||||
|
} RiffChunkHeader;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
u32 version;
|
||||||
|
u32 count;
|
||||||
|
char languageName[32];
|
||||||
|
} LanguageMeta;
|
||||||
|
STATIC_ASSERT(sizeof(LanguageMeta) == 40);
|
||||||
|
|
||||||
|
bool SetLanguage(const void* translation, u32 translation_size) {
|
||||||
|
u32 str_count;
|
||||||
|
const void* ptr = translation;
|
||||||
|
const RiffChunkHeader* riff_header;
|
||||||
|
const RiffChunkHeader* chunk_header;
|
||||||
|
|
||||||
|
// Free old translation data
|
||||||
|
if (translation_data) {
|
||||||
|
free(translation_data);
|
||||||
|
translation_data = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((ptr = GetLanguage(translation, translation_size, NULL, &str_count, NULL))) {
|
||||||
|
// load total size
|
||||||
|
riff_header = translation;
|
||||||
|
|
||||||
|
while ((u32)(ptr - translation) < riff_header->size + sizeof(RiffChunkHeader)) {
|
||||||
|
chunk_header = ptr;
|
||||||
|
|
||||||
|
if (memcmp(chunk_header->chunk_id, "SDAT", 4) == 0) { // string data
|
||||||
|
if (chunk_header->size > 0) {
|
||||||
|
translation_data = malloc(chunk_header->size);
|
||||||
|
if (!translation_data) goto fallback;
|
||||||
|
|
||||||
|
memcpy(translation_data, ptr + sizeof(RiffChunkHeader), chunk_header->size);
|
||||||
|
}
|
||||||
|
} else if (memcmp(chunk_header->chunk_id, "SMAP", 4) == 0) { // string map
|
||||||
|
// string data must come before the map
|
||||||
|
if (!translation_data && str_count > 0) goto fallback;
|
||||||
|
|
||||||
|
u16* string_map = (u16*)(ptr + sizeof(RiffChunkHeader));
|
||||||
|
|
||||||
|
// Load all the strings
|
||||||
|
for (u32 i = 0; i < countof(translation_ptrs); i++) {
|
||||||
|
if (i < str_count) {
|
||||||
|
*translation_ptrs[i] = (translation_data + string_map[i]);
|
||||||
|
} else {
|
||||||
|
*translation_ptrs[i] = translation_fallbacks[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr += sizeof(RiffChunkHeader) + chunk_header->size;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
fallback:
|
||||||
|
if (translation_data) {
|
||||||
|
free(translation_data);
|
||||||
|
translation_data = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (u32 i = 0; i < countof(translation_ptrs); i++) {
|
||||||
|
*translation_ptrs[i] = translation_fallbacks[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const void* GetLanguage(const void* riff, const u32 riff_size, u32* version, u32* count, char* language_name) {
|
||||||
|
const void* ptr = riff;
|
||||||
|
const RiffChunkHeader* riff_header;
|
||||||
|
const RiffChunkHeader* chunk_header;
|
||||||
|
|
||||||
|
// check header magic and load size
|
||||||
|
if (!ptr) return NULL;
|
||||||
|
riff_header = ptr;
|
||||||
|
if (memcmp(riff_header->chunk_id, "RIFF", 4) != 0) return NULL;
|
||||||
|
|
||||||
|
// ensure enough space is allocated
|
||||||
|
if (riff_header->size > riff_size) return NULL;
|
||||||
|
|
||||||
|
ptr += sizeof(RiffChunkHeader);
|
||||||
|
|
||||||
|
while ((u32)(ptr - riff) < riff_header->size + sizeof(RiffChunkHeader)) {
|
||||||
|
chunk_header = ptr;
|
||||||
|
|
||||||
|
// check for and load META section
|
||||||
|
if (memcmp(chunk_header->chunk_id, "META", 4) == 0) {
|
||||||
|
if (chunk_header->size != sizeof(LanguageMeta)) return NULL;
|
||||||
|
|
||||||
|
const LanguageMeta *meta = ptr + sizeof(RiffChunkHeader);
|
||||||
|
if (meta->version != TRANSLATION_VER || meta->count > countof(translation_ptrs)) return NULL;
|
||||||
|
|
||||||
|
// all good
|
||||||
|
if (version) *version = meta->version;
|
||||||
|
if (count) *count = meta->count;
|
||||||
|
if (language_name) strcpy(language_name, meta->languageName);
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr += sizeof(RiffChunkHeader) + chunk_header->size;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int compLanguage(const void* e1, const void* e2) {
|
||||||
|
const Language* entry2 = (const Language*) e2;
|
||||||
|
const Language* entry1 = (const Language*) e1;
|
||||||
|
return strncasecmp(entry1->name, entry2->name, 32);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LanguageMenu(char* result, const char* title) {
|
||||||
|
DirStruct* langDir = (DirStruct*)malloc(sizeof(DirStruct));
|
||||||
|
if (!langDir) return false;
|
||||||
|
|
||||||
|
char path[256];
|
||||||
|
if (!GetSupportDir(path, LANGUAGES_DIR)) return false;
|
||||||
|
GetDirContents(langDir, path);
|
||||||
|
|
||||||
|
char* header = (char*)malloc(0x2C0);
|
||||||
|
Language* langs = (Language*)malloc(langDir->n_entries * sizeof(Language));
|
||||||
|
int langCount = 0;
|
||||||
|
|
||||||
|
// Find all valid files and get their language names
|
||||||
|
for (u32 i = 0; i < langDir->n_entries; i++) {
|
||||||
|
if (langDir->entry[i].type == T_FILE) {
|
||||||
|
size_t fsize = FileGetSize(langDir->entry[i].path);
|
||||||
|
FileGetData(langDir->entry[i].path, header, 0x2C0, 0);
|
||||||
|
if (GetLanguage(header, fsize, NULL, NULL, langs[langCount].name)) {
|
||||||
|
memcpy(langs[langCount].path, langDir->entry[i].path, 256);
|
||||||
|
langCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(langDir);
|
||||||
|
free(header);
|
||||||
|
|
||||||
|
qsort(langs, langCount, sizeof(Language), compLanguage);
|
||||||
|
|
||||||
|
// Make an array of just the names for the select promt
|
||||||
|
const char* langNames[langCount];
|
||||||
|
for (int i = 0; i < langCount; i++) {
|
||||||
|
langNames[i] = langs[i].name;
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 selected = ShowSelectPrompt(langCount, langNames, "%s", title);
|
||||||
|
if (selected > 0 && result) {
|
||||||
|
memcpy(result, langs[selected - 1].path, 256);
|
||||||
|
}
|
||||||
|
|
||||||
|
return selected > 0;
|
||||||
|
}
|
12
arm9/source/language.h
Normal file
12
arm9/source/language.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#define STRING(what, def) extern const char* STR_##what;
|
||||||
|
#include "language.inl"
|
||||||
|
#undef STRING
|
||||||
|
|
||||||
|
bool SetLanguage(const void* translation, u32 translation_size);
|
||||||
|
const void* GetLanguage(const void* riff, u32 riff_size, u32* version, u32* count, char* language_name);
|
||||||
|
|
||||||
|
bool LanguageMenu(char* result, const char* title);
|
14
arm9/source/lua/README.md
Normal file
14
arm9/source/lua/README.md
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
This is Lua 5.4.7 with a few modifications:
|
||||||
|
* Patches made to silence warnings: https://github.com/ihaveamac/GodMode9/commit/9905b939b26aae3422c906c7858d8852764fa279
|
||||||
|
* lua.c, luac.c, lua.hpp removed (not useful in GodMode9)
|
||||||
|
* liolib.c, loslib.c removed (replaced with custom implementations)
|
||||||
|
|
||||||
|
## License of Lua 5.4.7
|
||||||
|
|
||||||
|
Copyright © 1994–2024 Lua.org, PUC-Rio.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
800
arm9/source/lua/gm9internalfs.c
Normal file
800
arm9/source/lua/gm9internalfs.c
Normal file
@ -0,0 +1,800 @@
|
|||||||
|
#ifndef NO_LUA
|
||||||
|
#include "gm9internalfs.h"
|
||||||
|
#include "fs.h"
|
||||||
|
#include "ui.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include "sha.h"
|
||||||
|
#include "nand.h"
|
||||||
|
#include "language.h"
|
||||||
|
#include "hid.h"
|
||||||
|
#include "game.h"
|
||||||
|
#include "gamecart.h"
|
||||||
|
|
||||||
|
#define _MAX_FOR_DEPTH 16
|
||||||
|
|
||||||
|
static u8 no_data_hash_256[32] = { SHA256_EMPTY_HASH };
|
||||||
|
static u8 no_data_hash_1[32] = { SHA1_EMPTY_HASH };
|
||||||
|
|
||||||
|
static bool PathIsDirectory(const char* path) {
|
||||||
|
FRESULT res;
|
||||||
|
FILINFO fno;
|
||||||
|
res = fvx_stat(path, &fno);
|
||||||
|
if (res != FR_OK) return false;
|
||||||
|
return fno.fattrib & AM_DIR;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void CreateStatTable(lua_State* L, FILINFO* fno) {
|
||||||
|
lua_createtable(L, 0, 4); // create nested table
|
||||||
|
lua_pushstring(L, fno->fname);
|
||||||
|
lua_setfield(L, -2, "name");
|
||||||
|
lua_pushstring(L, (fno->fattrib & AM_DIR) ? "dir" : "file");
|
||||||
|
lua_setfield(L, -2, "type");
|
||||||
|
lua_pushinteger(L, fno->fsize);
|
||||||
|
lua_setfield(L, -2, "size");
|
||||||
|
lua_pushboolean(L, fno->fattrib & AM_RDO);
|
||||||
|
lua_setfield(L, -2, "read_only");
|
||||||
|
// ... and leave this table on the stack for the caller to deal with
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_move(lua_State* L) {
|
||||||
|
bool extra = CheckLuaArgCountPlusExtra(L, 2, "_fs.move");
|
||||||
|
const char* path_src = luaL_checkstring(L, 1);
|
||||||
|
const char* path_dst = luaL_checkstring(L, 2);
|
||||||
|
FILINFO fno;
|
||||||
|
|
||||||
|
CheckWritePermissionsLuaError(L, path_src);
|
||||||
|
CheckWritePermissionsLuaError(L, path_dst);
|
||||||
|
|
||||||
|
u32 flags = BUILD_PATH;
|
||||||
|
if (extra) {
|
||||||
|
flags = GetFlagsFromTable(L, 3, flags, NO_CANCEL | SILENT | OVERWRITE_ALL | SKIP_ALL);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(flags & OVERWRITE_ALL) && (fvx_stat(path_dst, &fno) == FR_OK)) {
|
||||||
|
return luaL_error(L, "destination already exists on %s -> %s and {overwrite=true} was not used", path_src, path_dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(PathMoveCopy(path_dst, path_src, &flags, true))) {
|
||||||
|
return luaL_error(L, "PathMoveCopy failed on %s -> %s", path_src, path_dst);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_remove(lua_State* L) {
|
||||||
|
bool extra = CheckLuaArgCountPlusExtra(L, 1, "_fs.remove");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
CheckWritePermissionsLuaError(L, path);
|
||||||
|
|
||||||
|
u32 flags = 0;
|
||||||
|
if (extra) {
|
||||||
|
flags = GetFlagsFromTable(L, 2, flags, RECURSIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(flags & RECURSIVE)) {
|
||||||
|
if (PathIsDirectory(path)) {
|
||||||
|
return luaL_error(L, "requested directory remove without {recursive=true} on %s", path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(PathDelete(path))) {
|
||||||
|
return luaL_error(L, "PathDelete failed on %s", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_copy(lua_State* L) {
|
||||||
|
bool extra = CheckLuaArgCountPlusExtra(L, 2, "_fs.copy");
|
||||||
|
const char* path_src = luaL_checkstring(L, 1);
|
||||||
|
const char* path_dst = luaL_checkstring(L, 2);
|
||||||
|
FILINFO fno;
|
||||||
|
|
||||||
|
CheckWritePermissionsLuaError(L, path_dst);
|
||||||
|
|
||||||
|
u32 flags = BUILD_PATH;
|
||||||
|
if (extra) {
|
||||||
|
flags = GetFlagsFromTable(L, 3, flags, CALC_SHA | USE_SHA1 | NO_CANCEL | SILENT | OVERWRITE_ALL | SKIP_ALL | APPEND_ALL | RECURSIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(flags & RECURSIVE)) {
|
||||||
|
if (PathIsDirectory(path_src)) {
|
||||||
|
return luaL_error(L, "requested directory copy without {recursive=true} on %s -> %s", path_src, path_dst);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(flags & OVERWRITE_ALL) && (fvx_stat(path_dst, &fno) == FR_OK)) {
|
||||||
|
return luaL_error(L, "destination already exists on %s -> %s and {overwrite=true} was not used", path_src, path_dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(PathMoveCopy(path_dst, path_src, &flags, false))) {
|
||||||
|
return luaL_error(L, "PathMoveCopy failed on %s -> %s", path_src, path_dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_mkdir(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "_fs.mkdir");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
CheckWritePermissionsLuaError(L, path);
|
||||||
|
|
||||||
|
FRESULT res = fvx_rmkdir(path);
|
||||||
|
if (res != FR_OK) {
|
||||||
|
return luaL_error(L, "could not mkdir %s (%d)", path, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_list_dir(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "_fs.list_dir");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
lua_newtable(L);
|
||||||
|
|
||||||
|
DIR dir;
|
||||||
|
FILINFO fno;
|
||||||
|
|
||||||
|
FRESULT res = fvx_opendir(&dir, path);
|
||||||
|
if (res != FR_OK) {
|
||||||
|
lua_pop(L, 1); // remove final table from stack
|
||||||
|
return luaL_error(L, "could not opendir %s (%d)", path, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; true; i++) {
|
||||||
|
res = fvx_readdir(&dir, &fno);
|
||||||
|
if (res != FR_OK) {
|
||||||
|
fvx_closedir(&dir);
|
||||||
|
lua_pop(L, 1); // remove final table from stack
|
||||||
|
return luaL_error(L, "could not readdir %s (%d)", path, res);
|
||||||
|
}
|
||||||
|
if (fno.fname[0] == 0) break;
|
||||||
|
CreateStatTable(L, &fno);
|
||||||
|
lua_seti(L, -2, i); // add nested table to final table
|
||||||
|
}
|
||||||
|
fvx_closedir(&dir);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_stat(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "_fs.stat");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
FILINFO fno;
|
||||||
|
|
||||||
|
FRESULT res = fvx_stat(path, &fno);
|
||||||
|
if (res != FR_OK) {
|
||||||
|
return luaL_error(L, "could not stat %s (%d)", path, res);
|
||||||
|
}
|
||||||
|
CreateStatTable(L, &fno);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: make this manually check for permissions recursively
|
||||||
|
static int internalfs_fix_cmacs(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "_fs.fix_cmacs");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
ShowString("%s", STR_FIXING_CMACS_PLEASE_WAIT);
|
||||||
|
if (RecursiveFixFileCmac(path) != 0) {
|
||||||
|
return luaL_error(L, "fixcmac failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_stat_fs(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "_fs.stat_fs");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
u64 freespace = GetFreeSpace(path);
|
||||||
|
u64 totalspace = GetTotalSpace(path);
|
||||||
|
u64 usedspace = totalspace - freespace;
|
||||||
|
|
||||||
|
lua_createtable(L, 0, 3);
|
||||||
|
lua_pushinteger(L, freespace);
|
||||||
|
lua_setfield(L, -2, "free");
|
||||||
|
lua_pushinteger(L, totalspace);
|
||||||
|
lua_setfield(L, -2, "total");
|
||||||
|
lua_pushinteger(L, usedspace);
|
||||||
|
lua_setfield(L, -2, "used");
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_dir_info(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "_fs.dir_info");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
u64 tsize = 0;
|
||||||
|
u32 tdirs = 0;
|
||||||
|
u32 tfiles = 0;
|
||||||
|
if (!DirInfo(path, &tsize, &tdirs, &tfiles)) {
|
||||||
|
return luaL_error(L, "error when running DirInfo");
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_createtable(L, 0, 3);
|
||||||
|
lua_pushinteger(L, tsize);
|
||||||
|
lua_setfield(L, -2, "size");
|
||||||
|
lua_pushinteger(L, tdirs);
|
||||||
|
lua_setfield(L, -2, "dirs");
|
||||||
|
lua_pushinteger(L, tfiles);
|
||||||
|
lua_setfield(L, -2, "files");
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int FileDirSelector(lua_State* L, const char* path_orig, const char* prompt, bool is_dir, bool include_dirs, bool explorer) {
|
||||||
|
bool ret;
|
||||||
|
char path[_VAR_CNT_LEN] = { 0 };
|
||||||
|
char choice[_VAR_CNT_LEN] = { 0 };
|
||||||
|
strncpy(path, path_orig, _VAR_CNT_LEN);
|
||||||
|
if (strncmp(path, "Z:", 2) == 0) {
|
||||||
|
return luaL_error(L, "forbidden drive");
|
||||||
|
} else if (!is_dir) {
|
||||||
|
u32 flags_ext = include_dirs ? 0 : NO_DIRS;
|
||||||
|
char *npattern = strrchr(path, '/');
|
||||||
|
if (!npattern) {
|
||||||
|
return luaL_error(L, "invalid path");
|
||||||
|
}
|
||||||
|
*(npattern++) = '\0';
|
||||||
|
ret = FileSelector(choice, prompt, path, npattern, flags_ext, explorer);
|
||||||
|
} else {
|
||||||
|
ret = FileSelector(choice, prompt, path, NULL, NO_FILES | SELECT_DIRS, explorer);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret) {
|
||||||
|
lua_pushstring(L, choice);
|
||||||
|
} else {
|
||||||
|
lua_pushnil(L);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_ask_select_file(lua_State* L) {
|
||||||
|
bool extra = CheckLuaArgCountPlusExtra(L, 2, "_fs.ask_select_file");
|
||||||
|
const char* prompt = luaL_checkstring(L, 1);
|
||||||
|
const char* path = luaL_checkstring(L, 2);
|
||||||
|
|
||||||
|
u32 flags = 0;
|
||||||
|
if (extra) {
|
||||||
|
flags = GetFlagsFromTable(L, 3, flags, INCLUDE_DIRS | EXPLORER);
|
||||||
|
};
|
||||||
|
|
||||||
|
return FileDirSelector(L, path, prompt, false, (flags & INCLUDE_DIRS), (flags & EXPLORER));
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_ask_select_dir(lua_State* L) {
|
||||||
|
bool extra = CheckLuaArgCountPlusExtra(L, 2, "_fs.ask_select_dir");
|
||||||
|
const char* prompt = luaL_checkstring(L, 1);
|
||||||
|
const char* path = luaL_checkstring(L, 2);
|
||||||
|
|
||||||
|
u32 flags = 0;
|
||||||
|
if (extra) {
|
||||||
|
flags = GetFlagsFromTable(L, 3, flags, EXPLORER);
|
||||||
|
};
|
||||||
|
|
||||||
|
return FileDirSelector(L, path, prompt, true, true, (flags & EXPLORER));
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_find(lua_State* L) {
|
||||||
|
bool extra = CheckLuaArgCountPlusExtra(L, 1, "_fs.find");
|
||||||
|
const char* pattern = luaL_checkstring(L, 1);
|
||||||
|
char path[_VAR_CNT_LEN] = { 0 };
|
||||||
|
|
||||||
|
u32 flags = 0;
|
||||||
|
if (extra) {
|
||||||
|
flags = GetFlagsFromTable(L, 2, flags, FIND_FIRST);
|
||||||
|
}
|
||||||
|
|
||||||
|
u8 mode = (flags & FIND_FIRST) ? FN_LOWEST : FN_HIGHEST;
|
||||||
|
FRESULT res = fvx_findpath(path, pattern, mode);
|
||||||
|
if (res == FR_NO_PATH) {
|
||||||
|
lua_pushnil(L);
|
||||||
|
} else if (res != FR_OK) {
|
||||||
|
return luaL_error(L, "failed to find %s (%d)", pattern, res);
|
||||||
|
} else {
|
||||||
|
lua_pushstring(L, path);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// this should probably be rewritten
|
||||||
|
static int internalfs_find_all(lua_State* L) {
|
||||||
|
bool extra = CheckLuaArgCountPlusExtra(L, 2, "_fs.find_all");
|
||||||
|
const char* dir = luaL_checkstring(L, 1);
|
||||||
|
const char* pattern = luaL_checkstring(L, 2);
|
||||||
|
|
||||||
|
u32 flags = 0;
|
||||||
|
if (extra) {
|
||||||
|
flags = GetFlagsFromTable(L, 3, flags, RECURSIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
char forpath[_VAR_CNT_LEN] = { 0 };
|
||||||
|
|
||||||
|
// without re-implementing for_handler, i need to give it a "*" pattern
|
||||||
|
// and then manually compare each filename to see if it matches
|
||||||
|
// so that a recursive search actually works
|
||||||
|
bool forstatus = for_handler(NULL, dir, "*", flags & RECURSIVE);
|
||||||
|
if (!forstatus) {
|
||||||
|
return luaL_error(L, "could not open directory");
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_newtable(L);
|
||||||
|
int i = 1;
|
||||||
|
char* slash;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
forstatus = for_handler(forpath, NULL, NULL, false);
|
||||||
|
if (!forstatus) {
|
||||||
|
forpath[0] = '\0';
|
||||||
|
}
|
||||||
|
if (!forpath[0]) {
|
||||||
|
// finish for_handler
|
||||||
|
for_handler(NULL, NULL, NULL, false);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
slash = strrchr(forpath, '/');
|
||||||
|
if (!slash) bkpt; // this should never, ever happen
|
||||||
|
if (fvx_match_name(slash+1, pattern) == FR_OK) {
|
||||||
|
lua_pushstring(L, forpath);
|
||||||
|
lua_seti(L, -2, i++);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_find_not(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "_fs.find_not");
|
||||||
|
const char* pattern = luaL_checkstring(L, 1);
|
||||||
|
char path[_VAR_CNT_LEN] = { 0 };
|
||||||
|
|
||||||
|
FRESULT res = fvx_findnopath(path, pattern);
|
||||||
|
if (res != FR_OK) {
|
||||||
|
return luaL_error(L, "failed to find %s (%d)", pattern, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pushstring(L, path);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_exists(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "_fs.exists");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
FILINFO fno;
|
||||||
|
|
||||||
|
lua_pushboolean(L, (fvx_stat(path, &fno) == FR_OK));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_is_dir(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "_fs.is_dir");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
lua_pushboolean(L, PathIsDirectory(path));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_is_file(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "_fs.is_file");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
FILINFO fno;
|
||||||
|
|
||||||
|
FRESULT res = fvx_stat(path, &fno);
|
||||||
|
if (res != FR_OK) {
|
||||||
|
lua_pushboolean(L, false);
|
||||||
|
} else {
|
||||||
|
lua_pushboolean(L, !(fno.fattrib & AM_DIR));
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_read_file(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 3, "_fs.read_file");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
lua_Integer offset = luaL_checkinteger(L, 2);
|
||||||
|
lua_Integer size = luaL_checkinteger(L, 3);
|
||||||
|
|
||||||
|
char *buf = malloc(size);
|
||||||
|
if (!buf) {
|
||||||
|
return luaL_error(L, "could not allocate memory to read file");
|
||||||
|
}
|
||||||
|
UINT bytes_read = 0;
|
||||||
|
FRESULT res = fvx_qread(path, buf, offset, size, &bytes_read);
|
||||||
|
if (res != FR_OK) {
|
||||||
|
free(buf);
|
||||||
|
return luaL_error(L, "could not read %s (%d)", path, res);
|
||||||
|
}
|
||||||
|
lua_pushlstring(L, buf, bytes_read);
|
||||||
|
free(buf);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_write_file(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 3, "_fs.write_file");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
lua_Integer offset = luaL_checkinteger(L, 2);
|
||||||
|
size_t data_length = 0;
|
||||||
|
const char* data = luaL_checklstring(L, 3, &data_length);
|
||||||
|
|
||||||
|
CheckWritePermissionsLuaError(L, path);
|
||||||
|
|
||||||
|
UINT bytes_written = 0;
|
||||||
|
FRESULT res = fvx_qwrite(path, data, offset, data_length, &bytes_written);
|
||||||
|
if (res != FR_OK) {
|
||||||
|
return luaL_error(L, "error writing %s (%d)", path, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pushinteger(L, bytes_written);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_fill_file(lua_State* L) {
|
||||||
|
bool extra = CheckLuaArgCountPlusExtra(L, 4, "_fs.fill_file");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
lua_Integer offset = luaL_checkinteger(L, 2);
|
||||||
|
lua_Integer size = luaL_checkinteger(L, 3);
|
||||||
|
lua_Integer byte = luaL_checkinteger(L, 4);
|
||||||
|
|
||||||
|
u8 real_byte = byte & 0xFF;
|
||||||
|
|
||||||
|
u32 flags = ALLOW_EXPAND;
|
||||||
|
if (extra) {
|
||||||
|
flags = GetFlagsFromTable(L, 4, flags, NO_CANCEL);
|
||||||
|
};
|
||||||
|
|
||||||
|
if ((byte < 0) || (byte > 0xFF)) {
|
||||||
|
return luaL_error(L, "byte is not between 0x00 and 0xFF (got: %I)", byte);
|
||||||
|
}
|
||||||
|
|
||||||
|
CheckWritePermissionsLuaError(L, path);
|
||||||
|
|
||||||
|
if (!(FileSetByte(path, offset, size, real_byte, &flags))) {
|
||||||
|
return luaL_error(L, "FileSetByte failed on %s", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_make_dummy_file(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 2, "_fs.make_dummy_file");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
lua_Integer size = luaL_checkinteger(L, 2);
|
||||||
|
|
||||||
|
CheckWritePermissionsLuaError(L, path);
|
||||||
|
|
||||||
|
if (!(FileCreateDummy(path, NULL, size))) {
|
||||||
|
return luaL_error(L, "FileCreateDummy failed on %s", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_truncate(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 2, "_fs.truncate");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
lua_Integer size = luaL_checkinteger(L, 2);
|
||||||
|
FIL fp;
|
||||||
|
FRESULT res;
|
||||||
|
|
||||||
|
CheckWritePermissionsLuaError(L, path);
|
||||||
|
|
||||||
|
res = f_open(&fp, path, FA_READ | FA_WRITE);
|
||||||
|
if (res != FR_OK) {
|
||||||
|
return luaL_error(L, "failed to open %s (note: this only works on FAT filesystems, not virtual)", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
res = f_lseek(&fp, size);
|
||||||
|
if (res != FR_OK) {
|
||||||
|
f_close(&fp);
|
||||||
|
return luaL_error(L, "failed to seek on %s", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
res = f_truncate(&fp);
|
||||||
|
if (res != FR_OK) {
|
||||||
|
f_close(&fp);
|
||||||
|
return luaL_error(L, "failed to truncate %s", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
f_close(&fp);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_img_mount(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "_fs.img_mount");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
bool res = InitImgFS(path);
|
||||||
|
if (!res) {
|
||||||
|
return luaL_error(L, "failed to mount %s", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_img_umount(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 0, "_fs.img_umount");
|
||||||
|
|
||||||
|
InitImgFS(NULL);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_get_img_mount(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 0, "_fs.get_img_mount");
|
||||||
|
|
||||||
|
char path[256] = { 0 };
|
||||||
|
strncpy(path, GetMountPath(), 256);
|
||||||
|
if (path[0] == 0) {
|
||||||
|
// since lua treats "" as true, return a nil to make if/else easier
|
||||||
|
lua_pushnil(L);
|
||||||
|
} else {
|
||||||
|
lua_pushstring(L, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: what if someone does offset != 0 but size = 0 (end of file)?
|
||||||
|
static int internalfs_hash_file(lua_State* L) {
|
||||||
|
bool extra = CheckLuaArgCountPlusExtra(L, 3, "_fs.hash_file");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
lua_Integer offset = luaL_checkinteger(L, 2);
|
||||||
|
lua_Integer size = luaL_checkinteger(L, 3);
|
||||||
|
FRESULT res;
|
||||||
|
FILINFO fno;
|
||||||
|
|
||||||
|
if (size == 0) {
|
||||||
|
res = fvx_stat(path, &fno);
|
||||||
|
if (res != FR_OK) {
|
||||||
|
return luaL_error(L, "failed to stat %s", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
size = fno.fsize;
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 flags = 0;
|
||||||
|
if (extra) {
|
||||||
|
flags = GetFlagsFromTable(L, 4, flags, USE_SHA1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const u8 hashlen = (flags & USE_SHA1) ? 20 : 32;
|
||||||
|
u8 hash_fil[0x20];
|
||||||
|
|
||||||
|
if (size == 0) {
|
||||||
|
// shortcut by just returning the hash of empty data
|
||||||
|
memcpy(hash_fil, (flags & USE_SHA1) ? no_data_hash_1 : no_data_hash_256, hashlen);
|
||||||
|
} else if (!(FileGetSha(path, hash_fil, offset, size, (flags & USE_SHA1)))) {
|
||||||
|
return luaL_error(L, "FileGetSha failed on %s", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pushlstring(L, (char*)hash_fil, hashlen);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_hash_data(lua_State* L) {
|
||||||
|
bool extra = CheckLuaArgCountPlusExtra(L, 1, "_fs.hash_data");
|
||||||
|
size_t data_length = 0;
|
||||||
|
const char* data = luaL_checklstring(L, 1, &data_length);
|
||||||
|
|
||||||
|
u32 flags = 0;
|
||||||
|
if (extra) {
|
||||||
|
flags = GetFlagsFromTable(L, 2, flags, USE_SHA1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const u8 hashlen = (flags & USE_SHA1) ? 20 : 32;
|
||||||
|
u8 hash_fil[0x20];
|
||||||
|
|
||||||
|
if (data_length == 0) {
|
||||||
|
// shortcut by just returning the hash of empty data
|
||||||
|
memcpy(hash_fil, (flags & USE_SHA1) ? no_data_hash_1 : no_data_hash_256, hashlen);
|
||||||
|
} else {
|
||||||
|
sha_quick(hash_fil, data, data_length, (flags & USE_SHA1) ? SHA1_MODE : SHA256_MODE);
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pushlstring(L, (char*)hash_fil, hashlen);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_allow(lua_State* L) {
|
||||||
|
bool extra = CheckLuaArgCountPlusExtra(L, 1, "_fs.allow");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
u32 flags = 0;
|
||||||
|
bool allowed;
|
||||||
|
if (extra) {
|
||||||
|
flags = GetFlagsFromTable(L, 2, 0, ASK_ALL);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flags & ASK_ALL) {
|
||||||
|
allowed = CheckDirWritePermissions(path);
|
||||||
|
} else {
|
||||||
|
allowed = CheckWritePermissions(path);
|
||||||
|
}
|
||||||
|
lua_pushboolean(L, allowed);
|
||||||
|
return 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int internalfs_verify(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "_fs.verify");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
bool res;
|
||||||
|
|
||||||
|
u64 filetype = IdentifyFileType(path);
|
||||||
|
if (filetype & IMG_NAND) res = (ValidateNandDump(path) == 0);
|
||||||
|
else res = (VerifyGameFile(path) == 0);
|
||||||
|
|
||||||
|
lua_pushboolean(L, res);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_sd_is_mounted(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 0, "_fs.sd_is_mounted");
|
||||||
|
|
||||||
|
lua_pushboolean(L, CheckSDMountState());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_sd_switch(lua_State* L) {
|
||||||
|
bool extra = CheckLuaArgCountPlusExtra(L, 0, "_fs.sd_switch");
|
||||||
|
const char* message;
|
||||||
|
|
||||||
|
if (extra) {
|
||||||
|
message = luaL_checkstring(L, 1);
|
||||||
|
} else {
|
||||||
|
message = "Please switch the SD card now.";
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ret;
|
||||||
|
|
||||||
|
DeinitExtFS();
|
||||||
|
if (!(ret = CheckSDMountState())) {
|
||||||
|
return luaL_error(L, "%s", STR_SCRIPTERR_SD_NOT_MOUNTED);
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 pad_state;
|
||||||
|
DeinitSDCardFS();
|
||||||
|
ShowString("%s\n \n%s", message, STR_EJECT_SD_CARD);
|
||||||
|
while (!((pad_state = InputWait(0)) & (BUTTON_B|SD_EJECT)));
|
||||||
|
if (pad_state & SD_EJECT) {
|
||||||
|
ShowString("%s\n \n%s", message, STR_INSERT_SD_CARD);
|
||||||
|
while (!((pad_state = InputWait(0)) & (BUTTON_B|SD_INSERT)));
|
||||||
|
}
|
||||||
|
if (pad_state & BUTTON_B) {
|
||||||
|
return luaL_error(L, "user canceled");
|
||||||
|
}
|
||||||
|
|
||||||
|
InitSDCardFS();
|
||||||
|
AutoEmuNandBase(true);
|
||||||
|
InitExtFS();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_key_dump(lua_State* L) {
|
||||||
|
bool extra = CheckLuaArgCountPlusExtra(L, 1, "_fs.key_dump");
|
||||||
|
const char* opts[] = {SEEDINFO_NAME, TIKDB_NAME_ENC, TIKDB_NAME_DEC, NULL};
|
||||||
|
int opt = luaL_checkoption(L, 1, NULL, opts);
|
||||||
|
bool ret = false;
|
||||||
|
|
||||||
|
u32 flags = 0;
|
||||||
|
if (extra) {
|
||||||
|
flags = GetFlagsFromTable(L, 2, 0, OVERWRITE_ALL);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opt == 1 || opt == 2) {
|
||||||
|
bool tik_dec = opt == 2;
|
||||||
|
if (flags & OVERWRITE_ALL) fvx_unlink(tik_dec ? OUTPUT_PATH "/" TIKDB_NAME_DEC : OUTPUT_PATH "/" TIKDB_NAME_ENC);
|
||||||
|
if (BuildTitleKeyInfo(NULL, tik_dec, false) == 0) {
|
||||||
|
ShowString(STR_BUILDING_TO_OUT_ARG, OUTPUT_PATH, opts[opt]);
|
||||||
|
if (((BuildTitleKeyInfo("1:/dbs/ticket.db", tik_dec, false) == 0) ||
|
||||||
|
(BuildTitleKeyInfo("4:/dbs/ticket.db", tik_dec, false) == 0)) &&
|
||||||
|
(BuildTitleKeyInfo(NULL, tik_dec, true) == 0))
|
||||||
|
ret = true;
|
||||||
|
}
|
||||||
|
} else if (opt == 0) {
|
||||||
|
if (flags & OVERWRITE_ALL) fvx_unlink(OUTPUT_PATH "/" SEEDINFO_NAME);
|
||||||
|
if (BuildSeedInfo(NULL, false) == 0) {
|
||||||
|
ShowString(STR_BUILDING_TO_OUT_ARG, OUTPUT_PATH, opts[opt]);
|
||||||
|
if (((BuildSeedInfo("1:", false) == 0) ||
|
||||||
|
(BuildSeedInfo("4:", false) == 0)) &&
|
||||||
|
(BuildSeedInfo(NULL, true) == 0))
|
||||||
|
ret = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ret) {
|
||||||
|
return luaL_error(L, "building %s failed", opts[opt]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalfs_cart_dump(lua_State* L) {
|
||||||
|
bool extra = CheckLuaArgCountPlusExtra(L, 2, "_fs.cart_dump");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
u64 fsize = (u64)luaL_checkinteger(L, 2);
|
||||||
|
bool ret = false;
|
||||||
|
const char* errstr = "";
|
||||||
|
|
||||||
|
u32 flags = 0;
|
||||||
|
if (extra) {
|
||||||
|
flags = GetFlagsFromTable(L, 3, flags, ENCRYPTED);
|
||||||
|
}
|
||||||
|
|
||||||
|
CartData* cdata = (CartData*) malloc(sizeof(CartData));
|
||||||
|
u8* buf = (u8*) malloc(STD_BUFFER_SIZE);
|
||||||
|
ret = false;
|
||||||
|
if (!cdata || !buf) {
|
||||||
|
errstr = "out of memory";
|
||||||
|
} else if (InitCartRead(cdata) != 0){
|
||||||
|
errstr = "cart init fail";
|
||||||
|
} else {
|
||||||
|
SetSecureAreaEncryption(flags & ENCRYPTED);
|
||||||
|
fvx_unlink(path);
|
||||||
|
ret = true;
|
||||||
|
errstr = "cart dump failed or canceled";
|
||||||
|
for (u64 p = 0; p < fsize; p += STD_BUFFER_SIZE) {
|
||||||
|
u64 len = min((fsize - p), STD_BUFFER_SIZE);
|
||||||
|
ShowProgress(p, fsize, path);
|
||||||
|
if (!ShowProgress(p, fsize, path) ||
|
||||||
|
(ReadCartBytes(buf, p, len, cdata, false) != 0) ||
|
||||||
|
(fvx_qwrite(path, buf, p, len, NULL) != FR_OK)) {
|
||||||
|
ret = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(buf);
|
||||||
|
free(cdata);
|
||||||
|
|
||||||
|
if (!ret) {
|
||||||
|
return luaL_error(L, "%s", errstr);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const luaL_Reg internalfs_lib[] = {
|
||||||
|
{"move", internalfs_move},
|
||||||
|
{"remove", internalfs_remove},
|
||||||
|
{"copy", internalfs_copy},
|
||||||
|
{"mkdir", internalfs_mkdir},
|
||||||
|
{"list_dir", internalfs_list_dir},
|
||||||
|
{"stat", internalfs_stat},
|
||||||
|
{"stat_fs", internalfs_stat_fs},
|
||||||
|
{"dir_info", internalfs_dir_info},
|
||||||
|
{"ask_select_file", internalfs_ask_select_file},
|
||||||
|
{"ask_select_dir", internalfs_ask_select_dir},
|
||||||
|
{"find", internalfs_find},
|
||||||
|
{"find_all", internalfs_find_all},
|
||||||
|
{"find_not", internalfs_find_not},
|
||||||
|
{"exists", internalfs_exists},
|
||||||
|
{"is_dir", internalfs_is_dir},
|
||||||
|
{"is_file", internalfs_is_file},
|
||||||
|
{"read_file", internalfs_read_file},
|
||||||
|
{"write_file", internalfs_write_file},
|
||||||
|
{"fill_file", internalfs_fill_file},
|
||||||
|
{"make_dummy_file", internalfs_make_dummy_file},
|
||||||
|
{"truncate", internalfs_truncate},
|
||||||
|
{"img_mount", internalfs_img_mount},
|
||||||
|
{"img_umount", internalfs_img_umount},
|
||||||
|
{"get_img_mount", internalfs_get_img_mount},
|
||||||
|
{"hash_file", internalfs_hash_file},
|
||||||
|
{"hash_data", internalfs_hash_data},
|
||||||
|
{"verify", internalfs_verify},
|
||||||
|
{"allow", internalfs_allow},
|
||||||
|
{"sd_is_mounted", internalfs_sd_is_mounted},
|
||||||
|
{"sd_switch", internalfs_sd_switch},
|
||||||
|
{"fix_cmacs", internalfs_fix_cmacs},
|
||||||
|
{"key_dump", internalfs_key_dump},
|
||||||
|
{"cart_dump", internalfs_cart_dump},
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
int gm9lua_open_internalfs(lua_State* L) {
|
||||||
|
luaL_newlib(L, internalfs_lib);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#endif
|
23
arm9/source/lua/gm9internalfs.h
Normal file
23
arm9/source/lua/gm9internalfs.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "gm9lua.h"
|
||||||
|
|
||||||
|
#define GM9LUA_INTERNALFSLIBNAME "_fs"
|
||||||
|
|
||||||
|
#define SHA256_EMPTY_HASH \
|
||||||
|
0xE3, 0xB0, 0xC4, 0x42, \
|
||||||
|
0x98, 0xFC, 0x1C, 0x14, \
|
||||||
|
0x9A, 0xFB, 0xF4, 0xC8, \
|
||||||
|
0x99, 0x6F, 0xB9, 0x24, \
|
||||||
|
0x27, 0xAE, 0x41, 0xE4, \
|
||||||
|
0x64, 0x9B, 0x93, 0x4C, \
|
||||||
|
0xA4, 0x95, 0x99, 0x1B, \
|
||||||
|
0x78, 0x52, 0xB8, 0x55
|
||||||
|
|
||||||
|
#define SHA1_EMPTY_HASH \
|
||||||
|
0xDA, 0x39, 0xA3, 0xEE, \
|
||||||
|
0x5E, 0x6B, 0x4B, 0x0D, \
|
||||||
|
0x32, 0x55, 0xBF, 0xEF, \
|
||||||
|
0x95, 0x60, 0x18, 0x90, \
|
||||||
|
0xAF, 0xD8, 0x07, 0x09
|
||||||
|
|
||||||
|
int gm9lua_open_internalfs(lua_State* L);
|
181
arm9/source/lua/gm9internalsys.c
Normal file
181
arm9/source/lua/gm9internalsys.c
Normal file
@ -0,0 +1,181 @@
|
|||||||
|
#ifndef NO_LUA
|
||||||
|
#include "gm9internalsys.h"
|
||||||
|
#include "bootfirm.h"
|
||||||
|
#include "fs.h"
|
||||||
|
#include "pxi.h"
|
||||||
|
#include "game.h"
|
||||||
|
#include "power.h"
|
||||||
|
#include "sha.h"
|
||||||
|
#include "nand.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include "ui.h"
|
||||||
|
#include "rtc.h"
|
||||||
|
#include "godmode.h"
|
||||||
|
|
||||||
|
#define UNUSED(x) ((void)(x))
|
||||||
|
|
||||||
|
static int internalsys_boot(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "_sys.boot");
|
||||||
|
const char* path = lua_tostring(L, 1);
|
||||||
|
|
||||||
|
u8* firm = (u8*) malloc(FIRM_MAX_SIZE);
|
||||||
|
if (!firm) {
|
||||||
|
return luaL_error(L, "out of memory");
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t firm_size = FileGetData(path, firm, FIRM_MAX_SIZE, 0);
|
||||||
|
if (!(firm_size && IsBootableFirm(firm, firm_size))) {
|
||||||
|
return luaL_error(L, "not a bootable firm");
|
||||||
|
}
|
||||||
|
|
||||||
|
char fixpath[256] = { 0 };
|
||||||
|
if ((*path == '0') || (*path == '1'))
|
||||||
|
snprintf(fixpath, sizeof(fixpath), "%s%s", (*path == '0') ? "sdmc" : "nand", path + 1);
|
||||||
|
else strncpy(fixpath, path, 256);
|
||||||
|
fixpath[255] = '\0';
|
||||||
|
DeinitExtFS();
|
||||||
|
DeinitSDCardFS();
|
||||||
|
PXI_DoCMD(PXICMD_LEGACY_BOOT, NULL, 0);
|
||||||
|
PXI_Barrier(PXI_FIRMLAUNCH_BARRIER);
|
||||||
|
BootFirm((FirmHeader*)(void*)firm, fixpath);
|
||||||
|
while(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalsys_reboot(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 0, "_sys.reboot");
|
||||||
|
DeinitExtFS();
|
||||||
|
DeinitSDCardFS();
|
||||||
|
Reboot();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalsys_power_off(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 0, "_sys.power_off");
|
||||||
|
DeinitExtFS();
|
||||||
|
DeinitSDCardFS();
|
||||||
|
PowerOff();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalsys_get_id0(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "_sys.get_id0");
|
||||||
|
const char* path = lua_tostring(L, 1);
|
||||||
|
|
||||||
|
char env_id0[32+1];
|
||||||
|
u8 sd_keyy[0x10] __attribute__((aligned(4)));
|
||||||
|
if (FileGetData(path, sd_keyy, 0x10, 0x110) == 0x10) {
|
||||||
|
u32 sha256sum[8];
|
||||||
|
sha_quick(sha256sum, sd_keyy, 0x10, SHA256_MODE);
|
||||||
|
snprintf(env_id0, sizeof(env_id0), "%08lx%08lx%08lx%08lx",
|
||||||
|
sha256sum[0], sha256sum[1], sha256sum[2], sha256sum[3]);
|
||||||
|
lua_pushstring(L, env_id0);
|
||||||
|
} else {
|
||||||
|
lua_pushnil(L);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalsys_next_emu(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 0, "_sys.next_emu");
|
||||||
|
|
||||||
|
DismountDriveType(DRV_EMUNAND);
|
||||||
|
AutoEmuNandBase(false);
|
||||||
|
InitExtFS();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalsys_get_emu_base(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 0, "_sys.get_emu_base");
|
||||||
|
|
||||||
|
lua_pushinteger(L, GetEmuNandBase());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalsys_check_embedded_backup(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 0, "_sys.check_embedded_backup");
|
||||||
|
|
||||||
|
if (PathExist("S:/essential.exefs")) {
|
||||||
|
lua_pushboolean(L, true);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ncsd_check = CheckGenuineNandNcsd();
|
||||||
|
if (!ncsd_check) {
|
||||||
|
lua_pushnil(L);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ret = false;
|
||||||
|
|
||||||
|
if (ncsd_check && ShowPrompt(true, "%s", STR_ESSENTIAL_BACKUP_NOT_FOUND_CREATE_NOW)) {
|
||||||
|
if (EmbedEssentialBackup("S:/nand.bin") == 0) {
|
||||||
|
u32 flags = BUILD_PATH | SKIP_ALL;
|
||||||
|
PathCopy(OUTPUT_PATH, "S:/essential.exefs", &flags);
|
||||||
|
ShowPrompt(false, STR_BACKUP_EMBEDDED_WRITTEN_TO_OUT, OUTPUT_PATH);
|
||||||
|
ret = true;
|
||||||
|
} else {
|
||||||
|
ret = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ret = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pushboolean(L, ret);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalsys_check_raw_rtc(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 0, "_sys.check_raw_rtc");
|
||||||
|
|
||||||
|
bool result = false;
|
||||||
|
|
||||||
|
DsTime dstime;
|
||||||
|
get_dstime(&dstime);
|
||||||
|
if (DSTIMEGET(&dstime, bcd_Y) >= 18) {
|
||||||
|
result = true;
|
||||||
|
} else if (ShowPrompt(true, "%s", STR_RTC_DATE_TIME_SEEMS_TO_BE_WRONG_SET_NOW) &&
|
||||||
|
ShowRtcSetterPrompt(&dstime, "%s", STR_TITLE_SET_RTC_DATE_TIME)) {
|
||||||
|
//char timestr[UTF_BUFFER_BYTESIZE(32)];
|
||||||
|
set_dstime(&dstime);
|
||||||
|
// this is only in godmode.h
|
||||||
|
//GetTimeString(timestr, true, true);
|
||||||
|
// ShowPrompt(false, STR_NEW_RTC_DATE_TIME_IS_TIME, timestr);
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pushboolean(L, result);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int internalsys_global_bkpt(lua_State* L) {
|
||||||
|
UNUSED(L);
|
||||||
|
bkpt;
|
||||||
|
while(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const luaL_Reg internalsys_lib[] = {
|
||||||
|
{"boot", internalsys_boot},
|
||||||
|
{"reboot", internalsys_reboot},
|
||||||
|
{"power_off", internalsys_power_off},
|
||||||
|
{"get_id0", internalsys_get_id0},
|
||||||
|
{"next_emu", internalsys_next_emu},
|
||||||
|
{"get_emu_base", internalsys_get_emu_base},
|
||||||
|
{"check_embedded_backup", internalsys_check_embedded_backup},
|
||||||
|
{"check_raw_rtc", internalsys_check_raw_rtc},
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
static const luaL_Reg internalsys_global_lib[] = {
|
||||||
|
{"bkpt", internalsys_global_bkpt},
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
int gm9lua_open_internalsys(lua_State* L) {
|
||||||
|
luaL_newlib(L, internalsys_lib);
|
||||||
|
lua_pushglobaltable(L); // push global table to stack
|
||||||
|
luaL_setfuncs(L, internalsys_global_lib, 0); // set global funcs
|
||||||
|
lua_pop(L, 1); // pop global table from stack
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#endif
|
6
arm9/source/lua/gm9internalsys.h
Normal file
6
arm9/source/lua/gm9internalsys.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "gm9lua.h"
|
||||||
|
|
||||||
|
#define GM9LUA_INTERNALSYSLIBNAME "_sys"
|
||||||
|
|
||||||
|
int gm9lua_open_internalsys(lua_State* L);
|
128
arm9/source/lua/gm9loader.c
Normal file
128
arm9/source/lua/gm9loader.c
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
#ifndef NO_LUA
|
||||||
|
#include "gm9loader.h"
|
||||||
|
#include "gm9lua.h"
|
||||||
|
#include "vff.h"
|
||||||
|
#include "ui.h"
|
||||||
|
|
||||||
|
// a lot of this code is based on stuff in loadlib.c but adapted for GM9
|
||||||
|
|
||||||
|
// similar to readable
|
||||||
|
static int Readable(const char* filename) {
|
||||||
|
FIL f;
|
||||||
|
FRESULT res = fvx_open(&f, filename, FA_READ | FA_OPEN_EXISTING);
|
||||||
|
if (res != FR_OK) return 0;
|
||||||
|
fvx_close(&f);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// similar to getnextfilename
|
||||||
|
static const char* GetNextFileName(char** path, char* end) {
|
||||||
|
char *sep;
|
||||||
|
char *name = *path;
|
||||||
|
if (name == end)
|
||||||
|
return NULL; /* no more names */
|
||||||
|
else if (*name == '\0') { /* from previous iteration? */
|
||||||
|
*name = *LUA_PATH_SEP; /* restore separator */
|
||||||
|
name++; /* skip it */
|
||||||
|
}
|
||||||
|
sep = strchr(name, *LUA_PATH_SEP); /* find next separator */
|
||||||
|
if (sep == NULL) /* separator not found? */
|
||||||
|
sep = end; /* name goes until the end */
|
||||||
|
*sep = '\0'; /* finish file name */
|
||||||
|
*path = sep; /* will start next search from here */
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// similar to pusherrornotfound
|
||||||
|
static void PushErrorNotFound(lua_State* L, const char* path) {
|
||||||
|
luaL_Buffer b;
|
||||||
|
luaL_buffinit(L, &b);
|
||||||
|
luaL_addstring(&b, "no file '");
|
||||||
|
luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '");
|
||||||
|
luaL_addstring(&b, "'");
|
||||||
|
luaL_pushresult(&b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// similar to searchpath
|
||||||
|
static const char* SearchPath(lua_State* L, const char* name, const char* path, const char* sep) {
|
||||||
|
luaL_Buffer buff;
|
||||||
|
char* pathname;
|
||||||
|
char* endpathname;
|
||||||
|
const char* filename;
|
||||||
|
if (*sep != '\0' && strchr(name, *sep) != NULL)
|
||||||
|
name = luaL_gsub(L, name, sep, "/");
|
||||||
|
|
||||||
|
luaL_buffinit(L, &buff);
|
||||||
|
// add path to the buffer, replacing marks ('?') with the file name
|
||||||
|
luaL_addgsub(&buff, path, LUA_PATH_MARK, name);
|
||||||
|
luaL_addchar(&buff, '\0');
|
||||||
|
pathname = luaL_buffaddr(&buff);
|
||||||
|
endpathname = pathname + luaL_bufflen(&buff) + 1;
|
||||||
|
while ((filename = GetNextFileName(&pathname, endpathname)) != NULL) {
|
||||||
|
if (Readable(filename))
|
||||||
|
return lua_pushstring(L, filename);
|
||||||
|
}
|
||||||
|
luaL_pushresult(&buff);
|
||||||
|
PushErrorNotFound(L, lua_tostring(L, -1));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// similar to findfile
|
||||||
|
static const char* FindLuaFile(lua_State* L, const char* name, const char* pname) {
|
||||||
|
const char* path;
|
||||||
|
lua_getfield(L, lua_upvalueindex(1), pname); // gets 'package' table
|
||||||
|
path = lua_tostring(L, -1);
|
||||||
|
if (path == NULL) luaL_error(L, "'package.%s' must be a string", pname);
|
||||||
|
return SearchPath(L, name, path, ".");
|
||||||
|
}
|
||||||
|
|
||||||
|
// similar to checkload
|
||||||
|
static int CheckLoad(lua_State* L, int stat, const char* filename) {
|
||||||
|
if (stat) {
|
||||||
|
lua_pushstring(L, filename);
|
||||||
|
return 2; // return open function and filename
|
||||||
|
} else {
|
||||||
|
return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s",
|
||||||
|
lua_tostring(L, 1), filename, lua_tostring(L, -1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// similar to searcher_Lua
|
||||||
|
static int PackageSearcher(lua_State* L) {
|
||||||
|
const char *filename;
|
||||||
|
const char *name = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
filename = FindLuaFile(L, name, "path");
|
||||||
|
|
||||||
|
if (filename == NULL) return 1; // module not found in this path
|
||||||
|
return CheckLoad(L, (LoadLuaFile(L, filename) == LUA_OK), filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ResetPackageSearchersAndPath(lua_State* L) {
|
||||||
|
// get package module
|
||||||
|
lua_getglobal(L, "package");
|
||||||
|
|
||||||
|
// the default package.path only makes sense on a full OS
|
||||||
|
// maybe this should include the lua script's current directory somehow...
|
||||||
|
lua_pushliteral(L, GM9LUA_DEFAULT_PATH);
|
||||||
|
lua_setfield(L, -2, "path");
|
||||||
|
|
||||||
|
// package.cpath is for loading binary modules, useless on GM9
|
||||||
|
lua_pushliteral(L, "");
|
||||||
|
lua_setfield(L, -2, "cpath");
|
||||||
|
|
||||||
|
// the default package searchers only make sense on a full OS
|
||||||
|
// so here we replace the lua loader with a custom one, and remove the C/Croot loaders
|
||||||
|
// leaving the initial one (preload)
|
||||||
|
lua_getfield(L, -1, "searchers");
|
||||||
|
lua_pushvalue(L, -2); // copy 'package' to the top of the stack, to set 'package' as upvalue for all searchers
|
||||||
|
lua_pushcclosure(L, PackageSearcher, 1); // push PackageSearcher with one upvalue being the "packages" module/table
|
||||||
|
lua_rawseti(L, -2, 2); // replace default lua loader
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_rawseti(L, -2, 3); // remove C loader
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_rawseti(L, -2, 4); // remove C root loader
|
||||||
|
lua_pop(L, 1); // remove "searchers"
|
||||||
|
lua_pop(L, 1); // remove "packages"
|
||||||
|
}
|
||||||
|
#endif
|
15
arm9/source/lua/gm9loader.h
Normal file
15
arm9/source/lua/gm9loader.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#include "gm9lua.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 0:/gm9/luapackages/?.lua;
|
||||||
|
* 0:/gm9/luapackages/?/init.lua;
|
||||||
|
* V:/luapackages/?.lua;
|
||||||
|
* V:/luapackages/?/init.lua
|
||||||
|
*/
|
||||||
|
#define GM9LUA_DEFAULT_PATH \
|
||||||
|
"0:/gm9/luapackages/"LUA_PATH_MARK".lua" LUA_PATH_SEP \
|
||||||
|
"0:/gm9/luapackages/"LUA_PATH_MARK"/init.lua" LUA_PATH_SEP \
|
||||||
|
"V:/luapackages/"LUA_PATH_MARK".lua" LUA_PATH_SEP \
|
||||||
|
"V:/luapackages/"LUA_PATH_MARK"/init.lua"
|
||||||
|
|
||||||
|
void ResetPackageSearchersAndPath(lua_State* L);
|
216
arm9/source/lua/gm9lua.c
Normal file
216
arm9/source/lua/gm9lua.c
Normal file
@ -0,0 +1,216 @@
|
|||||||
|
#include "gm9lua.h"
|
||||||
|
#include "ui.h"
|
||||||
|
#include "language.h"
|
||||||
|
#ifndef NO_LUA
|
||||||
|
#include "fs.h"
|
||||||
|
#include "ff.h"
|
||||||
|
#include "vff.h"
|
||||||
|
#include "fsutil.h"
|
||||||
|
#include "unittype.h"
|
||||||
|
#include "nand.h"
|
||||||
|
#include "gm9loader.h"
|
||||||
|
#include "gm9os.h"
|
||||||
|
#include "gm9ui.h"
|
||||||
|
#include "gm9title.h"
|
||||||
|
#include "gm9internalfs.h"
|
||||||
|
#include "gm9internalsys.h"
|
||||||
|
|
||||||
|
#define DEBUGSP(x) ShowPrompt(false, (x))
|
||||||
|
|
||||||
|
typedef struct GM9LuaLoadF {
|
||||||
|
int n; // pre-read characters
|
||||||
|
FIL f;
|
||||||
|
FRESULT res;
|
||||||
|
char buff[BUFSIZ];
|
||||||
|
} GM9LuaLoadF;
|
||||||
|
|
||||||
|
// similar to "getF" in lauxlib.c
|
||||||
|
static const char* GetF(lua_State* L, void* ud, size_t* size) {
|
||||||
|
GM9LuaLoadF* lf = (GM9LuaLoadF*)ud;
|
||||||
|
UINT br = 0;
|
||||||
|
(void)L; // unused
|
||||||
|
if (lf->n > 0) { // check for pre-read characters
|
||||||
|
*size = lf->n; // return those
|
||||||
|
lf->n = 0;
|
||||||
|
} else {
|
||||||
|
if (fvx_eof(&lf->f)) return NULL;
|
||||||
|
lf->res = fvx_read(&lf->f, lf->buff, BUFSIZ, &br);
|
||||||
|
*size = (size_t)br;
|
||||||
|
if (lf->res != FR_OK) return NULL;
|
||||||
|
}
|
||||||
|
return lf->buff;
|
||||||
|
}
|
||||||
|
|
||||||
|
// similar to "errfile" in lauxlib.c
|
||||||
|
static int ErrFile(lua_State* L, const char* what, int fnameindex, FRESULT res) {
|
||||||
|
const char* filename = lua_tostring(L, fnameindex) + 1;
|
||||||
|
lua_pushfstring(L, "cannot %s %s:\nfatfs error %d", what, filename, res);
|
||||||
|
lua_remove(L, fnameindex);
|
||||||
|
return LUA_ERRFILE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int LoadLuaFile(lua_State* L, const char* filename) {
|
||||||
|
GM9LuaLoadF lf;
|
||||||
|
lf.n = 0;
|
||||||
|
int status;
|
||||||
|
int fnameindex = lua_gettop(L) + 1; // index of filename on the stack
|
||||||
|
lua_pushfstring(L, "@%s", filename);
|
||||||
|
lf.res = fvx_open(&lf.f, filename, FA_READ | FA_OPEN_EXISTING);
|
||||||
|
if (lf.res != FR_OK) return ErrFile(L, "open", fnameindex, lf.res);
|
||||||
|
|
||||||
|
status = lua_load(L, GetF, &lf, lua_tostring(L, -1), NULL);
|
||||||
|
fvx_close(&lf.f);
|
||||||
|
if (lf.res != FR_OK) {
|
||||||
|
lua_settop(L, fnameindex);
|
||||||
|
return ErrFile(L, "read", fnameindex, lf.res);
|
||||||
|
}
|
||||||
|
lua_remove(L, fnameindex);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 GetFlagsFromTable(lua_State* L, int pos, u32 flags_ext_starter, u32 allowed_flags) {
|
||||||
|
char types[FLAGS_COUNT][14] = { FLAGS_STR };
|
||||||
|
int types_int[FLAGS_COUNT] = { FLAGS_CONSTS };
|
||||||
|
u32 flags_ext = flags_ext_starter;
|
||||||
|
|
||||||
|
for (int i = 0; i < FLAGS_COUNT; i++) {
|
||||||
|
if (!(allowed_flags & types_int[i])) continue;
|
||||||
|
lua_getfield(L, pos, types[i]);
|
||||||
|
if (lua_toboolean(L, -1)) flags_ext |= types_int[i];
|
||||||
|
lua_pop(L, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return flags_ext;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CheckWritePermissionsLuaError(lua_State* L, const char* path) {
|
||||||
|
if (!CheckWritePermissions(path)) {
|
||||||
|
luaL_error(L, "writing not allowed: %s", path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static const luaL_Reg gm9lualibs[] = {
|
||||||
|
// built-ins
|
||||||
|
{LUA_GNAME, luaopen_base},
|
||||||
|
{LUA_LOADLIBNAME, luaopen_package},
|
||||||
|
{LUA_COLIBNAME, luaopen_coroutine},
|
||||||
|
{LUA_TABLIBNAME, luaopen_table},
|
||||||
|
{LUA_STRLIBNAME, luaopen_string},
|
||||||
|
{LUA_MATHLIBNAME, luaopen_math},
|
||||||
|
{LUA_UTF8LIBNAME, luaopen_utf8},
|
||||||
|
{LUA_DBLIBNAME, luaopen_debug},
|
||||||
|
|
||||||
|
// gm9 custom
|
||||||
|
{GM9LUA_OSLIBNAME, gm9lua_open_os},
|
||||||
|
{GM9LUA_UILIBNAME, gm9lua_open_ui},
|
||||||
|
{GM9LUA_TITLELIBNAME, gm9lua_open_title},
|
||||||
|
|
||||||
|
// gm9 custom internals (usually wrapped by a pure lua module)
|
||||||
|
{GM9LUA_INTERNALFSLIBNAME, gm9lua_open_internalfs},
|
||||||
|
{GM9LUA_INTERNALSYSLIBNAME, gm9lua_open_internalsys},
|
||||||
|
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
static void loadlibs(lua_State* L) {
|
||||||
|
const luaL_Reg* lib;
|
||||||
|
for (lib = gm9lualibs; lib->func; lib++) {
|
||||||
|
luaL_requiref(L, lib->name, lib->func, 1);
|
||||||
|
lua_pop(L, 1); // remove lib from stack
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool RunFile(lua_State* L, const char* file) {
|
||||||
|
int result = LoadLuaFile(L, file);
|
||||||
|
if (result != LUA_OK) {
|
||||||
|
char errstr[BUFSIZ] = {0};
|
||||||
|
strlcpy(errstr, lua_tostring(L, -1), BUFSIZ);
|
||||||
|
WordWrapString(errstr, 0);
|
||||||
|
ShowPrompt(false, "Error during loading:\n%s", errstr);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lua_pcall(L, 0, LUA_MULTRET, 0) != LUA_OK) {
|
||||||
|
char errstr[BUFSIZ] = {0};
|
||||||
|
strlcpy(errstr, lua_tostring(L, -1), BUFSIZ);
|
||||||
|
WordWrapString(errstr, 0);
|
||||||
|
ShowPrompt(false, "Error during execution:\n%s", errstr);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is also taken from scripting.c
|
||||||
|
static inline bool isntrboot(void) {
|
||||||
|
// taken over from Luma 3DS:
|
||||||
|
// https://github.com/AuroraWright/Luma3DS/blob/bb5518b0f68d89bcd8efaf326355a770d5e57856/source/main.c#L58-L62
|
||||||
|
const vu8 *bootMediaStatus = (const vu8 *) 0x1FFFE00C;
|
||||||
|
const vu32 *bootPartitionsStatus = (const vu32 *) 0x1FFFE010;
|
||||||
|
|
||||||
|
// shell closed, no error booting NTRCARD, NAND partitions not even considered
|
||||||
|
return (bootMediaStatus[3] == 2) && !bootMediaStatus[1] && !bootPartitionsStatus[0] && !bootPartitionsStatus[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ExecuteLuaScript(const char* path_script) {
|
||||||
|
lua_State* L = luaL_newstate();
|
||||||
|
loadlibs(L);
|
||||||
|
|
||||||
|
ResetPackageSearchersAndPath(L);
|
||||||
|
ClearOutputBuffer();
|
||||||
|
|
||||||
|
// current path
|
||||||
|
char curr_dir[_VAR_CNT_LEN];
|
||||||
|
if (path_script) {
|
||||||
|
strncpy(curr_dir, path_script, _VAR_CNT_LEN);
|
||||||
|
curr_dir[_VAR_CNT_LEN-1] = '\0';
|
||||||
|
char* slash = strrchr(curr_dir, '/');
|
||||||
|
if (slash) *slash = '\0';
|
||||||
|
|
||||||
|
lua_pushstring(L, curr_dir);
|
||||||
|
} else {
|
||||||
|
lua_pushnil(L);
|
||||||
|
}
|
||||||
|
lua_setglobal(L, "CURRDIR");
|
||||||
|
|
||||||
|
lua_pushliteral(L, VERSION);
|
||||||
|
lua_setglobal(L, "GM9VER");
|
||||||
|
|
||||||
|
lua_pushstring(L, path_script);
|
||||||
|
lua_setglobal(L, "SCRIPT");
|
||||||
|
|
||||||
|
lua_pushliteral(L, OUTPUT_PATH);
|
||||||
|
lua_setglobal(L, "GM9OUT");
|
||||||
|
|
||||||
|
lua_pushstring(L, IS_UNLOCKED ? (isntrboot() ? "ntrboot" : "sighax") : "");
|
||||||
|
lua_setglobal(L, "HAX");
|
||||||
|
|
||||||
|
lua_pushinteger(L, GetNandSizeSectors(NAND_SYSNAND) * 0x200);
|
||||||
|
lua_setglobal(L, "NANDSIZE");
|
||||||
|
|
||||||
|
lua_pushboolean(L, IS_DEVKIT);
|
||||||
|
lua_setglobal(L, "IS_DEVKIT");
|
||||||
|
|
||||||
|
lua_pushstring(L, IS_O3DS ? "O3DS" : "N3DS");
|
||||||
|
lua_setglobal(L, "CONSOLE_TYPE");
|
||||||
|
|
||||||
|
bool result = RunFile(L, "V:/preload.lua");
|
||||||
|
if (!result) {
|
||||||
|
ShowPrompt(false, "A fatal error happened in GodMode9's preload script.\n \nThis is not an error with your code, but with\nGodMode9. Please report it on GitHub.");
|
||||||
|
lua_close(L);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
RunFile(L, path_script);
|
||||||
|
|
||||||
|
lua_close(L);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
// No-Lua version
|
||||||
|
bool ExecuteLuaScript(const char* path_script) {
|
||||||
|
(void)path_script; // unused
|
||||||
|
ShowPrompt(false, "%s", STR_LUA_NOT_INCLUDED);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#endif
|
47
arm9/source/lua/gm9lua.h
Normal file
47
arm9/source/lua/gm9lua.h
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "common.h"
|
||||||
|
#include "lua.h"
|
||||||
|
#include "lauxlib.h"
|
||||||
|
#include "lualib.h"
|
||||||
|
#include "scripting.h"
|
||||||
|
|
||||||
|
// this should probably go in filesys/fsutil.h
|
||||||
|
#define RECURSIVE (1UL<<11)
|
||||||
|
#define TO_EMUNAND (1UL<<12)
|
||||||
|
#define LEGIT (1UL<<13)
|
||||||
|
#define FIND_FIRST (1UL<<14)
|
||||||
|
#define INCLUDE_DIRS (1UL<<15)
|
||||||
|
#define EXPLORER (1UL<<16)
|
||||||
|
#define ENCRYPTED (1UL<<17)
|
||||||
|
|
||||||
|
#define FLAGS_STR "no_cancel", "silent", "calc_sha", "sha1", "skip", "overwrite", "append", "all", "recursive", "to_emunand", "legit", "first", "include_dirs", "explorer", "encrypted"
|
||||||
|
#define FLAGS_CONSTS NO_CANCEL, SILENT, CALC_SHA, USE_SHA1, SKIP_ALL, OVERWRITE_ALL, APPEND_ALL, ASK_ALL, RECURSIVE, TO_EMUNAND, LEGIT, FIND_FIRST, INCLUDE_DIRS, EXPLORER, ENCRYPTED
|
||||||
|
#define FLAGS_COUNT 15
|
||||||
|
|
||||||
|
#define LUASCRIPT_EXT "lua"
|
||||||
|
#define LUASCRIPT_MAX_SIZE STD_BUFFER_SIZE
|
||||||
|
|
||||||
|
// taken from arm9/source/utils/scripting.c
|
||||||
|
#define _VAR_CNT_LEN 256
|
||||||
|
|
||||||
|
#ifndef NO_LUA
|
||||||
|
static inline void CheckLuaArgCount(lua_State* L, int argcount, const char* cmd) {
|
||||||
|
int args = lua_gettop(L);
|
||||||
|
if (args != argcount) {
|
||||||
|
luaL_error(L, "bad number of arguments passed to '%s' (expected %d, got %d)", cmd, argcount, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// this is used in cases where a function accepts a flags table or something else
|
||||||
|
static inline bool CheckLuaArgCountPlusExtra(lua_State* L, int argcount, const char* cmd) {
|
||||||
|
int args = lua_gettop(L);
|
||||||
|
if (args != argcount && args != argcount + 1) {
|
||||||
|
luaL_error(L, "bad number of arguments passed to '%s' (expected %d or %d, got %d)", cmd, argcount, argcount + 1, args);
|
||||||
|
}
|
||||||
|
return args == argcount + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int LoadLuaFile(lua_State* L, const char* filename);
|
||||||
|
u32 GetFlagsFromTable(lua_State* L, int pos, u32 flags_ext_starter, u32 allowed_flags);
|
||||||
|
void CheckWritePermissionsLuaError(lua_State* L, const char* path);
|
||||||
|
#endif
|
||||||
|
bool ExecuteLuaScript(const char* path_script);
|
686
arm9/source/lua/gm9os.c
Normal file
686
arm9/source/lua/gm9os.c
Normal file
@ -0,0 +1,686 @@
|
|||||||
|
#ifndef NO_LUA
|
||||||
|
#include "gm9os.h"
|
||||||
|
#include "timer.h"
|
||||||
|
#include "rtc.h"
|
||||||
|
#include "ui.h"
|
||||||
|
|
||||||
|
u64 osclock;
|
||||||
|
|
||||||
|
static inline bool isLeapYear(u32 year) {
|
||||||
|
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t getWeekday(bool abbreviated, char* out, u8 weekday) {
|
||||||
|
if (abbreviated) {
|
||||||
|
switch(weekday) {
|
||||||
|
case 1:
|
||||||
|
strcat(out, "Mon");
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
strcat(out, "Tue");
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
strcat(out, "Wed");
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
strcat(out, "Thu");
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
strcpy(out, "Fri");
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
strcat(out, "Sat");
|
||||||
|
break;
|
||||||
|
case 0:
|
||||||
|
case 7:
|
||||||
|
strcat(out, "Sun");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
strcat(out, "");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
switch(weekday) {
|
||||||
|
case 1:
|
||||||
|
strcat(out, "Monday");
|
||||||
|
return 6;
|
||||||
|
case 2:
|
||||||
|
strcat(out, "Tuesday");
|
||||||
|
return 7;
|
||||||
|
case 3:
|
||||||
|
strcat(out, "Wednesday");
|
||||||
|
return 9;
|
||||||
|
case 4:
|
||||||
|
strcat(out, "Thursday");
|
||||||
|
return 8;
|
||||||
|
case 5:
|
||||||
|
strcpy(out, "Friday");
|
||||||
|
return 6;
|
||||||
|
case 6:
|
||||||
|
strcat(out, "Saturday");
|
||||||
|
return 8;
|
||||||
|
case 7:
|
||||||
|
case 0:
|
||||||
|
strcat(out, "Sunday");
|
||||||
|
return 6;
|
||||||
|
default:
|
||||||
|
strcat(out, "");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t getMonthName(bool abbreviated, char* out, u8 month) {
|
||||||
|
if (abbreviated) {
|
||||||
|
switch(month) {
|
||||||
|
case 1:
|
||||||
|
strcat(out, "Jan");
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
strcat(out, "Feb");
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
strcat(out, "Mar");
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
strcat(out, "Apr");
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
strcat(out, "May");
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
strcat(out, "Jun");
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
strcat(out, "Jul");
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
|
strcat(out, "Aug");
|
||||||
|
break;
|
||||||
|
case 9:
|
||||||
|
strcat(out, "Sep");
|
||||||
|
break;
|
||||||
|
case 10:
|
||||||
|
strcat(out, "Oct");
|
||||||
|
break;
|
||||||
|
case 11:
|
||||||
|
strcat(out, "Nov");
|
||||||
|
break;
|
||||||
|
case 12:
|
||||||
|
strcat(out, "Dec");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
strcat(out, "");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
switch(month) {
|
||||||
|
case 1:
|
||||||
|
strcat(out, "January");
|
||||||
|
return 7;
|
||||||
|
case 2:
|
||||||
|
strcat(out, "February");
|
||||||
|
return 8;
|
||||||
|
case 3:
|
||||||
|
strcat(out, "March");
|
||||||
|
return 5;
|
||||||
|
case 4:
|
||||||
|
strcat(out, "April");
|
||||||
|
return 5;
|
||||||
|
case 5:
|
||||||
|
strcat(out, "May");
|
||||||
|
return 3;
|
||||||
|
case 6:
|
||||||
|
strcat(out, "Juny");
|
||||||
|
return 4;
|
||||||
|
case 7:
|
||||||
|
strcat(out, "July");
|
||||||
|
return 4;
|
||||||
|
case 8:
|
||||||
|
strcat(out, "August");
|
||||||
|
return 6;
|
||||||
|
case 9:
|
||||||
|
strcat(out, "September");
|
||||||
|
return 9;
|
||||||
|
case 10:
|
||||||
|
strcat(out, "October");
|
||||||
|
return 7;
|
||||||
|
case 11:
|
||||||
|
strcat(out, "November");
|
||||||
|
return 8;
|
||||||
|
case 12:
|
||||||
|
strcat(out, "December");
|
||||||
|
return 8;
|
||||||
|
default:
|
||||||
|
strcat(out, "");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
u16 getDaysMonths(u32 months, u8 years) {
|
||||||
|
u8 daysInMonth[12] = {31, isLeapYear(2000 + years) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //is this ?: bad practice?
|
||||||
|
u16 ret;
|
||||||
|
for (u32 month = 0; month < months - 1; month++) {
|
||||||
|
ret += daysInMonth[month];
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 calcUnixTime(u8 years, u8 months, u8 days, u8 hours, u8 minutes, u8 seconds) {
|
||||||
|
u8 daysInMonth[12] = {31, isLeapYear(2000 + years) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //is this ?: bad practice?
|
||||||
|
u32 curdays;
|
||||||
|
u64 ret = 0;
|
||||||
|
|
||||||
|
ret += seconds;
|
||||||
|
ret += minutes * 60;
|
||||||
|
ret += hours * 60 * 60;
|
||||||
|
ret += (days - 1) * 24 * 60 * 60;
|
||||||
|
|
||||||
|
for (u16 year = 0; year < years + 30; year++) { //+30 because unix time starting in 1970 but rtc starts in 2000
|
||||||
|
if (isLeapYear(2000 + year)) {
|
||||||
|
curdays = 366;
|
||||||
|
} else {
|
||||||
|
curdays = 365;
|
||||||
|
}
|
||||||
|
ret += curdays * 24 * 60 * 60;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (u16 month = 0; month < months - 1; month++) {
|
||||||
|
ret += daysInMonth[month] * 24 * 60 * 60;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 getUnixTimeFromRtc(DsTime *dstime) {
|
||||||
|
|
||||||
|
u8
|
||||||
|
seconds = DSTIMEGET(dstime, bcd_s),
|
||||||
|
minutes = DSTIMEGET(dstime, bcd_m),
|
||||||
|
hours = DSTIMEGET(dstime, bcd_h),
|
||||||
|
days = DSTIMEGET(dstime, bcd_D),
|
||||||
|
months = DSTIMEGET(dstime, bcd_M),
|
||||||
|
years = DSTIMEGET(dstime, bcd_Y);
|
||||||
|
return calcUnixTime(years, months, days, hours, minutes, seconds);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 timer_usec( u64 start_time ) {
|
||||||
|
return timer_ticks( start_time ) / (TICKS_PER_SEC/1000000);
|
||||||
|
}
|
||||||
|
|
||||||
|
void weekdayfix(DsTime *dstime) {
|
||||||
|
int days = getUnixTimeFromRtc(dstime) / 86400; //days since thursday 1 1 1970
|
||||||
|
u8 weekday = (days + 5) % 7;
|
||||||
|
dstime->weekday = NUM2BCD(weekday);
|
||||||
|
}
|
||||||
|
|
||||||
|
void unixtodstime(u64 unixtime, DsTime *dstime) {
|
||||||
|
u32 seconds, minutes, hours, days, year, month;
|
||||||
|
seconds = unixtime;
|
||||||
|
minutes = seconds / 60;
|
||||||
|
seconds %= 60;
|
||||||
|
hours = minutes / 60;
|
||||||
|
minutes %= 60;
|
||||||
|
days = hours / 24;
|
||||||
|
hours %= 24;
|
||||||
|
year = 1970;
|
||||||
|
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
bool leapYear = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
|
||||||
|
u16 daysInYear = leapYear ? 366 : 365;
|
||||||
|
if(days >= daysInYear)
|
||||||
|
{
|
||||||
|
days -= daysInYear;
|
||||||
|
++year;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
static const u8 daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||||
|
for(month = 0; month < 12; ++month)
|
||||||
|
{
|
||||||
|
u8 dim = daysInMonth[month];
|
||||||
|
|
||||||
|
if (month == 1 && leapYear)
|
||||||
|
++dim;
|
||||||
|
|
||||||
|
if (days >= dim)
|
||||||
|
days -= dim;
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
days++;
|
||||||
|
month++;
|
||||||
|
u8 bcd_year = year-2000;
|
||||||
|
dstime->bcd_Y = NUM2BCD(bcd_year);
|
||||||
|
dstime->bcd_M = NUM2BCD(month);
|
||||||
|
dstime->bcd_D = NUM2BCD(days);
|
||||||
|
dstime->bcd_h = NUM2BCD(hours);
|
||||||
|
dstime->bcd_m = NUM2BCD(minutes);
|
||||||
|
dstime->bcd_s = NUM2BCD(seconds);
|
||||||
|
dstime->weekday = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool my_strftime(char* _out, size_t _maxsize, const char* str, DsTime *dstime) { //my refers to github.com/Gruetzig
|
||||||
|
weekdayfix(dstime);
|
||||||
|
size_t strl = strlen(str);
|
||||||
|
size_t outpos = 0;
|
||||||
|
char out[_maxsize+10];
|
||||||
|
memset(out, 0, _maxsize+10);
|
||||||
|
u8 minute, hour, day, month, year, weekday, second, weeknumber;
|
||||||
|
u16 currentday, nextsunday, fyear;
|
||||||
|
char numbuf[3], numbuf2[5], numbuf3[9], numnum1[3], numnum2[3], numnum3[3];
|
||||||
|
if (!is_valid_dstime(dstime)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (size_t i = 0;i<strl;i++) {
|
||||||
|
if (str[i] == '%') {
|
||||||
|
i++;
|
||||||
|
switch(str[i]) {
|
||||||
|
case 'a':
|
||||||
|
outpos += getWeekday(true, out, DSTIMEGET(dstime, weekday));
|
||||||
|
break;
|
||||||
|
case 'A':
|
||||||
|
outpos += getWeekday(false, out, DSTIMEGET(dstime, weekday));
|
||||||
|
break;
|
||||||
|
case 'b':
|
||||||
|
outpos += getMonthName(true, out, DSTIMEGET(dstime, bcd_M));
|
||||||
|
break;
|
||||||
|
case 'B':
|
||||||
|
outpos += getMonthName(false, out, DSTIMEGET(dstime, bcd_M));
|
||||||
|
break;
|
||||||
|
case 'c':
|
||||||
|
char buf[100];
|
||||||
|
my_strftime(buf, _maxsize-outpos-10, "%a %b %d %X %Y", dstime);
|
||||||
|
strcat(out, buf);
|
||||||
|
outpos += strlen(buf);
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
day = DSTIMEGET(dstime, bcd_D);
|
||||||
|
if (day < 10) {
|
||||||
|
sprintf(numbuf, "0%d", day);
|
||||||
|
} else {
|
||||||
|
sprintf(numbuf, "%d", day);
|
||||||
|
}
|
||||||
|
strcat(out, numbuf);
|
||||||
|
outpos += 2;
|
||||||
|
break;
|
||||||
|
case 'H':
|
||||||
|
hour = DSTIMEGET(dstime, bcd_h);
|
||||||
|
if (day < 10) {
|
||||||
|
sprintf(numbuf, "0%d", hour);
|
||||||
|
} else {
|
||||||
|
sprintf(numbuf, "%d", hour);
|
||||||
|
}
|
||||||
|
strcat(out, numbuf);
|
||||||
|
outpos += 2;
|
||||||
|
break;
|
||||||
|
case 'I':
|
||||||
|
hour = DSTIMEGET(dstime, bcd_h);
|
||||||
|
if (hour > 12) {
|
||||||
|
hour = hour - 12;
|
||||||
|
}
|
||||||
|
if (!hour) {
|
||||||
|
hour = 12;
|
||||||
|
}
|
||||||
|
if (hour < 10) {
|
||||||
|
sprintf(numbuf, "0%d", hour);
|
||||||
|
} else {
|
||||||
|
sprintf(numbuf, "%d", hour);
|
||||||
|
}
|
||||||
|
strcat(out, numbuf);
|
||||||
|
outpos += 2;
|
||||||
|
break;
|
||||||
|
case 'j':
|
||||||
|
currentday = getDaysMonths(DSTIMEGET(dstime, bcd_M), DSTIMEGET(dstime, bcd_Y))+DSTIMEGET(dstime, bcd_D);
|
||||||
|
if (currentday < 10) {
|
||||||
|
sprintf(numbuf, "00%d", currentday);
|
||||||
|
} else if (currentday < 100) {
|
||||||
|
sprintf(numbuf, "0%d", currentday);
|
||||||
|
} else {
|
||||||
|
sprintf(numbuf, "%d", currentday);
|
||||||
|
}
|
||||||
|
strcat(out, numbuf);
|
||||||
|
outpos += 3;
|
||||||
|
break;
|
||||||
|
case 'm':
|
||||||
|
month = DSTIMEGET(dstime, bcd_M);
|
||||||
|
if (month < 10) {
|
||||||
|
sprintf(numbuf, "0%d", month);
|
||||||
|
} else {
|
||||||
|
sprintf(numbuf, "%d", month);
|
||||||
|
}
|
||||||
|
strcat(out, numbuf);
|
||||||
|
outpos += 2;
|
||||||
|
break;
|
||||||
|
case 'M':
|
||||||
|
minute = DSTIMEGET(dstime, bcd_m);
|
||||||
|
if (minute < 10) {
|
||||||
|
sprintf(numbuf, "0%d", minute);
|
||||||
|
} else {
|
||||||
|
sprintf(numbuf, "%d", minute);
|
||||||
|
}
|
||||||
|
strcat(out, numbuf);
|
||||||
|
outpos += 2;
|
||||||
|
break;
|
||||||
|
case 'p':
|
||||||
|
hour = DSTIMEGET(dstime, bcd_h);
|
||||||
|
if (hour >= 12) {
|
||||||
|
strcat(out, "PM");
|
||||||
|
} else {
|
||||||
|
strcat(out, "AM");
|
||||||
|
}
|
||||||
|
outpos += 2;
|
||||||
|
break;
|
||||||
|
case 'S':
|
||||||
|
second = DSTIMEGET(dstime, bcd_m);
|
||||||
|
if (second < 10) {
|
||||||
|
sprintf(numbuf, "0%d", second);
|
||||||
|
} else {
|
||||||
|
sprintf(numbuf, "%d", second);
|
||||||
|
}
|
||||||
|
strcat(out, numbuf);
|
||||||
|
outpos += 2;
|
||||||
|
break;
|
||||||
|
case 'U':
|
||||||
|
currentday = getDaysMonths(DSTIMEGET(dstime, bcd_M), DSTIMEGET(dstime, bcd_Y))+DSTIMEGET(dstime, bcd_D);
|
||||||
|
weekday = DSTIMEGET(dstime, weekday);
|
||||||
|
nextsunday = ((7-weekday)+currentday);
|
||||||
|
weeknumber = (nextsunday/7)+1;
|
||||||
|
if (weeknumber < 10) {
|
||||||
|
sprintf(numbuf, "0%d", weeknumber);
|
||||||
|
} else {
|
||||||
|
sprintf(numbuf, "%d", weeknumber);
|
||||||
|
}
|
||||||
|
strcat(out, numbuf);
|
||||||
|
outpos += 2;
|
||||||
|
break;
|
||||||
|
case 'w':
|
||||||
|
weekday = DSTIMEGET(dstime, weekday);
|
||||||
|
if (weekday == 7) {
|
||||||
|
weekday = 0;
|
||||||
|
}
|
||||||
|
sprintf(out, "%d", weekday);
|
||||||
|
strcat(out, numbuf);
|
||||||
|
outpos++;
|
||||||
|
break;
|
||||||
|
case 'W':
|
||||||
|
currentday = getDaysMonths(DSTIMEGET(dstime, bcd_M), DSTIMEGET(dstime, bcd_Y))+DSTIMEGET(dstime, bcd_D);
|
||||||
|
weekday = DSTIMEGET(dstime, weekday);
|
||||||
|
nextsunday = ((8-weekday)+currentday);
|
||||||
|
weeknumber = (nextsunday/7);
|
||||||
|
if (weeknumber < 10) {
|
||||||
|
sprintf(numbuf, "0%d", weeknumber);
|
||||||
|
} else {
|
||||||
|
sprintf(numbuf, "%d", weeknumber);
|
||||||
|
}
|
||||||
|
strcat(out, numbuf);
|
||||||
|
outpos += 2;
|
||||||
|
break;
|
||||||
|
case 'x':
|
||||||
|
month = DSTIMEGET(dstime, bcd_M);
|
||||||
|
day = DSTIMEGET(dstime, bcd_D);
|
||||||
|
year = DSTIMEGET(dstime, bcd_Y);
|
||||||
|
|
||||||
|
if (month < 10) {
|
||||||
|
sprintf(numnum1, "0%d", month);
|
||||||
|
} else {
|
||||||
|
sprintf(numnum1, "%d", month);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (day < 10) {
|
||||||
|
sprintf(numnum2, "0%d", day);
|
||||||
|
} else {
|
||||||
|
sprintf(numnum2, "%d", day);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (year < 10) {
|
||||||
|
sprintf(numnum3, "0%d", year);
|
||||||
|
} else {
|
||||||
|
sprintf(numnum3, "%d", year);
|
||||||
|
}
|
||||||
|
sprintf(numbuf3, "%s/%s/%s", numnum1, numnum2, numnum3);
|
||||||
|
strcat(out, numbuf3);
|
||||||
|
outpos += 8;
|
||||||
|
break;
|
||||||
|
case 'X':
|
||||||
|
hour = DSTIMEGET(dstime, bcd_h);
|
||||||
|
minute = DSTIMEGET(dstime, bcd_m);
|
||||||
|
second = DSTIMEGET(dstime, bcd_s);
|
||||||
|
|
||||||
|
if (hour < 10) {
|
||||||
|
sprintf(numnum1, "0%d", hour);
|
||||||
|
} else {
|
||||||
|
sprintf(numnum1, "%d", hour);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (minute < 10) {
|
||||||
|
sprintf(numnum2, "0%d", minute);
|
||||||
|
} else {
|
||||||
|
sprintf(numnum2, "%d", minute);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (second < 10) {
|
||||||
|
sprintf(numnum3, "0%d", second);
|
||||||
|
} else {
|
||||||
|
sprintf(numnum3, "%d", second);
|
||||||
|
}
|
||||||
|
sprintf(numbuf3, "%s:%s:%s", numnum1, numnum2, numnum3);
|
||||||
|
strcat(out, numbuf3);
|
||||||
|
outpos += 8;
|
||||||
|
break;
|
||||||
|
case 'y':
|
||||||
|
year = DSTIMEGET(dstime, bcd_Y);
|
||||||
|
if (year < 10) {
|
||||||
|
sprintf(numbuf, "0%d", year);
|
||||||
|
} else {
|
||||||
|
sprintf(numbuf, "%d", year);
|
||||||
|
}
|
||||||
|
strcat(out, numbuf);
|
||||||
|
outpos += 2;
|
||||||
|
break;
|
||||||
|
case 'Y':
|
||||||
|
fyear = DSTIMEGET(dstime, bcd_Y);
|
||||||
|
fyear += 2000;
|
||||||
|
sprintf(numbuf2, "%d", fyear);
|
||||||
|
strcat(out, numbuf2);
|
||||||
|
outpos += 4;
|
||||||
|
break;
|
||||||
|
case '%':
|
||||||
|
strcat(out, "%");
|
||||||
|
outpos++;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break; //not implemented
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
out[outpos] = str[i];
|
||||||
|
outpos++;
|
||||||
|
}
|
||||||
|
if (outpos > _maxsize) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
strncpy(_out, out, _maxsize);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int os_time(lua_State *L) {
|
||||||
|
int args = lua_gettop(L);
|
||||||
|
u64 unixtime;
|
||||||
|
switch(args) {
|
||||||
|
case 0:
|
||||||
|
DsTime dstime;
|
||||||
|
get_dstime(&dstime);
|
||||||
|
unixtime = getUnixTimeFromRtc(&dstime);
|
||||||
|
lua_pushinteger(L, unixtime);
|
||||||
|
return 1;
|
||||||
|
case 1:
|
||||||
|
lua_geti(L, 1, 1);
|
||||||
|
int year = lua_tointeger(L, -1);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
if (year >= 2000) year -= 2000;
|
||||||
|
|
||||||
|
lua_geti(L, 1, 2);
|
||||||
|
int month = lua_tointeger(L, -1);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
|
||||||
|
lua_geti(L, 1, 3);
|
||||||
|
int day = lua_tointeger(L, -1);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
|
||||||
|
lua_geti(L, 1, 4);
|
||||||
|
int hour = lua_tointeger(L, -1);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
|
||||||
|
lua_geti(L, 1, 5);
|
||||||
|
int minute = lua_tointeger(L, -1);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
|
||||||
|
lua_geti(L, 1, 6);
|
||||||
|
int second = lua_tointeger(L, -1);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
|
||||||
|
lua_pushinteger(L, calcUnixTime(year, month, day, hour, minute, second));
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
default:
|
||||||
|
return luaL_error(L, "not a valid amount of arguments");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int os_date(lua_State *L) {
|
||||||
|
DsTime dstime;
|
||||||
|
|
||||||
|
get_dstime(&dstime);
|
||||||
|
char retbuf[100];
|
||||||
|
memset(retbuf, 0, 100);
|
||||||
|
int args = lua_gettop(L);
|
||||||
|
switch(args) {
|
||||||
|
case 0:
|
||||||
|
my_strftime(retbuf, 100, "%c", &dstime);
|
||||||
|
lua_pushstring(L, retbuf);
|
||||||
|
return 1;
|
||||||
|
case 1:
|
||||||
|
const char* str = lua_tostring(L, 1);
|
||||||
|
if ((strcmp(str, "*t") == 0 || strcmp(str, "!*t") == 0)) {
|
||||||
|
weekdayfix(&dstime);
|
||||||
|
//return table with date values
|
||||||
|
lua_newtable(L);
|
||||||
|
//year
|
||||||
|
lua_pushinteger(L, 2000+DSTIMEGET(&dstime, bcd_Y));
|
||||||
|
lua_setfield(L, -2, "year");
|
||||||
|
//month
|
||||||
|
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_M));
|
||||||
|
lua_setfield(L, -2, "month");
|
||||||
|
//wday (weekday)
|
||||||
|
lua_pushinteger(L, DSTIMEGET(&dstime, weekday));
|
||||||
|
lua_setfield(L, -2, "wday");
|
||||||
|
//yday (yearday)
|
||||||
|
lua_pushinteger(L, getDaysMonths(DSTIMEGET(&dstime, bcd_M), DSTIMEGET(&dstime, bcd_Y))+DSTIMEGET(&dstime, bcd_D));
|
||||||
|
lua_setfield(L, -2, "yday");
|
||||||
|
//day
|
||||||
|
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_D));
|
||||||
|
lua_setfield(L, -2, "day");
|
||||||
|
//hour
|
||||||
|
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_h));
|
||||||
|
lua_setfield(L, -2, "hour");
|
||||||
|
//minute
|
||||||
|
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_m));
|
||||||
|
lua_setfield(L, -2, "min");
|
||||||
|
//second
|
||||||
|
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_s));
|
||||||
|
lua_setfield(L, -2, "sec");
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
my_strftime(retbuf, 100, lua_tostring(L, 1), &dstime);
|
||||||
|
lua_pushstring(L, retbuf);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
if (lua_tointeger(L, 2) < 946684800) { //unix timestamp is 01.01.2000 00:00:00, so everything before is previous century and not supported
|
||||||
|
return luaL_error(L, "unix timestamp from before 2000 is not supported");
|
||||||
|
}
|
||||||
|
const char* str2 = lua_tostring(L, 1);
|
||||||
|
if ((strcmp(str2, "*t") == 0 || strcmp(str2, "!*t") == 0)) {
|
||||||
|
unixtodstime( lua_tointeger(L, 2) , &dstime);
|
||||||
|
weekdayfix(&dstime);
|
||||||
|
//return table with date values
|
||||||
|
lua_newtable(L);
|
||||||
|
//year
|
||||||
|
lua_pushinteger(L, 2000+DSTIMEGET(&dstime, bcd_Y));
|
||||||
|
lua_setfield(L, -2, "year");
|
||||||
|
//month
|
||||||
|
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_M));
|
||||||
|
lua_setfield(L, -2, "month");
|
||||||
|
//wday (weekday)
|
||||||
|
lua_pushinteger(L, DSTIMEGET(&dstime, weekday));
|
||||||
|
lua_setfield(L, -2, "wday");
|
||||||
|
//yday (yearday)
|
||||||
|
lua_pushinteger(L, getDaysMonths(DSTIMEGET(&dstime, bcd_M), DSTIMEGET(&dstime, bcd_Y))+DSTIMEGET(&dstime, bcd_D));
|
||||||
|
lua_setfield(L, -2, "yday");
|
||||||
|
//day
|
||||||
|
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_D));
|
||||||
|
lua_setfield(L, -2, "day");
|
||||||
|
//hour
|
||||||
|
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_h));
|
||||||
|
lua_setfield(L, -2, "hour");
|
||||||
|
//minute
|
||||||
|
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_m));
|
||||||
|
lua_setfield(L, -2, "min");
|
||||||
|
//second
|
||||||
|
lua_pushinteger(L, DSTIMEGET(&dstime, bcd_s));
|
||||||
|
lua_setfield(L, -2, "sec");
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
unixtodstime( lua_tointeger(L, 2) , &dstime);
|
||||||
|
my_strftime(retbuf, 100, str2, &dstime);
|
||||||
|
lua_pushstring(L, retbuf);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return luaL_error(L, "not a valid amount of arguments");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int os_clock(lua_State *L) {
|
||||||
|
lua_pushnumber(L, timer_usec(osclock)/10000000.0);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int os_difftime(lua_State *L) {
|
||||||
|
u64 t2 = lua_tointeger(L, 1);
|
||||||
|
u64 t1 = lua_tointeger(L, 2);
|
||||||
|
lua_pushinteger(L, t2-t1);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// os.remove and os.rename are done in data/luapackages/fs.lua
|
||||||
|
static const luaL_Reg os[] = {
|
||||||
|
{"clock", os_clock},
|
||||||
|
{"time", os_time},
|
||||||
|
{"date", os_date},
|
||||||
|
{"difftime", os_difftime},
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
int gm9lua_open_os(lua_State* L) {
|
||||||
|
luaL_newlib(L, os);
|
||||||
|
osclock = timer_start();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#endif
|
6
arm9/source/lua/gm9os.h
Normal file
6
arm9/source/lua/gm9os.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "gm9lua.h"
|
||||||
|
|
||||||
|
#define GM9LUA_OSLIBNAME "os"
|
||||||
|
|
||||||
|
int gm9lua_open_os(lua_State* L);
|
183
arm9/source/lua/gm9title.c
Normal file
183
arm9/source/lua/gm9title.c
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
#ifndef NO_LUA
|
||||||
|
#include "gm9lua.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include "fs.h"
|
||||||
|
#include "language.h"
|
||||||
|
#include "ui.h"
|
||||||
|
#include "game.h"
|
||||||
|
#include "ips.h"
|
||||||
|
#include "bps.h"
|
||||||
|
|
||||||
|
static int title_decrypt(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "title.decrypt");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
const char* whichfailed = "";
|
||||||
|
|
||||||
|
u64 filetype = IdentifyFileType(path);
|
||||||
|
bool ret;
|
||||||
|
if (filetype & BIN_KEYDB) {
|
||||||
|
ret = (CryptAesKeyDb(path, true, false) == 0);
|
||||||
|
whichfailed = "CryptAesKeyDb";
|
||||||
|
} else {
|
||||||
|
ret = (CryptGameFile(path, true, false) == 0);
|
||||||
|
whichfailed = "CryptGameFile";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ret) {
|
||||||
|
return luaL_error(L, "%s failed on %s", whichfailed, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int title_encrypt(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "title.encrypt");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
const char* whichfailed = "";
|
||||||
|
|
||||||
|
u64 filetype = IdentifyFileType(path);
|
||||||
|
bool ret;
|
||||||
|
if (filetype & BIN_KEYDB) {
|
||||||
|
ret = (CryptAesKeyDb(path, true, true) == 0);
|
||||||
|
whichfailed = "CryptAesKeyDb";
|
||||||
|
} else {
|
||||||
|
ret = (CryptGameFile(path, true, true) == 0);
|
||||||
|
whichfailed = "CryptGameFile";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ret) {
|
||||||
|
return luaL_error(L, "%s failed on %s", whichfailed, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int title_install(lua_State* L) {
|
||||||
|
bool extra = CheckLuaArgCountPlusExtra(L, 1, "title.install");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
u32 flags = 0;
|
||||||
|
if (extra) {
|
||||||
|
flags = GetFlagsFromTable(L, 2, flags, TO_EMUNAND);
|
||||||
|
};
|
||||||
|
|
||||||
|
bool ret = (InstallGameFile(path, (flags & TO_EMUNAND)) == 0);
|
||||||
|
if (!ret) {
|
||||||
|
return luaL_error(L, "InstallGameFile failed on %s", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int title_build_cia(lua_State* L) {
|
||||||
|
bool extra = CheckLuaArgCountPlusExtra(L, 1, "title.build_cia");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
u32 flags = 0;
|
||||||
|
if (extra) {
|
||||||
|
flags = GetFlagsFromTable(L, 2, flags, LEGIT);
|
||||||
|
};
|
||||||
|
|
||||||
|
bool ret = (BuildCiaFromGameFile(path, (flags & LEGIT)) == 0);
|
||||||
|
if (!ret) {
|
||||||
|
return luaL_error(L, "BuildCiaFromGameFile failed on %s", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int title_extract_code(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 2, "title.extract_code");
|
||||||
|
const char* path_src = luaL_checkstring(L, 1);
|
||||||
|
const char* path_dst = luaL_checkstring(L, 2);
|
||||||
|
|
||||||
|
u64 filetype = IdentifyFileType(path_src);
|
||||||
|
if (!FTYPE_HASCODE(filetype)) {
|
||||||
|
return luaL_error(L, "%s does not have code", path_src);
|
||||||
|
} else {
|
||||||
|
CheckWritePermissionsLuaError(L, path_dst);
|
||||||
|
ShowString("%s", STR_EXTRACTING_DOT_CODE);
|
||||||
|
bool ret = (ExtractCodeFromCxiFile(path_src, path_dst, NULL) == 0);
|
||||||
|
if (!ret) {
|
||||||
|
return luaL_error(L, "failed to extract code from %s", path_src);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int title_compress_code(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 2, "title.compress_code");
|
||||||
|
const char* path_src = luaL_checkstring(L, 1);
|
||||||
|
const char* path_dst = luaL_checkstring(L, 2);
|
||||||
|
|
||||||
|
CheckWritePermissionsLuaError(L, path_dst);
|
||||||
|
ShowString("%s", STR_COMPRESSING_DOT_CODE);
|
||||||
|
bool ret = (CompressCode(path_src, path_dst) == 0);
|
||||||
|
if (!ret) {
|
||||||
|
return luaL_error(L, "failed to compress code from %s", path_src);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int title_apply_ips(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 3, "title.apply_ips");
|
||||||
|
const char* path_patch = luaL_checkstring(L, 1);
|
||||||
|
const char* path_src = luaL_checkstring(L, 2);
|
||||||
|
const char* path_target = luaL_checkstring(L, 3);
|
||||||
|
|
||||||
|
bool ret = (ApplyIPSPatch(path_patch, path_src, path_target) == 0);
|
||||||
|
if (!ret) {
|
||||||
|
return luaL_error(L, "ApplyIPSPatch failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int title_apply_bps(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 3, "title.apply_bps");
|
||||||
|
const char* path_patch = luaL_checkstring(L, 1);
|
||||||
|
const char* path_src = luaL_checkstring(L, 2);
|
||||||
|
const char* path_target = luaL_checkstring(L, 3);
|
||||||
|
|
||||||
|
bool ret = (ApplyBPSPatch(path_patch, path_src, path_target) == 0);
|
||||||
|
if (!ret) {
|
||||||
|
return luaL_error(L, "ApplyBPSPatch failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int title_apply_bpm(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 3, "title.apply_bpm");
|
||||||
|
const char* path_patch = luaL_checkstring(L, 1);
|
||||||
|
const char* path_src = luaL_checkstring(L, 2);
|
||||||
|
const char* path_target = luaL_checkstring(L, 3);
|
||||||
|
|
||||||
|
bool ret = (ApplyBPMPatch(path_patch, path_src, path_target) == 0);
|
||||||
|
if (!ret) {
|
||||||
|
return luaL_error(L, "ApplyBPMPatch failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const luaL_Reg title_lib[] = {
|
||||||
|
{"decrypt", title_decrypt},
|
||||||
|
{"encrypt", title_encrypt},
|
||||||
|
{"install", title_install},
|
||||||
|
{"build_cia", title_build_cia},
|
||||||
|
{"extract_code", title_extract_code},
|
||||||
|
{"compress_code", title_compress_code},
|
||||||
|
{"apply_ips", title_apply_ips},
|
||||||
|
{"apply_bps", title_apply_bps},
|
||||||
|
{"apply_bpm", title_apply_bpm},
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
int gm9lua_open_title(lua_State* L) {
|
||||||
|
luaL_newlib(L, title_lib);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#endif
|
6
arm9/source/lua/gm9title.h
Normal file
6
arm9/source/lua/gm9title.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "gm9lua.h"
|
||||||
|
|
||||||
|
#define GM9LUA_TITLELIBNAME "title"
|
||||||
|
|
||||||
|
int gm9lua_open_title(lua_State* L);
|
360
arm9/source/lua/gm9ui.c
Normal file
360
arm9/source/lua/gm9ui.c
Normal file
@ -0,0 +1,360 @@
|
|||||||
|
#ifndef NO_LUA
|
||||||
|
#include "gm9ui.h"
|
||||||
|
#include "ui.h"
|
||||||
|
#include "fs.h"
|
||||||
|
#include "png.h"
|
||||||
|
#include "swkbd.h"
|
||||||
|
#include "qrcodegen.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include "hid.h"
|
||||||
|
|
||||||
|
#define MAXOPTIONS 256
|
||||||
|
#define MAXOPTIONS_STR "256"
|
||||||
|
|
||||||
|
#define OUTPUTMAXLINES 24
|
||||||
|
#define OUTPUTMAXCHARSPERLINE 80 // make sure this includes space for '\0'
|
||||||
|
|
||||||
|
// this output buffer stuff is especially a test, it needs to take into account newlines and fonts that are not 8x10
|
||||||
|
|
||||||
|
char output_buffer[OUTPUTMAXLINES][OUTPUTMAXCHARSPERLINE]; // hold 24 lines
|
||||||
|
|
||||||
|
void ShiftOutputBufferUp(void) {
|
||||||
|
for (int i = 0; i < OUTPUTMAXLINES - 1; i++) {
|
||||||
|
memcpy(output_buffer[i], output_buffer[i + 1], OUTPUTMAXCHARSPERLINE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClearOutputBuffer(void) {
|
||||||
|
memset(output_buffer, 0, sizeof(output_buffer));
|
||||||
|
}
|
||||||
|
|
||||||
|
void WriteToOutputBuffer(char* text) {
|
||||||
|
strlcpy(output_buffer[OUTPUTMAXLINES - 1], text, OUTPUTMAXCHARSPERLINE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RenderOutputBuffer(void) {
|
||||||
|
ClearScreenF(false, true, COLOR_STD_BG);
|
||||||
|
for (int i = 0; i < OUTPUTMAXLINES; i++) {
|
||||||
|
DrawString(ALT_SCREEN, output_buffer[i], 0, i * 10, COLOR_STD_FONT, COLOR_TRANSPARENT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ui_echo(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "ui.echo");
|
||||||
|
const char* text = lua_tostring(L, 1);
|
||||||
|
|
||||||
|
ShowPrompt(false, "%s", text);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ui_ask(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "ui.ask");
|
||||||
|
const char* text = lua_tostring(L, 1);
|
||||||
|
|
||||||
|
bool ret = ShowPrompt(true, "%s", text);
|
||||||
|
lua_pushboolean(L, ret);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ui_ask_hex(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 3, "ui.ask_hex");
|
||||||
|
const char* text = lua_tostring(L, 1);
|
||||||
|
u64 initial_hex = lua_tonumber(L, 2);
|
||||||
|
u32 n_digits = lua_tonumber(L, 3);
|
||||||
|
|
||||||
|
u64 ret = ShowHexPrompt(initial_hex, n_digits, "%s", text);
|
||||||
|
if (ret == (u64) -1) {
|
||||||
|
lua_pushnil(L);
|
||||||
|
} else {
|
||||||
|
lua_pushnumber(L, ret);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ui_ask_number(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 2, "ui.ask_number");
|
||||||
|
const char* text = lua_tostring(L, 1);
|
||||||
|
u64 initial_num = lua_tonumber(L, 2);
|
||||||
|
|
||||||
|
u64 ret = ShowNumberPrompt(initial_num, "%s", text);
|
||||||
|
if (ret == (u64) -1) {
|
||||||
|
lua_pushnil(L);
|
||||||
|
} else {
|
||||||
|
lua_pushnumber(L, ret);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ui_ask_text(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 3, "ui.ask_text");
|
||||||
|
const char* prompt = lua_tostring(L, 1);
|
||||||
|
const char* _initial_text = lua_tostring(L, 2);
|
||||||
|
u32 initial_text_size = strlen(_initial_text)+1;
|
||||||
|
char initial_text[initial_text_size];
|
||||||
|
snprintf(initial_text, initial_text_size, "%s", _initial_text);
|
||||||
|
u32 max_size = lua_tonumber(L, 3);
|
||||||
|
bool result = ShowKeyboardOrPrompt(initial_text, max_size, "%s", prompt);
|
||||||
|
if (result)
|
||||||
|
lua_pushstring(L, initial_text);
|
||||||
|
else
|
||||||
|
lua_pushnil(L);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ui_clear(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 0, "ui.clear");
|
||||||
|
|
||||||
|
ClearScreen(ALT_SCREEN, COLOR_STD_BG);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ui_show_png(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "ui.show_png");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
u16 *screen = ALT_SCREEN;
|
||||||
|
u16 *bitmap = NULL;
|
||||||
|
u8* png = (u8*) malloc(SCREEN_SIZE(screen));
|
||||||
|
u32 bitmap_width, bitmap_height;
|
||||||
|
if (png) {
|
||||||
|
u32 png_size = FileGetData(path, png, SCREEN_SIZE(screen), 0);
|
||||||
|
if (!png_size) {
|
||||||
|
free(png);
|
||||||
|
return luaL_error(L, "Could not read %s", path);
|
||||||
|
}
|
||||||
|
if (png_size && png_size < SCREEN_SIZE(screen)) {
|
||||||
|
bitmap = PNG_Decompress(png, png_size, &bitmap_width, &bitmap_height);
|
||||||
|
if (!bitmap) {
|
||||||
|
free(png);
|
||||||
|
return luaL_error(L, "Invalid PNG file");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(png);
|
||||||
|
if (!bitmap) {
|
||||||
|
return luaL_error(L, "PNG too large");
|
||||||
|
} else if ((SCREEN_WIDTH(screen) < bitmap_width) || (SCREEN_HEIGHT < bitmap_height)) {
|
||||||
|
free(bitmap);
|
||||||
|
return luaL_error(L, "PNG too large");
|
||||||
|
} else {
|
||||||
|
ClearScreen(ALT_SCREEN, COLOR_STD_BG);
|
||||||
|
DrawBitmap(
|
||||||
|
screen, // screen
|
||||||
|
-1, // x coordinate from argument
|
||||||
|
-1, // y coordinate from argument
|
||||||
|
bitmap_width, // width
|
||||||
|
bitmap_height, // height
|
||||||
|
bitmap // bitmap
|
||||||
|
);
|
||||||
|
free(bitmap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ui_show_text(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "ui.show_text");
|
||||||
|
|
||||||
|
const char* text = lua_tostring(L, 1);
|
||||||
|
|
||||||
|
ClearScreen(ALT_SCREEN, COLOR_STD_BG);
|
||||||
|
DrawStringCenter(ALT_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s", text);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ui_show_game_info(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "ui.show_game_info");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
bool ret = (ShowGameFileIcon(path, ALT_SCREEN) == 0);
|
||||||
|
if (!ret) {
|
||||||
|
return luaL_error(L, "ShowGameFileIcon failed on %s", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ui_show_qr(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 2, "ui.show_qr");
|
||||||
|
size_t data_len;
|
||||||
|
const char* text = luaL_checkstring(L, 1);
|
||||||
|
const char* data = luaL_checklstring(L, 2, &data_len);
|
||||||
|
|
||||||
|
const u32 screen_size = SCREEN_SIZE(ALT_SCREEN);
|
||||||
|
u8* screen_copy = (u8*) malloc(screen_size);
|
||||||
|
u8 qrcode[qrcodegen_BUFFER_LEN_MAX];
|
||||||
|
u8 temp[qrcodegen_BUFFER_LEN_MAX];
|
||||||
|
bool ret = screen_copy && qrcodegen_encodeText(data, temp, qrcode, qrcodegen_Ecc_LOW,
|
||||||
|
qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
|
||||||
|
if (ret) {
|
||||||
|
memcpy(screen_copy, ALT_SCREEN, screen_size);
|
||||||
|
DrawQrCode(ALT_SCREEN, qrcode);
|
||||||
|
ShowPrompt(false, "%s", text);
|
||||||
|
memcpy(ALT_SCREEN, screen_copy, screen_size);
|
||||||
|
} else {
|
||||||
|
return luaL_error(L, "could not allocate memory");
|
||||||
|
}
|
||||||
|
free(screen_copy);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ui_show_text_viewer(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "ui.show_text_viewer");
|
||||||
|
size_t len = 0;
|
||||||
|
const char* text = luaL_tolstring(L, 1, &len);
|
||||||
|
|
||||||
|
// validate text ourselves so we can return a better error
|
||||||
|
// MemTextViewer calls ShowPrompt if it's bad, and i don't want that
|
||||||
|
|
||||||
|
if (!(ValidateText(text, len))) {
|
||||||
|
return luaL_error(L, "text validation failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(MemTextViewer(text, len, 1, false))) {
|
||||||
|
return luaL_error(L, "failed to run MemTextViewer");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ui_show_file_text_viewer(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "ui.show_file_text_viewer");
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
// validate text ourselves so we can return a better error
|
||||||
|
// MemTextViewer calls ShowPrompt if it's bad, and i don't want that
|
||||||
|
// and FileTextViewer calls the above function
|
||||||
|
|
||||||
|
char* text = malloc(STD_BUFFER_SIZE);
|
||||||
|
if (!text) {
|
||||||
|
return luaL_error(L, "could not allocate memory");
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: replace this with something that can detect file read errors and actual 0-length files
|
||||||
|
size_t flen = FileGetData(path, text, STD_BUFFER_SIZE - 1, 0);
|
||||||
|
|
||||||
|
text[flen] = '\0';
|
||||||
|
u32 len = (ptrdiff_t)memchr(text, '\0', flen + 1) - (ptrdiff_t)text;
|
||||||
|
|
||||||
|
if (!(ValidateText(text, len))) {
|
||||||
|
free(text);
|
||||||
|
return luaL_error(L, "text validation failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(MemTextViewer(text, len, 1, false))) {
|
||||||
|
free(text);
|
||||||
|
return luaL_error(L, "failed to run MemTextViewer");
|
||||||
|
}
|
||||||
|
|
||||||
|
free(text);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ui_ask_selection(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 2, "ui.ask_selection");
|
||||||
|
const char* text = lua_tostring(L, 1);
|
||||||
|
char* options[MAXOPTIONS];
|
||||||
|
const char* tmpstr;
|
||||||
|
size_t len;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
luaL_argcheck(L, lua_istable(L, 2), 2, "table expected");
|
||||||
|
|
||||||
|
lua_Integer opttablesize = luaL_len(L, 2);
|
||||||
|
luaL_argcheck(L, opttablesize <= MAXOPTIONS, 2, "more than " MAXOPTIONS_STR " options given");
|
||||||
|
for (i = 0; i < opttablesize; i++) {
|
||||||
|
lua_geti(L, 2, i + 1);
|
||||||
|
tmpstr = lua_tolstring(L, -1, &len);
|
||||||
|
options[i] = malloc(len + 1);
|
||||||
|
strlcpy(options[i], tmpstr, len + 1);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
}
|
||||||
|
int result = ShowSelectPrompt(opttablesize, (const char**)options, "%s", text);
|
||||||
|
for (i = 0; i < opttablesize; i++) free(options[i]);
|
||||||
|
// lua only treats "false" and "nil" as false values
|
||||||
|
// so to make this easier, return nil and not 0 if no choice was made
|
||||||
|
// https://www.lua.org/manual/5.4/manual.html#3.3.4
|
||||||
|
if (result)
|
||||||
|
lua_pushinteger(L, result);
|
||||||
|
else
|
||||||
|
lua_pushnil(L);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ui_format_bytes(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "ui.format_bytes");
|
||||||
|
lua_Integer size = luaL_checkinteger(L, 1);
|
||||||
|
|
||||||
|
char bytesstr[32] = { 0 };
|
||||||
|
FormatBytes(bytesstr, (u64)size);
|
||||||
|
|
||||||
|
lua_pushstring(L, bytesstr);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ui_global_print(lua_State* L) {
|
||||||
|
//const char* text = lua_tostring(L, 1);
|
||||||
|
char buf[OUTPUTMAXCHARSPERLINE] = {0};
|
||||||
|
int argcount = lua_gettop(L);
|
||||||
|
for (int i = 0; i < lua_gettop(L); i++) {
|
||||||
|
const char* str = luaL_tolstring(L, i+1, NULL);
|
||||||
|
if (str) {
|
||||||
|
strlcat(buf, str, OUTPUTMAXCHARSPERLINE);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
} else {
|
||||||
|
// idk
|
||||||
|
strlcat(buf, "(unknown)", OUTPUTMAXCHARSPERLINE);
|
||||||
|
}
|
||||||
|
if (i < argcount) strlcat(buf, " ", OUTPUTMAXCHARSPERLINE);
|
||||||
|
}
|
||||||
|
ShiftOutputBufferUp();
|
||||||
|
WriteToOutputBuffer((char*)buf);
|
||||||
|
RenderOutputBuffer();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: use luaL_checkoption which will auto-raise an error
|
||||||
|
// use BUTTON_STRINGS from common/hid_map.h
|
||||||
|
static int ui_check_key(lua_State* L) {
|
||||||
|
CheckLuaArgCount(L, 1, "ui.check_key");
|
||||||
|
const char* key = luaL_checkstring(L, 1);
|
||||||
|
|
||||||
|
lua_pushboolean(L, CheckButton(StringToButton((char*)key)));
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const luaL_Reg ui_lib[] = {
|
||||||
|
{"echo", ui_echo},
|
||||||
|
{"ask", ui_ask},
|
||||||
|
{"ask_hex", ui_ask_hex},
|
||||||
|
{"ask_number", ui_ask_number},
|
||||||
|
{"ask_text", ui_ask_text},
|
||||||
|
{"ask_selection", ui_ask_selection},
|
||||||
|
{"clear", ui_clear},
|
||||||
|
{"show_png", ui_show_png},
|
||||||
|
{"show_text", ui_show_text},
|
||||||
|
{"show_game_info", ui_show_game_info},
|
||||||
|
{"show_qr", ui_show_qr},
|
||||||
|
{"show_text_viewer", ui_show_text_viewer},
|
||||||
|
{"show_file_text_viewer", ui_show_file_text_viewer},
|
||||||
|
{"format_bytes", ui_format_bytes},
|
||||||
|
{"check_key", ui_check_key},
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
static const luaL_Reg ui_global_lib[] = {
|
||||||
|
{"print", ui_global_print},
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
int gm9lua_open_ui(lua_State* L) {
|
||||||
|
luaL_newlib(L, ui_lib);
|
||||||
|
lua_pushglobaltable(L); // push global table to stack
|
||||||
|
luaL_setfuncs(L, ui_global_lib, 0); // set global funcs
|
||||||
|
lua_pop(L, 1); // pop global table from stack
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#endif
|
10
arm9/source/lua/gm9ui.h
Normal file
10
arm9/source/lua/gm9ui.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "gm9lua.h"
|
||||||
|
|
||||||
|
#define GM9LUA_UILIBNAME "ui"
|
||||||
|
|
||||||
|
void ShiftOutputBufferUp(void);
|
||||||
|
void ClearOutputBuffer(void);
|
||||||
|
void RenderOutputBuffer(void);
|
||||||
|
void WriteToOutputBuffer(char* text);
|
||||||
|
int gm9lua_open_ui(lua_State* L);
|
1463
arm9/source/lua/lapi.c
Normal file
1463
arm9/source/lua/lapi.c
Normal file
File diff suppressed because it is too large
Load Diff
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