Compare commits

...

308 Commits

Author SHA1 Message Date
ZeroSkill1
cf1cb1cfc5 fix a few cases of improper varg usage in luaL_error 2025-04-26 14:38:59 +02:00
ihaveahax
9ae4351bce
Lua documentation fixes (#910)
* lua-doc: Fix fs.find_all documentation

It returns an array, not a string. Also document return information.

* lua-doc: Remove TODO for fs.verify_with_sha_file

No reason to add fs.read_file errors here as that function gets
pcall-ed. If it fails, nil gets returned.

* lua-doc: Consistently refer to tables/arrays as tables

That's what they're called within Lua all the time.

* lua-doc: fix incorrect references to util module
2025-04-26 14:38:25 +02:00
ihaveahax
99f1abd7a4 lua io: fix incorrect mode detection
string.find has pattern matching by default, so it was incorrectly
reading "r+" when the mode was supposed to be "r". So this disables the
pattern matching and does a plain substring search.
2025-04-26 14:37:46 +02:00
ihaveahax
15eb3b1ebe
GM9Megascript: remove "Un-install Hax" (#916)
This option is confusing because despite it saying "will completely
remove CFW", it is not sufficient to actually fully remove custom
firmware. It doesn't, and can't, detect everything like region changes,
modified system titles, and other things that could brick a stock
console.

There have been numerous cases of bricked consoles because someone saw
this, thinking it would do everything needed to restore a console back
to an unmodified state, and getting a bricked console that needs a
flashcart purchase to fix.

(There is also a issue where, even if everything else was in order, it
can still brick a console due to a bad SD card. For some reason it
copies the FIRM to the SD card, where it can potentially get corrupted
if a counterfeit card was used.)

A proper uninstall guide is required, and we have one here:
https://3ds.hacks.guide/uninstall-cfw
2025-04-26 14:36:36 +02:00
ihaveahax
4424c37a89
Fix fs.list_dir not closing directory (#902)
I forgot fvx_closedir oops!
2025-03-24 17:11:46 +01:00
TophattedWasTaken
bc84780036
Lua examples: Small error corrections (#898)
* HelloScript.lua: Correct typos

* Lua docs: fget corresponds to fs.read_file
2025-03-24 17:08:53 +01:00
d0k3
712df196f1 Updated 9th anniversary splash 2025-03-22 14:06:35 +01:00
d0k3
a20c955e2a Updated readme credits 2025-03-22 14:06:27 +01:00
Pk11
7b20581fce
Update translations with Lua strings (#893) 2025-03-21 13:00:41 +01:00
ihaveahax
ba0272ff5d
Lua support (#878)
* Test implementation of lua

* Trust that lua knows what its doing with this

Silence warnings

* actually update top screen when Thingy is called, disable unnecessary ShowPrompt calls

* readme

* change init for a simple test, print error on top screen too

* Readme

* change init for a simple test, print error on top screen too

* enable more lua libs, edit init.lua with string examples

* one more readme edit before bed

* Readme

* change init for a simple test, print error on top screen too

* enable more lua libs, edit init.lua with string examples

* make lua a proper file type, add test UI library with two functions, remove luacmd command

* remove old attempts at editing lauxlib and liolib

* README

* README

* FS lib, new UI stuff

* consistency with "type* ptr" maybe

* add custom package searcher, reset package.path

* new functions for UI including basic print output buffer, add "Lua scripts..." option to home/power menu

* build vram0.tar including subdirs of data

* move default path to GM9LUA_DEFAULT_PATH

* FS_FileGetData

* testfgd, add GM9VERSION global, update README, fix indentation

* FS_FileGetData will return a nil instead if it fails

* it's actually luaL_pushfail

* os

* use luaL_tolstring instead of lua_tostring

* fix test/remove debugging showprompt

* os.clock float attempt

* fix print for real

* fix swapped offset and size for FileGetData

* finish OS stuff

* fix os.clock

* shorten table in/out

* remove .vscode dir

* enum test

* support building without lua

* NO_LUA hides menu options (except when you directly select a lua file)

* update UI lib to better match the ideas on #1

* dockermake

* add DrawPNG function

* whoops its ShowPNG

* minor fixes, add DrawPNG

* fix AskPrompt, add all showprompts mentioned in #1

* add newly added functions to readme

* try to keep separate code and data

* update lua to 5.4.7

* remove test libraries now that i want to attempt to implement in a real api

* add nix flake for building

* add various lua functions, some of them taken from the old attempt but with new names

* add dev shell to flake.nix

* remove test lua scripts in root

* add test lua scripts in data/luascripts

* add a whole bunch more lua functions and stuff

* add more test lua scripts

* add more functions, add preload script, add test io compatibility module

* add more functions and test scripts

* more functions and stuff

* more functions and stuff

* more functions and stuff, plus a wip ctrcheck reimpl

* yet more functions and stuff

* even more functions and stuff

* command comparison table.ods

* update command comparison table.ods

* update command comparison table.ods

* Add files via upload

* update command comparison table.ods

* update ui.show_text to use DrawStringCenter, update ctrcheck rewrite

* Split up the ARM9 code (.text, .vectors) and data (.rodata, .data, .bss) sections into their own ELFs.

This allows us to use more ARM9 WRAM while leaving the 128k BootROM mirror intact.

* use the makefile definition

* add title module, move around some functions, update command comparison table

* add readme for lua

* remove liolib.c and loslib.c

* more functions and things, use CheckWritePermissionsLuaError in place of more manual checks, update command comparison table

* add missing constant

* set CURRDIR to nil instead of "(null)" if not found

* remove gm9enum (unused since the restart)

* add ui.check_key

* split fs module to lua overlay and _fs internal module, and add a check for fs.write_file in lua

* add fs.ask_select_file and fs.ask_select_dir

* add fs.key_dump, replace overwrite_all and append_all with overwrite and append

* add fs.cart_dump and sys.emu_base

* add ctrtool, update flake.lock

* add io append mode

* make sure io.open with write mode starts with an empty file, add os.remove and os.rename aliases

* properly implement os.remove compatibility

* add fs.verify_with_sha_file, fix PathIsDirectory by using stat instead of opendir

* add util.running_as_module (untested)

* move scripts over to https://github.com/ihaveamac/GM9-lua-script-experiments

* remove ods and dockermake.sh

* remove data/scripts

* add lua autorun (untested)

* fix syntax error

* add sys.check_embedded_backup

* remove accidental symlink

* add sys.check_raw_rtc

* fix ui.show_file_text_viewer not freeing memory or reporting an error if OOM happens

* add todo notes for ui

* work-in-progress lua doc

* formatting fix

* up heading level for all sections

* Revert "up heading level for all sections"

This reverts commit 6ef14b619536b4253e341ba40b4dea728358979d.

* separators

* fix name and error for fs.move

* do explicit permission checks in fs.move

* fix error string for fs.copy

* fix function name for fs.dir_info

* fix error string for fs.find and fs.find_not

* fix function name for fs.img_umount

* partial fs doc

* finished fs doc, string fixes for fs module

* document fs.cart_dump encrypted opt, remove stat from fs.verify_with_sha_file

* title doc

* sys doc, error string updates

* util doc

* add json.lua to lua-doc

* add fs.find_all

* add 3dstool to flake

* make fs.find_all recursive actually recursive, document fs.find_all

* add "for" to comparison table

* change fs.find to return nil if no path was found, instead of raising an error

* change ui.echo to automatically word wrap (untested)

* Revert "change ui.echo to automatically word wrap (untested)"

This reverts commit 2524e7707708e9818162c31f9f004b6301a3061b.

* switch devkitNix to upstream

* flake.lock: Update

Flake lock file updates:

• Updated input 'devkitNix':
    'github:ihaveamac/devkitNix/883d173b94e3da8dc4cc0860cdda8c36b738817c' (2024-12-05)
  → 'github:bandithedoge/devkitNix/95fd44f4ac7cecf24edf22daa899a516df73c6b7' (2025-01-11)
• Updated input 'devkitNix/nixpkgs':
    'github:NixOS/nixpkgs/566e53c2ad750c84f6d31f9ccb9d00f823165550' (2024-12-03)
  → 'github:NixOS/nixpkgs/4bc9c909d9ac828a039f288cf872d16d38185db8' (2025-01-08)
• Updated input 'hax-nur':
    'github:ihaveamac/nur-packages/c570b3830f7dd4d655afb109300529c896cd8855' (2024-12-05)
  → 'github:ihaveamac/nur-packages/cd49afba206c2eb10a349d92470fdf2cc942ae23' (2025-01-11)
• Updated input 'hax-nur/nixpkgs':
    'github:NixOS/nixpkgs/2c15aa59df0017ca140d9ba302412298ab4bf22a' (2024-12-02)
  → 'github:NixOS/nixpkgs/4bc9c909d9ac828a039f288cf872d16d38185db8' (2025-01-08)
• Updated input 'nixpkgs':
    'github:NixOS/nixpkgs/566e53c2ad750c84f6d31f9ccb9d00f823165550' (2024-12-03)
  → 'github:NixOS/nixpkgs/32af3611f6f05655ca166a0b1f47b57c762b5192' (2025-01-09)

* flake.lock: Update

Flake lock file updates:

• Updated input 'devkitNix':
    'github:bandithedoge/devkitNix/95fd44f4ac7cecf24edf22daa899a516df73c6b7' (2025-01-11)
  → 'github:bandithedoge/devkitNix/a344b0200a044f2d2ff99685f13ff7c53106428e' (2025-02-06)
• Updated input 'devkitNix/nixpkgs':
    'github:NixOS/nixpkgs/4bc9c909d9ac828a039f288cf872d16d38185db8' (2025-01-08)
  → 'github:NixOS/nixpkgs/5b2753b0356d1c951d7a3ef1d086ba5a71fff43c' (2025-02-05)
• Updated input 'hax-nur':
    'github:ihaveamac/nur-packages/cd49afba206c2eb10a349d92470fdf2cc942ae23' (2025-01-11)
  → 'github:ihaveamac/nur-packages/2ce890cab4e948109ad1ad82ba18e69240a0d352' (2025-02-06)
• Updated input 'hax-nur/nixpkgs':
    'github:NixOS/nixpkgs/4bc9c909d9ac828a039f288cf872d16d38185db8' (2025-01-08)
  → 'github:NixOS/nixpkgs/8532db2a88ba56de9188af72134d93e39fd825f3' (2025-02-02)
• Added input 'hax-nur/treefmt-nix':
    'github:numtide/treefmt-nix/bebf27d00f7d10ba75332a0541ac43676985dea3' (2025-01-28)
• Added input 'hax-nur/treefmt-nix/nixpkgs':
    follows 'hax-nur/nixpkgs'
• Updated input 'nixpkgs':
    'github:NixOS/nixpkgs/32af3611f6f05655ca166a0b1f47b57c762b5192' (2025-01-09)
  → 'github:NixOS/nixpkgs/5b2753b0356d1c951d7a3ef1d086ba5a71fff43c' (2025-02-05)

* make devkitNix and hax-nur inputs follow nixpkgs

* Also dump section headers on .dis file

* prepare for upstream merge

* Restore original README.
* Remove flake, was only used for my own testing

* fix accidental removal of LIBS

* copy lua-doc.md into release archive

* README: update to mention Lua in place of GM9Script, add credits

* lua-doc: fix typo

* add sample HelloScript

* lua-doc: remove wip notice, since all gm9script features are replicated

* remove accidental inclusion of language.inl

* Fix mixture of tabs and spaces

* remove accidental nix leftover

* re-add @ for add2tar command

---------

Co-authored-by: luigoalma <luigoalma@hotmail.com>
Co-authored-by: Gruetzig <florianavilov@gmail.com>
Co-authored-by: Florian <88926852+Gruetzig@users.noreply.github.com>
Co-authored-by: Wolfvak <soherrera1@hotmail.com>
2025-03-21 08:25:04 +01:00
Wolfvak
50270a820c
Split up the ARM9 code and data sections (#883)
* Split up the ARM9 code (.text, .vectors) and data (.rodata, .data, .bss) sections into their own ELFs.

This allows us to use more ARM9 WRAM while leaving the 128k BootROM mirror intact.

* use the makefile definition

* Also dump section headers on .dis file
2025-03-15 14:25:23 +01:00
Pk11
61c79e3e3f Add Fusion font for Chinese 2025-03-13 13:37:54 +01:00
Pk11
ab222de6b1 Update translations
- New! Chinese (Simplified) 100%
- German 100%
- French 94%
- Dutch 46%
- Spanish 41%
- Italian 27%
- Ryukyuan 18%
- Indonesian still 100%, but changed
2025-03-13 13:37:54 +01:00
Wolfvak
143fcf0d6b Display hours in ETA if the remaining seconds is >= 3600 2025-03-12 17:12:02 +01:00
Wolfvak
157848c770 Add .vscode to the gitignore 2025-03-09 17:06:18 -03:00
Pk11
8cde50e091
Automate translation files (#880)
* Automate language.inl file

* Move version number to JSON file

Better than being a magic number somewhere in the code, source code gets it from source.json

* Automate creation of .trf translation files
2024-11-30 08:24:15 -03:00
profi200
105f4ae5f7
Increased CTRCARD clock from 4.18 to 13.4 MHz on ROM reads. (#873) 2024-11-26 19:32:51 -03:00
ZeroSkill1
9e7df4c52d 'update CI to use upload-artifacts v4' 2024-11-14 12:49:09 +01:00
ZeroSkill1
208f12bde7 Fix multi RedNAND 2x not being set up properly 2024-11-14 12:49:09 +01:00
Wolfvak
7b6b478582
Use VA start and end linker symbols instead of length (#865) 2024-06-19 16:19:45 -03:00
Fra
eee63dd155
Fix fcram boot bug (#864)
Fixed old bug that appeared regarding the fcram boot failing.
2024-06-15 15:37:11 -03:00
Ricca665
dab90a9162 Update Makefile
Fixes python3 not found when running make on windows
2024-04-04 15:25:50 +02:00
Luís Marques
26990ca23a
Key 0x2C is different per platform (#842)
Change KeyY to generate same KeyNormal on dev
2024-03-17 11:27:27 -03:00
Pk11
338a2aa98a
Fix crashing in script runner mode (#840) 2024-02-20 15:13:57 -03:00
Luís Marques
399740b50e
Undefined behavior fix on ticket.c (#838)
Stack garbage is always a luck of the draw
This was causing issues on non-LTO build.
LTO was just lucky.
2024-01-25 11:28:03 +01:00
ihaveahax
ad8b5e0a8c
arm9/arm11 Makefile: Fix building with spaces in path (#828)
This quotes $(CURDIR) which fixes an issue with building GM9 in a path
with spaces, which Windows users are likely to have.
2023-08-25 21:58:22 -03:00
Wolfvak
14b390a943
Move the start of AHBWRAM to be immediately after the VECTORS (#827) 2023-08-23 10:51:35 -03:00
Wolfvak
031762a1fe
Embed the VRAM tar data in the ARM9 executable (#824)
* Embed the VRAM tar data in the ARM9 executable

* Fix Makefile dependency order

* Use address difference instead of absolute word for the VRAM drive limit
2023-07-24 10:17:37 -03:00
Danny Tsai
8b362c977a
Correct installable address whitelist (#816)
* Correct installable address whitelist

blacklist is stored at 0xb088~0xb0bf, which is ulong[14] in
{start(inclusive), end(exclusive)} pair.
one thing to note is that boot9 use inclusive comparing with
blacklist start for both section load address and
section load address + section size (comparing logic is
at 0xa42e~0xa449), so if the firm fits perfectly at the end
of the space right before the blacklisted range,
it'll also be rejected.

* shrink vram drive size to avoid bleeding into blacklisted range
2023-07-21 21:39:08 -03:00
Nemris
11b05d7a3d Fix leftover typo 2023-07-20 12:58:36 +02:00
Nemris
3008bfed61 Reduce noise and drop dataclasses 2023-07-20 12:58:36 +02:00
Nemris
f7a9b3eec8 Refactor to improve modularity
This commit adds documentation and type annotations, and allows the
script to be imported as a module.
2023-07-20 12:58:36 +02:00
Pk11
e1fa23a031 Update translations
- Spanish: 31%
- Japanese (kana only): New, 27%
- Ryukyuan (Uchinaaguchi): New, 18%
2023-07-10 16:35:42 +02:00
Pk11
8006fbd5db Update translations
- Improvements to Indonesian
2023-07-10 16:35:42 +02:00
Pk11
b4f04d3620 Update translations
- Indonesian: New and DONE!
- Italian: New, 11%
- Spanish, French, Dutch: Fairly minor improvements
2023-07-10 16:35:42 +02:00
Pk11
eb37a21354 Remove untranslated trailing strings 2023-07-10 16:35:42 +02:00
Pk11
a68d7d0cb7 Update translations
Added the date/time and number formats
I just did these myself since I wanted to test them more thoroughly, based on JavaScript's Intl
2023-07-10 16:35:42 +02:00
Pk11
87f9b82cd3 Update translations
- Added 3 new strings, only Japanese translated so far
2023-07-10 16:35:42 +02:00
Pk11
a293c008d5 Update translations
- Japanese: Improved
- German: 66%
- French: 53%
- Dutch: 27%
2023-07-10 16:35:42 +02:00
Pk11
780016933c Update translations
Japanese 100%!!
2023-07-10 16:35:42 +02:00
Pk11
041ff61d41 Update translations
- Japanese: 71%
- French: 39%
- 10 strings edited
2023-07-10 16:35:42 +02:00
Pk11
1fd15657d8 Update default font, add Japanese font 2023-07-10 16:35:42 +02:00
Pk11
0971b3d9fa Add translations
Spanish, French, Japanese, Dutch, Polish, and Russian
2023-07-10 16:35:42 +02:00
Danny Tsai
b7c97af144
Update README.md to match current VRAM drive info (#815) 2023-06-02 18:13:09 -03:00
Wolfvak
723529e2d8
Fix compilation warnings on dkA r60 (#808)
Mostly just switched out the bound strn* functions for their unbound variants when the string is a known constant
2023-04-25 10:13:28 -03:00
Wolfvak
b5fca3bc7e
Use the correct MCU LED period for calculations (#807)
Fixes #772
2023-04-25 10:13:07 -03:00
Pk11
620e5061c5 Add font/language info to README, Pk11 to credits 2023-04-15 12:54:02 +02:00
Pk11
5aaac66eef Use sizeof in snprintf where possible, ensure UTF_BUFFER_BYTESIZE
This commit looks a lot bigger than it really is, I noticed a couple spots where with these issues so I ran a regex to find all possible occurrences and switched all that could be, after manually ensuring it was actually correct

Using sizeof on the buffer (as long as the buffer is a char *array*, not a pointer!!) greatly reduces the chance of something having the wrong size because of a later change to the buffer, notably a couple snprintfs were missed in the UTF_BUFFER_BYTESIZE change
2023-04-15 12:54:02 +02:00
Pk11
439e06334b Add language selection on first load & from HOME 2023-04-15 12:54:02 +02:00
Pk11
8303440c19 Add loading translations from TRF
Reduce pointer magic use

Both translations and fonts
2023-04-15 12:54:02 +02:00
Pk11
93ee590cad Make strings translatable 2023-04-15 12:54:02 +02:00
Pk11
cae3d272d3
Only reserve space in trimmed NDS files for RSA key if it exists (#804) 2023-04-09 15:38:35 -03:00
ZeroSkill1
64414e12ab fix buffer overflow
This would cause exceptions when encrypting/decrypting CIA files to
`0:/gm9/out`.
2023-03-23 16:57:47 +01:00
Pk11
9514755989 Update Cyrillic homoglyphs
Whoops, forgot to update a couple of the Cyrillic letters where I changed their Latin homoglyphs
2023-01-31 13:21:52 +01:00
Pk11
ccd21984b2 Remake ASCII part of font
Actually a little more than ASCII, everything that was previously from the Linux kernel font
Linux is GPL2, GM9 is GPL3, thus this is sorta kinda maybe in violation of that
It's complicated since typefaces cannot be copyrighted in many countries, so arguably it's fine
2023-01-31 13:21:52 +01:00
d0k3
a23ba0e14b Fix a typo 2022-11-28 09:12:56 +01:00
d0k3
3710ed975b Enable compatibility with mGBA RTS savegames 2022-11-28 09:11:57 +01:00
Pk11
9416ec5ac0 Make select prompt scroll if too many options
Also a little cleanup to the file browse one for consistency
2022-10-24 14:35:04 +02:00
Myriachan
c9d792cb27 Add Brazil to SysInfo serial number recognition. 2022-09-27 16:59:57 +02:00
d0k3
096e6c3cb7 Add padding byte value to cart info file
Fixes #780
2022-07-22 21:43:23 +02:00
Gabriel Marcano
b11194397a
use sizeof with snprintf when target is an array (#778)
- GCC warned of a case when the size specified in a snprintf call was
   larger than the size of a target buffer. To fix, when possible use
   sizeof() to match buffer size.
2022-06-23 17:37:19 -03:00
aspargas2
658c9b491c use --use-blx linker argument
makes firm about 500 bytes smaller for free
2022-04-16 18:40:47 -04:00
d0k3
f611b31c0c Sixth Anniversary splash 2022-03-22 19:42:06 +01:00
d0k3
c13bba4cfe Fixed a boatload of compiler warnings 2022-03-22 19:40:47 +01:00
d0k3
d95a606ec2 Fix bad trimming of certain NDS dumps
Fixes #763
2022-03-08 18:47:32 +01:00
Pk11
586d30fafa Improve DSiWare save generation 2022-02-21 23:57:02 -06:00
aspargas2
682b570ef7 scripting: don't initialize dynamic env vars until they're needed
this should fix the long delay on loading any script
2022-01-12 11:33:04 -05:00
aspargas2
8d1557191f fix conflicting move/copy flags 2022-01-08 19:07:53 -05:00
aspargas2
cb11db6f1b fix crashes when showing certain error messages 2021-12-02 18:33:53 -05:00
aspargas2
1554aac4e1 fix hexeditor instructions formatting 2021-12-02 14:22:13 -05:00
aspargas2
fdbca10773 add format attribute to printf-like functions 2021-12-01 17:07:46 -05:00
d0k3
1f8de2af99 Add new default splash logo 2021-11-21 13:13:42 +01:00
d0k3
1b8bd121b6 Added raw cart dumper (R+A on cart drive) 2021-11-14 22:14:59 +01:00
d0k3
ba10ce96c3 Scripting: added cartdump command 2021-11-14 22:14:59 +01:00
d0k3
25bf8b3f93 Added installer for cifinish.bin files 2021-11-14 22:14:59 +01:00
Pk11
07cb94d99a Add 美咲ゴシック (Misaki Gothic) 8x8 Japanese font 2021-11-14 22:14:59 +01:00
Pk11
1ffbca7d46 Allow loading FRF fonts up to 0x20000 bytes 2021-11-14 22:14:59 +01:00
Pk11
942e67e507 Add Unicode hex input 2021-11-14 22:14:58 +01:00
Pk11
830479f50c Fix multibyte letters in keyboard and input prompt 2021-11-14 22:14:58 +01:00
Pk11
0275a85121 Convert old escapes to Unicode
Only does the ones below ASCII (0x00 - 0x1F), hopefully none of the high ones are important because they'll conflict with Unicode codepoints
2021-11-14 22:14:58 +01:00
Pk11
3eb92754bc Use lookup table for ASCII to avoid binary search 2021-11-14 22:14:58 +01:00
Pk11
77fc7af2f2 Use macro for UTF-8 byte count
Makes it more clear why all of the buffers are being multiplied by 4

Fix UTF-8 bytesize macro

Before UTF_BUFFER_BYTESIZE(str_width - 10)] would multiply the 10, not the whole number, by UTF_MAX_BYTES_PER_RUNE

Do (rune_count + 1) * 4 in UTF-8 bytesize macro

Fix Resize/Truncate String snprintf size

Before it would break if the last character was multi-byte
2021-11-14 22:14:53 +01:00
Pk11
b366200d4b Switch to a RIFF font format
Fix height of ラ character

I accidentally made it 1px too tall before

Add Cyrillic to default font

Make Я more like latin R

Right after I commit, looking at my screenshot I notice I forgot to tweak the Я to be more angled like this font's latin R...

Improve the default font's Kana

derp fix

Properly handle invalid UTF-8

Fix conversion PBMs with non-byte aligned rows

Rename font extension to .frf

For Font RiFf

Re-add PBM font support

Default converting to CP-437 and try guess size

Revert "Default converting to CP-437 and try guess size"

Reverts 2c9a47d224b28cbb51a3ee335fd9970265201b72 as I think the old behaviour works better given PBM font support being kept

Re-add mapping file for CP-437

Automatically use mapping file with same name as image

ex. for "font_6x10.pbm" it will use "font_6x10.txt" in the same directory
2021-11-14 22:13:55 +01:00
Pk11
13eb4f8869 Proper handling of UTF-8
Note: This commit may be slightly broken, I'm just splitting up it and the next one at the end.
2021-11-14 22:13:32 +01:00
TimmSkiller
55385a5502
Fixed inability to install a larger/smaller ticket when a smaller/large one is installed (#733)
* Fixed inability to install a larger ticket when a smaller one is installed

* improved AddBDRIEntry checks, formatting and added define for REPLACE_SIZE_MISMATCH
2021-10-23 13:19:13 +02:00
TimmSkiller
0dbe70928f
fix #729 - building titlekeys.bin files
* fix #729

* Improved code layout & show NAND type when building support files
2021-10-23 13:14:53 +02:00
aspargas2
7feeb51a65 split SHA-1 evenly over two lines (like SHA-256) 2021-10-21 18:22:07 -04:00
d0k3
c966acc851 Display SHA-1 over two lines (like SHA-256) 2021-10-09 11:58:47 +02:00
BuildTools
e042886db4 add user-facing sha1 support 2021-10-09 11:58:47 +02:00
d0k3
ddf577b88c Fix #739 2021-10-09 11:13:51 +02:00
d0k3
3124d944a6 Reenable searhcing titlekeys from illegit tickets
Fixes #595
2021-10-09 10:51:52 +02:00
Balint Kovacs
27e316571d Add a new type of flash chips, from Art Academy
It adds a new chip type, that works like regular 3DS saves. It
presumably is manufactured by Macronix as well.

Thank you for your help, @FerozElMejor on Discord and @Epicpkmn11
2021-09-26 16:36:19 +02:00
d0k3
0e46d4fca8 Ammend the borked previous commit 2021-08-31 13:36:57 +02:00
d0k3
33d59f6d3a Add 0x88 to NDS cart dumps trimmed size 2021-08-19 21:09:13 +02:00
lifehackerhansol
d85023b173 Append 0x88 to ntr_rom_size to preserve RSA keys 2021-08-12 20:39:18 +02:00
d0k3
4dc96d37e8 Fix #720 2021-07-23 14:01:19 +02:00
d0k3
c9b6a335f7 Add LARGEDLC mode for titles with > 1024 contents
Fixes #703 and is only active with `make LARGEDLC=1` and will break compatibility with other titles and CIAs. Thanks @luigoalma for new ticket builder code!
2021-07-07 18:00:11 +02:00
d0k3
33a115b75c Fix strings -> chars 2021-07-06 18:48:04 +02:00
TimmSkiller
ef161bce42 Fix NDS / DSi names being cut off when renaming to good name 2021-07-06 17:14:44 +02:00
WaluigiWare64
d8d43c14f3 Detect transparent pixels in DS icon and set them to white 2021-06-23 18:46:39 +01:00
Wolfvak
37c8c50097
fix the "conact" typo (#709)
also adds a direct link to the irc channel
2021-06-02 09:03:37 -03:00
Wolfvak
c2d96c0d9c add a "contact info" section in readme
contains both irc and discord links, and removes the discord link at the bottom
2021-06-01 20:07:02 +02:00
luigoalma
41d36c620f Make safe for editing certificate function
As long memory max bounds are still respected
2021-05-22 14:12:19 +02:00
luigoalma
4b5ac1a8e0 Certificate provide signature verification call 2021-05-22 14:12:19 +02:00
luigoalma
be289b4c55 Just search both nands for certs on callee
Since in all cases that LoadCertFromCertDb is called
is always twice, one for sysnand and another for emunand
just make it a single call and quit early when cert found.
2021-05-22 14:12:19 +02:00
luigoalma
3bfb9ef6ec Make cert bundle building nicer
At least in the caller perspective.
Also break down some functionalities into separate funcs,
interally calling them on cert.c to avoid too many checks.
And tried to avoid too much repeated code.
2021-05-22 14:12:19 +02:00
luigoalma
1f96b5e9e6 Certificate static storage
Decrease repeated load times
At least for retail/dev certs
2021-05-22 14:12:19 +02:00
luigoalma
8427e0776c Adjusting ticket.h
Just because there's a more neat to look at definition
on types.h for packed and aligned
2021-05-22 14:12:19 +02:00
luigoalma
61c17e491f Load from certs.db more accordingly
Also extra cert handling code
2021-05-22 14:12:19 +02:00
d0k3
236d2dc09c Fix CIA good renaming (add version) 2021-04-29 20:50:13 +02:00
d0k3
8680358aa1 Fix #702 2021-04-29 19:58:12 +02:00
d0k3
7e01954e48 Properly handle emanuals in update images 2021-04-01 09:29:49 +02:00
d0k3
0825139cb2 Scripting: add SDSIZE, SDFREE and NANDFREE global variables
fixes #691
2021-03-31 17:33:05 +02:00
clach04
9f431a5fde
helper text for SysNAND backup size
https://3ds.hacks.guide/finalizing-setup Section VIII, 3 - recommends 1.3 Gb free

On a Japanese firmware 11.10.0-43J - needed 950Mb free.
2021-03-31 17:07:01 +02:00
Balint Kovacs
cc99734fac Fix IR card support
This is based on the latest update of GBATEK.
2021-03-31 17:01:32 +02:00
Balint Kovacs
6799b24730 Fix a bug on 512B EEPROM saves
Fixes #690
2021-03-31 17:01:32 +02:00
Balint Kovacs
adb8ad260f Add some documentation to cart types, and remove old exports 2021-03-31 17:01:32 +02:00
Balint Kovacs
5c2ab6958c I found a new type of flash chip on a bootleg cart 2021-03-31 17:01:32 +02:00
Balint Kovacs
7af76b91bb Detect save size by the last byte of JEDEC id, if possible
This is my second attempt to resolve #553

Props to @wwylele for pointing out that the last byte of the JEDEC ID is
just the exponent of the size (base 2)
2021-03-31 17:01:31 +02:00
d0k3
ce50bd63a8 Add 5th anniversary splash screen 2021-03-22 17:43:33 +01:00
d0k3
1a27dcb1e8 Remove possibly faulty tickets when installing CIAs
fixes #685
2021-03-22 17:42:31 +01:00
d0k3
c20911047a Don't allow ticket installations from mounted media 2021-03-22 17:37:24 +01:00
d0k3
fd8c5d1897 Show title id & game icon in file handler menu 2021-03-18 22:44:33 +01:00
d0k3
0bbbc7c324 gamecart.c: fix indentation, a small merging mistake 2021-03-18 00:33:58 +01:00
Balint Kovacs
9f52deedad Card-2 saves: Detect save size from ExHeader
This also limits the save blanking to the actual save area. This way
every byte of the cart can be read again.
2021-03-18 00:28:44 +01:00
Balint Kovacs
60f2c5192d Add save chip JEDEC ID to gamecard info 2021-03-18 00:28:44 +01:00
Balint Kovacs
f2e52bd1c7 Initial support for CARD2 read
Can I get card2 writing to work?

Am I _that_ good?

Will I break by copy of X?

I won't know until I try!
2021-03-18 00:28:44 +01:00
Balint Kovacs
dfb2dff352 Some groundwork for CARD2 save support 2021-03-18 00:28:44 +01:00
d0k3
1cb72a87e1 Allow verifying incomplete DLCs 2021-03-18 00:08:14 +01:00
Margen67
3952de3a1e ci.yml: changes
Change image to ubuntu-latest.
Remove redundant :latest from container.
Make python3 pip install into one less line.
Add -j to make to speed it up.
Replace cd with working-directory and remove unneeded cd.
Add if-no-files-found: error to upload-artifact.
2021-03-14 14:59:05 +01:00
d0k3
294890057f Display full filename in file info (for long filenames) 2021-03-14 14:56:35 +01:00
d0k3
4e00b8b7b6 Include version number in renamed CIA / titles 2021-03-14 14:56:35 +01:00
d0k3
ddfdf81cf7 Combine & improve game info and CIA/title checker 2021-03-11 20:24:39 +01:00
SirNapkin1334
8b5af2c22f Update Discord Link :/ 2021-03-11 08:39:28 +01:00
MechanicalDragon
445688c5fb fix clear friendlist script missing end statement 2021-03-09 18:04:27 +01:00
d0k3
8fa85437dd Added ability to dump tickets in title manager 2021-03-08 23:52:55 +01:00
d0k3
2cd6acb31e Enabled title checker tool in Title Manager 2021-03-08 18:35:49 +01:00
Garrett Holmstrom
c70a7db0f3 Include scripts in ntr firms
Unless the SD card happens to both be accessible and contain a
`gm9/scripts` directory, a system booted with a NTR cartridge isn't going
to have access to the stock scripts and will just show an error upon
going to the menu and selecting `Scripts...`.  This patch adds scripts
to the vram tarball only for ntr builds so they can be available even
on completely untouched systems.  It shouldn't be necessary when we've
booted from something else because in those cases we've already written
GodMode9.firm to flash using some other means and could have copied all
the scripts then.

One might argue this is sub-optimal because the menu will point there
even if a scripts directory happens to exist on the SD card.  One might
instead argue that that behavior is preferable because there's no telling
what gm9 version the scripts on the SD card were intended for.
2021-03-08 18:31:40 +01:00
d0k3
7bdd01738a Show a wait... message when uninstalling a single title 2021-02-27 12:18:39 +01:00
d0k3
152c6c4d5a Allow in-place encryption/decryption only on SD card 2021-02-27 12:18:14 +01:00
d0k3
32936345a6 Change support file recommendations 2021-02-23 16:10:44 +01:00
d0k3
30c5e1fd67 Add firstrun instructions to software keyboard
Adresses #601
2021-02-23 16:10:10 +01:00
d0k3
3f7eb872b8 Remove aeskeydb.bin warning for entrypoints other than b9s
Fixes #676
2021-02-23 15:45:09 +01:00
d0k3
bea16124a4 Readme updates 2021-02-22 17:12:07 +01:00
d0k3
71e3b6f73c Trimming functionality for GBA cart dumps 2021-02-22 16:41:21 +01:00
d0k3
ee43fe328f Fix a compiler warning 2021-02-21 14:40:33 +01:00
Wolfvak
31389687ab event model seems to work
refactors all the ugly "pendingX" atomic operations into a single "Event" subsystem/interface thing with two operations

moves all existing code to use this instead

also changes the "bkpt" macro to also indicate unreachable code
2021-02-21 14:40:33 +01:00
Wolfvak
eadc1ab6b9 simplify the sharedmem buffers
also made the wait on boot unconditional
2021-02-21 14:40:33 +01:00
Wolfvak
9ecb90a2ba make interrupt handlers more lazy, most processing is done in interruptible context now
- completely moved MCU interrupt handling outside of the critical section
- refactored a bit of the PXI code and command names
- merge the I2C read and write cmds to be one
- remove SET_VMODE cmd, now it's always initialized to BGR565 on boot and to RGB565 on firmlaunch
- atomic-ize more stuff
2021-02-21 14:40:33 +01:00
Wolfvak
a6e20c641a initial mcu events implementation
- backlight power control is reinstated but currently buggy, for some reason the __builtin_trap is tripped on GFX_powerOnBacklights and GFX_powerOffBacklights

- also refactored a bunch of other code pertaining to mcu and other hw init, moved the gpu init to a later point since lcd init now depends on mcu events
2021-02-21 14:40:32 +01:00
d0k3
c4b3b582a7 Improved good renamer
... includes better naming for CIAs
2021-02-21 13:14:08 +01:00
d0k3
c31737c257 Title info and CIA metadata handling for encrypted CIA/CDN content 2021-02-20 12:21:40 +01:00
d0k3
2f61722aa4 Replace Tabs -> Spaces 2021-02-18 19:06:31 +01:00
d0k3
ab4316fd4e Prevent titleentry deletion in title manager 2021-02-18 18:55:36 +01:00
d0k3
24195c319a Install, build CIA, verify handling for TWL CDN content 2021-02-18 18:34:20 +01:00
d0k3
f9408a9c10 Improved CIA building, proper names for legit CIAs 2021-02-17 15:50:52 +01:00
d0k3
8114a0bd26 Higher level warning for editing 0:/Nintendo 3DS/ folder 2021-02-15 17:58:59 +01:00
d0k3
7620310b73 Build CIA and verify handling for TADs 2021-02-12 15:02:55 +01:00
d0k3
1e9fb36582 Check TMD signature when attempting legit CIA build 2021-02-12 12:50:23 +01:00
d0k3
9191a3244f Fix splash title underline for long build names 2021-02-09 19:14:24 +01:00
d0k3
af5e1a218e Show title id in title info 2021-02-08 19:04:12 +01:00
d0k3
667a1bf2c0 Install, verify, info handling for tickets 2021-02-06 14:47:40 +01:00
d0k3
48347c947a Reset to default splash.png 2021-02-05 13:16:01 +01:00
d0k3
e41b098843 Misc code improvements 2021-02-05 13:16:01 +01:00
d0k3
58fb9913d5 Fix possible overwriting of memory in vff.c 2021-02-01 18:59:55 +01:00
d0k3
899c8a8816 Updated mounted ticket.db categories
Fixes #641
2021-01-31 14:29:48 +01:00
d0k3
203cf7f9e3 Include obfuscated AES key database 2021-01-30 13:22:05 +01:00
d0k3
8ebb74b0bc Allow dumping NDS carts with secure area encrypted
Fixes #417
2021-01-30 13:22:05 +01:00
d0k3
af14376c84 Scripting: Allow escaping quotes with \"
Fixes #632
2021-01-30 13:21:20 +01:00
d0k3
647d5722aa gamecart: Replaced ID file with info file 2020-12-24 15:53:59 +01:00
David Korth
25a22d30d0 gamecart: Added a gamecart_id.bin file.
This contains the 4-byte chip ID.
2020-12-24 15:53:59 +01:00
d0k3
2f2b7faeb4 Disable title manager for mounted images 2020-12-24 15:53:59 +01:00
d0k3
efcfed31b3 Add title manager shortcuts 2020-12-24 15:53:59 +01:00
d0k3
7f6f6db410 Add title manager (replaces title search) 2020-12-24 15:53:59 +01:00
d0k3
e9599aad1c Fix building CIA from CDN files
fixes #661
2020-12-23 18:00:34 +01:00
d0k3
46a9a5819a System info: add UK as a sales region
Fixes #658
2020-12-23 16:32:57 +01:00
Wolfvak
65f6748dc1 Fix uninitialized variable on unaligned SPI Xfer
We use a stack-allocated u32 to store a temporary word that gets memcpy'd from a potentially unaligned buffer, but the size of the copy could be less than 4 bytes, therefore leaving garbage in the upper bits of said word. This fixes CODEC in Corgi3DS.
2020-12-23 14:56:18 +01:00
David Korth
d63db4bc6d gamecart: Use the chip ID's TWL flag to check if we should do TWL secure area init.
It's possible to flash a TWL-enhanced ROM image to an NTR dev cart.
This cart would function properly as a Nintendo DS game, but might
have issues on a DSi or 3DS.

GodMode9 couldn't dump this type of cartridge before because the ROM
header indicates TWL, but the cartridge doesn't understand the 0x3D
TWL secure area init command, so key exchange failed.
2020-12-09 21:53:43 +01:00
David Korth
cadd21508f gamecart: Use the chip ID to determine the ROM size.
Some development carts have an incorrect ROM header, but the cart ID
is always correct, so prefer the chip ID. If the chip ID is invalid
(unlikely), then fall back to the ROM header.
2020-12-09 21:53:30 +01:00
d0k3
01dd46ced3 Fix CMAC handling for TWLN and IMGNAND
Fixes #653
2020-11-25 23:48:36 +01:00
d0k3
e95e0fe90c Fix saves being deleted on uninstall 2020-11-25 23:08:00 +01:00
d0k3
608cf39e12 Fix #601
This adds SELECT as a shortcut ot the old button based input tool in the software keyboard
2020-11-11 00:07:09 +01:00
d0k3
df4619b213 Fix building standard CIA from TMD 2020-11-10 23:58:58 +01:00
aspargas2
cebc43792e
remove travis CI configuration 2020-10-28 18:40:26 -04:00
aspargas2
89be93384d
enable github workflows CI 2020-10-28 18:29:27 -04:00
d0k3
145bf6de54 Improve ticket searching functions
This is applies to encTitlekeys.db / decTitlekeys.bin builders as well as to to legit CIA building
2020-10-28 00:01:36 +01:00
d0k3
355519285a Verify TMD when installing / building CIA 2020-10-28 00:01:36 +01:00
d0k3
ae32e63074 Handle seed crypto when installing game images 2020-10-28 00:01:36 +01:00
d0k3
d5db8c7216 Fix signed integer sXX types
Greetings from 2017 @Wolfvak  /s
2020-10-28 00:01:36 +01:00
d0k3
f2876b2a61 Fix #628 2020-10-28 00:01:36 +01:00
d0k3
3b59b0bbfb Properly clear system saves on uninstall 2020-10-28 00:01:36 +01:00
aspargas2
bd7658a808 revert megascript refactor
because the refactor didn't work at all. my screwup.
2020-10-04 14:30:29 +00:00
aspargas2
7c01e1dfe9
allow exiting the megascript by pressing B 2020-09-26 15:27:22 -04:00
aspargas2
8e2d1d465e
update the SD cleanup section of the megascript 2020-09-24 22:09:09 -04:00
Gabriel Marcano
d010f2858b Remove trailing white space
- Removed trailing whitespace from all source code files (.c, .h. and
   .s) and the README.md
2020-08-26 23:01:58 +02:00
Gabriel Marcano
d682a65df6
Fix GCC warning, snprintf limit too long (#623)
- One snprintf in arm9/source/godmode.c call had a limit that was past
   the size of the datestr variable it was writing into. Fixed to match
   the size of the variable.
2020-08-24 23:42:56 -03:00
Wolfvak
1f2514f19e forgot to update non-FIXED_BRIGHTNESS mode 2020-08-22 17:15:10 -03:00
Wolfvak
a2e574a451 fix #622 2020-08-22 16:37:27 -03:00
aspargas2
9189320e8b
fix installing from CDN files 2020-08-20 14:31:16 -04:00
d0k3
896d548851 Additional functionality for .tie files 2020-08-20 19:43:52 +02:00
d0k3
df9b84fc66 Installer: show an error message for missing .dbs 2020-08-20 19:43:52 +02:00
d0k3
f7c229b424 Properly handle TWL saves when installing 2020-08-20 19:43:52 +02:00
d0k3
253cb2849c CIA building / installing: also take over SRL flags 2020-08-20 19:43:52 +02:00
d0k3
158fa78a5d Properly take over extdata ID when installing 2020-08-20 19:43:52 +02:00
d0k3
62fd3e93c5 TitleDB entries: add NCCH version only if DLP exists
Thanks @ihaveamac & @TurdPooCharger
2020-08-20 19:43:52 +02:00
d0k3
ffeb551a3f Don't allow game installs from images 2020-08-20 19:43:52 +02:00
d0k3
c7f10be1b4 CIA installer: only fix console ID when different 2020-08-20 19:43:51 +02:00
d0k3
f48bb58bac TicketDB mount: show console ID as big endian 2020-08-20 19:43:51 +02:00
d0k3
15bf632143 Fix building CIA from TMD 2020-08-20 19:43:51 +02:00
d0k3
50e3e1f19a Properly remount last image after searching tickets 2020-08-20 19:43:51 +02:00
d0k3
99b69754b2 Handle cleanup before installs and after failed installs 2020-08-20 19:43:51 +02:00
aspargas2
4af0268e5a visual fix 2020-08-20 19:43:51 +02:00
aspargas2
e5ffa885b6 fix directly remounting the file which is already mounted 2020-08-20 19:43:51 +02:00
d0k3
f0ad45dd60 Basic uninstaller support for title.db entries 2020-08-20 19:43:51 +02:00
d0k3
6eb546dfab Game installer: Misc code improvements 2020-08-20 19:43:51 +02:00
d0k3
18e6d9f0db Basic support for handling title.db entries
"Show title info"
2020-08-20 19:43:51 +02:00
d0k3
0af181ac6a Check for import.db and title.db before attempting install 2020-08-20 19:43:51 +02:00
d0k3
3aa9a1633a Fix game image installs to SD
Yes, I broke this (again)
2020-08-20 19:43:51 +02:00
Wolfvak
ce498103e1
change barrier ids
should allow old and bugged GM9 versions to boot the newer ones
2020-08-20 09:59:52 -03:00
Wolfvak
7edf8a998b
Merge pull request #620 from d0k3/barrier_racefix
add another pxi barrier for firmlaunch
2020-08-20 08:47:42 -03:00
Wolfvak
68a4ceac5b add another pxi barrier for firmlaunch
fixes a race condition when booting gm9 from itself (especially noticeable on new3DS consoles)
2020-08-19 23:11:45 -03:00
d0k3
0038e7d0ab Fix building via Travis CI 2020-08-16 22:31:08 +02:00
Wolfvak
03007c2b42 fix shared memory optimization problem 2020-08-15 20:30:27 -03:00
d0k3
8375434093 Properly handle TWL system data archives 2020-08-04 21:46:13 +02:00
Wolfvak
bf767f2c01
add a needed delay for new 3ds consoles (#617) 2020-08-04 16:41:10 -03:00
Wolfvak
4dc5661d58 use hardcoded configuration for ARM11 interrupts 2020-08-02 11:40:18 -03:00
d0k3
8863979a99 Merge branch 'thumb' 2020-08-02 15:43:28 +02:00
d0k3
0ee1368153 Fix building on Windows (cygwin) 2020-08-02 15:40:42 +02:00
d0k3
a051b07791 Don't allow the rom renamer in incompatible drives 2020-08-02 15:40:42 +02:00
d0k3
596e7e499c Scripting: install command for game images 2020-08-02 15:40:41 +02:00
d0k3
d4d8c9a0ff Remove force_nand from the game file installer options 2020-08-02 15:40:41 +02:00
d0k3
7fb194caea Allow batch fixing of borked NCCH crypto flags
Fixes #609
2020-08-02 15:40:41 +02:00
d0k3
521fb25075 Fix titledb entry manual detection 2020-08-02 15:40:41 +02:00
d0k3
829880994f Fix installer system CMD handling 2020-08-02 15:40:41 +02:00
aspargas2
e744be504b perform DISA/DIFF cmac fixing automatically upon unmounting 2020-08-02 15:40:41 +02:00
aspargas2
e7fdf993a7 fix some tab/space indent mixing
shame on you, @d0k3 /s
2020-08-02 15:40:41 +02:00
aspargas2
89c5107733 Fix title info entry product code for TWL titles 2020-08-02 15:40:41 +02:00
aspargas2
9766a0be5e fix handling of contents whose index and ID are unrelated 2020-08-02 15:40:41 +02:00
aspargas2
24d2a4ea5b fix install destination logic 2020-08-02 15:40:41 +02:00
aspargas2
214edfc399 don't sort tickets until a virtual ticket dir is read
this makes mounting of a ticket.db almost instantaneous regardless of number of tickets it contains
2020-08-02 15:40:41 +02:00
aspargas2
cb870d2b02 fix problems with vbdri new filename handling
this should now disallow having non-hex characters in the title id and allow changing of the NAME_TIK and NAME_TID macros
2020-08-02 15:40:41 +02:00
d0k3
d8aeb056cb Fix CMD & NCSD handling 2020-08-02 15:40:41 +02:00
d0k3
b8798f2aff Check for title.db before attempting install 2020-08-02 15:40:41 +02:00
d0k3
e559c2b4a1 Fix setting the CIA console ID 2020-08-02 15:40:41 +02:00
d0k3
32b6838d32 Fix TMD CDN CIA building 2020-08-02 15:40:26 +02:00
d0k3
8fcdde29c8 Misc code beautification 2020-08-02 15:40:26 +02:00
d0k3
77f1f94e13 Improved output for game install last step 2020-08-02 15:40:26 +02:00
d0k3
e568348086 Don't overwrite existing saves when installing
thanks @aspargas2
2020-08-02 15:40:26 +02:00
d0k3
6116545fef Fix batch install of game images 2020-08-02 15:40:26 +02:00
d0k3
e916476563 Take over @wolfvak's gameutil.c improvements 2020-08-02 15:40:26 +02:00
d0k3
d2c47b7977 Allow installation of game files
Should work for NCCH, NCSD, CIA, TMD from NUS/CDN and DSi eShop titles in NDS format
2020-08-02 15:40:26 +02:00
Wolfvak
8a7448995f fixed overlooked ARM9 exception handler issue where code would be dumped incorrectly, modified ARM11 exception vectors to not take an entire page of compiled code 2020-07-26 10:27:48 -03:00
Wolfvak
07c009de72 fix comments for bootrom functions 2020-07-24 23:38:31 -03:00
Wolfvak
f96daa407a potentially fix non-working FIRM builds, remove duplicated cycle wait functions
the sdmmc wait function is exactly the same as the one in the bootrom and worked as a drop in replacement
2020-07-24 14:22:38 -03:00
Wolfvak
f835469e19 rewrite the bootrom function header, add more operations and add ARM11 versions
- the bootrom is now mapped on the ARM11

- removed the waitClks in favor of a more canonical implementation (subs r0, r0, 4/5 + branch back)
2020-07-24 13:37:29 -03:00
d0k3
2791b42f6e Fix an exception for big file searches 2020-07-24 11:02:07 +02:00
Wolfvak
d7444e144a use a regular global pointer for sharedmem
fetching the thread id requires coprocessor access which means doing funky switches between thumb and arm -
it's faster to just allocate a single pointer and do an indirect load when necessary
2020-07-23 23:46:15 -03:00
Wolfvak
3973ce57df revert back to using Thumb code for the ARM9 binary
leads to better density and therefore much smaller FIRM sizes
2020-07-23 20:33:46 -03:00
Wolfvak
929cc7fdcf better hints to reduce compiled size by a few kb
mostly just added static const to constant arrays/buffers
2020-07-23 13:46:42 -03:00
Wolfvak
698ad9d891 fix Travis CI, take 2 2020-07-23 12:13:50 -03:00
d0k3
6b54290cf2 Fix Travis CI building (hopefully)
Thanks @vaguerant
2020-07-20 22:57:37 +02:00
d0k3
79768acef7 Fix a small typo in file attribute menu 2020-07-20 00:51:20 +02:00
Wolfvak
4e9721db9b new3DS FCRAM is always enabled nowadays, so the IS_UNLOCKED check is wrong 2020-07-19 12:08:47 -03:00
Wolfvak
5e307a3f32 limit size of initrd, fix building on msys2 2020-07-19 12:03:04 -03:00
Wolfvak
6487307cf0 improved mmu and gic code 2020-07-19 11:59:52 -03:00
Wolfvak
5905fb84fb removed the cross allocator, use the shared memory region instead for I2C and NVRAM transfers 2020-07-19 11:44:03 -03:00
Wolfvak
30f0b004c2 fixed screen init, hopefully the last commmit
- properly performs gpu/backlight reset

- nukes vram so the initrd had to be moved to arm9 memory, and have its size (at least temporarily) limited to 256k
2020-07-18 20:25:34 -03:00
d0k3
f20d2657fa Revert "Use .tie extension for titledb entries"
This reverts commit 8ee0fac6c44622c75fd2bf2fb15bf0c06662caf8.
2020-07-13 17:27:21 +02:00
d0k3
eb265e9575 NCSD CIA builder: Use proper index 2020-07-09 22:53:56 +02:00
d0k3
8ee0fac6c4 Use .tie extension for titledb entries 2020-07-09 22:51:59 +02:00
aspargas2
6e0b6d2d0a invalidate vbdri cached entry when a write fails 2020-06-30 19:22:18 +02:00
aspargas2
75cae95509 sort unsigned system tickets into the homebrew directory in ticket.db mounts 2020-06-30 19:22:18 +02:00
aspargas2
519855de5b fix write permission bypass bug
attempting to open a file in a bdri mount for reading only which did not exist but had a valid name would create the file without ever unlocking appropriate write permissions
2020-06-30 19:22:18 +02:00
aspargas2
6b6fe4741d minor visual fix
previously, it was possible to select and deselect drives on the root menu by using the shortcut keys to select all files, but this would not functionally do anything
2020-06-30 19:22:18 +02:00
aspargas2
fd4fd14ee7 allow deletion of files in virtual BDRI drives 2020-06-30 19:22:18 +02:00
aspargas2
8ffe774f77 remove remaining uses of brute-force ticket.db parsing 2020-06-30 19:22:18 +02:00
aspargas2
127008a274 fix oversight in AddBDRIEntry
this would cause an uninitialized u64 to be used for the entry size in certain circumstances
2020-06-30 19:22:18 +02:00
aspargas2
6e63fdf4bf avoid malloc(0) in vbdri 2020-06-30 19:22:18 +02:00
aspargas2
c70546d5ca implement virtual mounting of BDRI files 2020-06-30 19:22:18 +02:00
aspargas2
2b94b585eb improvements to bdri.c:
* fix a bug in ReadTicketFromDB that would fail to read tickets that were not a specific size
* fix the endianness of the title IDs outputted by ListBDRIEntryTitleIDs
* improve the speed of ReadBDRIEntry and GetBDRIEntrySize by taking advantage of the hash table
2020-06-30 19:22:18 +02:00
aspargas2
b51fc388c2 change a #include to make more sense 2020-06-30 19:22:18 +02:00
aspargas2
11c3a4a71b remove inaccurate comment
the title size field in a title info entry is infact not in bytes
2020-06-30 19:22:18 +02:00
aspargas2
fd93df60c5 deinit filesystems before booting a firm payload 2020-06-30 19:22:18 +02:00
aspargas2
77b83bc89e move structs that are only used locally from .h to .c in disadiff and bdri 2020-06-30 19:22:18 +02:00
d0k3
2fd6915075 Readme: fix an error
thanks @profi200 !
2020-06-02 17:04:32 +02:00
santiago
244d24ee2a fix compiling on dkA r54, libc is bloaty so please stick with r53 for now 2020-06-01 17:13:30 -03:00
d0k3
d6e09c9c15 Updated splash screen 2020-05-18 23:24:32 +02:00
d0k3
da1dff7cd7 Legit CIAs: Change personalized ticket warning 2020-05-18 23:24:32 +02:00
d0k3
43753535d7 Readme: Added "digital preservation" paragraph 2020-05-18 23:24:32 +02:00
d0k3
a7ce125d7f Allow building "pirate legit" CIAs (TMD & encryption intact) 2020-05-11 18:02:26 +02:00
d0k3
73dc54a754 Readme: add a note about 4GiB cart dumps missing the last byte
As asked for in #593
2020-05-11 17:19:27 +02:00
d0k3
2793daa55f Take over title version when building CIA from NCSD 2020-05-11 17:19:27 +02:00
d0k3
182f1ebe43 Fix garbage data in converted CIAs
Partial fix for #587
2020-05-11 17:19:27 +02:00
d0k3
da28c0ef40 Fix #585 2020-05-11 17:19:27 +02:00
aspargas2
c265e3ac2f fix config save dumping for citra in megascript 2020-05-11 17:19:27 +02:00
santiago
1e92163245 lazy fix for the screen init bug, hopefully 2020-05-09 16:24:40 -03:00
aspargas2
2aba1afb2e remove usm files in sd cleanup script
The unSAFE_MODE exploit that will be soon added to the 3ds guide adds a few more files that need to be removed.
2020-04-27 09:54:05 +02:00
aspargas2
52034cd41f
Always inject correct sighax signatures when installing b9s in the megascript (#592)
* megascript: always inject correct sigs when installing b9s

someone just bricked by accidentally using the dev firm because there was no check for it and this was not being done

* split up sighax literal
2020-03-30 01:08:06 -03:00
luigoalma
6541e52f05 Reinforcing alignment to buffer size 2020-03-28 07:05:42 -03:00
luigoalma
e763cbaf72 Changes to CIA building for DLC on legit cases
Ignore unowned content implied by ticket.
Standard building still should include everything due to fake ticket.
2020-03-28 07:05:42 -03:00
luigoalma
978c4f8b86 Fake ticket building changes 2020-03-28 07:05:42 -03:00
luigoalma
ccc0ddf300 Adding ability to check ticket content rights 2020-03-28 07:05:42 -03:00
luigoalma
f7b7459d9f Little stack usage optimization
Just in one function's buffer but still
2020-03-28 07:05:42 -03:00
luigoalma
d27cfc71e1 Indentation fixes
Indentation fixes everywhere
(mostly anyway under arm9/source/)
And some other tab to spaces
2020-03-28 07:05:42 -03:00
luigoalma
4e04849860 Support for variable sized tickets
Except for cia building or loading cia just yet.
Added more checks on ticket content index, mainly due to having effects
in the ticket format itself, and are unknown still.
Ability to determine ticket size.
Verify signature with ticket's proper size.
Changes to use the new Ticket struct with the flexible array member.
2020-03-28 07:05:42 -03:00
luigoalma
e0e72142bc Add BDRI function for getting size
In part, partial copy of ReadBDRIEntry changed just for getting size.
2020-03-28 07:05:42 -03:00
luigoalma
4e38973384 Fix a memory leak
When running CIA checker tool
2020-03-28 07:05:42 -03:00
luigoalma
2760bb4a38 Fixes to mymalloc functions 2020-03-28 07:05:42 -03:00
330 changed files with 59517 additions and 7920 deletions

42
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,42 @@
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
container: devkitpro/devkitarm
steps:
- uses: actions/checkout@v1
- name: Fix apt sources
run: |
apt-get update
apt-get -y install dirmngr
echo 'deb http://us.archive.ubuntu.com/ubuntu/ bionic main' >> /etc/apt/sources.list
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
apt-get update
- name: Install and update packages
run: |
apt-get -y install python3 python3-pip p7zip-full libarchive13
python3 --version
python3 -m pip install --upgrade pip setuptools
python3 -m pip install cryptography git+https://github.com/TuxSH/firmtool.git
- name: Build Project
run: make release -j$(nproc)
- name: Prepare build artifact
working-directory: release
run: |
ZIPNAME=$(ls GodMode9-*.zip)
rm $ZIPNAME
echo "OUTNAME=${ZIPNAME%.zip}" >> $GITHUB_ENV
- uses: actions/upload-artifact@v4
with:
name: ${{ env.OUTNAME }}
path: release/*
if-no-files-found: error

9
.gitignore vendored
View File

@ -5,6 +5,7 @@
*.obj
*.elf
*.map
*.dis
# Precompiled Headers
*.gch
@ -35,10 +36,14 @@
# OS leftovers
desktop.ini
.DS_Store
# Sublime files
*.sublime-*
# Visual Studio Code files
.vscode
# Build directories
/build
/output
@ -48,6 +53,6 @@ desktop.ini
/data/README_internal.md
# User additions
/data/aeskeydb.bin
/data/aeskeydb_.bin
/zzz_backup
/arm9/source/language.inl
*.trf

View File

@ -1,19 +0,0 @@
language: c
sudo: true
dist: bionic
before_install:
- wget "https://github.com/devkitPro/pacman/releases/latest/download/devkitpro-pacman.deb" -O dkp-pacman.deb
- export DEVKITPRO=/opt/devkitpro
- export DEVKITARM=${DEVKITPRO}/devkitARM
install:
- sudo dpkg -i dkp-pacman.deb
- sudo dkp-pacman -S --noconfirm devkitARM
- sudo apt-get -y install python3 python3-pip p7zip-full
- sudo pip3 install --upgrade pip setuptools
- sudo pip3 install cryptography
- sudo pip3 install git+https://github.com/TuxSH/firmtool.git
script:
- make release

View File

@ -16,14 +16,31 @@ export RELDIR := release
export COMMON_DIR := ../common
# Definitions for initial RAM disk
VRAM_OUT := $(OUTDIR)/vram0.tar
VRAM_DATA := data
VRAM_FLAGS := --make-new --path-limit 99 --size-limit 3145728
VRAM_TAR := $(OUTDIR)/vram0.tar
VRAM_DATA := data/*
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)
PY3 := py -3
ifeq ($(TERM),cygwin)
PY3 := py -3 # Windows / CMD/PowerShell
else
PY3 := python3
PY3 := py # Windows / MSYS2
endif
else
PY3 := python3 # Unix-like
endif
# Definitions for ARM binaries
@ -33,18 +50,18 @@ export ASFLAGS := -g -x assembler-with-cpp $(INCLUDE)
export CFLAGS := -DDBUILTS="\"$(DBUILTS)\"" -DDBUILTL="\"$(DBUILTL)\"" -DVERSION="\"$(VERSION)\"" -DFLAVOR="\"$(FLAVOR)\"" \
-g -Os -Wall -Wextra -Wcast-align -Wformat=2 -Wno-main \
-fomit-frame-pointer -ffast-math -std=gnu11 -MMD -MP \
-Wno-unused-function -Wno-format-truncation $(INCLUDE) -ffunction-sections -fdata-sections
export LDFLAGS := -Tlink.ld -nostartfiles -Wl,--gc-sections,-z,max-page-size=512
ELF := arm9/arm9.elf arm11/arm11.elf
-Wno-unused-function -Wno-format-truncation -Wno-format-nonliteral $(INCLUDE) -ffunction-sections -fdata-sections
export LDFLAGS := -Tlink.ld -nostartfiles -Wl,--gc-sections,-z,max-page-size=4096
ELF := arm9/arm9_code.elf arm9/arm9_data.elf arm11/arm11.elf
.PHONY: all firm vram0 elf release clean
.PHONY: all firm $(VRAM_TAR) elf release clean
all: firm
clean:
@set -e; for elf in $(ELF); do \
$(MAKE) --no-print-directory -C $$(dirname $$elf) clean; \
done
@rm -rf $(OUTDIR) $(RELDIR) $(FIRM) $(FIRMD) $(VRAM_OUT)
@rm -rf $(OUTDIR) $(RELDIR) $(FIRM) $(FIRMD) $(VRAM_TAR) $(LANGUAGE_INL) $(TRF_FILES)
unmarked_readme: .FORCE
@$(PY3) utils/unmark.py -f README.md data/README_internal.md
@ -68,29 +85,44 @@ release: clean unmarked_readme
@cp $(OUTDIR)/$(FLAVOR)_dev.firm.sha $(RELDIR)/
@cp $(ELF) $(RELDIR)/elf
@cp $(CURDIR)/README.md $(RELDIR)
@cp $(CURDIR)/resources/lua-doc.md $(RELDIR)/lua-doc.md
@cp -R $(CURDIR)/resources/gm9 $(RELDIR)/gm9
@cp -R $(CURDIR)/resources/sample $(RELDIR)/sample
@-7za a $(RELDIR)/$(FLAVOR)-$(VERSION)-$(DBUILTS).zip ./$(RELDIR)/*
vram0:
@mkdir -p "$(OUTDIR)"
@echo "Creating $(VRAM_OUT)"
@$(PY3) utils/add2tar.py $(VRAM_FLAGS) $(VRAM_OUT) $(shell ls -d $(SPLASH) $(OVERRIDE_FONT) $(VRAM_DATA)/*)
$(VRAM_TAR): $(SPLASH) $(OVERRIDE_FONT) $(VRAM_DATA) $(VRAM_SCRIPTS)
@mkdir -p "$(@D)"
@echo "Creating $@"
@$(PY3) utils/add2tar.py $(VRAM_FLAGS) $(VRAM_TAR) $(shell ls -d -1 $^)
$(LANGUAGE_INL): $(SOURCE_JSON)
@echo "Creating $@"
@$(PY3) utils/transcp.py $< $@
$(TRF_FOLDER)/%.trf: $(JSON_FOLDER)/%.json
@$(PY3) utils/transriff.py $< $@
%.elf: .FORCE
@echo "Building $@"
@$(MAKE) --no-print-directory -C $(@D)
@$(MAKE) --no-print-directory -C $(@D) $(@F)
firm: $(ELF) vram0
@test `wc -c <$(VRAM_OUT)` -le 3145728
# Indicate a few explicit dependencies:
# The ARM9 data section depends on the VRAM drive
arm9/arm9_data.elf: $(VRAM_TAR) $(LANGUAGE_INL)
# And the code section depends on the data section being built already
arm9/arm9_code.elf: arm9/arm9_data.elf
firm: $(ELF) $(TRF_FILES)
@mkdir -p $(call dirname,"$(FIRM)") $(call dirname,"$(FIRMD)")
@echo "[FLAVOR] $(FLAVOR)"
@echo "[VERSION] $(VERSION)"
@echo "[BUILD] $(DBUILTL)"
@echo "[FIRM] $(FIRM)"
@$(PY3) -m firmtool build $(FIRM) $(FTFLAGS) -g -A 0x18000000 -D $(ELF) $(VRAM_OUT) -C NDMA XDMA memcpy
@$(PY3) -m firmtool build $(FIRM) $(FTFLAGS) -g -D $(ELF) -C NDMA NDMA XDMA
@echo "[FIRM] $(FIRMD)"
@$(PY3) -m firmtool build $(FIRMD) $(FTDFLAGS) -g -A 0x18000000 -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:

View File

@ -1,4 +1,6 @@
LIBS ?=
OBJECTS := $(patsubst $(SOURCE)/%.s, $(BUILD)/%.o, \
$(patsubst $(SOURCE)/%.c, $(BUILD)/%.o, \
$(call rwildcard, $(SOURCE), *.s *.c)))
@ -11,11 +13,12 @@ all: $(TARGET).elf
.PHONY: clean
clean:
@rm -rf $(BUILD) $(TARGET).elf $(TARGET).map
@rm -rf $(BUILD) $(TARGET).elf $(TARGET).dis $(TARGET).map
$(TARGET).elf: $(OBJECTS) $(OBJECTS_COMMON)
@mkdir -p "$(@D)"
@$(CC) $(LDFLAGS) $^ -o $@
@$(CC) $(LDFLAGS) $^ -o $@ $(LIBS)
@$(OBJDUMP) -S -h $@ > $@.dis
$(BUILD)/%.cmn.o: $(COMMON_DIR)/%.c
@mkdir -p "$(@D)"

View File

@ -1,3 +1,4 @@
export OBJDUMP := arm-none-eabi-objdump
dirname = $(shell dirname $(1))
@ -25,6 +26,12 @@ else ifeq ($(FLAVOR),ZuishMode9)
CFLAGS += -DDEFAULT_FONT=\"font_zuish_8x8.pbm\"
endif
ifeq ($(LARGEDLC),1)
CFLAGS += -DTITLE_MAX_CONTENTS=1536
else
CFLAGS += -DTITLE_MAX_CONTENTS=1024
endif
ifeq ($(SALTMODE),1)
CFLAGS += -DSALTMODE
endif
@ -61,6 +68,10 @@ ifdef SD_TIMEOUT
CFLAGS += -DSD_TIMEOUT=$(SD_TIMEOUT)
endif
ifeq ($(NO_LUA),1)
CFLAGS += -DNO_LUA
endif
ifdef N_PANES
CFLAGS += -DN_PANES=$(N_PANES)
endif

View File

@ -39,9 +39,9 @@ GodMode9 is designed to be intuitive, buttons leading to the results you'd expec
## How to build this / developer info
Build `GodMode9.firm` via `make firm`. This requires [firmtool](https://github.com/TuxSH/firmtool), [Python 3.5+](https://www.python.org/downloads/) and [devkitARM](https://sourceforge.net/projects/devkitpro/) installed).
You may run `make release` to get a nice, release-ready package of all required files. To build __SafeMode9__ (a bricksafe variant of GodMode9, with limited write permissions) instead of GodMode9, compile with `make FLAVOR=SafeMode9`. To switch screens, compile with `make SWITCH_SCREENS=1`. For additional customization, you may choose the internal font by replacing `font_default.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.
@ -61,10 +61,14 @@ GodMode9 provides a write permissions system, which will protect you from accide
## Support files
For certain functionality, GodMode9 may need 'support files'. Support files should be placed into either `0:/gm9/support` or `1:/gm9/support`. Support files contain additional information that is required in decryption operations. A list of support files, and what they do, is found below. Please don't ask for support files - find them yourself.
* __`aeskeydb.bin`__: This should contain 0x25keyX, 0x18keyX and 0x1BkeyX to enable decryption of 7x / Secure3 / Secure4 encrypted NCCH files, 0x11key95 / 0x11key96 for FIRM decrypt support and 0x11keyOTP / 0x11keyIVOTP for 'secret' sector 0x96 crypto support. Entrypoints other than [boot9strap](https://github.com/SciresM/boot9strap) or [fastboot3ds](https://github.com/derrekr/fastboot3DS) may require a aeskeydb.bin file. 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!
* __`seeddb.bin`__: This file is required to decrypt and mount seed encrypted NCCHs and CIAs if the seed in question is not installed to your NAND. Note that your seeddb.bin must also contain the seed for the specific game you need to decrypt.
* __`encTitleKeys.bin`__ / __`decTitleKeys.bin`__: These files are optional and provide titlekeys, which are required to create updatable CIAs from NCCH / NCSD files. CIAs created without these files will still work, but won't be updatable from eShop.
* __`aeskeydb.bin`__: This should contain 0x25keyX, 0x18keyX and 0x1BkeyX to enable decryption of 7x / Secure3 / Secure4 encrypted NCCH files, 0x11key95 / 0x11key96 for FIRM decrypt support and 0x11keyOTP / 0x11keyIVOTP for 'secret' sector 0x96 crypto support. Entrypoints other than [boot9strap](https://github.com/SciresM/boot9strap) or [fastboot3ds](https://github.com/derrekr/fastboot3DS) may require a aeskeydb.bin file. This is now included in standard releases of GM9. No need to hunt down the file!
* __`seeddb.bin`__: This file is optional and required to decrypt and mount seed-encrypted NCCHs and CIAs (if the seed in question is not installed to your NAND). Note that your seeddb.bin must also contain the seed for the specific game you need to decrypt.
* __`encTitleKeys.bin`__ / __`decTitleKeys.bin`__: These files are optional and provide titlekeys, which are required to decrypt and install contents downloaded from CDN (for DSi and 3DS content).
### Fonts and translations
GodMode9 also supports custom fonts and translations as support files. These both use custom formats, fonts use FRF (Font RIFF) files which can be created using the `fontriff.py` Python script in the 'utils' folder. Translations use TRF (Translation RIFF) files from the `transriff.py` script. Examples of the inputs to these scripts can be found in the 'fonts' and 'languages' folders of the 'resources' folder respectively.
TRF files can be placed in `0:/gm9/languages` to show in the language menu accessible from the HOME menu and shown on first load. Official translations are provided from the community via the [GodMode9 Crowdin](https://crowdin.com/project/GodMode9). Languages can use a special font by having an FRF with the same name, for example `en.trf` and `en.frf`.
## Drives in GodMode9
GodMode9 provides access to system data via drives, a listing of what each drive contains and additional info follows below. Some of these drives are removable (such as drive `7:`), some will only turn up if they are available (drive `8:` and everything associated with EmuNAND, f.e.). Information on the 3DS console file system is also found on [3Dbrew.org](https://3dbrew.org/wiki/Flash_Filesystem).
@ -86,12 +90,22 @@ 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.
* __`G: GAME IMAGE`__: CIA/NCSD/NCCH/EXEFS/ROMFS/FIRM images can be accessed via this drive when mounted. This is read-only.
* __`K: AESKEYDB IMAGE`__: An `aeskeydb.bin` image can be mounted and accessed via this drive. The drive shows all keys inside the aeskeydb.bin. This is read-only.
* __`T: TICKET.DB IMAGE`__: 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).
* __`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.
## Digital preservation
GodMode9 is one of the most important tools for digital preservation of 3DS content data. Here's some stuff you should know:
* __Dumping game cartridges (size < 4GiB)__: Game cartridges turn up inside the `C:` drive (see above). For most carts all you need to do is copy the `.3DS` game image to some place of your choice. Game images dumped by GodMode9 contain no identifying info such as private headers or savegames. Private headers can be dumped in a separate image.
* __Dumping game cartridges (size = 4GiB)__: Everything written above applies here as well. However, the FAT32 file system (which is what the 3DS uses) is limited to _4GiB - 1byte_. Take note that the `.3DS` game image, as provided by GodMode9 actually misses the last byte in these cases. That byte is 0xFF and unused in all known cases. It is not required for playing the image. If you need to check, we also provide split files (`.000`, `.001)`, which contain all the data. If you need a valid checksum for the `.3DS` game image, append a 0xFF byte before checking.
* __Building CIAs (all types)__: You may convert compatible file types (game images, installed content, CDN content) to the CIA installable format using the A button menu. To get a list of installed content, press HOME, select `Title manager` and choose a drive. Take note that `standard` built CIAs are decrypted by default (decryption allows better compression by ZIP and 7Z). If you should need an encrypted CIA for some reason, apply the encryption to the CIA afterwards.
* __Building CIAs (legit type)__: Installed content can be built as `legit` or `standard` CIA. Legit CIAs preserve more of the original data and are thus recommended for preservation purposes. When building legit CIAs, GodMode9 keeps the original crypto and tries to find a genuine, signature-valid ticket. If it doesn't find one on your system, it will use a generic ticket instead. If it only finds a personalized one, it still offers to use a generic ticket. It is not recommended to use personalized tickets - only choose this if you know what you're doing.
* __Checking CIAs__: You may also check your CIA files with the builtin `CIA checker tool`. Legit CIAs with generic tickets are identified as `Universal Pirate Legit`, which is the recommended preservation format where `Universal Legit` is not available. Note: apart from system titles, `Universal Legit` is only available for a handful of preinstalled games from special edition 3DS consoles.
## What you can do with GodMode9
With the possibilites GodMode9 provides, not everything may be obvious at first glance. In short, __GodMode9 includes improved versions of basically everything that Decrypt9 has, and more__. Any kind of dumps and injections are handled via standard copy operations and more specific operations are found inside the A button menu. The A button menu also works for batch operations when multiple files are selected. For your convenience a (incomplete!) list of what GodMode9 can do follows below.
@ -100,7 +114,7 @@ With the possibilites GodMode9 provides, not everything may be obvious at first
* __Make screenshots__: Press R+L anywhere. Screenshots are stored in PNG format.
* __Use multiple panes__: Press R+left|right. This enables you to stay in one location in the first pane and open another in the second pane.
* __Search drives and folders__: Just press R+A on the drive / folder you want to search.
* __Compare and verify files__: Press the A button on the first file, select `Calculate SHA-256`. Do the same for the second file. If the two files are identical, you will get a message about them being identical. On the SDCARD drive (`0:`) you can also write a SHA file, so you can check for any modifications at a later point.
* __Compare and verify files__: Press the A button on the first file, select `Calculate SHA-256`. Do the same for the second file. If the two files are identical, you will get a message about them being identical. On the SDCARD drive (`0:`) you can also write an SHA file, so you can check for any modifications at a later point.
* __Hexview and hexedit any file__: Press the A button on a file and select `Show in Hexeditor`. A button again enables edit mode, hold the A button and press arrow buttons to edit bytes. You will get an additional confirmation prompt to take over changes. Take note that for certain files, write permissions can't be enabled.
* __View text files in a text viewer__: Press the A button on a file and select `Show in Textviewer` (only shows up for actual text files). You can enable wordwrapped mode via R+Y, and navigate around the file via R+X and the dpad.
* __Chainload FIRM payloads__: Press the A button on a FIRM file, select `FIRM options` -> `Boot FIRM`. Keep in mind you should not run FIRMs from dubious sources and that the write permissions system is no longer in place after booting a payload.
@ -108,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.
### 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 .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 from anywhere on your SD card__: You can run Lua scripts via the A button menu. For an overview of usable commands have a look into the documentation and sample scripts included in the release archive. *Don't run scripts from untrusted sources.*
* __Run Lua scripts via a neat menu__: Press the HOME button, select `More...` -> `Lua scripts...`. Any script you put into `0:/gm9/luascripts` (subdirs included) will be found here. Scripts ran via this method won't have the confirmation at the beginning either.
* __Run legacy .gm9 scripts__: The old format of .gm9 scripts is still available, but is deprecated and will see no further development.
### 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.
@ -119,16 +134,18 @@ 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.
### 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.
* __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.
* __List titles installed on your system__: Press HOME and select `Title manager`. This will also work via R+A for `CTRNAND` and `A:`/`B:` drives. This will list all titles installed in the selected location.
* __Install titles to your system__: Just press A on any file you want installed and select `Install game image` from the submenu. Works with NCCH / NCSD / CIA / DSiWare SRLs / 3DS CDN TMDs / DSi CDN TMDs / NUS TMDs.
* __(Batch) Uninstall titles from your system__: Most easily done via the HOME menu `Title manager`. Just select one or more titles and find the option inside the `Manage title...` submenu.
* __Build CIAs from NCCH / NCSD (.3DS) / SRL / TMD__: Press A on the file you want converted and the option will be shown. Installed contents are found most easily via the HOME menu `Title manager`. Where applicable, you will also be able to generate legit CIAs. Note: this works also from a file search and title listing.
* __Dump CXIs / NDS from TMD (installed contents)__: This works the same as building CIAs, but dumps decrypted CXIs or NDS rom dumps instead. Note: this works also from a file search and title listing.
* __Decrypt, encrypt and verify NCCH / NCSD / CIA / BOSS / FIRM images__: Options are found inside the A button menu. You will be able to decrypt/encrypt to the standard output directory or (where applicable) in place.
* __Decrypt content downloaded from CDN / NUS__: Press A on the file you want decrypted. For this to work, you need at least a TMD file (`encTitlekeys.bin` / `decTitlekeys.bin` also required, see _Support files_ below) or a CETK file. Either keep the names provided by CDN / NUS, or rename the downloaded content to `(anything).nus` or `(anything).cdn` and the CETK to `(anything).cetk`.
* __Batch mode for the above operations__: Just select multiple files of the same type via the L button, then press the A button on one of the selected files.
* __Access any file inside NCCH / NCSD / CIA / FIRM / NDS images__: Just mount the file via the A button menu and browse to the file you want. For CDN / NUS content, prior decryption is required for full access.
* __Rename your NCCH / NCSD / CIA / NDS / GBA files to proper names__: Find this feature inside the A button menu. Proper names include title id, game name, product code and region.
* __Trim NCCH / NCSD / NDS / 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.
* __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.
### NAND handling
* __Directly mount and access NAND dumps or standard FAT images__: Just press the A button on these files to get the option. You can only mount NAND dumps from the same console.
@ -145,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.
* __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 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.
### Support file handling
@ -156,6 +173,8 @@ With the possibilites GodMode9 provides, not everything may be obvious at first
## License
You may use this under the terms of the GNU General Public License GPL v2 or under the terms of any later revisions of the GPL. Refer to the provided `LICENSE.txt` file for further information.
## Contact info
You can chat directly with us via IRC @ #GodMode9 on [libera.chat](https://web.libera.chat/#GodMode9) or [Discord](https://discord.gg/BRcbvtFxX4)!
## Credits
This tool would not have been possible without the help of numerous people. Thanks go to (in no particular order)...
@ -165,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
* **SciresM** for helping me figure out RomFS and for boot9strap
* **SciresM**, **Myria**, **Normmatt**, **TuxSH** and **hedgeberg** for figuring out sighax and giving us access to bootrom
* **ihaveamac** for 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
* **dratini0** for savefile management, based on [TWLSaveTool](https://github.com/TuxSH/TWLSaveTool/)
* **Pk11** for unicode support and her ongoing work on GodMode9 translations and translation support
* **b1l1s** for helping me figure out A9LH compatibility
* **Gelex** and **AuroraWright** for helping me figure out various things
* **stuckpixel** for the new 6x10 font and help on various things
@ -176,12 +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
* **windows-server-2003** for the initial implementation of if-else-goto in .gm9 scripts
* **Kazuma77** for pushing forward scripting, for testing and for always useful advice
* **TurdPooCharger** for being one of the most meticulous software testers around
* **JaySea**, **YodaDaCoda**, **liomajor**, **Supster131**, **imanoob**, **Kasher_CS** and countless others from freenode #Cakey and the GBAtemp forums for testing, feedback and helpful hints
* **Shadowhand** for being awesome and [hosting my nightlies](https://d0k3.secretalgorithm.com/)
* **Plailect** for putting his trust in my tools and recommending this in [The Guide](https://3ds.guide/)
* **SirNapkin1334** for testing, bug reports and for hosting the 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
* **Project Nayuki** for [qrcodegen](https://github.com/nayuki/QR-Code-generator)
* **Amazingmax fonts** for the Amazdoom font
* **TakWolf** for [fusion-pixel-font](https://github.com/TakWolf/fusion-pixel-font) used for Chinese and Korean
* The fine folks on **the official GodMode9 IRC channel and Discord server**
* The fine folks on **freenode #Cakey**
* All **[3dbrew.org](https://www.3dbrew.org/wiki/Main_Page) editors**
* Everyone I possibly forgot, if you think you deserve to be mentioned, just contact me!
* Everyone I possibly forgot, if you think you deserve to be mentioned, just contact us!

View File

@ -1,17 +1,17 @@
PROCESSOR := ARM11
TARGET := $(shell basename $(CURDIR))
TARGET := $(shell basename "$(CURDIR)")
SOURCE := source
BUILD := build
SUBARCH := -D$(PROCESSOR) -marm -march=armv6k -mtune=mpcore -mfloat-abi=hard -mfpu=vfpv2 -mtp=soft
SUBARCH := -D$(PROCESSOR) -march=armv6k -mtune=mpcore -marm -mfloat-abi=hard -mfpu=vfpv2 -mtp=soft
INCDIRS := source
INCLUDE := $(foreach dir,$(INCDIRS),-I"$(shell pwd)/$(dir)")
ASFLAGS += $(SUBARCH) $(INCLUDE)
CFLAGS += $(SUBARCH) $(INCLUDE) -flto
LDFLAGS += $(SUBARCH) -Wl,-Map,$(TARGET).map -flto
LDFLAGS += $(SUBARCH) -Wl,--use-blx,-Map,$(TARGET).map -flto
include ../Makefile.common
include ../Makefile.build

View File

@ -4,28 +4,19 @@ ENTRY(__boot)
MEMORY
{
AXIWRAM (RWX) : ORIGIN = 0x1FF80000, LENGTH = 128K
AXIWRAM (RWX) : ORIGIN = 0x1FF80000, LENGTH = 96K
HIGHRAM (RWX) : ORIGIN = 0xFFFF0000, LENGTH = 4K
}
SECTIONS
{
.vector : ALIGN(4K)
{
__vector_pa = LOADADDR(.vector);
__vector_va = ABSOLUTE(.);
KEEP(*(.vector))
. = ALIGN(4K);
__vector_len = . - __vector_va;
} >HIGHRAM AT>AXIWRAM
.text : ALIGN(4K)
{
__text_pa = LOADADDR(.text);
__text_va = ABSOLUTE(.);
*(.text*)
. = ALIGN(4K);
__text_len = . - __text_va;
__text_va_end = .;
} >AXIWRAM
.data : ALIGN(4K)
@ -34,7 +25,7 @@ SECTIONS
__data_va = ABSOLUTE(.);
*(.data*)
. = ALIGN(4K);
__data_len = . - __data_va;
__data_va_end = .;
} >AXIWRAM
.rodata : ALIGN(4K)
@ -43,7 +34,16 @@ SECTIONS
__rodata_va = ABSOLUTE(.);
*(.rodata*)
. = ALIGN(4K);
__rodata_len = . - __rodata_va;
__rodata_va_end = .;
} >AXIWRAM
.shared (NOLOAD) : ALIGN(4K)
{
__shared_pa = LOADADDR(.shared);
__shared_va = ABSOLUTE(.);
*(.shared*)
. = ALIGN(4K);
__shared_va_end = .;
} >AXIWRAM
.bss (NOLOAD) : ALIGN(4K)
@ -52,6 +52,6 @@ SECTIONS
__bss_va = ABSOLUTE(.);
*(.bss*)
. = ALIGN(4K);
__bss_len = . - __bss_va;
__bss_va_end = .;
} >AXIWRAM
}

View File

@ -16,11 +16,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <types.h>
#include <common.h>
#include <arm.h>
#include <stdatomic.h>
#include "arm/gic.h"
#include "system/event.h"
/* Generic Interrupt Controller Registers */
#define REG_GIC(cpu, o, t) REG_ARM_PMR(0x200 + ((cpu) * 0x100) + (o), t)
@ -40,183 +44,257 @@
#define REG_DIC_CONTROL (*REG_DIC(0x00, u32))
#define REG_DIC_TYPE (*REG_DIC(0x04, u32))
#define REG_DIC_SETENABLE REG_DIC(0x100, u32)
#define REG_DIC_SETENABLE REG_DIC(0x100, u32) // 32 intcfg per reg
#define REG_DIC_CLRENABLE REG_DIC(0x180, u32)
#define REG_DIC_SETPENDING REG_DIC(0x200, u32)
#define REG_DIC_CLRPENDING REG_DIC(0x280, u32)
#define REG_DIC_PRIORITY REG_DIC(0x400, u8)
#define REG_DIC_TARGETPROC REG_DIC(0x800, u8)
#define REG_DIC_CFGREG REG_DIC(0xC00, u32)
#define REG_DIC_PRIORITY REG_DIC(0x400, u8) // 1 intcfg per reg (in upper 4 bits)
#define REG_DIC_TARGETCPU REG_DIC(0x800, u8) // 1 intcfg per reg
#define REG_DIC_CFGREG REG_DIC(0xC00, u32) // 16 intcfg per reg
#define REG_DIC_SOFTINT (*REG_DIC(0xF00, u32))
// used only on reset routines
#define REG_DIC_PRIORITY32 REG_DIC(0x400, u32) // 4 intcfg per reg (in upper 4 bits)
#define REG_DIC_TARGETCPU32 REG_DIC(0x800, u32) // 4 intcfg per reg
#define GIC_PRIO_NEVER32 \
(GIC_PRIO_NEVER | (GIC_PRIO_NEVER << 8) | \
(GIC_PRIO_NEVER << 16) | (GIC_PRIO_NEVER << 24))
#define GIC_PRIO_HIGH32 \
(GIC_PRIO_HIGHEST | (GIC_PRIO_HIGHEST << 8) | \
(GIC_PRIO_HIGHEST << 16) | (GIC_PRIO_HIGHEST << 24))
/* CPU source ID is present in Interrupt Acknowledge register? */
#define IRQN_SRC_MASK (0x7 << 10)
/* Interrupt Handling */
#define LOCAL_IRQS (32)
#define DIC_MAX_IRQ (LOCAL_IRQS + MAX_IRQ)
#define IRQN_IS_LOCAL(n) ((n) < LOCAL_IRQS)
#define COREMASK_VALID(x) (((x) > 0) && ((x) < BIT(MAX_CPU)))
#define IRQN_IS_VALID(n) ((n) < DIC_MAX_IRQ)
#define LOCAL_IRQ_OFF(c, n) (((c) * LOCAL_IRQS) + (n))
#define GLOBAL_IRQ_OFF(n) (((MAX_CPU-1) * LOCAL_IRQS) + (n))
#define IRQ_TABLE_OFF(c, n) \
(IRQN_IS_LOCAL(n) ? LOCAL_IRQ_OFF((c), (n)) : GLOBAL_IRQ_OFF(n))
static gicIrqHandler gicIrqHandlers[DIC_MAX_IRQ];
static _Atomic(u32) gicIrqPending[DIC_MAX_IRQ / 32];
static IRQ_Handler IRQ_Handlers[IRQ_TABLE_OFF(0, MAX_IRQ)];
static struct {
u8 tgt;
u8 prio;
} gicIrqConfig[DIC_MAX_IRQ];
static IRQ_Handler GIC_GetCB(u32 irqn)
{
irqn &= ~(15 << 10); // clear source CPU bits
if (IRQN_IS_VALID(irqn)) {
return IRQ_Handlers[IRQ_TABLE_OFF(ARM_CoreID(), irqn)];
} else {
// Possibly have a dummy handler function that
// somehow notifies of an unhandled interrupt?
return NULL;
// gets used whenever a NULL pointer is passed to gicEnableInterrupt
static void gicDummyHandler(u32 irqn) { (void)irqn; return; }
static const struct {
u8 low, high, mode;
} gicDefaultIrqCfg[] = {
{ .low = 0x00, .high = 0x1F, .mode = GIC_RISINGEDGE_NN },
{ .low = 0x20, .high = 0x23, .mode = GIC_LEVELHIGH_1N },
{ .low = 0x24, .high = 0x24, .mode = GIC_RISINGEDGE_1N },
{ .low = 0x25, .high = 0x27, .mode = GIC_LEVELHIGH_1N },
{ .low = 0x28, .high = 0x2D, .mode = GIC_RISINGEDGE_1N },
{ .low = 0x30, .high = 0x3B, .mode = GIC_LEVELHIGH_1N },
{ .low = 0x40, .high = 0x4E, .mode = GIC_RISINGEDGE_1N },
{ .low = 0x4F, .high = 0x4F, .mode = GIC_LEVELHIGH_1N },
{ .low = 0x50, .high = 0x57, .mode = GIC_RISINGEDGE_1N },
{ .low = 0x58, .high = 0x58, .mode = GIC_LEVELHIGH_1N },
{ .low = 0x59, .high = 0x75, .mode = GIC_RISINGEDGE_1N },
{ .low = 0x76, .high = 0x77, .mode = GIC_LEVELHIGH_1N },
{ .low = 0x78, .high = 0x78, .mode = GIC_RISINGEDGE_1N },
{ .low = 0x79, .high = 0x7d, .mode = GIC_LEVELHIGH_1N },
};
static u8 gicGetDefaultIrqCfg(u32 irqn) {
for (unsigned i = 0; i < countof(gicDefaultIrqCfg); i++) {
if ((irqn >= gicDefaultIrqCfg[i].low) && (irqn <= gicDefaultIrqCfg[i].high))
return gicDefaultIrqCfg[i].mode;
}
// TODO: would it be considerably faster to use bsearch?
return GIC_RISINGEDGE_1N;
}
static void GIC_SetCB(u32 irqn, u32 cpu, IRQ_Handler handler)
{
if (IRQN_IS_VALID(irqn)) {
IRQ_Handlers[IRQ_TABLE_OFF(cpu, irqn)] = handler;
}
}
static void GIC_ClearCB(u32 irqn, u32 cpu)
{
GIC_SetCB(irqn, cpu, NULL);
}
void GIC_MainHandler(void)
void gicTopHandler(void)
{
while(1) {
IRQ_Handler handler;
u32 irqn = REG_GIC_IRQACK(GIC_THIS_CPU_ALIAS);
if (irqn == GIC_IRQ_SPURIOUS)
u32 irqn, irqsource, index, mask;
/**
If more than one of these CPUs reads the Interrupt Acknowledge Register at the
same time, they can all acknowledge the same interrupt. The interrupt service
routine must ensure that only one of them tries to process the interrupt, with the
others returning after writing the ID to the End of Interrupt Register.
*/
irqsource = REG_GIC_IRQACK(GIC_THIS_CPU_ALIAS);
if (irqsource == GIC_IRQ_SPURIOUS) // no further processing is needed
break;
handler = GIC_GetCB(irqn);
if (handler != NULL)
handler(irqn);
irqn = irqsource & ~IRQN_SRC_MASK;
REG_GIC_IRQEND(GIC_THIS_CPU_ALIAS) = irqn;
index = irqn / 32;
mask = BIT(irqn % 32);
atomic_fetch_or(&gicIrqPending[index], mask);
(gicIrqHandlers[irqn])(irqsource);
// if the id is < 16, the source CPU can be obtained from irqn
// if the handler isn't set, it'll try to branch to 0 and trigger a prefetch abort
REG_GIC_IRQEND(GIC_THIS_CPU_ALIAS) = irqsource;
}
}
void GIC_GlobalReset(void)
void gicGlobalReset(void)
{
int gicn, intn;
u32 dic_type;
unsigned gicn, intn;
// Number of local controllers
gicn = ((REG_DIC_TYPE >> 5) & 3) + 1;
dic_type = REG_DIC_TYPE;
// Number of interrupt lines (up to 224 external + 32 fixed internal)
intn = ((REG_DIC_TYPE & 7) + 1) << 5;
// number of local controllers
gicn = ((dic_type >> 5) & 3) + 1;
// number of interrupt lines (up to 224 external + 32 fixed internal per CPU)
intn = ((dic_type & 7) + 1) * 32;
// clamp it down to the amount of CPUs designed to handle
if (gicn > MAX_CPU)
gicn = MAX_CPU;
// Clear the interrupt table
for (unsigned int i = 0; i < sizeof(IRQ_Handlers)/sizeof(*IRQ_Handlers); i++)
IRQ_Handlers[i] = NULL;
// clear the interrupt handler and config table
getEventIRQ()->reset();
memset(gicIrqHandlers, 0, sizeof(gicIrqHandlers));
memset(gicIrqConfig, 0, sizeof(gicIrqConfig));
// Disable all MP11 GICs
for (int i = 0; i < gicn; i++)
// disable all MP11 GICs
for (unsigned i = 0; i < gicn; i++)
REG_GIC_CONTROL(i) = 0;
// Disable the main DIC
// disable the main DIC
REG_DIC_CONTROL = 0;
// Clear all DIC interrupts
for (int i = 1; i < (intn / 32); i++) {
// clear all external interrupts
for (unsigned i = 1; i < (intn / 32); i++) {
REG_DIC_CLRENABLE[i] = ~0;
REG_DIC_CLRPENDING[i] = ~0;
}
// Reset all DIC priorities to lowest and clear target processor regs
for (int i = 32; i < intn; i++) {
REG_DIC_PRIORITY[i] = 0;
REG_DIC_TARGETPROC[i] = 0;
// reset all external priorities to highest by default
// clear target processor regs
for (unsigned i = 4; i < (intn / 4); i++) {
REG_DIC_PRIORITY32[i] = GIC_PRIO_HIGH32;
REG_DIC_TARGETCPU32[i] = 0;
}
// Set all interrupts to rising edge triggered and 1-N model
for (int i = 2; i < (intn / 16); i++)
REG_DIC_CFGREG[i] = ~0;
// set all interrupts to active level triggered in N-N model
for (unsigned i = 16; i < (intn / 16); i++)
REG_DIC_CFGREG[i] = 0;
// Enable the main DIC
// re enable the main DIC
REG_DIC_CONTROL = 1;
for (int i = 0; i < gicn; i++) {
// Compare all priority bits
for (unsigned i = 0; i < gicn; i++) {
// compare all priority bits
REG_GIC_POI(i) = 3;
// Don't mask any interrupt with low priority
// don't mask any interrupt with low priority
REG_GIC_PRIOMASK(i) = 0xF0;
// Enable the MP11 GIC
// enable all the MP11 GICs
REG_GIC_CONTROL(i) = 1;
}
}
void GIC_LocalReset(void)
void gicLocalReset(void)
{
u32 irq_s;
// Clear out local interrupt configuration bits
// disable all local interrupts
REG_DIC_CLRENABLE[0] = ~0;
REG_DIC_CLRPENDING[0] = ~0;
for (int i = 0; i < 32; i++) {
REG_DIC_PRIORITY[i] = 0;
REG_DIC_TARGETPROC[i] = 0;
for (unsigned i = 0; i < 4; i++) {
REG_DIC_PRIORITY32[i] = GIC_PRIO_HIGH32;
// local IRQs are always unmasked by default
// REG_DIC_TARGETCPU[i] = 0;
// not needed, always read as corresponding MP11 core
}
for (int i = 0; i < 2; i++)
REG_DIC_CFGREG[i] = ~0;
// Acknowledge until it gets a spurious IRQ
// ack until it gets a spurious IRQ
do {
irq_s = REG_GIC_PENDING(GIC_THIS_CPU_ALIAS);
REG_GIC_IRQEND(GIC_THIS_CPU_ALIAS) = irq_s;
} while(irq_s != GIC_IRQ_SPURIOUS);
}
int GIC_Enable(u32 irqn, u32 coremask, u32 prio, IRQ_Handler handler)
static void gicSetIrqCfg(u32 irqn) {
u32 smt, cfg;
smt = irqn & 15;
cfg = REG_DIC_CFGREG[irqn / 16];
cfg &= ~(3 << smt);
cfg |= gicGetDefaultIrqCfg(irqn) << smt;
REG_DIC_CFGREG[irqn / 16] = cfg;
}
void gicSetInterruptConfig(u32 irqn, u32 coremask, u32 prio, gicIrqHandler handler)
{
if (!IRQN_IS_VALID(irqn))
return -1;
if (handler == NULL) // maybe add runtime ptr checks here too?
handler = gicDummyHandler;
// in theory this should be replaced by a faster CLZ lookup
// in practice, meh, MAX_CPU will only be 2 anyway...
for (int i = 0; i < MAX_CPU; i++) {
if (coremask & BIT(i))
GIC_SetCB(irqn, i, handler);
gicIrqConfig[irqn].tgt = coremask;
gicIrqConfig[irqn].prio = prio;
gicIrqHandlers[irqn] = handler;
}
REG_DIC_CLRPENDING[irqn >> 5] |= BIT(irqn & 0x1F);
REG_DIC_SETENABLE[irqn >> 5] |= BIT(irqn & 0x1F);
REG_DIC_PRIORITY[irqn] = prio << 4;
REG_DIC_TARGETPROC[irqn] = coremask;
return 0;
}
int GIC_Disable(u32 irqn, u32 coremask)
void gicClearInterruptConfig(u32 irqn)
{
if (irqn >= MAX_IRQ)
return -1;
for (int i = 0; i < MAX_CPU; i++) {
if (coremask & BIT(i))
GIC_ClearCB(irqn, i);
memset(&gicIrqConfig[irqn], 0, sizeof(gicIrqConfig[irqn]));
gicIrqHandlers[irqn] = NULL;
}
REG_DIC_CLRPENDING[irqn >> 5] |= BIT(irqn & 0x1F);
REG_DIC_CLRENABLE[irqn >> 5] |= BIT(irqn & 0x1F);
REG_DIC_TARGETPROC[irqn] = 0;
return 0;
}
void GIC_TriggerSoftIRQ(u32 irqn, u32 mode, u32 coremask)
void gicEnableInterrupt(u32 irqn)
{
REG_DIC_SOFTINT = (mode << 24) | (coremask << 16) | irqn;
REG_DIC_PRIORITY[irqn] = gicIrqConfig[irqn].prio;
REG_DIC_TARGETCPU[irqn] = gicIrqConfig[irqn].tgt;
gicSetIrqCfg(irqn);
REG_DIC_CLRPENDING[irqn / 32] |= BIT(irqn & 0x1F);
REG_DIC_SETENABLE[irqn / 32] |= BIT(irqn & 0x1F);
}
void gicDisableInterrupt(u32 irqn)
{
REG_DIC_CLRENABLE[irqn / 32] |= BIT(irqn & 0x1F);
REG_DIC_CLRPENDING[irqn / 32] |= BIT(irqn & 0x1F);
}
void gicTriggerSoftInterrupt(u32 softirq)
{
REG_DIC_SOFTINT = softirq;
}
static void irqEvReset(void) {
memset(&gicIrqPending, 0, sizeof(gicIrqPending));
}
static u32 irqEvTest(u32 param, u32 clear) {
u32 index, tstmask, clrmask;
if (param >= DIC_MAX_IRQ)
bkpt;
index = param / 32;
tstmask = BIT(param % 32);
clrmask = clear ? tstmask : 0;
return !!(atomic_fetch_and(&gicIrqPending[index], ~clrmask) & tstmask);
}
static const EventInterface evIRQ = {
.reset = irqEvReset,
.test = irqEvTest
};
const EventInterface *getEventIRQ(void) {
return &evIRQ;
}

View File

@ -1,6 +1,6 @@
/*
* This file is part of GodMode9
* Copyright (C) 2017-2019 Wolfvak
* Copyright (C) 2017-2020 Wolfvak
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -17,28 +17,96 @@
*/
#pragma once
#include <types.h>
typedef void (*IRQ_Handler)(u32 irqn);
#include <common.h>
#include <arm.h>
typedef void (*gicIrqHandler)(u32 irqn);
enum {
GIC_LEVELHIGH_NN = 0, // no interrupts use level high triggers so far
GIC_LEVELHIGH_1N = 1,
GIC_RISINGEDGE_NN = 2,
GIC_RISINGEDGE_1N = 3
// With the 1-N model, an interrupt that is taken on any CPU clears the Pending
// status on all CPUs.
// With the N-N model, all CPUs receive the interrupt independently. The Pending
// status is cleared only for the CPU that takes it, not for the other CPUs
};
enum {
GIC_PRIO0 = 0x00,
GIC_PRIO1 = 0x10,
GIC_PRIO2 = 0x20,
GIC_PRIO3 = 0x30,
GIC_PRIO4 = 0x40,
GIC_PRIO5 = 0x50,
GIC_PRIO6 = 0x60,
GIC_PRIO7 = 0x70,
GIC_PRIO14 = 0xE0,
GIC_PRIO15 = 0xF0,
};
#define GIC_PRIO_HIGHEST GIC_PRIO0
#define GIC_PRIO_LOWEST GIC_PRIO14
#define GIC_PRIO_NEVER GIC_PRIO15
void gicGlobalReset(void);
void gicLocalReset(void);
/*
Notes from https://static.docs.arm.com/ddi0360/f/DDI0360F_arm11_mpcore_r2p0_trm.pdf
INTERRUPT ENABLE:
Interrupts 0-15 fields are read as one, that is, always enabled, and write to these fields
have no effect.
Notpresent interrupts (depending on the Interrupt Controller Type Register and
interrupt number field) related fields are read as zero and writes to these fields have no
effect.
INTERRUPT PRIORITY:
The first four registers are aliased for each MP11 CPU, that is, the priority set for
ID0-15 and ID29-31 can be different for each MP11 CPU. The priority of IPIs ID0-15
depends on the receiving CPU, not the sending CPU.
INTERRUPT CPU TARGET:
For MP11 CPU n, CPU targets 29, 30 and 31 are read as (1 << n). Writes are ignored.
For IT0-IT28, these fields are read as zero and writes are ignored.
INTERRUPT CONFIGURATION:
For ID0-ID15, bit 1 of the configuration pair is always read as one, that is, rising edge
sensitive.
For ID0-ID15, bit 0 (software model) can be configured and applies to the interrupts
sent from the writing MP11 CPU.
For ID29, and ID30, the configuration pair is always read as b10, that is rising edge
sensitive and N-N software model because these IDs are allocated to timer and
watchdog interrupts that are CPU-specific
*/
#define COREMASK_ALL (BIT(MAX_CPU) - 1)
void gicSetInterruptConfig(u32 irqn, u32 coremask, u32 prio, gicIrqHandler handler);
void gicClearInterruptConfig(u32 irqn);
void gicEnableInterrupt(u32 irqn);
void gicDisableInterrupt(u32 irqn);
enum {
GIC_SOFTIRQ_LIST = 0,
GIC_SOFTIRQ_OTHERS = 1, // all except self
GIC_SOFTIRQ_SELF = 2,
};
#define GIC_SOFTIRQ_SOURCE(n) (((n) >> 10) & 0xF)
#define GIC_SOFTIRQ_NUMBER(n) ((n) & 0x3FF)
#define GIC_SOFTIRQ_ID(n) ((n) & 0x3FF)
enum {
GIC_SOFTIRQ_NORMAL = 0,
GIC_SOFTIRQ_NOTSELF = 1,
GIC_SOFTIRQ_SELF = 2
};
#define GIC_SOFTIRQ_FMT(id, filter, coremask) \
((id) | ((coremask) << 16) | ((filter) << 24))
// id & 0xf, coremask & 3, filter & 3
// coremask is only used with filter == GIC_SOFTIRQ_LIST
enum {
GIC_HIGHEST_PRIO = 0x0,
GIC_LOWEST_PRIO = 0xE,
};
#define GIC_SOFTIRQ_SRC(x) (((x) >> 10) % MAX_CPU)
void GIC_GlobalReset(void);
void GIC_LocalReset(void);
int GIC_Enable(u32 irqn, u32 coremask, u32 prio, IRQ_Handler handler);
int GIC_Disable(u32 irqn, u32 coremask);
void GIC_TriggerSoftIRQ(u32 irqn, u32 mode, u32 coremask);
void gicTriggerSoftInterrupt(u32 softirq);

View File

@ -54,7 +54,7 @@
#define DESCRIPTOR_TYPE_MASK (3)
enum DescriptorType {
enum {
L1_UNMAPPED,
L1_COARSE,
L1_SECTION,
@ -67,76 +67,61 @@ enum DescriptorType {
typedef struct {
u32 desc[4096];
} __attribute__((aligned(16384))) MMU_Lvl1_Table;
} __attribute__((aligned(16384))) mmuLevel1Table;
typedef struct {
u32 desc[256];
} __attribute__((aligned(1024))) MMU_Lvl2_Table;
} __attribute__((aligned(1024))) mmuLevel2Table;
static MMU_Lvl1_Table MMU_Lvl1_TT;
static mmuLevel1Table mmuGlobalTT;
/* function to allocate 2nd level page tables */
#define MAX_SECOND_LEVEL (4)
static MMU_Lvl2_Table Lvl2_Tables[MAX_SECOND_LEVEL];
static u32 Lvl2_Allocated = 0;
static MMU_Lvl2_Table *Alloc_Lvl2(void)
// simple watermark allocator for 2nd level page tables
#define MAX_SECOND_LEVEL (8)
static mmuLevel2Table mmuCoarseTables[MAX_SECOND_LEVEL];
static u32 mmuCoarseAllocated = 0;
static mmuLevel2Table *mmuAllocateLevel2Table(void)
{
if (Lvl2_Allocated == MAX_SECOND_LEVEL)
return NULL;
return &Lvl2_Tables[Lvl2_Allocated++];
return &mmuCoarseTables[mmuCoarseAllocated++];
}
/* functions to convert from internal page flag format to ARM */
// functions to convert from internal page flag format to ARM
/* {TEX, CB} */
static const u8 MMU_TypeLUT[MEMORY_TYPES][2] = {
[STRONGLY_ORDERED] = {0, 0},
[NON_CACHEABLE] = {1, 0},
[DEVICE_SHARED] = {0, 1},
[DEVICE_NONSHARED] = {2, 0},
[CACHED_WT] = {0, 2},
[CACHED_WB] = {1, 3},
[CACHED_WB_ALLOC] = {1, 3},
// {TEX, CB} pairs
static const u8 mmuTypeLUT[MMU_MEMORY_TYPES][2] = {
[MMU_STRONG_ORDER] = {0, 0},
[MMU_UNCACHEABLE] = {1, 0},
[MMU_DEV_SHARED] = {0, 1},
[MMU_DEV_NONSHARED] = {2, 0},
[MMU_CACHE_WT] = {0, 2},
[MMU_CACHE_WB] = {1, 3},
[MMU_CACHE_WBA] = {1, 3},
};
static u32 MMU_GetTEX(u32 f)
{
return MMU_TypeLUT[MMU_FLAGS_TYPE(f)][0];
}
static u32 mmuGetTEX(u32 f)
{ return mmuTypeLUT[MMU_FLAGS_TYPE(f)][0]; }
static u32 mmuGetCB(u32 f)
{ return mmuTypeLUT[MMU_FLAGS_TYPE(f)][1]; }
static u32 mmuGetNX(u32 f)
{ return MMU_FLAGS_NOEXEC(f) ? 1 : 0; }
static u32 mmuGetShared(u32 f)
{ return MMU_FLAGS_SHARED(f) ? 1 : 0; }
static u32 MMU_GetCB(u32 f)
{
return MMU_TypeLUT[MMU_FLAGS_TYPE(f)][1];
}
// access permissions
static const u8 mmuAccessLUT[MMU_ACCESS_TYPES] = {
[MMU_NO_ACCESS] = 0,
[MMU_READ_ONLY] = 0x21,
[MMU_READ_WRITE] = 0x01,
};
static u32 MMU_GetAP(u32 f)
{
switch(MMU_FLAGS_ACCESS(f)) {
default:
case NO_ACCESS:
return 0;
case READ_ONLY:
return 0x21;
case READ_WRITE:
return 0x01;
}
}
static u32 mmuGetAP(u32 f)
{ return mmuAccessLUT[MMU_FLAGS_ACCESS(f)]; }
static u32 MMU_GetNX(u32 f)
// other misc helper functions
static unsigned mmuWalkTT(u32 va)
{
return MMU_FLAGS_NOEXEC(f) ? 1 : 0;
}
static u32 MMU_GetShared(u32 f)
{
return MMU_FLAGS_SHARED(f) ? 1 : 0;
}
static enum DescriptorType MMU_WalkTT(u32 va)
{
MMU_Lvl2_Table *coarsepd;
u32 desc = MMU_Lvl1_TT.desc[L1_VA_IDX(va)];
mmuLevel2Table *coarsepd;
u32 desc = mmuGlobalTT.desc[L1_VA_IDX(va)];
switch(desc & DESCRIPTOR_TYPE_MASK) {
case DESCRIPTOR_L1_UNMAPPED:
@ -152,7 +137,7 @@ static enum DescriptorType MMU_WalkTT(u32 va)
return L1_RESERVED;
}
coarsepd = (MMU_Lvl2_Table*)(desc & COARSE_MASK);
coarsepd = (mmuLevel2Table*)(desc & COARSE_MASK);
desc = coarsepd->desc[L2_VA_IDX(va)];
switch(desc & DESCRIPTOR_TYPE_MASK) {
@ -169,21 +154,20 @@ static enum DescriptorType MMU_WalkTT(u32 va)
}
}
static MMU_Lvl2_Table *MMU_CoarseFix(u32 va)
static mmuLevel2Table *mmuCoarseFix(u32 va)
{
enum DescriptorType type;
MMU_Lvl2_Table *coarsepd;
u32 type;
mmuLevel2Table *coarsepd;
type = MMU_WalkTT(va);
type = mmuWalkTT(va);
switch(type) {
case L1_UNMAPPED:
coarsepd = Alloc_Lvl2();
if (coarsepd != NULL)
MMU_Lvl1_TT.desc[L1_VA_IDX(va)] = (u32)coarsepd | DESCRIPTOR_L1_COARSE;
coarsepd = mmuAllocateLevel2Table();
mmuGlobalTT.desc[L1_VA_IDX(va)] = (u32)coarsepd | DESCRIPTOR_L1_COARSE;
break;
case L2_UNMAPPED:
coarsepd = (MMU_Lvl2_Table*)(MMU_Lvl1_TT.desc[L1_VA_IDX(va)] & COARSE_MASK);
coarsepd = (mmuLevel2Table*)(mmuGlobalTT.desc[L1_VA_IDX(va)] & COARSE_MASK);
break;
default:
@ -196,122 +180,91 @@ static MMU_Lvl2_Table *MMU_CoarseFix(u32 va)
/* Sections */
static u32 MMU_SectionFlags(u32 f)
{
return (MMU_GetShared(f) << 16) | (MMU_GetTEX(f) << 12) |
(MMU_GetAP(f) << 10) | (MMU_GetNX(f) << 4) |
(MMU_GetCB(f) << 2) | DESCRIPTOR_L1_SECTION;
static u32 mmuSectionFlags(u32 f)
{ // converts the internal format to the hardware L1 section format
return (mmuGetShared(f) << 16) | (mmuGetTEX(f) << 12) |
(mmuGetAP(f) << 10) | (mmuGetNX(f) << 4) |
(mmuGetCB(f) << 2) | DESCRIPTOR_L1_SECTION;
}
static bool MMU_MapSection(u32 va, u32 pa, u32 flags)
static void mmuMapSection(u32 va, u32 pa, u32 flags)
{
enum DescriptorType type = MMU_WalkTT(va);
if (type == L1_UNMAPPED) {
MMU_Lvl1_TT.desc[L1_VA_IDX(va)] = pa | MMU_SectionFlags(flags);
return true;
}
return false;
}
/* Large Pages */
static u32 MMU_LargePageFlags(u32 f)
{
return (MMU_GetNX(f) << 15) | (MMU_GetTEX(f) << 12) |
(MMU_GetShared(f) << 10) | (MMU_GetAP(f) << 4) |
(MMU_GetCB(f) << 2) | DESCRIPTOR_L2_LARGEPAGE;
}
static bool MMU_MapLargePage(u32 va, u32 pa, u32 flags)
{
MMU_Lvl2_Table *l2 = MMU_CoarseFix(va);
if (l2 == NULL)
return false;
for (u32 i = va; i < (va + 0x10000); i += 0x1000)
l2->desc[L2_VA_IDX(i)] = pa | MMU_LargePageFlags(flags);
return true;
mmuGlobalTT.desc[L1_VA_IDX(va)] = pa | mmuSectionFlags(flags);
}
/* Pages */
static u32 MMU_PageFlags(u32 f)
static u32 mmuPageFlags(u32 f)
{
return (MMU_GetShared(f) << 10) | (MMU_GetTEX(f) << 6) |
(MMU_GetAP(f) << 4) | (MMU_GetCB(f) << 2) |
(MMU_GetNX(f) ? DESCRIPTOR_L2_PAGE_NX : DESCRIPTOR_L2_PAGE_EXEC);
return (mmuGetShared(f) << 10) | (mmuGetTEX(f) << 6) |
(mmuGetAP(f) << 4) | (mmuGetCB(f) << 2) |
(mmuGetNX(f) ? DESCRIPTOR_L2_PAGE_NX : DESCRIPTOR_L2_PAGE_EXEC);
}
static bool MMU_MapPage(u32 va, u32 pa, u32 flags)
static void mmuMapPage(u32 va, u32 pa, u32 flags)
{
MMU_Lvl2_Table *l2 = MMU_CoarseFix(va);
if (l2 == NULL)
return false;
l2->desc[L2_VA_IDX(va)] = pa | MMU_PageFlags(flags);
return true;
mmuLevel2Table *l2 = mmuCoarseFix(va);
l2->desc[L2_VA_IDX(va)] = pa | mmuPageFlags(flags);
}
static bool MMU_MappingFits(u32 va, u32 pa, u32 len, u32 abits)
static bool mmuMappingFits(u32 va, u32 pa, u32 sz, u32 alignment)
{
return !((va | pa | len) & (BIT(abits) - 1));
return !((va | pa | sz) & (alignment));
}
u32 MMU_Map(u32 va, u32 pa, u32 size, u32 flags)
u32 mmuMapArea(u32 va, u32 pa, u32 size, u32 flags)
{
static const struct {
u32 bits;
bool (*mapfn)(u32,u32,u32);
u32 size;
void (*mapfn)(u32,u32,u32);
} VMappers[] = {
{
.bits = SECT_ADDR_SHIFT,
.mapfn = MMU_MapSection,
.size = BIT(SECT_ADDR_SHIFT),
.mapfn = mmuMapSection,
},
{
.bits = LPAGE_ADDR_SHIFT,
.mapfn = MMU_MapLargePage,
},
{
.bits = PAGE_ADDR_SHIFT,
.mapfn = MMU_MapPage,
.size = BIT(PAGE_ADDR_SHIFT),
.mapfn = mmuMapPage,
},
};
while(size > 0) {
size_t i = 0;
for (i = 0; i < countof(VMappers); i++) {
u32 abits = VMappers[i].bits;
u32 pgsize = VMappers[i].size;
if (MMU_MappingFits(va, pa, size, abits)) {
bool mapped = (VMappers[i].mapfn)(va, pa, flags);
u32 offset = BIT(abits);
if (mmuMappingFits(va, pa, size, pgsize-1)) {
(VMappers[i].mapfn)(va, pa, flags);
// no fun allowed
if (!mapped)
return size;
va += offset;
pa += offset;
size -= offset;
va += pgsize;
pa += pgsize;
size -= pgsize;
break;
}
}
/* alternatively return the unmapped remaining size:
if (i == countof(VMappers))
return size;
*/
}
return 0;
}
void MMU_Init(void)
void mmuInvalidate(void)
{
u32 ttbr0 = (u32)(&MMU_Lvl1_TT) | 0x12;
ARM_MCR(p15, 0, 0, c8, c7, 0);
}
void mmuInvalidateVA(u32 addr)
{
ARM_MCR(p15, 0, addr, c8, c7, 2);
}
void mmuInitRegisters(void)
{
u32 ttbr0 = (u32)(&mmuGlobalTT) | 0x12;
// Set up TTBR0/1 and the TTCR
ARM_MCR(p15, 0, ttbr0, c2, c0, 0);

View File

@ -20,21 +20,22 @@
#include <types.h>
enum MMU_MemoryType {
STRONGLY_ORDERED = 0,
NON_CACHEABLE,
DEVICE_SHARED,
DEVICE_NONSHARED,
CACHED_WT,
CACHED_WB,
CACHED_WB_ALLOC,
MEMORY_TYPES,
enum {
MMU_STRONG_ORDER = 0,
MMU_UNCACHEABLE,
MMU_DEV_SHARED,
MMU_DEV_NONSHARED,
MMU_CACHE_WT,
MMU_CACHE_WB,
MMU_CACHE_WBA,
MMU_MEMORY_TYPES,
};
enum MMU_MemoryAccess {
NO_ACCESS = 0,
READ_ONLY,
READ_WRITE,
enum {
MMU_NO_ACCESS = 0,
MMU_READ_ONLY,
MMU_READ_WRITE,
MMU_ACCESS_TYPES,
};
#define MMU_FLAGS(t, ap, nx, s) ((s) << 25 | (nx) << 24 | (ap) << 8 | (t))
@ -45,5 +46,9 @@ enum MMU_MemoryAccess {
#define MMU_FLAGS_NOEXEC(f) ((f) & BIT(24))
#define MMU_FLAGS_SHARED(f) ((f) & BIT(25))
u32 MMU_Map(u32 va, u32 pa, u32 size, u32 flags);
void MMU_Init(void);
u32 mmuMapArea(u32 va, u32 pa, u32 size, u32 flags);
void mmuInvalidate(void);
void mmuInvalidateVA(u32 addr); // DO NOT USE
void mmuInitRegisters(void);

View File

@ -30,3 +30,7 @@
#define CLK_MS_TO_TICKS(m) (((BASE_CLKRATE / 1000) * (m)) - 1)
void TIMER_WaitTicks(u32 ticks);
static inline void TIMER_WaitMS(u32 ms) {
TIMER_WaitTicks(CLK_MS_TO_TICKS(ms));
}

View File

@ -16,6 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// kinda hardcoded and all over the place, but it needs to stay simple
#include <types.h>
#include <arm.h>

View File

@ -1,6 +1,6 @@
/*
* This file is part of GodMode9
* Copyright (C) 2019 Wolfvak
* Copyright (C) 2020 Wolfvak
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -18,8 +18,4 @@
#pragma once
#include "types.h"
#define XALLOC_BUF_SIZE (16384)
void *XAlloc(size_t size);
u32 xrqInstallVectorTable(void);

View File

@ -28,47 +28,46 @@
.macro TRAP_ENTRY xrq
msr cpsr_f, #(\xrq << 29)
b XRQ_Main
b xrqMain
.endm
.section .vector, "ax"
vectors:
b XRQ_Reset
b XRQ_Undefined
b XRQ_SVC
b XRQ_PrefetchAbt
b XRQ_DataAbt
b XRQ_Reserved
b XRQ_IRQ
b XRQ_FIQ
xrqVectorTable:
ldr pc, =xrqReset
ldr pc, =xrqUndefined
ldr pc, =xrqSVC
ldr pc, =xrqPrefetchAbort
ldr pc, =xrqDataAbort
b . @ ignore the reserved exception
ldr pc, =xrqIRQ
ldr pc, =xrqFIQ
.pool
xrqVectorTableEnd:
XRQ_Reset:
xrqReset:
TRAP_ENTRY 0
XRQ_Undefined:
xrqUndefined:
TRAP_ENTRY 1
XRQ_SVC:
xrqSVC:
TRAP_ENTRY 2
XRQ_PrefetchAbt:
xrqPrefetchAbort:
TRAP_ENTRY 3
XRQ_DataAbt:
xrqDataAbort:
TRAP_ENTRY 4
XRQ_Reserved:
TRAP_ENTRY 5
XRQ_FIQ:
xrqFIQ:
TRAP_ENTRY 7
XRQ_Main:
ldr sp, =(exception_stack_top - 32*4)
stmia sp, {r0-r7}
xrqMain:
clrex
cpsid aif
ldr sp, =(xrqStackTop - 32*4)
stmia sp, {r0-r7}
mrs r1, cpsr
lsr r0, r1, #29
@ -82,11 +81,7 @@ XRQ_Main:
add r3, sp, #8*4
msr cpsr_c, r2
nop
nop
stmia r3!, {r8-r14}
nop
nop
msr cpsr_c, r1
mrc p15, 0, r4, c5, c0, 0 @ data fault status register
@ -99,7 +94,8 @@ XRQ_Main:
bl do_exception
XRQ_IRQ:
xrqIRQ:
clrex
sub lr, lr, #4 @ Fix return address
srsfd sp!, #SR_SVC_MODE @ Store IRQ mode LR and SPSR on the SVC stack
cps #SR_SVC_MODE @ Switch to SVC mode
@ -108,17 +104,26 @@ XRQ_IRQ:
and r4, sp, #7 @ Fix SP to be 8byte aligned
sub sp, sp, r4
mov lr, pc
ldr pc, =GIC_MainHandler
bl gicTopHandler
add sp, sp, r4
pop {r0-r4, r12, lr}
rfeia sp! @ Return from exception
.section .bss.xrq_stk
@ u32 xrqInstallVectorTable(void)
.global xrqInstallVectorTable
.type xrqInstallVectorTable, %function
xrqInstallVectorTable:
ldr r0, =xrqPage
ldr r1, =xrqVectorTable
mov r2, #(xrqVectorTableEnd - xrqVectorTable)
b memcpy
.section .bss.xrqPage
.align 12
exception_stack: @ reserve a single aligned page for the exception stack
.space 4096
exception_stack_top:
.global exception_stack_top
.global xrqPage
xrqPage:
.space 8192 @ reserve two 4K aligned pages for vectors and abort stack
.global xrqStackTop
xrqStackTop:

View File

@ -76,10 +76,12 @@ __boot:
b 1b
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 r1, =__bss_len
add r1, r0, r1
ldr r1, =__bss_va_end @ calculate the length of .bss using the VA start and end
ldr r2, =__bss_va
sub r1, r1, r2
add r1, r0, r1 @ fixup to be PA start and end
mov r2, #0
mov r3, #0
mov r4, #0

View File

@ -23,7 +23,7 @@
typedef struct {
s16 cpad_x, cpad_y;
s16 ts_x, ts_y;
u16 ts_x, ts_y;
} CODEC_Input;
void CODEC_Init(void);

View File

@ -16,216 +16,289 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <common.h>
#include <types.h>
#include <vram.h>
#include <arm.h>
#include "arm/timer.h"
#include "hw/i2c.h"
#include "hw/mcu.h"
#include "hw/gpulcd.h"
/* LCD Configuration Registers */
#define REG_LCD(x) ((vu32*)(0x10202000 + (x)))
void LCD_SetBrightness(u8 brightness)
#include "system/event.h"
static struct
{
*REG_LCD(0x240) = brightness;
*REG_LCD(0xA40) = brightness;
u16 lcdIds; // Bits 0-7 top screen, 8-15 bottom screen.
bool lcdIdsRead;
u8 lcdPower; // 1 = on. Bit 4 top light, bit 2 bottom light, bit 0 LCDs.
u8 lcdLights[2]; // LCD backlight brightness. Top, bottom.
u32 framebufs[2]; // For each screen
u8 doubleBuf[2]; // Top, bottom, 1 = enable.
u16 strides[2]; // Top, bottom
u32 formats[2]; // Top, bottom
} g_gfxState = {0};
static void setupDisplayController(u8 lcd);
static void resetLcdsMaybe(void);
static void waitLcdsReady(void);
static u32 gxModeWidth(unsigned c) {
switch(c) {
case 0: return 4;
case 1: return 3;
default: return 2;
}
}
u8 LCD_GetBrightness(void)
unsigned GFX_init(GfxFbFmt mode)
{
return *REG_LCD(0x240);
unsigned err = 0;
REG_CFG11_GPUPROT = 0;
// Reset
REG_PDN_GPU_CNT = PDN_GPU_CNT_CLK_E;
ARM_WaitCycles(12);
REG_PDN_GPU_CNT = PDN_GPU_CNT_CLK_E | PDN_GPU_CNT_RST_ALL;
REG_GX_GPU_CLK = 0x100;
REG_GX_PSC_VRAM = 0;
REG_GX_PSC_FILL0_CNT = 0;
REG_GX_PSC_FILL1_CNT = 0;
REG_GX_PPF_CNT = 0;
// LCD framebuffer setup.
g_gfxState.strides[0] = 240 * gxModeWidth(mode);
g_gfxState.strides[1] = 240 * gxModeWidth(mode);
g_gfxState.framebufs[0] = VRAM_TOP_LA;
g_gfxState.framebufs[1] = VRAM_BOT_A;
g_gfxState.formats[0] = mode | BIT(6) | BIT(9);
g_gfxState.formats[1] = mode | BIT(9);
setupDisplayController(0);
setupDisplayController(1);
REG_LCD_PDC0_SWAP = 0; // Select framebuf 0.
REG_LCD_PDC1_SWAP = 0;
REG_LCD_PDC0_CNT = PDC_CNT_OUT_E | PDC_CNT_I_MASK_ERR | PDC_CNT_I_MASK_H | PDC_CNT_E; // Start
REG_LCD_PDC1_CNT = PDC_CNT_OUT_E | PDC_CNT_I_MASK_ERR | PDC_CNT_I_MASK_H | PDC_CNT_E;
// LCD reg setup.
REG_LCD_ABL0_FILL = 1u<<24; // Force blackscreen
REG_LCD_ABL1_FILL = 1u<<24; // Force blackscreen
REG_LCD_PARALLAX_CNT = 0;
REG_LCD_PARALLAX_PWM = 0xA390A39;
REG_LCD_RST = 0;
REG_LCD_UNK00C = 0x10001;
// Clear used VRAM
REG_GX_PSC_FILL0_S_ADDR = VRAM_TOP_LA >> 3;
REG_GX_PSC_FILL0_E_ADDR = VRAM_END >> 3;
REG_GX_PSC_FILL0_VAL = 0;
REG_GX_PSC_FILL0_CNT = BIT(9) | BIT(0);
// Backlight and other stuff.
REG_LCD_ABL0_LIGHT = 0;
REG_LCD_ABL0_CNT = 0;
REG_LCD_ABL0_LIGHT_PWM = 0;
REG_LCD_ABL1_LIGHT = 0;
REG_LCD_ABL1_CNT = 0;
REG_LCD_ABL1_LIGHT_PWM = 0;
REG_LCD_RST = 1;
REG_LCD_UNK00C = 0;
TIMER_WaitMS(10);
resetLcdsMaybe();
MCU_controlLCDPower(2u); // Power on LCDs.
if(eventWait(getEventMCU(), 0x3Fu<<24, 0x3Fu<<24) != 2u<<24) __builtin_trap();
waitLcdsReady();
REG_LCD_ABL0_LIGHT_PWM = 0x1023E;
REG_LCD_ABL1_LIGHT_PWM = 0x1023E;
MCU_controlLCDPower(0x28u); // Power on backlights.
if(eventWait(getEventMCU(), 0x3Fu<<24, 0x3Fu<<24) != 0x28u<<24) __builtin_trap();
g_gfxState.lcdPower = 0x15; // All on.
// Make sure the fills finished.
REG_LCD_ABL0_FILL = 0;
REG_LCD_ABL1_FILL = 0;
// GPU stuff.
REG_GX_GPU_CLK = 0x70100;
*((vu32*)0x10400050) = 0x22221200;
*((vu32*)0x10400054) = 0xFF2;
GFX_setBrightness(0x80, 0x80);
return err;
}
void LCD_Initialize(u8 brightness)
static u16 getLcdIds(void)
{
*REG_LCD(0x014) = 0x00000001;
*REG_LCD(0x00C) &= 0xFFFEFFFE;
*REG_LCD(0x240) = brightness;
*REG_LCD(0xA40) = brightness;
*REG_LCD(0x244) = 0x1023E;
*REG_LCD(0xA44) = 0x1023E;
}
u16 ids;
void LCD_Deinitialize(void)
if(!g_gfxState.lcdIdsRead)
{
*REG_LCD(0x244) = 0;
*REG_LCD(0xA44) = 0;
*REG_LCD(0x00C) = 0x10001;
*REG_LCD(0x014) = 0;
g_gfxState.lcdIdsRead = true;
u16 top, bot;
I2C_writeReg(I2C_DEV_LCD0, 0x40, 0xFF);
I2C_readRegBuf(I2C_DEV_LCD0, 0x40, (u8*)&top, 2);
I2C_writeReg(I2C_DEV_LCD1, 0x40, 0xFF);
I2C_readRegBuf(I2C_DEV_LCD1, 0x40, (u8*)&bot, 2);
ids = top>>8;
ids |= bot & 0xFF00u;
g_gfxState.lcdIds = ids;
}
else ids = g_gfxState.lcdIds;
return ids;
}
/* GPU Control Registers */
#define REG_GPU_CNT ((vu32*)(0x10141200))
/* GPU DMA */
#define REG_GPU_PSC(n, x) ((vu32*)(0x10400010 + ((n) * 0x10) + (x)))
#define GPU_PSC_START (0x00)
#define GPU_PSC_END (0x04)
#define GPU_PSC_FILLVAL (0x08)
#define GPU_PSC_CNT (0x0C)
#define GPUDMA_ADDR(x) ((x) >> 3)
#define PSC_START (BIT(0))
#define PSC_DONE (BIT(1))
#define PSC_32BIT (2 << 8)
#define PSC_24BIT (1 << 8)
#define PSC_16BIT (0 << 8)
void GPU_PSCFill(u32 start, u32 end, u32 fv)
static void resetLcdsMaybe(void)
{
u32 mp;
if (start > end)
return;
const u16 ids = getLcdIds();
start = GPUDMA_ADDR(start);
end = GPUDMA_ADDR(end);
mp = (start + end) / 2;
*REG_GPU_PSC(0, GPU_PSC_START) = start;
*REG_GPU_PSC(0, GPU_PSC_END) = mp;
*REG_GPU_PSC(0, GPU_PSC_FILLVAL) = fv;
*REG_GPU_PSC(0, GPU_PSC_CNT) = PSC_START | PSC_32BIT;
*REG_GPU_PSC(1, GPU_PSC_START) = mp;
*REG_GPU_PSC(1, GPU_PSC_END) = end;
*REG_GPU_PSC(1, GPU_PSC_FILLVAL) = fv;
*REG_GPU_PSC(1, GPU_PSC_CNT) = PSC_START | PSC_32BIT;
while(!((*REG_GPU_PSC(0, GPU_PSC_CNT) | *REG_GPU_PSC(1, GPU_PSC_CNT)) & PSC_DONE));
}
/* GPU Display Registers */
#define GPU_PDC(n, x) ((vu32*)(0x10400400 + ((n) * 0x100) + x))
#define PDC_PARALLAX (BIT(5))
#define PDC_MAINSCREEN (BIT(6))
#define PDC_FIXSTRIP (BIT(7))
void GPU_SetFramebuffers(const u32 *framebuffers)
// Top screen
if(ids & 0xFFu) I2C_writeReg(I2C_DEV_LCD0, 0xFE, 0xAA);
else
{
*GPU_PDC(0, 0x68) = framebuffers[0];
*GPU_PDC(0, 0x6C) = framebuffers[1];
*GPU_PDC(0, 0x94) = framebuffers[2];
*GPU_PDC(0, 0x98) = framebuffers[3];
*GPU_PDC(1, 0x68) = framebuffers[4];
*GPU_PDC(1, 0x6C) = framebuffers[5];
*GPU_PDC(0, 0x78) = 0;
*GPU_PDC(1, 0x78) = 0;
I2C_writeReg(I2C_DEV_LCD0, 0x11, 0x10);
I2C_writeReg(I2C_DEV_LCD0, 0x50, 1);
}
void GPU_SetFramebufferMode(u32 screen, u8 mode)
// Bottom screen
if(ids>>8) I2C_writeReg(I2C_DEV_LCD1, 0xFE, 0xAA);
else I2C_writeReg(I2C_DEV_LCD1, 0x11, 0x10);
I2C_writeReg(I2C_DEV_LCD0, 0x60, 0);
I2C_writeReg(I2C_DEV_LCD1, 0x60, 0);
I2C_writeReg(I2C_DEV_LCD0, 1, 0x10);
I2C_writeReg(I2C_DEV_LCD1, 1, 0x10);
}
static void waitLcdsReady(void)
{
u32 stride, cfg;
vu32 *fbcfg_reg, *fbstr_reg;
const u16 ids = getLcdIds();
mode &= 7;
screen &= 1;
cfg = PDC_FIXSTRIP | mode;
if (screen) {
fbcfg_reg = GPU_PDC(1, 0x70);
fbstr_reg = GPU_PDC(1, 0x90);
} else {
fbcfg_reg = GPU_PDC(0, 0x70);
fbstr_reg = GPU_PDC(0, 0x90);
cfg |= PDC_MAINSCREEN;
}
stride = 240;
switch(mode) {
case PDC_RGBA8:
stride *= 4;
break;
case PDC_RGB24:
stride *= 3;
break;
default:
stride *= 2;
break;
}
*fbcfg_reg = cfg;
*fbstr_reg = stride;
}
void GPU_Init(void)
if((ids & 0xFFu) == 0 || (ids>>8) == 0) // Unknown LCD?
{
if (*REG_GPU_CNT == 0x1007F) {
MCU_PushToLCD(false);
TIMER_WaitMS(150);
}
else
{
u32 i = 0;
do
{
u16 top, bot;
I2C_writeReg(I2C_DEV_LCD0, 0x40, 0x62);
I2C_readRegBuf(I2C_DEV_LCD0, 0x40, (u8*)&top, 2);
I2C_writeReg(I2C_DEV_LCD1, 0x40, 0x62);
I2C_readRegBuf(I2C_DEV_LCD1, 0x40, (u8*)&bot, 2);
LCD_Deinitialize();
*REG_GPU_CNT = 0x10001;
TIMER_WaitTicks(CLK_MS_TO_TICKS(40));
if((top>>8) == 1 && (bot>>8) == 1) break;
TIMER_WaitMS(33);
} while(i++ < 10);
}
}
LCD_Initialize(0x20);
void GFX_powerOnBacklights(GfxBlight mask)
{
g_gfxState.lcdPower |= mask;
*REG_GPU_CNT = 0x1007F;
*GPU_PDC(0, 0x00) = 0x000001C2;
*GPU_PDC(0, 0x04) = 0x000000D1;
*GPU_PDC(0, 0x08) = 0x000001C1;
*GPU_PDC(0, 0x0C) = 0x000001C1;
*GPU_PDC(0, 0x10) = 0x00000000;
*GPU_PDC(0, 0x14) = 0x000000CF;
*GPU_PDC(0, 0x18) = 0x000000D1;
*GPU_PDC(0, 0x1C) = 0x01C501C1;
*GPU_PDC(0, 0x20) = 0x00010000;
*GPU_PDC(0, 0x24) = 0x0000019D;
*GPU_PDC(0, 0x28) = 0x00000002;
*GPU_PDC(0, 0x2C) = 0x00000192;
*GPU_PDC(0, 0x30) = 0x00000192;
*GPU_PDC(0, 0x34) = 0x00000192;
*GPU_PDC(0, 0x38) = 0x00000001;
*GPU_PDC(0, 0x3C) = 0x00000002;
*GPU_PDC(0, 0x40) = 0x01960192;
*GPU_PDC(0, 0x44) = 0x00000000;
*GPU_PDC(0, 0x48) = 0x00000000;
*GPU_PDC(0, 0x5C) = 0x00F00190;
*GPU_PDC(0, 0x60) = 0x01C100D1;
*GPU_PDC(0, 0x64) = 0x01920002;
*GPU_PDC(0, 0x68) = VRAM_START;
*GPU_PDC(0, 0x6C) = VRAM_START;
*GPU_PDC(0, 0x70) = 0x00080340;
*GPU_PDC(0, 0x74) = 0x00010501;
*GPU_PDC(0, 0x78) = 0x00000000;
*GPU_PDC(0, 0x90) = 0x000003C0;
*GPU_PDC(0, 0x94) = VRAM_START;
*GPU_PDC(0, 0x98) = VRAM_START;
*GPU_PDC(0, 0x9C) = 0x00000000;
for (u32 i = 0; i < 256; i++)
*GPU_PDC(0, 0x84) = 0x10101 * i;
*GPU_PDC(1, 0x00) = 0x000001C2;
*GPU_PDC(1, 0x04) = 0x000000D1;
*GPU_PDC(1, 0x08) = 0x000001C1;
*GPU_PDC(1, 0x0C) = 0x000001C1;
*GPU_PDC(1, 0x10) = 0x000000CD;
*GPU_PDC(1, 0x14) = 0x000000CF;
*GPU_PDC(1, 0x18) = 0x000000D1;
*GPU_PDC(1, 0x1C) = 0x01C501C1;
*GPU_PDC(1, 0x20) = 0x00010000;
*GPU_PDC(1, 0x24) = 0x0000019D;
*GPU_PDC(1, 0x28) = 0x00000052;
*GPU_PDC(1, 0x2C) = 0x00000192;
*GPU_PDC(1, 0x30) = 0x00000192;
*GPU_PDC(1, 0x34) = 0x0000004F;
*GPU_PDC(1, 0x38) = 0x00000050;
*GPU_PDC(1, 0x3C) = 0x00000052;
*GPU_PDC(1, 0x40) = 0x01980194;
*GPU_PDC(1, 0x44) = 0x00000000;
*GPU_PDC(1, 0x48) = 0x00000011;
*GPU_PDC(1, 0x5C) = 0x00F00140;
*GPU_PDC(1, 0x60) = 0x01C100d1;
*GPU_PDC(1, 0x64) = 0x01920052;
*GPU_PDC(1, 0x68) = VRAM_START;
*GPU_PDC(1, 0x6C) = VRAM_START;
*GPU_PDC(1, 0x70) = 0x00080300;
*GPU_PDC(1, 0x74) = 0x00010501;
*GPU_PDC(1, 0x78) = 0x00000000;
*GPU_PDC(1, 0x90) = 0x000003C0;
*GPU_PDC(1, 0x9C) = 0x00000000;
for (u32 i = 0; i < 256; i++)
*GPU_PDC(1, 0x84) = 0x10101 * i;
mask <<= 1;
MCU_controlLCDPower(mask); // Power on backlights.
eventWait(getEventMCU(), 0x3F<<24, 0x3F<<24);
/*if(mcuEventWait(0x3Fu<<24) != (u32)mask<<24)
__builtin_trap();*/
}
void GFX_powerOffBacklights(GfxBlight mask)
{
g_gfxState.lcdPower &= ~mask;
MCU_controlLCDPower(mask); // Power off backlights.
eventWait(getEventMCU(), 0x3F<<24, 0x3F<<24);
/*if(mcuEventWait(0x3Fu<<24) != (u32)mask<<24)
__builtin_trap();*/
}
u8 GFX_getBrightness(void)
{
return REG_LCD_ABL0_LIGHT;
}
void GFX_setBrightness(u8 top, u8 bot)
{
g_gfxState.lcdLights[0] = top;
g_gfxState.lcdLights[1] = bot;
REG_LCD_ABL0_LIGHT = top;
REG_LCD_ABL1_LIGHT = bot;
}
void GFX_setForceBlack(bool top, bool bot)
{
REG_LCD_ABL0_FILL = (u32)top<<24; // Force blackscreen
REG_LCD_ABL1_FILL = (u32)bot<<24; // Force blackscreen
}
static void setupDisplayController(u8 lcd)
{
if(lcd > 1) return;
static const u32 displayCfgs[2][24] =
{
{
// PDC0 regs 0-0x4C.
450, 209, 449, 449, 0, 207, 209, 453<<16 | 449,
1<<16 | 0, 413, 2, 402, 402, 402, 1, 2,
406<<16 | 402, 0, 0<<4 | 0, 0<<16 | 0xFF<<8 | 0,
// PDC0 regs 0x5C-0x64.
400<<16 | 240, // Width and height.
449<<16 | 209,
402<<16 | 2,
// PDC0 reg 0x9C.
0<<16 | 0
},
{
// PDC1 regs 0-0x4C.
450, 209, 449, 449, 205, 207, 209, 453<<16 | 449,
1<<16 | 0, 413, 82, 402, 402, 79, 80, 82,
408<<16 | 404, 0, 1<<4 | 1, 0<<16 | 0<<8 | 0xFF,
// PDC1 regs 0x5C-0x64.
320<<16 | 240, // Width and height.
449<<16 | 209,
402<<16 | 82,
// PDC1 reg 0x9C.
0<<16 | 0
}
};
const u32 *const cfg = displayCfgs[lcd];
vu32 *const regs = (vu32*)(GX_REGS_BASE + 0x400 + (0x100u * lcd));
for (unsigned i = 0; i < 0x50/4; i++)
regs[i] = cfg[i];
for (unsigned i = 0; i < 0xC/4; i++)
regs[23 + i] = cfg[20 + i];
regs[36] = g_gfxState.strides[lcd]; // PDC reg 0x90 stride.
regs[39] = cfg[23]; // PDC reg 0x9C.
// PDC regs 0x68, 0x6C, 0x94, 0x98 and 0x70.
regs[26] = g_gfxState.framebufs[lcd]; // Framebuffer A first address.
regs[27] = g_gfxState.framebufs[lcd]; // Framebuffer A second address.
regs[37] = g_gfxState.framebufs[lcd]; // Framebuffer B first address.
regs[38] = g_gfxState.framebufs[lcd]; // Framebuffer B second address.
regs[28] = g_gfxState.formats[lcd]; // Format
regs[32] = 0; // Gamma table index 0.
for(u32 i = 0; i < 256; i++) regs[33] = 0x10101u * i;
}

View File

@ -21,21 +21,166 @@
#define VBLANK_INTERRUPT (0x2A)
void LCD_SetBrightness(u8 brightness);
u8 LCD_GetBrightness(void);
enum
{
PDN_GPU_CNT_RST_REGS = 1u, // And more?
PDN_GPU_CNT_RST_PSC = 1u<<1, // ?
PDN_GPU_CNT_RST_GEOSHADER = 1u<<2, // ?
PDN_GPU_CNT_RST_RASTERIZER = 1u<<3, // ?
PDN_GPU_CNT_RST_PPF = 1u<<4,
PDN_GPU_CNT_RST_PDC = 1u<<5, // ?
PDN_GPU_CNT_RST_PDC2 = 1u<<6, // Maybe pixel pipeline or so?
void LCD_Deinitialize(void);
void GPU_PSCFill(u32 start, u32 end, u32 fv);
enum {
PDC_RGBA8 = 0,
PDC_RGB24 = 1,
PDC_RGB565 = 2,
PDC_RGB5A1 = 3,
PDC_RGBA4 = 4,
PDN_GPU_CNT_RST_ALL = (PDN_GPU_CNT_RST_PDC2<<1) - 1
};
void GPU_SetFramebufferMode(u32 screen, u8 mode);
void GPU_SetFramebuffers(const u32 *framebuffers);
void GPU_Init(void);
typedef enum
{
GFX_RGBA8 = 0, ///< RGBA8. (4 bytes)
GFX_BGR8 = 1, ///< BGR8. (3 bytes)
GFX_RGB565 = 2, ///< RGB565. (2 bytes)
GFX_RGB5A1 = 3, ///< RGB5A1. (2 bytes)
GFX_RGBA4 = 4 ///< RGBA4. (2 bytes)
} GfxFbFmt;
typedef enum
{
GFX_EVENT_PSC0 = 0u,
GFX_EVENT_PSC1 = 1u,
GFX_EVENT_PDC0 = 2u,
GFX_EVENT_PDC1 = 3u,
GFX_EVENT_PPF = 4u,
GFX_EVENT_P3D = 5u
} GfxEvent;
typedef enum
{
GFX_BLIGHT_BOT = 1u<<2,
GFX_BLIGHT_TOP = 1u<<4,
GFX_BLIGHT_BOTH = GFX_BLIGHT_TOP | GFX_BLIGHT_BOT
} GfxBlight;
#define REG_CFG11_GPUPROT *((vu16*)(0x10140140))
#define REG_PDN_GPU_CNT *((vu32*)(0x10141200))
#define PDN_GPU_CNT_CLK_E (1u<<16)
#define PDN_VRAM_CNT_CLK_E (1u)
#define GX_REGS_BASE (0x10400000)
#define REG_GX_GPU_CLK *((vu32*)(GX_REGS_BASE + 0x0004)) // ?
// PSC (memory fill) regs.
#define REG_GX_PSC_FILL0_S_ADDR *((vu32*)(GX_REGS_BASE + 0x0010)) // Start address
#define REG_GX_PSC_FILL0_E_ADDR *((vu32*)(GX_REGS_BASE + 0x0014)) // End address
#define REG_GX_PSC_FILL0_VAL *((vu32*)(GX_REGS_BASE + 0x0018)) // Fill value
#define REG_GX_PSC_FILL0_CNT *((vu32*)(GX_REGS_BASE + 0x001C))
#define REG_GX_PSC_FILL1_S_ADDR *((vu32*)(GX_REGS_BASE + 0x0020))
#define REG_GX_PSC_FILL1_E_ADDR *((vu32*)(GX_REGS_BASE + 0x0024))
#define REG_GX_PSC_FILL1_VAL *((vu32*)(GX_REGS_BASE + 0x0028))
#define REG_GX_PSC_FILL1_CNT *((vu32*)(GX_REGS_BASE + 0x002C))
#define REG_GX_PSC_VRAM *((vu32*)(GX_REGS_BASE + 0x0030)) // gsp mudule only changes bit 8-11.
#define REG_GX_PSC_STAT *((vu32*)(GX_REGS_BASE + 0x0034))
// PDC0/1 regs see lcd.h.
// PPF (transfer engine) regs.
#define REG_GX_PPF_IN_ADDR *((vu32*)(GX_REGS_BASE + 0x0C00))
#define REG_GX_PPF_OUT_ADDR *((vu32*)(GX_REGS_BASE + 0x0C04))
#define REG_GX_PPF_DT_OUTDIM *((vu32*)(GX_REGS_BASE + 0x0C08)) // Display transfer output dimensions.
#define REG_GX_PPF_DT_INDIM *((vu32*)(GX_REGS_BASE + 0x0C0C)) // Display transfer input dimensions.
#define REG_GX_PPF_FlAGS *((vu32*)(GX_REGS_BASE + 0x0C10))
#define REG_GX_PPF_UNK14 *((vu32*)(GX_REGS_BASE + 0x0C14)) // Transfer interval?
#define REG_GX_PPF_CNT *((vu32*)(GX_REGS_BASE + 0x0C18))
#define REG_GX_PPF_IRQ_POS *((vu32*)(GX_REGS_BASE + 0x0C1C)) // ?
#define REG_GX_PPF_LEN *((vu32*)(GX_REGS_BASE + 0x0C20)) // Texture copy size in bytes.
#define REG_GX_PPF_TC_INDIM *((vu32*)(GX_REGS_BASE + 0x0C24)) // Texture copy input width and gap in 16 byte units.
#define REG_GX_PPF_TC_OUTDIM *((vu32*)(GX_REGS_BASE + 0x0C28)) // Texture copy output width and gap in 16 byte units.
// P3D (GPU internal) regs. See gpu_regs.h.
#define REG_GX_P3D(reg) *((vu32*)(GX_REGS_BASE + 0x1000 + ((reg) * 4)))
// LCD/ABL regs.
#define LCD_REGS_BASE (0x10202000)
#define REG_LCD_PARALLAX_CNT *((vu32*)(LCD_REGS_BASE + 0x000)) // Controls PWM for the parallax barrier?
#define REG_LCD_PARALLAX_PWM *((vu32*)(LCD_REGS_BASE + 0x004)) // Frequency/other PWM stuff maybe?
#define REG_LCD_UNK00C *((vu32*)(LCD_REGS_BASE + 0x00C)) // Wtf is "FIX"?
#define REG_LCD_RST *((vu32*)(LCD_REGS_BASE + 0x014)) // Reset active low.
#define REG_LCD_ABL0_CNT *((vu32*)(LCD_REGS_BASE + 0x200)) // Bit 0 enables ABL aka power saving mode.
#define REG_LCD_ABL0_FILL *((vu32*)(LCD_REGS_BASE + 0x204))
#define REG_LCD_ABL0_LIGHT *((vu32*)(LCD_REGS_BASE + 0x240))
#define REG_LCD_ABL0_LIGHT_PWM *((vu32*)(LCD_REGS_BASE + 0x244))
#define REG_LCD_ABL1_CNT *((vu32*)(LCD_REGS_BASE + 0xA00)) // Bit 0 enables ABL aka power saving mode.
#define REG_LCD_ABL1_FILL *((vu32*)(LCD_REGS_BASE + 0xA04))
#define REG_LCD_ABL1_LIGHT *((vu32*)(LCD_REGS_BASE + 0xA40))
#define REG_LCD_ABL1_LIGHT_PWM *((vu32*)(LCD_REGS_BASE + 0xA44))
// Technically these regs belong in gx.h but they are used for LCD configuration so...
// Pitfall warning: The 3DS LCDs are physically rotated 90° CCW.
// PDC0 (top screen display controller) regs.
#define REG_LCD_PDC0_HTOTAL *((vu32*)(GX_REGS_BASE + 0x400))
#define REG_LCD_PDC0_VTOTAL *((vu32*)(GX_REGS_BASE + 0x424))
#define REG_LCD_PDC0_HPOS *((const vu32*)(GX_REGS_BASE + 0x450))
#define REG_LCD_PDC0_VPOS *((const vu32*)(GX_REGS_BASE + 0x454))
#define REG_LCD_PDC0_FB_A1 *((vu32*)(GX_REGS_BASE + 0x468))
#define REG_LCD_PDC0_FB_A2 *((vu32*)(GX_REGS_BASE + 0x46C))
#define REG_LCD_PDC0_FMT *((vu32*)(GX_REGS_BASE + 0x470))
#define REG_LCD_PDC0_CNT *((vu32*)(GX_REGS_BASE + 0x474))
#define REG_LCD_PDC0_SWAP *((vu32*)(GX_REGS_BASE + 0x478))
#define REG_LCD_PDC0_STAT *((const vu32*)(GX_REGS_BASE + 0x47C))
#define REG_LCD_PDC0_GTBL_IDX *((vu32*)(GX_REGS_BASE + 0x480)) // Gamma table index.
#define REG_LCD_PDC0_GTBL_FIFO *((vu32*)(GX_REGS_BASE + 0x484)) // Gamma table FIFO.
#define REG_LCD_PDC0_STRIDE *((vu32*)(GX_REGS_BASE + 0x490))
#define REG_LCD_PDC0_FB_B1 *((vu32*)(GX_REGS_BASE + 0x494))
#define REG_LCD_PDC0_FB_B2 *((vu32*)(GX_REGS_BASE + 0x498))
// PDC1 (bottom screen display controller) regs.
#define REG_LCD_PDC1_HTOTAL *((vu32*)(GX_REGS_BASE + 0x500))
#define REG_LCD_PDC1_VTOTAL *((vu32*)(GX_REGS_BASE + 0x524))
#define REG_LCD_PDC1_HPOS *((const vu32*)(GX_REGS_BASE + 0x550))
#define REG_LCD_PDC1_VPOS *((const vu32*)(GX_REGS_BASE + 0x554))
#define REG_LCD_PDC1_FB_A1 *((vu32*)(GX_REGS_BASE + 0x568))
#define REG_LCD_PDC1_FB_A2 *((vu32*)(GX_REGS_BASE + 0x56C))
#define REG_LCD_PDC1_FMT *((vu32*)(GX_REGS_BASE + 0x570))
#define REG_LCD_PDC1_CNT *((vu32*)(GX_REGS_BASE + 0x574))
#define REG_LCD_PDC1_SWAP *((vu32*)(GX_REGS_BASE + 0x578))
#define REG_LCD_PDC1_STAT *((const vu32*)(GX_REGS_BASE + 0x57C))
#define REG_LCD_PDC1_GTBL_IDX *((vu32*)(GX_REGS_BASE + 0x580)) // Gamma table index.
#define REG_LCD_PDC1_GTBL_FIFO *((vu32*)(GX_REGS_BASE + 0x584)) // Gamma table FIFO.
#define REG_LCD_PDC1_STRIDE *((vu32*)(GX_REGS_BASE + 0x590))
#define REG_LCD_PDC1_FB_B1 *((vu32*)(GX_REGS_BASE + 0x594))
#define REG_LCD_PDC1_FB_B2 *((vu32*)(GX_REGS_BASE + 0x598))
// REG_LCD_PDC_CNT
#define PDC_CNT_E (1u)
#define PDC_CNT_I_MASK_H (1u<<8) // Disables H(Blank?) IRQs.
#define PDC_CNT_I_MASK_V (1u<<9) // Disables VBlank IRQs.
#define PDC_CNT_I_MASK_ERR (1u<<10) // Disables error IRQs. What kind of errors?
#define PDC_CNT_OUT_E (1u<<16) // Output enable?
// REG_LCD_PDC_SWAP
// Masks
#define PDC_SWAP_NEXT (1u) // Next framebuffer.
#define PDC_SWAP_CUR (1u<<4) // Currently displaying framebuffer?
// Bits
#define PDC_SWAP_RST_FIFO (1u<<8) // Which FIFO?
#define PDC_SWAP_I_H (1u<<16) // H(Blank?) IRQ bit.
#define PDC_SWAP_I_V (1u<<17) // VBlank IRQ bit.
#define PDC_SWAP_I_ERR (1u<<18) // Error IRQ bit?
#define PDC_SWAP_I_ALL (PDC_SWAP_I_ERR | PDC_SWAP_I_V | PDC_SWAP_I_H)
unsigned GFX_init(GfxFbFmt mode);
void GFX_setForceBlack(bool top, bool bot);
void GFX_powerOnBacklights(GfxBlight mask);
void GFX_powerOffBacklights(GfxBlight mask);
u8 GFX_getBrightness(void);
void GFX_setBrightness(u8 top, u8 bot);

View File

@ -30,23 +30,15 @@ static u32 HID_ConvertCPAD(s16 cpad_x, s16 cpad_y)
{
u32 ret = 0;
switch(int_sign(cpad_x)) {
default:
break;
case 1:
if (cpad_x > 0) {
ret |= BUTTON_RIGHT;
break;
case -1:
} else if (cpad_x < 0) {
ret |= BUTTON_LEFT;
}
switch(int_sign(cpad_y)) {
default:
break;
case 1:
if (cpad_y > 0) {
ret |= BUTTON_UP;
break;
case -1:
} else if (cpad_y < 0) {
ret |= BUTTON_DOWN;
}
@ -60,7 +52,7 @@ u64 HID_GetState(void)
CODEC_Get(&codec);
ret = REG_HID | MCU_GetSpecialHID();
ret = REG_HID | mcuGetSpecialHID();
if (!(ret & BUTTON_ARROW))
ret |= HID_ConvertCPAD(codec.cpad_x, codec.cpad_y);

View File

@ -31,6 +31,9 @@
#define I2C_IRQ_ENABLE (1u<<6)
#define I2C_ENABLE (1u<<7)
#define I2C_DEV_LCD0 5
#define I2C_DEV_LCD1 6
#define I2C_GET_ACK(reg) ((bool)((reg)>>4 & 1u))
@ -62,3 +65,13 @@ bool I2C_readRegBuf(int devId, u8 regAddr, u8 *out, u32 size);
* @return Returns true on success and false on failure.
*/
bool I2C_writeRegBuf(int devId, u8 regAddr, const u8 *in, u32 size);
static inline u8 I2C_readReg(int devId, u8 regAddr) {
u8 v;
I2C_readRegBuf(devId, regAddr, &v, 1);
return v;
}
static inline void I2C_writeReg(int devId, u8 regAddr, u8 v) {
I2C_writeRegBuf(devId, regAddr, &v, 1);
}

View File

@ -20,40 +20,39 @@
#include <types.h>
#include <arm.h>
#include <stdatomic.h>
#include "common.h"
#include "arm/timer.h"
#include "hw/gpio.h"
#include "hw/gpulcd.h"
#include "hw/mcu.h"
enum {
MCU_PWR_BTN = 0,
MCU_PWR_HOLD = 1,
MCU_HOME_BTN = 2,
MCU_HOME_LIFT = 3,
MCU_WIFI_SWITCH = 4,
MCU_SHELL_CLOSE = 5,
MCU_SHELL_OPEN = 6,
MCU_VOL_SLIDER = 22,
};
#include "system/event.h"
#define MCUEV_HID_MASK ( \
MCUEV_HID_PWR_DOWN | MCUEV_HID_PWR_HOLD | \
MCUEV_HID_HOME_DOWN | MCUEV_HID_HOME_UP | MCUEV_HID_WIFI_SWITCH)
enum {
REG_VOL_SLIDER = 0x09,
MCUREG_VOLUME_SLIDER = 0x09,
REG_BATTERY_LEVEL = 0x0B,
REG_CONSOLE_STATE = 0x0F,
MCUREG_BATTERY_LEVEL = 0x0B,
MCUREG_CONSOLE_STATE = 0x0F,
REG_INT_MASK = 0x10,
REG_INT_EN = 0x18,
MCUREG_INT_MASK = 0x10,
MCUREG_INT_EN = 0x18,
REG_LCD_STATE = 0x22,
MCUREG_LCD_STATE = 0x22,
REG_LED_WIFI = 0x2A,
REG_LED_CAMERA = 0x2B,
REG_LED_SLIDER = 0x2C,
REG_LED_NOTIF = 0x2D,
MCUREG_LED_WIFI = 0x2A,
MCUREG_LED_CAMERA = 0x2B,
MCUREG_LED_SLIDER = 0x2C,
MCUREG_LED_STATUS = 0x2D,
REG_RTC = 0x30,
MCUREG_RTC = 0x30,
};
typedef struct {
@ -64,47 +63,70 @@ typedef struct {
u32 red[8];
u32 green[8];
u32 blue[8];
} PACKED_STRUCT MCU_NotificationLED;
} PACKED_STRUCT mcuStatusLED;
static u8 cached_volume_slider = 0;
static u32 spec_hid = 0, shell_state = SHELL_OPEN;
static u8 volumeSliderValue;
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;
}
static void MCU_UpdateShellState(bool open)
{
shell_state = open ? SHELL_OPEN : SHELL_CLOSED;
if (mask & MCUEV_HID_SHELL_CLOSE) {
shellState = SHELL_CLOSED;
}
u8 MCU_GetVolumeSlider(void)
{
return cached_volume_slider;
atomic_fetch_or(&pendingEvents, mask);
}
u32 MCU_GetSpecialHID(void)
u8 mcuGetVolumeSlider(void)
{
u32 ret = spec_hid | shell_state;
spec_hid = 0;
return ret;
mcuEventUpdate();
return volumeSliderValue;
}
void MCU_SetNotificationLED(u32 period_ms, u32 color)
u32 mcuGetSpecialHID(void)
{
u32 r, g, b;
MCU_NotificationLED led_state;
u32 ret = 0, pend = getEventMCU()->test(MCUEV_HID_MASK, MCUEV_HID_MASK);
// handle proper non-zero periods
// so small the hardware can't handle it
if (period_ms != 0 && period_ms < 63)
period_ms = 63;
// 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;
}
led_state.delay = (period_ms * 0x10) / 1000;
led_state.smoothing = 0x40;
led_state.loop_delay = 0x10;
led_state.unk = 0;
void mcuSetStatusLED(u32 period_ms, u32 color)
{
u32 r, g, b, delay;
mcuStatusLED ledState;
delay = clamp((period_ms * 0x200) / 1000, 1, 0xFF);
ledState.delay = delay;
ledState.smoothing = delay;
ledState.loop_delay = 0x10;
ledState.unk = 0;
// all colors look like 0x00ZZ00ZZ
// in order to alternate between
@ -112,100 +134,65 @@ void MCU_SetNotificationLED(u32 period_ms, u32 color)
r = (color >> 16) & 0xFF;
r |= r << 16;
for (int i = 0; i < 8; i++)
led_state.red[i] = r;
ledState.red[i] = r;
g = (color >> 8) & 0xFF;
g |= g << 16;
for (int i = 0; i < 8; i++)
led_state.green[i] = g;
ledState.green[i] = g;
b = color & 0xFF;
b |= b << 16;
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);
MCU_WriteReg(REG_LED_CAMERA, 0);
MCU_WriteReg(REG_LED_SLIDER, 0);
MCU_SetNotificationLED(0, 0);
mcuWriteReg(MCUREG_LED_WIFI, 0);
mcuWriteReg(MCUREG_LED_CAMERA, 0);
mcuWriteReg(MCUREG_LED_SLIDER, 0);
mcuSetStatusLED(0, 0);
}
void MCU_PushToLCD(bool enable)
void mcuReset(void)
{
MCU_WriteReg(REG_LCD_STATE, enable ? 0x2A : 0x01);
}
u32 intmask = 0;
void MCU_HandleInterrupts(u32 __attribute__((unused)) irqn)
{
u32 ints;
atomic_init(&pendingEvents, 0);
// Reading the pending mask automagically acknowledges
// the interrupts so all of them must be processed in one go
MCU_ReadRegBuf(REG_INT_MASK, (u8*)&ints, sizeof(ints));
// set register mask and clear any pending registers
mcuWriteRegBuf(MCUREG_INT_EN, (const u8*)&intmask, sizeof(intmask));
mcuReadRegBuf(MCUREG_INT_MASK, (u8*)&intmask, sizeof(intmask));
while(ints != 0) {
u32 mcu_int_id = 31 - __builtin_clz(ints);
mcuResetLEDs();
switch(mcu_int_id) {
case MCU_PWR_BTN:
case MCU_PWR_HOLD:
spec_hid |= BUTTON_POWER;
break;
case MCU_HOME_BTN:
spec_hid |= BUTTON_HOME;
break;
case MCU_HOME_LIFT:
spec_hid &= ~BUTTON_HOME;
break;
case MCU_WIFI_SWITCH:
spec_hid |= BUTTON_WIFI;
break;
case MCU_SHELL_OPEN:
MCU_PushToLCD(true);
MCU_UpdateShellState(true);
TIMER_WaitTicks(CLK_MS_TO_TICKS(5));
MCU_ResetLED();
break;
case MCU_SHELL_CLOSE:
MCU_PushToLCD(false);
MCU_UpdateShellState(false);
TIMER_WaitTicks(CLK_MS_TO_TICKS(5));
break;
case MCU_VOL_SLIDER:
MCU_UpdateVolumeSlider();
break;
default:
break;
}
ints &= ~BIT(mcu_int_id);
}
}
void MCU_Init(void)
{
u32 clrpend, mask = 0xFFBF0800;
/* 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));
volumeSliderValue = mcuReadReg(MCUREG_VOLUME_SLIDER);
shellState = SHELL_OPEN;
// assume the shell is always open on boot
// knowing the average 3DS user, there will be plenty
// of laughs when this comes back to bite us in the rear
GPIO_setBit(19, 9);
}
static void evReset(void) {
atomic_store(&pendingEvents, 0);
}
static u32 evTest(u32 mask, u32 clear) {
mcuEventUpdate();
return atomic_fetch_and(&pendingEvents, ~clear) & mask;
}
static const EventInterface evMCU = {
.reset = evReset,
.test = evTest
};
const EventInterface *getEventMCU(void) {
return &evMCU;
}

View File

@ -20,41 +20,54 @@
#include <types.h>
#include "arm/timer.h"
#include "hw/i2c.h"
#define MCU_INTERRUPT (0x71)
#define I2C_MCU_DEVICE (3)
u8 MCU_GetVolumeSlider(void);
u32 MCU_GetSpecialHID(void);
enum {
MCUEV_HID_PWR_DOWN = BIT(0),
MCUEV_HID_PWR_HOLD = BIT(1),
MCUEV_HID_HOME_DOWN = BIT(2),
MCUEV_HID_HOME_UP = BIT(3),
MCUEV_HID_WIFI_SWITCH = BIT(4),
MCUEV_HID_SHELL_CLOSE = BIT(5),
MCUEV_HID_SHELL_OPEN = BIT(6),
MCUEV_HID_VOLUME_SLIDER = BIT(22),
};
void MCU_SetNotificationLED(u32 period_ms, u32 color);
void MCU_ResetLED(void);
u8 mcuGetVolumeSlider(void);
u32 mcuGetSpecialHID(void);
void MCU_PushToLCD(bool enable);
void mcuSetStatusLED(u32 period_ms, u32 color);
void mcuResetLEDs(void);
void MCU_HandleInterrupts(u32 irqn);
void mcuReset(void);
void MCU_Init(void);
static inline u8 MCU_ReadReg(u8 addr)
static inline u8 mcuReadReg(u8 addr)
{
u8 val;
I2C_readRegBuf(I2C_MCU_DEVICE, addr, &val, 1);
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);
}
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);
}
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);
}
static inline void MCU_controlLCDPower(u8 bits)
{
mcuWriteReg(0x22u, bits);
}

View File

@ -31,126 +31,167 @@
#include "hw/nvram.h"
#include "system/sys.h"
#include "system/xalloc.h"
#include "system/event.h"
static GlobalSharedMemory SharedMemory_State;
#ifndef FIXED_BRIGHTNESS
static const u8 brightness_lvls[] = {
0x10, 0x17, 0x1E, 0x25,
0x2C, 0x34, 0x3C, 0x44,
0x4D, 0x56, 0x60, 0x6B,
0x79, 0x8C, 0xA7, 0xD2
};
static int prev_bright_lvl = -1;
static bool auto_brightness = true;
#ifndef FIXED_BRIGHTNESS
static int prev_bright_lvl;
static bool auto_brightness;
#endif
void VBlank_Handler(u32 __attribute__((unused)) irqn)
static SystemSHMEM __attribute__((section(".shared"))) sharedMem;
static void vblankUpdate(void)
{
if (!getEventIRQ()->test(VBLANK_INTERRUPT, true))
return;
#ifndef FIXED_BRIGHTNESS
int cur_bright_lvl = (MCU_GetVolumeSlider() >> 2) % countof(brightness_lvls);
int cur_bright_lvl = (mcuGetVolumeSlider() >> 2) % countof(brightness_lvls);
if ((cur_bright_lvl != prev_bright_lvl) && auto_brightness) {
prev_bright_lvl = cur_bright_lvl;
LCD_SetBrightness(brightness_lvls[cur_bright_lvl]);
u8 br = brightness_lvls[cur_bright_lvl];
GFX_setBrightness(br, br);
}
#endif
// the state should probably be stored on its own
// section without caching enabled, since it must
// be readable by the ARM9 at all times anyway
SharedMemory_State.hid_state = HID_GetState();
ARM_WbDC_Range(&SharedMemory_State, sizeof(SharedMemory_State));
ARM_DMB();
// 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);
}
static bool legacy_boot = false;
sharedMem.hidState.full = HID_GetState();
}
void PXI_RX_Handler(u32 __attribute__((unused)) irqn)
static u32 pxiRxUpdate(u32 *args)
{
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();
cmd = msg & 0xFFFF;
argc = msg >> 16;
lo = msg & 0xFFFF;
hi = msg >> 16;
if (argc >= PXI_MAX_ARGS) {
PXI_Send(0xFFFFFFFF);
return;
PXI_RecvArray(args, hi);
return lo;
}
PXI_RecvArray(args, argc);
switch (cmd) {
case PXI_LEGACY_MODE:
void __attribute__((noreturn)) MainLoop(void)
{
// TODO: If SMP is enabled, an IPI should be sent here (with a DSB)
legacy_boot = true;
ret = 0;
bool runPxiCmdProcessor = true;
#ifdef FIXED_BRIGHTNESS
u8 fixed_bright_lvl = brightness_lvls[clamp(FIXED_BRIGHTNESS, 0, countof(brightness_lvls)-1)];
GFX_setBrightness(fixed_bright_lvl, fixed_bright_lvl);
#else
prev_bright_lvl = -1;
auto_brightness = true;
#endif
// initialize state stuff
getEventIRQ()->reset();
getEventMCU()->reset();
memset(&sharedMem, 0, sizeof(sharedMem));
// configure interrupts
gicSetInterruptConfig(PXI_RX_INTERRUPT, BIT(0), GIC_PRIO0, NULL);
gicSetInterruptConfig(VBLANK_INTERRUPT, BIT(0), GIC_PRIO0, NULL);
gicSetInterruptConfig(MCU_INTERRUPT, BIT(0), GIC_PRIO0, NULL);
// enable interrupts
gicEnableInterrupt(MCU_INTERRUPT);
// perform gpu init after initializing mcu but before
// enabling the pxi system and the vblank handler
GFX_init(GFX_RGB565);
gicEnableInterrupt(PXI_RX_INTERRUPT);
gicEnableInterrupt(VBLANK_INTERRUPT);
// ARM9 won't try anything funny until this point
PXI_Barrier(PXI_BOOT_BARRIER);
// Process commands until the ARM9 tells
// us it's time to boot something else
// also handles VBlank events as needed
do {
u32 pxiCmd, pxiReply, args[PXI_MAX_ARGS];
vblankUpdate();
pxiCmd = pxiRxUpdate(args);
switch(pxiCmd) {
// ignore args and just wait until the next event
case PXICMD_NONE:
ARM_WFI();
break;
// revert to legacy boot mode
case PXICMD_LEGACY_BOOT:
runPxiCmdProcessor = false;
pxiReply = 0;
break;
// returns the shared memory address
case PXICMD_GET_SHMEM_ADDRESS:
pxiReply = (u32)&sharedMem;
break;
// takes in a single argument word and performs either an
// I2C read or write depending on the value of the top bit
case PXICMD_I2C_OP:
{
u32 devId, regAddr, size;
devId = (args[0] & 0xff);
regAddr = (args[0] >> 8) & 0xFF;
size = (args[0] >> 16) % SHMEM_BUFFER_SIZE;
if (args[0] & BIT(31)) {
pxiReply = I2C_writeRegBuf(devId, regAddr, sharedMem.dataBuffer.b, size);
} else {
pxiReply = I2C_readRegBuf(devId, regAddr, sharedMem.dataBuffer.b, size);
}
break;
}
case PXI_GET_SHMEM:
{
ret = (u32)&SharedMemory_State;
// checks whether the NVRAM chip is online (not doing any work)
case PXICMD_NVRAM_ONLINE:
pxiReply = (NVRAM_Status() & NVRAM_SR_WIP) == 0;
break;
}
case PXI_SET_VMODE:
{
int mode = args[0] ? PDC_RGB24 : PDC_RGB565;
GPU_SetFramebufferMode(0, mode);
GPU_SetFramebufferMode(1, mode);
ret = 0;
// reads data from the NVRAM chip
case PXICMD_NVRAM_READ:
NVRAM_Read(args[0], sharedMem.dataBuffer.w, args[1]);
pxiReply = 0;
break;
}
case PXI_I2C_READ:
{
ARM_InvDC_Range((void*)args[2], args[3]);
ret = I2C_readRegBuf(args[0], args[1], (u8*)args[2], args[3]);
ARM_WbDC_Range((void*)args[2], args[3]);
ARM_DMB();
// sets the notification LED with the given color and period
case PXICMD_SET_NOTIFY_LED:
mcuSetStatusLED(args[0], args[1]);
pxiReply = 0;
break;
}
case PXI_I2C_WRITE:
// sets the LCDs brightness (if FIXED_BRIGHTNESS is disabled)
case PXICMD_SET_BRIGHTNESS:
{
ARM_InvDC_Range((void*)args[2], args[3]);
ARM_DMB();
ret = I2C_writeRegBuf(args[0], args[1], (u8*)args[2], args[3]);
break;
}
case PXI_NVRAM_ONLINE:
{
ret = (NVRAM_Status() & NVRAM_SR_WIP) == 0;
break;
}
case PXI_NVRAM_READ:
{
ARM_InvDC_Range((void*)args[1], args[2]);
NVRAM_Read(args[0], (u32*)args[1], args[2]);
ARM_WbDC_Range((void*)args[1], args[2]);
ARM_DMB();
ret = 0;
break;
}
case PXI_NOTIFY_LED:
{
MCU_SetNotificationLED(args[0], args[1]);
ret = 0;
break;
}
case PXI_BRIGHTNESS:
{
ret = LCD_GetBrightness();
pxiReply = GFX_getBrightness();
#ifndef FIXED_BRIGHTNESS
if ((args[0] > 0) && (args[0] < 0x100)) {
LCD_SetBrightness(args[0]);
s32 newbrightness = (s32)args[0];
if ((newbrightness > 0) && (newbrightness < 0x100)) {
GFX_setBrightness(newbrightness, newbrightness);
auto_brightness = false;
} else {
prev_bright_lvl = -1;
@ -160,52 +201,28 @@ void PXI_RX_Handler(u32 __attribute__((unused)) irqn)
break;
}
case PXI_XALLOC:
{
ret = (u32)XAlloc(args[0]);
break;
}
/* New CMD template:
case CMD_ID:
{
<var declarations/assignments>
<execute the command>
<set the return value>
break;
}
*/
// replies -1 on default
default:
ret = 0xFFFFFFFF;
pxiReply = 0xFFFFFFFF;
break;
}
PXI_Send(ret);
}
if (pxiCmd != PXICMD_NONE)
PXI_Send(pxiReply); // was a command sent from the ARM9, send a response
} while(runPxiCmdProcessor);
void __attribute__((noreturn)) MainLoop(void)
{
#ifdef FIXED_BRIGHTNESS
LCD_SetBrightness(FIXED_BRIGHTNESS);
#endif
// perform deinit in reverse order
gicDisableInterrupt(VBLANK_INTERRUPT);
gicDisableInterrupt(PXI_RX_INTERRUPT);
// enable PXI RX interrupt
GIC_Enable(PXI_RX_INTERRUPT, BIT(0), GIC_HIGHEST_PRIO + 2, PXI_RX_Handler);
// unconditionally reinitialize the screens
// in RGB24 framebuffer mode
GFX_init(GFX_BGR8);
// enable MCU interrupts
GIC_Enable(MCU_INTERRUPT, BIT(0), GIC_HIGHEST_PRIO + 1, MCU_HandleInterrupts);
gicDisableInterrupt(MCU_INTERRUPT);
// set up VBlank interrupt to always have the highest priority
GIC_Enable(VBLANK_INTERRUPT, BIT(0), GIC_HIGHEST_PRIO, VBlank_Handler);
// ARM9 won't try anything funny until this point
PXI_Barrier(ARM11_READY_BARRIER);
// Process IRQs until the ARM9 tells us it's time to boot something else
do {
ARM_WFI();
} while(!legacy_boot);
// Wait for the ARM9 to do its firmlaunch setup
PXI_Barrier(PXI_FIRMLAUNCH_BARRIER);
SYS_CoreZeroShutdown();
SYS_CoreShutdown();

View 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;
}
}

View File

@ -20,16 +20,16 @@
#include <types.h>
#define DEF_SECT_(n) extern u32 __##n##_pa, __##n##_va, __##n##_len;
DEF_SECT_(vector)
#define DEF_SECT_(n) extern u32 __##n##_pa, __##n##_va, __##n##_va_end;
DEF_SECT_(text)
DEF_SECT_(data)
DEF_SECT_(rodata)
DEF_SECT_(bss)
DEF_SECT_(shared)
#undef DEF_SECT_
#define SECTION_VA(n) ((u32)&__##n##_va)
#define SECTION_PA(n) ((u32)&__##n##_pa)
#define SECTION_LEN(n) ((u32)&__##n##_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)

View File

@ -24,7 +24,7 @@
#include "arm/gic.h"
#include "arm/mmu.h"
#include "arm/scu.h"
#include "arm/timer.h"
#include "arm/xrq.h"
#include "hw/codec.h"
#include "hw/gpulcd.h"
@ -57,18 +57,20 @@ static void SYS_EnableClkMult(void)
// state might get a bit messed up so it has to be done
// as early as possible in the initialization chain
if (SYS_IsNewConsole() && !SYS_ClkMultEnabled()) {
GIC_Enable(88, BIT(0), GIC_HIGHEST_PRIO, NULL);
gicSetInterruptConfig(88, BIT(0), GIC_PRIO_HIGHEST, NULL);
gicEnableInterrupt(88);
*CFG11_MPCORE_CLKCNT = 0x8001;
do {
ARM_WFI();
} while(!(*CFG11_MPCORE_CLKCNT & 0x8000));
GIC_Disable(88, BIT(0));
gicDisableInterrupt(88);
gicClearInterruptConfig(88);
}
}
void SYS_CoreZeroInit(void)
{
GIC_GlobalReset();
gicGlobalReset();
*LEGACY_BOOT_ENTRYPOINT = 0;
@ -77,57 +79,54 @@ void SYS_CoreZeroInit(void)
SCU_Init();
// Map all sections here
MMU_Map(SECTION_TRI(vector), MMU_FLAGS(CACHED_WT, READ_ONLY, 0, 0));
MMU_Map(SECTION_TRI(text), MMU_FLAGS(CACHED_WT, READ_ONLY, 0, 1));
MMU_Map(SECTION_TRI(data), MMU_FLAGS(CACHED_WB_ALLOC, READ_WRITE, 1, 1));
MMU_Map(SECTION_TRI(rodata), MMU_FLAGS(CACHED_WT, READ_ONLY, 1, 1));
MMU_Map(SECTION_TRI(bss), MMU_FLAGS(CACHED_WB_ALLOC, READ_WRITE, 1, 1));
mmuMapArea(SECTION_TRI(text), MMU_FLAGS(MMU_CACHE_WT, MMU_READ_ONLY, 0, 1));
mmuMapArea(SECTION_TRI(data), MMU_FLAGS(MMU_CACHE_WBA, MMU_READ_WRITE, 1, 1));
mmuMapArea(SECTION_TRI(rodata), MMU_FLAGS(MMU_CACHE_WT, MMU_READ_ONLY, 1, 1));
mmuMapArea(SECTION_TRI(bss), MMU_FLAGS(MMU_CACHE_WBA, MMU_READ_WRITE, 1, 1));
mmuMapArea(SECTION_TRI(shared), MMU_FLAGS(MMU_STRONG_ORDER, MMU_READ_WRITE, 1, 1));
// High exception vectors
mmuMapArea(0xFFFF0000, xrqInstallVectorTable(), 4UL << 10, MMU_FLAGS(MMU_CACHE_WT, MMU_READ_ONLY, 0, 0));
// BootROM
mmuMapArea(0x00010000, 0x00010000, 32UL << 10, MMU_FLAGS(MMU_CACHE_WT, MMU_READ_ONLY, 0, 1));
// IO Registers
MMU_Map(0x10100000, 0x10100000, 4UL << 20, MMU_FLAGS(DEVICE_SHARED, READ_WRITE, 1, 1));
mmuMapArea(0x10100000, 0x10100000, 4UL << 20, MMU_FLAGS(MMU_DEV_SHARED, MMU_READ_WRITE, 1, 1));
// MPCore Private Memory Region
MMU_Map(0x17E00000, 0x17E00000, 8UL << 10, MMU_FLAGS(DEVICE_SHARED, READ_WRITE, 1, 1));
mmuMapArea(0x17E00000, 0x17E00000, 8UL << 10, MMU_FLAGS(MMU_DEV_SHARED, MMU_READ_WRITE, 1, 1));
// VRAM
MMU_Map(0x18000000, 0x18000000, 6UL << 20, MMU_FLAGS(CACHED_WT, READ_WRITE, 1, 1));
mmuMapArea(0x18000000, 0x18000000, 6UL << 20, MMU_FLAGS(MMU_CACHE_WT, MMU_READ_WRITE, 1, 1));
// FCRAM
if (SYS_IsNewConsole()) {
MMU_Map(0x20000000, 0x20000000, 256UL << 20, MMU_FLAGS(CACHED_WB, READ_WRITE, 1, 1));
mmuMapArea(0x20000000, 0x20000000, 256UL << 20, MMU_FLAGS(MMU_CACHE_WB, MMU_READ_WRITE, 1, 1));
} else {
MMU_Map(0x20000000, 0x20000000, 128UL << 20, MMU_FLAGS(CACHED_WB, READ_WRITE, 1, 1));
mmuMapArea(0x20000000, 0x20000000, 128UL << 20, MMU_FLAGS(MMU_CACHE_WB, MMU_READ_WRITE, 1, 1));
}
// screen init magicks
TIMER_WaitMS(64);
// Initialize peripherals
PXI_Reset();
I2C_init();
MCU_Init();
mcuReset();
SPI_Init();
CODEC_Init();
GPU_Init();
GPU_PSCFill(VRAM_START, VRAM_END, 0);
GPU_SetFramebuffers((u32[]){VRAM_TOP_LA, VRAM_TOP_LB,
VRAM_TOP_RA, VRAM_TOP_RB,
VRAM_BOT_A, VRAM_BOT_B});
GPU_SetFramebufferMode(0, PDC_RGB24);
GPU_SetFramebufferMode(1, PDC_RGB24);
MCU_PushToLCD(true);
TIMER_WaitTicks(CLK_MS_TO_TICKS(5));
}
void SYS_CoreInit(void)
{
// Reset local GIC registers
GIC_LocalReset();
gicLocalReset();
// Set up MMU registers
MMU_Init();
mmuInitRegisters();
// Enable fancy ARM11 features
ARM_SetACR(ARM_GetACR() |
@ -144,7 +143,7 @@ void SYS_CoreInit(void)
void SYS_CoreZeroShutdown(void)
{
ARM_DisableInterrupts();
GIC_GlobalReset();
gicGlobalReset();
}
void __attribute__((noreturn)) SYS_CoreShutdown(void)
@ -153,7 +152,7 @@ void __attribute__((noreturn)) SYS_CoreShutdown(void)
ARM_DisableInterrupts();
GIC_LocalReset();
gicLocalReset();
ARM_WbInvDC();
ARM_InvIC();

View File

@ -1,37 +0,0 @@
/*
* This file is part of GodMode9
* Copyright (C) 2019 Wolfvak
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// super simple watermark allocator for ARM9 <-> ARM11 xfers
// designed to be request once, free never
#include "system/xalloc.h"
static char ALIGN(4096) xalloc_buf[XALLOC_BUF_SIZE];
static size_t mark = 0;
void *XAlloc(size_t size)
{ // not thread-safe at all
void *ret;
size_t end = size + mark;
if (end >= XALLOC_BUF_SIZE)
return NULL;
ret = &xalloc_buf[mark];
mark = end;
return ret;
}

View File

@ -1,17 +1,24 @@
PROCESSOR := ARM9
TARGET := $(shell basename $(CURDIR))
TARGET := $(shell basename "$(CURDIR)")
SOURCE := source
BUILD := build
SUBARCH := -D$(PROCESSOR) -marm -march=armv5te -mtune=arm946e-s -mfloat-abi=soft -mno-thumb-interwork
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
SUBARCH := -D$(PROCESSOR) -march=armv5te -mtune=arm946e-s -mthumb -mfloat-abi=soft
INCDIRS := source source/common source/filesys source/crypto source/fatfs source/nand source/virtual source/game source/gamecart source/lodepng source/lua source/qrcodegen source/system source/utils
INCLUDE := $(foreach dir,$(INCDIRS),-I"$(shell pwd)/$(dir)")
ASFLAGS += $(SUBARCH) $(INCLUDE)
CFLAGS += $(SUBARCH) $(INCLUDE) -fno-builtin-memcpy -flto
LDFLAGS += $(SUBARCH) -Wl,-Map,$(TARGET).map -flto
LDFLAGS += $(SUBARCH) -Wl,--use-blx,-Map,$(TARGET).map -flto
LIBS += -lm
include ../Makefile.common
include ../Makefile.build
arm9_data.elf: arm9.elf
$(OBJCOPY) -O elf32-littlearm -j .rodata* -j .data* -j .bss* $< $@
arm9_code.elf: arm9.elf
$(OBJCOPY) -O elf32-littlearm -j .text* -j .vectors* $< $@

View File

@ -4,8 +4,10 @@ ENTRY(_start)
MEMORY
{
AHBWRAM (RWX) : ORIGIN = 0x08006000, LENGTH = 512K
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
@ -16,7 +18,7 @@ SECTIONS
KEEP(*(.vectors));
. = ALIGN(4);
__vectors_len = ABSOLUTE(.) - __vectors_vma;
} >VECTORS AT>AHBWRAM
} >VECTORS AT>CODEMEM
.text : ALIGN(4) {
__text_s = ABSOLUTE(.);
@ -24,24 +26,28 @@ SECTIONS
*(.text*);
. = ALIGN(4);
__text_e = ABSOLUTE(.);
} >AHBWRAM
} >CODEMEM
.rodata : ALIGN(4) {
*(.rodata*);
. = ALIGN(4);
} >AHBWRAM
__exidx_start = .;
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
__exidx_end = .;
. = ALIGN(4);
} >DATAMEM
.data : ALIGN(4) {
*(.data*);
. = ALIGN(4);
} >AHBWRAM
} >DATAMEM
.bss : ALIGN(4) {
.bss (NOLOAD) : ALIGN(4) {
__bss_start = .;
*(.bss*);
. = ALIGN(4);
__bss_end = .;
} >AHBWRAM
} >DATAMEM
__end__ = ABSOLUTE(.);
}

View File

@ -19,7 +19,7 @@ static void SetNotificationLED(u32 period_ms, u32 rgb565_color)
(rgb565_color >> 5) << (8+2) |
(rgb565_color << 3));
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
@ -27,12 +27,12 @@ static void SetNotificationLED(u32 period_ms, u32 rgb565_color)
// separate things - hopefully LTO won't get in the way
u32 HID_ReadState(void)
{
return ARM_GetSHMEM()->hid_state;
return ARM_GetSHMEM()->hidState.keys;
}
u32 HID_ReadRawTouchState(void)
{
return ARM_GetSHMEM()->hid_state >> 32;
return ARM_GetSHMEM()->hidState.touch;
}
// ts_mult indicates a scalar for each axis

View File

@ -12,7 +12,7 @@ u32 SetScreenBrightness(int level) {
arg = 0;
}
return PXI_DoCMD(PXI_BRIGHTNESS, &arg, 1);
return PXI_DoCMD(PXICMD_SET_BRIGHTNESS, &arg, 1);
}
u32 GetBatteryPercent() {

View File

@ -27,7 +27,7 @@ void CreateScreenshot(void) {
fvx_rmkdir(OUTPUT_PATH);
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_h, dstime.bcd_m, dstime.bcd_s);
filename[63] = '\0';

View File

@ -2,8 +2,8 @@
#include "common.h"
const u8 sig_nand_firm_retail[256];
const u8 sig_nand_firm_retail_alt[256];
const u8 sig_nand_firm_dev[256];
const u8 sig_nand_ncsd_retail[256];
const u8 sig_nand_ncsd_dev[256];
extern const u8 sig_nand_firm_retail[256];
extern const u8 sig_nand_firm_retail_alt[256];
extern const u8 sig_nand_firm_dev[256];
extern const u8 sig_nand_ncsd_retail[256];
extern const u8 sig_nand_ncsd_dev[256];

View File

@ -1,8 +1,10 @@
#include <stdarg.h>
#include "language.h"
#include "swkbd.h"
#include "timer.h"
#include "hid.h"
#include "utf.h"
static inline char to_uppercase(char c) {
@ -58,10 +60,7 @@ static bool BuildKeyboard(TouchBox* swkbd, const char* keys, const u8* layout) {
}
// next row
if (layout[l++] != 0) {
ShowPrompt(false, "Oh shit %lu %lu", k, l);
return false; // error!!!! THIS HAS TO GO!
}
if (layout[l++] != 0) return false;
p_y += SWKBD_STDKEY_HEIGHT + SWKDB_KEY_SPACING;
}
@ -82,7 +81,7 @@ static void DrawKey(const TouchBox* key, const bool pressed, const u32 uppercase
if (key->id == KEY_TXTBOX) return;
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 {
keystr[0] = (uppercase) ? to_uppercase(key->id) : key->id;
keystr[1] = 0;
@ -93,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;
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) {
@ -122,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) {
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 u16 x = txtbox->x;
const u16 y = txtbox->y;
// fix scroll
if (cursor < *scroll) *scroll = cursor;
else if (cursor - *scroll > input_shown) *scroll = cursor - input_shown;
if (cursor < *scroll) {
*scroll = cursor;
} else {
int scroll_adjust = -input_shown_length;
for (u32 i = *scroll; i < cursor; i++) {
if (i >= inputstr_size || (inputstr[i] & 0xC0) != 0x80) scroll_adjust++;
}
for (int i = 0; i < scroll_adjust; i++)
*scroll += *scroll >= inputstr_size ? 1 : GetCharSize(inputstr + *scroll);
}
u32 input_shown_size = 0;
for (u32 i = 0; i < input_shown_length || (*scroll + input_shown_size < inputstr_size && (inputstr[*scroll + input_shown_size] & 0xC0) == 0x80); input_shown_size++) {
if (*scroll + input_shown_size >= inputstr_size || (inputstr[*scroll + input_shown_size] & 0xC0) != 0x80) i++;
}
// draw input string
DrawStringF(BOT_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%c%-*.*s%-*.*s%c",
DrawStringF(BOT_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%c%-*.*s%c",
(*scroll) ? '<' : '|',
(inputstr_size > input_shown) ? input_shown : inputstr_size,
(inputstr_size > input_shown) ? input_shown : inputstr_size,
inputstr + *scroll,
(inputstr_size > input_shown) ? 0 : input_shown - inputstr_size,
(inputstr_size > input_shown) ? 0 : input_shown - inputstr_size,
"",
(inputstr_size - (s32) *scroll > input_shown) ? '>' : '|'
(int) input_shown_size,
(int) input_shown_size,
(*scroll > inputstr_size) ? "" : inputstr + *scroll,
(inputstr_size - (s32) *scroll > input_shown_size) ? '>' : '|'
);
// draw cursor
u16 cpos = 0;
for (u16 i = *scroll; i < cursor; i++) {
if (i >= inputstr_size || (inputstr[i] & 0xC0) != 0x80) cpos++;
}
DrawStringF(BOT_SCREEN, x-(FONT_WIDTH_EXT/2), y+10, COLOR_STD_FONT, COLOR_STD_BG, "%-*.*s^%-*.*s",
1 + cursor - *scroll,
1 + cursor - *scroll,
(int) (1 + cpos),
(int) (1 + cpos),
"",
input_shown - (cursor - *scroll),
input_shown - (cursor - *scroll),
(int) (input_shown_length - cpos),
(int) (input_shown_length - cpos),
""
);
}
@ -166,8 +181,17 @@ static void MoveTextBoxCursor(const TouchBox* txtbox, const char* inputstr, cons
const TouchBox* tb = TouchBoxGet(&id, x, y, txtbox, 0);
if (id == KEY_TXTBOX) {
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
if (*cursor != cursor_next) {
if (cursor_next < max_size) *cursor = cursor_next;
@ -176,10 +200,10 @@ static void MoveTextBoxCursor(const TouchBox* txtbox, const char* inputstr, cons
}
// move beyound visible field
if (timer_msec(timer) >= scroll_cooldown) {
if ((cpos == 0) && (*scroll > 0))
(*scroll)--;
else if ((cpos >= input_shown) && (*cursor < (max_size-1)))
(*scroll)++;
if ((cpos_length == 0) && (*scroll > 0))
*scroll -= GetPrevCharSize(inputstr + *scroll);
else if ((cpos_length >= input_shown) && (*cursor < (max_size-1)))
*scroll += GetCharSize(inputstr + *scroll);
}
}
}
@ -199,6 +223,7 @@ static char KeyboardWait(TouchBox* swkbd, bool uppercase) {
else if (pressed & BUTTON_R1) return KEY_CAPS;
else if (pressed & BUTTON_RIGHT) return KEY_RIGHT;
else if (pressed & BUTTON_LEFT) return KEY_LEFT;
else if (pressed & BUTTON_SELECT) return KEY_SWITCH;
else if (pressed & BUTTON_TOUCH) break;
}
@ -228,6 +253,13 @@ bool ShowKeyboard(char* inputstr, const u32 max_size, const char *format, ...) {
TouchBox swkbd_numpad[32];
TouchBox* textbox = swkbd_alphabet; // always use this textbox
static bool show_instr = true;
const char* instr = STR_KEYBOARD_CONTROLS_DETAILS;
if (show_instr) {
ShowPrompt(false, "%s", instr);
show_instr = false;
}
// generate keyboards
if (!BuildKeyboard(swkbd_alphabet, keys_alphabet, layout_alphabet)) return false;
if (!BuildKeyboard(swkbd_special, keys_special, layout_special)) return false;
@ -237,7 +269,7 @@ bool ShowKeyboard(char* inputstr, const u32 max_size, const char *format, ...) {
char str[512]; // arbitrary limit, should be more than enough
va_list va;
va_start(va, format);
vsnprintf(str, 512, format, va);
vsnprintf(str, sizeof(str), format, va);
va_end(va);
u32 str_width = GetDrawStringWidth(str);
if (str_width < (24 * FONT_WIDTH_EXT)) str_width = 24 * FONT_WIDTH_EXT;
@ -281,22 +313,51 @@ bool ShowKeyboard(char* inputstr, const u32 max_size, const char *format, ...) {
break;
} else if (key == KEY_BKSPC) {
if (cursor) {
int size = GetPrevCharSize(inputstr + cursor);
if (cursor <= inputstr_size) {
memmove(inputstr + cursor - 1, inputstr + cursor, inputstr_size - cursor + 1);
inputstr_size--;
memmove(inputstr + cursor - size, inputstr + cursor, inputstr_size - cursor + size);
inputstr_size -= size;
}
cursor--;
cursor -= size;
}
} else if (key == KEY_LEFT) {
if (cursor) cursor--;
if (cursor) cursor -= GetPrevCharSize(inputstr + cursor);
} 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) {
swkbd = swkbd_alphabet;
} else if (key == KEY_SPECIAL) {
swkbd = swkbd_special;
} else if (key == KEY_NUMPAD) {
swkbd = swkbd_numpad;
} else if (key == KEY_SWITCH) {
ClearScreen(BOT_SCREEN, COLOR_STD_BG);
return ShowStringPrompt(inputstr, max_size, "%s", str);
} else if (key == KEY_UNICODE) {
if (cursor > 3 && cursor <= inputstr_size) {
u16 codepoint = 0;
for (char *c = inputstr + cursor - 4; c < inputstr + cursor; c++) {
if ((*c >= '0' && *c <= '9') || (*c >= 'A' && *c <= 'F') || (*c >= 'a' && *c <= 'f')) {
codepoint <<= 4;
codepoint |= *c - (*c <= '9' ? '0' : ((*c <= 'F' ? 'A' : 'a') - 10));
} else {
codepoint = 0;
break;
}
}
if(codepoint != 0) {
char character[5] = {0};
u16 input[2] = {codepoint, 0};
utf16_to_utf8((u8*)character, input, 4, 1);
u32 char_size = GetCharSize(character);
memmove(inputstr + cursor - 4 + char_size, inputstr + cursor, max_size - cursor + 4 - char_size);
memcpy(inputstr + cursor - 4, character, char_size);
cursor -= 4 - char_size;
}
}
} else if (key && (key < 0x80)) {
if ((cursor < (max_size-1)) && (inputstr_size < max_size)) {
// pad string (if cursor beyound string size)

View File

@ -18,16 +18,17 @@ enum {
KEY_LEFT = 0x88,
KEY_RIGHT = 0x89,
KEY_ESCAPE = 0x8A,
KEY_SWITCH = 0x8B,
KEY_UNICODE = 0x8C,
KEY_TXTBOX = 0xFF
};
// special key strings
#define SWKBD_KEYSTR "", "DEL", "INS", "SUBMIT", "CAPS", "#$@", "123", "ABC", "\x1b", "\x1a", "ESC"
#define SWKBD_KEYSTR "", "DEL", "INS", "SUBMIT", "CAPS", "#$@", "123", "ABC", "←", "→", "ESC", "SWITCH", "U+"
#define COLOR_SWKBD_NORMAL COLOR_GREY
#define COLOR_SWKBD_PRESSED COLOR_LIGHTGREY
#define COLOR_SWKBD_BOX COLOR_DARKGREY
#define COLOR_SWKBD_TEXTBOX COLOR_DARKGREY
#define COLOR_SWKBD_CHARS COLOR_BLACK
#define COLOR_SWKBD_ENTER COLOR_TINTEDBLUE
#define COLOR_SWKBD_CAPS COLOR_TINTEDYELLOW
@ -56,7 +57,7 @@ enum {
'4', '5', '6', 'D', 'C', \
'3', '2', '1', 'B', 'A', \
'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
#define SWKBD_LAYOUT_ALPHABET \
@ -80,9 +81,9 @@ enum {
5, 0, \
5, 0, \
5, 18, 18, 0, \
3, 30, 34, 30, 0, \
4, 20, 20, 31, 20, 0, \
0
#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, ...);

View File

@ -2,6 +2,7 @@
#include "ui.h"
#include "hid.h"
#include "crc16.h"
#include "language.h"
#include "spiflash.h"
#include "support.h"
@ -46,8 +47,8 @@ bool ShowTouchCalibrationDialog(void)
// clear screen, draw instructions
ClearScreen(BOT_SCREEN, COLOR_STD_BG);
DrawStringCenter(BOT_SCREEN, COLOR_STD_FONT, COLOR_STD_BG,
"Touch the red crosshairs to\ncalibrate your touchscreen.\n \nUse the stylus for best\nresults!");
DrawStringCenter(BOT_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s",
STR_TOUCH_CROSSHAIRS_TO_CALIBRATE_TOUCHSCREEN_USE_STYLUS);
// set calibration defaults
SetCalibrationDefaults();

View File

@ -15,21 +15,134 @@
#include "power.h"
#include "hid.h"
#include "fixp.h"
#include "language.h"
#define STRBUF_SIZE 512 // maximum size of the string buffer
#define FONT_MAX_WIDTH 8
#define FONT_MAX_HEIGHT 10
#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_height = 0;
static u32 font_count = 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))
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 pbm_w = 0;
u32 pbm_h = 0;
@ -90,27 +203,101 @@ u8* GetFontFromPbm(const void* pbm, const u32 pbm_size, u32* w, u32* h) {
return (u8*) pbm + p;
}
// sets the font from a given PBM
// if no PBM is given, the PBM is fetched from the default VRAM0 location
bool SetFontFromPbm(const void* pbm, u32 pbm_size) {
u32 w, h;
u8* ptr = NULL;
const u8* GetFontFromRiff(const void* riff, const u32 riff_size, u32* w, u32* h, u16* count) {
const void* ptr = riff;
const RiffChunkHeader* riff_header;
const RiffChunkHeader* chunk_header;
if (!pbm) {
u64 pbm_size64 = 0;
pbm = FindVTarFileInfo(VRAM0_FONT_PBM, &pbm_size64);
pbm_size = (u32) pbm_size64;
// 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 != 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;
}
if (pbm)
ptr = GetFontFromPbm(pbm, pbm_size, &w, &h);
ptr += sizeof(RiffChunkHeader) + chunk_header->size;
}
if (!ptr) {
return NULL;
}
// 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;
} else if (w > 8) {
if ((ptr = GetFontFromRiff(font, font_size, &w, &h, &count))) { // RIFF font
font_width = w;
font_height = 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;
memset(font_bin, 0x00, w * h / 8);
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++) {
@ -118,7 +305,7 @@ bool SetFontFromPbm(const void* pbm, u32 pbm_size) {
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;
font_bin[(cp437_sorted[(cy << 4) + cx] * font_height) + row] = byte;
}
ptr += font_width << 1;
}
@ -126,7 +313,24 @@ bool SetFontFromPbm(const void* pbm, u32 pbm_size) {
} else {
font_width = w;
font_height = h / 256;
memcpy(font_bin, ptr, h);
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);
@ -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++) {
int xDisplacement = x * SCREEN_HEIGHT;
int yDisplacement = SCREEN_HEIGHT - (y + yy) - 1;
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--) {
if ((charPos >> xx) & 1) {
*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 len = (strlen(str) > max_len) ? max_len : strlen(str);
for (size_t i = 0; i < len; i++) {
char c = (char) (fix_utf8 && str[i] >= 0x80) ? '?' : str[i];
DrawCharacter(screen, c, x + i * font_width, y, color, bgcolor);
for (size_t i = 0; i < max_len && *str; i++) {
DrawCharacter(screen, GetCharacter(&str), 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);
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, ...)
@ -287,16 +489,44 @@ u32 GetDrawStringHeight(const char* str) {
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 width = 0;
char* old_lf = (char*) str;
char* str_end = (char*) str + strnlen(str, STRBUF_SIZE);
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;
}
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;
return width;
}
@ -309,6 +539,26 @@ u32 GetFontHeight(void) {
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) {
char* last_brk = 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) {
int osize = strnlen(orig, 256);
if (nsize < osize) {
TruncateString(dest, orig, nsize, tpos);
} else if (!align_right) {
snprintf(dest, nsize + 1, "%-*.*s", nsize, nsize, orig);
// dest must be at least 4x the size of nlength to account for UTF-8
void ResizeString(char* dest, const char* orig, int nlength, int tpos, bool align_right) {
int olength = 0;
for (int i = 0; i < 256 && orig[i]; i++) {
if ((orig[i] & 0xC0) != 0x80) olength++;
}
if (nlength < olength) {
TruncateString(dest, orig, nlength, tpos);
} 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) {
int osize = strnlen(orig, 256);
if (nsize < 0) {
// dest must be at least 4x the size of nlength to account for UTF-8
void TruncateString(char* dest, const char* orig, int nlength, int tpos) {
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;
} else if ((nsize <= 3) || (nsize >= osize)) {
snprintf(dest, nsize + 1, "%s", orig);
} else if ((nlength <= 3) || (nlength >= olength)) {
strcpy(dest, orig);
} else {
if (tpos + 3 > nsize) tpos = nsize - 3;
snprintf(dest, nsize + 1, "%-.*s...%-.*s", tpos, orig, nsize - (3 + tpos), orig + osize - (nsize - (3 + tpos)));
if (tpos + 3 > nlength) tpos = nlength - 3;
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 (; mag1000 > 0; mag1000 /= 1000) {
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
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 {
u32 scale = 1;
u64 bytes100 = (bytes * 100) >> 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);
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, ...)
@ -454,7 +728,7 @@ bool ShowPrompt(bool ask, const char *format, ...)
ClearScreenF(true, false, COLOR_STD_BG);
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) {
u32 pad_state = InputWait(0);
@ -474,7 +748,7 @@ bool ShowPrompt(bool ask, const char *format, ...)
#define PRNG (*(volatile u32*)0x10011000)
bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
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 = 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;
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_font = COLOR_BLACK;
color_off = COLOR_BLACK;
@ -509,13 +783,13 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
ClearScreenF(true, false, color_bg);
DrawStringF(MAIN_SCREEN, x, y, color_font, color_bg, "%s", str);
#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
const char dpad_symbols[] = { '\x1A', '\x1B', '\x18', '\x19' }; // R L U D
const char *dpad_symbols[] = { "", "", "", "" }; // R L U D
u32 sequence[seqlen_max];
char seqsymbols[seqlen_max];
const char *seqsymbols[seqlen_max];
u32 lastlsh = (u32) -1;
for (u32 n = 0; n < (seqlen-1); n++) {
u32 lsh = lastlsh;
@ -525,13 +799,13 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
seqsymbols[n] = dpad_symbols[lsh];
}
sequence[seqlen-1] = BUTTON_A;
seqsymbols[seqlen-1] = 'A';
seqsymbols[seqlen-1] = "A";
while (true) {
for (u32 n = 0; n < seqlen; n++) {
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)
break;
@ -546,7 +820,7 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
lvl = 0;
}
#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)) {
for (u32 n = 0; n < seqlen; n++) {
@ -573,10 +847,11 @@ bool ShowUnlockSequence(u32 seqlvl, const char *format, ...) {
}
#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 x, y, yopt;
u32 sel = 0;
int sel = 0, scroll = 0;
int n_show = min(n, 10);
char str[STRBUF_SIZE];
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;
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;
for (u32 i = 0; i < n; i++) if (str_width < GetDrawStringWidth(options[i]))
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]) + (3 * font_width);
x = (str_width >= SCREEN_WIDTH_MAIN) ? 0 : (SCREEN_WIDTH_MAIN - str_width) / 2;
y = (str_height >= SCREEN_HEIGHT) ? 0 : (SCREEN_HEIGHT - str_height) / 2;
yopt = y + GetDrawStringHeight(str) + 8;
ClearScreenF(true, false, COLOR_STD_BG);
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) {
for (u32 i = 0; i < n; i++) {
DrawStringF(MAIN_SCREEN, x, yopt + ((line_height+2)*i), (sel == i) ? COLOR_STD_FONT : COLOR_LIGHTGREY, COLOR_STD_BG, "%2.2s %s",
for (int i = scroll; i < scroll+n_show; i++) {
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]);
}
// 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);
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_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_B) {
sel = n;
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);
@ -619,12 +928,12 @@ u32 ShowSelectPrompt(u32 n, const char** options, const char *format, ...) {
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 x, y, yopt;
const u32 item_width = SCREEN_WIDTH(MAIN_SCREEN) - 40;
int sel = 0, scroll = 0;
u32 n_show = min(n, 10);
int n_show = min(n, 10);
char str[STRBUF_SIZE];
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);
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) {
for (u32 i = scroll; i < scroll+n_show; i++) {
for (int i = scroll; i < scroll+n_show; i++) {
char bytestr[16];
FormatBytes(bytestr, options[i]->size);
char content_str[64 + 1];
char content_str[UTF_BUFFER_BYTESIZE(fname_len)];
char temp_str[256];
strncpy(temp_str, options[i]->name, 255);
strncpy(temp_str, options[i]->name, 256);
char* dot = strrchr(temp_str, '.');
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);
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 == (int)i) ? "->" : "", content_str);
(sel == i) ? COLOR_STD_FONT : COLOR_ENTRY(options[i]), COLOR_STD_BG, "%2.2s %s",
(sel == i) ? "->" : "", content_str);
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",
(options[i]->type == T_DIR) ? "(dir)" : (options[i]->type == T_DOTDOT) ? "(..)" : bytestr);
(sel == i) ? COLOR_STD_FONT : COLOR_ENTRY(options[i]), COLOR_STD_BG, "%10.10s",
(options[i]->type == T_DIR) ? STR_DIR : (options[i]->type == T_DOTDOT) ? "(..)" : bytestr);
}
// show [n more]
if (n - n_show - scroll) {
char more_str[64 + 1];
snprintf(more_str, 64, " [%d more]", (int)(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);
if (n - n_show - scroll > 0) {
char more_str[UTF_BUFFER_BYTESIZE(item_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, 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
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);
u32 pad_state = InputWait(0);
if (pad_state & BUTTON_DOWN) sel++;
else if (pad_state & BUTTON_UP) sel--;
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_RIGHT) sel += n_show;
else if (pad_state & BUTTON_LEFT) sel -= n_show;
else if (pad_state & BUTTON_A) break;
@ -698,15 +1008,15 @@ u32 ShowFileScrollPrompt(u32 n, const DirEntry** options, bool hide_ext, const c
break;
}
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;
else if (sel == (int) n-1 && sel >= (int)(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 == 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;
}
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, ...) {
@ -723,7 +1033,7 @@ u32 ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *f
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 \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);
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) {
const u32 alphabet_size = strnlen(alphabet, 256);
const u32 input_shown = 22;
const u32 input_shown_length = 22;
const u32 fast_scroll = 4;
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);
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,
"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
while (HID_ReadState() & BUTTON_ANY);
@ -785,25 +1095,43 @@ bool ShowInputPrompt(char* inputstr, u32 max_size, u32 resize, const char* alpha
while (true) {
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;
while (scroll && (inputstr_size - scroll < input_shown)) scroll--;
DrawStringF(MAIN_SCREEN, x, y + str_height - 76, COLOR_STD_FONT, COLOR_STD_BG, "%c%-*.*s%c%-*.*s\n%-*.*s^%-*.*s",
if (cursor_s < scroll) {
scroll = cursor_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) ? '<' : '|',
(inputstr_size > input_shown) ? input_shown : inputstr_size,
(inputstr_size > input_shown) ? input_shown : inputstr_size,
inputstr + scroll,
(inputstr_size - scroll > input_shown) ? '>' : '|',
(inputstr_size > input_shown) ? 0 : input_shown - inputstr_size,
(inputstr_size > input_shown) ? 0 : input_shown - inputstr_size,
(int) input_shown_size,
(int) input_shown_size,
(scroll > inputstr_size) ? "" : inputstr + scroll,
(inputstr_size - (s32) scroll > input_shown_size) ? '>' : '|',
(int) 1 + cpos,
(int) 1 + cpos,
"",
1 + cursor_s - scroll,
1 + cursor_s - scroll,
"",
input_shown - (cursor_s - scroll),
input_shown - (cursor_s - scroll),
(int) input_shown_length - cpos,
(int) input_shown_length - cpos,
""
);
if (cursor_a < 0) {
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) {
if (resize && (inputstr_size > resize)) {
char* inputfrom = inputstr + cursor_s - (cursor_s % resize) + resize;
char* inputto = inputstr + cursor_s - (cursor_s % resize);
u32 char_index = 0;
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));
inputstr_size -= resize;
while (cursor_s >= inputstr_size)
inputstr_size -= inputfrom - inputto;
while (cursor_s >= inputstr_size || (inputstr[cursor_s] & 0xC0) == 0x80)
cursor_s--;
cursor_a = -1;
} else if (resize == 1) {
@ -864,18 +1207,29 @@ bool ShowInputPrompt(char* inputstr, u32 max_size, u32 resize, const char* alpha
cursor_a = 0;
}
} 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 = cursor_a % alphabet_size;
inputstr[cursor_s] = alphabet[cursor_a];
} 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;
if (cursor_a < 0) cursor_a = alphabet_size + cursor_a;
inputstr[cursor_s] = alphabet[cursor_a];
} 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;
} 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) {
memset(inputstr + cursor_s, alphabet[0], resize);
inputstr[cursor_s + resize] = '\0';
@ -911,7 +1265,7 @@ u64 ShowHexPrompt(u64 start_val, u32 n_digits, const char *format, ...) {
va_list va;
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);
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;
va_list va;
snprintf(inputstr, 20 + 1, "%llu", start_val);
snprintf(inputstr, sizeof(inputstr), "%llu", start_val);
va_start(va, format);
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;
u32 prog_width = ((total > 0) && (current <= total)) ? (current * (bar_width-4)) / total : 0;
u32 prog_percent = ((total > 0) && (current <= total)) ? (current * 100) / total : 0;
char tempstr[64];
char progstr[64];
char tempstr[UTF_BUFFER_BYTESIZE(64)];
char progstr[UTF_BUFFER_BYTESIZE(64)];
static u64 last_msec_elapsed = 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);
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);
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) {
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);
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;
@ -1095,13 +1453,7 @@ int ShowBrightnessConfig(int set_brightness)
u32 btn_input, bar_count;
int bar_x_pos, bar_y_pos, bar_width, bar_height;
const char *brightness_str =
"[\x1B] Decrease brightness\n"
"[\x1A] Increase brightness\n"
" \n"
"[X] Use volume slider control\n"
"[A] Set current brightness\n"
"[B] Cancel";
const char *brightness_str = STR_BRIGHTNESS_CONTROLS;
static const u16 brightness_slider_colmasks[] = {
COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE
};
@ -1122,7 +1474,7 @@ int ShowBrightnessConfig(int set_brightness)
// draw initial UI stuff
DrawStringF(MAIN_SCREEN,
(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
for (int x = 0; x < bar_width; x++) {

View File

@ -21,6 +21,11 @@
#define FONT_WIDTH_EXT GetFontWidth()
#define FONT_HEIGHT_EXT GetFontHeight()
#define UTF_MAX_BYTES_PER_RUNE 4
#define UTF_BUFFER_BYTESIZE(rune_count) (((rune_count) + 1) * UTF_MAX_BYTES_PER_RUNE)
#define PRINTF_ARGS(n) __attribute__ ((format (printf, (n), (n) + 1)))
#define TOP_SCREEN ((u16*)VRAM_TOP_LA)
#define BOT_SCREEN ((u16*)VRAM_BOT_A)
@ -40,13 +45,14 @@
#ifndef AUTO_UNLOCK
bool ShowUnlockSequence(u32 seqlvl, const char *format, ...);
bool PRINTF_ARGS(2) ShowUnlockSequence(u32 seqlvl, const char *format, ...);
#else
#define ShowUnlockSequence ShowPrompt
#endif
u8* GetFontFromPbm(const void* pbm, const u32 pbm_size, u32* w, u32* h);
bool SetFontFromPbm(const void* pbm, const u32 pbm_size);
const u8* GetFontFromPbm(const void* pbm, const u32 riff_size, u32* w, u32* h);
const u8* GetFontFromRiff(const void* riff, const u32 riff_size, u32* w, u32* h, u16* count);
bool SetFont(const void* font, const u32 font_size);
u16 GetColor(const u16 *screen, int x, int y);
@ -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 DrawQrCode(u16 *screen, const u8* qrcode);
void DrawCharacter(u16 *screen, int 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 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 DrawCharacter(u16 *screen, u32 character, int x, int y, u32 color, u32 bgcolor);
void DrawString(u16 *screen, const char *str, int x, int y, u32 color, u32 bgcolor);
void PRINTF_ARGS(6) DrawStringF(u16 *screen, int x, int y, u32 color, u32 bgcolor, const char *format, ...);
void PRINTF_ARGS(4) DrawStringCenter(u16 *screen, u32 color, u32 bgcolor, const char *format, ...);
u32 GetCharSize(const char* str);
u32 GetPrevCharSize(const char* str);
u32 GetDrawStringHeight(const char* str);
u32 GetDrawStringWidth(const char* str);
u32 GetFontWidth(void);
u32 GetFontHeight(void);
void MultiLineString(char* dest, const char* orig, int llen, int maxl);
void WordWrapString(char* str, int llen);
void ResizeString(char* dest, const char* orig, int nsize, int tpos, bool align_right);
void TruncateString(char* dest, const char* orig, int nsize, int tpos);
void ResizeString(char* dest, const char* orig, int nlength, int tpos, bool align_right);
void TruncateString(char* dest, const char* orig, int nlength, int tpos);
void FormatNumber(char* str, u64 number);
void FormatBytes(char* str, u64 bytes);
void ShowString(const char *format, ...);
void ShowStringF(u16* screen, const char *format, ...);
void ShowIconString(u16* icon, int w, int h, const char *format, ...);
void ShowIconStringF(u16* screen, u16* icon, int w, int h, const char *format, ...);
bool ShowPrompt(bool ask, const char *format, ...);
u32 ShowSelectPrompt(u32 n, const char** options, const char *format, ...);
u32 ShowFileScrollPrompt(u32 n, const DirEntry** entries, bool hide_ext, const char *format, ...);
u32 ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *format, ...);
bool ShowStringPrompt(char* inputstr, u32 max_size, const char *format, ...);
u64 ShowHexPrompt(u64 start_val, u32 n_digits, const char *format, ...);
u64 ShowNumberPrompt(u64 start_val, const char *format, ...);
bool ShowDataPrompt(u8* data, u32* size, const char *format, ...);
bool ShowRtcSetterPrompt(void* time, const char *format, ...);
void PRINTF_ARGS(1) ShowString(const char *format, ...);
void PRINTF_ARGS(2) ShowStringF(u16* screen, const char *format, ...);
void PRINTF_ARGS(4) ShowIconString(u16* icon, int w, int h, const char *format, ...);
void PRINTF_ARGS(5) ShowIconStringF(u16* screen, u16* icon, int w, int h, const char *format, ...);
bool PRINTF_ARGS(2) ShowPrompt(bool ask, const char *format, ...);
u32 PRINTF_ARGS(3) ShowSelectPrompt(int n, const char** options, const char *format, ...);
u32 PRINTF_ARGS(4) ShowFileScrollPrompt(int n, const DirEntry** entries, bool hide_ext, const char *format, ...);
u32 PRINTF_ARGS(4) ShowHotkeyPrompt(u32 n, const char** options, const u32* keys, const char *format, ...);
bool PRINTF_ARGS(3) ShowStringPrompt(char* inputstr, u32 max_size, const char *format, ...);
u64 PRINTF_ARGS(3) ShowHexPrompt(u64 start_val, u32 n_digits, const char *format, ...);
u64 PRINTF_ARGS(2) ShowNumberPrompt(u64 start_val, const char *format, ...);
bool PRINTF_ARGS(3) ShowDataPrompt(u8* data, u32* size, const char *format, ...);
bool PRINTF_ARGS(2) ShowRtcSetterPrompt(void* time, const char *format, ...);
bool ShowProgress(u64 current, u64 total, const char* opstr);
int ShowBrightnessConfig(int set_brightness);

View File

@ -7,7 +7,7 @@
// see: https://github.com/TASVideos/desmume/blob/master/desmume/src/bios.cpp#L1070tions
u16 crc16_quick(const void* src, u32 len) {
const u16 tabval[] = { CRC16_TABVAL };
static const u16 tabval[] = { CRC16_TABVAL };
u16* data = (u16*) src;
u16 crc = 0xFFFF;

View File

@ -40,10 +40,13 @@ u32 GetUnitKeysType(void)
}
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 };
memcpy(ctr, (void*) info, 12); // CTR -> slot + type + id + zeroes
setup_aeskeyY(0x2C, zeroes);
setup_aeskeyY(0x2C, aeskeyY);
use_aeskey(0x2C);
set_ctr(ctr);
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
if (!found && (type != 'I')) {
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 : "");
found = (LoadSupportFile(fname, key, 16) == 16);
}
@ -226,25 +229,3 @@ u32 InitKeyDb(const char* path)
free(keydb);
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
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;
}

View File

@ -30,4 +30,3 @@ u32 GetUnitKeysType(void);
void CryptAesKeyInfo(AesKeyInfo* info);
u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id);
u32 InitKeyDb(const char* path);
u32 CheckRecommendedKeyDb(const char* path);

View File

@ -1,5 +1,6 @@
#include "filetype.h"
#include "fsutil.h"
#include "image.h"
#include "fatmbr.h"
#include "nand.h"
#include "game.h"
@ -7,20 +8,21 @@
#include "keydb.h"
#include "ctrtransfer.h"
#include "scripting.h"
#include "gm9lua.h"
#include "png.h"
#include "ui.h" // only for font file detection
u64 IdentifyFileType(const char* path) {
const u8 romfs_magic[] = { ROMFS_MAGIC };
const u8 diff_magic[] = { DIFF_MAGIC };
const u8 disa_magic[] = { DISA_MAGIC };
const u8 tickdb_magic[] = { TICKDB_MAGIC };
const u8 smdh_magic[] = { SMDH_MAGIC };
const u8 threedsx_magic[] = { THREEDSX_EXT_MAGIC };
const u8 png_magic[] = { PNG_MAGIC };
static const u8 romfs_magic[] = { ROMFS_MAGIC };
static const u8 diff_magic[] = { DIFF_MAGIC };
static const u8 disa_magic[] = { DISA_MAGIC };
static const u8 tickdb_magic[] = { TICKDB_MAGIC };
static const u8 smdh_magic[] = { SMDH_MAGIC };
static const u8 threedsx_magic[] = { THREEDSX_EXT_MAGIC };
static const u8 png_magic[] = { PNG_MAGIC };
if (!path) return 0; // safety
u8 ALIGN(32) header[0x200]; // minimum required size
u8 ALIGN(32) header[0x2C0]; // minimum required size
void* data = (void*) header;
size_t fsize = FileGetSize(path);
char* fname = strrchr(path, '/');
@ -36,7 +38,7 @@ u64 IdentifyFileType(const char* path) {
} else {
ext = "";
}
if (FileGetData(path, header, 0x200, 0) < min(0x200, fsize)) return 0;
if (FileGetData(path, header, 0x2C0, 0) < min(0x2C0, fsize)) return 0;
if (!fsize) return 0;
if (fsize >= 0x200) {
@ -85,11 +87,14 @@ u64 IdentifyFileType(const char* path) {
return GAME_ROMFS; // RomFS file (check could be better)
} else if (ValidateTmd((TitleMetaData*) data) == 0) {
if (fsize == TMD_SIZE_N(getbe16(header + 0x1DE)) + TMD_CDNCERT_SIZE)
return GAME_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)))
return GAME_TMD; // TMD file
} else if (ValidateTwlTmd((TitleMetaData*) data) == 0) {
if (fsize == TMD_SIZE_TWL + TMD_CDNCERT_SIZE)
return GAME_TWLTMD; // TMD file from NUS/CDN (TWL)
} else if (ValidateTicket((Ticket*) data) == 0) {
return GAME_TICKET; // Ticket file (not used for anything right now)
return GAME_TICKET; // Ticket file
} else if (ValidateFirmHeader((FirmHeader*) data, fsize) == 0) {
return SYS_FIRM; // FIRM file
} else if ((ValidateAgbSaveHeader((AgbSaveHeader*) data) == 0) && (fsize >= AGBSAVE_MAX_SIZE)) {
@ -112,8 +117,21 @@ u64 IdentifyFileType(const char* path) {
}
}
if (GetFontFromPbm(data, fsize, NULL, NULL)) {
if (fsize == sizeof(TitleInfoEntry) && (strncasecmp(path, "T:/", 3) == 0)) {
const char* mntpath = GetMountPath();
if (mntpath && *mntpath) {
if ((strncasecmp(mntpath, "1:/dbs/title.db", 16) == 0) ||
(strncasecmp(mntpath, "4:/dbs/title.db", 16) == 0) ||
(strncasecmp(mntpath, "A:/dbs/title.db", 16) == 0) ||
(strncasecmp(mntpath, "B:/dbs/title.db", 16) == 0))
return GAME_TIE;
}
} else if (GetFontFromPbm(data, fsize, NULL, NULL)) {
return FONT_PBM;
} else if (GetFontFromRiff(data, fsize, NULL, NULL, NULL)) {
return FONT_RIFF;
} else if (GetLanguage(data, fsize, NULL, NULL, NULL)) {
return TRANSLATION;
} else if ((fsize > sizeof(AgbHeader)) &&
(ValidateAgbHeader((AgbHeader*) data) == 0)) {
return GAME_GBA;
@ -124,7 +142,7 @@ u64 IdentifyFileType(const char* path) {
(memcmp(data, threedsx_magic, sizeof(threedsx_magic)) == 0)) {
return GAME_3DSX; // 3DSX (executable) file
} else if ((fsize > sizeof(CmdHeader)) &&
CheckCmdSize((CmdHeader*) data, fsize) == 0) {
(CMD_SIZE((CmdHeader*) data) == fsize)) {
return GAME_CMD; // CMD file
} else if ((fsize > sizeof(NcchInfoHeader)) &&
(GetNcchInfoVersion((NcchInfoHeader*) data)) &&
@ -133,14 +151,6 @@ u64 IdentifyFileType(const char* path) {
} else if ((strncasecmp(ext, "png", 4) == 0) &&
(fsize > sizeof(png_magic)) && (memcmp(data, png_magic, sizeof(png_magic)) == 0)) {
return GFX_PNG;
} else if ((strncasecmp(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) {
return BIN_TIKDB | FLAG_ENC; // titlekey database / encrypted
} else if (strncasecmp(fname, TIKDB_NAME_DEC, sizeof(TIKDB_NAME_DEC)+1) == 0) {
@ -149,10 +159,16 @@ u64 IdentifyFileType(const char* path) {
return BIN_KEYDB; // key database
} else if ((sscanf(fname, "slot%02lXKey", &id) == 1) && (strncasecmp(ext, "bin", 4) == 0) && (fsize = 16) && (id < 0x40)) {
return BIN_LEGKEY; // legacy key file
} else if ((strncmp((char*) data, CIFINISH_MAGIC, strlen(CIFINISH_MAGIC)) == 0) &&
(fsize == CIFINISH_SIZE((void*) data)) && (fsize > sizeof(CifinishHeader))) {
return BIN_CIFNSH;
} else if (ValidateText((char*) data, (fsize > 0x200) ? 0x200 : fsize)) {
u64 type = 0;
if ((fsize < SCRIPT_MAX_SIZE) && (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)
// this should check if it's compiled lua bytecode (done with luac), which is NOT text
else if ((fsize < LUASCRIPT_MAX_SIZE) && (strcasecmp(ext, LUASCRIPT_EXT) == 0))
type |= TXT_LUA;
if (fsize < STD_BUFFER_SIZE) type |= TXT_GENERIC;
return type;
} else if ((strncmp(path + 2, "/Nintendo DSiWare/", 18) == 0) &&
@ -166,12 +182,12 @@ u64 IdentifyFileType(const char* path) {
char* name_cdn = path_cdn + (fname - path);
strncpy(path_cdn, path, 256);
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)
return GAME_NUSCDN; // NUS/CDN type 1
return GAME_NUSCDN; // NUS/CDN, recognized by TMD
strncpy(name_cdn, "cetk", 5);
if (FileGetSize(path_cdn) > 0)
return GAME_NUSCDN; // NUS/CDN type 1
return GAME_NUSCDN; // NUS/CDN, recognized by CETK
}
return 0;

View File

@ -8,55 +8,65 @@
#define GAME_NCSD (1ULL<<3)
#define GAME_NCCH (1ULL<<4)
#define GAME_TMD (1ULL<<5)
#define GAME_CMD (1ULL<<6)
#define GAME_EXEFS (1ULL<<7)
#define GAME_ROMFS (1ULL<<8)
#define GAME_BOSS (1ULL<<9)
#define GAME_NUSCDN (1ULL<<10)
#define GAME_TICKET (1ULL<<11)
#define GAME_SMDH (1ULL<<12)
#define GAME_3DSX (1ULL<<13)
#define GAME_NDS (1ULL<<14)
#define GAME_GBA (1ULL<<15)
#define GAME_TAD (1ULL<<16)
#define SYS_FIRM (1ULL<<17)
#define SYS_DIFF (1ULL<<18)
#define SYS_DISA (1ULL<<19)
#define SYS_AGBSAVE (1ULL<<20)
#define SYS_TICKDB (1ULL<<21)
#define BIN_NCCHNFO (1ULL<<22)
#define BIN_TIKDB (1ULL<<23)
#define BIN_KEYDB (1ULL<<24)
#define BIN_LEGKEY (1ULL<<25)
#define TXT_SCRIPT (1ULL<<26)
#define TXT_GENERIC (1ULL<<27)
#define GFX_PNG (1ULL<<28)
#define FONT_PBM (1ULL<<29)
#define NOIMG_NAND (1ULL<<30)
#define HDR_NAND (1ULL<<31)
#define TYPE_BASE 0xFFFFFFFFULL // 32 bit reserved for base types
#define GAME_CDNTMD (1ULL<<6)
#define GAME_TWLTMD (1ULL<<7)
#define GAME_CMD (1ULL<<8)
#define GAME_EXEFS (1ULL<<9)
#define GAME_ROMFS (1ULL<<10)
#define GAME_BOSS (1ULL<<11)
#define GAME_NUSCDN (1ULL<<12)
#define GAME_TICKET (1ULL<<13)
#define GAME_TIE (1ULL<<14)
#define GAME_SMDH (1ULL<<15)
#define GAME_3DSX (1ULL<<16)
#define GAME_NDS (1ULL<<17)
#define GAME_GBA (1ULL<<18)
#define GAME_TAD (1ULL<<19)
#define SYS_FIRM (1ULL<<20)
#define SYS_DIFF (1ULL<<21)
#define SYS_DISA (1ULL<<22)
#define SYS_AGBSAVE (1ULL<<23)
#define SYS_TICKDB (1ULL<<24)
#define BIN_CIFNSH (1ULL<<25)
#define BIN_NCCHNFO (1ULL<<26)
#define BIN_TIKDB (1ULL<<27)
#define BIN_KEYDB (1ULL<<28)
#define BIN_LEGKEY (1ULL<<29)
#define TXT_SCRIPT (1ULL<<30)
#define TXT_GENERIC (1ULL<<31)
#define GFX_PNG (1ULL<<32)
#define FONT_PBM (1ULL<<33)
#define FONT_RIFF (1ULL<<34)
#define NOIMG_NAND (1ULL<<35)
#define HDR_NAND (1ULL<<36)
#define TRANSLATION (1ULL<<37)
#define TXT_LUA (1ULL<<38)
#define TYPE_BASE 0xFFFFFFFFFFULL // 40 bit reserved for base types
// #define FLAG_FIRM (1ULL<<57) // <--- for CXIs containing FIRMs
// #define FLAG_GBAVC (1ULL<<58) // <--- for GBAVC CXIs
#define FLAG_DSIW (1ULL<<59)
#define FLAG_ENC (1ULL<<60)
#define FLAG_CTR (1ULL<<61)
#define FLAG_NUSCDN (1ULL<<62)
// #define FLAG_FIRM (1ULL<<58) // <--- for CXIs containing FIRMs
// #define FLAG_GBAVC (1ULL<<59) // <--- for GBAVC CXIs
#define FLAG_DSIW (1ULL<<60)
#define FLAG_ENC (1ULL<<61)
#define FLAG_CTR (1ULL<<62)
#define FLAG_CXI (1ULL<<63)
#define FTYPE_MOUNTABLE(tp) (tp&(IMG_FAT|IMG_NAND|GAME_CIA|GAME_NCSD|GAME_NCCH|GAME_EXEFS|GAME_ROMFS|GAME_NDS|GAME_TAD|SYS_FIRM|SYS_DIFF|SYS_DISA|SYS_TICKDB|BIN_KEYDB))
#define FTYPE_VERIFICABLE(tp) (tp&(IMG_NAND|GAME_CIA|GAME_NCSD|GAME_NCCH|GAME_TMD|GAME_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_ENCRYPTABLE(tp) (tp&(GAME_CIA|GAME_NCSD|GAME_NCCH|GAME_BOSS|BIN_KEYDB))
#define FTYPE_CIABUILD(tp) ((tp&(GAME_NCSD|GAME_NCCH|GAME_TMD)) || ((tp&GAME_NDS)&&(tp&(FLAG_DSIW))))
#define FTYPE_CIABUILD_L(tp) (FTYPE_CIABUILD(tp) && (tp&(GAME_TMD)))
#define FTYPE_CXIDUMP(tp) (tp&(GAME_TMD))
#define FTYPE_CIABUILD(tp) ((tp&(GAME_NCSD|GAME_NCCH|GAME_TMD|GAME_CDNTMD|GAME_TWLTMD|GAME_TIE|GAME_TAD)) || ((tp&GAME_NDS)&&(tp&(FLAG_DSIW))))
#define FTYPE_CIABUILD_L(tp) (tp&(GAME_TMD|GAME_CDNTMD|GAME_TIE|GAME_TAD))
#define FTYPE_CIAINSTALL(tp) ((tp&(GAME_NCSD|GAME_NCCH|GAME_CIA|GAME_CDNTMD|GAME_TWLTMD)) || ((tp&GAME_NDS)&&(tp&(FLAG_DSIW))))
#define FTYPE_TIKINSTALL(tp) (tp&(GAME_TICKET))
#define FTYPE_CIFINSTALL(tp) (tp&(BIN_CIFNSH))
#define FTYPE_TIKDUMP(tp) (tp&(GAME_TIE))
#define FTYPE_CXIDUMP(tp) (tp&(GAME_TMD|GAME_TIE))
#define FTYPE_UNINSTALL(tp) (tp&(GAME_TIE))
#define FTYPE_TIKBUILD(tp) (tp&(GAME_TICKET|SYS_TICKDB|BIN_TIKDB))
#define FTYPE_KEYBUILD(tp) (tp&(BIN_KEYDB|BIN_LEGKEY))
#define FTYPE_TITLEINFO(tp) (tp&(GAME_SMDH|GAME_NCCH|GAME_NCSD|GAME_CIA|GAME_TMD|GAME_NDS|GAME_GBA|GAME_TAD|GAME_3DSX))
#define FTYPE_CIACHECK(tp) (tp&GAME_CIA)
#define FTYPE_TITLEINFO(tp) (tp&(GAME_TIE|GAME_CIA|GAME_TMD|GAME_CDNTMD|GAME_TWLTMD))
#define FTYPE_RENAMABLE(tp) (tp&(GAME_NCCH|GAME_NCSD|GAME_CIA|GAME_NDS|GAME_GBA))
#define FTYPE_TRIMABLE(tp) (tp&(IMG_NAND|GAME_NCCH|GAME_NCSD|GAME_NDS|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_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))
@ -68,9 +78,11 @@
#define FTYPE_KEYINIT(tp) (tp&(BIN_KEYDB))
#define FTYPE_KEYINSTALL(tp) (tp&(BIN_KEYDB))
#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_SETABLE(tp) (tp&(FONT_PBM))
#define FTYPE_SETABLE(tp) (tp&(FONT_PBM|FONT_RIFF|TRANSLATION))
#define FTYPE_BOOTABLE(tp) (tp&(SYS_FIRM))
#define FTYPE_INSTALLABLE(tp) (tp&(SYS_FIRM))
#define FTYPE_AGBSAVE(tp) (tp&(SYS_AGBSAVE))

View File

@ -1,6 +1,7 @@
#include "fsdrive.h"
#include "fsgame.h"
#include "fsinit.h"
#include "language.h"
#include "virtual.h"
#include "vcart.h"
#include "sddata.h"
@ -11,7 +12,7 @@
// last search pattern, path & mode
static char search_pattern[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 type = DRV_UNKNOWN;
@ -21,6 +22,8 @@ int DriveType(const char* path) {
type = DRV_FAT | DRV_ALIAS | ((*path == 'A') ? DRV_SYSNAND : DRV_EMUNAND);
} else if (*search_pattern && *search_path && (strncmp(path, "Z:", 3) == 0)) {
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)) {
if (pdrv == 0) {
type = DRV_FAT | DRV_SDCARD | DRV_STDFAT;
@ -46,13 +49,13 @@ int DriveType(const char* path) {
type = DRV_VIRTUAL | DRV_SYSNAND;
} else if (vsrc == VRT_EMUNAND) {
type = DRV_VIRTUAL | DRV_EMUNAND;
} else if ((vsrc == VRT_IMGNAND) || (vsrc == VRT_DISADIFF)) {
} else if ((vsrc == VRT_IMGNAND) || (vsrc == VRT_DISADIFF) || (vsrc == VRT_BDRI)) {
type = DRV_VIRTUAL | DRV_IMAGE;
} else if (vsrc == VRT_XORPAD) {
type = DRV_VIRTUAL | DRV_XORPAD;
} else if (vsrc == VRT_MEMORY) {
type = DRV_VIRTUAL | DRV_MEMORY;
} else if ((vsrc == VRT_GAME) || (vsrc == VRT_KEYDB) || (vsrc == VRT_TICKDB)) {
} else if ((vsrc == VRT_GAME) || (vsrc == VRT_KEYDB)) {
type = DRV_VIRTUAL | DRV_GAME | DRV_IMAGE;
} else if (vsrc == VRT_CART) {
type = DRV_VIRTUAL | DRV_CART;
@ -64,28 +67,31 @@ int DriveType(const char* path) {
return type;
}
void SetFSSearch(const char* pattern, const char* path, bool mode) {
void SetFSSearch(const char* pattern, const char* path) {
if (pattern && path) {
strncpy(search_pattern, pattern, 256);
search_pattern[255] = '\0';
strncpy(search_path, path, 256);
search_path[255] = '\0';
search_title_mode = mode;
} else *search_pattern = *search_path = '\0';
}
void SetTitleManagerMode(bool mode) {
title_manager_mode = mode;
}
bool GetFATVolumeLabel(const char* drv, char* label) {
return (f_getlabel(drv, label, NULL) == FR_OK);
}
bool GetRootDirContentsWorker(DirStruct* contents) {
static const char* drvname[] = { FS_DRVNAME };
const char* drvname[] = { FS_DRVNAME };
static const char* drvnum[] = { FS_DRVNUM };
u32 n_entries = 0;
char sdlabel[DRV_LABEL_LEN];
if (!GetFATVolumeLabel("0:", sdlabel) || !(*sdlabel))
strcpy(sdlabel, "NOLABEL");
strcpy(sdlabel, STR_LAB_NOLABEL);
char carttype[16];
GetVCartTypeString(carttype);
@ -96,15 +102,15 @@ bool GetRootDirContentsWorker(DirStruct* contents) {
if (!DriveType(drvnum[i])) continue; // drive not available
entry->p_name = 4;
entry->name = entry->path + entry->p_name;
memset(entry->path, 0x00, 64);
memset(entry->path, 0x00, 256);
snprintf(entry->path, 4, "%s", drvnum[i]);
if ((*(drvnum[i]) >= '7') && (*(drvnum[i]) <= '9') && !(GetMountState() & IMG_NAND)) // Drive 7...9 handling
snprintf(entry->name, 32, "[%s] %s", drvnum[i],
(*(drvnum[i]) == '7') ? "FAT IMAGE" :
(*(drvnum[i]) == '8') ? "BONUS DRIVE" :
(*(drvnum[i]) == '9') ? "RAMDRIVE" : "UNK");
snprintf(entry->name, 252, "[%s] %s", drvnum[i],
(*(drvnum[i]) == '7') ? STR_LAB_FAT_IMAGE :
(*(drvnum[i]) == '8') ? STR_LAB_BONUS_DRIVE :
(*(drvnum[i]) == '9') ? STR_LAB_RAMDRIVE : "UNK");
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_NCSD ) ? "NCSD" :
(GetMountState() & GAME_NCCH ) ? "NCCH" :
@ -114,10 +120,10 @@ bool GetRootDirContentsWorker(DirStruct* contents) {
(GetMountState() & SYS_FIRM ) ? "FIRM" :
(GetMountState() & GAME_TAD ) ? "DSIWARE" : "UNK", drvname[i]);
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
snprintf(entry->name, 32, "[%s] %s (%s)", drvnum[i], drvname[i], sdlabel);
else snprintf(entry->name, 32, "[%s] %s", drvnum[i], drvname[i]);
snprintf(entry->name, 252, "[%s] %s (%s)", drvnum[i], drvname[i], sdlabel);
else snprintf(entry->name, 252, "[%s] %s", drvnum[i], drvname[i]);
entry->size = GetTotalSpace(entry->path);
entry->type = T_ROOT;
entry->marked = 0;
@ -151,6 +157,10 @@ bool GetDirContentsWorker(DirStruct* contents, char* fpath, int fnsize, const ch
break;
} else if (!pattern || (fvx_match_name(fname, pattern) == FR_OK)) {
DirEntry* entry = &(contents->entry[contents->n_entries]);
if (contents->n_entries >= MAX_DIR_ENTRIES) {
ret = true; // Too many entries, still okay if we stop here
break;
}
strncpy(entry->path, fpath, 256);
entry->path[255] = '\0';
entry->p_name = fname - fpath;
@ -163,12 +173,8 @@ bool GetDirContentsWorker(DirStruct* contents, char* fpath, int fnsize, const ch
entry->size = fno.fsize;
}
entry->marked = 0;
if (!recursive || (entry->type != T_DIR)) {
if (++(contents->n_entries) >= MAX_DIR_ENTRIES) {
ret = true; // Too many entries, still okay if we stop here
break;
}
}
if (!recursive || (entry->type != T_DIR))
++(contents->n_entries);
}
if (recursive && (fno.fattrib & AM_DIR)) {
if (!GetDirContentsWorker(contents, fpath, fnsize, pattern, recursive))
@ -206,9 +212,12 @@ void SearchDirContents(DirStruct* contents, const char* path, const char* patter
void GetDirContents(DirStruct* contents, const char* path) {
if (*search_path && (DriveType(path) & DRV_SEARCH)) {
ShowString("Searching, please wait...");
ShowString("%s", STR_SEARCHING_PLEASE_WAIT);
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);
} else SearchDirContents(contents, path, NULL, false);
if (*path) SortDirStruct(contents);
@ -223,7 +232,7 @@ uint64_t GetFreeSpace(const char* path)
FATFS* fsobj = GetMountedFSObject(path);
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)
return 0;

View File

@ -25,30 +25,36 @@
#define DRV_VRAM (1UL<<13)
#define DRV_ALIAS (1UL<<14)
#define DRV_BONUS (1UL<<15)
#define DRV_SEARCH (1UL<<16)
#define DRV_STDFAT (1UL<<17) // standard FAT drive without limitations
#define DRV_TITLEMAN (1UL<<16)
#define DRV_SEARCH (1UL<<17)
#define DRV_STDFAT (1UL<<18) // standard FAT drive without limitations
#define DRV_LABEL_LEN (36)
#define FS_DRVNAME \
"SDCARD", \
"SYSNAND CTRNAND", "SYSNAND TWLN", "SYSNAND TWLP", "SYSNAND SD", "SYSNAND VIRTUAL", \
"EMUNAND CTRNAND", "EMUNAND TWLN", "EMUNAND TWLP", "EMUNAND SD", "EMUNAND VIRTUAL", \
"IMGNAND CTRNAND", "IMGNAND TWLN", "IMGNAND TWLP", "IMGNAND VIRTUAL", \
"GAMECART", \
"GAME IMAGE", "AESKEYDB IMAGE", "TICKET.DB IMAGE", "DISA/DIFF IMAGE", \
"MEMORY VIRTUAL", \
"VRAM VIRTUAL", \
"LAST SEARCH" \
STR_LAB_SDCARD, \
STR_LAB_SYSNAND_CTRNAND, STR_LAB_SYSNAND_TWLN, STR_LAB_SYSNAND_TWLP, STR_LAB_SYSNAND_SD, STR_LAB_SYSNAND_VIRTUAL, \
STR_LAB_EMUNAND_CTRNAND, STR_LAB_EMUNAND_TWLN, STR_LAB_EMUNAND_TWLP, STR_LAB_EMUNAND_SD, STR_LAB_EMUNAND_VIRTUAL, \
STR_LAB_IMGNAND_CTRNAND, STR_LAB_IMGNAND_TWLN, STR_LAB_IMGNAND_TWLP, STR_LAB_IMGNAND_VIRTUAL, \
STR_LAB_GAMECART, \
STR_LAB_GAME_IMAGE, STR_LAB_AESKEYDB_IMAGE, STR_LAB_BDRI_IMAGE, STR_LAB_DISA_DIFF_IMAGE, \
STR_LAB_MEMORY_VIRTUAL, \
STR_LAB_VRAM_VIRTUAL, \
STR_LAB_TITLE_MANAGER, \
STR_LAB_LAST_SEARCH
#define FS_DRVNUM \
"0:", "1:", "2:", "3:", "A:", "S:", "4:", "5:", "6:", "B:", "E:", "7:", "8:", "9:", "I:", "C:", "G:", "K:", "T:", "D:", "M:", "V:", "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 **/
int DriveType(const char* path);
/** 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 **/
bool GetFATVolumeLabel(const char* drv, char* label);

View File

@ -1,14 +1,17 @@
#include "fsgame.h"
#include "fsperm.h"
#include "gameutil.h"
#include "language.h"
#include "tie.h"
#include "ui.h"
#include "ff.h"
#include "vff.h"
void SetDirGoodNames(DirStruct* contents) {
void SetupTitleManager(DirStruct* contents) {
char goodname[256];
ShowProgress(0, 0, "");
for (u32 s = 0; s < contents->n_entries; s++) {
DirEntry* entry = &(contents->entry[s]);
// set good name for entry
u32 plen = strnlen(entry->path, 256);
if (!ShowProgress(s+1, contents->n_entries, entry->path)) break;
if ((GetGoodName(goodname, entry->path, false) != 0) ||
@ -17,6 +20,11 @@ void SetDirGoodNames(DirStruct* contents) {
entry->p_name = plen + 1;
entry->name = entry->path + entry->p_name;
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;
if (ask) { // ask for confirmatiom
char oldname_tr[32+1];
char oldname_tr[UTF_BUFFER_BYTESIZE(32)];
char newname_ww[256];
TruncateString(oldname_tr, entry->name, 32, 8);
strncpy(newname_ww, goodname, 256);
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
}

View File

@ -3,5 +3,5 @@
#include "common.h"
#include "fsdir.h"
void SetDirGoodNames(DirStruct* contents);
void SetupTitleManager(DirStruct* contents);
bool GoodRenamer(DirEntry* entry, bool ask);

View File

@ -21,7 +21,7 @@ bool InitExtFS() {
for (u32 i = 1; i < NORM_FS; i++) {
char fsname[8];
snprintf(fsname, 7, "%lu:", i);
snprintf(fsname, sizeof(fsname), "%lu:", i);
if (fs_mounted[i]) continue;
fs_mounted[i] = (f_mount(fs + i, fsname, 1) == FR_OK);
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;
char fsname[8];
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;
}
// deinit virtual filesystem
@ -58,20 +58,20 @@ bool InitImgFS(const char* path) {
else if ((type&IMG_FAT) && (drv_i < NORM_FS - IMGN_FS + 1)) drv_i = NORM_FS - IMGN_FS + 1;
// reinit image filesystem
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);
}
return GetMountState();
}
void DeinitExtFS() {
InitImgFS(NULL);
SetupNandSdDrive(NULL, NULL, NULL, 0);
SetupNandSdDrive(NULL, NULL, NULL, 1);
InitImgFS(NULL);
for (u32 i = NORM_FS - 1; i > 0; i--) {
if (fs_mounted[i]) {
char fsname[8];
snprintf(fsname, 7, "%lu:", i);
snprintf(fsname, sizeof(fsname), "%lu:", i);
f_mount(NULL, fsname, 1);
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++) {
char fsname[8];
snprintf(fsname, 7, "%lu:", i);
snprintf(fsname, sizeof(fsname), "%lu:", i);
if (!fs_mounted[i] || !(type & DriveType(fsname)))
continue;
f_mount(NULL, fsname, 1);

View File

@ -4,6 +4,7 @@
#include "image.h"
#include "unittype.h"
#include "essentials.h"
#include "language.h"
#include "ui.h"
#include "sdmmc.h"
@ -20,7 +21,7 @@
static u32 write_permissions = PERM_BASE;
bool CheckWritePermissions(const char* path) {
char area_name[16];
char area_name[UTF_BUFFER_BYTESIZE(16)];
int drvtype = DriveType(path);
u32 perm;
@ -40,13 +41,13 @@ bool CheckWritePermissions(const char* path) {
// SD card write protection check
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;
}
// check drive type, get permission type
if (drvtype & DRV_SYSNAND) {
u32 perms[] = { PERM_SYS_LVL0, PERM_SYS_LVL1, PERM_SYS_LVL2, PERM_SYS_LVL3 };
static const u32 perms[] = { PERM_SYS_LVL0, PERM_SYS_LVL1, PERM_SYS_LVL2, PERM_SYS_LVL3 };
u32 lvl = (drvtype & (DRV_TWLNAND|DRV_ALIAS|DRV_CTRNAND)) ? 1 : 0;
if (drvtype & (DRV_CTRNAND|DRV_VIRTUAL)) { // check for paths
const char* path_lvl3[] = { PATH_SYS_LVL3 };
@ -63,9 +64,9 @@ bool CheckWritePermissions(const char* path) {
if ((drvtype & DRV_CTRNAND) || (lvl == 2)) lvl = 3;
}
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) {
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;
if (drvtype & DRV_VIRTUAL) { // check for paths
const char* path_lvl1[] = { PATH_EMU_LVL1 };
@ -73,34 +74,34 @@ bool CheckWritePermissions(const char* path) {
if (strncasecmp(path_f, path_lvl1[i], 256) == 0) lvl = 1;
}
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) {
perm = PERM_GAME;
snprintf(area_name, 16, "game images");
snprintf(area_name, sizeof(area_name), "%s", STR_GAME_IMAGES);
} else if (drvtype & DRV_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) {
perm = PERM_VRAM;
snprintf(area_name, 16, "vram0");
snprintf(area_name, sizeof(area_name), "vram0");
} else if (drvtype & DRV_XORPAD) {
perm = PERM_XORPAD;
snprintf(area_name, 16, "XORpads");
snprintf(area_name, sizeof(area_name), "XORpads");
} else if (drvtype & DRV_IMAGE) {
perm = PERM_IMAGE;
snprintf(area_name, 16, "images");
snprintf(area_name, sizeof(area_name), "%s", STR_IMAGES);
} else if (drvtype & DRV_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
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) {
perm = PERM_SDCARD;
snprintf(area_name, 16, "SD card");
snprintf(area_name, sizeof(area_name), "%s", STR_SD_CARD);
} else if (drvtype & DRV_RAMDRIVE) {
perm = PERM_RAMDRIVE;
snprintf(area_name, 16, "RAM drive");
snprintf(area_name, sizeof(area_name), "%s", STR_RAM_DRIVE);
} else {
return false;
}
@ -112,19 +113,19 @@ bool CheckWritePermissions(const char* path) {
// offer unlock if possible
if (!(perm & (PERM_VRAM|PERM_GAME|PERM_XORPAD))) {
// 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 SetWritePermissions(perm, true);
}
// 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;
}
bool CheckDirWritePermissions(const char* path) {
const char* path_chk[] = { PATH_SYS_LVL3, PATH_SYS_LVL2, PATH_SYS_LVL1, PATH_EMU_LVL1 };
static const char* path_chk[] = { PATH_SYS_LVL3, PATH_SYS_LVL2, PATH_SYS_LVL1, PATH_EMU_LVL1 };
for (u32 i = 0; i < sizeof(path_chk) / sizeof(char*); i++) {
const char* path_cmp = path_chk[i];
u32 p = 0;
@ -144,65 +145,65 @@ bool SetWritePermissions(u32 perm, bool add_perm) {
switch (perm) {
case PERM_BASE:
if (!ShowUnlockSequence(1, "You want to enable base\nwriting permissions."))
if (!ShowUnlockSequence(1, "%s", STR_ENABLE_BASE_WRITE))
return false;
break;
case PERM_SDCARD:
if (!ShowUnlockSequence(1, "You want to enable SD card\nwriting permissions."))
if (!ShowUnlockSequence(1, "%s", STR_ENABLE_SD_WRITE))
return false;
break;
case PERM_IMAGE:
if (!ShowUnlockSequence(1, "You want to enable image\nwriting permissions."))
if (!ShowUnlockSequence(1, "%s", STR_ENABLE_IMAGE_WRITE))
return false;
break;
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;
break;
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;
break;
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;
break;
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;
break;
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."))
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."))
if (!ShowUnlockSequence(2, "%s", STR_ENABLE_SYSNAND_1_WRITE))
return false;
break;
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;
break;
#ifndef SAFEMODE
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;
break;
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;
break;
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;
break;
default:
ShowPrompt(false, "Unlock write permission is not allowed.");
ShowPrompt(false, "%s", STR_UNLOCK_WRITE_NOT_ALLOWED);
return false;
break;
#else
default:
ShowPrompt(false, "Can't unlock write permission.\nTry GodMode9 instead!");
ShowPrompt(false, "%s", STR_CANT_UNLOCK_WRITE_TRY_GODMODE9);
return false;
break;
#endif

View File

@ -21,7 +21,7 @@
#define PERM_BASE (PERM_SDCARD | PERM_IMAGE | PERM_RAMDRIVE | PERM_EMU_LVL0 | PERM_SYS_LVL0)
// 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_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))

View File

@ -11,9 +11,10 @@
#include "ff.h"
#include "ui.h"
#include "swkbd.h"
#include "language.h"
#define SKIP_CUR (1UL<<10)
#define OVERWRITE_CUR (1UL<<11)
#define SKIP_CUR (1UL<<11)
#define OVERWRITE_CUR (1UL<<12)
#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
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;
}
// Write protection check
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;
}
@ -67,15 +68,15 @@ bool FormatSDCard(u64 hidden_mb, u32 cluster_size, const char* label) {
// one last warning....
// 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;
ShowString("Formatting SD, please wait...");
ShowString("%s", STR_FORMATTING_SD_PLEASE_WAIT);
// write the MBR to disk
// !this assumes a fully deinitialized file system!
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)))) {
ShowPrompt(false, "Error: SD card i/o failure");
ShowPrompt(false, "%s", STR_ERROR_SD_CARD_IO_FAILURE);
return false;
}
@ -104,9 +105,9 @@ bool FormatSDCard(u64 hidden_mb, u32 cluster_size, const char* label) {
}
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;
ShowString("Formatting drive, please wait...");
ShowString("%s", STR_FORMATTING_DRIVE_PLEASE_WAIT);
if (GetMountState() & IMG_NAND) InitImgFS(NULL);
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 ((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);
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);
if (fx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return false;
@ -161,7 +162,7 @@ size_t FileGetSize(const char* path) {
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;
FIL file;
u64 fsize;
@ -179,7 +180,7 @@ bool FileGetSha256(const char* path, u8* sha256, u64 offset, u64 size) {
if (!buffer) return false;
ShowProgress(0, 0, path);
sha_init(SHA256_MODE);
sha_init(sha1 ? SHA1_MODE : SHA256_MODE);
for (u64 pos = 0; (pos < size) && ret; pos += bufsiz) {
UINT read_bytes = min(bufsiz, size - pos);
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_get(sha256);
sha_get(hash);
fvx_close(&file);
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 (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;
}
@ -269,12 +270,12 @@ bool FileInjectFile(const char* dest, const char* orig, u64 off_dest, u64 off_or
// check file limits
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(&ofile);
return false;
} 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(&ofile);
return false;
@ -295,8 +296,8 @@ bool FileInjectFile(const char* dest, const char* orig, u64 off_dest, u64 off_or
ret = false;
if (ret && !ShowProgress(pos + bytes_read, size, orig)) {
if (flags && (*flags & NO_CANCEL)) {
ShowPrompt(false, "Cancel is not allowed here");
} else ret = !ShowPrompt(true, "B button detected. Cancel?");
ShowPrompt(false, "%s", STR_CANCEL_IS_NOT_ALLOWED_HERE);
} else ret = !ShowPrompt(true, "%s", STR_B_DETECTED_CANCEL);
ShowProgress(0, 0, 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
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);
return false;
}
@ -345,8 +346,8 @@ bool FileSetByte(const char* dest, u64 offset, u64 size, u8 fillbyte, u32* flags
ret = false;
if (ret && !ShowProgress(pos + bytes_written, size, dest)) {
if (flags && (*flags & NO_CANCEL)) {
ShowPrompt(false, "Cancel is not allowed here");
} else ret = !ShowPrompt(true, "B button detected. Cancel?");
ShowPrompt(false, "%s", STR_CANCEL_IS_NOT_ALLOWED_HERE);
} else ret = !ShowPrompt(true, "%s", STR_B_DETECTED_CANCEL);
ShowProgress(0, 0, 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) {
char npath[256]; // 256 is the maximum length of a full path
if (!CheckWritePermissions(cpath)) return false;
if (filename) snprintf(npath, 255, "%s/%s", cpath, filename);
else snprintf(npath, 255, "%s", cpath);
if (filename) snprintf(npath, sizeof(npath), "%s/%s", cpath, filename);
else snprintf(npath, sizeof(npath), "%s", cpath);
// create dummy file (fail if already existing)
// 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) {
char npath[256]; // 256 is the maximum length of a full path
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;
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 append = (flags && (*flags & APPEND_ALL));
bool calcsha = (flags && (*flags & CALC_SHA) && !append);
bool sha1 = (flags && (*flags & USE_SHA1));
bool ret = false;
// 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
// path string (for output)
char deststr[36 + 1];
char deststr[UTF_BUFFER_BYTESIZE(36)];
TruncateString(deststr, dest, 36, 8);
// the copy process takes place here
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);
}
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);
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;
}
// create the destination folder if it does not already exist
if (fvx_opendir(&pdir, 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;
}
} 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
if (fvx_stat(dest, &fno) != FR_OK) return false;
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;
}
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)) &&
(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);
return false;
}
@ -543,7 +545,7 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
osize = fvx_size(&ofile);
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 (!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;
}
@ -552,7 +554,7 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
fvx_lseek(&ofile, 0);
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) {
UINT bytes_read = 0;
UINT bytes_written = 0;
@ -565,8 +567,8 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
u64 total = osize;
if (ret && !ShowProgress(current, total, orig)) {
if (flags && (*flags & NO_CANCEL)) {
ShowPrompt(false, "%s\nCancel is not allowed here", deststr);
} else ret = !ShowPrompt(true, "%s\nB button detected. Cancel?", deststr);
ShowPrompt(false, "%s\n%s", deststr, STR_CANCEL_IS_NOT_ALLOWED_HERE);
} else ret = !ShowPrompt(true, "%s\n%s", deststr, STR_B_DETECTED_CANCEL);
ShowProgress(0, 0, 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))) {
fvx_unlink(dest);
} else if (!to_virtual && calcsha) {
u8 sha256[0x20];
u8 hash[0x20];
char* ext_sha = dest + strnlen(dest, 256);
strncpy(ext_sha, ".sha", 256 - (ext_sha - dest));
sha_get(sha256);
FileSetData(dest, sha256, 0x20, 0, true);
snprintf(ext_sha, 256 - (ext_sha - dest), ".sha%c", sha1 ? '1' : '\0');
sha_get(hash);
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);
char ldest[256]; // 256 is the maximum length of a full path
char lorig[256];
strncpy(ldest, dest, 255);
strncpy(lorig, orig, 255);
char deststr[36 + 1];
strncpy(ldest, dest, 256);
strncpy(lorig, orig, 256);
char deststr[UTF_BUFFER_BYTESIZE(36)];
TruncateString(deststr, ldest, 36, 8);
// moving only for regular FAT drives (= not alias drives)
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;
}
// is destination part of origin?
u32 olen = strnlen(lorig, 255);
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;
}
@ -632,7 +634,7 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
// check & fix destination == origin
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;
}
@ -643,12 +645,11 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
return true;
}
const char* optionstr[5] =
{"Choose new name", "Overwrite file(s)", "Skip file(s)", "Overwrite all", "Skip all"};
u32 user_select = ShowSelectPrompt((*flags & ASK_ALL) ? 5 : 3, optionstr,
"Destination already exists:\n%s", deststr);
{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, STR_DESTINATION_ALREADY_EXISTS, deststr);
if (user_select == 1) {
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;
} while (fa_stat(ldest, NULL) == FR_OK);
} else if (user_select == 2) {
@ -672,7 +673,7 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
// setup buffer
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
if (!buffer) {
ShowPrompt(false, "Out of memory.");
ShowPrompt(false, "%s", STR_OUT_OF_MEMORY);
return false;
}
@ -689,7 +690,7 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
bool force_unmount = false;
// handle NAND image unmounts
if (ddrvtype & (DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE)) {
if ((ddrvtype & (DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE)) && !(GetVirtualSource(dest) & (VRT_DISADIFF | VRT_BDRI))) {
FILINFO fno;
// virtual NAND files over 4 MB require unmount, totally arbitrary limit (hacky!)
if ((fvx_stat(ldest, &fno) == FR_OK) && (fno.fsize > 4 * 1024 * 1024))
@ -698,20 +699,20 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
// prevent illegal operations
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;
}
// check destination == origin
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;
}
// setup buffer
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
if (!buffer) {
ShowPrompt(false, "Out of memory.");
ShowPrompt(false, "%s", STR_OUT_OF_MEMORY);
return false;
}
@ -730,26 +731,26 @@ bool PathCopy(const char* destdir, const char* orig, u32* flags) {
char dest[256]; // maximum path name length in FAT
char* oname = strrchr(orig, '/');
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
if (GetVirtualSource(destdir)) {
if (GetVirtualSource(destdir) & ~VRT_BDRI) {
u64 osize = FileGetSize(orig);
VirtualFile dvfile;
if (!osize) return false;
if (!GetVirtualFile(&dvfile, dest)) {
if (!GetVirtualFile(&dvfile, dest, FA_WRITE)) {
VirtualDir vdir;
if (!GetVirtualDir(&vdir, destdir)) return false;
while (true) { // search by size should be a last resort solution
if (!ReadVirtualDir(&dvfile, &vdir)) return false;
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;
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...
char deststr[36 + 1];
char origstr[36 + 1];
char deststr[UTF_BUFFER_BYTESIZE(36)];
char origstr[UTF_BUFFER_BYTESIZE(36)];
char osizestr[32];
char dsizestr[32];
TruncateString(deststr, dest, 36, 8);
@ -757,7 +758,7 @@ bool PathCopy(const char* destdir, const char* orig, u32* flags) {
FormatBytes(osizestr, osize);
FormatBytes(dsizestr, dvfile.size);
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;
}
}
@ -771,7 +772,7 @@ bool PathMove(const char* destdir, const char* orig, u32* flags) {
char dest[256]; // maximum path name length in FAT
char* oname = strrchr(orig, '/');
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);
}
@ -821,7 +822,7 @@ bool FileSelectorWorker(char* result, const char* text, const char* path, const
GetDirContents(contents, path_local);
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 };
u32 n_opt = 0;
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))
continue;
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;
}
if (!new_style) {
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)) {
char* dot = strrchr(temp_str, '.');
if (dot) *dot = '\0';
@ -848,7 +849,7 @@ bool FileSelectorWorker(char* result, const char* text, const char* path, const
n_found++;
}
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;
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
char pathstr[32+1];
char pathstr[UTF_BUFFER_BYTESIZE(32)];
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;
}
}
@ -883,6 +884,7 @@ bool FileSelector(char* result, const char* text, const char* path, const char*
void* buffer = (void*) malloc(sizeof(DirStruct));
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);
free(buffer);
return ret;

View File

@ -7,12 +7,13 @@
#define NO_CANCEL (1UL<<1)
#define SILENT (1UL<<2)
#define CALC_SHA (1UL<<3)
#define BUILD_PATH (1UL<<4)
#define ALLOW_EXPAND (1UL<<5)
#define ASK_ALL (1UL<<6)
#define SKIP_ALL (1UL<<7)
#define OVERWRITE_ALL (1UL<<8)
#define APPEND_ALL (1UL<<9)
#define USE_SHA1 (1UL<<4)
#define BUILD_PATH (1UL<<5)
#define ALLOW_EXPAND (1UL<<6)
#define ASK_ALL (1UL<<7)
#define SKIP_ALL (1UL<<8)
#define OVERWRITE_ALL (1UL<<9)
#define APPEND_ALL (1UL<<10)
// file selector flags
#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);
/** 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 **/
u32 FileFindData(const char* path, u8* data, u32 size_data, u32 offset_file);

View File

@ -1,11 +1,14 @@
#include "image.h"
#include "vff.h"
#include "nandcmac.h"
static FIL mount_file;
static u64 mount_state = 0;
static char mount_path[256] = { 0 };
static bool fix_cmac = false;
int ReadImageBytes(void* buffer, u64 offset, u64 count) {
UINT bytes_read;
@ -28,6 +31,7 @@ int WriteImageBytes(const void* buffer, u64 offset, u64 count) {
if (fvx_tell(&mount_file) != offset)
fvx_lseek(&mount_file, offset);
ret = fvx_write(&mount_file, buffer, count, &bytes_written);
if (ret == 0) fix_cmac = true;
return (ret != 0) ? (int) ret : (bytes_written != count) ? -1 : 0;
}
@ -56,18 +60,20 @@ const char* GetMountPath(void) {
}
u64 MountImage(const char* path) {
u64 type = (path) ? IdentifyFileType(path) : 0;
if (mount_state) {
fvx_close(&mount_file);
if (fix_cmac) FixFileCmac(mount_path, false);
fix_cmac = false;
mount_state = 0;
*mount_path = 0;
}
u64 type = (path) ? IdentifyFileType(path) : 0;
if (!type) return 0;
if ((fvx_open(&mount_file, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING) != FR_OK) &&
(fvx_open(&mount_file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK))
return 0;
fvx_lseek(&mount_file, 0);
fvx_sync(&mount_file);
strncpy(mount_path, path, 255);
strncpy(mount_path, path, 256);
return (mount_state = type);
}

View File

@ -15,10 +15,10 @@ typedef struct {
static FilCryptInfo filcrypt[NUM_FILCRYPTINFO] = { 0 };
char alias_drv[NUM_ALIAS_DRV]; // 1 char ASCII drive number of the alias drive / 0x00 if unused
char alias_path[NUM_ALIAS_DRV][128]; // full path to resolve the alias into
static char alias_drv[NUM_ALIAS_DRV]; // 1 char ASCII drive number of the alias drive / 0x00 if unused
static char alias_path[NUM_ALIAS_DRV][128]; // full path to resolve the alias into
u8 sd_keyy[NUM_ALIAS_DRV][16] __attribute__((aligned(4))); // key Y belonging to alias drive
static u8 sd_keyy[NUM_ALIAS_DRV][16] __attribute__((aligned(4))); // key Y belonging to alias drive
int alias_num (const TCHAR* path) {
int num = -1;
@ -35,9 +35,9 @@ int alias_num (const TCHAR* path) {
void dealias_path (TCHAR* alias, const TCHAR* path) {
int num = alias_num(path);
u32 p_offs = (path[2] == '/' && ((path[3] == '/') || (path[3] == '\0'))) ? 3 : 2;
if (num >= 0) // set alias (alias is assumed to be 256 byte)
if (num >= 0) // set alias (alias is assumed to be 256 byte!)
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) {
@ -276,7 +276,7 @@ bool SetupNandSdDrive(const char* path, const char* sd_path, const char* movable
// build the alias path (id0)
u32 sha256sum[8];
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]);
// find the alias path (id1)

View File

@ -17,7 +17,7 @@ bool CheckSupportFile(const char* fname)
const char* base_paths[] = { SUPPORT_FILE_PATHS };
for (u32 i = 0; i < countof(base_paths); i++) {
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)
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++) {
UINT len32;
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)
return len32;
}
@ -68,7 +68,7 @@ bool SaveSupportFile(const char* fname, void* buffer, size_t len)
// write support file
if (idx >= 0) {
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);
if (fvx_qwrite(path, buffer, 0, len, NULL) == FR_OK)
return true;
@ -115,6 +115,7 @@ bool CheckSupportDir(const char* dname)
bool FileSelectorSupport(char* result, const char* text, const char* dname, const char* pattern)
{
char path[256];
// result needs to be at least 256 bytes long for this to work!
if (!GetSupportDir(path, dname)) return false;
return FileSelector(result, text, path, pattern, HIDE_EXT, false);
}

View File

@ -3,7 +3,9 @@
#include "common.h"
// scripts / payloads dir names
#define LANGUAGES_DIR "languages"
#define SCRIPTS_DIR "scripts"
#define LUASCRIPTS_DIR "luascripts"
#define PAYLOADS_DIR "payloads"
bool CheckSupportFile(const char* fname);
@ -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 SetAsSupportFile(const char* fname, const char* source);
bool GetSupportDir(char* path, const char* dname);
bool CheckSupportDir(const char* fpath);
bool FileSelectorSupport(char* result, const char* text, const char* dname, const char* pattern);

View File

@ -18,7 +18,7 @@ FRESULT fvx_open (FIL* fp, const TCHAR* path, BYTE mode) {
#if _VFIL_ENABLED
VirtualFile* vfile = VFIL(fp);
memset(fp, 0, sizeof(FIL));
if (GetVirtualFile(vfile, path)) {
if (GetVirtualFile(vfile, path, mode)) {
fp->obj.fs = NULL;
fp->obj.objsize = vfile->size;
fp->fptr = 0;
@ -81,7 +81,7 @@ FRESULT fvx_sync (FIL* fp) {
FRESULT fvx_stat (const TCHAR* path, FILINFO* fno) {
if (GetVirtualSource(path)) {
VirtualFile vfile;
if (!GetVirtualFile(&vfile, path)) return FR_NO_PATH;
if (!GetVirtualFile(&vfile, path, FA_READ)) return FR_NO_PATH;
if (fno) {
fno->fsize = vfile.size;
fno->fdate = (1<<5)|(1<<0); // 1 for month / day
@ -102,7 +102,7 @@ FRESULT fvx_rename (const TCHAR* path_old, const TCHAR* path_new) {
FRESULT fvx_unlink (const TCHAR* path) {
if (GetVirtualSource(path)) {
VirtualFile vfile;
if (!GetVirtualFile(&vfile, path)) return FR_NO_PATH;
if (!GetVirtualFile(&vfile, path, FA_READ)) return FR_NO_PATH;
if (DeleteVirtualFile(&vfile) != 0) return FR_DENIED;
return FR_OK;
} else return fa_unlink( path );
@ -193,6 +193,44 @@ FRESULT fvx_qwrite (const TCHAR* path, const void* buff, FSIZE_t ofs, UINT btw,
return res;
}
FRESULT fvx_qcreate (const TCHAR* path, UINT btc) {
FIL fp;
FRESULT res;
res = fvx_open(&fp, path, FA_WRITE | FA_CREATE_ALWAYS);
if (res != FR_OK) return res;
res = fvx_lseek(&fp, btc);
fvx_close(&fp);
return res;
}
/* // untested / unused, might come in handy at a later point
FRESULT fvx_qfill (const TCHAR* path, const void* buff, UINT btb) {
FIL fp;
FRESULT res;
UINT bwtt = 0;
UINT fsiz = 0;
res = fvx_open(&fp, path, FA_WRITE | FA_OPEN_EXISTING);
if (res != FR_OK) return res;
fsiz = fvx_size(&fp);
while (bwtt < fsiz) {
UINT btw = ((fsiz - bwtt) >= btb) ? btb : (fsiz - bwtt);
UINT bwt;
res = fvx_write(&fp, buff, btw, &bwt);
if ((res == FR_OK) && (bwt != btw)) res = FR_DENIED;
if (res != FR_OK) break;
bwtt += bwt;
}
fvx_close(&fp);
return res;
}*/
FSIZE_t fvx_qsize (const TCHAR* path) {
FILINFO fno;
return (fvx_stat(path, &fno) == FR_OK) ? fno.fsize : 0;
@ -219,7 +257,8 @@ FRESULT worker_fvx_rmkdir (TCHAR* tpath) {
FRESULT fvx_rmkdir (const TCHAR* path) {
#if !_LFN_UNICODE // this will not work for unicode
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 );
#else
return FR_DENIED;
@ -229,7 +268,8 @@ FRESULT fvx_rmkdir (const TCHAR* path) {
FRESULT fvx_rmkpath (const TCHAR* path) {
#if !_LFN_UNICODE // this will not work for unicode
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, '/');
if (!slash) return FR_DENIED;
*slash = '\0';
@ -257,7 +297,7 @@ FRESULT worker_fvx_runlink (TCHAR* tpath) {
while (fvx_readdir(&pdir, &fno) == FR_OK) {
if ((strncmp(fno.fname, ".", 2) == 0) || (strncmp(fno.fname, "..", 3) == 0))
continue; // filter out virtual entries
strncpy(fname, fno.fname, tpath + 255 - fname);
strcpy(fname, fno.fname);
if (fno.fname[0] == 0) {
break;
} else { // return value won't matter
@ -275,7 +315,8 @@ FRESULT worker_fvx_runlink (TCHAR* tpath) {
FRESULT fvx_runlink (const TCHAR* path) {
#if !_LFN_UNICODE // this will not work for unicode
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 );
#else
return FR_DENIED;
@ -318,7 +359,8 @@ FRESULT fvx_preaddir (DIR* dp, FILINFO* fno, const TCHAR* pattern) {
}
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, '/');
if (!fname) return FR_DENIED;
*fname = '\0';
@ -338,7 +380,7 @@ FRESULT fvx_findpath (TCHAR* path, const TCHAR* pattern, BYTE mode) {
while ((fvx_preaddir(&pdir, &fno, npattern) == FR_OK) && *(fno.fname)) {
int cmp = strncmp(fno.fname, fname, _MAX_FN_LEN);
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;
}
fvx_closedir( &pdir );
@ -347,7 +389,8 @@ FRESULT fvx_findpath (TCHAR* path, const TCHAR* pattern, BYTE mode) {
}
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, '/');
if (!fname) return FR_DENIED;
fname++;

View File

@ -8,6 +8,7 @@
#define fvx_tell(fp) ((fp)->fptr)
#define fvx_size(fp) ((fp)->obj.objsize)
#define fvx_eof(fp) (fvx_tell(fp) == fvx_size(fp))
#define FN_ANY 0x00
#define FN_HIGHEST 0x01
@ -29,9 +30,10 @@ FRESULT fvx_opendir (DIR* dp, const TCHAR* path);
FRESULT fvx_closedir (DIR* dp);
FRESULT fvx_readdir (DIR* dp, FILINFO* fno);
// additional quick read / write functions
// additional quick read / write / create functions
FRESULT fvx_qread (const TCHAR* path, void* buff, FSIZE_t ofs, UINT btr, UINT* br);
FRESULT fvx_qwrite (const TCHAR* path, const void* buff, FSIZE_t ofs, UINT btw, UINT* bw);
FRESULT fvx_qcreate (const TCHAR* path, UINT btc);
// additional quick file info functions
FSIZE_t fvx_qsize (const TCHAR* path);

View File

@ -2,11 +2,81 @@
#include "vff.h"
#define FAT_ENTRY_SIZE 2 * sizeof(u32)
#define REPLACE_SIZE_MISMATCH 2
#define getfatflag(uv) (((uv) & 0x80000000UL) != 0)
#define getfatindex(uv) ((uv) & 0x7FFFFFFFUL)
#define buildfatuv(index, flag) ((index) | ((flag) ? 0x80000000UL : 0))
typedef struct {
char magic[4]; // "BDRI"
u32 version; // == 0x30000
u64 info_offset; // == 0x20
u64 image_size; // in blocks; and including the pre-header
u32 image_block_size;
u8 padding1[4];
u8 unknown[4];
u32 data_block_size;
u64 dht_offset;
u32 dht_bucket_count;
u8 padding2[4];
u64 fht_offset;
u32 fht_bucket_count;
u8 padding3[4];
u64 fat_offset;
u32 fat_entry_count; // exculdes 0th entry
u8 padding4[4];
u64 data_offset;
u32 data_block_count; // == fat_entry_count
u8 padding5[4];
u32 det_start_block;
u32 det_block_count;
u32 max_dir_count;
u8 padding6[4];
u32 fet_start_block;
u32 fet_block_count;
u32 max_file_count;
u8 padding7[4];
} __attribute__((packed)) BDRIFsHeader;
typedef struct {
char magic[8]; // varies based on media type and importdb vs titledb
u8 reserved[0x78];
BDRIFsHeader fs_header;
} __attribute__((packed)) TitleDBPreHeader;
typedef struct {
char magic[4]; // "TICK"
u32 unknown1; // usually (assuming always) == 1
u32 unknown2;
u32 unknown3;
BDRIFsHeader fs_header;
} __attribute__((packed)) TickDBPreHeader;
typedef struct {
u32 parent_index;
u8 title_id[8];
u32 next_sibling_index;
u8 padding1[4];
u32 start_block_index;
u64 size; // in bytes
u8 padding2[8];
u32 hash_bucket_next_index;
} __attribute__((packed)) TdbFileEntry;
typedef struct {
u32 total_entry_count;
u32 max_entry_count; // == max_file_count + 1
u8 padding[32];
u32 next_dummy_index;
} __attribute__((packed)) DummyFileEntry;
typedef struct {
u32 unknown; // usually (assuming always) == 1
u32 ticket_size; // == 0x350 == sizeof(Ticket)
Ticket ticket;
} __attribute__((packed, aligned(4))) TicketEntry;
static FIL* bdrifp;
static FRESULT BDRIRead(UINT ofs, UINT btr, void* buf) {
@ -57,12 +127,44 @@ static u32 GetHashBucket(const u8* tid, u32 parent_dir_index, u32 bucket_count)
return hash % bucket_count;
}
static u32 GetBDRIEntrySize(const BDRIFsHeader* fs_header, const u32 fs_header_offset, const u8* title_id, u32* size) {
if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count)) // Could be more thorough
return 1;
const u32 data_offset = fs_header_offset + fs_header->data_offset;
const u32 fet_offset = data_offset + fs_header->fet_start_block * fs_header->data_block_size;
const u32 fht_offset = fs_header_offset + fs_header->fht_offset;
u32 index = 0;
TdbFileEntry file_entry;
u64 tid_be = getbe64(title_id);
u8* title_id_be = (u8*) &tid_be;
const u32 hash_bucket = GetHashBucket(title_id_be, 1, fs_header->fht_bucket_count);
if (BDRIRead(fht_offset + hash_bucket * sizeof(u32), sizeof(u32), &(file_entry.hash_bucket_next_index)) != FR_OK)
return 1;
// Find the file entry for the tid specified, fail if it doesn't exist
do {
if (file_entry.hash_bucket_next_index == 0)
return 1;
index = file_entry.hash_bucket_next_index;
if (BDRIRead(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
return 1;
} while (memcmp(title_id_be, file_entry.title_id, 8) != 0);
*size = file_entry.size;
return 0;
}
static u32 ReadBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offset, const u8* title_id, u8* entry, const u32 expected_size) {
if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count)) // Could be more thorough
return 1;
const u32 data_offset = fs_header_offset + fs_header->data_offset;
const u32 det_offset = data_offset + fs_header->det_start_block * fs_header->data_block_size;
const u32 fet_offset = data_offset + fs_header->fet_start_block * fs_header->data_block_size;
const u32 fht_offset = fs_header_offset + fs_header->fht_offset;
const u32 fat_offset = fs_header_offset + fs_header->fat_offset;
@ -71,17 +173,17 @@ static u32 ReadBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offs
TdbFileEntry file_entry;
u64 tid_be = getbe64(title_id);
u8* title_id_be = (u8*) &tid_be;
const u32 hash_bucket = GetHashBucket(title_id_be, 1, fs_header->fht_bucket_count);
// Read the index of the first file entry from the directory entry table
if (BDRIRead(det_offset + 0x2C, sizeof(u32), &(file_entry.next_sibling_index)) != FR_OK)
if (BDRIRead(fht_offset + hash_bucket * sizeof(u32), sizeof(u32), &(file_entry.hash_bucket_next_index)) != FR_OK)
return 1;
// Find the file entry for the tid specified, fail if it doesn't exist
do {
if (file_entry.next_sibling_index == 0)
if (file_entry.hash_bucket_next_index == 0)
return 1;
index = file_entry.next_sibling_index;
index = file_entry.hash_bucket_next_index;
if (BDRIRead(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
return 1;
@ -90,15 +192,6 @@ static u32 ReadBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offs
if (expected_size && (file_entry.size != expected_size))
return 1;
const u32 hash_bucket = GetHashBucket(file_entry.title_id, file_entry.parent_index, fs_header->fht_bucket_count);
u32 index_hash = 0;
if (BDRIRead(fht_offset + hash_bucket * sizeof(u32), sizeof(u32), &index_hash) != FR_OK)
return 1;
if (index != index_hash)
return 1;
index = file_entry.start_block_index + 1; // FAT entry index
u32 bytes_read = 0;
@ -277,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 (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 {
do_replace = true;
break;
@ -387,7 +481,7 @@ static u32 AddBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offse
u32 bytes_written = 0, fat_index_write = fat_index;
while (bytes_written < file_entry.size) { // Write the full entry, walking the FAT node chain
while (bytes_written < size) { // Write the full entry, walking the FAT node chain
// Can't assume contiguity here, because we might be replacing an existing entry
u32 write_start = fat_index_write - 1; // Data region block index
u32 write_count = 0;
@ -414,7 +508,7 @@ static u32 AddBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offse
fat_index_write = next_index;
u32 btw = min(file_entry.size - bytes_written, write_count * fs_header->data_block_size);
u32 btw = min(size - bytes_written, write_count * fs_header->data_block_size);
if (BDRIWrite(data_offset + write_start * fs_header->data_block_size, btw, entry + bytes_written) != FR_OK)
return 1;
@ -516,7 +610,10 @@ static u32 ListBDRIEntryTitleIDs(const BDRIFsHeader* fs_header, const u32 fs_hea
while ((file_entry.next_sibling_index != 0) && (num_entries < max_title_ids)) {
if (BDRIRead(fet_offset + file_entry.next_sibling_index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
return 1;
memcpy(title_ids + num_entries * 8, file_entry.title_id, 8);
u64 tid_be = getbe64(file_entry.title_id);
memcpy(title_ids + num_entries * 8, (u8*) &tid_be, 8);
num_entries++;
}
@ -636,12 +733,13 @@ u32 ReadTitleInfoEntryFromDB(const char* path, const u8* title_id, TitleInfoEntr
return 0;
}
u32 ReadTicketFromDB(const char* path, const u8* title_id, Ticket* ticket)
{
u32 ReadTicketFromDB(const char* path, const u8* title_id, Ticket** ticket) {
FIL file;
TickDBPreHeader pre_header;
TicketEntry te;
TicketEntry* te = NULL;
u32 entry_size;
*ticket = NULL;
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return 1;
@ -649,8 +747,12 @@ u32 ReadTicketFromDB(const char* path, const u8* title_id, Ticket* ticket)
if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) ||
!CheckDBMagic((u8*) &pre_header, true) ||
(ReadBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id, (u8*) &te,
sizeof(TicketEntry)) != 0)) {
(GetBDRIEntrySize(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id, &entry_size) != 0) ||
entry_size < sizeof(TicketEntry) + 0x14 ||
(te = (TicketEntry*)malloc(entry_size), te == NULL) ||
(ReadBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id, (u8*) te,
entry_size) != 0)) {
free(te); // if allocated
fvx_close(bdrifp);
bdrifp = NULL;
return 1;
@ -659,11 +761,21 @@ u32 ReadTicketFromDB(const char* path, const u8* title_id, Ticket* ticket)
fvx_close(bdrifp);
bdrifp = NULL;
if (te.ticket_size != sizeof(Ticket))
if (te->ticket_size != GetTicketSize(&te->ticket)) {
free(te);
return 1;
}
if (ticket) *ticket = te.ticket;
if (ticket) {
u32 size = te->ticket_size;
memmove(te, &te->ticket, size); // recycle this memory, instead of allocating another
Ticket* tik = realloc(te, size);
if(!tik) tik = (Ticket*)te;
*ticket = tik;
return 0;
}
free(te);
return 0;
}
@ -737,26 +849,39 @@ u32 AddTitleInfoEntryToDB(const char* path, const u8* title_id, const TitleInfoE
u32 AddTicketToDB(const char* path, const u8* title_id, const Ticket* ticket, bool replace) {
FIL file;
TickDBPreHeader pre_header;
u32 entry_size = sizeof(TicketEntry) + GetTicketContentIndexSize(ticket);
TicketEntry te;
te.unknown = 1;
te.ticket_size = sizeof(Ticket);
te.ticket = *ticket;
if (fvx_open(&file, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING) != FR_OK)
TicketEntry* te = (TicketEntry*)malloc(entry_size);
if (!te) {
return 1;
}
te->unknown = 1;
te->ticket_size = GetTicketSize(ticket);
memcpy(&te->ticket, ticket, te->ticket_size);
if (fvx_open(&file, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING) != FR_OK) {
free(te);
return 1;
}
bdrifp = &file;
u32 add_bdri_res = 0;
if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) ||
!CheckDBMagic((u8*) &pre_header, true) ||
((add_bdri_res = AddBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id,
(const u8*) te, entry_size, replace)) == 1) ||
(add_bdri_res == REPLACE_SIZE_MISMATCH && ((RemoveBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id) != 0) ||
(AddBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id,
(const u8*) &te, sizeof(TicketEntry), replace) != 0)) {
(const u8*) te, entry_size, replace) != 0)))) {
free(te);
fvx_close(bdrifp);
bdrifp = NULL;
return 1;
}
free(te);
fvx_close(bdrifp);
bdrifp = NULL;
return 0;

View File

@ -2,105 +2,16 @@
#include "common.h"
#include "ticket.h"
// There's probably a better place to put this
#define SD_TITLEDB_PATH(emu) ((emu) ? "B:/dbs/title.db" : "A:/dbs/title.db")
#include "tie.h"
// https://www.3dbrew.org/wiki/Inner_FAT
// https://www.3dbrew.org/wiki/Title_Database
typedef struct {
char magic[4]; // "BDRI"
u32 version; // == 0x30000
u64 info_offset; // == 0x20
u64 image_size; // in blocks; and including the pre-header
u32 image_block_size;
u8 padding1[4];
u8 unknown[4];
u32 data_block_size;
u64 dht_offset;
u32 dht_bucket_count;
u8 padding2[4];
u64 fht_offset;
u32 fht_bucket_count;
u8 padding3[4];
u64 fat_offset;
u32 fat_entry_count; // exculdes 0th entry
u8 padding4[4];
u64 data_offset;
u32 data_block_count; // == fat_entry_count
u8 padding5[4];
u32 det_start_block;
u32 det_block_count;
u32 max_dir_count;
u8 padding6[4];
u32 fet_start_block;
u32 fet_block_count;
u32 max_file_count;
u8 padding7[4];
} __attribute__((packed)) BDRIFsHeader;
typedef struct {
char magic[8]; // varies based on media type and importdb vs titledb
u8 reserved[0x78];
BDRIFsHeader fs_header;
} __attribute__((packed)) TitleDBPreHeader;
typedef struct {
char magic[4]; // "TICK"
u32 unknown1; // usually (assuming always) == 1
u32 unknown2;
u32 unknown3;
BDRIFsHeader fs_header;
} __attribute__((packed)) TickDBPreHeader;
typedef struct {
u32 parent_index;
u8 title_id[8];
u32 next_sibling_index;
u8 padding1[4];
u32 start_block_index;
u64 size; // in bytes
u8 padding2[8];
u32 hash_bucket_next_index;
} __attribute__((packed)) TdbFileEntry;
typedef struct {
u32 total_entry_count;
u32 max_entry_count; // == max_file_count + 1
u8 padding[32];
u32 next_dummy_index;
} __attribute__((packed)) DummyFileEntry;
typedef struct {
u64 title_size; // in bytes
u32 title_type; // usually == 0x40
u32 title_version;
u8 flags_0[4];
u32 tmd_content_id;
u32 cmd_content_id;
u8 flags_1[4];
u32 extdata_id_low; // 0 if the title doesn't use extdata
u8 reserved1[4];
u8 flags_2[8];
char product_code[16];
u8 reserved2[16];
u8 unknown[4]; // appears to not matter what's here
u8 reserved3[44];
} __attribute__((packed)) TitleInfoEntry;
typedef struct {
u32 unknown; // usually (assuming always) == 1
u32 ticket_size; // == 0x350 == sizeof(Ticket)
Ticket ticket;
} __attribute__((packed, aligned(4))) TicketEntry;
u32 GetNumTitleInfoEntries(const char* path);
u32 GetNumTickets(const char* path);
u32 ListTitleInfoEntryTitleIDs(const char* path, u8* title_ids, u32 max_title_ids);
u32 ListTicketTitleIDs(const char* path, u8* title_ids, u32 max_title_ids);
u32 ReadTitleInfoEntryFromDB(const char* path, const u8* title_id, TitleInfoEntry* tie);
u32 ReadTicketFromDB(const char* path, const u8* title_id, Ticket* ticket);
u32 ReadTicketFromDB(const char* path, const u8* title_id, Ticket** ticket);
u32 RemoveTitleInfoEntryFromDB(const char* path, const u8* title_id);
u32 RemoveTicketFromDB(const char* path, const u8* title_id);
u32 AddTitleInfoEntryToDB(const char* path, const u8* title_id, const TitleInfoEntry* tie, bool replace);

View File

@ -18,6 +18,7 @@
#include <libgen.h>
#include "language.h"
#include "common.h"
#include "timer.h"
#include "crc32.h"
@ -143,18 +144,18 @@ static bool BEAT_UpdateProgress(const BEAT_Context *ctx)
static const char *BEAT_ErrString(int error)
{ // Get an error description string
switch(error) {
case BEAT_OK: return "No error";
case BEAT_EOAL: return "End of action list";
case BEAT_ABORTED: return "Aborted by user";
case BEAT_IO_ERROR: return "Failed to read/write file";
case BEAT_OVERFLOW: return "Attempted to write beyond end of file";
case BEAT_BADPATCH: return "Invalid patch file";
case BEAT_BADINPUT: return "Invalid input file";
case BEAT_BADOUTPUT: return "Output file checksum mismatch";
case BEAT_BADCHKSUM: return "File checksum failed";
case BEAT_PATCH_EXPECT: return "Expected more patch data";
case BEAT_OUT_OF_MEMORY: return "Out of memory";
default: return "Unknown error";
case BEAT_OK: return STR_BEAT_NO_ERROR;
case BEAT_EOAL: return STR_BEAT_END_OF_ACTION_LIST;
case BEAT_ABORTED: return STR_BEAT_ABORTED_BY_USER;
case BEAT_IO_ERROR: return STR_BEAT_FAILED_TO_READ_WRITE_FILE;
case BEAT_OVERFLOW: return STR_BEAT_ATTEMPTED_TO_WRITE_BEYOND_EOF;
case BEAT_BADPATCH: return STR_BEAT_INVALID_PATCH_FILE;
case BEAT_BADINPUT: return STR_BEAT_INVALID_INPUT_FILE;
case BEAT_BADOUTPUT: return STR_BEAT_OUTPUT_FILE_CHECKSUM_MISMATCH;
case BEAT_BADCHKSUM: return STR_BEAT_FILE_CHECKSUM_FAILED;
case BEAT_PATCH_EXPECT: return STR_BEAT_EXPECTED_MORE_PATCH_DATA;
case BEAT_OUT_OF_MEMORY: return STR_BEAT_OUT_OF_MEMORY;
default: return STR_BEAT_UNKNOWN_ERROR;
}
}
@ -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)
{ // Parses an action list and runs commands specified in `acts`
u32 vli, len;
int cmd, res;
int cmd, res = BEAT_OK;
while((res == BEAT_OK) &&
(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();
res = (bpm ? BPM_InitCTX : BPS_InitCTX)(&ctx, p, s, d);
if (res != BEAT_OK) {
ShowPrompt(false, "Failed to initialize %s file:\n%s",
bpm ? "BPM" : "BPS", BEAT_ErrString(res));
ShowPrompt(false, bpm ? STR_FAILED_TO_INITIALIZE_BPM_FILE : STR_FAILED_TO_INITIALIZE_BPS_FILE, BEAT_ErrString(res));
} else {
res = (bpm ? BPM_RunActions : BPS_RunActions)(&ctx);
switch(res) {
case BEAT_OK:
ShowPrompt(false, "Patch successfully applied");
ShowPrompt(false, "%s", STR_PATCH_SUCCESSFULLY_APPLIED);
break;
case BEAT_ABORTED:
ShowPrompt(false, "Patching aborted by user");
ShowPrompt(false, "%s", STR_PATCHING_ABORTED_BY_USER);
break;
default:
ShowPrompt(false, "Failed to run patch:\n%s", BEAT_ErrString(res));
ShowPrompt(false, STR_FAILED_TO_RUN_PATCH, BEAT_ErrString(res));
break;
}
}

View File

@ -1,21 +1,816 @@
#include "cert.h"
#include "ff.h"
#include "disadiff.h"
#include "rsa.h"
u32 LoadCertFromCertDb(u64 offset, Certificate* cert, u32* mod, u32* exp) {
Certificate cert_local;
FIL db;
UINT bytes_read;
typedef struct {
char magic[4]; // "CERT"
u8 unk[4]; // afaik, always 0
u8 used_size[4]; // size used after this header
u8 garbage[4]; // literally garbage values
} PACKED_STRUCT CertsDbPartitionHeader;
// not much in terms of error checking here
if (f_open(&db, "1:/dbs/certs.db", FA_READ | FA_OPEN_EXISTING) != FR_OK)
static inline void GetCertDBPath(char* path, bool emunand) {
path[0] = emunand ? '4' : '1';
strcpy(&path[1], ":/dbs/certs.db");
}
#define CERT_RETAIL_CA3_IDENT BIT(0)
#define CERT_RETAIL_XSc_IDENT BIT(1)
#define CERT_RETAIL_CPb_IDENT BIT(2)
#define CERT_DEV_CA4_IDENT BIT(3)
#define CERT_DEV_XS9_IDENT BIT(4)
#define CERT_DEV_CPa_IDENT BIT(5)
#define CERT_NO_STORE_SPACE (0xFF)
static struct {
u32 loaded_certs_flg;
u8 retail_CA3_raw[CERT_RSA4096_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
u8 retail_XSc_raw[CERT_RSA2048_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
u8 retail_CPb_raw[CERT_RSA2048_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
u8 dev_CA4_raw[CERT_RSA4096_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
u8 dev_XS9_raw[CERT_RSA2048_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
u8 dev_CPa_raw[CERT_RSA2048_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
Certificate retail_CA3;
Certificate retail_XSc;
Certificate retail_CPb;
Certificate dev_CA4;
Certificate dev_XS9;
Certificate dev_CPa;
} _CommonCertsStorage = {
0, // none loaded yet, ident defines used to say what's loaded
{0}, {0}, {0}, {0}, {0}, {0}, // no data yet
// cert structs pre-point already to raw certs
{
(CertificateSignature*)&_CommonCertsStorage.retail_CA3_raw[0],
(CertificateBody*)&_CommonCertsStorage.retail_CA3_raw[CERT_RSA4096_SIG_SIZE]
},
{
(CertificateSignature*)&_CommonCertsStorage.retail_XSc_raw[0],
(CertificateBody*)&_CommonCertsStorage.retail_XSc_raw[CERT_RSA2048_SIG_SIZE]
},
{
(CertificateSignature*)&_CommonCertsStorage.retail_CPb_raw[0],
(CertificateBody*)&_CommonCertsStorage.retail_CPb_raw[CERT_RSA2048_SIG_SIZE]
},
{
(CertificateSignature*)&_CommonCertsStorage.dev_CA4_raw[0],
(CertificateBody*)&_CommonCertsStorage.dev_CA4_raw[CERT_RSA4096_SIG_SIZE]
},
{
(CertificateSignature*)&_CommonCertsStorage.dev_XS9_raw[0],
(CertificateBody*)&_CommonCertsStorage.dev_XS9_raw[CERT_RSA2048_SIG_SIZE]
},
{
(CertificateSignature*)&_CommonCertsStorage.dev_CPa_raw[0],
(CertificateBody*)&_CommonCertsStorage.dev_CPa_raw[CERT_RSA2048_SIG_SIZE]
}
};
static inline void _Certificate_CleanupImpl(Certificate* cert);
bool Certificate_IsValid(const Certificate* cert) {
if (!cert || !cert->sig || !cert->data)
return false;
u32 sig_type = getbe32(cert->sig->sig_type);
if (sig_type < 0x10000 || sig_type > 0x10005)
return false;
u32 keytype = getbe32(cert->data->keytype);
if (keytype > 2)
return false;
size_t issuer_len = strnlen(cert->data->issuer, 0x40);
size_t name_len = strnlen(cert->data->name, 0x40);
// if >= 0x40, cert can't fit as issuer for other objects later
// since later objects using the certificate as their issuer will have them use it as certissuer-certname
if (!issuer_len || !name_len || (issuer_len + name_len + 1) >= 0x40)
return false;
return true;
}
bool Certificate_IsRSA(const Certificate* cert) {
if (!Certificate_IsValid(cert)) return false;
if (getbe32(cert->data->keytype) >= 2) return false;
return true;
}
bool Certificate_IsECC(const Certificate* cert) {
if (!Certificate_IsValid(cert)) return false;
if (getbe32(cert->data->keytype) != 2) return false;
return true;
}
u32 Certificate_GetSignatureSize(const Certificate* cert, u32* size) {
if (!size || !Certificate_IsValid(cert)) return 1;
u32 sig_type = getbe32(cert->sig->sig_type);
if (sig_type == 0x10000 || sig_type == 0x10003)
*size = 0x200;
else if (sig_type == 0x10001 || sig_type == 0x10004)
*size = 0x100;
else if (sig_type == 0x10002 || sig_type == 0x10005)
*size = 0x3C;
else
return 1;
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;
}
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;
}

View File

@ -2,21 +2,54 @@
#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
// all numbers in big endian
typedef struct {
u8 sig_type[4]; // expected: 0x010004 / RSA_2048 SHA256
u8 signature[0x100];
u8 padding0[0x3C];
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;
u8 sig_type[4];
u8 signature[];
} PACKED_ALIGN(1) CertificateSignature;
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);

View File

@ -4,11 +4,12 @@
#include "ff.h"
#include "aes.h"
#include "sha.h"
#include "cert.h"
u32 ValidateCiaHeader(CiaHeader* header) {
if ((header->size_header != CIA_HEADER_SIZE) ||
(header->size_cert != CIA_CERT_SIZE) ||
(header->size_ticket != TICKET_SIZE) ||
(header->size_ticket != TICKET_COMMON_SIZE) ||
(header->size_tmd < TMD_SIZE_MIN) ||
(header->size_tmd > TMD_SIZE_MAX) ||
(header->size_content == 0))
@ -50,30 +51,23 @@ u32 FixCiaHeaderForTmd(CiaHeader* header, TitleMetaData* tmd) {
}
u32 BuildCiaCert(u8* ciacert) {
const u8 cert_hash_expected[0x20] = {
static const u8 cert_hash_expected[0x20] = {
0xC7, 0x2E, 0x1C, 0xA5, 0x61, 0xDC, 0x9B, 0xC8, 0x05, 0x58, 0x58, 0x9C, 0x63, 0x08, 0x1C, 0x8A,
0x10, 0x78, 0xDF, 0x42, 0x99, 0x80, 0x3A, 0x68, 0x58, 0xF0, 0x41, 0xF9, 0xCB, 0x10, 0xE6, 0x35
};
const u8 cert_hash_expected_dev[0x20] = {
static const u8 cert_hash_expected_dev[0x20] = {
0xFB, 0xD2, 0xC0, 0x47, 0x95, 0xB9, 0x4C, 0xC8, 0x0B, 0x64, 0x58, 0x96, 0xF6, 0x61, 0x0F, 0x52,
0x18, 0x83, 0xAF, 0xE0, 0xF4, 0xE5, 0x62, 0xBA, 0x69, 0xEE, 0x72, 0x2A, 0xC2, 0x4E, 0x95, 0xB3
};
// open certs.db file on SysNAND
FIL db;
UINT bytes_read;
if (f_open(&db, "1:/dbs/certs.db", FA_READ | FA_OPEN_EXISTING) != FR_OK)
static const char* const retail_issuers[] = {"Root-CA00000003", "Root-CA00000003-XS0000000c", "Root-CA00000003-CP0000000b"};
static const char* const dev_issuers[] = {"Root-CA00000004", "Root-CA00000004-XS00000009", "Root-CA00000004-CP0000000a"};
size_t size = CIA_CERT_SIZE;
if (BuildRawCertBundleFromCertDb(ciacert, &size, !IS_DEVKIT ? retail_issuers : dev_issuers, 3) ||
size != CIA_CERT_SIZE) {
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
u8 cert_hash[0x20];
@ -95,12 +89,12 @@ u32 BuildCiaMeta(CiaMeta* meta, void* exthdr, void* smdh) {
return 0;
}
u32 BuildCiaHeader(CiaHeader* header) {
u32 BuildCiaHeader(CiaHeader* header, u32 ticket_size) {
memset(header, 0, sizeof(CiaHeader));
// sizes in header - fill only known sizes, others zero
header->size_header = sizeof(CiaHeader);
header->size_cert = CIA_CERT_SIZE;
header->size_ticket = sizeof(Ticket);
header->size_ticket = ticket_size;
header->size_tmd = 0;
header->size_meta = 0;
header->size_content = 0;

View File

@ -34,8 +34,8 @@ typedef struct {
u8 header_padding[0x40 - (CIA_HEADER_SIZE % 0x40)];
u8 cert[CIA_CERT_SIZE];
// cert is aligned and needs no padding
Ticket ticket;
u8 ticket_padding[0x40 - (TICKET_SIZE % 0x40)];
TicketCommon ticket;
u8 ticket_padding[0x40 - (TICKET_COMMON_SIZE % 0x40)];
TitleMetaData tmd;
TmdContentChunk content_list[TMD_MAX_CONTENTS];
} PACKED_ALIGN(16) CiaStub;
@ -66,7 +66,7 @@ u32 FixCiaHeaderForTmd(CiaHeader* header, TitleMetaData* tmd);
u32 BuildCiaCert(u8* ciacert);
u32 BuildCiaMeta(CiaMeta* meta, void* exthdr, void* smdh);
u32 BuildCiaHeader(CiaHeader* header);
u32 BuildCiaHeader(CiaHeader* header, u32 ticket_size);
u32 DecryptCiaContentSequential(void* data, u32 size, u8* ctr, const u8* titlekey);
u32 EncryptCiaContentSequential(void* data, u32 size, u8* ctr, const u8* titlekey);

View 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;

View File

@ -1,11 +1,63 @@
#include "cmd.h"
u32 CheckCmdSize(CmdHeader* cmd, u64 fsize) {
u64 cmdsize = sizeof(CmdHeader) +
(cmd->n_entries * sizeof(u32)) +
(cmd->n_cmacs * sizeof(u32)) +
(cmd->n_entries * 0x10);
CmdHeader* BuildAllocCmdData(TitleMetaData* tmd) {
CmdHeader proto;
CmdHeader* cmd = NULL;
u32 content_count = getbe16(tmd->content_count);
u16 max_cnt_idx = 0;
return (fsize == cmdsize) ? 0 : 1;
// sanity check
if (!content_count)
return NULL;
// find max content id
TmdContentChunk* chunk = (TmdContentChunk*) (tmd + 1);
for (u32 i = 0; (i < content_count) && (i < TMD_MAX_CONTENTS); i++, chunk++)
if (getbe16(chunk->index) > max_cnt_idx) max_cnt_idx = getbe16(chunk->index);
// allocate memory for CMD / basic setup
proto.cmd_id = 1;
proto.n_entries = max_cnt_idx + 1;
proto.n_cmacs = content_count;
proto.unknown = 1;
memset(proto.cmac, 0x00, 0x10);
cmd = (CmdHeader*) malloc(CMD_SIZE(&proto));
if (!cmd) return NULL;
memset(cmd, 0x00, CMD_SIZE(&proto));
memcpy(cmd, &proto, sizeof(CmdHeader));
cmd->unknown = 0x0; // this means no CMACs, only valid for NAND
// copy content ids
u32* cnt_id = (u32*) (cmd + 1);
u32* cnt_id_2nd = cnt_id + cmd->n_entries;
chunk = (TmdContentChunk*) (tmd + 1);
memset(cnt_id, 0xFF, cmd->n_entries * sizeof(u32));
for (u32 i = 0; (i < content_count) && (i < TMD_MAX_CONTENTS); i++, chunk++) {
u32 chunk_id = getbe32(chunk->id);
cnt_id[getbe16(chunk->index)] = chunk_id;
*(cnt_id_2nd++) = chunk_id;
}
// bubble sort the second content id list
bool bs_finished = false;
cnt_id_2nd = cnt_id + cmd->n_entries;
while (!bs_finished) {
bs_finished = true;
for (u32 b = 1; b < cmd->n_cmacs; b++) {
if (cnt_id_2nd[b] < cnt_id_2nd[b-1]) {
u32 swp = cnt_id_2nd[b];
cnt_id_2nd[b] = cnt_id_2nd[b-1];
cnt_id_2nd[b-1] = swp;
bs_finished = false;
}
}
}
// set CMACs to 0x00
u8* cnt_cmac = (u8*) (cnt_id_2nd + cmd->n_cmacs);
memset(cnt_cmac, 0x00, 0x10 * cmd->n_entries);
// we still need to fix / set the CMACs inside the CMD file!
return cmd;
}

View File

@ -1,7 +1,12 @@
#pragma once
#include "common.h"
#include "tmd.h"
#define CMD_SIZE(cmd) (sizeof(CmdHeader) + \
(((cmd)->n_entries) * sizeof(u32)) + \
(((cmd)->n_cmacs) * sizeof(u32)) + \
(((cmd)->unknown) ? (((cmd)->n_entries) * 0x10) : 0))
// from: http://3dbrew.org/wiki/Titles#Data_Structure
typedef struct {
@ -15,4 +20,4 @@ typedef struct {
// followed by <n_entries> CMACs (may contain garbage)
} __attribute__((packed, aligned(4))) CmdHeader;
u32 CheckCmdSize(CmdHeader* cmd, u64 fsize);
CmdHeader* BuildAllocCmdData(TitleMetaData* tmd);

View File

@ -1,4 +1,5 @@
#include "codelzss.h"
#include "language.h"
#include "ui.h"
#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
while ((ptr_in > comp_start) && (ptr_out > comp_start)) {
if (!ShowProgress(data_end - ptr_out, data_end - data_start, "Decompressing .code...")) {
if (ShowPrompt(true, "Decompressing .code...\nB button detected. Cancel?")) return 1;
ShowProgress(0, data_end - data_start, "Decompressing .code...");
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, "%s", STR_DECOMPRESSING_DOT_CODE_B_DETECTED_CANCEL)) return 1;
ShowProgress(0, data_end - data_start, STR_DECOMPRESSING_DOT_CODE);
ShowProgress(data_end - ptr_out, data_end - data_start, STR_DECOMPRESSING_DOT_CODE);
}
// sanity check
@ -242,13 +243,13 @@ bool CompressCodeLzss(const u8* a_pUncompressed, u32 a_uUncompressedSize, u8* a_
u8* pDest = a_pCompressed + a_uUncompressedSize;
while (pSrc - a_pUncompressed > 0 && pDest - a_pCompressed > 0) {
if (!ShowProgress((u32)(a_pUncompressed + a_uUncompressedSize - pSrc), a_uUncompressedSize, "Compressing .code...")) {
if (ShowPrompt(true, "Compressing .code...\nB button detected. Cancel?")) {
if (!ShowProgress((u32)(a_pUncompressed + a_uUncompressedSize - pSrc), a_uUncompressedSize, STR_COMPRESSING_DOT_CODE)) {
if (ShowPrompt(true, "%s", STR_COMPRESSING_DOT_CODE_B_DETECTED_CANCEL)) {
bResult = false;
break;
}
ShowProgress(0, a_uUncompressedSize, "Compressing .code...");
ShowProgress((u32)(a_pUncompressed + a_uUncompressedSize - pSrc), a_uUncompressedSize, "Compressing .code...");
ShowProgress(0, a_uUncompressedSize, STR_COMPRESSING_DOT_CODE);
ShowProgress((u32)(a_pUncompressed + a_uUncompressedSize - pSrc), a_uUncompressedSize, STR_COMPRESSING_DOT_CODE);
}
u8* pFlag = --pDest;

View File

@ -5,6 +5,98 @@
#define GET_DPFS_BIT(b, lvl) (((((u32*) (void*) lvl)[b >> 5]) >> (31 - (b % 32))) & 1)
typedef struct {
u8 magic[8]; // "DISA" 0x00040000
u32 n_partitions;
u8 padding0[4];
u64 offset_table1;
u64 offset_table0;
u64 size_table;
u64 offset_descA;
u64 size_descA;
u64 offset_descB;
u64 size_descB;
u64 offset_partitionA;
u64 size_partitionA;
u64 offset_partitionB;
u64 size_partitionB;
u8 active_table; // 0 or 1
u8 padding1[3];
u8 hash_table[0x20]; // for the active table
u8 unused[0x74];
} PACKED_STRUCT DisaHeader;
typedef struct {
u8 magic[8]; // "DIFF" 0x00030000
u64 offset_table1; // also desc offset
u64 offset_table0; // also desc offset
u64 size_table; // includes desc size
u64 offset_partition;
u64 size_partition;
u32 active_table; // 0 or 1
u8 hash_table[0x20]; // for the active table
u64 unique_id; // see: http://3dbrew.org/wiki/Extdata
u8 unused[0xA4];
} PACKED_STRUCT DiffHeader;
typedef struct {
u8 magic[8]; // "DIFI" 0x00010000
u64 offset_ivfc; // always 0x44
u64 size_ivfc; // always 0x78
u64 offset_dpfs; // always 0xBC
u64 size_dpfs; // always 0x50
u64 offset_hash; // always 0x10C
u64 size_hash; // may include padding
u8 ivfc_use_extlvl4;
u8 dpfs_lvl1_selector;
u8 padding[2];
u64 ivfc_offset_extlvl4;
} PACKED_STRUCT DifiHeader;
typedef struct {
u8 magic[8]; // "IVFC" 0x00020000
u64 size_hash; // same as the one in DIFI, may include padding
u64 offset_lvl1;
u64 size_lvl1;
u32 log_lvl1;
u8 padding0[4];
u64 offset_lvl2;
u64 size_lvl2;
u32 log_lvl2;
u8 padding1[4];
u64 offset_lvl3;
u64 size_lvl3;
u32 log_lvl3;
u8 padding2[4];
u64 offset_lvl4;
u64 size_lvl4;
u64 log_lvl4;
u64 size_ivfc; // 0x78
} PACKED_STRUCT IvfcDescriptor;
typedef struct {
u8 magic[8]; // "DPFS" 0x00010000
u64 offset_lvl1;
u64 size_lvl1;
u32 log_lvl1;
u8 padding0[4];
u64 offset_lvl2;
u64 size_lvl2;
u32 log_lvl2;
u8 padding1[4];
u64 offset_lvl3;
u64 size_lvl3;
u32 log_lvl3;
u8 padding2[4];
} PACKED_STRUCT DpfsDescriptor;
typedef struct {
DifiHeader difi;
IvfcDescriptor ivfc;
DpfsDescriptor dpfs;
u8 hash[0x20];
u8 padding[4]; // all zeroes when encrypted
} PACKED_STRUCT DifiStruct;
static FIL ddfile;
static FIL* ddfp = NULL;
@ -67,11 +159,11 @@ inline static FRESULT DisaDiffQWrite(const TCHAR* path, const void* buf, UINT of
}
u32 GetDisaDiffRWInfo(const char* path, DisaDiffRWInfo* info, bool partitionB) {
const u8 disa_magic[] = { DISA_MAGIC };
const u8 diff_magic[] = { DIFF_MAGIC };
const u8 ivfc_magic[] = { IVFC_MAGIC };
const u8 dpfs_magic[] = { DPFS_MAGIC };
const u8 difi_magic[] = { DIFI_MAGIC };
static const u8 disa_magic[] = { DISA_MAGIC };
static const u8 diff_magic[] = { DIFF_MAGIC };
static const u8 ivfc_magic[] = { IVFC_MAGIC };
static const u8 dpfs_magic[] = { DPFS_MAGIC };
static const u8 difi_magic[] = { DIFI_MAGIC };
// reset reader info
memset(info, 0x00, sizeof(DisaDiffRWInfo));

View File

@ -14,99 +14,6 @@
#define DIFI_MAGIC 'D', 'I', 'F', 'I', 0x00, 0x00, 0x01, 0x00
typedef struct {
u8 magic[8]; // "DISA" 0x00040000
u32 n_partitions;
u8 padding0[4];
u64 offset_table1;
u64 offset_table0;
u64 size_table;
u64 offset_descA;
u64 size_descA;
u64 offset_descB;
u64 size_descB;
u64 offset_partitionA;
u64 size_partitionA;
u64 offset_partitionB;
u64 size_partitionB;
u8 active_table; // 0 or 1
u8 padding1[3];
u8 hash_table[0x20]; // for the active table
u8 unused[0x74];
} PACKED_STRUCT DisaHeader;
typedef struct {
u8 magic[8]; // "DIFF" 0x00030000
u64 offset_table1; // also desc offset
u64 offset_table0; // also desc offset
u64 size_table; // includes desc size
u64 offset_partition;
u64 size_partition;
u32 active_table; // 0 or 1
u8 hash_table[0x20]; // for the active table
u64 unique_id; // see: http://3dbrew.org/wiki/Extdata
u8 unused[0xA4];
} PACKED_STRUCT DiffHeader;
typedef struct {
u8 magic[8]; // "DIFI" 0x00010000
u64 offset_ivfc; // always 0x44
u64 size_ivfc; // always 0x78
u64 offset_dpfs; // always 0xBC
u64 size_dpfs; // always 0x50
u64 offset_hash; // always 0x10C
u64 size_hash; // may include padding
u8 ivfc_use_extlvl4;
u8 dpfs_lvl1_selector;
u8 padding[2];
u64 ivfc_offset_extlvl4;
} PACKED_STRUCT DifiHeader;
typedef struct {
u8 magic[8]; // "IVFC" 0x00020000
u64 size_hash; // same as the one in DIFI, may include padding
u64 offset_lvl1;
u64 size_lvl1;
u32 log_lvl1;
u8 padding0[4];
u64 offset_lvl2;
u64 size_lvl2;
u32 log_lvl2;
u8 padding1[4];
u64 offset_lvl3;
u64 size_lvl3;
u32 log_lvl3;
u8 padding2[4];
u64 offset_lvl4;
u64 size_lvl4;
u64 log_lvl4;
u64 size_ivfc; // 0x78
} PACKED_STRUCT IvfcDescriptor;
typedef struct {
u8 magic[8]; // "DPFS" 0x00010000
u64 offset_lvl1;
u64 size_lvl1;
u32 log_lvl1;
u8 padding0[4];
u64 offset_lvl2;
u64 size_lvl2;
u32 log_lvl2;
u8 padding1[4];
u64 offset_lvl3;
u64 size_lvl3;
u32 log_lvl3;
u8 padding2[4];
} PACKED_STRUCT DpfsDescriptor;
typedef struct {
DifiHeader difi;
IvfcDescriptor ivfc;
DpfsDescriptor dpfs;
u8 hash[0x20];
u8 padding[4]; // all zeroes when encrypted
} PACKED_STRUCT DifiStruct;
// condensed info to enable reading/writing IVFC lvl4
typedef struct {
u32 offset_table;

View File

@ -12,18 +12,19 @@
// valid addresses for FIRM section loading
// pairs of start / end address, provided by Wolfvak
#define FIRM_VALID_ADDRESS \
0x08000040, 0x08100000, \
0x18000000, 0x18600000, \
0x1FF00000, 0x1FFFFC00
// valid addresses (installable) for FIRM section loading
#define FIRM_VALID_ADDRESS_INSTALL \
FIRM_VALID_ADDRESS, \
0x08000040, 0x080F7FFF, \
0x10000000, 0x10200000
// valid addresses (bootable) for FIRM section loading
#define FIRM_VALID_ADDRESS_BOOT \
FIRM_VALID_ADDRESS, \
0x08000040, 0x08100000, \
0x20000000, 0x27FFFA00
static const u32 whitelist_boot[] = { FIRM_VALID_ADDRESS_BOOT };

View File

@ -14,6 +14,10 @@
#include "tad.h"
#include "3dsx.h"
#include "tmd.h"
#include "ticket.h"
#include "tie.h"
#include "cmd.h"
#include "bdri.h"
#include "ticketdb.h"
#include "ncchinfo.h"
#include "cifinish.h"

View File

@ -7,7 +7,7 @@
0x84, 0x9D, 0xA0, 0xD5, 0x6F, 0x5A, 0x34, 0xC4, 0x81, 0x06, 0x0C, 0x9F, 0xF2, 0xFA, 0xD8, 0x18
u32 ValidateAgbSaveHeader(AgbSaveHeader* header) {
u8 magic[] = { AGBSAVE_MAGIC };
static u8 magic[] = { AGBSAVE_MAGIC };
// basic checks
if ((memcmp(header->magic, magic, sizeof(magic)) != 0) ||
@ -28,7 +28,7 @@ u32 ValidateAgbSaveHeader(AgbSaveHeader* header) {
// http://problemkaputt.de/gbatek.htm#gbacartridgeheader
u32 ValidateAgbHeader(AgbHeader* agb) {
const u8 logo_sha[0x20] = { AGBLOGO_SHA256 };
static const u8 logo_sha[0x20] = { AGBLOGO_SHA256 };
u8 logo[0x9C] __attribute__((aligned(4)));
// check fixed value
@ -87,3 +87,17 @@ u32 ValidateAgbHeader(AgbHeader* agb) {
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;
}
}

View File

@ -1,6 +1,7 @@
#pragma once
#include "common.h"
#include "language.h"
#define GBAVC_MAGIC '.', 'C', 'A', 'A'
#define AGBSAVE_MAGIC '.', 'S', 'A', 'V'
@ -28,16 +29,6 @@
((size) == GBASAVE_FLASH_64K) || \
((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
// still a lot of unknowns in here, also redundant stuff left out
@ -89,5 +80,8 @@ typedef struct {
} __attribute__((packed, aligned(16))) AgbHeader;
u32 ValidateAgbSaveHeader(AgbSaveHeader* header);
u32 ValidateAgbHeader(AgbHeader* agb);
const char* AgbDestStr(const char* code);

View File

@ -5,6 +5,7 @@
#include "ips.h"
#include "common.h"
#include "fsperm.h"
#include "language.h"
#include "ui.h"
#include "vff.h"
@ -30,21 +31,21 @@ char errName[256];
int displayError(int errcode) {
switch(errcode) {
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:
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:
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:
ShowPrompt(false, "%s\nThe patch is invalid.", errName); break;
ShowPrompt(false, "%s\n%s", errName, STR_PATCH_IS_INVALID); break;
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:
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:
ShowPrompt(false, "%s\nPatching canceled.", errName); break;
ShowPrompt(false, "%s\n%s", errName, STR_PATCHING_CANCELED); break;
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(&inFile);
@ -112,7 +113,7 @@ UINT read24() {
int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName) {
int error = IPS_INVALID;
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);
patchSize = fvx_size(&patchFile);
@ -140,7 +141,7 @@ int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName
while (offset != 0x454F46) // 454F46=EOF
{
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(patchOffset, patchSize, patchName);
}
@ -211,7 +212,7 @@ int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName
while (offset != 0x454F46)
{
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(offset, outSize, outName);
}

View File

@ -1,10 +1,7 @@
#include "ncch.h"
#include "support.h"
#include "disadiff.h"
#include "keydb.h"
#include "aes.h"
#include "sha.h"
#include "ff.h"
#define EXEFS_KEYID(name) (((strncmp(name, "banner", 8) == 0) || (strncmp(name, "icon", 8) == 0)) ? 0 : 1)
@ -51,100 +48,6 @@ u32 GetNcchCtr(u8* ctr, NcchHeader* ncch, u8 section) {
return 0;
}
u32 GetNcchSeed(u8* seed, NcchHeader* ncch) {
static u8 lseed[16+8] __attribute__((aligned(4))) = { 0 }; // seed plus title ID for easy validation
u64 titleId = ncch->programId;
u32 hash_seed = ncch->hash_seed;
u32 sha256sum[8];
memcpy(lseed+16, &(ncch->programId), 8);
sha_quick(sha256sum, lseed, 16 + 8, SHA256_MODE);
if (hash_seed == sha256sum[0]) {
memcpy(seed, lseed, 16);
return 0;
}
// setup a large enough buffer
u8* buffer = (u8*) malloc(max(STD_BUFFER_SIZE, SEEDSAVE_AREA_SIZE));
if (!buffer) return 1;
// try to grab the seed from NAND database
const char* nand_drv[] = {"1:", "4:"}; // SysNAND and EmuNAND
for (u32 i = 0; i < countof(nand_drv); i++) {
UINT btr = 0;
FIL file;
char path[128];
// grab the key Y from movable.sed
u8 movable_keyy[16];
snprintf(path, 128, "%s/private/movable.sed", nand_drv[i]);
if (f_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
continue;
f_lseek(&file, 0x110);
f_read(&file, movable_keyy, 0x10, &btr);
f_close(&file);
// build the seed save path
sha_quick(sha256sum, movable_keyy, 0x10, SHA256_MODE);
snprintf(path, 128, "%s/data/%08lX%08lX%08lX%08lX/sysdata/0001000F/00000000",
nand_drv[i], sha256sum[0], sha256sum[1], sha256sum[2], sha256sum[3]);
// check seedsave for seed
u8* seeddb = buffer;
if (ReadDisaDiffIvfcLvl4(path, NULL, SEEDSAVE_AREA_OFFSET, SEEDSAVE_AREA_SIZE, seeddb) != SEEDSAVE_AREA_SIZE)
continue;
// search for the seed
for (u32 s = 0; s < SEEDSAVE_MAX_ENTRIES; s++) {
if (titleId != getle64(seeddb + (s*8))) continue;
memcpy(lseed, seeddb + (SEEDSAVE_MAX_ENTRIES*8) + (s*16), 16);
sha_quick(sha256sum, lseed, 16 + 8, SHA256_MODE);
if (hash_seed == sha256sum[0]) {
memcpy(seed, lseed, 16);
free(buffer);
return 0; // found!
}
}
}
// not found -> try seeddb.bin
SeedInfo* seeddb = (SeedInfo*) (void*) buffer;
size_t len = LoadSupportFile(SEEDDB_NAME, seeddb, STD_BUFFER_SIZE);
if (len && (seeddb->n_entries <= (len - 16) / 32)) { // check filesize / seeddb size
for (u32 s = 0; s < seeddb->n_entries; s++) {
if (titleId != seeddb->entries[s].titleId)
continue;
memcpy(lseed, seeddb->entries[s].seed, 16);
sha_quick(sha256sum, lseed, 16 + 8, SHA256_MODE);
if (hash_seed == sha256sum[0]) {
memcpy(seed, lseed, 16);
free(buffer);
return 0; // found!
}
}
}
// out of options -> failed!
free(buffer);
return 1;
}
u32 AddSeedToDb(SeedInfo* seed_info, SeedInfoEntry* seed_entry) {
if (!seed_entry) { // no seed entry -> reset database
memset(seed_info, 0, 16);
return 0;
}
// check if entry already in DB
u32 n_entries = seed_info->n_entries;
SeedInfoEntry* seed = seed_info->entries;
for (u32 i = 0; i < n_entries; i++, seed++)
if (seed->titleId == seed_entry->titleId) return 0;
// actually a new seed entry
memcpy(seed, seed_entry, sizeof(SeedInfoEntry));
seed_info->n_entries++;
return 0;
}
u32 SetNcchKey(NcchHeader* ncch, u16 crypto, u32 keyid) {
u8 flags3 = (crypto >> 8) & 0xFF;
u8 flags7 = crypto & 0xFF;
@ -177,7 +80,7 @@ u32 SetNcchKey(NcchHeader* ncch, u16 crypto, u32 keyid) {
if ((memcmp(lsignature, ncch->signature, 16) != 0) || (ltitleId != ncch->programId)) {
u8 keydata[16+16] __attribute__((aligned(4)));
memcpy(keydata, ncch->signature, 16);
if (GetNcchSeed(keydata + 16, ncch) != 0)
if (FindSeed(keydata + 16, ncch->programId, ncch->hash_seed) != 0)
return 1;
sha_quick(seedkeyY, keydata, 32, SHA256_MODE);
memcpy(lsignature, ncch->signature, 16);
@ -357,3 +260,12 @@ u32 SetNcchSdFlag(void* data) { // data must be at least 0x600 byte and start wi
return 0;
}
u32 SetupSystemForNcch(NcchHeader* ncch, bool to_emunand) {
u16 crypto = NCCH_GET_CRYPTO(ncch);
if ((crypto & 0x20) && // seed crypto
(SetupSeedSystemCrypto(ncch->programId, ncch->hash_seed, to_emunand) != 0) &&
(SetupSeedPrePurchase(ncch->programId, to_emunand) != 0))
return 1;
return 0;
}

View File

@ -2,6 +2,7 @@
#include "common.h"
#include "exefs.h"
#include "seedsave.h"
#define NCCH_MEDIA_UNIT 0x200
@ -17,13 +18,6 @@
#define NCCH_STDCRYPTO 0x0000
#define NCCH_GET_CRYPTO(ncch) (!NCCH_ENCRYPTED(ncch) ? NCCH_NOCRYPTO : (((ncch)->flags[3] << 8) | ((ncch)->flags[7]&(0x01|0x20))))
#define SEEDDB_NAME "seeddb.bin"
#define SEEDDB_SIZE(sdb) (16 + ((sdb)->n_entries * sizeof(SeedInfoEntry)))
#define SEEDSAVE_MAX_ENTRIES 2000
#define SEEDSAVE_AREA_OFFSET 0x4000
#define SEEDSAVE_AREA_SIZE (SEEDSAVE_MAX_ENTRIES * (8+16))
// wrapper defines
#define DecryptNcch(data, offset, size, ncch, exefs) CryptNcch(data, offset, size, ncch, exefs, NCCH_NOCRYPTO)
#define EncryptNcch(data, offset, size, ncch, exefs, crypto) CryptNcch(data, offset, size, ncch, exefs, crypto)
@ -89,23 +83,10 @@ typedef struct {
u8 hash_romfs[0x20];
} __attribute__((packed, aligned(16))) NcchHeader;
typedef struct {
u64 titleId;
u8 seed[16];
u8 reserved[8];
} PACKED_STRUCT SeedInfoEntry;
typedef struct {
u32 n_entries;
u8 padding[12];
SeedInfoEntry entries[256]; // this number is only a placeholder
} PACKED_STRUCT SeedInfo;
u32 ValidateNcchHeader(NcchHeader* header);
u32 SetNcchKey(NcchHeader* ncch, u16 crypto, u32 keyid);
u32 SetupNcchCrypto(NcchHeader* ncch, u16 crypt_to);
u32 CryptNcch(void* data, u32 offset, u32 size, NcchHeader* ncch, ExeFsHeader* exefs, u16 crypto);
u32 CryptNcchSequential(void* data, u32 offset, u32 size, u16 crypto);
u32 SetNcchSdFlag(void* data);
u32 AddSeedToDb(SeedInfo* seed_info, SeedInfoEntry* seed_entry);
u32 SetupSystemForNcch(NcchHeader* ncch, bool to_emunand);

View File

@ -2,7 +2,7 @@
#include "ncch.h"
u32 ValidateNcsdHeader(NcsdHeader* header) {
u8 zeroes[16] = { 0 };
static const u8 zeroes[16] = { 0 };
if ((memcmp(header->magic, "NCSD", 4) != 0) || // check magic number
(memcmp(header->partitions_fs_type, zeroes, 8) != 0) || !header->mediaId) // prevent detection of NAND images
return 1;

View File

@ -1,4 +1,5 @@
#include "nds.h"
#include "fatmbr.h"
#include "vff.h"
#include "crc16.h"
#include "utf.h"
@ -26,6 +27,87 @@ u32 ValidateTwlHeader(TwlHeader* twl) {
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) {
const u16 sct_size = 0x200;
if (size / (u32) sct_size > 0xFFFF)
return 1;
// fit max number of sectors into size
// that's how Nintendo does it ¯\_(ツ)_/¯
const u16 n_sct_max = size / sct_size;
u16 n_sct = 1;
u16 sct_track = 1;
u16 sct_heads = 1;
u16 n_sct_next = 0;
while (n_sct_next <= n_sct_max) {
n_sct_next = sct_track * (sct_heads + 1) * (sct_heads + 1);
if (n_sct_next <= n_sct_max) {
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_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)
u8 clr_size = (n_sct > 8 * 1024) ? 8 : (n_sct > 1024) ? 4 : 1;
// how many FAT sectors do we need?
u16 tot_clr = align(n_sct, clr_size) / clr_size;
u32 fat_byte = (align(tot_clr, 2) / 2) * 3; // 2 sectors -> 3 byte
u16 fat_size = align(fat_byte, sct_size) / sct_size;
// build the FAT header
Fat16Header* fat = sav;
memset(fat, 0x00, sizeof(Fat16Header));
fat->jmp[0] = 0xE9; // E9 00 00
memcpy(fat->oemname, "MSWIN4.1", 8);
fat->sct_size = sct_size; // 512 byte / sector
fat->clr_size = clr_size; // sectors per cluster
fat->sct_reserved = 0x0001; // 1 reserved sector
fat->fat_n = 0x02; // 2 FATs
fat->root_n = 0x0020; // 32 root dir entries (2 sectors)
fat->reserved0 = n_sct; // sectors in filesystem
fat->mediatype = 0xF8; // "hard disk"
fat->fat_size = fat_size; // sectors per fat (1 sector)
fat->sct_track = sct_track; // sectors per track (legacy? see above)
fat->sct_heads = sct_heads; // sectors per head (legacy? see above)
fat->ndrive = 0x05; // for whatever reason
fat->boot_sig = 0x29; // "boot signature"
fat->vol_id = 0x12345678; // volume id
memcpy(fat->vol_label, "VOLUMELABEL", 11); // standard volume label
memcpy(fat->fs_type, "FAT12 ", 8); // filesystem type
fat->magic = 0xAA55;
return 0;
}
u32 LoadTwlMetaData(const char* path, TwlHeader* hdr, TwlIconData* icon) {
u8 ALIGN(32) ntr_header[0x200]; // we only need the NTR header (ignore TWL stuff)
TwlHeader* twl = hdr ? hdr : (void*) ntr_header;
@ -65,11 +147,19 @@ u32 GetTwlIcon(u16* icon, const TwlIconData* twl_icon) {
u32 ix = x + (i & 0x7);
u32 iy = y + (i >> 3);
pix555 = palette[((i%2) ? (*pix4 >> 4) : *pix4) & 0xF];
int palette_index = ((i%2) ? (*pix4 >> 4) : *pix4) & 0xF;
if (palette_index) {
pix555 = palette[palette_index];
r = pix555 & 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;
if (i % 2) pix4++;
}

View File

@ -102,28 +102,33 @@ typedef struct {
// extended mode stuff (DSi only)
u8 ignored0[0x30]; // ignored
u32 region_flags;
u8 ignored1[0xC]; // ignored
u32 access_control;
u32 arm7_scfg_ext7;
u8 reserved2[3];
u8 srl_flag;
u32 arm9i_rom_offset;
u32 reserved2;
u32 reserved3;
u32 arm9i_load_adress;
u32 arm9i_size;
u32 arm7i_rom_offset;
u32 unknown1;
u32 arm7i_load_adress;
u32 arm7i_size;
u8 ignored2[0x30]; // ignored
u8 ignored1[0x30]; // ignored
u32 ntr_twl_rom_size;
u8 unknown2[12];
u8 ignored3[0x10]; // ignored
u8 ignored2[0x10]; // ignored
u64 title_id;
u32 pubsav_size;
u32 prvsav_size;
u8 reserved3[176];
u8 reserved4[176];
u8 unknown3[0x10];
u8 ignored4[0xD00]; // ignored
u8 ignored3[0xD00]; // ignored
} PACKED_STRUCT TwlHeader;
u32 ValidateTwlHeader(TwlHeader* twl);
u32 VerifyTwlIconData(TwlIconData* icon, u32 size);
u32 BuildTwlSaveHeader(void* sav, u32 size);
u32 LoadTwlMetaData(const char* path, TwlHeader* hdr, TwlIconData* icon);
u32 GetTwlTitle(char* desc, const TwlIconData* twl_icon);
u32 GetTwlIcon(u16* icon, const TwlIconData* twl_icon);

View File

@ -1,4 +1,5 @@
#include "common.h"
#include "language.h"
#include "region.h"
// Names of system regions, short form.
@ -12,13 +13,16 @@ const char* const g_regionNamesShort[SMDH_NUM_REGIONS] = {
"TWN",
};
// Names of system regions, long form.
const char* const g_regionNamesLong[SMDH_NUM_REGIONS] = {
"Japan",
"Americas",
"Europe",
"Australia",
"China",
"Korea",
"Taiwan",
// Names of system regions, long form and translatable.
const char* regionNameLong(int region) {
switch(region) {
case REGION_JPN: return STR_REGION_JAPAN;
case REGION_USA: return STR_REGION_AMERICAS;
case REGION_EUR: return STR_REGION_EUROPE;
case REGION_AUS: return STR_REGION_AUSTRALIA;
case REGION_CHN: return STR_REGION_CHINA;
case REGION_KOR: return STR_REGION_KOREA;
case REGION_TWN: return STR_REGION_TAIWAN;
default: return STR_REGION_UNKNOWN;
}
};

View File

@ -27,5 +27,5 @@
// Names of system regions, short form.
extern const char* const g_regionNamesShort[SMDH_NUM_REGIONS];
// Names of system regions, long form.
extern const char* const g_regionNamesLong[SMDH_NUM_REGIONS];
// Names of system regions, long form and translatable.
const char* regionNameLong(int region);

View File

@ -19,7 +19,7 @@ u64 GetRomFsLvOffset(RomFsIvfcHeader* ivfc, u32 lvl) {
// validate IVFC header by checking offsets and hash sizes
u32 ValidateRomFsHeader(RomFsIvfcHeader* ivfc, u32 max_size) {
u8 magic[] = { ROMFS_MAGIC };
static const u8 magic[] = { ROMFS_MAGIC };
// check magic number
if (memcmp(magic, ivfc->magic, sizeof(magic)) != 0)

240
arm9/source/game/seedsave.c Normal file
View File

@ -0,0 +1,240 @@
#include "seedsave.h"
#include "support.h"
#include "nandcmac.h"
#include "sha.h"
#include "ff.h"
#define TITLETAG_MAX_ENTRIES 2000 // same as SEEDSAVE_MAX_ENTRIES
#define TITLETAG_AREA_OFFSET 0x10000 // thanks @luigoalma
// this structure is 0x80 bytes, thanks @luigoalma
typedef struct {
char magic[4]; // "PREP" for prepurchase install. NIM excepts "PREP" to do seed downloads on the background.
// playable date parameters
// 2000-01-01 is a safe bet for a stub entry
s32 year;
u8 month;
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.
// everything after this point can be 0 padded
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_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.
// It's memset to 0 by NIM
u8 unknown[0x68];
} PACKED_STRUCT TitleTagEntry;
typedef struct {
u32 unknown0;
u32 n_entries;
u8 unknown1[0x1000 - 0x8];
u64 titleId[TITLETAG_MAX_ENTRIES];
TitleTagEntry tag[TITLETAG_MAX_ENTRIES];
} PACKED_STRUCT TitleTag;
u32 GetSeedPath(char* path, const char* drv) {
u8 movable_keyy[16] = { 0 };
u32 sha256sum[8];
UINT btr = 0;
FIL file;
// grab the key Y from movable.sed
// wrong result if movable.sed does not have it
snprintf(path, 128, "%2.2s/private/movable.sed", drv);
if (f_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return 1;
f_lseek(&file, 0x110);
f_read(&file, movable_keyy, 0x10, &btr);
f_close(&file);
if (btr != 0x10)
return 1;
// build the seed save path
sha_quick(sha256sum, movable_keyy, 0x10, SHA256_MODE);
snprintf(path, 128, "%2.2s/data/%08lX%08lX%08lX%08lX/sysdata/0001000F/00000000",
drv, sha256sum[0], sha256sum[1], sha256sum[2], sha256sum[3]);
return 0;
}
u32 FindSeed(u8* seed, u64 titleId, u32 hash_seed) {
static u8 lseed[16+8] __attribute__((aligned(4))) = { 0 }; // seed plus title ID for easy validation
u32 sha256sum[8];
memcpy(lseed+16, &titleId, 8);
sha_quick(sha256sum, lseed, 16 + 8, SHA256_MODE);
if (hash_seed == sha256sum[0]) {
memcpy(seed, lseed, 16);
return 0;
}
// setup a large enough buffer
u8* buffer = (u8*) malloc(max(STD_BUFFER_SIZE, sizeof(SeedDb)));
if (!buffer) return 1;
// try to grab the seed from NAND database
const char* nand_drv[] = {"1:", "4:"}; // SysNAND and EmuNAND
for (u32 i = 0; i < countof(nand_drv); i++) {
char path[128];
SeedDb* seeddb = (SeedDb*) (void*) buffer;
// read SEEDDB from file
if (GetSeedPath(path, nand_drv[i]) != 0) continue;
if ((ReadDisaDiffIvfcLvl4(path, NULL, SEEDSAVE_AREA_OFFSET, sizeof(SeedDb), seeddb) != sizeof(SeedDb)) ||
(seeddb->n_entries > SEEDSAVE_MAX_ENTRIES))
continue;
// search for the seed
for (u32 s = 0; s < seeddb->n_entries; s++) {
if (titleId != seeddb->titleId[s]) continue;
memcpy(lseed, &(seeddb->seed[s]), sizeof(Seed));
sha_quick(sha256sum, lseed, 16 + 8, SHA256_MODE);
if (hash_seed == sha256sum[0]) {
memcpy(seed, lseed, 16);
free(buffer);
return 0; // found!
}
}
}
// not found -> try seeddb.bin
SeedInfo* seeddb = (SeedInfo*) (void*) buffer;
size_t len = LoadSupportFile(SEEDINFO_NAME, seeddb, STD_BUFFER_SIZE);
if (len && (seeddb->n_entries <= (len - 16) / 32)) { // check filesize / seeddb size
for (u32 s = 0; s < seeddb->n_entries; s++) {
if (titleId != seeddb->entries[s].titleId)
continue;
memcpy(lseed, &(seeddb->entries[s].seed), sizeof(Seed));
sha_quick(sha256sum, lseed, 16 + 8, SHA256_MODE);
if (hash_seed == sha256sum[0]) {
memcpy(seed, lseed, 16);
free(buffer);
return 0; // found!
}
}
}
// out of options -> failed!
free(buffer);
return 1;
}
u32 AddSeedToDb(SeedInfo* seed_info, SeedInfoEntry* seed_entry) {
if (!seed_entry) { // no seed entry -> reset database
memset(seed_info, 0, 16);
return 0;
}
// check if entry already in DB
u32 n_entries = seed_info->n_entries;
SeedInfoEntry* seed = seed_info->entries;
for (u32 i = 0; i < n_entries; i++, seed++)
if (seed->titleId == seed_entry->titleId) return 0;
// actually a new seed entry
memcpy(seed, seed_entry, sizeof(SeedInfoEntry));
seed_info->n_entries++;
return 0;
}
u32 InstallSeedDbToSystem(SeedInfo* seed_info, bool to_emunand) {
char path[128];
SeedDb* seeddb = (SeedDb*) malloc(sizeof(SeedDb));
if (!seeddb) return 1;
// read the current SEEDDB database
if ((GetSeedPath(path, to_emunand ? "4:" : "1:") != 0) ||
(ReadDisaDiffIvfcLvl4(path, NULL, SEEDSAVE_AREA_OFFSET, sizeof(SeedDb), seeddb) != sizeof(SeedDb)) ||
(seeddb->n_entries >= SEEDSAVE_MAX_ENTRIES)) {
free (seeddb);
return 1;
}
// find free slots, insert seeds from SeedInfo
for (u32 slot = 0, s = 0; s < seed_info->n_entries; s++) {
SeedInfoEntry* entry = &(seed_info->entries[s]);
for (slot = 0; slot < seeddb->n_entries; slot++)
if (seeddb->titleId[slot] == entry->titleId) break;
if (slot >= SEEDSAVE_MAX_ENTRIES) break;
if (slot >= seeddb->n_entries) seeddb->n_entries = slot + 1;
seeddb->titleId[slot] = entry->titleId;
memcpy(&(seeddb->seed[slot]), &(entry->seed), sizeof(Seed));
}
// write back to system (warning: no write protection checks here)
u32 size = WriteDisaDiffIvfcLvl4(path, NULL, SEEDSAVE_AREA_OFFSET, sizeof(SeedDb), seeddb);
FixFileCmac(path, false);
free (seeddb);
return (size == sizeof(SeedDb)) ? 0 : 1;
}
u32 SetupSeedPrePurchase(u64 titleId, bool to_emunand) {
// here, we ask the system to install the seed for us
TitleTag* titletag = (TitleTag*) malloc(sizeof(TitleTag));
if (!titletag) return 1;
char path[128];
if ((GetSeedPath(path, to_emunand ? "4:" : "1:") != 0) ||
(ReadDisaDiffIvfcLvl4(path, NULL, TITLETAG_AREA_OFFSET, sizeof(TitleTag), titletag) != sizeof(TitleTag)) ||
(titletag->n_entries >= TITLETAG_MAX_ENTRIES)) {
free (titletag);
return 1;
}
// pointers for TITLETAG title IDs and seeds
// find a free slot, insert titletag
u32 slot = 0;
for (; slot < titletag->n_entries; slot++)
if (titletag->titleId[slot] == titleId) break;
if (slot >= titletag->n_entries)
titletag->n_entries = slot + 1;
TitleTagEntry* ttag = &(titletag->tag[slot]);
titletag->titleId[slot] = titleId;
memset(ttag, 0, sizeof(TitleTagEntry));
memcpy(ttag->magic, "PREP", 4);
ttag->year = 2000;
ttag->month = 1;
ttag->day = 1;
ttag->country_code = 1;
// write back to system (warning: no write protection checks here)
u32 size = WriteDisaDiffIvfcLvl4(path, NULL, TITLETAG_AREA_OFFSET, sizeof(TitleTag), titletag);
FixFileCmac(path, false);
free(titletag);
return (size == sizeof(TitleTag)) ? 0 : 1;
}
u32 SetupSeedSystemCrypto(u64 titleId, u32 hash_seed, bool to_emunand) {
// attempt to find the seed inside the seeddb.bin support file
SeedInfo* seeddb = (SeedInfo*) malloc(STD_BUFFER_SIZE);
if (!seeddb) return 1;
size_t len = LoadSupportFile(SEEDINFO_NAME, seeddb, STD_BUFFER_SIZE);
if (len && (seeddb->n_entries <= (len - 16) / 32)) { // check filesize / seeddb size
for (u32 s = 0; s < seeddb->n_entries; s++) {
if (titleId != seeddb->entries[s].titleId)
continue;
// found a candidate, hash and verify it
u8 lseed[16+8] __attribute__((aligned(4))) = { 0 }; // seed plus title ID for easy validation
u32 sha256sum[8];
memcpy(lseed+16, &titleId, 8);
memcpy(lseed, &(seeddb->entries[s].seed), sizeof(Seed));
sha_quick(sha256sum, lseed, 16 + 8, SHA256_MODE);
u32 res = 0; // assuming the installed seed to be correct
if (hash_seed == sha256sum[0]) {
// found, install it
seeddb->n_entries = 1;
seeddb->entries[0].titleId = titleId;
memcpy(&(seeddb->entries[0].seed), lseed, sizeof(Seed));
res = InstallSeedDbToSystem(seeddb, to_emunand);
}
free(seeddb);
return res;
}
}
free(seeddb);
return 1;
}

View File

@ -0,0 +1,41 @@
#pragma once
#include "common.h"
#include "disadiff.h"
#define SEEDINFO_NAME "seeddb.bin"
#define SEEDINFO_SIZE(sdb) (16 + ((sdb)->n_entries * sizeof(SeedInfoEntry)))
#define SEEDSAVE_MAX_ENTRIES 2000
#define SEEDSAVE_AREA_OFFSET 0x3000
typedef struct {
u8 byte[16];
} PACKED_STRUCT Seed;
typedef struct {
u64 titleId;
Seed seed;
u8 reserved[8];
} PACKED_STRUCT SeedInfoEntry;
typedef struct {
u32 n_entries;
u8 padding[12];
SeedInfoEntry entries[SEEDSAVE_MAX_ENTRIES]; // this number is only a placeholder
} PACKED_STRUCT SeedInfo;
typedef struct {
u32 unknown0;
u32 n_entries;
u8 unknown1[0x1000 - 0x8];
u64 titleId[SEEDSAVE_MAX_ENTRIES];
Seed seed[SEEDSAVE_MAX_ENTRIES];
} PACKED_STRUCT SeedDb;
u32 GetSeedPath(char* path, const char* drv);
u32 FindSeed(u8* seed, u64 titleId, u32 hash_seed);
u32 AddSeedToDb(SeedInfo* seed_info, SeedInfoEntry* seed_entry);
u32 InstallSeedDbToSystem(SeedInfo* seed_info, bool to_emunand);
u32 SetupSeedPrePurchase(u64 titleId, bool to_emunand);
u32 SetupSeedSystemCrypto(u64 titleId, u32 hash_seed, bool to_emunand);

View File

@ -9,7 +9,7 @@
36, 37, 44, 45, 38, 39, 46, 47, 52, 53, 60, 61, 54, 55, 62, 63
u32 ConvertSmdhIcon(u16* icon, const u16* smdh_icon, u32 w, u32 h) {
const u32 lut[8*8] = { SMDH_LUT };
static const u32 lut[8*8] = { SMDH_LUT };
u16* pix565 = (u16*) smdh_icon;
for (u32 y = 0; y < h; y += 8) {
for (u32 x = 0; x < w; x += 8) {

View File

@ -1,6 +1,20 @@
#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) {
TadHeader* hdr = (TadHeader*) header;
TadContentTable* tbl = (TadContentTable*) table;

View File

@ -56,4 +56,14 @@ typedef struct {
u8 padding[0x4];
} 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);

View File

@ -6,83 +6,155 @@
#include "ff.h"
u32 ValidateTicket(Ticket* ticket) {
const u8 magic[] = { TICKET_SIG_TYPE };
static const u8 magic[] = { TICKET_SIG_TYPE };
if ((memcmp(ticket->sig_type, magic, sizeof(magic)) != 0) ||
((strncmp((char*) ticket->issuer, TICKET_ISSUER, 0x40) != 0) &&
(strncmp((char*) ticket->issuer, TICKET_ISSUER_DEV, 0x40) != 0)) ||
(ticket->commonkey_idx >= 6))
(ticket->commonkey_idx >= 6) ||
(getbe32(&ticket->content_index[0]) != 0x10014) ||
(getbe32(&ticket->content_index[4]) < 0x14) ||
(getbe32(&ticket->content_index[12]) != 0x10014) ||
(getbe32(&ticket->content_index[16]) != 0))
return 1;
return 0;
}
u32 ValidateTwlTicket(Ticket* ticket) {
static const u8 magic[] = { TICKET_SIG_TYPE_TWL };
if ((memcmp(ticket->sig_type, magic, sizeof(magic)) != 0) ||
(strncmp((char*) ticket->issuer, TICKET_ISSUER_TWL, 0x40) != 0))
return 1;
return 0;
}
u32 ValidateTicketSignature(Ticket* ticket) {
static bool got_modexp = false;
static u32 mod[0x100 / 0x4] = { 0 };
static u32 exp = 0;
Certificate cert;
if (!got_modexp) {
// grab mod/exp from cert from cert.db
if (LoadCertFromCertDb(0x3F10, NULL, mod, &exp) == 0)
got_modexp = true;
else return 1;
}
if (!RSA_setKey2048(3, mod, exp) ||
!RSA_verify2048((void*) &(ticket->signature), (void*) &(ticket->issuer), 0x210))
// grab cert from certs.db
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));
}
*ticket = _ticket;
*ticket_size = _ticket_size;
return 0;
}
u32 BuildFakeTicket(Ticket* ticket, u8* title_id) {
const u8 sig_type[4] = { TICKET_SIG_TYPE }; // RSA_2048 SHA256
const u8 ticket_cnt_index[] = { // whatever this is
0x00, 0x01, 0x00, 0x14, 0x00, 0x00, 0x00, 0xAC, 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x00, 0x14,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x84,
0x00, 0x00, 0x00, 0x84, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
// set ticket all zero for a clean start
memset(ticket, 0x00, sizeof(Ticket));
// 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));
u32 BuildFakeTicket(Ticket* ticket, const u8* title_id) {
Ticket* tik = NULL;
u32 ticket_size = sizeof(TicketCommon);
u32 res = BuildVariableFakeTicket(&tik, &ticket_size, title_id, TICKET_MAX_CONTENTS);
if (res != 0) return res;
memcpy(ticket, tik, ticket_size);
free(tik);
return 0;
}
u32 GetTicketContentIndexSize(const Ticket* ticket) {
return getbe32(&ticket->content_index[4]);
}
u32 GetTicketSize(const Ticket* ticket) {
return sizeof(Ticket) + GetTicketContentIndexSize(ticket);
}
u32 BuildTicketCert(u8* tickcert) {
const u8 cert_hash_expected[0x20] = {
static const u8 cert_hash_expected[0x20] = {
0xDC, 0x15, 0x3C, 0x2B, 0x8A, 0x0A, 0xC8, 0x74, 0xA9, 0xDC, 0x78, 0x61, 0x0E, 0x6A, 0x8F, 0xE3,
0xE6, 0xB1, 0x34, 0xD5, 0x52, 0x88, 0x73, 0xC9, 0x61, 0xFB, 0xC7, 0x95, 0xCB, 0x47, 0xE6, 0x97
};
const u8 cert_hash_expected_dev[0x20] = {
static const u8 cert_hash_expected_dev[0x20] = {
0x97, 0x2A, 0x32, 0xFF, 0x9D, 0x4B, 0xAA, 0x2F, 0x1A, 0x24, 0xCF, 0x21, 0x13, 0x87, 0xF5, 0x38,
0xC6, 0x4B, 0xD4, 0x8F, 0xDF, 0x13, 0x21, 0x3D, 0xFC, 0x72, 0xFC, 0x8D, 0x9F, 0xDD, 0x01, 0x0E
};
// open certs.db file on SysNAND
FIL db;
UINT bytes_read;
if (f_open(&db, "1:/dbs/certs.db", FA_READ | FA_OPEN_EXISTING) != FR_OK)
static const char* const retail_issuers[] = {"Root-CA00000003-XS0000000c", "Root-CA00000003"};
static const char* const dev_issuers[] = {"Root-CA00000004-XS00000009", "Root-CA00000004"};
size_t size = TICKET_CDNCERT_SIZE;
if (BuildRawCertBundleFromCertDb(tickcert, &size, !IS_DEVKIT ? retail_issuers : dev_issuers, 2) ||
size != TICKET_CDNCERT_SIZE) {
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
u8 cert_hash[0x20];
@ -92,3 +164,56 @@ u32 BuildTicketCert(u8* tickcert) {
return 0;
}
u32 TicketRightsCheck_InitContext(TicketRightsCheck* ctx, Ticket* ticket) {
if (!ticket || ValidateTicket(ticket)) return 1;
const TicketContentIndexMainHeader* mheader = (const TicketContentIndexMainHeader*)&ticket->content_index[0];
u32 dheader_pos = getbe32(&mheader->data_header_relative_offset[0]);
u32 cindex_size = getbe32(&mheader->content_index_size[0]);
// data header is not inbounds, so it's not valid for use
if (cindex_size < dheader_pos || dheader_pos + sizeof(TicketContentIndexDataHeader) > cindex_size) return 1;
const TicketContentIndexDataHeader* dheader = (const TicketContentIndexDataHeader*)&ticket->content_index[dheader_pos];
u32 data_pos = getbe32(&dheader->data_relative_offset[0]);
u32 count = getbe32(&dheader->max_entry_count[0]);
u32 data_max_size = cindex_size - data_pos;
count = min(data_max_size / sizeof(TicketRightsField), count);
// if no entries or data type isn't what we want or not enough space for at least one entry,
// it still is valid, but it will just follow other rules
if (count == 0 || getbe16(&dheader->data_type[0]) != 3) {
ctx->count = 0;
ctx->rights = NULL;
} else {
ctx->count = count;
ctx->rights = (const TicketRightsField*)&ticket->content_index[data_pos];
}
return 0;
}
bool TicketRightsCheck_CheckIndex(TicketRightsCheck* ctx, u16 index) {
if (ctx->count == 0) return index < 256; // when no fields, true if below 256
bool hasright = false;
// it loops until one of these happens:
// - we run out of bit fields
// - at the first encounter of an index offset field that's bigger than index
// - at the first encounter of a positive indicator of content rights
for (u32 i = 0; i < ctx->count; i++) {
u16 indexoffset = getbe16(&ctx->rights[i].indexoffset[0]);
if (index < indexoffset) break;
u16 bitpos = index - indexoffset;
if (bitpos >= 1024) continue; // not in this field
if (ctx->rights[i].rightsbitfield[bitpos / 8] & (1 << (bitpos % 8))) {
hasright = true;
break;
}
}
return hasright;
}

View File

@ -1,49 +1,108 @@
#pragma once
#include "common.h"
#include "tmd.h"
#define TICKET_SIZE sizeof(Ticket)
#define TICKET_COMMON_SIZE sizeof(TicketCommon)
#define TICKET_MINIMUM_SIZE sizeof(TicketMinimum)
#define TICKET_TWL_SIZE sizeof(Ticket)
#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_DEV "Root-CA00000004-XS00000009"
#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)
// from: https://github.com/profi200/Project_CTR/blob/02159e17ee225de3f7c46ca195ff0f9ba3b3d3e4/ctrtool/tik.h#L15-L39
// all numbers in big endian
#define TICKETBASE \
u8 sig_type[4]; \
u8 signature[0x100]; \
u8 padding1[0x3C]; \
u8 issuer[0x40]; \
u8 ecdsa[0x3C]; \
u8 version; \
u8 ca_crl_version; \
u8 signer_crl_version; \
u8 titlekey[0x10]; \
u8 reserved0; \
u8 ticket_id[8]; \
u8 console_id[4]; \
u8 title_id[8]; \
u8 sys_access[2]; \
u8 ticket_version[2]; \
u8 time_mask[4]; \
u8 permit_mask[4]; \
u8 title_export; \
u8 commonkey_idx; \
u8 reserved1[0x2A]; \
u8 eshop_id[4]; \
u8 reserved2; \
u8 audit; \
u8 content_permissions[0x40]; \
u8 reserved3[2]; \
u8 timelimits[0x40]
typedef struct {
u8 sig_type[4];
u8 signature[0x100];
u8 padding1[0x3C];
u8 issuer[0x40];
u8 ecdsa[0x3C];
u8 version;
u8 ca_crl_version;
u8 signer_crl_version;
u8 titlekey[0x10];
u8 reserved0;
u8 ticket_id[8];
u8 console_id[4];
u8 title_id[8];
u8 sys_access[2];
u8 ticket_version[2];
u8 time_mask[4];
u8 permit_mask[4];
u8 title_export;
u8 commonkey_idx;
u8 reserved1[0x2A];
u8 eshop_id[4];
u8 reserved2;
u8 audit;
u8 content_permissions[0x40];
u8 reserved3[2];
u8 timelimits[0x40];
u8 content_index[0xAC];
} __attribute__((packed, aligned(4))) Ticket;
TICKETBASE;
u8 content_index[];
} PACKED_STRUCT Ticket;
typedef struct {
TICKETBASE;
u8 content_index[TICKET_COMMON_CNT_INDEX_SIZE];
} PACKED_STRUCT TicketCommon;
// minimum allowed content_index is 0x14
typedef struct {
TICKETBASE;
u8 content_index[0x14];
} PACKED_STRUCT TicketMinimum;
typedef struct {
u8 unk1[2];
u8 unk2[2];
u8 content_index_size[4];
u8 data_header_relative_offset[4]; // relative to content index start
u8 unk3[2];
u8 unk4[2];
u8 unk5[4];
} PACKED_ALIGN(1) TicketContentIndexMainHeader;
typedef struct {
u8 data_relative_offset[4]; // relative to content index start
u8 max_entry_count[4];
u8 size_per_entry[4]; // but has 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 unknown[2]; // or padding
} PACKED_ALIGN(1) TicketContentIndexDataHeader;
// data type == 3
typedef struct {
u8 unk[2]; // seemly has no meaning
u8 indexoffset[2];
u8 rightsbitfield[0x80];
} PACKED_ALIGN(1) TicketRightsField;
typedef struct {
size_t count;
const TicketRightsField* rights; // points within ticket pointer
} TicketRightsCheck;
u32 ValidateTicket(Ticket* ticket);
u32 ValidateTwlTicket(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 GetTicketSize(const Ticket* ticket);
u32 BuildTicketCert(u8* tickcert);
u32 TicketRightsCheck_InitContext(TicketRightsCheck* ctx, Ticket* ticket);
bool TicketRightsCheck_CheckIndex(TicketRightsCheck* ctx, u16 index);

View File

@ -1,9 +1,11 @@
#include "ticketdb.h"
#include "disadiff.h"
#include "bdri.h"
#include "support.h"
#include "aes.h"
#include "ff.h"
#include "fsinit.h"
#include "image.h"
#define PART_PATH "D:/partitionA.bin"
u32 CryptTitleKey(TitleKeyEntry* tik, bool encrypt, bool devkit) {
// From https://github.com/profi200/Project_CTR/blob/master/makerom/pki/prod.h#L19
@ -24,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
{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;
u8 ctr[16] = { 0 };
// setup key 0x3D // ctr
if (tik->commonkey_idx >= 6) return 1;
if (getbe16(tik->title_id) == 0x3) { // setup TWL key
setup_aeskey(0x11, (void*) common_key_twl);
use_aeskey(0x11);
} else { // setup key 0x3D // ctr
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);
set_ctr(ctr);
@ -52,48 +60,56 @@ u32 GetTitleKey(u8* titlekey, Ticket* ticket) {
return 0;
}
Ticket* TicketFromTickDbChunk(u8* chunk, u8* title_id, bool legit_pls) {
// chunk must be aligned to 0x200 byte in file and at least 0x400 byte big
Ticket* tick = (Ticket*) (void*) (chunk + 0x18);
if ((getle32(chunk + 0x10) == 0) || (getle32(chunk + 0x14) != sizeof(Ticket))) return NULL;
if (ValidateTicket(tick) != 0) return NULL; // ticket not validated
if (title_id && (memcmp(title_id, tick->title_id, 8) != 0)) return NULL; // title id not matching
if (legit_pls && (ValidateTicketSignature(tick) != 0)) return NULL; // legit check using RSA sig
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;
return tick;
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
u8* data = (u8*) malloc(TICKDB_AREA_SIZE);
if (!data) return 1;
char path_store[256] = { 0 };
char* path_bak = NULL;
// read and decode ticket.db DIFF partition
if (ReadDisaDiffIvfcLvl4(path_db, NULL, TICKDB_AREA_OFFSET, TICKDB_AREA_SIZE, data) != TICKDB_AREA_SIZE) {
free(data);
// just to be safe
*ticket = NULL;
// store previous mount path
strncpy(path_store, GetMountPath(), 256);
if (*path_store) path_bak = path_store;
if (!InitImgFS(path_db))
return 1;
// search ticket in database
if (ReadTicketFromDB(PART_PATH, title_id, ticket) != 0) {
InitImgFS(path_bak);
return 1;
}
// parse the decoded data for a ticket
bool found = false;
for (u32 i = 0; !found && (i <= TICKDB_AREA_SIZE - 0x400); i += 0x200) {
Ticket* tick = TicketFromTickDbChunk(data + i, title_id, force_legit);
if (!tick) continue;
memcpy(ticket, tick, sizeof(Ticket));
found = true;
// (optional) validate ticket signature
if (force_legit && (ValidateTicketSignature(*ticket) != 0)) {
free(*ticket);
*ticket = NULL;
InitImgFS(path_bak);
return 1;
}
free(data);
return (found) ? 0 : 1;
InitImgFS(path_bak);
return 0;
}
u32 FindTitleKey(Ticket* ticket, u8* title_id) {
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
// 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++) {
u32 len = LoadSupportFile((enc) ? TIKDB_NAME_ENC : TIKDB_NAME_DEC, tikdb, STD_BUFFER_SIZE);
@ -112,11 +128,31 @@ u32 FindTitleKey(Ticket* ticket, u8* title_id) {
break;
}
}
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;
}
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) {
if (!tik_entry) { // no titlekey entry -> reset database
memset(tik_info, 0, 16);
@ -142,3 +178,9 @@ u32 AddTicketToInfo(TitleKeysInfo* tik_info, Ticket* ticket, bool decrypt) { //
tik.commonkey_idx = ticket->commonkey_idx;
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;
}

View File

@ -35,8 +35,10 @@ typedef struct {
u32 GetTitleKey(u8* titlekey, Ticket* ticket);
Ticket* TicketFromTickDbChunk(u8* chunk, u8* title_id, bool legit_pls);
u32 FindTicket(Ticket* ticket, u8* title_id, bool force_legit, bool emunand);
u32 SetTitleKey(const u8* titlekey, Ticket* ticket);
u32 FindTicket(Ticket** ticket, u8* title_id, bool force_legit, bool emunand);
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 AddTicketToInfo(TitleKeysInfo* tik_info, Ticket* ticket, bool decrypt);
u32 CryptTitleKeyInfo(TitleKeysInfo* tik_info, bool encrypt);

112
arm9/source/game/tie.c Normal file
View File

@ -0,0 +1,112 @@
#include "tie.h"
#include "cmd.h"
#define CMD_SIZE_ALIGN(sd) (sd ? 0x8000 : 0x4000)
u32 BuildTitleInfoEntryTmd(TitleInfoEntry* tie, TitleMetaData* tmd, bool sd) {
u64 title_id = getbe64(tmd->title_id);
bool has_idx1 = false;
bool has_idx2 = false;
// set basic values
memset(tie, 0x00, sizeof(TitleInfoEntry));
tie->title_type = 0x40;
// title version, product code, cmd id
tie->title_version = getbe16(tmd->title_version);
tie->cmd_content_id = 0x01;
memcpy(tie->unknown, "GM9", 4); // GM9 install magic number
// calculate base title size
// align size: 0x4000 for TWL and CTRNAND, 0x8000 for SD
u32 align_size = CMD_SIZE_ALIGN(sd);
u32 content_count = getbe16(tmd->content_count);
tie->title_size =
(align_size * 3) + // base folder + 'content' + 'cmd'
align(TMD_SIZE_N(content_count), align_size) + // TMD
align_size; // CMD, placeholder (!!!)
if (getle32(tmd->save_size) || getle32(tmd->twl_privsave_size) || (tmd->twl_flag & 0x2)) {
tie->title_size +=
align_size + // data folder
align(getle32(tmd->save_size), align_size) +
align(getle32(tmd->twl_privsave_size), align_size) +
((tmd->twl_flag & 0x2) ? align(sizeof(TwlIconData), align_size) : 0);
}
// contents title size + some additional stuff
TmdContentChunk* chunk = (TmdContentChunk*) (tmd + 1);
tie->content0_id = getbe32(chunk->id);
for (u32 i = 0; (i < content_count) && (i < TMD_MAX_CONTENTS); i++, chunk++) {
if (getbe16(chunk->index) == 1) has_idx1 = true; // will be useful later
else if (getbe16(chunk->index) == 2) has_idx2 = true; // will be useful later
tie->title_size += align(getbe64(chunk->size), align_size);
}
// manual? dlp? save? (we need to properly check this later)
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_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
}
return 0;
}
u32 BuildTitleInfoEntryTwl(TitleInfoEntry* tie, TitleMetaData* tmd, TwlHeader* twl) {
u64 title_id = getbe64(tmd->title_id);
// build the basic titledb entry
if (BuildTitleInfoEntryTmd(tie, tmd, false) != 0) return 1;
// proper handling of system data archives - thanks @aspargas!
// see: http://3dbrew.org/wiki/Title_list#0004800F_-_System_Data_Archives
if ((title_id >> 32) != 0x0004800F) {
if (ValidateTwlHeader(twl) != 0) return 1;
memcpy(tie->product_code, twl->game_title, 12);
}
// specific flags for DSiWare ports
// see: http://3dbrew.org/wiki/Titles
// see: http://3dbrew.org/wiki/Title_list#00048004_-_DSiWare_Ports
if ((title_id >> 32) == 0x00048004) { // TWL app / game
tie->flags_2[0] = 0x01;
tie->flags_2[4] = 0x01;
tie->flags_2[5] = 0x01;
} else tie->content0_id = 0;
return 0;
}
u32 BuildTitleInfoEntryNcch(TitleInfoEntry* tie, TitleMetaData* tmd, NcchHeader* ncch, NcchExtHeader* exthdr, bool sd) {
u64 title_id = getbe64(tmd->title_id);
if (ValidateNcchHeader(ncch) != 0) return 1;
if (BuildTitleInfoEntryTmd(tie, tmd, sd) != 0) return 1;
// product code, extended title version
memcpy(tie->product_code, ncch->productcode, 0x10);
tie->title_version &= ((ncch->version << 16) | 0xFFFF);
// NCCH titles need no content0 ID
tie->content0_id = 0;
// specific flags
// see: http://3dbrew.org/wiki/Titles
if (!((title_id >> 32) & 0x10)) // not a system title
tie->flags_2[4] = 0x01;
// stuff from extheader
if (exthdr) {
// extdata ID low (hacky, we navigate to storage info)
tie->extdata_id_low = getle32(exthdr->aci_data + (0x30 - 0x0C));
} else {
tie->flags_0[0] = 0x00; // no manual
tie->flags_1[0] = 0x00; // no sd save
tie->title_version &= 0xFFFF; // no dlp
}
return 0;
}

32
arm9/source/game/tie.h Normal file
View File

@ -0,0 +1,32 @@
#pragma once
#include "common.h"
#include "tmd.h"
#include "ncch.h"
#include "nds.h"
// There's probably a better place to put this
#define SD_TITLEDB_PATH(emu) ((emu) ? "B:/dbs/title.db" : "A:/dbs/title.db")
// see: https://www.3dbrew.org/wiki/Title_Database
typedef struct {
u64 title_size;
u32 title_type; // usually == 0x40
u32 title_version;
u8 flags_0[4];
u32 tmd_content_id;
u32 cmd_content_id;
u8 flags_1[4];
u32 extdata_id_low; // 0 if the title doesn't use extdata
u8 reserved1[4];
u8 flags_2[8];
char product_code[16];
u8 reserved2[12];
u32 content0_id; // only relevant for TWL?
u8 unknown[4]; // appears to not matter what's here
u8 reserved3[44];
} __attribute__((packed)) TitleInfoEntry;
u32 BuildTitleInfoEntryTwl(TitleInfoEntry* tie, TitleMetaData* tmd, TwlHeader* twl);
u32 BuildTitleInfoEntryNcch(TitleInfoEntry* tie, TitleMetaData* tmd, NcchHeader* ncch, NcchExtHeader* exthdr, bool sd);

View File

@ -6,7 +6,7 @@
#include "ff.h"
u32 ValidateTmd(TitleMetaData* tmd) {
const u8 magic[] = { TMD_SIG_TYPE };
static const u8 magic[] = { TMD_SIG_TYPE };
if ((memcmp(tmd->sig_type, magic, sizeof(magic)) != 0) ||
((strncmp((char*) tmd->issuer, TMD_ISSUER, 0x40) != 0) &&
(strncmp((char*) tmd->issuer, TMD_ISSUER_DEV, 0x40) != 0)))
@ -14,23 +14,27 @@ u32 ValidateTmd(TitleMetaData* tmd) {
return 0;
}
u32 ValidateTmdSignature(TitleMetaData* tmd) {
static bool got_modexp = false;
static u32 mod[0x100 / 4] = { 0 };
static u32 exp = 0;
if (!got_modexp) {
// grab mod/exp from cert from cert.db
if (LoadCertFromCertDb(0x3C10, NULL, mod, &exp) == 0)
got_modexp = true;
else return 1;
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;
}
if (!RSA_setKey2048(3, mod, exp) ||
!RSA_verify2048((void*) &(tmd->signature), (void*) &(tmd->issuer), 0xC4))
u32 ValidateTmdSignature(TitleMetaData* tmd) {
Certificate cert;
// grab cert from certs.db
if (LoadCertFromCertDb(&cert, (char*)(tmd->issuer)) != 0)
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) {
@ -76,8 +80,8 @@ u32 FixTmdHashes(TitleMetaData* tmd) {
return 0;
}
u32 BuildFakeTmd(TitleMetaData* tmd, u8* title_id, u32 n_contents, u32 save_size, u32 twl_privsave_size) {
const u8 sig_type[4] = { TMD_SIG_TYPE };
u32 BuildFakeTmd(TitleMetaData* tmd, u8* title_id, u32 n_contents, u32 save_size, u32 twl_privsave_size, u8 twl_flag) {
static const u8 sig_type[4] = { TMD_SIG_TYPE };
// safety check: number of contents
if (n_contents > TMD_MAX_CONTENTS) return 1; // potential incompatibility here (!)
// set TMD all zero for a clean start
@ -91,6 +95,7 @@ u32 BuildFakeTmd(TitleMetaData* tmd, u8* title_id, u32 n_contents, u32 save_size
tmd->title_type[3] = 0x40; // whatever
for (u32 i = 0; i < 4; i++) tmd->save_size[i] = (save_size >> (i*8)) & 0xFF; // le save size
for (u32 i = 0; i < 4; i++) tmd->twl_privsave_size[i] = (twl_privsave_size >> (i*8)) & 0xFF; // le privsave size
tmd->twl_flag = twl_flag;
tmd->content_count[0] = (u8) ((n_contents >> 8) & 0xFF);
tmd->content_count[1] = (u8) (n_contents & 0xFF);
memset(tmd->contentinfo_hash, 0xFF, 0x20); // placeholder (hash)
@ -102,28 +107,23 @@ u32 BuildFakeTmd(TitleMetaData* tmd, u8* title_id, u32 n_contents, u32 save_size
}
u32 BuildTmdCert(u8* tmdcert) {
const u8 cert_hash_expected[0x20] = {
static const u8 cert_hash_expected[0x20] = {
0x91, 0x5F, 0x77, 0x3A, 0x07, 0x82, 0xD4, 0x27, 0xC4, 0xCE, 0xF5, 0x49, 0x25, 0x33, 0xE8, 0xEC,
0xF6, 0xFE, 0xA1, 0xEB, 0x8C, 0xCF, 0x59, 0x6E, 0x69, 0xBA, 0x2A, 0x38, 0x8D, 0x73, 0x8A, 0xE1
};
const u8 cert_hash_expected_dev[0x20] = {
static const u8 cert_hash_expected_dev[0x20] = {
0x49, 0xC9, 0x41, 0x56, 0xCA, 0x86, 0xBD, 0x1F, 0x36, 0x51, 0x51, 0x6A, 0x4A, 0x9F, 0x54, 0xA1,
0xC2, 0xE9, 0xCA, 0x93, 0x94, 0xF4, 0x29, 0xA0, 0x38, 0x54, 0x75, 0xFF, 0xAB, 0x6E, 0x8E, 0x71
};
// open certs.db file on SysNAND
FIL db;
UINT bytes_read;
if (f_open(&db, "1:/dbs/certs.db", FA_READ | FA_OPEN_EXISTING) != FR_OK)
static const char* const retail_issuers[] = {"Root-CA00000003-CP0000000b", "Root-CA00000003"};
static const char* const dev_issuers[] = {"Root-CA00000004-CP0000000a", "Root-CA00000004"};
size_t size = TMD_CDNCERT_SIZE;
if (BuildRawCertBundleFromCertDb(tmdcert, &size, !IS_DEVKIT ? retail_issuers : dev_issuers, 2) ||
size != TMD_CDNCERT_SIZE) {
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
u8 cert_hash[0x20];

View File

@ -2,17 +2,22 @@
#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_MAX (sizeof(TitleMetaData) + (TMD_MAX_CONTENTS*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_ISSUER "Root-CA00000003-CP0000000b"
#define TMD_ISSUER_DEV "Root-CA00000004-CP0000000a"
#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
// from: https://github.com/profi200/Project_CTR/blob/02159e17ee225de3f7c46ca195ff0f9ba3b3d3e4/ctrtool/tmd.h#L18-L59;
@ -58,9 +63,10 @@ typedef struct {
} __attribute__((packed, aligned(4))) TitleMetaData;
u32 ValidateTmd(TitleMetaData* tmd);
u32 ValidateTwlTmd(TitleMetaData* tmd);
u32 ValidateTmdSignature(TitleMetaData* tmd);
u32 VerifyTmd(TitleMetaData* tmd);
u32 GetTmdCtr(u8* ctr, TmdContentChunk* chunk);
u32 FixTmdHashes(TitleMetaData* tmd);
u32 BuildFakeTmd(TitleMetaData* tmd, u8* title_id, u32 n_contents, u32 save_size, u32 twl_privsave_size);
u32 BuildFakeTmd(TitleMetaData* tmd, u8* title_id, u32 n_contents, u32 save_size, u32 twl_privsave_size, u8 twl_flag);
u32 BuildTmdCert(u8* tmdcert);

Some files were not shown because too many files have changed in this diff Show More