Parcourir la source

[comboot] Add COMBOOT and COM32 support

tags/v0.9.4
Daniel Verkamp il y a 15 ans
Parent
révision
e8b22f203f

+ 1
- 0
src/arch/i386/Makefile Voir le fichier

@@ -11,6 +11,7 @@ SRCDIRS		+= arch/i386/drivers
11 11
 SRCDIRS		+= arch/i386/drivers/net
12 12
 SRCDIRS		+= arch/i386/interface/pcbios
13 13
 SRCDIRS		+= arch/i386/interface/pxe
14
+SRCDIRS 	+= arch/i386/interface/syslinux
14 15
 
15 16
 # The various xxx_loader.c files are #included into core/loader.c and
16 17
 # should not be compiled directly.

+ 275
- 0
src/arch/i386/image/com32.c Voir le fichier

@@ -0,0 +1,275 @@
1
+/*
2
+ * Copyright (C) 2008 Daniel Verkamp <daniel@drv.nu>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+/**
20
+ * @file
21
+ *
22
+ * SYSLINUX COM32 image format
23
+ *
24
+ */
25
+
26
+#include <stdint.h>
27
+#include <stdlib.h>
28
+#include <string.h>
29
+#include <strings.h>
30
+#include <errno.h>
31
+#include <assert.h>
32
+#include <realmode.h>
33
+#include <basemem.h>
34
+#include <comboot.h>
35
+#include <gpxe/uaccess.h>
36
+#include <gpxe/image.h>
37
+#include <gpxe/segment.h>
38
+#include <gpxe/init.h>
39
+#include <gpxe/memmap.h>
40
+
41
+struct image_type com32_image_type __image_type ( PROBE_NORMAL );
42
+
43
+/**
44
+ * Execute COMBOOT image
45
+ *
46
+ * @v image		COM32 image
47
+ * @ret rc		Return status code
48
+ */
49
+static int com32_exec ( struct image *image ) {
50
+	struct memory_map memmap;
51
+	unsigned int i;
52
+	int state;
53
+	uint32_t avail_mem_top;
54
+
55
+	state = setjmp ( comboot_return );
56
+
57
+	switch ( state ) {
58
+	case 0: /* First time through; invoke COM32 program */
59
+
60
+		/* Get memory map */
61
+		get_memmap ( &memmap );
62
+
63
+		/* Find end of block covering COM32 image loading area */
64
+		for ( i = 0, avail_mem_top = 0 ; i < memmap.count ; i++ ) {
65
+			if ( (memmap.regions[i].start <= COM32_START_PHYS) &&
66
+			     (memmap.regions[i].end > COM32_START_PHYS + image->len) ) {
67
+				avail_mem_top = memmap.regions[i].end;
68
+				break;
69
+			}
70
+		}
71
+
72
+		DBGC ( image, "COM32 %p: available memory top = 0x%x\n",
73
+			 image, (int)avail_mem_top );
74
+
75
+		assert ( avail_mem_top != 0 );
76
+
77
+		com32_external_esp = phys_to_virt ( avail_mem_top );
78
+
79
+		/* Hook COMBOOT API interrupts */
80
+		hook_comboot_interrupts( );
81
+
82
+		/* Temporarily de-register image, so that a "boot" command
83
+		 * doesn't throw us into an execution loop.  Hold a reference
84
+		 * to avoid the image's being freed.
85
+		 */
86
+		image_get ( image );
87
+		unregister_image ( image );
88
+
89
+		__asm__ __volatile__ (
90
+			"movl %%esp, (com32_internal_esp)\n\t" /* Save internal virtual address space ESP */
91
+			"movl (com32_external_esp), %%esp\n\t" /* Switch to COM32 ESP (top of available memory) */
92
+			"call _virt_to_phys\n\t"               /* Switch to flat physical address space */
93
+			"pushl %0\n\t"                         /* Pointer to CDECL helper function */
94
+			"pushl %1\n\t"                         /* Pointer to FAR call helper function */
95
+			"pushl %2\n\t"                         /* Size of low memory bounce buffer */
96
+			"pushl %3\n\t"                         /* Pointer to low memory bounce buffer */
97
+			"pushl %4\n\t"                         /* Pointer to INT call helper function */
98
+			"pushl %5\n\t"                         /* Pointer to the command line arguments */
99
+			"pushl $6\n\t"                         /* Number of additional arguments */
100
+			"call *%6\n\t"                         /* Execute image */
101
+			"call _phys_to_virt\n\t"               /* Switch back to internal virtual address space */
102
+			"movl (com32_internal_esp), %%esp\n\t" /* Switch back to internal stack */
103
+		:
104
+		:
105
+			/* %0 */ "r" ( virt_to_phys ( com32_cfarcall_wrapper ) ),
106
+			/* %1 */ "r" ( virt_to_phys ( com32_farcall_wrapper ) ),
107
+			/* %2 */ "r" ( get_fbms() * 1024 - (COM32_BOUNCE_SEG << 4) ),
108
+			/* %3 */ "i" ( COM32_BOUNCE_SEG << 4 ),
109
+			/* %4 */ "r" ( virt_to_phys ( com32_intcall_wrapper ) ),
110
+			/* %5 */ "r" ( virt_to_phys ( image->cmdline ) ),
111
+			/* %6 */ "r" ( COM32_START_PHYS )
112
+		:
113
+			"memory" );
114
+		break;
115
+
116
+	case COMBOOT_RETURN_RUN_KERNEL:
117
+		DBGC ( image, "COM32 %p: returned to run kernel...\n", image );
118
+		comboot_run_kernel ( );
119
+		break;
120
+
121
+	case COMBOOT_RETURN_EXIT:
122
+		break;
123
+
124
+	}
125
+
126
+	comboot_force_text_mode ( );
127
+
128
+	DBGC ( image, "COM32 %p returned\n", image );
129
+
130
+	/* Re-register image and return */
131
+	register_image ( image );
132
+	image_put ( image );
133
+
134
+	return 0;
135
+}
136
+
137
+/**
138
+ * Check image name extension
139
+ * 
140
+ * @v image		COM32 image
141
+ * @ret rc		Return status code
142
+ */
143
+static int com32_identify ( struct image *image ) {
144
+	const char *ext;
145
+	static const uint8_t magic[] = { 0xB8, 0xFF, 0x4C, 0xCD, 0x21 };
146
+	uint8_t buf[5];
147
+	
148
+	if ( image->len >= 5 ) {
149
+		/* Check for magic number
150
+		 * mov eax,21cd4cffh
151
+		 * B8 FF 4C CD 21
152
+		 */
153
+		copy_from_user ( buf, image->data, 0, sizeof(buf) );
154
+		if ( ! memcmp ( buf, magic, sizeof(buf) ) ) {
155
+			DBGC ( image, "COM32 %p: found magic number\n",
156
+			       image );
157
+			return 0;
158
+		}
159
+	}
160
+
161
+	/* Magic number not found; check filename extension */
162
+
163
+	ext = strrchr( image->name, '.' );
164
+
165
+	if ( ! ext ) {
166
+		DBGC ( image, "COM32 %p: no extension\n",
167
+		       image );
168
+		return -ENOEXEC;
169
+	}
170
+
171
+	++ext;
172
+
173
+	if ( strcasecmp( ext, "c32" ) ) {
174
+		DBGC ( image, "COM32 %p: unrecognized extension %s\n",
175
+		       image, ext );
176
+		return -ENOEXEC;
177
+	}
178
+
179
+	return 0;
180
+}
181
+
182
+
183
+/**
184
+ * Load COM32 image into memory
185
+ * @v image		COM32 image
186
+ * @ret rc		Return status code
187
+ */
188
+static int comboot_load_image ( struct image *image ) {
189
+	size_t filesz, memsz;
190
+	userptr_t buffer;
191
+	int rc;
192
+
193
+	filesz = image->len;
194
+	memsz = filesz;
195
+	buffer = phys_to_user ( COM32_START_PHYS );
196
+	if ( ( rc = prep_segment ( buffer, filesz, memsz ) ) != 0 ) {
197
+		DBGC ( image, "COM32 %p: could not prepare segment: %s\n",
198
+		       image, strerror ( rc ) );
199
+		return rc;
200
+	}
201
+
202
+	/* Copy image to segment */
203
+	memcpy_user ( buffer, 0, image->data, 0, filesz );
204
+
205
+	return 0;
206
+}
207
+
208
+/**
209
+ * Prepare COM32 low memory bounce buffer
210
+ * @v image		COM32 image
211
+ * @ret rc		Return status code
212
+ */
213
+static int comboot_prepare_bounce_buffer ( struct image * image ) {
214
+	unsigned int seg;
215
+	userptr_t seg_userptr;
216
+	size_t filesz, memsz;
217
+	int rc;
218
+
219
+	seg = COM32_BOUNCE_SEG;
220
+	seg_userptr = real_to_user ( seg, 0 );
221
+
222
+	/* Ensure the entire 64k segment is free */
223
+	memsz = 0xFFFF;
224
+	filesz = 0;
225
+
226
+	/* Prepare, verify, and load the real-mode segment */
227
+	if ( ( rc = prep_segment ( seg_userptr, filesz, memsz ) ) != 0 ) {
228
+		DBGC ( image, "COM32 %p: could not prepare bounce buffer segment: %s\n",
229
+		       image, strerror ( rc ) );
230
+		return rc;
231
+	}
232
+
233
+	return 0;
234
+}
235
+
236
+/**
237
+ * Load COM32 image into memory
238
+ *
239
+ * @v image		COM32 image
240
+ * @ret rc		Return status code
241
+ */
242
+static int com32_load ( struct image *image ) {
243
+	int rc;
244
+
245
+	DBGC ( image, "COM32 %p: name '%s', cmdline '%s'\n",
246
+	       image, image->name, image->cmdline );
247
+
248
+	/* Check if this is a COMBOOT image */
249
+	if ( ( rc = com32_identify ( image ) ) != 0 ) {
250
+		return rc;
251
+	}
252
+
253
+	/* This is a COM32 image, valid or otherwise */
254
+	if ( ! image->type )
255
+		image->type = &com32_image_type;
256
+
257
+	/* Load image */
258
+	if ( ( rc = comboot_load_image ( image ) ) != 0 ) {
259
+		return rc;
260
+	}
261
+
262
+	/* Prepare bounce buffer segment */
263
+	if ( ( rc = comboot_prepare_bounce_buffer ( image ) ) != 0 ) {
264
+		return rc;
265
+	}
266
+
267
+	return 0;
268
+}
269
+
270
+/** SYSLINUX COM32 image type */
271
+struct image_type com32_image_type __image_type ( PROBE_NORMAL ) = {
272
+	.name = "COM32",
273
+	.load = com32_load,
274
+	.exec = com32_exec,
275
+};

+ 312
- 0
src/arch/i386/image/comboot.c Voir le fichier

@@ -0,0 +1,312 @@
1
+/*
2
+ * Copyright (C) 2008 Daniel Verkamp <daniel@drv.nu>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+/**
20
+ * @file
21
+ *
22
+ * SYSLINUX COMBOOT (16-bit) image format
23
+ *
24
+ */
25
+
26
+#include <stdint.h>
27
+#include <stdlib.h>
28
+#include <string.h>
29
+#include <strings.h>
30
+#include <errno.h>
31
+#include <assert.h>
32
+#include <realmode.h>
33
+#include <basemem.h>
34
+#include <comboot.h>
35
+#include <gpxe/uaccess.h>
36
+#include <gpxe/image.h>
37
+#include <gpxe/segment.h>
38
+#include <gpxe/init.h>
39
+#include <gpxe/features.h>
40
+
41
+FEATURE ( FEATURE_IMAGE, "COMBOOT", DHCP_EB_FEATURE_COMBOOT, 1 );
42
+
43
+struct image_type comboot_image_type __image_type ( PROBE_NORMAL );
44
+
45
+/**
46
+ * COMBOOT PSP, copied to offset 0 of code segment
47
+ */
48
+struct comboot_psp {
49
+	/** INT 20 instruction, executed if COMBOOT image returns with RET */
50
+	uint16_t int20;
51
+	/** Segment of first non-free paragraph of memory */
52
+	uint16_t first_non_free_para;
53
+};
54
+
55
+/** Offset in PSP of command line */
56
+#define COMBOOT_PSP_CMDLINE_OFFSET 0x81
57
+
58
+/** Maximum length of command line in PSP
59
+ * (127 bytes minus space and CR) */
60
+#define COMBOOT_MAX_CMDLINE_LEN    125
61
+
62
+
63
+/**
64
+ * Copy command line to PSP
65
+ * 
66
+ * @v image		COMBOOT image
67
+ */
68
+static void comboot_copy_cmdline ( struct image * image, userptr_t seg_userptr ) {
69
+	const char *cmdline = ( image->cmdline ? image->cmdline : "" );
70
+	int cmdline_len = strlen ( cmdline );
71
+	if( cmdline_len > COMBOOT_MAX_CMDLINE_LEN )
72
+		cmdline_len = COMBOOT_MAX_CMDLINE_LEN;
73
+	uint8_t len_byte = cmdline_len;
74
+	char spc = ' ', cr = '\r';
75
+
76
+	/* Copy length to byte before command line */
77
+	copy_to_user ( seg_userptr, COMBOOT_PSP_CMDLINE_OFFSET - 1,
78
+	               &len_byte, 1 );
79
+
80
+	/* Command line starts with space */
81
+	copy_to_user ( seg_userptr,
82
+	               COMBOOT_PSP_CMDLINE_OFFSET,
83
+	               &spc, 1 );
84
+
85
+	/* Copy command line */
86
+	copy_to_user ( seg_userptr,
87
+	               COMBOOT_PSP_CMDLINE_OFFSET + 1,
88
+	               cmdline, cmdline_len );
89
+
90
+	/* Command line ends with CR */
91
+	copy_to_user ( seg_userptr,
92
+	               COMBOOT_PSP_CMDLINE_OFFSET + cmdline_len + 1,
93
+	               &cr, 1 );
94
+}
95
+
96
+/**
97
+ * Initialize PSP
98
+ * 
99
+ * @v image		COMBOOT image
100
+ * @v seg_userptr	segment to initialize
101
+ */
102
+static void comboot_init_psp ( struct image * image, userptr_t seg_userptr ) {
103
+	struct comboot_psp psp;
104
+
105
+	/* Fill PSP */
106
+
107
+	/* INT 20h instruction, byte order reversed */
108
+	psp.int20 = 0x20CD;
109
+
110
+	/* get_fbms() returns BIOS free base memory counter, which is in
111
+	 * kilobytes; x * 1024 / 16 == x * 64 == x << 6 */
112
+	psp.first_non_free_para = get_fbms() << 6;
113
+
114
+	DBGC ( image, "COMBOOT %p: first non-free paragraph = 0x%x\n",
115
+	       image, psp.first_non_free_para );
116
+
117
+	/* Copy the PSP to offset 0 of segment.
118
+	 * The rest of the PSP was already zeroed by
119
+	 * comboot_prepare_segment. */
120
+	copy_to_user ( seg_userptr, 0, &psp, sizeof( psp ) );
121
+
122
+	/* Copy the command line to the PSP */
123
+	comboot_copy_cmdline ( image, seg_userptr );
124
+}
125
+
126
+/**
127
+ * Execute COMBOOT image
128
+ *
129
+ * @v image		COMBOOT image
130
+ * @ret rc		Return status code
131
+ */
132
+static int comboot_exec ( struct image *image ) {
133
+	userptr_t seg_userptr = real_to_user ( COMBOOT_PSP_SEG, 0 );
134
+	int state;
135
+
136
+	state = setjmp ( comboot_return );
137
+
138
+	switch ( state ) {
139
+	case 0: /* First time through; invoke COMBOOT program */
140
+
141
+		/* Initialize PSP */
142
+		comboot_init_psp ( image, seg_userptr );
143
+
144
+		/* Hook COMBOOT API interrupts */
145
+		hook_comboot_interrupts ( );
146
+
147
+		DBGC ( image, "executing 16-bit COMBOOT image at %4x:0100\n",
148
+			 COMBOOT_PSP_SEG );
149
+
150
+		/* Temporarily de-register image, so that a "boot" command
151
+		 * doesn't throw us into an execution loop.  Hold a reference
152
+		 * to avoid the image's being freed.
153
+		 */
154
+		image_get ( image );
155
+		unregister_image ( image );
156
+
157
+		/* Store stack segment at 0x38 and stack pointer at 0x3A
158
+		 * in the PSP and jump to the image */
159
+		__asm__ __volatile__ (
160
+		    REAL_CODE ( /* Save return address with segment on old stack */
161
+				    "popw %%ax\n\t"
162
+				    "pushw %%cs\n\t"
163
+				    "pushw %%ax\n\t"
164
+				    /* Set DS=ES=segment with image */
165
+				    "movw %w0, %%ds\n\t"
166
+				    "movw %w0, %%es\n\t"
167
+				    /* Set SS:SP to new stack (end of image segment) */
168
+				    "movw %w0, %%ss\n\t"
169
+				    "xor %%sp, %%sp\n\t"
170
+				    "pushw $0\n\t"
171
+				    "pushw %w0\n\t"
172
+				    "pushw $0x100\n\t"
173
+				    /* Zero registers (some COM files assume GP regs are 0) */
174
+				    "xorw %%ax, %%ax\n\t"
175
+				    "xorw %%bx, %%bx\n\t"
176
+				    "xorw %%cx, %%cx\n\t"
177
+				    "xorw %%dx, %%dx\n\t"
178
+				    "xorw %%si, %%si\n\t"
179
+				    "xorw %%di, %%di\n\t"
180
+				    "xorw %%bp, %%bp\n\t"
181
+				    "lret\n\t" )
182
+					 : : "r" ( COMBOOT_PSP_SEG ) : "eax" );
183
+		break;
184
+
185
+	case COMBOOT_RETURN_RUN_KERNEL:
186
+		DBGC ( image, "COMBOOT %p: returned to run kernel...\n", image );
187
+		comboot_run_kernel ( );
188
+		break;
189
+
190
+	case COMBOOT_RETURN_EXIT:
191
+		break;
192
+
193
+	}
194
+
195
+	comboot_force_text_mode ( );
196
+
197
+	DBGC ( image, "COMBOOT %p returned\n", image );
198
+
199
+	/* Re-register image and return */
200
+	register_image ( image );
201
+	image_put ( image );
202
+	
203
+	return 0;
204
+}
205
+
206
+/**
207
+ * Check image name extension
208
+ * 
209
+ * @v image		COMBOOT image
210
+ * @ret rc		Return status code
211
+ */
212
+static int comboot_identify ( struct image *image ) {
213
+	const char *ext;
214
+
215
+	ext = strrchr( image->name, '.' );
216
+
217
+	if ( ! ext ) {
218
+		DBGC ( image, "COMBOOT %p: no extension\n",
219
+		       image );
220
+		return -ENOEXEC;
221
+	}
222
+
223
+	++ext;
224
+
225
+	if ( strcasecmp( ext, "com" ) && strcasecmp( ext, "cbt" ) ) {
226
+		DBGC ( image, "COMBOOT %p: unrecognized extension %s\n",
227
+		       image, ext );
228
+		return -ENOEXEC;
229
+	}
230
+
231
+	return 0;
232
+}
233
+
234
+/**
235
+ * Load COMBOOT image into memory, preparing a segment and returning it
236
+ * @v image		COMBOOT image
237
+ * @ret rc		Return status code
238
+ */
239
+static int comboot_prepare_segment ( struct image *image )
240
+{
241
+	userptr_t seg_userptr;
242
+	size_t filesz, memsz;
243
+	int rc;
244
+
245
+	/* Load image in segment */
246
+	seg_userptr = real_to_user ( COMBOOT_PSP_SEG, 0 );
247
+
248
+	/* Allow etra 0x100 bytes before image for PSP */
249
+	filesz = image->len + 0x100; 
250
+
251
+	/* Ensure the entire 64k segment is free */
252
+	memsz = 0xFFFF;
253
+
254
+	/* Prepare, verify, and load the real-mode segment */
255
+	if ( ( rc = prep_segment ( seg_userptr, filesz, memsz ) ) != 0 ) {
256
+		DBGC ( image, "COMBOOT %p: could not prepare segment: %s\n",
257
+		       image, strerror ( rc ) );
258
+		return rc;
259
+	}
260
+
261
+	/* Zero PSP */
262
+	memset_user ( seg_userptr, 0, 0, 0x100 );
263
+
264
+	/* Copy image to segment:0100 */
265
+	memcpy_user ( seg_userptr, 0x100, image->data, 0, image->len );
266
+
267
+	return 0;
268
+}
269
+
270
+/**
271
+ * Load COMBOOT image into memory
272
+ *
273
+ * @v image		COMBOOT image
274
+ * @ret rc		Return status code
275
+ */
276
+static int comboot_load ( struct image *image ) {
277
+	int rc;
278
+
279
+	DBGC ( image, "COMBOOT %p: name '%s'\n",
280
+	       image, image->name );
281
+
282
+	/* Check if this is a COMBOOT image */
283
+	if ( ( rc = comboot_identify ( image ) ) != 0 ) {
284
+		
285
+		return rc;
286
+	}
287
+
288
+	/* This is a 16-bit COMBOOT image, valid or otherwise */
289
+	if ( ! image->type )
290
+		image->type = &comboot_image_type;
291
+	
292
+	/* Sanity check for filesize */
293
+	if( image->len >= 0xFF00 ) {
294
+		DBGC( image, "COMBOOT %p: image too large\n",
295
+		      image );
296
+		return -ENOEXEC;
297
+	}
298
+
299
+	/* Prepare segment and load image */
300
+	if ( ( rc = comboot_prepare_segment ( image ) ) != 0 ) {
301
+		return rc;
302
+	}
303
+
304
+	return 0;
305
+}
306
+
307
+/** SYSLINUX COMBOOT (16-bit) image type */
308
+struct image_type comboot_image_type __image_type ( PROBE_NORMAL ) = {
309
+	.name = "COMBOOT",
310
+	.load = comboot_load,
311
+	.exec = comboot_exec,
312
+};

+ 3
- 0
src/arch/i386/include/bits/errfile.h Voir le fichier

@@ -21,6 +21,9 @@
21 21
 #define ERRFILE_nbi	       ( ERRFILE_ARCH | ERRFILE_IMAGE | 0x00040000 )
22 22
 #define ERRFILE_pxe_image      ( ERRFILE_ARCH | ERRFILE_IMAGE | 0x00050000 )
23 23
 #define ERRFILE_elfboot	       ( ERRFILE_ARCH | ERRFILE_IMAGE | 0x00060000 )
24
+#define ERRFILE_comboot        ( ERRFILE_ARCH | ERRFILE_IMAGE | 0x00070000 )
25
+#define ERRFILE_com32          ( ERRFILE_ARCH | ERRFILE_IMAGE | 0x00080000 )
26
+#define ERRFILE_comboot_resolv ( ERRFILE_ARCH | ERRFILE_IMAGE | 0x00090000 )
24 27
 
25 28
 #define ERRFILE_undi		 ( ERRFILE_ARCH | ERRFILE_NET | 0x00000000 )
26 29
 #define ERRFILE_undiload	 ( ERRFILE_ARCH | ERRFILE_NET | 0x00010000 )

+ 102
- 0
src/arch/i386/include/comboot.h Voir le fichier

@@ -0,0 +1,102 @@
1
+#ifndef COMBOOT_H
2
+#define COMBOOT_H
3
+
4
+/**
5
+ * @file
6
+ *
7
+ * SYSLINUX COMBOOT
8
+ */
9
+
10
+#include <stdint.h>
11
+#include <setjmp.h>
12
+#include <gpxe/in.h>
13
+
14
+/** Segment used for COMBOOT PSP and image */
15
+#define COMBOOT_PSP_SEG 0x07C0
16
+
17
+/** Entry point address of COM32 images */
18
+#define COM32_START_PHYS 0x101000
19
+
20
+/** COM32 bounce buffer segment */
21
+#define COM32_BOUNCE_SEG 0x07C0
22
+
23
+/** Size of SYSLINUX file block in bytes */
24
+#define COMBOOT_FILE_BLOCKSZ 512
25
+
26
+/** COMBOOT feature flags (INT 22h AX=15h) */
27
+#define COMBOOT_FEATURE_LOCAL_BOOT (1 << 0)
28
+#define COMBOOT_FEATURE_IDLE_LOOP  (1 << 1)
29
+
30
+/** Maximum number of shuffle descriptors for 
31
+ * shuffle and boot functions
32
+ * (INT 22h AX=0012h, 001Ah, 001Bh)
33
+ */
34
+#define COMBOOT_MAX_SHUFFLE_DESCRIPTORS 682
35
+
36
+typedef union {
37
+	uint32_t l;
38
+	uint16_t w[2];
39
+	uint8_t  b[4];
40
+} com32_reg32_t;
41
+
42
+typedef struct {
43
+	uint16_t gs;                /* Offset  0 */
44
+	uint16_t fs;                /* Offset  2 */
45
+	uint16_t es;                /* Offset  4 */
46
+	uint16_t ds;                /* Offset  6 */
47
+
48
+	com32_reg32_t edi;          /* Offset  8 */
49
+	com32_reg32_t esi;          /* Offset 12 */
50
+	com32_reg32_t ebp;          /* Offset 16 */
51
+	com32_reg32_t _unused_esp;  /* Offset 20 */
52
+	com32_reg32_t ebx;          /* Offset 24 */
53
+	com32_reg32_t edx;          /* Offset 28 */
54
+	com32_reg32_t ecx;          /* Offset 32 */
55
+	com32_reg32_t eax;          /* Offset 36 */
56
+
57
+	com32_reg32_t eflags;       /* Offset 40 */
58
+} com32sys_t;
59
+
60
+typedef struct {
61
+	uint32_t dest;
62
+	uint32_t src;
63
+	uint32_t len;
64
+} comboot_shuffle_descriptor;
65
+
66
+extern void hook_comboot_interrupts ( );
67
+
68
+/* These are not the correct prototypes, but it doens't matter, 
69
+ * as we only ever get the address of these functions;
70
+ * they are only called from COM32 code running in PHYS_CODE
71
+ */
72
+extern void com32_intcall_wrapper ( );
73
+extern void com32_farcall_wrapper ( );
74
+extern void com32_cfarcall_wrapper ( );
75
+
76
+/* Resolve a hostname to an (IPv4) address */
77
+extern int comboot_resolv ( const char *name, struct in_addr *address );
78
+
79
+/* setjmp/longjmp context buffer used to return after loading an image */
80
+extern jmp_buf comboot_return;
81
+
82
+/* Command line to execute when returning via comboot_return 
83
+ * with COMBOOT_RETURN_RUN_KERNEL
84
+ */
85
+extern char *comboot_kernel_cmdline;
86
+
87
+/* Execute comboot_image_cmdline */
88
+extern void comboot_run_kernel ( );
89
+
90
+extern void *com32_external_esp;
91
+
92
+#define COMBOOT_RETURN_EXIT 1
93
+#define COMBOOT_RETURN_RUN_KERNEL 2
94
+
95
+extern void comboot_force_text_mode ( void );
96
+
97
+#define COMBOOT_VIDEO_GRAPHICS    0x01
98
+#define COMBOOT_VIDEO_NONSTANDARD 0x02
99
+#define COMBOOT_VIDEO_VESA        0x04
100
+#define COMBOOT_VIDEO_NOTEXT      0x08
101
+
102
+#endif

+ 1
- 0
src/arch/i386/include/pxe_call.h Voir le fichier

@@ -30,5 +30,6 @@ extern void pxe_hook_int1a ( void );
30 30
 extern int pxe_unhook_int1a ( void );
31 31
 extern void pxe_init_structures ( void );
32 32
 extern int pxe_start_nbp ( void );
33
+extern __cdecl void pxe_api_call ( struct i386_all_regs *ix86 );
33 34
 
34 35
 #endif /* _PXE_CALL_H */

+ 188
- 0
src/arch/i386/interface/syslinux/com32_call.c Voir le fichier

@@ -0,0 +1,188 @@
1
+/*
2
+ * Copyright (C) 2008 Daniel Verkamp <daniel@drv.nu>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+/**
20
+ * @file SYSLINUX COM32 helpers
21
+ *
22
+ */
23
+
24
+#include <stdint.h>
25
+#include <realmode.h>
26
+#include <comboot.h>
27
+#include <assert.h>
28
+#include <gpxe/uaccess.h>
29
+
30
+static com32sys_t __bss16 ( com32_regs );
31
+#define com32_regs __use_data16 ( com32_regs )
32
+
33
+static uint8_t __bss16 ( com32_int_vector );
34
+#define com32_int_vector __use_data16 ( com32_int_vector )
35
+
36
+static uint32_t __bss16 ( com32_farcall_proc );
37
+#define com32_farcall_proc __use_data16 ( com32_farcall_proc )
38
+
39
+uint16_t __bss16 ( com32_saved_sp );
40
+
41
+/**
42
+ * Interrupt call helper
43
+ */
44
+void __cdecl com32_intcall ( uint8_t interrupt, physaddr_t inregs_phys, physaddr_t outregs_phys ) {
45
+
46
+	memcpy_user ( virt_to_user( &com32_regs ), 0,
47
+	              phys_to_user ( inregs_phys ), 0,
48
+	              sizeof(com32sys_t) );
49
+
50
+	com32_int_vector = interrupt;
51
+
52
+	__asm__ __volatile__ (
53
+		REAL_CODE ( /* Save all registers */
54
+		            "pushal\n\t"
55
+		            "pushw %%ds\n\t"
56
+		            "pushw %%es\n\t"
57
+		            "pushw %%fs\n\t"
58
+		            "pushw %%gs\n\t"
59
+		            /* Mask off unsafe flags */
60
+		            "movl (com32_regs + 40), %%eax\n\t"
61
+		            "andl $0x200cd7, %%eax\n\t"
62
+		            "movl %%eax, (com32_regs + 40)\n\t"
63
+		            /* Load com32_regs into the actual registers */
64
+		            "movw %%sp, %%ss:(com32_saved_sp)\n\t"
65
+		            "movw $com32_regs, %%sp\n\t"
66
+		            "popw %%gs\n\t"
67
+		            "popw %%fs\n\t"
68
+		            "popw %%es\n\t"
69
+		            "popw %%ds\n\t"
70
+		            "popal\n\t"
71
+		            "popfl\n\t"
72
+		            "movw %%ss:(com32_saved_sp), %%sp\n\t"
73
+		            /* patch INT instruction */
74
+		            "pushw %%ax\n\t"
75
+		            "movb %%ss:(com32_int_vector), %%al\n\t"
76
+		            "movb %%al, %%cs:(com32_intcall_instr + 1)\n\t" 
77
+		            /* perform a jump to avoid problems with cache
78
+		             * consistency in self-modifying code on some CPUs (486)
79
+		             */
80
+		            "jmp 1f\n"
81
+		            "1:\n\t"
82
+		            "popw %%ax\n\t"
83
+		            "com32_intcall_instr:\n\t"
84
+		            /* INT instruction to be patched */
85
+		            "int $0xFF\n\t"
86
+		            /* Copy regs back to com32_regs */
87
+		            "movw %%sp, %%ss:(com32_saved_sp)\n\t"
88
+		            "movw $(com32_regs + 44), %%sp\n\t"
89
+		            "pushfl\n\t"
90
+		            "pushal\n\t"
91
+		            "pushw %%ds\n\t"
92
+		            "pushw %%es\n\t"
93
+		            "pushw %%fs\n\t"
94
+		            "pushw %%gs\n\t"
95
+		            "movw %%ss:(com32_saved_sp), %%sp\n\t"
96
+		            /* Restore registers */
97
+		            "popw %%gs\n\t"
98
+		            "popw %%fs\n\t"
99
+		            "popw %%es\n\t"
100
+		            "popw %%ds\n\t"
101
+		            "popal\n\t")
102
+		            : : );
103
+
104
+	if ( outregs_phys ) {
105
+		memcpy_user ( phys_to_user ( outregs_phys ), 0,
106
+		              virt_to_user( &com32_regs ), 0, 
107
+		              sizeof(com32sys_t) );
108
+	}
109
+}
110
+
111
+/**
112
+ * Farcall helper
113
+ */
114
+void __cdecl com32_farcall ( uint32_t proc, physaddr_t inregs_phys, physaddr_t outregs_phys ) {
115
+
116
+	memcpy_user ( virt_to_user( &com32_regs ), 0,
117
+	              phys_to_user ( inregs_phys ), 0,
118
+	              sizeof(com32sys_t) );
119
+
120
+	com32_farcall_proc = proc;
121
+
122
+	__asm__ __volatile__ (
123
+		REAL_CODE ( /* Save all registers */
124
+		            "pushal\n\t"
125
+		            "pushw %%ds\n\t"
126
+		            "pushw %%es\n\t"
127
+		            "pushw %%fs\n\t"
128
+		            "pushw %%gs\n\t"
129
+		            /* Mask off unsafe flags */
130
+		            "movl (com32_regs + 40), %%eax\n\t"
131
+		            "andl $0x200cd7, %%eax\n\t"
132
+		            "movl %%eax, (com32_regs + 40)\n\t"
133
+		            /* Load com32_regs into the actual registers */
134
+		            "movw %%sp, %%ss:(com32_saved_sp)\n\t"
135
+		            "movw $com32_regs, %%sp\n\t"
136
+		            "popw %%gs\n\t"
137
+		            "popw %%fs\n\t"
138
+		            "popw %%es\n\t"
139
+		            "popw %%ds\n\t"
140
+		            "popal\n\t"
141
+		            "popfl\n\t"
142
+		            "movw %%ss:(com32_saved_sp), %%sp\n\t"
143
+		            /* Call procedure */
144
+		            "lcall *%%ss:(com32_farcall_proc)\n\t"
145
+		            /* Copy regs back to com32_regs */
146
+		            "movw %%sp, %%ss:(com32_saved_sp)\n\t"
147
+		            "movw $(com32_regs + 44), %%sp\n\t"
148
+		            "pushfl\n\t"
149
+		            "pushal\n\t"
150
+		            "pushw %%ds\n\t"
151
+		            "pushw %%es\n\t"
152
+		            "pushw %%fs\n\t"
153
+		            "pushw %%gs\n\t"
154
+		            "movw %%ss:(com32_saved_sp), %%sp\n\t"
155
+		            /* Restore registers */
156
+		            "popw %%gs\n\t"
157
+		            "popw %%fs\n\t"
158
+		            "popw %%es\n\t"
159
+		            "popw %%ds\n\t"
160
+		            "popal\n\t")
161
+		            : : );
162
+
163
+	if ( outregs_phys ) {
164
+		memcpy_user ( phys_to_user ( outregs_phys ), 0,
165
+		              virt_to_user( &com32_regs ), 0, 
166
+		              sizeof(com32sys_t) );
167
+	}
168
+}
169
+
170
+/**
171
+ * CDECL farcall helper
172
+ */
173
+int __cdecl com32_cfarcall ( uint32_t proc, physaddr_t stack, size_t stacksz ) {
174
+	int32_t eax;
175
+
176
+	copy_user_to_rm_stack ( phys_to_user ( stack ), stacksz );
177
+	com32_farcall_proc = proc;
178
+
179
+	__asm__ __volatile__ (
180
+		REAL_CODE ( "lcall *%%ss:(com32_farcall_proc)\n\t" )
181
+		: "=a" (eax)
182
+		: 
183
+		: "ecx", "edx" );
184
+
185
+	remove_user_from_rm_stack ( 0, stacksz );
186
+
187
+	return eax;
188
+}

+ 92
- 0
src/arch/i386/interface/syslinux/com32_wrapper.S Voir le fichier

@@ -0,0 +1,92 @@
1
+/*
2
+ * Copyright (C) 2008 Daniel Verkamp <daniel@drv.nu>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+	.text
20
+	.arch i386
21
+	.code32
22
+
23
+	.globl com32_farcall_wrapper
24
+com32_farcall_wrapper:
25
+
26
+	movl $com32_farcall, %eax
27
+	jmp com32_wrapper
28
+
29
+
30
+	.globl com32_cfarcall_wrapper
31
+com32_cfarcall_wrapper:
32
+
33
+	movl $com32_cfarcall, %eax
34
+	jmp com32_wrapper
35
+
36
+
37
+	.globl com32_intcall_wrapper
38
+com32_intcall_wrapper:
39
+
40
+	movl $com32_intcall, %eax
41
+	/*jmp com32_wrapper*/ /* fall through */
42
+
43
+com32_wrapper:
44
+
45
+	/* Switch to internal virtual address space */
46
+	call _phys_to_virt
47
+
48
+	mov %eax, (com32_helper_function)
49
+
50
+	/* Save external COM32 stack pointer */
51
+	movl %esp, (com32_external_esp)
52
+
53
+	/* Copy arguments to caller-save registers */
54
+	movl 12(%esp), %eax
55
+	movl 8(%esp), %ecx
56
+	movl 4(%esp), %edx
57
+
58
+	/* Switch to internal stack */
59
+	movl (com32_internal_esp), %esp
60
+
61
+	/* Copy arguments to internal stack */
62
+	pushl %eax
63
+	pushl %ecx
64
+	pushl %edx
65
+
66
+	call *(com32_helper_function)
67
+
68
+	/* Clean up stack */
69
+	addl $12, %esp
70
+
71
+	/* Save internal stack pointer and restore external stack pointer */
72
+	movl %esp, (com32_internal_esp)
73
+	movl (com32_external_esp), %esp
74
+
75
+	/* Switch to external flat physical address space */
76
+	call _virt_to_phys
77
+
78
+	ret
79
+
80
+
81
+	.data
82
+
83
+/* Internal gPXE virtual address space %esp */
84
+.globl com32_internal_esp
85
+.lcomm com32_internal_esp, 4
86
+
87
+/* External flat physical address space %esp */
88
+.globl com32_external_esp
89
+.lcomm com32_external_esp, 4
90
+
91
+/* Function pointer of helper to call */
92
+.lcomm com32_helper_function, 4

+ 598
- 0
src/arch/i386/interface/syslinux/comboot_call.c Voir le fichier

@@ -0,0 +1,598 @@
1
+/*
2
+ * Copyright (C) 2008 Daniel Verkamp <daniel@drv.nu>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+/**
20
+ * @file SYSLINUX COMBOOT API
21
+ *
22
+ */
23
+
24
+#include <errno.h>
25
+#include <realmode.h>
26
+#include <biosint.h>
27
+#include <console.h>
28
+#include <stdlib.h>
29
+#include <comboot.h>
30
+#include <bzimage.h>
31
+#include <pxe_call.h>
32
+#include <setjmp.h>
33
+#include <string.h>
34
+#include <gpxe/posix_io.h>
35
+#include <gpxe/process.h>
36
+#include <gpxe/serial.h>
37
+#include <gpxe/init.h>
38
+
39
+/** The "SYSLINUX" version string */
40
+static char __data16_array ( syslinux_version, [] ) = "gPXE " VERSION;
41
+#define syslinux_version __use_data16 ( syslinux_version )
42
+
43
+/** The "SYSLINUX" copyright string */
44
+static char __data16_array ( syslinux_copyright, [] ) = "http://etherboot.org";
45
+#define syslinux_copyright __use_data16 ( syslinux_copyright )
46
+
47
+static char __data16_array ( syslinux_configuration_file, [] ) = "";
48
+#define syslinux_configuration_file __use_data16 ( syslinux_configuration_file )
49
+
50
+/** Feature flags */
51
+static uint8_t __data16 ( comboot_feature_flags ) = COMBOOT_FEATURE_IDLE_LOOP;
52
+#define comboot_feature_flags __use_data16 ( comboot_feature_flags )
53
+
54
+static struct segoff __text16 ( int20_vector );
55
+#define int20_vector __use_text16 ( int20_vector )
56
+
57
+static struct segoff __text16 ( int21_vector );
58
+#define int21_vector __use_text16 ( int21_vector )
59
+
60
+static struct segoff __text16 ( int22_vector );
61
+#define int22_vector __use_text16 ( int22_vector )
62
+
63
+extern void int20_wrapper ( void );
64
+extern void int21_wrapper ( void );
65
+extern void int22_wrapper ( void );
66
+
67
+/* setjmp/longjmp context buffer used to return after loading an image */
68
+jmp_buf comboot_return;
69
+
70
+/* Command line to execute when returning via comboot_return 
71
+ * with COMBOOT_RETURN_RUN_KERNEL
72
+ */
73
+char *comboot_kernel_cmdline;
74
+
75
+/* Mode flags set by INT 22h AX=0017h */
76
+static uint16_t comboot_graphics_mode = 0;
77
+
78
+
79
+/**
80
+ * Print a string with a particular terminator
81
+ */
82
+static void print_user_string ( unsigned int segment, unsigned int offset, char terminator ) {
83
+	int i = 0;
84
+	char c;
85
+	userptr_t str = real_to_user ( segment, offset );
86
+	for ( ; ; ) {
87
+		copy_from_user ( &c, str, i, 1 );
88
+		if ( c == terminator ) break;
89
+		putchar ( c );
90
+		i++;
91
+	}
92
+}
93
+
94
+
95
+/**
96
+ * Perform a series of memory copies from a list in low memory
97
+ */
98
+static void shuffle ( unsigned int list_segment, unsigned int list_offset, unsigned int count )
99
+{
100
+	comboot_shuffle_descriptor shuf[COMBOOT_MAX_SHUFFLE_DESCRIPTORS];
101
+	unsigned int i;
102
+
103
+	/* Copy shuffle descriptor list so it doesn't get overwritten */
104
+	copy_from_user ( shuf, real_to_user ( list_segment, list_offset ), 0,
105
+	                 count * sizeof( comboot_shuffle_descriptor ) );
106
+
107
+	/* Do the copies */
108
+	for ( i = 0; i < count; i++ ) {
109
+		userptr_t src_u = phys_to_user ( shuf[ i ].src );
110
+		userptr_t dest_u = phys_to_user ( shuf[ i ].dest );
111
+
112
+		if ( shuf[ i ].src == 0xFFFFFFFF ) {
113
+			/* Fill with 0 instead of copying */
114
+			memset_user ( dest_u, 0, 0, shuf[ i ].len );
115
+		} else if ( shuf[ i ].dest == 0xFFFFFFFF ) {
116
+			/* Copy new list of descriptors */
117
+			count = shuf[ i ].len / sizeof( comboot_shuffle_descriptor );
118
+			assert ( count <= COMBOOT_MAX_SHUFFLE_DESCRIPTORS );
119
+			copy_from_user ( shuf, src_u, 0, shuf[ i ].len );
120
+			i = -1;
121
+		} else {
122
+			/* Regular copy */
123
+			memmove_user ( dest_u, 0, src_u, 0, shuf[ i ].len );
124
+		}
125
+	}
126
+}
127
+
128
+
129
+/**
130
+ * Set default text mode
131
+ */
132
+void comboot_force_text_mode ( void ) {
133
+	if ( comboot_graphics_mode & COMBOOT_VIDEO_VESA ) {
134
+		/* Set VGA mode 3 via VESA VBE mode set */
135
+		__asm__ __volatile__ (
136
+			REAL_CODE (
137
+				"mov $0x4F02, %%ax\n\t"
138
+				"mov $0x03, %%bx\n\t"
139
+				"int $0x10\n\t"
140
+			)
141
+		: : );
142
+	} else if ( comboot_graphics_mode & COMBOOT_VIDEO_GRAPHICS ) {
143
+		/* Set VGA mode 3 via standard VGA mode set */
144
+		__asm__ __volatile__ (
145
+			REAL_CODE (
146
+				"mov $0x03, %%ax\n\t"
147
+				"int $0x10\n\t"
148
+			)
149
+		: : );
150
+	}
151
+
152
+	comboot_graphics_mode = 0;
153
+}
154
+
155
+
156
+/**
157
+ * Run the kernel specified in comboot_kernel_cmdline
158
+ */
159
+void comboot_run_kernel ( )
160
+{
161
+	char *initrd;
162
+
163
+	comboot_force_text_mode ( );
164
+
165
+	DBG ( "COMBOOT: executing image '%s'\n", comboot_kernel_cmdline );
166
+
167
+	/* Find initrd= parameter, if any */
168
+	if ( ( initrd = strstr ( comboot_kernel_cmdline, "initrd=" ) ) ) {
169
+		char old_char = '\0';
170
+		char *initrd_end = strchr( initrd, ' ' );
171
+
172
+		/* Replace space after end of parameter
173
+		 * with a nul terminator if this is not
174
+		 * the last parameter
175
+		 */
176
+		if ( initrd_end ) {
177
+			old_char = *initrd_end;
178
+			*initrd_end = '\0';
179
+		}
180
+
181
+		/* Replace = with space to get 'initrd filename'
182
+		 * command suitable for system()
183
+		 */
184
+		initrd[6] = ' ';
185
+
186
+		DBG( "COMBOOT: loading initrd '%s'\n", initrd );
187
+
188
+		system ( initrd );
189
+
190
+		/* Restore space after parameter */
191
+		if ( initrd_end ) {
192
+			*initrd_end = old_char;
193
+		}
194
+
195
+		/* Restore = */
196
+		initrd[6] = '=';
197
+	}
198
+
199
+	/* Load kernel */
200
+	DBG ( "COMBOOT: loading kernel '%s'\n", comboot_kernel_cmdline );
201
+	system ( comboot_kernel_cmdline );
202
+
203
+	free ( comboot_kernel_cmdline );
204
+
205
+	/* Boot */
206
+	system ( "boot" );
207
+
208
+	DBG ( "COMBOOT: back from executing command\n" );
209
+}
210
+
211
+
212
+/**
213
+ * Terminate program interrupt handler
214
+ */
215
+static __cdecl void int20 ( struct i386_all_regs *ix86 __unused ) {
216
+	longjmp ( comboot_return, COMBOOT_RETURN_EXIT );
217
+}
218
+
219
+
220
+/**
221
+ * DOS-compatible API
222
+ */
223
+static __cdecl void int21 ( struct i386_all_regs *ix86 ) {
224
+	ix86->flags |= CF;
225
+
226
+	switch ( ix86->regs.ah ) {
227
+	case 0x00:
228
+	case 0x4C: /* Terminate program */
229
+		longjmp ( comboot_return, COMBOOT_RETURN_EXIT );
230
+		break;
231
+
232
+	case 0x01: /* Get Key with Echo */
233
+	case 0x08: /* Get Key without Echo */
234
+		/* TODO: handle extended characters? */
235
+		ix86->regs.al = getchar( );
236
+
237
+		/* Enter */
238
+		if ( ix86->regs.al == 0x0A )
239
+			ix86->regs.al = 0x0D;
240
+
241
+		if ( ix86->regs.ah == 0x01 )
242
+			putchar ( ix86->regs.al );
243
+
244
+		ix86->flags &= ~CF;
245
+		break;
246
+
247
+	case 0x02: /* Write Character */
248
+		putchar ( ix86->regs.dl );
249
+		ix86->flags &= ~CF;
250
+		break;
251
+
252
+	case 0x04: /* Write Character to Serial Port */
253
+		serial_putc ( ix86->regs.dl );
254
+		ix86->flags &= ~CF;
255
+		break;
256
+
257
+	case 0x09: /* Write DOS String to Console */
258
+		print_user_string ( ix86->segs.ds, ix86->regs.dx, '$' );
259
+		ix86->flags &= ~CF;
260
+		break;
261
+
262
+	case 0x0B: /* Check Keyboard */
263
+		if ( iskey() )
264
+			ix86->regs.al = 0xFF;
265
+		else
266
+			ix86->regs.al = 0x00;
267
+
268
+		ix86->flags &= ~CF;
269
+		break;
270
+
271
+	case 0x30: /* Check DOS Version */
272
+		/* Bottom halves all 0; top halves spell "SYSLINUX" */
273
+		ix86->regs.eax = 0x59530000;
274
+		ix86->regs.ebx = 0x4C530000;
275
+		ix86->regs.ecx = 0x4E490000;
276
+		ix86->regs.edx = 0x58550000;
277
+		ix86->flags &= ~CF;
278
+		break;
279
+
280
+	default:
281
+		DBG ( "COMBOOT unknown int21 function %02x\n", ix86->regs.ah );
282
+		break;
283
+	}
284
+}
285
+
286
+
287
+/**
288
+ * SYSLINUX API
289
+ */
290
+static __cdecl void int22 ( struct i386_all_regs *ix86 ) {
291
+	ix86->flags |= CF;
292
+
293
+	switch ( ix86->regs.ax ) {
294
+	case 0x0001: /* Get Version */
295
+
296
+		/* Number of INT 22h API functions available */
297
+		ix86->regs.ax = 0x0018;
298
+
299
+		/* SYSLINUX version number */
300
+		ix86->regs.ch = 0; /* major */
301
+		ix86->regs.cl = 0; /* minor */
302
+
303
+		/* SYSLINUX derivative ID */
304
+		ix86->regs.dl = BZI_LOADER_TYPE_GPXE;
305
+
306
+		/* SYSLINUX version and copyright strings */
307
+		ix86->segs.es = rm_ds;
308
+		ix86->regs.si = ( ( unsigned ) __from_data16 ( syslinux_version ) );
309
+		ix86->regs.di = ( ( unsigned ) __from_data16 ( syslinux_copyright ) );
310
+
311
+		ix86->flags &= ~CF;
312
+		break;
313
+
314
+	case 0x0002: /* Write String */
315
+		print_user_string ( ix86->segs.es, ix86->regs.bx, '\0' );
316
+		ix86->flags &= ~CF;
317
+		break;
318
+
319
+	case 0x0003: /* Run command */
320
+		{
321
+			userptr_t cmd_u = real_to_user ( ix86->segs.es, ix86->regs.bx );
322
+			int len = strlen_user ( cmd_u, 0 );
323
+			char cmd[len + 1];
324
+			copy_from_user ( cmd, cmd_u, 0, len + 1 );
325
+			DBG ( "COMBOOT: executing command '%s'\n", cmd );
326
+
327
+			comboot_kernel_cmdline = strdup ( cmd );
328
+
329
+			DBG ( "COMBOOT: returning to run image...\n" );
330
+			longjmp ( comboot_return, COMBOOT_RETURN_RUN_KERNEL );
331
+		}
332
+		break;
333
+
334
+	case 0x0004: /* Run default command */
335
+		/* FIXME: just exit for now */
336
+		longjmp ( comboot_return, COMBOOT_RETURN_EXIT );
337
+		break;
338
+
339
+	case 0x0005: /* Force text mode */
340
+		comboot_force_text_mode ( );
341
+		ix86->flags &= ~CF;
342
+		break;
343
+
344
+	case 0x0006: /* Open file */
345
+		{
346
+			int fd;
347
+			userptr_t file_u = real_to_user ( ix86->segs.es, ix86->regs.si );
348
+			int len = strlen_user ( file_u, 0 );
349
+			char file[len + 1];
350
+
351
+			copy_from_user ( file, file_u, 0, len + 1 );
352
+
353
+			if ( file[0] == '\0' ) {
354
+				DBG ( "COMBOOT: attempted open with empty file name\n" );
355
+				break;
356
+			}
357
+
358
+			DBG ( "COMBOOT: opening file '%s'\n", file );
359
+
360
+			fd = open ( file );
361
+
362
+			if ( fd < 0 ) {
363
+				DBG ( "COMBOOT: error opening file %s\n", file );
364
+				break;
365
+			}
366
+
367
+			/* This relies on the fact that a gPXE POSIX fd will
368
+			 * always fit in 16 bits.
369
+			 */
370
+#if (POSIX_FD_MAX > 65535)
371
+#error POSIX_FD_MAX too large
372
+#endif
373
+			ix86->regs.si = (uint16_t) fd;
374
+
375
+			ix86->regs.cx = COMBOOT_FILE_BLOCKSZ;
376
+			ix86->regs.eax = fsize ( fd );
377
+			ix86->flags &= ~CF;
378
+		}
379
+		break;
380
+
381
+	case 0x0007: /* Read file */
382
+		{
383
+			int fd = ix86->regs.si;
384
+			int len = ix86->regs.cx * COMBOOT_FILE_BLOCKSZ;
385
+			int rc;
386
+			fd_set fds;
387
+			userptr_t buf = real_to_user ( ix86->segs.es, ix86->regs.bx );
388
+
389
+			/* Wait for data ready to read */
390
+			FD_ZERO ( &fds );
391
+			FD_SET ( fd, &fds );
392
+
393
+			select ( &fds, 1 );
394
+
395
+			rc = read_user ( fd, buf, 0, len );
396
+			if ( rc < 0 ) {
397
+				DBG ( "COMBOOT: read failed\n" );
398
+				ix86->regs.si = 0;
399
+				break;
400
+			}
401
+
402
+			ix86->regs.ecx = rc;
403
+			ix86->flags &= ~CF;
404
+		}
405
+		break;
406
+
407
+	case 0x0008: /* Close file */
408
+		{
409
+			int fd = ix86->regs.si;
410
+			close ( fd );
411
+			ix86->flags &= ~CF;
412
+		}
413
+		break;
414
+
415
+	case 0x0009: /* Call PXE Stack */
416
+		pxe_api_call ( ix86 );
417
+		ix86->flags &= ~CF;
418
+		break;
419
+
420
+	case 0x000A: /* Get Derivative-Specific Information */
421
+
422
+		/* gPXE has its own derivative ID, so there is no defined
423
+		 * output here; just return AL for now */
424
+		ix86->regs.al = BZI_LOADER_TYPE_GPXE;
425
+		ix86->flags &= ~CF;
426
+		break;
427
+
428
+	case 0x000B: /* Get Serial Console Configuration */
429
+		/* FIXME: stub */
430
+		ix86->regs.dx = 0;
431
+		ix86->flags &= ~CF;
432
+		break;
433
+
434
+	case 0x000E: /* Get configuration file name */
435
+		/* FIXME: stub */
436
+		ix86->segs.es = rm_ds;
437
+		ix86->regs.bx = ( ( unsigned ) __from_data16 ( syslinux_configuration_file ) );
438
+		ix86->flags &= ~CF;
439
+		break;
440
+
441
+	case 0x000F: /* Get IPAPPEND strings */
442
+		/* FIXME: stub */
443
+		ix86->regs.cx = 0;
444
+		ix86->segs.es = 0;
445
+		ix86->regs.bx = 0;
446
+		ix86->flags &= ~CF;
447
+		break;
448
+
449
+	case 0x0010: /* Resolve hostname */
450
+		{
451
+			userptr_t hostname_u = real_to_user ( ix86->segs.es, ix86->regs.bx );
452
+			int len = strlen_user ( hostname_u, 0 );
453
+			char hostname[len];
454
+			struct in_addr addr;
455
+
456
+			copy_from_user ( hostname, hostname_u, 0, len + 1 );
457
+			
458
+			/* TODO:
459
+			 * "If the hostname does not contain a dot (.), the
460
+			 * local domain name is automatically appended."
461
+			 */
462
+
463
+			comboot_resolv ( hostname, &addr );
464
+
465
+			ix86->regs.eax = addr.s_addr;
466
+			ix86->flags &= ~CF;
467
+		}
468
+		break;
469
+
470
+	case 0x0011: /* Maximum number of shuffle descriptors */
471
+		ix86->regs.cx = COMBOOT_MAX_SHUFFLE_DESCRIPTORS;
472
+		ix86->flags &= ~CF;
473
+		break;
474
+
475
+	case 0x0012: /* Cleanup, shuffle and boot */
476
+		if ( ix86->regs.cx > COMBOOT_MAX_SHUFFLE_DESCRIPTORS )
477
+			break;
478
+
479
+		/* Perform final cleanup */
480
+		shutdown ( SHUTDOWN_BOOT );
481
+
482
+		/* Perform sequence of copies */
483
+		shuffle ( ix86->segs.es, ix86->regs.di, ix86->regs.cx );
484
+
485
+		/* Jump to real-mode entry point */
486
+		__asm__ __volatile__ (
487
+			REAL_CODE ( 
488
+				"pushw %0\n\t"
489
+				"popw %%ds\n\t"
490
+				"pushl %1\n\t"
491
+				"lret\n\t"
492
+			)
493
+			:
494
+			: "r" ( ix86->segs.ds ),
495
+			  "r" ( ix86->regs.ebp ),
496
+			  "d" ( ix86->regs.ebx ),
497
+			  "S" ( ix86->regs.esi ) );
498
+
499
+		assert ( 0 ); /* Execution should never reach this point */
500
+
501
+		break;
502
+
503
+	case 0x0013: /* Idle loop call */
504
+		step ( );
505
+		ix86->flags &= ~CF;
506
+		break;
507
+
508
+	case 0x0015: /* Get feature flags */
509
+		ix86->segs.es = rm_ds;
510
+		ix86->regs.bx = ( ( unsigned ) __from_data16 ( &comboot_feature_flags ) );
511
+		ix86->regs.cx = 1; /* Number of feature flag bytes */
512
+		ix86->flags &= ~CF;
513
+		break;
514
+
515
+	case 0x0016: /* Run kernel image */
516
+		{
517
+			userptr_t file_u = real_to_user ( ix86->segs.ds, ix86->regs.si );
518
+			userptr_t cmd_u = real_to_user ( ix86->segs.es, ix86->regs.bx );
519
+			int file_len = strlen_user ( file_u, 0 );
520
+			int cmd_len = strlen_user ( cmd_u, 0 );
521
+			char file[file_len + 1 + cmd_len + 7 + 1];
522
+			char cmd[cmd_len + 1];
523
+
524
+			memcpy( file, "kernel ", 7 );
525
+			copy_from_user ( file + 7, file_u, 0, file_len + 1 );
526
+			copy_from_user ( cmd, cmd_u, 0, cmd_len + 1 );
527
+			strcat ( file, " " );
528
+			strcat ( file, cmd );
529
+
530
+			DBG ( "COMBOOT: run kernel image '%s'\n", file );
531
+
532
+			comboot_kernel_cmdline = strdup ( file );			
533
+
534
+			DBG ( "COMBOOT: returning to run image...\n" );
535
+			longjmp ( comboot_return, COMBOOT_RETURN_RUN_KERNEL );
536
+		}
537
+		break;
538
+
539
+	case 0x0017: /* Report video mode change */
540
+		comboot_graphics_mode = ix86->regs.bx;
541
+		ix86->flags &= ~CF;
542
+		break;
543
+
544
+	case 0x0018: /* Query custom font */
545
+		/* FIXME: stub */
546
+		ix86->regs.al = 0;
547
+		ix86->segs.es = 0;
548
+		ix86->regs.bx = 0;
549
+		ix86->flags &= ~CF;
550
+		break;
551
+
552
+	default:
553
+		DBG ( "COMBOOT unknown int22 function %04x\n", ix86->regs.ax );
554
+		break;
555
+	}
556
+}
557
+
558
+/**
559
+ * Hook BIOS interrupts related to COMBOOT API (INT 20h, 21h, 22h)
560
+ */
561
+void hook_comboot_interrupts ( ) {
562
+
563
+	__asm__ __volatile__ (
564
+		TEXT16_CODE ( "\nint20_wrapper:\n\t"
565
+		              "pushl %0\n\t"
566
+		              "pushw %%cs\n\t"
567
+		              "call prot_call\n\t"
568
+		              "addw $4, %%sp\n\t"
569
+		              "iret\n\t" )
570
+		          : : "i" ( int20 ) );
571
+
572
+	hook_bios_interrupt ( 0x20, ( unsigned int ) int20_wrapper,
573
+		                      &int20_vector );
574
+
575
+	__asm__ __volatile__ (
576
+		TEXT16_CODE ( "\nint21_wrapper:\n\t"
577
+		              "pushl %0\n\t"
578
+		              "pushw %%cs\n\t"
579
+		              "call prot_call\n\t"
580
+		              "addw $4, %%sp\n\t"
581
+		              "iret\n\t" )
582
+		          : : "i" ( int21 ) );
583
+
584
+	hook_bios_interrupt ( 0x21, ( unsigned int ) int21_wrapper,
585
+	                      &int21_vector );
586
+
587
+	__asm__  __volatile__ (
588
+		TEXT16_CODE ( "\nint22_wrapper:\n\t"
589
+		              "pushl %0\n\t"
590
+		              "pushw %%cs\n\t"
591
+		              "call prot_call\n\t"
592
+		              "addw $4, %%sp\n\t"
593
+		              "iret\n\t" )
594
+		          : : "i" ( int22) );
595
+
596
+	hook_bios_interrupt ( 0x22, ( unsigned int ) int22_wrapper,
597
+	                      &int22_vector );
598
+}

+ 58
- 0
src/arch/i386/interface/syslinux/comboot_resolv.c Voir le fichier

@@ -0,0 +1,58 @@
1
+#include <errno.h>
2
+#include <comboot.h>
3
+#include <gpxe/in.h>
4
+#include <gpxe/list.h>
5
+#include <gpxe/process.h>
6
+#include <gpxe/resolv.h>
7
+
8
+static int comboot_resolv_rc;
9
+static struct in_addr comboot_resolv_addr;
10
+
11
+static void comboot_resolv_done ( struct resolv_interface *resolv,
12
+				  struct sockaddr *sa, int rc ) {
13
+	struct sockaddr_in *sin;
14
+
15
+	resolv_unplug ( resolv );
16
+
17
+	if ( rc != 0 ) {
18
+		comboot_resolv_rc = rc;
19
+		return;
20
+	}
21
+
22
+	if ( sa->sa_family != AF_INET ) {
23
+		comboot_resolv_rc = -EAFNOSUPPORT;
24
+		return;
25
+	}
26
+
27
+	sin = ( ( struct sockaddr_in * ) sa );
28
+	comboot_resolv_addr = sin->sin_addr;
29
+
30
+	comboot_resolv_rc = 0;
31
+}
32
+
33
+static struct resolv_interface_operations comboot_resolv_ops = {
34
+	.done = comboot_resolv_done,
35
+};
36
+
37
+static struct resolv_interface comboot_resolver = {
38
+	.intf = {
39
+		.dest = &null_resolv.intf,
40
+		.refcnt = NULL,
41
+	},
42
+	.op = &comboot_resolv_ops,
43
+};
44
+
45
+int comboot_resolv ( const char *name, struct in_addr *address ) {
46
+	int rc;
47
+
48
+	comboot_resolv_rc = -EINPROGRESS;
49
+
50
+	if ( ( rc = resolv ( &comboot_resolver, name, NULL ) ) != 0 )
51
+		return rc;
52
+
53
+	while ( comboot_resolv_rc == -EINPROGRESS )
54
+		step();
55
+
56
+	*address = comboot_resolv_addr;
57
+	return comboot_resolv_rc;
58
+}

+ 1
- 0
src/config.h Voir le fichier

@@ -131,6 +131,7 @@
131 131
 #define	IMAGE_PXE		/* PXE image support */
132 132
 #define IMAGE_SCRIPT		/* gPXE script image support */
133 133
 #define IMAGE_BZIMAGE		/* Linux bzImage image support */
134
+#define IMAGE_COMBOOT		/* SYSLINUX COMBOOT image support */
134 135
 
135 136
 /* @END general.h */ 
136 137
 

+ 8
- 0
src/core/config.c Voir le fichier

@@ -158,6 +158,14 @@ REQUIRE_OBJECT ( bzimage );
158 158
 #ifdef IMAGE_ELTORITO
159 159
 REQUIRE_OBJECT ( eltorito );
160 160
 #endif
161
+#ifdef IMAGE_COMBOOT
162
+REQUIRE_OBJECT ( comboot );
163
+REQUIRE_OBJECT ( com32 );
164
+REQUIRE_OBJECT ( comboot_call );
165
+REQUIRE_OBJECT ( com32_call );
166
+REQUIRE_OBJECT ( com32_wrapper );
167
+REQUIRE_OBJECT ( comboot_resolv );
168
+#endif
161 169
 
162 170
 /*
163 171
  * Drag in all requested commands

+ 1
- 0
src/include/gpxe/features.h Voir le fichier

@@ -45,6 +45,7 @@
45 45
 #define DHCP_EB_FEATURE_NBI		0x20 /**< NBI format */
46 46
 #define DHCP_EB_FEATURE_PXE		0x21 /**< PXE format */
47 47
 #define DHCP_EB_FEATURE_ELF		0x22 /**< ELF format */
48
+#define DHCP_EB_FEATURE_COMBOOT		0x23 /**< COMBOOT format */
48 49
 
49 50
 /** @} */
50 51
 

+ 40
- 0
src/tests/comboot/shuffle-simple.asm Voir le fichier

@@ -0,0 +1,40 @@
1
+	bits 16
2
+	org 100h
3
+
4
+	jmp start
5
+	
6
+shuffle_start:
7
+	push 0xB800
8
+	pop es
9
+	mov cx, 80*24*2
10
+	mov ax, 'AA'
11
+	xor di, di
12
+	rep stosw
13
+.lbl:	jmp .lbl
14
+shuffle_end:
15
+	nop
16
+shuffle_len equ (shuffle_end - shuffle_start + 1)
17
+
18
+start:
19
+	; calculate physical address of shuffled part
20
+	xor eax, eax
21
+	push ds
22
+	pop ax
23
+	shl eax, 4
24
+	add ax, shuffle_start
25
+	mov dword [source], eax
26
+
27
+	mov ax, 0012h
28
+	mov di, shuffle_descriptors
29
+	mov cx, num_shuffle_descriptors
30
+	mov ebp, 0x7c00
31
+	int 22h
32
+	int3
33
+
34
+shuffle_descriptors:
35
+	dd 0x7C00
36
+source:	dd 0
37
+	dd shuffle_len
38
+
39
+num_shuffle_descriptors equ 1
40
+

+ 136
- 0
src/tests/comboot/version.asm Voir le fichier

@@ -0,0 +1,136 @@
1
+	bits 16
2
+	org 100h
3
+
4
+_start:
5
+	; first check for SYSLINUX
6
+	mov ah, 30h
7
+	int 21h
8
+
9
+	cmp eax, 59530000h
10
+	jne .not_syslinux
11
+	cmp ebx, 4c530000h
12
+	jne .not_syslinux
13
+	cmp ecx, 4e490000h
14
+	jne .not_syslinux
15
+	cmp edx, 58550000h
16
+	jne .not_syslinux
17
+
18
+	; now get syslinux version
19
+	mov ax, 0001h
20
+	int 22h
21
+
22
+	push cx
23
+	push dx
24
+	push di
25
+	push si
26
+	push es
27
+
28
+	; print version string
29
+	mov dx, str_version
30
+	mov ah, 09h
31
+	int 21h
32
+
33
+	pop es
34
+	pop bx
35
+	push es
36
+	mov ax, 0002h
37
+	int 22h
38
+
39
+	; print copyright string
40
+	mov dx, str_copyright
41
+	mov ah, 09h
42
+	int 21h
43
+
44
+	pop es
45
+	pop bx
46
+	mov ax, 0002h
47
+	int 22h
48
+
49
+	; print syslinux derivative id
50
+	mov dx, str_derivative
51
+	mov ah, 09h
52
+	int 21h
53
+
54
+	pop ax
55
+	call print_hex_byte
56
+
57
+	; print version number
58
+	mov dx, str_version_num
59
+	mov ah, 09h
60
+	int 21h
61
+
62
+	pop cx
63
+	push cx
64
+	mov ax, cx
65
+	and ax, 0FFh
66
+	call print_dec_word
67
+
68
+	mov dl, '.'
69
+	mov ah, 02h
70
+	int 21h
71
+
72
+	pop cx
73
+	mov ax, cx
74
+	shr ax, 8
75
+	call print_dec_word
76
+
77
+	ret
78
+
79
+
80
+.not_syslinux:
81
+	mov dx, str_not_syslinux
82
+	mov ah, 09h
83
+	int 21h
84
+	ret
85
+
86
+; input: al = byte to print in hex
87
+print_hex_byte:
88
+	push ax
89
+	shr al, 4
90
+	call print_hex_nybble
91
+	pop ax
92
+	call print_hex_nybble
93
+	ret
94
+
95
+; input: bottom half of al = nybble to print in hex
96
+print_hex_nybble:
97
+	push ax
98
+	mov bl, al
99
+	and bx, 1111b
100
+	mov dl, [str_hex + bx]
101
+	mov ah, 02h
102
+	int 21h
103
+	pop ax
104
+	ret
105
+
106
+str_hex: db "01234567890abcdef"
107
+
108
+; input: ax = word to print
109
+print_dec_word:
110
+	mov cx, 10
111
+	mov word [.count], 0
112
+.loop:
113
+	xor dx, dx
114
+	div cx
115
+	inc word [.count]
116
+	push dx
117
+	test ax, ax
118
+	jnz .loop
119
+
120
+.print:
121
+	pop dx
122
+	add dx, '0'
123
+	mov ah, 02h
124
+	int 21h
125
+	dec word [.count]
126
+	jnz .print
127
+
128
+	ret
129
+
130
+.count:	dw 0
131
+
132
+str_not_syslinux: db "Not SYSLINUX or derivative (running on DOS?)$"
133
+str_version: db "Version: $"
134
+str_copyright: db 10, "Copyright: $"
135
+str_derivative: db 10, "Derivative ID: 0x$"
136
+str_version_num: db 10, "Version number: $"

Chargement…
Annuler
Enregistrer