2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
28 #include <ipxe/list.h>
29 #include <ipxe/blockdev.h>
31 #include <ipxe/open.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>
41 #include <ipxe/iso9660.h>
42 #include <ipxe/eltorito.h>
46 #include <bootsector.h>
53 * This module provides a mechanism for exporting block devices via
54 * the BIOS INT 13 disk interrupt interface.
59 * Overall timeout for INT 13 commands (independent of underlying device
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.
67 #define INT13_COMMAND_TIMEOUT ( 15 * TICKS_PER_SEC )
69 /** An INT 13 emulated drive */
71 /** Reference count */
73 /** List of all registered drives */
74 struct list_head list;
76 /** Block device URI */
78 /** Underlying block device interface */
79 struct interface block;
81 /** BIOS in-use drive number (0x00-0xff) */
83 /** BIOS natural drive number (0x00-0xff)
85 * This is the drive number that would have been assigned by
86 * 'naturally' appending the drive to the end of the BIOS
89 * If the emulated drive replaces a preexisting drive, this is
90 * the drive number that the preexisting drive gets remapped
93 unsigned int natural_drive;
95 /** Block device capacity */
96 struct block_device_capacity capacity;
97 /** INT 13 emulated blocksize shift
99 * To allow for emulation of CD-ROM access, this represents
100 * the left-shift required to translate from INT 13 blocks to
103 unsigned int blksize_shift;
105 /** Number of cylinders
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
113 unsigned int cylinders;
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.
122 /** Number of sectors per track
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.
128 unsigned int sectors_per_track;
130 /** Drive is a CD-ROM */
132 /** Address of El Torito boot catalog (if any) */
133 unsigned int boot_catalog;
135 /** Underlying device status, if in error */
137 /** Status of last operation */
141 /** Vector for chaining to other INT 13 handlers */
142 static struct segoff __text16 ( int13_vector );
143 #define int13_vector __use_text16 ( int13_vector )
145 /** Assembly wrapper */
146 extern void int13_wrapper ( void );
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,
155 #define int13_fdd_params __use_data16 ( int13_fdd_params )
157 /** List of registered emulated drives */
158 static LIST_HEAD ( int13s );
163 * This is a cached copy of the BIOS Data Area equipment word at
166 static uint16_t equipment_word;
169 * Number of BIOS floppy disk drives
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.
174 static uint8_t __text16 ( num_fdds );
175 #define num_fdds __use_text16 ( num_fdds )
178 * Number of BIOS hard disk drives
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.
184 static uint8_t __text16 ( num_drives );
185 #define num_drives __use_text16 ( num_drives )
188 * Calculate INT 13 drive sector size
190 * @v int13 Emulated drive
191 * @ret blksize Sector size
193 static inline size_t int13_blksize ( struct int13_drive *int13 ) {
194 return ( int13->capacity.blksize << int13->blksize_shift );
198 * Calculate INT 13 drive capacity
200 * @v int13 Emulated drive
201 * @ret blocks Number of blocks
203 static inline uint64_t int13_capacity ( struct int13_drive *int13 ) {
204 return ( int13->capacity.blocks >> int13->blksize_shift );
208 * Calculate INT 13 drive capacity (limited to 32 bits)
210 * @v int13 Emulated drive
211 * @ret blocks Number of blocks
213 static inline uint32_t int13_capacity32 ( struct int13_drive *int13 ) {
214 uint64_t capacity = int13_capacity ( int13 );
215 return ( ( capacity <= 0xffffffffUL ) ? capacity : 0xffffffff );
219 * Test if INT 13 drive is a floppy disk drive
221 * @v int13 Emulated drive
222 * @ret is_fdd Emulated drive is a floppy disk
224 static inline int int13_is_fdd ( struct int13_drive *int13 ) {
225 return ( ! ( int13->drive & 0x80 ) );
228 /** An INT 13 command */
229 struct int13_command {
233 struct int13_drive *int13;
234 /** Underlying block device interface */
235 struct interface block;
236 /** Command timeout timer */
237 struct retry_timer timer;
241 * Record INT 13 drive capacity
243 * @v command INT 13 command
244 * @v capacity Block device capacity
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 ) );
253 * Close INT 13 command
255 * @v command INT 13 command
256 * @v rc Reason for close
258 static void int13_command_close ( struct int13_command *command, int rc ) {
259 intf_restart ( &command->block, rc );
260 stop_timer ( &command->timer );
265 * Handle INT 13 command timer expiry
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 );
274 int13_command_close ( command, -ETIMEDOUT );
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 ),
284 /** INT 13 command interface descriptor */
285 static struct interface_descriptor int13_command_desc =
286 INTF_DESC ( struct int13_command, block, int13_command_op );
289 * Open (or reopen) INT 13 emulated drive underlying block device
291 * @v int13 Emulated drive
292 * @ret rc Return status code
294 static int int13_reopen_block ( struct int13_drive *int13 ) {
297 /* Close any existing block device */
298 intf_restart ( &int13->block, -ECONNRESET );
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;
308 /* Clear block device error status */
315 * Prepare to issue INT 13 command
317 * @v command INT 13 command
318 * @v int13 Emulated drive
319 * @ret rc Return status code
321 static int int13_command_start ( struct int13_command *command,
322 struct int13_drive *int13 ) {
326 assert ( command->int13 == NULL );
327 assert ( ! timer_running ( &command->timer ) );
329 /* Reopen block device if necessary */
330 if ( ( int13->block_rc != 0 ) &&
331 ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) )
334 /* Initialise command */
335 command->rc = -EINPROGRESS;
336 command->int13 = int13;
337 start_timer_fixed ( &command->timer, INT13_COMMAND_TIMEOUT );
339 /* Wait for block control interface to become ready */
340 while ( ( command->rc == -EINPROGRESS ) &&
341 ( xfer_window ( &int13->block ) == 0 ) ) {
345 return ( ( command->rc == -EINPROGRESS ) ?
346 int13->block_rc : command->rc );
350 * Wait for INT 13 command to complete
352 * @v command INT 13 command
353 * @ret rc Return status code
355 static int int13_command_wait ( struct int13_command *command ) {
358 assert ( timer_running ( &command->timer ) );
360 /* Wait for command to complete */
361 while ( command->rc == -EINPROGRESS )
364 assert ( ! timer_running ( &command->timer ) );
369 * Terminate INT 13 command
371 * @v command INT 13 command
373 static void int13_command_stop ( struct int13_command *command ) {
374 stop_timer ( &command->timer );
375 command->int13 = NULL;
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 ),
385 * Read from or write to INT 13 drive
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
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;
405 /* Translate to underlying blocksize */
406 lba <<= int13->blksize_shift;
407 count <<= int13->blksize_shift;
411 /* Determine fragment length */
413 if ( frag_count > int13->capacity.max_count )
414 frag_count = int13->capacity.max_count;
415 frag_len = ( int13->capacity.blksize * frag_count );
418 if ( ( ( rc = int13_command_start ( command, int13 ) ) != 0 ) ||
419 ( ( rc = block_rw ( &int13->block, &command->block, lba,
421 frag_len ) ) != 0 ) ||
422 ( ( rc = int13_command_wait ( command ) ) != 0 ) ) {
423 int13_command_stop ( command );
426 int13_command_stop ( command );
428 /* Move to next fragment */
431 buffer = userptr_add ( buffer, frag_len );
438 * Read INT 13 drive capacity
440 * @v int13 Emulated drive
441 * @ret rc Return status code
443 static int int13_read_capacity ( struct int13_drive *int13 ) {
444 struct int13_command *command = &int13_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 );
456 int13_command_stop ( command );
461 * Parse ISO9660 parameters
463 * @v int13 Emulated drive
464 * @v scratch Scratch area for single-sector reads
465 * @ret rc Return status code
467 * Reads and parses ISO9660 parameters, if present.
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,
474 struct iso9660_primary_descriptor *primary = scratch;
475 static const struct eltorito_descriptor_fixed boot_check = {
476 .type = ISO9660_TYPE_BOOT,
479 .system_id = "EL TORITO SPECIFICATION",
481 struct eltorito_descriptor *boot = scratch;
482 unsigned int blksize;
483 unsigned int blksize_shift;
486 /* Calculate required blocksize shift */
487 blksize = int13_blksize ( int13 );
489 while ( blksize < ISO9660_BLKSIZE ) {
493 if ( blksize > ISO9660_BLKSIZE ) {
494 /* Do nothing if the blksize is invalid for CD-ROM access */
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 ) );
508 /* Do nothing unless this is an ISO image */
509 if ( memcmp ( primary, &primary_check, sizeof ( primary_check ) ) != 0 )
511 DBGC ( int13, "INT13 drive %02x contains an ISO9660 filesystem; "
512 "treating as CD-ROM\n", int13->drive );
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 ) );
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 );
531 DBGC ( int13, "INT13 drive %02x has no El Torito boot "
532 "catalog\n", int13->drive );
535 /* Configure drive for no-emulation CD-ROM access */
536 int13->blksize_shift += blksize_shift;
542 * Guess INT 13 hard disk drive geometry
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
550 * Guesses the drive geometry by inspecting the partition table.
552 static int int13_guess_geometry_hdd ( struct int13_drive *int13, void *scratch,
554 unsigned int *sectors ) {
555 struct master_boot_record *mbr = scratch;
556 struct partition_table_entry *partition;
560 /* Default guess is xx/255/63 */
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 ) );
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 );
577 /* Scan through partition table and modify guesses for
578 * heads and sectors_per_track if we find any used
581 for ( i = 0 ; i < 4 ; i++ ) {
582 partition = &mbr->partitions[i];
583 if ( ! partition->type )
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 "
589 int13->drive, *heads, *sectors, ( i + 1 ) );
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 ),
620 * Guess INT 13 floppy disk drive geometry
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
627 * Guesses the drive geometry by inspecting the disk size.
629 static int int13_guess_geometry_fdd ( struct int13_drive *int13,
631 unsigned int *sectors ) {
632 unsigned int blocks = int13_capacity ( int13 );
633 const struct int13_fdd_geometry *geometry;
634 unsigned int cylinders;
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 ) );
652 /* Otherwise, assume a partial disk image in the most common
653 * format (1440K, 80/2/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 ) );
663 * Guess INT 13 drive geometry
665 * @v int13 Emulated drive
666 * @v scratch Scratch area for single-sector reads
667 * @ret rc Return status code
669 static int int13_guess_geometry ( struct int13_drive *int13, void *scratch ) {
670 unsigned int guessed_heads;
671 unsigned int guessed_sectors;
673 unsigned int blocks_per_cyl;
676 /* Don't even try when the blksize is invalid for C/H/S access */
677 if ( int13_blksize ( int13 ) != INT13_BLKSIZE )
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)
686 if ( ( rc = int13_guess_geometry_hdd ( int13, scratch,
688 &guessed_sectors )) != 0)
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;
711 * Update BIOS drive count
713 static void int13_sync_num_drives ( void ) {
714 struct int13_drive *int13;
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 );
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 ) {
734 DBGC ( int13, "INT13 drive %02x added to drive count: "
735 "%d HDDs, %d FDDs\n",
736 int13->drive, num_drives, num_fdds );
740 /* Update current drive count */
741 equipment_word &= ~( ( 0x3 << 6 ) | 0x0001 );
743 equipment_word |= ( 0x0001 |
744 ( ( ( num_fdds - 1 ) & 0x3 ) << 6 ) );
746 put_real ( equipment_word, BDA_SEG, BDA_EQUIPMENT_WORD );
747 put_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
751 * Check number of drives
753 static void int13_check_num_drives ( void ) {
754 uint16_t check_equipment_word;
755 uint8_t check_num_drives;
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();
766 * INT 13, 00 - Reset disk system
768 * @v int13 Emulated drive
769 * @ret status Status code
771 static int int13_reset ( struct int13_drive *int13,
772 struct i386_all_regs *ix86 __unused ) {
775 DBGC2 ( int13, "Reset drive\n" );
777 /* Reopen underlying block device */
778 if ( ( rc = int13_reopen_block ( int13 ) ) != 0 )
779 return -INT13_STATUS_RESET_FAILED;
781 /* Check that block device is functional */
782 if ( ( rc = int13_read_capacity ( int13 ) ) != 0 )
783 return -INT13_STATUS_RESET_FAILED;
789 * INT 13, 01 - Get status of last operation
791 * @v int13 Emulated drive
792 * @ret status Status code
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;
801 * Read / write sectors
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
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
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,
822 unsigned int cylinder, head, sector;
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;
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;
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 );
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,
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;
868 * INT 13, 02 - Read sectors
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
876 * @v es:bx Data buffer
877 * @ret status Status code
878 * @ret al Number of sectors read
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 );
887 * INT 13, 03 - Write sectors
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
895 * @v es:bx Data buffer
896 * @ret status Status code
897 * @ret al Number of sectors written
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 );
906 * INT 13, 08 - Get drive parameters
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
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 */
922 DBGC2 ( int13, "Get drive parameters\n" );
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;
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 );
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 );
949 * INT 13, 15 - Get disk type
951 * @v int13 Emulated drive
953 * @ret cx:dx Sector count
954 * @ret status Status code / disk type
956 static int int13_get_disk_type ( struct int13_drive *int13,
957 struct i386_all_regs *ix86 ) {
960 DBGC2 ( int13, "Get disk type\n" );
962 if ( int13_is_fdd ( int13 ) ) {
963 return INT13_DISK_TYPE_FDD;
965 blocks = int13_capacity32 ( int13 );
966 ix86->regs.cx = ( blocks >> 16 );
967 ix86->regs.dx = ( blocks & 0xffff );
968 return INT13_DISK_TYPE_HDD;
973 * INT 13, 41 - Extensions installation check
975 * @v int13 Emulated drive
978 * @ret cx Extensions API support bitmap
979 * @ret status Status code / API version
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;
991 return -INT13_STATUS_INVALID;
996 * Extended read / write
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
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,
1011 struct int13_disk_address addr;
1014 unsigned long count;
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.
1022 if ( int13_is_fdd ( int13 ) )
1023 return -INT13_STATUS_INVALID;
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;
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 );
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 ) );
1045 buffer = real_to_user ( addr.buffer.segment,
1046 addr.buffer.offset );
1047 DBGC2 ( int13, "%04x:%04x", addr.buffer.segment,
1048 addr.buffer.offset );
1050 if ( addr.count <= 0x7f ) {
1052 } else if ( addr.count == 0xff ) {
1053 count = addr.long_count;
1055 DBGC2 ( int13, " <invalid count %#02x>\n", addr.count );
1056 return -INT13_STATUS_INVALID;
1058 DBGC2 ( int13, " (count %ld)\n", count );
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 */
1066 put_real ( addr.count, ix86->segs.ds,
1068 offsetof ( typeof ( addr ), count ) ) );
1069 return -INT13_STATUS_READ_ERROR;
1076 * INT 13, 42 - Extended read
1078 * @v int13 Emulated drive
1079 * @v ds:si Disk address packet
1080 * @ret status Status code
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 );
1089 * INT 13, 43 - Extended write
1091 * @v int13 Emulated drive
1092 * @v ds:si Disk address packet
1093 * @ret status Status code
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 );
1102 * INT 13, 44 - Verify sectors
1104 * @v int13 Emulated drive
1105 * @v ds:si Disk address packet
1106 * @ret status Status code
1108 static int int13_extended_verify ( struct int13_drive *int13,
1109 struct i386_all_regs *ix86 ) {
1110 struct int13_disk_address addr;
1112 unsigned long count;
1114 /* Read parameters from disk address structure */
1116 copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si,
1120 DBGC2 ( int13, "Verify: LBA %08llx (count %ld)\n",
1121 ( ( unsigned long long ) lba ), count );
1124 /* We have no mechanism for verifying sectors */
1125 return -INT13_STATUS_INVALID;
1129 * INT 13, 44 - Extended seek
1131 * @v int13 Emulated drive
1132 * @v ds:si Disk address packet
1133 * @ret status Status code
1135 static int int13_extended_seek ( struct int13_drive *int13,
1136 struct i386_all_regs *ix86 ) {
1137 struct int13_disk_address addr;
1139 unsigned long count;
1141 /* Read parameters from disk address structure */
1143 copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si,
1147 DBGC2 ( int13, "Seek: LBA %08llx (count %ld)\n",
1148 ( ( unsigned long long ) lba ), count );
1151 /* Ignore and return success */
1156 * Build device path information
1158 * @v int13 Emulated drive
1159 * @v dpi Device path information
1160 * @ret rc Return status code
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;
1170 /* Reopen block device if necessary */
1171 if ( ( int13->block_rc != 0 ) &&
1172 ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) )
1175 /* Get underlying hardware device */
1176 device = identify_device ( &int13->block );
1178 DBGC ( int13, "INT13 drive %02x cannot identify hardware "
1179 "device\n", int13->drive );
1183 /* Fill in bus type and interface path */
1184 desc = &device->desc;
1185 switch ( desc->bus_type ) {
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 */
1194 DBGC ( int13, "INT13 drive %02x unrecognised bus type %d\n",
1195 int13->drive, desc->bus_type );
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 ) );
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;
1218 * INT 13, 48 - Get extended parameters
1220 * @v int13 Emulated drive
1221 * @v ds:si Drive parameter table
1222 * @ret status Status code
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 );
1232 /* Get buffer size */
1233 get_real ( bufsize, ix86->segs.ds,
1234 ( ix86->regs.si + offsetof ( typeof ( params ), bufsize )));
1236 DBGC2 ( int13, "Get extended drive parameters to %04x:%04x+%02x\n",
1237 ix86->segs.ds, ix86->regs.si, bufsize );
1239 /* Build drive parameters */
1240 memset ( ¶ms, 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;
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 ( ¶ms.dpte, 0xff, sizeof ( params.dpte ) );
1252 if ( ( rc = int13_device_path_info ( int13, ¶ms.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 );
1259 /* Calculate returned "buffer size" (which will be less than
1260 * the length actually copied if device path information is
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 );
1268 params.bufsize = offsetof ( typeof ( params ), dpi );
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, ¶ms, len );
1277 /* Return drive parameters */
1278 if ( len > bufsize )
1280 copy_to_real ( ix86->segs.ds, ix86->regs.si, ¶ms, len );
1286 * INT 13, 4b - Get status or terminate CD-ROM emulation
1288 * @v int13 Emulated drive
1289 * @v ds:si Specification packet
1290 * @ret status Status code
1292 static int int13_cdrom_status_terminate ( struct int13_drive *int13,
1293 struct i386_all_regs *ix86 ) {
1294 struct int13_cdrom_specification specification;
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" ) );
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",
1304 return -INT13_STATUS_INVALID;
1307 /* Build specification packet */
1308 memset ( &specification, 0, sizeof ( specification ) );
1309 specification.size = sizeof ( specification );
1310 specification.drive = int13->drive;
1312 /* Return specification packet */
1313 copy_to_real ( ix86->segs.ds, ix86->regs.si, &specification,
1314 sizeof ( specification ) );
1321 * INT 13, 4d - Read CD-ROM boot catalog
1323 * @v int13 Emulated drive
1324 * @v ds:si Command packet
1325 * @ret status Status code
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;
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 );
1337 /* Fail if we have no boot catalog */
1338 if ( ! int13->boot_catalog ) {
1339 DBGC ( int13, "INT13 drive %02x has no boot catalog\n",
1341 return -INT13_STATUS_INVALID;
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;
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;
1366 /* Check BIOS hasn't killed off our drive */
1367 int13_check_num_drives();
1369 list_for_each_entry ( int13, &int13s, list ) {
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;
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 */
1388 DBGC2 ( int13, "INT13,%02x (%02x): ",
1389 ix86->regs.ah, bios_drive );
1391 switch ( command ) {
1393 status = int13_reset ( int13, ix86 );
1395 case INT13_GET_LAST_STATUS:
1396 status = int13_get_last_status ( int13, ix86 );
1398 case INT13_READ_SECTORS:
1399 status = int13_read_sectors ( int13, ix86 );
1401 case INT13_WRITE_SECTORS:
1402 status = int13_write_sectors ( int13, ix86 );
1404 case INT13_GET_PARAMETERS:
1405 status = int13_get_parameters ( int13, ix86 );
1407 case INT13_GET_DISK_TYPE:
1408 status = int13_get_disk_type ( int13, ix86 );
1410 case INT13_EXTENSION_CHECK:
1411 status = int13_extension_check ( int13, ix86 );
1413 case INT13_EXTENDED_READ:
1414 status = int13_extended_read ( int13, ix86 );
1416 case INT13_EXTENDED_WRITE:
1417 status = int13_extended_write ( int13, ix86 );
1419 case INT13_EXTENDED_VERIFY:
1420 status = int13_extended_verify ( int13, ix86 );
1422 case INT13_EXTENDED_SEEK:
1423 status = int13_extended_seek ( int13, ix86 );
1425 case INT13_GET_EXTENDED_PARAMETERS:
1426 status = int13_get_extended_parameters ( int13, ix86 );
1428 case INT13_CDROM_STATUS_TERMINATE:
1429 status = int13_cdrom_status_terminate ( int13, ix86 );
1431 case INT13_CDROM_READ_BOOT_CATALOG:
1432 status = int13_cdrom_read_boot_catalog ( int13, ix86 );
1435 DBGC2 ( int13, "*** Unrecognised INT13 ***\n" );
1436 status = -INT13_STATUS_INVALID;
1440 /* Store status for INT 13,01 */
1441 int13->last_status = status;
1443 /* Negative status indicates an error */
1446 DBGC ( int13, "INT13,%02x (%02x) failed with status "
1447 "%02x\n", ix86->regs.ah, int13->drive, status );
1451 ix86->regs.ah = status;
1453 /* Set OF to indicate to wrapper not to chain this call */
1461 * Hook INT 13 handler
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()).
1469 __asm__ __volatile__ (
1470 TEXT16_CODE ( "\nint13_wrapper:\n\t"
1471 /* Preserve %ax and %dx for future reference */
1473 "movw %%sp, %%bp\n\t"
1476 /* Clear OF, set CF, call int13() */
1481 "call prot_call\n\t"
1482 /* Chain if OF not set */
1485 "lcall *%%cs:int13_vector\n\t"
1487 /* Overwrite flags for iret */
1492 * INT 13,15 : do nothing if hard disk
1493 * INT 13,08 : load with number of drives
1494 * all others: restore original value
1496 "cmpb $0x15, -1(%%bp)\n\t"
1498 "testb $0x80, -4(%%bp)\n\t"
1501 "movb -4(%%bp), %%dl\n\t"
1502 "cmpb $0x08, -1(%%bp)\n\t"
1504 "testb $0x80, %%dl\n\t"
1505 "movb %%cs:num_drives, %%dl\n\t"
1507 "movb %%cs:num_fdds, %%dl\n\t"
1510 "movw %%bp, %%sp\n\t"
1513 : : "i" ( int13 ) );
1515 hook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
1520 * Unhook INT 13 handler
1522 static void int13_unhook_vector ( void ) {
1523 unhook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
1528 * Check INT13 emulated drive flow control window
1530 * @v int13 Emulated drive
1532 static size_t int13_block_window ( struct int13_drive *int13 __unused ) {
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.
1542 * Handle INT 13 emulated drive underlying block device closing
1544 * @v int13 Emulated drive
1545 * @v rc Reason for close
1547 static void int13_block_close ( struct int13_drive *int13, int rc ) {
1549 /* Any closing is an error from our point of view */
1553 DBGC ( int13, "INT13 drive %02x went away: %s\n",
1554 int13->drive, strerror ( rc ) );
1556 /* Record block device error code */
1557 int13->block_rc = rc;
1559 /* Shut down interfaces */
1560 intf_restart ( &int13->block, rc );
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 ),
1569 /** INT 13 drive interface descriptor */
1570 static struct interface_descriptor int13_block_desc =
1571 INTF_DESC ( struct int13_drive, block, int13_block_op );
1574 * Free INT 13 emulated drive
1576 * @v refcnt Reference count
1578 static void int13_free ( struct refcnt *refcnt ) {
1579 struct int13_drive *int13 =
1580 container_of ( refcnt, struct int13_drive, refcnt );
1582 uri_put ( int13->uri );
1587 * Hook INT 13 emulated drive
1590 * @v drive Drive number
1591 * @ret rc Return status code
1593 * Registers the drive with the INT 13 emulation subsystem, and hooks
1594 * the INT 13 interrupt vector (if not already hooked).
1596 static int int13_hook ( struct uri *uri, unsigned int drive ) {
1597 struct int13_drive *int13;
1598 unsigned int natural_drive;
1602 /* Calculate natural drive number */
1603 int13_sync_num_drives();
1604 natural_drive = ( ( drive & 0x80 ) ? ( num_drives | 0x80 ) : num_fdds );
1606 /* Check that drive number is not in use */
1607 list_for_each_entry ( int13, &int13s, list ) {
1608 if ( int13->drive == drive ) {
1614 /* Allocate and initialise structure */
1615 int13 = zalloc ( sizeof ( *int13 ) );
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;
1626 /* Open block device interface */
1627 if ( ( rc = int13_reopen_block ( int13 ) ) != 0 )
1628 goto err_reopen_block;
1630 /* Read device capacity */
1631 if ( ( rc = int13_read_capacity ( int13 ) ) != 0 )
1632 goto err_read_capacity;
1634 /* Allocate scratch area */
1635 scratch = malloc ( int13_blksize ( int13 ) );
1637 goto err_alloc_scratch;
1639 /* Parse parameters, if present */
1640 if ( ( rc = int13_parse_iso9660 ( int13, scratch ) ) != 0 )
1641 goto err_parse_iso9660;
1643 /* Give drive a default geometry */
1644 if ( ( rc = int13_guess_geometry ( int13, scratch ) ) != 0 )
1645 goto err_guess_geometry;
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 );
1651 /* Hook INT 13 vector if not already hooked */
1652 if ( list_empty ( &int13s ) ) {
1653 int13_hook_vector();
1657 /* Add to list of emulated drives */
1658 list_add ( &int13->list, &int13s );
1660 /* Update BIOS drive count */
1661 int13_sync_num_drives();
1672 intf_shutdown ( &int13->block, rc );
1673 ref_put ( &int13->refcnt );
1680 * Find INT 13 emulated drive by drive number
1682 * @v drive Drive number
1683 * @ret int13 Emulated drive, or NULL
1685 static struct int13_drive * int13_find ( unsigned int drive ) {
1686 struct int13_drive *int13;
1688 list_for_each_entry ( int13, &int13s, list ) {
1689 if ( int13->drive == drive )
1696 * Unhook INT 13 emulated drive
1698 * @v drive Drive number
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
1704 static void int13_unhook ( unsigned int drive ) {
1705 struct int13_drive *int13;
1708 int13 = int13_find ( drive );
1710 DBG ( "INT13 cannot find emulated drive %02x\n", drive );
1714 /* Shut down interfaces */
1715 intf_shutdown ( &int13->block, 0 );
1717 /* Remove from list of emulated drives */
1718 list_del ( &int13->list );
1720 /* Should adjust BIOS drive count, but it's difficult
1721 * to do so reliably.
1724 DBGC ( int13, "INT13 drive %02x unregistered\n", int13->drive );
1726 /* Unhook INT 13 vector if no more drives */
1727 if ( list_empty ( &int13s ) ) {
1729 int13_unhook_vector();
1732 /* Drop list's reference to drive */
1733 ref_put ( &int13->refcnt );
1737 * Load and verify master boot record from INT 13 drive
1739 * @v drive Drive number
1740 * @v address Boot code address to fill in
1741 * @ret rc Return status code
1743 static int int13_load_mbr ( unsigned int drive, struct segoff *address ) {
1745 int discard_b, discard_c, discard_d;
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"
1758 "sti\n\t" /* BIOS bugs */
1760 "xorw %%ax, %%ax\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 ) );
1768 DBG ( "INT13 drive %02x could not read MBR (status %02x)\n",
1773 /* Check magic signature */
1774 get_real ( magic, address->segment,
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",
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 ),
1793 #define eltorito_cmd __use_data16 ( eltorito_cmd )
1795 /** El Torito disk address packet */
1796 static struct int13_disk_address __bss16 ( eltorito_address );
1797 #define eltorito_address __use_data16 ( eltorito_address )
1800 * Load and verify El Torito boot record from INT 13 drive
1802 * @v drive Drive number
1803 * @v address Boot code address to fill in
1804 * @ret rc Return status code
1806 static int int13_load_eltorito ( unsigned int drive, struct segoff *address ) {
1808 struct eltorito_validation_entry valid;
1809 struct eltorito_boot_entry boot;
1810 } __attribute__ (( packed )) catalog;
1813 /* Use INT 13, 4d to read the boot catalog */
1814 __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
1817 "sti\n\t" /* BIOS bugs */
1819 "xorw %%ax, %%ax\n\t"
1822 : "a" ( 0x4d00 ), "d" ( drive ),
1823 "S" ( __from_data16 ( &eltorito_cmd ) ) );
1825 DBG ( "INT13 drive %02x could not read El Torito boot catalog "
1826 "(status %02x)\n", drive, status );
1829 copy_from_user ( &catalog, phys_to_user ( eltorito_cmd.buffer ), 0,
1830 sizeof ( catalog ) );
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 );
1838 if ( catalog.boot.indicator != ELTORITO_BOOTABLE ) {
1839 DBG ( "INT13 drive %02x El Torito is not bootable\n", drive );
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 );
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 );
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"
1864 "sti\n\t" /* BIOS bugs */
1866 "xorw %%ax, %%ax\n\t"
1869 : "a" ( 0x4200 ), "d" ( drive ),
1870 "S" ( __from_data16 ( &eltorito_address ) ) );
1872 DBG ( "INT13 drive %02x could not read El Torito boot image "
1873 "(status %02x)\n", drive, status );
1881 * Attempt to boot from an INT 13 drive
1883 * @v drive Drive number
1884 * @ret rc Return status code
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).
1891 * Note that this function can never return success, by definition.
1893 static int int13_boot ( unsigned int drive ) {
1894 struct memory_map memmap;
1895 struct segoff address;
1898 /* Look for a usable boot sector */
1899 if ( ( ( rc = int13_load_mbr ( drive, &address ) ) != 0 ) &&
1900 ( ( rc = int13_load_eltorito ( drive, &address ) ) != 0 ) )
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
1908 get_memmap ( &memmap );
1910 /* Jump to boot sector */
1911 if ( ( rc = call_bootsector ( address.segment, address.offset,
1913 DBG ( "INT13 drive %02x boot returned: %s\n",
1914 drive, strerror ( rc ) );
1918 return -ECANCELED; /* -EIMPOSSIBLE */
1921 /** A boot firmware table generated by iPXE */
1924 struct acpi_description_header acpi;
1929 /** The boot firmware table generated by iPXE */
1930 static union xbft_table __bss16 ( xbftab ) __attribute__ (( aligned ( 16 ) ));
1931 #define xbftab __use_data16 ( xbftab )
1934 * Describe INT 13 emulated drive for SAN-booted operating system
1936 * @v drive Drive number
1937 * @ret rc Return status code
1939 static int int13_describe ( unsigned int drive ) {
1940 struct int13_drive *int13;
1941 struct segoff xbft_address;
1945 int13 = int13_find ( drive );
1947 DBG ( "INT13 cannot find emulated drive %02x\n", drive );
1951 /* Reopen block device if necessary */
1952 if ( ( int13->block_rc != 0 ) &&
1953 ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) )
1957 memset ( &xbftab, 0, sizeof ( xbftab ) );
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 ) );
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 ) );
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 ) );
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 );