Pārlūkot izejas kodu

Moved NBI support to nbi.c, modified to work with new load mechanism

tags/v0.9.3
Michael Brown 19 gadus atpakaļ
vecāks
revīzija
205a7ad5e0
2 mainītis faili ar 306 papildinājumiem un 201 dzēšanām
  1. 0
    201
      src/arch/i386/core/tagged_loader.c
  2. 306
    0
      src/arch/i386/image/nbi.c

+ 0
- 201
src/arch/i386/core/tagged_loader.c Parādīt failu

@@ -1,201 +0,0 @@
1
-#include "realmode.h"
2
-
3
-struct segheader
4
-{
5
-	unsigned char length;
6
-	unsigned char vendortag;
7
-	unsigned char reserved;
8
-	unsigned char flags;
9
-	unsigned long loadaddr;
10
-	unsigned long imglength;
11
-	unsigned long memlength;
12
-};
13
-
14
-struct imgheader
15
-{
16
-	unsigned long magic;
17
-	unsigned long length;			/* and flags */
18
-	union
19
-	{
20
-		segoff_t segoff;
21
-		unsigned long location;
22
-	} u;
23
-	unsigned long execaddr;
24
-};
25
-
26
-/* Keep all context about loaded image in one place */
27
-static struct tagged_context
28
-{
29
-	struct imgheader	img;			/* copy of header */
30
-	unsigned long		linlocation;		/* addr of header */
31
-	unsigned long		last0, last1;		/* of prev segment */
32
-	unsigned long		segaddr, seglen;	/* of current segment */
33
-	unsigned char		segflags;
34
-	unsigned char		first;
35
-	unsigned long		curaddr;
36
-} tctx;
37
-
38
-#define	TAGGED_PROGRAM_RETURNS	(tctx.img.length & 0x00000100)	/* bit 8 */
39
-#define	LINEAR_EXEC_ADDR	(tctx.img.length & 0x80000000)	/* bit 31 */
40
-
41
-static sector_t tagged_download(unsigned char *data, unsigned int len, int eof);
42
-void xstart16 (unsigned long execaddr, segoff_t location,
43
-	       struct bootpd_t *bootp);
44
-
45
-static inline os_download_t tagged_probe(unsigned char *data, unsigned int len)
46
-{
47
-	struct segheader	*sh;
48
-	unsigned long loc;
49
-	if (*((uint32_t *)data) != 0x1B031336L) {
50
-		return 0;
51
-	}
52
-	printf("(NBI)");
53
-	/* If we don't have enough data give up */
54
-	if (len < 512)
55
-		return dead_download;
56
-	/* Zero all context info */
57
-	memset(&tctx, 0, sizeof(tctx));
58
-	/* Copy first 4 longwords */
59
-	memcpy(&tctx.img, data, sizeof(tctx.img));
60
-	/* Memory location where we are supposed to save it */
61
-	tctx.segaddr = tctx.linlocation = 
62
-		((tctx.img.u.segoff.segment) << 4) + tctx.img.u.segoff.offset;
63
-	if (!prep_segment(tctx.segaddr, tctx.segaddr + 512, tctx.segaddr + 512,
64
-			  0, 512)) {
65
-		return dead_download;
66
-	}
67
-	/* Now verify the segments we are about to load */
68
-	loc = 512;
69
-	for(sh = (struct segheader *)(data
70
-				      + ((tctx.img.length & 0x0F) << 2)
71
-				      + ((tctx.img.length & 0xF0) >> 2) ); 
72
-		(sh->length > 0) && ((unsigned char *)sh < data + 512); 
73
-		sh = (struct segheader *)((unsigned char *)sh
74
-					  + ((sh->length & 0x0f) << 2) + ((sh->length & 0xf0) >> 2)) ) {
75
-		if (!prep_segment(
76
-			sh->loadaddr,
77
-			sh->loadaddr + sh->imglength,
78
-			sh->loadaddr + sh->imglength,
79
-			loc, loc + sh->imglength)) {
80
-			return dead_download;
81
-		}
82
-		loc = loc + sh->imglength;
83
-		if (sh->flags & 0x04) 
84
-			break;
85
-	}
86
-	if (!(sh->flags & 0x04))
87
-		return dead_download;
88
-	/* Grab a copy */
89
-	memcpy(phys_to_virt(tctx.segaddr), data, 512);
90
-	/* Advance to first segment descriptor */
91
-	tctx.segaddr += ((tctx.img.length & 0x0F) << 2)
92
-		+ ((tctx.img.length & 0xF0) >> 2);
93
-	/* Remember to skip the first 512 data bytes */
94
-	tctx.first = 1;
95
-	
96
-	return tagged_download;
97
-
98
-}
99
-static sector_t tagged_download(unsigned char *data, unsigned int len, int eof)
100
-{
101
-	int	i;
102
-
103
-	if (tctx.first) {
104
-		tctx.first = 0;
105
-		if (len > 512) {
106
-			len -= 512;
107
-			data += 512;
108
-			/* and fall through to deal with rest of block */
109
-		} else 
110
-			return 0;
111
-	}
112
-	for (;;) {
113
-		if (len == 0) /* Detect truncated files */
114
-			eof = 0;
115
-		while (tctx.seglen == 0) {
116
-			struct segheader	sh;
117
-			if (tctx.segflags & 0x04) {
118
-				done(1);
119
-				if (LINEAR_EXEC_ADDR) {
120
-					int result;
121
-					/* no gateA20_unset for PM call */
122
-					result = xstart32(tctx.img.execaddr,
123
-						virt_to_phys(&loaderinfo),
124
-						tctx.linlocation,
125
-						virt_to_phys(BOOTP_DATA_ADDR));
126
-					printf("Secondary program returned %d\n",
127
-						result);
128
-					if (!TAGGED_PROGRAM_RETURNS) {
129
-						/* We shouldn't have returned */
130
-						result = -2;
131
-					}
132
-					if (result == 0)
133
-						result = -2;
134
-					longjmp(restart_etherboot, result);
135
-						
136
-				} else {
137
-					gateA20_unset();
138
-					xstart16(tctx.img.execaddr, 
139
-						 tctx.img.u.segoff,
140
-						 BOOTP_DATA_ADDR);
141
-					longjmp(restart_etherboot, -2);
142
-				}
143
-			}
144
-			sh = *((struct segheader *)phys_to_virt(tctx.segaddr));
145
-			tctx.seglen = sh.imglength;
146
-			if ((tctx.segflags = sh.flags & 0x03) == 0)
147
-				tctx.curaddr = sh.loadaddr;
148
-			else if (tctx.segflags == 0x01)
149
-				tctx.curaddr = tctx.last1 + sh.loadaddr;
150
-			else if (tctx.segflags == 0x02)
151
-				tctx.curaddr = (Address)(meminfo.memsize * 1024L
152
-						    + 0x100000L)
153
-					- sh.loadaddr;
154
-			else
155
-				tctx.curaddr = tctx.last0 - sh.loadaddr;
156
-			tctx.last1 = (tctx.last0 = tctx.curaddr) + sh.memlength;
157
-			tctx.segflags = sh.flags;
158
-			tctx.segaddr += ((sh.length & 0x0F) << 2)
159
-				+ ((sh.length & 0xF0) >> 2);
160
-			/* Avoid lock-up */
161
-			if ( sh.length == 0 ) longjmp(restart_etherboot, -2); 
162
-		}
163
-		if ((len <= 0) && !eof)
164
-			break;
165
-		i = (tctx.seglen > len) ? len : tctx.seglen;
166
-		memcpy(phys_to_virt(tctx.curaddr), data, i);
167
-		tctx.seglen -= i;
168
-		tctx.curaddr += i;
169
-		len -= i;
170
-		data += i;
171
-	} 
172
-	return 0;
173
-}
174
-
175
-void xstart16 (unsigned long execaddr, segoff_t location,
176
-	       struct bootpd_t *bootp) {
177
-	uint16_t basemem_bootp;
178
-	int discard_D, discard_S, discard_b;
179
-
180
-	/* AFAICT, execaddr is actually already a segment:offset */
181
-	basemem_bootp = BASEMEM_PARAMETER_INIT ( *bootp );
182
-	REAL_EXEC ( rm_xstart16,
183
-		    "pushw %%ds\n\t"	/* far pointer to bootp data copy */
184
-		    "pushw %%bx\n\t"
185
-		    "pushl %%esi\n\t"	/* location */
186
-		    "pushw %%cs\n\t"	/* lcall execaddr */
187
-		    "call 1f\n\t"
188
-		    "jmp 2f\n\t"
189
-		    "\n1:\n\t"
190
-		    "pushl %%edi\n\t"
191
-		    "lret\n\t"
192
-		    "\n2:\n\t"
193
-		    "addw $8,%%sp\n\t",	/* pop location and bootp ptr */
194
-		    3,
195
-		    OUT_CONSTRAINTS ( "=D" ( discard_D ), "=S" ( discard_S ),
196
-				      "=b" ( discard_b ) ),
197
-		    IN_CONSTRAINTS ( "D" ( execaddr ), "S" ( location ),
198
-				     "b" ( basemem_bootp ) ),
199
-		    CLOBBER ( "eax", "ecx", "edx", "ebp" ) );
200
-	BASEMEM_PARAMETER_DONE ( *bootp );
201
-}

+ 306
- 0
src/arch/i386/image/nbi.c Parādīt failu

@@ -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
+};

Notiek ielāde…
Atcelt
Saglabāt