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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <gpxe/list.h>
25 #include <gpxe/blockdev.h>
29 #include <bootsector.h>
36 * This module provides a mechanism for exporting block devices via
37 * the BIOS INT 13 disk interrupt interface.
41 /** Vector for chaining to other INT 13 handlers */
42 static struct segoff __text16 ( int13_vector );
43 #define int13_vector __use_text16 ( int13_vector )
45 /** Assembly wrapper */
46 extern void int13_wrapper ( void );
48 /** List of registered emulated drives */
49 static LIST_HEAD ( drives );
52 * INT 13, 00 - Reset disk system
54 * @v drive Emulated drive
55 * @ret status Status code
57 static int int13_reset ( struct int13_drive *drive __unused,
58 struct i386_all_regs *ix86 __unused ) {
59 DBG ( "Reset drive\n" );
64 * INT 13, 01 - Get status of last operation
66 * @v drive Emulated drive
67 * @ret status Status code
69 static int int13_get_last_status ( struct int13_drive *drive,
70 struct i386_all_regs *ix86 __unused ) {
71 DBG ( "Get status of last operation\n" );
72 return drive->last_status;
76 * Read / write sectors
78 * @v drive Emulated drive
79 * @v al Number of sectors to read or write (must be nonzero)
80 * @v ch Low bits of cylinder number
81 * @v cl (bits 7:6) High bits of cylinder number
82 * @v cl (bits 5:0) Sector number
84 * @v es:bx Data buffer
85 * @v io Read / write method
86 * @ret status Status code
87 * @ret al Number of sectors read or written
89 static int int13_rw_sectors ( struct int13_drive *drive,
90 struct i386_all_regs *ix86,
91 int ( * io ) ( struct block_device *blockdev,
94 userptr_t buffer ) ) {
95 struct block_device *blockdev = drive->blockdev;
96 unsigned int cylinder, head, sector;
101 /* Validate blocksize */
102 if ( blockdev->blksize != INT13_BLKSIZE ) {
103 DBG ( "Invalid blocksize (%zd) for non-extended read/write\n",
105 return -INT13_STATUS_INVALID;
108 /* Calculate parameters */
109 cylinder = ( ( ( ix86->regs.cl & 0xc0 ) << 2 ) | ix86->regs.ch );
110 assert ( cylinder < drive->cylinders );
111 head = ix86->regs.dh;
112 assert ( head < drive->heads );
113 sector = ( ix86->regs.cl & 0x3f );
114 assert ( ( sector >= 1 ) && ( sector <= drive->sectors_per_track ) );
115 lba = ( ( ( ( cylinder * drive->heads ) + head )
116 * drive->sectors_per_track ) + sector - 1 );
117 count = ix86->regs.al;
118 buffer = real_to_user ( ix86->segs.es, ix86->regs.bx );
120 DBG ( "C/H/S %d/%d/%d = LBA %#lx <-> %04x:%04x (count %d)\n", cylinder,
121 head, sector, lba, ix86->segs.es, ix86->regs.bx, count );
123 /* Read from / write to block device */
124 if ( io ( blockdev, lba, count, buffer ) != 0 )
125 return -INT13_STATUS_READ_ERROR;
131 * INT 13, 02 - Read sectors
133 * @v drive Emulated drive
134 * @v al Number of sectors to read (must be nonzero)
135 * @v ch Low bits of cylinder number
136 * @v cl (bits 7:6) High bits of cylinder number
137 * @v cl (bits 5:0) Sector number
139 * @v es:bx Data buffer
140 * @ret status Status code
141 * @ret al Number of sectors read
143 static int int13_read_sectors ( struct int13_drive *drive,
144 struct i386_all_regs *ix86 ) {
146 return int13_rw_sectors ( drive, ix86, drive->blockdev->read );
150 * INT 13, 03 - Write sectors
152 * @v drive Emulated drive
153 * @v al Number of sectors to write (must be nonzero)
154 * @v ch Low bits of cylinder number
155 * @v cl (bits 7:6) High bits of cylinder number
156 * @v cl (bits 5:0) Sector number
158 * @v es:bx Data buffer
159 * @ret status Status code
160 * @ret al Number of sectors written
162 static int int13_write_sectors ( struct int13_drive *drive,
163 struct i386_all_regs *ix86 ) {
165 return int13_rw_sectors ( drive, ix86, drive->blockdev->write );
169 * INT 13, 08 - Get drive parameters
171 * @v drive Emulated drive
172 * @ret status Status code
173 * @ret ch Low bits of maximum cylinder number
174 * @ret cl (bits 7:6) High bits of maximum cylinder number
175 * @ret cl (bits 5:0) Maximum sector number
176 * @ret dh Maximum head number
177 * @ret dl Number of drives
179 static int int13_get_parameters ( struct int13_drive *drive,
180 struct i386_all_regs *ix86 ) {
181 unsigned int max_cylinder = drive->cylinders - 1;
182 unsigned int max_head = drive->heads - 1;
183 unsigned int max_sector = drive->sectors_per_track; /* sic */
185 DBG ( "Get drive parameters\n" );
187 ix86->regs.ch = ( max_cylinder & 0xff );
188 ix86->regs.cl = ( ( ( max_cylinder >> 8 ) << 6 ) | max_sector );
189 ix86->regs.dh = max_head;
190 get_real ( ix86->regs.dl, BDA_SEG, BDA_NUM_DRIVES );
195 * INT 13, 15 - Get disk type
197 * @v drive Emulated drive
199 * @ret cx:dx Sector count
200 * @ret status Status code / disk type
202 static int int13_get_disk_type ( struct int13_drive *drive,
203 struct i386_all_regs *ix86 ) {
204 DBG ( "Get disk type\n" );
205 ix86->regs.cx = ( drive->cylinders >> 16 );
206 ix86->regs.dx = ( drive->cylinders & 0xffff );
207 return INT13_DISK_TYPE_HDD;
211 * INT 13, 41 - Extensions installation check
213 * @v drive Emulated drive
216 * @ret cx Extensions API support bitmap
217 * @ret status Status code / API version
219 static int int13_extension_check ( struct int13_drive *drive __unused,
220 struct i386_all_regs *ix86 ) {
221 if ( ix86->regs.bx == 0x55aa ) {
222 DBG ( "INT 13 extensions installation check\n" );
223 ix86->regs.bx = 0xaa55;
224 ix86->regs.cx = INT13_EXTENSION_LINEAR;
225 return INT13_EXTENSION_VER_1_X;
227 return -INT13_STATUS_INVALID;
232 * Extended read / write
234 * @v drive Emulated drive
235 * @v ds:si Disk address packet
236 * @v io Read / write method
237 * @ret status Status code
239 static int int13_extended_rw ( struct int13_drive *drive,
240 struct i386_all_regs *ix86,
241 int ( * io ) ( struct block_device *blockdev,
244 userptr_t buffer ) ) {
245 struct block_device *blockdev = drive->blockdev;
246 struct int13_disk_address addr;
251 /* Read parameters from disk address structure */
252 copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si, sizeof ( addr ));
255 buffer = real_to_user ( addr.buffer.segment, addr.buffer.offset );
257 DBG ( "LBA %#llx <-> %04x:%04x (count %ld)\n", (unsigned long long)lba,
258 addr.buffer.segment, addr.buffer.offset, count );
260 /* Read from / write to block device */
261 if ( io ( blockdev, lba, count, buffer ) != 0 )
262 return -INT13_STATUS_READ_ERROR;
268 * INT 13, 42 - Extended read
270 * @v drive Emulated drive
271 * @v ds:si Disk address packet
272 * @ret status Status code
274 static int int13_extended_read ( struct int13_drive *drive,
275 struct i386_all_regs *ix86 ) {
276 DBG ( "Extended read: " );
277 return int13_extended_rw ( drive, ix86, drive->blockdev->read );
281 * INT 13, 43 - Extended write
283 * @v drive Emulated drive
284 * @v ds:si Disk address packet
285 * @ret status Status code
287 static int int13_extended_write ( struct int13_drive *drive,
288 struct i386_all_regs *ix86 ) {
289 DBG ( "Extended write: " );
290 return int13_extended_rw ( drive, ix86, drive->blockdev->write );
294 * INT 13, 48 - Get extended parameters
296 * @v drive Emulated drive
297 * @v ds:si Drive parameter table
298 * @ret status Status code
300 static int int13_get_extended_parameters ( struct int13_drive *drive,
301 struct i386_all_regs *ix86 ) {
302 struct int13_disk_parameters params = {
303 .bufsize = sizeof ( params ),
304 .flags = INT13_FL_DMA_TRANSPARENT,
305 .cylinders = drive->cylinders,
306 .heads = drive->heads,
307 .sectors_per_track = drive->sectors_per_track,
308 .sectors = drive->blockdev->blocks,
309 .sector_size = drive->blockdev->blksize,
312 DBG ( "Get extended drive parameters to %04x:%04x\n",
313 ix86->segs.ds, ix86->regs.si );
315 copy_to_real ( ix86->segs.ds, ix86->regs.si, ¶ms,
320 struct int13_cdrom_specification {
321 /** Size of packet in bytes */
323 /** Boot media type */
330 * INT 13, 4b - Get CD-ROM status / terminate emulation
332 * @v drive Emulated drive
333 * @v ds:si El Torito specification packet to fill in
334 * @ret status Status code
336 static int int13_cdrom_status_terminate ( struct int13_drive *drive,
337 struct i386_all_regs *ix86 ) {
338 struct int13_cdrom_specification specification;
340 DBG ( "Get CD-ROM emulation parameters to %04x:%04x\n",
341 ix86->segs.ds, ix86->regs.di );
343 memset ( &specification, 0, sizeof ( specification ) );
344 specification.size = sizeof ( specification );
345 specification.drive = drive->drive;
347 copy_to_real ( ix86->segs.ds, ix86->regs.si, &specification,
348 sizeof ( specification ) );
356 static void int13 ( struct i386_all_regs *ix86 ) {
357 int command = ix86->regs.ah;
358 unsigned int bios_drive = ix86->regs.dl;
359 unsigned int original_bios_drive = bios_drive;
360 struct int13_drive *drive;
363 list_for_each_entry ( drive, &drives, list ) {
364 if ( drive->drive > bios_drive )
366 if ( drive->drive < bios_drive ) {
367 original_bios_drive--;
371 DBG ( "INT 13,%04x (%02x): ", ix86->regs.ax, drive->drive );
375 status = int13_reset ( drive, ix86 );
377 case INT13_GET_LAST_STATUS:
378 status = int13_get_last_status ( drive, ix86 );
380 case INT13_READ_SECTORS:
381 status = int13_read_sectors ( drive, ix86 );
383 case INT13_WRITE_SECTORS:
384 status = int13_write_sectors ( drive, ix86 );
386 case INT13_GET_PARAMETERS:
387 status = int13_get_parameters ( drive, ix86 );
389 case INT13_GET_DISK_TYPE:
390 status = int13_get_disk_type ( drive, ix86 );
392 case INT13_EXTENSION_CHECK:
393 status = int13_extension_check ( drive, ix86 );
395 case INT13_EXTENDED_READ:
396 status = int13_extended_read ( drive, ix86 );
398 case INT13_EXTENDED_WRITE:
399 status = int13_extended_write ( drive, ix86 );
401 case INT13_GET_EXTENDED_PARAMETERS:
402 status = int13_get_extended_parameters ( drive, ix86 );
404 case INT13_CDROM_STATUS_TERMINATE:
405 status = int13_cdrom_status_terminate ( drive, ix86 );
408 DBG ( "*** Unrecognised INT 13 ***\n" );
409 status = -INT13_STATUS_INVALID;
413 /* Store status for INT 13,01 */
414 drive->last_status = status;
416 /* Negative status indicates an error */
419 DBG ( "INT13 failed with status %x\n", status );
423 ix86->regs.ah = status;
425 /* Set OF to indicate to wrapper not to chain this call */
431 /* Remap BIOS drive */
432 if ( bios_drive != original_bios_drive ) {
433 DBG ( "INT 13,%04x (%02x) remapped to (%02x)\n",
434 ix86->regs.ax, bios_drive, original_bios_drive );
436 ix86->regs.dl = original_bios_drive;
440 * Hook INT 13 handler
443 static void hook_int13 ( void ) {
444 /* Assembly wrapper to call int13(). int13() sets OF if we
445 * should not chain to the previous handler. (The wrapper
446 * clears CF and OF before calling int13()).
448 __asm__ __volatile__ (
449 TEXT16_CODE ( "\nint13_wrapper:\n\t"
450 /* Preserve %ax and %dx for future reference */
452 "movw %%sp, %%bp\n\t"
455 /* Clear OF, set CF, call int13() */
461 /* Chain if OF not set */
464 "lcall *%%cs:int13_vector\n\t"
466 /* Overwrite flags for iret */
471 * INT 13,15 : do nothing
472 * INT 13,08 : load with number of drives
473 * all others: restore original value
475 "cmpb $0x15, -1(%%bp)\n\t"
477 "movb -4(%%bp), %%dl\n\t"
478 "cmpb $0x08, -1(%%bp)\n\t"
487 "movw %%bp, %%sp\n\t"
490 : : "i" ( int13 ), "i" ( BDA_SEG ), "i" ( BDA_NUM_DRIVES ) );
492 hook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
497 * Unhook INT 13 handler
499 static void unhook_int13 ( void ) {
500 unhook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
505 * Guess INT 13 drive geometry
507 * @v drive Emulated drive
509 * Guesses the drive geometry by inspecting the partition table.
511 static void guess_int13_geometry ( struct int13_drive *drive ) {
512 struct master_boot_record mbr;
513 struct partition_table_entry *partition;
514 unsigned int guessed_heads = 255;
515 unsigned int guessed_sectors_per_track = 63;
516 unsigned long blocks;
517 unsigned long blocks_per_cyl;
520 /* Don't even try when the blksize is invalid for C/H/S access */
521 if ( drive->blockdev->blksize != INT13_BLKSIZE )
524 /* Scan through partition table and modify guesses for heads
525 * and sectors_per_track if we find any used partitions.
527 if ( drive->blockdev->read ( drive->blockdev, 0, 1,
528 virt_to_user ( &mbr ) ) == 0 ) {
529 for ( i = 0 ; i < 4 ; i++ ) {
530 partition = &mbr.partitions[i];
531 if ( ! partition->type )
534 ( PART_HEAD ( partition->chs_end ) + 1 );
535 guessed_sectors_per_track =
536 PART_SECTOR ( partition->chs_end );
537 DBG ( "Guessing C/H/S xx/%d/%d based on partition "
538 "%d\n", guessed_heads,
539 guessed_sectors_per_track, ( i + 1 ) );
542 DBG ( "Could not read partition table to guess geometry\n" );
545 /* Apply guesses if no geometry already specified */
546 if ( ! drive->heads )
547 drive->heads = guessed_heads;
548 if ( ! drive->sectors_per_track )
549 drive->sectors_per_track = guessed_sectors_per_track;
550 if ( ! drive->cylinders ) {
551 /* Avoid attempting a 64-bit divide on a 32-bit system */
552 blocks = ( ( drive->blockdev->blocks <= ULONG_MAX ) ?
553 drive->blockdev->blocks : ULONG_MAX );
554 blocks_per_cyl = ( drive->heads * drive->sectors_per_track );
555 assert ( blocks_per_cyl != 0 );
556 drive->cylinders = ( blocks / blocks_per_cyl );
557 if ( drive->cylinders > 1024 )
558 drive->cylinders = 1024;
563 * Register INT 13 emulated drive
565 * @v drive Emulated drive
567 * Registers the drive with the INT 13 emulation subsystem, and hooks
568 * the INT 13 interrupt vector (if not already hooked).
570 * The underlying block device must be valid. A drive number and
571 * geometry will be assigned if left blank.
573 void register_int13_drive ( struct int13_drive *drive ) {
576 /* Give drive a default geometry if none specified */
577 guess_int13_geometry ( drive );
579 /* Assign drive number if none specified, update BIOS drive count */
580 get_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
581 if ( ! drive->drive )
582 drive->drive = ( num_drives | 0x80 );
584 if ( num_drives <= ( drive->drive & 0x7f ) )
585 num_drives = ( ( drive->drive & 0x7f ) + 1 );
586 put_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
588 DBG ( "Registered INT13 drive %02x with C/H/S geometry %d/%d/%d\n",
589 drive->drive, drive->cylinders, drive->heads,
590 drive->sectors_per_track );
592 /* Hook INT 13 vector if not already hooked */
593 if ( list_empty ( &drives ) )
596 /* Add to list of emulated drives */
597 list_add ( &drive->list, &drives );
601 * Unregister INT 13 emulated drive
603 * @v drive Emulated drive
605 * Unregisters the drive from the INT 13 emulation subsystem. If this
606 * is the last emulated drive, the INT 13 vector is unhooked (if
609 void unregister_int13_drive ( struct int13_drive *drive ) {
610 /* Remove from list of emulated drives */
611 list_del ( &drive->list );
613 /* Should adjust BIOS drive count, but it's difficult to do so
617 DBG ( "Unregistered INT13 drive %02x\n", drive->drive );
619 /* Unhook INT 13 vector if no more drives */
620 if ( list_empty ( &drives ) )
625 * Attempt to boot from an INT 13 drive
627 * @v drive Drive number
628 * @ret rc Return status code
630 * This boots from the specified INT 13 drive by loading the Master
631 * Boot Record to 0000:7c00 and jumping to it. INT 18 is hooked to
632 * capture an attempt by the MBR to boot the next device. (This is
633 * the closest thing to a return path from an MBR).
635 * Note that this function can never return success, by definition.
637 int int13_boot ( unsigned int drive ) {
638 int status, signature;
639 int discard_c, discard_d;
642 DBG ( "Booting from INT 13 drive %02x\n", drive );
644 /* Use INT 13 to read the boot sector */
645 __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
650 "sti\n\t" /* BIOS bugs */
652 "xorl %%eax, %%eax\n\t"
654 "movzwl %%es:0x7dfe, %%ebx\n\t"
656 : "=a" ( status ), "=b" ( signature ),
657 "=c" ( discard_c ), "=d" ( discard_d )
658 : "a" ( 0x0201 ), "b" ( 0x7c00 ),
659 "c" ( 1 ), "d" ( drive ) );
663 /* Check signature is correct */
664 if ( signature != be16_to_cpu ( 0x55aa ) ) {
665 DBG ( "Invalid disk signature %#04x (should be 0x55aa)\n",
666 cpu_to_be16 ( signature ) );
670 /* Jump to boot sector */
671 if ( ( rc = call_bootsector ( 0x0, 0x7c00, drive ) ) != 0 ) {
672 DBG ( "INT 13 drive %02x boot returned\n", drive );
676 return -ECANCELED; /* -EIMPOSSIBLE */