File: bin2bios.c

package info (click to toggle)
faumachine 20180503-4
  • links: PTS
  • area: main
  • in suites: buster
  • size: 61,272 kB
  • sloc: ansic: 272,290; makefile: 6,199; asm: 4,251; sh: 3,022; perl: 886; xml: 563; pascal: 311; lex: 214; vhdl: 204
file content (263 lines) | stat: -rw-r--r-- 5,071 bytes parent folder | download
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
/*
 * Copyright (C) 2004-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define BLOCKSIZE	512

const char *progname;
const char *filename;

static long
getnearestpoweroftwo(long n)
{
	long x;
	
	x = 1;
	while (x < n) {
		x <<= 1;
	}
	return x;
}

static void
bin2bios(void)
{
	unsigned char image[0x10 * 0x10000];
	int len;
	int fd;
	int found;
	unsigned char sum;
	int i;
	int j;
	int ret;

	/*
	 * Read file.
	 */
	fd = open(filename, O_RDONLY);
	if (fd < 0) {
		fprintf(stderr, "%s: can't open %s: %s.\n", progname,
				filename, strerror(errno));
		exit(1);
	}

	len = read(fd, image, sizeof(image));
	if (len < 0) {
		fprintf(stderr, "%s: can't read %s: %s.\n", progname,
				filename, strerror(errno));
		exit(1);
	}
	
	memset(image + len, 0, sizeof(image) - len);

	ret = close(fd);
	assert(0 <= ret);

	fprintf(stderr, "%s: %d bytes\n", filename, len);

	/*
	 * Patch image.
	 */
	/* Calculate new image size. */
	len = getnearestpoweroftwo(len);

	/* Patch size byte. */
	image[2] = (len + BLOCKSIZE - 1) / BLOCKSIZE;

	/* Patch "_32_" checksum entry. */
	found = 0;
	for (i = 0; i < len; i += 16) {
		if (image[i + 0x00] == '_'
		 && image[i + 0x01] == '3'
		 && image[i + 0x02] == '2'
		 && image[i + 0x03] == '_'
		 && image[i + 0x08] == 0
		 && image[i + 0x0a] == 0xff
		 && image[i + 0x0b] == 0
		 && image[i + 0x0c] == 0
		 && image[i + 0x0d] == 0
		 && image[i + 0x0e] == 0
		 && image[i + 0x0f] == 0) {
			if (! found) {
				fprintf(stderr, "_32_ structure found at offset 0x%04x.\n", i);
			} else {
				fprintf(stderr, "WARNING: another _32_ structure found at offset 0x%04x.\n", i);
			}

			sum = 0;
			for (j = 0; j < 16; j++) {
				sum += image[i + j];
			}
			image[i + 0xa] -= sum;

			found = 1;
		}
	}

	/* Patch "$PnP" checksum entry. */
	found = 0;
	for (i = 0; i < len; i += 16) {
		if (image[i + 0x00] == '$'
		 && image[i + 0x01] == 'P'
		 && image[i + 0x02] == 'n'
		 && image[i + 0x03] == 'P') {
			uint16_t size;

			size = image[i + 5];

			if (! found) {
				fprintf(stderr, "$PnP structure found at offset 0x%04x (size=0x%x).\n", i, size);
			} else {
				fprintf(stderr, "WARNING: another $PnP structure found at offset 0x%04x.\n", i);
			}

			sum = 0;
			for (j = 0; j < size; j++) {
				sum += image[i + j];
			}
			image[i + 0x8] -= sum;

			found = 1;
		}
	}

	/* Patch "$PIR" checksum entry. */
	found = 0;
	for (i = 0; i < len; i += 16) {
		if (image[i + 0x00] == '$'
		 && image[i + 0x01] == 'P'
		 && image[i + 0x02] == 'I'
		 && image[i + 0x03] == 'R') {
			unsigned short size;

			size = (image[i + 6] << 0) + (image[i + 7] << 8);

			if (! found) {
				fprintf(stderr, "$PIR structure found at offset 0x%04x (size=0x%x).\n", i, size);
			} else {
				fprintf(stderr, "WARNING: another $PIR structure found at offset 0x%04x.\n", i);
			}

			sum = 0;
			for (j = 0; j < size; j++) {
				sum += image[i + j];
			}
			image[i + 0x1f] -= sum;

			found = 1;
		}
	}

	/* Patch "_DMI_" checksum entry. */
	found = 0;
	for (i = 0; i < len; i += 16) {
		if (image[i + 0x00] == '_'
		 && image[i + 0x01] == 'D'
		 && image[i + 0x02] == 'M'
		 && image[i + 0x03] == 'I'
		 && image[i + 0x04] == '_') {
			if (! found) {
				fprintf(stderr, "_DMI_ structure found at offset 0x%04x.\n", i);
			} else {
				fprintf(stderr, "WARNING: another _DMI_ structure found at offset 0x%04x.\n", i);
			}

			sum = 0;
			for (j = 0; j < 15; j++) {
				sum += image[i + j];
			}
			image[i + 0x05] -= sum;
		}
	}

	/* Patch checksum of BIOS. */
	sum = 0;
	for (i = 0; i < len; i++) {
		sum += image[i];
	}
	image[len - 1] -= sum;

	/*
	 * Write file to stdout.
	 */
	ret = write(1, image, len);
	if (ret < 0) {
		fprintf(stderr, "%s: can't write to stdout: %s.\n", progname,
				strerror(errno));
		exit(1);
	}
	if (ret < len) {
		fprintf(stderr, "%s: can't write to stdout: %s.\n", progname,
				"Short write");
		exit(1);
	}
	assert(ret == len);
}

static void
usage(int retval) __attribute__((__noreturn__));
static void
usage(int retval)
{
	fprintf(stderr, "Usage: %s [-p] [-e] filename\n", progname);
	fprintf(stderr, "Options:\n");
	fprintf(stderr, "-e   Do patch the bios entry\n");
	fprintf(stderr, "-p   Do not pad the size of the image\n");
	fprintf(stderr, "     to the nearest power of 2\n");
	exit(retval);
}

int
main(int argc, char **argv)
{
	int c;

	/*
	 * Get program name.
	 */
	progname = *argv;

	/*
	 * Get options.
	 */
	while ((c = getopt(argc, argv, "ep")) != -1) {
		switch (c) {
		default:
			usage(1);
		}
	}
	argv += optind;
	argc -= optind;

	/*
	 * Get parameter.
	 */
	if (0 < argc) {
		filename = *argv;
		argv++;
		argc--;
	} else {
		usage(1);
	}

	if (argc != 0) {
		usage(1);
	}

	bin2bios();

	return 0;
}