Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / interface / pcbios / int13.c
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <limits.h>
25 #include <byteswap.h>
26 #include <errno.h>
27 #include <assert.h>
28 #include <ipxe/list.h>
29 #include <ipxe/blockdev.h>
30 #include <ipxe/io.h>
31 #include <ipxe/open.h>
32 #include <ipxe/uri.h>
33 #include <ipxe/process.h>
34 #include <ipxe/xfer.h>
35 #include <ipxe/retry.h>
36 #include <ipxe/timer.h>
37 #include <ipxe/acpi.h>
38 #include <ipxe/sanboot.h>
39 #include <ipxe/device.h>
40 #include <ipxe/pci.h>
41 #include <ipxe/iso9660.h>
42 #include <ipxe/eltorito.h>
43 #include <realmode.h>
44 #include <bios.h>
45 #include <biosint.h>
46 #include <bootsector.h>
47 #include <int13.h>
48
49 /** @file
50  *
51  * INT 13 emulation
52  *
53  * This module provides a mechanism for exporting block devices via
54  * the BIOS INT 13 disk interrupt interface.  
55  *
56  */
57
58 /**
59  * Overall timeout for INT 13 commands (independent of underlying device
60  *
61  * Underlying devices should ideally never become totally stuck.
62  * However, if they do, then the INT 13 mechanism provides no means
63  * for the caller to cancel the operation, and the machine appears to
64  * hang.  Use an overall timeout for all commands to avoid this
65  * problem and bounce timeout failures to the caller.
66  */
67 #define INT13_COMMAND_TIMEOUT ( 15 * TICKS_PER_SEC )
68
69 /** An INT 13 emulated drive */
70 struct int13_drive {
71         /** Reference count */
72         struct refcnt refcnt;
73         /** List of all registered drives */
74         struct list_head list;
75
76         /** Block device URI */
77         struct uri *uri;
78         /** Underlying block device interface */
79         struct interface block;
80
81         /** BIOS in-use drive number (0x00-0xff) */
82         unsigned int drive;
83         /** BIOS natural drive number (0x00-0xff)
84          *
85          * This is the drive number that would have been assigned by
86          * 'naturally' appending the drive to the end of the BIOS
87          * drive list.
88          *
89          * If the emulated drive replaces a preexisting drive, this is
90          * the drive number that the preexisting drive gets remapped
91          * to.
92          */
93         unsigned int natural_drive;
94
95         /** Block device capacity */
96         struct block_device_capacity capacity;
97         /** INT 13 emulated blocksize shift
98          *
99          * To allow for emulation of CD-ROM access, this represents
100          * the left-shift required to translate from INT 13 blocks to
101          * underlying blocks.
102          */
103         unsigned int blksize_shift;
104
105         /** Number of cylinders
106          *
107          * The cylinder number field in an INT 13 call is ten bits
108          * wide, giving a maximum of 1024 cylinders.  Conventionally,
109          * when the 7.8GB limit of a CHS address is exceeded, it is
110          * the number of cylinders that is increased beyond the
111          * addressable limit.
112          */
113         unsigned int cylinders;
114         /** Number of heads
115          *
116          * The head number field in an INT 13 call is eight bits wide,
117          * giving a maximum of 256 heads.  However, apparently all
118          * versions of MS-DOS up to and including Win95 fail with 256
119          * heads, so the maximum encountered in practice is 255.
120          */
121         unsigned int heads;
122         /** Number of sectors per track
123          *
124          * The sector number field in an INT 13 call is six bits wide,
125          * giving a maximum of 63 sectors, since sector numbering
126          * (unlike head and cylinder numbering) starts at 1, not 0.
127          */
128         unsigned int sectors_per_track;
129
130         /** Drive is a CD-ROM */
131         int is_cdrom;
132         /** Address of El Torito boot catalog (if any) */
133         unsigned int boot_catalog;
134
135         /** Underlying device status, if in error */
136         int block_rc;
137         /** Status of last operation */
138         int last_status;
139 };
140
141 /** Vector for chaining to other INT 13 handlers */
142 static struct segoff __text16 ( int13_vector );
143 #define int13_vector __use_text16 ( int13_vector )
144
145 /** Assembly wrapper */
146 extern void int13_wrapper ( void );
147
148 /** Dummy floppy disk parameter table */
149 static struct int13_fdd_parameters __data16 ( int13_fdd_params ) = {
150         /* 512 bytes per sector */
151         .bytes_per_sector = 0x02,
152         /* Highest sectors per track that we ever return */
153         .sectors_per_track = 48,
154 };
155 #define int13_fdd_params __use_data16 ( int13_fdd_params )
156
157 /** List of registered emulated drives */
158 static LIST_HEAD ( int13s );
159
160 /**
161  * Equipment word
162  *
163  * This is a cached copy of the BIOS Data Area equipment word at
164  * 40:10.
165  */
166 static uint16_t equipment_word;
167
168 /**
169  * Number of BIOS floppy disk drives
170  *
171  * This is derived from the equipment word.  It is held in .text16 to
172  * allow for easy access by the INT 13,08 wrapper.
173  */
174 static uint8_t __text16 ( num_fdds );
175 #define num_fdds __use_text16 ( num_fdds )
176
177 /**
178  * Number of BIOS hard disk drives
179  *
180  * This is a cached copy of the BIOS Data Area number of hard disk
181  * drives at 40:75.  It is held in .text16 to allow for easy access by
182  * the INT 13,08 wrapper.
183  */
184 static uint8_t __text16 ( num_drives );
185 #define num_drives __use_text16 ( num_drives )
186
187 /**
188  * Calculate INT 13 drive sector size
189  *
190  * @v int13             Emulated drive
191  * @ret blksize         Sector size
192  */
193 static inline size_t int13_blksize ( struct int13_drive *int13 ) {
194         return ( int13->capacity.blksize << int13->blksize_shift );
195 }
196
197 /**
198  * Calculate INT 13 drive capacity
199  *
200  * @v int13             Emulated drive
201  * @ret blocks          Number of blocks
202  */
203 static inline uint64_t int13_capacity ( struct int13_drive *int13 ) {
204         return ( int13->capacity.blocks >> int13->blksize_shift );
205 }
206
207 /**
208  * Calculate INT 13 drive capacity (limited to 32 bits)
209  *
210  * @v int13             Emulated drive
211  * @ret blocks          Number of blocks
212  */
213 static inline uint32_t int13_capacity32 ( struct int13_drive *int13 ) {
214         uint64_t capacity = int13_capacity ( int13 );
215         return ( ( capacity <= 0xffffffffUL ) ? capacity : 0xffffffff );
216 }
217
218 /**
219  * Test if INT 13 drive is a floppy disk drive
220  *
221  * @v int13             Emulated drive
222  * @ret is_fdd          Emulated drive is a floppy disk
223  */
224 static inline int int13_is_fdd ( struct int13_drive *int13 ) {
225         return ( ! ( int13->drive & 0x80 ) );
226 }
227
228 /** An INT 13 command */
229 struct int13_command {
230         /** Status */
231         int rc;
232         /** INT 13 drive */
233         struct int13_drive *int13;
234         /** Underlying block device interface */
235         struct interface block;
236         /** Command timeout timer */
237         struct retry_timer timer;
238 };
239
240 /**
241  * Record INT 13 drive capacity
242  *
243  * @v command           INT 13 command
244  * @v capacity          Block device capacity
245  */
246 static void int13_command_capacity ( struct int13_command *command,
247                                      struct block_device_capacity *capacity ) {
248         memcpy ( &command->int13->capacity, capacity,
249                  sizeof ( command->int13->capacity ) );
250 }
251
252 /**
253  * Close INT 13 command
254  *
255  * @v command           INT 13 command
256  * @v rc                Reason for close
257  */
258 static void int13_command_close ( struct int13_command *command, int rc ) {
259         intf_restart ( &command->block, rc );
260         stop_timer ( &command->timer );
261         command->rc = rc;
262 }
263
264 /**
265  * Handle INT 13 command timer expiry
266  *
267  * @v timer             Timer
268  */
269 static void int13_command_expired ( struct retry_timer *timer,
270                                     int over __unused ) {
271         struct int13_command *command =
272                 container_of ( timer, struct int13_command, timer );
273
274         int13_command_close ( command, -ETIMEDOUT );
275 }
276
277 /** INT 13 command interface operations */
278 static struct interface_operation int13_command_op[] = {
279         INTF_OP ( intf_close, struct int13_command *, int13_command_close ),
280         INTF_OP ( block_capacity, struct int13_command *,
281                   int13_command_capacity ),
282 };
283
284 /** INT 13 command interface descriptor */
285 static struct interface_descriptor int13_command_desc =
286         INTF_DESC ( struct int13_command, block, int13_command_op );
287
288 /**
289  * Open (or reopen) INT 13 emulated drive underlying block device
290  *
291  * @v int13             Emulated drive
292  * @ret rc              Return status code
293  */
294 static int int13_reopen_block ( struct int13_drive *int13 ) {
295         int rc;
296
297         /* Close any existing block device */
298         intf_restart ( &int13->block, -ECONNRESET );
299
300         /* Open block device */
301         if ( ( rc = xfer_open_uri ( &int13->block, int13->uri ) ) != 0 ) {
302                 DBGC ( int13, "INT13 drive %02x could not reopen block "
303                        "device: %s\n", int13->drive, strerror ( rc ) );
304                 int13->block_rc = rc;
305                 return rc;
306         }
307
308         /* Clear block device error status */
309         int13->block_rc = 0;
310
311         return 0;
312 }
313
314 /**
315  * Prepare to issue INT 13 command
316  *
317  * @v command           INT 13 command
318  * @v int13             Emulated drive
319  * @ret rc              Return status code
320  */
321 static int int13_command_start ( struct int13_command *command,
322                                  struct int13_drive *int13 ) {
323         int rc;
324
325         /* Sanity check */
326         assert ( command->int13 == NULL );
327         assert ( ! timer_running ( &command->timer ) );
328
329         /* Reopen block device if necessary */
330         if ( ( int13->block_rc != 0 ) &&
331              ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) )
332                 return rc;
333
334         /* Initialise command */
335         command->rc = -EINPROGRESS;
336         command->int13 = int13;
337         start_timer_fixed ( &command->timer, INT13_COMMAND_TIMEOUT );
338
339         /* Wait for block control interface to become ready */
340         while ( ( command->rc == -EINPROGRESS ) &&
341                 ( xfer_window ( &int13->block ) == 0 ) ) {
342                 step();
343         }
344
345         return ( ( command->rc == -EINPROGRESS ) ?
346                  int13->block_rc : command->rc );
347 }
348
349 /**
350  * Wait for INT 13 command to complete
351  *
352  * @v command           INT 13 command
353  * @ret rc              Return status code
354  */
355 static int int13_command_wait ( struct int13_command *command ) {
356
357         /* Sanity check */
358         assert ( timer_running ( &command->timer ) );
359
360         /* Wait for command to complete */
361         while ( command->rc == -EINPROGRESS )
362                 step();
363
364         assert ( ! timer_running ( &command->timer ) );
365         return command->rc;
366 }
367
368 /**
369  * Terminate INT 13 command
370  *
371  * @v command           INT 13 command
372  */
373 static void int13_command_stop ( struct int13_command *command ) {
374         stop_timer ( &command->timer );
375         command->int13 = NULL;
376 }
377
378 /** The single active INT 13 command */
379 static struct int13_command int13_command = {
380         .block = INTF_INIT ( int13_command_desc ),
381         .timer = TIMER_INIT ( int13_command_expired ),
382 };
383
384 /**
385  * Read from or write to INT 13 drive
386  *
387  * @v int13             Emulated drive
388  * @v lba               Starting logical block address
389  * @v count             Number of logical blocks
390  * @v buffer            Data buffer
391  * @v block_rw          Block read/write method
392  * @ret rc              Return status code
393  */
394 static int int13_rw ( struct int13_drive *int13, uint64_t lba,
395                       unsigned int count, userptr_t buffer,
396                       int ( * block_rw ) ( struct interface *control,
397                                            struct interface *data,
398                                            uint64_t lba, unsigned int count,
399                                            userptr_t buffer, size_t len ) ) {
400         struct int13_command *command = &int13_command;
401         unsigned int frag_count;
402         size_t frag_len;
403         int rc;
404
405         /* Translate to underlying blocksize */
406         lba <<= int13->blksize_shift;
407         count <<= int13->blksize_shift;
408
409         while ( count ) {
410
411                 /* Determine fragment length */
412                 frag_count = count;
413                 if ( frag_count > int13->capacity.max_count )
414                         frag_count = int13->capacity.max_count;
415                 frag_len = ( int13->capacity.blksize * frag_count );
416
417                 /* Issue command */
418                 if ( ( ( rc = int13_command_start ( command, int13 ) ) != 0 ) ||
419                      ( ( rc = block_rw ( &int13->block, &command->block, lba,
420                                          frag_count, buffer,
421                                          frag_len ) ) != 0 ) ||
422                      ( ( rc = int13_command_wait ( command ) ) != 0 ) ) {
423                         int13_command_stop ( command );
424                         return rc;
425                 }
426                 int13_command_stop ( command );
427
428                 /* Move to next fragment */
429                 lba += frag_count;
430                 count -= frag_count;
431                 buffer = userptr_add ( buffer, frag_len );
432         }
433
434         return 0;
435 }
436
437 /**
438  * Read INT 13 drive capacity
439  *
440  * @v int13             Emulated drive
441  * @ret rc              Return status code
442  */
443 static int int13_read_capacity ( struct int13_drive *int13 ) {
444         struct int13_command *command = &int13_command;
445         int rc;
446
447         /* Issue command */
448         if ( ( ( rc = int13_command_start ( command, int13 ) ) != 0 ) ||
449              ( ( rc = block_read_capacity ( &int13->block,
450                                             &command->block ) ) != 0 ) ||
451              ( ( rc = int13_command_wait ( command ) ) != 0 ) ) {
452                 int13_command_stop ( command );
453                 return rc;
454         }
455
456         int13_command_stop ( command );
457         return 0;
458 }
459
460 /**
461  * Parse ISO9660 parameters
462  *
463  * @v int13             Emulated drive
464  * @v scratch           Scratch area for single-sector reads
465  * @ret rc              Return status code
466  *
467  * Reads and parses ISO9660 parameters, if present.
468  */
469 static int int13_parse_iso9660 ( struct int13_drive *int13, void *scratch ) {
470         static const struct iso9660_primary_descriptor_fixed primary_check = {
471                 .type = ISO9660_TYPE_PRIMARY,
472                 .id = ISO9660_ID,
473         };
474         struct iso9660_primary_descriptor *primary = scratch;
475         static const struct eltorito_descriptor_fixed boot_check = {
476                 .type = ISO9660_TYPE_BOOT,
477                 .id = ISO9660_ID,
478                 .version = 1,
479                 .system_id = "EL TORITO SPECIFICATION",
480         };
481         struct eltorito_descriptor *boot = scratch;
482         unsigned int blksize;
483         unsigned int blksize_shift;
484         int rc;
485
486         /* Calculate required blocksize shift */
487         blksize = int13_blksize ( int13 );
488         blksize_shift = 0;
489         while ( blksize < ISO9660_BLKSIZE ) {
490                 blksize <<= 1;
491                 blksize_shift++;
492         }
493         if ( blksize > ISO9660_BLKSIZE ) {
494                 /* Do nothing if the blksize is invalid for CD-ROM access */
495                 return 0;
496         }
497
498         /* Read primary volume descriptor */
499         if ( ( rc = int13_rw ( int13,
500                                ( ISO9660_PRIMARY_LBA << blksize_shift ), 1,
501                                virt_to_user ( primary ), block_read ) ) != 0 ){
502                 DBGC ( int13, "INT13 drive %02x could not read ISO9660 "
503                        "primary volume descriptor: %s\n",
504                        int13->drive, strerror ( rc ) );
505                 return rc;
506         }
507
508         /* Do nothing unless this is an ISO image */
509         if ( memcmp ( primary, &primary_check, sizeof ( primary_check ) ) != 0 )
510                 return 0;
511         DBGC ( int13, "INT13 drive %02x contains an ISO9660 filesystem; "
512                "treating as CD-ROM\n", int13->drive );
513         int13->is_cdrom = 1;
514
515         /* Read boot record volume descriptor */
516         if ( ( rc = int13_rw ( int13,
517                                ( ELTORITO_LBA << blksize_shift ), 1,
518                                virt_to_user ( boot ), block_read ) ) != 0 ) {
519                 DBGC ( int13, "INT13 drive %02x could not read El Torito boot "
520                        "record volume descriptor: %s\n",
521                        int13->drive, strerror ( rc ) );
522                 return rc;
523         }
524
525         /* Check for an El Torito boot catalog */
526         if ( memcmp ( boot, &boot_check, sizeof ( boot_check ) ) == 0 ) {
527                 int13->boot_catalog = boot->sector;
528                 DBGC ( int13, "INT13 drive %02x has an El Torito boot catalog "
529                        "at LBA %08x\n", int13->drive, int13->boot_catalog );
530         } else {
531                 DBGC ( int13, "INT13 drive %02x has no El Torito boot "
532                        "catalog\n", int13->drive );
533         }
534
535         /* Configure drive for no-emulation CD-ROM access */
536         int13->blksize_shift += blksize_shift;
537
538         return 0;
539 }
540
541 /**
542  * Guess INT 13 hard disk drive geometry
543  *
544  * @v int13             Emulated drive
545  * @v scratch           Scratch area for single-sector reads
546  * @ret heads           Guessed number of heads
547  * @ret sectors         Guessed number of sectors per track
548  * @ret rc              Return status code
549  *
550  * Guesses the drive geometry by inspecting the partition table.
551  */
552 static int int13_guess_geometry_hdd ( struct int13_drive *int13, void *scratch,
553                                       unsigned int *heads,
554                                       unsigned int *sectors ) {
555         struct master_boot_record *mbr = scratch;
556         struct partition_table_entry *partition;
557         unsigned int i;
558         int rc;
559
560         /* Default guess is xx/255/63 */
561         *heads = 255;
562         *sectors = 63;
563
564         /* Read partition table */
565         if ( ( rc = int13_rw ( int13, 0, 1, virt_to_user ( mbr ),
566                                block_read ) ) != 0 ) {
567                 DBGC ( int13, "INT13 drive %02x could not read "
568                        "partition table to guess geometry: %s\n",
569                        int13->drive, strerror ( rc ) );
570                 return rc;
571         }
572         DBGC2 ( int13, "INT13 drive %02x has MBR:\n", int13->drive );
573         DBGC2_HDA ( int13, 0, mbr, sizeof ( *mbr ) );
574         DBGC ( int13, "INT13 drive %02x has signature %08x\n",
575                int13->drive, mbr->signature );
576
577         /* Scan through partition table and modify guesses for
578          * heads and sectors_per_track if we find any used
579          * partitions.
580          */
581         for ( i = 0 ; i < 4 ; i++ ) {
582                 partition = &mbr->partitions[i];
583                 if ( ! partition->type )
584                         continue;
585                 *heads = ( PART_HEAD ( partition->chs_end ) + 1 );
586                 *sectors = PART_SECTOR ( partition->chs_end );
587                 DBGC ( int13, "INT13 drive %02x guessing C/H/S xx/%d/%d based "
588                        "on partition %d\n",
589                        int13->drive, *heads, *sectors, ( i + 1 ) );
590         }
591
592         return 0;
593 }
594
595 /** Recognised floppy disk geometries */
596 static const struct int13_fdd_geometry int13_fdd_geometries[] = {
597         INT13_FDD_GEOMETRY ( 40, 1, 8 ),
598         INT13_FDD_GEOMETRY ( 40, 1, 9 ),
599         INT13_FDD_GEOMETRY ( 40, 2, 8 ),
600         INT13_FDD_GEOMETRY ( 40, 1, 9 ),
601         INT13_FDD_GEOMETRY ( 80, 2, 8 ),
602         INT13_FDD_GEOMETRY ( 80, 2, 9 ),
603         INT13_FDD_GEOMETRY ( 80, 2, 15 ),
604         INT13_FDD_GEOMETRY ( 80, 2, 18 ),
605         INT13_FDD_GEOMETRY ( 80, 2, 20 ),
606         INT13_FDD_GEOMETRY ( 80, 2, 21 ),
607         INT13_FDD_GEOMETRY ( 82, 2, 21 ),
608         INT13_FDD_GEOMETRY ( 83, 2, 21 ),
609         INT13_FDD_GEOMETRY ( 80, 2, 22 ),
610         INT13_FDD_GEOMETRY ( 80, 2, 23 ),
611         INT13_FDD_GEOMETRY ( 80, 2, 24 ),
612         INT13_FDD_GEOMETRY ( 80, 2, 36 ),
613         INT13_FDD_GEOMETRY ( 80, 2, 39 ),
614         INT13_FDD_GEOMETRY ( 80, 2, 40 ),
615         INT13_FDD_GEOMETRY ( 80, 2, 44 ),
616         INT13_FDD_GEOMETRY ( 80, 2, 48 ),
617 };
618
619 /**
620  * Guess INT 13 floppy disk drive geometry
621  *
622  * @v int13             Emulated drive
623  * @ret heads           Guessed number of heads
624  * @ret sectors         Guessed number of sectors per track
625  * @ret rc              Return status code
626  *
627  * Guesses the drive geometry by inspecting the disk size.
628  */
629 static int int13_guess_geometry_fdd ( struct int13_drive *int13,
630                                       unsigned int *heads,
631                                       unsigned int *sectors ) {
632         unsigned int blocks = int13_capacity ( int13 );
633         const struct int13_fdd_geometry *geometry;
634         unsigned int cylinders;
635         unsigned int i;
636
637         /* Look for a match against a known geometry */
638         for ( i = 0 ; i < ( sizeof ( int13_fdd_geometries ) /
639                             sizeof ( int13_fdd_geometries[0] ) ) ; i++ ) {
640                 geometry = &int13_fdd_geometries[i];
641                 cylinders = INT13_FDD_CYLINDERS ( geometry );
642                 *heads = INT13_FDD_HEADS ( geometry );
643                 *sectors = INT13_FDD_SECTORS ( geometry );
644                 if ( ( cylinders * (*heads) * (*sectors) ) == blocks ) {
645                         DBGC ( int13, "INT13 drive %02x guessing C/H/S "
646                                "%d/%d/%d based on size %dK\n", int13->drive,
647                                cylinders, *heads, *sectors, ( blocks / 2 ) );
648                         return 0;
649                 }
650         }
651
652         /* Otherwise, assume a partial disk image in the most common
653          * format (1440K, 80/2/18).
654          */
655         *heads = 2;
656         *sectors = 18;
657         DBGC ( int13, "INT13 drive %02x guessing C/H/S xx/%d/%d based on size "
658                "%dK\n", int13->drive, *heads, *sectors, ( blocks / 2 ) );
659         return 0;
660 }
661
662 /**
663  * Guess INT 13 drive geometry
664  *
665  * @v int13             Emulated drive
666  * @v scratch           Scratch area for single-sector reads
667  * @ret rc              Return status code
668  */
669 static int int13_guess_geometry ( struct int13_drive *int13, void *scratch ) {
670         unsigned int guessed_heads;
671         unsigned int guessed_sectors;
672         unsigned int blocks;
673         unsigned int blocks_per_cyl;
674         int rc;
675
676         /* Don't even try when the blksize is invalid for C/H/S access */
677         if ( int13_blksize ( int13 ) != INT13_BLKSIZE )
678                 return 0;
679
680         /* Guess geometry according to drive type */
681         if ( int13_is_fdd ( int13 ) ) {
682                 if ( ( rc = int13_guess_geometry_fdd ( int13, &guessed_heads,
683                                                        &guessed_sectors )) != 0)
684                         return rc;
685         } else {
686                 if ( ( rc = int13_guess_geometry_hdd ( int13, scratch,
687                                                        &guessed_heads,
688                                                        &guessed_sectors )) != 0)
689                         return rc;
690         }
691
692         /* Apply guesses if no geometry already specified */
693         if ( ! int13->heads )
694                 int13->heads = guessed_heads;
695         if ( ! int13->sectors_per_track )
696                 int13->sectors_per_track = guessed_sectors;
697         if ( ! int13->cylinders ) {
698                 /* Avoid attempting a 64-bit divide on a 32-bit system */
699                 blocks = int13_capacity32 ( int13 );
700                 blocks_per_cyl = ( int13->heads * int13->sectors_per_track );
701                 assert ( blocks_per_cyl != 0 );
702                 int13->cylinders = ( blocks / blocks_per_cyl );
703                 if ( int13->cylinders > 1024 )
704                         int13->cylinders = 1024;
705         }
706
707         return 0;
708 }
709
710 /**
711  * Update BIOS drive count
712  */
713 static void int13_sync_num_drives ( void ) {
714         struct int13_drive *int13;
715         uint8_t *counter;
716         uint8_t max_drive;
717         uint8_t required;
718
719         /* Get current drive counts */
720         get_real ( equipment_word, BDA_SEG, BDA_EQUIPMENT_WORD );
721         get_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
722         num_fdds = ( ( equipment_word & 0x0001 ) ?
723                      ( ( ( equipment_word >> 6 ) & 0x3 ) + 1 ) : 0 );
724
725         /* Ensure count is large enough to cover all of our emulated drives */
726         list_for_each_entry ( int13, &int13s, list ) {
727                 counter = ( int13_is_fdd ( int13 ) ? &num_fdds : &num_drives );
728                 max_drive = int13->drive;
729                 if ( max_drive < int13->natural_drive )
730                         max_drive = int13->natural_drive;
731                 required = ( ( max_drive & 0x7f ) + 1 );
732                 if ( *counter < required ) {
733                         *counter = required;
734                         DBGC ( int13, "INT13 drive %02x added to drive count: "
735                                "%d HDDs, %d FDDs\n",
736                                int13->drive, num_drives, num_fdds );
737                 }
738         }
739
740         /* Update current drive count */
741         equipment_word &= ~( ( 0x3 << 6 ) | 0x0001 );
742         if ( num_fdds ) {
743                 equipment_word |= ( 0x0001 |
744                                     ( ( ( num_fdds - 1 ) & 0x3 ) << 6 ) );
745         }
746         put_real ( equipment_word, BDA_SEG, BDA_EQUIPMENT_WORD );
747         put_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
748 }
749
750 /**
751  * Check number of drives
752  */
753 static void int13_check_num_drives ( void ) {
754         uint16_t check_equipment_word;
755         uint8_t check_num_drives;
756
757         get_real ( check_equipment_word, BDA_SEG, BDA_EQUIPMENT_WORD );
758         get_real ( check_num_drives, BDA_SEG, BDA_NUM_DRIVES );
759         if ( ( check_equipment_word != equipment_word ) ||
760              ( check_num_drives != num_drives ) ) {
761                 int13_sync_num_drives();
762         }
763 }
764
765 /**
766  * INT 13, 00 - Reset disk system
767  *
768  * @v int13             Emulated drive
769  * @ret status          Status code
770  */
771 static int int13_reset ( struct int13_drive *int13,
772                          struct i386_all_regs *ix86 __unused ) {
773         int rc;
774
775         DBGC2 ( int13, "Reset drive\n" );
776
777         /* Reopen underlying block device */
778         if ( ( rc = int13_reopen_block ( int13 ) ) != 0 )
779                 return -INT13_STATUS_RESET_FAILED;
780
781         /* Check that block device is functional */
782         if ( ( rc = int13_read_capacity ( int13 ) ) != 0 )
783                 return -INT13_STATUS_RESET_FAILED;
784
785         return 0;
786 }
787
788 /**
789  * INT 13, 01 - Get status of last operation
790  *
791  * @v int13             Emulated drive
792  * @ret status          Status code
793  */
794 static int int13_get_last_status ( struct int13_drive *int13,
795                                    struct i386_all_regs *ix86 __unused ) {
796         DBGC2 ( int13, "Get status of last operation\n" );
797         return int13->last_status;
798 }
799
800 /**
801  * Read / write sectors
802  *
803  * @v int13             Emulated drive
804  * @v al                Number of sectors to read or write (must be nonzero)
805  * @v ch                Low bits of cylinder number
806  * @v cl (bits 7:6)     High bits of cylinder number
807  * @v cl (bits 5:0)     Sector number
808  * @v dh                Head number
809  * @v es:bx             Data buffer
810  * @v block_rw          Block read/write method
811  * @ret status          Status code
812  * @ret al              Number of sectors read or written
813  */
814 static int int13_rw_sectors ( struct int13_drive *int13,
815                               struct i386_all_regs *ix86,
816                               int ( * block_rw ) ( struct interface *control,
817                                                    struct interface *data,
818                                                    uint64_t lba,
819                                                    unsigned int count,
820                                                    userptr_t buffer,
821                                                    size_t len ) ) {
822         unsigned int cylinder, head, sector;
823         unsigned long lba;
824         unsigned int count;
825         userptr_t buffer;
826         int rc;
827
828         /* Validate blocksize */
829         if ( int13_blksize ( int13 ) != INT13_BLKSIZE ) {
830                 DBGC ( int13, "\nINT 13 drive %02x invalid blocksize (%zd) "
831                        "for non-extended read/write\n",
832                        int13->drive, int13_blksize ( int13 ) );
833                 return -INT13_STATUS_INVALID;
834         }
835
836         /* Calculate parameters */
837         cylinder = ( ( ( ix86->regs.cl & 0xc0 ) << 2 ) | ix86->regs.ch );
838         head = ix86->regs.dh;
839         sector = ( ix86->regs.cl & 0x3f );
840         if ( ( cylinder >= int13->cylinders ) ||
841              ( head >= int13->heads ) ||
842              ( sector < 1 ) || ( sector > int13->sectors_per_track ) ) {
843                 DBGC ( int13, "C/H/S %d/%d/%d out of range for geometry "
844                        "%d/%d/%d\n", cylinder, head, sector, int13->cylinders,
845                        int13->heads, int13->sectors_per_track );
846                 return -INT13_STATUS_INVALID;
847         }
848         lba = ( ( ( ( cylinder * int13->heads ) + head )
849                   * int13->sectors_per_track ) + sector - 1 );
850         count = ix86->regs.al;
851         buffer = real_to_user ( ix86->segs.es, ix86->regs.bx );
852
853         DBGC2 ( int13, "C/H/S %d/%d/%d = LBA %08lx <-> %04x:%04x (count %d)\n",
854                 cylinder, head, sector, lba, ix86->segs.es, ix86->regs.bx,
855                 count );
856
857         /* Read from / write to block device */
858         if ( ( rc = int13_rw ( int13, lba, count, buffer, block_rw ) ) != 0 ) {
859                 DBGC ( int13, "INT13 drive %02x I/O failed: %s\n",
860                        int13->drive, strerror ( rc ) );
861                 return -INT13_STATUS_READ_ERROR;
862         }
863
864         return 0;
865 }
866
867 /**
868  * INT 13, 02 - Read sectors
869  *
870  * @v int13             Emulated drive
871  * @v al                Number of sectors to read (must be nonzero)
872  * @v ch                Low bits of cylinder number
873  * @v cl (bits 7:6)     High bits of cylinder number
874  * @v cl (bits 5:0)     Sector number
875  * @v dh                Head number
876  * @v es:bx             Data buffer
877  * @ret status          Status code
878  * @ret al              Number of sectors read
879  */
880 static int int13_read_sectors ( struct int13_drive *int13,
881                                 struct i386_all_regs *ix86 ) {
882         DBGC2 ( int13, "Read: " );
883         return int13_rw_sectors ( int13, ix86, block_read );
884 }
885
886 /**
887  * INT 13, 03 - Write sectors
888  *
889  * @v int13             Emulated drive
890  * @v al                Number of sectors to write (must be nonzero)
891  * @v ch                Low bits of cylinder number
892  * @v cl (bits 7:6)     High bits of cylinder number
893  * @v cl (bits 5:0)     Sector number
894  * @v dh                Head number
895  * @v es:bx             Data buffer
896  * @ret status          Status code
897  * @ret al              Number of sectors written
898  */
899 static int int13_write_sectors ( struct int13_drive *int13,
900                                  struct i386_all_regs *ix86 ) {
901         DBGC2 ( int13, "Write: " );
902         return int13_rw_sectors ( int13, ix86, block_write );
903 }
904
905 /**
906  * INT 13, 08 - Get drive parameters
907  *
908  * @v int13             Emulated drive
909  * @ret status          Status code
910  * @ret ch              Low bits of maximum cylinder number
911  * @ret cl (bits 7:6)   High bits of maximum cylinder number
912  * @ret cl (bits 5:0)   Maximum sector number
913  * @ret dh              Maximum head number
914  * @ret dl              Number of drives
915  */
916 static int int13_get_parameters ( struct int13_drive *int13,
917                                   struct i386_all_regs *ix86 ) {
918         unsigned int max_cylinder = int13->cylinders - 1;
919         unsigned int max_head = int13->heads - 1;
920         unsigned int max_sector = int13->sectors_per_track; /* sic */
921
922         DBGC2 ( int13, "Get drive parameters\n" );
923
924         /* Validate blocksize */
925         if ( int13_blksize ( int13 ) != INT13_BLKSIZE ) {
926                 DBGC ( int13, "\nINT 13 drive %02x invalid blocksize (%zd) "
927                        "for non-extended parameters\n",
928                        int13->drive, int13_blksize ( int13 ) );
929                 return -INT13_STATUS_INVALID;
930         }
931
932         /* Common parameters */
933         ix86->regs.ch = ( max_cylinder & 0xff );
934         ix86->regs.cl = ( ( ( max_cylinder >> 8 ) << 6 ) | max_sector );
935         ix86->regs.dh = max_head;
936         ix86->regs.dl = ( int13_is_fdd ( int13 ) ? num_fdds : num_drives );
937
938         /* Floppy-specific parameters */
939         if ( int13_is_fdd ( int13 ) ) {
940                 ix86->regs.bl = INT13_FDD_TYPE_1M44;
941                 ix86->segs.es = rm_ds;
942                 ix86->regs.di = __from_data16 ( &int13_fdd_params );
943         }
944
945         return 0;
946 }
947
948 /**
949  * INT 13, 15 - Get disk type
950  *
951  * @v int13             Emulated drive
952  * @ret ah              Type code
953  * @ret cx:dx           Sector count
954  * @ret status          Status code / disk type
955  */
956 static int int13_get_disk_type ( struct int13_drive *int13,
957                                  struct i386_all_regs *ix86 ) {
958         uint32_t blocks;
959
960         DBGC2 ( int13, "Get disk type\n" );
961
962         if ( int13_is_fdd ( int13 ) ) {
963                 return INT13_DISK_TYPE_FDD;
964         } else {
965                 blocks = int13_capacity32 ( int13 );
966                 ix86->regs.cx = ( blocks >> 16 );
967                 ix86->regs.dx = ( blocks & 0xffff );
968                 return INT13_DISK_TYPE_HDD;
969         }
970 }
971
972 /**
973  * INT 13, 41 - Extensions installation check
974  *
975  * @v int13             Emulated drive
976  * @v bx                0x55aa
977  * @ret bx              0xaa55
978  * @ret cx              Extensions API support bitmap
979  * @ret status          Status code / API version
980  */
981 static int int13_extension_check ( struct int13_drive *int13 __unused,
982                                    struct i386_all_regs *ix86 ) {
983         if ( ix86->regs.bx == 0x55aa ) {
984                 DBGC2 ( int13, "INT13 extensions installation check\n" );
985                 ix86->regs.bx = 0xaa55;
986                 ix86->regs.cx = ( INT13_EXTENSION_LINEAR |
987                                   INT13_EXTENSION_EDD |
988                                   INT13_EXTENSION_64BIT );
989                 return INT13_EXTENSION_VER_3_0;
990         } else {
991                 return -INT13_STATUS_INVALID;
992         }
993 }
994
995 /**
996  * Extended read / write
997  *
998  * @v int13             Emulated drive
999  * @v ds:si             Disk address packet
1000  * @v block_rw          Block read/write method
1001  * @ret status          Status code
1002  */
1003 static int int13_extended_rw ( struct int13_drive *int13,
1004                                struct i386_all_regs *ix86,
1005                                int ( * block_rw ) ( struct interface *control,
1006                                                     struct interface *data,
1007                                                     uint64_t lba,
1008                                                     unsigned int count,
1009                                                     userptr_t buffer,
1010                                                     size_t len ) ) {
1011         struct int13_disk_address addr;
1012         uint8_t bufsize;
1013         uint64_t lba;
1014         unsigned long count;
1015         userptr_t buffer;
1016         int rc;
1017
1018         /* Extended reads are not allowed on floppy drives.
1019          * ELTORITO.SYS seems to assume that we are really a CD-ROM if
1020          * we support extended reads for a floppy drive.
1021          */
1022         if ( int13_is_fdd ( int13 ) )
1023                 return -INT13_STATUS_INVALID;
1024
1025         /* Get buffer size */
1026         get_real ( bufsize, ix86->segs.ds,
1027                    ( ix86->regs.si + offsetof ( typeof ( addr ), bufsize ) ) );
1028         if ( bufsize < offsetof ( typeof ( addr ), buffer_phys ) ) {
1029                 DBGC2 ( int13, "<invalid buffer size %#02x\n>\n", bufsize );
1030                 return -INT13_STATUS_INVALID;
1031         }
1032
1033         /* Read parameters from disk address structure */
1034         memset ( &addr, 0, sizeof ( addr ) );
1035         copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si, bufsize );
1036         lba = addr.lba;
1037         DBGC2 ( int13, "LBA %08llx <-> ", ( ( unsigned long long ) lba ) );
1038         if ( ( addr.count == 0xff ) ||
1039              ( ( addr.buffer.segment == 0xffff ) &&
1040                ( addr.buffer.offset == 0xffff ) ) ) {
1041                 buffer = phys_to_user ( addr.buffer_phys );
1042                 DBGC2 ( int13, "%08llx",
1043                         ( ( unsigned long long ) addr.buffer_phys ) );
1044         } else {
1045                 buffer = real_to_user ( addr.buffer.segment,
1046                                         addr.buffer.offset );
1047                 DBGC2 ( int13, "%04x:%04x", addr.buffer.segment,
1048                         addr.buffer.offset );
1049         }
1050         if ( addr.count <= 0x7f ) {
1051                 count = addr.count;
1052         } else if ( addr.count == 0xff ) {
1053                 count = addr.long_count;
1054         } else {
1055                 DBGC2 ( int13, " <invalid count %#02x>\n", addr.count );
1056                 return -INT13_STATUS_INVALID;
1057         }
1058         DBGC2 ( int13, " (count %ld)\n", count );
1059
1060         /* Read from / write to block device */
1061         if ( ( rc = int13_rw ( int13, lba, count, buffer, block_rw ) ) != 0 ) {
1062                 DBGC ( int13, "INT13 drive %02x extended I/O failed: %s\n",
1063                        int13->drive, strerror ( rc ) );
1064                 /* Record that no blocks were transferred successfully */
1065                 addr.count = 0;
1066                 put_real ( addr.count, ix86->segs.ds,
1067                            ( ix86->regs.si +
1068                              offsetof ( typeof ( addr ), count ) ) );
1069                 return -INT13_STATUS_READ_ERROR;
1070         }
1071
1072         return 0;
1073 }
1074
1075 /**
1076  * INT 13, 42 - Extended read
1077  *
1078  * @v int13             Emulated drive
1079  * @v ds:si             Disk address packet
1080  * @ret status          Status code
1081  */
1082 static int int13_extended_read ( struct int13_drive *int13,
1083                                  struct i386_all_regs *ix86 ) {
1084         DBGC2 ( int13, "Extended read: " );
1085         return int13_extended_rw ( int13, ix86, block_read );
1086 }
1087
1088 /**
1089  * INT 13, 43 - Extended write
1090  *
1091  * @v int13             Emulated drive
1092  * @v ds:si             Disk address packet
1093  * @ret status          Status code
1094  */
1095 static int int13_extended_write ( struct int13_drive *int13,
1096                                   struct i386_all_regs *ix86 ) {
1097         DBGC2 ( int13, "Extended write: " );
1098         return int13_extended_rw ( int13, ix86, block_write );
1099 }
1100
1101 /**
1102  * INT 13, 44 - Verify sectors
1103  *
1104  * @v int13             Emulated drive
1105  * @v ds:si             Disk address packet
1106  * @ret status          Status code
1107  */
1108 static int int13_extended_verify ( struct int13_drive *int13,
1109                                    struct i386_all_regs *ix86 ) {
1110         struct int13_disk_address addr;
1111         uint64_t lba;
1112         unsigned long count;
1113
1114         /* Read parameters from disk address structure */
1115         if ( DBG_EXTRA ) {
1116                 copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si,
1117                                  sizeof ( addr ));
1118                 lba = addr.lba;
1119                 count = addr.count;
1120                 DBGC2 ( int13, "Verify: LBA %08llx (count %ld)\n",
1121                         ( ( unsigned long long ) lba ), count );
1122         }
1123
1124         /* We have no mechanism for verifying sectors */
1125         return -INT13_STATUS_INVALID;
1126 }
1127
1128 /**
1129  * INT 13, 44 - Extended seek
1130  *
1131  * @v int13             Emulated drive
1132  * @v ds:si             Disk address packet
1133  * @ret status          Status code
1134  */
1135 static int int13_extended_seek ( struct int13_drive *int13,
1136                                  struct i386_all_regs *ix86 ) {
1137         struct int13_disk_address addr;
1138         uint64_t lba;
1139         unsigned long count;
1140
1141         /* Read parameters from disk address structure */
1142         if ( DBG_EXTRA ) {
1143                 copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si,
1144                                  sizeof ( addr ));
1145                 lba = addr.lba;
1146                 count = addr.count;
1147                 DBGC2 ( int13, "Seek: LBA %08llx (count %ld)\n",
1148                         ( ( unsigned long long ) lba ), count );
1149         }
1150
1151         /* Ignore and return success */
1152         return 0;
1153 }
1154
1155 /**
1156  * Build device path information
1157  *
1158  * @v int13             Emulated drive
1159  * @v dpi               Device path information
1160  * @ret rc              Return status code
1161  */
1162 static int int13_device_path_info ( struct int13_drive *int13,
1163                                     struct edd_device_path_information *dpi ) {
1164         struct device *device;
1165         struct device_description *desc;
1166         unsigned int i;
1167         uint8_t sum = 0;
1168         int rc;
1169
1170         /* Reopen block device if necessary */
1171         if ( ( int13->block_rc != 0 ) &&
1172              ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) )
1173                 return rc;
1174
1175         /* Get underlying hardware device */
1176         device = identify_device ( &int13->block );
1177         if ( ! device ) {
1178                 DBGC ( int13, "INT13 drive %02x cannot identify hardware "
1179                        "device\n", int13->drive );
1180                 return -ENODEV;
1181         }
1182
1183         /* Fill in bus type and interface path */
1184         desc = &device->desc;
1185         switch ( desc->bus_type ) {
1186         case BUS_TYPE_PCI:
1187                 dpi->host_bus_type.type = EDD_BUS_TYPE_PCI;
1188                 dpi->interface_path.pci.bus = PCI_BUS ( desc->location );
1189                 dpi->interface_path.pci.slot = PCI_SLOT ( desc->location );
1190                 dpi->interface_path.pci.function = PCI_FUNC ( desc->location );
1191                 dpi->interface_path.pci.channel = 0xff; /* unused */
1192                 break;
1193         default:
1194                 DBGC ( int13, "INT13 drive %02x unrecognised bus type %d\n",
1195                        int13->drive, desc->bus_type );
1196                 return -ENOTSUP;
1197         }
1198
1199         /* Get EDD block device description */
1200         if ( ( rc = edd_describe ( &int13->block, &dpi->interface_type,
1201                                    &dpi->device_path ) ) != 0 ) {
1202                 DBGC ( int13, "INT13 drive %02x cannot identify block device: "
1203                        "%s\n", int13->drive, strerror ( rc ) );
1204                 return rc;
1205         }
1206
1207         /* Fill in common fields and fix checksum */
1208         dpi->key = EDD_DEVICE_PATH_INFO_KEY;
1209         dpi->len = sizeof ( *dpi );
1210         for ( i = 0 ; i < sizeof ( *dpi ) ; i++ )
1211                 sum += *( ( ( uint8_t * ) dpi ) + i );
1212         dpi->checksum -= sum;
1213
1214         return 0;
1215 }
1216
1217 /**
1218  * INT 13, 48 - Get extended parameters
1219  *
1220  * @v int13             Emulated drive
1221  * @v ds:si             Drive parameter table
1222  * @ret status          Status code
1223  */
1224 static int int13_get_extended_parameters ( struct int13_drive *int13,
1225                                            struct i386_all_regs *ix86 ) {
1226         struct int13_disk_parameters params;
1227         struct segoff address;
1228         size_t len = sizeof ( params );
1229         uint16_t bufsize;
1230         int rc;
1231
1232         /* Get buffer size */
1233         get_real ( bufsize, ix86->segs.ds,
1234                    ( ix86->regs.si + offsetof ( typeof ( params ), bufsize )));
1235
1236         DBGC2 ( int13, "Get extended drive parameters to %04x:%04x+%02x\n",
1237                 ix86->segs.ds, ix86->regs.si, bufsize );
1238
1239         /* Build drive parameters */
1240         memset ( &params, 0, sizeof ( params ) );
1241         params.flags = INT13_FL_DMA_TRANSPARENT;
1242         if ( ( int13->cylinders < 1024 ) &&
1243              ( int13_capacity ( int13 ) <= INT13_MAX_CHS_SECTORS ) ) {
1244                 params.flags |= INT13_FL_CHS_VALID;
1245         }
1246         params.cylinders = int13->cylinders;
1247         params.heads = int13->heads;
1248         params.sectors_per_track = int13->sectors_per_track;
1249         params.sectors = int13_capacity ( int13 );
1250         params.sector_size = int13_blksize ( int13 );
1251         memset ( &params.dpte, 0xff, sizeof ( params.dpte ) );
1252         if ( ( rc = int13_device_path_info ( int13, &params.dpi ) ) != 0 ) {
1253                 DBGC ( int13, "INT13 drive %02x could not provide device "
1254                        "path information: %s\n",
1255                        int13->drive, strerror ( rc ) );
1256                 len = offsetof ( typeof ( params ), dpi );
1257         }
1258
1259         /* Calculate returned "buffer size" (which will be less than
1260          * the length actually copied if device path information is
1261          * present).
1262          */
1263         if ( bufsize < offsetof ( typeof ( params ), dpte ) )
1264                 return -INT13_STATUS_INVALID;
1265         if ( bufsize < offsetof ( typeof ( params ), dpi ) ) {
1266                 params.bufsize = offsetof ( typeof ( params ), dpte );
1267         } else {
1268                 params.bufsize = offsetof ( typeof ( params ), dpi );
1269         }
1270
1271         DBGC ( int13, "INT 13 drive %02x described using extended "
1272                "parameters:\n", int13->drive );
1273         address.segment = ix86->segs.ds;
1274         address.offset = ix86->regs.si;
1275         DBGC_HDA ( int13, address, &params, len );
1276
1277         /* Return drive parameters */
1278         if ( len > bufsize )
1279                 len = bufsize;
1280         copy_to_real ( ix86->segs.ds, ix86->regs.si, &params, len );
1281
1282         return 0;
1283 }
1284
1285 /**
1286  * INT 13, 4b - Get status or terminate CD-ROM emulation
1287  *
1288  * @v int13             Emulated drive
1289  * @v ds:si             Specification packet
1290  * @ret status          Status code
1291  */
1292 static int int13_cdrom_status_terminate ( struct int13_drive *int13,
1293                                           struct i386_all_regs *ix86 ) {
1294         struct int13_cdrom_specification specification;
1295
1296         DBGC2 ( int13, "Get CD-ROM emulation status to %04x:%04x%s\n",
1297                 ix86->segs.ds, ix86->regs.si,
1298                 ( ix86->regs.al ? "" : " and terminate" ) );
1299
1300         /* Fail if we are not a CD-ROM */
1301         if ( ! int13->is_cdrom ) {
1302                 DBGC ( int13, "INT13 drive %02x is not a CD-ROM\n",
1303                        int13->drive );
1304                 return -INT13_STATUS_INVALID;
1305         }
1306
1307         /* Build specification packet */
1308         memset ( &specification, 0, sizeof ( specification ) );
1309         specification.size = sizeof ( specification );
1310         specification.drive = int13->drive;
1311
1312         /* Return specification packet */
1313         copy_to_real ( ix86->segs.ds, ix86->regs.si, &specification,
1314                        sizeof ( specification ) );
1315
1316         return 0;
1317 }
1318
1319
1320 /**
1321  * INT 13, 4d - Read CD-ROM boot catalog
1322  *
1323  * @v int13             Emulated drive
1324  * @v ds:si             Command packet
1325  * @ret status          Status code
1326  */
1327 static int int13_cdrom_read_boot_catalog ( struct int13_drive *int13,
1328                                            struct i386_all_regs *ix86 ) {
1329         struct int13_cdrom_boot_catalog_command command;
1330         int rc;
1331
1332         /* Read parameters from command packet */
1333         copy_from_real ( &command, ix86->segs.ds, ix86->regs.si,
1334                          sizeof ( command ) );
1335         DBGC2 ( int13, "Read CD-ROM boot catalog to %08x\n", command.buffer );
1336
1337         /* Fail if we have no boot catalog */
1338         if ( ! int13->boot_catalog ) {
1339                 DBGC ( int13, "INT13 drive %02x has no boot catalog\n",
1340                        int13->drive );
1341                 return -INT13_STATUS_INVALID;
1342         }
1343
1344         /* Read from boot catalog */
1345         if ( ( rc = int13_rw ( int13, ( int13->boot_catalog + command.start ),
1346                                command.count, phys_to_user ( command.buffer ),
1347                                block_read ) ) != 0 ) {
1348                 DBGC ( int13, "INT13 drive %02x could not read boot catalog: "
1349                        "%s\n", int13->drive, strerror ( rc ) );
1350                 return -INT13_STATUS_READ_ERROR;
1351         }
1352
1353         return 0;
1354 }
1355
1356 /**
1357  * INT 13 handler
1358  *
1359  */
1360 static __asmcall void int13 ( struct i386_all_regs *ix86 ) {
1361         int command = ix86->regs.ah;
1362         unsigned int bios_drive = ix86->regs.dl;
1363         struct int13_drive *int13;
1364         int status;
1365
1366         /* Check BIOS hasn't killed off our drive */
1367         int13_check_num_drives();
1368
1369         list_for_each_entry ( int13, &int13s, list ) {
1370
1371                 if ( bios_drive != int13->drive ) {
1372                         /* Remap any accesses to this drive's natural number */
1373                         if ( bios_drive == int13->natural_drive ) {
1374                                 DBGC2 ( int13, "INT13,%02x (%02x) remapped to "
1375                                         "(%02x)\n", ix86->regs.ah,
1376                                         bios_drive, int13->drive );
1377                                 ix86->regs.dl = int13->drive;
1378                                 return;
1379                         } else if ( ( ( bios_drive & 0x7f ) == 0x7f ) &&
1380                                     ( command == INT13_CDROM_STATUS_TERMINATE )
1381                                     && int13->is_cdrom ) {
1382                                 /* Catch non-drive-specific CD-ROM calls */
1383                         } else {
1384                                 continue;
1385                         }
1386                 }
1387                 
1388                 DBGC2 ( int13, "INT13,%02x (%02x): ",
1389                         ix86->regs.ah, bios_drive );
1390
1391                 switch ( command ) {
1392                 case INT13_RESET:
1393                         status = int13_reset ( int13, ix86 );
1394                         break;
1395                 case INT13_GET_LAST_STATUS:
1396                         status = int13_get_last_status ( int13, ix86 );
1397                         break;
1398                 case INT13_READ_SECTORS:
1399                         status = int13_read_sectors ( int13, ix86 );
1400                         break;
1401                 case INT13_WRITE_SECTORS:
1402                         status = int13_write_sectors ( int13, ix86 );
1403                         break;
1404                 case INT13_GET_PARAMETERS:
1405                         status = int13_get_parameters ( int13, ix86 );
1406                         break;
1407                 case INT13_GET_DISK_TYPE:
1408                         status = int13_get_disk_type ( int13, ix86 );
1409                         break;
1410                 case INT13_EXTENSION_CHECK:
1411                         status = int13_extension_check ( int13, ix86 );
1412                         break;
1413                 case INT13_EXTENDED_READ:
1414                         status = int13_extended_read ( int13, ix86 );
1415                         break;
1416                 case INT13_EXTENDED_WRITE:
1417                         status = int13_extended_write ( int13, ix86 );
1418                         break;
1419                 case INT13_EXTENDED_VERIFY:
1420                         status = int13_extended_verify ( int13, ix86 );
1421                         break;
1422                 case INT13_EXTENDED_SEEK:
1423                         status = int13_extended_seek ( int13, ix86 );
1424                         break;
1425                 case INT13_GET_EXTENDED_PARAMETERS:
1426                         status = int13_get_extended_parameters ( int13, ix86 );
1427                         break;
1428                 case INT13_CDROM_STATUS_TERMINATE:
1429                         status = int13_cdrom_status_terminate ( int13, ix86 );
1430                         break;
1431                 case INT13_CDROM_READ_BOOT_CATALOG:
1432                         status = int13_cdrom_read_boot_catalog ( int13, ix86 );
1433                         break;
1434                 default:
1435                         DBGC2 ( int13, "*** Unrecognised INT13 ***\n" );
1436                         status = -INT13_STATUS_INVALID;
1437                         break;
1438                 }
1439
1440                 /* Store status for INT 13,01 */
1441                 int13->last_status = status;
1442
1443                 /* Negative status indicates an error */
1444                 if ( status < 0 ) {
1445                         status = -status;
1446                         DBGC ( int13, "INT13,%02x (%02x) failed with status "
1447                                "%02x\n", ix86->regs.ah, int13->drive, status );
1448                 } else {
1449                         ix86->flags &= ~CF;
1450                 }
1451                 ix86->regs.ah = status;
1452
1453                 /* Set OF to indicate to wrapper not to chain this call */
1454                 ix86->flags |= OF;
1455
1456                 return;
1457         }
1458 }
1459
1460 /**
1461  * Hook INT 13 handler
1462  *
1463  */
1464 static void int13_hook_vector ( void ) {
1465         /* Assembly wrapper to call int13().  int13() sets OF if we
1466          * should not chain to the previous handler.  (The wrapper
1467          * clears CF and OF before calling int13()).
1468          */
1469         __asm__  __volatile__ (
1470                TEXT16_CODE ( "\nint13_wrapper:\n\t"
1471                              /* Preserve %ax and %dx for future reference */
1472                              "pushw %%bp\n\t"
1473                              "movw %%sp, %%bp\n\t"                           
1474                              "pushw %%ax\n\t"
1475                              "pushw %%dx\n\t"
1476                              /* Clear OF, set CF, call int13() */
1477                              "orb $0, %%al\n\t" 
1478                              "stc\n\t"
1479                              "pushl %0\n\t"
1480                              "pushw %%cs\n\t"
1481                              "call prot_call\n\t"
1482                              /* Chain if OF not set */
1483                              "jo 1f\n\t"
1484                              "pushfw\n\t"
1485                              "lcall *%%cs:int13_vector\n\t"
1486                              "\n1:\n\t"
1487                              /* Overwrite flags for iret */
1488                              "pushfw\n\t"
1489                              "popw 6(%%bp)\n\t"
1490                              /* Fix up %dl:
1491                               *
1492                               * INT 13,15 : do nothing if hard disk
1493                               * INT 13,08 : load with number of drives
1494                               * all others: restore original value
1495                               */
1496                              "cmpb $0x15, -1(%%bp)\n\t"
1497                              "jne 2f\n\t"
1498                              "testb $0x80, -4(%%bp)\n\t"
1499                              "jnz 3f\n\t"
1500                              "\n2:\n\t"
1501                              "movb -4(%%bp), %%dl\n\t"
1502                              "cmpb $0x08, -1(%%bp)\n\t"
1503                              "jne 3f\n\t"
1504                              "testb $0x80, %%dl\n\t"
1505                              "movb %%cs:num_drives, %%dl\n\t"
1506                              "jnz 3f\n\t"
1507                              "movb %%cs:num_fdds, %%dl\n\t"
1508                              /* Return */
1509                              "\n3:\n\t"
1510                              "movw %%bp, %%sp\n\t"
1511                              "popw %%bp\n\t"
1512                              "iret\n\t" )
1513                : : "i" ( int13 ) );
1514
1515         hook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
1516                               &int13_vector );
1517 }
1518
1519 /**
1520  * Unhook INT 13 handler
1521  */
1522 static void int13_unhook_vector ( void ) {
1523         unhook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
1524                                 &int13_vector );
1525 }
1526
1527 /**
1528  * Check INT13 emulated drive flow control window
1529  *
1530  * @v int13             Emulated drive
1531  */
1532 static size_t int13_block_window ( struct int13_drive *int13 __unused ) {
1533
1534         /* We are never ready to receive data via this interface.
1535          * This prevents objects that support both block and stream
1536          * interfaces from attempting to send us stream data.
1537          */
1538         return 0;
1539 }
1540
1541 /**
1542  * Handle INT 13 emulated drive underlying block device closing
1543  *
1544  * @v int13             Emulated drive
1545  * @v rc                Reason for close
1546  */
1547 static void int13_block_close ( struct int13_drive *int13, int rc ) {
1548
1549         /* Any closing is an error from our point of view */
1550         if ( rc == 0 )
1551                 rc = -ENOTCONN;
1552
1553         DBGC ( int13, "INT13 drive %02x went away: %s\n",
1554                int13->drive, strerror ( rc ) );
1555
1556         /* Record block device error code */
1557         int13->block_rc = rc;
1558
1559         /* Shut down interfaces */
1560         intf_restart ( &int13->block, rc );
1561 }
1562
1563 /** INT 13 drive interface operations */
1564 static struct interface_operation int13_block_op[] = {
1565         INTF_OP ( xfer_window, struct int13_drive *, int13_block_window ),
1566         INTF_OP ( intf_close, struct int13_drive *, int13_block_close ),
1567 };
1568
1569 /** INT 13 drive interface descriptor */
1570 static struct interface_descriptor int13_block_desc =
1571         INTF_DESC ( struct int13_drive, block, int13_block_op );
1572
1573 /**
1574  * Free INT 13 emulated drive
1575  *
1576  * @v refcnt            Reference count
1577  */
1578 static void int13_free ( struct refcnt *refcnt ) {
1579         struct int13_drive *int13 =
1580                 container_of ( refcnt, struct int13_drive, refcnt );
1581
1582         uri_put ( int13->uri );
1583         free ( int13 );
1584 }
1585
1586 /**
1587  * Hook INT 13 emulated drive
1588  *
1589  * @v uri               URI
1590  * @v drive             Drive number
1591  * @ret rc              Return status code
1592  *
1593  * Registers the drive with the INT 13 emulation subsystem, and hooks
1594  * the INT 13 interrupt vector (if not already hooked).
1595  */
1596 static int int13_hook ( struct uri *uri, unsigned int drive ) {
1597         struct int13_drive *int13;
1598         unsigned int natural_drive;
1599         void *scratch;
1600         int rc;
1601
1602         /* Calculate natural drive number */
1603         int13_sync_num_drives();
1604         natural_drive = ( ( drive & 0x80 ) ? ( num_drives | 0x80 ) : num_fdds );
1605
1606         /* Check that drive number is not in use */
1607         list_for_each_entry ( int13, &int13s, list ) {
1608                 if ( int13->drive == drive ) {
1609                         rc = -EADDRINUSE;
1610                         goto err_in_use;
1611                 }
1612         }
1613
1614         /* Allocate and initialise structure */
1615         int13 = zalloc ( sizeof ( *int13 ) );
1616         if ( ! int13 ) {
1617                 rc = -ENOMEM;
1618                 goto err_zalloc;
1619         }
1620         ref_init ( &int13->refcnt, int13_free );
1621         intf_init ( &int13->block, &int13_block_desc, &int13->refcnt );
1622         int13->uri = uri_get ( uri );
1623         int13->drive = drive;
1624         int13->natural_drive = natural_drive;
1625
1626         /* Open block device interface */
1627         if ( ( rc = int13_reopen_block ( int13 ) ) != 0 )
1628                 goto err_reopen_block;
1629
1630         /* Read device capacity */
1631         if ( ( rc = int13_read_capacity ( int13 ) ) != 0 )
1632                 goto err_read_capacity;
1633
1634         /* Allocate scratch area */
1635         scratch = malloc ( int13_blksize ( int13 ) );
1636         if ( ! scratch )
1637                 goto err_alloc_scratch;
1638
1639         /* Parse parameters, if present */
1640         if ( ( rc = int13_parse_iso9660 ( int13, scratch ) ) != 0 )
1641                 goto err_parse_iso9660;
1642
1643         /* Give drive a default geometry */
1644         if ( ( rc = int13_guess_geometry ( int13, scratch ) ) != 0 )
1645                 goto err_guess_geometry;
1646
1647         DBGC ( int13, "INT13 drive %02x (naturally %02x) registered with C/H/S "
1648                "geometry %d/%d/%d\n", int13->drive, int13->natural_drive,
1649                int13->cylinders, int13->heads, int13->sectors_per_track );
1650
1651         /* Hook INT 13 vector if not already hooked */
1652         if ( list_empty ( &int13s ) ) {
1653                 int13_hook_vector();
1654                 devices_get();
1655         }
1656
1657         /* Add to list of emulated drives */
1658         list_add ( &int13->list, &int13s );
1659
1660         /* Update BIOS drive count */
1661         int13_sync_num_drives();
1662
1663         free ( scratch );
1664         return 0;
1665
1666  err_guess_geometry:
1667  err_parse_iso9660:
1668         free ( scratch );
1669  err_alloc_scratch:
1670  err_read_capacity:
1671  err_reopen_block:
1672         intf_shutdown ( &int13->block, rc );
1673         ref_put ( &int13->refcnt );
1674  err_zalloc:
1675  err_in_use:
1676         return rc;
1677 }
1678
1679 /**
1680  * Find INT 13 emulated drive by drive number
1681  *
1682  * @v drive             Drive number
1683  * @ret int13           Emulated drive, or NULL
1684  */
1685 static struct int13_drive * int13_find ( unsigned int drive ) {
1686         struct int13_drive *int13;
1687
1688         list_for_each_entry ( int13, &int13s, list ) {
1689                 if ( int13->drive == drive )
1690                         return int13;
1691         }
1692         return NULL;
1693 }
1694
1695 /**
1696  * Unhook INT 13 emulated drive
1697  *
1698  * @v drive             Drive number
1699  *
1700  * Unregisters the drive from the INT 13 emulation subsystem.  If this
1701  * is the last emulated drive, the INT 13 vector is unhooked (if
1702  * possible).
1703  */
1704 static void int13_unhook ( unsigned int drive ) {
1705         struct int13_drive *int13;
1706
1707         /* Find drive */
1708         int13 = int13_find ( drive );
1709         if ( ! int13 ) {
1710                 DBG ( "INT13 cannot find emulated drive %02x\n", drive );
1711                 return;
1712         }
1713
1714         /* Shut down interfaces */
1715         intf_shutdown ( &int13->block, 0 );
1716
1717         /* Remove from list of emulated drives */
1718         list_del ( &int13->list );
1719
1720         /* Should adjust BIOS drive count, but it's difficult
1721          * to do so reliably.
1722          */
1723
1724         DBGC ( int13, "INT13 drive %02x unregistered\n", int13->drive );
1725
1726         /* Unhook INT 13 vector if no more drives */
1727         if ( list_empty ( &int13s ) ) {
1728                 devices_put();
1729                 int13_unhook_vector();
1730         }
1731
1732         /* Drop list's reference to drive */
1733         ref_put ( &int13->refcnt );
1734 }
1735
1736 /**
1737  * Load and verify master boot record from INT 13 drive
1738  *
1739  * @v drive             Drive number
1740  * @v address           Boot code address to fill in
1741  * @ret rc              Return status code
1742  */
1743 static int int13_load_mbr ( unsigned int drive, struct segoff *address ) {
1744         uint8_t status;
1745         int discard_b, discard_c, discard_d;
1746         uint16_t magic;
1747
1748         /* Use INT 13, 02 to read the MBR */
1749         address->segment = 0;
1750         address->offset = 0x7c00;
1751         __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
1752                                            "pushl %%ebx\n\t"
1753                                            "popw %%bx\n\t"
1754                                            "popw %%es\n\t"
1755                                            "stc\n\t"
1756                                            "sti\n\t"
1757                                            "int $0x13\n\t"
1758                                            "sti\n\t" /* BIOS bugs */
1759                                            "jc 1f\n\t"
1760                                            "xorw %%ax, %%ax\n\t"
1761                                            "\n1:\n\t"
1762                                            "popw %%es\n\t" )
1763                                : "=a" ( status ), "=b" ( discard_b ),
1764                                  "=c" ( discard_c ), "=d" ( discard_d )
1765                                : "a" ( 0x0201 ), "b" ( *address ),
1766                                  "c" ( 1 ), "d" ( drive ) );
1767         if ( status ) {
1768                 DBG ( "INT13 drive %02x could not read MBR (status %02x)\n",
1769                       drive, status );
1770                 return -EIO;
1771         }
1772
1773         /* Check magic signature */
1774         get_real ( magic, address->segment,
1775                    ( address->offset +
1776                      offsetof ( struct master_boot_record, magic ) ) );
1777         if ( magic != INT13_MBR_MAGIC ) {
1778                 DBG ( "INT13 drive %02x does not contain a valid MBR\n",
1779                       drive );
1780                 return -ENOEXEC;
1781         }
1782
1783         return 0;
1784 }
1785
1786 /** El Torito boot catalog command packet */
1787 static struct int13_cdrom_boot_catalog_command __data16 ( eltorito_cmd ) = {
1788         .size = sizeof ( struct int13_cdrom_boot_catalog_command ),
1789         .count = 1,
1790         .buffer = 0x7c00,
1791         .start = 0,
1792 };
1793 #define eltorito_cmd __use_data16 ( eltorito_cmd )
1794
1795 /** El Torito disk address packet */
1796 static struct int13_disk_address __bss16 ( eltorito_address );
1797 #define eltorito_address __use_data16 ( eltorito_address )
1798
1799 /**
1800  * Load and verify El Torito boot record from INT 13 drive
1801  *
1802  * @v drive             Drive number
1803  * @v address           Boot code address to fill in
1804  * @ret rc              Return status code
1805  */
1806 static int int13_load_eltorito ( unsigned int drive, struct segoff *address ) {
1807         struct {
1808                 struct eltorito_validation_entry valid;
1809                 struct eltorito_boot_entry boot;
1810         } __attribute__ (( packed )) catalog;
1811         uint8_t status;
1812
1813         /* Use INT 13, 4d to read the boot catalog */
1814         __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
1815                                            "sti\n\t"
1816                                            "int $0x13\n\t"
1817                                            "sti\n\t" /* BIOS bugs */
1818                                            "jc 1f\n\t"
1819                                            "xorw %%ax, %%ax\n\t"
1820                                            "\n1:\n\t" )
1821                                : "=a" ( status )
1822                                : "a" ( 0x4d00 ), "d" ( drive ),
1823                                  "S" ( __from_data16 ( &eltorito_cmd ) ) );
1824         if ( status ) {
1825                 DBG ( "INT13 drive %02x could not read El Torito boot catalog "
1826                       "(status %02x)\n", drive, status );
1827                 return -EIO;
1828         }
1829         copy_from_user ( &catalog, phys_to_user ( eltorito_cmd.buffer ), 0,
1830                          sizeof ( catalog ) );
1831
1832         /* Sanity checks */
1833         if ( catalog.valid.platform_id != ELTORITO_PLATFORM_X86 ) {
1834                 DBG ( "INT13 drive %02x El Torito specifies unknown platform "
1835                       "%02x\n", drive, catalog.valid.platform_id );
1836                 return -ENOEXEC;
1837         }
1838         if ( catalog.boot.indicator != ELTORITO_BOOTABLE ) {
1839                 DBG ( "INT13 drive %02x El Torito is not bootable\n", drive );
1840                 return -ENOEXEC;
1841         }
1842         if ( catalog.boot.media_type != ELTORITO_NO_EMULATION ) {
1843                 DBG ( "INT13 drive %02x El Torito requires emulation "
1844                        "type %02x\n", drive, catalog.boot.media_type );
1845                 return -ENOTSUP;
1846         }
1847         DBG ( "INT13 drive %02x El Torito boot image at LBA %08x (count %d)\n",
1848               drive, catalog.boot.start, catalog.boot.length );
1849         address->segment = ( catalog.boot.load_segment ?
1850                              catalog.boot.load_segment : 0x7c0 );
1851         address->offset = 0;
1852         DBG ( "INT13 drive %02x El Torito boot image loads at %04x:%04x\n",
1853               drive, address->segment, address->offset );
1854
1855         /* Use INT 13, 42 to read the boot image */
1856         eltorito_address.bufsize =
1857                 offsetof ( typeof ( eltorito_address ), buffer_phys );
1858         eltorito_address.count = catalog.boot.length;
1859         eltorito_address.buffer = *address;
1860         eltorito_address.lba = catalog.boot.start;
1861         __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
1862                                            "sti\n\t"
1863                                            "int $0x13\n\t"
1864                                            "sti\n\t" /* BIOS bugs */
1865                                            "jc 1f\n\t"
1866                                            "xorw %%ax, %%ax\n\t"
1867                                            "\n1:\n\t" )
1868                                : "=a" ( status )
1869                                : "a" ( 0x4200 ), "d" ( drive ),
1870                                  "S" ( __from_data16 ( &eltorito_address ) ) );
1871         if ( status ) {
1872                 DBG ( "INT13 drive %02x could not read El Torito boot image "
1873                       "(status %02x)\n", drive, status );
1874                 return -EIO;
1875         }
1876
1877         return 0;
1878 }
1879
1880 /**
1881  * Attempt to boot from an INT 13 drive
1882  *
1883  * @v drive             Drive number
1884  * @ret rc              Return status code
1885  *
1886  * This boots from the specified INT 13 drive by loading the Master
1887  * Boot Record to 0000:7c00 and jumping to it.  INT 18 is hooked to
1888  * capture an attempt by the MBR to boot the next device.  (This is
1889  * the closest thing to a return path from an MBR).
1890  *
1891  * Note that this function can never return success, by definition.
1892  */
1893 static int int13_boot ( unsigned int drive ) {
1894         struct memory_map memmap;
1895         struct segoff address;
1896         int rc;
1897
1898         /* Look for a usable boot sector */
1899         if ( ( ( rc = int13_load_mbr ( drive, &address ) ) != 0 ) &&
1900              ( ( rc = int13_load_eltorito ( drive, &address ) ) != 0 ) )
1901                 return rc;
1902
1903         /* Dump out memory map prior to boot, if memmap debugging is
1904          * enabled.  Not required for program flow, but we have so
1905          * many problems that turn out to be memory-map related that
1906          * it's worth doing.
1907          */
1908         get_memmap ( &memmap );
1909
1910         /* Jump to boot sector */
1911         if ( ( rc = call_bootsector ( address.segment, address.offset,
1912                                       drive ) ) != 0 ) {
1913                 DBG ( "INT13 drive %02x boot returned: %s\n",
1914                       drive, strerror ( rc ) );
1915                 return rc;
1916         }
1917
1918         return -ECANCELED; /* -EIMPOSSIBLE */
1919 }
1920
1921 /** A boot firmware table generated by iPXE */
1922 union xbft_table {
1923         /** ACPI header */
1924         struct acpi_description_header acpi;
1925         /** Padding */
1926         char pad[768];
1927 };
1928
1929 /** The boot firmware table generated by iPXE */
1930 static union xbft_table __bss16 ( xbftab ) __attribute__ (( aligned ( 16 ) ));
1931 #define xbftab __use_data16 ( xbftab )
1932
1933 /**
1934  * Describe INT 13 emulated drive for SAN-booted operating system
1935  *
1936  * @v drive             Drive number
1937  * @ret rc              Return status code
1938  */
1939 static int int13_describe ( unsigned int drive ) {
1940         struct int13_drive *int13;
1941         struct segoff xbft_address;
1942         int rc;
1943
1944         /* Find drive */
1945         int13 = int13_find ( drive );
1946         if ( ! int13 ) {
1947                 DBG ( "INT13 cannot find emulated drive %02x\n", drive );
1948                 return -ENODEV;
1949         }
1950
1951         /* Reopen block device if necessary */
1952         if ( ( int13->block_rc != 0 ) &&
1953              ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) )
1954                 return rc;
1955
1956         /* Clear table */
1957         memset ( &xbftab, 0, sizeof ( xbftab ) );
1958
1959         /* Fill in common parameters */
1960         strncpy ( xbftab.acpi.oem_id, "FENSYS",
1961                   sizeof ( xbftab.acpi.oem_id ) );
1962         strncpy ( xbftab.acpi.oem_table_id, "iPXE",
1963                   sizeof ( xbftab.acpi.oem_table_id ) );
1964
1965         /* Fill in remaining parameters */
1966         if ( ( rc = acpi_describe ( &int13->block, &xbftab.acpi,
1967                                     sizeof ( xbftab ) ) ) != 0 ) {
1968                 DBGC ( int13, "INT13 drive %02x could not create ACPI "
1969                        "description: %s\n", int13->drive, strerror ( rc ) );
1970                 return rc;
1971         }
1972
1973         /* Fix up ACPI checksum */
1974         acpi_fix_checksum ( &xbftab.acpi );
1975         xbft_address.segment = rm_ds;
1976         xbft_address.offset = __from_data16 ( &xbftab );
1977         DBGC ( int13, "INT13 drive %02x described using boot firmware "
1978                "table:\n", int13->drive );
1979         DBGC_HDA ( int13, xbft_address, &xbftab,
1980                    le32_to_cpu ( xbftab.acpi.length ) );
1981
1982         return 0;
1983 }
1984
1985 PROVIDE_SANBOOT_INLINE ( pcbios, san_default_drive );
1986 PROVIDE_SANBOOT ( pcbios, san_hook, int13_hook );
1987 PROVIDE_SANBOOT ( pcbios, san_unhook, int13_unhook );
1988 PROVIDE_SANBOOT ( pcbios, san_boot, int13_boot );
1989 PROVIDE_SANBOOT ( pcbios, san_describe, int13_describe );