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.

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