Fix n3ds firm patching

This commit is contained in:
ahezard 2016-08-16 23:02:13 +02:00
parent ab5c38969e
commit 34b24195e9
7 changed files with 161 additions and 1 deletions

7
go_n3ds.cmd Normal file
View File

@ -0,0 +1,7 @@
mkdir temp\
cp input\firmware_twl.bin temp\
cd scripts
patch_dev_launcher_n3ds.cmd

7
go_o3ds.cmd Normal file
View File

@ -0,0 +1,7 @@
mkdir temp\
cp input\firmware_twl.bin temp\
cd scripts
patch_dev_launcher_o3ds.cmd

View File

@ -0,0 +1,112 @@
# -*- coding: utf8 -*-
# Patch an .nds (works with homebrew and ds demo only) to make it ready for make_cia
#
# 2016-02-28, Ahezard
#
# inspired by
# Apache Thunder .nds edited files and comments
# https://github.com/Relys/Project_CTR/blob/master/makerom/srl.h
# https://dsibrew.org/wiki/DSi_Cartridge_Header
# if the header size of the input nds file is 0x200 (homebrew)
# the header size of the output nds file will be patched to 0x4000 (normal ds/dsi header), 0x3E00 offset
from struct import *
from collections import namedtuple
from collections import OrderedDict
from pprint import pprint
import os, sys
import binascii
import argparse
parser = argparse.ArgumentParser(description='extract devLauncherSrl from the twl_bg.cxi')
parser.add_argument('--cxi', help='cxi file')
parser.add_argument('--out', help='output cxi file')
args = parser.parse_args()
#
# CRC16 MODULE
#
# includes CRC16 and CRC16 MODBUS
#
from ctypes import c_ushort
# from https://github.com/cristianav/PyCRC/blob/master/demo.py
class CRC16(object):
crc16_tab = []
# The CRC's are computed using polynomials. Here is the most used
# coefficient for CRC16
crc16_constant = 0xA001 # 40961
def __init__(self, modbus_flag=False):
# initialize the precalculated tables
if not len(self.crc16_tab):
self.init_crc16()
self.mdflag = bool(modbus_flag)
def calculate(self, input_data=None):
try:
is_string = isinstance(input_data, str)
is_bytes = isinstance(input_data, (bytes, bytearray))
if not is_string and not is_bytes:
raise Exception("Please provide a string or a byte sequence "
"as argument for calculation.")
crc_value = 0x0000 if not self.mdflag else 0xffff
for c in input_data:
d = ord(c) if is_string else c
tmp = crc_value ^ d
rotated = crc_value >> 8
crc_value = rotated ^ self.crc16_tab[(tmp & 0x00ff)]
return crc_value
except Exception as e:
print("EXCEPTION(calculate): {}".format(e))
def init_crc16(self):
"""The algorithm uses tables with precalculated values"""
for i in range(0, 256):
crc = c_ushort(i).value
for j in range(0, 8):
if crc & 0x0001:
crc = c_ushort(crc >> 1).value ^ self.crc16_constant
else:
crc = c_ushort(crc >> 1).value
self.crc16_tab.append(crc)
def getSize(fileobject):
current = fileobject.tell()
fileobject.seek(0,2) # move the cursor to the end of the file
size = fileobject.tell()
fileobject.seek(current,0)
return size
def skipUntilAddress(f_in,f_out, caddr, taddr):
chunk = f_in.read(taddr-caddr)
f_out.write(chunk)
def writeBlankuntilAddress(f_out, caddr, taddr):
f_out.write("\x00"*(taddr-caddr))
cxi_fname=args.cxi
srlsizeoffset=0xCDE88
srlsize=0xB9400
# write the file
filer = open(cxi_fname, 'rb')
filew = open(args.out, "wb")
filesize=getSize(filer)
filer.read(srlsizeoffset);
skipUntilAddress(filer,filew,srlsizeoffset,srlsize+srlsizeoffset)
filew.close()
filer.close()

View File

@ -9,7 +9,7 @@ pause
..\tools\3dstool.exe -xuvtf exefs twlBg.exefs --header twlBg.exefs.header --exefs-dir twlBg_original_exefs
python ..\scripts\extract_dev_launcher_srl_from_twlbg_cxi.py --cxi twlBg_original_exefs/code.bin --out devSRLlauncher_original_enc.nds
python ..\scripts\extract_dev_launcher_srl_from_twlbg_cxi_n3ds.py --cxi twlBg_original_exefs/code.bin --out devSRLlauncher_original_enc.nds
..\tools\twltool.exe modcrypt --in devSRLlauncher_original_enc.nds --out devSRLlauncher_original_dec.nds

View File

@ -0,0 +1,34 @@
@@Echo off
cd ../temp
..\tools\ctrtool.exe -xt firm --firmdir firm firmware_twl.bin
cp firm/firm_0_18000000.bin twlBg_original.cxi
pause
..\tools\3dstool.exe -xvtf cxi twlBg_original.cxi --header twlBg.ncch.header --exh twlBg.exheader.bin --exefs twlBg.exefs
..\tools\3dstool.exe -xuvtf exefs twlBg.exefs --header twlBg.exefs.header --exefs-dir twlBg_original_exefs
python ..\scripts\extract_dev_launcher_srl_from_twlbg_cxi_o3ds.py --cxi twlBg_original_exefs/code.bin --out devSRLlauncher_original_enc.nds
..\tools\twltool.exe modcrypt --in devSRLlauncher_original_enc.nds --out devSRLlauncher_original_dec.nds
..\tools\ndstool.exe -i devSRLlauncher_original_dec.nds > devSRLlauncher_original_header.txt
..\tools\ndstool.exe -x -9 devSRLlauncher_original_dec.arm9 -7 devSRLlauncher_original_dec.arm7 devSRLlauncher_original_dec.nds
python ..\scripts\patchDevLauncher.py --srl devSRLlauncher_original_dec.nds --out devSRLlauncher_patched_dec.nds
copy devSRLlauncher_patched_dec.nds devSRLlauncher_patched_enc.nds
..\tools\TWLTool.exe modcrypt --in %1 devSRLlauncher_patched_enc.nds
mkdir twlBg_patched_exefs
python ..\scripts\build_twlbg_code_from_dev_launcher_srl.py --srl=devSRLlauncher_patched_enc.nds --code=twlBg_original_exefs\code.bin --out=twlBg_patched_exefs\code.bin
..\tools\3dstool.exe -czvtf exefs twlBg_patched.exefs --header twlBg.exefs.header --exefs-dir twlBg_patched_exefs
..\tools\3dstool.exe -cvtf cxi ..\twlBg.cxi --header twlBg.ncch.header --exh twlBg.exheader.bin --exefs twlBg_patched.exefs
cd ..\scripts
pause