You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.2 KiB

#!/usr/bin/env python3
"""
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
"""
from argparse import ArgumentParser, FileType
from io import SEEK_END
from struct import unpack
BANNER_SIZES = {
0x0001: 0x840,
0x0002: 0x940,
0x0003: 0xA40,
0x0103: 0x23C0
}
def extractBanner(backup, banner):
# NDS file starts at 0x2000, banner offset is 0x68 within that
backup.seek(0x2068)
bannerOfs, = unpack("<I", backup.read(4))
if bannerOfs < 0x8000:
print("Error: File does not have banner")
return False
# Read banner version, to know the size of it
backup.seek(0x2000 + bannerOfs)
bannerVer, = unpack("<H", backup.read(2))
backup.seek(0x2000 + bannerOfs)
# Extract banner
banner.write(backup.read(BANNER_SIZES[bannerVer]))
return True
def injectBanner(backup, banner):
# NDS file starts at 0x2000, banner offset is 0x68 within that
backup.seek(0x2068)
bannerOfs, = unpack("<I", backup.read(4))
if bannerOfs < 0x8000:
print("Error: File does not have banner")
return False
# Read existing banner version
backup.seek(0x2000 + bannerOfs)
bannerVer, = unpack("<H", backup.read(2))
backup.seek(0x2000 + bannerOfs)
# Check that new banner size matches
banner.seek(0, SEEK_END)
if banner.tell() != BANNER_SIZES[bannerVer]:
print("Error: New banner version does not match old one")
return False
banner.seek(0)
# Inject banner
backup.write(banner.read())
return True
if __name__ == "__main__":
parser = ArgumentParser(description="Extracts/Injects a banner from/to an ntrboot backup")
parser.add_argument("-x", "--extract", metavar="banner.bin", type=FileType("wb"), help="banner file to extract")
parser.add_argument("-i", "--inject", metavar="banner.bin", type=FileType("rb"), help="banner file to inject")
parser.add_argument("backup", metavar="backup.bin", type=FileType("rb+"), help="flashcard backup to extract/inject from/to")
args = parser.parse_args()
if args.extract and not args.inject:
extractBanner(args.backup, args.extract)
elif args.inject and not args.extract:
injectBanner(args.backup, args.inject)
else:
print("Error: You must do *one* of -x or -i")