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 struct int13_drive *drive;
361 list_for_each_entry ( drive, &drives, list ) {
362 if ( drive->drive != ix86->regs.dl )
365 DBG ( "INT 13,%04x (%02x): ", ix86->regs.ax, drive->drive );
369 status = int13_reset ( drive, ix86 );
371 case INT13_GET_LAST_STATUS:
372 status = int13_get_last_status ( drive, ix86 );
374 case INT13_READ_SECTORS:
375 status = int13_read_sectors ( drive, ix86 );
377 case INT13_WRITE_SECTORS:
378 status = int13_write_sectors ( drive, ix86 );
380 case INT13_GET_PARAMETERS:
381 status = int13_get_parameters ( drive, ix86 );
383 case INT13_GET_DISK_TYPE:
384 status = int13_get_disk_type ( drive, ix86 );
386 case INT13_EXTENSION_CHECK:
387 status = int13_extension_check ( drive, ix86 );
389 case INT13_EXTENDED_READ:
390 status = int13_extended_read ( drive, ix86 );
392 case INT13_EXTENDED_WRITE:
393 status = int13_extended_write ( drive, ix86 );
395 case INT13_GET_EXTENDED_PARAMETERS:
396 status = int13_get_extended_parameters ( drive, ix86 );
398 case INT13_CDROM_STATUS_TERMINATE:
399 status = int13_cdrom_status_terminate ( drive, ix86 );
402 DBG ( "*** Unrecognised INT 13 ***\n" );
403 status = -INT13_STATUS_INVALID;
407 /* Store status for INT 13,01 */
408 drive->last_status = status;
410 /* Negative status indicates an error */
414 DBG ( "INT13 failed with status %x\n", status );
416 ix86->regs.ah = status;
418 /* Set OF to indicate to wrapper not to chain this call */
426 * Hook INT 13 handler
429 static void hook_int13 ( void ) {
430 /* Assembly wrapper to call int13(). int13() sets OF if we
431 * should not chain to the previous handler. (The wrapper
432 * clears CF and OF before calling int13()).
434 __asm__ __volatile__ (
435 TEXT16_CODE ( "\nint13_wrapper:\n\t"
436 "orb $0, %%al\n\t" /* clear CF and OF */
437 "pushl %0\n\t" /* call int13() */
440 "jo 1f\n\t" /* chain if OF not set */
442 "lcall *%%cs:int13_vector\n\t"
444 "call 2f\n\t" /* return with flags intact */
447 "ret $4\n\t" ) : : "i" ( int13 ) );
449 hook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
454 * Unhook INT 13 handler
456 static void unhook_int13 ( void ) {
457 unhook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
462 * Guess INT 13 drive geometry
464 * @v drive Emulated drive
466 * Guesses the drive geometry by inspecting the partition table.
468 static void guess_int13_geometry ( struct int13_drive *drive ) {
469 struct master_boot_record mbr;
470 struct partition_table_entry *partition;
471 unsigned int guessed_heads = 255;
472 unsigned int guessed_sectors_per_track = 63;
473 unsigned long blocks;
474 unsigned long blocks_per_cyl;
477 /* Don't even try when the blksize is invalid for C/H/S access */
478 if ( drive->blockdev->blksize != INT13_BLKSIZE )
481 /* Scan through partition table and modify guesses for heads
482 * and sectors_per_track if we find any used partitions.
484 if ( drive->blockdev->read ( drive->blockdev, 0, 1,
485 virt_to_user ( &mbr ) ) == 0 ) {
486 for ( i = 0 ; i < 4 ; i++ ) {
487 partition = &mbr.partitions[i];
488 if ( ! partition->type )
491 ( PART_HEAD ( partition->chs_end ) + 1 );
492 guessed_sectors_per_track =
493 PART_SECTOR ( partition->chs_end );
494 DBG ( "Guessing C/H/S xx/%d/%d based on partition "
495 "%d\n", guessed_heads,
496 guessed_sectors_per_track, ( i + 1 ) );
499 DBG ( "Could not read partition table to guess geometry\n" );
502 /* Apply guesses if no geometry already specified */
503 if ( ! drive->heads )
504 drive->heads = guessed_heads;
505 if ( ! drive->sectors_per_track )
506 drive->sectors_per_track = guessed_sectors_per_track;
507 if ( ! drive->cylinders ) {
508 /* Avoid attempting a 64-bit divide on a 32-bit system */
509 blocks = ( ( drive->blockdev->blocks <= ULONG_MAX ) ?
510 drive->blockdev->blocks : ULONG_MAX );
511 blocks_per_cyl = ( drive->heads * drive->sectors_per_track );
512 assert ( blocks_per_cyl != 0 );
513 drive->cylinders = ( blocks / blocks_per_cyl );
514 if ( drive->cylinders > 1024 )
515 drive->cylinders = 1024;
520 * Register INT 13 emulated drive
522 * @v drive Emulated drive
524 * Registers the drive with the INT 13 emulation subsystem, and hooks
525 * the INT 13 interrupt vector (if not already hooked).
527 * The underlying block device must be valid. A drive number and
528 * geometry will be assigned if left blank.
530 void register_int13_drive ( struct int13_drive *drive ) {
533 /* Give drive a default geometry if none specified */
534 guess_int13_geometry ( drive );
536 /* Assign drive number if none specified, update BIOS drive count */
537 get_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
538 if ( ! drive->drive )
539 drive->drive = ( num_drives | 0x80 );
540 if ( num_drives <= ( drive->drive & 0x7f ) )
541 num_drives = ( ( drive->drive & 0x7f ) + 1 );
542 put_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
544 DBG ( "Registered INT13 drive %02x with C/H/S geometry %d/%d/%d\n",
545 drive->drive, drive->cylinders, drive->heads,
546 drive->sectors_per_track );
548 /* Hook INT 13 vector if not already hooked */
549 if ( list_empty ( &drives ) )
552 /* Add to list of emulated drives */
553 list_add ( &drive->list, &drives );
557 * Unregister INT 13 emulated drive
559 * @v drive Emulated drive
561 * Unregisters the drive from the INT 13 emulation subsystem. If this
562 * is the last emulated drive, the INT 13 vector is unhooked (if
565 void unregister_int13_drive ( struct int13_drive *drive ) {
566 /* Remove from list of emulated drives */
567 list_del ( &drive->list );
569 DBG ( "Unregistered INT13 drive %02x\n", drive->drive );
571 /* Unhook INT 13 vector if no more drives */
572 if ( list_empty ( &drives ) )
577 * Attempt to boot from an INT 13 drive
579 * @v drive Drive number
580 * @ret rc Return status code
582 * This boots from the specified INT 13 drive by loading the Master
583 * Boot Record to 0000:7c00 and jumping to it. INT 18 is hooked to
584 * capture an attempt by the MBR to boot the next device. (This is
585 * the closest thing to a return path from an MBR).
587 * Note that this function can never return success, by definition.
589 int int13_boot ( unsigned int drive ) {
590 int status, signature;
591 int discard_c, discard_d;
594 DBG ( "Booting from INT 13 drive %02x\n", drive );
596 /* Use INT 13 to read the boot sector */
597 __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
602 "sti\n\t" /* BIOS bugs */
604 "xorl %%eax, %%eax\n\t"
606 "movzwl %%es:0x7dfe, %%ebx\n\t"
608 : "=a" ( status ), "=b" ( signature ),
609 "=c" ( discard_c ), "=d" ( discard_d )
610 : "a" ( 0x0201 ), "b" ( 0x7c00 ),
611 "c" ( 1 ), "d" ( drive ) );
615 /* Check signature is correct */
616 if ( signature != be16_to_cpu ( 0x55aa ) ) {
617 DBG ( "Invalid disk signature %#04x (should be 0x55aa)\n",
618 cpu_to_be16 ( signature ) );
622 /* Jump to boot sector */
623 if ( ( rc = call_bootsector ( 0x0, 0x7c00, drive ) ) != 0 ) {
624 DBG ( "INT 13 drive %02x boot returned\n", drive );
628 return -ECANCELED; /* -EIMPOSSIBLE */