aboutsummaryrefslogtreecommitdiffstats
path: root/gpxe/src/arch/i386/image/comboot.c
blob: a00b2b954073e30731b58389b097e3f9913619d1 (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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 * Copyright (C) 2008 Daniel Verkamp <daniel@drv.nu>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/**
 * @file
 *
 * SYSLINUX COMBOOT (16-bit) image format
 *
 */

FILE_LICENCE ( GPL2_OR_LATER );

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <assert.h>
#include <realmode.h>
#include <basemem.h>
#include <comboot.h>
#include <gpxe/uaccess.h>
#include <gpxe/image.h>
#include <gpxe/segment.h>
#include <gpxe/init.h>
#include <gpxe/features.h>

FEATURE ( FEATURE_IMAGE, "COMBOOT", DHCP_EB_FEATURE_COMBOOT, 1 );

struct image_type comboot_image_type __image_type ( PROBE_NORMAL );

/**
 * COMBOOT PSP, copied to offset 0 of code segment
 */
struct comboot_psp {
	/** INT 20 instruction, executed if COMBOOT image returns with RET */
	uint16_t int20;
	/** Segment of first non-free paragraph of memory */
	uint16_t first_non_free_para;
};

/** Offset in PSP of command line */
#define COMBOOT_PSP_CMDLINE_OFFSET 0x81

/** Maximum length of command line in PSP
 * (127 bytes minus space and CR) */
#define COMBOOT_MAX_CMDLINE_LEN    125


/**
 * Copy command line to PSP
 * 
 * @v image		COMBOOT image
 */
static void comboot_copy_cmdline ( struct image * image, userptr_t seg_userptr ) {
	const char *cmdline = ( image->cmdline ? image->cmdline : "" );
	int cmdline_len = strlen ( cmdline );
	if( cmdline_len > COMBOOT_MAX_CMDLINE_LEN )
		cmdline_len = COMBOOT_MAX_CMDLINE_LEN;
	uint8_t len_byte = cmdline_len;
	char spc = ' ', cr = '\r';

	/* Copy length to byte before command line */
	copy_to_user ( seg_userptr, COMBOOT_PSP_CMDLINE_OFFSET - 1,
	               &len_byte, 1 );

	/* Command line starts with space */
	copy_to_user ( seg_userptr,
	               COMBOOT_PSP_CMDLINE_OFFSET,
	               &spc, 1 );

	/* Copy command line */
	copy_to_user ( seg_userptr,
	               COMBOOT_PSP_CMDLINE_OFFSET + 1,
	               cmdline, cmdline_len );

	/* Command line ends with CR */
	copy_to_user ( seg_userptr,
	               COMBOOT_PSP_CMDLINE_OFFSET + cmdline_len + 1,
	               &cr, 1 );
}

/**
 * Initialize PSP
 * 
 * @v image		COMBOOT image
 * @v seg_userptr	segment to initialize
 */
static void comboot_init_psp ( struct image * image, userptr_t seg_userptr ) {
	struct comboot_psp psp;

	/* Fill PSP */

	/* INT 20h instruction, byte order reversed */
	psp.int20 = 0x20CD;

	/* get_fbms() returns BIOS free base memory counter, which is in
	 * kilobytes; x * 1024 / 16 == x * 64 == x << 6 */
	psp.first_non_free_para = get_fbms() << 6;

	DBGC ( image, "COMBOOT %p: first non-free paragraph = 0x%x\n",
	       image, psp.first_non_free_para );

	/* Copy the PSP to offset 0 of segment.
	 * The rest of the PSP was already zeroed by
	 * comboot_prepare_segment. */
	copy_to_user ( seg_userptr, 0, &psp, sizeof( psp ) );

	/* Copy the command line to the PSP */
	comboot_copy_cmdline ( image, seg_userptr );
}

/**
 * Execute COMBOOT image
 *
 * @v image		COMBOOT image
 * @ret rc		Return status code
 */
static int comboot_exec ( struct image *image ) {
	userptr_t seg_userptr = real_to_user ( COMBOOT_PSP_SEG, 0 );
	int state;

	state = rmsetjmp ( comboot_return );

	switch ( state ) {
	case 0: /* First time through; invoke COMBOOT program */

		/* Initialize PSP */
		comboot_init_psp ( image, seg_userptr );

		/* Hook COMBOOT API interrupts */
		hook_comboot_interrupts();

		DBGC ( image, "executing 16-bit COMBOOT image at %4x:0100\n",
		       COMBOOT_PSP_SEG );

		/* Unregister image, so that a "boot" command doesn't
		 * throw us into an execution loop.  We never
		 * reregister ourselves; COMBOOT images expect to be
		 * removed on exit.
		 */
		unregister_image ( image );

		/* Store stack segment at 0x38 and stack pointer at 0x3A
		 * in the PSP and jump to the image */
		__asm__ __volatile__ (
		    REAL_CODE ( /* Save return address with segment on old stack */
				    "popw %%ax\n\t"
				    "pushw %%cs\n\t"
				    "pushw %%ax\n\t"
				    /* Set DS=ES=segment with image */
				    "movw %w0, %%ds\n\t"
				    "movw %w0, %%es\n\t"
				    /* Set SS:SP to new stack (end of image segment) */
				    "movw %w0, %%ss\n\t"
				    "xor %%sp, %%sp\n\t"
				    "pushw $0\n\t"
				    "pushw %w0\n\t"
				    "pushw $0x100\n\t"
				    /* Zero registers (some COM files assume GP regs are 0) */
				    "xorw %%ax, %%ax\n\t"
				    "xorw %%bx, %%bx\n\t"
				    "xorw %%cx, %%cx\n\t"
				    "xorw %%dx, %%dx\n\t"
				    "xorw %%si, %%si\n\t"
				    "xorw %%di, %%di\n\t"
				    "xorw %%bp, %%bp\n\t"
				    "lret\n\t" )
					 : : "r" ( COMBOOT_PSP_SEG ) : "eax" );
		DBGC ( image, "COMBOOT %p: returned\n", image );
		break;

	case COMBOOT_EXIT:
		DBGC ( image, "COMBOOT %p: exited\n", image );
		break;

	case COMBOOT_EXIT_RUN_KERNEL:
		DBGC ( image, "COMBOOT %p: exited to run kernel %p\n",
		       image, comboot_replacement_image );
		image->replacement = comboot_replacement_image;
		comboot_replacement_image = NULL;
		image_autoload ( image->replacement );
		break;

	case COMBOOT_EXIT_COMMAND:
		DBGC ( image, "COMBOOT %p: exited after executing command\n",
		       image );
		break;

	default:
		assert ( 0 );
		break;
	}

	unhook_comboot_interrupts();
	comboot_force_text_mode();

	return 0;
}

/**
 * Check image name extension
 * 
 * @v image		COMBOOT image
 * @ret rc		Return status code
 */
static int comboot_identify ( struct image *image ) {
	const char *ext;

	ext = strrchr( image->name, '.' );

	if ( ! ext ) {
		DBGC ( image, "COMBOOT %p: no extension\n",
		       image );
		return -ENOEXEC;
	}

	++ext;

	if ( strcasecmp( ext, "com" ) && strcasecmp( ext, "cbt" ) ) {
		DBGC ( image, "COMBOOT %p: unrecognized extension %s\n",
		       image, ext );
		return -ENOEXEC;
	}

	return 0;
}

/**
 * Load COMBOOT image into memory, preparing a segment and returning it
 * @v image		COMBOOT image
 * @ret rc		Return status code
 */
static int comboot_prepare_segment ( struct image *image )
{
	userptr_t seg_userptr;
	size_t filesz, memsz;
	int rc;

	/* Load image in segment */
	seg_userptr = real_to_user ( COMBOOT_PSP_SEG, 0 );

	/* Allow etra 0x100 bytes before image for PSP */
	filesz = image->len + 0x100; 

	/* Ensure the entire 64k segment is free */
	memsz = 0xFFFF;

	/* Prepare, verify, and load the real-mode segment */
	if ( ( rc = prep_segment ( seg_userptr, filesz, memsz ) ) != 0 ) {
		DBGC ( image, "COMBOOT %p: could not prepare segment: %s\n",
		       image, strerror ( rc ) );
		return rc;
	}

	/* Zero PSP */
	memset_user ( seg_userptr, 0, 0, 0x100 );

	/* Copy image to segment:0100 */
	memcpy_user ( seg_userptr, 0x100, image->data, 0, image->len );

	return 0;
}

/**
 * Load COMBOOT image into memory
 *
 * @v image		COMBOOT image
 * @ret rc		Return status code
 */
static int comboot_load ( struct image *image ) {
	int rc;

	DBGC ( image, "COMBOOT %p: name '%s'\n",
	       image, image->name );

	/* Check if this is a COMBOOT image */
	if ( ( rc = comboot_identify ( image ) ) != 0 ) {
		
		return rc;
	}

	/* This is a 16-bit COMBOOT image, valid or otherwise */
	if ( ! image->type )
		image->type = &comboot_image_type;
	
	/* Sanity check for filesize */
	if( image->len >= 0xFF00 ) {
		DBGC( image, "COMBOOT %p: image too large\n",
		      image );
		return -ENOEXEC;
	}

	/* Prepare segment and load image */
	if ( ( rc = comboot_prepare_segment ( image ) ) != 0 ) {
		return rc;
	}

	return 0;
}

/** SYSLINUX COMBOOT (16-bit) image type */
struct image_type comboot_image_type __image_type ( PROBE_NORMAL ) = {
	.name = "COMBOOT",
	.load = comboot_load,
	.exec = comboot_exec,
};