|
@@ -0,0 +1,306 @@
|
|
1
|
+#include "image.h"
|
|
2
|
+#include "memsizes.h"
|
|
3
|
+#include "realmode.h"
|
|
4
|
+#include "gateA20.h"
|
|
5
|
+#include "osloader.h"
|
|
6
|
+#include "etherboot.h"
|
|
7
|
+
|
|
8
|
+/* An NBI image header */
|
|
9
|
+struct imgheader {
|
|
10
|
+ unsigned long magic;
|
|
11
|
+ union {
|
|
12
|
+ unsigned char length;
|
|
13
|
+ unsigned long flags;
|
|
14
|
+ };
|
|
15
|
+ segoff_t location;
|
|
16
|
+ union {
|
|
17
|
+ segoff_t segoff;
|
|
18
|
+ unsigned long linear;
|
|
19
|
+ } execaddr;
|
|
20
|
+} __attribute__ (( packed ));
|
|
21
|
+
|
|
22
|
+/* NBI magic number */
|
|
23
|
+#define NBI_MAGIC 0x1B031336UL
|
|
24
|
+
|
|
25
|
+/* Interpretation of the "length" fields */
|
|
26
|
+#define NBI_NONVENDOR_LENGTH(len) ( ( (len) & 0x0f ) << 2 )
|
|
27
|
+#define NBI_VENDOR_LENGTH(len) ( ( (len) & 0xf0 ) >> 2 )
|
|
28
|
+#define NBI_LENGTH(len) ( NBI_NONVENDOR_LENGTH(len) + NBI_VENDOR_LENGTH(len) )
|
|
29
|
+
|
|
30
|
+/* Interpretation of the "flags" fields */
|
|
31
|
+#define NBI_PROGRAM_RETURNS(flags) ( (flags) & ( 1 << 8 ) )
|
|
32
|
+#define NBI_LINEAR_EXEC_ADDR(flags) ( (flags) & ( 1 << 31 ) )
|
|
33
|
+
|
|
34
|
+/* NBI header length */
|
|
35
|
+#define NBI_HEADER_LENGTH 512
|
|
36
|
+
|
|
37
|
+/* An NBI segment header */
|
|
38
|
+struct segheader {
|
|
39
|
+ unsigned char length;
|
|
40
|
+ unsigned char vendortag;
|
|
41
|
+ unsigned char reserved;
|
|
42
|
+ unsigned char flags;
|
|
43
|
+ unsigned long loadaddr;
|
|
44
|
+ unsigned long imglength;
|
|
45
|
+ unsigned long memlength;
|
|
46
|
+};
|
|
47
|
+
|
|
48
|
+/* Interpretation of the "flags" fields */
|
|
49
|
+#define NBI_LOADADDR_FLAGS(flags) ( (flags) & 0x03 )
|
|
50
|
+#define NBI_LOADADDR_ABS 0x00
|
|
51
|
+#define NBI_LOADADDR_AFTER 0x01
|
|
52
|
+#define NBI_LOADADDR_END 0x02
|
|
53
|
+#define NBI_LOADADDR_BEFORE 0x03
|
|
54
|
+#define NBI_LAST_SEGHEADER(flags) ( (flags) & ( 1 << 2 ) )
|
|
55
|
+
|
|
56
|
+/* Info passed to NBI image */
|
|
57
|
+static struct ebinfo loaderinfo = {
|
|
58
|
+ VERSION_MAJOR, VERSION_MINOR,
|
|
59
|
+ 0
|
|
60
|
+};
|
|
61
|
+
|
|
62
|
+/*
|
|
63
|
+ * Determine whether or not this is a valid NBI image
|
|
64
|
+ *
|
|
65
|
+ */
|
|
66
|
+static int nbi_probe ( physaddr_t start, off_t len, void **context ) {
|
|
67
|
+ static struct imgheader imgheader;
|
|
68
|
+
|
|
69
|
+ if ( (unsigned)len < sizeof ( imgheader ) ) {
|
|
70
|
+ DBG ( "NBI image too small\n" );
|
|
71
|
+ return 0;
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ copy_from_phys ( &imgheader, start, sizeof ( imgheader ) );
|
|
75
|
+
|
|
76
|
+ if ( imgheader.magic != NBI_MAGIC )
|
|
77
|
+ return 0;
|
|
78
|
+
|
|
79
|
+ /* Record image context */
|
|
80
|
+ DBG ( "NBI found valid image\n" );
|
|
81
|
+ *context = &imgheader;
|
|
82
|
+ return 1;
|
|
83
|
+}
|
|
84
|
+
|
|
85
|
+/*
|
|
86
|
+ * Prepare a segment for an NBI image
|
|
87
|
+ *
|
|
88
|
+ */
|
|
89
|
+static int nbi_prepare_segment ( physaddr_t dest, off_t imglen, off_t memlen,
|
|
90
|
+ physaddr_t src __unused ) {
|
|
91
|
+ DBG ( "NBI preparing segment [%x,%x) (imglen %d memlen %d)\n",
|
|
92
|
+ dest, dest + memlen, imglen, memlen );
|
|
93
|
+ return prep_segment ( dest, dest + imglen, dest + memlen );
|
|
94
|
+}
|
|
95
|
+
|
|
96
|
+/*
|
|
97
|
+ * Load a segment for an NBI image
|
|
98
|
+ *
|
|
99
|
+ */
|
|
100
|
+static int nbi_load_segment ( physaddr_t dest, off_t imglen,
|
|
101
|
+ off_t memlen __unused, physaddr_t src ) {
|
|
102
|
+ DBG ( "NBI loading segment [%x,%x)\n", dest, dest + imglen );
|
|
103
|
+ copy_phys_to_phys ( dest, src, imglen );
|
|
104
|
+ return 1;
|
|
105
|
+}
|
|
106
|
+
|
|
107
|
+/*
|
|
108
|
+ * Process segments of an NBI image
|
|
109
|
+ *
|
|
110
|
+ */
|
|
111
|
+static int nbi_process_segments ( physaddr_t start, off_t len,
|
|
112
|
+ struct imgheader *imgheader,
|
|
113
|
+ int ( * process ) ( physaddr_t dest,
|
|
114
|
+ off_t imglen,
|
|
115
|
+ off_t memlen,
|
|
116
|
+ physaddr_t src ) ) {
|
|
117
|
+ struct segheader sh;
|
|
118
|
+ off_t offset = 0;
|
|
119
|
+ off_t sh_off;
|
|
120
|
+ physaddr_t dest;
|
|
121
|
+ off_t dest_imglen, dest_memlen;
|
|
122
|
+
|
|
123
|
+ /* Copy header to target location */
|
|
124
|
+ dest = ( ( imgheader->location.segment << 4 ) +
|
|
125
|
+ imgheader->location.offset );
|
|
126
|
+ dest_imglen = dest_memlen = NBI_HEADER_LENGTH;
|
|
127
|
+ if ( ! process ( dest, dest_imglen, dest_memlen, start + offset ) )
|
|
128
|
+ return 0;
|
|
129
|
+ offset += dest_imglen;
|
|
130
|
+
|
|
131
|
+ /* Process segments in turn */
|
|
132
|
+ sh_off = NBI_LENGTH ( imgheader->length );
|
|
133
|
+ do {
|
|
134
|
+ /* Read segment header */
|
|
135
|
+ copy_from_phys ( &sh, start + sh_off, sizeof ( sh ) );
|
|
136
|
+ if ( sh.length == 0 ) {
|
|
137
|
+ /* Avoid infinite loop? */
|
|
138
|
+ DBG ( "NBI invalid segheader length 0\n" );
|
|
139
|
+ return 0;
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+ /* Calculate segment load address */
|
|
143
|
+ switch ( NBI_LOADADDR_FLAGS ( sh.flags ) ) {
|
|
144
|
+ case NBI_LOADADDR_ABS:
|
|
145
|
+ dest = sh.loadaddr;
|
|
146
|
+ break;
|
|
147
|
+ case NBI_LOADADDR_AFTER:
|
|
148
|
+ dest = dest + dest_memlen + sh.loadaddr;
|
|
149
|
+ break;
|
|
150
|
+ case NBI_LOADADDR_BEFORE:
|
|
151
|
+ dest = dest - sh.loadaddr;
|
|
152
|
+ break;
|
|
153
|
+ case NBI_LOADADDR_END:
|
|
154
|
+ /* Not correct according to the spec, but
|
|
155
|
+ * maintains backwards compatibility with
|
|
156
|
+ * previous versions of Etherboot.
|
|
157
|
+ */
|
|
158
|
+ dest = ( meminfo.memsize * 1024 + 0x100000UL )
|
|
159
|
+ - sh.loadaddr;
|
|
160
|
+ break;
|
|
161
|
+ default:
|
|
162
|
+ DBG ( "NBI can't count up to three\n" );
|
|
163
|
+ return 0;
|
|
164
|
+ }
|
|
165
|
+
|
|
166
|
+ /* Process this segment */
|
|
167
|
+ dest_imglen = sh.imglength;
|
|
168
|
+ dest_memlen = sh.memlength;
|
|
169
|
+ if ( ! process ( dest, dest_imglen, dest_memlen,
|
|
170
|
+ start + offset ) )
|
|
171
|
+ return 0;
|
|
172
|
+ offset += dest_imglen;
|
|
173
|
+
|
|
174
|
+ /* Next segheader */
|
|
175
|
+ sh_off += NBI_LENGTH ( sh.length );
|
|
176
|
+ if ( sh_off >= NBI_HEADER_LENGTH ) {
|
|
177
|
+ DBG ( "NBI header overflow\n" );
|
|
178
|
+ return 0;
|
|
179
|
+ }
|
|
180
|
+
|
|
181
|
+ } while ( ! NBI_LAST_SEGHEADER ( sh.flags ) );
|
|
182
|
+
|
|
183
|
+ if ( offset != len ) {
|
|
184
|
+ DBG ( "NBI length mismatch (file %d, metadata %d)\n",
|
|
185
|
+ len, offset );
|
|
186
|
+ return 0;
|
|
187
|
+ }
|
|
188
|
+
|
|
189
|
+ return 1;
|
|
190
|
+}
|
|
191
|
+
|
|
192
|
+/*
|
|
193
|
+ * Load an NBI image into memory
|
|
194
|
+ *
|
|
195
|
+ */
|
|
196
|
+static int nbi_load ( physaddr_t start, off_t len, void *context ) {
|
|
197
|
+ struct imgheader *imgheader = context;
|
|
198
|
+
|
|
199
|
+ /* If we don't have enough data give up */
|
|
200
|
+ if ( len < NBI_HEADER_LENGTH )
|
|
201
|
+ return 0;
|
|
202
|
+
|
|
203
|
+ DBG ( "NBI placing header at %hx:%hx\n",
|
|
204
|
+ imgheader->location.segment, imgheader->location.offset );
|
|
205
|
+
|
|
206
|
+ /* NBI files can have overlaps between segments; the bss of
|
|
207
|
+ * one segment may overlap the initialised data of another. I
|
|
208
|
+ * assume this is a design flaw, but there are images out
|
|
209
|
+ * there that we need to work with. We therefore do two
|
|
210
|
+ * passes: first to initialise the segments, then to copy the
|
|
211
|
+ * data. This avoids zeroing out already-copied data.
|
|
212
|
+ */
|
|
213
|
+ if ( ! nbi_process_segments ( start, len, imgheader,
|
|
214
|
+ nbi_prepare_segment ) )
|
|
215
|
+ return 0;
|
|
216
|
+ if ( ! nbi_process_segments ( start, len, imgheader,
|
|
217
|
+ nbi_load_segment ) )
|
|
218
|
+ return 0;
|
|
219
|
+
|
|
220
|
+ return 1;
|
|
221
|
+}
|
|
222
|
+
|
|
223
|
+/*
|
|
224
|
+ * Boot a 16-bit NBI image
|
|
225
|
+ *
|
|
226
|
+ */
|
|
227
|
+static int nbi_boot16 ( struct imgheader *imgheader ) {
|
|
228
|
+ uint16_t basemem_bootp;
|
|
229
|
+ int discard_D, discard_S, discard_b;
|
|
230
|
+
|
|
231
|
+ DBG ( "NBI executing 16-bit image at %hx:%hx\n",
|
|
232
|
+ imgheader->execaddr.segoff.segment,
|
|
233
|
+ imgheader->execaddr.segoff.offset );
|
|
234
|
+
|
|
235
|
+ gateA20_unset();
|
|
236
|
+
|
|
237
|
+ basemem_bootp = BASEMEM_PARAMETER_INIT ( bootp_data );
|
|
238
|
+ REAL_EXEC ( rm_xstart16,
|
|
239
|
+ "pushw %%ds\n\t" /* far pointer to bootp data copy */
|
|
240
|
+ "pushw %%bx\n\t"
|
|
241
|
+ "pushl %%esi\n\t" /* location */
|
|
242
|
+ "pushw %%cs\n\t" /* lcall execaddr */
|
|
243
|
+ "call 1f\n\t"
|
|
244
|
+ "jmp 2f\n\t"
|
|
245
|
+ "\n1:\n\t"
|
|
246
|
+ "pushl %%edi\n\t"
|
|
247
|
+ "lret\n\t"
|
|
248
|
+ "\n2:\n\t"
|
|
249
|
+ "addw $8,%%sp\n\t", /* pop location and bootp ptr */
|
|
250
|
+ 3,
|
|
251
|
+ OUT_CONSTRAINTS ( "=D" ( discard_D ), "=S" ( discard_S ),
|
|
252
|
+ "=b" ( discard_b ) ),
|
|
253
|
+ IN_CONSTRAINTS ( "D" ( imgheader->execaddr.segoff ),
|
|
254
|
+ "S" ( imgheader->location ),
|
|
255
|
+ "b" ( basemem_bootp ) ),
|
|
256
|
+ CLOBBER ( "eax", "ecx", "edx", "ebp" ) );
|
|
257
|
+ BASEMEM_PARAMETER_DONE ( bootp_data );
|
|
258
|
+
|
|
259
|
+ return 0;
|
|
260
|
+}
|
|
261
|
+
|
|
262
|
+/*
|
|
263
|
+ * Boot a 32-bit NBI image
|
|
264
|
+ *
|
|
265
|
+ */
|
|
266
|
+static int nbi_boot32 ( struct imgheader *imgheader ) {
|
|
267
|
+ int rc = 0;
|
|
268
|
+
|
|
269
|
+ DBG ( "NBI executing 32-bit image at %x\n",
|
|
270
|
+ imgheader->execaddr.linear );
|
|
271
|
+
|
|
272
|
+ /* no gateA20_unset for PM call */
|
|
273
|
+ rc = xstart32 ( imgheader->execaddr.linear,
|
|
274
|
+ virt_to_phys ( &loaderinfo ),
|
|
275
|
+ ( ( imgheader->location.segment << 4 ) +
|
|
276
|
+ imgheader->location.offset ),
|
|
277
|
+ virt_to_phys ( &bootp_data ) );
|
|
278
|
+ printf ( "Secondary program returned %d\n", rc );
|
|
279
|
+ if ( ! NBI_PROGRAM_RETURNS ( imgheader->flags ) ) {
|
|
280
|
+ /* We shouldn't have returned */
|
|
281
|
+ rc = 0;
|
|
282
|
+ }
|
|
283
|
+
|
|
284
|
+ return rc;
|
|
285
|
+}
|
|
286
|
+
|
|
287
|
+/*
|
|
288
|
+ * Boot a loaded NBI image
|
|
289
|
+ *
|
|
290
|
+ */
|
|
291
|
+static int nbi_boot ( void *context ) {
|
|
292
|
+ struct imgheader *imgheader = context;
|
|
293
|
+
|
|
294
|
+ if ( NBI_LINEAR_EXEC_ADDR ( imgheader->flags ) ) {
|
|
295
|
+ return nbi_boot32 ( imgheader );
|
|
296
|
+ } else {
|
|
297
|
+ return nbi_boot16 ( imgheader );
|
|
298
|
+ }
|
|
299
|
+}
|
|
300
|
+
|
|
301
|
+static struct image nbi_image __image = {
|
|
302
|
+ .name = "NBI",
|
|
303
|
+ .probe = nbi_probe,
|
|
304
|
+ .load = nbi_load,
|
|
305
|
+ .boot = nbi_boot,
|
|
306
|
+};
|