[Port/Release] 3DSFAT16Tool - C Rewrite by d0k3

Discussion in '3DS - Homebrew Development and Emulators' started by d0k3, Jun 16, 2015.

  1. d0k3
    OP

    d0k3 3DS Homebrew Legend

    Member
    2,558
    2,571
    Dec 3, 2004
    Gambia, The
    This is just a C rewrite of 3DSFAT16Tool, which was orginally written by @cearp and has been improved upon by @swarzesherz. You may use this to dump and decrypt / inject and encrypt the FAT16 partition from your NAND backup. Other than the original script, this doesn't need Python and additional libraries installed on your PC. It also has a pretty low memory requirement and might be faster than the original one. Dumping and decrypting the CTRNAND FAT16 partition from the N3DS NAND.bin for me takes less than 10 seconds.

    Usage:
    Code:
    3DSFAT16tool [-d|-i] [NAND] [FAT16] [XORPAD]
    -d   Dump FAT16 from NAND file
    -i   Inject FAT16 to NAND file
    
    NAND   -> your NAND backup, dump via Decrypt9, GW launcher.dat or rxTools
    FAT16  -> the FAT16 image, must exist for injecting, will be created/overwritten for dumping
    XORPAD -> NAND XORPAD, generate using Decrypt9 or rxTools
    
    Example:
    3DSFAT16tool -d NAND.bin NAND.fat16.bin NAND.fat16.xorpad
    
    It will automagically recognize xorpad type by xorpad size. It will process O3DS / N3DS CTRNAND and TWLN xorpads. TWLN xorpads can be used to set up SudokuHax. All types of xorpads can be generated via my work-in-progress fork of Decrypt9. Always generate the xorpads on the same 3DS the NAND backup is from.

    The FAT16 image can be viewed and edited via WinImage or OSFmount (the better alternative) on Windows. On other OSes just changing the extension to .ISO and mounting it may be enough.

    Source code and Windows binary ('EXE') is included and the download is attached. You may also download the release and participate on the development on Github.


    Update: For all command line illiterates among you, @x7l7j8cc has made a GUI for this. Grab it from here. Also, @Apache Thunder has made a simple batch script to streamline the process of dumping / editing / injecting and released it in the post below.
     

    Attached Files:

    Last edited by d0k3, Dec 28, 2015
    hausa51, ihaveamac, klear and 5 others like this.


  2. Apache Thunder

    Apache Thunder I have cameras in your head!

    Member
    4,088
    3,987
    Oct 7, 2007
    United States
    Levelland, Texas
    I can finally stop switching to/from Python 64bit now. The old tool only worked with Python 64 bit and I have other things that don't get along with it. :P


    EDIT:

    I made a simple batch script that you can use for quick edits and stuff. Adjust the command switches if you are using a n3DS. I have a o3DS, so that's what I tailored it for:

    Code:
    @Echo off
    if not exist 3DSFat16tool.exe Echo 3DSFat16tool.exe not found!
    if not exist 3DSFat16tool.exe Echo Are you using this script from the correct location?
    if not exist 3DSFat16tool.exe goto error
    if not exist nand.bin Echo nand.bin not found! Did you extract it to the correct location?
    if not exist nand.bin goto error
    if not exist fat16.xorpad fat16.xorpad not found! Did you generate a xorpad?
    if not exist fat16.xorpad It's required for decryption!
    if not exist fat16.xorpad goto error
    if exist nand.img Echo nand.img already exists! Continuing this script will delete it!
    if exist nand.img pause
    if exist nand.img del nand.img
    cls
    Echo Ready to extract CTR_NAND Partition? Ctrl-C to cancel.
    pause
    cls
    Echo Extracting and decrypting CTR_NAND...
    3DSFAT16tool -d -o nand.bin nand.img fat16.xorpad
    if not exist nand.img Echo Error! FAT16 file not found. Extraction failed?!
    if not exist nand.img goto error
    Echo Make your edits to nand.img now. Then press any key to re-encrypt once done.
    pause
    cls
    Echo Re-encrypting and injecting CTR_NAND....
    if not exist nand.img Echo FAT16 file not found! Did you delete it by accident?
    if not exist nand.img goto error
    if not exist nand.bin Echo nand.bin not found! Did you extract it to the correct location?
    if not exist nand.bin goto error
    if not exist fat16.xorpad fat16.xorpad not found! Did you generate a xorpad?
    if not exist fat16.xorpad It's required for decryption!
    if not exist fat16.xorpad goto error
    3DSFAT16tool -i -o nand.bin nand.img fat16.xorpad
    goto exit
    :error
    Echo Check your files and try again!
    pause
    exit
    :exit
    Echo Done! You may now inject this NAND to Emunand/Sysnand!
    Echo If you want to keep decrypted nand.img close this script now.
    Echo Otherwise pressing any key now will delete it.
    pause
    if exist nand.img del nand.img
    exit
    
    Copy the above and paste it into a new file and give it a .bat or .cmd file extension when you save it. :D

    This script assumes your nand is nand.bin and will create a decrypted file called nand.img. You edit it, then return to the script (it pauses to let you do things to the file) to continue. It will then re-encrypt/inject in one go thanks to the new program and then delete nand.img when done. (it will warn you before doing this so you have a chance to back it up if you want to keep it for whatever reason)
     
    Last edited by Apache Thunder, Jun 16, 2015
    d0k3 likes this.
  3. Slushie3DS

    Slushie3DS Cold Beverage Lover

    Member
    707
    294
    Jan 9, 2015
    United States
    Good work!
     
  4. d0k3
    OP

    d0k3 3DS Homebrew Legend

    Member
    2,558
    2,571
    Dec 3, 2004
    Gambia, The
    BTW, I've never used the Python version. Didn't get it to work. Is the C rewrite faster (meaning less time needed for dump/decrypt) for you?
     
  5. Apache Thunder

    Apache Thunder I have cameras in your head!

    Member
    4,088
    3,987
    Oct 7, 2007
    United States
    Levelland, Texas
    The speed of the dumping process isn't much faster. However the inject/reencryption process is much faster then the older python script. (and the python script wasn't doing any encryption. It was just putting the file back into the nand image)

    I'd imagine the injection process might be a bit slower if I made more substantial changes that makes the encryption part take longer. But otherwise it runs better then the python script.
     
    Last edited by Apache Thunder, Jun 16, 2015
  6. d0k3
    OP

    d0k3 3DS Homebrew Legend

    Member
    2,558
    2,571
    Dec 3, 2004
    Gambia, The
    The speed of the encryption/decryption should not change, regardless of what is in there. I can't say for 100% sure, though.
     
  7. Apache Thunder

    Apache Thunder I have cameras in your head!

    Member
    4,088
    3,987
    Oct 7, 2007
    United States
    Levelland, Texas
    Padxorer will take longer if I made any significant structural changes to the folder layout. That's what it seems like anyways.
     
  8. d0k3
    OP

    d0k3 3DS Homebrew Legend

    Member
    2,558
    2,571
    Dec 3, 2004
    Gambia, The
    Also, be sure to have a backup of your EmuNAND ready if you try injection. Keep in mind that this tool is pretty fresh. Bugs (at least the worst sort) are unlikely, though, as it is pretty simple code. The speed improvement might be from the fact that this encrypts in memory and writes directly to the NAND.
     
  9. Jaitsu

    Jaitsu Yet Another GBATemp Furry

    Member
    152
    51
    Jun 2, 2015
    United States
    So, I'm curious. What if one were to take the NAND from 3DS A, decrypt it with 3DS A's xorpad, re-encrypt it with 3DS B's xorpad, and inject it into 3DS B's NAND? Does anyone know if that would work? I'm incredibly curious about what it is that prevents one 3DS' NAND from running on another 3DS.
     
  10. d0k3
    OP

    d0k3 3DS Homebrew Legend

    Member
    2,558
    2,571
    Dec 3, 2004
    Gambia, The
    You need to keep in mind that this tool only handles one (although big) part of the NAND (the CTR FAT16 partition), while other parts are not handled at all. See here for more info:
    http://3dbrew.org/wiki/Flash_Filesystem

    Encrypting with a different XORpad will work of course, but there might be stuff in the actual (unencrypted) files that would prevent it working on the second 3DS.

    I'm pretty sure someone else can give you a better explanation, though.
     
  11. Jaitsu

    Jaitsu Yet Another GBATemp Furry

    Member
    152
    51
    Jun 2, 2015
    United States
    I see. Looking through that article, there it is: "Note that re-encrypting a NAND image alone from another 3DS for use on a different 3DS is not enough to use that NAND image on a different 3DS: certain files in the "nand" partition would need modified/replaced as well." I'm quite curious, since knowing what DOES need to be replaced/modified, and how, might open the door for downgrading systems (probably via hard-mod) without a pre-9.2 NAND backup. But maybe I'm going off-topic. :P
     
  12. d0k3
    OP

    d0k3 3DS Homebrew Legend

    Member
    2,558
    2,571
    Dec 3, 2004
    Gambia, The
    Nope, that is in fact interesting and on-topic. However, as I said before, the NAND contains other stuff besides the FAT16 partition, other partitions, too. The Firmware partition, f.e. might be interesting for that. If you would want to perform a downgrade via NAND flash, all these partitions would need to be reencrypted (besides, you have to think about those "certain" files). To my knowledge, we cannot decrypt / encrypt all of these partitions yet, though. rxTools, for example, doesn't handle the TWL NAND partition.
     
  13. hippy dave

    hippy dave Butts Butts Megabutts

    Member
    2,569
    1,791
    Apr 30, 2012
    You wouldn't be able to re-encrypt it for the destination console if the destination console wasn't exploitable.
     
  14. Jaitsu

    Jaitsu Yet Another GBATemp Furry

    Member
    152
    51
    Jun 2, 2015
    United States
    ...hm. True, you'd have to get the destination xorpads (and whatever other info ties the NAND to the specific system) somehow. It could still have some other uses, but that does complicate things. Even on 9.2, though, I hear downgrading with the Gateway.dg method can cause some issues, so... I don't know. I need to do some reading up on how to code for/exploit this thing. And I need to get off my lazy arse and do a hard-mod so I can do more than speculate. :D
     
  15. x7l7j8cc

    x7l7j8cc Member

    Newcomer
    34
    40
    Jan 26, 2014
    Italy
    Italy
    If someone is interested, ive done a GUI for this tool.
    You can Grab the Latest version, HERE.

    This already include the 2 files from d0k3.
    And obviously, the credits for the tools go to him, ive only done the GUI for it.

    Hope you like it.
    if you find any bugs, please tell me, ok?
     
    Last edited by x7l7j8cc, Jun 19, 2015
    d0k3 likes this.
  16. d0k3
    OP

    d0k3 3DS Homebrew Legend

    Member
    2,558
    2,571
    Dec 3, 2004
    Gambia, The
    Nice! I'll link to this from the start post, but I have a few remarks first (two actually).:
    • "New NAND Name" is a bit misleading. FAT 16 Image, FAT16 Partition Image or even FAT16 CTR Partition Image (that would be the 100% correct name ;)) is better. Reason: The NAND actually contains a lot more stuff than just the FAT16 partition my tool handles. The extracted file therefore is not a new NAND but rather just a part of that.
    • Text on buttons, "Browse", "Browse" and "Save": For dumping, the text is correct - NAND.bin and XORPad only get read, and the FAT16 image file gets written to. For injecting, FAT16 and XORPad get read and the NAND.bin is written to.
    Other than that, great work! I like that it also includes a progress bar.
     
    Melon__Bread likes this.
  17. Melon__Bread

    Melon__Bread ~It's A Magik~

    Member
    709
    399
    Dec 9, 2013
    United States
    Compiled on Linux and works great.
    (Had to compile using a C++ compiler over just a C compiler)
     
  18. x7l7j8cc

    x7l7j8cc Member

    Newcomer
    34
    40
    Jan 26, 2014
    Italy
    Italy
    I just made the changes you suggested.
    In fact, I had forgotten to put the change to the choice between inject/dump.

    You can Grab the Latest version, HERE.
    if you find any bugs, please tell me, ok?

    Anyway, thanks for the compliments @d0k3 . ;)
     
    Last edited by x7l7j8cc, Jun 19, 2015
    d0k3 likes this.
  19. d0k3
    OP

    d0k3 3DS Homebrew Legend

    Member
    2,558
    2,571
    Dec 3, 2004
    Gambia, The
    I was pretty sure there's no CPP stuff in there... Can you tell me what error messages you get from the C compiler?
     
  20. Wuigi

    Wuigi GBAtemp Regular

    Member
    297
    100
    Sep 14, 2012
    United States
    To compile using gcc from MinGW I had to set the standard to gnu99 and include stdbool.h, works great on Windows so far.