aboutsummaryrefslogtreecommitdiffstats
path: root/gpxe/src/arch/i386/image/eltorito.c
blob: 9d573106d3a03f25162809db193ec04b48d18515 (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
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * 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
 *
 * El Torito bootable ISO image format
 *
 */

#include <stdint.h>
#include <errno.h>
#include <assert.h>
#include <realmode.h>
#include <bootsector.h>
#include <int13.h>
#include <gpxe/uaccess.h>
#include <gpxe/image.h>
#include <gpxe/segment.h>
#include <gpxe/ramdisk.h>
#include <gpxe/init.h>

#define ISO9660_BLKSIZE 2048
#define ELTORITO_VOL_DESC_OFFSET ( 17 * ISO9660_BLKSIZE )

/** An El Torito Boot Record Volume Descriptor */
struct eltorito_vol_desc {
	/** Boot record indicator; must be 0 */
	uint8_t record_indicator;
	/** ISO-9660 identifier; must be "CD001" */
	uint8_t iso9660_id[5];
	/** Version, must be 1 */
	uint8_t version;
	/** Boot system indicator; must be "EL TORITO SPECIFICATION" */
	uint8_t system_indicator[32];
	/** Unused */
	uint8_t unused[32];
	/** Boot catalog sector */
	uint32_t sector;
} __attribute__ (( packed ));

/** An El Torito Boot Catalog Validation Entry */
struct eltorito_validation_entry {
	/** Header ID; must be 1 */
	uint8_t header_id;
	/** Platform ID
	 *
	 * 0 = 80x86
	 * 1 = PowerPC
	 * 2 = Mac
	 */
	uint8_t platform_id;
	/** Reserved */
	uint16_t reserved;
	/** ID string */
	uint8_t id_string[24];
	/** Checksum word */
	uint16_t checksum;
	/** Signature; must be 0xaa55 */
	uint16_t signature;
} __attribute__ (( packed ));

/** A bootable entry in the El Torito Boot Catalog */
struct eltorito_boot_entry {
	/** Boot indicator
	 *
	 * Must be @c ELTORITO_BOOTABLE for a bootable ISO image
	 */
	uint8_t indicator;
	/** Media type
	 *
	 */
	uint8_t media_type;
	/** Load segment */
	uint16_t load_segment;
	/** System type */
	uint8_t filesystem;
	/** Unused */
	uint8_t reserved_a;
	/** Sector count */
	uint16_t length;
	/** Starting sector */
	uint32_t start;
	/** Unused */
	uint8_t reserved_b[20];
} __attribute__ (( packed ));

/** Boot indicator for a bootable ISO image */
#define ELTORITO_BOOTABLE 0x88

/** El Torito media types */
enum eltorito_media_type {
	/** No emulation */
	ELTORITO_NO_EMULATION = 0,
};

struct image_type eltorito_image_type __image_type ( PROBE_NORMAL );

/**
 * Calculate 16-bit word checksum
 *
 * @v data		Data to checksum
 * @v len		Length (in bytes, must be even)
 * @ret sum		Checksum
 */
static unsigned int word_checksum ( void *data, size_t len ) {
	uint16_t *words;
	uint16_t sum = 0;

	for ( words = data ; len ; words++, len -= 2 ) {
		sum += *words;
	}
	return sum;
}

/**
 * Execute El Torito image
 *
 * @v image		El Torito image
 * @ret rc		Return status code
 */
static int eltorito_exec ( struct image *image ) {
	struct ramdisk ramdisk;
	struct int13_drive int13_drive;
	unsigned int load_segment = image->priv.ul;
	unsigned int load_offset = ( load_segment ? 0 : 0x7c00 );
	int rc;

	memset ( &ramdisk, 0, sizeof ( ramdisk ) );
	init_ramdisk ( &ramdisk, image->data, image->len, ISO9660_BLKSIZE );
	
	memset ( &int13_drive, 0, sizeof ( int13_drive ) );
	int13_drive.blockdev = &ramdisk.blockdev;
	register_int13_drive ( &int13_drive );

	if ( ( rc = call_bootsector ( load_segment, load_offset, 
				      int13_drive.drive ) ) != 0 ) {
		DBGC ( image, "ElTorito %p boot failed: %s\n",
		       image, strerror ( rc ) );
		goto err;
	}
	
	rc = -ECANCELED; /* -EIMPOSSIBLE */
 err:
	unregister_int13_drive ( &int13_drive );
	return rc;
}

/**
 * Read and verify El Torito Boot Record Volume Descriptor
 *
 * @v image		El Torito file
 * @ret catalog_offset	Offset of Boot Catalog
 * @ret rc		Return status code
 */
static int eltorito_read_voldesc ( struct image *image,
				   unsigned long *catalog_offset ) {
	static const struct eltorito_vol_desc vol_desc_signature = {
		.record_indicator = 0,
		.iso9660_id = "CD001",
		.version = 1,
		.system_indicator = "EL TORITO SPECIFICATION",
	};
	struct eltorito_vol_desc vol_desc;

	/* Sanity check */
	if ( image->len < ( ELTORITO_VOL_DESC_OFFSET + ISO9660_BLKSIZE ) ) {
		DBGC ( image, "ElTorito %p too short\n", image );
		return -ENOEXEC;
	}

	/* Read and verify Boot Record Volume Descriptor */
	copy_from_user ( &vol_desc, image->data, ELTORITO_VOL_DESC_OFFSET,
			 sizeof ( vol_desc ) );
	if ( memcmp ( &vol_desc, &vol_desc_signature,
		      offsetof ( typeof ( vol_desc ), sector ) ) != 0 ) {
		DBGC ( image, "ElTorito %p invalid Boot Record Volume "
		       "Descriptor\n", image );
		return -ENOEXEC;
	}
	*catalog_offset = ( vol_desc.sector * ISO9660_BLKSIZE );

	DBGC ( image, "ElTorito %p boot catalog at offset %#lx\n",
	       image, *catalog_offset );

	return 0;
}

/**
 * Read and verify El Torito Boot Catalog
 *
 * @v image		El Torito file
 * @v catalog_offset	Offset of Boot Catalog
 * @ret boot_entry	El Torito boot entry
 * @ret rc		Return status code
 */
static int eltorito_read_catalog ( struct image *image,
				   unsigned long catalog_offset,
				   struct eltorito_boot_entry *boot_entry ) {
	struct eltorito_validation_entry validation_entry;

	/* Sanity check */
	if ( image->len < ( catalog_offset + ISO9660_BLKSIZE ) ) {
		DBGC ( image, "ElTorito %p bad boot catalog offset %#lx\n",
		       image, catalog_offset );
		return -ENOEXEC;
	}

	/* Read and verify the Validation Entry of the Boot Catalog */
	copy_from_user ( &validation_entry, image->data, catalog_offset,
			 sizeof ( validation_entry ) );
	if ( word_checksum ( &validation_entry,
			     sizeof ( validation_entry ) ) != 0 ) {
		DBGC ( image, "ElTorito %p bad Validation Entry checksum\n",
		       image );
		return -ENOEXEC;
	}

	/* Read and verify the Initial/Default entry */
	copy_from_user ( boot_entry, image->data,
			 ( catalog_offset + sizeof ( validation_entry ) ),
			 sizeof ( *boot_entry ) );
	if ( boot_entry->indicator != ELTORITO_BOOTABLE ) {
		DBGC ( image, "ElTorito %p not bootable\n", image );
		return -ENOEXEC;
	}
	if ( boot_entry->media_type != ELTORITO_NO_EMULATION ) {
		DBGC ( image, "ElTorito %p cannot support media type %d\n",
		       image, boot_entry->media_type );
		return -ENOTSUP;
	}

	DBGC ( image, "ElTorito %p media type %d segment %04x\n",
	       image, boot_entry->media_type, boot_entry->load_segment );

	return 0;
}

/**
 * Load El Torito virtual disk image into memory
 *
 * @v image		El Torito file
 * @v boot_entry	El Torito boot entry
 * @ret rc		Return status code
 */
static int eltorito_load_disk ( struct image *image,
				struct eltorito_boot_entry *boot_entry ) {
	unsigned long start = ( boot_entry->start * ISO9660_BLKSIZE );
	unsigned long length = ( boot_entry->length * ISO9660_BLKSIZE );
	unsigned int load_segment;
	userptr_t buffer;
	int rc;

	/* Sanity check */
	if ( image->len < ( start + length ) ) {
		DBGC ( image, "ElTorito %p virtual disk lies outside image\n",
		       image );
		return -ENOEXEC;
	}
	DBGC ( image, "ElTorito %p virtual disk at %#lx+%#lx\n",
	       image, start, length );

	/* Calculate load address */
	load_segment = boot_entry->load_segment;
	buffer = real_to_user ( load_segment, ( load_segment ? 0 : 0x7c00 ) );

	/* Verify and prepare segment */
	if ( ( rc = prep_segment ( buffer, length, length ) ) != 0 ) {
		DBGC ( image, "ElTorito %p could not prepare segment: %s\n",
		       image, strerror ( rc ) );
		return rc;
	}

	/* Copy image to segment */
	memcpy_user ( buffer, 0, image->data, start, length );

	return 0;
}

/**
 * Load El Torito image into memory
 *
 * @v image		El Torito file
 * @ret rc		Return status code
 */
static int eltorito_load ( struct image *image ) {
	struct eltorito_boot_entry boot_entry;
	unsigned long bootcat_offset;
	int rc;

	/* Read Boot Record Volume Descriptor, if present */
	if ( ( rc = eltorito_read_voldesc ( image, &bootcat_offset ) ) != 0 )
		return rc;

	/* This is an El Torito image, valid or otherwise */
	if ( ! image->type )
		image->type = &eltorito_image_type;

	/* Read Boot Catalog */
	if ( ( rc = eltorito_read_catalog ( image, bootcat_offset,
					    &boot_entry ) ) != 0 )
		return rc;

	/* Load Virtual Disk image */
	if ( ( rc = eltorito_load_disk ( image, &boot_entry ) ) != 0 )
		return rc;

	/* Record load segment in image private data field */
	image->priv.ul = boot_entry.load_segment;

	return 0;
}

/** El Torito image type */
struct image_type eltorito_image_type __image_type ( PROBE_NORMAL ) = {
	.name = "El Torito",
	.load = eltorito_load,
	.exec = eltorito_exec,
};