You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

int13.c 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  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.
  8. *
  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.
  13. *
  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.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <limits.h>
  21. #include <byteswap.h>
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include <ipxe/list.h>
  25. #include <ipxe/blockdev.h>
  26. #include <ipxe/memmap.h>
  27. #include <realmode.h>
  28. #include <bios.h>
  29. #include <biosint.h>
  30. #include <bootsector.h>
  31. #include <int13.h>
  32. /** @file
  33. *
  34. * INT 13 emulation
  35. *
  36. * This module provides a mechanism for exporting block devices via
  37. * the BIOS INT 13 disk interrupt interface.
  38. *
  39. */
  40. /** Vector for chaining to other INT 13 handlers */
  41. static struct segoff __text16 ( int13_vector );
  42. #define int13_vector __use_text16 ( int13_vector )
  43. /** Assembly wrapper */
  44. extern void int13_wrapper ( void );
  45. /** List of registered emulated drives */
  46. static LIST_HEAD ( drives );
  47. /**
  48. * Number of BIOS drives
  49. *
  50. * Note that this is the number of drives in the system as a whole
  51. * (i.e. a mirror of the counter at 40:75), rather than a count of the
  52. * number of emulated drives.
  53. */
  54. static uint8_t num_drives;
  55. /**
  56. * Update BIOS drive count
  57. */
  58. static void int13_set_num_drives ( void ) {
  59. struct int13_drive *drive;
  60. /* Get current drive count */
  61. get_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
  62. /* Ensure count is large enough to cover all of our emulated drives */
  63. list_for_each_entry ( drive, &drives, list ) {
  64. if ( num_drives <= ( drive->drive & 0x7f ) )
  65. num_drives = ( ( drive->drive & 0x7f ) + 1 );
  66. }
  67. /* Update current drive count */
  68. put_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
  69. }
  70. /**
  71. * Check number of drives
  72. */
  73. static void int13_check_num_drives ( void ) {
  74. uint8_t check_num_drives;
  75. get_real ( check_num_drives, BDA_SEG, BDA_NUM_DRIVES );
  76. if ( check_num_drives != num_drives ) {
  77. int13_set_num_drives();
  78. DBG ( "INT13 fixing up number of drives from %d to %d\n",
  79. check_num_drives, num_drives );
  80. }
  81. }
  82. /**
  83. * INT 13, 00 - Reset disk system
  84. *
  85. * @v drive Emulated drive
  86. * @ret status Status code
  87. */
  88. static int int13_reset ( struct int13_drive *drive __unused,
  89. struct i386_all_regs *ix86 __unused ) {
  90. DBG ( "Reset drive\n" );
  91. return 0;
  92. }
  93. /**
  94. * INT 13, 01 - Get status of last operation
  95. *
  96. * @v drive Emulated drive
  97. * @ret status Status code
  98. */
  99. static int int13_get_last_status ( struct int13_drive *drive,
  100. struct i386_all_regs *ix86 __unused ) {
  101. DBG ( "Get status of last operation\n" );
  102. return drive->last_status;
  103. }
  104. /**
  105. * Read / write sectors
  106. *
  107. * @v drive Emulated drive
  108. * @v al Number of sectors to read or write (must be nonzero)
  109. * @v ch Low bits of cylinder number
  110. * @v cl (bits 7:6) High bits of cylinder number
  111. * @v cl (bits 5:0) Sector number
  112. * @v dh Head number
  113. * @v es:bx Data buffer
  114. * @v io Read / write method
  115. * @ret status Status code
  116. * @ret al Number of sectors read or written
  117. */
  118. static int int13_rw_sectors ( struct int13_drive *drive,
  119. struct i386_all_regs *ix86,
  120. int ( * io ) ( struct block_device *blockdev,
  121. uint64_t block,
  122. unsigned long count,
  123. userptr_t buffer ) ) {
  124. struct block_device *blockdev = drive->blockdev;
  125. unsigned int cylinder, head, sector;
  126. unsigned long lba;
  127. unsigned int count;
  128. userptr_t buffer;
  129. int rc;
  130. /* Validate blocksize */
  131. if ( blockdev->blksize != INT13_BLKSIZE ) {
  132. DBG ( "Invalid blocksize (%zd) for non-extended read/write\n",
  133. blockdev->blksize );
  134. return -INT13_STATUS_INVALID;
  135. }
  136. /* Calculate parameters */
  137. cylinder = ( ( ( ix86->regs.cl & 0xc0 ) << 2 ) | ix86->regs.ch );
  138. assert ( cylinder < drive->cylinders );
  139. head = ix86->regs.dh;
  140. assert ( head < drive->heads );
  141. sector = ( ix86->regs.cl & 0x3f );
  142. assert ( ( sector >= 1 ) && ( sector <= drive->sectors_per_track ) );
  143. lba = ( ( ( ( cylinder * drive->heads ) + head )
  144. * drive->sectors_per_track ) + sector - 1 );
  145. count = ix86->regs.al;
  146. buffer = real_to_user ( ix86->segs.es, ix86->regs.bx );
  147. DBG ( "C/H/S %d/%d/%d = LBA %#lx <-> %04x:%04x (count %d)\n", cylinder,
  148. head, sector, lba, ix86->segs.es, ix86->regs.bx, count );
  149. /* Read from / write to block device */
  150. if ( ( rc = io ( blockdev, lba, count, buffer ) ) != 0 ) {
  151. DBG ( "INT 13 failed: %s\n", strerror ( rc ) );
  152. return -INT13_STATUS_READ_ERROR;
  153. }
  154. return 0;
  155. }
  156. /**
  157. * INT 13, 02 - Read sectors
  158. *
  159. * @v drive Emulated drive
  160. * @v al Number of sectors to read (must be nonzero)
  161. * @v ch Low bits of cylinder number
  162. * @v cl (bits 7:6) High bits of cylinder number
  163. * @v cl (bits 5:0) Sector number
  164. * @v dh Head number
  165. * @v es:bx Data buffer
  166. * @ret status Status code
  167. * @ret al Number of sectors read
  168. */
  169. static int int13_read_sectors ( struct int13_drive *drive,
  170. struct i386_all_regs *ix86 ) {
  171. DBG ( "Read: " );
  172. return int13_rw_sectors ( drive, ix86, drive->blockdev->op->read );
  173. }
  174. /**
  175. * INT 13, 03 - Write sectors
  176. *
  177. * @v drive Emulated drive
  178. * @v al Number of sectors to write (must be nonzero)
  179. * @v ch Low bits of cylinder number
  180. * @v cl (bits 7:6) High bits of cylinder number
  181. * @v cl (bits 5:0) Sector number
  182. * @v dh Head number
  183. * @v es:bx Data buffer
  184. * @ret status Status code
  185. * @ret al Number of sectors written
  186. */
  187. static int int13_write_sectors ( struct int13_drive *drive,
  188. struct i386_all_regs *ix86 ) {
  189. DBG ( "Write: " );
  190. return int13_rw_sectors ( drive, ix86, drive->blockdev->op->write );
  191. }
  192. /**
  193. * INT 13, 08 - Get drive parameters
  194. *
  195. * @v drive Emulated drive
  196. * @ret status Status code
  197. * @ret ch Low bits of maximum cylinder number
  198. * @ret cl (bits 7:6) High bits of maximum cylinder number
  199. * @ret cl (bits 5:0) Maximum sector number
  200. * @ret dh Maximum head number
  201. * @ret dl Number of drives
  202. */
  203. static int int13_get_parameters ( struct int13_drive *drive,
  204. struct i386_all_regs *ix86 ) {
  205. unsigned int max_cylinder = drive->cylinders - 1;
  206. unsigned int max_head = drive->heads - 1;
  207. unsigned int max_sector = drive->sectors_per_track; /* sic */
  208. DBG ( "Get drive parameters\n" );
  209. ix86->regs.ch = ( max_cylinder & 0xff );
  210. ix86->regs.cl = ( ( ( max_cylinder >> 8 ) << 6 ) | max_sector );
  211. ix86->regs.dh = max_head;
  212. get_real ( ix86->regs.dl, BDA_SEG, BDA_NUM_DRIVES );
  213. return 0;
  214. }
  215. /**
  216. * INT 13, 15 - Get disk type
  217. *
  218. * @v drive Emulated drive
  219. * @ret ah Type code
  220. * @ret cx:dx Sector count
  221. * @ret status Status code / disk type
  222. */
  223. static int int13_get_disk_type ( struct int13_drive *drive,
  224. struct i386_all_regs *ix86 ) {
  225. uint32_t blocks;
  226. DBG ( "Get disk type\n" );
  227. blocks = ( ( drive->blockdev->blocks <= 0xffffffffUL ) ?
  228. drive->blockdev->blocks : 0xffffffffUL );
  229. ix86->regs.cx = ( blocks >> 16 );
  230. ix86->regs.dx = ( blocks & 0xffff );
  231. return INT13_DISK_TYPE_HDD;
  232. }
  233. /**
  234. * INT 13, 41 - Extensions installation check
  235. *
  236. * @v drive Emulated drive
  237. * @v bx 0x55aa
  238. * @ret bx 0xaa55
  239. * @ret cx Extensions API support bitmap
  240. * @ret status Status code / API version
  241. */
  242. static int int13_extension_check ( struct int13_drive *drive __unused,
  243. struct i386_all_regs *ix86 ) {
  244. if ( ix86->regs.bx == 0x55aa ) {
  245. DBG ( "INT 13 extensions installation check\n" );
  246. ix86->regs.bx = 0xaa55;
  247. ix86->regs.cx = INT13_EXTENSION_LINEAR;
  248. return INT13_EXTENSION_VER_1_X;
  249. } else {
  250. return -INT13_STATUS_INVALID;
  251. }
  252. }
  253. /**
  254. * Extended read / write
  255. *
  256. * @v drive Emulated drive
  257. * @v ds:si Disk address packet
  258. * @v io Read / write method
  259. * @ret status Status code
  260. */
  261. static int int13_extended_rw ( struct int13_drive *drive,
  262. struct i386_all_regs *ix86,
  263. int ( * io ) ( struct block_device *blockdev,
  264. uint64_t block,
  265. unsigned long count,
  266. userptr_t buffer ) ) {
  267. struct block_device *blockdev = drive->blockdev;
  268. struct int13_disk_address addr;
  269. uint64_t lba;
  270. unsigned long count;
  271. userptr_t buffer;
  272. int rc;
  273. /* Read parameters from disk address structure */
  274. copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si, sizeof ( addr ));
  275. lba = addr.lba;
  276. count = addr.count;
  277. buffer = real_to_user ( addr.buffer.segment, addr.buffer.offset );
  278. DBG ( "LBA %#llx <-> %04x:%04x (count %ld)\n", (unsigned long long)lba,
  279. addr.buffer.segment, addr.buffer.offset, count );
  280. /* Read from / write to block device */
  281. if ( ( rc = io ( blockdev, lba, count, buffer ) ) != 0 ) {
  282. DBG ( "INT 13 failed: %s\n", strerror ( rc ) );
  283. return -INT13_STATUS_READ_ERROR;
  284. }
  285. return 0;
  286. }
  287. /**
  288. * INT 13, 42 - Extended read
  289. *
  290. * @v drive Emulated drive
  291. * @v ds:si Disk address packet
  292. * @ret status Status code
  293. */
  294. static int int13_extended_read ( struct int13_drive *drive,
  295. struct i386_all_regs *ix86 ) {
  296. DBG ( "Extended read: " );
  297. return int13_extended_rw ( drive, ix86, drive->blockdev->op->read );
  298. }
  299. /**
  300. * INT 13, 43 - Extended write
  301. *
  302. * @v drive Emulated drive
  303. * @v ds:si Disk address packet
  304. * @ret status Status code
  305. */
  306. static int int13_extended_write ( struct int13_drive *drive,
  307. struct i386_all_regs *ix86 ) {
  308. DBG ( "Extended write: " );
  309. return int13_extended_rw ( drive, ix86, drive->blockdev->op->write );
  310. }
  311. /**
  312. * INT 13, 48 - Get extended parameters
  313. *
  314. * @v drive Emulated drive
  315. * @v ds:si Drive parameter table
  316. * @ret status Status code
  317. */
  318. static int int13_get_extended_parameters ( struct int13_drive *drive,
  319. struct i386_all_regs *ix86 ) {
  320. struct int13_disk_parameters params = {
  321. .bufsize = sizeof ( params ),
  322. .flags = INT13_FL_DMA_TRANSPARENT,
  323. .cylinders = drive->cylinders,
  324. .heads = drive->heads,
  325. .sectors_per_track = drive->sectors_per_track,
  326. .sectors = drive->blockdev->blocks,
  327. .sector_size = drive->blockdev->blksize,
  328. };
  329. DBG ( "Get extended drive parameters to %04x:%04x\n",
  330. ix86->segs.ds, ix86->regs.si );
  331. copy_to_real ( ix86->segs.ds, ix86->regs.si, &params,
  332. sizeof ( params ) );
  333. return 0;
  334. }
  335. /**
  336. * INT 13 handler
  337. *
  338. */
  339. static __asmcall void int13 ( struct i386_all_regs *ix86 ) {
  340. int command = ix86->regs.ah;
  341. unsigned int bios_drive = ix86->regs.dl;
  342. struct int13_drive *drive;
  343. int status;
  344. /* Check BIOS hasn't killed off our drive */
  345. int13_check_num_drives();
  346. list_for_each_entry ( drive, &drives, list ) {
  347. if ( bios_drive != drive->drive ) {
  348. /* Remap any accesses to this drive's natural number */
  349. if ( bios_drive == drive->natural_drive ) {
  350. DBG ( "INT 13,%04x (%02x) remapped to "
  351. "(%02x)\n", ix86->regs.ax,
  352. bios_drive, drive->drive );
  353. ix86->regs.dl = drive->drive;
  354. return;
  355. }
  356. continue;
  357. }
  358. DBG ( "INT 13,%04x (%02x): ", ix86->regs.ax, drive->drive );
  359. switch ( command ) {
  360. case INT13_RESET:
  361. status = int13_reset ( drive, ix86 );
  362. break;
  363. case INT13_GET_LAST_STATUS:
  364. status = int13_get_last_status ( drive, ix86 );
  365. break;
  366. case INT13_READ_SECTORS:
  367. status = int13_read_sectors ( drive, ix86 );
  368. break;
  369. case INT13_WRITE_SECTORS:
  370. status = int13_write_sectors ( drive, ix86 );
  371. break;
  372. case INT13_GET_PARAMETERS:
  373. status = int13_get_parameters ( drive, ix86 );
  374. break;
  375. case INT13_GET_DISK_TYPE:
  376. status = int13_get_disk_type ( drive, ix86 );
  377. break;
  378. case INT13_EXTENSION_CHECK:
  379. status = int13_extension_check ( drive, ix86 );
  380. break;
  381. case INT13_EXTENDED_READ:
  382. status = int13_extended_read ( drive, ix86 );
  383. break;
  384. case INT13_EXTENDED_WRITE:
  385. status = int13_extended_write ( drive, ix86 );
  386. break;
  387. case INT13_GET_EXTENDED_PARAMETERS:
  388. status = int13_get_extended_parameters ( drive, ix86 );
  389. break;
  390. default:
  391. DBG ( "*** Unrecognised INT 13 ***\n" );
  392. status = -INT13_STATUS_INVALID;
  393. break;
  394. }
  395. /* Store status for INT 13,01 */
  396. drive->last_status = status;
  397. /* Negative status indicates an error */
  398. if ( status < 0 ) {
  399. status = -status;
  400. DBG ( "INT 13 returning failure status %x\n", status );
  401. } else {
  402. ix86->flags &= ~CF;
  403. }
  404. ix86->regs.ah = status;
  405. /* Set OF to indicate to wrapper not to chain this call */
  406. ix86->flags |= OF;
  407. return;
  408. }
  409. }
  410. /**
  411. * Hook INT 13 handler
  412. *
  413. */
  414. static void hook_int13 ( void ) {
  415. /* Assembly wrapper to call int13(). int13() sets OF if we
  416. * should not chain to the previous handler. (The wrapper
  417. * clears CF and OF before calling int13()).
  418. */
  419. __asm__ __volatile__ (
  420. TEXT16_CODE ( "\nint13_wrapper:\n\t"
  421. /* Preserve %ax and %dx for future reference */
  422. "pushw %%bp\n\t"
  423. "movw %%sp, %%bp\n\t"
  424. "pushw %%ax\n\t"
  425. "pushw %%dx\n\t"
  426. /* Clear OF, set CF, call int13() */
  427. "orb $0, %%al\n\t"
  428. "stc\n\t"
  429. "pushl %0\n\t"
  430. "pushw %%cs\n\t"
  431. "call prot_call\n\t"
  432. /* Chain if OF not set */
  433. "jo 1f\n\t"
  434. "pushfw\n\t"
  435. "lcall *%%cs:int13_vector\n\t"
  436. "\n1:\n\t"
  437. /* Overwrite flags for iret */
  438. "pushfw\n\t"
  439. "popw 6(%%bp)\n\t"
  440. /* Fix up %dl:
  441. *
  442. * INT 13,15 : do nothing
  443. * INT 13,08 : load with number of drives
  444. * all others: restore original value
  445. */
  446. "cmpb $0x15, -1(%%bp)\n\t"
  447. "je 2f\n\t"
  448. "movb -4(%%bp), %%dl\n\t"
  449. "cmpb $0x08, -1(%%bp)\n\t"
  450. "jne 2f\n\t"
  451. "pushw %%ds\n\t"
  452. "pushw %1\n\t"
  453. "popw %%ds\n\t"
  454. "movb %c2, %%dl\n\t"
  455. "popw %%ds\n\t"
  456. /* Return */
  457. "\n2:\n\t"
  458. "movw %%bp, %%sp\n\t"
  459. "popw %%bp\n\t"
  460. "iret\n\t" )
  461. : : "i" ( int13 ), "i" ( BDA_SEG ), "i" ( BDA_NUM_DRIVES ) );
  462. hook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
  463. &int13_vector );
  464. }
  465. /**
  466. * Unhook INT 13 handler
  467. */
  468. static void unhook_int13 ( void ) {
  469. unhook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
  470. &int13_vector );
  471. }
  472. /**
  473. * Guess INT 13 drive geometry
  474. *
  475. * @v drive Emulated drive
  476. *
  477. * Guesses the drive geometry by inspecting the partition table.
  478. */
  479. static void guess_int13_geometry ( struct int13_drive *drive ) {
  480. struct master_boot_record mbr;
  481. struct partition_table_entry *partition;
  482. unsigned int guessed_heads = 255;
  483. unsigned int guessed_sectors_per_track = 63;
  484. unsigned long blocks;
  485. unsigned long blocks_per_cyl;
  486. unsigned int i;
  487. /* Don't even try when the blksize is invalid for C/H/S access */
  488. if ( drive->blockdev->blksize != INT13_BLKSIZE )
  489. return;
  490. /* Scan through partition table and modify guesses for heads
  491. * and sectors_per_track if we find any used partitions.
  492. */
  493. if ( drive->blockdev->op->read ( drive->blockdev, 0, 1,
  494. virt_to_user ( &mbr ) ) == 0 ) {
  495. for ( i = 0 ; i < 4 ; i++ ) {
  496. partition = &mbr.partitions[i];
  497. if ( ! partition->type )
  498. continue;
  499. guessed_heads =
  500. ( PART_HEAD ( partition->chs_end ) + 1 );
  501. guessed_sectors_per_track =
  502. PART_SECTOR ( partition->chs_end );
  503. DBG ( "Guessing C/H/S xx/%d/%d based on partition "
  504. "%d\n", guessed_heads,
  505. guessed_sectors_per_track, ( i + 1 ) );
  506. }
  507. } else {
  508. DBG ( "Could not read partition table to guess geometry\n" );
  509. }
  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;
  524. }
  525. }
  526. /**
  527. * Register INT 13 emulated drive
  528. *
  529. * @v drive Emulated drive
  530. *
  531. * Registers the drive with the INT 13 emulation subsystem, and hooks
  532. * the INT 13 interrupt vector (if not already hooked).
  533. *
  534. * The underlying block device must be valid. A drive number and
  535. * geometry will be assigned if left blank.
  536. */
  537. void register_int13_drive ( struct int13_drive *drive ) {
  538. uint8_t num_drives;
  539. /* Give drive a default geometry if none specified */
  540. guess_int13_geometry ( drive );
  541. /* Assign natural drive number */
  542. get_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
  543. drive->natural_drive = ( num_drives | 0x80 );
  544. /* Assign drive number */
  545. if ( ( drive->drive & 0xff ) == 0xff ) {
  546. /* Drive number == -1 => use natural drive number */
  547. drive->drive = drive->natural_drive;
  548. } else {
  549. /* Use specified drive number (+0x80 if necessary) */
  550. drive->drive |= 0x80;
  551. }
  552. DBG ( "Registered INT13 drive %02x (naturally %02x) with C/H/S "
  553. "geometry %d/%d/%d\n", drive->drive, drive->natural_drive,
  554. drive->cylinders, drive->heads, drive->sectors_per_track );
  555. /* Hook INT 13 vector if not already hooked */
  556. if ( list_empty ( &drives ) )
  557. hook_int13();
  558. /* Add to list of emulated drives */
  559. list_add ( &drive->list, &drives );
  560. /* Update BIOS drive count */
  561. int13_set_num_drives();
  562. }
  563. /**
  564. * Unregister INT 13 emulated drive
  565. *
  566. * @v drive Emulated drive
  567. *
  568. * Unregisters the drive from the INT 13 emulation subsystem. If this
  569. * is the last emulated drive, the INT 13 vector is unhooked (if
  570. * possible).
  571. */
  572. void unregister_int13_drive ( struct int13_drive *drive ) {
  573. /* Remove from list of emulated drives */
  574. list_del ( &drive->list );
  575. /* Should adjust BIOS drive count, but it's difficult to do so
  576. * reliably.
  577. */
  578. DBG ( "Unregistered INT13 drive %02x\n", drive->drive );
  579. /* Unhook INT 13 vector if no more drives */
  580. if ( list_empty ( &drives ) )
  581. unhook_int13();
  582. }
  583. /**
  584. * Attempt to boot from an INT 13 drive
  585. *
  586. * @v drive Drive number
  587. * @ret rc Return status code
  588. *
  589. * This boots from the specified INT 13 drive by loading the Master
  590. * Boot Record to 0000:7c00 and jumping to it. INT 18 is hooked to
  591. * capture an attempt by the MBR to boot the next device. (This is
  592. * the closest thing to a return path from an MBR).
  593. *
  594. * Note that this function can never return success, by definition.
  595. */
  596. int int13_boot ( unsigned int drive ) {
  597. struct memory_map memmap;
  598. int status, signature;
  599. int discard_c, discard_d;
  600. int rc;
  601. DBG ( "Booting from INT 13 drive %02x\n", drive );
  602. /* Use INT 13 to read the boot sector */
  603. __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
  604. "pushw $0\n\t"
  605. "popw %%es\n\t"
  606. "stc\n\t"
  607. "sti\n\t"
  608. "int $0x13\n\t"
  609. "sti\n\t" /* BIOS bugs */
  610. "jc 1f\n\t"
  611. "xorl %%eax, %%eax\n\t"
  612. "\n1:\n\t"
  613. "movzwl %%es:0x7dfe, %%ebx\n\t"
  614. "popw %%es\n\t" )
  615. : "=a" ( status ), "=b" ( signature ),
  616. "=c" ( discard_c ), "=d" ( discard_d )
  617. : "a" ( 0x0201 ), "b" ( 0x7c00 ),
  618. "c" ( 1 ), "d" ( drive ) );
  619. if ( status )
  620. return -EIO;
  621. /* Check signature is correct */
  622. if ( signature != be16_to_cpu ( 0x55aa ) ) {
  623. DBG ( "Invalid disk signature %#04x (should be 0x55aa)\n",
  624. cpu_to_be16 ( signature ) );
  625. return -ENOEXEC;
  626. }
  627. /* Dump out memory map prior to boot, if memmap debugging is
  628. * enabled. Not required for program flow, but we have so
  629. * many problems that turn out to be memory-map related that
  630. * it's worth doing.
  631. */
  632. get_memmap ( &memmap );
  633. /* Jump to boot sector */
  634. if ( ( rc = call_bootsector ( 0x0, 0x7c00, drive ) ) != 0 ) {
  635. DBG ( "INT 13 drive %02x boot returned: %s\n",
  636. drive, strerror ( rc ) );
  637. return rc;
  638. }
  639. return -ECANCELED; /* -EIMPOSSIBLE */
  640. }