From ae4a677bd40c8f02f3c7ce328088d0470d21b40d Mon Sep 17 00:00:00 2001 From: yoshida_teruhisa Date: Tue, 14 Apr 2009 02:35:03 +0000 Subject: [PATCH] =?UTF-8?q?tools=E5=89=8A=E9=99=A4=E9=9D=A2=E5=80=92?= =?UTF-8?q?=E3=81=AA=E3=81=AE=E3=81=A7=E4=B8=80=E9=83=A8=E6=88=BB=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: file:///Users/lillianskinner/Downloads/platinum/twl/TwlIPL/branches/20090413_for_systemupdater_ck_0_1@2806 b08762b0-b915-fc4b-9d8c-17b2551a87ff --- build/Makefile | 4 + build/tools/Makefile | 34 +++ build/tools/attach_dummyromheader/Makefile | 55 ++++ .../attach_dummyromheader.c | 247 ++++++++++++++++++ build/tools/attach_dummyromheader/misc.h | 23 ++ build/tools/rombreaker/Makefile | 55 ++++ build/tools/rombreaker/misc.h | 23 ++ build/tools/rombreaker/rombreaker.c | 168 ++++++++++++ 8 files changed, 609 insertions(+) create mode 100644 build/tools/Makefile create mode 100644 build/tools/attach_dummyromheader/Makefile create mode 100644 build/tools/attach_dummyromheader/attach_dummyromheader.c create mode 100644 build/tools/attach_dummyromheader/misc.h create mode 100644 build/tools/rombreaker/Makefile create mode 100644 build/tools/rombreaker/misc.h create mode 100644 build/tools/rombreaker/rombreaker.c diff --git a/build/Makefile b/build/Makefile index 38c1abac..c06c0e2a 100644 --- a/build/Makefile +++ b/build/Makefile @@ -21,6 +21,10 @@ include $(TWL_IPL_RED_ROOT)/build/buildtools/commondefs #---------------------------------------------------------------------------- +ifneq ($(TWL_KEYSDIR),) +SUBDIRS = tools +endif + SUBDIRS += libraries_sysmenu \ components \ systemMenu_RED diff --git a/build/tools/Makefile b/build/tools/Makefile new file mode 100644 index 00000000..f8004c4a --- /dev/null +++ b/build/tools/Makefile @@ -0,0 +1,34 @@ +#! make -f +#---------------------------------------------------------------------------- +# Project: TwlIPL - firmware +# File: Makefile +# +# Copyright 2007 Nintendo. All rights reserved. +# +# These coded instructions, statements, and computer programs contain +# proprietary information of Nintendo of America Inc. and/or Nintendo +# Company Ltd., and are protected by Federal copyright law. They may +# not be disclosed to third parties or copied or duplicated in any form, +# in whole or in part, without the prior written consent of Nintendo. +# +# $Date:: $ +# $Rev$ +# $Author$ +#---------------------------------------------------------------------------- + +include $(TWL_IPL_RED_ROOT)/build/buildtools/commondefs + + +#---------------------------------------------------------------------------- + +SUBDIRS = \ + attach_dummyromheader \ + rombreaker \ + + +#---------------------------------------------------------------------------- + +include $(TWL_IPL_RED_ROOT)/build/buildtools/modulerules + + +#===== End of Makefile ===== diff --git a/build/tools/attach_dummyromheader/Makefile b/build/tools/attach_dummyromheader/Makefile new file mode 100644 index 00000000..6fc9d510 --- /dev/null +++ b/build/tools/attach_dummyromheader/Makefile @@ -0,0 +1,55 @@ +#! make -f +#--------------------------------------------------------------------------- +# Project: TwlSDK - dummyromheader - +# File: Makefile +# +# Copyright 2007 Nintendo. All rights reserved. +# +# These coded instructions, statements, and computer programs contain +# proprietary information of Nintendo of America Inc. and/or Nintendo +# Company Ltd., and are protected by Federal copyright law. They may +# not be disclosed to third parties or copied or duplicated in any form, +# in whole or in part, without the prior written consent of Nintendo. +# +# $Date:: +# $Rev: +# $Author: +#--------------------------------------------------------------------------- + + +#---------------------------------------------------------------------------- + +#---------------------------------------------------------------------------- + +TARGET_FIRM = SYSTEMMENU + +include $(TWL_IPL_RED_ROOT)/build/buildtools/commondefs + +#---------------------------------------------------------------------------- + +TARGETS = attach_dummyromheader.exe + + +SOURCES = attach_dummyromheader.c + +OBJECTS = $(SOURCES:.c=.o) + + +LDIRT_CLEAN = $(OBJECTS) $(TARGETS) +INSTALL_TARGETS = $(TARGETS) +INSTALL_DIR = $(SYSMENU_TOOLSDIR)/bin + + +include $(TWLSDK_ROOT)/build/buildtools/twl/modulerules.x86 + +#---------------------------------------------------------------------------- +# build +#---------------------------------------------------------------------------- +do-build: $(TARGETS) + +$(TARGETS): $(OBJECTS) + $(CC_X86) $+ -o $@ + + + +#===== End of Makefile ===== diff --git a/build/tools/attach_dummyromheader/attach_dummyromheader.c b/build/tools/attach_dummyromheader/attach_dummyromheader.c new file mode 100644 index 00000000..382b2b69 --- /dev/null +++ b/build/tools/attach_dummyromheader/attach_dummyromheader.c @@ -0,0 +1,247 @@ +/*---------------------------------------------------------------------------* + Project: TwlIPL - tools - attach_dummyromheader + File: attach_dummyromheader.c + + Copyright 2008 Nintendo. All rights reserved. + + These coded instructions, statements, and computer programs contain + proprietary information of Nintendo of America Inc. and/or Nintendo + Company Ltd., and are protected by Federal copyright law. They may + not be disclosed to third parties or copied or duplicated in any form, + in whole or in part, without the prior written consent of Nintendo. + + $Date:: + $Rev: + $Author: + *---------------------------------------------------------------------------*/ + +#include +#include +#include +#include +#include +#include "misc.h" +#include + +/* + attach_dummyromheader.exe -s -l -i nwm_firm.bin -c WFW0 -o m.srl + maketad m.srl -s +*/ + +static ROM_Header rom_header; + +#define FILE_READ_BUFFER_LENGTH 0x1000 + +/* maketadのソースから引用 */ +#define TAD_BOOT_IDX 0 +#define TITLE_NAME_OFFSET 0x0 +#define PUBLISHER_CODE 0x3 + +#define CHANNEL_SYSTEM_BIT 0x1 +#define CHANNEL_SHOW_BIT 0x2 +#define CHANNEL_CARD_BIT 0x4 +#define CHANNEL_DATA_ONLY_BIT 0x8 + + +int main(int argc, char *argv[]) +{ + char *binFile = NULL; + char *srlFile = NULL; + char *g_code = NULL; + FILE *bin_fp; + FILE *srl_fp; + int n; + int read_size; + BOOL isSystem = FALSE; + BOOL isLaunch = FALSE; + unsigned char file_read_buffer[FILE_READ_BUFFER_LENGTH]; + + while ((n = getopt(argc, argv, "hiocsl")) != -1) { + switch (n) { + case 'h': + printf("Usage:\n" + " %s -s -l -c -i inputfile -o outputfile\n" + " -s (Systemfile) \n" + " -l (Not Launch) \n" + " -c XXXX ( GameCode )\n" + " -i XXXX ( input file )\n" + " -o XXXX ( output file )\n\n", + argv[0]); + return 0; + + case 'i': + binFile = strdup( argv[optind] ); + printf("input file = %s\n", binFile); + break; + case 'o': + srlFile = strdup( argv[optind] ); + printf("srl file = %s\n", srlFile); + break; + + case 'c': + g_code = strdup( argv[optind] ); + printf("game code = %s\n", g_code); + if( 4 != strlen(g_code) ) { + fprintf(stderr,"Error: invalid game code %s\n",g_code); + return -1; + } + + break; + case 's': + isSystem = TRUE; + printf("system file\n"); + break; + case 'l': + isLaunch = TRUE; + printf("Not Launch file\n"); + break; + default: + break; + } + + } + + if( binFile && srlFile ) { + if( NULL == (bin_fp = fopen(binFile, "rb")) ) { + fprintf(stderr,"Error:binFile open error %s\n",binFile); + return -1; + } + if( NULL == (srl_fp = fopen(srlFile, "wb+")) ) { + fprintf(stderr,"Error:srlFile open error %s\n",srlFile); + return -1; + } + } + else { + fprintf(stderr,"Error:not specify binFile or srlFile\n"); + return -1; + } + + /* ROM Header情報生成 */ + memset((void *)&rom_header, 0 , sizeof(ROM_Header)); + + // strncpy((void *)&rom_header.s.title_name, "WLANFIRMWARE", TITLE_NAME_MAX); + strncpy((void *)&rom_header.s.title_name, g_code, TITLE_NAME_MAX); + + // strncpy((void *)&rom_header.s.game_code,"WFW0",GAME_CODE_MAX); // Game code + strncpy((void *)&rom_header.s.game_code, g_code ,GAME_CODE_MAX); // Game code + + rom_header.s.maker_code[0] = 0; // Maker code + rom_header.s.maker_code[1] = 3; // Maker code + rom_header.s.platform_code = 0x03; /* Platform code bit0: not support NTR, bit1: support TWL + ( NTR_only=0x00, NTR/TWL=0x03, TWL_only=0x02 ) */ + + // rom_header.s.rom_type; + // rom_header.s.rom_size; /* Rom size (2のrom_size乗 Mbit: ex. 128Mbitのときrom_size = 7) */ + + // rom_header.s.enable_signature:1; // enable ROM Header signature + // rom_header.s.enable_aes:1; // enable AES encryption + // rom_header.s.developer_encrypt_old:1; // 開発用セキュリティがかかっている場合に"1"。製品版では"0" + + // u32 arm7_scfg_ext; // SCFG-EXT + // u8 arm7_scfg_clk; // SCFG-CLK + + + // 0x1BF - TWL expansion flags + // u8 codec_mode:1; // 0:NTR mode, 1:TWL mode // undeveloped + // u8 otherParentalControls:1; // 1: Enable OtherParentalControls + // u8 subBannerFile:1; // 1: Enable SubBannerFile + rom_header.s.exFlags.codec_mode = 1; // 0:NTR mode, 1:TWL mode // undeveloped + rom_header.s.exFlags.availableSubBannerFile = 0; // 1: Enable SubBannerFile + + + // u8 titleID_Lo[ 4 ]; + // u32 titleID_Hi; + + rom_header.s.titleID_Lo[ 3 ] = *(g_code); + rom_header.s.titleID_Lo[ 2 ] = *(g_code+1); + rom_header.s.titleID_Lo[ 1 ] = *(g_code+2); + rom_header.s.titleID_Lo[ 0 ] = *(g_code+3); + + /* + ビットフィールド: + [0]: AppType ( 0: User, 1: System ) + [1]: Launch ( 0: Launch, 1: Not launch ) + [2]: Media ( 0: GameCard, 1: NAND ) + [3-15]: rsv. + [16-31]: PublisherCode( 2:Broadon, 3:Nintendo) + */ + + rom_header.s.titleID_Hi = ( 3 /* Nintendo */ << 16) | + CHANNEL_CARD_BIT | + CHANNEL_DATA_ONLY_BIT | + (isLaunch? 2:0) | + (isSystem? 1:0); + + printf("titleID = 0x%08X%08x\n", (unsigned int)rom_header.s.titleID_Hi, (unsigned int)*((u32 *)&(rom_header.s.titleID_Lo[0]))); + + // 0x02f0 - 0x0300 Parental Controls Rating Info + // u8 ParentalControlsRatingInfo[ 0x10 ]; + + rom_header.s.parental_control_rating_info[ 0x0 ] = 0; + rom_header.s.parental_control_rating_info[ 0x1 ] = 1; + rom_header.s.parental_control_rating_info[ 0x2 ] = 2; + rom_header.s.parental_control_rating_info[ 0x3 ] = 3; + rom_header.s.parental_control_rating_info[ 0x4 ] = 4; + rom_header.s.parental_control_rating_info[ 0x5 ] = 5; + rom_header.s.parental_control_rating_info[ 0x6 ] = 6; + rom_header.s.parental_control_rating_info[ 0x7 ] = 7; + rom_header.s.parental_control_rating_info[ 0x8 ] = 8; + rom_header.s.parental_control_rating_info[ 0x9 ] = 9; + rom_header.s.parental_control_rating_info[ 0xa ] = 0xa; + rom_header.s.parental_control_rating_info[ 0xb ] = 0xb; + rom_header.s.parental_control_rating_info[ 0xc ] = 0xc; + rom_header.s.parental_control_rating_info[ 0xd ] = 0xd; + rom_header.s.parental_control_rating_info[ 0xe ] = 0xe; + rom_header.s.parental_control_rating_info[ 0xf ] = 0xf; + + /* ROMバージョンの設定 */ + { + /* FWファイルの先頭1バイト(version)を取得 */ + unsigned char version = fgetc(bin_fp); + + rom_header.s.rom_version = version; + + if (fseek(bin_fp, 0, SEEK_SET)) { + fprintf(stderr,"Error:file seek error(bin file)\n"); + return -1; + } + } + + /* SRLファイルの書き出し */ + if(1 != fwrite((void *)&rom_header, sizeof(ROM_Header), 1, srl_fp) ) { + fprintf(stderr,"Error:file write error(ROM_Header)\n"); + return -1; + } + + + while( 1 ) { + read_size = fread((void *)file_read_buffer, 1, FILE_READ_BUFFER_LENGTH, bin_fp); + if( read_size ) { + if( 1 != fwrite((void *)&file_read_buffer, read_size, 1, srl_fp) ) { + fprintf(stderr,"Error:file write error(bin file)\n"); + return -1; + } + } + else { + break; + } + } + /* SRLファイルは最低16バイトの倍数にしておく */ +#define PADDING_TARGET 16 + { + long fraction = ftell(srl_fp) % PADDING_TARGET; + if( fraction ) { + long zero[4] = { 0, 0, 0, 0 }; + if( 1 != fwrite((void*)zero, PADDING_TARGET - fraction, 1, srl_fp) ) { + fprintf(stderr,"Error:file write error(padding)\n"); + return -1; + } + } + } + + fclose(bin_fp); + fclose(srl_fp); + + printf("success!\n"); + return 0; +} diff --git a/build/tools/attach_dummyromheader/misc.h b/build/tools/attach_dummyromheader/misc.h new file mode 100644 index 00000000..26a1cbc6 --- /dev/null +++ b/build/tools/attach_dummyromheader/misc.h @@ -0,0 +1,23 @@ +#ifndef MISC_H_ +#define MISC_H_ +#include + +typedef enum +{ + FALSE = 0, + TRUE = 1 +} +BOOL; + +typedef unsigned char u8; +typedef unsigned short int u16; +typedef unsigned long int u32; +typedef unsigned long long u64; + +typedef signed char s8; +typedef signed short int s16; +typedef signed long int s32; +typedef signed long long s64; + +#endif //MISC_H_ + diff --git a/build/tools/rombreaker/Makefile b/build/tools/rombreaker/Makefile new file mode 100644 index 00000000..395086ac --- /dev/null +++ b/build/tools/rombreaker/Makefile @@ -0,0 +1,55 @@ +#! make -f +#--------------------------------------------------------------------------- +# Project: TwlSDK - dummyromheader - +# File: Makefile +# +# Copyright 2007 Nintendo. All rights reserved. +# +# These coded instructions, statements, and computer programs contain +# proprietary information of Nintendo of America Inc. and/or Nintendo +# Company Ltd., and are protected by Federal copyright law. They may +# not be disclosed to third parties or copied or duplicated in any form, +# in whole or in part, without the prior written consent of Nintendo. +# +# $Date:: +# $Rev: +# $Author: +#--------------------------------------------------------------------------- + + +#---------------------------------------------------------------------------- + +#---------------------------------------------------------------------------- + +TARGET_FIRM = SYSTEMMENU + +include $(TWL_IPL_RED_ROOT)/build/buildtools/commondefs + +#---------------------------------------------------------------------------- + +TARGETS = rombreaker.exe + + +SOURCES = rombreaker.c + +OBJECTS = $(SOURCES:.c=.o) + + +LDIRT_CLEAN = $(OBJECTS) $(TARGETS) +INSTALL_TARGETS = $(TARGETS) +INSTALL_DIR = $(SYSMENU_TOOLSDIR)/bin + + +include $(TWLSDK_ROOT)/build/buildtools/twl/modulerules.x86 + +#---------------------------------------------------------------------------- +# build +#---------------------------------------------------------------------------- +do-build: $(TARGETS) + +$(TARGETS): $(OBJECTS) + $(CC_X86) $+ -o $@ + + + +#===== End of Makefile ===== diff --git a/build/tools/rombreaker/misc.h b/build/tools/rombreaker/misc.h new file mode 100644 index 00000000..26a1cbc6 --- /dev/null +++ b/build/tools/rombreaker/misc.h @@ -0,0 +1,23 @@ +#ifndef MISC_H_ +#define MISC_H_ +#include + +typedef enum +{ + FALSE = 0, + TRUE = 1 +} +BOOL; + +typedef unsigned char u8; +typedef unsigned short int u16; +typedef unsigned long int u32; +typedef unsigned long long u64; + +typedef signed char s8; +typedef signed short int s16; +typedef signed long int s32; +typedef signed long long s64; + +#endif //MISC_H_ + diff --git a/build/tools/rombreaker/rombreaker.c b/build/tools/rombreaker/rombreaker.c new file mode 100644 index 00000000..05ba3810 --- /dev/null +++ b/build/tools/rombreaker/rombreaker.c @@ -0,0 +1,168 @@ +/*---------------------------------------------------------------------------* + Project: TwlIPL - tools - rombreaker + File: rombreaker.c + + Copyright 2008 Nintendo. All rights reserved. + + These coded instructions, statements, and computer programs contain + proprietary information of Nintendo of America Inc. and/or Nintendo + Company Ltd., and are protected by Federal copyright law. They may + not be disclosed to third parties or copied or duplicated in any form, + in whole or in part, without the prior written consent of Nintendo. + + $Date:: + $Rev: + $Author: + *---------------------------------------------------------------------------*/ + +/* + コマンドライン引数で指定されたROMファイルを読み込む。 + 破壊オプションで指定された領域をできるだけ安全に破壊する。 +*/ + +#include +#include +#include +#include +#include +#include "misc.h" +#include + +/* + rombreaker.exe -i input.srl -o output.srl -b HEADER +*/ + +static ROM_Header_Short *rom_header_short; + +#define MATH_ROUNDUP(x, base) (((x) + ((base)-1)) & ~((base)-1)) + +// 1MBぐらいずつ +#define FILE_READ_BUFFER_LENGTH ( 1 * 1024 * 1024 ) + +int main(int argc, char *argv[]) +{ + char *inFile = NULL; + char *outFile = NULL; + char *breakOpt = NULL; + FILE *in_fp; + FILE *out_fp; + int n; + int read_size; + unsigned char file_read_buffer[FILE_READ_BUFFER_LENGTH]; + u32 breakoffset; + u32 fcounter; + + while ((n = getopt(argc, argv, "hiob")) != -1) { + switch (n) { + case 'h': + printf("Usage:\n" + " %s -b breakopt -i inputfile -o outputfile\n" + " -b HEADER/ARM9FLX/ARM9LTD/ARM7FLX/ARM7LTD ( BreakOption )\n" + " -i XXXX ( input srl file )\n" + " -o XXXX ( output srl file )\n\n", + argv[0]); + return 0; + + case 'i': + inFile = strdup( argv[optind] ); + printf("input srl file = %s\n", inFile); + break; + case 'o': + outFile = strdup( argv[optind] ); + printf("output srl file = %s\n", outFile); + break; + case 'b': + breakOpt = strdup( argv[optind] ); + printf("break opt = %s\n", breakOpt); + break; + default: + break; + } + } + + if( inFile && outFile ) { + // 面倒なのでinとoutが同じ場合は対応しない(要望があれば対応) + if( strncmp( inFile, outFile, 256 ) == 0 ) + { + fprintf(stderr,"Error:output is input.\n"); + return -1; + } + if( NULL == (in_fp = fopen(inFile, "rb")) ) { + fprintf(stderr,"Error:input srl file open error %s\n",inFile); + return -1; + } + if( NULL == (out_fp = fopen(outFile, "wb+")) ) { + fprintf(stderr,"Error:output srl file open error %s\n",outFile); + return -1; + } + } + else { + fprintf(stderr,"Error:not specify input or output\n"); + return -1; + } + +/* + FLXとLTDはできるだけコードに影響を与えないよう後ろから1バイト破壊。 + HEADERは署名範囲内で誰も使っていない予約領域を1バイト破壊。 +*/ + + /* ROM Header情報取得 */ + rom_header_short = (ROM_Header_Short *)file_read_buffer; + read_size = fread( (void *)rom_header_short, 1, sizeof(ROM_Header_Short), in_fp ); + if( read_size != sizeof(ROM_Header_Short) ) + { + fprintf(stderr,"Error:rom_header_short read error\n"); + return -1; + } + + if( strncmp( "HEADER", breakOpt, 7 ) == 0 ) + { + breakoffset = 0x180 - 1; + }else if( strncmp( "ARM9FLX", breakOpt, 8 ) == 0 ) + { + breakoffset = rom_header_short->main_rom_offset + rom_header_short->main_size - 1; + }else if( strncmp( "ARM9LTD", breakOpt, 8 ) == 0 ) + { + breakoffset = rom_header_short->main_ltd_rom_offset + rom_header_short->main_ltd_size - 1; + }else if( strncmp( "ARM7FLX", breakOpt, 8 ) == 0 ) + { + breakoffset = rom_header_short->sub_rom_offset + rom_header_short->sub_size - 1; + }else if( strncmp( "ARM7LTD", breakOpt, 8 ) == 0 ) + { + breakoffset = rom_header_short->sub_ltd_rom_offset + rom_header_short->sub_ltd_size - 1; + }else + { + // 破壊オプション指定ミス + fprintf(stderr,"Error: invalid break opt %s\n",breakOpt); + return -1; + } + + // inputのファイルポインタを先頭に戻す(面倒なので) + fseek( in_fp, 0, SEEK_SET ); + + // 壊す場所を書き換えながらコピー + while( 1 ) { + read_size = fread((void *)file_read_buffer, 1, FILE_READ_BUFFER_LENGTH, in_fp); + if( read_size ) { + fcounter += read_size; + // 書き換え + if( fcounter > breakoffset ) + { + file_read_buffer[ breakoffset - ( fcounter - read_size ) ] += 1; // 1足すだけで壊れる + } + if( read_size != fwrite((void *)&file_read_buffer, 1, read_size, out_fp) ) { + fprintf(stderr,"Error:file write error\n"); + return -1; + } + } + else { + break; + } + } + + fclose(in_fp); + fclose(out_fp); + + printf("success!\n"); + return 0; +}