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,
324 static void int13 ( struct i386_all_regs *ix86 ) {
325 int command = ix86->regs.ah;
326 unsigned int bios_drive = ix86->regs.dl;
327 unsigned int original_bios_drive = bios_drive;
328 struct int13_drive *drive;
331 list_for_each_entry ( drive, &drives, list ) {
332 if ( drive->drive > bios_drive )
334 if ( drive->drive < bios_drive ) {
335 original_bios_drive--;
339 DBG ( "INT 13,%04x (%02x): ", ix86->regs.ax, drive->drive );
343 status = int13_reset ( drive, ix86 );
345 case INT13_GET_LAST_STATUS:
346 status = int13_get_last_status ( drive, ix86 );
348 case INT13_READ_SECTORS:
349 status = int13_read_sectors ( drive, ix86 );
351 case INT13_WRITE_SECTORS:
352 status = int13_write_sectors ( drive, ix86 );
354 case INT13_GET_PARAMETERS:
355 status = int13_get_parameters ( drive, ix86 );
357 case INT13_GET_DISK_TYPE:
358 status = int13_get_disk_type ( drive, ix86 );
360 case INT13_EXTENSION_CHECK:
361 status = int13_extension_check ( drive, ix86 );
363 case INT13_EXTENDED_READ:
364 status = int13_extended_read ( drive, ix86 );
366 case INT13_EXTENDED_WRITE:
367 status = int13_extended_write ( drive, ix86 );
369 case INT13_GET_EXTENDED_PARAMETERS:
370 status = int13_get_extended_parameters ( drive, ix86 );
373 DBG ( "*** Unrecognised INT 13 ***\n" );
374 status = -INT13_STATUS_INVALID;
378 /* Store status for INT 13,01 */
379 drive->last_status = status;
381 /* Negative status indicates an error */
384 DBG ( "INT13 failed with status %x\n", status );
388 ix86->regs.ah = status;
390 /* Set OF to indicate to wrapper not to chain this call */
396 /* Remap BIOS drive */
397 if ( bios_drive != original_bios_drive ) {
398 DBG ( "INT 13,%04x (%02x) remapped to (%02x)\n",
399 ix86->regs.ax, bios_drive, original_bios_drive );
401 ix86->regs.dl = original_bios_drive;
405 * Hook INT 13 handler
408 static void hook_int13 ( void ) {
409 /* Assembly wrapper to call int13(). int13() sets OF if we
410 * should not chain to the previous handler. (The wrapper
411 * clears CF and OF before calling int13()).
413 __asm__ __volatile__ (
414 TEXT16_CODE ( "\nint13_wrapper:\n\t"
415 /* Preserve %ax and %dx for future reference */
417 "movw %%sp, %%bp\n\t"
420 /* Clear OF, set CF, call int13() */
426 /* Chain if OF not set */
429 "lcall *%%cs:int13_vector\n\t"
431 /* Overwrite flags for iret */
436 * INT 13,15 : do nothing
437 * INT 13,08 : load with number of drives
438 * all others: restore original value
440 "cmpb $0x15, -1(%%bp)\n\t"
442 "movb -4(%%bp), %%dl\n\t"
443 "cmpb $0x08, -1(%%bp)\n\t"
452 "movw %%bp, %%sp\n\t"
455 : : "i" ( int13 ), "i" ( BDA_SEG ), "i" ( BDA_NUM_DRIVES ) );
457 hook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
462 * Unhook INT 13 handler
464 static void unhook_int13 ( void ) {
465 unhook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
470 * Guess INT 13 drive geometry
472 * @v drive Emulated drive
474 * Guesses the drive geometry by inspecting the partition table.
476 static void guess_int13_geometry ( struct int13_drive *drive ) {
477 struct master_boot_record mbr;
478 struct partition_table_entry *partition;
479 unsigned int guessed_heads = 255;
480 unsigned int guessed_sectors_per_track = 63;
481 unsigned long blocks;
482 unsigned long blocks_per_cyl;
485 /* Don't even try when the blksize is invalid for C/H/S access */
486 if ( drive->blockdev->blksize != INT13_BLKSIZE )
489 /* Scan through partition table and modify guesses for heads
490 * and sectors_per_track if we find any used partitions.
492 if ( drive->blockdev->read ( drive->blockdev, 0, 1,
493 virt_to_user ( &mbr ) ) == 0 ) {
494 for ( i = 0 ; i < 4 ; i++ ) {
495 partition = &mbr.partitions[i];
496 if ( ! partition->type )
499 ( PART_HEAD ( partition->chs_end ) + 1 );
500 guessed_sectors_per_track =
501 PART_SECTOR ( partition->chs_end );
502 DBG ( "Guessing C/H/S xx/%d/%d based on partition "
503 "%d\n", guessed_heads,
504 guessed_sectors_per_track, ( i + 1 ) );
507 DBG ( "Could not read partition table to guess geometry\n" );
510 /* Apply guesses if no geometry already specified */
511 if ( ! drive->heads )
512 drive->heads = guessed_heads;
513 if ( ! drive->sectors_per_track )
514 drive->sectors_per_track = guessed_sectors_per_track;
515 if ( ! drive->cylinders ) {
516 /* Avoid attempting a 64-bit divide on a 32-bit system */
517 blocks = ( ( drive->blockdev->blocks <= ULONG_MAX ) ?
518 drive->blockdev->blocks : ULONG_MAX );
519 blocks_per_cyl = ( drive->heads * drive->sectors_per_track );
520 assert ( blocks_per_cyl != 0 );
521 drive->cylinders = ( blocks / blocks_per_cyl );
522 if ( drive->cylinders > 1024 )
523 drive->cylinders = 1024;
528 * Register INT 13 emulated drive
530 * @v drive Emulated drive
532 * Registers the drive with the INT 13 emulation subsystem, and hooks
533 * the INT 13 interrupt vector (if not already hooked).
535 * The underlying block device must be valid. A drive number and
536 * geometry will be assigned if left blank.
538 void register_int13_drive ( struct int13_drive *drive ) {
541 /* Give drive a default geometry if none specified */
542 guess_int13_geometry ( drive );
544 /* Assign drive number if none specified, update BIOS drive count */
545 get_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
546 if ( ! drive->drive )
547 drive->drive = ( num_drives | 0x80 );
549 if ( num_drives <= ( drive->drive & 0x7f ) )
550 num_drives = ( ( drive->drive & 0x7f ) + 1 );
551 put_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
553 DBG ( "Registered INT13 drive %02x with C/H/S geometry %d/%d/%d\n",
554 drive->drive, drive->cylinders, drive->heads,
555 drive->sectors_per_track );
557 /* Hook INT 13 vector if not already hooked */
558 if ( list_empty ( &drives ) )
561 /* Add to list of emulated drives */
562 list_add ( &drive->list, &drives );
566 * Unregister INT 13 emulated drive
568 * @v drive Emulated drive
570 * Unregisters the drive from the INT 13 emulation subsystem. If this
571 * is the last emulated drive, the INT 13 vector is unhooked (if
574 void unregister_int13_drive ( struct int13_drive *drive ) {
575 /* Remove from list of emulated drives */
576 list_del ( &drive->list );
578 /* Should adjust BIOS drive count, but it's difficult to do so
582 DBG ( "Unregistered INT13 drive %02x\n", drive->drive );
584 /* Unhook INT 13 vector if no more drives */
585 if ( list_empty ( &drives ) )
590 * Attempt to boot from an INT 13 drive
592 * @v drive Drive number
593 * @ret rc Return status code
595 * This boots from the specified INT 13 drive by loading the Master
596 * Boot Record to 0000:7c00 and jumping to it. INT 18 is hooked to
597 * capture an attempt by the MBR to boot the next device. (This is
598 * the closest thing to a return path from an MBR).
600 * Note that this function can never return success, by definition.
602 int int13_boot ( unsigned int drive ) {
603 int status, signature;
604 int discard_c, discard_d;
607 DBG ( "Booting from INT 13 drive %02x\n", drive );
609 /* Use INT 13 to read the boot sector */
610 __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
616 "sti\n\t" /* BIOS bugs */
618 "xorl %%eax, %%eax\n\t"
620 "movzwl %%es:0x7dfe, %%ebx\n\t"
622 : "=a" ( status ), "=b" ( signature ),
623 "=c" ( discard_c ), "=d" ( discard_d )
624 : "a" ( 0x0201 ), "b" ( 0x7c00 ),
625 "c" ( 1 ), "d" ( drive ) );
629 /* Check signature is correct */
630 if ( signature != be16_to_cpu ( 0x55aa ) ) {
631 DBG ( "Invalid disk signature %#04x (should be 0x55aa)\n",
632 cpu_to_be16 ( signature ) );
636 /* Jump to boot sector */
637 if ( ( rc = call_bootsector ( 0x0, 0x7c00, drive ) ) != 0 ) {
638 DBG ( "INT 13 drive %02x boot returned\n", drive );
642 return -ECANCELED; /* -EIMPOSSIBLE */