summaryrefslogtreecommitdiff
path: root/src/saturn-mkboot.c
blob: 2ddd0ed3a2c65396d21e5373b9eb5c6677924933 (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
273
274
275
276
277
278
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

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

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

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

static void serialize_region_code(char *out, const struct symbolname *region)
{
	static const char defcode[32] = "\xa0\x0e\x00\x09" "For                         ";
	size_t namelen = region ? strlen(region->name) : 0;

	memcpy(out, defcode, sizeof defcode);
	if (namelen) memcpy(out+8, region->name, namelen);
	out[8+namelen] = '.';
}

#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)
{
	/* system id - 0x100 bytes */
	WRITE  (fp, &sysid, offsetof(struct systemid, bootsize));
	WRITE32(fp, sysid.bootsize);
	WRITE32(fp, 0); /* reserved bytes */
	WRITE32(fp, sysid.stack_master);
	WRITE32(fp, sysid.stack_slave);
	WRITE32(fp, sysid.load_addr);
	WRITE32(fp, sysid.load_size);
	WRITE32(fp, 0); /* reserved bytes */
	WRITE32(fp, 0); /* reserved bytes */

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

static FILE *open_installed_boot_file(const char *filename)
{
	static const char *bootpath = DATA_PATH "/boot";
	char *path = malloc(strlen(bootpath) + strlen(filename) + 2);
	if (!path) return NULL;
	sprintf(path, "%s/%s", bootpath, filename);
	return fopen(path, "rb");
}

static FILE *open_boot_file(const char *filename)
{
	FILE *fp = fopen(filename, "rb");
	if (fp) return fp;
	if (!strchr(filename, '/')) return open_installed_boot_file(filename);
	return NULL;
}

static size_t readfile(FILE *fp, char *out, size_t maxsize, const char *errprefix)
{
	size_t size = fread(out, 1, maxsize, fp);
	if (size == maxsize && fgetc(fp) != EOF) {
		fprintf(stderr, "%s: exceeds maximum size\n", errprefix);
		size = -1;
	}
	return size;
}

static size_t load_security_code(char *out)
{
	const char *errprefix = "Error loading security code";
	FILE *fp;
	if (!(fp = fopen(DATA_PATH "/securitycode.bin", "rb"))) {
		perror(errprefix);
		return -1;
	}
	size_t size = readfile(fp, out, 0xd00, errprefix);
	if (size != -1 && size != 0xd00) fprintf(stderr, "%s: wrong size\n", errprefix);
	fclose(fp);
	return size;
}

static size_t load_ip(char *out, size_t maxsize)
{
	FILE *fp = ipfile ? open_boot_file(ipfile) : open_installed_boot_file("simple");
	const char *errprefix = "Error loading initial program";

	if (!fp) {
		perror(errprefix);
		return -1;
	}

	size_t size = readfile(fp, out, maxsize, errprefix);
	fclose(fp);
	return size;
}

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

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

	printf("usage: %s -h\n", progname);
	printf("       %s -o[iprms]\n\n", progname);
	printf("\t%-*s%s\n", width, "-h",              "Show this help text");
	printf("\t%-*s%s\n", width, "-o output",       "Output file");
	printf("\t%-*s%s\n", width, "-i ip.bin",       "Initial program code file");
	printf("\t%-*s%s\n", width, "-p peripherals",  "Supported peripherals (default: control pad):");
	print_symbols(width, peripheraldefs);
	printf("\t%-*s%s\n", width, "-r regions",      "Geographical regions (default: all):");
	print_symbols(width, regiondefs);
	printf("\t%-*s%s\n", width, "-m master_stack", "Master stack address (default 0x06002000)");
	printf("\t%-*s%s\n", width, "-s slave_stack",  "Slave stack address (default 0x06001000)");

	exit(0);
}

static int process_symbols(const char *progname, char *list, size_t size, const char *arg)
{
	while (size-- && *arg) *list++ = *arg++;
	while (size--) *list++ = ' ';
	return *arg;
}

static int process_address(const char *progname, uint32_t *addr, const char *arg)
{
	char *end;
	errno = 0;
	unsigned long val = strtoul(arg, &end, 16);
	if (end == arg || *end || errno || val > (uint32_t)-1) return 1;
	*addr = val;
	return 0;
}

#define FLAG_o (1U << 3)

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

	extern char *optarg;
	extern int optind, optopt;
	static const char *optpat = "hi:o:p:r:m:s:";
	while ((opt = getopt(argc, argv, optpat)) != -1) {
		uint32_t flag = opt == '?' ? 0 : 1U << strchr(optpat, opt) - optpat;
		if (seen & flag) {
			fprintf(stderr, "%s: Duplicate option -%c\n", argv[0], opt);
			fail = 1;
			continue;
		}
		seen |= flag;

		switch (opt) {
		case 'h':
			usage(argv[0]);

		case 'i':
			ipfile = optarg;
			break;

		case 'o':
			outfile = optarg;
			break;

		case 'm':
			if (process_address(argv[0], &sysid.stack_master, optarg)) {
				fprintf(stderr, "%s: Invalid master stack\n", argv[0]);
				fail = 1;
			}
			break;

		case 's':
			if (process_address(argv[0], &sysid.stack_slave, optarg)) {
				fprintf(stderr, "%s: Invalid slave stack\n", argv[0]);
				fail = 1;
			}
			break;

		case 'p':
			if (process_symbols(argv[0], sysid.peripherals, sizeof sysid.peripherals, optarg)) {
				fprintf(stderr, "%s: Too many peripherals specified (max 16)\n", argv[0]);
				fail = 1;
			}
			break;

		case 'r':
			if (process_symbols(argv[0], sysid.regions, sizeof sysid.regions, optarg)) {
				fprintf(stderr, "%s: Too many regions specified (max 10)\n", argv[0]);
				fail = 1;
			}
			break;

		default:
			fail = 1;
		}
	}

	if (!(seen & FLAG_o)) {
		fprintf(stderr, "%s: Missing required option -o\n", argv[0]);
		fail = 1;
	}
	if (argv[optind]) {
		fprintf(stderr, "%s: Unknown option '%s'\n", argv[0], argv[optind]);
		fail = 1;
	}
	if (fail) fprintf(stderr, "\nUse -h option for help on correct usage.\n");

	return fail;
}

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

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

	ipsize = load_security_code(ipout);
	if (ipsize == -1) goto fail;
	ipout += ipsize;

	for (int i = 0; i < 8; i++) {
		serialize_region_code(ipout, find_symbol(regiondefs, sysid.regions[i]));
		ipout          += 32;
		sysid.bootsize += 32;
	}

	ipsize = load_ip(ipout, sizeof ipbuf - (ipout - ipbuf));
	if (ipsize == -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;
}