1 
2 /***************************************************************************
3  *                    __            __ _ ___________                       *
4  *                    \ \          / /| |____   ____|                      *
5  *                     \ \        / / | |    | |                           *
6  *                      \ \  /\  / /  | |    | |                           *
7  *                       \ \/  \/ /   | |    | |                           *
8  *                        \  /\  /    | |    | |                           *
9  *                         \/  \/     |_|    |_|                           *
10  *                                                                         *
11  *                           Wiimms ISO Tools                              *
12  *                         http://wit.wiimm.de/                            *
13  *                                                                         *
14  ***************************************************************************
15  *                                                                         *
16  *   This file is part of the WIT project.                                 *
17  *   Visit http://wit.wiimm.de/ for project details and sources.           *
18  *                                                                         *
19  *   Copyright (c) 2009-2013 by Dirk Clemens <wiimm@wiimm.de>              *
20  *                                                                         *
21  ***************************************************************************
22  *                                                                         *
23  *   This program is free software; you can redistribute it and/or modify  *
24  *   it under the terms of the GNU General Public License as published by  *
25  *   the Free Software Foundation; either version 2 of the License, or     *
26  *   (at your option) any later version.                                   *
27  *                                                                         *
28  *   This program is distributed in the hope that it will be useful,       *
29  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
30  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
31  *   GNU General Public License for more details.                          *
32  *                                                                         *
33  *   See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt       *
34  *                                                                         *
35  ***************************************************************************/
36 
37 #ifndef WIT_PATCH_H
38 #define WIT_PATCH_H 1
39 
40 #include <stdio.h>
41 #include "types.h"
42 #include "lib-std.h"
43 #include "wiidisc.h"
44 
45 //
46 ///////////////////////////////////////////////////////////////////////////////
47 ///////////////			global options			///////////////
48 ///////////////////////////////////////////////////////////////////////////////
49 
50 extern int opt_hook; // true: force relocation hook
51 
52 //-----------------------------------------------------------------------------
53 
54 typedef enum enumEncoding
55 {
56 	// some flags
57 
58 	ENCODE_F_FAST		= 0x0001, // fast encoding wanted
59 	ENCODE_F_ENCRYPT	= 0x0002, // encryption and any signing wanted
60 
61 	// the basic jobs
62 
63 	ENCODE_CLEAR_HASH	= 0x0010, // clear hash area of each sector
64 	ENCODE_CALC_HASH	= 0x0020, // calc hash values for each sector
65 	ENCODE_DECRYPT		= 0x0100, // decrypt sectors
66 	ENCODE_ENCRYPT		= 0x0200, // encrypt sectors
67 	ENCODE_NO_SIGN		= 0x1000, // clear signing area
68 	ENCODE_SIGN		= 0x2000, // fake sign
69 
70 	// the masks
71 
72 	ENCODE_M_HASH		= ENCODE_CLEAR_HASH | ENCODE_CALC_HASH,
73 	ENCODE_M_CRYPT		= ENCODE_DECRYPT | ENCODE_ENCRYPT,
74 	ENCODE_M_SIGN		= ENCODE_NO_SIGN | ENCODE_SIGN,
75 
76 	ENCODE_MASK		= ENCODE_M_HASH | ENCODE_M_CRYPT | ENCODE_M_SIGN
77 				| ENCODE_F_FAST | ENCODE_F_ENCRYPT,
78 
79 	ENCODE_DEFAULT		= 0,	// initial value
80 
81 } enumEncoding;
82 
83 extern enumEncoding encoding;
84 enumEncoding ScanEncoding ( ccp arg );
85 int ScanOptEncoding ( ccp arg );
86 enumEncoding SetEncoding
87 	( enumEncoding val, enumEncoding set_mask, enumEncoding default_mask );
88 
89 //-----------------------------------------------------------------------------
90 
91 typedef enum enumRegion
92 {
93 	REGION_JAP	= 0,	// Japan
94 	REGION_USA	= 1,	// USA
95 	REGION_EUR	= 2,	// Europe
96 	REGION_KOR	= 4,	// Korea
97 
98 	REGION__AUTO	= 0x7ffffffe,	// auto detect
99 	REGION__FILE	= 0x7fffffff,	// try to load from file
100 	REGION__ERROR	= -1,		// hint: error while scanning
101 
102 } enumRegion;
103 
104 extern enumRegion opt_region;
105 enumRegion ScanRegion ( ccp arg );
106 int ScanOptRegion ( ccp arg );
107 ccp GetRegionName ( enumRegion region, ccp unkown_value );
108 
109 //-----------------------------------------------------------------------------
110 
111 typedef struct RegionInfo_t
112 {
113 	enumRegion reg;	    // the region
114 	bool mandatory;	    // is 'reg' mandatory?
115 	char name4[4];	    // short name with maximal 4 characters
116 	ccp name;	    // long region name
117 
118 } RegionInfo_t;
119 
120 const RegionInfo_t * GetRegionInfo ( char region_code );
121 
122 //-----------------------------------------------------------------------------
123 
124 extern enumRegion opt_common_key;
125 wd_ckey_index_t ScanCommonKey ( ccp arg );
126 int ScanOptCommonKey ( ccp arg );
127 
128 //-----------------------------------------------------------------------------
129 
130 extern u64 opt_ios;
131 extern bool opt_ios_valid;
132 
133 bool ScanSysVersion ( u64 * ios, ccp arg );
134 int ScanOptIOS ( ccp arg );
135 
136 //-----------------------------------------------------------------------------
137 
138 extern wd_modify_t opt_modify;
139 wd_modify_t ScanModify ( ccp arg );
140 int ScanOptModify ( ccp arg );
141 
142 //-----------------------------------------------------------------------------
143 
144 extern ccp modify_name;
145 extern ccp modify_id;
146 extern ccp modify_disc_id;
147 extern ccp modify_boot_id;
148 extern ccp modify_ticket_id;
149 extern ccp modify_tmd_id;
150 extern ccp modify_wbfs_id;
151 
152 int ScanOptName ( ccp arg );
153 int ScanOptId ( ccp arg );
154 int ScanOptDiscId ( ccp arg );
155 int ScanOptBootId ( ccp arg );
156 int ScanOptTicketId ( ccp arg );
157 int ScanOptTmdId ( ccp arg );
158 int ScanOptWbfsId ( ccp arg );
159 void NormalizeIdOptions();
160 
161 bool PatchId
162 (
163     void	*dest_id,	// destination with 'maxlen' byte
164     ccp		patch_id,	// NULL or patch string
165     int		skip,		// 'patch_id' starts at index 'skip'
166     int		maxlen		// length of destination ID
167 );
168 
169 bool CopyPatchId
170 (
171     void	*dest,		// destination with 'maxlen' byte
172     const void	*src,		// source of ID. If NULL or empty: clear dest
173     ccp		patch_id,	// NULL or patch string
174     int		maxlen,		// length of destination ID
175     bool	null_term	// true: Add an additional 0 byte to end of dest
176 );
177 
178 bool CopyPatchWbfsId ( char *dest_id6, const void * source_id6 );
179 bool CopyPatchDiscId ( char *dest_id6, const void * source_id6 );
180 
181 bool PatchIdCond ( void * id, int skip, int maxlen, wd_modify_t condition );
182 bool PatchName ( void * name, wd_modify_t condition );
183 bool PatchDiscHeader ( void * dhead, const void * patch_id, const void * patch_name );
184 
185 //-----------------------------------------------------------------------------
186 
187 extern wd_trim_mode_t opt_trim;
188 
189 wd_trim_mode_t ScanTrim
190 (
191     ccp arg,			// argument to scan
192     ccp err_text_extend		// error message extention
193 );
194 
195 int ScanOptTrim
196 (
197     ccp arg			// argument to scan
198 );
199 
200 //-----------------------------------------------------------------------------
201 
202 extern u32  opt_align1;
203 extern u32  opt_align2;
204 extern u32  opt_align3;
205 extern u32  opt_align_part;
206 extern bool opt_align_files;
207 
208 int ScanOptAlign ( ccp arg );
209 int ScanOptAlignPart ( ccp arg );
210 
211 //-----------------------------------------------------------------------------
212 
213 extern u64 opt_disc_size;
214 
215 int ScanOptDiscSize ( ccp arg );
216 
217 //-----------------------------------------------------------------------------
218 
219 extern StringField_t add_file;
220 extern StringField_t repl_file;
221 
222 int ScanOptFile ( ccp arg, bool add );
223 
224 //
225 ///////////////////////////////////////////////////////////////////////////////
226 ///////////////			write patch files		///////////////
227 ///////////////////////////////////////////////////////////////////////////////
228 
229 typedef struct WritePatch_t
230 {
231     //--- file
232 
233     ccp		fname;			// filename for error messages
234 					//  => never NULL, is freed on closing
235     FILE	* file;			// output file
236 
237 } WritePatch_t;
238 
239 ///////////////////////////////////////////////////////////////////////////////
240 
241 void SetupWritePatch
242 (
243     WritePatch_t	* pat		// patch data structure
244 );
245 
246 //-----------------------------------------------------------------------------
247 
248 enumError CloseWritePatch
249 (
250     WritePatch_t	* pat		// patch data structure
251 );
252 
253 //-----------------------------------------------------------------------------
254 
255 enumError CreateWritePatch
256 (
257     WritePatch_t	* pat,		// patch data structure
258     ccp			filename	// filename of output file
259 );
260 
261 //-----------------------------------------------------------------------------
262 
263 enumError CreateWritePatchF
264 (
265     WritePatch_t	* pat,		// patch data structure
266     FILE		* file,		// open output file
267     ccp			filename	// NULL or known filename
268 );
269 
270 ///////////////////////////////////////////////////////////////////////////////
271 
272 enumError WritePatchComment
273 (
274     WritePatch_t	* pat,		// patch data structure
275     ccp			format,		// format string
276     ...					// arguments for 'format'
277 )
278 __attribute__ ((__format__(__printf__,2,3)));
279 
280 //
281 ///////////////////////////////////////////////////////////////////////////////
282 ///////////////			read patch files		///////////////
283 ///////////////////////////////////////////////////////////////////////////////
284 
285 typedef struct ReadPatch_t
286 {
287     //--- status
288 
289     bool	is_valid;		// until now source is a valid patch file
290     bool	is_compatible;		// source has supported format
291     u32		version;		// patch file version
292     u32		compatible;		// patch file down compatible to version
293 
294     //--- file
295 
296     ccp		fname;			// filename for error messages
297 					//  => never NULL, is freed on closing
298     FILE	* file;			// input file
299 
300     //--- read buffer
301 
302     char	read_buf[1024];		// read chache
303     off_t	read_buf_off;		// file offset of first byte in 'read_buf'
304     int		read_buf_len;		// valid data of 'read_buf'
305 
306     //--- current object in read_buf
307 
308     wpat_type_t	cur_type;		// current object type
309     u32		cur_size;		// size of current object
310 					// 'read_buf' is filled as much as possible
311 
312 } ReadPatch_t;
313 
314 ///////////////////////////////////////////////////////////////////////////////
315 
316 void SetupReadPatch
317 (
318     ReadPatch_t		* pat		// patch data structure
319 );
320 
321 //-----------------------------------------------------------------------------
322 
323 enumError CloseReadPatch
324 (
325     ReadPatch_t		* pat		// patch data structure
326 );
327 
328 //-----------------------------------------------------------------------------
329 
330 enumError OpenReadPatch
331 (
332     ReadPatch_t		* pat,		// patch data structure
333     ccp			filename	// filename of input file
334 );
335 
336 //-----------------------------------------------------------------------------
337 
338 enumError OpenReadPatchF
339 (
340     ReadPatch_t		* pat,		// patch data structure
341     FILE		* file,		// open input file
342     ccp			filename	// NULL or known filename
343 );
344 
345 //-----------------------------------------------------------------------------
346 
347 enumError SupportedVersionReadPatch
348 (
349     ReadPatch_t		* pat,		// patch data structure
350     bool		silent		// true: suppress error message
351 );
352 
353 ///////////////////////////////////////////////////////////////////////////////
354 
355 enumError GetNextReadPatch
356 (
357     ReadPatch_t		* pat		// patch data structure
358 );
359 
360 //
361 ///////////////////////////////////////////////////////////////////////////////
362 ///////////////				END			///////////////
363 ///////////////////////////////////////////////////////////////////////////////
364 
365 #endif // WIT_PATCH_H
366