Compare commits

..

198 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
274 changed files with 54484 additions and 4637 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 *.obj
*.elf *.elf
*.map *.map
*.dis
# Precompiled Headers # Precompiled Headers
*.gch *.gch
@ -35,10 +36,14 @@
# OS leftovers # OS leftovers
desktop.ini desktop.ini
.DS_Store
# Sublime files # Sublime files
*.sublime-* *.sublime-*
# Visual Studio Code files
.vscode
# Build directories # Build directories
/build /build
/output /output
@ -48,6 +53,6 @@ desktop.ini
/data/README_internal.md /data/README_internal.md
# User additions # User additions
/data/aeskeydb.bin
/data/aeskeydb_.bin
/zzz_backup /zzz_backup
/arm9/source/language.inl
*.trf

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.amd64.deb" -O dkp-pacman.deb
- export DEVKITPRO=/opt/devkitpro
- export DEVKITARM=${DEVKITPRO}/devkitARM
install:
- sudo apt-get -y install python3 python3-pip p7zip-full libarchive13
- sudo dpkg -i dkp-pacman.deb
- sudo dkp-pacman -S --noconfirm devkitARM
- 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,15 +16,28 @@ export RELDIR := release
export COMMON_DIR := ../common export COMMON_DIR := ../common
# Definitions for initial RAM disk # Definitions for initial RAM disk
VRAM_OUT := $(OUTDIR)/vram0.tar VRAM_TAR := $(OUTDIR)/vram0.tar
VRAM_DATA := data VRAM_DATA := data/*
VRAM_FLAGS := --make-new --path-limit 99 --size-limit 262144 VRAM_FLAGS := --make-new --path-limit 99
ifeq ($(NTRBOOT),1)
VRAM_SCRIPTS := resources/gm9/scripts/*
endif
# Definitions for translation files
JSON_FOLDER := resources/languages
TRF_FOLDER := resources/gm9/languages
SOURCE_JSON := $(JSON_FOLDER)/source.json
LANGUAGE_INL := arm9/source/language.inl
JSON_FILES := $(filter-out $(SOURCE_JSON),$(wildcard $(JSON_FOLDER)/*.json))
TRF_FILES := $(subst $(JSON_FOLDER),$(TRF_FOLDER),$(JSON_FILES:.json=.trf))
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
ifeq ($(TERM),cygwin) ifeq ($(TERM),cygwin)
PY3 := py -3 # Windows / CMD/PowerShell PY3 := py -3 # Windows / CMD/PowerShell
else else
PY3 := python3 # Windows / MSYS2 PY3 := py # Windows / MSYS2
endif endif
else else
PY3 := python3 # Unix-like PY3 := python3 # Unix-like
@ -37,18 +50,18 @@ export ASFLAGS := -g -x assembler-with-cpp $(INCLUDE)
export CFLAGS := -DDBUILTS="\"$(DBUILTS)\"" -DDBUILTL="\"$(DBUILTL)\"" -DVERSION="\"$(VERSION)\"" -DFLAVOR="\"$(FLAVOR)\"" \ export CFLAGS := -DDBUILTS="\"$(DBUILTS)\"" -DDBUILTL="\"$(DBUILTL)\"" -DVERSION="\"$(VERSION)\"" -DFLAVOR="\"$(FLAVOR)\"" \
-g -Os -Wall -Wextra -Wcast-align -Wformat=2 -Wno-main \ -g -Os -Wall -Wextra -Wcast-align -Wformat=2 -Wno-main \
-fomit-frame-pointer -ffast-math -std=gnu11 -MMD -MP \ -fomit-frame-pointer -ffast-math -std=gnu11 -MMD -MP \
-Wno-unused-function -Wno-format-truncation $(INCLUDE) -ffunction-sections -fdata-sections -Wno-unused-function -Wno-format-truncation -Wno-format-nonliteral $(INCLUDE) -ffunction-sections -fdata-sections
export LDFLAGS := -Tlink.ld -nostartfiles -Wl,--gc-sections,-z,max-page-size=4096 export LDFLAGS := -Tlink.ld -nostartfiles -Wl,--gc-sections,-z,max-page-size=4096
ELF := arm9/arm9.elf arm11/arm11.elf ELF := arm9/arm9_code.elf arm9/arm9_data.elf arm11/arm11.elf
.PHONY: all firm vram0 elf release clean .PHONY: all firm $(VRAM_TAR) elf release clean
all: firm all: firm
clean: clean:
@set -e; for elf in $(ELF); do \ @set -e; for elf in $(ELF); do \
$(MAKE) --no-print-directory -C $$(dirname $$elf) clean; \ $(MAKE) --no-print-directory -C $$(dirname $$elf) clean; \
done done
@rm -rf $(OUTDIR) $(RELDIR) $(FIRM) $(FIRMD) $(VRAM_OUT) @rm -rf $(OUTDIR) $(RELDIR) $(FIRM) $(FIRMD) $(VRAM_TAR) $(LANGUAGE_INL) $(TRF_FILES)
unmarked_readme: .FORCE unmarked_readme: .FORCE
@$(PY3) utils/unmark.py -f README.md data/README_internal.md @$(PY3) utils/unmark.py -f README.md data/README_internal.md
@ -72,29 +85,44 @@ release: clean unmarked_readme
@cp $(OUTDIR)/$(FLAVOR)_dev.firm.sha $(RELDIR)/ @cp $(OUTDIR)/$(FLAVOR)_dev.firm.sha $(RELDIR)/
@cp $(ELF) $(RELDIR)/elf @cp $(ELF) $(RELDIR)/elf
@cp $(CURDIR)/README.md $(RELDIR) @cp $(CURDIR)/README.md $(RELDIR)
@cp $(CURDIR)/resources/lua-doc.md $(RELDIR)/lua-doc.md
@cp -R $(CURDIR)/resources/gm9 $(RELDIR)/gm9 @cp -R $(CURDIR)/resources/gm9 $(RELDIR)/gm9
@cp -R $(CURDIR)/resources/sample $(RELDIR)/sample @cp -R $(CURDIR)/resources/sample $(RELDIR)/sample
@-7za a $(RELDIR)/$(FLAVOR)-$(VERSION)-$(DBUILTS).zip ./$(RELDIR)/* @-7za a $(RELDIR)/$(FLAVOR)-$(VERSION)-$(DBUILTS).zip ./$(RELDIR)/*
vram0: $(VRAM_TAR): $(SPLASH) $(OVERRIDE_FONT) $(VRAM_DATA) $(VRAM_SCRIPTS)
@mkdir -p "$(OUTDIR)" @mkdir -p "$(@D)"
@echo "Creating $(VRAM_OUT)" @echo "Creating $@"
@$(PY3) utils/add2tar.py $(VRAM_FLAGS) $(VRAM_OUT) $(shell ls -d $(SPLASH) $(OVERRIDE_FONT) $(VRAM_DATA)/*) @$(PY3) utils/add2tar.py $(VRAM_FLAGS) $(VRAM_TAR) $(shell ls -d -1 $^)
$(LANGUAGE_INL): $(SOURCE_JSON)
@echo "Creating $@"
@$(PY3) utils/transcp.py $< $@
$(TRF_FOLDER)/%.trf: $(JSON_FOLDER)/%.json
@$(PY3) utils/transriff.py $< $@
%.elf: .FORCE %.elf: .FORCE
@echo "Building $@" @echo "Building $@"
@$(MAKE) --no-print-directory -C $(@D) @$(MAKE) --no-print-directory -C $(@D) $(@F)
firm: $(ELF) vram0 # Indicate a few explicit dependencies:
@test `wc -c <$(VRAM_OUT)` -le 262144 # The ARM9 data section depends on the VRAM drive
arm9/arm9_data.elf: $(VRAM_TAR) $(LANGUAGE_INL)
# And the code section depends on the data section being built already
arm9/arm9_code.elf: arm9/arm9_data.elf
firm: $(ELF) $(TRF_FILES)
@mkdir -p $(call dirname,"$(FIRM)") $(call dirname,"$(FIRMD)") @mkdir -p $(call dirname,"$(FIRM)") $(call dirname,"$(FIRMD)")
@echo "[FLAVOR] $(FLAVOR)" @echo "[FLAVOR] $(FLAVOR)"
@echo "[VERSION] $(VERSION)" @echo "[VERSION] $(VERSION)"
@echo "[BUILD] $(DBUILTL)" @echo "[BUILD] $(DBUILTL)"
@echo "[FIRM] $(FIRM)" @echo "[FIRM] $(FIRM)"
@$(PY3) -m firmtool build $(FIRM) $(FTFLAGS) -g -A 0x80C0000 -D $(ELF) $(VRAM_OUT) -C NDMA XDMA memcpy @$(PY3) -m firmtool build $(FIRM) $(FTFLAGS) -g -D $(ELF) -C NDMA NDMA XDMA
@echo "[FIRM] $(FIRMD)" @echo "[FIRM] $(FIRMD)"
@$(PY3) -m firmtool build $(FIRMD) $(FTDFLAGS) -g -A 0x80C0000 -D $(ELF) $(VRAM_OUT) -C NDMA XDMA memcpy @$(PY3) -m firmtool build $(FIRMD) $(FTDFLAGS) -g -D $(ELF) -C NDMA NDMA XDMA
vram0: $(VRAM_TAR) .FORCE # legacy target name
.FORCE: .FORCE:

View File

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

View File

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

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 ## How to build this / developer info
Build `GodMode9.firm` via `make firm`. This requires [firmtool](https://github.com/TuxSH/firmtool), [Python 3.5+](https://www.python.org/downloads/) and [devkitARM](https://sourceforge.net/projects/devkitpro/) installed). Build `GodMode9.firm` via `make firm`. This requires [firmtool](https://github.com/TuxSH/firmtool), [Python 3.5+](https://www.python.org/downloads/) and [devkitARM](https://sourceforge.net/projects/devkitpro/) installed).
You may run `make release` to get a nice, release-ready package of all required files. To build __SafeMode9__ (a bricksafe variant of GodMode9, with limited write permissions) instead of GodMode9, compile with `make FLAVOR=SafeMode9`. To switch screens, compile with `make SWITCH_SCREENS=1`. For additional customization, you may choose the internal font by replacing `font_default.pbm` inside the `data` directory. You may also hardcode the brightness via `make FIXED_BRIGHTNESS=x`, whereas `x` is a value between 0...15. You may run `make release` to get a nice, release-ready package of all required files. To build __SafeMode9__ (a bricksafe variant of GodMode9, with limited write permissions) instead of GodMode9, compile with `make FLAVOR=SafeMode9`. To switch screens, compile with `make SWITCH_SCREENS=1`. For additional customization, you may choose the internal font by replacing `font_default.frf` inside the `data` directory. You may also hardcode the brightness via `make FIXED_BRIGHTNESS=x`, whereas `x` is a value between 0...15.
Further customization is possible by hardcoding `aeskeydb.bin` (just put the file into the `data` folder when compiling). All files put into the `data` folder will turn up in the `V:` drive, but keep in mind there's a hard 3MB limit for all files inside, including overhead. A standalone script runner is compiled by providing `autorun.gm9` (again, in the `data` folder) and building with `make SCRIPT_RUNNER=1`. There's more possibility for customization, read the Makefiles to learn more. Further customization is possible by hardcoding `aeskeydb.bin` (just put the file into the `data` folder when compiling). All files put into the `data` folder will turn up in the `V:` drive, but keep in mind there's a hard 223.5KiB limit for all files inside, including overhead. A standalone script runner is compiled by providing `autorun.lua` or `autorun.gm9` (again, in the `data` folder) and building with `make SCRIPT_RUNNER=1`. There's more possibility for customization, read the Makefiles to learn more.
To build a .firm signed with SPI boot keys (for ntrboot and the like), run `make NTRBOOT=1`. You may need to rename the output files if the ntrboot installer you use uses hardcoded filenames. Some features such as boot9 / boot11 access are not currently available from the ntrboot environment. To build a .firm signed with SPI boot keys (for ntrboot and the like), run `make NTRBOOT=1`. You may need to rename the output files if the ntrboot installer you use uses hardcoded filenames. Some features such as boot9 / boot11 access are not currently available from the ntrboot environment.
@ -61,10 +61,14 @@ GodMode9 provides a write permissions system, which will protect you from accide
## Support files ## Support files
For certain functionality, GodMode9 may need 'support files'. Support files should be placed into either `0:/gm9/support` or `1:/gm9/support`. Support files contain additional information that is required in decryption operations. A list of support files, and what they do, is found below. Please don't ask for support files - find them yourself. For certain functionality, GodMode9 may need 'support files'. Support files should be placed into either `0:/gm9/support` or `1:/gm9/support`. Support files contain additional information that is required in decryption operations. A list of support files, and what they do, is found below. Please don't ask for support files - find them yourself.
* __`aeskeydb.bin`__: This should contain 0x25keyX, 0x18keyX and 0x1BkeyX to enable decryption of 7x / Secure3 / Secure4 encrypted NCCH files, 0x11key95 / 0x11key96 for FIRM decrypt support and 0x11keyOTP / 0x11keyIVOTP for 'secret' sector 0x96 crypto support. Entrypoints other than [boot9strap](https://github.com/SciresM/boot9strap) or [fastboot3ds](https://github.com/derrekr/fastboot3DS) may require a aeskeydb.bin file. A known perfect `aeskeydb.bin` can be found somewhere on the net, is exactly 1024 byte big and has an MD5 of A5B28945A7C051D7A0CD18AF0E580D1B. Have fun hunting! * __`aeskeydb.bin`__: This should contain 0x25keyX, 0x18keyX and 0x1BkeyX to enable decryption of 7x / Secure3 / Secure4 encrypted NCCH files, 0x11key95 / 0x11key96 for FIRM decrypt support and 0x11keyOTP / 0x11keyIVOTP for 'secret' sector 0x96 crypto support. Entrypoints other than [boot9strap](https://github.com/SciresM/boot9strap) or [fastboot3ds](https://github.com/derrekr/fastboot3DS) may require a aeskeydb.bin file. This is now included in standard releases of GM9. No need to hunt down the file!
* __`seeddb.bin`__: This file is required to decrypt and mount seed encrypted NCCHs and CIAs if the seed in question is not installed to your NAND. Note that your seeddb.bin must also contain the seed for the specific game you need to decrypt. * __`seeddb.bin`__: This file is optional and required to decrypt and mount seed-encrypted NCCHs and CIAs (if the seed in question is not installed to your NAND). Note that your seeddb.bin must also contain the seed for the specific game you need to decrypt.
* __`encTitleKeys.bin`__ / __`decTitleKeys.bin`__: These files are optional and provide titlekeys, which are required to create updatable CIAs from NCCH / NCSD files. CIAs created without these files will still work, but won't be updatable from eShop. * __`encTitleKeys.bin`__ / __`decTitleKeys.bin`__: These files are optional and provide titlekeys, which are required to decrypt and install contents downloaded from CDN (for DSi and 3DS content).
### Fonts and translations
GodMode9 also supports custom fonts and translations as support files. These both use custom formats, fonts use FRF (Font RIFF) files which can be created using the `fontriff.py` Python script in the 'utils' folder. Translations use TRF (Translation RIFF) files from the `transriff.py` script. Examples of the inputs to these scripts can be found in the 'fonts' and 'languages' folders of the 'resources' folder respectively.
TRF files can be placed in `0:/gm9/languages` to show in the language menu accessible from the HOME menu and shown on first load. Official translations are provided from the community via the [GodMode9 Crowdin](https://crowdin.com/project/GodMode9). Languages can use a special font by having an FRF with the same name, for example `en.trf` and `en.frf`.
## Drives in GodMode9 ## Drives in GodMode9
GodMode9 provides access to system data via drives, a listing of what each drive contains and additional info follows below. Some of these drives are removable (such as drive `7:`), some will only turn up if they are available (drive `8:` and everything associated with EmuNAND, f.e.). Information on the 3DS console file system is also found on [3Dbrew.org](https://3dbrew.org/wiki/Flash_Filesystem). GodMode9 provides access to system data via drives, a listing of what each drive contains and additional info follows below. Some of these drives are removable (such as drive `7:`), some will only turn up if they are available (drive `8:` and everything associated with EmuNAND, f.e.). Information on the 3DS console file system is also found on [3Dbrew.org](https://3dbrew.org/wiki/Flash_Filesystem).
@ -86,9 +90,10 @@ GodMode9 provides access to system data via drives, a listing of what each drive
* __`C: GAMECART`__: This is read-only and provides access to the game cartridge currently inserted into the cart slot. This can be used for dumps of CTR and TWL mode cartridges. Flash cards are supported only to a limited extent. * __`C: GAMECART`__: This is read-only and provides access to the game cartridge currently inserted into the cart slot. This can be used for dumps of CTR and TWL mode cartridges. Flash cards are supported only to a limited extent.
* __`G: GAME IMAGE`__: CIA/NCSD/NCCH/EXEFS/ROMFS/FIRM images can be accessed via this drive when mounted. This is read-only. * __`G: GAME IMAGE`__: CIA/NCSD/NCCH/EXEFS/ROMFS/FIRM images can be accessed via this drive when mounted. This is read-only.
* __`K: AESKEYDB IMAGE`__: An `aeskeydb.bin` image can be mounted and accessed via this drive. The drive shows all keys inside the aeskeydb.bin. This is read-only. * __`K: AESKEYDB IMAGE`__: An `aeskeydb.bin` image can be mounted and accessed via this drive. The drive shows all keys inside the aeskeydb.bin. This is read-only.
* __`T: TICKET.DB IMAGE`__: Ticket database files can be mounted and accessed via this drive. This provides easy and quick access to all tickets inside the `ticket.db`. This is read-only. * __`T: TICKET.DB IMAGE / BDRI IMAGE`__: Ticket database files can be mounted and accessed via this drive. This provides easy and quick access to all tickets inside the `ticket.db`. This drive also provides access to other BDRI images, such as the Title database (`title.db`).
* __`M: MEMORY VIRTUAL`__: This provides access to various memory regions. This is protected by a special write permission, and caution is advised when doing modifications inside this drive. This drive also gives access to `boot9.bin`, `boot11.bin` (boot9strap only) and `otp.mem` (sighaxed systems only). * __`M: MEMORY VIRTUAL`__: This provides access to various memory regions. This is protected by a special write permission, and caution is advised when doing modifications inside this drive. This drive also gives access to `boot9.bin`, `boot11.bin` (boot9strap only) and `otp.mem` (sighaxed systems only).
* __`V: VRAM VIRTUAL`__: This drive resides in the first VRAM bank and contains files essential to GodMode9. The font (in PBM format), the splash logo (in PNG format) and the readme file are found there, as well as any file that is provided inside the `data` folder at build time. This is read-only. * __`V: VRAM VIRTUAL`__: This drive resides in part of ARM9 internal memory and contains files essential to GodMode9. The font (in FRF format), the splash logo (in PNG format) and the readme file are found there, as well as any file that is provided inside the `data` folder at build time. This is read-only.
* __`Y: TITLE MANAGER`__: The title manager is accessed via the HOME menu and provides easy access to all installed titles.
* __`Z: LAST SEARCH`__: After a search operation, search results are found inside this drive. The drive can be accessed at a later point to return to the former search results. * __`Z: LAST SEARCH`__: After a search operation, search results are found inside this drive. The drive can be accessed at a later point to return to the former search results.
@ -96,8 +101,8 @@ GodMode9 provides access to system data via drives, a listing of what each drive
GodMode9 is one of the most important tools for digital preservation of 3DS content data. Here's some stuff you should know: GodMode9 is one of the most important tools for digital preservation of 3DS content data. Here's some stuff you should know:
* __Dumping game cartridges (size < 4GiB)__: Game cartridges turn up inside the `C:` drive (see above). For most carts all you need to do is copy the `.3DS` game image to some place of your choice. Game images dumped by GodMode9 contain no identifying info such as private headers or savegames. Private headers can be dumped in a separate image. * __Dumping game cartridges (size < 4GiB)__: Game cartridges turn up inside the `C:` drive (see above). For most carts all you need to do is copy the `.3DS` game image to some place of your choice. Game images dumped by GodMode9 contain no identifying info such as private headers or savegames. Private headers can be dumped in a separate image.
* __Dumping game cartridges (size = 4GiB)__: Everything written above applies here as well. However, the FAT32 file system (which is what the 3DS uses) is limited to _4GiB - 1byte_. Take note that the `.3DS` game image, as provided by GodMode9 actually misses the last byte in these cases. That byte is 0xFF and unused in all known cases. It is not required for playing the image. If you need to check, we also provide split files (`.000`, `.001)`, which contain all the data. If you need a valid checksum for the `.3DS` game image, append a 0xFF byte before checking. * __Dumping game cartridges (size = 4GiB)__: Everything written above applies here as well. However, the FAT32 file system (which is what the 3DS uses) is limited to _4GiB - 1byte_. Take note that the `.3DS` game image, as provided by GodMode9 actually misses the last byte in these cases. That byte is 0xFF and unused in all known cases. It is not required for playing the image. If you need to check, we also provide split files (`.000`, `.001)`, which contain all the data. If you need a valid checksum for the `.3DS` game image, append a 0xFF byte before checking.
* __Building CIAs (all types)__: You may convert compatible file types (game images, installed content) to the CIA installable format using the A button menu. To get a list of installed content, press R+A on one of the compatible drives (`1:`, `2:`, `A:`, ...) and select `Search for titles`. Take note that `standard` built CIAs are decrypted by default (decryption allows better compression by ZIP and 7Z). If you should need an encrypted CIA for some reason, apply the encryption to the CIA afterwards. * __Building CIAs (all types)__: You may convert compatible file types (game images, installed content, CDN content) to the CIA installable format using the A button menu. To get a list of installed content, press HOME, select `Title manager` and choose a drive. Take note that `standard` built CIAs are decrypted by default (decryption allows better compression by ZIP and 7Z). If you should need an encrypted CIA for some reason, apply the encryption to the CIA afterwards.
* __Building CIAs (legit type)__: Installed content can be built as `legit` or `standard` CIA. Legit CIAs preserve more of the original data and are thus recommended for preservation purposes. When building legit CIAs, GodMode9 keeps the original crypto and tries to find a genuine, signature-valid ticket. If it doesn't find one on your system or if it only finds a personalized one, it offers to use a generic ticket instead. It is not recommended to use personalized tickets - only choose this if you know what you're doing. * __Building CIAs (legit type)__: Installed content can be built as `legit` or `standard` CIA. Legit CIAs preserve more of the original data and are thus recommended for preservation purposes. When building legit CIAs, GodMode9 keeps the original crypto and tries to find a genuine, signature-valid ticket. If it doesn't find one on your system, it will use a generic ticket instead. If it only finds a personalized one, it still offers to use a generic ticket. It is not recommended to use personalized tickets - only choose this if you know what you're doing.
* __Checking CIAs__: You may also check your CIA files with the builtin `CIA checker tool`. Legit CIAs with generic tickets are identified as `Universal Pirate Legit`, which is the recommended preservation format where `Universal Legit` is not available. Note: apart from system titles, `Universal Legit` is only available for a handful of preinstalled games from special edition 3DS consoles. * __Checking CIAs__: You may also check your CIA files with the builtin `CIA checker tool`. Legit CIAs with generic tickets are identified as `Universal Pirate Legit`, which is the recommended preservation format where `Universal Legit` is not available. Note: apart from system titles, `Universal Legit` is only available for a handful of preinstalled games from special edition 3DS consoles.
@ -113,12 +118,13 @@ With the possibilites GodMode9 provides, not everything may be obvious at first
* __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. * __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. * __View text files in a text viewer__: Press the A button on a file and select `Show in Textviewer` (only shows up for actual text files). You can enable wordwrapped mode via R+Y, and navigate around the file via R+X and the dpad.
* __Chainload FIRM payloads__: Press the A button on a FIRM file, select `FIRM options` -> `Boot FIRM`. Keep in mind you should not run FIRMs from dubious sources and that the write permissions system is no longer in place after booting a payload. * __Chainload FIRM payloads__: Press the A button on a FIRM file, select `FIRM options` -> `Boot FIRM`. Keep in mind you should not run FIRMs from dubious sources and that the write permissions system is no longer in place after booting a payload.
* __Chainload FIRM payloads from a neat menu__: The `payloads` menu is found inside the HOME button menu. It provides any FIRM found in `0:/gm9/payloads` for quick chainloading. * __Chainload FIRM payloads from a neat menu__: The `payloads` menu is found inside the HOME button menu. It provides any FIRM found in `0:/gm9/payloads` for quick chainloading.
* __Inject a file to another file__: Put exactly one file (the file to be injected from) into the clipboard (via the Y button). Press A on the file to be injected to. There will be an option to inject the first file into it. * __Inject a file to another file__: Put exactly one file (the file to be injected from) into the clipboard (via the Y button). Press A on the file to be injected to. There will be an option to inject the first file into it.
### Scripting functionality ### Scripting functionality
* __Run .gm9 scripts from anywhere on your SD card__: You can run scripts in .gm9 format via the A button menu. .gm9 scripts use a shell-like language and can be edited in any text editor. For an overview of usable commands have a look into the sample scripts included in the release archive. *Don't run scripts from untrusted sources.* * __Run .lua scripts from anywhere on your SD card__: You can run Lua scripts via the A button menu. For an overview of usable commands have a look into the documentation and sample scripts included in the release archive. *Don't run scripts from untrusted sources.*
* __Run .gm9 scripts via a neat menu__: Press the HOME button, select `More...` -> `Scripts...`. Any script you put into `0:/gm9/scripts` (subdirs included) will be found here. Scripts ran via this method won't have the confirmation at the beginning either. * __Run Lua scripts via a neat menu__: Press the HOME button, select `More...` -> `Lua scripts...`. Any script you put into `0:/gm9/luascripts` (subdirs included) will be found here. Scripts ran via this method won't have the confirmation at the beginning either.
* __Run legacy .gm9 scripts__: The old format of .gm9 scripts is still available, but is deprecated and will see no further development.
### SD card handling ### SD card handling
* __Format your SD card / setup an EmuNAND__: Press the HOME button, select `More...` -> `SD format menu`. This also allows to setup a RedNAND (single/multi) or GW type EmuNAND on your SD card. You will get a warning prompt and an unlock sequence before any operation starts. * __Format your SD card / setup an EmuNAND__: Press the HOME button, select `More...` -> `SD format menu`. This also allows to setup a RedNAND (single/multi) or GW type EmuNAND on your SD card. You will get a warning prompt and an unlock sequence before any operation starts.
@ -128,15 +134,17 @@ With the possibilites GodMode9 provides, not everything may be obvious at first
* __Set (and use) the RTC clock__: For correct modification / creation dates in your file system, you need to setup the RTC clock first. Press the HOME Button and select `More...` to find the option. Keep in mind that modifying the RTC clock means you should also fix system OS time afterwards. * __Set (and use) the RTC clock__: For correct modification / creation dates in your file system, you need to setup the RTC clock first. Press the HOME Button and select `More...` to find the option. Keep in mind that modifying the RTC clock means you should also fix system OS time afterwards.
### Game file handling ### Game file handling
* __List titles installed on your system__: Press R+A on a /title dir or a subdir below that. This will also work directly for `CTRNAND`, `TWLN` and `A:`/`B:` drives. This will list all titles installed in the selected location. Works best with the below two features. * __List titles installed on your system__: Press HOME and select `Title manager`. This will also work via R+A for `CTRNAND` and `A:`/`B:` drives. This will list all titles installed in the selected location.
* __Build CIAs from NCCH / NCSD (.3DS) / TMD (installed contents)__: Press A on the file you want converted and the option will be shown. Installed contents are found (among others) in `1:/titles/`(SysNAND) and `A:/titles/`(SD installed). Where applicable, you will also be able to generate legit CIAs. Note: this works also from a file search and title listing. * __Install titles to your system__: Just press A on any file you want installed and select `Install game image` from the submenu. Works with NCCH / NCSD / CIA / DSiWare SRLs / 3DS CDN TMDs / DSi CDN TMDs / NUS TMDs.
* __(Batch) Uninstall titles from your system__: Most easily done via the HOME menu `Title manager`. Just select one or more titles and find the option inside the `Manage title...` submenu.
* __Build CIAs from NCCH / NCSD (.3DS) / SRL / TMD__: Press A on the file you want converted and the option will be shown. Installed contents are found most easily via the HOME menu `Title manager`. Where applicable, you will also be able to generate legit CIAs. Note: this works also from a file search and title listing.
* __Dump CXIs / NDS from TMD (installed contents)__: This works the same as building CIAs, but dumps decrypted CXIs or NDS rom dumps instead. Note: this works also from a file search and title listing. * __Dump CXIs / NDS from TMD (installed contents)__: This works the same as building CIAs, but dumps decrypted CXIs or NDS rom dumps instead. Note: this works also from a file search and title listing.
* __Decrypt, encrypt and verify NCCH / NCSD / CIA / BOSS / FIRM images__: Options are found inside the A button menu. You will be able to decrypt/encrypt to the standard output directory or (where applicable) in place. * __Decrypt, encrypt and verify NCCH / NCSD / CIA / BOSS / FIRM images__: Options are found inside the A button menu. You will be able to decrypt/encrypt to the standard output directory or (where applicable) in place.
* __Decrypt content downloaded from CDN / NUS__: Press A on the file you want decrypted. For this to work, you need at least a TMD file (`encTitlekeys.bin` / `decTitlekeys.bin` also required, see _Support files_ below) or a CETK file. Either keep the names provided by CDN / NUS, or rename the downloaded content to `(anything).nus` or `(anything).cdn` and the CETK to `(anything).cetk`. * __Decrypt content downloaded from CDN / NUS__: Press A on the file you want decrypted. For this to work, you need at least a TMD file (`encTitlekeys.bin` / `decTitlekeys.bin` also required, see _Support files_ below) or a CETK file. Either keep the names provided by CDN / NUS, or rename the downloaded content to `(anything).nus` or `(anything).cdn` and the CETK to `(anything).cetk`.
* __Batch mode for the above operations__: Just select multiple files of the same type via the L button, then press the A button on one of the selected files. * __Batch mode for the above operations__: Just select multiple files of the same type via the L button, then press the A button on one of the selected files.
* __Access any file inside NCCH / NCSD / CIA / FIRM / NDS images__: Just mount the file via the A button menu and browse to the file you want. For CDN / NUS content, prior decryption is required for full access. * __Access any file inside NCCH / NCSD / CIA / FIRM / NDS images__: Just mount the file via the A button menu and browse to the file you want. For CDN / NUS content, prior decryption is required for full access.
* __Rename your NCCH / NCSD / CIA / NDS / GBA files to proper names__: Find this feature inside the A button menu. Proper names include title id, game name, product code and region. * __Rename your NCCH / NCSD / CIA / NDS / GBA files to proper names__: Find this feature inside the A button menu. Proper names include title id, game name, product code and region.
* __Trim NCCH / NCSD / NDS / FIRM / NAND images__: This feature is found inside the A button menu. It allows you to trim excess data from supported file types. *Warning: Excess data may not be empty, bonus drives are stored there for NAND images, NCSD card2 images store savedata there, for FIRMs parts of the A9LH exploit may be stored there*. * __Trim NCCH / NCSD / NDS / GBA / FIRM / NAND images__: This feature is found inside the A button menu. It allows you to trim excess data from supported file types. *Warning: Excess data may not be empty, bonus drives are stored there for NAND images, NCSD card2 images store savedata there, for FIRMs parts of the A9LH exploit may be stored there*.
* __Dump 3DS / NDS / DSi type retail game cartridges__: Insert the cartridge and take a look inside the `C:` drive. You may also dump private headers from 3DS game cartridges. The `C:` drive also gives you read/write access to the saves on the cartridges. Note: For 4GiB cartridges, the last byte is not included in the .3ds file dump. This is due to restrictrions of the FAT32 file system. * __Dump 3DS / NDS / DSi type retail game cartridges__: Insert the cartridge and take a look inside the `C:` drive. You may also dump private headers from 3DS game cartridges. The `C:` drive also gives you read/write access to the saves on the cartridges. Note: For 4GiB cartridges, the last byte is not included in the .3ds file dump. This is due to restrictrions of the FAT32 file system.
### NAND handling ### NAND handling
@ -154,7 +162,7 @@ With the possibilites GodMode9 provides, not everything may be obvious at first
* __Check and fix CMACs (for any file that has them)__: The option will turn up in the A button menu if it is available for a given file (f.e. system savegames, `ticket.db`, etc...). This can also be done for multiple files at once if they are marked. * __Check and fix CMACs (for any file that has them)__: The option will turn up in the A button menu if it is available for a given file (f.e. system savegames, `ticket.db`, etc...). This can also be done for multiple files at once if they are marked.
* __Mount ticket.db files and dump tickets__: Mount the file via the A button menu. Tickets are sorted into `eshop` (stuff from eshop), `system` (system tickets), `unknown` (typically empty) and `hidden` (hidden tickets, found via a deeper scan) categories. All tickets displayed are legit, fake tickets are ignored * __Mount ticket.db files and dump tickets__: Mount the file via the A button menu. Tickets are sorted into `eshop` (stuff from eshop), `system` (system tickets), `unknown` (typically empty) and `hidden` (hidden tickets, found via a deeper scan) categories. All tickets displayed are legit, fake tickets are ignored
* __Inject any NCCH CXI file into Health & Safety__: The option is found inside the A button menu for any NCCH CXI file. NCCH CXIs are found, f.e. inside of CIAs. Keep in mind there is a (system internal) size restriction on H&S injectable apps. * __Inject any NCCH CXI file into Health & Safety__: The option is found inside the A button menu for any NCCH CXI file. NCCH CXIs are found, f.e. inside of CIAs. Keep in mind there is a (system internal) size restriction on H&S injectable apps.
* __Inject and dump GBA VC saves__: Find the options to do this inside the A button menu for `agbsave.bin` in the `S:` drive. Keep in mind that you need to start the specific GBA game on your console before dumping / injecting the save. * __Inject and dump GBA VC saves__: Find the options to do this inside the A button menu for `agbsave.bin` in the `S:` drive. Keep in mind that you need to start the specific GBA game on your console before dumping / injecting the save. _To inject a save it needs to be in the clipboard_.
* __Dump a copy of boot9, boot11 & your OTP__: This works on sighax, via boot9strap only. These files are found inside the `M:` drive and can be copied from there to any other place. * __Dump a copy of boot9, boot11 & your OTP__: This works on sighax, via boot9strap only. These files are found inside the `M:` drive and can be copied from there to any other place.
### Support file handling ### Support file handling
@ -165,6 +173,8 @@ With the possibilites GodMode9 provides, not everything may be obvious at first
## License ## License
You may use this under the terms of the GNU General Public License GPL v2 or under the terms of any later revisions of the GPL. Refer to the provided `LICENSE.txt` file for further information. You may use this under the terms of the GNU General Public License GPL v2 or under the terms of any later revisions of the GPL. Refer to the provided `LICENSE.txt` file for further information.
## Contact info
You can chat directly with us via IRC @ #GodMode9 on [libera.chat](https://web.libera.chat/#GodMode9) or [Discord](https://discord.gg/BRcbvtFxX4)!
## Credits ## Credits
This tool would not have been possible without the help of numerous people. Thanks go to (in no particular order)... This tool would not have been possible without the help of numerous people. Thanks go to (in no particular order)...
@ -174,9 +184,13 @@ This tool would not have been possible without the help of numerous people. Than
* **Wolfvak** for ARM11 code, FIRM binary launcher, exception handlers, PCX code, Makefile and for help on countless other occasions * **Wolfvak** for ARM11 code, FIRM binary launcher, exception handlers, PCX code, Makefile and for help on countless other occasions
* **SciresM** for helping me figure out RomFS and for boot9strap * **SciresM** for helping me figure out RomFS and for boot9strap
* **SciresM**, **Myria**, **Normmatt**, **TuxSH** and **hedgeberg** for figuring out sighax and giving us access to bootrom * **SciresM**, **Myria**, **Normmatt**, **TuxSH** and **hedgeberg** for figuring out sighax and giving us access to bootrom
* **ihaveamac** for first developing the simple CIA generation method and for being of great help in porting it * **ihaveamac** for implementing Lua support, and first developing the simple CIA generation method and for being of great help in porting it
* **DarkRTA** for linker support during the implementation of Lua
* **luigoalma** for fixing Lua to compile without issues
* **Gruetzig** for re-implementing the Lua os module
* **wwylele** and **aspargas2** for documenting and implementing the DISA, DIFF, and BDRI formats * **wwylele** and **aspargas2** for documenting and implementing the DISA, DIFF, and BDRI formats
* **dratini0** for savefile management, based on [TWLSaveTool](https://github.com/TuxSH/TWLSaveTool/) * **dratini0** for savefile management, based on [TWLSaveTool](https://github.com/TuxSH/TWLSaveTool/)
* **Pk11** for unicode support and her ongoing work on GodMode9 translations and translation support
* **b1l1s** for helping me figure out A9LH compatibility * **b1l1s** for helping me figure out A9LH compatibility
* **Gelex** and **AuroraWright** for helping me figure out various things * **Gelex** and **AuroraWright** for helping me figure out various things
* **stuckpixel** for the new 6x10 font and help on various things * **stuckpixel** for the new 6x10 font and help on various things
@ -185,14 +199,16 @@ This tool would not have been possible without the help of numerous people. Than
* **profi200** for always useful advice and helpful hints on various things * **profi200** for always useful advice and helpful hints on various things
* **windows-server-2003** for the initial implementation of if-else-goto in .gm9 scripts * **windows-server-2003** for the initial implementation of if-else-goto in .gm9 scripts
* **Kazuma77** for pushing forward scripting, for testing and for always useful advice * **Kazuma77** for pushing forward scripting, for testing and for always useful advice
* **TurdPooCharger** for being one of the most meticulous software testers around
* **JaySea**, **YodaDaCoda**, **liomajor**, **Supster131**, **imanoob**, **Kasher_CS** and countless others from freenode #Cakey and the GBAtemp forums for testing, feedback and helpful hints * **JaySea**, **YodaDaCoda**, **liomajor**, **Supster131**, **imanoob**, **Kasher_CS** and countless others from freenode #Cakey and the GBAtemp forums for testing, feedback and helpful hints
* **Shadowhand** for being awesome and [hosting my nightlies](https://d0k3.secretalgorithm.com/) * **Shadowhand** for being awesome and [hosting my nightlies](https://d0k3.secretalgorithm.com/)
* **Plailect** for putting his trust in my tools and recommending this in [The Guide](https://3ds.guide/) * **Plailect** for putting his trust in my tools and recommending this in [The Guide](https://3ds.guide/)
* **SirNapkin1334** for testing, bug reports and for hosting the official [GodMode9 Discord channel](https://discord.gg/EGu6Qxw) * **SirNapkin1334** for testing, bug reports and for hosting the original GodMode9 Discord server
* **Lilith Valentine** for testing and helpful advice * **Lilith Valentine** for testing and helpful advice
* **Project Nayuki** for [qrcodegen](https://github.com/nayuki/QR-Code-generator) * **Project Nayuki** for [qrcodegen](https://github.com/nayuki/QR-Code-generator)
* **Amazingmax fonts** for the Amazdoom font * **Amazingmax fonts** for the Amazdoom font
* The fine folks on **the official GodMode9 Discord** * **TakWolf** for [fusion-pixel-font](https://github.com/TakWolf/fusion-pixel-font) used for Chinese and Korean
* The fine folks on **the official GodMode9 IRC channel and Discord server**
* The fine folks on **freenode #Cakey** * The fine folks on **freenode #Cakey**
* All **[3dbrew.org](https://www.3dbrew.org/wiki/Main_Page) editors** * All **[3dbrew.org](https://www.3dbrew.org/wiki/Main_Page) editors**
* Everyone I possibly forgot, if you think you deserve to be mentioned, just contact me! * Everyone I possibly forgot, if you think you deserve to be mentioned, just contact us!

View File

@ -1,6 +1,6 @@
PROCESSOR := ARM11 PROCESSOR := ARM11
TARGET := $(shell basename $(CURDIR)) TARGET := $(shell basename "$(CURDIR)")
SOURCE := source SOURCE := source
BUILD := build BUILD := build
@ -11,7 +11,7 @@ INCLUDE := $(foreach dir,$(INCDIRS),-I"$(shell pwd)/$(dir)")
ASFLAGS += $(SUBARCH) $(INCLUDE) ASFLAGS += $(SUBARCH) $(INCLUDE)
CFLAGS += $(SUBARCH) $(INCLUDE) -flto CFLAGS += $(SUBARCH) $(INCLUDE) -flto
LDFLAGS += $(SUBARCH) -Wl,-Map,$(TARGET).map -flto LDFLAGS += $(SUBARCH) -Wl,--use-blx,-Map,$(TARGET).map -flto
include ../Makefile.common include ../Makefile.common
include ../Makefile.build include ../Makefile.build

View File

@ -16,7 +16,7 @@ SECTIONS
__text_va = ABSOLUTE(.); __text_va = ABSOLUTE(.);
*(.text*) *(.text*)
. = ALIGN(4K); . = ALIGN(4K);
__text_len = . - __text_va; __text_va_end = .;
} >AXIWRAM } >AXIWRAM
.data : ALIGN(4K) .data : ALIGN(4K)
@ -25,7 +25,7 @@ SECTIONS
__data_va = ABSOLUTE(.); __data_va = ABSOLUTE(.);
*(.data*) *(.data*)
. = ALIGN(4K); . = ALIGN(4K);
__data_len = . - __data_va; __data_va_end = .;
} >AXIWRAM } >AXIWRAM
.rodata : ALIGN(4K) .rodata : ALIGN(4K)
@ -34,7 +34,7 @@ SECTIONS
__rodata_va = ABSOLUTE(.); __rodata_va = ABSOLUTE(.);
*(.rodata*) *(.rodata*)
. = ALIGN(4K); . = ALIGN(4K);
__rodata_len = . - __rodata_va; __rodata_va_end = .;
} >AXIWRAM } >AXIWRAM
.shared (NOLOAD) : ALIGN(4K) .shared (NOLOAD) : ALIGN(4K)
@ -43,7 +43,7 @@ SECTIONS
__shared_va = ABSOLUTE(.); __shared_va = ABSOLUTE(.);
*(.shared*) *(.shared*)
. = ALIGN(4K); . = ALIGN(4K);
__shared_len = . - __shared_va; __shared_va_end = .;
} >AXIWRAM } >AXIWRAM
.bss (NOLOAD) : ALIGN(4K) .bss (NOLOAD) : ALIGN(4K)
@ -52,6 +52,6 @@ SECTIONS
__bss_va = ABSOLUTE(.); __bss_va = ABSOLUTE(.);
*(.bss*) *(.bss*)
. = ALIGN(4K); . = ALIGN(4K);
__bss_len = . - __bss_va; __bss_va_end = .;
} >AXIWRAM } >AXIWRAM
} }

View File

@ -19,8 +19,12 @@
#include <common.h> #include <common.h>
#include <arm.h> #include <arm.h>
#include <stdatomic.h>
#include "arm/gic.h" #include "arm/gic.h"
#include "system/event.h"
/* Generic Interrupt Controller Registers */ /* Generic Interrupt Controller Registers */
#define REG_GIC(cpu, o, t) REG_ARM_PMR(0x200 + ((cpu) * 0x100) + (o), t) #define REG_GIC(cpu, o, t) REG_ARM_PMR(0x200 + ((cpu) * 0x100) + (o), t)
@ -72,6 +76,7 @@
#define IRQN_IS_VALID(n) ((n) < DIC_MAX_IRQ) #define IRQN_IS_VALID(n) ((n) < DIC_MAX_IRQ)
static gicIrqHandler gicIrqHandlers[DIC_MAX_IRQ]; static gicIrqHandler gicIrqHandlers[DIC_MAX_IRQ];
static _Atomic(u32) gicIrqPending[DIC_MAX_IRQ / 32];
static struct { static struct {
u8 tgt; u8 tgt;
@ -106,14 +111,13 @@ static u8 gicGetDefaultIrqCfg(u32 irqn) {
return gicDefaultIrqCfg[i].mode; return gicDefaultIrqCfg[i].mode;
} }
// TODO: would it be considerably faster to use bsearch? // TODO: would it be considerably faster to use bsearch?
return GIC_RISINGEDGE_1N; return GIC_RISINGEDGE_1N;
} }
void gicTopHandler(void) void gicTopHandler(void)
{ {
while(1) { while(1) {
u32 irqn; u32 irqn, irqsource, index, mask;
/** /**
If more than one of these CPUs reads the Interrupt Acknowledge Register at the If more than one of these CPUs reads the Interrupt Acknowledge Register at the
@ -121,16 +125,22 @@ void gicTopHandler(void)
routine must ensure that only one of them tries to process the interrupt, with the routine must ensure that only one of them tries to process the interrupt, with the
others returning after writing the ID to the End of Interrupt Register. others returning after writing the ID to the End of Interrupt Register.
*/ */
irqn = REG_GIC_IRQACK(GIC_THIS_CPU_ALIAS); irqsource = REG_GIC_IRQACK(GIC_THIS_CPU_ALIAS);
if (irqn == GIC_IRQ_SPURIOUS) // no further processing is needed if (irqsource == GIC_IRQ_SPURIOUS) // no further processing is needed
break; break;
(gicIrqHandlers[irqn & ~IRQN_SRC_MASK])(irqn); irqn = irqsource & ~IRQN_SRC_MASK;
index = irqn / 32;
mask = BIT(irqn % 32);
atomic_fetch_or(&gicIrqPending[index], mask);
(gicIrqHandlers[irqn])(irqsource);
// if the id is < 16, the source CPU can be obtained from irqn // if the id is < 16, the source CPU can be obtained from irqn
// if the handler isn't set, it'll try to branch to 0 and trigger a prefetch abort // if the handler isn't set, it'll try to branch to 0 and trigger a prefetch abort
REG_GIC_IRQEND(GIC_THIS_CPU_ALIAS) = irqn; REG_GIC_IRQEND(GIC_THIS_CPU_ALIAS) = irqsource;
} }
} }
@ -152,6 +162,7 @@ void gicGlobalReset(void)
gicn = MAX_CPU; gicn = MAX_CPU;
// clear the interrupt handler and config table // clear the interrupt handler and config table
getEventIRQ()->reset();
memset(gicIrqHandlers, 0, sizeof(gicIrqHandlers)); memset(gicIrqHandlers, 0, sizeof(gicIrqHandlers));
memset(gicIrqConfig, 0, sizeof(gicIrqConfig)); memset(gicIrqConfig, 0, sizeof(gicIrqConfig));
@ -263,3 +274,27 @@ void gicTriggerSoftInterrupt(u32 softirq)
{ {
REG_DIC_SOFTINT = softirq; REG_DIC_SOFTINT = softirq;
} }
static void irqEvReset(void) {
memset(&gicIrqPending, 0, sizeof(gicIrqPending));
}
static u32 irqEvTest(u32 param, u32 clear) {
u32 index, tstmask, clrmask;
if (param >= DIC_MAX_IRQ)
bkpt;
index = param / 32;
tstmask = BIT(param % 32);
clrmask = clear ? tstmask : 0;
return !!(atomic_fetch_and(&gicIrqPending[index], ~clrmask) & tstmask);
}
static const EventInterface evIRQ = {
.reset = irqEvReset,
.test = irqEvTest
};
const EventInterface *getEventIRQ(void) {
return &evIRQ;
}

View File

@ -31,7 +31,7 @@ enum {
GIC_RISINGEDGE_1N = 3 GIC_RISINGEDGE_1N = 3
// With the 1-N model, an interrupt that is taken on any CPU clears the Pending // With the 1-N model, an interrupt that is taken on any CPU clears the Pending
// status on all CPUs. // status on all CPUs.
// With the N-N model, all CPUs receive the interrupt independently. The Pending // 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 // status is cleared only for the CPU that takes it, not for the other CPUs
}; };

View File

@ -76,10 +76,12 @@ __boot:
b 1b b 1b
corezero_start: corezero_start:
@ assume __bss_len is 128 byte aligned @ assumes the .bss section size is 128 byte aligned (or zero)
ldr r0, =__bss_pa ldr r0, =__bss_pa
ldr r1, =__bss_len ldr r1, =__bss_va_end @ calculate the length of .bss using the VA start and end
add r1, r0, r1 ldr r2, =__bss_va
sub r1, r1, r2
add r1, r0, r1 @ fixup to be PA start and end
mov r2, #0 mov r2, #0
mov r3, #0 mov r3, #0
mov r4, #0 mov r4, #0

View File

@ -27,6 +27,8 @@
#include "hw/mcu.h" #include "hw/mcu.h"
#include "hw/gpulcd.h" #include "hw/gpulcd.h"
#include "system/event.h"
static struct static struct
{ {
u16 lcdIds; // Bits 0-7 top screen, 8-15 bottom screen. u16 lcdIds; // Bits 0-7 top screen, 8-15 bottom screen.
@ -112,13 +114,13 @@ unsigned GFX_init(GfxFbFmt mode)
TIMER_WaitMS(10); TIMER_WaitMS(10);
resetLcdsMaybe(); resetLcdsMaybe();
MCU_controlLCDPower(2u); // Power on LCDs. MCU_controlLCDPower(2u); // Power on LCDs.
if(MCU_waitEvents(0x3Fu<<24) != 2u<<24) __builtin_trap(); if(eventWait(getEventMCU(), 0x3Fu<<24, 0x3Fu<<24) != 2u<<24) __builtin_trap();
waitLcdsReady(); waitLcdsReady();
REG_LCD_ABL0_LIGHT_PWM = 0x1023E; REG_LCD_ABL0_LIGHT_PWM = 0x1023E;
REG_LCD_ABL1_LIGHT_PWM = 0x1023E; REG_LCD_ABL1_LIGHT_PWM = 0x1023E;
MCU_controlLCDPower(0x28u); // Power on backlights. MCU_controlLCDPower(0x28u); // Power on backlights.
if(MCU_waitEvents(0x3Fu<<24) != 0x28u<<24) __builtin_trap(); if(eventWait(getEventMCU(), 0x3Fu<<24, 0x3Fu<<24) != 0x28u<<24) __builtin_trap();
g_gfxState.lcdPower = 0x15; // All on. g_gfxState.lcdPower = 0x15; // All on.
// Make sure the fills finished. // Make sure the fills finished.
@ -212,8 +214,9 @@ void GFX_powerOnBacklights(GfxBlight mask)
mask <<= 1; mask <<= 1;
MCU_controlLCDPower(mask); // Power on backlights. MCU_controlLCDPower(mask); // Power on backlights.
if(MCU_waitEvents(0x3Fu<<24) != (u32)mask<<24) eventWait(getEventMCU(), 0x3F<<24, 0x3F<<24);
__builtin_trap(); /*if(mcuEventWait(0x3Fu<<24) != (u32)mask<<24)
__builtin_trap();*/
} }
void GFX_powerOffBacklights(GfxBlight mask) void GFX_powerOffBacklights(GfxBlight mask)
@ -221,8 +224,9 @@ void GFX_powerOffBacklights(GfxBlight mask)
g_gfxState.lcdPower &= ~mask; g_gfxState.lcdPower &= ~mask;
MCU_controlLCDPower(mask); // Power off backlights. MCU_controlLCDPower(mask); // Power off backlights.
if(MCU_waitEvents(0x3Fu<<24) != (u32)mask<<24) eventWait(getEventMCU(), 0x3F<<24, 0x3F<<24);
__builtin_trap(); /*if(mcuEventWait(0x3Fu<<24) != (u32)mask<<24)
__builtin_trap();*/
} }
u8 GFX_getBrightness(void) u8 GFX_getBrightness(void)

View File

@ -52,7 +52,7 @@ u64 HID_GetState(void)
CODEC_Get(&codec); CODEC_Get(&codec);
ret = REG_HID | MCU_GetSpecialHID(); ret = REG_HID | mcuGetSpecialHID();
if (!(ret & BUTTON_ARROW)) if (!(ret & BUTTON_ARROW))
ret |= HID_ConvertCPAD(codec.cpad_x, codec.cpad_y); ret |= HID_ConvertCPAD(codec.cpad_x, codec.cpad_y);

View File

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

View File

@ -26,51 +26,48 @@
#define MCU_INTERRUPT (0x71) #define MCU_INTERRUPT (0x71)
#define I2C_MCU_DEVICE (3) #define I2C_MCU_DEVICE (3)
u8 MCU_GetVolumeSlider(void); enum {
u32 MCU_GetSpecialHID(void); MCUEV_HID_PWR_DOWN = BIT(0),
MCUEV_HID_PWR_HOLD = BIT(1),
MCUEV_HID_HOME_DOWN = BIT(2),
MCUEV_HID_HOME_UP = BIT(3),
MCUEV_HID_WIFI_SWITCH = BIT(4),
MCUEV_HID_SHELL_CLOSE = BIT(5),
MCUEV_HID_SHELL_OPEN = BIT(6),
MCUEV_HID_VOLUME_SLIDER = BIT(22),
};
void MCU_SetNotificationLED(u32 period_ms, u32 color); u8 mcuGetVolumeSlider(void);
void MCU_ResetLED(void); u32 mcuGetSpecialHID(void);
void MCU_HandleInterrupts(u32 irqn); void mcuSetStatusLED(u32 period_ms, u32 color);
void mcuResetLEDs(void);
void MCU_Init(void); void mcuReset(void);
static inline u8 MCU_ReadReg(u8 addr) static inline u8 mcuReadReg(u8 addr)
{ {
u8 val; u8 val;
I2C_readRegBuf(I2C_MCU_DEVICE, addr, &val, 1); I2C_readRegBuf(I2C_MCU_DEVICE, addr, &val, 1);
return val; return val;
} }
static inline bool MCU_ReadRegBuf(u8 addr, u8 *buf, u32 size) static inline bool mcuReadRegBuf(u8 addr, u8 *buf, u32 size)
{ {
return I2C_readRegBuf(I2C_MCU_DEVICE, addr, buf, size); return I2C_readRegBuf(I2C_MCU_DEVICE, addr, buf, size);
} }
static inline bool MCU_WriteReg(u8 addr, u8 val) static inline bool mcuWriteReg(u8 addr, u8 val)
{ {
return I2C_writeRegBuf(I2C_MCU_DEVICE, addr, &val, 1); return I2C_writeRegBuf(I2C_MCU_DEVICE, addr, &val, 1);
} }
static inline bool MCU_WriteRegBuf(u8 addr, const u8 *buf, u32 size) static inline bool mcuWriteRegBuf(u8 addr, const u8 *buf, u32 size)
{ {
return I2C_writeRegBuf(I2C_MCU_DEVICE, addr, buf, size); return I2C_writeRegBuf(I2C_MCU_DEVICE, addr, buf, size);
} }
static inline u32 MCU_waitEvents(u32 mask) {
u32 v;
while(1) {
TIMER_WaitMS(10);
MCU_ReadRegBuf(0x10, (u8*)&v, 4);
v &= mask;
if (v)
break;
}
return v;
}
static inline void MCU_controlLCDPower(u8 bits) static inline void MCU_controlLCDPower(u8 bits)
{ {
MCU_WriteReg(0x22u, bits); mcuWriteReg(0x22u, bits);
} }

View File

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

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

View File

@ -107,20 +107,17 @@ void SYS_CoreZeroInit(void)
mmuMapArea(0x20000000, 0x20000000, 128UL << 20, MMU_FLAGS(MMU_CACHE_WB, MMU_READ_WRITE, 1, 1)); mmuMapArea(0x20000000, 0x20000000, 128UL << 20, MMU_FLAGS(MMU_CACHE_WB, MMU_READ_WRITE, 1, 1));
} }
if (SYS_IsNewConsole()) { // screen init magicks
TIMER_WaitMS(150); TIMER_WaitMS(64);
}
// Initialize peripherals // Initialize peripherals
PXI_Reset(); PXI_Reset();
I2C_init(); I2C_init();
MCU_Init(); mcuReset();
SPI_Init(); SPI_Init();
CODEC_Init(); CODEC_Init();
GFX_init(GFX_RGB565);
} }
void SYS_CoreInit(void) void SYS_CoreInit(void)

View File

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

View File

@ -4,8 +4,10 @@ ENTRY(_start)
MEMORY MEMORY
{ {
AHBWRAM (RWX) : ORIGIN = 0x08006000, LENGTH = 512K
VECTORS (RX) : ORIGIN = 0x08000000, LENGTH = 64 VECTORS (RX) : ORIGIN = 0x08000000, LENGTH = 64
CODEMEM (RX) : ORIGIN = 0x08000040, LENGTH = 512K - 64
BOOTROM (R) : ORIGIN = 0x08080000, LENGTH = 128K /* BootROM mirrors, don't touch! */
DATAMEM (RW) : ORIGIN = 0x080A0000, LENGTH = 384K
} }
SECTIONS SECTIONS
@ -16,7 +18,7 @@ SECTIONS
KEEP(*(.vectors)); KEEP(*(.vectors));
. = ALIGN(4); . = ALIGN(4);
__vectors_len = ABSOLUTE(.) - __vectors_vma; __vectors_len = ABSOLUTE(.) - __vectors_vma;
} >VECTORS AT>AHBWRAM } >VECTORS AT>CODEMEM
.text : ALIGN(4) { .text : ALIGN(4) {
__text_s = ABSOLUTE(.); __text_s = ABSOLUTE(.);
@ -24,24 +26,28 @@ SECTIONS
*(.text*); *(.text*);
. = ALIGN(4); . = ALIGN(4);
__text_e = ABSOLUTE(.); __text_e = ABSOLUTE(.);
} >AHBWRAM } >CODEMEM
.rodata : ALIGN(4) { .rodata : ALIGN(4) {
*(.rodata*); *(.rodata*);
. = ALIGN(4); . = ALIGN(4);
} >AHBWRAM __exidx_start = .;
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
__exidx_end = .;
. = ALIGN(4);
} >DATAMEM
.data : ALIGN(4) { .data : ALIGN(4) {
*(.data*); *(.data*);
. = ALIGN(4); . = ALIGN(4);
} >AHBWRAM } >DATAMEM
.bss : ALIGN(4) { .bss (NOLOAD) : ALIGN(4) {
__bss_start = .; __bss_start = .;
*(.bss*); *(.bss*);
. = ALIGN(4); . = ALIGN(4);
__bss_end = .; __bss_end = .;
} >AHBWRAM } >DATAMEM
__end__ = ABSOLUTE(.); __end__ = ABSOLUTE(.);
} }

View File

@ -19,7 +19,7 @@ static void SetNotificationLED(u32 period_ms, u32 rgb565_color)
(rgb565_color >> 5) << (8+2) | (rgb565_color >> 5) << (8+2) |
(rgb565_color << 3)); (rgb565_color << 3));
u32 args[] = {period_ms, rgb888_color}; u32 args[] = {period_ms, rgb888_color};
PXI_DoCMD(PXI_NOTIFY_LED, args, 2); PXI_DoCMD(PXICMD_SET_NOTIFY_LED, args, 2);
} }
// there's some weird thing going on when reading this // there's some weird thing going on when reading this

View File

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

View File

@ -7,23 +7,23 @@ bool is_valid_dstime(DsTime* dstime) {
(DSTIMEGET(dstime, bcd_m) >= 60) || (DSTIMEGET(dstime, bcd_m) >= 60) ||
(DSTIMEGET(dstime, bcd_s) >= 60)) (DSTIMEGET(dstime, bcd_s) >= 60))
return false; return false;
// check the date... // check the date...
u32 year = 2000 + DSTIMEGET(dstime, bcd_Y); u32 year = 2000 + DSTIMEGET(dstime, bcd_Y);
u32 month = DSTIMEGET(dstime, bcd_M); u32 month = DSTIMEGET(dstime, bcd_M);
u32 day = DSTIMEGET(dstime, bcd_D); u32 day = DSTIMEGET(dstime, bcd_D);
// date: year & month // date: year & month
if ((year >= 2100) || (month == 0) || (month > 12)) if ((year >= 2100) || (month == 0) || (month > 12))
return false; return false;
// date: day // date: day
// see: https://github.com/devkitPro/libnds/blob/9678bf09389cb1fcdc99dfa0357ec0cbe51dd0b7/source/arm7/clock.c#L224-L262 // see: https://github.com/devkitPro/libnds/blob/9678bf09389cb1fcdc99dfa0357ec0cbe51dd0b7/source/arm7/clock.c#L224-L262
u32 months_lastday[1+12] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; u32 months_lastday[1+12] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
u32 leap = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0; u32 leap = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0;
u32 days_in_month = months_lastday[month] + ((month == 2) ? leap : 0); u32 days_in_month = months_lastday[month] + ((month == 2) ? leap : 0);
if (day > days_in_month) return false; if (day > days_in_month) return false;
return true; return true;
} }

View File

@ -27,7 +27,7 @@ void CreateScreenshot(void) {
fvx_rmkdir(OUTPUT_PATH); fvx_rmkdir(OUTPUT_PATH);
get_dstime(&dstime); get_dstime(&dstime);
snprintf(filename, 64, OUTPUT_PATH "/snap_%02X%02X%02X%02X%02X%02X.png", snprintf(filename, sizeof(filename), OUTPUT_PATH "/snap_%02X%02X%02X%02X%02X%02X.png",
dstime.bcd_Y, dstime.bcd_M, dstime.bcd_D, dstime.bcd_Y, dstime.bcd_M, dstime.bcd_D,
dstime.bcd_h, dstime.bcd_m, dstime.bcd_s); dstime.bcd_h, dstime.bcd_m, dstime.bcd_s);
filename[63] = '\0'; filename[63] = '\0';

View File

@ -1,8 +1,10 @@
#include <stdarg.h> #include <stdarg.h>
#include "language.h"
#include "swkbd.h" #include "swkbd.h"
#include "timer.h" #include "timer.h"
#include "hid.h" #include "hid.h"
#include "utf.h"
static inline char to_uppercase(char c) { static inline char to_uppercase(char c) {
@ -58,10 +60,7 @@ static bool BuildKeyboard(TouchBox* swkbd, const char* keys, const u8* layout) {
} }
// next row // next row
if (layout[l++] != 0) { if (layout[l++] != 0) return false;
ShowPrompt(false, "Oh shit %lu %lu", k, l);
return false; // error!!!! THIS HAS TO GO!
}
p_y += SWKBD_STDKEY_HEIGHT + SWKDB_KEY_SPACING; p_y += SWKBD_STDKEY_HEIGHT + SWKDB_KEY_SPACING;
} }
@ -73,7 +72,7 @@ static bool BuildKeyboard(TouchBox* swkbd, const char* keys, const u8* layout) {
static void DrawKey(const TouchBox* key, const bool pressed, const u32 uppercase) { static void DrawKey(const TouchBox* key, const bool pressed, const u32 uppercase) {
const char* keystrs[] = { SWKBD_KEYSTR }; const char* keystrs[] = { SWKBD_KEYSTR };
const u32 color = (pressed) ? COLOR_SWKBD_PRESSED : const u32 color = (pressed) ? COLOR_SWKBD_PRESSED :
(key->id == KEY_ENTER) ? COLOR_SWKBD_ENTER : (key->id == KEY_ENTER) ? COLOR_SWKBD_ENTER :
((key->id == KEY_CAPS) && (uppercase > 1)) ? COLOR_SWKBD_CAPS : ((key->id == KEY_CAPS) && (uppercase > 1)) ? COLOR_SWKBD_CAPS :
COLOR_SWKBD_NORMAL; COLOR_SWKBD_NORMAL;
@ -82,7 +81,7 @@ static void DrawKey(const TouchBox* key, const bool pressed, const u32 uppercase
if (key->id == KEY_TXTBOX) return; if (key->id == KEY_TXTBOX) return;
char keystr[16]; char keystr[16];
if (key->id >= 0x80) snprintf(keystr, 16, "%s", keystrs[key->id - 0x80]); if (key->id >= 0x80) snprintf(keystr, sizeof(keystr), "%s", keystrs[key->id - 0x80]);
else { else {
keystr[0] = (uppercase) ? to_uppercase(key->id) : key->id; keystr[0] = (uppercase) ? to_uppercase(key->id) : key->id;
keystr[1] = 0; keystr[1] = 0;
@ -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; const u32 f_offs_y = (key->h - FONT_HEIGHT_EXT) / 2;
DrawRectangle(BOT_SCREEN, key->x, key->y, key->w, key->h, color); DrawRectangle(BOT_SCREEN, key->x, key->y, key->w, key->h, color);
DrawString(BOT_SCREEN, keystr, key->x + f_offs_x, key->y + f_offs_y, COLOR_SWKBD_CHARS, color, false); DrawString(BOT_SCREEN, keystr, key->x + f_offs_x, key->y + f_offs_y, COLOR_SWKBD_CHARS, color);
} }
static void DrawKeyBoardBox(TouchBox* swkbd, u32 color) { static void DrawKeyBoardBox(TouchBox* swkbd, u32 color) {
@ -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) { static void DrawTextBox(const TouchBox* txtbox, const char* inputstr, const u32 cursor, u32* scroll) {
const u32 input_shown = (txtbox->w / FONT_WIDTH_EXT) - 2; const u32 input_shown_length = (txtbox->w / FONT_WIDTH_EXT) - 2;
const u32 inputstr_size = strlen(inputstr); // we rely on a zero terminated string const u32 inputstr_size = strlen(inputstr); // we rely on a zero terminated string
const u16 x = txtbox->x; const u16 x = txtbox->x;
const u16 y = txtbox->y; const u16 y = txtbox->y;
// fix scroll // fix scroll
if (cursor < *scroll) *scroll = cursor; if (cursor < *scroll) {
else if (cursor - *scroll > input_shown) *scroll = cursor - input_shown; *scroll = cursor;
} else {
int scroll_adjust = -input_shown_length;
for (u32 i = *scroll; i < cursor; i++) {
if (i >= inputstr_size || (inputstr[i] & 0xC0) != 0x80) scroll_adjust++;
}
for (int i = 0; i < scroll_adjust; i++)
*scroll += *scroll >= inputstr_size ? 1 : GetCharSize(inputstr + *scroll);
}
u32 input_shown_size = 0;
for (u32 i = 0; i < input_shown_length || (*scroll + input_shown_size < inputstr_size && (inputstr[*scroll + input_shown_size] & 0xC0) == 0x80); input_shown_size++) {
if (*scroll + input_shown_size >= inputstr_size || (inputstr[*scroll + input_shown_size] & 0xC0) != 0x80) i++;
}
// draw input string // draw input string
DrawStringF(BOT_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%c%-*.*s%-*.*s%c", DrawStringF(BOT_SCREEN, x, y, COLOR_STD_FONT, COLOR_STD_BG, "%c%-*.*s%c",
(*scroll) ? '<' : '|', (*scroll) ? '<' : '|',
(inputstr_size > input_shown) ? input_shown : inputstr_size, (int) input_shown_size,
(inputstr_size > input_shown) ? input_shown : inputstr_size, (int) input_shown_size,
inputstr + *scroll, (*scroll > inputstr_size) ? "" : inputstr + *scroll,
(inputstr_size > input_shown) ? 0 : input_shown - inputstr_size, (inputstr_size - (s32) *scroll > input_shown_size) ? '>' : '|'
(inputstr_size > input_shown) ? 0 : input_shown - inputstr_size,
"",
(inputstr_size - (s32) *scroll > input_shown) ? '>' : '|'
); );
// draw cursor // draw cursor
u16 cpos = 0;
for (u16 i = *scroll; i < cursor; i++) {
if (i >= inputstr_size || (inputstr[i] & 0xC0) != 0x80) cpos++;
}
DrawStringF(BOT_SCREEN, x-(FONT_WIDTH_EXT/2), y+10, COLOR_STD_FONT, COLOR_STD_BG, "%-*.*s^%-*.*s", DrawStringF(BOT_SCREEN, x-(FONT_WIDTH_EXT/2), y+10, COLOR_STD_FONT, COLOR_STD_BG, "%-*.*s^%-*.*s",
1 + cursor - *scroll, (int) (1 + cpos),
1 + cursor - *scroll, (int) (1 + cpos),
"", "",
input_shown - (cursor - *scroll), (int) (input_shown_length - cpos),
input_shown - (cursor - *scroll), (int) (input_shown_length - cpos),
"" ""
); );
} }
@ -166,8 +181,17 @@ static void MoveTextBoxCursor(const TouchBox* txtbox, const char* inputstr, cons
const TouchBox* tb = TouchBoxGet(&id, x, y, txtbox, 0); const TouchBox* tb = TouchBoxGet(&id, x, y, txtbox, 0);
if (id == KEY_TXTBOX) { if (id == KEY_TXTBOX) {
u16 x_tb = x - tb->x; u16 x_tb = x - tb->x;
u16 cpos = (x_tb < (FONT_WIDTH_EXT/2)) ? 0 : (x_tb - (FONT_WIDTH_EXT/2)) / FONT_WIDTH_EXT;
u32 cursor_next = *scroll + ((cpos <= input_shown) ? cpos : input_shown); const u32 inputstr_size = strlen(inputstr);
const u16 cpos_x = (x_tb < (FONT_WIDTH_EXT/2)) ? 0 : (x_tb - (FONT_WIDTH_EXT/2)) / FONT_WIDTH_EXT;
u16 cpos_length = 0;
u16 cpos_size = 0;
while ((cpos_length < cpos_x && cpos_length < input_shown) || (*scroll + cpos_size < inputstr_size && (inputstr[*scroll + cpos_size] & 0xC0) == 0x80)) {
if (*scroll + cpos_size >= inputstr_size || (inputstr[*scroll + cpos_size] & 0xC0) != 0x80) cpos_length++;
cpos_size++;
}
u32 cursor_next = *scroll + cpos_size;
// move cursor to position pointed to // move cursor to position pointed to
if (*cursor != cursor_next) { if (*cursor != cursor_next) {
if (cursor_next < max_size) *cursor = cursor_next; if (cursor_next < max_size) *cursor = cursor_next;
@ -176,10 +200,10 @@ static void MoveTextBoxCursor(const TouchBox* txtbox, const char* inputstr, cons
} }
// move beyound visible field // move beyound visible field
if (timer_msec(timer) >= scroll_cooldown) { if (timer_msec(timer) >= scroll_cooldown) {
if ((cpos == 0) && (*scroll > 0)) if ((cpos_length == 0) && (*scroll > 0))
(*scroll)--; *scroll -= GetPrevCharSize(inputstr + *scroll);
else if ((cpos >= input_shown) && (*cursor < (max_size-1))) else if ((cpos_length >= input_shown) && (*cursor < (max_size-1)))
(*scroll)++; *scroll += GetCharSize(inputstr + *scroll);
} }
} }
} }
@ -199,6 +223,7 @@ static char KeyboardWait(TouchBox* swkbd, bool uppercase) {
else if (pressed & BUTTON_R1) return KEY_CAPS; else if (pressed & BUTTON_R1) return KEY_CAPS;
else if (pressed & BUTTON_RIGHT) return KEY_RIGHT; else if (pressed & BUTTON_RIGHT) return KEY_RIGHT;
else if (pressed & BUTTON_LEFT) return KEY_LEFT; else if (pressed & BUTTON_LEFT) return KEY_LEFT;
else if (pressed & BUTTON_SELECT) return KEY_SWITCH;
else if (pressed & BUTTON_TOUCH) break; 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 swkbd_numpad[32];
TouchBox* textbox = swkbd_alphabet; // always use this textbox TouchBox* textbox = swkbd_alphabet; // always use this textbox
static bool show_instr = true;
const char* instr = STR_KEYBOARD_CONTROLS_DETAILS;
if (show_instr) {
ShowPrompt(false, "%s", instr);
show_instr = false;
}
// generate keyboards // generate keyboards
if (!BuildKeyboard(swkbd_alphabet, keys_alphabet, layout_alphabet)) return false; if (!BuildKeyboard(swkbd_alphabet, keys_alphabet, layout_alphabet)) return false;
if (!BuildKeyboard(swkbd_special, keys_special, layout_special)) return false; if (!BuildKeyboard(swkbd_special, keys_special, layout_special)) return false;
@ -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 char str[512]; // arbitrary limit, should be more than enough
va_list va; va_list va;
va_start(va, format); va_start(va, format);
vsnprintf(str, 512, format, va); vsnprintf(str, sizeof(str), format, va);
va_end(va); va_end(va);
u32 str_width = GetDrawStringWidth(str); u32 str_width = GetDrawStringWidth(str);
if (str_width < (24 * FONT_WIDTH_EXT)) str_width = 24 * FONT_WIDTH_EXT; if (str_width < (24 * FONT_WIDTH_EXT)) str_width = 24 * FONT_WIDTH_EXT;
@ -281,22 +313,51 @@ bool ShowKeyboard(char* inputstr, const u32 max_size, const char *format, ...) {
break; break;
} else if (key == KEY_BKSPC) { } else if (key == KEY_BKSPC) {
if (cursor) { if (cursor) {
int size = GetPrevCharSize(inputstr + cursor);
if (cursor <= inputstr_size) { if (cursor <= inputstr_size) {
memmove(inputstr + cursor - 1, inputstr + cursor, inputstr_size - cursor + 1); memmove(inputstr + cursor - size, inputstr + cursor, inputstr_size - cursor + size);
inputstr_size--; inputstr_size -= size;
} }
cursor--; cursor -= size;
} }
} else if (key == KEY_LEFT) { } else if (key == KEY_LEFT) {
if (cursor) cursor--; if (cursor) cursor -= GetPrevCharSize(inputstr + cursor);
} else if (key == KEY_RIGHT) { } else if (key == KEY_RIGHT) {
if (cursor < (max_size-1)) cursor++; int size = cursor > inputstr_size ? 1 : GetCharSize(inputstr + cursor);
if (cursor + size < max_size) cursor += size;
} else if (key == KEY_ALPHA) { } else if (key == KEY_ALPHA) {
swkbd = swkbd_alphabet; swkbd = swkbd_alphabet;
} else if (key == KEY_SPECIAL) { } else if (key == KEY_SPECIAL) {
swkbd = swkbd_special; swkbd = swkbd_special;
} else if (key == KEY_NUMPAD) { } else if (key == KEY_NUMPAD) {
swkbd = swkbd_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)) { } else if (key && (key < 0x80)) {
if ((cursor < (max_size-1)) && (inputstr_size < max_size)) { if ((cursor < (max_size-1)) && (inputstr_size < max_size)) {
// pad string (if cursor beyound string size) // pad string (if cursor beyound string size)

View File

@ -18,16 +18,17 @@ enum {
KEY_LEFT = 0x88, KEY_LEFT = 0x88,
KEY_RIGHT = 0x89, KEY_RIGHT = 0x89,
KEY_ESCAPE = 0x8A, KEY_ESCAPE = 0x8A,
KEY_SWITCH = 0x8B,
KEY_UNICODE = 0x8C,
KEY_TXTBOX = 0xFF KEY_TXTBOX = 0xFF
}; };
// special key strings // special key strings
#define SWKBD_KEYSTR "", "DEL", "INS", "SUBMIT", "CAPS", "#$@", "123", "ABC", "\x1b", "\x1a", "ESC" #define SWKBD_KEYSTR "", "DEL", "INS", "SUBMIT", "CAPS", "#$@", "123", "ABC", "←", "→", "ESC", "SWITCH", "U+"
#define COLOR_SWKBD_NORMAL COLOR_GREY #define COLOR_SWKBD_NORMAL COLOR_GREY
#define COLOR_SWKBD_PRESSED COLOR_LIGHTGREY #define COLOR_SWKBD_PRESSED COLOR_LIGHTGREY
#define COLOR_SWKBD_BOX COLOR_DARKGREY #define COLOR_SWKBD_BOX COLOR_DARKGREY
#define COLOR_SWKBD_TEXTBOX COLOR_DARKGREY
#define COLOR_SWKBD_CHARS COLOR_BLACK #define COLOR_SWKBD_CHARS COLOR_BLACK
#define COLOR_SWKBD_ENTER COLOR_TINTEDBLUE #define COLOR_SWKBD_ENTER COLOR_TINTEDBLUE
#define COLOR_SWKBD_CAPS COLOR_TINTEDYELLOW #define COLOR_SWKBD_CAPS COLOR_TINTEDYELLOW
@ -56,7 +57,7 @@ enum {
'4', '5', '6', 'D', 'C', \ '4', '5', '6', 'D', 'C', \
'3', '2', '1', 'B', 'A', \ '3', '2', '1', 'B', 'A', \
'0', '.', '_', KEY_LEFT, KEY_RIGHT, \ '0', '.', '_', KEY_LEFT, KEY_RIGHT, \
KEY_ALPHA, ' ', KEY_BKSPC KEY_ALPHA, KEY_UNICODE, ' ', KEY_BKSPC
// offset, num of keys in row, width of special keys (...), 0 // offset, num of keys in row, width of special keys (...), 0
#define SWKBD_LAYOUT_ALPHABET \ #define SWKBD_LAYOUT_ALPHABET \
@ -80,9 +81,9 @@ enum {
5, 0, \ 5, 0, \
5, 0, \ 5, 0, \
5, 18, 18, 0, \ 5, 18, 18, 0, \
3, 30, 34, 30, 0, \ 4, 20, 20, 31, 20, 0, \
0 0
#define ShowKeyboardOrPrompt (TouchIsCalibrated() ? ShowKeyboard : ShowStringPrompt) #define ShowKeyboardOrPrompt (TouchIsCalibrated() ? ShowKeyboard : ShowStringPrompt)
bool ShowKeyboard(char* inputstr, u32 max_size, const char *format, ...); bool PRINTF_ARGS(3) ShowKeyboard(char* inputstr, u32 max_size, const char *format, ...);

View File

@ -7,7 +7,7 @@ u64 timer_start( void ) {
if (!(*TIMER_CNT0 & *TIMER_CNT1 & *TIMER_CNT2 & *TIMER_CNT3 & TIMER_ACTIVE) || if (!(*TIMER_CNT0 & *TIMER_CNT1 & *TIMER_CNT2 & *TIMER_CNT3 & TIMER_ACTIVE) ||
!(*TIMER_CNT1 & *TIMER_CNT2 & *TIMER_CNT3 & TIMER_COUNT_UP)) !(*TIMER_CNT1 & *TIMER_CNT2 & *TIMER_CNT3 & TIMER_COUNT_UP))
timer_init = true; timer_init = true;
if (timer_init) { if (timer_init) {
// deactivate, then reset timers // deactivate, then reset timers
*TIMER_CNT0 = 0; *TIMER_CNT0 = 0;
@ -17,7 +17,7 @@ u64 timer_start( void ) {
// start timers // start timers
*TIMER_CNT0 = TIMER_ACTIVE; *TIMER_CNT0 = TIMER_ACTIVE;
*TIMER_CNT1 = *TIMER_CNT2 = *TIMER_CNT3 = TIMER_ACTIVE | TIMER_COUNT_UP; *TIMER_CNT1 = *TIMER_CNT2 = *TIMER_CNT3 = TIMER_ACTIVE | TIMER_COUNT_UP;
// timer initialized // timer initialized
timer_init = false; timer_init = false;
} }

View File

@ -2,6 +2,7 @@
#include "ui.h" #include "ui.h"
#include "hid.h" #include "hid.h"
#include "crc16.h" #include "crc16.h"
#include "language.h"
#include "spiflash.h" #include "spiflash.h"
#include "support.h" #include "support.h"
@ -46,8 +47,8 @@ bool ShowTouchCalibrationDialog(void)
// clear screen, draw instructions // clear screen, draw instructions
ClearScreen(BOT_SCREEN, COLOR_STD_BG); ClearScreen(BOT_SCREEN, COLOR_STD_BG);
DrawStringCenter(BOT_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, DrawStringCenter(BOT_SCREEN, COLOR_STD_FONT, COLOR_STD_BG, "%s",
"Touch the red crosshairs to\ncalibrate your touchscreen.\n \nUse the stylus for best\nresults!"); STR_TOUCH_CROSSHAIRS_TO_CALIBRATE_TOUCHSCREEN_USE_STYLUS);
// set calibration defaults // set calibration defaults
SetCalibrationDefaults(); SetCalibrationDefaults();
@ -89,7 +90,7 @@ bool CalibrateTouchFromSupportFile(void) {
size_t size = LoadSupportFile(TOUCH_CALIB_FILENAME, calibrations, sizeof(calibrations)); size_t size = LoadSupportFile(TOUCH_CALIB_FILENAME, calibrations, sizeof(calibrations));
u32 n_dots = size / sizeof(HID_CalibrationData); u32 n_dots = size / sizeof(HID_CalibrationData);
is_calibrated = (n_dots == 0) ? false : is_calibrated = (n_dots == 0) ? false :
HID_SetCalibrationData(calibrations, n_dots, SCREEN_WIDTH_BOT, SCREEN_HEIGHT); HID_SetCalibrationData(calibrations, n_dots, SCREEN_WIDTH_BOT, SCREEN_HEIGHT);
return is_calibrated; return is_calibrated;
} }
@ -99,11 +100,11 @@ bool CalibrateTouchFromFlash(void) {
// set calibration defaults // set calibration defaults
SetCalibrationDefaults(); SetCalibrationDefaults();
// check SPIflash status // check SPIflash status
if (!spiflash_get_status()) if (!spiflash_get_status())
return false; return false;
// check NVRAM console ID // check NVRAM console ID
u32 console_id = 0; u32 console_id = 0;
if (!spiflash_read(0x001C, 4, (u8*)&console_id)) if (!spiflash_read(0x001C, 4, (u8*)&console_id))

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -189,7 +189,7 @@ int utf16_to_utf8(u8 *out, const u16 *in, int len_out, int len_in)
units = decode_utf16(&code, in); units = decode_utf16(&code, in);
if(units == -1) if(units == -1)
return -1; return -1;
if (len_in >= units) if (len_in >= units)
len_in -= units; len_in -= units;
else return -1; else return -1;
@ -238,7 +238,7 @@ int utf8_to_utf16(u16 *out, const u8 *in, int len_out, int len_in)
units = decode_utf8(&code, in); units = decode_utf8(&code, in);
if(units == -1) if(units == -1)
return -1; return -1;
if (len_in >= units) if (len_in >= units)
len_in -= units; len_in -= units;
else return -1; else return -1;

View File

@ -213,11 +213,11 @@ void ctr_decrypt_byte(void *inbuf, void *outbuf, size_t size, size_t off, uint32
uint8_t *in = inbuf; uint8_t *in = inbuf;
uint8_t *out = outbuf; uint8_t *out = outbuf;
uint32_t i; uint32_t i;
for (i=0; i<AES_BLOCK_SIZE; i++) // setup local ctr for (i=0; i<AES_BLOCK_SIZE; i++) // setup local ctr
ctr_local[i] = ctr[i]; ctr_local[i] = ctr[i];
add_ctr(ctr_local, off / AES_BLOCK_SIZE); add_ctr(ctr_local, off / AES_BLOCK_SIZE);
if (off_fix) // handle misaligned offset (at beginning) if (off_fix) // handle misaligned offset (at beginning)
{ {
size_t last_byte = ((off_fix + bytes_left) >= AES_BLOCK_SIZE) ? size_t last_byte = ((off_fix + bytes_left) >= AES_BLOCK_SIZE) ?
@ -229,7 +229,7 @@ void ctr_decrypt_byte(void *inbuf, void *outbuf, size_t size, size_t off, uint32
*(out++) = temp[i]; *(out++) = temp[i];
bytes_left -= last_byte - off_fix; bytes_left -= last_byte - off_fix;
} }
if (bytes_left >= AES_BLOCK_SIZE) if (bytes_left >= AES_BLOCK_SIZE)
{ {
size_t blocks = bytes_left / AES_BLOCK_SIZE; size_t blocks = bytes_left / AES_BLOCK_SIZE;
@ -238,7 +238,7 @@ void ctr_decrypt_byte(void *inbuf, void *outbuf, size_t size, size_t off, uint32
out += AES_BLOCK_SIZE * blocks; out += AES_BLOCK_SIZE * blocks;
bytes_left -= AES_BLOCK_SIZE * blocks; bytes_left -= AES_BLOCK_SIZE * blocks;
} }
if (bytes_left) // handle misaligned offset (at end) if (bytes_left) // handle misaligned offset (at end)
{ {
for (i=0; i<bytes_left; i++) for (i=0; i<bytes_left; i++)
@ -300,7 +300,7 @@ void aes_cmac(void* inbuf, void* outbuf, size_t size)
AES_CNT_INPUT_ENDIAN | AES_CNT_OUTPUT_ENDIAN; AES_CNT_INPUT_ENDIAN | AES_CNT_OUTPUT_ENDIAN;
uint32_t* out = (uint32_t*) outbuf; uint32_t* out = (uint32_t*) outbuf;
uint32_t* in = (uint32_t*) inbuf; uint32_t* in = (uint32_t*) inbuf;
// create xorpad for last block // create xorpad for last block
set_ctr(zeroes); set_ctr(zeroes);
aes_decrypt(xorpad, xorpad, 1, mode); aes_decrypt(xorpad, xorpad, 1, mode);
@ -312,7 +312,7 @@ void aes_cmac(void* inbuf, void* outbuf, size_t size)
} }
xorpadb[15] <<= 1; xorpadb[15] <<= 1;
xorpadb[15] ^= finalxor; xorpadb[15] ^= finalxor;
// process blocks // process blocks
for (uint32_t i = 0; i < 4; i++) for (uint32_t i = 0; i < 4; i++)
out[i] = 0; out[i] = 0;

View File

@ -10,7 +10,7 @@ u16 crc16_quick(const void* src, u32 len) {
static const u16 tabval[] = { CRC16_TABVAL }; static const u16 tabval[] = { CRC16_TABVAL };
u16* data = (u16*) src; u16* data = (u16*) src;
u16 crc = 0xFFFF; u16 crc = 0xFFFF;
for (len >>= 1; len; len--) { for (len >>= 1; len; len--) {
u16 curr = *(data++); u16 curr = *(data++);
for (u32 i = 0; i < 4; i++) { for (u32 i = 0; i < 4; i++) {
@ -21,6 +21,6 @@ u16 crc16_quick(const void* src, u32 len) {
crc ^= tval; crc ^= tval;
} }
} }
return crc; return crc;
} }

View File

@ -73,7 +73,7 @@ u32 crc32_calculate_from_file(const char* fileName, u32 offset, u32 length) {
return crc32; return crc32;
} }
fvx_lseek(&inputFile, offset); fvx_lseek(&inputFile, offset);
bool ret = true; bool ret = true;
for (u64 pos = 0; (pos < length) && ret; pos += bufsiz) { for (u64 pos = 0; (pos < length) && ret; pos += bufsiz) {
UINT read_bytes = min(bufsiz, length - pos); UINT read_bytes = min(bufsiz, length - pos);
@ -83,7 +83,7 @@ u32 crc32_calculate_from_file(const char* fileName, u32 offset, u32 length) {
ret = false; ret = false;
if (ret) crc32 = crc32_calculate(crc32, buffer, read_bytes); if (ret) crc32 = crc32_calculate(crc32, buffer, read_bytes);
} }
fvx_close(&inputFile); fvx_close(&inputFile);
free(buffer); free(buffer);
return ~crc32; return ~crc32;

View File

@ -17,7 +17,7 @@ static u64 keyYState = 0;
u32 GetUnitKeysType(void) u32 GetUnitKeysType(void)
{ {
static u32 keys_type = KEYS_UNKNOWN; static u32 keys_type = KEYS_UNKNOWN;
if (keys_type == KEYS_UNKNOWN) { if (keys_type == KEYS_UNKNOWN) {
static const u8 slot0x2CSampleRetail[16] = { static const u8 slot0x2CSampleRetail[16] = {
0xBC, 0xC4, 0x16, 0x2C, 0x2A, 0x06, 0x91, 0xEE, 0x47, 0x18, 0x86, 0xB8, 0xEB, 0x2F, 0xB5, 0x48 }; 0xBC, 0xC4, 0x16, 0x2C, 0x2A, 0x06, 0x91, 0xEE, 0x47, 0x18, 0x86, 0xB8, 0xEB, 0x2F, 0xB5, 0x48 };
@ -35,15 +35,18 @@ u32 GetUnitKeysType(void)
keys_type = KEYS_DEVKIT; keys_type = KEYS_DEVKIT;
} }
} }
return keys_type; return keys_type;
} }
void CryptAesKeyInfo(AesKeyInfo* info) { void CryptAesKeyInfo(AesKeyInfo* info) {
static u8 zeroes[16] = { 0 }; static const u8 zeros[16] = { 0 };
static const u8 keyY_dev[16] = {
0xA2, 0x32, 0x4A, 0x7E, 0x7C, 0xE6, 0x1A, 0x9A, 0x45, 0x4A, 0x52, 0x19, 0xB3, 0x5B, 0xE9, 0x3B };
const u8* aeskeyY = GetUnitKeysType() == KEYS_DEVKIT ? &keyY_dev[0] : &zeros[0];
u8 ctr[16] = { 0 }; u8 ctr[16] = { 0 };
memcpy(ctr, (void*) info, 12); // CTR -> slot + type + id + zeroes memcpy(ctr, (void*) info, 12); // CTR -> slot + type + id + zeroes
setup_aeskeyY(0x2C, zeroes); setup_aeskeyY(0x2C, aeskeyY);
use_aeskey(0x2C); use_aeskey(0x2C);
set_ctr(ctr); set_ctr(ctr);
aes_decrypt(info->key, info->key, 1, AES_CNT_CTRNAND_MODE); aes_decrypt(info->key, info->key, 1, AES_CNT_CTRNAND_MODE);
@ -67,15 +70,15 @@ u32 CheckKeySlot(u32 keyslot, char type)
{ 0xBC, 0x83, 0x7C, 0xC9, 0x99, 0xC8, 0x80, 0x9E, 0x8A, 0xDE, 0x4A, 0xFA, 0xAA, 0x72, 0x08, 0x28 } } { 0xBC, 0x83, 0x7C, 0xC9, 0x99, 0xC8, 0x80, 0x9E, 0x8A, 0xDE, 0x4A, 0xFA, 0xAA, 0x72, 0x08, 0x28 } }
}; };
u64* state = (type == 'X') ? &keyXState : (type == 'Y') ? &keyYState : (type == 'N') ? &keyState : NULL; u64* state = (type == 'X') ? &keyXState : (type == 'Y') ? &keyYState : (type == 'N') ? &keyState : NULL;
// just to be safe... // just to be safe...
if (keyslot >= 0x40) if (keyslot >= 0x40)
return 1; return 1;
// check if key is already loaded // check if key is already loaded
if ((type != 'I') && ((*state >> keyslot) & 1)) if ((type != 'I') && ((*state >> keyslot) & 1))
return 0; return 0;
// if is not, we may still be able to verify the currently set one (for NCCH keys) // if is not, we may still be able to verify the currently set one (for NCCH keys)
for (u32 p = 0; (type == 'X') && (p < sizeof(keyNcchSamples) / sizeof(AesNcchSampleInfo)); p++) { for (u32 p = 0; (type == 'X') && (p < sizeof(keyNcchSamples) / sizeof(AesNcchSampleInfo)); p++) {
if (keyNcchSamples[p].slot != keyslot) // only for keyslots in the keyNcchSamples table! if (keyNcchSamples[p].slot != keyslot) // only for keyslots in the keyNcchSamples table!
@ -93,7 +96,7 @@ u32 CheckKeySlot(u32 keyslot, char type)
return 0; return 0;
} }
} }
// not set up if getting here // not set up if getting here
return 1; return 1;
} }
@ -102,13 +105,13 @@ u32 CheckKeySlot(u32 keyslot, char type)
// to get rid of these, you may replace this function with anything that works for you // to get rid of these, you may replace this function with anything that works for you
u32 LoadKeyDb(const char* path_db, AesKeyInfo* keydb, u32 bsize) { u32 LoadKeyDb(const char* path_db, AesKeyInfo* keydb, u32 bsize) {
UINT fsize = 0; UINT fsize = 0;
if (path_db) { if (path_db) {
if (fvx_qread (path_db, keydb, 0, bsize, &fsize) != FR_OK) fsize = 0; if (fvx_qread (path_db, keydb, 0, bsize, &fsize) != FR_OK) fsize = 0;
} else if (fvx_qread ("S:/" KEYDB_NAME, keydb, 0, bsize, &fsize) != FR_OK) { } else if (fvx_qread ("S:/" KEYDB_NAME, keydb, 0, bsize, &fsize) != FR_OK) {
fsize = LoadSupportFile(KEYDB_NAME, keydb, bsize); // load key database support file fsize = LoadSupportFile(KEYDB_NAME, keydb, bsize); // load key database support file
} }
u32 nkeys = 0; u32 nkeys = 0;
if (fsize && !(fsize % sizeof(AesKeyInfo))) if (fsize && !(fsize % sizeof(AesKeyInfo)))
nkeys = fsize / sizeof(AesKeyInfo); nkeys = fsize / sizeof(AesKeyInfo);
@ -119,24 +122,24 @@ u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id)
{ {
u8 keystore[16] __attribute__((aligned(32))) = {0}; u8 keystore[16] __attribute__((aligned(32))) = {0};
bool found = false; bool found = false;
// checking the obvious // checking the obvious
if ((keyslot >= 0x40) || ((type != 'X') && (type != 'Y') && (type != 'N') && (type != 'I'))) if ((keyslot >= 0x40) || ((type != 'X') && (type != 'Y') && (type != 'N') && (type != 'I')))
return 1; return 1;
// check if already loaded // check if already loaded
if (!key && !id && (CheckKeySlot(keyslot, type) == 0)) return 0; if (!key && !id && (CheckKeySlot(keyslot, type) == 0)) return 0;
// use keystore if key == NULL // use keystore if key == NULL
if (!key) key = keystore; if (!key) key = keystore;
// try to get key from 'aeskeydb.bin' file // try to get key from 'aeskeydb.bin' file
AesKeyInfo* keydb = (AesKeyInfo*) malloc(STD_BUFFER_SIZE); AesKeyInfo* keydb = (AesKeyInfo*) malloc(STD_BUFFER_SIZE);
u32 nkeys = (keydb) ? LoadKeyDb(NULL, keydb, STD_BUFFER_SIZE) : 0; u32 nkeys = (keydb) ? LoadKeyDb(NULL, keydb, STD_BUFFER_SIZE) : 0;
for (u32 i = 0; i < nkeys; i++) { for (u32 i = 0; i < nkeys; i++) {
AesKeyInfo* info = &(keydb[i]); AesKeyInfo* info = &(keydb[i]);
if (!((info->slot == keyslot) && (info->type == type) && if (!((info->slot == keyslot) && (info->type == type) &&
((!id && !(info->id[0])) || (id && (strncmp(id, info->id, 10) == 0))) && ((!id && !(info->id[0])) || (id && (strncmp(id, info->id, 10) == 0))) &&
(!info->keyUnitType || (info->keyUnitType == GetUnitKeysType())))) (!info->keyUnitType || (info->keyUnitType == GetUnitKeysType()))))
continue; continue;
@ -146,23 +149,23 @@ u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id)
memcpy(key, info->key, 16); memcpy(key, info->key, 16);
break; break;
} }
free(keydb); free(keydb);
// load legacy slot0x??Key?.bin file instead // load legacy slot0x??Key?.bin file instead
if (!found && (type != 'I')) { if (!found && (type != 'I')) {
char fname[64]; char fname[64];
snprintf(fname, 64, "slot0x%02lXKey%s%s.bin", keyslot, snprintf(fname, sizeof(fname), "slot0x%02lXKey%s%s.bin", keyslot,
(type == 'X') ? "X" : (type == 'Y') ? "Y" : (type == 'I') ? "IV" : "", (id) ? id : ""); (type == 'X') ? "X" : (type == 'Y') ? "Y" : (type == 'I') ? "IV" : "", (id) ? id : "");
found = (LoadSupportFile(fname, key, 16) == 16); found = (LoadSupportFile(fname, key, 16) == 16);
} }
// key still not found (duh) // key still not found (duh)
if (!found) return 1; // out of options here if (!found) return 1; // out of options here
// done if this is an IV // done if this is an IV
if (type == 'I') return 0; if (type == 'I') return 0;
// now, setup the key // now, setup the key
if (type == 'X') { if (type == 'X') {
setup_aeskeyX(keyslot, key); setup_aeskeyX(keyslot, key);
@ -177,7 +180,7 @@ u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id)
keyYState |= 1ull << keyslot; keyYState |= 1ull << keyslot;
} }
use_aeskey(keyslot); use_aeskey(keyslot);
return 0; return 0;
} }
@ -186,11 +189,11 @@ u32 InitKeyDb(const char* path)
// use this to quickly initialize all applicable keys in aeskeydb.bin // use this to quickly initialize all applicable keys in aeskeydb.bin
static const u64 keyslot_whitelist = (1ull<<0x02)|(1ull<<0x03)|(1ull<<0x05)|(1ull<<0x18)|(1ull<<0x19)|(1ull<<0x1A)|(1ull<<0x1B)| static const u64 keyslot_whitelist = (1ull<<0x02)|(1ull<<0x03)|(1ull<<0x05)|(1ull<<0x18)|(1ull<<0x19)|(1ull<<0x1A)|(1ull<<0x1B)|
(1ull<<0x1C)|(1ull<<0x1D)|(1ull<<0x1E)|(1ull<<0x1F)|(1ull<<0x24)|(1ull<<0x25)|(1ull<<0x2F); (1ull<<0x1C)|(1ull<<0x1D)|(1ull<<0x1E)|(1ull<<0x1F)|(1ull<<0x24)|(1ull<<0x25)|(1ull<<0x2F);
// try to load aeskeydb.bin file // try to load aeskeydb.bin file
AesKeyInfo* keydb = (AesKeyInfo*) malloc(STD_BUFFER_SIZE); AesKeyInfo* keydb = (AesKeyInfo*) malloc(STD_BUFFER_SIZE);
u32 nkeys = (keydb) ? LoadKeyDb(path, keydb, STD_BUFFER_SIZE) : 0; u32 nkeys = (keydb) ? LoadKeyDb(path, keydb, STD_BUFFER_SIZE) : 0;
// apply all applicable keys // apply all applicable keys
for (u32 i = 0; i < nkeys; i++) { for (u32 i = 0; i < nkeys; i++) {
AesKeyInfo* info = &(keydb[i]); AesKeyInfo* info = &(keydb[i]);
@ -202,7 +205,7 @@ u32 InitKeyDb(const char* path)
if ((info->type == 'I') || (*(info->id)) || (info->keyUnitType && (info->keyUnitType != GetUnitKeysType())) || if ((info->type == 'I') || (*(info->id)) || (info->keyUnitType && (info->keyUnitType != GetUnitKeysType())) ||
(CheckKeySlot(info->slot, info->type) == 0)) continue; // most likely valid, but not applicable or already set (CheckKeySlot(info->slot, info->type) == 0)) continue; // most likely valid, but not applicable or already set
if (info->isEncrypted) CryptAesKeyInfo(info); // decrypt key if (info->isEncrypted) CryptAesKeyInfo(info); // decrypt key
// apply key // apply key
u8 key[16] __attribute__((aligned(32))) = {0}; u8 key[16] __attribute__((aligned(32))) = {0};
char type = info->type; char type = info->type;
@ -222,29 +225,7 @@ u32 InitKeyDb(const char* path)
} }
use_aeskey(keyslot); use_aeskey(keyslot);
} }
free(keydb); free(keydb);
return (nkeys) ? 0 : 1; return (nkeys) ? 0 : 1;
} }
// creates dependency to "sha.h", not required for base keydb functions
u32 CheckRecommendedKeyDb(const char* path)
{
// SHA-256 of the recommended aeskeydb.bin file
// equals MD5 A5B28945A7C051D7A0CD18AF0E580D1B
static const u8 recommended_sha[0x20] = {
0x40, 0x76, 0x54, 0x3D, 0xA3, 0xFF, 0x91, 0x1C, 0xE1, 0xCC, 0x4E, 0xC7, 0x2F, 0x92, 0xE4, 0xB7,
0x2B, 0x24, 0x00, 0x15, 0xBE, 0x9B, 0xFC, 0xDE, 0x7F, 0xED, 0x95, 0x1D, 0xD5, 0xAB, 0x2D, 0xCB
};
// try to load aeskeydb.bin file
AesKeyInfo* keydb = (AesKeyInfo*) malloc(STD_BUFFER_SIZE);
if (!keydb) return 1;
u32 nkeys = LoadKeyDb(path, keydb, STD_BUFFER_SIZE);
// compare with recommended SHA
bool res = (nkeys && (sha_cmp(recommended_sha, keydb, nkeys * sizeof(AesKeyInfo), SHA256_MODE) == 0));
free(keydb);
return res ? 0 : 1;
}

View File

@ -14,10 +14,10 @@
#define KEYS_UNKNOWN 0 #define KEYS_UNKNOWN 0
#define KEYS_DEVKIT 1 #define KEYS_DEVKIT 1
#define KEYS_RETAIL 2 #define KEYS_RETAIL 2
typedef struct { typedef struct {
u8 slot; // keyslot, 0x00...0x3F u8 slot; // keyslot, 0x00...0x3F
char type; // type 'X' / 'Y' / 'N' for normalKey / 'I' for IV char type; // type 'X' / 'Y' / 'N' for normalKey / 'I' for IV
char id[10]; // key ID for special keys, all zero for standard keys char id[10]; // key ID for special keys, all zero for standard keys
u8 reserved[2]; // reserved space u8 reserved[2]; // reserved space
@ -30,4 +30,3 @@ u32 GetUnitKeysType(void);
void CryptAesKeyInfo(AesKeyInfo* info); void CryptAesKeyInfo(AesKeyInfo* info);
u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id); u32 LoadKeyFromFile(void* key, u32 keyslot, char type, char* id);
u32 InitKeyDb(const char* path); u32 InitKeyDb(const char* path);
u32 CheckRecommendedKeyDb(const char* path);

View File

@ -13,9 +13,9 @@ void sha_init(u32 mode)
} }
void sha_update(const void* src, u32 size) void sha_update(const void* src, u32 size)
{ {
const u32* src32 = (const u32*)src; const u32* src32 = (const u32*)src;
while(size >= 0x40) { while(size >= 0x40) {
while(*REG_SHACNT & 1); while(*REG_SHACNT & 1);
*((volatile _sha_block*)REG_SHAINFIFO) = *((const _sha_block*)src32); *((volatile _sha_block*)REG_SHAINFIFO) = *((const _sha_block*)src32);

View File

@ -63,7 +63,7 @@ FATpartition DriveInfo[13] = {
{ TYPE_RAMDRV, SUBTYPE_NONE, 0, 0, 0xFF } // Z - RAMDRIVE { TYPE_RAMDRV, SUBTYPE_NONE, 0, 0, 0xFF } // Z - RAMDRIVE
}; };
static BYTE imgnand_mode = 0x00; static BYTE imgnand_mode = 0x00;
@ -81,7 +81,7 @@ DWORD get_fattime( void ) {
((DSTIMEGET(&dstime, bcd_D)&0x1F) << 16) | ((DSTIMEGET(&dstime, bcd_D)&0x1F) << 16) |
((DSTIMEGET(&dstime, bcd_M)&0x0F) << 21) | ((DSTIMEGET(&dstime, bcd_M)&0x0F) << 21) |
(((DSTIMEGET(&dstime, bcd_Y)+(2000-1980))&0x7F) << 25); (((DSTIMEGET(&dstime, bcd_Y)+(2000-1980))&0x7F) << 25);
return fattime; return fattime;
} }
@ -113,10 +113,10 @@ DSTATUS disk_initialize (
imgnand_mode = (GetMountState() & IMG_NAND) ? 0x01 : 0x00; imgnand_mode = (GetMountState() & IMG_NAND) ? 0x01 : 0x00;
FATpartition* fat_info = PART_INFO(pdrv); FATpartition* fat_info = PART_INFO(pdrv);
BYTE type = PART_TYPE(pdrv); BYTE type = PART_TYPE(pdrv);
fat_info->offset = fat_info->size = 0; fat_info->offset = fat_info->size = 0;
fat_info->keyslot = 0xFF; fat_info->keyslot = 0xFF;
if (type == TYPE_SDCARD) { if (type == TYPE_SDCARD) {
if (sdmmc_sdcard_init() != 0) return STA_NOINIT|STA_NODISK; if (sdmmc_sdcard_init() != 0) return STA_NOINIT|STA_NODISK;
fat_info->size = getMMCDevice(1)->total_size; fat_info->size = getMMCDevice(1)->total_size;
@ -149,7 +149,7 @@ DSTATUS disk_initialize (
InitRamDrive(); InitRamDrive();
fat_info->size = (GetRamDriveSize() + 0x1FF) / 0x200; fat_info->size = (GetRamDriveSize() + 0x1FF) / 0x200;
} }
return RES_OK; return RES_OK;
} }
@ -166,9 +166,9 @@ DRESULT disk_read (
DWORD sector, /* Sector address in LBA */ DWORD sector, /* Sector address in LBA */
UINT count /* Number of sectors to read */ UINT count /* Number of sectors to read */
) )
{ {
BYTE type = PART_TYPE(pdrv); BYTE type = PART_TYPE(pdrv);
if (type == TYPE_NONE) { if (type == TYPE_NONE) {
return RES_PARERR; return RES_PARERR;
} else if (type == TYPE_SDCARD) { } else if (type == TYPE_SDCARD) {
@ -205,7 +205,7 @@ DRESULT disk_write (
) )
{ {
BYTE type = PART_TYPE(pdrv); BYTE type = PART_TYPE(pdrv);
if (type == TYPE_NONE) { if (type == TYPE_NONE) {
return RES_PARERR; return RES_PARERR;
} else if (type == TYPE_SDCARD) { } else if (type == TYPE_SDCARD) {
@ -245,7 +245,7 @@ DRESULT disk_ioctl (
{ {
BYTE type = PART_TYPE(pdrv); BYTE type = PART_TYPE(pdrv);
FATpartition* fat_info = PART_INFO(pdrv); FATpartition* fat_info = PART_INFO(pdrv);
switch (cmd) { switch (cmd) {
case GET_SECTOR_SIZE: case GET_SECTOR_SIZE:
*((DWORD*) buff) = 0x200; *((DWORD*) buff) = 0x200;
@ -262,7 +262,7 @@ DRESULT disk_ioctl (
// nothing else to do here - sdmmc.c handles the rest // nothing else to do here - sdmmc.c handles the rest
return RES_OK; return RES_OK;
} }
return RES_PARERR; return RES_PARERR;
} }
#endif #endif

View File

@ -5,7 +5,7 @@ u32 ValidateMbrHeader(MbrHeader* mbr) {
u32 sector = 1; // check partitions u32 sector = 1; // check partitions
for (u32 i = 0; i < 4; i++) { for (u32 i = 0; i < 4; i++) {
MbrPartitionInfo* partition = mbr->partitions + i; MbrPartitionInfo* partition = mbr->partitions + i;
if (!partition->count && i) continue; if (!partition->count && i) continue;
else if (!partition->count) return 1; // first partition can't be empty else if (!partition->count) return 1; // first partition can't be empty
if ((partition->type != 0x1) && (partition->type != 0x4) && (partition->type != 0x6) && if ((partition->type != 0x1) && (partition->type != 0x4) && (partition->type != 0x6) &&
(partition->type != 0xB) && (partition->type != 0xC) && (partition->type != 0xE)) (partition->type != 0xB) && (partition->type != 0xC) && (partition->type != 0xE))
@ -22,7 +22,7 @@ u32 ValidateFatHeader(void* fat) {
if (strncmp(fat32->fs_type, "FAT32 ", 8) == 0) if (strncmp(fat32->fs_type, "FAT32 ", 8) == 0)
return 0; // is FAT32 header return 0; // is FAT32 header
Fat16Header* fat16 = (Fat16Header*) fat; Fat16Header* fat16 = (Fat16Header*) fat;
if ((strncmp(fat16->fs_type, "FAT16 ", 8) == 0) || if ((strncmp(fat16->fs_type, "FAT16 ", 8) == 0) ||
(strncmp(fat16->fs_type, "FAT12 ", 8) == 0) || (strncmp(fat16->fs_type, "FAT12 ", 8) == 0) ||
(strncmp(fat16->fs_type, "FAT ", 8) == 0)) (strncmp(fat16->fs_type, "FAT ", 8) == 0))
return 0; // is FAT16 / FAT12 header return 0; // is FAT16 / FAT12 header

View File

@ -15,7 +15,7 @@ typedef struct {
typedef struct { typedef struct {
char text[446]; char text[446];
MbrPartitionInfo partitions[4]; MbrPartitionInfo partitions[4];
u16 magic; // 0xAA55 u16 magic; // 0xAA55
} PACKED_ALIGN(2) MbrHeader; } PACKED_ALIGN(2) MbrHeader;
@ -50,7 +50,7 @@ typedef struct {
u32 clr_root; // 0x02 u32 clr_root; // 0x02
u16 sct_fsinfo; // 0x01 u16 sct_fsinfo; // 0x01
u16 sct_backup; // 0x06 u16 sct_backup; // 0x06
u8 reserved3[12]; u8 reserved3[12];
u8 ndrive; // 0x80 u8 ndrive; // 0x80
u8 head_cur; // 0x00 u8 head_cur; // 0x00
u8 boot_sig; // 0x29 u8 boot_sig; // 0x29

View File

@ -8,6 +8,7 @@
#include "keydb.h" #include "keydb.h"
#include "ctrtransfer.h" #include "ctrtransfer.h"
#include "scripting.h" #include "scripting.h"
#include "gm9lua.h"
#include "png.h" #include "png.h"
#include "ui.h" // only for font file detection #include "ui.h" // only for font file detection
@ -86,11 +87,14 @@ u64 IdentifyFileType(const char* path) {
return GAME_ROMFS; // RomFS file (check could be better) return GAME_ROMFS; // RomFS file (check could be better)
} else if (ValidateTmd((TitleMetaData*) data) == 0) { } else if (ValidateTmd((TitleMetaData*) data) == 0) {
if (fsize == TMD_SIZE_N(getbe16(header + 0x1DE)) + TMD_CDNCERT_SIZE) if (fsize == TMD_SIZE_N(getbe16(header + 0x1DE)) + TMD_CDNCERT_SIZE)
return GAME_TMD | FLAG_NUSCDN; // TMD file from NUS/CDN return GAME_CDNTMD; // TMD file from NUS/CDN
else if (fsize >= TMD_SIZE_N(getbe16(header + 0x1DE))) else if (fsize >= TMD_SIZE_N(getbe16(header + 0x1DE)))
return GAME_TMD; // TMD file return GAME_TMD; // TMD file
} else if (ValidateTwlTmd((TitleMetaData*) data) == 0) {
if (fsize == TMD_SIZE_TWL + TMD_CDNCERT_SIZE)
return GAME_TWLTMD; // TMD file from NUS/CDN (TWL)
} else if (ValidateTicket((Ticket*) data) == 0) { } else if (ValidateTicket((Ticket*) data) == 0) {
return GAME_TICKET; // Ticket file (not used for anything right now) return GAME_TICKET; // Ticket file
} else if (ValidateFirmHeader((FirmHeader*) data, fsize) == 0) { } else if (ValidateFirmHeader((FirmHeader*) data, fsize) == 0) {
return SYS_FIRM; // FIRM file return SYS_FIRM; // FIRM file
} else if ((ValidateAgbSaveHeader((AgbSaveHeader*) data) == 0) && (fsize >= AGBSAVE_MAX_SIZE)) { } else if ((ValidateAgbSaveHeader((AgbSaveHeader*) data) == 0) && (fsize >= AGBSAVE_MAX_SIZE)) {
@ -112,7 +116,7 @@ u64 IdentifyFileType(const char* path) {
} }
} }
} }
if (fsize == sizeof(TitleInfoEntry) && (strncasecmp(path, "T:/", 3) == 0)) { if (fsize == sizeof(TitleInfoEntry) && (strncasecmp(path, "T:/", 3) == 0)) {
const char* mntpath = GetMountPath(); const char* mntpath = GetMountPath();
if (mntpath && *mntpath) { if (mntpath && *mntpath) {
@ -124,6 +128,10 @@ u64 IdentifyFileType(const char* path) {
} }
} else if (GetFontFromPbm(data, fsize, NULL, NULL)) { } else if (GetFontFromPbm(data, fsize, NULL, NULL)) {
return FONT_PBM; return FONT_PBM;
} else if (GetFontFromRiff(data, fsize, NULL, NULL, NULL)) {
return FONT_RIFF;
} else if (GetLanguage(data, fsize, NULL, NULL, NULL)) {
return TRANSLATION;
} else if ((fsize > sizeof(AgbHeader)) && } else if ((fsize > sizeof(AgbHeader)) &&
(ValidateAgbHeader((AgbHeader*) data) == 0)) { (ValidateAgbHeader((AgbHeader*) data) == 0)) {
return GAME_GBA; return GAME_GBA;
@ -143,14 +151,6 @@ u64 IdentifyFileType(const char* path) {
} else if ((strncasecmp(ext, "png", 4) == 0) && } else if ((strncasecmp(ext, "png", 4) == 0) &&
(fsize > sizeof(png_magic)) && (memcmp(data, png_magic, sizeof(png_magic)) == 0)) { (fsize > sizeof(png_magic)) && (memcmp(data, png_magic, sizeof(png_magic)) == 0)) {
return GFX_PNG; return GFX_PNG;
} else if ((strncasecmp(ext, "cdn", 4) == 0) || (strncasecmp(ext, "nus", 4) == 0)) {
char path_cetk[256];
char* ext_cetk = path_cetk + (ext - path);
strncpy(path_cetk, path, 256);
path_cetk[255] = '\0';
strncpy(ext_cetk, "cetk", 5);
if (FileGetSize(path_cetk) > 0)
return GAME_NUSCDN; // NUS/CDN type 2
} else if (strncasecmp(fname, TIKDB_NAME_ENC, sizeof(TIKDB_NAME_ENC)+1) == 0) { } else if (strncasecmp(fname, TIKDB_NAME_ENC, sizeof(TIKDB_NAME_ENC)+1) == 0) {
return BIN_TIKDB | FLAG_ENC; // titlekey database / encrypted return BIN_TIKDB | FLAG_ENC; // titlekey database / encrypted
} else if (strncasecmp(fname, TIKDB_NAME_DEC, sizeof(TIKDB_NAME_DEC)+1) == 0) { } else if (strncasecmp(fname, TIKDB_NAME_DEC, sizeof(TIKDB_NAME_DEC)+1) == 0) {
@ -159,10 +159,16 @@ u64 IdentifyFileType(const char* path) {
return BIN_KEYDB; // key database return BIN_KEYDB; // key database
} else if ((sscanf(fname, "slot%02lXKey", &id) == 1) && (strncasecmp(ext, "bin", 4) == 0) && (fsize = 16) && (id < 0x40)) { } else if ((sscanf(fname, "slot%02lXKey", &id) == 1) && (strncasecmp(ext, "bin", 4) == 0) && (fsize = 16) && (id < 0x40)) {
return BIN_LEGKEY; // legacy key file return BIN_LEGKEY; // legacy key file
} else if ((strncmp((char*) data, CIFINISH_MAGIC, strlen(CIFINISH_MAGIC)) == 0) &&
(fsize == CIFINISH_SIZE((void*) data)) && (fsize > sizeof(CifinishHeader))) {
return BIN_CIFNSH;
} else if (ValidateText((char*) data, (fsize > 0x200) ? 0x200 : fsize)) { } else if (ValidateText((char*) data, (fsize > 0x200) ? 0x200 : fsize)) {
u64 type = 0; u64 type = 0;
if ((fsize < SCRIPT_MAX_SIZE) && (strncasecmp(ext, SCRIPT_EXT, strnlen(SCRIPT_EXT, 16) + 1) == 0)) if ((fsize < SCRIPT_MAX_SIZE) && (strcasecmp(ext, SCRIPT_EXT) == 0))
type |= TXT_SCRIPT; // should be a script (which is also generic text) type |= TXT_SCRIPT; // should be a script (which is also generic text)
// this should check if it's compiled lua bytecode (done with luac), which is NOT text
else if ((fsize < LUASCRIPT_MAX_SIZE) && (strcasecmp(ext, LUASCRIPT_EXT) == 0))
type |= TXT_LUA;
if (fsize < STD_BUFFER_SIZE) type |= TXT_GENERIC; if (fsize < STD_BUFFER_SIZE) type |= TXT_GENERIC;
return type; return type;
} else if ((strncmp(path + 2, "/Nintendo DSiWare/", 18) == 0) && } else if ((strncmp(path + 2, "/Nintendo DSiWare/", 18) == 0) &&
@ -176,13 +182,13 @@ u64 IdentifyFileType(const char* path) {
char* name_cdn = path_cdn + (fname - path); char* name_cdn = path_cdn + (fname - path);
strncpy(path_cdn, path, 256); strncpy(path_cdn, path, 256);
path_cdn[255] = '\0'; path_cdn[255] = '\0';
strncpy(name_cdn, "tmd", 4); strncpy(name_cdn, "tmd", 4); // this will not catch tmd with version
if (FileGetSize(path_cdn) > 0) if (FileGetSize(path_cdn) > 0)
return GAME_NUSCDN; // NUS/CDN type 1 return GAME_NUSCDN; // NUS/CDN, recognized by TMD
strncpy(name_cdn, "cetk", 5); strncpy(name_cdn, "cetk", 5);
if (FileGetSize(path_cdn) > 0) if (FileGetSize(path_cdn) > 0)
return GAME_NUSCDN; // NUS/CDN type 1 return GAME_NUSCDN; // NUS/CDN, recognized by CETK
} }
return 0; return 0;
} }

View File

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

View File

@ -1,6 +1,7 @@
#include "fsdrive.h" #include "fsdrive.h"
#include "fsgame.h" #include "fsgame.h"
#include "fsinit.h" #include "fsinit.h"
#include "language.h"
#include "virtual.h" #include "virtual.h"
#include "vcart.h" #include "vcart.h"
#include "sddata.h" #include "sddata.h"
@ -11,16 +12,18 @@
// last search pattern, path & mode // last search pattern, path & mode
static char search_pattern[256] = { 0 }; static char search_pattern[256] = { 0 };
static char search_path[256] = { 0 }; static char search_path[256] = { 0 };
static bool search_title_mode = false; static bool title_manager_mode = false;
int DriveType(const char* path) { int DriveType(const char* path) {
int type = DRV_UNKNOWN; int type = DRV_UNKNOWN;
int pdrv = GetMountedFSNum(path); int pdrv = GetMountedFSNum(path);
if (CheckAliasDrive(path)) { if (CheckAliasDrive(path)) {
type = DRV_FAT | DRV_ALIAS | ((*path == 'A') ? DRV_SYSNAND : DRV_EMUNAND); type = DRV_FAT | DRV_ALIAS | ((*path == 'A') ? DRV_SYSNAND : DRV_EMUNAND);
} else if (*search_pattern && *search_path && (strncmp(path, "Z:", 3) == 0)) { } else if (*search_pattern && *search_path && (strncmp(path, "Z:", 3) == 0)) {
type = DRV_SEARCH; type = DRV_SEARCH;
} else if (title_manager_mode && (strncmp(path, "Y:", 3) == 0)) {
type = DRV_VIRTUAL | DRV_TITLEMAN;
} else if ((pdrv >= 0) && (pdrv < NORM_FS)) { } else if ((pdrv >= 0) && (pdrv < NORM_FS)) {
if (pdrv == 0) { if (pdrv == 0) {
type = DRV_FAT | DRV_SDCARD | DRV_STDFAT; type = DRV_FAT | DRV_SDCARD | DRV_STDFAT;
@ -39,7 +42,7 @@ int DriveType(const char* path) {
} else if ((pdrv >= 7) && (pdrv <= 9) && } else if ((pdrv >= 7) && (pdrv <= 9) &&
(GetMountState() & (IMG_FAT|IMG_NAND))) { (GetMountState() & (IMG_FAT|IMG_NAND))) {
type = DRV_FAT | DRV_IMAGE | DRV_STDFAT; type = DRV_FAT | DRV_IMAGE | DRV_STDFAT;
} }
} else if (CheckVirtualDrive(path)) { } else if (CheckVirtualDrive(path)) {
int vsrc = GetVirtualSource(path); int vsrc = GetVirtualSource(path);
if (vsrc == VRT_SYSNAND) { if (vsrc == VRT_SYSNAND) {
@ -58,53 +61,56 @@ int DriveType(const char* path) {
type = DRV_VIRTUAL | DRV_CART; type = DRV_VIRTUAL | DRV_CART;
} else if (vsrc == VRT_VRAM) { } else if (vsrc == VRT_VRAM) {
type = DRV_VIRTUAL | DRV_VRAM; type = DRV_VIRTUAL | DRV_VRAM;
} }
} }
return type; return type;
} }
void SetFSSearch(const char* pattern, const char* path, bool mode) { void SetFSSearch(const char* pattern, const char* path) {
if (pattern && path) { if (pattern && path) {
strncpy(search_pattern, pattern, 256); strncpy(search_pattern, pattern, 256);
search_pattern[255] = '\0'; search_pattern[255] = '\0';
strncpy(search_path, path, 256); strncpy(search_path, path, 256);
search_path[255] = '\0'; search_path[255] = '\0';
search_title_mode = mode;
} else *search_pattern = *search_path = '\0'; } else *search_pattern = *search_path = '\0';
} }
void SetTitleManagerMode(bool mode) {
title_manager_mode = mode;
}
bool GetFATVolumeLabel(const char* drv, char* label) { bool GetFATVolumeLabel(const char* drv, char* label) {
return (f_getlabel(drv, label, NULL) == FR_OK); return (f_getlabel(drv, label, NULL) == FR_OK);
} }
bool GetRootDirContentsWorker(DirStruct* contents) { bool GetRootDirContentsWorker(DirStruct* contents) {
static const char* drvname[] = { FS_DRVNAME }; const char* drvname[] = { FS_DRVNAME };
static const char* drvnum[] = { FS_DRVNUM }; static const char* drvnum[] = { FS_DRVNUM };
u32 n_entries = 0; u32 n_entries = 0;
char sdlabel[DRV_LABEL_LEN]; char sdlabel[DRV_LABEL_LEN];
if (!GetFATVolumeLabel("0:", sdlabel) || !(*sdlabel)) if (!GetFATVolumeLabel("0:", sdlabel) || !(*sdlabel))
strcpy(sdlabel, "NOLABEL"); strcpy(sdlabel, STR_LAB_NOLABEL);
char carttype[16]; char carttype[16];
GetVCartTypeString(carttype); GetVCartTypeString(carttype);
// virtual root objects hacked in // virtual root objects hacked in
for (u32 i = 0; (i < countof(drvnum)) && (n_entries < MAX_DIR_ENTRIES); i++) { for (u32 i = 0; (i < countof(drvnum)) && (n_entries < MAX_DIR_ENTRIES); i++) {
DirEntry* entry = &(contents->entry[n_entries]); DirEntry* entry = &(contents->entry[n_entries]);
if (!DriveType(drvnum[i])) continue; // drive not available if (!DriveType(drvnum[i])) continue; // drive not available
entry->p_name = 4; entry->p_name = 4;
entry->name = entry->path + entry->p_name; entry->name = entry->path + entry->p_name;
memset(entry->path, 0x00, 64); memset(entry->path, 0x00, 256);
snprintf(entry->path, 4, "%s", drvnum[i]); snprintf(entry->path, 4, "%s", drvnum[i]);
if ((*(drvnum[i]) >= '7') && (*(drvnum[i]) <= '9') && !(GetMountState() & IMG_NAND)) // Drive 7...9 handling if ((*(drvnum[i]) >= '7') && (*(drvnum[i]) <= '9') && !(GetMountState() & IMG_NAND)) // Drive 7...9 handling
snprintf(entry->name, 32, "[%s] %s", drvnum[i], snprintf(entry->name, 252, "[%s] %s", drvnum[i],
(*(drvnum[i]) == '7') ? "FAT IMAGE" : (*(drvnum[i]) == '7') ? STR_LAB_FAT_IMAGE :
(*(drvnum[i]) == '8') ? "BONUS DRIVE" : (*(drvnum[i]) == '8') ? STR_LAB_BONUS_DRIVE :
(*(drvnum[i]) == '9') ? "RAMDRIVE" : "UNK"); (*(drvnum[i]) == '9') ? STR_LAB_RAMDRIVE : "UNK");
else if (*(drvnum[i]) == 'G') // Game drive special handling else if (*(drvnum[i]) == 'G') // Game drive special handling
snprintf(entry->name, 32, "[%s] %s %s", drvnum[i], snprintf(entry->name, 252, "[%s] %s %s", drvnum[i],
(GetMountState() & GAME_CIA ) ? "CIA" : (GetMountState() & GAME_CIA ) ? "CIA" :
(GetMountState() & GAME_NCSD ) ? "NCSD" : (GetMountState() & GAME_NCSD ) ? "NCSD" :
(GetMountState() & GAME_NCCH ) ? "NCCH" : (GetMountState() & GAME_NCCH ) ? "NCCH" :
@ -114,17 +120,17 @@ bool GetRootDirContentsWorker(DirStruct* contents) {
(GetMountState() & SYS_FIRM ) ? "FIRM" : (GetMountState() & SYS_FIRM ) ? "FIRM" :
(GetMountState() & GAME_TAD ) ? "DSIWARE" : "UNK", drvname[i]); (GetMountState() & GAME_TAD ) ? "DSIWARE" : "UNK", drvname[i]);
else if (*(drvnum[i]) == 'C') // Game cart handling else if (*(drvnum[i]) == 'C') // Game cart handling
snprintf(entry->name, 32, "[%s] %s (%s)", drvnum[i], drvname[i], carttype); snprintf(entry->name, 252, "[%s] %s (%s)", drvnum[i], drvname[i], carttype);
else if (*(drvnum[i]) == '0') // SD card handling else if (*(drvnum[i]) == '0') // SD card handling
snprintf(entry->name, 32, "[%s] %s (%s)", drvnum[i], drvname[i], sdlabel); snprintf(entry->name, 252, "[%s] %s (%s)", drvnum[i], drvname[i], sdlabel);
else snprintf(entry->name, 32, "[%s] %s", drvnum[i], drvname[i]); else snprintf(entry->name, 252, "[%s] %s", drvnum[i], drvname[i]);
entry->size = GetTotalSpace(entry->path); entry->size = GetTotalSpace(entry->path);
entry->type = T_ROOT; entry->type = T_ROOT;
entry->marked = 0; entry->marked = 0;
n_entries++; n_entries++;
} }
contents->n_entries = n_entries; contents->n_entries = n_entries;
return contents->n_entries; return contents->n_entries;
} }
@ -133,11 +139,11 @@ bool GetDirContentsWorker(DirStruct* contents, char* fpath, int fnsize, const ch
FILINFO fno; FILINFO fno;
char* fname = fpath + strnlen(fpath, fnsize - 1); char* fname = fpath + strnlen(fpath, fnsize - 1);
bool ret = false; bool ret = false;
if (fvx_opendir(&pdir, fpath) != FR_OK) if (fvx_opendir(&pdir, fpath) != FR_OK)
return false; return false;
if (*(fname-1) != '/') *(fname++) = '/'; if (*(fname-1) != '/') *(fname++) = '/';
while (fvx_readdir(&pdir, &fno) == FR_OK) { while (fvx_readdir(&pdir, &fno) == FR_OK) {
if ((strncmp(fno.fname, ".", 2) == 0) || (strncmp(fno.fname, "..", 3) == 0)) if ((strncmp(fno.fname, ".", 2) == 0) || (strncmp(fno.fname, "..", 3) == 0))
continue; // filter out virtual entries continue; // filter out virtual entries
@ -176,7 +182,7 @@ bool GetDirContentsWorker(DirStruct* contents, char* fpath, int fnsize, const ch
} }
} }
fvx_closedir(&pdir); fvx_closedir(&pdir);
return ret; return ret;
} }
@ -206,9 +212,12 @@ void SearchDirContents(DirStruct* contents, const char* path, const char* patter
void GetDirContents(DirStruct* contents, const char* path) { void GetDirContents(DirStruct* contents, const char* path) {
if (*search_path && (DriveType(path) & DRV_SEARCH)) { if (*search_path && (DriveType(path) & DRV_SEARCH)) {
ShowString("Searching, please wait..."); ShowString("%s", STR_SEARCHING_PLEASE_WAIT);
SearchDirContents(contents, search_path, search_pattern, true); SearchDirContents(contents, search_path, search_pattern, true);
if (search_title_mode) SetDirGoodNames(contents); ClearScreenF(true, false, COLOR_STD_BG);
} else if (title_manager_mode && (DriveType(path) & DRV_TITLEMAN)) {
SearchDirContents(contents, "T:", "*", false);
SetupTitleManager(contents);
ClearScreenF(true, false, COLOR_STD_BG); ClearScreenF(true, false, COLOR_STD_BG);
} else SearchDirContents(contents, path, NULL, false); } else SearchDirContents(contents, path, NULL, false);
if (*path) SortDirStruct(contents); if (*path) SortDirStruct(contents);
@ -222,8 +231,8 @@ uint64_t GetFreeSpace(const char* path)
int pdrv = GetMountedFSNum(path); int pdrv = GetMountedFSNum(path);
FATFS* fsobj = GetMountedFSObject(path); FATFS* fsobj = GetMountedFSObject(path);
if ((pdrv < 0) || !fsobj) return 0; if ((pdrv < 0) || !fsobj) return 0;
snprintf(fsname, 3, "%i:", pdrv); snprintf(fsname, sizeof(fsname), "%i:", pdrv);
if (f_getfree(fsname, &free_clusters, &fsptr) != FR_OK) if (f_getfree(fsname, &free_clusters, &fsptr) != FR_OK)
return 0; return 0;

View File

@ -25,30 +25,36 @@
#define DRV_VRAM (1UL<<13) #define DRV_VRAM (1UL<<13)
#define DRV_ALIAS (1UL<<14) #define DRV_ALIAS (1UL<<14)
#define DRV_BONUS (1UL<<15) #define DRV_BONUS (1UL<<15)
#define DRV_SEARCH (1UL<<16) #define DRV_TITLEMAN (1UL<<16)
#define DRV_STDFAT (1UL<<17) // standard FAT drive without limitations #define DRV_SEARCH (1UL<<17)
#define DRV_STDFAT (1UL<<18) // standard FAT drive without limitations
#define DRV_LABEL_LEN (36) #define DRV_LABEL_LEN (36)
#define FS_DRVNAME \ #define FS_DRVNAME \
"SDCARD", \ STR_LAB_SDCARD, \
"SYSNAND CTRNAND", "SYSNAND TWLN", "SYSNAND TWLP", "SYSNAND SD", "SYSNAND VIRTUAL", \ STR_LAB_SYSNAND_CTRNAND, STR_LAB_SYSNAND_TWLN, STR_LAB_SYSNAND_TWLP, STR_LAB_SYSNAND_SD, STR_LAB_SYSNAND_VIRTUAL, \
"EMUNAND CTRNAND", "EMUNAND TWLN", "EMUNAND TWLP", "EMUNAND SD", "EMUNAND VIRTUAL", \ STR_LAB_EMUNAND_CTRNAND, STR_LAB_EMUNAND_TWLN, STR_LAB_EMUNAND_TWLP, STR_LAB_EMUNAND_SD, STR_LAB_EMUNAND_VIRTUAL, \
"IMGNAND CTRNAND", "IMGNAND TWLN", "IMGNAND TWLP", "IMGNAND VIRTUAL", \ STR_LAB_IMGNAND_CTRNAND, STR_LAB_IMGNAND_TWLN, STR_LAB_IMGNAND_TWLP, STR_LAB_IMGNAND_VIRTUAL, \
"GAMECART", \ STR_LAB_GAMECART, \
"GAME IMAGE", "AESKEYDB IMAGE", "BDRI IMAGE", "DISA/DIFF IMAGE", \ STR_LAB_GAME_IMAGE, STR_LAB_AESKEYDB_IMAGE, STR_LAB_BDRI_IMAGE, STR_LAB_DISA_DIFF_IMAGE, \
"MEMORY VIRTUAL", \ STR_LAB_MEMORY_VIRTUAL, \
"VRAM VIRTUAL", \ STR_LAB_VRAM_VIRTUAL, \
"LAST SEARCH" \ STR_LAB_TITLE_MANAGER, \
STR_LAB_LAST_SEARCH
#define FS_DRVNUM \ #define FS_DRVNUM \
"0:", "1:", "2:", "3:", "A:", "S:", "4:", "5:", "6:", "B:", "E:", "7:", "8:", "9:", "I:", "C:", "G:", "K:", "T:", "D:", "M:", "V:", "Z:" "0:", "1:", "2:", "3:", "A:", "S:", "4:", "5:", "6:", "B:", "E:", "7:", "8:", "9:", \
"I:", "C:", "G:", "K:", "T:", "D:", "M:", "V:", "Y:", "Z:"
/** Function to identify the type of a drive **/ /** Function to identify the type of a drive **/
int DriveType(const char* path); int DriveType(const char* path);
/** Set search pattern / path / mode for special Z: drive **/ /** Set search pattern / path / mode for special Z: drive **/
void SetFSSearch(const char* pattern, const char* path, bool mode); void SetFSSearch(const char* pattern, const char* path);
/** Enable title manager for special processing of mounted title.db **/
void SetTitleManagerMode(bool mode);
/** Read the FAT volume label of a partition **/ /** Read the FAT volume label of a partition **/
bool GetFATVolumeLabel(const char* drv, char* label); bool GetFATVolumeLabel(const char* drv, char* label);

View File

@ -1,14 +1,17 @@
#include "fsgame.h" #include "fsgame.h"
#include "fsperm.h" #include "fsperm.h"
#include "gameutil.h" #include "gameutil.h"
#include "language.h"
#include "tie.h"
#include "ui.h" #include "ui.h"
#include "ff.h" #include "vff.h"
void SetDirGoodNames(DirStruct* contents) { void SetupTitleManager(DirStruct* contents) {
char goodname[256]; char goodname[256];
ShowProgress(0, 0, ""); ShowProgress(0, 0, "");
for (u32 s = 0; s < contents->n_entries; s++) { for (u32 s = 0; s < contents->n_entries; s++) {
DirEntry* entry = &(contents->entry[s]); DirEntry* entry = &(contents->entry[s]);
// set good name for entry
u32 plen = strnlen(entry->path, 256); u32 plen = strnlen(entry->path, 256);
if (!ShowProgress(s+1, contents->n_entries, entry->path)) break; if (!ShowProgress(s+1, contents->n_entries, entry->path)) break;
if ((GetGoodName(goodname, entry->path, false) != 0) || if ((GetGoodName(goodname, entry->path, false) != 0) ||
@ -17,6 +20,11 @@ void SetDirGoodNames(DirStruct* contents) {
entry->p_name = plen + 1; entry->p_name = plen + 1;
entry->name = entry->path + entry->p_name; entry->name = entry->path + entry->p_name;
snprintf(entry->name, 256 - entry->p_name, "%s", goodname); snprintf(entry->name, 256 - entry->p_name, "%s", goodname);
// grab title size from tie
TitleInfoEntry tie;
if (fvx_qread(entry->path, &tie, 0, sizeof(TitleInfoEntry), NULL) != FR_OK)
continue;
entry->size = tie.title_size;
} }
} }
@ -25,17 +33,17 @@ bool GoodRenamer(DirEntry* entry, bool ask) {
if ((GetGoodName(goodname, entry->path, false) != 0) || if ((GetGoodName(goodname, entry->path, false) != 0) ||
(strncmp(goodname + strnlen(goodname, 256) - 4, ".tmd", 4) == 0)) // no TMD, please (strncmp(goodname + strnlen(goodname, 256) - 4, ".tmd", 4) == 0)) // no TMD, please
return false; return false;
if (ask) { // ask for confirmatiom if (ask) { // ask for confirmatiom
char oldname_tr[32+1]; char oldname_tr[UTF_BUFFER_BYTESIZE(32)];
char newname_ww[256]; char newname_ww[256];
TruncateString(oldname_tr, entry->name, 32, 8); TruncateString(oldname_tr, entry->name, 32, 8);
strncpy(newname_ww, goodname, 256); strncpy(newname_ww, goodname, 256);
WordWrapString(newname_ww, 32); WordWrapString(newname_ww, 32);
if (!ShowPrompt(true, "%s\nRename to good name?\n \n%s", oldname_tr, newname_ww)) if (!ShowPrompt(true, "%s\n%s\n \n%s", oldname_tr, STR_RENAME_TO_GOOD_NAME, newname_ww))
return true; // call it a success because user choice return true; // call it a success because user choice
} }
char npath[256]; // get new path char npath[256]; // get new path
strncpy(npath, entry->path, 256); strncpy(npath, entry->path, 256);
char* nname = strrchr(npath, '/'); char* nname = strrchr(npath, '/');
@ -47,6 +55,6 @@ bool GoodRenamer(DirEntry* entry, bool ask) {
if (f_rename(entry->path, npath) != FR_OK) return false; if (f_rename(entry->path, npath) != FR_OK) return false;
strncpy(entry->path, npath, 256); strncpy(entry->path, npath, 256);
entry->name = entry->path + (nname - npath); entry->name = entry->path + (nname - npath);
return true; return true;
} }

View File

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

View File

@ -18,10 +18,10 @@ bool InitSDCardFS() {
bool InitExtFS() { bool InitExtFS() {
static bool ramdrv_ready = false; static bool ramdrv_ready = false;
for (u32 i = 1; i < NORM_FS; i++) { for (u32 i = 1; i < NORM_FS; i++) {
char fsname[8]; char fsname[8];
snprintf(fsname, 7, "%lu:", i); snprintf(fsname, sizeof(fsname), "%lu:", i);
if (fs_mounted[i]) continue; if (fs_mounted[i]) continue;
fs_mounted[i] = (f_mount(fs + i, fsname, 1) == FR_OK); fs_mounted[i] = (f_mount(fs + i, fsname, 1) == FR_OK);
if ((!fs_mounted[i] || !ramdrv_ready) && (i == NORM_FS - 1) && !(GetMountState() & IMG_NAND)) { if ((!fs_mounted[i] || !ramdrv_ready) && (i == NORM_FS - 1) && !(GetMountState() & IMG_NAND)) {
@ -44,7 +44,7 @@ bool InitImgFS(const char* path) {
u32 drv_i = NORM_FS - IMGN_FS; u32 drv_i = NORM_FS - IMGN_FS;
char fsname[8]; char fsname[8];
for (; drv_i < NORM_FS; drv_i++) { for (; drv_i < NORM_FS; drv_i++) {
snprintf(fsname, 7, "%lu:", drv_i); snprintf(fsname, sizeof(fsname), "%lu:", drv_i);
if (!(DriveType(fsname)&DRV_IMAGE)) break; if (!(DriveType(fsname)&DRV_IMAGE)) break;
} }
// deinit virtual filesystem // deinit virtual filesystem
@ -58,7 +58,7 @@ bool InitImgFS(const char* path) {
else if ((type&IMG_FAT) && (drv_i < NORM_FS - IMGN_FS + 1)) drv_i = NORM_FS - IMGN_FS + 1; else if ((type&IMG_FAT) && (drv_i < NORM_FS - IMGN_FS + 1)) drv_i = NORM_FS - IMGN_FS + 1;
// reinit image filesystem // reinit image filesystem
for (u32 i = NORM_FS - IMGN_FS; i < drv_i; i++) { for (u32 i = NORM_FS - IMGN_FS; i < drv_i; i++) {
snprintf(fsname, 7, "%lu:", i); snprintf(fsname, sizeof(fsname), "%lu:", i);
fs_mounted[i] = (f_mount(fs + i, fsname, 1) == FR_OK); fs_mounted[i] = (f_mount(fs + i, fsname, 1) == FR_OK);
} }
return GetMountState(); return GetMountState();
@ -71,7 +71,7 @@ void DeinitExtFS() {
for (u32 i = NORM_FS - 1; i > 0; i--) { for (u32 i = NORM_FS - 1; i > 0; i--) {
if (fs_mounted[i]) { if (fs_mounted[i]) {
char fsname[8]; char fsname[8];
snprintf(fsname, 7, "%lu:", i); snprintf(fsname, sizeof(fsname), "%lu:", i);
f_mount(NULL, fsname, 1); f_mount(NULL, fsname, 1);
fs_mounted[i] = false; fs_mounted[i] = false;
} }
@ -91,7 +91,7 @@ void DismountDriveType(u32 type) { // careful with this - no safety checks
} }
for (u32 i = 0; i < NORM_FS; i++) { for (u32 i = 0; i < NORM_FS; i++) {
char fsname[8]; char fsname[8];
snprintf(fsname, 7, "%lu:", i); snprintf(fsname, sizeof(fsname), "%lu:", i);
if (!fs_mounted[i] || !(type & DriveType(fsname))) if (!fs_mounted[i] || !(type & DriveType(fsname)))
continue; continue;
f_mount(NULL, fsname, 1); f_mount(NULL, fsname, 1);

View File

@ -6,7 +6,7 @@
// init SD card filesystem - required(?) for everything else // init SD card filesystem - required(?) for everything else
bool InitSDCardFS(); bool InitSDCardFS();
// init fill external fileystem // init fill external fileystem
bool InitExtFS(); bool InitExtFS();
// mount and init image file system // mount and init image file system
@ -21,7 +21,7 @@ void DeinitSDCardFS();
// dismount drives of a certain type // dismount drives of a certain type
void DismountDriveType(u32 type); void DismountDriveType(u32 type);
// returns the mount state of the SD card // returns the mount state of the SD card
bool CheckSDMountState(void); bool CheckSDMountState(void);
// get number of mounted file system (only for FATFS filesystems) // get number of mounted file system (only for FATFS filesystems)

View File

@ -4,10 +4,11 @@
#include "image.h" #include "image.h"
#include "unittype.h" #include "unittype.h"
#include "essentials.h" #include "essentials.h"
#include "language.h"
#include "ui.h" #include "ui.h"
#include "sdmmc.h" #include "sdmmc.h"
#define PATH_SYS_LVL1 "S:/twln.bin", "S:/twlp.bin" #define PATH_SYS_LVL1 "S:/twln.bin", "S:/twlp.bin"
#define PATH_SYS_LVL2 "1:/rw/sys/LocalFriendCodeSeed_B", "1:/rw/sys/LocalFriendCodeSeed_A", \ #define PATH_SYS_LVL2 "1:/rw/sys/LocalFriendCodeSeed_B", "1:/rw/sys/LocalFriendCodeSeed_A", \
"1:/rw/sys/SecureInfo_A", "1:/rw/sys/SecureInfo_B", \ "1:/rw/sys/SecureInfo_A", "1:/rw/sys/SecureInfo_B", \
"1:/private/movable.sed", "1:/ro/sys/HWCAL0.dat", "1:/ro/sys/HWCAL1.dat", \ "1:/private/movable.sed", "1:/ro/sys/HWCAL0.dat", "1:/ro/sys/HWCAL1.dat", \
@ -20,10 +21,10 @@
static u32 write_permissions = PERM_BASE; static u32 write_permissions = PERM_BASE;
bool CheckWritePermissions(const char* path) { bool CheckWritePermissions(const char* path) {
char area_name[16]; char area_name[UTF_BUFFER_BYTESIZE(16)];
int drvtype = DriveType(path); int drvtype = DriveType(path);
u32 perm; u32 perm;
// create a standardized path string // create a standardized path string
char path_f[256]; char path_f[256];
char* p = (char*) path; char* p = (char*) path;
@ -37,13 +38,13 @@ bool CheckWritePermissions(const char* path) {
// check mounted image write permissions // check mounted image write permissions
if ((drvtype & DRV_IMAGE) && !CheckWritePermissions(GetMountPath())) if ((drvtype & DRV_IMAGE) && !CheckWritePermissions(GetMountPath()))
return false; // endless loop when mounted file inside image, but not possible return false; // endless loop when mounted file inside image, but not possible
// SD card write protection check // SD card write protection check
if ((drvtype & (DRV_SDCARD | DRV_EMUNAND | DRV_ALIAS)) && SD_WRITE_PROTECTED) { if ((drvtype & (DRV_SDCARD | DRV_EMUNAND | DRV_ALIAS)) && SD_WRITE_PROTECTED) {
ShowPrompt(false, "SD card is write protected!\nCan't continue."); ShowPrompt(false, "%s", STR_SD_WRITE_PROTECTED_CANT_CONTINUE);
return false; return false;
} }
// check drive type, get permission type // check drive type, get permission type
if (drvtype & DRV_SYSNAND) { if (drvtype & DRV_SYSNAND) {
static const 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 };
@ -63,7 +64,7 @@ bool CheckWritePermissions(const char* path) {
if ((drvtype & DRV_CTRNAND) || (lvl == 2)) lvl = 3; if ((drvtype & DRV_CTRNAND) || (lvl == 2)) lvl = 3;
} }
perm = perms[lvl]; perm = perms[lvl];
snprintf(area_name, 16, "SysNAND (lvl%lu)", lvl); snprintf(area_name, sizeof(area_name), STR_SYSNAND_LVL_N, lvl);
} else if (drvtype & DRV_EMUNAND) { } else if (drvtype & DRV_EMUNAND) {
static const u32 perms[] = { PERM_EMU_LVL0, PERM_EMU_LVL1 }; static const u32 perms[] = { PERM_EMU_LVL0, PERM_EMU_LVL1 };
u32 lvl = (drvtype & (DRV_ALIAS|DRV_CTRNAND)) ? 1 : 0; u32 lvl = (drvtype & (DRV_ALIAS|DRV_CTRNAND)) ? 1 : 0;
@ -73,53 +74,53 @@ bool CheckWritePermissions(const char* path) {
if (strncasecmp(path_f, path_lvl1[i], 256) == 0) lvl = 1; if (strncasecmp(path_f, path_lvl1[i], 256) == 0) lvl = 1;
} }
perm = perms[lvl]; perm = perms[lvl];
snprintf(area_name, 16, "EmuNAND (lvl%lu)", lvl); snprintf(area_name, sizeof(area_name), STR_EMUNAND_LVL_N, lvl);
} else if (drvtype & DRV_GAME) { } else if (drvtype & DRV_GAME) {
perm = PERM_GAME; perm = PERM_GAME;
snprintf(area_name, 16, "game images"); snprintf(area_name, sizeof(area_name), "%s", STR_GAME_IMAGES);
} else if (drvtype & DRV_CART) { } else if (drvtype & DRV_CART) {
perm = PERM_CART; perm = PERM_CART;
snprintf(area_name, 16, "gamecart saves"); snprintf(area_name, sizeof(area_name), "%s", STR_GAMECART_SAVES);
} else if (drvtype & DRV_VRAM) { } else if (drvtype & DRV_VRAM) {
perm = PERM_VRAM; perm = PERM_VRAM;
snprintf(area_name, 16, "vram0"); snprintf(area_name, sizeof(area_name), "vram0");
} else if (drvtype & DRV_XORPAD) { } else if (drvtype & DRV_XORPAD) {
perm = PERM_XORPAD; perm = PERM_XORPAD;
snprintf(area_name, 16, "XORpads"); snprintf(area_name, sizeof(area_name), "XORpads");
} else if (drvtype & DRV_IMAGE) { } else if (drvtype & DRV_IMAGE) {
perm = PERM_IMAGE; perm = PERM_IMAGE;
snprintf(area_name, 16, "images"); snprintf(area_name, sizeof(area_name), "%s", STR_IMAGES);
} else if (drvtype & DRV_MEMORY) { } else if (drvtype & DRV_MEMORY) {
perm = PERM_MEMORY; perm = PERM_MEMORY;
snprintf(area_name, 16, "memory areas"); snprintf(area_name, sizeof(area_name), "%s", STR_MEMORY_AREAS);
} else if (strncasecmp(path_f, "0:/Nintendo 3DS", 15) == 0) { // this check could be better } else if (strncasecmp(path_f, "0:/Nintendo 3DS", 15) == 0) { // this check could be better
perm = PERM_SDDATA; perm = PERM_SDDATA;
snprintf(area_name, 16, "SD system data"); snprintf(area_name, sizeof(area_name), "%s", STR_SD_SYSTEM_DATA);
} else if (drvtype & DRV_SDCARD) { } else if (drvtype & DRV_SDCARD) {
perm = PERM_SDCARD; perm = PERM_SDCARD;
snprintf(area_name, 16, "SD card"); snprintf(area_name, sizeof(area_name), "%s", STR_SD_CARD);
} else if (drvtype & DRV_RAMDRIVE) { } else if (drvtype & DRV_RAMDRIVE) {
perm = PERM_RAMDRIVE; perm = PERM_RAMDRIVE;
snprintf(area_name, 16, "RAM drive"); snprintf(area_name, sizeof(area_name), "%s", STR_RAM_DRIVE);
} else { } else {
return false; return false;
} }
// check permission, return if already set // check permission, return if already set
if ((write_permissions & perm) == perm) if ((write_permissions & perm) == perm)
return true; return true;
// offer unlock if possible // offer unlock if possible
if (!(perm & (PERM_VRAM|PERM_GAME|PERM_XORPAD))) { if (!(perm & (PERM_VRAM|PERM_GAME|PERM_XORPAD))) {
// ask the user // ask the user
if (!ShowPrompt(true, "Writing to %s is locked!\nUnlock it now?", area_name)) if (!ShowPrompt(true, STR_WRITING_TO_DRIVE_IS_LOCKED_UNLOCK_NOW, area_name))
return false; return false;
return SetWritePermissions(perm, true); return SetWritePermissions(perm, true);
} }
// unlock not possible // unlock not possible
ShowPrompt(false, "Unlock write permission for\n%s is not allowed.", area_name); ShowPrompt(false, STR_UNLOCK_WRITE_FOR_DRIVE_NOT_ALLOWED, area_name);
return false; return false;
} }
@ -141,75 +142,75 @@ bool SetWritePermissions(u32 perm, bool add_perm) {
if (!add_perm) write_permissions = perm; if (!add_perm) write_permissions = perm;
return true; return true;
} }
switch (perm) { switch (perm) {
case PERM_BASE: case PERM_BASE:
if (!ShowUnlockSequence(1, "You want to enable base\nwriting permissions.")) if (!ShowUnlockSequence(1, "%s", STR_ENABLE_BASE_WRITE))
return false; return false;
break; break;
case PERM_SDCARD: case PERM_SDCARD:
if (!ShowUnlockSequence(1, "You want to enable SD card\nwriting permissions.")) if (!ShowUnlockSequence(1, "%s", STR_ENABLE_SD_WRITE))
return false; return false;
break; break;
case PERM_IMAGE: case PERM_IMAGE:
if (!ShowUnlockSequence(1, "You want to enable image\nwriting permissions.")) if (!ShowUnlockSequence(1, "%s", STR_ENABLE_IMAGE_WRITE))
return false; return false;
break; break;
case PERM_RAMDRIVE: case PERM_RAMDRIVE:
if (!ShowUnlockSequence(1, "You want to enable RAM drive\nwriting permissions.")) if (!ShowUnlockSequence(1, "%s", STR_ENABLE_RAM_DRIVE_WRITE))
return false; return false;
break; break;
case PERM_EMU_LVL0: case PERM_EMU_LVL0:
if (!ShowUnlockSequence(1, "You want to enable EmuNAND\nlvl0 writing permissions.")) if (!ShowUnlockSequence(1, "%s", STR_ENABLE_EMUNAND_0_WRITE))
return false; return false;
break; break;
case PERM_SYS_LVL0: case PERM_SYS_LVL0:
if (!ShowUnlockSequence(1, "You want to enable SysNAND\nlvl0 writing permissions.")) if (!ShowUnlockSequence(1, "%s", STR_ENABLE_SYSNAND_0_WRITE))
return false; return false;
break; break;
case PERM_EMU_LVL1: case PERM_EMU_LVL1:
if (!ShowUnlockSequence(2, "You want to enable EmuNAND\nlvl1 writing permissions.\n \nThis enables you to modify\nrecoverable system data,\nuser data & savegames.")) if (!ShowUnlockSequence(2, "%s", STR_ENABLE_EMUNAND_1_WRITE))
return false; return false;
break; break;
case PERM_SYS_LVL1: case PERM_SYS_LVL1:
if (!ShowUnlockSequence(2, "You want to enable SysNAND\nlvl1 writing permissions.\n \nThis enables you to modify\nsystem data, installations,\nuser data & savegames.")) if (!ShowUnlockSequence(2, "%s", STR_ENABLE_SYSNAND_1_WRITE))
return false;
break;
case PERM_SDDATA:
if (!ShowUnlockSequence(2, "You want to enable SD data\nwriting permissions.\n \nThis enables you to modify\ninstallations, user data &\nsavegames."))
return false; return false;
break; break;
case PERM_CART: case PERM_CART:
if (!ShowUnlockSequence(2, "You want to enable gamecart\nsave writing permissions.")) if (!ShowUnlockSequence(2, "%s", STR_ENABLE_GAMECART_SAVE_WRITE))
return false; return false;
break; break;
#ifndef SAFEMODE #ifndef SAFEMODE
case PERM_SYS_LVL2: case PERM_SYS_LVL2:
if (!ShowUnlockSequence(3, "!Better be careful!\n \nYou want to enable SysNAND\nlvl2 writing permissions.\n \nThis enables you to modify\nirrecoverable system data!")) if (!ShowUnlockSequence(3, "%s", STR_ENABLE_SYSNAND_2_WRITE))
return false; return false;
break; break;
case PERM_MEMORY: case PERM_MEMORY:
if (!ShowUnlockSequence(4, "!Better be careful!\n \nYou want to enable memory\nwriting permissions.\n \nWriting to certain areas may\nlead to unexpected results.")) if (!ShowUnlockSequence(4, "%s", STR_ENABLE_MEMORY_WRITE))
return false;
break;
case PERM_SDDATA:
if (!ShowUnlockSequence(5, "%s", STR_ENABLE_SD_DATA_WRITE))
return false; return false;
break; break;
case PERM_SYS_LVL3: case PERM_SYS_LVL3:
if (!ShowUnlockSequence(6, "!THIS IS YOUR ONLY WARNING!\n \nYou want to enable SysNAND\nlvl3 writing permissions.\n \nThis enables you to OVERWRITE\nyour bootloader installation,\nessential system files and/or\nBRICK your console!")) if (!ShowUnlockSequence(6, "%s", STR_ENABLE_SYSNAND_3_WRITE))
return false; return false;
break; break;
default: default:
ShowPrompt(false, "Unlock write permission is not allowed."); ShowPrompt(false, "%s", STR_UNLOCK_WRITE_NOT_ALLOWED);
return false; return false;
break; break;
#else #else
default: default:
ShowPrompt(false, "Can't unlock write permission.\nTry GodMode9 instead!"); ShowPrompt(false, "%s", STR_CANT_UNLOCK_WRITE_TRY_GODMODE9);
return false; return false;
break; break;
#endif #endif
} }
write_permissions = add_perm ? write_permissions | perm : perm; write_permissions = add_perm ? write_permissions | perm : perm;
return true; return true;
} }

View File

@ -21,7 +21,7 @@
#define PERM_BASE (PERM_SDCARD | PERM_IMAGE | PERM_RAMDRIVE | PERM_EMU_LVL0 | PERM_SYS_LVL0) #define PERM_BASE (PERM_SDCARD | PERM_IMAGE | PERM_RAMDRIVE | PERM_EMU_LVL0 | PERM_SYS_LVL0)
// permission levels / colors // permission levels / colors
#define PERM_BLUE (GetWritePermissions()&PERM_MEMORY) #define PERM_BLUE (GetWritePermissions()&(PERM_MEMORY|(PERM_SDDATA&~PERM_SDCARD)))
#define PERM_RED (GetWritePermissions()&(PERM_SYS_LVL3&~PERM_SYS_LVL2)) #define PERM_RED (GetWritePermissions()&(PERM_SYS_LVL3&~PERM_SYS_LVL2))
#define PERM_ORANGE (GetWritePermissions()&(PERM_SYS_LVL2&~PERM_SYS_LVL1)) #define PERM_ORANGE (GetWritePermissions()&(PERM_SYS_LVL2&~PERM_SYS_LVL1))
#define PERM_YELLOW (GetWritePermissions()&((PERM_SYS_LVL1&~PERM_SYS_LVL0)|(PERM_EMU_LVL1&~PERM_EMU_LVL0)|(PERM_SDDATA&~PERM_SDCARD)|PERM_CART)) #define PERM_YELLOW (GetWritePermissions()&((PERM_SYS_LVL1&~PERM_SYS_LVL0)|(PERM_EMU_LVL1&~PERM_EMU_LVL0)|(PERM_SDDATA&~PERM_SDCARD)|PERM_CART))

View File

@ -11,9 +11,10 @@
#include "ff.h" #include "ff.h"
#include "ui.h" #include "ui.h"
#include "swkbd.h" #include "swkbd.h"
#include "language.h"
#define SKIP_CUR (1UL<<10) #define SKIP_CUR (1UL<<11)
#define OVERWRITE_CUR (1UL<<11) #define OVERWRITE_CUR (1UL<<12)
#define _MAX_FS_OPT 8 // max file selector options #define _MAX_FS_OPT 8 // max file selector options
@ -43,19 +44,19 @@ bool FormatSDCard(u64 hidden_mb, u32 cluster_size, const char* label) {
u32 emu_size = (u32) ((hidden_mb * 1024 * 1024) / 512); u32 emu_size = (u32) ((hidden_mb * 1024 * 1024) / 512);
u32 fat_sector = align(emu_sector + emu_size, 0x2000); // align to 4MB u32 fat_sector = align(emu_sector + emu_size, 0x2000); // align to 4MB
u32 fat_size = (fat_sector < sd_size) ? sd_size - fat_sector : 0; u32 fat_size = (fat_sector < sd_size) ? sd_size - fat_sector : 0;
// FAT size check // FAT size check
if (fat_size < 0x80000) { // minimum free space: 256MB if (fat_size < 0x80000) { // minimum free space: 256MB
ShowPrompt(false, "Error: SD card is too small"); ShowPrompt(false, "%s", STR_ERROR_SD_TOO_SMALL);
return false; return false;
} }
// Write protection check // Write protection check
if (SD_WRITE_PROTECTED) { if (SD_WRITE_PROTECTED) {
ShowPrompt(false, "SD card is write protected!\nCan't continue."); ShowPrompt(false, "%s", STR_SD_WRITE_PROTECTED_CANT_CONTINUE);
return false; return false;
} }
// build the MBR // build the MBR
memcpy(mbrdata + 0x08, &fat_sector, 4); memcpy(mbrdata + 0x08, &fat_sector, 4);
memcpy(mbrdata + 0x0C, &fat_size, 4); memcpy(mbrdata + 0x0C, &fat_size, 4);
@ -64,25 +65,25 @@ bool FormatSDCard(u64 hidden_mb, u32 cluster_size, const char* label) {
memcpy(mbr + 0x1BE, mbrdata, 0x42); memcpy(mbr + 0x1BE, mbrdata, 0x42);
if (hidden_mb) memcpy(mbr, "GATEWAYNAND", 12); // legacy if (hidden_mb) memcpy(mbr, "GATEWAYNAND", 12); // legacy
else memset(mbr + 0x1CE, 0, 0x10); else memset(mbr + 0x1CE, 0, 0x10);
// one last warning.... // one last warning....
// 0:/Nintendo 3DS/ write permission is ignored here, this warning is enough // 0:/Nintendo 3DS/ write permission is ignored here, this warning is enough
if (!ShowUnlockSequence(5, "!WARNING!\n \nProceeding will format this SD.\nThis will irreversibly delete\nALL data on it.")) if (!ShowUnlockSequence(5, "%s", STR_WARNING_PROCEEDING_WILL_FORMAT_SD_DELETE_ALL_DATA))
return false; return false;
ShowString("Formatting SD, please wait..."); ShowString("%s", STR_FORMATTING_SD_PLEASE_WAIT);
// write the MBR to disk // write the MBR to disk
// !this assumes a fully deinitialized file system! // !this assumes a fully deinitialized file system!
if ((sdmmc_sdcard_init() != 0) || (sdmmc_sdcard_writesectors(0, 1, mbr) != 0) || if ((sdmmc_sdcard_init() != 0) || (sdmmc_sdcard_writesectors(0, 1, mbr) != 0) ||
(emu_size && ((sdmmc_nand_readsectors(0, 1, ncsd) != 0) || (sdmmc_sdcard_writesectors(1, 1, ncsd) != 0)))) { (emu_size && ((sdmmc_nand_readsectors(0, 1, ncsd) != 0) || (sdmmc_sdcard_writesectors(1, 1, ncsd) != 0)))) {
ShowPrompt(false, "Error: SD card i/o failure"); ShowPrompt(false, "%s", STR_ERROR_SD_CARD_IO_FAILURE);
return false; return false;
} }
// format the SD card // format the SD card
VolToPart[0].pt = 1; // workaround to prevent FatFS rebuilding the MBR VolToPart[0].pt = 1; // workaround to prevent FatFS rebuilding the MBR
InitSDCardFS(); InitSDCardFS();
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE); u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
if (!buffer) bkpt; // will not happen if (!buffer) bkpt; // will not happen
MKFS_PARM opt0, opt1; MKFS_PARM opt0, opt1;
@ -92,28 +93,28 @@ bool FormatSDCard(u64 hidden_mb, u32 cluster_size, const char* label) {
opt0.align = opt1.align = 0; opt0.align = opt1.align = 0;
opt0.n_fat = opt1.n_fat = 1; opt0.n_fat = opt1.n_fat = 1;
opt0.n_root = opt1.n_root = 0; opt0.n_root = opt1.n_root = 0;
bool ret = ((f_mkfs("0:", &opt0, buffer, STD_BUFFER_SIZE) == FR_OK) || bool ret = ((f_mkfs("0:", &opt0, buffer, STD_BUFFER_SIZE) == FR_OK) ||
(f_mkfs("0:", &opt1, buffer, STD_BUFFER_SIZE) == FR_OK)) && (f_mkfs("0:", &opt1, buffer, STD_BUFFER_SIZE) == FR_OK)) &&
(f_setlabel((label) ? label : "0:GM9SD") == FR_OK); (f_setlabel((label) ? label : "0:GM9SD") == FR_OK);
free(buffer); free(buffer);
DeinitSDCardFS(); DeinitSDCardFS();
VolToPart[0].pt = 0; // revert workaround to prevent SD mount problems VolToPart[0].pt = 0; // revert workaround to prevent SD mount problems
return ret; return ret;
} }
bool SetupBonusDrive(void) { bool SetupBonusDrive(void) {
if (!ShowUnlockSequence(3, "Format the bonus drive?\nThis will irreversibly delete\nALL data on it.")) if (!ShowUnlockSequence(3, "%s", STR_FORMAT_BONUS_DRIVE_DELETE_ALL_DATA))
return false; return false;
ShowString("Formatting drive, please wait..."); ShowString("%s", STR_FORMATTING_DRIVE_PLEASE_WAIT);
if (GetMountState() & IMG_NAND) InitImgFS(NULL); if (GetMountState() & IMG_NAND) InitImgFS(NULL);
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE); u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
if (!buffer) bkpt; if (!buffer) bkpt;
bool ret = (f_mkfs("8:", NULL, buffer, STD_BUFFER_SIZE) == FR_OK); bool ret = (f_mkfs("8:", NULL, buffer, STD_BUFFER_SIZE) == FR_OK);
free(buffer); free(buffer);
if (ret) { if (ret) {
f_setlabel("8:BONUS"); f_setlabel("8:BONUS");
InitExtFS(); InitExtFS();
@ -124,20 +125,20 @@ bool SetupBonusDrive(void) {
bool FileUnlock(const char* path) { bool FileUnlock(const char* path) {
FIL file; FIL file;
FRESULT res; FRESULT res;
if (!(DriveType(path) & DRV_FAT)) return true; // can't really check this if (!(DriveType(path) & DRV_FAT)) return true; // can't really check this
if ((res = fx_open(&file, path, FA_READ | FA_OPEN_EXISTING)) != FR_OK) { if ((res = fx_open(&file, path, FA_READ | FA_OPEN_EXISTING)) != FR_OK) {
char pathstr[32 + 1]; char pathstr[UTF_BUFFER_BYTESIZE(32)];
TruncateString(pathstr, path, 32, 8); TruncateString(pathstr, path, 32, 8);
if (GetMountState() && (res == FR_LOCKED) && if (GetMountState() && (res == FR_LOCKED) &&
(ShowPrompt(true, "%s\nFile is currently mounted.\nUnmount to unlock?", pathstr))) { (ShowPrompt(true, "%s\n%s", pathstr, STR_FILE_IS_MOUNTED_UNMOUNT_TO_UNLOCK))) {
InitImgFS(NULL); InitImgFS(NULL);
if (fx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) if (fx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return false; return false;
} else return false; } else return false;
} }
fx_close(&file); fx_close(&file);
return true; return true;
} }
@ -161,25 +162,25 @@ size_t FileGetSize(const char* path) {
return fno.fsize; return fno.fsize;
} }
bool FileGetSha256(const char* path, u8* sha256, u64 offset, u64 size) { bool FileGetSha(const char* path, u8* hash, u64 offset, u64 size, bool sha1) {
bool ret = true; bool ret = true;
FIL file; FIL file;
u64 fsize; u64 fsize;
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return false; return false;
fsize = fvx_size(&file); fsize = fvx_size(&file);
if (offset + size > fsize) return false; if (offset + size > fsize) return false;
if (!size) size = fsize - offset; if (!size) size = fsize - offset;
fvx_lseek(&file, offset); fvx_lseek(&file, offset);
u32 bufsiz = min(STD_BUFFER_SIZE, fsize); u32 bufsiz = min(STD_BUFFER_SIZE, fsize);
u8* buffer = (u8*) malloc(bufsiz); u8* buffer = (u8*) malloc(bufsiz);
if (!buffer) return false; if (!buffer) return false;
ShowProgress(0, 0, path); ShowProgress(0, 0, path);
sha_init(SHA256_MODE); sha_init(sha1 ? SHA1_MODE : SHA256_MODE);
for (u64 pos = 0; (pos < size) && ret; pos += bufsiz) { for (u64 pos = 0; (pos < size) && ret; pos += bufsiz) {
UINT read_bytes = min(bufsiz, size - pos); UINT read_bytes = min(bufsiz, size - pos);
UINT bytes_read = 0; UINT bytes_read = 0;
@ -189,13 +190,13 @@ bool FileGetSha256(const char* path, u8* sha256, u64 offset, u64 size) {
ret = false; ret = false;
sha_update(buffer, bytes_read); sha_update(buffer, bytes_read);
} }
sha_get(sha256); sha_get(hash);
fvx_close(&file); fvx_close(&file);
free(buffer); free(buffer);
ShowProgress(1, 1, path); ShowProgress(1, 1, path);
return ret; return ret;
} }
@ -203,13 +204,13 @@ u32 FileFindData(const char* path, u8* data, u32 size_data, u32 offset_file) {
FIL file; // used for FAT & virtual FIL file; // used for FAT & virtual
u64 found = (u64) -1; u64 found = (u64) -1;
u64 fsize = FileGetSize(path); u64 fsize = FileGetSize(path);
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return found; return found;
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE); u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
if (!buffer) return false; if (!buffer) return false;
// main routine // main routine
for (u32 pass = 0; pass < 2; pass++) { for (u32 pass = 0; pass < 2; pass++) {
bool show_progress = false; bool show_progress = false;
@ -236,10 +237,10 @@ u32 FileFindData(const char* path, u8* data, u32 size_data, u32 offset_file) {
break; break;
} }
} }
free(buffer); free(buffer);
fvx_close(&file); fvx_close(&file);
return found; return found;
} }
@ -247,13 +248,13 @@ bool FileInjectFile(const char* dest, const char* orig, u64 off_dest, u64 off_or
FIL ofile; FIL ofile;
FIL dfile; FIL dfile;
bool allow_expand = (flags && (*flags & ALLOW_EXPAND)); bool allow_expand = (flags && (*flags & ALLOW_EXPAND));
if (!CheckWritePermissions(dest)) return false; if (!CheckWritePermissions(dest)) return false;
if (strncasecmp(dest, orig, 256) == 0) { if (strncasecmp(dest, orig, 256) == 0) {
ShowPrompt(false, "Error: Can't inject file into itself"); ShowPrompt(false, "%s", STR_ERROR_CANT_INJECT_FILE_INTO_ITSELF);
return false; return false;
} }
// open destination / origin // open destination / origin
if (fvx_open(&dfile, dest, FA_WRITE | ((allow_expand) ? FA_OPEN_ALWAYS : FA_OPEN_EXISTING)) != FR_OK) if (fvx_open(&dfile, dest, FA_WRITE | ((allow_expand) ? FA_OPEN_ALWAYS : FA_OPEN_EXISTING)) != FR_OK)
return false; return false;
@ -266,23 +267,23 @@ bool FileInjectFile(const char* dest, const char* orig, u64 off_dest, u64 off_or
fvx_lseek(&ofile, off_orig); fvx_lseek(&ofile, off_orig);
if (!size && (off_orig < fvx_size(&ofile))) if (!size && (off_orig < fvx_size(&ofile)))
size = fvx_size(&ofile) - off_orig; size = fvx_size(&ofile) - off_orig;
// check file limits // check file limits
if (!allow_expand && (off_dest + size > fvx_size(&dfile))) { if (!allow_expand && (off_dest + size > fvx_size(&dfile))) {
ShowPrompt(false, "Operation would write beyond end of file"); ShowPrompt(false, "%s", STR_OPERATION_WOULD_WRITE_BEYOND_EOF);
fvx_close(&dfile); fvx_close(&dfile);
fvx_close(&ofile); fvx_close(&ofile);
return false; return false;
} else if (off_orig + size > fvx_size(&ofile)) { } else if (off_orig + size > fvx_size(&ofile)) {
ShowPrompt(false, "Not enough data in file"); ShowPrompt(false, "%s", STR_NOT_ENOUGH_DATA_IN_FILE);
fvx_close(&dfile); fvx_close(&dfile);
fvx_close(&ofile); fvx_close(&ofile);
return false; return false;
} }
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE); u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
if (!buffer) return false; if (!buffer) return false;
bool ret = true; bool ret = true;
ShowProgress(0, 0, orig); ShowProgress(0, 0, orig);
for (u64 pos = 0; (pos < size) && ret; pos += STD_BUFFER_SIZE) { for (u64 pos = 0; (pos < size) && ret; pos += STD_BUFFER_SIZE) {
@ -295,46 +296,46 @@ bool FileInjectFile(const char* dest, const char* orig, u64 off_dest, u64 off_or
ret = false; ret = false;
if (ret && !ShowProgress(pos + bytes_read, size, orig)) { if (ret && !ShowProgress(pos + bytes_read, size, orig)) {
if (flags && (*flags & NO_CANCEL)) { if (flags && (*flags & NO_CANCEL)) {
ShowPrompt(false, "Cancel is not allowed here"); ShowPrompt(false, "%s", STR_CANCEL_IS_NOT_ALLOWED_HERE);
} else ret = !ShowPrompt(true, "B button detected. Cancel?"); } else ret = !ShowPrompt(true, "%s", STR_B_DETECTED_CANCEL);
ShowProgress(0, 0, orig); ShowProgress(0, 0, orig);
ShowProgress(pos + bytes_read, size, orig); ShowProgress(pos + bytes_read, size, orig);
} }
} }
ShowProgress(1, 1, orig); ShowProgress(1, 1, orig);
free(buffer); free(buffer);
fvx_close(&dfile); fvx_close(&dfile);
fvx_close(&ofile); fvx_close(&ofile);
return ret; return ret;
} }
bool FileSetByte(const char* dest, u64 offset, u64 size, u8 fillbyte, u32* flags) { bool FileSetByte(const char* dest, u64 offset, u64 size, u8 fillbyte, u32* flags) {
FIL dfile; FIL dfile;
bool allow_expand = (flags && (*flags & ALLOW_EXPAND)); bool allow_expand = (flags && (*flags & ALLOW_EXPAND));
if (!CheckWritePermissions(dest)) return false; if (!CheckWritePermissions(dest)) return false;
// open destination // open destination
if (fvx_open(&dfile, dest, FA_WRITE | ((allow_expand) ? FA_OPEN_ALWAYS : FA_OPEN_EXISTING)) != FR_OK) if (fvx_open(&dfile, dest, FA_WRITE | ((allow_expand) ? FA_OPEN_ALWAYS : FA_OPEN_EXISTING)) != FR_OK)
return false; return false;
fvx_lseek(&dfile, offset); fvx_lseek(&dfile, offset);
if (!size && (offset < fvx_size(&dfile))) if (!size && (offset < fvx_size(&dfile)))
size = fvx_size(&dfile) - offset; size = fvx_size(&dfile) - offset;
// check file limits // check file limits
if (!allow_expand && (offset + size > fvx_size(&dfile))) { if (!allow_expand && (offset + size > fvx_size(&dfile))) {
ShowPrompt(false, "Operation would write beyond end of file"); ShowPrompt(false, "%s", STR_OPERATION_WOULD_WRITE_BEYOND_EOF);
fvx_close(&dfile); fvx_close(&dfile);
return false; return false;
} }
u32 bufsiz = min(STD_BUFFER_SIZE, size); u32 bufsiz = min(STD_BUFFER_SIZE, size);
u8* buffer = (u8*) malloc(bufsiz); u8* buffer = (u8*) malloc(bufsiz);
if (!buffer) return false; if (!buffer) return false;
memset(buffer, fillbyte, bufsiz); memset(buffer, fillbyte, bufsiz);
bool ret = true; bool ret = true;
ShowProgress(0, 0, dest); ShowProgress(0, 0, dest);
for (u64 pos = 0; (pos < size) && ret; pos += bufsiz) { for (u64 pos = 0; (pos < size) && ret; pos += bufsiz) {
@ -345,26 +346,26 @@ bool FileSetByte(const char* dest, u64 offset, u64 size, u8 fillbyte, u32* flags
ret = false; ret = false;
if (ret && !ShowProgress(pos + bytes_written, size, dest)) { if (ret && !ShowProgress(pos + bytes_written, size, dest)) {
if (flags && (*flags & NO_CANCEL)) { if (flags && (*flags & NO_CANCEL)) {
ShowPrompt(false, "Cancel is not allowed here"); ShowPrompt(false, "%s", STR_CANCEL_IS_NOT_ALLOWED_HERE);
} else ret = !ShowPrompt(true, "B button detected. Cancel?"); } else ret = !ShowPrompt(true, "%s", STR_B_DETECTED_CANCEL);
ShowProgress(0, 0, dest); ShowProgress(0, 0, dest);
ShowProgress(pos + bytes_written, size, dest); ShowProgress(pos + bytes_written, size, dest);
} }
} }
ShowProgress(1, 1, dest); ShowProgress(1, 1, dest);
free(buffer); free(buffer);
fvx_close(&dfile); fvx_close(&dfile);
return ret; return ret;
} }
bool FileCreateDummy(const char* cpath, const char* filename, u64 size) { bool FileCreateDummy(const char* cpath, const char* filename, u64 size) {
char npath[256]; // 256 is the maximum length of a full path char npath[256]; // 256 is the maximum length of a full path
if (!CheckWritePermissions(cpath)) return false; if (!CheckWritePermissions(cpath)) return false;
if (filename) snprintf(npath, 255, "%s/%s", cpath, filename); if (filename) snprintf(npath, sizeof(npath), "%s/%s", cpath, filename);
else snprintf(npath, 255, "%s", cpath); else snprintf(npath, sizeof(npath), "%s", cpath);
// create dummy file (fail if already existing) // create dummy file (fail if already existing)
// then, expand the file size via cluster preallocation // then, expand the file size via cluster preallocation
FIL dfile; FIL dfile;
@ -373,14 +374,14 @@ bool FileCreateDummy(const char* cpath, const char* filename, u64 size) {
f_lseek(&dfile, size > 0xFFFFFFFF ? 0xFFFFFFFF : (FSIZE_t) size); f_lseek(&dfile, size > 0xFFFFFFFF ? 0xFFFFFFFF : (FSIZE_t) size);
f_sync(&dfile); f_sync(&dfile);
fx_close(&dfile); fx_close(&dfile);
return (fa_stat(npath, NULL) == FR_OK); return (fa_stat(npath, NULL) == FR_OK);
} }
bool DirCreate(const char* cpath, const char* dirname) { bool DirCreate(const char* cpath, const char* dirname) {
char npath[256]; // 256 is the maximum length of a full path char npath[256]; // 256 is the maximum length of a full path
if (!CheckWritePermissions(cpath)) return false; if (!CheckWritePermissions(cpath)) return false;
snprintf(npath, 255, "%s/%s", cpath, dirname); snprintf(npath, sizeof(npath), "%s/%s", cpath, dirname);
if (fa_mkdir(npath) != FR_OK) return false; if (fa_mkdir(npath) != FR_OK) return false;
return (fa_stat(npath, NULL) == FR_OK); return (fa_stat(npath, NULL) == FR_OK);
} }
@ -425,7 +426,7 @@ bool DirInfoWorker(char* fpath, bool virtual, u64* tsize, u32* tdirs, u32* tfile
} }
f_closedir(&pdir); f_closedir(&pdir);
} }
return ret; return ret;
} }
@ -448,24 +449,25 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
bool silent = (flags && (*flags & SILENT)); bool silent = (flags && (*flags & SILENT));
bool append = (flags && (*flags & APPEND_ALL)); bool append = (flags && (*flags & APPEND_ALL));
bool calcsha = (flags && (*flags & CALC_SHA) && !append); bool calcsha = (flags && (*flags & CALC_SHA) && !append);
bool sha1 = (flags && (*flags & USE_SHA1));
bool ret = false; bool ret = false;
// check destination write permission (special paths only) // check destination write permission (special paths only)
if (((*dest == '1') || (strncmp(dest, "0:/Nintendo 3DS", 16) == 0)) && if (((*dest == '1') || (strncmp(dest, "0:/Nintendo 3DS", 16) == 0)) &&
(!flags || !(*flags & OVERRIDE_PERM)) && (!flags || !(*flags & OVERRIDE_PERM)) &&
!CheckWritePermissions(dest)) return false; !CheckWritePermissions(dest)) return false;
FILINFO fno; FILINFO fno;
if (fvx_stat(orig, &fno) != FR_OK) return false; // origin does not exist if (fvx_stat(orig, &fno) != FR_OK) return false; // origin does not exist
if (move && (to_virtual || fno.fattrib & AM_VRT)) return false; // trying to move a virtual file if (move && (to_virtual || fno.fattrib & AM_VRT)) return false; // trying to move a virtual file
// path string (for output) // path string (for output)
char deststr[36 + 1]; char deststr[UTF_BUFFER_BYTESIZE(36)];
TruncateString(deststr, dest, 36, 8); TruncateString(deststr, dest, 36, 8);
// the copy process takes place here // the copy process takes place here
if (!ShowProgress(0, 0, orig) && !(flags && (*flags & NO_CANCEL))) { if (!ShowProgress(0, 0, orig) && !(flags && (*flags & NO_CANCEL))) {
if (ShowPrompt(true, "%s\nB button detected. Cancel?", deststr)) return false; if (ShowPrompt(true, "%s\n%s", deststr, STR_B_DETECTED_CANCEL)) return false;
ShowProgress(0, 0, orig); ShowProgress(0, 0, orig);
} }
if (move && fvx_stat(dest, NULL) != FR_OK) { // moving if dest not existing if (move && fvx_stat(dest, NULL) != FR_OK) { // moving if dest not existing
@ -473,24 +475,24 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
} else if (fno.fattrib & AM_DIR) { // processing folders (same for move & copy) } else if (fno.fattrib & AM_DIR) { // processing folders (same for move & copy)
DIR pdir; DIR pdir;
char* fname = orig + strnlen(orig, 256); char* fname = orig + strnlen(orig, 256);
if (append) { if (append) {
if (!silent) ShowPrompt(false, "%s\nError: Cannot append a folder", deststr); if (!silent) ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_CANNOT_APPEND_FOLDER);
return false; return false;
} }
// create the destination folder if it does not already exist // create the destination folder if it does not already exist
if (fvx_opendir(&pdir, dest) != FR_OK) { if (fvx_opendir(&pdir, dest) != FR_OK) {
if (fvx_mkdir(dest) != FR_OK) { if (fvx_mkdir(dest) != FR_OK) {
if (!silent) ShowPrompt(false, "%s\nError: Overwriting file with dir", deststr); if (!silent) ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_OVERWRITING_FILE_WITH_DIR);
return false; return false;
} }
} else fvx_closedir(&pdir); } else fvx_closedir(&pdir);
if (fvx_opendir(&pdir, orig) != FR_OK) if (fvx_opendir(&pdir, orig) != FR_OK)
return false; return false;
*(fname++) = '/'; *(fname++) = '/';
while (fvx_readdir(&pdir, &fno) == FR_OK) { while (fvx_readdir(&pdir, &fno) == FR_OK) {
if ((strncmp(fno.fname, ".", 2) == 0) || (strncmp(fno.fname, "..", 3) == 0)) if ((strncmp(fno.fname, ".", 2) == 0) || (strncmp(fno.fname, "..", 3) == 0))
continue; // filter out virtual entries continue; // filter out virtual entries
@ -509,13 +511,13 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
if (!res) break; if (!res) break;
} }
} }
fvx_closedir(&pdir); fvx_closedir(&pdir);
*(--fname) = '\0'; *(--fname) = '\0';
} else if (move) { // moving if destination exists } else if (move) { // moving if destination exists
if (fvx_stat(dest, &fno) != FR_OK) return false; if (fvx_stat(dest, &fno) != FR_OK) return false;
if (fno.fattrib & AM_DIR) { if (fno.fattrib & AM_DIR) {
if (!silent) ShowPrompt(false, "%s\nError: Overwriting dir with file", deststr); if (!silent) ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_OVERWRITING_DIR_WITH_FILE);
return false; return false;
} }
if (fvx_unlink(dest) != FR_OK) return false; if (fvx_unlink(dest) != FR_OK) return false;
@ -525,37 +527,37 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
FIL dfile; FIL dfile;
u64 osize; u64 osize;
u64 dsize; u64 dsize;
if (fvx_open(&ofile, orig, FA_READ | FA_OPEN_EXISTING) != FR_OK) { if (fvx_open(&ofile, orig, FA_READ | FA_OPEN_EXISTING) != FR_OK) {
if (!FileUnlock(orig) || (fvx_open(&ofile, orig, FA_READ | FA_OPEN_EXISTING) != FR_OK)) if (!FileUnlock(orig) || (fvx_open(&ofile, orig, FA_READ | FA_OPEN_EXISTING) != FR_OK))
return false; return false;
ShowProgress(0, 0, orig); // reinit progress bar ShowProgress(0, 0, orig); // reinit progress bar
} }
if ((!append || (fvx_open(&dfile, dest, FA_WRITE | FA_OPEN_EXISTING) != FR_OK)) && if ((!append || (fvx_open(&dfile, dest, FA_WRITE | FA_OPEN_EXISTING) != FR_OK)) &&
(fvx_open(&dfile, dest, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK)) { (fvx_open(&dfile, dest, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK)) {
if (!silent) ShowPrompt(false, "%s\nError: Cannot open destination file", deststr); if (!silent) ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_CANNOT_OPEN_DESTINATION_FILE);
fvx_close(&ofile); fvx_close(&ofile);
return false; return false;
} }
ret = true; // destination file exists by now, so we need to handle deletion ret = true; // destination file exists by now, so we need to handle deletion
osize = fvx_size(&ofile); osize = fvx_size(&ofile);
dsize = append ? fvx_size(&dfile) : 0; // always 0 if not appending to file dsize = append ? fvx_size(&dfile) : 0; // always 0 if not appending to file
if ((fvx_lseek(&dfile, (osize + dsize)) != FR_OK) || (fvx_sync(&dfile) != FR_OK) || (fvx_tell(&dfile) != (osize + dsize))) { // check space via cluster preallocation if ((fvx_lseek(&dfile, (osize + dsize)) != FR_OK) || (fvx_sync(&dfile) != FR_OK) || (fvx_tell(&dfile) != (osize + dsize))) { // check space via cluster preallocation
if (!silent) ShowPrompt(false, "%s\nError: Not enough space available", deststr); if (!silent) ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_NOT_ENOUGH_SPACE_AVAILABLE);
ret = false; ret = false;
} }
fvx_lseek(&dfile, dsize); fvx_lseek(&dfile, dsize);
fvx_sync(&dfile); fvx_sync(&dfile);
fvx_lseek(&ofile, 0); fvx_lseek(&ofile, 0);
fvx_sync(&ofile); fvx_sync(&ofile);
if (calcsha) sha_init(SHA256_MODE); if (calcsha) sha_init(sha1 ? SHA1_MODE : SHA256_MODE);
for (u64 pos = 0; (pos < osize) && ret; pos += bufsiz) { for (u64 pos = 0; (pos < osize) && ret; pos += bufsiz) {
UINT bytes_read = 0; UINT bytes_read = 0;
UINT bytes_written = 0; UINT bytes_written = 0;
if ((fvx_read(&ofile, buffer, bufsiz, &bytes_read) != FR_OK) || if ((fvx_read(&ofile, buffer, bufsiz, &bytes_read) != FR_OK) ||
(fvx_write(&dfile, buffer, bytes_read, &bytes_written) != FR_OK) || (fvx_write(&dfile, buffer, bytes_read, &bytes_written) != FR_OK) ||
(bytes_read != bytes_written)) (bytes_read != bytes_written))
@ -565,8 +567,8 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
u64 total = osize; u64 total = osize;
if (ret && !ShowProgress(current, total, orig)) { if (ret && !ShowProgress(current, total, orig)) {
if (flags && (*flags & NO_CANCEL)) { if (flags && (*flags & NO_CANCEL)) {
ShowPrompt(false, "%s\nCancel is not allowed here", deststr); ShowPrompt(false, "%s\n%s", deststr, STR_CANCEL_IS_NOT_ALLOWED_HERE);
} else ret = !ShowPrompt(true, "%s\nB button detected. Cancel?", deststr); } else ret = !ShowPrompt(true, "%s\n%s", deststr, STR_B_DETECTED_CANCEL);
ShowProgress(0, 0, orig); ShowProgress(0, 0, orig);
ShowProgress(current, total, orig); ShowProgress(current, total, orig);
} }
@ -574,20 +576,20 @@ bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move, u8* buffer,
sha_update(buffer, bytes_read); sha_update(buffer, bytes_read);
} }
ShowProgress(1, 1, orig); ShowProgress(1, 1, orig);
fvx_close(&ofile); fvx_close(&ofile);
fvx_close(&dfile); fvx_close(&dfile);
if (!ret && ((dsize == 0) || (fvx_lseek(&dfile, dsize) != FR_OK) || (f_truncate(&dfile) != FR_OK))) { if (!ret && ((dsize == 0) || (fvx_lseek(&dfile, dsize) != FR_OK) || (f_truncate(&dfile) != FR_OK))) {
fvx_unlink(dest); fvx_unlink(dest);
} else if (!to_virtual && calcsha) { } else if (!to_virtual && calcsha) {
u8 sha256[0x20]; u8 hash[0x20];
char* ext_sha = dest + strnlen(dest, 256); char* ext_sha = dest + strnlen(dest, 256);
strncpy(ext_sha, ".sha", 256 - (ext_sha - dest)); snprintf(ext_sha, 256 - (ext_sha - dest), ".sha%c", sha1 ? '1' : '\0');
sha_get(sha256); sha_get(hash);
FileSetData(dest, sha256, 0x20, 0, true); FileSetData(dest, hash, sha1 ? 20 : 32, 0, true);
} }
} }
return ret; return ret;
} }
@ -597,45 +599,45 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
if (!CheckWritePermissions(dest)) return false; if (!CheckWritePermissions(dest)) return false;
if (move && !CheckDirWritePermissions(orig)) return false; if (move && !CheckDirWritePermissions(orig)) return false;
} }
// reset local flags // reset local flags
if (flags) *flags = *flags & ~(SKIP_CUR|OVERWRITE_CUR); if (flags) *flags = *flags & ~(SKIP_CUR|OVERWRITE_CUR);
// preparations // preparations
int ddrvtype = DriveType(dest); int ddrvtype = DriveType(dest);
int odrvtype = DriveType(orig); int odrvtype = DriveType(orig);
char ldest[256]; // 256 is the maximum length of a full path char ldest[256]; // 256 is the maximum length of a full path
char lorig[256]; char lorig[256];
strncpy(ldest, dest, 255); strncpy(ldest, dest, 256);
strncpy(lorig, orig, 255); strncpy(lorig, orig, 256);
char deststr[36 + 1]; char deststr[UTF_BUFFER_BYTESIZE(36)];
TruncateString(deststr, ldest, 36, 8); TruncateString(deststr, ldest, 36, 8);
// moving only for regular FAT drives (= not alias drives) // moving only for regular FAT drives (= not alias drives)
if (move && !(ddrvtype & odrvtype & DRV_STDFAT)) { if (move && !(ddrvtype & odrvtype & DRV_STDFAT)) {
ShowPrompt(false, "Error: Only FAT files can be moved"); ShowPrompt(false, "%s", STR_ERROR_ONLY_FAT_FILES_CAN_BE_MOVED);
return false; return false;
} }
// is destination part of origin? // is destination part of origin?
u32 olen = strnlen(lorig, 255); u32 olen = strnlen(lorig, 255);
if ((strncasecmp(ldest, lorig, olen) == 0) && (ldest[olen] == '/')) { if ((strncasecmp(ldest, lorig, olen) == 0) && (ldest[olen] == '/')) {
ShowPrompt(false, "%s\nError: Destination is part of origin", deststr); ShowPrompt(false, "%s\n%s", deststr, STR_ERROR_DESTINATION_IS_PART_OF_ORIGIN);
return false; return false;
} }
if (!(ddrvtype & DRV_VIRTUAL)) { // FAT destination handling if (!(ddrvtype & DRV_VIRTUAL)) { // FAT destination handling
// get destination name // get destination name
char* dname = strrchr(ldest, '/'); char* dname = strrchr(ldest, '/');
if (!dname) return false; if (!dname) return false;
dname++; dname++;
// check & fix destination == origin // check & fix destination == origin
while (strncasecmp(ldest, lorig, 255) == 0) { while (strncasecmp(ldest, lorig, 255) == 0) {
if (!ShowKeyboardOrPrompt(dname, 255 - (dname - ldest), "%s\nDestination equals origin\nChoose another name?", deststr)) if (!ShowKeyboardOrPrompt(dname, 255 - (dname - ldest), "%s\n%s", deststr, STR_ERROR_DESTINATION_EQUALS_ORIGIN_CHOOSE_ANOTHER_NAME))
return false; return false;
} }
// check if destination exists // check if destination exists
if (flags && !(*flags & (OVERWRITE_CUR|OVERWRITE_ALL|APPEND_ALL)) && (fa_stat(ldest, NULL) == FR_OK)) { if (flags && !(*flags & (OVERWRITE_CUR|OVERWRITE_ALL|APPEND_ALL)) && (fa_stat(ldest, NULL) == FR_OK)) {
if (*flags & SKIP_ALL) { if (*flags & SKIP_ALL) {
@ -643,12 +645,11 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
return true; return true;
} }
const char* optionstr[5] = const char* optionstr[5] =
{"Choose new name", "Overwrite file(s)", "Skip file(s)", "Overwrite all", "Skip all"}; {STR_CHOOSE_NEW_NAME, STR_OVERWRITE_FILES, STR_SKIP_FILES, STR_OVERWRITE_ALL, STR_SKIP_ALL};
u32 user_select = ShowSelectPrompt((*flags & ASK_ALL) ? 5 : 3, optionstr, u32 user_select = ShowSelectPrompt((*flags & ASK_ALL) ? 5 : 3, optionstr, STR_DESTINATION_ALREADY_EXISTS, deststr);
"Destination already exists:\n%s", deststr);
if (user_select == 1) { if (user_select == 1) {
do { do {
if (!ShowKeyboardOrPrompt(dname, 255 - (dname - ldest), "Choose new destination name")) if (!ShowKeyboardOrPrompt(dname, 255 - (dname - ldest), "%s", STR_CHOOSE_NEW_DESTINATION_NAME))
return false; return false;
} while (fa_stat(ldest, NULL) == FR_OK); } while (fa_stat(ldest, NULL) == FR_OK);
} else if (user_select == 2) { } else if (user_select == 2) {
@ -665,29 +666,29 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
return false; return false;
} }
} }
// ensure the destination path exists // ensure the destination path exists
if (flags && (*flags & BUILD_PATH)) fvx_rmkpath(ldest); if (flags && (*flags & BUILD_PATH)) fvx_rmkpath(ldest);
// setup buffer // setup buffer
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE); u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
if (!buffer) { if (!buffer) {
ShowPrompt(false, "Out of memory."); ShowPrompt(false, "%s", STR_OUT_OF_MEMORY);
return false; return false;
} }
// actual move / copy operation // actual move / copy operation
bool same_drv = (strncasecmp(lorig, ldest, 2) == 0); bool same_drv = (strncasecmp(lorig, ldest, 2) == 0);
bool res = PathMoveCopyRec(ldest, lorig, flags, move && same_drv, buffer, STD_BUFFER_SIZE); bool res = PathMoveCopyRec(ldest, lorig, flags, move && same_drv, buffer, STD_BUFFER_SIZE);
if (move && res && (!flags || !(*flags&SKIP_CUR))) PathDelete(lorig); if (move && res && (!flags || !(*flags&SKIP_CUR))) PathDelete(lorig);
free(buffer); free(buffer);
return res; return res;
} else { // virtual destination handling } else { // virtual destination handling
// can't write an SHA file to a virtual destination // can't write an SHA file to a virtual destination
if (flags) *flags &= ~CALC_SHA; if (flags) *flags &= ~CALC_SHA;
bool force_unmount = false; bool force_unmount = false;
// handle NAND image unmounts // handle NAND image unmounts
if ((ddrvtype & (DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE)) && !(GetVirtualSource(dest) & (VRT_DISADIFF | VRT_BDRI))) { if ((ddrvtype & (DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE)) && !(GetVirtualSource(dest) & (VRT_DISADIFF | VRT_BDRI))) {
FILINFO fno; FILINFO fno;
@ -695,31 +696,31 @@ bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
if ((fvx_stat(ldest, &fno) == FR_OK) && (fno.fsize > 4 * 1024 * 1024)) if ((fvx_stat(ldest, &fno) == FR_OK) && (fno.fsize > 4 * 1024 * 1024))
force_unmount = true; force_unmount = true;
} }
// prevent illegal operations // prevent illegal operations
if (force_unmount && (odrvtype & ddrvtype & (DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE))) { if (force_unmount && (odrvtype & ddrvtype & (DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE))) {
ShowPrompt(false, "Copy operation is not allowed"); ShowPrompt(false, "%s", STR_COPY_OPERATION_IS_NOT_ALLOWED);
return false;
}
// check destination == origin
if (strncasecmp(ldest, lorig, 255) == 0) {
ShowPrompt(false, "%s\nDestination equals origin", deststr);
return false; return false;
} }
// check destination == origin
if (strncasecmp(ldest, lorig, 255) == 0) {
ShowPrompt(false, "%s\n%s", deststr, STR_DESTINATION_EQUALS_ORIGIN);
return false;
}
// setup buffer // setup buffer
u8* buffer = (u8*) malloc(STD_BUFFER_SIZE); u8* buffer = (u8*) malloc(STD_BUFFER_SIZE);
if (!buffer) { if (!buffer) {
ShowPrompt(false, "Out of memory."); ShowPrompt(false, "%s", STR_OUT_OF_MEMORY);
return false; return false;
} }
// actual virtual copy operation // actual virtual copy operation
if (force_unmount) DismountDriveType(DriveType(ldest)&(DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE)); if (force_unmount) DismountDriveType(DriveType(ldest)&(DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE));
bool res = PathMoveCopyRec(ldest, lorig, flags, false, buffer, STD_BUFFER_SIZE); bool res = PathMoveCopyRec(ldest, lorig, flags, false, buffer, STD_BUFFER_SIZE);
if (force_unmount) InitExtFS(); if (force_unmount) InitExtFS();
free(buffer); free(buffer);
return res; return res;
} }
@ -730,8 +731,8 @@ bool PathCopy(const char* destdir, const char* orig, u32* flags) {
char dest[256]; // maximum path name length in FAT char dest[256]; // maximum path name length in FAT
char* oname = strrchr(orig, '/'); char* oname = strrchr(orig, '/');
if (oname == NULL) return false; // not a proper origin path if (oname == NULL) return false; // not a proper origin path
snprintf(dest, 255, "%s/%s", destdir, (++oname)); snprintf(dest, sizeof(dest), "%s/%s", destdir, (++oname));
// virtual destination special handling // virtual destination special handling
if (GetVirtualSource(destdir) & ~VRT_BDRI) { if (GetVirtualSource(destdir) & ~VRT_BDRI) {
u64 osize = FileGetSize(orig); u64 osize = FileGetSize(orig);
@ -744,12 +745,12 @@ bool PathCopy(const char* destdir, const char* orig, u32* flags) {
if (!ReadVirtualDir(&dvfile, &vdir)) return false; if (!ReadVirtualDir(&dvfile, &vdir)) return false;
if (dvfile.size == osize) break; // file found if (dvfile.size == osize) break; // file found
} }
if (!ShowPrompt(true, "Entry not found: %s\nInject into %s instead?", dest, dvfile.name)) if (!ShowPrompt(true, STR_ENTRY_NOT_FOUND_PATH_INJECT_INTO_PATH_INSTEAD, dest, dvfile.name))
return false; return false;
snprintf(dest, 255, "%s/%s", destdir, dvfile.name); snprintf(dest, sizeof(dest), "%s/%s", destdir, dvfile.name);
} else if (osize < dvfile.size) { // if origin is smaller than destination... } else if (osize < dvfile.size) { // if origin is smaller than destination...
char deststr[36 + 1]; char deststr[UTF_BUFFER_BYTESIZE(36)];
char origstr[36 + 1]; char origstr[UTF_BUFFER_BYTESIZE(36)];
char osizestr[32]; char osizestr[32];
char dsizestr[32]; char dsizestr[32];
TruncateString(deststr, dest, 36, 8); TruncateString(deststr, dest, 36, 8);
@ -757,12 +758,12 @@ bool PathCopy(const char* destdir, const char* orig, u32* flags) {
FormatBytes(osizestr, osize); FormatBytes(osizestr, osize);
FormatBytes(dsizestr, dvfile.size); FormatBytes(dsizestr, dvfile.size);
if (dvfile.size > osize) { if (dvfile.size > osize) {
if (!ShowPrompt(true, "File smaller than available space:\n%s (%s)\n%s (%s)\nContinue?", origstr, osizestr, deststr, dsizestr)) if (!ShowPrompt(true, STR_FILE_SMALLER_THAN_SPACE_SIZES_CONTINUE, origstr, osizestr, deststr, dsizestr))
return false; return false;
} }
} }
} }
return PathMoveCopy(dest, orig, flags, false); return PathMoveCopy(dest, orig, flags, false);
} }
@ -771,8 +772,8 @@ bool PathMove(const char* destdir, const char* orig, u32* flags) {
char dest[256]; // maximum path name length in FAT char dest[256]; // maximum path name length in FAT
char* oname = strrchr(orig, '/'); char* oname = strrchr(orig, '/');
if (oname == NULL) return false; // not a proper origin path if (oname == NULL) return false; // not a proper origin path
snprintf(dest, 255, "%s/%s", destdir, (++oname)); snprintf(dest, sizeof(dest), "%s/%s", destdir, (++oname));
return PathMoveCopy(dest, orig, flags, true); return PathMoveCopy(dest, orig, flags, true);
} }
@ -784,13 +785,13 @@ bool PathDelete(const char* path) {
bool PathRename(const char* path, const char* newname) { bool PathRename(const char* path, const char* newname) {
char npath[256]; // 256 is the maximum length of a full path char npath[256]; // 256 is the maximum length of a full path
char* oldname = strrchr(path, '/'); char* oldname = strrchr(path, '/');
if (!CheckDirWritePermissions(path)) return false; if (!CheckDirWritePermissions(path)) return false;
if (!oldname) return false; if (!oldname) return false;
oldname++; oldname++;
strncpy(npath, path, oldname - path); strncpy(npath, path, oldname - path);
strncpy(npath + (oldname - path), newname, 255 - (oldname - path)); strncpy(npath + (oldname - path), newname, 255 - (oldname - path));
if (fvx_rename(path, npath) != FR_OK) return false; if (fvx_rename(path, npath) != FR_OK) return false;
if ((strncasecmp(path, npath, 256) != 0) && if ((strncasecmp(path, npath, 256) != 0) &&
((fvx_stat(path, NULL) == FR_OK) || (fvx_stat(npath, NULL) != FR_OK))) ((fvx_stat(path, NULL) == FR_OK) || (fvx_stat(npath, NULL) != FR_OK)))
@ -808,20 +809,20 @@ bool FileSelectorWorker(char* result, const char* text, const char* path, const
char path_local[256]; char path_local[256];
strncpy(path_local, path, 256); strncpy(path_local, path, 256);
path_local[255] = '\0'; path_local[255] = '\0';
bool no_dirs = flags & NO_DIRS; bool no_dirs = flags & NO_DIRS;
bool no_files = flags & NO_FILES; bool no_files = flags & NO_FILES;
bool hide_ext = flags & HIDE_EXT; bool hide_ext = flags & HIDE_EXT;
bool select_dirs = flags & SELECT_DIRS; bool select_dirs = flags & SELECT_DIRS;
// main loop // main loop
while (true) { while (true) {
u32 n_found = 0; u32 n_found = 0;
u32 pos = 0; u32 pos = 0;
GetDirContents(contents, path_local); GetDirContents(contents, path_local);
while (pos < contents->n_entries) { while (pos < contents->n_entries) {
char opt_names[_MAX_FS_OPT+1][32+1]; char opt_names[_MAX_FS_OPT+1][UTF_BUFFER_BYTESIZE(32)];
DirEntry* res_entry[MAX_DIR_ENTRIES+1] = { NULL }; DirEntry* res_entry[MAX_DIR_ENTRIES+1] = { NULL };
u32 n_opt = 0; u32 n_opt = 0;
for (; pos < contents->n_entries; pos++) { for (; pos < contents->n_entries; pos++) {
@ -831,13 +832,13 @@ bool FileSelectorWorker(char* result, const char* text, const char* path, const
(entry->type == T_DOTDOT) || (strncmp(entry->name, "._", 2) == 0)) (entry->type == T_DOTDOT) || (strncmp(entry->name, "._", 2) == 0))
continue; continue;
if (!new_style && n_opt == _MAX_FS_OPT) { if (!new_style && n_opt == _MAX_FS_OPT) {
snprintf(opt_names[n_opt++], 32, "[more...]"); snprintf(opt_names[n_opt++], 32, "%s", STR_BRACKET_MORE);
break; break;
} }
if (!new_style) { if (!new_style) {
char temp_str[256]; char temp_str[256];
snprintf(temp_str, 256, "%s", entry->name); snprintf(temp_str, sizeof(temp_str), "%s", entry->name);
if (hide_ext && (entry->type == T_FILE)) { if (hide_ext && (entry->type == T_FILE)) {
char* dot = strrchr(temp_str, '.'); char* dot = strrchr(temp_str, '.');
if (dot) *dot = '\0'; if (dot) *dot = '\0';
@ -848,9 +849,9 @@ bool FileSelectorWorker(char* result, const char* text, const char* path, const
n_found++; n_found++;
} }
if ((pos >= contents->n_entries) && (n_opt < n_found) && !new_style) if ((pos >= contents->n_entries) && (n_opt < n_found) && !new_style)
snprintf(opt_names[n_opt++], 32, "[more...]"); snprintf(opt_names[n_opt++], 32, "%s", STR_BRACKET_MORE);
if (!n_opt) break; if (!n_opt) break;
const char* optionstr[_MAX_FS_OPT+1] = { NULL }; const char* optionstr[_MAX_FS_OPT+1] = { NULL };
for (u32 i = 0; i <= _MAX_FS_OPT; i++) optionstr[i] = opt_names[i]; for (u32 i = 0; i <= _MAX_FS_OPT; i++) optionstr[i] = opt_names[i];
u32 user_select = new_style ? ShowFileScrollPrompt(n_opt, (const DirEntry**)res_entry, hide_ext, "%s", text) u32 user_select = new_style ? ShowFileScrollPrompt(n_opt, (const DirEntry**)res_entry, hide_ext, "%s", text)
@ -871,10 +872,10 @@ bool FileSelectorWorker(char* result, const char* text, const char* path, const
} }
} }
if (!n_found) { // not a single matching entry found if (!n_found) { // not a single matching entry found
char pathstr[32+1]; char pathstr[UTF_BUFFER_BYTESIZE(32)];
TruncateString(pathstr, path_local, 32, 8); TruncateString(pathstr, path_local, 32, 8);
ShowPrompt(false, "%s\nNo usable entries found.", pathstr); ShowPrompt(false, "%s\n%s", pathstr, STR_NO_USABLE_ENTRIES_FOUND);
return false; return false;
} }
} }
} }
@ -882,7 +883,8 @@ bool FileSelectorWorker(char* result, const char* text, const char* path, const
bool FileSelector(char* result, const char* text, const char* path, const char* pattern, u32 flags, bool new_style) { bool FileSelector(char* result, const char* text, const char* path, const char* pattern, u32 flags, bool new_style) {
void* buffer = (void*) malloc(sizeof(DirStruct)); void* buffer = (void*) malloc(sizeof(DirStruct));
if (!buffer) return false; if (!buffer) return false;
// for this to work, result needs to be at least 256 bytes in size
bool ret = FileSelectorWorker(result, text, path, pattern, flags, buffer, new_style); bool ret = FileSelectorWorker(result, text, path, pattern, flags, buffer, new_style);
free(buffer); free(buffer);
return ret; return ret;

View File

@ -7,12 +7,13 @@
#define NO_CANCEL (1UL<<1) #define NO_CANCEL (1UL<<1)
#define SILENT (1UL<<2) #define SILENT (1UL<<2)
#define CALC_SHA (1UL<<3) #define CALC_SHA (1UL<<3)
#define BUILD_PATH (1UL<<4) #define USE_SHA1 (1UL<<4)
#define ALLOW_EXPAND (1UL<<5) #define BUILD_PATH (1UL<<5)
#define ASK_ALL (1UL<<6) #define ALLOW_EXPAND (1UL<<6)
#define SKIP_ALL (1UL<<7) #define ASK_ALL (1UL<<7)
#define OVERWRITE_ALL (1UL<<8) #define SKIP_ALL (1UL<<8)
#define APPEND_ALL (1UL<<9) #define OVERWRITE_ALL (1UL<<9)
#define APPEND_ALL (1UL<<10)
// file selector flags // file selector flags
#define NO_DIRS (1UL<<0) #define NO_DIRS (1UL<<0)
@ -43,7 +44,7 @@ size_t FileGetData(const char* path, void* data, size_t size, size_t foffset);
size_t FileGetSize(const char* path); size_t FileGetSize(const char* path);
/** Get SHA-256 of file **/ /** Get SHA-256 of file **/
bool FileGetSha256(const char* path, u8* sha256, u64 offset, u64 size); bool FileGetSha(const char* path, u8* hash, u64 offset, u64 size, bool sha1);
/** Find data in file **/ /** Find data in file **/
u32 FileFindData(const char* path, u8* data, u32 size_data, u32 offset_file); u32 FileFindData(const char* path, u8* data, u32 size_data, u32 offset_file);

View File

@ -17,7 +17,7 @@ int ReadImageBytes(void* buffer, u64 offset, u64 count) {
if (!mount_state) return FR_INVALID_OBJECT; if (!mount_state) return FR_INVALID_OBJECT;
if (fvx_tell(&mount_file) != offset) { if (fvx_tell(&mount_file) != offset) {
if (fvx_size(&mount_file) < offset) return -1; if (fvx_size(&mount_file) < offset) return -1;
fvx_lseek(&mount_file, offset); fvx_lseek(&mount_file, offset);
} }
ret = fvx_read(&mount_file, buffer, count, &bytes_read); ret = fvx_read(&mount_file, buffer, count, &bytes_read);
return (ret != 0) ? (int) ret : (bytes_read != count) ? -1 : 0; return (ret != 0) ? (int) ret : (bytes_read != count) ? -1 : 0;
@ -74,6 +74,6 @@ u64 MountImage(const char* path) {
return 0; return 0;
fvx_lseek(&mount_file, 0); fvx_lseek(&mount_file, 0);
fvx_sync(&mount_file); fvx_sync(&mount_file);
strncpy(mount_path, path, 255); strncpy(mount_path, path, 256);
return (mount_state = type); return (mount_state = type);
} }

View File

@ -35,14 +35,14 @@ int alias_num (const TCHAR* path) {
void dealias_path (TCHAR* alias, const TCHAR* path) { void dealias_path (TCHAR* alias, const TCHAR* path) {
int num = alias_num(path); int num = alias_num(path);
u32 p_offs = (path[2] == '/' && ((path[3] == '/') || (path[3] == '\0'))) ? 3 : 2; u32 p_offs = (path[2] == '/' && ((path[3] == '/') || (path[3] == '\0'))) ? 3 : 2;
if (num >= 0) // set alias (alias is assumed to be 256 byte) if (num >= 0) // set alias (alias is assumed to be 256 byte!)
snprintf(alias, 256, "%s%s", alias_path[num], path + p_offs); snprintf(alias, 256, "%s%s", alias_path[num], path + p_offs);
else strncpy(alias, path, 256); else snprintf(alias, 256, "%s", path);
} }
FilCryptInfo* fx_find_cryptinfo(FIL* fptr) { FilCryptInfo* fx_find_cryptinfo(FIL* fptr) {
FilCryptInfo* info = NULL; FilCryptInfo* info = NULL;
for (u32 i = 0; i < NUM_FILCRYPTINFO; i++) { for (u32 i = 0; i < NUM_FILCRYPTINFO; i++) {
if (!info && !filcrypt[i].fptr) // use first free if (!info && !filcrypt[i].fptr) // use first free
info = &filcrypt[i]; info = &filcrypt[i];
@ -51,7 +51,7 @@ FilCryptInfo* fx_find_cryptinfo(FIL* fptr) {
break; break;
} }
} }
return info; return info;
} }
@ -59,27 +59,27 @@ FRESULT fx_decrypt_dsiware (FIL* fp, void* buff, FSIZE_t ofs, UINT len) {
const u32 mode = AES_CNT_TITLEKEY_DECRYPT_MODE; const u32 mode = AES_CNT_TITLEKEY_DECRYPT_MODE;
const u32 num_tbl = sizeof(TadContentTable) / sizeof(u32); const u32 num_tbl = sizeof(TadContentTable) / sizeof(u32);
const FSIZE_t ofs0 = f_tell(fp); const FSIZE_t ofs0 = f_tell(fp);
u8 __attribute__((aligned(16))) iv[AES_BLOCK_SIZE]; u8 __attribute__((aligned(16))) iv[AES_BLOCK_SIZE];
u32 tbl[num_tbl]; u32 tbl[num_tbl];
u8 hdr[TAD_HEADER_LEN]; u8 hdr[TAD_HEADER_LEN];
FRESULT res; FRESULT res;
UINT br; UINT br;
// read and decrypt header // read and decrypt header
if ((res = f_lseek(fp, TAD_HEADER_OFFSET)) != FR_OK) return res; if ((res = f_lseek(fp, TAD_HEADER_OFFSET)) != FR_OK) return res;
if ((res = f_read(fp, hdr, TAD_HEADER_LEN, &br)) != FR_OK) return res; if ((res = f_read(fp, hdr, TAD_HEADER_LEN, &br)) != FR_OK) return res;
if (br != TAD_HEADER_LEN) return FR_DENIED; if (br != TAD_HEADER_LEN) return FR_DENIED;
memcpy(iv, hdr + TAD_HEADER_LEN - AES_BLOCK_SIZE, AES_BLOCK_SIZE); memcpy(iv, hdr + TAD_HEADER_LEN - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
cbc_decrypt(hdr, hdr, sizeof(TadHeader) / AES_BLOCK_SIZE, mode, iv); cbc_decrypt(hdr, hdr, sizeof(TadHeader) / AES_BLOCK_SIZE, mode, iv);
// setup the table // setup the table
if (BuildTadContentTable(tbl, hdr) != 0) return FR_DENIED; if (BuildTadContentTable(tbl, hdr) != 0) return FR_DENIED;
if (tbl[num_tbl-1] > f_size(fp)) return FR_DENIED; // obviously missing data if (tbl[num_tbl-1] > f_size(fp)) return FR_DENIED; // obviously missing data
// process sections // process sections
u32 sct_start = 0; u32 sct_start = 0;
u32 sct_end = 0; u32 sct_end = 0;
@ -87,21 +87,21 @@ FRESULT fx_decrypt_dsiware (FIL* fp, void* buff, FSIZE_t ofs, UINT len) {
sct_end = tbl[i]; sct_end = tbl[i];
if (sct_start == sct_end) continue; // nothing in section if (sct_start == sct_end) continue; // nothing in section
if ((ofs + len <= sct_start) || (ofs >= sct_end)) continue; // section not in data if ((ofs + len <= sct_start) || (ofs >= sct_end)) continue; // section not in data
const u32 crypt_end = sct_end - (AES_BLOCK_SIZE * 2); const u32 crypt_end = sct_end - (AES_BLOCK_SIZE * 2);
const u32 data_end = min(crypt_end, ofs + len); const u32 data_end = min(crypt_end, ofs + len);
u32 data_pos = max(ofs, sct_start); u32 data_pos = max(ofs, sct_start);
if (ofs >= crypt_end) continue; // nothing to do if (ofs >= crypt_end) continue; // nothing to do
if ((sct_start < ofs) || (sct_end > ofs + len)) { // incomplete section, ugh if ((sct_start < ofs) || (sct_end > ofs + len)) { // incomplete section, ugh
u8 __attribute__((aligned(16))) block[AES_BLOCK_SIZE]; u8 __attribute__((aligned(16))) block[AES_BLOCK_SIZE];
// load iv0 // load iv0
FSIZE_t block0_ofs = data_pos - (data_pos % AES_BLOCK_SIZE); FSIZE_t block0_ofs = data_pos - (data_pos % AES_BLOCK_SIZE);
FSIZE_t iv0_ofs = ((block0_ofs > sct_start) ? block0_ofs : sct_end) - AES_BLOCK_SIZE; FSIZE_t iv0_ofs = ((block0_ofs > sct_start) ? block0_ofs : sct_end) - AES_BLOCK_SIZE;
if ((res = f_lseek(fp, iv0_ofs)) != FR_OK) return res; if ((res = f_lseek(fp, iv0_ofs)) != FR_OK) return res;
if ((res = f_read(fp, iv, AES_BLOCK_SIZE, &br)) != FR_OK) return res; if ((res = f_read(fp, iv, AES_BLOCK_SIZE, &br)) != FR_OK) return res;
// load and decrypt block0 (if misaligned) // load and decrypt block0 (if misaligned)
if (data_pos % AES_BLOCK_SIZE) { if (data_pos % AES_BLOCK_SIZE) {
if ((res = f_lseek(fp, block0_ofs)) != FR_OK) return res; if ((res = f_lseek(fp, block0_ofs)) != FR_OK) return res;
@ -110,7 +110,7 @@ FRESULT fx_decrypt_dsiware (FIL* fp, void* buff, FSIZE_t ofs, UINT len) {
data_pos = min(block0_ofs + AES_BLOCK_SIZE, data_end); data_pos = min(block0_ofs + AES_BLOCK_SIZE, data_end);
memcpy(buff, block + (ofs - block0_ofs), data_pos - ofs); memcpy(buff, block + (ofs - block0_ofs), data_pos - ofs);
} }
// decrypt blocks in between // decrypt blocks in between
u32 num_blocks = (data_end - data_pos) / AES_BLOCK_SIZE; u32 num_blocks = (data_end - data_pos) / AES_BLOCK_SIZE;
if (num_blocks) { if (num_blocks) {
@ -118,7 +118,7 @@ FRESULT fx_decrypt_dsiware (FIL* fp, void* buff, FSIZE_t ofs, UINT len) {
cbc_decrypt(blocks, blocks, num_blocks, mode, iv); cbc_decrypt(blocks, blocks, num_blocks, mode, iv);
data_pos += num_blocks * AES_BLOCK_SIZE; data_pos += num_blocks * AES_BLOCK_SIZE;
} }
// decrypt last block // decrypt last block
if (data_pos < data_end) { if (data_pos < data_end) {
u8* lbuff = (u8*) buff + (data_pos - ofs); u8* lbuff = (u8*) buff + (data_pos - ofs);
@ -137,7 +137,7 @@ FRESULT fx_decrypt_dsiware (FIL* fp, void* buff, FSIZE_t ofs, UINT len) {
cbc_decrypt(blocks, blocks, num_blocks, mode, iv); cbc_decrypt(blocks, blocks, num_blocks, mode, iv);
} }
} }
return f_lseek(fp, ofs0); return f_lseek(fp, ofs0);
} }
@ -145,7 +145,7 @@ FRESULT fx_open (FIL* fp, const TCHAR* path, BYTE mode) {
int num = alias_num(path); int num = alias_num(path);
FilCryptInfo* info = fx_find_cryptinfo(fp); FilCryptInfo* info = fx_find_cryptinfo(fp);
if (info) info->fptr = NULL; if (info) info->fptr = NULL;
if (info && (num >= 0)) { if (info && (num >= 0)) {
// DSIWare Export, mark with the magic number // DSIWare Export, mark with the magic number
if (strncmp(path + 2, "/" DSIWARE_MAGIC, 1 + 16) == 0) { if (strncmp(path + 2, "/" DSIWARE_MAGIC, 1 + 16) == 0) {
@ -172,7 +172,7 @@ FRESULT fx_open (FIL* fp, const TCHAR* path, BYTE mode) {
memcpy(info->keyy, sd_keyy[num], 16); memcpy(info->keyy, sd_keyy[num], 16);
info->fptr = fp; info->fptr = fp;
} }
return fa_open(fp, path, mode); return fa_open(fp, path, mode);
} }
@ -193,12 +193,12 @@ FRESULT fx_write (FIL* fp, const void* buff, UINT btw, UINT* bw) {
FilCryptInfo* info = fx_find_cryptinfo(fp); FilCryptInfo* info = fx_find_cryptinfo(fp);
FSIZE_t off = f_tell(fp); FSIZE_t off = f_tell(fp);
FRESULT res = FR_OK; FRESULT res = FR_OK;
if (info && info->fptr) { if (info && info->fptr) {
if (memcmp(info->ctr, DSIWARE_MAGIC, 16) == 0) return FR_DENIED; if (memcmp(info->ctr, DSIWARE_MAGIC, 16) == 0) return FR_DENIED;
void* crypt_buff = (void*) malloc(min(btw, STD_BUFFER_SIZE)); void* crypt_buff = (void*) malloc(min(btw, STD_BUFFER_SIZE));
if (!crypt_buff) return FR_DENIED; if (!crypt_buff) return FR_DENIED;
setup_aeskeyY(0x34, info->keyy); setup_aeskeyY(0x34, info->keyy);
use_aeskey(0x34); use_aeskey(0x34);
*bw = 0; *bw = 0;
@ -210,7 +210,7 @@ FRESULT fx_write (FIL* fp, const void* buff, UINT btw, UINT* bw) {
res = f_write(fp, (const void*) crypt_buff, pcount, &bwl); res = f_write(fp, (const void*) crypt_buff, pcount, &bwl);
*bw += bwl; *bw += bwl;
} }
free(crypt_buff); free(crypt_buff);
} else res = f_write(fp, buff, btw, bw); } else res = f_write(fp, buff, btw, bw);
return res; return res;
@ -255,12 +255,12 @@ FRESULT fa_unlink (const TCHAR* path) {
// special functions for access of virtual NAND SD drives // special functions for access of virtual NAND SD drives
bool SetupNandSdDrive(const char* path, const char* sd_path, const char* movable, int num) { bool SetupNandSdDrive(const char* path, const char* sd_path, const char* movable, int num) {
char alias[128]; char alias[128];
// initial checks // initial checks
if ((num >= NUM_ALIAS_DRV) || (num < 0)) return false; if ((num >= NUM_ALIAS_DRV) || (num < 0)) return false;
alias_drv[num] = 0; alias_drv[num] = 0;
if (!sd_path || !movable || !path) return true; if (!sd_path || !movable || !path) return true;
// grab the key Y from movable.sed // grab the key Y from movable.sed
UINT bytes_read = 0; UINT bytes_read = 0;
FIL file; FIL file;
@ -272,13 +272,13 @@ bool SetupNandSdDrive(const char* path, const char* sd_path, const char* movable
return false; return false;
} }
f_close(&file); f_close(&file);
// build the alias path (id0) // build the alias path (id0)
u32 sha256sum[8]; u32 sha256sum[8];
sha_quick(sha256sum, sd_keyy[num], 0x10, SHA256_MODE); sha_quick(sha256sum, sd_keyy[num], 0x10, SHA256_MODE);
snprintf(alias, 127, "%s/Nintendo 3DS/%08lX%08lX%08lX%08lX", snprintf(alias, sizeof(alias), "%s/Nintendo 3DS/%08lX%08lX%08lX%08lX",
sd_path, sha256sum[0], sha256sum[1], sha256sum[2], sha256sum[3]); sd_path, sha256sum[0], sha256sum[1], sha256sum[2], sha256sum[3]);
// find the alias path (id1) // find the alias path (id1)
char* id1 = alias + strnlen(alias, 127); char* id1 = alias + strnlen(alias, 127);
DIR pdir; DIR pdir;
@ -297,7 +297,7 @@ bool SetupNandSdDrive(const char* path, const char* sd_path, const char* movable
} }
f_closedir(&pdir); f_closedir(&pdir);
if (!(*id1)) return false; if (!(*id1)) return false;
// create the alias drive // create the alias drive
return SetupAliasDrive(path, alias, num); return SetupAliasDrive(path, alias, num);
} }
@ -307,12 +307,12 @@ bool SetupAliasDrive(const char* path, const char* alias, int num) {
if ((num >= NUM_ALIAS_DRV) || (num < 0)) return false; if ((num >= NUM_ALIAS_DRV) || (num < 0)) return false;
alias_drv[num] = 0; alias_drv[num] = 0;
if (!alias || !path) return true; if (!alias || !path) return true;
// take over drive path and alias // take over drive path and alias
strncpy(alias_path[num], alias, 128); strncpy(alias_path[num], alias, 128);
if (path[1] != ':') return false; if (path[1] != ':') return false;
alias_drv[num] = path[0]; alias_drv[num] = path[0];
return true; return true;
} }

View File

@ -12,16 +12,16 @@ bool CheckSupportFile(const char* fname)
// try VRAM0 first // try VRAM0 first
if (FindVTarFileInfo(fname, NULL)) if (FindVTarFileInfo(fname, NULL))
return true; return true;
// try support file paths // try support file paths
const char* base_paths[] = { SUPPORT_FILE_PATHS }; const char* base_paths[] = { SUPPORT_FILE_PATHS };
for (u32 i = 0; i < countof(base_paths); i++) { for (u32 i = 0; i < countof(base_paths); i++) {
char path[256]; char path[256];
snprintf(path, 256, "%s/%s", base_paths[i], fname); snprintf(path, sizeof(path), "%s/%s", base_paths[i], fname);
if (fvx_stat(path, NULL) == FR_OK) if (fvx_stat(path, NULL) == FR_OK)
return true; return true;
} }
return false; return false;
} }
@ -34,17 +34,17 @@ size_t LoadSupportFile(const char* fname, void* buffer, size_t max_len)
memcpy(buffer, data, len64); memcpy(buffer, data, len64);
return (size_t) len64; return (size_t) len64;
} }
// try support file paths // try support file paths
const char* base_paths[] = { SUPPORT_FILE_PATHS }; const char* base_paths[] = { SUPPORT_FILE_PATHS };
for (u32 i = 0; i < countof(base_paths); i++) { for (u32 i = 0; i < countof(base_paths); i++) {
UINT len32; UINT len32;
char path[256]; char path[256];
snprintf(path, 256, "%s/%s", base_paths[i], fname); snprintf(path, sizeof(path), "%s/%s", base_paths[i], fname);
if (fvx_qread(path, buffer, 0, max_len, &len32) == FR_OK) if (fvx_qread(path, buffer, 0, max_len, &len32) == FR_OK)
return len32; return len32;
} }
return 0; return 0;
} }
@ -68,12 +68,12 @@ bool SaveSupportFile(const char* fname, void* buffer, size_t len)
// write support file // write support file
if (idx >= 0) { if (idx >= 0) {
char path[256]; char path[256];
snprintf(path, 256, "%s/%s", base_paths[idx], fname); snprintf(path, sizeof(path), "%s/%s", base_paths[idx], fname);
fvx_unlink(path); fvx_unlink(path);
if (fvx_qwrite(path, buffer, 0, len, NULL) == FR_OK) if (fvx_qwrite(path, buffer, 0, len, NULL) == FR_OK)
return true; return true;
} }
return false; return false;
} }
@ -102,7 +102,7 @@ bool GetSupportDir(char* path, const char* dname)
if ((fvx_stat(path, &fno) == FR_OK) && (fno.fattrib & AM_DIR)) if ((fvx_stat(path, &fno) == FR_OK) && (fno.fattrib & AM_DIR))
return true; return true;
} }
return false; return false;
} }
@ -115,6 +115,7 @@ bool CheckSupportDir(const char* dname)
bool FileSelectorSupport(char* result, const char* text, const char* dname, const char* pattern) bool FileSelectorSupport(char* result, const char* text, const char* dname, const char* pattern)
{ {
char path[256]; char path[256];
// result needs to be at least 256 bytes long for this to work!
if (!GetSupportDir(path, dname)) return false; if (!GetSupportDir(path, dname)) return false;
return FileSelector(result, text, path, pattern, HIDE_EXT, false); return FileSelector(result, text, path, pattern, HIDE_EXT, false);
} }

View File

@ -3,7 +3,9 @@
#include "common.h" #include "common.h"
// scripts / payloads dir names // scripts / payloads dir names
#define LANGUAGES_DIR "languages"
#define SCRIPTS_DIR "scripts" #define SCRIPTS_DIR "scripts"
#define LUASCRIPTS_DIR "luascripts"
#define PAYLOADS_DIR "payloads" #define PAYLOADS_DIR "payloads"
bool CheckSupportFile(const char* fname); bool CheckSupportFile(const char* fname);
@ -11,5 +13,6 @@ size_t LoadSupportFile(const char* fname, void* buffer, size_t max_len);
bool SaveSupportFile(const char* fname, void* buffer, size_t len); bool SaveSupportFile(const char* fname, void* buffer, size_t len);
bool SetAsSupportFile(const char* fname, const char* source); bool SetAsSupportFile(const char* fname, const char* source);
bool GetSupportDir(char* path, const char* dname);
bool CheckSupportDir(const char* fpath); bool CheckSupportDir(const char* fpath);
bool FileSelectorSupport(char* result, const char* text, const char* dname, const char* pattern); bool FileSelectorSupport(char* result, const char* text, const char* dname, const char* pattern);

View File

@ -151,22 +151,22 @@ FRESULT fvx_qread (const TCHAR* path, void* buff, FSIZE_t ofs, UINT btr, UINT* b
FIL fp; FIL fp;
FRESULT res; FRESULT res;
UINT brt = 0; UINT brt = 0;
res = fvx_open(&fp, path, FA_READ | FA_OPEN_EXISTING); res = fvx_open(&fp, path, FA_READ | FA_OPEN_EXISTING);
if (res != FR_OK) return res; if (res != FR_OK) return res;
res = fvx_lseek(&fp, ofs); res = fvx_lseek(&fp, ofs);
if (res != FR_OK) { if (res != FR_OK) {
fvx_close(&fp); fvx_close(&fp);
return res; return res;
} }
res = fvx_read(&fp, buff, btr, &brt); res = fvx_read(&fp, buff, btr, &brt);
fvx_close(&fp); fvx_close(&fp);
if (br) *br = brt; if (br) *br = brt;
else if ((res == FR_OK) && (brt != btr)) res = FR_DENIED; else if ((res == FR_OK) && (brt != btr)) res = FR_DENIED;
return res; return res;
} }
@ -174,29 +174,29 @@ FRESULT fvx_qwrite (const TCHAR* path, const void* buff, FSIZE_t ofs, UINT btw,
FIL fp; FIL fp;
FRESULT res; FRESULT res;
UINT bwt = 0; UINT bwt = 0;
res = fvx_open(&fp, path, FA_WRITE | FA_OPEN_ALWAYS); res = fvx_open(&fp, path, FA_WRITE | FA_OPEN_ALWAYS);
if (res != FR_OK) return res; if (res != FR_OK) return res;
res = fvx_lseek(&fp, ofs); res = fvx_lseek(&fp, ofs);
if (res != FR_OK) { if (res != FR_OK) {
fvx_close(&fp); fvx_close(&fp);
return res; return res;
} }
res = fvx_write(&fp, buff, btw, &bwt); res = fvx_write(&fp, buff, btw, &bwt);
fvx_close(&fp); fvx_close(&fp);
if (bw) *bw = bwt; if (bw) *bw = bwt;
else if ((res == FR_OK) && (bwt != btw)) res = FR_DENIED; else if ((res == FR_OK) && (bwt != btw)) res = FR_DENIED;
return res; return res;
} }
FRESULT fvx_qcreate (const TCHAR* path, UINT btc) { FRESULT fvx_qcreate (const TCHAR* path, UINT btc) {
FIL fp; FIL fp;
FRESULT res; FRESULT res;
res = fvx_open(&fp, path, FA_WRITE | FA_CREATE_ALWAYS); res = fvx_open(&fp, path, FA_WRITE | FA_CREATE_ALWAYS);
if (res != FR_OK) return res; if (res != FR_OK) return res;
@ -212,7 +212,7 @@ FRESULT fvx_qfill (const TCHAR* path, const void* buff, UINT btb) {
FRESULT res; FRESULT res;
UINT bwtt = 0; UINT bwtt = 0;
UINT fsiz = 0; UINT fsiz = 0;
res = fvx_open(&fp, path, FA_WRITE | FA_OPEN_EXISTING); res = fvx_open(&fp, path, FA_WRITE | FA_OPEN_EXISTING);
if (res != FR_OK) return res; if (res != FR_OK) return res;
@ -257,7 +257,8 @@ FRESULT worker_fvx_rmkdir (TCHAR* tpath) {
FRESULT fvx_rmkdir (const TCHAR* path) { FRESULT fvx_rmkdir (const TCHAR* path) {
#if !_LFN_UNICODE // this will not work for unicode #if !_LFN_UNICODE // this will not work for unicode
TCHAR tpath[_MAX_FN_LEN+1]; TCHAR tpath[_MAX_FN_LEN+1];
strncpy(tpath, path, _MAX_FN_LEN); if (strlen(path) > _MAX_FN_LEN) return FR_INVALID_NAME;
strcpy(tpath, path);
return worker_fvx_rmkdir( tpath ); return worker_fvx_rmkdir( tpath );
#else #else
return FR_DENIED; return FR_DENIED;
@ -267,7 +268,8 @@ FRESULT fvx_rmkdir (const TCHAR* path) {
FRESULT fvx_rmkpath (const TCHAR* path) { FRESULT fvx_rmkpath (const TCHAR* path) {
#if !_LFN_UNICODE // this will not work for unicode #if !_LFN_UNICODE // this will not work for unicode
TCHAR tpath[_MAX_FN_LEN+1]; TCHAR tpath[_MAX_FN_LEN+1];
strncpy(tpath, path, _MAX_FN_LEN); if (strlen(path) > _MAX_FN_LEN) return FR_INVALID_NAME;
strcpy(tpath, path);
TCHAR* slash = strrchr(tpath, '/'); TCHAR* slash = strrchr(tpath, '/');
if (!slash) return FR_DENIED; if (!slash) return FR_DENIED;
*slash = '\0'; *slash = '\0';
@ -281,21 +283,21 @@ FRESULT fvx_rmkpath (const TCHAR* path) {
FRESULT worker_fvx_runlink (TCHAR* tpath) { FRESULT worker_fvx_runlink (TCHAR* tpath) {
FILINFO fno; FILINFO fno;
FRESULT res; FRESULT res;
// this code handles directory content deletion // this code handles directory content deletion
if ((res = fvx_stat(tpath, &fno)) != FR_OK) return res; // tpath does not exist if ((res = fvx_stat(tpath, &fno)) != FR_OK) return res; // tpath does not exist
if (fno.fattrib & AM_DIR) { // process folder contents if (fno.fattrib & AM_DIR) { // process folder contents
DIR pdir; DIR pdir;
TCHAR* fname = tpath + strnlen(tpath, 255); TCHAR* fname = tpath + strnlen(tpath, 255);
if ((res = fa_opendir(&pdir, tpath)) != FR_OK) return res; if ((res = fa_opendir(&pdir, tpath)) != FR_OK) return res;
*(fname++) = '/'; *(fname++) = '/';
while (fvx_readdir(&pdir, &fno) == FR_OK) { while (fvx_readdir(&pdir, &fno) == FR_OK) {
if ((strncmp(fno.fname, ".", 2) == 0) || (strncmp(fno.fname, "..", 3) == 0)) if ((strncmp(fno.fname, ".", 2) == 0) || (strncmp(fno.fname, "..", 3) == 0))
continue; // filter out virtual entries continue; // filter out virtual entries
strncpy(fname, fno.fname, tpath + 255 - fname); strcpy(fname, fno.fname);
if (fno.fname[0] == 0) { if (fno.fname[0] == 0) {
break; break;
} else { // return value won't matter } else { // return value won't matter
@ -305,7 +307,7 @@ FRESULT worker_fvx_runlink (TCHAR* tpath) {
fvx_closedir(&pdir); fvx_closedir(&pdir);
*(--fname) = '\0'; *(--fname) = '\0';
} }
return fvx_unlink( tpath ); return fvx_unlink( tpath );
} }
#endif #endif
@ -313,7 +315,8 @@ FRESULT worker_fvx_runlink (TCHAR* tpath) {
FRESULT fvx_runlink (const TCHAR* path) { FRESULT fvx_runlink (const TCHAR* path) {
#if !_LFN_UNICODE // this will not work for unicode #if !_LFN_UNICODE // this will not work for unicode
TCHAR tpath[_MAX_FN_LEN+1]; TCHAR tpath[_MAX_FN_LEN+1];
strncpy(tpath, path, _MAX_FN_LEN); if (strlen(path) > _MAX_FN_LEN) return FR_INVALID_NAME;
strcpy(tpath, path);
return worker_fvx_runlink( tpath ); return worker_fvx_runlink( tpath );
#else #else
return FR_DENIED; return FR_DENIED;
@ -344,7 +347,7 @@ FRESULT fvx_match_name(const TCHAR* path, const TCHAR* pattern) {
if (fvx_match_name(path, pattern + 1) == FR_OK) return FR_OK; if (fvx_match_name(path, pattern + 1) == FR_OK) return FR_OK;
} }
} }
return FR_NO_FILE; return FR_NO_FILE;
} }
@ -356,40 +359,42 @@ FRESULT fvx_preaddir (DIR* dp, FILINFO* fno, const TCHAR* pattern) {
} }
FRESULT fvx_findpath (TCHAR* path, const TCHAR* pattern, BYTE mode) { FRESULT fvx_findpath (TCHAR* path, const TCHAR* pattern, BYTE mode) {
strncpy(path, pattern, _MAX_FN_LEN); if (strlen(pattern) > _MAX_FN_LEN) return FR_INVALID_NAME;
strcpy(path, pattern);
TCHAR* fname = strrchr(path, '/'); TCHAR* fname = strrchr(path, '/');
if (!fname) return FR_DENIED; if (!fname) return FR_DENIED;
*fname = '\0'; *fname = '\0';
TCHAR* npattern = strrchr(pattern, '/'); TCHAR* npattern = strrchr(pattern, '/');
if (!npattern) return FR_DENIED; if (!npattern) return FR_DENIED;
npattern++; npattern++;
DIR pdir; DIR pdir;
FILINFO fno; FILINFO fno;
FRESULT res; FRESULT res;
if ((res = fvx_opendir(&pdir, path)) != FR_OK) return res; if ((res = fvx_opendir(&pdir, path)) != FR_OK) return res;
*(fname++) = '/'; *(fname++) = '/';
*fname = '\0'; *fname = '\0';
while ((fvx_preaddir(&pdir, &fno, npattern) == FR_OK) && *(fno.fname)) { while ((fvx_preaddir(&pdir, &fno, npattern) == FR_OK) && *(fno.fname)) {
int cmp = strncmp(fno.fname, fname, _MAX_FN_LEN); int cmp = strncmp(fno.fname, fname, _MAX_FN_LEN);
if (((mode & FN_HIGHEST) && (cmp > 0)) || ((mode & FN_LOWEST) && (cmp < 0)) || !(*fname)) if (((mode & FN_HIGHEST) && (cmp > 0)) || ((mode & FN_LOWEST) && (cmp < 0)) || !(*fname))
strncpy(fname, fno.fname, _MAX_FN_LEN - (fname - path)); strcpy(fname, fno.fname);
if (!(mode & (FN_HIGHEST|FN_LOWEST))) break; if (!(mode & (FN_HIGHEST|FN_LOWEST))) break;
} }
fvx_closedir( &pdir ); fvx_closedir( &pdir );
return (*fname) ? FR_OK : FR_NO_PATH; return (*fname) ? FR_OK : FR_NO_PATH;
} }
FRESULT fvx_findnopath (TCHAR* path, const TCHAR* pattern) { FRESULT fvx_findnopath (TCHAR* path, const TCHAR* pattern) {
strncpy(path, pattern, _MAX_FN_LEN); if (strlen(pattern) > _MAX_FN_LEN) return FR_INVALID_NAME;
strcpy(path, pattern);
TCHAR* fname = strrchr(path, '/'); TCHAR* fname = strrchr(path, '/');
if (!fname) return FR_DENIED; if (!fname) return FR_DENIED;
fname++; fname++;
TCHAR* rep[16]; TCHAR* rep[16];
u32 n_rep = 0; u32 n_rep = 0;
for (u32 i = 0; fname[i]; i++) { for (u32 i = 0; fname[i]; i++) {
@ -400,7 +405,7 @@ FRESULT fvx_findnopath (TCHAR* path, const TCHAR* pattern) {
if (n_rep >= 16) return FR_DENIED; if (n_rep >= 16) return FR_DENIED;
} }
if (!n_rep) return (fvx_stat(path, NULL) == FR_OK) ? FR_NO_PATH : FR_OK; if (!n_rep) return (fvx_stat(path, NULL) == FR_OK) ? FR_NO_PATH : FR_OK;
while (fvx_stat(path, NULL) == FR_OK) { while (fvx_stat(path, NULL) == FR_OK) {
for (int i = n_rep - 1; (i >= 0); i--) { for (int i = n_rep - 1; (i >= 0); i--) {
if (*(rep[i]) == '9') { if (*(rep[i]) == '9') {
@ -412,7 +417,7 @@ FRESULT fvx_findnopath (TCHAR* path, const TCHAR* pattern) {
} }
} }
} }
return FR_OK; return FR_OK;
} }

View File

@ -8,6 +8,7 @@
#define fvx_tell(fp) ((fp)->fptr) #define fvx_tell(fp) ((fp)->fptr)
#define fvx_size(fp) ((fp)->obj.objsize) #define fvx_size(fp) ((fp)->obj.objsize)
#define fvx_eof(fp) (fvx_tell(fp) == fvx_size(fp))
#define FN_ANY 0x00 #define FN_ANY 0x00
#define FN_HIGHEST 0x01 #define FN_HIGHEST 0x01

View File

@ -2,6 +2,7 @@
#include "vff.h" #include "vff.h"
#define FAT_ENTRY_SIZE 2 * sizeof(u32) #define FAT_ENTRY_SIZE 2 * sizeof(u32)
#define REPLACE_SIZE_MISMATCH 2
#define getfatflag(uv) (((uv) & 0x80000000UL) != 0) #define getfatflag(uv) (((uv) & 0x80000000UL) != 0)
#define getfatindex(uv) ((uv) & 0x7FFFFFFFUL) #define getfatindex(uv) ((uv) & 0x7FFFFFFFUL)
@ -105,8 +106,8 @@ static FRESULT BDRIWrite(UINT ofs, UINT btw, const void* buf) {
bool CheckDBMagic(const u8* pre_header, bool tickdb) { bool CheckDBMagic(const u8* pre_header, bool tickdb) {
const TitleDBPreHeader* title = (TitleDBPreHeader*) pre_header; const TitleDBPreHeader* title = (TitleDBPreHeader*) pre_header;
const TickDBPreHeader* tick = (TickDBPreHeader*) pre_header; const TickDBPreHeader* tick = (TickDBPreHeader*) pre_header;
return (tickdb ? ((strncmp(tick->magic, "TICK", 4) == 0) && (tick->unknown1 == 1)) : return (tickdb ? ((strncmp(tick->magic, "TICK", 4) == 0) && (tick->unknown1 == 1)) :
((strcmp(title->magic, "NANDIDB") == 0) || (strcmp(title->magic, "NANDTDB") == 0) || ((strcmp(title->magic, "NANDIDB") == 0) || (strcmp(title->magic, "NANDTDB") == 0) ||
(strcmp(title->magic, "TEMPIDB") == 0) || (strcmp(title->magic, "TEMPTDB") == 0))) && (strcmp(title->magic, "TEMPIDB") == 0) || (strcmp(title->magic, "TEMPTDB") == 0))) &&
(strcmp((tickdb ? tick->fs_header : title->fs_header).magic, "BDRI") == 0) && (strcmp((tickdb ? tick->fs_header : title->fs_header).magic, "BDRI") == 0) &&
@ -133,13 +134,13 @@ static u32 GetBDRIEntrySize(const BDRIFsHeader* fs_header, const u32 fs_header_o
const u32 data_offset = fs_header_offset + fs_header->data_offset; 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 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 fht_offset = fs_header_offset + fs_header->fht_offset;
u32 index = 0; u32 index = 0;
TdbFileEntry file_entry; TdbFileEntry file_entry;
u64 tid_be = getbe64(title_id); u64 tid_be = getbe64(title_id);
u8* title_id_be = (u8*) &tid_be; u8* title_id_be = (u8*) &tid_be;
const u32 hash_bucket = GetHashBucket(title_id_be, 1, fs_header->fht_bucket_count); 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) if (BDRIRead(fht_offset + hash_bucket * sizeof(u32), sizeof(u32), &(file_entry.hash_bucket_next_index)) != FR_OK)
return 1; return 1;
@ -147,13 +148,13 @@ static u32 GetBDRIEntrySize(const BDRIFsHeader* fs_header, const u32 fs_header_o
do { do {
if (file_entry.hash_bucket_next_index == 0) if (file_entry.hash_bucket_next_index == 0)
return 1; return 1;
index = file_entry.hash_bucket_next_index; index = file_entry.hash_bucket_next_index;
if (BDRIRead(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK) if (BDRIRead(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
return 1; return 1;
} while (memcmp(title_id_be, file_entry.title_id, 8) != 0); } while (memcmp(title_id_be, file_entry.title_id, 8) != 0);
*size = file_entry.size; *size = file_entry.size;
return 0; return 0;
@ -162,18 +163,18 @@ static u32 GetBDRIEntrySize(const BDRIFsHeader* fs_header, const u32 fs_header_o
static u32 ReadBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offset, const u8* title_id, u8* entry, const u32 expected_size) { 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 if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count)) // Could be more thorough
return 1; return 1;
const u32 data_offset = fs_header_offset + fs_header->data_offset; 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 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 fht_offset = fs_header_offset + fs_header->fht_offset;
const u32 fat_offset = fs_header_offset + fs_header->fat_offset; const u32 fat_offset = fs_header_offset + fs_header->fat_offset;
u32 index = 0; u32 index = 0;
TdbFileEntry file_entry; TdbFileEntry file_entry;
u64 tid_be = getbe64(title_id); u64 tid_be = getbe64(title_id);
u8* title_id_be = (u8*) &tid_be; u8* title_id_be = (u8*) &tid_be;
const u32 hash_bucket = GetHashBucket(title_id_be, 1, fs_header->fht_bucket_count); 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) if (BDRIRead(fht_offset + hash_bucket * sizeof(u32), sizeof(u32), &(file_entry.hash_bucket_next_index)) != FR_OK)
return 1; return 1;
@ -181,108 +182,108 @@ static u32 ReadBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offs
do { do {
if (file_entry.hash_bucket_next_index == 0) if (file_entry.hash_bucket_next_index == 0)
return 1; return 1;
index = file_entry.hash_bucket_next_index; index = file_entry.hash_bucket_next_index;
if (BDRIRead(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK) if (BDRIRead(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
return 1; return 1;
} while (memcmp(title_id_be, file_entry.title_id, 8) != 0); } while (memcmp(title_id_be, file_entry.title_id, 8) != 0);
if (expected_size && (file_entry.size != expected_size)) if (expected_size && (file_entry.size != expected_size))
return 1; return 1;
index = file_entry.start_block_index + 1; // FAT entry index index = file_entry.start_block_index + 1; // FAT entry index
u32 bytes_read = 0; u32 bytes_read = 0;
u32 fat_entry[2]; u32 fat_entry[2];
while (bytes_read < file_entry.size) { // Read the full entry, walking the FAT node chain while (bytes_read < file_entry.size) { // Read the full entry, walking the FAT node chain
u32 read_start = index - 1; // Data region block index u32 read_start = index - 1; // Data region block index
u32 read_count = 0; u32 read_count = 0;
if (BDRIRead(fat_offset + index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK) if (BDRIRead(fat_offset + index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
return 1; return 1;
if ((bytes_read == 0) && !getfatflag(fat_entry[0])) if ((bytes_read == 0) && !getfatflag(fat_entry[0]))
return 1; return 1;
u32 next_index = getfatindex(fat_entry[1]); u32 next_index = getfatindex(fat_entry[1]);
if (getfatflag(fat_entry[1])) { // Multi-entry node if (getfatflag(fat_entry[1])) { // Multi-entry node
if (BDRIRead(fat_offset + (index + 1) * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK) if (BDRIRead(fat_offset + (index + 1) * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
return 1; return 1;
if (!getfatflag(fat_entry[0]) || getfatflag(fat_entry[1]) || (getfatindex(fat_entry[0]) != index) || (getfatindex(fat_entry[0]) >= getfatindex(fat_entry[1]))) if (!getfatflag(fat_entry[0]) || getfatflag(fat_entry[1]) || (getfatindex(fat_entry[0]) != index) || (getfatindex(fat_entry[0]) >= getfatindex(fat_entry[1])))
return 1; return 1;
read_count = getfatindex(fat_entry[1]) + 1 - index; read_count = getfatindex(fat_entry[1]) + 1 - index;
} else { // Single-entry node } else { // Single-entry node
read_count = 1; read_count = 1;
} }
index = next_index; index = next_index;
u32 btr = min(file_entry.size - bytes_read, read_count * fs_header->data_block_size); u32 btr = min(file_entry.size - bytes_read, read_count * fs_header->data_block_size);
if (entry && (BDRIRead(data_offset + read_start * fs_header->data_block_size, btr, entry + bytes_read) != FR_OK)) if (entry && (BDRIRead(data_offset + read_start * fs_header->data_block_size, btr, entry + bytes_read) != FR_OK))
return 1; return 1;
bytes_read += btr; bytes_read += btr;
} }
return 0; return 0;
} }
static u32 RemoveBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offset, const u8* title_id) { static u32 RemoveBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offset, const u8* title_id) {
if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count)) // Could be more thorough if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count)) // Could be more thorough
return 1; return 1;
const u32 data_offset = fs_header_offset + fs_header->data_offset; 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 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 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 fht_offset = fs_header_offset + fs_header->fht_offset;
const u32 fat_offset = fs_header_offset + fs_header->fat_offset; const u32 fat_offset = fs_header_offset + fs_header->fat_offset;
u32 index = 0, previous_index = 0; u32 index = 0, previous_index = 0;
TdbFileEntry file_entry; TdbFileEntry file_entry;
u64 tid_be = getbe64(title_id); u64 tid_be = getbe64(title_id);
u8* title_id_be = (u8*) &tid_be; u8* title_id_be = (u8*) &tid_be;
// Read the index of the first file entry from the directory entry table // 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(det_offset + 0x2C, sizeof(u32), &(file_entry.next_sibling_index)) != FR_OK)
return 1; return 1;
// Find the file entry for the tid specified, fail if it doesn't exist // Find the file entry for the tid specified, fail if it doesn't exist
do { do {
previous_index = index; previous_index = index;
index = file_entry.next_sibling_index; index = file_entry.next_sibling_index;
if (index == 0) if (index == 0)
return 1; return 1;
if (BDRIRead(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK) if (BDRIRead(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
return 1; return 1;
} while (memcmp(title_id_be, file_entry.title_id, 8) != 0); } while (memcmp(title_id_be, file_entry.title_id, 8) != 0);
DummyFileEntry dummy_entry; DummyFileEntry dummy_entry;
// Read the 0th entry in the FET, which is always a dummy entry // Read the 0th entry in the FET, which is always a dummy entry
if (BDRIRead(fet_offset, sizeof(DummyFileEntry), &dummy_entry) != FR_OK) if (BDRIRead(fet_offset, sizeof(DummyFileEntry), &dummy_entry) != FR_OK)
return 1; return 1;
if (dummy_entry.max_entry_count != fs_header->max_file_count + 1) if (dummy_entry.max_entry_count != fs_header->max_file_count + 1)
return 1; return 1;
if ((BDRIWrite(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &dummy_entry) != FR_OK) || if ((BDRIWrite(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &dummy_entry) != FR_OK) ||
(BDRIWrite(fet_offset + 0x28, sizeof(u32), &index) != FR_OK) || (BDRIWrite(fet_offset + 0x28, sizeof(u32), &index) != FR_OK) ||
(BDRIWrite((previous_index == 0) ? det_offset + 0x2C : fet_offset + previous_index * sizeof(TdbFileEntry) + 0xC, sizeof(u32), &(file_entry.next_sibling_index)) != FR_OK)) (BDRIWrite((previous_index == 0) ? det_offset + 0x2C : fet_offset + previous_index * sizeof(TdbFileEntry) + 0xC, sizeof(u32), &(file_entry.next_sibling_index)) != FR_OK))
return 1; return 1;
const u32 hash_bucket = GetHashBucket(file_entry.title_id, file_entry.parent_index, fs_header->fht_bucket_count); const u32 hash_bucket = GetHashBucket(file_entry.title_id, file_entry.parent_index, fs_header->fht_bucket_count);
u32 index_hash = 0; u32 index_hash = 0;
if (BDRIRead(fht_offset + hash_bucket * sizeof(u32), sizeof(u32), &index_hash) != FR_OK) if (BDRIRead(fht_offset + hash_bucket * sizeof(u32), sizeof(u32), &index_hash) != FR_OK)
return 1; return 1;
if (index_hash == index) { if (index_hash == index) {
if (BDRIWrite(fht_offset + hash_bucket * sizeof(u32), sizeof(u32), &(file_entry.hash_bucket_next_index)) != FR_OK) if (BDRIWrite(fht_offset + hash_bucket * sizeof(u32), sizeof(u32), &(file_entry.hash_bucket_next_index)) != FR_OK)
return 1; return 1;
@ -290,96 +291,97 @@ static u32 RemoveBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_of
do { do {
if (index_hash == 0) // This shouldn't happen if the entry was properly added if (index_hash == 0) // This shouldn't happen if the entry was properly added
break; break;
if (BDRIRead(fet_offset + index_hash * sizeof(TdbFileEntry) + 0x28, sizeof(u32), &index_hash) != FR_OK) if (BDRIRead(fet_offset + index_hash * sizeof(TdbFileEntry) + 0x28, sizeof(u32), &index_hash) != FR_OK)
return 1; return 1;
} while (index_hash != index); } while (index_hash != index);
if ((index_hash != 0) && BDRIWrite(fet_offset + index_hash * sizeof(TdbFileEntry) + 0x28, sizeof(u32), &(file_entry.hash_bucket_next_index)) != FR_OK) if ((index_hash != 0) && BDRIWrite(fet_offset + index_hash * sizeof(TdbFileEntry) + 0x28, sizeof(u32), &(file_entry.hash_bucket_next_index)) != FR_OK)
return 1; return 1;
} }
u32 fat_entry[2]; u32 fat_entry[2];
if (BDRIRead(fat_offset, FAT_ENTRY_SIZE, fat_entry) != FR_OK) if (BDRIRead(fat_offset, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
return 1; return 1;
if (getfatflag(fat_entry[1]) || (fat_entry[0] != 0)) if (getfatflag(fat_entry[1]) || (fat_entry[0] != 0))
return 1; return 1;
u32 next_free_index = getfatindex(fat_entry[1]), fat_index = file_entry.start_block_index + 1; u32 next_free_index = getfatindex(fat_entry[1]), fat_index = file_entry.start_block_index + 1;
if (BDRIWrite(fat_offset + sizeof(u32), sizeof(u32), &fat_index) != FR_OK) if (BDRIWrite(fat_offset + sizeof(u32), sizeof(u32), &fat_index) != FR_OK)
return 1; return 1;
fat_entry[1] = fat_index; fat_entry[1] = fat_index;
do { do {
fat_index = getfatindex(fat_entry[1]); fat_index = getfatindex(fat_entry[1]);
if (BDRIRead(fat_offset + FAT_ENTRY_SIZE * fat_index, FAT_ENTRY_SIZE, fat_entry) != FR_OK) if (BDRIRead(fat_offset + FAT_ENTRY_SIZE * fat_index, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
return 1; return 1;
} while (getfatindex(fat_entry[1]) != 0); } while (getfatindex(fat_entry[1]) != 0);
fat_entry[1] |= next_free_index; fat_entry[1] |= next_free_index;
if ((BDRIWrite(fat_offset + fat_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK) || if ((BDRIWrite(fat_offset + fat_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK) ||
(BDRIRead(fat_offset + next_free_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)) (BDRIRead(fat_offset + next_free_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK))
return 1; return 1;
fat_entry[0] = buildfatuv(fat_index, false); fat_entry[0] = buildfatuv(fat_index, false);
if (BDRIWrite(fat_offset + next_free_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK) if (BDRIWrite(fat_offset + next_free_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
return 1; return 1;
return 0; return 0;
} }
static u32 AddBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offset, const u8* title_id, const u8* entry, const u32 size, bool replace) { static u32 AddBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offset, const u8* title_id, const u8* entry, const u32 size, bool replace) {
if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count)) // Could be more thorough if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count)) // Could be more thorough
return 1; return 1;
if (!entry || !size) if (!entry || !size)
return 1; return 1;
const u32 data_offset = fs_header_offset + fs_header->data_offset; 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 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 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 fht_offset = fs_header_offset + fs_header->fht_offset;
const u32 fat_offset = fs_header_offset + fs_header->fat_offset; const u32 fat_offset = fs_header_offset + fs_header->fat_offset;
const u32 size_blocks = (size / fs_header->data_block_size) + (((size % fs_header->data_block_size) == 0) ? 0 : 1); const u32 size_blocks = (size / fs_header->data_block_size) + (((size % fs_header->data_block_size) == 0) ? 0 : 1);
u32 index = 0, max_index = 0; u32 index = 0, max_index = 0;
TdbFileEntry file_entry; TdbFileEntry file_entry;
u64 tid_be = getbe64(title_id); u64 tid_be = getbe64(title_id);
u8* title_id_be = (u8*) &tid_be; u8* title_id_be = (u8*) &tid_be;
bool do_replace = false; bool do_replace = false;
// Read the index of the first file entry from the directory entry table // 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(det_offset + 0x2C, sizeof(u32), &(file_entry.next_sibling_index)) != FR_OK)
return 1; return 1;
// Try to find the file entry for the tid specified // Try to find the file entry for the tid specified
while (file_entry.next_sibling_index != 0) { while (file_entry.next_sibling_index != 0) {
index = file_entry.next_sibling_index; index = file_entry.next_sibling_index;
max_index = max(index, max_index); max_index = max(index, max_index);
if (BDRIRead(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK) if (BDRIRead(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
return 1; return 1;
// If an entry for the tid already existed that is already the specified size and replace was specified, just replace the existing entry // If an entry for the tid already existed that is already the specified size and replace was specified, just replace the existing entry
if (memcmp(title_id_be, file_entry.title_id, 8) == 0) { if (memcmp(title_id_be, file_entry.title_id, 8) == 0) {
if (!replace || (file_entry.size != size)) return 1; if (!replace) return 1;
else if (file_entry.size != size) return REPLACE_SIZE_MISMATCH;
else { else {
do_replace = true; do_replace = true;
break; break;
} }
} }
} }
u32 fat_entry[2]; u32 fat_entry[2];
u32 fat_index = 0; u32 fat_index = 0;
if (!do_replace) { if (!do_replace) {
if (BDRIRead(fat_offset, FAT_ENTRY_SIZE, fat_entry) != FR_OK) if (BDRIRead(fat_offset, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
return 1; return 1;
@ -476,48 +478,48 @@ static u32 AddBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offse
if (BDRIWrite(fat_offset + previous_free_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK) if (BDRIWrite(fat_offset + previous_free_index * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
return 1; return 1;
} else fat_index = file_entry.start_block_index + 1; } else fat_index = file_entry.start_block_index + 1;
u32 bytes_written = 0, fat_index_write = fat_index; u32 bytes_written = 0, fat_index_write = fat_index;
while (bytes_written < 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 // 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_start = fat_index_write - 1; // Data region block index
u32 write_count = 0; u32 write_count = 0;
if (BDRIRead(fat_offset + fat_index_write * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK) if (BDRIRead(fat_offset + fat_index_write * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
return 1; return 1;
if ((bytes_written == 0) && !getfatflag(fat_entry[0])) if ((bytes_written == 0) && !getfatflag(fat_entry[0]))
return 1; return 1;
u32 next_index = getfatindex(fat_entry[1]); u32 next_index = getfatindex(fat_entry[1]);
if (getfatflag(fat_entry[1])) { // Multi-entry node if (getfatflag(fat_entry[1])) { // Multi-entry node
if (BDRIRead(fat_offset + (fat_index_write + 1) * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK) if (BDRIRead(fat_offset + (fat_index_write + 1) * FAT_ENTRY_SIZE, FAT_ENTRY_SIZE, fat_entry) != FR_OK)
return 1; return 1;
if (!getfatflag(fat_entry[0]) || getfatflag(fat_entry[1]) || (getfatindex(fat_entry[0]) != fat_index_write) || (getfatindex(fat_entry[0]) >= getfatindex(fat_entry[1]))) if (!getfatflag(fat_entry[0]) || getfatflag(fat_entry[1]) || (getfatindex(fat_entry[0]) != fat_index_write) || (getfatindex(fat_entry[0]) >= getfatindex(fat_entry[1])))
return 1; return 1;
write_count = getfatindex(fat_entry[1]) + 1 - fat_index_write; write_count = getfatindex(fat_entry[1]) + 1 - fat_index_write;
} else { // Single-entry node } else { // Single-entry node
write_count = 1; write_count = 1;
} }
fat_index_write = next_index; fat_index_write = next_index;
u32 btw = min(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) if (BDRIWrite(data_offset + write_start * fs_header->data_block_size, btw, entry + bytes_written) != FR_OK)
return 1; return 1;
bytes_written += btw; bytes_written += btw;
} }
if (!do_replace) { if (!do_replace) {
DummyFileEntry dummy_entry; DummyFileEntry dummy_entry;
// Read the 0th entry in the FET, which is always a dummy entry // Read the 0th entry in the FET, which is always a dummy entry
if (BDRIRead(fet_offset, sizeof(DummyFileEntry), &dummy_entry) != FR_OK) if (BDRIRead(fet_offset, sizeof(DummyFileEntry), &dummy_entry) != FR_OK)
return 1; return 1;
if (dummy_entry.max_entry_count != fs_header->max_file_count + 1) if (dummy_entry.max_entry_count != fs_header->max_file_count + 1)
@ -559,83 +561,83 @@ static u32 AddBDRIEntry(const BDRIFsHeader* fs_header, const u32 fs_header_offse
if (BDRIWrite(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK) if (BDRIWrite(fet_offset + index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
return 1; return 1;
} }
return 0; return 0;
} }
static u32 GetNumBDRIEntries(const BDRIFsHeader* fs_header, const u32 fs_header_offset) { static u32 GetNumBDRIEntries(const BDRIFsHeader* fs_header, const u32 fs_header_offset) {
if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count)) // Could be more thorough if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count)) // Could be more thorough
return 0; return 0;
const u32 data_offset = fs_header_offset + fs_header->data_offset; 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 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 fet_offset = data_offset + fs_header->fet_start_block * fs_header->data_block_size;
u32 num_entries = 0; u32 num_entries = 0;
TdbFileEntry file_entry; TdbFileEntry file_entry;
// Read the index of the first file entry from the directory entry table // 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(det_offset + 0x2C, sizeof(u32), &(file_entry.next_sibling_index)) != FR_OK)
return 0; return 0;
while (file_entry.next_sibling_index != 0) { while (file_entry.next_sibling_index != 0) {
num_entries++; num_entries++;
if (BDRIRead(fet_offset + file_entry.next_sibling_index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK) if (BDRIRead(fet_offset + file_entry.next_sibling_index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
return 0; return 0;
} }
return num_entries; return num_entries;
} }
static u32 ListBDRIEntryTitleIDs(const BDRIFsHeader* fs_header, const u32 fs_header_offset, u8* title_ids, u32 max_title_ids) { static u32 ListBDRIEntryTitleIDs(const BDRIFsHeader* fs_header, const u32 fs_header_offset, u8* title_ids, u32 max_title_ids) {
if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count)) if ((fs_header->info_offset != 0x20) || (fs_header->fat_entry_count != fs_header->data_block_count))
return 0; return 0;
const u32 data_offset = fs_header_offset + fs_header->data_offset; 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 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 fet_offset = data_offset + fs_header->fet_start_block * fs_header->data_block_size;
u32 num_entries = 0; u32 num_entries = 0;
TdbFileEntry file_entry; TdbFileEntry file_entry;
for (u32 i = 0; i < max_title_ids; i++) for (u32 i = 0; i < max_title_ids; i++)
memset(title_ids, 0, max_title_ids * 8); memset(title_ids, 0, max_title_ids * 8);
// Read the index of the first file entry from the directory entry table // 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(det_offset + 0x2C, sizeof(u32), &(file_entry.next_sibling_index)) != FR_OK)
return 1; return 1;
while ((file_entry.next_sibling_index != 0) && (num_entries < max_title_ids)) { 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) if (BDRIRead(fet_offset + file_entry.next_sibling_index * sizeof(TdbFileEntry), sizeof(TdbFileEntry), &file_entry) != FR_OK)
return 1; return 1;
u64 tid_be = getbe64(file_entry.title_id); u64 tid_be = getbe64(file_entry.title_id);
memcpy(title_ids + num_entries * 8, (u8*) &tid_be, 8); memcpy(title_ids + num_entries * 8, (u8*) &tid_be, 8);
num_entries++; num_entries++;
} }
return 0; return 0;
} }
u32 GetNumTitleInfoEntries(const char* path) { u32 GetNumTitleInfoEntries(const char* path) {
FIL file; FIL file;
TitleDBPreHeader pre_header; TitleDBPreHeader pre_header;
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return 0; return 0;
bdrifp = &file; bdrifp = &file;
if ((BDRIRead(0, sizeof(TitleDBPreHeader), &pre_header) != FR_OK) || if ((BDRIRead(0, sizeof(TitleDBPreHeader), &pre_header) != FR_OK) ||
!CheckDBMagic((u8*) &pre_header, false)) { !CheckDBMagic((u8*) &pre_header, false)) {
fvx_close(bdrifp); fvx_close(bdrifp);
bdrifp = NULL; bdrifp = NULL;
return 0; return 0;
} }
u32 num = GetNumBDRIEntries(&(pre_header.fs_header), sizeof(TitleDBPreHeader) - sizeof(BDRIFsHeader)); u32 num = GetNumBDRIEntries(&(pre_header.fs_header), sizeof(TitleDBPreHeader) - sizeof(BDRIFsHeader));
fvx_close(bdrifp); fvx_close(bdrifp);
bdrifp = NULL; bdrifp = NULL;
return num; return num;
@ -644,12 +646,12 @@ u32 GetNumTitleInfoEntries(const char* path) {
u32 GetNumTickets(const char* path) { u32 GetNumTickets(const char* path) {
FIL file; FIL file;
TickDBPreHeader pre_header; TickDBPreHeader pre_header;
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return 0; return 0;
bdrifp = &file; bdrifp = &file;
if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) || if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) ||
!CheckDBMagic((u8*) &pre_header, true)) { !CheckDBMagic((u8*) &pre_header, true)) {
fvx_close(bdrifp); fvx_close(bdrifp);
@ -658,7 +660,7 @@ u32 GetNumTickets(const char* path) {
} }
u32 num = GetNumBDRIEntries(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader)); u32 num = GetNumBDRIEntries(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader));
fvx_close(bdrifp); fvx_close(bdrifp);
bdrifp = NULL; bdrifp = NULL;
return num; return num;
@ -667,12 +669,12 @@ u32 GetNumTickets(const char* path) {
u32 ListTitleInfoEntryTitleIDs(const char* path, u8* title_ids, u32 max_title_ids) { u32 ListTitleInfoEntryTitleIDs(const char* path, u8* title_ids, u32 max_title_ids) {
FIL file; FIL file;
TitleDBPreHeader pre_header; TitleDBPreHeader pre_header;
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return 1; return 1;
bdrifp = &file; bdrifp = &file;
if ((BDRIRead(0, sizeof(TitleDBPreHeader), &pre_header) != FR_OK) || if ((BDRIRead(0, sizeof(TitleDBPreHeader), &pre_header) != FR_OK) ||
!CheckDBMagic((u8*) &pre_header, false) || !CheckDBMagic((u8*) &pre_header, false) ||
(ListBDRIEntryTitleIDs(&(pre_header.fs_header), sizeof(TitleDBPreHeader) - sizeof(BDRIFsHeader), title_ids, max_title_ids) != 0)) { (ListBDRIEntryTitleIDs(&(pre_header.fs_header), sizeof(TitleDBPreHeader) - sizeof(BDRIFsHeader), title_ids, max_title_ids) != 0)) {
@ -680,7 +682,7 @@ u32 ListTitleInfoEntryTitleIDs(const char* path, u8* title_ids, u32 max_title_id
bdrifp = NULL; bdrifp = NULL;
return 1; return 1;
} }
fvx_close(bdrifp); fvx_close(bdrifp);
bdrifp = NULL; bdrifp = NULL;
return 0; return 0;
@ -689,12 +691,12 @@ u32 ListTitleInfoEntryTitleIDs(const char* path, u8* title_ids, u32 max_title_id
u32 ListTicketTitleIDs(const char* path, u8* title_ids, u32 max_title_ids) { u32 ListTicketTitleIDs(const char* path, u8* title_ids, u32 max_title_ids) {
FIL file; FIL file;
TickDBPreHeader pre_header; TickDBPreHeader pre_header;
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return 1; return 1;
bdrifp = &file; bdrifp = &file;
if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) || if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) ||
!CheckDBMagic((u8*) &pre_header, true) || !CheckDBMagic((u8*) &pre_header, true) ||
(ListBDRIEntryTitleIDs(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_ids, max_title_ids) != 0)) { (ListBDRIEntryTitleIDs(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_ids, max_title_ids) != 0)) {
@ -702,7 +704,7 @@ u32 ListTicketTitleIDs(const char* path, u8* title_ids, u32 max_title_ids) {
bdrifp = NULL; bdrifp = NULL;
return 1; return 1;
} }
fvx_close(bdrifp); fvx_close(bdrifp);
bdrifp = NULL; bdrifp = NULL;
return 0; return 0;
@ -711,12 +713,12 @@ u32 ListTicketTitleIDs(const char* path, u8* title_ids, u32 max_title_ids) {
u32 ReadTitleInfoEntryFromDB(const char* path, const u8* title_id, TitleInfoEntry* tie) { u32 ReadTitleInfoEntryFromDB(const char* path, const u8* title_id, TitleInfoEntry* tie) {
FIL file; FIL file;
TitleDBPreHeader pre_header; TitleDBPreHeader pre_header;
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return 1; return 1;
bdrifp = &file; bdrifp = &file;
if ((BDRIRead(0, sizeof(TitleDBPreHeader), &pre_header) != FR_OK) || if ((BDRIRead(0, sizeof(TitleDBPreHeader), &pre_header) != FR_OK) ||
!CheckDBMagic((u8*) &pre_header, false) || !CheckDBMagic((u8*) &pre_header, false) ||
(ReadBDRIEntry(&(pre_header.fs_header), sizeof(TitleDBPreHeader) - sizeof(BDRIFsHeader), title_id, (u8*) tie, (ReadBDRIEntry(&(pre_header.fs_header), sizeof(TitleDBPreHeader) - sizeof(BDRIFsHeader), title_id, (u8*) tie,
@ -725,7 +727,7 @@ u32 ReadTitleInfoEntryFromDB(const char* path, const u8* title_id, TitleInfoEntr
bdrifp = NULL; bdrifp = NULL;
return 1; return 1;
} }
fvx_close(bdrifp); fvx_close(bdrifp);
bdrifp = NULL; bdrifp = NULL;
return 0; return 0;
@ -737,15 +739,16 @@ u32 ReadTicketFromDB(const char* path, const u8* title_id, Ticket** ticket) {
TicketEntry* te = NULL; TicketEntry* te = NULL;
u32 entry_size; u32 entry_size;
*ticket = NULL;
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return 1; return 1;
bdrifp = &file; bdrifp = &file;
if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) || if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) ||
!CheckDBMagic((u8*) &pre_header, true) || !CheckDBMagic((u8*) &pre_header, true) ||
(GetBDRIEntrySize(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id, &entry_size) != 0) || (GetBDRIEntrySize(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id, &entry_size) != 0) ||
entry_size < sizeof(TicketEntry) + 0x14 || entry_size < sizeof(TicketEntry) + 0x14 ||
(te = (TicketEntry*)malloc(entry_size), te == NULL) || (te = (TicketEntry*)malloc(entry_size), te == NULL) ||
(ReadBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id, (u8*) te, (ReadBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id, (u8*) te,
entry_size) != 0)) { entry_size) != 0)) {
@ -754,15 +757,15 @@ u32 ReadTicketFromDB(const char* path, const u8* title_id, Ticket** ticket) {
bdrifp = NULL; bdrifp = NULL;
return 1; return 1;
} }
fvx_close(bdrifp); fvx_close(bdrifp);
bdrifp = NULL; bdrifp = NULL;
if (te->ticket_size != GetTicketSize(&te->ticket)) { if (te->ticket_size != GetTicketSize(&te->ticket)) {
free(te); free(te);
return 1; return 1;
} }
if (ticket) { if (ticket) {
u32 size = te->ticket_size; u32 size = te->ticket_size;
memmove(te, &te->ticket, size); // recycle this memory, instead of allocating another memmove(te, &te->ticket, size); // recycle this memory, instead of allocating another
@ -771,7 +774,7 @@ u32 ReadTicketFromDB(const char* path, const u8* title_id, Ticket** ticket) {
*ticket = tik; *ticket = tik;
return 0; return 0;
} }
free(te); free(te);
return 0; return 0;
} }
@ -779,12 +782,12 @@ u32 ReadTicketFromDB(const char* path, const u8* title_id, Ticket** ticket) {
u32 RemoveTitleInfoEntryFromDB(const char* path, const u8* title_id) { u32 RemoveTitleInfoEntryFromDB(const char* path, const u8* title_id) {
FIL file; FIL file;
TitleDBPreHeader pre_header; TitleDBPreHeader pre_header;
if (fvx_open(&file, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING) != FR_OK) if (fvx_open(&file, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING) != FR_OK)
return 1; return 1;
bdrifp = &file; bdrifp = &file;
if ((BDRIRead(0, sizeof(TitleDBPreHeader), &pre_header) != FR_OK) || if ((BDRIRead(0, sizeof(TitleDBPreHeader), &pre_header) != FR_OK) ||
!CheckDBMagic((u8*) &pre_header, false) || !CheckDBMagic((u8*) &pre_header, false) ||
(RemoveBDRIEntry(&(pre_header.fs_header), sizeof(TitleDBPreHeader) - sizeof(BDRIFsHeader), title_id) != 0)) { (RemoveBDRIEntry(&(pre_header.fs_header), sizeof(TitleDBPreHeader) - sizeof(BDRIFsHeader), title_id) != 0)) {
@ -792,7 +795,7 @@ u32 RemoveTitleInfoEntryFromDB(const char* path, const u8* title_id) {
bdrifp = NULL; bdrifp = NULL;
return 1; return 1;
} }
fvx_close(bdrifp); fvx_close(bdrifp);
bdrifp = NULL; bdrifp = NULL;
return 0; return 0;
@ -801,12 +804,12 @@ u32 RemoveTitleInfoEntryFromDB(const char* path, const u8* title_id) {
u32 RemoveTicketFromDB(const char* path, const u8* title_id) { u32 RemoveTicketFromDB(const char* path, const u8* title_id) {
FIL file; FIL file;
TickDBPreHeader pre_header; TickDBPreHeader pre_header;
if (fvx_open(&file, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING) != FR_OK) if (fvx_open(&file, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING) != FR_OK)
return 1; return 1;
bdrifp = &file; bdrifp = &file;
if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) || if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) ||
!CheckDBMagic((u8*) &pre_header, true) || !CheckDBMagic((u8*) &pre_header, true) ||
(RemoveBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id) != 0)) { (RemoveBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id) != 0)) {
@ -814,7 +817,7 @@ u32 RemoveTicketFromDB(const char* path, const u8* title_id) {
bdrifp = NULL; bdrifp = NULL;
return 1; return 1;
} }
fvx_close(&file); fvx_close(&file);
bdrifp = NULL; bdrifp = NULL;
return 0; return 0;
@ -823,13 +826,13 @@ u32 RemoveTicketFromDB(const char* path, const u8* title_id) {
u32 AddTitleInfoEntryToDB(const char* path, const u8* title_id, const TitleInfoEntry* tie, bool replace) { u32 AddTitleInfoEntryToDB(const char* path, const u8* title_id, const TitleInfoEntry* tie, bool replace) {
FIL file; FIL file;
TitleDBPreHeader pre_header; TitleDBPreHeader pre_header;
if (fvx_open(&file, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING) != FR_OK) if (fvx_open(&file, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING) != FR_OK)
return 1; return 1;
bdrifp = &file; bdrifp = &file;
if ((BDRIRead(0, sizeof(TitleDBPreHeader), &pre_header) != FR_OK) || if ((BDRIRead(0, sizeof(TitleDBPreHeader), &pre_header) != FR_OK) ||
!CheckDBMagic((u8*) &pre_header, false) || !CheckDBMagic((u8*) &pre_header, false) ||
(AddBDRIEntry(&(pre_header.fs_header), sizeof(TitleDBPreHeader) - sizeof(BDRIFsHeader), title_id, (AddBDRIEntry(&(pre_header.fs_header), sizeof(TitleDBPreHeader) - sizeof(BDRIFsHeader), title_id,
(const u8*) tie, sizeof(TitleInfoEntry), replace) != 0)) { (const u8*) tie, sizeof(TitleInfoEntry), replace) != 0)) {
@ -837,7 +840,7 @@ u32 AddTitleInfoEntryToDB(const char* path, const u8* title_id, const TitleInfoE
bdrifp = NULL; bdrifp = NULL;
return 1; return 1;
} }
fvx_close(bdrifp); fvx_close(bdrifp);
bdrifp = NULL; bdrifp = NULL;
return 0; return 0;
@ -847,7 +850,7 @@ u32 AddTicketToDB(const char* path, const u8* title_id, const Ticket* ticket, bo
FIL file; FIL file;
TickDBPreHeader pre_header; TickDBPreHeader pre_header;
u32 entry_size = sizeof(TicketEntry) + GetTicketContentIndexSize(ticket); u32 entry_size = sizeof(TicketEntry) + GetTicketContentIndexSize(ticket);
TicketEntry* te = (TicketEntry*)malloc(entry_size); TicketEntry* te = (TicketEntry*)malloc(entry_size);
if (!te) { if (!te) {
return 1; return 1;
@ -860,13 +863,18 @@ u32 AddTicketToDB(const char* path, const u8* title_id, const Ticket* ticket, bo
free(te); free(te);
return 1; return 1;
} }
bdrifp = &file; bdrifp = &file;
u32 add_bdri_res = 0;
if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) || if ((BDRIRead(0, sizeof(TickDBPreHeader), &pre_header) != FR_OK) ||
!CheckDBMagic((u8*) &pre_header, true) || !CheckDBMagic((u8*) &pre_header, true) ||
(AddBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id, ((add_bdri_res = AddBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id,
(const u8*) te, entry_size, replace) != 0)) { (const u8*) te, entry_size, replace)) == 1) ||
(add_bdri_res == REPLACE_SIZE_MISMATCH && ((RemoveBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id) != 0) ||
(AddBDRIEntry(&(pre_header.fs_header), sizeof(TickDBPreHeader) - sizeof(BDRIFsHeader), title_id,
(const u8*) te, entry_size, replace) != 0)))) {
free(te); free(te);
fvx_close(bdrifp); fvx_close(bdrifp);
bdrifp = NULL; bdrifp = NULL;

View File

@ -7,7 +7,7 @@ u32 CheckBossHash(BossHeader* boss, bool encrypted) {
u8 hash_area[0x14] __attribute__((aligned(4))) = { 0 }; u8 hash_area[0x14] __attribute__((aligned(4))) = { 0 };
u8 boss_sha256[0x20]; u8 boss_sha256[0x20];
u8 l_sha256[0x20]; u8 l_sha256[0x20];
// calculate hash // calculate hash
memcpy(hash_area, ((u8*) boss) + 0x28, 0x12); memcpy(hash_area, ((u8*) boss) + 0x28, 0x12);
memcpy(boss_sha256, boss->hash_header, 0x20); memcpy(boss_sha256, boss->hash_header, 0x20);
@ -16,13 +16,13 @@ u32 CheckBossHash(BossHeader* boss, bool encrypted) {
CryptBoss(boss_sha256, 0x28 + 0x12, 0x20, boss); CryptBoss(boss_sha256, 0x28 + 0x12, 0x20, boss);
} }
sha_quick(l_sha256, hash_area, 0x14, SHA256_MODE); sha_quick(l_sha256, hash_area, 0x14, SHA256_MODE);
return (memcmp(boss_sha256, l_sha256, 0x20) == 0) ? 0 : 1; return (memcmp(boss_sha256, l_sha256, 0x20) == 0) ? 0 : 1;
} }
u32 ValidateBossHeader(BossHeader* header, u32 fsize) { u32 ValidateBossHeader(BossHeader* header, u32 fsize) {
u8 boss_magic[] = { BOSS_MAGIC }; u8 boss_magic[] = { BOSS_MAGIC };
// base checks // base checks
if ((memcmp(header->magic, boss_magic, sizeof(boss_magic)) != 0) || if ((memcmp(header->magic, boss_magic, sizeof(boss_magic)) != 0) ||
(fsize && (fsize != getbe32(header->filesize))) || (fsize && (fsize != getbe32(header->filesize))) ||
@ -31,12 +31,12 @@ u32 ValidateBossHeader(BossHeader* header, u32 fsize) {
(getbe16(header->cnthdr_hash_type) != 0x0002) || (getbe16(header->cnthdr_hash_type) != 0x0002) ||
(getbe16(header->cnthdr_rsa_size) != 0x0002)) (getbe16(header->cnthdr_rsa_size) != 0x0002))
return 1; return 1;
// hash check // hash check
if ((CheckBossHash(header, false) != 0) && if ((CheckBossHash(header, false) != 0) &&
(CheckBossHash(header, true) != 0)) (CheckBossHash(header, true) != 0))
return 1; return 1;
return 0; return 0;
} }
@ -59,14 +59,14 @@ u32 CryptBoss(void* data, u32 offset, u32 size, BossHeader* boss) {
size -= 0x28 - offset; size -= 0x28 - offset;
offset = 0x28; offset = 0x28;
} }
// decrypt BOSS data // decrypt BOSS data
u8 ctr[16] = { 0 }; u8 ctr[16] = { 0 };
memcpy(ctr, boss->ctr12, 12); memcpy(ctr, boss->ctr12, 12);
ctr[15] = 0x01; ctr[15] = 0x01;
use_aeskey(0x38); use_aeskey(0x38);
ctr_decrypt_byte(data, data, size, offset - 0x28, AES_CNT_CTRNAND_MODE, ctr); ctr_decrypt_byte(data, data, size, offset - 0x28, AES_CNT_CTRNAND_MODE, ctr);
return 0; return 0;
} }
@ -76,7 +76,7 @@ u32 CryptBossSequential(void* data, u32 offset, u32 size) {
// unexpected results otherwise // unexpected results otherwise
static BossHeader boss = { 0 }; static BossHeader boss = { 0 };
static BossHeader* bossptr = NULL; static BossHeader* bossptr = NULL;
// fetch boss header from data // fetch boss header from data
if ((offset == 0) && (size >= sizeof(BossHeader))) { if ((offset == 0) && (size >= sizeof(BossHeader))) {
bossptr = NULL; bossptr = NULL;
@ -87,9 +87,9 @@ u32 CryptBossSequential(void* data, u32 offset, u32 size) {
return 1; return 1;
bossptr = &boss; bossptr = &boss;
} }
// safety check, boss pointer // safety check, boss pointer
if (!bossptr) return 1; if (!bossptr) return 1;
return CryptBoss(data, offset, size, bossptr); return CryptBoss(data, offset, size, bossptr);
} }

View File

@ -14,7 +14,7 @@
typedef struct { typedef struct {
// actual BOSS header // actual BOSS header
u8 magic[8]; // "boss" + 0x00010001, see above u8 magic[8]; // "boss" + 0x00010001, see above
u8 filesize[4]; // big endian u8 filesize[4]; // big endian
u8 release_date[8]; u8 release_date[8];
u8 unknown0[2]; // always 0x0001 u8 unknown0[2]; // always 0x0001
u8 padding[2]; u8 padding[2];
@ -27,7 +27,7 @@ typedef struct {
u8 hash_header[0x20]; u8 hash_header[0x20];
u8 signature_header[0x100]; u8 signature_header[0x100];
// payload header, first 0x1C byte used for hash (0x15A) // payload header, first 0x1C byte used for hash (0x15A)
u8 programId[8]; u8 programId[8];
u8 unknown2[4]; // typically zero u8 unknown2[4]; // typically zero
u8 data_type[4]; u8 data_type[4];
u8 size_payload[4]; u8 size_payload[4];

View File

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

View File

@ -1,21 +1,816 @@
#include "cert.h" #include "cert.h"
#include "ff.h" #include "disadiff.h"
#include "rsa.h"
u32 LoadCertFromCertDb(u64 offset, Certificate* cert, u32* mod, u32* exp) { typedef struct {
Certificate cert_local; char magic[4]; // "CERT"
FIL db; u8 unk[4]; // afaik, always 0
UINT bytes_read; u8 used_size[4]; // size used after this header
u8 garbage[4]; // literally garbage values
} PACKED_STRUCT CertsDbPartitionHeader;
// not much in terms of error checking here static inline void GetCertDBPath(char* path, bool emunand) {
if (f_open(&db, "1:/dbs/certs.db", FA_READ | FA_OPEN_EXISTING) != FR_OK) path[0] = emunand ? '4' : '1';
strcpy(&path[1], ":/dbs/certs.db");
}
#define CERT_RETAIL_CA3_IDENT BIT(0)
#define CERT_RETAIL_XSc_IDENT BIT(1)
#define CERT_RETAIL_CPb_IDENT BIT(2)
#define CERT_DEV_CA4_IDENT BIT(3)
#define CERT_DEV_XS9_IDENT BIT(4)
#define CERT_DEV_CPa_IDENT BIT(5)
#define CERT_NO_STORE_SPACE (0xFF)
static struct {
u32 loaded_certs_flg;
u8 retail_CA3_raw[CERT_RSA4096_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
u8 retail_XSc_raw[CERT_RSA2048_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
u8 retail_CPb_raw[CERT_RSA2048_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
u8 dev_CA4_raw[CERT_RSA4096_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
u8 dev_XS9_raw[CERT_RSA2048_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
u8 dev_CPa_raw[CERT_RSA2048_SIG_SIZE + CERT_RSA2048_BODY_SIZE];
Certificate retail_CA3;
Certificate retail_XSc;
Certificate retail_CPb;
Certificate dev_CA4;
Certificate dev_XS9;
Certificate dev_CPa;
} _CommonCertsStorage = {
0, // none loaded yet, ident defines used to say what's loaded
{0}, {0}, {0}, {0}, {0}, {0}, // no data yet
// cert structs pre-point already to raw certs
{
(CertificateSignature*)&_CommonCertsStorage.retail_CA3_raw[0],
(CertificateBody*)&_CommonCertsStorage.retail_CA3_raw[CERT_RSA4096_SIG_SIZE]
},
{
(CertificateSignature*)&_CommonCertsStorage.retail_XSc_raw[0],
(CertificateBody*)&_CommonCertsStorage.retail_XSc_raw[CERT_RSA2048_SIG_SIZE]
},
{
(CertificateSignature*)&_CommonCertsStorage.retail_CPb_raw[0],
(CertificateBody*)&_CommonCertsStorage.retail_CPb_raw[CERT_RSA2048_SIG_SIZE]
},
{
(CertificateSignature*)&_CommonCertsStorage.dev_CA4_raw[0],
(CertificateBody*)&_CommonCertsStorage.dev_CA4_raw[CERT_RSA4096_SIG_SIZE]
},
{
(CertificateSignature*)&_CommonCertsStorage.dev_XS9_raw[0],
(CertificateBody*)&_CommonCertsStorage.dev_XS9_raw[CERT_RSA2048_SIG_SIZE]
},
{
(CertificateSignature*)&_CommonCertsStorage.dev_CPa_raw[0],
(CertificateBody*)&_CommonCertsStorage.dev_CPa_raw[CERT_RSA2048_SIG_SIZE]
}
};
static inline void _Certificate_CleanupImpl(Certificate* cert);
bool Certificate_IsValid(const Certificate* cert) {
if (!cert || !cert->sig || !cert->data)
return false;
u32 sig_type = getbe32(cert->sig->sig_type);
if (sig_type < 0x10000 || sig_type > 0x10005)
return false;
u32 keytype = getbe32(cert->data->keytype);
if (keytype > 2)
return false;
size_t issuer_len = strnlen(cert->data->issuer, 0x40);
size_t name_len = strnlen(cert->data->name, 0x40);
// if >= 0x40, cert can't fit as issuer for other objects later
// since later objects using the certificate as their issuer will have them use it as certissuer-certname
if (!issuer_len || !name_len || (issuer_len + name_len + 1) >= 0x40)
return false;
return true;
}
bool Certificate_IsRSA(const Certificate* cert) {
if (!Certificate_IsValid(cert)) return false;
if (getbe32(cert->data->keytype) >= 2) return false;
return true;
}
bool Certificate_IsECC(const Certificate* cert) {
if (!Certificate_IsValid(cert)) return false;
if (getbe32(cert->data->keytype) != 2) return false;
return true;
}
u32 Certificate_GetSignatureSize(const Certificate* cert, u32* size) {
if (!size || !Certificate_IsValid(cert)) return 1;
u32 sig_type = getbe32(cert->sig->sig_type);
if (sig_type == 0x10000 || sig_type == 0x10003)
*size = 0x200;
else if (sig_type == 0x10001 || sig_type == 0x10004)
*size = 0x100;
else if (sig_type == 0x10002 || sig_type == 0x10005)
*size = 0x3C;
else
return 1; return 1;
f_lseek(&db, offset);
if (!cert) cert = &cert_local;
f_read(&db, cert, CERT_SIZE, &bytes_read);
f_close(&db);
if (mod) memcpy(mod, cert->mod, 0x100);
if (exp) *exp = getle32(cert->exp);
return 0; return 0;
} }
u32 Certificate_GetModulusSize(const Certificate* cert, u32* size) {
if (!size || !Certificate_IsRSA(cert)) return 1;
u32 keytype = getbe32(cert->data->keytype);
if (keytype == 0)
*size = 4096 / 8;
else if (keytype == 1)
*size = 2048 / 8;
else return 1;
return 0;
}
u32 Certificate_GetModulus(const Certificate* cert, void* mod) {
u32 size;
if (!mod || Certificate_GetModulusSize(cert, &size)) return 1;
memcpy(mod, cert->data->pub_key_data, size);
return 0;
}
u32 Certificate_GetExponent(const Certificate* cert, void* exp) {
u32 size;
if (!exp || Certificate_GetModulusSize(cert, &size)) return 1;
memcpy(exp, &cert->data->pub_key_data[size], 4);
return 0;
}
u32 Certificate_GetEccSingleCoordinateSize(const Certificate* cert, u32* size) {
if (!size || !Certificate_IsECC(cert)) return 1;
u32 keytype = getbe32(cert->data->keytype);
if (keytype == 2)
*size = 0x3C / 2;
else return 1;
return 0;
}
u32 Certificate_GetEccXY(const Certificate* cert, void* X, void* Y) {
u32 size;
if (!X || !Y || Certificate_GetEccSingleCoordinateSize(cert, &size)) return 1;
memcpy(X, cert->data->pub_key_data, size);
memcpy(Y, &cert->data->pub_key_data[size], size);
return 0;
}
static inline u32 _Certificate_GetSignatureChunkSizeFromType(u32 sig_type) {
if (sig_type == 0x10000 || sig_type == 0x10003)
return CERT_RSA4096_SIG_SIZE;
else if (sig_type == 0x10001 || sig_type == 0x10004)
return CERT_RSA2048_SIG_SIZE;
else if (sig_type == 0x10002 || sig_type == 0x10005)
return CERT_ECC_SIG_SIZE;
return 0;
}
u32 Certificate_GetSignatureChunkSize(const Certificate* cert, u32* size) {
if (!size || !Certificate_IsValid(cert)) return 1;
u32 _size = _Certificate_GetSignatureChunkSizeFromType(getbe32(cert->sig->sig_type));
if (_size == 0) return 1;
*size = _size;
return 0;
}
static inline u32 _Certificate_GetDataChunkSizeFromType(u32 keytype) {
if (keytype == 0)
return CERT_RSA4096_BODY_SIZE;
else if (keytype == 1)
return CERT_RSA2048_BODY_SIZE;
else if (keytype == 2)
return CERT_ECC_BODY_SIZE;
return 0;
}
u32 Certificate_GetDataChunkSize(const Certificate* cert, u32* size) {
if (!size || !Certificate_IsValid(cert)) return 1;
u32 _size = _Certificate_GetDataChunkSizeFromType(getbe32(cert->data->keytype));
if (_size == 0) return 1;
*size = _size;
return 0;
}
u32 Certificate_GetFullSize(const Certificate* cert, u32* size) {
if (!size || !Certificate_IsValid(cert)) return 1;
u32 sig_size = _Certificate_GetSignatureChunkSizeFromType(getbe32(cert->sig->sig_type));
u32 data_size = _Certificate_GetDataChunkSizeFromType(getbe32(cert->data->keytype));
if (sig_size == 0 || data_size == 0)
return 1;
*size = sig_size + data_size;
return 0;
}
static inline u32 _Certificate_KeytypeSignatureSize(u32 keytype) {
if (keytype == 0)
return 0x200;
else if (keytype == 1)
return 0x100;
else if (keytype == 2)
return 0x3C;
return 0;
}
static inline u32 _Certificate_VerifyRSA4096(const Certificate* cert, const void* sig, const void* data, u32 data_size, bool sha256) {
(void)cert; (void)sig; (void)data; (void)data_size; (void)sha256;
return 2; // not implemented
}
// noipa, to avoid form of inlining, cloning, etc, to avoid the extra stack usage when unneeded
static __attribute__((noipa)) bool _Certificate_SetKey2048Misaligned(const Certificate* cert) {
u32 mod[2048/8];
u32 exp;
memcpy(mod, cert->data->pub_key_data, 2048/8);
exp = getle32(&cert->data->pub_key_data[2048/8]);
return RSA_setKey2048(3, mod, exp);
}
static inline u32 _Certificate_VerifyRSA2048(const Certificate* cert, const void* sig, const void* data, u32 data_size, bool sha256) {
if (!sha256)
return 2; // not implemented
int ret;
if (((u32)&cert->data->pub_key_data[0]) & 0x3)
ret = !_Certificate_SetKey2048Misaligned(cert);
else
ret = !RSA_setKey2048(3, (const u32*)(const void*)&cert->data->pub_key_data[0], getle32(&cert->data->pub_key_data[2048/8]));
if (ret)
return ret;
return !RSA_verify2048(sig, data, data_size);
}
static inline u32 _Certificate_VerifyECC(const Certificate* cert, const void* sig, const void* data, u32 data_size, bool sha256) {
(void)cert; (void)sig; (void)data; (void)data_size; (void)sha256;
return 2; // not implemented
}
u32 Certificate_VerifySignatureBlock(const Certificate* cert, const void* sig, u32 sig_size, const void* data, u32 data_size, bool sha256) {
if (!sig || !sig_size || (!data && data_size) || !Certificate_IsValid(cert))
return 1;
u32 keytype = getbe32(cert->data->keytype);
u32 _sig_size = _Certificate_KeytypeSignatureSize(keytype);
if (sig_size != _sig_size)
return 1;
if (keytype == 0)
return _Certificate_VerifyRSA4096(cert, sig, data, data_size, sha256);
if (keytype == 1)
return _Certificate_VerifyRSA2048(cert, sig, data, data_size, sha256);
if (keytype == 2)
return _Certificate_VerifyECC(cert, sig, data, data_size, sha256);
return 1;
}
static inline void* _Certificate_SafeRealloc(void* ptr, size_t size, size_t oldsize) {
void* new_ptr;
size_t min_size = min(oldsize, size);
if ((u32)ptr >= (u32)&_CommonCertsStorage && (u32)ptr < (u32)&_CommonCertsStorage + sizeof(_CommonCertsStorage)) {
new_ptr = malloc(size);
if (new_ptr) memcpy(new_ptr, ptr, min_size);
} else {
new_ptr = realloc(ptr, size);
}
if (!new_ptr) return NULL;
memset(&((u8*)new_ptr)[min_size], 0, size - min_size);
return new_ptr;
}
// will reallocate memory for certificate signature and body to fit the max possible size.
// will also allocate an empty object if Certificate is NULL initialized.
// if certificate points to static storage, an allocated version will be created.
// if function fails, the Certificate, even if previously NULL initialized, still has to be passed to cleaned up after use.
u32 Certificate_MakeEditSafe(Certificate* cert) {
if (!cert) return 1;
bool isvalid = Certificate_IsValid(cert);
if ((cert->sig || cert->data) && !isvalid) return 1;
Certificate cert_local;
u32 sig_size = isvalid ? _Certificate_GetSignatureChunkSizeFromType(getbe32(cert->sig->sig_type)) : 0;
u32 data_size = isvalid ? _Certificate_GetDataChunkSizeFromType(getbe32(cert->data->keytype)) : 0;
if (isvalid && (sig_size == 0 || data_size == 0)) return 1;
cert_local.sig = _Certificate_SafeRealloc(cert->sig, CERT_RSA4096_SIG_SIZE, sig_size);
if (!cert_local.sig) return 1;
cert->sig = cert_local.sig;
cert_local.data = _Certificate_SafeRealloc(cert->data, CERT_RSA4096_BODY_SIZE, data_size);
if (!cert_local.data) return 1;
cert->data = cert_local.data;
return 0;
}
static u32 _Certificate_AllocCopyOutImpl(const Certificate* cert, Certificate* out_cert) {
u32 sig_size = _Certificate_GetSignatureChunkSizeFromType(getbe32(cert->sig->sig_type));
u32 data_size = _Certificate_GetDataChunkSizeFromType(getbe32(cert->data->keytype));
if (sig_size == 0 || data_size == 0)
return 1;
out_cert->sig = (CertificateSignature*)malloc(sig_size);
out_cert->data = (CertificateBody*)malloc(data_size);
if (!out_cert->sig || !out_cert->data) {
_Certificate_CleanupImpl(out_cert);
return 1;
}
memcpy(out_cert->sig, cert->sig, sig_size);
memcpy(out_cert->data, cert->data, data_size);
return 0;
}
u32 Certificate_AllocCopyOut(const Certificate* cert, Certificate* out_cert) {
if (!out_cert || !Certificate_IsValid(cert)) return 1;
return _Certificate_AllocCopyOutImpl(cert, out_cert);
}
static u32 _Certificate_RawCopyImpl(const Certificate* cert, void* raw) {
u32 sig_size = _Certificate_GetSignatureChunkSizeFromType(getbe32(cert->sig->sig_type));
u32 data_size = _Certificate_GetDataChunkSizeFromType(getbe32(cert->data->keytype));
if (sig_size == 0 || data_size == 0)
return 1;
memcpy(raw, cert->sig, sig_size);
memcpy(&((u8*)raw)[sig_size], cert->data, data_size);
return 0;
}
u32 Certificate_RawCopy(const Certificate* cert, void* raw) {
if (!raw || !Certificate_IsValid(cert)) return 1;
return _Certificate_RawCopyImpl(cert, raw);
}
// ptr free check, to not free if ptr is pointing to static storage!!
static inline void _Certificate_SafeFree(void* ptr) {
if ((u32)ptr >= (u32)&_CommonCertsStorage && (u32)ptr < (u32)&_CommonCertsStorage + sizeof(_CommonCertsStorage))
return;
free(ptr);
}
static inline void _Certificate_CleanupImpl(Certificate* cert) {
_Certificate_SafeFree(cert->sig);
_Certificate_SafeFree(cert->data);
cert->sig = NULL;
cert->data = NULL;
}
u32 Certificate_Cleanup(Certificate* cert) {
if (!cert) return 1;
_Certificate_CleanupImpl(cert);
return 0;
}
static u32 _Issuer_To_StorageIdent(const char* issuer) {
if (strncmp(issuer, "Root-CA0000000", 14) != 0)
return CERT_NO_STORE_SPACE;
if (issuer[14] == '3') { // retail
if (issuer[15] == 0)
return CERT_RETAIL_CA3_IDENT;
if (issuer[15] != '-')
return CERT_NO_STORE_SPACE;
if (!strcmp(&issuer[16], "XS0000000c"))
return CERT_RETAIL_XSc_IDENT;
if (!strcmp(&issuer[16], "CP0000000b"))
return CERT_RETAIL_CPb_IDENT;
}
if (issuer[14] == '4') { // dev
if (issuer[15] == 0)
return CERT_DEV_CA4_IDENT;
if (issuer[15] != '-')
return CERT_NO_STORE_SPACE;
if (!strcmp(&issuer[16], "XS00000009"))
return CERT_DEV_XS9_IDENT;
if (!strcmp(&issuer[16], "CP0000000a"))
return CERT_DEV_CPa_IDENT;
}
return CERT_NO_STORE_SPACE;
}
static bool _LoadFromCertStorage(Certificate* cert, u32 ident) {
if (ident == CERT_NO_STORE_SPACE)
return false;
Certificate* _cert = NULL;
switch (ident) {
case CERT_RETAIL_CA3_IDENT:
if (_CommonCertsStorage.loaded_certs_flg & CERT_RETAIL_CA3_IDENT)
_cert = &_CommonCertsStorage.retail_CA3;
break;
case CERT_RETAIL_XSc_IDENT:
if (_CommonCertsStorage.loaded_certs_flg & CERT_RETAIL_XSc_IDENT)
_cert = &_CommonCertsStorage.retail_XSc;
break;
case CERT_RETAIL_CPb_IDENT:
if (_CommonCertsStorage.loaded_certs_flg & CERT_RETAIL_CPb_IDENT)
_cert = &_CommonCertsStorage.retail_CPb;
break;
case CERT_DEV_CA4_IDENT:
if (_CommonCertsStorage.loaded_certs_flg & CERT_DEV_CA4_IDENT)
_cert = &_CommonCertsStorage.dev_CA4;
break;
case CERT_DEV_XS9_IDENT:
if (_CommonCertsStorage.loaded_certs_flg & CERT_DEV_XS9_IDENT)
_cert = &_CommonCertsStorage.dev_XS9;
break;
case CERT_DEV_CPa_IDENT:
if (_CommonCertsStorage.loaded_certs_flg & CERT_DEV_CPa_IDENT)
_cert = &_CommonCertsStorage.dev_CPa;
break;
default:
break;
}
if (!_cert)
return false;
*cert = *_cert;
return true;
}
static void _SaveToCertStorage(const Certificate* cert, u32 ident) {
if (ident == CERT_NO_STORE_SPACE)
return;
Certificate* _cert = NULL;
u8* raw_space = NULL;
u32 raw_size = 0;
switch (ident) {
case CERT_RETAIL_CA3_IDENT:
if (!(_CommonCertsStorage.loaded_certs_flg & CERT_RETAIL_CA3_IDENT)) {
_cert = &_CommonCertsStorage.retail_CA3;
raw_space = &_CommonCertsStorage.retail_CA3_raw[0];
raw_size = sizeof(_CommonCertsStorage.retail_CA3_raw);
}
break;
case CERT_RETAIL_XSc_IDENT:
if (!(_CommonCertsStorage.loaded_certs_flg & CERT_RETAIL_XSc_IDENT)) {
_cert = &_CommonCertsStorage.retail_XSc;
raw_space = &_CommonCertsStorage.retail_XSc_raw[0];
raw_size = sizeof(_CommonCertsStorage.retail_XSc_raw);
}
break;
case CERT_RETAIL_CPb_IDENT:
if (!(_CommonCertsStorage.loaded_certs_flg & CERT_RETAIL_CPb_IDENT)) {
_cert = &_CommonCertsStorage.retail_CPb;
raw_space = &_CommonCertsStorage.retail_CPb_raw[0];
raw_size = sizeof(_CommonCertsStorage.retail_CPb_raw);
}
break;
case CERT_DEV_CA4_IDENT:
if (!(_CommonCertsStorage.loaded_certs_flg & CERT_DEV_CA4_IDENT)) {
_cert = &_CommonCertsStorage.dev_CA4;
raw_space = &_CommonCertsStorage.dev_CA4_raw[0];
raw_size = sizeof(_CommonCertsStorage.dev_CA4_raw);
}
break;
case CERT_DEV_XS9_IDENT:
if (!(_CommonCertsStorage.loaded_certs_flg & CERT_DEV_XS9_IDENT)) {
_cert = &_CommonCertsStorage.dev_XS9;
raw_space = &_CommonCertsStorage.dev_XS9_raw[0];
raw_size = sizeof(_CommonCertsStorage.dev_XS9_raw);
}
break;
case CERT_DEV_CPa_IDENT:
if (!(_CommonCertsStorage.loaded_certs_flg & CERT_DEV_CPa_IDENT)) {
_cert = &_CommonCertsStorage.dev_CPa;
raw_space = &_CommonCertsStorage.dev_CPa_raw[0];
raw_size = sizeof(_CommonCertsStorage.dev_CPa_raw);
}
break;
default:
break;
}
if (!_cert || !raw_space || !raw_size)
return;
u32 sig_size = _Certificate_GetSignatureChunkSizeFromType(getbe32(cert->sig->sig_type));
u32 data_size = _Certificate_GetDataChunkSizeFromType(getbe32(cert->data->keytype));
if (sig_size == 0 || data_size == 0)
return;
if (sig_size + data_size != raw_size)
return;
if (!_Certificate_RawCopyImpl(cert, raw_space)) {
_CommonCertsStorage.loaded_certs_flg |= ident;
}
}
// grumble grumble, gotta avoid repeated code when possible or at least if significant enough
static u32 _DisaOpenCertDb(char (*path)[16], bool emunand, DisaDiffRWInfo* info, u8** cache, u32* offset, u32* max_offset) {
GetCertDBPath(*path, emunand);
u8* _cache = NULL;
if (GetDisaDiffRWInfo(*path, info, false) != 0) return 1;
_cache = (u8*)malloc(info->size_dpfs_lvl2);
if (!_cache) return 1;
if (BuildDisaDiffDpfsLvl2Cache(*path, info, _cache, info->size_dpfs_lvl2) != 0) {
free(_cache);
return 1;
}
CertsDbPartitionHeader header;
if (ReadDisaDiffIvfcLvl4(*path, info, 0, sizeof(CertsDbPartitionHeader), &header) != sizeof(CertsDbPartitionHeader)) {
free(_cache);
return 1;
}
if (getbe32(header.magic) != 0x43455254 /* 'CERT' */ ||
getbe32(header.unk) != 0 ||
getle32(header.used_size) & 0xFF) {
free(_cache);
return 1;
}
*cache = _cache;
*offset = sizeof(CertsDbPartitionHeader);
*max_offset = getle32(header.used_size) + sizeof(CertsDbPartitionHeader);
return 0;
}
static u32 _ProcessNextCertDbEntry(const char* path, DisaDiffRWInfo* info, Certificate* cert, u32 *full_size, char (*full_issuer)[0x41], u32* offset, u32 max_offset) {
u8 sig_type_data[4];
u8 keytype_data[4];
if (*offset + 4 > max_offset) return 1;
if (ReadDisaDiffIvfcLvl4(path, info, *offset, 4, sig_type_data) != 4)
return 1;
u32 sig_type = getbe32(sig_type_data);
if (sig_type == 0x10002 || sig_type == 0x10005) return 1; // ECC signs not allowed on db
u32 sig_size = _Certificate_GetSignatureChunkSizeFromType(sig_type);
if (sig_size == 0) return 1;
u32 keytype_off = *offset + sig_size + offsetof(CertificateBody, keytype);
if (keytype_off + 4 > max_offset) return 1;
if (ReadDisaDiffIvfcLvl4(path, info, keytype_off, 4, keytype_data) != 4)
return 1;
u32 keytype = getbe32(keytype_data);
if (keytype == 2) return 1; // ECC keys not allowed on db
u32 data_size = _Certificate_GetDataChunkSizeFromType(keytype);
if (data_size == 0) return 1;
*full_size = sig_size + data_size;
if (*offset + *full_size > max_offset) return 1;
cert->sig = (CertificateSignature*)malloc(sig_size);
cert->data = (CertificateBody*)malloc(data_size);
if (!cert->sig || !cert->data)
return 1;
if (ReadDisaDiffIvfcLvl4(path, info, *offset, sig_size, cert->sig) != sig_size)
return 1;
if (ReadDisaDiffIvfcLvl4(path, info, *offset + sig_size, data_size, cert->data) != data_size)
return 1;
if (!Certificate_IsValid(cert))
return 1;
if (snprintf(*full_issuer, 0x41, "%s-%s", cert->data->issuer, cert->data->name) > 0x40)
return 1;
return 0;
}
// certificates returned by this call are not to be deemed safe to edit, pointers or pointed data
u32 LoadCertFromCertDb(Certificate* cert, const char* issuer) {
if (!issuer || !cert) return 1;
u32 _ident = _Issuer_To_StorageIdent(issuer);
if (_LoadFromCertStorage(cert, _ident)) {
return 0;
}
int ret = 1;
for (int i = 0; i < 2 && ret; ++i) {
Certificate cert_local = CERTIFICATE_NULL_INIT;
char path[16];
DisaDiffRWInfo info;
u8* cache;
u32 offset, max_offset;
if (_DisaOpenCertDb(&path, i ? true : false, &info, &cache, &offset, &max_offset))
return 1;
// certs.db has no filesystem.. its pretty plain, certificates after another
// but also, certificates are not equally sized
// so most cases of bad data, leads to giving up
while (offset < max_offset) {
char full_issuer[0x41];
u32 full_size;
if (_ProcessNextCertDbEntry(path, &info, &cert_local, &full_size, &full_issuer, &offset, max_offset))
break;
if (!strcmp(full_issuer, issuer)) {
ret = 0;
break;
}
_Certificate_CleanupImpl(&cert_local);
offset += full_size;
}
if (ret) {
_Certificate_CleanupImpl(&cert_local);
} else {
*cert = cert_local;
_SaveToCertStorage(&cert_local, _ident);
}
free(cache);
}
return ret;
}
// I dont expect many certs on a cert bundle, so I'll cap it to 8
u32 BuildRawCertBundleFromCertDb(void* rawout, size_t* size, const char* const* cert_issuers, int count) {
if (!rawout || !size || !cert_issuers || count < 0 || count > 8) return 1;
if (!*size && count) return 1;
if (!count) { // *shrug*
*size = 0;
return 0;
}
for (int i = 0; i < count; ++i) {
if (!cert_issuers[i])
return 1;
}
Certificate certs[8];
u8 certs_loaded = 0;
memset(certs, 0, sizeof(certs));
int loaded_count = 0;
// search static storage first
for (int i = 0; i < count; ++i) {
u32 _ident = _Issuer_To_StorageIdent(cert_issuers[i]);
if (_LoadFromCertStorage(&certs[i], _ident)) {
certs_loaded |= BIT(i);
++loaded_count;
}
}
int ret = 0;
for (int i = 0; i < 2 && loaded_count != count && !ret; ++i) {
Certificate cert_local = CERTIFICATE_NULL_INIT;
char path[16];
DisaDiffRWInfo info;
u8* cache;
u32 offset, max_offset;
if (_DisaOpenCertDb(&path, i ? true : false, &info, &cache, &offset, &max_offset))
continue;
while (offset < max_offset) {
char full_issuer[0x41];
u32 full_size;
if (_ProcessNextCertDbEntry(path, &info, &cert_local, &full_size, &full_issuer, &offset, max_offset))
break;
for (int j = 0; j < count; j++) {
if (certs_loaded & BIT(j)) continue;
if (!strcmp(full_issuer, cert_issuers[j])) {
ret = _Certificate_AllocCopyOutImpl(&cert_local, &certs[j]);
if (ret) break;
certs_loaded |= BIT(j);
++loaded_count;
}
}
// while at it, try to save to static storage, if applicable
u32 _ident = _Issuer_To_StorageIdent(full_issuer);
_SaveToCertStorage(&cert_local, _ident);
_Certificate_CleanupImpl(&cert_local);
if (loaded_count == count || ret) // early exit
break;
offset += full_size;
}
free(cache);
}
if (!ret && loaded_count == count) {
u8* out = (u8*)rawout;
size_t limit = *size, written = 0;
for (int i = 0; i < count; ++i) {
u32 sig_size = _Certificate_GetSignatureChunkSizeFromType(getbe32(certs[i].sig->sig_type));
u32 data_size = _Certificate_GetDataChunkSizeFromType(getbe32(certs[i].data->keytype));
if (sig_size == 0 || data_size == 0) {
ret = 1;
break;
}
u32 full_size = sig_size + data_size;
if (written + full_size > limit) {
ret = 1;
break;
}
if (_Certificate_RawCopyImpl(&certs[i], out)) {
ret = 1;
break;
}
out += full_size;
written += full_size;
}
if (!ret)
*size = written;
} else {
ret = 1;
}
for (int i = 0; i < count; ++i) {
if (certs_loaded & BIT(i))
_Certificate_CleanupImpl(&certs[i]);
}
return ret;
}

View File

@ -2,21 +2,54 @@
#include "common.h" #include "common.h"
#define CERT_SIZE sizeof(Certificate) #define CERT_MAX_SIZE (sizeof(CertificateSignature) + 0x23C + sizeof(CertificateBody) + 0x238)
#define CERT_RSA4096_SIG_SIZE (sizeof(CertificateSignature) + 0x23C)
#define CERT_RSA2048_SIG_SIZE (sizeof(CertificateSignature) + 0x13C)
#define CERT_ECC_SIG_SIZE (sizeof(CertificateSignature) + 0x7C)
#define CERT_RSA4096_BODY_SIZE (sizeof(CertificateBody) + 0x238)
#define CERT_RSA2048_BODY_SIZE (sizeof(CertificateBody) + 0x138)
#define CERT_ECC_BODY_SIZE (sizeof(CertificateBody) + 0x78)
#define CERTIFICATE_NULL_INIT ((Certificate){NULL, NULL})
// from: http://3dbrew.org/wiki/Certificates // from: http://3dbrew.org/wiki/Certificates
// all numbers in big endian // all numbers in big endian
typedef struct { typedef struct {
u8 sig_type[4]; // expected: 0x010004 / RSA_2048 SHA256 u8 sig_type[4];
u8 signature[0x100]; u8 signature[];
u8 padding0[0x3C]; } PACKED_ALIGN(1) CertificateSignature;
u8 issuer[0x40];
u8 keytype[4]; // expected: 0x01 / RSA_2048
u8 name[0x40];
u8 unknown[4];
u8 mod[0x100];
u8 exp[0x04];
u8 padding1[0x34];
} PACKED_STRUCT Certificate;
u32 LoadCertFromCertDb(u64 offset, Certificate* cert, u32* mod, u32* exp); typedef struct {
char issuer[0x40];
u8 keytype[4];
char name[0x40];
u8 expiration[4];
u8 pub_key_data[];
} PACKED_ALIGN(1) CertificateBody;
typedef struct {
CertificateSignature* sig;
CertificateBody* data;
} Certificate;
bool Certificate_IsValid(const Certificate* cert);
bool Certificate_IsRSA(const Certificate* cert);
bool Certificate_IsECC(const Certificate* cert);
u32 Certificate_GetSignatureSize(const Certificate* cert, u32* size);
u32 Certificate_GetModulusSize(const Certificate* cert, u32* size);
u32 Certificate_GetModulus(const Certificate* cert, void* mod);
u32 Certificate_GetExponent(const Certificate* cert, void* exp);
u32 Certificate_GetEccSingleCoordinateSize(const Certificate* cert, u32* size);
u32 Certificate_GetEccXY(const Certificate* cert, void* X, void* Y);
u32 Certificate_GetSignatureChunkSize(const Certificate* cert, u32* size);
u32 Certificate_GetDataChunkSize(const Certificate* cert, u32* size);
u32 Certificate_GetFullSize(const Certificate* cert, u32* size);
u32 Certificate_VerifySignatureBlock(const Certificate* cert, const void* sig, u32 sig_size, const void* data, u32 data_size, bool sha256);
u32 Certificate_MakeEditSafe(Certificate* cert);
u32 Certificate_AllocCopyOut(const Certificate* cert, Certificate* out_cert);
u32 Certificate_RawCopy(const Certificate* cert, void* raw);
u32 Certificate_Cleanup(Certificate* cert);
u32 LoadCertFromCertDb(Certificate* cert, const char* issuer);
u32 BuildRawCertBundleFromCertDb(void* rawout, size_t* size, const char* const* cert_issuers, int count);

View File

@ -4,6 +4,7 @@
#include "ff.h" #include "ff.h"
#include "aes.h" #include "aes.h"
#include "sha.h" #include "sha.h"
#include "cert.h"
u32 ValidateCiaHeader(CiaHeader* header) { u32 ValidateCiaHeader(CiaHeader* header) {
if ((header->size_header != CIA_HEADER_SIZE) || if ((header->size_header != CIA_HEADER_SIZE) ||
@ -18,20 +19,20 @@ u32 ValidateCiaHeader(CiaHeader* header) {
u32 GetCiaInfo(CiaInfo* info, CiaHeader* header) { u32 GetCiaInfo(CiaInfo* info, CiaHeader* header) {
if ((u8*) info != (u8*) header) memcpy(info, header, 0x20); // take over first 0x20 byte if ((u8*) info != (u8*) header) memcpy(info, header, 0x20); // take over first 0x20 byte
info->offset_cert = align(header->size_header, 64); info->offset_cert = align(header->size_header, 64);
info->offset_ticket = info->offset_cert + align(header->size_cert, 64); info->offset_ticket = info->offset_cert + align(header->size_cert, 64);
info->offset_tmd = info->offset_ticket + align(header->size_ticket, 64); info->offset_tmd = info->offset_ticket + align(header->size_ticket, 64);
info->offset_content = info->offset_tmd + align(header->size_tmd, 64); info->offset_content = info->offset_tmd + align(header->size_tmd, 64);
info->offset_meta = (header->size_meta) ? info->offset_content + align(header->size_content, 64) : 0; info->offset_meta = (header->size_meta) ? info->offset_content + align(header->size_content, 64) : 0;
info->offset_content_list = info->offset_tmd + sizeof(TitleMetaData); info->offset_content_list = info->offset_tmd + sizeof(TitleMetaData);
info->size_content_list = info->size_tmd - sizeof(TitleMetaData); info->size_content_list = info->size_tmd - sizeof(TitleMetaData);
info->size_cia = (header->size_meta) ? info->offset_meta + info->size_meta : info->size_cia = (header->size_meta) ? info->offset_meta + info->size_meta :
info->offset_content + info->size_content; info->offset_content + info->size_content;
info->max_contents = (info->size_tmd - sizeof(TitleMetaData)) / sizeof(TmdContentChunk); info->max_contents = (info->size_tmd - sizeof(TitleMetaData)) / sizeof(TmdContentChunk);
return 0; return 0;
} }
@ -58,29 +59,22 @@ u32 BuildCiaCert(u8* ciacert) {
0xFB, 0xD2, 0xC0, 0x47, 0x95, 0xB9, 0x4C, 0xC8, 0x0B, 0x64, 0x58, 0x96, 0xF6, 0x61, 0x0F, 0x52, 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 0x18, 0x83, 0xAF, 0xE0, 0xF4, 0xE5, 0x62, 0xBA, 0x69, 0xEE, 0x72, 0x2A, 0xC2, 0x4E, 0x95, 0xB3
}; };
// open certs.db file on SysNAND static const char* const retail_issuers[] = {"Root-CA00000003", "Root-CA00000003-XS0000000c", "Root-CA00000003-CP0000000b"};
FIL db; static const char* const dev_issuers[] = {"Root-CA00000004", "Root-CA00000004-XS00000009", "Root-CA00000004-CP0000000a"};
UINT bytes_read;
if (f_open(&db, "1:/dbs/certs.db", FA_READ | FA_OPEN_EXISTING) != FR_OK) size_t size = CIA_CERT_SIZE;
if (BuildRawCertBundleFromCertDb(ciacert, &size, !IS_DEVKIT ? retail_issuers : dev_issuers, 3) ||
size != CIA_CERT_SIZE) {
return 1; return 1;
// grab CIA cert from 4 offsets }
f_lseek(&db, 0x0C10);
f_read(&db, ciacert + 0x000, 0x1F0, &bytes_read);
f_lseek(&db, 0x3A00);
f_read(&db, ciacert + 0x1F0, 0x210, &bytes_read);
f_lseek(&db, 0x3F10);
f_read(&db, ciacert + 0x400, 0x300, &bytes_read);
f_lseek(&db, 0x3C10);
f_read(&db, ciacert + 0x700, 0x300, &bytes_read);
f_close(&db);
// check the certificate hash // check the certificate hash
u8 cert_hash[0x20]; u8 cert_hash[0x20];
sha_quick(cert_hash, ciacert, CIA_CERT_SIZE, SHA256_MODE); sha_quick(cert_hash, ciacert, CIA_CERT_SIZE, SHA256_MODE);
if (memcmp(cert_hash, IS_DEVKIT ? cert_hash_expected_dev : cert_hash_expected, 0x20) != 0) if (memcmp(cert_hash, IS_DEVKIT ? cert_hash_expected_dev : cert_hash_expected, 0x20) != 0)
return 1; return 1;
return 0; return 0;
} }

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,4 +1,5 @@
#include "codelzss.h" #include "codelzss.h"
#include "language.h"
#include "ui.h" #include "ui.h"
#define CODE_COMP_SIZE(f) ((f)->off_size_comp & 0xFFFFFF) #define CODE_COMP_SIZE(f) ((f)->off_size_comp & 0xFFFFFF)
@ -16,10 +17,10 @@ typedef struct {
u32 GetCodeLzssUncompressedSize(void* footer, u32 comp_size) { u32 GetCodeLzssUncompressedSize(void* footer, u32 comp_size) {
if (comp_size < sizeof(CodeLzssFooter)) return 0; if (comp_size < sizeof(CodeLzssFooter)) return 0;
CodeLzssFooter* f = (CodeLzssFooter*) footer; CodeLzssFooter* f = (CodeLzssFooter*) footer;
if ((CODE_COMP_SIZE(f) > comp_size) || (CODE_COMP_END(f) < 0)) return 0; if ((CODE_COMP_SIZE(f) > comp_size) || (CODE_COMP_END(f) < 0)) return 0;
return CODE_DEC_SIZE(f) + (comp_size - CODE_COMP_SIZE(f)); return CODE_DEC_SIZE(f) + (comp_size - CODE_COMP_SIZE(f));
} }
@ -27,40 +28,40 @@ u32 GetCodeLzssUncompressedSize(void* footer, u32 comp_size) {
u32 DecompressCodeLzss(u8* code, u32* code_size, u32 max_size) { u32 DecompressCodeLzss(u8* code, u32* code_size, u32 max_size) {
u8* data_start = code; u8* data_start = code;
u8* comp_start = data_start; u8* comp_start = data_start;
// get footer, fix comp_start offset // get footer, fix comp_start offset
if ((*code_size < sizeof(CodeLzssFooter)) || (*code_size > max_size)) return 1; if ((*code_size < sizeof(CodeLzssFooter)) || (*code_size > max_size)) return 1;
CodeLzssFooter* footer = (CodeLzssFooter*) (void*) (data_start + *code_size - sizeof(CodeLzssFooter)); CodeLzssFooter* footer = (CodeLzssFooter*) (void*) (data_start + *code_size - sizeof(CodeLzssFooter));
if (CODE_COMP_SIZE(footer) <= *code_size) comp_start += *code_size - CODE_COMP_SIZE(footer); if (CODE_COMP_SIZE(footer) <= *code_size) comp_start += *code_size - CODE_COMP_SIZE(footer);
else return 1; else return 1;
// more sanity checks // more sanity checks
if ((CODE_COMP_END(footer) < 0) || (CODE_DEC_SIZE(footer) > max_size)) if ((CODE_COMP_END(footer) < 0) || (CODE_DEC_SIZE(footer) > max_size))
return 1; // not reverse LZSS compressed code or too big uncompressed return 1; // not reverse LZSS compressed code or too big uncompressed
// set pointers // set pointers
u8* data_end = (u8*) comp_start + CODE_DEC_SIZE(footer); u8* data_end = (u8*) comp_start + CODE_DEC_SIZE(footer);
u8* ptr_in = (u8*) comp_start + CODE_COMP_END(footer); u8* ptr_in = (u8*) comp_start + CODE_COMP_END(footer);
u8* ptr_out = data_end; u8* ptr_out = data_end;
// main decompression loop // main decompression loop
while ((ptr_in > comp_start) && (ptr_out > comp_start)) { while ((ptr_in > comp_start) && (ptr_out > comp_start)) {
if (!ShowProgress(data_end - ptr_out, data_end - data_start, "Decompressing .code...")) { if (!ShowProgress(data_end - ptr_out, data_end - data_start, STR_DECOMPRESSING_DOT_CODE)) {
if (ShowPrompt(true, "Decompressing .code...\nB button detected. Cancel?")) return 1; if (ShowPrompt(true, "%s", STR_DECOMPRESSING_DOT_CODE_B_DETECTED_CANCEL)) return 1;
ShowProgress(0, data_end - data_start, "Decompressing .code..."); ShowProgress(0, data_end - data_start, STR_DECOMPRESSING_DOT_CODE);
ShowProgress(data_end - ptr_out, data_end - data_start, "Decompressing .code..."); ShowProgress(data_end - ptr_out, data_end - data_start, STR_DECOMPRESSING_DOT_CODE);
} }
// sanity check // sanity check
if (ptr_out < ptr_in) return 1; if (ptr_out < ptr_in) return 1;
// read and process control byte // read and process control byte
u8 ctrlbyte = *(--ptr_in); u8 ctrlbyte = *(--ptr_in);
for (int i = 7; i >= 0; i--) { for (int i = 7; i >= 0; i--) {
// end conditions met? // end conditions met?
if ((ptr_in <= comp_start) || (ptr_out <= comp_start)) if ((ptr_in <= comp_start) || (ptr_out <= comp_start))
break; break;
// process control byte // process control byte
if ((ctrlbyte >> i) & 0x1) { if ((ctrlbyte >> i) & 0x1) {
// control bit set, read segment code // control bit set, read segment code
@ -69,11 +70,11 @@ u32 DecompressCodeLzss(u8* code, u32* code_size, u32 max_size) {
if (ptr_in < comp_start) return 1; // corrupted code if (ptr_in < comp_start) return 1; // corrupted code
u32 seg_off = CODE_SEG_OFFSET(seg_code); u32 seg_off = CODE_SEG_OFFSET(seg_code);
u32 seg_len = CODE_SEG_SIZE(seg_code); u32 seg_len = CODE_SEG_SIZE(seg_code);
// sanity check for output pointer // sanity check for output pointer
if ((ptr_out - seg_len < comp_start) || (ptr_out + seg_off >= data_end)) if ((ptr_out - seg_len < comp_start) || (ptr_out + seg_off >= data_end))
return 1; return 1;
// copy data to the correct place // copy data to the correct place
for (u32 c = 0; c < seg_len; c++) { for (u32 c = 0; c < seg_len; c++) {
u8 byte = *(ptr_out + seg_off); u8 byte = *(ptr_out + seg_off);
@ -83,17 +84,17 @@ u32 DecompressCodeLzss(u8* code, u32* code_size, u32 max_size) {
// sanity check for both pointers // sanity check for both pointers
if ((ptr_out == comp_start) || (ptr_in == comp_start)) if ((ptr_out == comp_start) || (ptr_in == comp_start))
return 1; return 1;
// control bit not set, copy byte verbatim // control bit not set, copy byte verbatim
*(--ptr_out) = *(--ptr_in); *(--ptr_out) = *(--ptr_in);
} }
} }
} }
// check pointers // check pointers
if ((ptr_in != comp_start) || (ptr_out != comp_start)) if ((ptr_in != comp_start) || (ptr_out != comp_start))
return 1; return 1;
// all fine if arriving here - return the result // all fine if arriving here - return the result
*code_size = data_end - data_start; *code_size = data_end - data_start;
return 0; return 0;
@ -116,7 +117,7 @@ void initTable(sCompressInfo* a_pInfo, void* a_pWork) {
a_pInfo->ReversedOffsetTable = (s16*)(a_pWork) + 4098; a_pInfo->ReversedOffsetTable = (s16*)(a_pWork) + 4098;
a_pInfo->ByteTable = (s16*)(a_pWork) + 4098 + 4098; a_pInfo->ByteTable = (s16*)(a_pWork) + 4098 + 4098;
a_pInfo->EndTable = (s16*)(a_pWork) + 4098 + 4098 + 256; a_pInfo->EndTable = (s16*)(a_pWork) + 4098 + 4098 + 256;
for (int i = 0; i < 256; i++) { for (int i = 0; i < 256; i++) {
a_pInfo->ByteTable[i] = -1; a_pInfo->ByteTable[i] = -1;
a_pInfo->EndTable[i] = -1; a_pInfo->EndTable[i] = -1;
@ -127,35 +128,35 @@ int search(sCompressInfo* a_pInfo, const u8* a_pSrc, int* a_nOffset, int a_nMaxS
if (a_nMaxSize < 3) { if (a_nMaxSize < 3) {
return 0; return 0;
} }
const u8* pSearch = NULL; const u8* pSearch = NULL;
int nSize = 2; int nSize = 2;
const u16 uWindowPos = a_pInfo->WindowPos; const u16 uWindowPos = a_pInfo->WindowPos;
const u16 uWindowLen = a_pInfo->WindowLen; const u16 uWindowLen = a_pInfo->WindowLen;
s16* pReversedOffsetTable = a_pInfo->ReversedOffsetTable; s16* pReversedOffsetTable = a_pInfo->ReversedOffsetTable;
for (s16 nOffset = a_pInfo->EndTable[*(a_pSrc - 1)]; nOffset != -1; nOffset = pReversedOffsetTable[nOffset]) { for (s16 nOffset = a_pInfo->EndTable[*(a_pSrc - 1)]; nOffset != -1; nOffset = pReversedOffsetTable[nOffset]) {
if (nOffset < uWindowPos) { if (nOffset < uWindowPos) {
pSearch = a_pSrc + uWindowPos - nOffset; pSearch = a_pSrc + uWindowPos - nOffset;
} else { } else {
pSearch = a_pSrc + uWindowLen + uWindowPos - nOffset; pSearch = a_pSrc + uWindowLen + uWindowPos - nOffset;
} }
if (pSearch - a_pSrc < 3) { if (pSearch - a_pSrc < 3) {
continue; continue;
} }
if (*(pSearch - 2) != *(a_pSrc - 2) || *(pSearch - 3) != *(a_pSrc - 3)) { if (*(pSearch - 2) != *(a_pSrc - 2) || *(pSearch - 3) != *(a_pSrc - 3)) {
continue; continue;
} }
int nMaxSize = (int)((s64)min(a_nMaxSize, pSearch - a_pSrc)); int nMaxSize = (int)((s64)min(a_nMaxSize, pSearch - a_pSrc));
int nCurrentSize = 3; int nCurrentSize = 3;
while (nCurrentSize < nMaxSize && *(pSearch - nCurrentSize - 1) == *(a_pSrc - nCurrentSize - 1)) { while (nCurrentSize < nMaxSize && *(pSearch - nCurrentSize - 1) == *(a_pSrc - nCurrentSize - 1)) {
nCurrentSize++; nCurrentSize++;
} }
if (nCurrentSize > nSize) { if (nCurrentSize > nSize) {
nSize = nCurrentSize; nSize = nCurrentSize;
*a_nOffset = (int)(pSearch - a_pSrc); *a_nOffset = (int)(pSearch - a_pSrc);
@ -164,11 +165,11 @@ int search(sCompressInfo* a_pInfo, const u8* a_pSrc, int* a_nOffset, int a_nMaxS
} }
} }
} }
if (nSize < 3) { if (nSize < 3) {
return 0; return 0;
} }
return nSize; return nSize;
} }
@ -181,33 +182,33 @@ void slideByte(sCompressInfo* a_pInfo, const u8* a_pSrc) {
s16* pReversedOffsetTable = a_pInfo->ReversedOffsetTable; s16* pReversedOffsetTable = a_pInfo->ReversedOffsetTable;
s16* pByteTable = a_pInfo->ByteTable; s16* pByteTable = a_pInfo->ByteTable;
s16* pEndTable = a_pInfo->EndTable; s16* pEndTable = a_pInfo->EndTable;
if (uWindowLen == 4098) { if (uWindowLen == 4098) {
u8 uOutData = *(a_pSrc + 4097); u8 uOutData = *(a_pSrc + 4097);
if ((pByteTable[uOutData] = pOffsetTable[pByteTable[uOutData]]) == -1) { if ((pByteTable[uOutData] = pOffsetTable[pByteTable[uOutData]]) == -1) {
pEndTable[uOutData] = -1; pEndTable[uOutData] = -1;
} else { } else {
pReversedOffsetTable[pByteTable[uOutData]] = -1; pReversedOffsetTable[pByteTable[uOutData]] = -1;
} }
uInsertOffset = uWindowPos; uInsertOffset = uWindowPos;
} else { } else {
uInsertOffset = uWindowLen; uInsertOffset = uWindowLen;
} }
s16 nOffset = pEndTable[uInData]; s16 nOffset = pEndTable[uInData];
if (nOffset == -1) { if (nOffset == -1) {
pByteTable[uInData] = uInsertOffset; pByteTable[uInData] = uInsertOffset;
} else { } else {
pOffsetTable[nOffset] = uInsertOffset; pOffsetTable[nOffset] = uInsertOffset;
} }
pEndTable[uInData] = uInsertOffset; pEndTable[uInData] = uInsertOffset;
pOffsetTable[uInsertOffset] = -1; pOffsetTable[uInsertOffset] = -1;
pReversedOffsetTable[uInsertOffset] = nOffset; pReversedOffsetTable[uInsertOffset] = nOffset;
if (uWindowLen == 4098) { if (uWindowLen == 4098) {
a_pInfo->WindowPos = (uWindowPos + 1) % 4098; a_pInfo->WindowPos = (uWindowPos + 1) % 4098;
} else { } else {
@ -228,42 +229,42 @@ s64 alignBytes(s64 a_nData, s64 a_nAlignment) {
bool CompressCodeLzss(const u8* a_pUncompressed, u32 a_uUncompressedSize, u8* a_pCompressed, u32* a_uCompressedSize) { bool CompressCodeLzss(const u8* a_pUncompressed, u32 a_uUncompressedSize, u8* a_pCompressed, u32* a_uCompressedSize) {
const int s_nCompressWorkSize = (4098 + 4098 + 256 + 256) * sizeof(s16); const int s_nCompressWorkSize = (4098 + 4098 + 256 + 256) * sizeof(s16);
bool bResult = true; bool bResult = true;
if (a_uUncompressedSize > sizeof(CodeLzssFooter) && *a_uCompressedSize >= a_uUncompressedSize) { if (a_uUncompressedSize > sizeof(CodeLzssFooter) && *a_uCompressedSize >= a_uUncompressedSize) {
u8* pWork = malloc(s_nCompressWorkSize * sizeof(u8)); u8* pWork = malloc(s_nCompressWorkSize * sizeof(u8));
if (!pWork) return false; if (!pWork) return false;
do { do {
sCompressInfo info; sCompressInfo info;
initTable(&info, pWork); initTable(&info, pWork);
const int nMaxSize = 0xF + 3; const int nMaxSize = 0xF + 3;
const u8* pSrc = a_pUncompressed + a_uUncompressedSize; const u8* pSrc = a_pUncompressed + a_uUncompressedSize;
u8* pDest = a_pCompressed + a_uUncompressedSize; u8* pDest = a_pCompressed + a_uUncompressedSize;
while (pSrc - a_pUncompressed > 0 && pDest - a_pCompressed > 0) { while (pSrc - a_pUncompressed > 0 && pDest - a_pCompressed > 0) {
if (!ShowProgress((u32)(a_pUncompressed + a_uUncompressedSize - pSrc), a_uUncompressedSize, "Compressing .code...")) { if (!ShowProgress((u32)(a_pUncompressed + a_uUncompressedSize - pSrc), a_uUncompressedSize, STR_COMPRESSING_DOT_CODE)) {
if (ShowPrompt(true, "Compressing .code...\nB button detected. Cancel?")) { if (ShowPrompt(true, "%s", STR_COMPRESSING_DOT_CODE_B_DETECTED_CANCEL)) {
bResult = false; bResult = false;
break; break;
} }
ShowProgress(0, a_uUncompressedSize, "Compressing .code..."); ShowProgress(0, a_uUncompressedSize, STR_COMPRESSING_DOT_CODE);
ShowProgress((u32)(a_pUncompressed + a_uUncompressedSize - pSrc), a_uUncompressedSize, "Compressing .code..."); ShowProgress((u32)(a_pUncompressed + a_uUncompressedSize - pSrc), a_uUncompressedSize, STR_COMPRESSING_DOT_CODE);
} }
u8* pFlag = --pDest; u8* pFlag = --pDest;
*pFlag = 0; *pFlag = 0;
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
int nOffset = 0; int nOffset = 0;
int nSize = search(&info, pSrc, &nOffset, (int)((s64)min((s64)min(nMaxSize, pSrc - a_pUncompressed), a_pUncompressed + a_uUncompressedSize - pSrc))); int nSize = search(&info, pSrc, &nOffset, (int)((s64)min((s64)min(nMaxSize, pSrc - a_pUncompressed), a_pUncompressed + a_uUncompressedSize - pSrc)));
if (nSize < 3) { if (nSize < 3) {
if (pDest - a_pCompressed < 1) { if (pDest - a_pCompressed < 1) {
bResult = false; bResult = false;
break; break;
} }
slide(&info, pSrc, 1); slide(&info, pSrc, 1);
*--pDest = *--pSrc; *--pDest = *--pSrc;
} else { } else {
@ -271,7 +272,7 @@ bool CompressCodeLzss(const u8* a_pUncompressed, u32 a_uUncompressedSize, u8* a_
bResult = false; bResult = false;
break; break;
} }
*pFlag |= 0x80 >> i; *pFlag |= 0x80 >> i;
slide(&info, pSrc, nSize); slide(&info, pSrc, nSize);
pSrc -= nSize; pSrc -= nSize;
@ -279,29 +280,29 @@ bool CompressCodeLzss(const u8* a_pUncompressed, u32 a_uUncompressedSize, u8* a_
*--pDest = (nSize << 4 & 0xF0) | ((nOffset - 3) >> 8 & 0x0F); *--pDest = (nSize << 4 & 0xF0) | ((nOffset - 3) >> 8 & 0x0F);
*--pDest = (nOffset - 3) & 0xFF; *--pDest = (nOffset - 3) & 0xFF;
} }
if (pSrc - a_pUncompressed <= 0) { if (pSrc - a_pUncompressed <= 0) {
break; break;
} }
} }
if (!bResult) { if (!bResult) {
break; break;
} }
} }
if (!bResult) { if (!bResult) {
break; break;
} }
*a_uCompressedSize = (u32)(a_pCompressed + a_uUncompressedSize - pDest); *a_uCompressedSize = (u32)(a_pCompressed + a_uUncompressedSize - pDest);
} while (false); } while (false);
free(pWork); free(pWork);
} else { } else {
bResult = false; bResult = false;
} }
if (bResult) { if (bResult) {
u32 uOrigSize = a_uUncompressedSize; u32 uOrigSize = a_uUncompressedSize;
u8* pCompressBuffer = a_pCompressed + a_uUncompressedSize - *a_uCompressedSize; u8* pCompressBuffer = a_pCompressed + a_uUncompressedSize - *a_uCompressedSize;
@ -309,10 +310,10 @@ bool CompressCodeLzss(const u8* a_pUncompressed, u32 a_uUncompressedSize, u8* a_
u32 uOrigSafe = 0; u32 uOrigSafe = 0;
u32 uCompressSafe = 0; u32 uCompressSafe = 0;
bool bOver = false; bool bOver = false;
while (uOrigSize > 0) { while (uOrigSize > 0) {
u8 uFlag = pCompressBuffer[--uCompressBufferSize]; u8 uFlag = pCompressBuffer[--uCompressBufferSize];
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
if ((uFlag << i & 0x80) == 0) { if ((uFlag << i & 0x80) == 0) {
uCompressBufferSize--; uCompressBufferSize--;
@ -321,7 +322,7 @@ bool CompressCodeLzss(const u8* a_pUncompressed, u32 a_uUncompressedSize, u8* a_
int nSize = (pCompressBuffer[--uCompressBufferSize] >> 4 & 0x0F) + 3; int nSize = (pCompressBuffer[--uCompressBufferSize] >> 4 & 0x0F) + 3;
uCompressBufferSize--; uCompressBufferSize--;
uOrigSize -= nSize; uOrigSize -= nSize;
if (uOrigSize < uCompressBufferSize) { if (uOrigSize < uCompressBufferSize) {
uOrigSafe = uOrigSize; uOrigSafe = uOrigSize;
uCompressSafe = uCompressBufferSize; uCompressSafe = uCompressBufferSize;
@ -329,24 +330,24 @@ bool CompressCodeLzss(const u8* a_pUncompressed, u32 a_uUncompressedSize, u8* a_
break; break;
} }
} }
if (uOrigSize <= 0) { if (uOrigSize <= 0) {
break; break;
} }
} }
if (bOver) { if (bOver) {
break; break;
} }
} }
u32 uCompressedSize = *a_uCompressedSize - uCompressSafe; u32 uCompressedSize = *a_uCompressedSize - uCompressSafe;
u32 uPadOffset = uOrigSafe + uCompressedSize; u32 uPadOffset = uOrigSafe + uCompressedSize;
u32 uCompFooterOffset = (u32)(alignBytes(uPadOffset, 4)); u32 uCompFooterOffset = (u32)(alignBytes(uPadOffset, 4));
*a_uCompressedSize = uCompFooterOffset + sizeof(CodeLzssFooter); *a_uCompressedSize = uCompFooterOffset + sizeof(CodeLzssFooter);
u32 uTop = *a_uCompressedSize - uOrigSafe; u32 uTop = *a_uCompressedSize - uOrigSafe;
u32 uBottom = *a_uCompressedSize - uPadOffset; u32 uBottom = *a_uCompressedSize - uPadOffset;
if (*a_uCompressedSize >= a_uUncompressedSize || uTop > 0xFFFFFF) { if (*a_uCompressedSize >= a_uUncompressedSize || uTop > 0xFFFFFF) {
bResult = false; bResult = false;
} else { } else {
@ -358,6 +359,6 @@ bool CompressCodeLzss(const u8* a_pUncompressed, u32 a_uUncompressedSize, u8* a_
pCompFooter->addsize_dec = a_uUncompressedSize - *a_uCompressedSize; pCompFooter->addsize_dec = a_uUncompressedSize - *a_uCompressedSize;
} }
} }
return bResult; return bResult;
} }

View File

@ -107,13 +107,13 @@ inline static u32 DisaDiffSize(const TCHAR* path) {
inline static FRESULT DisaDiffOpen(const TCHAR* path) { inline static FRESULT DisaDiffOpen(const TCHAR* path) {
FRESULT res = FR_OK; FRESULT res = FR_OK;
ddfp = NULL; ddfp = NULL;
if (path) { if (path) {
res = fvx_open(&ddfile, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING); res = fvx_open(&ddfile, path, FA_READ | FA_WRITE | FA_OPEN_EXISTING);
if (res == FR_OK) ddfp = &ddfile; if (res == FR_OK) ddfp = &ddfile;
} else if (!GetMountState()) res = FR_DENIED; } else if (!GetMountState()) res = FR_DENIED;
return res; return res;
} }
@ -164,16 +164,16 @@ u32 GetDisaDiffRWInfo(const char* path, DisaDiffRWInfo* info, bool partitionB) {
static const u8 ivfc_magic[] = { IVFC_MAGIC }; static const u8 ivfc_magic[] = { IVFC_MAGIC };
static const u8 dpfs_magic[] = { DPFS_MAGIC }; static const u8 dpfs_magic[] = { DPFS_MAGIC };
static const u8 difi_magic[] = { DIFI_MAGIC }; static const u8 difi_magic[] = { DIFI_MAGIC };
// reset reader info // reset reader info
memset(info, 0x00, sizeof(DisaDiffRWInfo)); memset(info, 0x00, sizeof(DisaDiffRWInfo));
// get file size, header at header offset // get file size, header at header offset
u32 file_size = DisaDiffSize(path); u32 file_size = DisaDiffSize(path);
u8 header[0x100]; u8 header[0x100];
if (DisaDiffQRead(path, header, 0x100, 0x100) != FR_OK) if (DisaDiffQRead(path, header, 0x100, 0x100) != FR_OK)
return 1; return 1;
// DISA/DIFF header: find partition offset & size and DIFI descriptor // DISA/DIFF header: find partition offset & size and DIFI descriptor
u32 offset_partition = 0; u32 offset_partition = 0;
u32 size_partition = 0; u32 size_partition = 0;
@ -207,22 +207,22 @@ u32 GetDisaDiffRWInfo(const char* path, DisaDiffRWInfo* info, bool partitionB) {
} else { } else {
return 1; return 1;
} }
// check the output so far // check the output so far
if (!offset_difi || (offset_difi + sizeof(DifiStruct) > file_size) || (offset_partition + size_partition > file_size)) if (!offset_difi || (offset_difi + sizeof(DifiStruct) > file_size) || (offset_partition + size_partition > file_size))
return 1; return 1;
info->offset_difi = offset_difi; info->offset_difi = offset_difi;
// read DIFI struct from file // read DIFI struct from file
const DifiStruct difis; const DifiStruct difis;
if (DisaDiffQRead(path, (DifiStruct*) &difis, offset_difi, sizeof(DifiStruct)) != FR_OK) if (DisaDiffQRead(path, (DifiStruct*) &difis, offset_difi, sizeof(DifiStruct)) != FR_OK)
return 1; return 1;
if ((memcmp(difis.difi.magic, difi_magic, 8) != 0) || if ((memcmp(difis.difi.magic, difi_magic, 8) != 0) ||
(memcmp(difis.ivfc.magic, ivfc_magic, 8) != 0) || (memcmp(difis.ivfc.magic, ivfc_magic, 8) != 0) ||
(memcmp(difis.dpfs.magic, dpfs_magic, 8) != 0)) (memcmp(difis.dpfs.magic, dpfs_magic, 8) != 0))
return 1; return 1;
// check & get data from DIFI header // check & get data from DIFI header
const DifiHeader* difi = &(difis.difi); const DifiHeader* difi = &(difis.difi);
if ((difi->offset_ivfc != sizeof(DifiHeader)) || if ((difi->offset_ivfc != sizeof(DifiHeader)) ||
@ -232,12 +232,12 @@ u32 GetDisaDiffRWInfo(const char* path, DisaDiffRWInfo* info, bool partitionB) {
(difi->offset_hash != difi->offset_dpfs + difi->size_dpfs) || (difi->offset_hash != difi->offset_dpfs + difi->size_dpfs) ||
(difi->size_hash < 0x20)) (difi->size_hash < 0x20))
return 1; return 1;
info->dpfs_lvl1_selector = difi->dpfs_lvl1_selector; info->dpfs_lvl1_selector = difi->dpfs_lvl1_selector;
info->ivfc_use_extlvl4 = difi->ivfc_use_extlvl4; info->ivfc_use_extlvl4 = difi->ivfc_use_extlvl4;
info->offset_ivfc_lvl4 = (u32) (offset_partition + difi->ivfc_offset_extlvl4); info->offset_ivfc_lvl4 = (u32) (offset_partition + difi->ivfc_offset_extlvl4);
info->offset_master_hash = (u32) difi->offset_hash; info->offset_master_hash = (u32) difi->offset_hash;
// check & get data from DPFS descriptor // check & get data from DPFS descriptor
const DpfsDescriptor* dpfs = &(difis.dpfs); const DpfsDescriptor* dpfs = &(difis.dpfs);
if ((dpfs->offset_lvl1 + dpfs->size_lvl1 > dpfs->offset_lvl2) || if ((dpfs->offset_lvl1 + dpfs->size_lvl1 > dpfs->offset_lvl2) ||
@ -246,7 +246,7 @@ u32 GetDisaDiffRWInfo(const char* path, DisaDiffRWInfo* info, bool partitionB) {
(2 > dpfs->log_lvl2) || (dpfs->log_lvl2 > dpfs->log_lvl3) || (2 > dpfs->log_lvl2) || (dpfs->log_lvl2 > dpfs->log_lvl3) ||
!dpfs->size_lvl1 || !dpfs->size_lvl2 || !dpfs->size_lvl3) !dpfs->size_lvl1 || !dpfs->size_lvl2 || !dpfs->size_lvl3)
return 1; return 1;
info->offset_dpfs_lvl1 = (u32) (offset_partition + dpfs->offset_lvl1); info->offset_dpfs_lvl1 = (u32) (offset_partition + dpfs->offset_lvl1);
info->offset_dpfs_lvl2 = (u32) (offset_partition + dpfs->offset_lvl2); info->offset_dpfs_lvl2 = (u32) (offset_partition + dpfs->offset_lvl2);
info->offset_dpfs_lvl3 = (u32) (offset_partition + dpfs->offset_lvl3); info->offset_dpfs_lvl3 = (u32) (offset_partition + dpfs->offset_lvl3);
@ -255,7 +255,7 @@ u32 GetDisaDiffRWInfo(const char* path, DisaDiffRWInfo* info, bool partitionB) {
info->size_dpfs_lvl3 = (u32) dpfs->size_lvl3; info->size_dpfs_lvl3 = (u32) dpfs->size_lvl3;
info->log_dpfs_lvl2 = (u32) dpfs->log_lvl2; info->log_dpfs_lvl2 = (u32) dpfs->log_lvl2;
info->log_dpfs_lvl3 = (u32) dpfs->log_lvl3; info->log_dpfs_lvl3 = (u32) dpfs->log_lvl3;
// check & get data from IVFC descriptor // check & get data from IVFC descriptor
const IvfcDescriptor* ivfc = &(difis.ivfc); const IvfcDescriptor* ivfc = &(difis.ivfc);
if ((ivfc->size_hash != difi->size_hash) || if ((ivfc->size_hash != difi->size_hash) ||
@ -264,16 +264,16 @@ u32 GetDisaDiffRWInfo(const char* path, DisaDiffRWInfo* info, bool partitionB) {
(ivfc->offset_lvl2 + ivfc->size_lvl2 > ivfc->offset_lvl3) || (ivfc->offset_lvl2 + ivfc->size_lvl2 > ivfc->offset_lvl3) ||
(ivfc->offset_lvl3 + ivfc->size_lvl3 > dpfs->size_lvl3)) (ivfc->offset_lvl3 + ivfc->size_lvl3 > dpfs->size_lvl3))
return 1; return 1;
if (!info->ivfc_use_extlvl4) { if (!info->ivfc_use_extlvl4) {
if ((ivfc->offset_lvl3 + ivfc->size_lvl3 > ivfc->offset_lvl4) || if ((ivfc->offset_lvl3 + ivfc->size_lvl3 > ivfc->offset_lvl4) ||
(ivfc->offset_lvl4 + ivfc->size_lvl4 > dpfs->size_lvl3)) (ivfc->offset_lvl4 + ivfc->size_lvl4 > dpfs->size_lvl3))
return 1; return 1;
info->offset_ivfc_lvl4 = (u32) ivfc->offset_lvl4; info->offset_ivfc_lvl4 = (u32) ivfc->offset_lvl4;
} else if (info->offset_ivfc_lvl4 + ivfc->size_lvl4 > offset_partition + size_partition) } else if (info->offset_ivfc_lvl4 + ivfc->size_lvl4 > offset_partition + size_partition)
return 1; return 1;
info->log_ivfc_lvl1 = (u32) ivfc->log_lvl1; info->log_ivfc_lvl1 = (u32) ivfc->log_lvl1;
info->log_ivfc_lvl2 = (u32) ivfc->log_lvl2; info->log_ivfc_lvl2 = (u32) ivfc->log_lvl2;
info->log_ivfc_lvl3 = (u32) ivfc->log_lvl3; info->log_ivfc_lvl3 = (u32) ivfc->log_lvl3;
@ -285,7 +285,7 @@ u32 GetDisaDiffRWInfo(const char* path, DisaDiffRWInfo* info, bool partitionB) {
info->size_ivfc_lvl2 = (u32) ivfc->size_lvl2; info->size_ivfc_lvl2 = (u32) ivfc->size_lvl2;
info->size_ivfc_lvl3 = (u32) ivfc->size_lvl3; info->size_ivfc_lvl3 = (u32) ivfc->size_lvl3;
info->size_ivfc_lvl4 = (u32) ivfc->size_lvl4; info->size_ivfc_lvl4 = (u32) ivfc->size_lvl4;
return 0; return 0;
} }
@ -293,31 +293,31 @@ u32 BuildDisaDiffDpfsLvl2Cache(const char* path, const DisaDiffRWInfo* info, u8*
const u32 min_cache_bits = (info->size_dpfs_lvl3 + (1 << info->log_dpfs_lvl3) - 1) >> info->log_dpfs_lvl3; const u32 min_cache_bits = (info->size_dpfs_lvl3 + (1 << info->log_dpfs_lvl3) - 1) >> info->log_dpfs_lvl3;
const u32 min_cache_size = ((min_cache_bits + 31) >> (3 + 2)) << 2; const u32 min_cache_size = ((min_cache_bits + 31) >> (3 + 2)) << 2;
const u32 offset_lvl1 = info->offset_dpfs_lvl1 + ((info->dpfs_lvl1_selector) ? info->size_dpfs_lvl1 : 0); const u32 offset_lvl1 = info->offset_dpfs_lvl1 + ((info->dpfs_lvl1_selector) ? info->size_dpfs_lvl1 : 0);
// safety (this still assumes all the checks from GetDisaDiffRWInfo()) // safety (this still assumes all the checks from GetDisaDiffRWInfo())
if ((cache_size < min_cache_size) || if ((cache_size < min_cache_size) ||
(min_cache_size > info->size_dpfs_lvl2) || (min_cache_size > info->size_dpfs_lvl2) ||
(min_cache_size > (info->size_dpfs_lvl1 << (3 + info->log_dpfs_lvl2)))) { (min_cache_size > (info->size_dpfs_lvl1 << (3 + info->log_dpfs_lvl2)))) {
return 1; return 1;
} }
// allocate memory // allocate memory
u8* lvl1 = (u8*) malloc(info->size_dpfs_lvl1); u8* lvl1 = (u8*) malloc(info->size_dpfs_lvl1);
if (!lvl1) return 1; // this is never more than 8 byte in reality -___- if (!lvl1) return 1; // this is never more than 8 byte in reality -___-
// open file pointer // open file pointer
if (DisaDiffOpen(path) != FR_OK) { if (DisaDiffOpen(path) != FR_OK) {
free(lvl1); free(lvl1);
return 1; return 1;
} }
// read lvl1 // read lvl1
u32 ret = 0; u32 ret = 0;
if ((ret != 0) || DisaDiffRead(lvl1, info->size_dpfs_lvl1, offset_lvl1)) ret = 1; if ((ret != 0) || DisaDiffRead(lvl1, info->size_dpfs_lvl1, offset_lvl1)) ret = 1;
// read full lvl2_0 to cache // read full lvl2_0 to cache
if ((ret != 0) || DisaDiffRead(cache, info->size_dpfs_lvl2, info->offset_dpfs_lvl2)) ret = 1; if ((ret != 0) || DisaDiffRead(cache, info->size_dpfs_lvl2, info->offset_dpfs_lvl2)) ret = 1;
// cherry-pick lvl2_1 // cherry-pick lvl2_1
u32 log_lvl2 = info->log_dpfs_lvl2; u32 log_lvl2 = info->log_dpfs_lvl2;
u32 offset_lvl2_1 = info->offset_dpfs_lvl2 + info->size_dpfs_lvl2; u32 offset_lvl2_1 = info->offset_dpfs_lvl2 + info->size_dpfs_lvl2;
@ -330,7 +330,7 @@ u32 BuildDisaDiffDpfsLvl2Cache(const char* path, const DisaDiffRWInfo* info, u8*
} }
} }
} }
((DisaDiffRWInfo*) info)->dpfs_lvl2_cache = cache; ((DisaDiffRWInfo*) info)->dpfs_lvl2_cache = cache;
free(lvl1); free(lvl1);
DisaDiffClose(); DisaDiffClose();
@ -344,11 +344,11 @@ static u32 ReadDisaDiffDpfsLvl3(const DisaDiffRWInfo* info, u32 offset, u32 size
const u32 offset_lvl3_0 = info->offset_dpfs_lvl3; const u32 offset_lvl3_0 = info->offset_dpfs_lvl3;
const u32 offset_lvl3_1 = offset_lvl3_0 + info->size_dpfs_lvl3; const u32 offset_lvl3_1 = offset_lvl3_0 + info->size_dpfs_lvl3;
const u32 log_lvl3 = info->log_dpfs_lvl3; const u32 log_lvl3 = info->log_dpfs_lvl3;
u32 read_start = offset_start; u32 read_start = offset_start;
u32 read_end = read_start; u32 read_end = read_start;
u32 bit_state = 0; u32 bit_state = 0;
// full reading below // full reading below
while (size && (read_start < offset_end)) { while (size && (read_start < offset_end)) {
// read bits until bit_state does not match // read bits until bit_state does not match
@ -370,7 +370,7 @@ static u32 ReadDisaDiffDpfsLvl3(const DisaDiffRWInfo* info, u32 offset, u32 size
// flip the bit_state // flip the bit_state
bit_state = ~bit_state & 0x1; bit_state = ~bit_state & 0x1;
} }
return size; return size;
} }
@ -381,11 +381,11 @@ static u32 WriteDisaDiffDpfsLvl3(const DisaDiffRWInfo* info, u32 offset, u32 siz
const u32 offset_lvl3_0 = info->offset_dpfs_lvl3; const u32 offset_lvl3_0 = info->offset_dpfs_lvl3;
const u32 offset_lvl3_1 = offset_lvl3_0 + info->size_dpfs_lvl3; const u32 offset_lvl3_1 = offset_lvl3_0 + info->size_dpfs_lvl3;
const u32 log_lvl3 = info->log_dpfs_lvl3; const u32 log_lvl3 = info->log_dpfs_lvl3;
u32 write_start = offset_start; u32 write_start = offset_start;
u32 write_end = write_start; u32 write_end = write_start;
u32 bit_state = 0; u32 bit_state = 0;
// full reading below // full reading below
while (size && (write_start < offset_end)) { while (size && (write_start < offset_end)) {
// write bits until bit_state does not match // write bits until bit_state does not match
@ -407,40 +407,40 @@ static u32 WriteDisaDiffDpfsLvl3(const DisaDiffRWInfo* info, u32 offset, u32 siz
// flip the bit_state // flip the bit_state
bit_state = ~bit_state & 0x1; bit_state = ~bit_state & 0x1;
} }
return size; return size;
} }
u32 FixDisaDiffPartitionHash(const DisaDiffRWInfo* info) { u32 FixDisaDiffPartitionHash(const DisaDiffRWInfo* info) {
const u32 size = info->size_table; const u32 size = info->size_table;
u8 sha_buf[0x20]; u8 sha_buf[0x20];
u8* buf; u8* buf;
if (!(buf = malloc(size))) if (!(buf = malloc(size)))
return 1; return 1;
if (DisaDiffRead(buf, size, info->offset_table) != FR_OK) { if (DisaDiffRead(buf, size, info->offset_table) != FR_OK) {
free(buf); free(buf);
return 1; return 1;
} }
sha_quick(sha_buf, buf, size, SHA256_MODE); sha_quick(sha_buf, buf, size, SHA256_MODE);
free(buf); free(buf);
if (DisaDiffWrite(sha_buf, 0x20, info->offset_partition_hash) != FR_OK) if (DisaDiffWrite(sha_buf, 0x20, info->offset_partition_hash) != FR_OK)
return 1; return 1;
return 0; return 0;
} }
u32 FixDisaDiffIvfcLevel(const DisaDiffRWInfo* info, u32 level, u32 offset, u32 size, u32* next_offset, u32* next_size) { u32 FixDisaDiffIvfcLevel(const DisaDiffRWInfo* info, u32 level, u32 offset, u32 size, u32* next_offset, u32* next_size) {
if (level == 0) if (level == 0)
return FixDisaDiffPartitionHash(info); return FixDisaDiffPartitionHash(info);
if (level > 4) if (level > 4)
return 1; return 1;
const u32 offset_ivfc_lvl = (&(info->offset_ivfc_lvl1))[level - 1]; const u32 offset_ivfc_lvl = (&(info->offset_ivfc_lvl1))[level - 1];
const u32 size_ivfc_lvl = (&(info->size_ivfc_lvl1))[level - 1]; const u32 size_ivfc_lvl = (&(info->size_ivfc_lvl1))[level - 1];
const u32 log_ivfc_lvl = (&(info->log_ivfc_lvl1))[level - 1]; const u32 log_ivfc_lvl = (&(info->log_ivfc_lvl1))[level - 1];
@ -448,48 +448,48 @@ u32 FixDisaDiffIvfcLevel(const DisaDiffRWInfo* info, u32 level, u32 offset, u32
u32 read_size = block_size; u32 read_size = block_size;
u32 align_offset = (offset >> log_ivfc_lvl) << log_ivfc_lvl; // align starting offset u32 align_offset = (offset >> log_ivfc_lvl) << log_ivfc_lvl; // align starting offset
u32 align_size = size + offset - align_offset; // increase size by the amount starting offset decreased when aligned u32 align_size = size + offset - align_offset; // increase size by the amount starting offset decreased when aligned
if (level != 1) { if (level != 1) {
if (next_offset) *next_offset = (align_offset >> log_ivfc_lvl) * 0x20; if (next_offset) *next_offset = (align_offset >> log_ivfc_lvl) * 0x20;
if (next_size) *next_size = ((align_size >> log_ivfc_lvl) + (((align_size % block_size) == 0) ? 0 : 1)) * 0x20; if (next_size) *next_size = ((align_size >> log_ivfc_lvl) + (((align_size % block_size) == 0) ? 0 : 1)) * 0x20;
} }
u8 sha_buf[0x20]; u8 sha_buf[0x20];
u8* buf; u8* buf;
if (!(buf = malloc(block_size))) if (!(buf = malloc(block_size)))
return 1; return 1;
while (align_size > 0) { while (align_size > 0) {
if (align_offset + block_size > size_ivfc_lvl) { if (align_offset + block_size > size_ivfc_lvl) {
memset(buf, 0, block_size); memset(buf, 0, block_size);
read_size -= (align_offset + block_size - size_ivfc_lvl); read_size -= (align_offset + block_size - size_ivfc_lvl);
} }
if (((level == 4) && info->ivfc_use_extlvl4) ? (DisaDiffRead(buf, read_size, align_offset + offset_ivfc_lvl) != FR_OK) : if (((level == 4) && info->ivfc_use_extlvl4) ? (DisaDiffRead(buf, read_size, align_offset + offset_ivfc_lvl) != FR_OK) :
(ReadDisaDiffDpfsLvl3(info, align_offset + offset_ivfc_lvl, read_size, buf) != read_size)) { (ReadDisaDiffDpfsLvl3(info, align_offset + offset_ivfc_lvl, read_size, buf) != read_size)) {
free(buf); free(buf);
return 1; return 1;
} }
sha_quick(sha_buf, buf, block_size, SHA256_MODE); sha_quick(sha_buf, buf, block_size, SHA256_MODE);
if ((level == 1) ? (DisaDiffWrite(sha_buf, 0x20, info->offset_difi + info->offset_master_hash + ((align_offset >> log_ivfc_lvl) * 0x20)) != FR_OK) : if ((level == 1) ? (DisaDiffWrite(sha_buf, 0x20, info->offset_difi + info->offset_master_hash + ((align_offset >> log_ivfc_lvl) * 0x20)) != FR_OK) :
(WriteDisaDiffDpfsLvl3(info, (&(info->offset_ivfc_lvl1))[level - 2] + ((align_offset >> log_ivfc_lvl) * 0x20), 0x20, sha_buf) != 0x20)) { (WriteDisaDiffDpfsLvl3(info, (&(info->offset_ivfc_lvl1))[level - 2] + ((align_offset >> log_ivfc_lvl) * 0x20), 0x20, sha_buf) != 0x20)) {
free(buf); free(buf);
return 1; return 1;
} }
align_offset += block_size; align_offset += block_size;
align_size = ((align_size < block_size) ? 0 : (align_size - block_size)); align_size = ((align_size < block_size) ? 0 : (align_size - block_size));
} }
free(buf); free(buf);
return 0; return 0;
} }
u32 ReadDisaDiffIvfcLvl4(const char* path, const DisaDiffRWInfo* info, u32 offset, u32 size, void* buffer) { // offset: offset inside IVFC lvl4 u32 ReadDisaDiffIvfcLvl4(const char* path, const DisaDiffRWInfo* info, u32 offset, u32 size, void* buffer) { // offset: offset inside IVFC lvl4
// DisaDiffRWInfo not provided? // DisaDiffRWInfo not provided?
DisaDiffRWInfo info_l; DisaDiffRWInfo info_l;
@ -504,15 +504,15 @@ u32 ReadDisaDiffIvfcLvl4(const char* path, const DisaDiffRWInfo* info, u32 offse
return 0; return 0;
} }
} }
// open file pointer // open file pointer
if (DisaDiffOpen(path) != FR_OK) if (DisaDiffOpen(path) != FR_OK)
size = 0; size = 0;
// sanity checks - offset & size // sanity checks - offset & size
if (offset > info->size_ivfc_lvl4) return 0; if (offset > info->size_ivfc_lvl4) return 0;
else if (offset + size > info->size_ivfc_lvl4) size = info->size_ivfc_lvl4 - offset; else if (offset + size > info->size_ivfc_lvl4) size = info->size_ivfc_lvl4 - offset;
if (info->ivfc_use_extlvl4) { if (info->ivfc_use_extlvl4) {
if (DisaDiffRead(buffer, size, info->offset_ivfc_lvl4 + offset) != FR_OK) if (DisaDiffRead(buffer, size, info->offset_ivfc_lvl4 + offset) != FR_OK)
size = 0; size = 0;
@ -541,22 +541,22 @@ u32 WriteDisaDiffIvfcLvl4(const char* path, const DisaDiffRWInfo* info, u32 offs
return 0; return 0;
} }
} }
// sanity check - offset & size // sanity check - offset & size
if (offset + size > info->size_ivfc_lvl4) if (offset + size > info->size_ivfc_lvl4)
return 0; return 0;
// open file pointer // open file pointer
if (DisaDiffOpen(path) != FR_OK) if (DisaDiffOpen(path) != FR_OK)
size = 0; size = 0;
if (info->ivfc_use_extlvl4) { if (info->ivfc_use_extlvl4) {
if (DisaDiffWrite(buffer, size, info->offset_ivfc_lvl4 + offset) != FR_OK) if (DisaDiffWrite(buffer, size, info->offset_ivfc_lvl4 + offset) != FR_OK)
size = 0; size = 0;
} else { } else {
size = WriteDisaDiffDpfsLvl3(info, info->offset_ivfc_lvl4 + offset, size, buffer); size = WriteDisaDiffDpfsLvl3(info, info->offset_ivfc_lvl4 + offset, size, buffer);
} }
if ((size != 0) && ddfp) { // if we're writing to a mounted image, the hash chain will be handled later by vdisadiff if ((size != 0) && ddfp) { // if we're writing to a mounted image, the hash chain will be handled later by vdisadiff
u32 hashfix_offset = offset, hashfix_size = size; u32 hashfix_offset = offset, hashfix_size = size;
for (int i = 4; i >= 0; i--) { for (int i = 4; i >= 0; i--) {
@ -566,7 +566,7 @@ u32 WriteDisaDiffIvfcLvl4(const char* path, const DisaDiffRWInfo* info, u32 offs
} }
} }
} }
DisaDiffClose(); DisaDiffClose();
if (cache) free(cache); if (cache) free(cache);
return size; return size;

View File

@ -12,18 +12,19 @@
// valid addresses for FIRM section loading // valid addresses for FIRM section loading
// pairs of start / end address, provided by Wolfvak // pairs of start / end address, provided by Wolfvak
#define FIRM_VALID_ADDRESS \ #define FIRM_VALID_ADDRESS \
0x08000040, 0x08100000, \
0x18000000, 0x18600000, \ 0x18000000, 0x18600000, \
0x1FF00000, 0x1FFFFC00 0x1FF00000, 0x1FFFFC00
// valid addresses (installable) for FIRM section loading // valid addresses (installable) for FIRM section loading
#define FIRM_VALID_ADDRESS_INSTALL \ #define FIRM_VALID_ADDRESS_INSTALL \
FIRM_VALID_ADDRESS, \ FIRM_VALID_ADDRESS, \
0x08000040, 0x080F7FFF, \
0x10000000, 0x10200000 0x10000000, 0x10200000
// valid addresses (bootable) for FIRM section loading // valid addresses (bootable) for FIRM section loading
#define FIRM_VALID_ADDRESS_BOOT \ #define FIRM_VALID_ADDRESS_BOOT \
FIRM_VALID_ADDRESS, \ FIRM_VALID_ADDRESS, \
0x08000040, 0x08100000, \
0x20000000, 0x27FFFA00 0x20000000, 0x27FFFA00
static const u32 whitelist_boot[] = { FIRM_VALID_ADDRESS_BOOT }; static const u32 whitelist_boot[] = { FIRM_VALID_ADDRESS_BOOT };
@ -169,10 +170,10 @@ u32 SetupSecretKey(u32 keynum) {
static u8 __attribute__((aligned(32))) sector[0x200]; static u8 __attribute__((aligned(32))) sector[0x200];
static u32 got_keys = 0; static u32 got_keys = 0;
u8* key = sector + (keynum*0x10); u8* key = sector + (keynum*0x10);
if (keynum >= 0x200/0x10) if (keynum >= 0x200/0x10)
return 1; // safety return 1; // safety
// try to load full secret sector // try to load full secret sector
if (!got_keys) { if (!got_keys) {
ReadNandSectors(sector, 0x96, 1, 0x11, NAND_SYSNAND); ReadNandSectors(sector, 0x96, 1, 0x11, NAND_SYSNAND);
@ -185,34 +186,34 @@ u32 SetupSecretKey(u32 keynum) {
if (LoadKeyFromFile(key, 0x11, 'N', (keynum == 0) ? "95" : "96") == 0) if (LoadKeyFromFile(key, 0x11, 'N', (keynum == 0) ? "95" : "96") == 0)
got_keys |= (0x1<<keynum); // got at least this key got_keys |= (0x1<<keynum); // got at least this key
} }
// setup the key // setup the key
if (got_keys & (0x1<<keynum)) { if (got_keys & (0x1<<keynum)) {
setup_aeskey(0x11, key); setup_aeskey(0x11, key);
use_aeskey(0x11); use_aeskey(0x11);
return 0; return 0;
} }
// out of options // out of options
return 1; return 1;
} }
u32 DecryptA9LHeader(FirmA9LHeader* header) { u32 DecryptA9LHeader(FirmA9LHeader* header) {
u32 type = A9L_CRYPTO_TYPE(header); u32 type = A9L_CRYPTO_TYPE(header);
if (SetupSecretKey(0) != 0) return 1; if (SetupSecretKey(0) != 0) return 1;
aes_decrypt(header->keyX0x15, header->keyX0x15, 1, AES_CNT_ECB_DECRYPT_MODE); aes_decrypt(header->keyX0x15, header->keyX0x15, 1, AES_CNT_ECB_DECRYPT_MODE);
if (type) { if (type) {
if (SetupSecretKey((type == 1) ? 0 : 1) != 0) return 1; if (SetupSecretKey((type == 1) ? 0 : 1) != 0) return 1;
aes_decrypt(header->keyX0x16, header->keyX0x16, 1, AES_CNT_ECB_DECRYPT_MODE); aes_decrypt(header->keyX0x16, header->keyX0x16, 1, AES_CNT_ECB_DECRYPT_MODE);
} }
return 0; return 0;
} }
u32 SetupArm9BinaryCrypto(FirmA9LHeader* header) { u32 SetupArm9BinaryCrypto(FirmA9LHeader* header) {
u32 type = A9L_CRYPTO_TYPE(header); u32 type = A9L_CRYPTO_TYPE(header);
if (type == 0) { if (type == 0) {
u8 __attribute__((aligned(32))) keyX0x15[0x10]; u8 __attribute__((aligned(32))) keyX0x15[0x10];
memcpy(keyX0x15, header->keyX0x15, 0x10); memcpy(keyX0x15, header->keyX0x15, 0x10);
@ -230,24 +231,24 @@ u32 SetupArm9BinaryCrypto(FirmA9LHeader* header) {
setup_aeskeyY(0x16, header->keyY0x150x16); setup_aeskeyY(0x16, header->keyY0x150x16);
use_aeskey(0x16); use_aeskey(0x16);
} }
return 0; return 0;
} }
u32 DecryptArm9Binary(void* data, u32 offset, u32 size, FirmA9LHeader* a9l) { u32 DecryptArm9Binary(void* data, u32 offset, u32 size, FirmA9LHeader* a9l) {
// offset == offset inside ARM9 binary // offset == offset inside ARM9 binary
// ARM9 binary begins 0x800 byte after the ARM9 loader header // ARM9 binary begins 0x800 byte after the ARM9 loader header
// only process actual ARM9 binary // only process actual ARM9 binary
u32 size_bin = GetArm9BinarySize(a9l); u32 size_bin = GetArm9BinarySize(a9l);
if (offset >= size_bin) return 0; if (offset >= size_bin) return 0;
else if (size >= size_bin - offset) else if (size >= size_bin - offset)
size = size_bin - offset; size = size_bin - offset;
// decrypt data // decrypt data
if (SetupArm9BinaryCrypto(a9l) != 0) return 1; if (SetupArm9BinaryCrypto(a9l) != 0) return 1;
ctr_decrypt_byte(data, data, size, offset, AES_CNT_CTRNAND_MODE, a9l->ctr); ctr_decrypt_byte(data, data, size, offset, AES_CNT_CTRNAND_MODE, a9l->ctr);
return 0; return 0;
} }
@ -256,16 +257,16 @@ u32 DecryptFirm(void* data, u32 offset, u32 size, FirmHeader* firm, FirmA9LHeade
FirmSectionHeader* arm9s = FindFirmArm9Section(firm); FirmSectionHeader* arm9s = FindFirmArm9Section(firm);
u32 offset_arm9bin = arm9s->offset + ARM9BIN_OFFSET; u32 offset_arm9bin = arm9s->offset + ARM9BIN_OFFSET;
u32 size_arm9bin = GetArm9BinarySize(a9l); u32 size_arm9bin = GetArm9BinarySize(a9l);
// sanity checks // sanity checks
if (!size_arm9bin || (size_arm9bin + ARM9BIN_OFFSET > arm9s->size)) if (!size_arm9bin || (size_arm9bin + ARM9BIN_OFFSET > arm9s->size))
return 1; // bad header / data return 1; // bad header / data
// check if ARM9 binary in data // check if ARM9 binary in data
if ((offset_arm9bin >= offset + size) || if ((offset_arm9bin >= offset + size) ||
(offset >= offset_arm9bin + size_arm9bin)) (offset >= offset_arm9bin + size_arm9bin))
return 0; // section not in data return 0; // section not in data
// determine data / offset / size // determine data / offset / size
u8* data8 = (u8*)data; u8* data8 = (u8*)data;
u8* data_i = data8; u8* data_i = data8;
@ -277,7 +278,7 @@ u32 DecryptFirm(void* data, u32 offset, u32 size, FirmHeader* firm, FirmA9LHeade
size_i = size_arm9bin - offset_i; size_i = size_arm9bin - offset_i;
if (size_i > size - (data_i - data8)) if (size_i > size - (data_i - data8))
size_i = size - (data_i - data8); size_i = size - (data_i - data8);
return DecryptArm9Binary(data_i, offset_i, size_i, a9l); return DecryptArm9Binary(data_i, offset_i, size_i, a9l);
} }
@ -290,7 +291,7 @@ u32 DecryptFirmSequential(void* data, u32 offset, u32 size) {
static FirmHeader* firmptr = NULL; static FirmHeader* firmptr = NULL;
static FirmA9LHeader* a9lptr = NULL; static FirmA9LHeader* a9lptr = NULL;
static FirmSectionHeader* arm9s = NULL; static FirmSectionHeader* arm9s = NULL;
// fetch firm header from data // fetch firm header from data
if ((offset == 0) && (size >= sizeof(FirmHeader))) { if ((offset == 0) && (size >= sizeof(FirmHeader))) {
memcpy(&firm, data, sizeof(FirmHeader)); memcpy(&firm, data, sizeof(FirmHeader));
@ -298,17 +299,17 @@ u32 DecryptFirmSequential(void* data, u32 offset, u32 size) {
arm9s = (firmptr) ? FindFirmArm9Section(firmptr) : NULL; arm9s = (firmptr) ? FindFirmArm9Section(firmptr) : NULL;
a9lptr = NULL; a9lptr = NULL;
} }
// safety check, firm header pointer // safety check, firm header pointer
if (!firmptr) return 1; if (!firmptr) return 1;
// fetch ARM9 loader header from data // fetch ARM9 loader header from data
if (arm9s && !a9lptr && (offset <= arm9s->offset) && if (arm9s && !a9lptr && (offset <= arm9s->offset) &&
((offset + size) >= arm9s->offset + sizeof(FirmA9LHeader))) { ((offset + size) >= arm9s->offset + sizeof(FirmA9LHeader))) {
memcpy(&a9l, (u8*)data + arm9s->offset - offset, sizeof(FirmA9LHeader)); memcpy(&a9l, (u8*)data + arm9s->offset - offset, sizeof(FirmA9LHeader));
a9lptr = (ValidateFirmA9LHeader(&a9l) == 0) ? &a9l : NULL; a9lptr = (ValidateFirmA9LHeader(&a9l) == 0) ? &a9l : NULL;
} }
return (a9lptr) ? DecryptFirm(data, offset, size, firmptr, a9lptr) : 0; return (a9lptr) ? DecryptFirm(data, offset, size, firmptr, a9lptr) : 0;
} }
@ -318,17 +319,17 @@ u32 DecryptFirmFull(void* data, u32 size) {
FirmSectionHeader* arm9s = FindFirmArm9Section(firm); FirmSectionHeader* arm9s = FindFirmArm9Section(firm);
if (ValidateFirmHeader(firm, size) != 0) return 1; // not a proper firm if (ValidateFirmHeader(firm, size) != 0) return 1; // not a proper firm
if (!arm9s) return 0; // no ARM9 section -> not encrypted -> done if (!arm9s) return 0; // no ARM9 section -> not encrypted -> done
FirmA9LHeader* a9l = (FirmA9LHeader*)(void*) ((u8*) data + arm9s->offset); FirmA9LHeader* a9l = (FirmA9LHeader*)(void*) ((u8*) data + arm9s->offset);
if (ValidateFirmA9LHeader(a9l) != 0) return 0; // no ARM9bin -> not encrypted -> done if (ValidateFirmA9LHeader(a9l) != 0) return 0; // no ARM9bin -> not encrypted -> done
// decrypt FIRM and ARM9loader header // decrypt FIRM and ARM9loader header
if ((DecryptFirm(data, 0, size, firm, a9l) != 0) || (DecryptA9LHeader(a9l) != 0)) if ((DecryptFirm(data, 0, size, firm, a9l) != 0) || (DecryptA9LHeader(a9l) != 0))
return 1; return 1;
// fix ARM9 section SHA and ARM9 entrypoint // fix ARM9 section SHA and ARM9 entrypoint
sha_quick(arm9s->hash, (u8*) data + arm9s->offset, arm9s->size, SHA256_MODE); sha_quick(arm9s->hash, (u8*) data + arm9s->offset, arm9s->size, SHA256_MODE);
firm->entry_arm9 = ARM9ENTRY_FIX(firm); firm->entry_arm9 = ARM9ENTRY_FIX(firm);
return 0; return 0;
} }

View File

@ -20,3 +20,4 @@
#include "bdri.h" #include "bdri.h"
#include "ticketdb.h" #include "ticketdb.h"
#include "ncchinfo.h" #include "ncchinfo.h"
#include "cifinish.h"

View File

@ -8,20 +8,20 @@
u32 ValidateAgbSaveHeader(AgbSaveHeader* header) { u32 ValidateAgbSaveHeader(AgbSaveHeader* header) {
static u8 magic[] = { AGBSAVE_MAGIC }; static u8 magic[] = { AGBSAVE_MAGIC };
// basic checks // basic checks
if ((memcmp(header->magic, magic, sizeof(magic)) != 0) || if ((memcmp(header->magic, magic, sizeof(magic)) != 0) ||
(header->unknown0 != 1) || (header->save_start != 0x200) || (header->unknown0 != 1) || (header->save_start != 0x200) ||
(header->save_size > AGBSAVE_MAX_SSIZE) || !(GBASAVE_VALID(header->save_size))) (header->save_size > AGBSAVE_MAX_SSIZE) || !(GBASAVE_VALID(header->save_size)))
return 1; return 1;
// reserved area checks // reserved area checks
// disabled due to a weird quirk https://github.com/d0k3/GodMode9/issues/412 // disabled due to a weird quirk https://github.com/d0k3/GodMode9/issues/412
// for (u32 i = 0; i < sizeof(header->reserved0); i++) if (header->reserved0[i] != 0xFF) return 1; // for (u32 i = 0; i < sizeof(header->reserved0); i++) if (header->reserved0[i] != 0xFF) return 1;
// for (u32 i = 0; i < sizeof(header->reserved1); i++) if (header->reserved1[i] != 0xFF) return 1; // for (u32 i = 0; i < sizeof(header->reserved1); i++) if (header->reserved1[i] != 0xFF) return 1;
// for (u32 i = 0; i < sizeof(header->reserved2); i++) if (header->reserved2[i] != 0xFF) return 1; // for (u32 i = 0; i < sizeof(header->reserved2); i++) if (header->reserved2[i] != 0xFF) return 1;
// for (u32 i = 0; i < sizeof(header->reserved3); i++) if (header->reserved3[i] != 0xFF) return 1; // for (u32 i = 0; i < sizeof(header->reserved3); i++) if (header->reserved3[i] != 0xFF) return 1;
// all fine if arriving here // all fine if arriving here
return 0; return 0;
} }
@ -30,60 +30,74 @@ u32 ValidateAgbSaveHeader(AgbSaveHeader* header) {
u32 ValidateAgbHeader(AgbHeader* agb) { u32 ValidateAgbHeader(AgbHeader* agb) {
static const u8 logo_sha[0x20] = { AGBLOGO_SHA256 }; static const u8 logo_sha[0x20] = { AGBLOGO_SHA256 };
u8 logo[0x9C] __attribute__((aligned(4))); u8 logo[0x9C] __attribute__((aligned(4)));
// check fixed value // check fixed value
if (agb->fixed != 0x96) return 1; if (agb->fixed != 0x96) return 1;
// header checksum // header checksum
u8* hdr = (u8*) agb; u8* hdr = (u8*) agb;
u8 checksum = 0x00 - 0x19; u8 checksum = 0x00 - 0x19;
for (u32 i = 0xA0; i < 0xBD; i++) for (u32 i = 0xA0; i < 0xBD; i++)
checksum -= hdr[i]; checksum -= hdr[i];
if (agb->checksum != checksum) return 1; if (agb->checksum != checksum) return 1;
// logo SHA check // logo SHA check
memcpy(logo, agb->logo, 0x9C); memcpy(logo, agb->logo, 0x9C);
logo[0x98] &= ~0x84; logo[0x98] &= ~0x84;
logo[0x9A] &= ~0x03; logo[0x9A] &= ~0x03;
if (sha_cmp(logo_sha, logo, 0x9C, SHA256_MODE) != 0) if (sha_cmp(logo_sha, logo, 0x9C, SHA256_MODE) != 0)
return 1; return 1;
return 0; return 0;
} }
/* u32 ValidateAgbVc(void* data, u32 len) { /* u32 ValidateAgbVc(void* data, u32 len) {
const u8 magic[] = { GBAVC_MAGIC }; const u8 magic[] = { GBAVC_MAGIC };
if (len < sizeof(AgbHeader) + sizeof(AgbVcFooter)) if (len < sizeof(AgbHeader) + sizeof(AgbVcFooter))
return 1; return 1;
AgbHeader* header = (AgbHeader*) data; AgbHeader* header = (AgbHeader*) data;
AgbVcFooter* footer = (AgbVcFooter*) (((u8*) data) + len - sizeof(AgbVcFooter)); AgbVcFooter* footer = (AgbVcFooter*) (((u8*) data) + len - sizeof(AgbVcFooter));
if ((ValidateAgbHeader(header) != 0) || (memcmp(footer->magic, magic, sizeof(magic)) != 0) || if ((ValidateAgbHeader(header) != 0) || (memcmp(footer->magic, magic, sizeof(magic)) != 0) ||
(footer->rom_size != len - sizeof(AgbVcFooter))) (footer->rom_size != len - sizeof(AgbVcFooter)))
return 1; return 1;
return 0; return 0;
} */ } */
// basically reverse ValidateAgbSaveHeader() // basically reverse ValidateAgbSaveHeader()
/* u32 BuildAgbSaveHeader(AgbSaveHeader* header, u64 title_id, u32 save_size) { /* u32 BuildAgbSaveHeader(AgbSaveHeader* header, u64 title_id, u32 save_size) {
const u8 magic[] = { AGBSAVE_MAGIC }; const u8 magic[] = { AGBSAVE_MAGIC };
memset(header, 0x00, sizeof(AgbSaveHeader)); memset(header, 0x00, sizeof(AgbSaveHeader));
memset(header->reserved0, 0xFF, sizeof(header->reserved0)); memset(header->reserved0, 0xFF, sizeof(header->reserved0));
memset(header->reserved1, 0xFF, sizeof(header->reserved1)); memset(header->reserved1, 0xFF, sizeof(header->reserved1));
memset(header->reserved2, 0xFF, sizeof(header->reserved2)); memset(header->reserved2, 0xFF, sizeof(header->reserved2));
memset(header->reserved3, 0xFF, sizeof(header->reserved3)); memset(header->reserved3, 0xFF, sizeof(header->reserved3));
memcpy(header->magic, magic, sizeof(magic)); memcpy(header->magic, magic, sizeof(magic));
header->unknown0 = 0x01; header->unknown0 = 0x01;
header->title_id = title_id; header->title_id = title_id;
header->save_start = 0x200; header->save_start = 0x200;
header->save_size = save_size; header->save_size = save_size;
sdmmc_get_cid(0, (u32*) (void*) &(header->sd_cid)); sdmmc_get_cid(0, (u32*) (void*) &(header->sd_cid));
return 0; return 0;
} */ } */
// see: http://problemkaputt.de/gbatek.htm#gbacartridgeheader
const char* AgbDestStr(const char* code) {
switch(code[3]) {
case 'J': return STR_REGION_JAPAN;
case 'E': return STR_REGION_AMERICAS;
case 'P': return STR_REGION_EUROPE;
case 'D': return STR_REGION_GERMANY;
case 'F': return STR_REGION_FRANCE;
case 'I': return STR_REGION_ITALY;
case 'S': return STR_REGION_SPAIN;
default: return STR_REGION_UNKNOWN;
}
}

View File

@ -1,7 +1,8 @@
#pragma once #pragma once
#include "common.h" #include "common.h"
#include "language.h"
#define GBAVC_MAGIC '.', 'C', 'A', 'A' #define GBAVC_MAGIC '.', 'C', 'A', 'A'
#define AGBSAVE_MAGIC '.', 'S', 'A', 'V' #define AGBSAVE_MAGIC '.', 'S', 'A', 'V'
#define AGBSAVE_MAX_SIZE (0x000180 * 0x200) // standard size of the NAND partition #define AGBSAVE_MAX_SIZE (0x000180 * 0x200) // standard size of the NAND partition
@ -20,25 +21,15 @@
((tp >= 0x4) && (tp <= 0x9)) ? GBASAVE_FLASH_64K : \ ((tp >= 0x4) && (tp <= 0x9)) ? GBASAVE_FLASH_64K : \
((tp >= 0xA) && (tp <= 0xD)) ? GBASAVE_FLASH_128K : \ ((tp >= 0xA) && (tp <= 0xD)) ? GBASAVE_FLASH_128K : \
(tp == 0xE) ? GBASAVE_SRAM_32K : 0); // last one means invalid (tp == 0xE) ? GBASAVE_SRAM_32K : 0); // last one means invalid
#define GBASAVE_VALID(size) \ #define GBASAVE_VALID(size) \
(((size) == GBASAVE_EEPROM_512) || \ (((size) == GBASAVE_EEPROM_512) || \
((size) == GBASAVE_EEPROM_8K) || \ ((size) == GBASAVE_EEPROM_8K) || \
((size) == GBASAVE_SRAM_32K) || \ ((size) == GBASAVE_SRAM_32K) || \
((size) == GBASAVE_FLASH_64K) || \ ((size) == GBASAVE_FLASH_64K) || \
((size) == GBASAVE_FLASH_128K)) ((size) == GBASAVE_FLASH_128K))
// see: http://problemkaputt.de/gbatek.htm#gbacartridgeheader
#define AGB_DESTSTR(code) \
(((code)[3] == 'J') ? "Japan" : \
((code)[3] == 'E') ? "USA/English" : \
((code)[3] == 'P') ? "Europe/Elsewhere" : \
((code)[3] == 'D') ? "German" : \
((code)[3] == 'F') ? "French" : \
((code)[3] == 'I') ? "Italian" : \
((code)[3] == 'S') ? "Spanish" : "Unknown")
// see: http://3dbrew.org/wiki/3DS_Virtual_Console#Footer // see: http://3dbrew.org/wiki/3DS_Virtual_Console#Footer
// still a lot of unknowns in here, also redundant stuff left out // still a lot of unknowns in here, also redundant stuff left out
typedef struct { typedef struct {
@ -73,7 +64,7 @@ typedef struct {
// see: http://problemkaputt.de/gbatek.htm#gbacartridgeheader // see: http://problemkaputt.de/gbatek.htm#gbacartridgeheader
typedef struct { typedef struct {
u32 arm7_rom_entry; u32 arm7_rom_entry;
u8 logo[0x9C]; u8 logo[0x9C];
char game_title[12]; char game_title[12];
char game_code[4]; char game_code[4];
@ -89,5 +80,8 @@ typedef struct {
} __attribute__((packed, aligned(16))) AgbHeader; } __attribute__((packed, aligned(16))) AgbHeader;
u32 ValidateAgbSaveHeader(AgbSaveHeader* header); u32 ValidateAgbSaveHeader(AgbSaveHeader* header);
u32 ValidateAgbHeader(AgbHeader* agb); u32 ValidateAgbHeader(AgbHeader* agb);
const char* AgbDestStr(const char* code);

View File

@ -5,6 +5,7 @@
#include "ips.h" #include "ips.h"
#include "common.h" #include "common.h"
#include "fsperm.h" #include "fsperm.h"
#include "language.h"
#include "ui.h" #include "ui.h"
#include "vff.h" #include "vff.h"
@ -30,21 +31,21 @@ char errName[256];
int displayError(int errcode) { int displayError(int errcode) {
switch(errcode) { switch(errcode) {
case IPS_NOTTHIS: case IPS_NOTTHIS:
ShowPrompt(false, "%s\nThe patch is most likely not intended for this file.", errName); break; ShowPrompt(false, "%s\n%s", errName, STR_PATCH_MOST_LIKELY_NOT_FOR_THIS_FILE); break;
case IPS_THISOUT: case IPS_THISOUT:
ShowPrompt(false, "%s\nYou most likely applied the patch on the output file.", errName); break; ShowPrompt(false, "%s\n%s", errName, STR_YOU_MOST_LIKELY_APPLIED_PATCH_ON_OUTPUT); break;
case IPS_SCRAMBLED: case IPS_SCRAMBLED:
ShowPrompt(false, "%s\nThe patch is technically valid,\nbut seems scrambled or malformed.", errName); break; ShowPrompt(false, "%s\n%s", errName, STR_PATCH_TECHNICALLY_VALID_BUT_SEEMS_SCRAMBLED); break;
case IPS_INVALID: case IPS_INVALID:
ShowPrompt(false, "%s\nThe patch is invalid.", errName); break; ShowPrompt(false, "%s\n%s", errName, STR_PATCH_IS_INVALID); break;
case IPS_16MB: case IPS_16MB:
ShowPrompt(false, "%s\nOne or both files is bigger than 16MB.\nThe IPS format doesn't support that.", errName); break; ShowPrompt(false, "%s\n%s", errName, STR_FILES_BIGGER_THAN_16MB_IPS_DOESNT_SUPPORT_THAT); break;
case IPS_INVALID_FILE_PATH: case IPS_INVALID_FILE_PATH:
ShowPrompt(false, "%s\nThe requested file path was invalid.", errName); break; ShowPrompt(false, "%s\n%s", errName, STR_REQUESTED_FILE_PATH_WAS_INVALID); break;
case IPS_CANCELED: case IPS_CANCELED:
ShowPrompt(false, "%s\nPatching canceled.", errName); break; ShowPrompt(false, "%s\n%s", errName, STR_PATCHING_CANCELED); break;
case IPS_MEMORY: case IPS_MEMORY:
ShowPrompt(false, "%s\nNot enough memory.", errName); break; ShowPrompt(false, "%s\n%s", errName, STR_NOT_ENOUGH_MEMORY); break;
} }
fvx_close(&patchFile); fvx_close(&patchFile);
fvx_close(&inFile); fvx_close(&inFile);
@ -112,15 +113,15 @@ UINT read24() {
int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName) { int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName) {
int error = IPS_INVALID; int error = IPS_INVALID;
UINT outlen_min, outlen_max, outlen_min_mem; UINT outlen_min, outlen_max, outlen_min_mem;
snprintf(errName, 256, "%s", patchName); snprintf(errName, sizeof(errName), "%s", patchName);
if (fvx_open(&patchFile, patchName, FA_READ) != FR_OK) return displayError(IPS_INVALID_FILE_PATH); if (fvx_open(&patchFile, patchName, FA_READ) != FR_OK) return displayError(IPS_INVALID_FILE_PATH);
patchSize = fvx_size(&patchFile); patchSize = fvx_size(&patchFile);
ShowProgress(0, patchSize, patchName); ShowProgress(0, patchSize, patchName);
patch = malloc(patchSize); patch = malloc(patchSize);
if (!patch || fvx_read(&patchFile, patch, patchSize, NULL) != FR_OK) return displayError(IPS_MEMORY); if (!patch || fvx_read(&patchFile, patch, patchSize, NULL) != FR_OK) return displayError(IPS_MEMORY);
// Check validity of patch // Check validity of patch
if (patchSize < 8) return displayError(IPS_INVALID); if (patchSize < 8) return displayError(IPS_INVALID);
if (read8() != 'P' || if (read8() != 'P' ||
@ -131,7 +132,7 @@ int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName
{ {
return displayError(IPS_INVALID); return displayError(IPS_INVALID);
} }
unsigned int offset = read24(); unsigned int offset = read24();
unsigned int outlen = 0; unsigned int outlen = 0;
unsigned int thisout = 0; unsigned int thisout = 0;
@ -140,11 +141,11 @@ int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName
while (offset != 0x454F46) // 454F46=EOF while (offset != 0x454F46) // 454F46=EOF
{ {
if (!ShowProgress(patchOffset, patchSize, patchName)) { if (!ShowProgress(patchOffset, patchSize, patchName)) {
if (ShowPrompt(true, "%s\nB button detected. Cancel?", patchName)) return displayError(IPS_CANCELED); if (ShowPrompt(true, "%s\n%s", patchName, STR_B_DETECTED_CANCEL)) return displayError(IPS_CANCELED);
ShowProgress(0, patchSize, patchName); ShowProgress(0, patchSize, patchName);
ShowProgress(patchOffset, patchSize, patchName); ShowProgress(patchOffset, patchSize, patchName);
} }
unsigned int size = read16(); unsigned int size = read16();
if (size == 0) if (size == 0)
{ {
@ -180,7 +181,7 @@ int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName
outlen_min = outlen; outlen_min = outlen;
error = IPS_OK; error = IPS_OK;
if (w_scrambled) error = IPS_SCRAMBLED; if (w_scrambled) error = IPS_SCRAMBLED;
// start applying patch // start applying patch
bool inPlace = false; bool inPlace = false;
if (!CheckWritePermissions(outName)) return displayError(IPS_INVALID_FILE_PATH); if (!CheckWritePermissions(outName)) return displayError(IPS_INVALID_FILE_PATH);
@ -193,36 +194,36 @@ int ApplyIPSPatch(const char* patchName, const char* inName, const char* outName
else if ((fvx_open(&inFile, inName, FA_READ) != FR_OK) || else if ((fvx_open(&inFile, inName, FA_READ) != FR_OK) ||
(fvx_open(&outFile, outName, FA_CREATE_ALWAYS | FA_WRITE | FA_READ) != FR_OK)) (fvx_open(&outFile, outName, FA_CREATE_ALWAYS | FA_WRITE | FA_READ) != FR_OK))
return displayError(IPS_INVALID_FILE_PATH); return displayError(IPS_INVALID_FILE_PATH);
size_t inSize = fvx_size(&inFile); size_t inSize = fvx_size(&inFile);
outlen = max(outlen_min, min(inSize, outlen_max)); outlen = max(outlen_min, min(inSize, outlen_max));
fvx_lseek(&outFile, max(outlen, outlen_min_mem)); fvx_lseek(&outFile, max(outlen, outlen_min_mem));
fvx_lseek(&outFile, 0); fvx_lseek(&outFile, 0);
size_t outSize = outlen; size_t outSize = outlen;
ShowProgress(0, outSize, outName); ShowProgress(0, outSize, outName);
fvx_lseek(&inFile, 0); fvx_lseek(&inFile, 0);
if (!inPlace && !IPScopy(COPY_IN, min(inSize, outlen), 0)) return displayError(IPS_MEMORY); if (!inPlace && !IPScopy(COPY_IN, min(inSize, outlen), 0)) return displayError(IPS_MEMORY);
fvx_lseek(&outFile, inSize); fvx_lseek(&outFile, inSize);
if (outSize > inSize && !IPScopy(COPY_RLE, outSize - inSize, 0)) return displayError(IPS_MEMORY); if (outSize > inSize && !IPScopy(COPY_RLE, outSize - inSize, 0)) return displayError(IPS_MEMORY);
fvx_lseek(&patchFile, 5); fvx_lseek(&patchFile, 5);
offset = read24(); offset = read24();
while (offset != 0x454F46) while (offset != 0x454F46)
{ {
if (!ShowProgress(offset, outSize, outName)) { if (!ShowProgress(offset, outSize, outName)) {
if (ShowPrompt(true, "%s\nB button detected. Cancel?", outName)) return displayError(IPS_CANCELED); if (ShowPrompt(true, "%s\n%s", outName, STR_B_DETECTED_CANCEL)) return displayError(IPS_CANCELED);
ShowProgress(0, outSize, outName); ShowProgress(0, outSize, outName);
ShowProgress(offset, outSize, outName); ShowProgress(offset, outSize, outName);
} }
fvx_lseek(&outFile, offset); fvx_lseek(&outFile, offset);
unsigned int size = read16(); unsigned int size = read16();
if (size == 0 && !IPScopy(COPY_RLE, read16(), read8())) return displayError(IPS_MEMORY); if (size == 0 && !IPScopy(COPY_RLE, read16(), read8())) return displayError(IPS_MEMORY);
else if (size != 0 && !IPScopy(COPY_PATCH, size, 0)) return displayError(IPS_MEMORY); else if (size != 0 && !IPScopy(COPY_PATCH, size, 0)) return displayError(IPS_MEMORY);
offset = read24(); offset = read24();
} }
fvx_lseek(&outFile, outSize); fvx_lseek(&outFile, outSize);
f_truncate(&outFile); f_truncate(&outFile);
return displayError(error); return displayError(error);

View File

@ -1,17 +1,14 @@
#include "ncch.h" #include "ncch.h"
#include "support.h"
#include "disadiff.h"
#include "keydb.h" #include "keydb.h"
#include "aes.h" #include "aes.h"
#include "sha.h" #include "sha.h"
#include "ff.h"
#define EXEFS_KEYID(name) (((strncmp(name, "banner", 8) == 0) || (strncmp(name, "icon", 8) == 0)) ? 0 : 1) #define EXEFS_KEYID(name) (((strncmp(name, "banner", 8) == 0) || (strncmp(name, "icon", 8) == 0)) ? 0 : 1)
u32 ValidateNcchHeader(NcchHeader* header) { u32 ValidateNcchHeader(NcchHeader* header) {
if (memcmp(header->magic, "NCCH", 4) != 0) // check magic number if (memcmp(header->magic, "NCCH", 4) != 0) // check magic number
return 1; return 1;
u32 ncch_units = (NCCH_EXTHDR_OFFSET + header->size_exthdr) / NCCH_MEDIA_UNIT; // exthdr u32 ncch_units = (NCCH_EXTHDR_OFFSET + header->size_exthdr) / NCCH_MEDIA_UNIT; // exthdr
if (header->size_plain) { // plain region if (header->size_plain) { // plain region
if (header->offset_plain < ncch_units) return 1; // overlapping plain region if (header->offset_plain < ncch_units) return 1; // overlapping plain region
@ -26,8 +23,8 @@ u32 ValidateNcchHeader(NcchHeader* header) {
ncch_units = (header->offset_romfs + header->size_romfs); ncch_units = (header->offset_romfs + header->size_romfs);
} }
// size check // size check
if (ncch_units > header->size) return 1; if (ncch_units > header->size) return 1;
return 0; return 0;
} }
@ -36,7 +33,7 @@ u32 GetNcchCtr(u8* ctr, NcchHeader* ncch, u8 section) {
if (ncch->version == 1) { if (ncch->version == 1) {
memcpy(ctr, &(ncch->partitionId), 8); memcpy(ctr, &(ncch->partitionId), 8);
if (section == 1) { // ExtHeader ctr if (section == 1) { // ExtHeader ctr
add_ctr(ctr, NCCH_EXTHDR_OFFSET); add_ctr(ctr, NCCH_EXTHDR_OFFSET);
} else if (section == 2) { // ExeFS ctr } else if (section == 2) { // ExeFS ctr
add_ctr(ctr, ncch->offset_exefs * NCCH_MEDIA_UNIT); add_ctr(ctr, ncch->offset_exefs * NCCH_MEDIA_UNIT);
} else if (section == 3) { // RomFS ctr } else if (section == 3) { // RomFS ctr
@ -47,101 +44,7 @@ u32 GetNcchCtr(u8* ctr, NcchHeader* ncch, u8 section) {
ctr[i] = ((u8*) &(ncch->partitionId))[7-i]; ctr[i] = ((u8*) &(ncch->partitionId))[7-i];
ctr[8] = section; ctr[8] = 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; return 0;
} }
@ -150,10 +53,10 @@ u32 SetNcchKey(NcchHeader* ncch, u16 crypto, u32 keyid) {
u8 flags7 = crypto & 0xFF; u8 flags7 = crypto & 0xFF;
u32 keyslot = (!keyid || !flags3) ? 0x2C : // standard / secure3 / secure4 / 7.x crypto u32 keyslot = (!keyid || !flags3) ? 0x2C : // standard / secure3 / secure4 / 7.x crypto
(flags3 == 0x0A) ? 0x18 : (flags3 == 0x0B) ? 0x1B : 0x25; (flags3 == 0x0A) ? 0x18 : (flags3 == 0x0B) ? 0x1B : 0x25;
if (flags7 & 0x04) if (flags7 & 0x04)
return 1; return 1;
if (flags7 & 0x01) { // fixed key crypto if (flags7 & 0x01) { // fixed key crypto
// from https://github.com/profi200/Project_CTR/blob/master/makerom/pki/dev.h // from https://github.com/profi200/Project_CTR/blob/master/makerom/pki/dev.h
u8 zeroKey[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, u8 zeroKey[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@ -164,11 +67,11 @@ u32 SetNcchKey(NcchHeader* ncch, u16 crypto, u32 keyid) {
use_aeskey(0x11); use_aeskey(0x11);
return 0; return 0;
} }
// load key X from file if required // load key X from file if required
if ((keyslot != 0x2C) && (LoadKeyFromFile(NULL, keyslot, 'X', NULL) != 0)) if ((keyslot != 0x2C) && (LoadKeyFromFile(NULL, keyslot, 'X', NULL) != 0))
return 1; return 1;
// key Y for seed and non seed // key Y for seed and non seed
if (keyid && (flags7 & 0x20)) { // seed crypto if (keyid && (flags7 & 0x20)) { // seed crypto
static u8 seedkeyY[16+16] __attribute__((aligned(32))) = { 0 }; static u8 seedkeyY[16+16] __attribute__((aligned(32))) = { 0 };
@ -177,7 +80,7 @@ u32 SetNcchKey(NcchHeader* ncch, u16 crypto, u32 keyid) {
if ((memcmp(lsignature, ncch->signature, 16) != 0) || (ltitleId != ncch->programId)) { if ((memcmp(lsignature, ncch->signature, 16) != 0) || (ltitleId != ncch->programId)) {
u8 keydata[16+16] __attribute__((aligned(4))); u8 keydata[16+16] __attribute__((aligned(4)));
memcpy(keydata, ncch->signature, 16); memcpy(keydata, ncch->signature, 16);
if (GetNcchSeed(keydata + 16, ncch) != 0) if (FindSeed(keydata + 16, ncch->programId, ncch->hash_seed) != 0)
return 1; return 1;
sha_quick(seedkeyY, keydata, 32, SHA256_MODE); sha_quick(seedkeyY, keydata, 32, SHA256_MODE);
memcpy(lsignature, ncch->signature, 16); memcpy(lsignature, ncch->signature, 16);
@ -188,12 +91,12 @@ u32 SetNcchKey(NcchHeader* ncch, u16 crypto, u32 keyid) {
setup_aeskeyY(keyslot, ncch->signature); setup_aeskeyY(keyslot, ncch->signature);
} }
use_aeskey(keyslot); use_aeskey(keyslot);
return 0; return 0;
} }
// this is used to force and check crypto setup // this is used to force and check crypto setup
// (also prevents SHA register usage later on) // (also prevents SHA register usage later on)
u32 SetupNcchCrypto(NcchHeader* ncch, u16 crypt_to) { u32 SetupNcchCrypto(NcchHeader* ncch, u16 crypt_to) {
u16 crypt_from = NCCH_GET_CRYPTO(ncch); u16 crypt_from = NCCH_GET_CRYPTO(ncch);
u32 res_from = ((crypt_from & NCCH_NOCRYPTO) || u32 res_from = ((crypt_from & NCCH_NOCRYPTO) ||
@ -207,14 +110,14 @@ u32 CryptNcchSection(void* data, u32 offset_data, u32 size_data, u32 offset_sect
u32 offset_ctr, NcchHeader* ncch, u32 snum, u16 crypt_to, u32 keyid) { u32 offset_ctr, NcchHeader* ncch, u32 snum, u16 crypt_to, u32 keyid) {
u16 crypt_from = NCCH_GET_CRYPTO(ncch); u16 crypt_from = NCCH_GET_CRYPTO(ncch);
const u32 mode = AES_CNT_CTRNAND_MODE; const u32 mode = AES_CNT_CTRNAND_MODE;
// check if section in data // check if section in data
if ((offset_section >= offset_data + size_data) || if ((offset_section >= offset_data + size_data) ||
(offset_data >= offset_section + size_section) || (offset_data >= offset_section + size_section) ||
!size_section) { !size_section) {
return 0; // section not in data return 0; // section not in data
} }
// determine data / offset / size // determine data / offset / size
u8* data8 = (u8*)data; u8* data8 = (u8*)data;
u8* data_i = data8; u8* data_i = data8;
@ -226,7 +129,7 @@ u32 CryptNcchSection(void* data, u32 offset_data, u32 size_data, u32 offset_sect
size_i = size_section - offset_i; size_i = size_section - offset_i;
if (size_i > size_data - (data_i - data8)) if (size_i > size_data - (data_i - data8))
size_i = size_data - (data_i - data8); size_i = size_data - (data_i - data8);
// actual decryption stuff // actual decryption stuff
u8 ctr[16]; u8 ctr[16];
GetNcchCtr(ctr, ncch, snum); GetNcchCtr(ctr, ncch, snum);
@ -238,7 +141,7 @@ u32 CryptNcchSection(void* data, u32 offset_data, u32 size_data, u32 offset_sect
if (SetNcchKey(ncch, crypt_to, keyid) != 0) return 1; if (SetNcchKey(ncch, crypt_to, keyid) != 0) return 1;
ctr_decrypt_byte(data_i, data_i, size_i, offset_i + offset_ctr, mode, ctr); ctr_decrypt_byte(data_i, data_i, size_i, offset_i + offset_ctr, mode, ctr);
} }
return 0; return 0;
} }
@ -247,11 +150,11 @@ u32 CryptNcch(void* data, u32 offset, u32 size, NcchHeader* ncch, ExeFsHeader* e
const u32 offset_flag3 = 0x188 + 3; const u32 offset_flag3 = 0x188 + 3;
const u32 offset_flag7 = 0x188 + 7; const u32 offset_flag7 = 0x188 + 7;
u16 crypt_from = NCCH_GET_CRYPTO(ncch); u16 crypt_from = NCCH_GET_CRYPTO(ncch);
// check for encryption // check for encryption
if ((crypt_to & crypt_from & NCCH_NOCRYPTO) || (crypt_to == crypt_from)) if ((crypt_to & crypt_from & NCCH_NOCRYPTO) || (crypt_to == crypt_from))
return 0; // desired end result already met return 0; // desired end result already met
// ncch flags handling // ncch flags handling
if ((offset <= offset_flag3) && (offset + size > offset_flag3)) if ((offset <= offset_flag3) && (offset + size > offset_flag3))
((u8*)data)[offset_flag3 - offset] = (crypt_to >> 8); ((u8*)data)[offset_flag3 - offset] = (crypt_to >> 8);
@ -259,7 +162,7 @@ u32 CryptNcch(void* data, u32 offset, u32 size, NcchHeader* ncch, ExeFsHeader* e
((u8*)data)[offset_flag7 - offset] &= ~(0x01|0x20|0x04); ((u8*)data)[offset_flag7 - offset] &= ~(0x01|0x20|0x04);
((u8*)data)[offset_flag7 - offset] |= (crypt_to & (0x01|0x20|0x04)); ((u8*)data)[offset_flag7 - offset] |= (crypt_to & (0x01|0x20|0x04));
} }
// exthdr handling // exthdr handling
if (ncch->size_exthdr) { if (ncch->size_exthdr) {
if (CryptNcchSection(data, offset, size, if (CryptNcchSection(data, offset, size,
@ -267,14 +170,14 @@ u32 CryptNcch(void* data, u32 offset, u32 size, NcchHeader* ncch, ExeFsHeader* e
NCCH_EXTHDR_SIZE, NCCH_EXTHDR_SIZE,
0, ncch, 1, crypt_to, 0) != 0) return 1; 0, ncch, 1, crypt_to, 0) != 0) return 1;
} }
// exefs handling // exefs handling
if (ncch->size_exefs) { if (ncch->size_exefs) {
// exefs header handling // exefs header handling
if (CryptNcchSection(data, offset, size, if (CryptNcchSection(data, offset, size,
ncch->offset_exefs * NCCH_MEDIA_UNIT, ncch->offset_exefs * NCCH_MEDIA_UNIT,
0x200, 0, ncch, 2, crypt_to, 0) != 0) return 1; 0x200, 0, ncch, 2, crypt_to, 0) != 0) return 1;
// exefs file handling // exefs file handling
if (exefs) for (u32 i = 0; i < 10; i++) { if (exefs) for (u32 i = 0; i < 10; i++) {
ExeFsFileHeader* file = exefs->files + i; ExeFsFileHeader* file = exefs->files + i;
@ -291,7 +194,7 @@ u32 CryptNcch(void* data, u32 offset, u32 size, NcchHeader* ncch, ExeFsHeader* e
ncch, 2, crypt_to, 0) != 0) return 1; ncch, 2, crypt_to, 0) != 0) return 1;
} }
} }
// romfs handling // romfs handling
if (ncch->size_romfs) { if (ncch->size_romfs) {
if (CryptNcchSection(data, offset, size, if (CryptNcchSection(data, offset, size,
@ -299,7 +202,7 @@ u32 CryptNcch(void* data, u32 offset, u32 size, NcchHeader* ncch, ExeFsHeader* e
ncch->size_romfs * NCCH_MEDIA_UNIT, ncch->size_romfs * NCCH_MEDIA_UNIT,
0, ncch, 3, crypt_to, 1) != 0) return 1; 0, ncch, 3, crypt_to, 1) != 0) return 1;
} }
return 0; return 0;
} }
@ -311,31 +214,31 @@ u32 CryptNcchSequential(void* data, u32 offset, u32 size, u16 crypt_to) {
static ExeFsHeader exefs = { 0 }; static ExeFsHeader exefs = { 0 };
static NcchHeader* ncchptr = NULL; static NcchHeader* ncchptr = NULL;
static ExeFsHeader* exefsptr = NULL; static ExeFsHeader* exefsptr = NULL;
// fetch ncch header from data // fetch ncch header from data
if ((offset == 0) && (size >= sizeof(NcchHeader))) { if ((offset == 0) && (size >= sizeof(NcchHeader))) {
memcpy(&ncch, data, sizeof(NcchHeader)); memcpy(&ncch, data, sizeof(NcchHeader));
ncchptr = (ValidateNcchHeader(&ncch) == 0) ? &ncch : NULL; ncchptr = (ValidateNcchHeader(&ncch) == 0) ? &ncch : NULL;
exefsptr = NULL; exefsptr = NULL;
} }
// safety check, ncch pointer // safety check, ncch pointer
if (!ncchptr) return 1; if (!ncchptr) return 1;
// fetch exefs header from data // fetch exefs header from data
if (ncchptr->offset_exefs && !exefsptr) { if (ncchptr->offset_exefs && !exefsptr) {
u32 offset_exefs = ncchptr->offset_exefs * NCCH_MEDIA_UNIT; u32 offset_exefs = ncchptr->offset_exefs * NCCH_MEDIA_UNIT;
if ((offset <= offset_exefs) && if ((offset <= offset_exefs) &&
((offset + size) >= offset_exefs + sizeof(ExeFsHeader))) { ((offset + size) >= offset_exefs + sizeof(ExeFsHeader))) {
memcpy(&exefs, (u8*)data + offset_exefs - offset, sizeof(ExeFsHeader)); memcpy(&exefs, (u8*)data + offset_exefs - offset, sizeof(ExeFsHeader));
if ((NCCH_ENCRYPTED(ncchptr)) && if ((NCCH_ENCRYPTED(ncchptr)) &&
(DecryptNcch((u8*) &exefs, offset_exefs, sizeof(ExeFsHeader), ncchptr, NULL) != 0)) (DecryptNcch((u8*) &exefs, offset_exefs, sizeof(ExeFsHeader), ncchptr, NULL) != 0))
return 1; return 1;
if (ValidateExeFsHeader(&exefs, 0) != 0) return 1; if (ValidateExeFsHeader(&exefs, 0) != 0) return 1;
exefsptr = &exefs; exefsptr = &exefs;
} }
} }
return CryptNcch(data, offset, size, ncchptr, exefsptr, crypt_to); return CryptNcch(data, offset, size, ncchptr, exefsptr, crypt_to);
} }
@ -343,17 +246,26 @@ u32 SetNcchSdFlag(void* data) { // data must be at least 0x600 byte and start wi
NcchHeader* ncch = (NcchHeader*) data; NcchHeader* ncch = (NcchHeader*) data;
NcchExtHeader* exthdr = (NcchExtHeader*) (void*) ((u8*)data + NCCH_EXTHDR_OFFSET); NcchExtHeader* exthdr = (NcchExtHeader*) (void*) ((u8*)data + NCCH_EXTHDR_OFFSET);
NcchExtHeader exthdr_dec; NcchExtHeader exthdr_dec;
if ((ValidateNcchHeader(ncch) != 0) || (!ncch->size_exthdr)) if ((ValidateNcchHeader(ncch) != 0) || (!ncch->size_exthdr))
return 0; // no extheader return 0; // no extheader
memcpy(&exthdr_dec, exthdr, sizeof(NcchExtHeader)); memcpy(&exthdr_dec, exthdr, sizeof(NcchExtHeader));
if (DecryptNcch((u8*) &exthdr_dec, NCCH_EXTHDR_OFFSET, sizeof(NcchExtHeader), ncch, NULL) != 0) if (DecryptNcch((u8*) &exthdr_dec, NCCH_EXTHDR_OFFSET, sizeof(NcchExtHeader), ncch, NULL) != 0)
return 1; return 1;
if (exthdr_dec.flag & (1<<1)) return 0; // flag already set if (exthdr_dec.flag & (1<<1)) return 0; // flag already set
exthdr_dec.flag |= (1<<1); exthdr_dec.flag |= (1<<1);
exthdr->flag ^= (1<<1); exthdr->flag ^= (1<<1);
sha_quick(ncch->hash_exthdr, &exthdr_dec, 0x400, SHA256_MODE); sha_quick(ncch->hash_exthdr, &exthdr_dec, 0x400, SHA256_MODE);
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; return 0;
} }

View File

@ -2,6 +2,7 @@
#include "common.h" #include "common.h"
#include "exefs.h" #include "exefs.h"
#include "seedsave.h"
#define NCCH_MEDIA_UNIT 0x200 #define NCCH_MEDIA_UNIT 0x200
@ -17,13 +18,6 @@
#define NCCH_STDCRYPTO 0x0000 #define NCCH_STDCRYPTO 0x0000
#define NCCH_GET_CRYPTO(ncch) (!NCCH_ENCRYPTED(ncch) ? NCCH_NOCRYPTO : (((ncch)->flags[3] << 8) | ((ncch)->flags[7]&(0x01|0x20)))) #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 // wrapper defines
#define DecryptNcch(data, offset, size, ncch, exefs) CryptNcch(data, offset, size, ncch, exefs, NCCH_NOCRYPTO) #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) #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]; u8 hash_romfs[0x20];
} __attribute__((packed, aligned(16))) NcchHeader; } __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 ValidateNcchHeader(NcchHeader* header);
u32 SetNcchKey(NcchHeader* ncch, u16 crypto, u32 keyid); u32 SetNcchKey(NcchHeader* ncch, u16 crypto, u32 keyid);
u32 SetupNcchCrypto(NcchHeader* ncch, u16 crypt_to); u32 SetupNcchCrypto(NcchHeader* ncch, u16 crypt_to);
u32 CryptNcch(void* data, u32 offset, u32 size, NcchHeader* ncch, ExeFsHeader* exefs, u16 crypto); 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 CryptNcchSequential(void* data, u32 offset, u32 size, u16 crypto);
u32 SetNcchSdFlag(void* data); u32 SetNcchSdFlag(void* data);
u32 SetupSystemForNcch(NcchHeader* ncch, bool to_emunand);
u32 AddSeedToDb(SeedInfo* seed_info, SeedInfoEntry* seed_entry);

View File

@ -18,25 +18,25 @@ u32 FixNcchInfoEntry(NcchInfoEntry* entry, u32 version) {
} else if (version != 4) { // !ncchinfo v4.0/v4.1/v4.2 } else if (version != 4) { // !ncchinfo v4.0/v4.1/v4.2
return 1; return 1;
} }
// poor man's UTF-16 -> UTF-8 // poor man's UTF-16 -> UTF-8
if (entry->filename[1] == 0x00) { if (entry->filename[1] == 0x00) {
for (u32 i = 1; i < (sizeof(entry->filename) / 2); i++) for (u32 i = 1; i < (sizeof(entry->filename) / 2); i++)
entry->filename[i] = entry->filename[i*2]; entry->filename[i] = entry->filename[i*2];
} }
// fix sdmc: prefix // fix sdmc: prefix
if (memcmp(entry->filename, "sdmc:", 5) == 0) if (memcmp(entry->filename, "sdmc:", 5) == 0)
memmove(entry->filename, entry->filename + 5, 112 - 5); memmove(entry->filename, entry->filename + 5, 112 - 5);
// workaround (1) for older (v4.0) ncchinfo.bin // workaround (1) for older (v4.0) ncchinfo.bin
// this combination means seed crypto rather than FixedKey // this combination means seed crypto rather than FixedKey
if ((entry->ncchFlag7 == 0x01) && entry->ncchFlag3) if ((entry->ncchFlag7 == 0x01) && entry->ncchFlag3)
entry->ncchFlag7 = 0x20; entry->ncchFlag7 = 0x20;
// workaround (2) for older (v4.1) ncchinfo.bin // workaround (2) for older (v4.1) ncchinfo.bin
if (!entry->size_b) entry->size_b = entry->size_mb * 1024 * 1024; if (!entry->size_b) entry->size_b = entry->size_mb * 1024 * 1024;
return 0; return 0;
} }
@ -50,10 +50,10 @@ u32 BuildNcchInfoXorpad(void* buffer, NcchInfoEntry* entry, u32 size, u32 offset
ncch.programId = ncch.partitionId = entry->titleId; ncch.programId = ncch.partitionId = entry->titleId;
if (SetNcchKey(&ncch, NCCH_GET_CRYPTO(&ncch), 1) != 0) if (SetNcchKey(&ncch, NCCH_GET_CRYPTO(&ncch), 1) != 0)
return 1; return 1;
// write xorpad // write xorpad
memset(buffer, 0, size); memset(buffer, 0, size);
ctr_decrypt_byte(buffer, buffer, size, offset, AES_CNT_CTRNAND_MODE, entry->ctr); ctr_decrypt_byte(buffer, buffer, size, offset, AES_CNT_CTRNAND_MODE, entry->ctr);
return 0; return 0;
} }

View File

@ -6,7 +6,7 @@ u32 ValidateNcsdHeader(NcsdHeader* header) {
if ((memcmp(header->magic, "NCSD", 4) != 0) || // check magic number 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 (memcmp(header->partitions_fs_type, zeroes, 8) != 0) || !header->mediaId) // prevent detection of NAND images
return 1; return 1;
u32 data_units = 0; u32 data_units = 0;
for (u32 i = 0; i < 8; i++) { for (u32 i = 0; i < 8; i++) {
NcchPartition* partition = header->partitions + i; NcchPartition* partition = header->partitions + i;
@ -18,7 +18,7 @@ u32 ValidateNcsdHeader(NcsdHeader* header) {
} }
if (data_units > header->size) if (data_units > header->size)
return 1; return 1;
return 0; return 0;
} }
@ -30,7 +30,7 @@ u64 GetNcsdTrimmedSize(NcsdHeader* header) {
if (!partition->size) continue; if (!partition->size) continue;
data_units = (partition_end > data_units) ? partition_end : data_units; data_units = (partition_end > data_units) ? partition_end : data_units;
} }
return data_units * NCSD_MEDIA_UNIT; return data_units * NCSD_MEDIA_UNIT;
} }
@ -38,11 +38,11 @@ u64 GetNcsdTrimmedSize(NcsdHeader* header) {
u32 CryptNcsdSequential(void* data, u32 offset_data, u32 size_data, u16 crypto) { u32 CryptNcsdSequential(void* data, u32 offset_data, u32 size_data, u16 crypto) {
// warning: this will only work for sequential processing // warning: this will only work for sequential processing
static NcsdHeader ncsd; static NcsdHeader ncsd;
// fetch ncsd header from data // fetch ncsd header from data
if ((offset_data == 0) && (size_data >= sizeof(NcsdHeader))) if ((offset_data == 0) && (size_data >= sizeof(NcsdHeader)))
memcpy(&ncsd, data, sizeof(NcsdHeader)); memcpy(&ncsd, data, sizeof(NcsdHeader));
for (u32 i = 0; i < 8; i++) { for (u32 i = 0; i < 8; i++) {
NcchPartition* partition = ncsd.partitions + i; NcchPartition* partition = ncsd.partitions + i;
u32 offset_p = partition->offset * NCSD_MEDIA_UNIT; u32 offset_p = partition->offset * NCSD_MEDIA_UNIT;
@ -68,7 +68,7 @@ u32 CryptNcsdSequential(void* data, u32 offset_data, u32 size_data, u16 crypto)
if (CryptNcchSequential(data_i, offset_i, size_i, crypto) != 0) if (CryptNcchSequential(data_i, offset_i, size_i, crypto) != 0)
return 1; return 1;
} }
return 0; return 0;
} }

View File

@ -27,32 +27,53 @@ u32 ValidateTwlHeader(TwlHeader* twl) {
return (crc16_quick(twl->logo, sizeof(twl->logo)) == NDS_LOGO_CRC16) ? 0 : 1; return (crc16_quick(twl->logo, sizeof(twl->logo)) == NDS_LOGO_CRC16) ? 0 : 1;
} }
u32 VerifyTwlIconData(TwlIconData* icon, u32 version) {
u32 tsize = TWLICON_SIZE_DATA(version);
u32 isize = TWLICON_SIZE_DATA(icon->version);
u8* icn = (u8*) icon;
if (!isize) return 1;
if (version && (!tsize || tsize > isize)) return 1;
u32 size = version ? tsize : isize;
if ((size >= 0x0840) && (crc16_quick(icn + 0x0020, 0x0840 - 0x0020) != icon->crc_0x0020_0x0840)) return 1;
if ((size >= 0x0940) && (crc16_quick(icn + 0x0020, 0x0940 - 0x0020) != icon->crc_0x0020_0x0940)) return 1;
if ((size >= 0x1240) && (crc16_quick(icn + 0x0020, 0x0A40 - 0x0020) != icon->crc_0x0020_0x0A40)) return 1;
if ((size >= 0x23C0) && (crc16_quick(icn + 0x1240, 0x23C0 - 0x1240) != icon->crc_0x1240_0x23C0)) return 1;
return 0;
}
u32 BuildTwlSaveHeader(void* sav, u32 size) { u32 BuildTwlSaveHeader(void* sav, u32 size) {
const u16 sct_size = 0x200; const u16 sct_size = 0x200;
if (size / (u32) sct_size > 0xFFFF) if (size / (u32) sct_size > 0xFFFF)
return 1; return 1;
// fit max number of sectors into size // fit max number of sectors into size
// that's how Nintendo does it ¯\_(ツ)_/¯ // that's how Nintendo does it ¯\_(ツ)_/¯
const u16 n_sct_max = size / sct_size; const u16 n_sct_max = size / sct_size;
u16 n_sct = 1; u16 n_sct = 1;
u16 sct_track = 1; u16 sct_track = 1;
u16 sct_heads = 1; u16 sct_heads = 1;
while (true) { u16 n_sct_next = 0;
if (sct_heads < sct_track) { while (n_sct_next <= n_sct_max) {
u16 n_sct_next = sct_track * (sct_heads+1) * (sct_heads+1); n_sct_next = sct_track * (sct_heads + 1) * (sct_heads + 1);
if (n_sct_next < n_sct_max) { if (n_sct_next <= n_sct_max) {
sct_heads++; sct_heads++;
n_sct = n_sct_next;
sct_track++;
n_sct_next = sct_track * sct_heads * sct_heads;
if (n_sct_next <= n_sct_max) {
n_sct = n_sct_next; n_sct = n_sct_next;
} else break; }
} else {
u16 n_sct_next = (sct_track+1) * sct_heads * sct_heads;
if (n_sct_next < n_sct_max) {
sct_track++;
n_sct = n_sct_next;
} else break;
} }
} }
n_sct_next = (sct_track + 1) * sct_heads * sct_heads;
if (n_sct_next <= n_sct_max) {
sct_track++;
n_sct = n_sct_next;
}
// sectors per cluster (should be identical to Nintendo) // sectors per cluster (should be identical to Nintendo)
u8 clr_size = (n_sct > 8 * 1024) ? 8 : (n_sct > 1024) ? 4 : 1; u8 clr_size = (n_sct > 8 * 1024) ? 8 : (n_sct > 1024) ? 4 : 1;
@ -126,11 +147,19 @@ u32 GetTwlIcon(u16* icon, const TwlIconData* twl_icon) {
u32 ix = x + (i & 0x7); u32 ix = x + (i & 0x7);
u32 iy = y + (i >> 3); u32 iy = y + (i >> 3);
pix555 = palette[((i%2) ? (*pix4 >> 4) : *pix4) & 0xF]; int palette_index = ((i%2) ? (*pix4 >> 4) : *pix4) & 0xF;
r = pix555 & 0x1F; if (palette_index) {
g = ((pix555 >> 5) & 0x1F) << 1; pix555 = palette[palette_index];
g |= (g >> 1) & 1; r = pix555 & 0x1F;
b = (pix555 >> 10) & 0x1F; g = ((pix555 >> 5) & 0x1F) << 1;
g |= (g >> 1) & 1;
b = (pix555 >> 10) & 0x1F;
} else {
// Set transparent pixels to white
r = 31;
g = 63;
b = 31;
}
icon[(iy * w) + ix] = (r << 11) | (g << 5) | b; icon[(iy * w) + ix] = (r << 11) | (g << 5) | b;
if (i % 2) pix4++; if (i % 2) pix4++;
} }
@ -143,15 +172,15 @@ u32 FindNitroRomDir(u32 dirid, u32* fileid, u8** fnt_entry, TwlHeader* hdr, u8*
NitroFntBaseEntry* fnt_base = (NitroFntBaseEntry*) fnt; NitroFntBaseEntry* fnt_base = (NitroFntBaseEntry*) fnt;
NitroFntBaseEntry* fnt_dir = &((NitroFntBaseEntry*) fnt)[dirid]; NitroFntBaseEntry* fnt_dir = &((NitroFntBaseEntry*) fnt)[dirid];
NitroFatEntry* fat_lut = (NitroFatEntry*) fat; NitroFatEntry* fat_lut = (NitroFatEntry*) fat;
// base sanity checks // base sanity checks
if (fnt_base->parent_id*sizeof(NitroFntBaseEntry) > fnt_base->subtable_offset) return 1; // invalid FNT if (fnt_base->parent_id*sizeof(NitroFntBaseEntry) > fnt_base->subtable_offset) return 1; // invalid FNT
if (dirid >= fnt_base->parent_id) return 1; // dir ID out of bounds if (dirid >= fnt_base->parent_id) return 1; // dir ID out of bounds
// set first FNT entry / fileid // set first FNT entry / fileid
*fnt_entry = fnt + fnt_dir->subtable_offset; *fnt_entry = fnt + fnt_dir->subtable_offset;
*fileid = fnt_dir->file0_id; *fileid = fnt_dir->file0_id;
// check subtable / directory validity // check subtable / directory validity
u32 fid = *fileid; u32 fid = *fileid;
for (u8* entry = *fnt_entry;; entry = FNT_ENTRY_NEXT(entry)) { for (u8* entry = *fnt_entry;; entry = FNT_ENTRY_NEXT(entry)) {
@ -161,29 +190,29 @@ u32 FindNitroRomDir(u32 dirid, u32* fileid, u8** fnt_entry, TwlHeader* hdr, u8*
if (!FNT_ENTRY_ISDIR(entry)) fid++; if (!FNT_ENTRY_ISDIR(entry)) fid++;
} }
if (fid*sizeof(NitroFatEntry) > hdr->fat_size) return 1; // corrupt fnt / fat if (fid*sizeof(NitroFatEntry) > hdr->fat_size) return 1; // corrupt fnt / fat
return 0; return 0;
} }
u32 NextNitroRomEntry(u32* fileid, u8** fnt_entry) { u32 NextNitroRomEntry(u32* fileid, u8** fnt_entry) {
// check for end of subtable // check for end of subtable
if (!*fnt_entry || !**fnt_entry) return 1; if (!*fnt_entry || !**fnt_entry) return 1;
// advance to next entry // advance to next entry
if (!FNT_ENTRY_ISDIR(*fnt_entry)) (*fileid)++; if (!FNT_ENTRY_ISDIR(*fnt_entry)) (*fileid)++;
*fnt_entry += FNT_ENTRY_LEN(*fnt_entry); *fnt_entry += FNT_ENTRY_LEN(*fnt_entry);
// check for end of subtable // check for end of subtable
if (!**fnt_entry) return 1; if (!**fnt_entry) return 1;
return 0; return 0;
} }
u32 ReadNitroRomEntry(u64* offset, u64* size, bool* is_dir, u32 fileid, u8* fnt_entry, u8* fat) { u32 ReadNitroRomEntry(u64* offset, u64* size, bool* is_dir, u32 fileid, u8* fnt_entry, u8* fat) {
// check for end of subtable // check for end of subtable
if (!fnt_entry || !*fnt_entry) return 1; if (!fnt_entry || !*fnt_entry) return 1;
// decipher FNT entry // decipher FNT entry
*is_dir = FNT_ENTRY_ISDIR(fnt_entry); *is_dir = FNT_ENTRY_ISDIR(fnt_entry);
if (!(*is_dir)) { // for files if (!(*is_dir)) { // for files
@ -195,6 +224,6 @@ u32 ReadNitroRomEntry(u64* offset, u64* size, bool* is_dir, u32 fileid, u8* fnt_
*offset = (u64) (fnt_entry[1+fnlen]|(fnt_entry[1+fnlen+1]<<8)) & 0xFFF; // dir ID goes in offset *offset = (u64) (fnt_entry[1+fnlen]|(fnt_entry[1+fnlen+1]<<8)) & 0xFFF; // dir ID goes in offset
*size = 0; *size = 0;
} }
return 0; return 0;
} }

View File

@ -7,7 +7,7 @@
// see: http://problemkaputt.de/gbatek.htm#dscartridgeicontitle // see: http://problemkaputt.de/gbatek.htm#dscartridgeicontitle
// v0x0001 -> 0x0840 byte (contains JPN, USA, FRE, GER, ITA, ESP titles) // v0x0001 -> 0x0840 byte (contains JPN, USA, FRE, GER, ITA, ESP titles)
// v0x0002 -> 0x0940 byte (adds CHN title) // v0x0002 -> 0x0940 byte (adds CHN title)
// v0x0003 -> 0x0A40 byte (adds KOR title) // v0x0003 -> 0x0A40 byte (adds KOR title)
// v0x0103 -> 0x23C0 byte (adds TWL animated icon data) // v0x0103 -> 0x23C0 byte (adds TWL animated icon data)
#define TWLICON_SIZE_DATA(v) ((v == 0x0001) ? 0x0840 : (v == 0x0002) ? 0x0940 : \ #define TWLICON_SIZE_DATA(v) ((v == 0x0001) ? 0x0840 : (v == 0x0002) ? 0x0940 : \
(v == 0x0003) ? 0x1240 : (v == 0x0103) ? 0x23C0 : 0x0000) (v == 0x0003) ? 0x1240 : (v == 0x0103) ? 0x23C0 : 0x0000)
@ -127,6 +127,7 @@ typedef struct {
} PACKED_STRUCT TwlHeader; } PACKED_STRUCT TwlHeader;
u32 ValidateTwlHeader(TwlHeader* twl); u32 ValidateTwlHeader(TwlHeader* twl);
u32 VerifyTwlIconData(TwlIconData* icon, u32 size);
u32 BuildTwlSaveHeader(void* sav, u32 size); u32 BuildTwlSaveHeader(void* sav, u32 size);
u32 LoadTwlMetaData(const char* path, TwlHeader* hdr, TwlIconData* icon); u32 LoadTwlMetaData(const char* path, TwlHeader* hdr, TwlIconData* icon);
u32 GetTwlTitle(char* desc, const TwlIconData* twl_icon); u32 GetTwlTitle(char* desc, const TwlIconData* twl_icon);

View File

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

View File

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

View File

@ -58,12 +58,12 @@ u32 BuildLv3Index(RomFsLv3Index* index, u8* lv3) {
index->filehash = (u32*) (void*) (lv3 + hdr->offset_filehash); index->filehash = (u32*) (void*) (lv3 + hdr->offset_filehash);
index->filemeta = lv3 + hdr->offset_filemeta; index->filemeta = lv3 + hdr->offset_filemeta;
index->filedata = NULL; index->filedata = NULL;
index->mod_dir = (hdr->size_dirhash / sizeof(u32)); index->mod_dir = (hdr->size_dirhash / sizeof(u32));
index->mod_file = (hdr->size_filehash / sizeof(u32)); index->mod_file = (hdr->size_filehash / sizeof(u32));
index->size_dirmeta = hdr->size_dirmeta; index->size_dirmeta = hdr->size_dirmeta;
index->size_filemeta = hdr->size_filemeta; index->size_filemeta = hdr->size_filemeta;
return 0; return 0;
} }
@ -77,13 +77,13 @@ u32 HashLv3Path(u16* wname, u32 name_len, u32 offset_parent) {
RomFsLv3DirMeta* GetLv3DirMeta(const char* name, u32 offset_parent, RomFsLv3Index* index) { RomFsLv3DirMeta* GetLv3DirMeta(const char* name, u32 offset_parent, RomFsLv3Index* index) {
RomFsLv3DirMeta* meta; RomFsLv3DirMeta* meta;
// wide (UTF-16) name // wide (UTF-16) name
u16 wname[256]; u16 wname[256];
int name_len = utf8_to_utf16(wname, (u8*) name, 255, 255); int name_len = utf8_to_utf16(wname, (u8*) name, 255, 255);
if (name_len <= 0) return NULL; if (name_len <= 0) return NULL;
wname[name_len] = 0; wname[name_len] = 0;
// hashing, first offset // hashing, first offset
u32 hash = HashLv3Path(wname, name_len, offset_parent); u32 hash = HashLv3Path(wname, name_len, offset_parent);
u32 offset = index->dirhash[hash % index->mod_dir]; u32 offset = index->dirhash[hash % index->mod_dir];
@ -96,19 +96,19 @@ RomFsLv3DirMeta* GetLv3DirMeta(const char* name, u32 offset_parent, RomFsLv3Inde
(memcmp(wname, meta->wname, name_len * 2) == 0)) (memcmp(wname, meta->wname, name_len * 2) == 0))
break; break;
} }
return (offset >= index->size_dirmeta) ? NULL : meta; return (offset >= index->size_dirmeta) ? NULL : meta;
} }
RomFsLv3FileMeta* GetLv3FileMeta(const char* name, u32 offset_parent, RomFsLv3Index* index) { RomFsLv3FileMeta* GetLv3FileMeta(const char* name, u32 offset_parent, RomFsLv3Index* index) {
RomFsLv3FileMeta* meta; RomFsLv3FileMeta* meta;
// wide (UTF-16) name // wide (UTF-16) name
u16 wname[256]; u16 wname[256];
int name_len = utf8_to_utf16(wname, (u8*) name, 255, 255); int name_len = utf8_to_utf16(wname, (u8*) name, 255, 255);
if (name_len <= 0) return NULL; if (name_len <= 0) return NULL;
wname[name_len] = 0; wname[name_len] = 0;
// hashing, first offset // hashing, first offset
u32 hash = HashLv3Path(wname, name_len, offset_parent); u32 hash = HashLv3Path(wname, name_len, offset_parent);
u32 offset = index->filehash[hash % index->mod_file]; u32 offset = index->filehash[hash % index->mod_file];
@ -121,6 +121,6 @@ RomFsLv3FileMeta* GetLv3FileMeta(const char* name, u32 offset_parent, RomFsLv3In
(memcmp(wname, meta->wname, name_len * 2) == 0)) (memcmp(wname, meta->wname, name_len * 2) == 0))
break; break;
} }
return (offset >= index->size_filemeta) ? NULL : meta; return (offset >= index->size_filemeta) ? NULL : meta;
} }

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

@ -1,17 +1,31 @@
#include "tad.h" #include "tad.h"
#include "sha.h"
u32 VerifyTadStub(TadStub* tad) {
TadFooter* ftr = &(tad->footer);
TadHeader* hdr = &(tad->header);
TadBanner* bnr = &(tad->banner);
if ((strncmp(hdr->magic, TAD_HEADER_MAGIC, strlen(TAD_HEADER_MAGIC)) != 0) ||
(sha_cmp(ftr->banner_sha256, bnr, sizeof(TadBanner), SHA256_MODE) != 0) ||
(sha_cmp(ftr->header_sha256, hdr, sizeof(TadHeader), SHA256_MODE) != 0))
return 1;
return 0;
}
u32 BuildTadContentTable(void* table, void* header) { u32 BuildTadContentTable(void* table, void* header) {
TadHeader* hdr = (TadHeader*) header; TadHeader* hdr = (TadHeader*) header;
TadContentTable* tbl = (TadContentTable*) table; TadContentTable* tbl = (TadContentTable*) table;
if (strncmp(hdr->magic, TAD_HEADER_MAGIC, strlen(TAD_HEADER_MAGIC)) != 0) if (strncmp(hdr->magic, TAD_HEADER_MAGIC, strlen(TAD_HEADER_MAGIC)) != 0)
return 1; return 1;
tbl->banner_end = 0 + sizeof(TadBanner) + sizeof(TadBlockMetaData); tbl->banner_end = 0 + sizeof(TadBanner) + sizeof(TadBlockMetaData);
tbl->header_end = tbl->banner_end + sizeof(TadHeader) + sizeof(TadBlockMetaData); tbl->header_end = tbl->banner_end + sizeof(TadHeader) + sizeof(TadBlockMetaData);
tbl->footer_end = tbl->header_end + sizeof(TadFooter) + sizeof(TadBlockMetaData); tbl->footer_end = tbl->header_end + sizeof(TadFooter) + sizeof(TadBlockMetaData);
u32 content_end_last = tbl->footer_end; u32 content_end_last = tbl->footer_end;
for (u32 i = 0; i < TAD_NUM_CONTENT; i++) { for (u32 i = 0; i < TAD_NUM_CONTENT; i++) {
tbl->content_end[i] = content_end_last; tbl->content_end[i] = content_end_last;
@ -19,6 +33,6 @@ u32 BuildTadContentTable(void* table, void* header) {
tbl->content_end[i] += align(hdr->content_size[i], 0x10) + sizeof(TadBlockMetaData); tbl->content_end[i] += align(hdr->content_size[i], 0x10) + sizeof(TadBlockMetaData);
content_end_last = tbl->content_end[i]; content_end_last = tbl->content_end[i];
} }
return 0; return 0;
} }

View File

@ -56,4 +56,14 @@ typedef struct {
u8 padding[0x4]; u8 padding[0x4];
} PACKED_STRUCT TadFooter; } PACKED_STRUCT TadFooter;
typedef struct {
TadBanner banner;
TadBlockMetaData banner_bmd;
TadHeader header;
TadBlockMetaData header_bmd;
TadFooter footer;
TadBlockMetaData footer_bmd;
} PACKED_STRUCT TadStub;
u32 VerifyTadStub(TadStub* tad);
u32 BuildTadContentTable(void* table, void* header); u32 BuildTadContentTable(void* table, void* header);

View File

@ -19,47 +19,113 @@ u32 ValidateTicket(Ticket* ticket) {
return 0; return 0;
} }
u32 ValidateTicketSignature(Ticket* ticket) { u32 ValidateTwlTicket(Ticket* ticket) {
static bool got_modexp = false; static const u8 magic[] = { TICKET_SIG_TYPE_TWL };
static u32 mod[0x100 / 0x4] = { 0 }; if ((memcmp(ticket->sig_type, magic, sizeof(magic)) != 0) ||
static u32 exp = 0; (strncmp((char*) ticket->issuer, TICKET_ISSUER_TWL, 0x40) != 0))
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), GetTicketSize(ticket) - 0x140))
return 1; return 1;
return 0; return 0;
} }
u32 BuildFakeTicket(Ticket* ticket, u8* title_id) { u32 ValidateTicketSignature(Ticket* ticket) {
Certificate cert;
// 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 static const u8 sig_type[4] = { TICKET_SIG_TYPE }; // RSA_2048 SHA256
static const u8 ticket_cnt_index[] = { // whatever this is
0x00, 0x01, 0x00, 0x14, 0x00, 0x00, 0x00, 0xAC, 0x00, 0x00, 0x00, 0x14, 0x00, 0x01, 0x00, 0x14, // calculate sizes and determine pointers to use
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x84, u32 rights_field_count = (min(index_max, 0x10000) + 1023) >> 10; // round up to 1024 and cap at 65536, and div by 1024
0x00, 0x00, 0x00, 0x84, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 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 // set ticket all zero for a clean start
memset(ticket, 0x00, TICKET_COMMON_SIZE); // 0xAC being size of this fake ticket's content index memset(_ticket, 0x00, _ticket_size);
// fill ticket values // fill ticket values
memcpy(ticket->sig_type, sig_type, 4); memcpy(_ticket->sig_type, sig_type, 4);
memset(ticket->signature, 0xFF, 0x100); memset(_ticket->signature, 0xFF, 0x100);
snprintf((char*) ticket->issuer, 0x40, IS_DEVKIT ? TICKET_ISSUER_DEV : TICKET_ISSUER); snprintf((char*) _ticket->issuer, 0x40, IS_DEVKIT ? TICKET_ISSUER_DEV : TICKET_ISSUER);
memset(ticket->ecdsa, 0xFF, 0x3C); memset(_ticket->ecdsa, 0xFF, 0x3C);
ticket->version = 0x01; _ticket->version = 0x01;
memset(ticket->titlekey, 0xFF, 16); memset(_ticket->titlekey, 0xFF, 16);
memcpy(ticket->title_id, title_id, 8); if (title_id) memcpy(_ticket->title_id, title_id, 8);
ticket->commonkey_idx = 0x00; // eshop _ticket->commonkey_idx = 0x00; // eshop
ticket->audit = 0x01; // whatever _ticket->audit = 0x01; // whatever
memcpy(ticket->content_index, ticket_cnt_index, sizeof(ticket_cnt_index));
memset(&ticket->content_index[sizeof(ticket_cnt_index)], 0xFF, 0x80); // 1024 content indexes // 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, 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; return 0;
} }
@ -73,34 +139,29 @@ u32 GetTicketSize(const Ticket* ticket) {
u32 BuildTicketCert(u8* tickcert) { u32 BuildTicketCert(u8* tickcert) {
static 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, 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 0xE6, 0xB1, 0x34, 0xD5, 0x52, 0x88, 0x73, 0xC9, 0x61, 0xFB, 0xC7, 0x95, 0xCB, 0x47, 0xE6, 0x97
}; };
static 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, 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 0xC6, 0x4B, 0xD4, 0x8F, 0xDF, 0x13, 0x21, 0x3D, 0xFC, 0x72, 0xFC, 0x8D, 0x9F, 0xDD, 0x01, 0x0E
}; };
// open certs.db file on SysNAND static const char* const retail_issuers[] = {"Root-CA00000003-XS0000000c", "Root-CA00000003"};
FIL db; static const char* const dev_issuers[] = {"Root-CA00000004-XS00000009", "Root-CA00000004"};
UINT bytes_read;
if (f_open(&db, "1:/dbs/certs.db", FA_READ | FA_OPEN_EXISTING) != FR_OK) size_t size = TICKET_CDNCERT_SIZE;
if (BuildRawCertBundleFromCertDb(tickcert, &size, !IS_DEVKIT ? retail_issuers : dev_issuers, 2) ||
size != TICKET_CDNCERT_SIZE) {
return 1; return 1;
// grab ticket cert from 3 offsets }
f_lseek(&db, 0x3F10);
f_read(&db, tickcert + 0x000, 0x300, &bytes_read);
f_lseek(&db, 0x0C10);
f_read(&db, tickcert + 0x300, 0x1F0, &bytes_read);
f_lseek(&db, 0x3A00);
f_read(&db, tickcert + 0x4F0, 0x210, &bytes_read);
f_close(&db);
// check the certificate hash // check the certificate hash
u8 cert_hash[0x20]; u8 cert_hash[0x20];
sha_quick(cert_hash, tickcert, TICKET_CDNCERT_SIZE, SHA256_MODE); sha_quick(cert_hash, tickcert, TICKET_CDNCERT_SIZE, SHA256_MODE);
if (memcmp(cert_hash, IS_DEVKIT ? cert_hash_expected_dev : cert_hash_expected, 0x20) != 0) if (memcmp(cert_hash, IS_DEVKIT ? cert_hash_expected_dev : cert_hash_expected, 0x20) != 0)
return 1; return 1;
return 0; return 0;
} }

View File

@ -1,15 +1,22 @@
#pragma once #pragma once
#include "common.h" #include "common.h"
#include "tmd.h"
#define TICKET_COMMON_SIZE sizeof(TicketCommon) #define TICKET_COMMON_SIZE sizeof(TicketCommon)
#define TICKET_MINIMUM_SIZE sizeof(TicketMinimum) #define TICKET_MINIMUM_SIZE sizeof(TicketMinimum)
#define TICKET_TWL_SIZE sizeof(Ticket)
#define TICKET_CDNCERT_SIZE 0x700 #define TICKET_CDNCERT_SIZE 0x700
#define TICKET_MAX_CONTENTS TITLE_MAX_CONTENTS // should be TMD_MAX_CONTENTS
#define TICKET_COMMON_CNT_INDEX_SIZE (0x28 + (((TICKET_MAX_CONTENTS + 1023) >> 10) * 0x84))
#define TICKET_ISSUER "Root-CA00000003-XS0000000c" #define TICKET_ISSUER "Root-CA00000003-XS0000000c"
#define TICKET_ISSUER_DEV "Root-CA00000004-XS00000009" #define TICKET_ISSUER_DEV "Root-CA00000004-XS00000009"
#define TICKET_SIG_TYPE 0x00, 0x01, 0x00, 0x04 // RSA_2048 SHA256 #define TICKET_SIG_TYPE 0x00, 0x01, 0x00, 0x04 // RSA_2048 SHA256
#define TICKET_ISSUER_TWL "Root-CA00000001-XS00000006"
#define TICKET_SIG_TYPE_TWL 0x00, 0x01, 0x00, 0x01 // RSA_2048 SHA1
#define TICKET_DEVKIT(tick) (strncmp((char*)tick->issuer, TICKET_ISSUER_DEV, 0x40) == 0) #define TICKET_DEVKIT(tick) (strncmp((char*)tick->issuer, TICKET_ISSUER_DEV, 0x40) == 0)
// from: https://github.com/profi200/Project_CTR/blob/02159e17ee225de3f7c46ca195ff0f9ba3b3d3e4/ctrtool/tik.h#L15-L39 // from: https://github.com/profi200/Project_CTR/blob/02159e17ee225de3f7c46ca195ff0f9ba3b3d3e4/ctrtool/tik.h#L15-L39
@ -46,18 +53,18 @@
typedef struct { typedef struct {
TICKETBASE; TICKETBASE;
u8 content_index[]; u8 content_index[];
} __attribute__((packed, aligned(4))) Ticket; } PACKED_STRUCT Ticket;
typedef struct { typedef struct {
TICKETBASE; TICKETBASE;
u8 content_index[0xAC]; u8 content_index[TICKET_COMMON_CNT_INDEX_SIZE];
} __attribute__((packed, aligned(4))) TicketCommon; } PACKED_STRUCT TicketCommon;
// minimum allowed content_index is 0x14 // minimum allowed content_index is 0x14
typedef struct { typedef struct {
TICKETBASE; TICKETBASE;
u8 content_index[0x14]; u8 content_index[0x14];
} __attribute__((packed, aligned(4))) TicketMinimum; } PACKED_STRUCT TicketMinimum;
typedef struct { typedef struct {
u8 unk1[2]; u8 unk1[2];
@ -67,7 +74,7 @@ typedef struct {
u8 unk3[2]; u8 unk3[2];
u8 unk4[2]; u8 unk4[2];
u8 unk5[4]; u8 unk5[4];
} __attribute__((packed)) TicketContentIndexMainHeader; } PACKED_ALIGN(1) TicketContentIndexMainHeader;
typedef struct { typedef struct {
u8 data_relative_offset[4]; // relative to content index start u8 data_relative_offset[4]; // relative to content index start
@ -76,14 +83,14 @@ typedef struct {
u8 total_size_used[4]; // also no effect u8 total_size_used[4]; // also no effect
u8 data_type[2]; // perhaps, does have effect and change with different data like on 0004000D tickets u8 data_type[2]; // perhaps, does have effect and change with different data like on 0004000D tickets
u8 unknown[2]; // or padding u8 unknown[2]; // or padding
} __attribute__((packed)) TicketContentIndexDataHeader; } PACKED_ALIGN(1) TicketContentIndexDataHeader;
// data type == 3 // data type == 3
typedef struct { typedef struct {
u8 unk[2]; // seemly has no meaning u8 unk[2]; // seemly has no meaning
u8 indexoffset[2]; u8 indexoffset[2];
u8 rightsbitfield[0x80]; u8 rightsbitfield[0x80];
} __attribute__((packed)) TicketRightsField; } PACKED_ALIGN(1) TicketRightsField;
typedef struct { typedef struct {
size_t count; size_t count;
@ -91,8 +98,9 @@ typedef struct {
} TicketRightsCheck; } TicketRightsCheck;
u32 ValidateTicket(Ticket* ticket); u32 ValidateTicket(Ticket* ticket);
u32 ValidateTwlTicket(Ticket* ticket);
u32 ValidateTicketSignature(Ticket* ticket); u32 ValidateTicketSignature(Ticket* ticket);
u32 BuildFakeTicket(Ticket* ticket, u8* title_id); u32 BuildFakeTicket(Ticket* ticket, const u8* title_id);
u32 GetTicketContentIndexSize(const Ticket* ticket); u32 GetTicketContentIndexSize(const Ticket* ticket);
u32 GetTicketSize(const Ticket* ticket); u32 GetTicketSize(const Ticket* ticket);
u32 BuildTicketCert(u8* tickcert); u32 BuildTicketCert(u8* tickcert);

View File

@ -1,17 +1,11 @@
#include "ticketdb.h" #include "ticketdb.h"
#include "vbdri.h" #include "bdri.h"
#include "support.h" #include "support.h"
#include "aes.h" #include "aes.h"
#include "vff.h"
#include "fsinit.h" #include "fsinit.h"
#include "image.h" #include "image.h"
const char* virtual_tickdb_dirs[] = { #define PART_PATH "D:/partitionA.bin"
"homebrew",
"eshop",
"system",
"unknown",
};
u32 CryptTitleKey(TitleKeyEntry* tik, bool encrypt, bool devkit) { u32 CryptTitleKey(TitleKeyEntry* tik, bool encrypt, bool devkit) {
// From https://github.com/profi200/Project_CTR/blob/master/makerom/pki/prod.h#L19 // From https://github.com/profi200/Project_CTR/blob/master/makerom/pki/prod.h#L19
@ -32,18 +26,24 @@ u32 CryptTitleKey(TitleKeyEntry* tik, bool encrypt, bool devkit) {
{0x75, 0x05, 0x52, 0xBF, 0xAA, 0x1C, 0x04, 0x07, 0x55, 0xC8, 0xD5, 0x9A, 0x55, 0xF9, 0xAD, 0x1F} , // 4 {0x75, 0x05, 0x52, 0xBF, 0xAA, 0x1C, 0x04, 0x07, 0x55, 0xC8, 0xD5, 0x9A, 0x55, 0xF9, 0xAD, 0x1F} , // 4
{0xAA, 0xDA, 0x4C, 0xA8, 0xF6, 0xE5, 0xA9, 0x77, 0xE0, 0xA0, 0xF9, 0xE4, 0x76, 0xCF, 0x0D, 0x63} , // 5 {0xAA, 0xDA, 0x4C, 0xA8, 0xF6, 0xE5, 0xA9, 0x77, 0xE0, 0xA0, 0xF9, 0xE4, 0x76, 0xCF, 0x0D, 0x63} , // 5
}; };
// From unknown source
static const u8 common_key_twl[16] __attribute__((aligned(16))) =
{0xAF, 0x1B, 0xF5, 0x16, 0xA8, 0x07, 0xD2, 0x1A, 0xEA, 0x45, 0x98, 0x4F, 0x04, 0x74, 0x28, 0x61}; // TWL
u32 mode = (encrypt) ? AES_CNT_TITLEKEY_ENCRYPT_MODE : AES_CNT_TITLEKEY_DECRYPT_MODE; u32 mode = (encrypt) ? AES_CNT_TITLEKEY_ENCRYPT_MODE : AES_CNT_TITLEKEY_DECRYPT_MODE;
u8 ctr[16] = { 0 }; u8 ctr[16] = { 0 };
// setup key 0x3D // ctr if (getbe16(tik->title_id) == 0x3) { // setup TWL key
if (tik->commonkey_idx >= 6) return 1; setup_aeskey(0x11, (void*) common_key_twl);
if (!devkit) setup_aeskeyY(0x3D, (void*) common_keyy[tik->commonkey_idx]); use_aeskey(0x11);
else setup_aeskey(0x3D, (void*) common_key_dev[tik->commonkey_idx]); } else { // setup key 0x3D // ctr
use_aeskey(0x3D); if (!devkit) setup_aeskeyY(0x3D, (void*) common_keyy[tik->commonkey_idx]);
else setup_aeskey(0x3D, (void*) common_key_dev[tik->commonkey_idx]);
use_aeskey(0x3D);
}
memcpy(ctr, tik->title_id, 8); memcpy(ctr, tik->title_id, 8);
set_ctr(ctr); set_ctr(ctr);
// decrypt / encrypt the titlekey // decrypt / encrypt the titlekey
aes_decrypt(tik->titlekey, tik->titlekey, 1, mode); aes_decrypt(tik->titlekey, tik->titlekey, 1, mode);
return 0; return 0;
@ -54,78 +54,65 @@ u32 GetTitleKey(u8* titlekey, Ticket* ticket) {
memcpy(tik.title_id, ticket->title_id, 8); memcpy(tik.title_id, ticket->title_id, 8);
memcpy(tik.titlekey, ticket->titlekey, 16); memcpy(tik.titlekey, ticket->titlekey, 16);
tik.commonkey_idx = ticket->commonkey_idx; tik.commonkey_idx = ticket->commonkey_idx;
if (CryptTitleKey(&tik, false, TICKET_DEVKIT(ticket)) != 0) return 1; if (CryptTitleKey(&tik, false, TICKET_DEVKIT(ticket)) != 0) return 1;
memcpy(titlekey, tik.titlekey, 16); memcpy(titlekey, tik.titlekey, 16);
return 0; return 0;
} }
u32 SetTitleKey(const u8* titlekey, Ticket* ticket) {
TitleKeyEntry tik = { 0 };
memcpy(tik.title_id, ticket->title_id, 8);
memcpy(tik.titlekey, titlekey, 16);
tik.commonkey_idx = ticket->commonkey_idx;
if (CryptTitleKey(&tik, true, TICKET_DEVKIT(ticket)) != 0) return 1;
memcpy(ticket->titlekey, tik.titlekey, 16);
return 0;
}
u32 FindTicket(Ticket** ticket, u8* title_id, bool force_legit, bool emunand) { u32 FindTicket(Ticket** ticket, u8* title_id, bool force_legit, bool emunand) {
const char* path_db = TICKDB_PATH(emunand); // EmuNAND / SysNAND const char* path_db = TICKDB_PATH(emunand); // EmuNAND / SysNAND
char path_store[256] = { 0 }; char path_store[256] = { 0 };
char* path_bak = NULL; char* path_bak = NULL;
// just to be safe
*ticket = NULL;
// store previous mount path
strncpy(path_store, GetMountPath(), 256); strncpy(path_store, GetMountPath(), 256);
if (*path_store) path_bak = path_store; if (*path_store) path_bak = path_store;
if (!InitImgFS(path_db)) if (!InitImgFS(path_db))
return 1; return 1;
char tid_string[17]; // search ticket in database
u64 tid = getbe64(title_id); if (ReadTicketFromDB(PART_PATH, title_id, ticket) != 0) {
snprintf(tid_string, 17, "%016llX", tid); InitImgFS(path_bak);
return 1;
DIR dir;
FILINFO fno;
char dir_path[12];
char tik_path[64];
for (u32 i = force_legit ? 1 : 0; i < 4; i++) {
snprintf(dir_path, 12, "T:/%s", virtual_tickdb_dirs[i]);
if (fvx_opendir(&dir, dir_path) != FR_OK) {
InitImgFS(path_bak);
return 1;
}
while ((fvx_readdir(&dir, &fno) == FR_OK) && *(fno.fname)) {
if (strncmp(tid_string, fno.fname, 16) == 0) {
snprintf(tik_path, 64, "%s/%s", dir_path, fno.fname);
u32 size = fvx_qsize(tik_path);
if (!(*ticket = malloc(size))) {
InitImgFS(path_bak);
return 1;
}
if ((fvx_qread(tik_path, *ticket, 0, size, NULL) != FR_OK) ||
(force_legit && (ValidateTicketSignature(*ticket) != 0))) {
free(*ticket);
InitImgFS(path_bak);
return 1;
}
InitImgFS(path_bak);
return 0;
}
}
fvx_closedir(&dir);
} }
// (optional) validate ticket signature
if (force_legit && (ValidateTicketSignature(*ticket) != 0)) {
free(*ticket);
*ticket = NULL;
InitImgFS(path_bak);
return 1;
}
InitImgFS(path_bak); InitImgFS(path_bak);
return 1; return 0;
} }
u32 FindTitleKey(Ticket* ticket, u8* title_id) { u32 FindTitleKey(Ticket* ticket, u8* title_id) {
bool found = false; bool found = false;
TitleKeysInfo* tikdb = (TitleKeysInfo*) malloc(STD_BUFFER_SIZE); // more than enough
if (!tikdb) return 1;
// search for a titlekey inside encTitleKeys.bin / decTitleKeys.bin // search for a titlekey inside encTitleKeys.bin / decTitleKeys.bin
// when found, add it to the ticket // when found, add it to the ticket
TitleKeysInfo* tikdb = (TitleKeysInfo*) malloc(STD_BUFFER_SIZE); // more than enough
if (!tikdb) return 1;
for (u32 enc = 0; (enc <= 1) && !found; enc++) { for (u32 enc = 0; (enc <= 1) && !found; enc++) {
u32 len = LoadSupportFile((enc) ? TIKDB_NAME_ENC : TIKDB_NAME_DEC, tikdb, STD_BUFFER_SIZE); u32 len = LoadSupportFile((enc) ? TIKDB_NAME_ENC : TIKDB_NAME_DEC, tikdb, STD_BUFFER_SIZE);
if (len == 0) continue; // file not found if (len == 0) continue; // file not found
if (tikdb->n_entries > (len - 16) / 32) if (tikdb->n_entries > (len - 16) / 32)
continue; // filesize / titlekey db size mismatch continue; // filesize / titlekey db size mismatch
@ -141,11 +128,31 @@ u32 FindTitleKey(Ticket* ticket, u8* title_id) {
break; break;
} }
} }
free(tikdb); free(tikdb);
// desperate measures - search in the internal ticket database
Ticket* ticket_tmp = NULL;
if (FindTicket(&ticket_tmp, title_id, false, false) == 0) {
memcpy(ticket->titlekey, ticket_tmp->titlekey, 16);
ticket->commonkey_idx = ticket_tmp->commonkey_idx;
free(ticket_tmp);
found = true;
}
return (found) ? 0 : 1; return (found) ? 0 : 1;
} }
u32 FindTitleKeyForId(u8* titlekey, u8* title_id) {
TicketCommon tik;
if ((BuildFakeTicket((Ticket*) &tik, title_id) != 0) ||
(FindTitleKey((Ticket*) &tik, title_id) != 0) ||
(GetTitleKey(titlekey, (Ticket*) &tik) != 0))
return 1;
return 0;
}
u32 AddTitleKeyToInfo(TitleKeysInfo* tik_info, TitleKeyEntry* tik_entry, bool decrypted_in, bool decrypted_out, bool devkit) { u32 AddTitleKeyToInfo(TitleKeysInfo* tik_info, TitleKeyEntry* tik_entry, bool decrypted_in, bool decrypted_out, bool devkit) {
if (!tik_entry) { // no titlekey entry -> reset database if (!tik_entry) { // no titlekey entry -> reset database
memset(tik_info, 0, 16); memset(tik_info, 0, 16);
@ -171,3 +178,9 @@ u32 AddTicketToInfo(TitleKeysInfo* tik_info, Ticket* ticket, bool decrypt) { //
tik.commonkey_idx = ticket->commonkey_idx; tik.commonkey_idx = ticket->commonkey_idx;
return AddTitleKeyToInfo(tik_info, &tik, false, decrypt, TICKET_DEVKIT(ticket)); return AddTitleKeyToInfo(tik_info, &tik, false, decrypt, TICKET_DEVKIT(ticket));
} }
u32 CryptTitleKeyInfo(TitleKeysInfo* tik_info, bool encrypt) {
for (u32 t = 0; t < tik_info->n_entries; t++)
CryptTitleKey(tik_info->entries + t, encrypt, false);
return 0;
}

View File

@ -35,7 +35,10 @@ typedef struct {
u32 GetTitleKey(u8* titlekey, Ticket* ticket); u32 GetTitleKey(u8* titlekey, Ticket* ticket);
u32 SetTitleKey(const u8* titlekey, Ticket* ticket);
u32 FindTicket(Ticket** ticket, u8* title_id, bool force_legit, bool emunand); u32 FindTicket(Ticket** ticket, u8* title_id, bool force_legit, bool emunand);
u32 FindTitleKey(Ticket* ticket, u8* title_id); u32 FindTitleKey(Ticket* ticket, u8* title_id);
u32 FindTitleKeyForId(u8* titlekey, u8* title_id);
u32 AddTitleKeyToInfo(TitleKeysInfo* tik_info, TitleKeyEntry* tik_entry, bool decrypted_in, bool decrypted_out, bool devkit); u32 AddTitleKeyToInfo(TitleKeysInfo* tik_info, TitleKeyEntry* tik_entry, bool decrypted_in, bool decrypted_out, bool devkit);
u32 AddTicketToInfo(TitleKeysInfo* tik_info, Ticket* ticket, bool decrypt); u32 AddTicketToInfo(TitleKeysInfo* tik_info, Ticket* ticket, bool decrypt);
u32 CryptTitleKeyInfo(TitleKeysInfo* tik_info, bool encrypt);

View File

@ -8,7 +8,7 @@ u32 BuildTitleInfoEntryTmd(TitleInfoEntry* tie, TitleMetaData* tmd, bool sd) {
u64 title_id = getbe64(tmd->title_id); u64 title_id = getbe64(tmd->title_id);
bool has_idx1 = false; bool has_idx1 = false;
bool has_idx2 = false; bool has_idx2 = false;
// set basic values // set basic values
memset(tie, 0x00, sizeof(TitleInfoEntry)); memset(tie, 0x00, sizeof(TitleInfoEntry));
tie->title_type = 0x40; tie->title_type = 0x40;
@ -34,7 +34,7 @@ u32 BuildTitleInfoEntryTmd(TitleInfoEntry* tie, TitleMetaData* tmd, bool sd) {
((tmd->twl_flag & 0x2) ? align(sizeof(TwlIconData), align_size) : 0); ((tmd->twl_flag & 0x2) ? align(sizeof(TwlIconData), align_size) : 0);
} }
// contents title size + some additional stuff // contents title size + some additional stuff
TmdContentChunk* chunk = (TmdContentChunk*) (tmd + 1); TmdContentChunk* chunk = (TmdContentChunk*) (tmd + 1);
tie->content0_id = getbe32(chunk->id); tie->content0_id = getbe32(chunk->id);
for (u32 i = 0; (i < content_count) && (i < TMD_MAX_CONTENTS); i++, chunk++) { for (u32 i = 0; (i < content_count) && (i < TMD_MAX_CONTENTS); i++, chunk++) {
@ -44,7 +44,9 @@ u32 BuildTitleInfoEntryTmd(TitleInfoEntry* tie, TitleMetaData* tmd, bool sd) {
} }
// manual? dlp? save? (we need to properly check this later) // manual? dlp? save? (we need to properly check this later)
if (((title_id >> 32) == 0x00040000) || ((title_id >> 32) == 0x00040010)) { if (((title_id >> 32) == 0x00040000) ||
((title_id >> 32) == 0x0004000E) ||
((title_id >> 32) == 0x00040010)) {
if (has_idx1) tie->flags_0[0] = 0x1; // this may have a manual if (has_idx1) tie->flags_0[0] = 0x1; // this may have a manual
if (has_idx2) tie->title_version |= (0xFFFF << 16); // this may have a dlp if (has_idx2) tie->title_version |= (0xFFFF << 16); // this may have a dlp
if (getle32(tmd->save_size)) tie->flags_1[0] = 0x01; // this may have an sd save if (getle32(tmd->save_size)) tie->flags_1[0] = 0x01; // this may have an sd save
@ -55,7 +57,7 @@ u32 BuildTitleInfoEntryTmd(TitleInfoEntry* tie, TitleMetaData* tmd, bool sd) {
u32 BuildTitleInfoEntryTwl(TitleInfoEntry* tie, TitleMetaData* tmd, TwlHeader* twl) { u32 BuildTitleInfoEntryTwl(TitleInfoEntry* tie, TitleMetaData* tmd, TwlHeader* twl) {
u64 title_id = getbe64(tmd->title_id); u64 title_id = getbe64(tmd->title_id);
// build the basic titledb entry // build the basic titledb entry
if (BuildTitleInfoEntryTmd(tie, tmd, false) != 0) return 1; if (BuildTitleInfoEntryTmd(tie, tmd, false) != 0) return 1;
@ -90,7 +92,7 @@ u32 BuildTitleInfoEntryNcch(TitleInfoEntry* tie, TitleMetaData* tmd, NcchHeader*
// NCCH titles need no content0 ID // NCCH titles need no content0 ID
tie->content0_id = 0; tie->content0_id = 0;
// specific flags // specific flags
// see: http://3dbrew.org/wiki/Titles // see: http://3dbrew.org/wiki/Titles
if (!((title_id >> 32) & 0x10)) // not a system title if (!((title_id >> 32) & 0x10)) // not a system title

View File

@ -14,25 +14,29 @@ u32 ValidateTmd(TitleMetaData* tmd) {
return 0; return 0;
} }
u32 ValidateTmdSignature(TitleMetaData* tmd) { u32 ValidateTwlTmd(TitleMetaData* tmd) {
static bool got_modexp = false; static const u8 magic[] = { TMD_SIG_TYPE_TWL };
static u32 mod[0x100 / 4] = { 0 }; if ((memcmp(tmd->sig_type, magic, sizeof(magic)) != 0) ||
static u32 exp = 0; (strncmp((char*) tmd->issuer, TMD_ISSUER_TWL, 0x40) != 0) ||
(getbe16(tmd->content_count) != 1))
if (!got_modexp) {
// grab mod/exp from cert from cert.db
if (LoadCertFromCertDb(0x3C10, NULL, mod, &exp) == 0)
got_modexp = true;
else return 1;
}
if (!RSA_setKey2048(3, mod, exp) ||
!RSA_verify2048((void*) &(tmd->signature), (void*) &(tmd->issuer), 0xC4))
return 1; return 1;
return 0; return 0;
} }
u32 ValidateTmdSignature(TitleMetaData* tmd) {
Certificate cert;
// grab cert from certs.db
if (LoadCertFromCertDb(&cert, (char*)(tmd->issuer)) != 0)
return 1;
int ret = Certificate_VerifySignatureBlock(&cert, &(tmd->signature), 0x100, (void*)&(tmd->issuer), 0xC4, true);
Certificate_Cleanup(&cert);
return ret;
}
u32 VerifyTmd(TitleMetaData* tmd) { u32 VerifyTmd(TitleMetaData* tmd) {
TmdContentChunk* content_list = (TmdContentChunk*) (tmd + 1); TmdContentChunk* content_list = (TmdContentChunk*) (tmd + 1);
u32 content_count = getbe16(tmd->content_count); u32 content_count = getbe16(tmd->content_count);
@ -98,39 +102,34 @@ u32 BuildFakeTmd(TitleMetaData* tmd, u8* title_id, u32 n_contents, u32 save_size
memcpy(tmd->contentinfo[0].cmd_count, tmd->content_count, 2); memcpy(tmd->contentinfo[0].cmd_count, tmd->content_count, 2);
memset(tmd->contentinfo[0].hash, 0xFF, 0x20); // placeholder (hash) memset(tmd->contentinfo[0].hash, 0xFF, 0x20); // placeholder (hash)
// nothing to do for content list (yet) // nothing to do for content list (yet)
return 0; return 0;
} }
u32 BuildTmdCert(u8* tmdcert) { u32 BuildTmdCert(u8* tmdcert) {
static 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, 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 0xF6, 0xFE, 0xA1, 0xEB, 0x8C, 0xCF, 0x59, 0x6E, 0x69, 0xBA, 0x2A, 0x38, 0x8D, 0x73, 0x8A, 0xE1
}; };
static 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, 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 0xC2, 0xE9, 0xCA, 0x93, 0x94, 0xF4, 0x29, 0xA0, 0x38, 0x54, 0x75, 0xFF, 0xAB, 0x6E, 0x8E, 0x71
}; };
// open certs.db file on SysNAND static const char* const retail_issuers[] = {"Root-CA00000003-CP0000000b", "Root-CA00000003"};
FIL db; static const char* const dev_issuers[] = {"Root-CA00000004-CP0000000a", "Root-CA00000004"};
UINT bytes_read;
if (f_open(&db, "1:/dbs/certs.db", FA_READ | FA_OPEN_EXISTING) != FR_OK) size_t size = TMD_CDNCERT_SIZE;
if (BuildRawCertBundleFromCertDb(tmdcert, &size, !IS_DEVKIT ? retail_issuers : dev_issuers, 2) ||
size != TMD_CDNCERT_SIZE) {
return 1; return 1;
// grab TMD cert from 3 offsets }
f_lseek(&db, 0x3C10);
f_read(&db, tmdcert + 0x000, 0x300, &bytes_read);
f_lseek(&db, 0x0C10);
f_read(&db, tmdcert + 0x300, 0x1F0, &bytes_read);
f_lseek(&db, 0x3A00);
f_read(&db, tmdcert + 0x4F0, 0x210, &bytes_read);
f_close(&db);
// check the certificate hash // check the certificate hash
u8 cert_hash[0x20]; u8 cert_hash[0x20];
sha_quick(cert_hash, tmdcert, TMD_CDNCERT_SIZE, SHA256_MODE); sha_quick(cert_hash, tmdcert, TMD_CDNCERT_SIZE, SHA256_MODE);
if (memcmp(cert_hash, IS_DEVKIT ? cert_hash_expected_dev : cert_hash_expected, 0x20) != 0) if (memcmp(cert_hash, IS_DEVKIT ? cert_hash_expected_dev : cert_hash_expected, 0x20) != 0)
return 1; return 1;
return 0; return 0;
} }

View File

@ -2,17 +2,22 @@
#include "common.h" #include "common.h"
#define TMD_MAX_CONTENTS 1000 // 383 // theme CIAs contain maximum 100 themes + 1 index content #define TMD_MAX_CONTENTS TITLE_MAX_CONTENTS // 1024 // 383 // theme CIAs contain maximum 100 themes + 1 index content
#define TMD_SIZE_MIN sizeof(TitleMetaData) #define TMD_SIZE_MIN sizeof(TitleMetaData)
#define TMD_SIZE_MAX (sizeof(TitleMetaData) + (TMD_MAX_CONTENTS*sizeof(TmdContentChunk))) #define TMD_SIZE_MAX (sizeof(TitleMetaData) + (TMD_MAX_CONTENTS*sizeof(TmdContentChunk)))
#define TMD_SIZE_N(n) (sizeof(TitleMetaData) + (n*sizeof(TmdContentChunk))) #define TMD_SIZE_N(n) (sizeof(TitleMetaData) + (n*sizeof(TmdContentChunk)))
#define TMD_SIZE_STUB (TMD_SIZE_MIN - (0x20 + (64 * sizeof(TmdContentInfo))))
#define TMD_SIZE_TWL (TMD_SIZE_STUB + 0x24)
#define TMD_CDNCERT_SIZE 0x700 #define TMD_CDNCERT_SIZE 0x700
#define TMD_ISSUER "Root-CA00000003-CP0000000b" #define TMD_ISSUER "Root-CA00000003-CP0000000b"
#define TMD_ISSUER_DEV "Root-CA00000004-CP0000000a" #define TMD_ISSUER_DEV "Root-CA00000004-CP0000000a"
#define TMD_SIG_TYPE 0x00, 0x01, 0x00, 0x04 // RSA_2048 SHA256 #define TMD_SIG_TYPE 0x00, 0x01, 0x00, 0x04 // RSA_2048 SHA256
#define TMD_ISSUER_TWL "Root-CA00000001-CP00000007"
#define TMD_SIG_TYPE_TWL 0x00, 0x01, 0x00, 0x01 // RSA_2048 SHA1
#define DLC_TID_HIGH 0x00, 0x04, 0x00, 0x8C // title id high for DLC #define DLC_TID_HIGH 0x00, 0x04, 0x00, 0x8C // title id high for DLC
// from: https://github.com/profi200/Project_CTR/blob/02159e17ee225de3f7c46ca195ff0f9ba3b3d3e4/ctrtool/tmd.h#L18-L59; // from: https://github.com/profi200/Project_CTR/blob/02159e17ee225de3f7c46ca195ff0f9ba3b3d3e4/ctrtool/tmd.h#L18-L59;
@ -58,6 +63,7 @@ typedef struct {
} __attribute__((packed, aligned(4))) TitleMetaData; } __attribute__((packed, aligned(4))) TitleMetaData;
u32 ValidateTmd(TitleMetaData* tmd); u32 ValidateTmd(TitleMetaData* tmd);
u32 ValidateTwlTmd(TitleMetaData* tmd);
u32 ValidateTmdSignature(TitleMetaData* tmd); u32 ValidateTmdSignature(TitleMetaData* tmd);
u32 VerifyTmd(TitleMetaData* tmd); u32 VerifyTmd(TitleMetaData* tmd);
u32 GetTmdCtr(u8* ctr, TmdContentChunk* chunk); u32 GetTmdCtr(u8* ctr, TmdContentChunk* chunk);

View File

@ -18,6 +18,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/> * along with this program. If not, see <http://www.gnu.org/licenses/>
*/ */
#include <arm.h>
#include "card_spi.h" #include "card_spi.h"
#include <spi.h> #include <spi.h>
#include "timer.h" #include "timer.h"
@ -30,7 +31,7 @@
#define SPI_512B_EEPROM_CMD_RDLO 3 #define SPI_512B_EEPROM_CMD_RDLO 3
#define SPI_512B_EEPROM_CMD_RDHI 11 #define SPI_512B_EEPROM_CMD_RDHI 11
#define SPI_EEPROM_CMD_WRITE 2 #define SPI_EEPROM_CMD_WRITE 2
#define SPI_CMD_READ 3 #define SPI_CMD_READ 3
@ -57,58 +58,56 @@ int CardSPIWriteSaveData_24bit_erase_program(CardSPIType type, u32 offset, const
int CardSPIEraseSector_emulated(CardSPIType type, u32 offset); int CardSPIEraseSector_emulated(CardSPIType type, u32 offset);
int CardSPIEraseSector_real(CardSPIType type, u32 offset); int CardSPIEraseSector_real(CardSPIType type, u32 offset);
const CardSPITypeData EEPROM_512B_ = { CardSPIEnableWriting_512B, CardSPIReadSaveData_9bit, CardSPIWriteSaveData_9bit, CardSPIEraseSector_emulated, 0xffffff, 1 << 9, 16, 16, 16, 0, 0, 0 }; const CardSPITypeData EEPROM_512B = { CardSPIEnableWriting_512B, CardSPIReadSaveData_9bit, CardSPIWriteSaveData_9bit, CardSPIEraseSector_emulated, 0xffff, 1 << 9, 16, 16, 16, 0, 0, 0 };
const CardSPITypeData EEPROM_DUMMY = { CardSPIEnableWriting_regular, CardSPIReadSaveData_16bit, CardSPIWriteSaveData_16bit, CardSPIEraseSector_emulated, 0xffffff, UINT32_MAX, 1, 1, 1, SPI_EEPROM_CMD_WRITE, 0, 0 }; const CardSPITypeData EEPROM_DUMMY = { CardSPIEnableWriting_regular, CardSPIReadSaveData_16bit, CardSPIWriteSaveData_16bit, CardSPIEraseSector_emulated, 0xffff, UINT32_MAX, 1, 1, 1, SPI_EEPROM_CMD_WRITE, 0, 0 };
const CardSPITypeData EEPROMTypes[] = { const CardSPITypeData EEPROMTypes[] = {
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_16bit, CardSPIWriteSaveData_16bit, CardSPIEraseSector_emulated, 0xffffff, 1 << 13, 32, 32, 32, SPI_EEPROM_CMD_WRITE, 0, 0}, // EEPROM 8 KB { CardSPIEnableWriting_regular, CardSPIReadSaveData_16bit, CardSPIWriteSaveData_16bit, CardSPIEraseSector_emulated, 0xffff, 1 << 13, 32, 32, 32, SPI_EEPROM_CMD_WRITE, 0, 0}, // EEPROM 8 KB
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_16bit, CardSPIWriteSaveData_16bit, CardSPIEraseSector_emulated, 0xffffff, 1 << 16, 128, 128, 128, SPI_EEPROM_CMD_WRITE, 0, 0}, // EEPROM 64 KB { CardSPIEnableWriting_regular, CardSPIReadSaveData_16bit, CardSPIWriteSaveData_16bit, CardSPIEraseSector_emulated, 0xffff, 1 << 16, 128, 128, 128, SPI_EEPROM_CMD_WRITE, 0, 0}, // EEPROM 64 KB
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_write, CardSPIEraseSector_emulated, 0xffffff, 1 << 17, 256, 256, 256, SPI_EEPROM_CMD_WRITE, 0, 0}, // EEPROM 128 KB { CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_write, CardSPIEraseSector_emulated, 0xffff, 1 << 17, 256, 256, 256, SPI_EEPROM_CMD_WRITE, 0, 0}, // EEPROM 128 KB
}; };
const CardSPITypeData FLASH_DUMMY = { NULL, CardSPIReadSaveData_24bit, NULL, NULL, 0x0, 0, 0, 0, 0, 0, 0, 0 }; const CardSPITypeData FLASH_DUMMY = { NULL, CardSPIReadSaveData_24bit, NULL, NULL, 0x0, 0, 0, 0, 0, 0, 0, 0 };
const CardSPITypeData flashTypes[] = { const CardSPITypeData flashTypes[] = {
// NTR/TWL // NTR/TWL
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_write, CardSPIEraseSector_real, 0x204012, 1 << 18, 65536, 256, 256, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0x621600, 1 << 18, 65536, 256, 65536, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE }, // Most common flash chip in DS games, in 3 different sizes (256kB: ST M45PE20, 512kB: ST M45PE40, 1MB: ST M45PE80)
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_write, CardSPIEraseSector_real, 0x204013, 1 << 19, 65536, 256, 256, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE }, { CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_write, CardSPIEraseSector_real, 0x2040, 0, 65536, 256, 256, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_write, CardSPIEraseSector_real, 0x621100, 1 << 19, 65536, 256, 256, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_write, CardSPIEraseSector_real, 0x204014, 1 << 20, 65536, 256, 256, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE }, // Found in Mario Kart, for example (always 256kB, Sanyo LE25FW203T)
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0x202017, 1 << 23, 65536, 256, 65536, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE }, { CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0x6216, 1 << 18, 65536, 256, 65536, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
// CTR
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0xC22211, 1 << 17, 4096, 32, 4096, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_MXIC_SE }, // Found in some copies of Pokemon Diamond/Pearl (always 512kB, Sanyo ??)
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0xC22213, 1 << 19, 4096, 32, 4096, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_MXIC_SE }, { CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_write, CardSPIEraseSector_real, 0x6211, 1 << 19, 65536, 256, 256, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0xC22214, 1 << 20, 4096, 32, 4096, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_MXIC_SE },
// Found in Art Academy, and maybe some other ones (8MB: ST M25P64)
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0x2020, 0, 65536, 256, 65536, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
// Found in some DS bootlegs (ST M25PE40VP in my case)
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0x2080, 0, 65536, 256, 65536, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_SE },
// This is the most common type of flash 3DS cartridges. Not normally found in DS ones, but check anyway. (Custom MXIC chips)
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0xC222, 0, 4096, 32, 4096, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_MXIC_SE },
// Found this in some copies of ArtAcademy, I think it's something from MXIC (Thank you for the help @FerozElMejor on Discord)
{ CardSPIEnableWriting_regular, CardSPIReadSaveData_24bit, CardSPIWriteSaveData_24bit_erase_program, CardSPIEraseSector_real, 0xC220, 0, 4096, 32, 4096, SPI_FLASH_CMD_PW, SPI_CMD_PP, SPI_FLASH_CMD_MXIC_SE },
}; };
const CardSPITypeData * const EEPROM_512B = &EEPROM_512B_; const CardSPITypeData * const FLASH_CTR_GENERIC = flashTypes + 5;
const CardSPITypeData * const EEPROM_8KB = EEPROMTypes + 0;
const CardSPITypeData * const EEPROM_64KB = EEPROMTypes + 1;
const CardSPITypeData * const EEPROM_128KB = EEPROMTypes + 2;
const CardSPITypeData * const FLASH_256KB_1 = flashTypes + 0;
const CardSPITypeData * const FLASH_256KB_2 = flashTypes + 1;
const CardSPITypeData * const FLASH_512KB_1 = flashTypes + 2;
const CardSPITypeData * const FLASH_512KB_2 = flashTypes + 3;
const CardSPITypeData * const FLASH_1MB = flashTypes + 4;
const CardSPITypeData * const FLASH_8MB = flashTypes + 5;
const CardSPITypeData * const FLASH_128KB_CTR = flashTypes + 6;
const CardSPITypeData * const FLASH_512KB_CTR = flashTypes + 7;
const CardSPITypeData * const FLASH_1MB_CTR = flashTypes + 8;
#define REG_CFG9_CARDCTL *((vu16*)0x1000000C) #define REG_CFG9_CARDCTL *((vu16*)0x1000000C)
#define CARDCTL_SPICARD (1u<<8) #define CARDCTL_SPICARD (1u<<8)
int CardSPIWriteRead(CardSPIType type, const void* cmd, u32 cmdSize, void* answer, u32 answerSize, const void* data, u32 dataSize) { int CardSPIWriteRead(bool infrared, const void* cmd, u32 cmdSize, void* answer, u32 answerSize, const void* data, u32 dataSize) {
u32 headerFooterVal = 0; u32 headerFooterVal = 0;
REG_CFG9_CARDCTL |= CARDCTL_SPICARD; REG_CFG9_CARDCTL |= CARDCTL_SPICARD;
if (type.infrared) { if (infrared) {
SPI_XferInfo irXfer = { &headerFooterVal, 1, false }; SPI_XferInfo irXfer = { &headerFooterVal, 1, false };
SPI_DoXfer(SPI_DEV_CART_IR, &irXfer, 1, false); SPI_DoXfer(SPI_DEV_CART_IR, &irXfer, 1, false);
// Wait as specified by GBATEK (0x800 cycles at 33 MHz)
ARM_WaitCycles(0x800 * 4);
} }
SPI_XferInfo transfers[3] = { SPI_XferInfo transfers[3] = {
@ -119,17 +118,22 @@ int CardSPIWriteRead(CardSPIType type, const void* cmd, u32 cmdSize, void* answe
SPI_DoXfer(SPI_DEV_CART_FLASH, transfers, 3, true); SPI_DoXfer(SPI_DEV_CART_FLASH, transfers, 3, true);
REG_CFG9_CARDCTL &= ~CARDCTL_SPICARD; REG_CFG9_CARDCTL &= ~CARDCTL_SPICARD;
if (infrared) {
// Wait as specified by GBATEK (0x800 cycles at 33 MHz)
ARM_WaitCycles(0x800 * 4);
}
return 0; return 0;
} }
int CardSPIWaitWriteEnd(CardSPIType type, u32 timeout) { int CardSPIWaitWriteEnd(bool infrared, u32 timeout) {
u8 cmd = SPI_CMD_RDSR, statusReg = 0; u8 cmd = SPI_CMD_RDSR, statusReg = 0;
int res = 0; int res = 0;
u64 time_start = timer_start(); u64 time_start = timer_start();
do { do {
res = CardSPIWriteRead(type, &cmd, 1, &statusReg, 1, 0, 0); res = CardSPIWriteRead(infrared, &cmd, 1, &statusReg, 1, 0, 0);
if (res) return res; if (res) return res;
if (timer_msec(time_start) > timeout) return 1; if (timer_msec(time_start) > timeout) return 1;
} while(statusReg & SPI_FLG_WIP); } while(statusReg & SPI_FLG_WIP);
@ -139,21 +143,21 @@ int CardSPIWaitWriteEnd(CardSPIType type, u32 timeout) {
int CardSPIEnableWriting_512B(CardSPIType type) { int CardSPIEnableWriting_512B(CardSPIType type) {
u8 cmd = SPI_CMD_WREN; u8 cmd = SPI_CMD_WREN;
return CardSPIWriteRead(type, &cmd, 1, NULL, 0, 0, 0); return CardSPIWriteRead(type.infrared, &cmd, 1, NULL, 0, 0, 0);
} }
int CardSPIEnableWriting_regular(CardSPIType type) { int CardSPIEnableWriting_regular(CardSPIType type) {
u8 cmd = SPI_CMD_WREN, statusReg = 0; u8 cmd = SPI_CMD_WREN, statusReg = 0;
int res = CardSPIWriteRead(type, &cmd, 1, NULL, 0, 0, 0); int res = CardSPIWriteRead(type.infrared, &cmd, 1, NULL, 0, 0, 0);
if (res) return res; if (res) return res;
cmd = SPI_CMD_RDSR; cmd = SPI_CMD_RDSR;
do { do {
res = CardSPIWriteRead(type, &cmd, 1, &statusReg, 1, 0, 0); res = CardSPIWriteRead(type.infrared, &cmd, 1, &statusReg, 1, 0, 0);
if (res) return res; if (res) return res;
} while(statusReg & ~SPI_FLG_WEL); } while(statusReg & ~SPI_FLG_WEL);
return 0; return 0;
} }
@ -165,28 +169,28 @@ int CardSPIEnableWriting(CardSPIType type) {
int _SPIWriteTransaction(CardSPIType type, void* cmd, u32 cmdSize, const void* data, u32 dataSize) { int _SPIWriteTransaction(CardSPIType type, void* cmd, u32 cmdSize, const void* data, u32 dataSize) {
int res; int res;
if ((res = CardSPIEnableWriting(type))) return res; if ((res = CardSPIEnableWriting(type))) return res;
if ((res = CardSPIWriteRead(type, cmd, cmdSize, NULL, 0, (void*) ((u8*) data), dataSize))) return res; if ((res = CardSPIWriteRead(type.infrared, cmd, cmdSize, NULL, 0, (void*) ((u8*) data), dataSize))) return res;
return CardSPIWaitWriteEnd(type, 1000); return CardSPIWaitWriteEnd(type.infrared, 10000);
} }
int CardSPIReadJEDECIDAndStatusReg(CardSPIType type, u32* id, u8* statusReg) { int CardSPIReadJEDECIDAndStatusReg(bool infrared, u32* id, u8* statusReg) {
u8 cmd = SPI_FLASH_CMD_RDID; u8 cmd = SPI_FLASH_CMD_RDID;
u8 reg = 0; u8 reg = 0;
u8 idbuf[3] = { 0 }; u8 idbuf[3] = { 0 };
u32 id_ = 0; u32 id_ = 0;
int res = CardSPIWaitWriteEnd(type, 0); int res = CardSPIWaitWriteEnd(infrared, 10);
if (res) return res; if (res) return res;
if ((res = CardSPIWriteRead(type, &cmd, 1, idbuf, 3, 0, 0))) return res; if ((res = CardSPIWriteRead(infrared, &cmd, 1, idbuf, 3, 0, 0))) return res;
id_ = (idbuf[0] << 16) | (idbuf[1] << 8) | idbuf[2]; id_ = (idbuf[0] << 16) | (idbuf[1] << 8) | idbuf[2];
cmd = SPI_CMD_RDSR; cmd = SPI_CMD_RDSR;
if ((res = CardSPIWriteRead(type, &cmd, 1, &reg, 1, 0, 0))) return res; if ((res = CardSPIWriteRead(infrared, &cmd, 1, &reg, 1, 0, 0))) return res;
if (id) *id = id_; if (id) *id = id_;
if (statusReg) *statusReg = reg; if (statusReg) *statusReg = reg;
return 0; return 0;
} }
@ -202,24 +206,36 @@ u32 CardSPIGetEraseSize(CardSPIType type) {
u32 CardSPIGetCapacity(CardSPIType type) { u32 CardSPIGetCapacity(CardSPIType type) {
if (type.chip == NO_CHIP) return 0; if (type.chip == NO_CHIP) return 0;
return type.chip->capacity; if (type.chip->capacity != 0) {
return type.chip->capacity;
} else {
u32 jedecid;
CardSPIReadJEDECIDAndStatusReg(type.infrared, &jedecid, NULL);
u8 size_exponent = jedecid & 0xff;
if ((size_exponent >= 8) && (size_exponent <= 24)) {
// Thanks to @wwylele for pointing this out
return 1 << size_exponent;
} else {
return 0;
}
}
} }
int CardSPIWriteSaveData_9bit(CardSPIType type, u32 offset, const void* data, u32 size) { int CardSPIWriteSaveData_9bit(CardSPIType type, u32 offset, const void* data, u32 size) {
u8 cmd[2] = { (offset >= 0x100) ? SPI_512B_EEPROM_CMD_WRHI : SPI_512B_EEPROM_CMD_WRLO, (u8) offset }; u8 cmd[2] = { (offset >= 0x100) ? SPI_512B_EEPROM_CMD_WRHI : SPI_512B_EEPROM_CMD_WRLO, (u8) offset };
return _SPIWriteTransaction(type, cmd, 2, (void*) ((u8*) data), size); return _SPIWriteTransaction(type, cmd, 2, (void*) ((u8*) data), size);
} }
int CardSPIWriteSaveData_16bit(CardSPIType type, u32 offset, const void* data, u32 size) { int CardSPIWriteSaveData_16bit(CardSPIType type, u32 offset, const void* data, u32 size) {
u8 cmd[3] = { type.chip->writeCommand, (u8)(offset >> 8), (u8) offset }; u8 cmd[3] = { type.chip->writeCommand, (u8)(offset >> 8), (u8) offset };
return _SPIWriteTransaction(type, cmd, 3, (void*) ((u8*) data), size); return _SPIWriteTransaction(type, cmd, 3, (void*) ((u8*) data), size);
} }
int CardSPIWriteSaveData_24bit_write(CardSPIType type, u32 offset, const void* data, u32 size) { int CardSPIWriteSaveData_24bit_write(CardSPIType type, u32 offset, const void* data, u32 size) {
u8 cmd[4] = { type.chip->writeCommand, (u8)(offset >> 16), (u8)(offset >> 8), (u8) offset }; u8 cmd[4] = { type.chip->writeCommand, (u8)(offset >> 16), (u8)(offset >> 8), (u8) offset };
return _SPIWriteTransaction(type, cmd, 4, (void*) ((u8*) data), size); return _SPIWriteTransaction(type, cmd, 4, (void*) ((u8*) data), size);
} }
@ -256,7 +272,7 @@ int CardSPIWriteSaveData_24bit_erase_program(CardSPIType type, u32 offset, const
if (!(res = _SPIWriteTransaction(type, cmd, 4, (void*) ((u8*) data - offset + pos), pageSize))) { if (!(res = _SPIWriteTransaction(type, cmd, 4, (void*) ((u8*) data - offset + pos), pageSize))) {
break; break;
} }
CardSPIWriteRead(type, "\x04", 1, NULL, 0, NULL, 0); CardSPIWriteRead(type.infrared, "\x04", 1, NULL, 0, NULL, 0);
} }
if(res) { if(res) {
free(newData); free(newData);
@ -270,84 +286,81 @@ int CardSPIWriteSaveData_24bit_erase_program(CardSPIType type, u32 offset, const
int CardSPIWriteSaveData(CardSPIType type, u32 offset, const void* data, u32 size) { int CardSPIWriteSaveData(CardSPIType type, u32 offset, const void* data, u32 size) {
if (type.chip == NO_CHIP) return 1; if (type.chip == NO_CHIP) return 1;
if (size == 0) return 0; if (size == 0) return 0;
size = min(size, CardSPIGetCapacity(type) - offset);
u32 end = offset + size; u32 end = offset + size;
u32 pos = offset; u32 pos = offset;
u32 writeSize = type.chip->writeSize; u32 writeSize = type.chip->writeSize;
if (writeSize == 0) return 0xC8E13404; if (writeSize == 0) return 0xC8E13404;
int res = CardSPIWaitWriteEnd(type, 1000); int res = CardSPIWaitWriteEnd(type.infrared, 1000);
if (res) return res; if (res) return res;
while(pos < end) { while(pos < end) {
u32 remaining = end - pos; u32 remaining = end - pos;
u32 nb = writeSize - (pos % writeSize); u32 nb = writeSize - (pos % writeSize);
u32 dataSize = (remaining < nb) ? remaining : nb; u32 dataSize = (remaining < nb) ? remaining : nb;
if ((res = type.chip->writeSaveData(type, pos, (void*) ((u8*) data - offset + pos), dataSize))) return res; if ((res = type.chip->writeSaveData(type, pos, (void*) ((u8*) data - offset + pos), dataSize))) return res;
pos = ((pos / writeSize) + 1) * writeSize; // truncate pos = ((pos / writeSize) + 1) * writeSize; // truncate
} }
return 0; return 0;
} }
int CardSPIReadSaveData_9bit(CardSPIType type, u32 pos, void* data, u32 size) { int CardSPIReadSaveData_9bit(CardSPIType type, u32 pos, void* data, u32 size) {
u8 cmd[4]; u8 cmd[4];
u32 cmdSize = 2; u32 cmdSize = 2;
u32 end = pos + size; u32 end = pos + size;
u32 read = 0; u32 read = 0;
if (pos < 0x100) { if (pos < 0x100) {
u32 len = 0x100 - pos; u32 len = min(0x100 - pos, size);
cmd[0] = SPI_512B_EEPROM_CMD_RDLO; cmd[0] = SPI_512B_EEPROM_CMD_RDLO;
cmd[1] = (u8) pos; cmd[1] = (u8) pos;
int res = CardSPIWriteRead(type, cmd, cmdSize, data, len, NULL, 0); int res = CardSPIWriteRead(type.infrared, cmd, cmdSize, data, len, NULL, 0);
if (res) return res; if (res) return res;
read += len; read += len;
} }
if (end >= 0x100) { if (end >= 0x100) {
u32 len = end - 0x100; u32 len = min(end - 0x100, size);
cmd[0] = SPI_512B_EEPROM_CMD_RDHI; cmd[0] = SPI_512B_EEPROM_CMD_RDHI;
cmd[1] = (u8)(pos + read); cmd[1] = (u8)(pos + read);
int res = CardSPIWriteRead(type, cmd, cmdSize, (void*)((u8*)data + read), len, NULL, 0); int res = CardSPIWriteRead(type.infrared, cmd, cmdSize, (void*)((u8*)data + read), len, NULL, 0);
if (res) return res; if (res) return res;
} }
return 0; return 0;
} }
int CardSPIReadSaveData_16bit(CardSPIType type, u32 offset, void* data, u32 size) { int CardSPIReadSaveData_16bit(CardSPIType type, u32 offset, void* data, u32 size) {
u8 cmd[3] = { SPI_CMD_READ, (u8)(offset >> 8), (u8) offset }; u8 cmd[3] = { SPI_CMD_READ, (u8)(offset >> 8), (u8) offset };
return CardSPIWriteRead(type, cmd, 3, data, size, NULL, 0); return CardSPIWriteRead(type.infrared, cmd, 3, data, size, NULL, 0);
} }
int CardSPIReadSaveData_24bit(CardSPIType type, u32 offset, void* data, u32 size) { int CardSPIReadSaveData_24bit(CardSPIType type, u32 offset, void* data, u32 size) {
u8 cmd[4] = { SPI_CMD_READ, (u8)(offset >> 16), (u8)(offset >> 8), (u8) offset }; u8 cmd[4] = { SPI_CMD_READ, (u8)(offset >> 16), (u8)(offset >> 8), (u8) offset };
return CardSPIWriteRead(type, cmd, 4, data, size, NULL, 0); return CardSPIWriteRead(type.infrared, cmd, 4, data, size, NULL, 0);
} }
int CardSPIReadSaveData(CardSPIType type, u32 offset, void* data, u32 size) { int CardSPIReadSaveData(CardSPIType type, u32 offset, void* data, u32 size) {
if (type.chip == NO_CHIP) return 1; if (type.chip == NO_CHIP) return 1;
if (size == 0) return 0; if (size == 0) return 0;
int res = CardSPIWaitWriteEnd(type, 1000); int res = CardSPIWaitWriteEnd(type.infrared, 1000);
if (res) return res; if (res) return res;
size = (size <= CardSPIGetCapacity(type) - offset) ? size : CardSPIGetCapacity(type) - offset;
return type.chip->readSaveData(type, offset, data, size); return type.chip->readSaveData(type, offset, data, size);
} }
@ -358,7 +371,7 @@ int CardSPIEraseSector_emulated(CardSPIType type, u32 offset) {
if (!fill_buf) return 1; if (!fill_buf) return 1;
memset(fill_buf, 0xff, blockSize); memset(fill_buf, 0xff, blockSize);
offset = (offset / blockSize) * blockSize; offset = (offset / blockSize) * blockSize;
int res = CardSPIWriteSaveData(type, offset, fill_buf, blockSize); int res = CardSPIWriteSaveData(type, offset, fill_buf, blockSize);
free(fill_buf); free(fill_buf);
return res; return res;
@ -366,10 +379,10 @@ int CardSPIEraseSector_emulated(CardSPIType type, u32 offset) {
int CardSPIEraseSector_real(CardSPIType type, u32 offset) { int CardSPIEraseSector_real(CardSPIType type, u32 offset) {
u8 cmd[4] = { type.chip->eraseCommand, (u8)(offset >> 16), (u8)(offset >> 8), (u8) offset }; u8 cmd[4] = { type.chip->eraseCommand, (u8)(offset >> 16), (u8)(offset >> 8), (u8) offset };
int res = CardSPIWaitWriteEnd(type, 10000); int res = CardSPIWaitWriteEnd(type.infrared, 10000);
if (res) return res; if (res) return res;
return _SPIWriteTransaction(type, cmd, 4, NULL, 0); return _SPIWriteTransaction(type, cmd, 4, NULL, 0);
} }
@ -399,92 +412,81 @@ int CardSPIErase(CardSPIType type) {
* *
* Copyright (C) Pokedoc (2010) * Copyright (C) Pokedoc (2010)
*/ */
/* /*
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, but * This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details. * for more details.
* *
* You should have received a copy of the GNU General Public License along * You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., * with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
int _SPIIsDataMirrored(CardSPIType type, int size, bool* mirrored) { int _SPIIsDataMirrored(CardSPIType type, int size, bool* mirrored) {
u32 offset0 = (size-1); // n KB u32 offset0 = (size-1); // n KB
u32 offset1 = (2*size-1); // 2n KB u32 offset1 = (2*size-1); // 2n KB
u8 buf1; // +0k data read -> write u8 buf1; // +0k data read -> write
u8 buf2; // +n k data read -> read u8 buf2; // +n k data read -> read
u8 buf3; // +0k ~data write u8 buf3; // +0k ~data write
u8 buf4; // +n k data new comp buf2 u8 buf4; // +n k data new comp buf2
int res; int res;
if ((res = CardSPIReadSaveData(type, offset0, &buf1, 1))) return res; if ((res = CardSPIReadSaveData(type, offset0, &buf1, 1))) return res;
if ((res = CardSPIReadSaveData(type, offset1, &buf2, 1))) return res; if ((res = CardSPIReadSaveData(type, offset1, &buf2, 1))) return res;
buf3=~buf1; buf3=~buf1;
if ((res = CardSPIWriteSaveData(type, offset0, &buf3, 1))) return res; if ((res = CardSPIWriteSaveData(type, offset0, &buf3, 1))) return res;
if ((res = CardSPIReadSaveData(type, offset1, &buf4, 1))) return res; if ((res = CardSPIReadSaveData(type, offset1, &buf4, 1))) return res;
if ((res = CardSPIWriteSaveData(type, offset0, &buf1, 1))) return res; if ((res = CardSPIWriteSaveData(type, offset0, &buf1, 1))) return res;
*mirrored = buf2 != buf4; *mirrored = buf2 != buf4;
return 0; return 0;
} }
int CardSPIGetCardSPIType(CardSPIType* type, bool infrared) { CardSPIType CardSPIGetCardSPIType(bool infrared) {
u8 sr = 0; u8 sr = 0;
u32 jedec = 0; u32 jedec = 0;
CardSPIType t = {NO_CHIP, infrared}; CardSPIType t = {NO_CHIP, infrared};
int res; int res;
if(infrared) { res = CardSPIReadJEDECIDAndStatusReg(infrared, &jedec, &sr);
// Infrared carts currently not supported, need additional handling! if (res) return (CardSPIType) {NO_CHIP, false};
*type = (CardSPIType) {NO_CHIP, true};
return 0; if ((sr & 0xfd) == 0x00 && (jedec != 0x00ffffff)) { t.chip = &FLASH_DUMMY; }
if ((sr & 0xfd) == 0xF0 && (jedec == 0x00ffffff)) { return (CardSPIType) { &EEPROM_512B, false }; }
if ((sr & 0xfd) == 0x00 && (jedec == 0x00ffffff)) { t = (CardSPIType) { &EEPROM_DUMMY, false }; }
if(t.chip == NO_CHIP) {
return (CardSPIType) {NO_CHIP, false};
} }
res = CardSPIReadJEDECIDAndStatusReg(t, &jedec, &sr);
if (res) return res;
if ((sr & 0xfd) == 0x00 && (jedec != 0x00ffffff)) { t.chip = &FLASH_DUMMY; }
if ((sr & 0xfd) == 0xF0 && (jedec == 0x00ffffff)) { *type = (CardSPIType) { EEPROM_512B, false }; return 0; }
if ((sr & 0xfd) == 0x00 && (jedec == 0x00ffffff)) { t = (CardSPIType) { &EEPROM_DUMMY, false }; }
if(t.chip == NO_CHIP) {
*type = (CardSPIType) {NO_CHIP, false};
return 0;
}
if (t.chip == &EEPROM_DUMMY) { if (t.chip == &EEPROM_DUMMY) {
bool mirrored = false; bool mirrored = false;
size_t i; size_t i;
for(i = 0; i < sizeof(EEPROMTypes) / sizeof(CardSPITypeData) - 1; i++) { for(i = 0; i < sizeof(EEPROMTypes) / sizeof(CardSPITypeData) - 1; i++) {
if ((res = _SPIIsDataMirrored(t, CardSPIGetCapacity((CardSPIType) {EEPROMTypes + i, false}), &mirrored))) return res; if ((res = _SPIIsDataMirrored(t, CardSPIGetCapacity((CardSPIType) {EEPROMTypes + i, false}), &mirrored))) return (CardSPIType) {NO_CHIP, false};
if (mirrored) { if (mirrored) {
*type = (CardSPIType) {EEPROMTypes + i, false}; return (CardSPIType) {EEPROMTypes + i, false};
return 0;
} }
} }
*type = (CardSPIType) { EEPROMTypes + i, false }; return (CardSPIType) { EEPROMTypes + i, false };
return 0;
} }
for(size_t i = 0; i < sizeof(flashTypes) / sizeof(CardSPITypeData); i++) { for(size_t i = 0; i < sizeof(flashTypes) / sizeof(CardSPITypeData); i++) {
if (flashTypes[i].jedecId == jedec) { if (flashTypes[i].jedecId == (jedec >> 8)) {
*type = (CardSPIType) { flashTypes + i, infrared }; return (CardSPIType) { flashTypes + i, infrared };
return 0;
} }
} }
*type = (CardSPIType) { NO_CHIP, infrared }; return (CardSPIType) { NO_CHIP, infrared };
return 0;
} }

View File

@ -1,7 +1,7 @@
/* /*
* This file is based on SPI.h from TWLSaveTool. Its copyright notice is * This file is based on SPI.h from TWLSaveTool. Its copyright notice is
* reproduced below. * reproduced below.
* *
* Copyright (C) 2015-2016 TuxSH * Copyright (C) 2015-2016 TuxSH
* *
* TWLSaveTool is free software: you can redistribute it and/or modify * TWLSaveTool is free software: you can redistribute it and/or modify
@ -37,7 +37,7 @@ struct CardSPITypeData {
int (*readSaveData) (CardSPIType type, u32 offset, void* data, u32 size); int (*readSaveData) (CardSPIType type, u32 offset, void* data, u32 size);
int (*writeSaveData) (CardSPIType type, u32 offset, const void* data, u32 size); int (*writeSaveData) (CardSPIType type, u32 offset, const void* data, u32 size);
int (*eraseSector) (CardSPIType type, u32 offset); int (*eraseSector) (CardSPIType type, u32 offset);
u32 jedecId; u16 jedecId;
u32 capacity; u32 capacity;
u32 eraseSize; u32 eraseSize;
u32 pageSize; u32 pageSize;
@ -49,28 +49,13 @@ struct CardSPITypeData {
#define NO_CHIP NULL #define NO_CHIP NULL
extern const CardSPITypeData * const EEPROM_512B; extern const CardSPITypeData * const FLASH_CTR_GENERIC; // Handles each 3ds cartridge the exact same
extern const CardSPITypeData * const EEPROM_8KB; int CardSPIWriteRead(bool infrared, const void* cmd, u32 cmdSize, void* answer, u32 answerSize, const void* data, u32 dataSize);
extern const CardSPITypeData * const EEPROM_64KB; int CardSPIWaitWriteEnd(bool infrared, u32 timeout);
extern const CardSPITypeData * const EEPROM_128KB;
extern const CardSPITypeData * const FLASH_256KB_1;
extern const CardSPITypeData * const FLASH_256KB_2;
extern const CardSPITypeData * const FLASH_512KB_1;
extern const CardSPITypeData * const FLASH_512KB_2;
extern const CardSPITypeData * const FLASH_1MB;
extern const CardSPITypeData * const FLASH_8MB;
extern const CardSPITypeData * const FLASH_128KB_CTR; // Most common, including Ocarina of time 3D
extern const CardSPITypeData * const FLASH_512KB_CTR; // Also common, including Detective Pikachu
extern const CardSPITypeData * const FLASH_1MB_CTR; // For example Pokemon Ultra Sun
int CardSPIWriteRead(CardSPIType type, const void* cmd, u32 cmdSize, void* answer, u32 answerSize, const void* data, u32 dataSize);
int CardSPIWaitWriteEnd(CardSPIType type, u32 timeout);
int CardSPIEnableWriting(CardSPIType type); int CardSPIEnableWriting(CardSPIType type);
int CardSPIReadJEDECIDAndStatusReg(CardSPIType type, u32* id, u8* statusReg); int CardSPIReadJEDECIDAndStatusReg(bool infrared, u32* id, u8* statusReg);
int CardSPIGetCardSPIType(CardSPIType* type, bool infrared); CardSPIType CardSPIGetCardSPIType(bool infrared);
u32 CardSPIGetPageSize(CardSPIType type); u32 CardSPIGetPageSize(CardSPIType type);
u32 CardSPIGetCapacity(CardSPIType type); u32 CardSPIGetCapacity(CardSPIType type);
u32 CardSPIGetEraseSize(CardSPIType type); u32 CardSPIGetEraseSize(CardSPIType type);

View File

@ -27,7 +27,7 @@ void CTR_CmdReadData(u32 sector, u32 length, u32 blocks, void* buffer)
(u32)((sector << 9) & 0xFFFFFFFF), (u32)((sector << 9) & 0xFFFFFFFF),
0x00000000, 0x00000000 0x00000000, 0x00000000
}; };
CTR_SendCommand(read_cmd, length, blocks, 0x704822C, buffer); CTR_SendCommand(read_cmd, length, blocks, 0x104822C, buffer); // Clock divider 5 (13.4 MHz). Same as Process9.
} }
void CTR_CmdReadHeader(void* buffer) void CTR_CmdReadHeader(void* buffer)

View File

@ -42,9 +42,9 @@ void NTR_CmdReadHeader (u8* buffer)
while(REG_NTRCARDROMCNT&NTRCARD_BUSY) ; while(REG_NTRCARDROMCNT&NTRCARD_BUSY) ;
u32 iCardId=cardReadID(NTRCARD_CLK_SLOW); u32 iCardId=cardReadID(NTRCARD_CLK_SLOW);
while(REG_NTRCARDROMCNT&NTRCARD_BUSY) ; while(REG_NTRCARDROMCNT&NTRCARD_BUSY) ;
u32 iCheapCard=iCardId&0x80000000; u32 iCheapCard=iCardId&0x80000000;
if(iCheapCard) if(iCheapCard)
{ {
//this is magic of wood goblins //this is magic of wood goblins

View File

@ -15,5 +15,5 @@ void NTR_CmdEnter16ByteMode(void);
void NTR_CmdReadHeader (u8* buffer); void NTR_CmdReadHeader (u8* buffer);
void NTR_CmdReadData (u32 offset, void* buffer); void NTR_CmdReadData (u32 offset, void* buffer);
bool NTR_Secure_Init (u8* buffer, u32 CartID, int iCardDevice); bool NTR_Secure_Init (u8* buffer, u8* sa_copy, u32 CartID, int iCardDevice);

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