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>
35 * This module provides a mechanism for exporting block devices via
36 * the BIOS INT 13 disk interrupt interface.
40 /** Vector for chaining to other INT 13 handlers */
41 static struct segoff __text16 ( int13_vector );
42 #define int13_vector __use_text16 ( int13_vector )
44 /** Assembly wrapper */
45 extern void int13_wrapper ( void );
47 /** Vector for storing original INT 18 handler
49 * We do not chain to this vector, so there is no need to place it in
52 static struct segoff int18_vector;
54 /** Vector for storing original INT 19 handler
56 * We do not chain to this vector, so there is no need to place it in
59 static struct segoff int19_vector;
61 /** Restart point for INT 18 or 19 */
62 extern void int13_exec_fail ( void );
64 /** List of registered emulated drives */
65 static LIST_HEAD ( drives );
68 * INT 13, 00 - Reset disk system
70 * @v drive Emulated drive
71 * @ret status Status code
73 static int int13_reset ( struct int13_drive *drive __unused,
74 struct i386_all_regs *ix86 __unused ) {
75 DBG ( "Reset drive\n" );
80 * INT 13, 01 - Get status of last operation
82 * @v drive Emulated drive
83 * @ret status Status code
85 static int int13_get_last_status ( struct int13_drive *drive,
86 struct i386_all_regs *ix86 __unused ) {
87 DBG ( "Get status of last operation\n" );
88 return drive->last_status;
92 * Read / write sectors
94 * @v drive Emulated drive
95 * @v al Number of sectors to read or write (must be nonzero)
96 * @v ch Low bits of cylinder number
97 * @v cl (bits 7:6) High bits of cylinder number
98 * @v cl (bits 5:0) Sector number
100 * @v es:bx Data buffer
101 * @v io Read / write method
102 * @ret status Status code
103 * @ret al Number of sectors read or written
105 static int int13_rw_sectors ( struct int13_drive *drive,
106 struct i386_all_regs *ix86,
107 int ( * io ) ( struct block_device *blockdev,
110 userptr_t buffer ) ) {
111 struct block_device *blockdev = drive->blockdev;
112 unsigned int cylinder, head, sector;
117 /* Calculate parameters */
118 cylinder = ( ( ( ix86->regs.cl & 0xc0 ) << 8 ) | ix86->regs.ch );
119 assert ( cylinder < drive->cylinders );
120 head = ix86->regs.dh;
121 assert ( head < drive->heads );
122 sector = ( ix86->regs.cl & 0x3f );
123 assert ( ( sector >= 1 ) && ( sector <= drive->sectors_per_track ) );
124 lba = ( ( ( ( cylinder * drive->heads ) + head )
125 * drive->sectors_per_track ) + sector - 1 );
126 count = ix86->regs.al;
127 buffer = real_to_user ( ix86->segs.es, ix86->regs.bx );
129 DBG ( "C/H/S %d/%d/%d = LBA %#lx <-> %04x:%04x (count %d)\n", cylinder,
130 head, sector, lba, ix86->segs.es, ix86->regs.bx, count );
132 /* Validate blocksize */
133 if ( blockdev->blksize != INT13_BLKSIZE ) {
134 DBG ( "Invalid blocksize (%zd) for non-extended read/write\n",
136 return INT13_STATUS_INVALID;
139 /* Read from / write to block device */
140 if ( io ( blockdev, lba, count, buffer ) != 0 )
141 return INT13_STATUS_READ_ERROR;
147 * INT 13, 02 - Read sectors
149 * @v drive Emulated drive
150 * @v al Number of sectors to read (must be nonzero)
151 * @v ch Low bits of cylinder number
152 * @v cl (bits 7:6) High bits of cylinder number
153 * @v cl (bits 5:0) Sector number
155 * @v es:bx Data buffer
156 * @ret status Status code
157 * @ret al Number of sectors read
159 static int int13_read_sectors ( struct int13_drive *drive,
160 struct i386_all_regs *ix86 ) {
162 return int13_rw_sectors ( drive, ix86, drive->blockdev->read );
166 * INT 13, 03 - Write sectors
168 * @v drive Emulated drive
169 * @v al Number of sectors to write (must be nonzero)
170 * @v ch Low bits of cylinder number
171 * @v cl (bits 7:6) High bits of cylinder number
172 * @v cl (bits 5:0) Sector number
174 * @v es:bx Data buffer
175 * @ret status Status code
176 * @ret al Number of sectors written
178 static int int13_write_sectors ( struct int13_drive *drive,
179 struct i386_all_regs *ix86 ) {
181 return int13_rw_sectors ( drive, ix86, drive->blockdev->write );
185 * INT 13, 08 - Get drive parameters
187 * @v drive Emulated drive
188 * @ret status Status code
189 * @ret ch Low bits of maximum cylinder number
190 * @ret cl (bits 7:6) High bits of maximum cylinder number
191 * @ret cl (bits 5:0) Maximum sector number
192 * @ret dh Maximum head number
193 * @ret dl Number of drives
195 static int int13_get_parameters ( struct int13_drive *drive,
196 struct i386_all_regs *ix86 ) {
197 unsigned int max_cylinder = drive->cylinders - 1;
198 unsigned int max_head = drive->heads - 1;
199 unsigned int max_sector = drive->sectors_per_track; /* sic */
201 DBG ( "Get drive parameters\n" );
203 ix86->regs.ch = ( max_cylinder & 0xff );
204 ix86->regs.cl = ( ( ( max_cylinder >> 8 ) << 6 ) | max_sector );
205 ix86->regs.dh = max_head;
206 get_real ( ix86->regs.dl, BDA_SEG, BDA_NUM_DRIVES );
211 * INT 13, 41 - Extensions installation check
213 * @v drive Emulated drive
216 * @ret cx Extensions API support bitmap
217 * @ret status Status code
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;
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 struct int13_drive *drive;
328 list_for_each_entry ( drive, &drives, list ) {
329 if ( drive->drive != ix86->regs.dl )
332 DBG ( "INT 13,%02x (%02x): ", ix86->regs.ah,
335 switch ( ix86->regs.ah ) {
337 status = int13_reset ( drive, ix86 );
339 case INT13_GET_LAST_STATUS:
340 status = int13_get_last_status ( drive, ix86 );
342 case INT13_READ_SECTORS:
343 status = int13_read_sectors ( drive, ix86 );
345 case INT13_WRITE_SECTORS:
346 status = int13_write_sectors ( drive, ix86 );
348 case INT13_GET_PARAMETERS:
349 status = int13_get_parameters ( drive, ix86 );
351 case INT13_EXTENSION_CHECK:
352 status = int13_extension_check ( drive, ix86 );
354 case INT13_EXTENDED_READ:
355 status = int13_extended_read ( drive, ix86 );
357 case INT13_EXTENDED_WRITE:
358 status = int13_extended_write ( drive, ix86 );
360 case INT13_GET_EXTENDED_PARAMETERS:
361 status = int13_get_extended_parameters ( drive, ix86 );
364 DBG ( "Unrecognised INT 13\n" );
365 status = INT13_STATUS_INVALID;
369 /* Store status for INT 13,01 */
370 drive->last_status = status;
371 /* All functions return status via %ah and CF */
372 ix86->regs.ah = status;
375 DBG ( "INT13 failed with status %x\n", status );
377 /* Set OF to indicate to wrapper not to chain this call */
383 * Hook INT 13 handler
386 static void hook_int13 ( void ) {
387 /* Assembly wrapper to call int13(). int13() sets OF if we
388 * should not chain to the previous handler. (The wrapper
389 * clears CF and OF before calling int13()).
391 __asm__ __volatile__ ( ".section \".text16\", \"ax\", @progbits\n\t"
393 "\nint13_wrapper:\n\t"
394 "orb $0, %%al\n\t" /* clear CF and OF */
395 "pushl %0\n\t" /* call int13() */
396 "data32 call prot_call\n\t"
397 "jo 1f\n\t" /* chain if OF not set */
399 "lcall *%%cs:int13_vector\n\t"
401 "call 2f\n\t" /* return with flags intact */
409 hook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
414 * Unhook INT 13 handler
416 static void unhook_int13 ( void ) {
417 unhook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
422 * Register INT 13 emulated drive
424 * @v drive Emulated drive
426 * Registers the drive with the INT 13 emulation subsystem, and hooks
427 * the INT 13 interrupt vector (if not already hooked).
429 * The underlying block device must be valid. A drive number and
430 * geometry will be assigned if left blank.
432 void register_int13_drive ( struct int13_drive *drive ) {
434 unsigned long blocks;
435 unsigned long blocks_per_cyl;
437 /* Give drive a default geometry if none specified */
438 if ( ! drive->heads )
440 if ( ! drive->sectors_per_track )
441 drive->sectors_per_track = 63;
442 if ( ! drive->cylinders ) {
443 /* Avoid attempting a 64-bit divide on a 32-bit system */
444 blocks = ( ( drive->blockdev->blocks <= ULONG_MAX ) ?
445 drive->blockdev->blocks : ULONG_MAX );
446 blocks_per_cyl = ( drive->heads * drive->sectors_per_track );
447 assert ( blocks_per_cyl != 0 );
448 drive->cylinders = ( blocks / blocks_per_cyl );
451 /* Assign drive number if none specified, update BIOS drive count */
452 get_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
453 if ( ! drive->drive )
454 drive->drive = ( num_drives | 0x80 );
455 if ( num_drives <= ( drive->drive & 0x7f ) )
456 num_drives = ( ( drive->drive & 0x7f ) + 1 );
457 put_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
459 DBG ( "Registered INT13 drive %02x with C/H/S geometry %d/%d/%d\n",
460 drive->drive, drive->cylinders, drive->heads,
461 drive->sectors_per_track );
463 /* Hook INT 13 vector if not already hooked */
464 if ( list_empty ( &drives ) )
467 /* Add to list of emulated drives */
468 list_add ( &drive->list, &drives );
472 * Unregister INT 13 emulated drive
474 * @v drive Emulated drive
476 * Unregisters the drive from the INT 13 emulation subsystem. If this
477 * is the last emulated drive, the INT 13 vector is unhooked (if
480 void unregister_int13_drive ( struct int13_drive *drive ) {
481 /* Remove from list of emulated drives */
482 list_del ( &drive->list );
484 DBG ( "Unregistered INT13 drive %02x\n", drive->drive );
486 /* Unhook INT 13 vector if no more drives */
487 if ( list_empty ( &drives ) )
492 * Attempt to boot from an INT 13 drive
494 * @v drive Drive number
495 * @ret rc Return status code
497 * This boots from the specified INT 13 drive by loading the Master
498 * Boot Record to 0000:7c00 and jumping to it. INT 18 is hooked to
499 * capture an attempt by the MBR to boot the next device. (This is
500 * the closest thing to a return path from an MBR).
502 * Note that this function can never return success, by definition.
504 int int13_boot ( unsigned int drive ) {
505 int status, signature;
506 int discard_c, discard_d;
508 DBG ( "Booting from INT 13 drive %02x\n", drive );
510 /* Use INT 13 to read the boot sector */
511 __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
516 "sti\n\t" /* BIOS bugs */
518 "xorl %%eax, %%eax\n\t"
520 "movzwl %%es:0x7dfe, %%ebx\n\t"
522 : "=a" ( status ), "=b" ( signature ),
523 "=c" ( discard_c ), "=d" ( discard_d )
524 : "a" ( 0x0201 ), "b" ( 0x7c00 ),
525 "c" ( 1 ), "d" ( drive ) );
529 /* Check signature is correct */
530 if ( signature != be16_to_cpu ( 0x55aa ) ) {
531 DBG ( "Invalid disk signature %#04x (should be 0x55aa)\n",
532 cpu_to_be16 ( signature ) );
536 /* Hook INTs 18 and 19 to capture failure paths */
537 hook_bios_interrupt ( 0x18, ( unsigned int ) int13_exec_fail,
539 hook_bios_interrupt ( 0x19, ( unsigned int ) int13_exec_fail,
542 /* Boot the loaded sector */
543 __asm__ __volatile__ ( REAL_CODE ( /* Save segment registers */
548 /* Save stack pointer */
549 "movw %%ss, %%ax\n\t"
550 "movw %%ax, %%cs:int13_saved_ss\n\t"
551 "movw %%sp, %%cs:int13_saved_sp\n\t"
552 "ljmp $0, $0x7c00\n\t"
553 "\nint13_saved_ss: .word 0\n\t"
554 "\nint13_saved_sp: .word 0\n\t"
555 "\nint13_exec_fail:\n\t"
556 "movw %%cs:int13_saved_ss, %%ax\n\t"
557 "movw %%ax, %%ss\n\t"
558 "movw %%cs:int13_saved_sp, %%sp\n\t"
563 : "=d" ( discard_d ) : "d" ( drive )
564 : "eax", "ebx", "ecx", "esi", "edi", "ebp" );
566 DBG ( "Booted disk returned via INT 18 or 19\n" );
568 /* Unhook INTs 18 and 19 */
569 unhook_bios_interrupt ( 0x18, ( unsigned int ) int13_exec_fail,
571 unhook_bios_interrupt ( 0x19, ( unsigned int ) int13_exec_fail,