summaryrefslogtreecommitdiffstats
path: root/main.c
blob: 4410b6adcb7f819f33e74a3a7f49fd5c56d487cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
 * Wii WAD Tool
 */

#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <locale.h>
#include <stdlib.h>

#include "types.h"
#include "patcher.h"
#include "wii_wad.h"
#include "ucs.h"

#define VERSION "1.0.0"

void print_usage(int argc, char **argv)
{
	printf("Usage: %s [OPTIONS] FILE\n", argv[0]);
	printf("Modify/show information about Wii WAD FILE.\n\n");
	printf("  -d, --dump\t\tprint detailed information about WAD file\n");
	printf("  -r, --region\t\tchange the region code of the WAD file\n");
	printf("  -v, --version\t\toutput version information and exit\n");
	printf("  -h, --help\t\tprints usage information\n");
	printf("\n");
}

enum mode_type {
	INFO = 0,
	DUMP = 1,
	CHANGE_REGION = 2
};

char *basename(char *name) {
	return strrchr(name, '/')+1;
};

int main(int argc, char **argv)
{
	FILE *f;
	wad_header wad;
	tmd_header tmd;
	wii_tik tik;
	wii_imet imet;
	wii_build_info bi;
	int i;
	char mode = INFO;
	char *wad_filename = NULL;

	if (argc < 2) {
		print_usage(argc, argv);
		return 0;
	}

	setlocale(LC_ALL,"");

	/* parse cmdline args */
	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) {
			printf("Wii WAD Tool %s\n", VERSION);
			return 0;
		}
		else if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--dump")) {
			mode = DUMP;
			continue;
		}
		else if (!strcmp(argv[i], "-r") || !strcmp(argv[i], "--region")) {
			mode = CHANGE_REGION;
			continue;
		}
		else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
			print_usage(argc, argv);
			return 0;
		}
		else {
			if(wad_filename != NULL) {
				print_usage(argc, argv);
				return 0;
			}
			wad_filename = argv[i];
		}
	}

	if (wad_filename == NULL) {
		print_usage(argc, argv);
		return 0;
	}

	f = fopen(wad_filename, "r+");
	if (f == NULL) {
		fprintf(stderr, "Failed to open file %s\n", basename(wad_filename));
		return 0;
	}

	if(wad_read(f, &wad) == 0) {
		fprintf(stderr, "Invalid WAD file %s\n", basename(wad_filename));
		return 0;
	};
	wad_read_tmd(f, &wad, &tmd);
	tmd_content_record tmd_content[tmd.num_contents+1];
	
	switch(mode)
	{
		case INFO:
			tmd_read_content_records(f, &tmd, tmd_content);
			wad_read_tik(f, &wad, &tmd, &tik);

			if(tmd.title_category != TITLE_CATEGORY_SYSTEM) {
				wad_read_app_info(f, &wad, &tmd_content[0], &tik, &bi, &imet);
			}

			printf("- information\n");
			printf("File        : %s\n", basename(wad_filename));

			if(tmd.title_category != TITLE_CATEGORY_SYSTEM) {
				printf("System      : %s\n", tmd_lookup_system_code_name(tmd.title_id));
				printf("Title       : "); print_ucs2_as_utf8(imet.name[IMET_LANG_EN]);
				printf("\n");
			}

			printf("Category    : %s (%08x)\n", tmd_lookup_title_category_name(tmd.title_category), tmd.title_category);

			if(tmd.title_category == TITLE_CATEGORY_SYSTEM) {
				printf("System      : %s\n", "Firmware");
				char name[12];
				tmd_get_system_title_name(be32((u8*)&tmd.title_id), name);
				printf("Title ID    : %s (%08x)\n", name, tmd.title_id);
			}
			else
			{
				printf("Title ID    : %.4s\n", (char*)&tmd.title_id);
			}

			printf("Region Code : %s (%d)\n", region_names[tmd.region_code], tmd.region_code);
			printf("Blocks      : %d\n", tmd_get_block_size(&tmd, tmd_content));
			printf("Publisher ID: %s (%04x)\n", tmd_lookup_group_name(tmd.group_id), tmd.group_id);
			if(tmd.sys_version)
				printf("Firmware    : IOS%llu\n", tmd.sys_version & 0xff);
			printf("Version     : %d\n", tmd.title_version);
		break;
		case CHANGE_REGION:
			change_region_code(f, wad.tmd_size);
		break;
		case DUMP:
			/* dump all information we can parse */
			print_wad(&wad);
			print_tmd(&tmd);

			tmd_read_content_records(f, &tmd, tmd_content);
			print_tmd_content_records(&tmd, tmd_content);
			
			/* read ticket */
			wad_read_tik(f, &wad, &tmd, &tik);
			print_tik(&tik);
			
			if(tmd.title_category != TITLE_CATEGORY_SYSTEM) {
				/* read imet tag from first app content */
				printf("- application info\n");
				printf("Blocks        : %d\n", tmd_get_block_size(&tmd, tmd_content));
				wad_read_app_info(f, &wad, &tmd_content[0], &tik, &bi, &imet);
				printf("Dirname       : %s\n", bi.dirname);
				printf("Build Host    : %s\n", bi.host);
				printf("Unknown Flag 0: %04x\n", bi.unk_flag0);
				printf("Unknown Flag 1: %04x\n", bi.unk_flag1);
				for (i = 0; i < IMET_NAME_COUNT; i++) {
					printf("Name[%d]       : ", i);
					print_ucs2_as_utf8(imet.name[i]);
					printf("\n");
				}
			}
		break;
	}

	fclose(f);

	return 0;
}