summaryrefslogtreecommitdiff
path: root/sys/src/9/xen/xenbin.c
blob: 1f6844e0ea8dec64a4ecd9ea0cce3cbbad413611 (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
/*
 * Transform a Plan 9 386 bootable image to make it compatible with
 * the Xen binary image loader:
 *
 * - pad the beginning of the text with zeroes so that the image can be loaded at
 *    guest 'physical' address 0
 * - insert a Xen header
 * - pad the end of the text so that data segment is page-aligned in the file
 * - adjust the linenumber-pc table so Plan 9 debuggers won't be confused
 */

#include <u.h>
#include <libc.h>
#include <bio.h>
#include <mach.h>

#define PAGE	4096
#define PLAN9HDR	32
#define XENHDR	32
#define	KZERO	0x80000000
#define FLAG_VALID	(1<<16)
#define FLAG_PAE	(1<<14)

void
lput(long n)
{
	char buf[sizeof(long)];
	int i;

	for (i = sizeof(long)-1; i >= 0; i--) {
		buf[i] = n;
		n >>= 8;
	}
	write(1, buf, sizeof(long));
}

void
rput(long n)
{
	char buf[sizeof(long)];
	int i;

	for (i = 0; i < sizeof(long); i++) {
		buf[i] = n;
		n >>= 8;
	}
	write(1, buf, sizeof(long));
}

void
copy(long n)
{
	char buf[PAGE];
	int m;

	while (n > 0) {
		m = sizeof buf;
		if (m > n)
			m = n;
		read(0, buf, m);
		write(1, buf, m);
		n -= m;
	}
}

void pad(int n)
{
	char buf[PAGE];
	int m;

	memset(buf, 0, sizeof buf);
	while (n > 0) {
		m = sizeof buf;
		if (m > n)
			m = n;
		write(1, buf, m);
		n -= m;
	}
}

/*
 * See /sys/src/cmd/8l/span.c:/^asmlc
 */
void adjustlnpc(int v)
{
	char buf[PAGE];
	int n, s;

	n = 0;
	while (v) {
		s = 127;
		if (v < 127)
			s = v;
		buf[n++] = s+128;
		if (n == sizeof buf) {
			write(1, buf, n);
			n = 0;
		}
		v -= s;
	}
	if (n > 0)
		write(1, buf, n);
}

void
main(int argc, char **argv)
{
	Fhdr fhdr;
	long newtxtsz;
	long newentry;
	long newlnpcsz;
	long prepad, postpad;
	long flags;

	flags = FLAG_VALID;
	if (argc > 1 && strcmp(argv[1], "-p") == 0)
		flags |= FLAG_PAE;

	crackhdr(0, &fhdr);

	newtxtsz = ((fhdr.txtsz+PLAN9HDR+PAGE-1)&~(PAGE-1)) - PLAN9HDR;
	newentry = KZERO+PLAN9HDR;
	prepad = fhdr.entry - newentry;
	postpad = newtxtsz - fhdr.txtsz;
	newtxtsz += prepad;
	newlnpcsz = fhdr.lnpcsz;
	if (newlnpcsz)
		newlnpcsz += (prepad+126)/127;

	/* plan 9 header */
	lput(4*11*11+7);		/* magic */
	lput(newtxtsz);			/* sizes */
	lput(fhdr.datsz);
	lput(fhdr.bsssz);
	lput(fhdr.symsz);		/* nsyms */
	lput(newentry);		/* va of entry */
	lput(fhdr.sppcsz);		/* sp offsets */
	lput(newlnpcsz);		/* line offsets */

	/* xen header */
	rput(0x336EC578);	/* magic */
	rput(flags);		/* flags */
	rput(-(0x336EC578+flags));	/* checksum */
	rput(newentry);	/* header_addr */
	rput(KZERO);	/* load_addr */
	rput(KZERO+newtxtsz+fhdr.datsz);	/* load_end_addr */
	rput(KZERO+newtxtsz+fhdr.datsz+fhdr.bsssz);	/* bss_end_addr */
	rput(fhdr.entry);	/* entry_addr */

	pad(prepad-XENHDR);

	seek(0, fhdr.txtoff, 0);
	copy(fhdr.txtsz);
	pad(postpad);
	copy(fhdr.datsz);
	copy(fhdr.symsz);
	if (newlnpcsz) {
		adjustlnpc(prepad);
		copy(fhdr.lnpcsz);
	}
	exits(0);
}