summaryrefslogtreecommitdiff
path: root/src/satmkboot.c
blob: d3b595828e35d9c723f1ab3d1609c94b8b998089 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "bootinfo.h"
#include "symbols.h"

static struct systemid sysid = {
	.title        = "TEST TITLE                                                                                                      ",
	.maker        = "SEGA TP T-000   ",
	.product      = "T-000000G ",
	.version      = "V0.001",
	.reldate      = "20000101",
	.device       = "CD-1/1  ",
	.regions      = "          ",
	.peripherals  = "                ",
	.bootsize     = 0xe00,
	.stack_master = 0,
	.stack_slave  = 0,
	.load_addr    = 0x06004000,
	.load_size    = 0,
};

static char ipbuf[0x7200];
static char *ipfile, *outfile;

static char *serialize_region_code(char *out, const struct symbolname *region)
{
	size_t namelen  = strlen(region->name);
	char  *tmp = out;
	char  *end = out+32;

	memcpy(tmp, "\xa0\x0e\x00\x09For ", 8); tmp += 8;
	memcpy(tmp, region->name,     namelen); tmp += namelen;
	*tmp++ = '.';
	memset(tmp, ' ', end - tmp);
	return end;
}

#define WRITE(f,p,s) fwrite(p,s,1,f)
#define WRITE32(f,v) fwrite((char[]){(v)>>24,(v)>>16,(v)>>8,(v)},4,1,f)

static int write_output(FILE *fp)
{
	static const unsigned char zeros[16];
	extern const unsigned char securitycode[];
	extern const size_t        securitycode_size;

	/* system id - 0x100 bytes */
	WRITE  (fp, "SEGA SEGASATURN ", 16);
	WRITE  (fp, sysid.maker,        16);
	WRITE  (fp, sysid.product,      10);
	WRITE  (fp, sysid.version,       6);
	WRITE  (fp, sysid.reldate,       8);
	WRITE  (fp, sysid.device,        8);
	WRITE  (fp, sysid.regions,      10);
	WRITE  (fp, "      ",            6);
	WRITE  (fp, sysid.peripherals,  16);
	WRITE  (fp, sysid.title,       112);
	WRITE  (fp, zeros,              16); /* reserved bytes */
	WRITE32(fp, sysid.bootsize);
	WRITE  (fp, zeros,               4); /* reserved bytes */
	WRITE32(fp, sysid.stack_master);
	WRITE32(fp, sysid.stack_slave);
	WRITE32(fp, sysid.load_addr);
	WRITE32(fp, sysid.load_size);
	WRITE  (fp, zeros,               8); /* reserved bytes */

	/* security code */
	WRITE(fp, securitycode, securitycode_size);

	/* initial program */
	WRITE(fp, ipbuf, sysid.bootsize - 0xe00);
	return ferror(fp) ? -1 : 0;
}

static size_t load_ip(char *filename, char *out, size_t maxsize)
{
	FILE  *fp;
	size_t size;
	const char *errprefix = "Error loading initial program";

	if (!(fp = fopen(filename, "rb"))) {
		goto fail_perror;
	}
	if ((size = fread(out, 1, maxsize, fp)) < maxsize) {
		/* read fewer than requested amount. determine if we hit error or eof */
		if (ferror(fp)) {
			goto fail_perror;
		}
	} else {
		/* successfully read the requested amount. verify we reached eof */
		if (fgetc(fp) != EOF) {
			fprintf(stderr, "%s: exceeds maximum size\n", errprefix);
			goto fail;
		}
	}

	fclose(fp);
	return size;

fail_perror:
	perror(errprefix);
fail:
	if (fp) fclose(fp);
	return -1;
}

static int append_symbol(char *list, size_t size, char symbol)
{
	for (size_t i = 0; i < size; i++) {
		if (list[i] == ' ')
			list[i] = symbol;
		if (list[i] == symbol)
			return 0;
	}
	return -1;
}

static void print_symbols(FILE *fp, int width, const struct symbolname *symbols)
{
	for (; symbols->symbol; symbols++) {
		fprintf(fp, "\t%*s%c: %s\n", width+4, "", symbols->symbol, symbols->name);
	}
}

static void usage(const char *progname)
{
	const int width = 20;

	fprintf(stderr, "usage: %s -h\n", progname);
	fprintf(stderr, "       %s -io[pr]\n\n", progname);
	fprintf(stderr, "\t%-*s%s\n", width, "-h",              "Show this help text");
	fprintf(stderr, "\t%-*s%s\n", width, "-i ip.bin",       "Initial program code file");
	fprintf(stderr, "\t%-*s%s\n", width, "-o output",       "Output file");
	fprintf(stderr, "\t%-*s%s\n", width, "-p peripherals",  "Supported peripherals (default: control pad):");
	print_symbols(stderr, width, peripheraldefs);
	fprintf(stderr, "\t%-*s%s\n", width, "-r regions",      "Geographical regions (default: all):");
	print_symbols(stderr, width, regiondefs);
}

#define ERR_UNKNOWN_OPTION      0
#define ERR_MISSING_OPTION      1
#define ERR_DUPLICATE_INPUT     2
#define ERR_DUPLICATE_OUTPUT    3
#define ERR_REGION_OVERFLOW     4
#define ERR_PERIPHERAL_OVERFLOW 5

static const char *errmsgs[] = {
	"Unknown extra argument",
	"Missing required option",
	"Duplicate -i parameter",
	"Duplicate -o parameter",
	"Too many regions specified (max 10)",
	"Too many peripherals specified (max 16)",
};

static int show_arg_error(const char *progname, int prev_errors, int error)
{
	if (!(prev_errors & 1 << error))
		fprintf(stderr, "%s: %s\n", progname, errmsgs[error]);
	return prev_errors | 1 << error;
}

static void process_symbols(const char *progname, char *list, size_t size, const char *arg, int *errors, int overflow)
{
	for (; *arg; arg++) {
		if (append_symbol(list, size, *arg)) {
			*errors = show_arg_error(progname, *errors, overflow);
			return;
		}
	}
}

static int process_args(int argc, char **argv)
{
	int errors = 0, fail = 0, help = 0;
	int opt;

	extern char *optarg;
	extern int optind, optopt;
	while ((opt = getopt(argc, argv, "hi:o:r:p:")) != -1) {
		switch (opt) {
		case 'h':
			help = 1;
			break;

		case 'i':
			if (ipfile)
				errors = show_arg_error(argv[0], errors, ERR_DUPLICATE_INPUT);
			else
				ipfile = optarg;
			break;

		case 'o':
			if (outfile)
				errors = show_arg_error(argv[0], errors, ERR_DUPLICATE_OUTPUT);
			else
				outfile = optarg;
			break;

		case 'p':
			process_symbols(argv[0], sysid.peripherals, sizeof sysid.peripherals, optarg, &errors, ERR_PERIPHERAL_OVERFLOW);
			break;

		case 'r':
			process_symbols(argv[0], sysid.regions, sizeof sysid.regions, optarg, &errors, ERR_REGION_OVERFLOW);
			break;

		default:
			fail = 1;
		}
	}

	if (help) {
		usage(argv[0]);
	} else if (!fail) {
		if (argv[optind])
			errors = show_arg_error(argv[0], errors, ERR_UNKNOWN_OPTION);
		if (!ipfile || !outfile)
			errors = show_arg_error(argv[0], errors, ERR_MISSING_OPTION);
		fail = errors;
	}

	if (fail) {
		fprintf(stderr, "\nUse -h option for help on correct usage.\n");
	}

	return fail | help;
}

int main(int argc, char **argv)
{
	FILE  *outfp = NULL;
	char  *ipout = ipbuf;
	size_t ipsize;

	if (process_args(argc, argv)) {
		return 1;
	}

	if (sysid.regions[0]     == ' ') strcpy(sysid.regions, "JTUBKAEL");
	if (sysid.peripherals[0] == ' ') sysid.peripherals[0] = 'J';

	for (int i = 0; i < 10 && sysid.regions[i] != ' '; i++) {
		const struct symbolname *region = find_symbol(regiondefs, sysid.regions[i]);
		if (region) ipout = serialize_region_code(ipout, region);
	}

	if ((ipsize = load_ip(ipfile, ipout, sizeof ipbuf - (ipout - ipbuf))) == -1) {
		goto fail;
	}
	sysid.bootsize += ipsize;

	if (!(outfp = fopen(outfile, "wb"))) {
		perror("Error opening output file");
		goto fail;
	}

	if (write_output(outfp)) {
		perror("Error writing output");
		goto fail;
	}

	fclose(outfp);
	return 0;

fail:
	if (outfp) fclose(outfp);
	return 1;
}