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 38KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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 <stdlib.h>
  21. #include <limits.h>
  22. #include <byteswap.h>
  23. #include <errno.h>
  24. #include <assert.h>
  25. #include <ipxe/list.h>
  26. #include <ipxe/blockdev.h>
  27. #include <ipxe/io.h>
  28. #include <ipxe/open.h>
  29. #include <ipxe/uri.h>
  30. #include <ipxe/process.h>
  31. #include <ipxe/xfer.h>
  32. #include <ipxe/retry.h>
  33. #include <ipxe/timer.h>
  34. #include <ipxe/acpi.h>
  35. #include <ipxe/sanboot.h>
  36. #include <ipxe/device.h>
  37. #include <ipxe/pci.h>
  38. #include <realmode.h>
  39. #include <bios.h>
  40. #include <biosint.h>
  41. #include <bootsector.h>
  42. #include <int13.h>
  43. /** @file
  44. *
  45. * INT 13 emulation
  46. *
  47. * This module provides a mechanism for exporting block devices via
  48. * the BIOS INT 13 disk interrupt interface.
  49. *
  50. */
  51. /**
  52. * Overall timeout for INT 13 commands (independent of underlying device
  53. *
  54. * Underlying devices should ideally never become totally stuck.
  55. * However, if they do, then the INT 13 mechanism provides no means
  56. * for the caller to cancel the operation, and the machine appears to
  57. * hang. Use an overall timeout for all commands to avoid this
  58. * problem and bounce timeout failures to the caller.
  59. */
  60. #define INT13_COMMAND_TIMEOUT ( 15 * TICKS_PER_SEC )
  61. /** An INT 13 emulated drive */
  62. struct int13_drive {
  63. /** Reference count */
  64. struct refcnt refcnt;
  65. /** List of all registered drives */
  66. struct list_head list;
  67. /** Block device URI */
  68. struct uri *uri;
  69. /** Underlying block device interface */
  70. struct interface block;
  71. /** BIOS in-use drive number (0x80-0xff) */
  72. unsigned int drive;
  73. /** BIOS natural drive number (0x80-0xff)
  74. *
  75. * This is the drive number that would have been assigned by
  76. * 'naturally' appending the drive to the end of the BIOS
  77. * drive list.
  78. *
  79. * If the emulated drive replaces a preexisting drive, this is
  80. * the drive number that the preexisting drive gets remapped
  81. * to.
  82. */
  83. unsigned int natural_drive;
  84. /** Block device capacity */
  85. struct block_device_capacity capacity;
  86. /** Number of cylinders
  87. *
  88. * The cylinder number field in an INT 13 call is ten bits
  89. * wide, giving a maximum of 1024 cylinders. Conventionally,
  90. * when the 7.8GB limit of a CHS address is exceeded, it is
  91. * the number of cylinders that is increased beyond the
  92. * addressable limit.
  93. */
  94. unsigned int cylinders;
  95. /** Number of heads
  96. *
  97. * The head number field in an INT 13 call is eight bits wide,
  98. * giving a maximum of 256 heads. However, apparently all
  99. * versions of MS-DOS up to and including Win95 fail with 256
  100. * heads, so the maximum encountered in practice is 255.
  101. */
  102. unsigned int heads;
  103. /** Number of sectors per track
  104. *
  105. * The sector number field in an INT 13 call is six bits wide,
  106. * giving a maximum of 63 sectors, since sector numbering
  107. * (unlike head and cylinder numbering) starts at 1, not 0.
  108. */
  109. unsigned int sectors_per_track;
  110. /** Underlying device status, if in error */
  111. int block_rc;
  112. /** Status of last operation */
  113. int last_status;
  114. };
  115. /** Vector for chaining to other INT 13 handlers */
  116. static struct segoff __text16 ( int13_vector );
  117. #define int13_vector __use_text16 ( int13_vector )
  118. /** Assembly wrapper */
  119. extern void int13_wrapper ( void );
  120. /** List of registered emulated drives */
  121. static LIST_HEAD ( int13s );
  122. /**
  123. * Number of BIOS drives
  124. *
  125. * Note that this is the number of drives in the system as a whole
  126. * (i.e. a mirror of the counter at 40:75), rather than a count of the
  127. * number of emulated drives.
  128. */
  129. static uint8_t num_drives;
  130. /** An INT 13 command */
  131. struct int13_command {
  132. /** Status */
  133. int rc;
  134. /** INT 13 drive */
  135. struct int13_drive *int13;
  136. /** Underlying block device interface */
  137. struct interface block;
  138. /** Command timeout timer */
  139. struct retry_timer timer;
  140. };
  141. /**
  142. * Record INT 13 drive capacity
  143. *
  144. * @v command INT 13 command
  145. * @v capacity Block device capacity
  146. */
  147. static void int13_command_capacity ( struct int13_command *command,
  148. struct block_device_capacity *capacity ) {
  149. memcpy ( &command->int13->capacity, capacity,
  150. sizeof ( command->int13->capacity ) );
  151. }
  152. /**
  153. * Close INT 13 command
  154. *
  155. * @v command INT 13 command
  156. * @v rc Reason for close
  157. */
  158. static void int13_command_close ( struct int13_command *command, int rc ) {
  159. intf_restart ( &command->block, rc );
  160. stop_timer ( &command->timer );
  161. command->rc = rc;
  162. }
  163. /**
  164. * Handle INT 13 command timer expiry
  165. *
  166. * @v timer Timer
  167. */
  168. static void int13_command_expired ( struct retry_timer *timer,
  169. int over __unused ) {
  170. struct int13_command *command =
  171. container_of ( timer, struct int13_command, timer );
  172. int13_command_close ( command, -ETIMEDOUT );
  173. }
  174. /** INT 13 command interface operations */
  175. static struct interface_operation int13_command_op[] = {
  176. INTF_OP ( intf_close, struct int13_command *, int13_command_close ),
  177. INTF_OP ( block_capacity, struct int13_command *,
  178. int13_command_capacity ),
  179. };
  180. /** INT 13 command interface descriptor */
  181. static struct interface_descriptor int13_command_desc =
  182. INTF_DESC ( struct int13_command, block, int13_command_op );
  183. /**
  184. * Prepare to issue INT 13 command
  185. *
  186. * @v command INT 13 command
  187. * @v int13 Emulated drive
  188. * @ret rc Return status code
  189. */
  190. static int int13_command_start ( struct int13_command *command,
  191. struct int13_drive *int13 ) {
  192. /* Sanity check */
  193. assert ( command->int13 == NULL );
  194. assert ( ! timer_running ( &command->timer ) );
  195. /* Initialise command */
  196. command->rc = -EINPROGRESS;
  197. command->int13 = int13;
  198. start_timer_fixed ( &command->timer, INT13_COMMAND_TIMEOUT );
  199. /* Wait for block control interface to become ready */
  200. while ( ( command->rc == -EINPROGRESS ) &&
  201. ( xfer_window ( &int13->block ) == 0 ) ) {
  202. step();
  203. }
  204. return ( ( command->rc == -EINPROGRESS ) ?
  205. int13->block_rc : command->rc );
  206. }
  207. /**
  208. * Wait for INT 13 command to complete
  209. *
  210. * @v command INT 13 command
  211. * @ret rc Return status code
  212. */
  213. static int int13_command_wait ( struct int13_command *command ) {
  214. /* Sanity check */
  215. assert ( timer_running ( &command->timer ) );
  216. /* Wait for command to complete */
  217. while ( command->rc == -EINPROGRESS )
  218. step();
  219. assert ( ! timer_running ( &command->timer ) );
  220. return command->rc;
  221. }
  222. /**
  223. * Terminate INT 13 command
  224. *
  225. * @v command INT 13 command
  226. */
  227. static void int13_command_stop ( struct int13_command *command ) {
  228. stop_timer ( &command->timer );
  229. command->int13 = NULL;
  230. }
  231. /** The single active INT 13 command */
  232. static struct int13_command int13_command = {
  233. .block = INTF_INIT ( int13_command_desc ),
  234. .timer = TIMER_INIT ( int13_command_expired ),
  235. };
  236. /**
  237. * Read from or write to INT 13 drive
  238. *
  239. * @v int13 Emulated drive
  240. * @v lba Starting logical block address
  241. * @v count Number of logical blocks
  242. * @v buffer Data buffer
  243. * @v block_rw Block read/write method
  244. * @ret rc Return status code
  245. */
  246. static int int13_rw ( struct int13_drive *int13, uint64_t lba,
  247. unsigned int count, userptr_t buffer,
  248. int ( * block_rw ) ( struct interface *control,
  249. struct interface *data,
  250. uint64_t lba, unsigned int count,
  251. userptr_t buffer, size_t len ) ) {
  252. struct int13_command *command = &int13_command;
  253. unsigned int frag_count;
  254. size_t frag_len;
  255. int rc;
  256. while ( count ) {
  257. /* Determine fragment length */
  258. frag_count = count;
  259. if ( frag_count > int13->capacity.max_count )
  260. frag_count = int13->capacity.max_count;
  261. frag_len = ( int13->capacity.blksize * frag_count );
  262. /* Issue command */
  263. if ( ( ( rc = int13_command_start ( command, int13 ) ) != 0 ) ||
  264. ( ( rc = block_rw ( &int13->block, &command->block, lba,
  265. frag_count, buffer,
  266. frag_len ) ) != 0 ) ||
  267. ( ( rc = int13_command_wait ( command ) ) != 0 ) ) {
  268. int13_command_stop ( command );
  269. return rc;
  270. }
  271. int13_command_stop ( command );
  272. /* Move to next fragment */
  273. lba += frag_count;
  274. count -= frag_count;
  275. buffer = userptr_add ( buffer, frag_len );
  276. }
  277. return 0;
  278. }
  279. /**
  280. * Read INT 13 drive capacity
  281. *
  282. * @v int13 Emulated drive
  283. * @ret rc Return status code
  284. */
  285. static int int13_read_capacity ( struct int13_drive *int13 ) {
  286. struct int13_command *command = &int13_command;
  287. int rc;
  288. /* Issue command */
  289. if ( ( ( rc = int13_command_start ( command, int13 ) ) != 0 ) ||
  290. ( ( rc = block_read_capacity ( &int13->block,
  291. &command->block ) ) != 0 ) ||
  292. ( ( rc = int13_command_wait ( command ) ) != 0 ) ) {
  293. int13_command_stop ( command );
  294. return rc;
  295. }
  296. int13_command_stop ( command );
  297. return 0;
  298. }
  299. /**
  300. * Guess INT 13 drive geometry
  301. *
  302. * @v int13 Emulated drive
  303. * @ret rc Return status code
  304. *
  305. * Guesses the drive geometry by inspecting the partition table.
  306. */
  307. static int int13_guess_geometry ( struct int13_drive *int13 ) {
  308. struct master_boot_record mbr;
  309. struct partition_table_entry *partition;
  310. unsigned int guessed_heads = 255;
  311. unsigned int guessed_sectors_per_track = 63;
  312. unsigned long blocks;
  313. unsigned long blocks_per_cyl;
  314. unsigned int i;
  315. int rc;
  316. /* Don't even try when the blksize is invalid for C/H/S access */
  317. if ( int13->capacity.blksize != INT13_BLKSIZE )
  318. return 0;
  319. /* Read partition table */
  320. if ( ( rc = int13_rw ( int13, 0, 1, virt_to_user ( &mbr ),
  321. block_read ) ) != 0 ) {
  322. DBGC ( int13, "INT13 drive %02x could not read partition "
  323. "table to guess geometry: %s\n",
  324. int13->drive, strerror ( rc ) );
  325. return rc;
  326. }
  327. DBGC ( int13, "INT13 drive %02x has signature %08x\n",
  328. int13->drive, mbr.signature );
  329. /* Scan through partition table and modify guesses for heads
  330. * and sectors_per_track if we find any used partitions.
  331. */
  332. for ( i = 0 ; i < 4 ; i++ ) {
  333. partition = &mbr.partitions[i];
  334. if ( ! partition->type )
  335. continue;
  336. guessed_heads = ( PART_HEAD ( partition->chs_end ) + 1 );
  337. guessed_sectors_per_track = PART_SECTOR ( partition->chs_end );
  338. DBGC ( int13, "INT13 drive %02x guessing C/H/S xx/%d/%d based "
  339. "on partition %d\n", int13->drive, guessed_heads,
  340. guessed_sectors_per_track, ( i + 1 ) );
  341. }
  342. /* Apply guesses if no geometry already specified */
  343. if ( ! int13->heads )
  344. int13->heads = guessed_heads;
  345. if ( ! int13->sectors_per_track )
  346. int13->sectors_per_track = guessed_sectors_per_track;
  347. if ( ! int13->cylinders ) {
  348. /* Avoid attempting a 64-bit divide on a 32-bit system */
  349. blocks = ( ( int13->capacity.blocks <= ULONG_MAX ) ?
  350. int13->capacity.blocks : ULONG_MAX );
  351. blocks_per_cyl = ( int13->heads * int13->sectors_per_track );
  352. assert ( blocks_per_cyl != 0 );
  353. int13->cylinders = ( blocks / blocks_per_cyl );
  354. if ( int13->cylinders > 1024 )
  355. int13->cylinders = 1024;
  356. }
  357. return 0;
  358. }
  359. /**
  360. * Open (or reopen) INT 13 emulated drive underlying block device
  361. *
  362. * @v int13 Emulated drive
  363. * @ret rc Return status code
  364. */
  365. static int int13_reopen_block ( struct int13_drive *int13 ) {
  366. int rc;
  367. /* Close any existing block device */
  368. intf_restart ( &int13->block, -ECONNRESET );
  369. /* Open block device */
  370. if ( ( rc = xfer_open_uri ( &int13->block, int13->uri ) ) != 0 ) {
  371. DBGC ( int13, "INT13 drive %02x could not reopen block "
  372. "device: %s\n", int13->drive, strerror ( rc ) );
  373. int13->block_rc = rc;
  374. return rc;
  375. }
  376. /* Clear block device error status */
  377. int13->block_rc = 0;
  378. /* Read device capacity */
  379. if ( ( rc = int13_read_capacity ( int13 ) ) != 0 )
  380. return rc;
  381. return 0;
  382. }
  383. /**
  384. * Update BIOS drive count
  385. */
  386. static void int13_set_num_drives ( void ) {
  387. struct int13_drive *int13;
  388. /* Get current drive count */
  389. get_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
  390. /* Ensure count is large enough to cover all of our emulated drives */
  391. list_for_each_entry ( int13, &int13s, list ) {
  392. if ( num_drives <= ( int13->drive & 0x7f ) )
  393. num_drives = ( ( int13->drive & 0x7f ) + 1 );
  394. }
  395. /* Update current drive count */
  396. put_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
  397. }
  398. /**
  399. * Check number of drives
  400. */
  401. static void int13_check_num_drives ( void ) {
  402. uint8_t check_num_drives;
  403. get_real ( check_num_drives, BDA_SEG, BDA_NUM_DRIVES );
  404. if ( check_num_drives != num_drives ) {
  405. int13_set_num_drives();
  406. DBG ( "INT13 fixing up number of drives from %d to %d\n",
  407. check_num_drives, num_drives );
  408. }
  409. }
  410. /**
  411. * INT 13, 00 - Reset disk system
  412. *
  413. * @v int13 Emulated drive
  414. * @ret status Status code
  415. */
  416. static int int13_reset ( struct int13_drive *int13,
  417. struct i386_all_regs *ix86 __unused ) {
  418. int rc;
  419. DBGC2 ( int13, "Reset drive\n" );
  420. /* Reopen underlying block device */
  421. if ( ( rc = int13_reopen_block ( int13 ) ) != 0 )
  422. return -INT13_STATUS_RESET_FAILED;
  423. return 0;
  424. }
  425. /**
  426. * INT 13, 01 - Get status of last operation
  427. *
  428. * @v int13 Emulated drive
  429. * @ret status Status code
  430. */
  431. static int int13_get_last_status ( struct int13_drive *int13,
  432. struct i386_all_regs *ix86 __unused ) {
  433. DBGC2 ( int13, "Get status of last operation\n" );
  434. return int13->last_status;
  435. }
  436. /**
  437. * Read / write sectors
  438. *
  439. * @v int13 Emulated drive
  440. * @v al Number of sectors to read or write (must be nonzero)
  441. * @v ch Low bits of cylinder number
  442. * @v cl (bits 7:6) High bits of cylinder number
  443. * @v cl (bits 5:0) Sector number
  444. * @v dh Head number
  445. * @v es:bx Data buffer
  446. * @v block_rw Block read/write method
  447. * @ret status Status code
  448. * @ret al Number of sectors read or written
  449. */
  450. static int int13_rw_sectors ( struct int13_drive *int13,
  451. struct i386_all_regs *ix86,
  452. int ( * block_rw ) ( struct interface *control,
  453. struct interface *data,
  454. uint64_t lba,
  455. unsigned int count,
  456. userptr_t buffer,
  457. size_t len ) ) {
  458. unsigned int cylinder, head, sector;
  459. unsigned long lba;
  460. unsigned int count;
  461. userptr_t buffer;
  462. int rc;
  463. /* Validate blocksize */
  464. if ( int13->capacity.blksize != INT13_BLKSIZE ) {
  465. DBGC ( int13, "\nINT 13 drive %02x invalid blocksize (%zd) "
  466. "for non-extended read/write\n",
  467. int13->drive, int13->capacity.blksize );
  468. return -INT13_STATUS_INVALID;
  469. }
  470. /* Calculate parameters */
  471. cylinder = ( ( ( ix86->regs.cl & 0xc0 ) << 2 ) | ix86->regs.ch );
  472. assert ( cylinder < int13->cylinders );
  473. head = ix86->regs.dh;
  474. assert ( head < int13->heads );
  475. sector = ( ix86->regs.cl & 0x3f );
  476. assert ( ( sector >= 1 ) && ( sector <= int13->sectors_per_track ) );
  477. lba = ( ( ( ( cylinder * int13->heads ) + head )
  478. * int13->sectors_per_track ) + sector - 1 );
  479. count = ix86->regs.al;
  480. buffer = real_to_user ( ix86->segs.es, ix86->regs.bx );
  481. DBGC2 ( int13, "C/H/S %d/%d/%d = LBA %08lx <-> %04x:%04x (count %d)\n",
  482. cylinder, head, sector, lba, ix86->segs.es, ix86->regs.bx,
  483. count );
  484. /* Read from / write to block device */
  485. if ( ( rc = int13_rw ( int13, lba, count, buffer, block_rw ) ) != 0 ) {
  486. DBGC ( int13, "INT13 drive %02x I/O failed: %s\n",
  487. int13->drive, strerror ( rc ) );
  488. return -INT13_STATUS_READ_ERROR;
  489. }
  490. return 0;
  491. }
  492. /**
  493. * INT 13, 02 - Read sectors
  494. *
  495. * @v int13 Emulated drive
  496. * @v al Number of sectors to read (must be nonzero)
  497. * @v ch Low bits of cylinder number
  498. * @v cl (bits 7:6) High bits of cylinder number
  499. * @v cl (bits 5:0) Sector number
  500. * @v dh Head number
  501. * @v es:bx Data buffer
  502. * @ret status Status code
  503. * @ret al Number of sectors read
  504. */
  505. static int int13_read_sectors ( struct int13_drive *int13,
  506. struct i386_all_regs *ix86 ) {
  507. DBGC2 ( int13, "Read: " );
  508. return int13_rw_sectors ( int13, ix86, block_read );
  509. }
  510. /**
  511. * INT 13, 03 - Write sectors
  512. *
  513. * @v int13 Emulated drive
  514. * @v al Number of sectors to write (must be nonzero)
  515. * @v ch Low bits of cylinder number
  516. * @v cl (bits 7:6) High bits of cylinder number
  517. * @v cl (bits 5:0) Sector number
  518. * @v dh Head number
  519. * @v es:bx Data buffer
  520. * @ret status Status code
  521. * @ret al Number of sectors written
  522. */
  523. static int int13_write_sectors ( struct int13_drive *int13,
  524. struct i386_all_regs *ix86 ) {
  525. DBGC2 ( int13, "Write: " );
  526. return int13_rw_sectors ( int13, ix86, block_write );
  527. }
  528. /**
  529. * INT 13, 08 - Get drive parameters
  530. *
  531. * @v int13 Emulated drive
  532. * @ret status Status code
  533. * @ret ch Low bits of maximum cylinder number
  534. * @ret cl (bits 7:6) High bits of maximum cylinder number
  535. * @ret cl (bits 5:0) Maximum sector number
  536. * @ret dh Maximum head number
  537. * @ret dl Number of drives
  538. */
  539. static int int13_get_parameters ( struct int13_drive *int13,
  540. struct i386_all_regs *ix86 ) {
  541. unsigned int max_cylinder = int13->cylinders - 1;
  542. unsigned int max_head = int13->heads - 1;
  543. unsigned int max_sector = int13->sectors_per_track; /* sic */
  544. DBGC2 ( int13, "Get drive parameters\n" );
  545. ix86->regs.ch = ( max_cylinder & 0xff );
  546. ix86->regs.cl = ( ( ( max_cylinder >> 8 ) << 6 ) | max_sector );
  547. ix86->regs.dh = max_head;
  548. get_real ( ix86->regs.dl, BDA_SEG, BDA_NUM_DRIVES );
  549. return 0;
  550. }
  551. /**
  552. * INT 13, 15 - Get disk type
  553. *
  554. * @v int13 Emulated drive
  555. * @ret ah Type code
  556. * @ret cx:dx Sector count
  557. * @ret status Status code / disk type
  558. */
  559. static int int13_get_disk_type ( struct int13_drive *int13,
  560. struct i386_all_regs *ix86 ) {
  561. uint32_t blocks;
  562. DBGC2 ( int13, "Get disk type\n" );
  563. blocks = ( ( int13->capacity.blocks <= 0xffffffffUL ) ?
  564. int13->capacity.blocks : 0xffffffffUL );
  565. ix86->regs.cx = ( blocks >> 16 );
  566. ix86->regs.dx = ( blocks & 0xffff );
  567. return INT13_DISK_TYPE_HDD;
  568. }
  569. /**
  570. * INT 13, 41 - Extensions installation check
  571. *
  572. * @v int13 Emulated drive
  573. * @v bx 0x55aa
  574. * @ret bx 0xaa55
  575. * @ret cx Extensions API support bitmap
  576. * @ret status Status code / API version
  577. */
  578. static int int13_extension_check ( struct int13_drive *int13 __unused,
  579. struct i386_all_regs *ix86 ) {
  580. if ( ix86->regs.bx == 0x55aa ) {
  581. DBGC2 ( int13, "INT13 extensions installation check\n" );
  582. ix86->regs.bx = 0xaa55;
  583. ix86->regs.cx = ( INT13_EXTENSION_LINEAR |
  584. INT13_EXTENSION_EDD |
  585. INT13_EXTENSION_64BIT );
  586. return INT13_EXTENSION_VER_3_0;
  587. } else {
  588. return -INT13_STATUS_INVALID;
  589. }
  590. }
  591. /**
  592. * Extended read / write
  593. *
  594. * @v int13 Emulated drive
  595. * @v ds:si Disk address packet
  596. * @v block_rw Block read/write method
  597. * @ret status Status code
  598. */
  599. static int int13_extended_rw ( struct int13_drive *int13,
  600. struct i386_all_regs *ix86,
  601. int ( * block_rw ) ( struct interface *control,
  602. struct interface *data,
  603. uint64_t lba,
  604. unsigned int count,
  605. userptr_t buffer,
  606. size_t len ) ) {
  607. struct int13_disk_address addr;
  608. uint8_t bufsize;
  609. uint64_t lba;
  610. unsigned long count;
  611. userptr_t buffer;
  612. int rc;
  613. /* Get buffer size */
  614. get_real ( bufsize, ix86->segs.ds,
  615. ( ix86->regs.si + offsetof ( typeof ( addr ), bufsize ) ) );
  616. if ( bufsize < offsetof ( typeof ( addr ), buffer_phys ) ) {
  617. DBGC2 ( int13, "<invalid buffer size %#02x\n>\n", bufsize );
  618. return -INT13_STATUS_INVALID;
  619. }
  620. /* Read parameters from disk address structure */
  621. memset ( &addr, 0, sizeof ( addr ) );
  622. copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si, bufsize );
  623. lba = addr.lba;
  624. DBGC2 ( int13, "LBA %08llx <-> ", ( ( unsigned long long ) lba ) );
  625. if ( ( addr.count == 0xff ) ||
  626. ( ( addr.buffer.segment == 0xffff ) &&
  627. ( addr.buffer.offset == 0xffff ) ) ) {
  628. buffer = phys_to_user ( addr.buffer_phys );
  629. DBGC2 ( int13, "%08llx",
  630. ( ( unsigned long long ) addr.buffer_phys ) );
  631. } else {
  632. buffer = real_to_user ( addr.buffer.segment,
  633. addr.buffer.offset );
  634. DBGC2 ( int13, "%04x:%04x", addr.buffer.segment,
  635. addr.buffer.offset );
  636. }
  637. if ( addr.count <= 0x7f ) {
  638. count = addr.count;
  639. } else if ( addr.count == 0xff ) {
  640. count = addr.long_count;
  641. } else {
  642. DBGC2 ( int13, " <invalid count %#02x>\n", addr.count );
  643. return -INT13_STATUS_INVALID;
  644. }
  645. DBGC2 ( int13, " (count %ld)\n", count );
  646. /* Read from / write to block device */
  647. if ( ( rc = int13_rw ( int13, lba, count, buffer, block_rw ) ) != 0 ) {
  648. DBGC ( int13, "INT13 drive %02x extended I/O failed: %s\n",
  649. int13->drive, strerror ( rc ) );
  650. /* Record that no blocks were transferred successfully */
  651. addr.count = 0;
  652. put_real ( addr.count, ix86->segs.ds,
  653. ( ix86->regs.si +
  654. offsetof ( typeof ( addr ), count ) ) );
  655. return -INT13_STATUS_READ_ERROR;
  656. }
  657. return 0;
  658. }
  659. /**
  660. * INT 13, 42 - Extended read
  661. *
  662. * @v int13 Emulated drive
  663. * @v ds:si Disk address packet
  664. * @ret status Status code
  665. */
  666. static int int13_extended_read ( struct int13_drive *int13,
  667. struct i386_all_regs *ix86 ) {
  668. DBGC2 ( int13, "Extended read: " );
  669. return int13_extended_rw ( int13, ix86, block_read );
  670. }
  671. /**
  672. * INT 13, 43 - Extended write
  673. *
  674. * @v int13 Emulated drive
  675. * @v ds:si Disk address packet
  676. * @ret status Status code
  677. */
  678. static int int13_extended_write ( struct int13_drive *int13,
  679. struct i386_all_regs *ix86 ) {
  680. DBGC2 ( int13, "Extended write: " );
  681. return int13_extended_rw ( int13, ix86, block_write );
  682. }
  683. /**
  684. * INT 13, 44 - Verify sectors
  685. *
  686. * @v int13 Emulated drive
  687. * @v ds:si Disk address packet
  688. * @ret status Status code
  689. */
  690. static int int13_extended_verify ( struct int13_drive *int13,
  691. struct i386_all_regs *ix86 ) {
  692. struct int13_disk_address addr;
  693. uint64_t lba;
  694. unsigned long count;
  695. /* Read parameters from disk address structure */
  696. if ( DBG_EXTRA ) {
  697. copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si,
  698. sizeof ( addr ));
  699. lba = addr.lba;
  700. count = addr.count;
  701. DBGC2 ( int13, "Verify: LBA %08llx (count %ld)\n",
  702. ( ( unsigned long long ) lba ), count );
  703. }
  704. /* We have no mechanism for verifying sectors */
  705. return -INT13_STATUS_INVALID;
  706. }
  707. /**
  708. * INT 13, 44 - Extended seek
  709. *
  710. * @v int13 Emulated drive
  711. * @v ds:si Disk address packet
  712. * @ret status Status code
  713. */
  714. int int13_extended_seek ( struct int13_drive *int13,
  715. struct i386_all_regs *ix86 ) {
  716. struct int13_disk_address addr;
  717. uint64_t lba;
  718. unsigned long count;
  719. /* Read parameters from disk address structure */
  720. if ( DBG_EXTRA ) {
  721. copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si,
  722. sizeof ( addr ));
  723. lba = addr.lba;
  724. count = addr.count;
  725. DBGC2 ( int13, "Seek: LBA %08llx (count %ld)\n",
  726. ( ( unsigned long long ) lba ), count );
  727. }
  728. /* Ignore and return success */
  729. return 0;
  730. }
  731. /**
  732. * Build device path information
  733. *
  734. * @v int13 Emulated drive
  735. * @v dpi Device path information
  736. * @ret rc Return status code
  737. */
  738. static int int13_device_path_info ( struct int13_drive *int13,
  739. struct edd_device_path_information *dpi ) {
  740. struct device *device;
  741. struct device_description *desc;
  742. unsigned int i;
  743. uint8_t sum = 0;
  744. int rc;
  745. /* Get underlying hardware device */
  746. device = identify_device ( &int13->block );
  747. if ( ! device ) {
  748. DBGC ( int13, "INT13 drive %02x cannot identify hardware "
  749. "device\n", int13->drive );
  750. return -ENODEV;
  751. }
  752. /* Fill in bus type and interface path */
  753. desc = &device->desc;
  754. switch ( desc->bus_type ) {
  755. case BUS_TYPE_PCI:
  756. dpi->host_bus_type.type = EDD_BUS_TYPE_PCI;
  757. dpi->interface_path.pci.bus = PCI_BUS ( desc->location );
  758. dpi->interface_path.pci.slot = PCI_SLOT ( desc->location );
  759. dpi->interface_path.pci.function = PCI_FUNC ( desc->location );
  760. dpi->interface_path.pci.channel = 0xff; /* unused */
  761. break;
  762. default:
  763. DBGC ( int13, "INT13 drive %02x unrecognised bus type %d\n",
  764. int13->drive, desc->bus_type );
  765. return -ENOTSUP;
  766. }
  767. /* Get EDD block device description */
  768. if ( ( rc = edd_describe ( &int13->block, &dpi->interface_type,
  769. &dpi->device_path ) ) != 0 ) {
  770. DBGC ( int13, "INT13 drive %02x cannot identify block device: "
  771. "%s\n", int13->drive, strerror ( rc ) );
  772. return rc;
  773. }
  774. /* Fill in common fields and fix checksum */
  775. dpi->key = EDD_DEVICE_PATH_INFO_KEY;
  776. dpi->len = sizeof ( *dpi );
  777. for ( i = 0 ; i < sizeof ( *dpi ) ; i++ )
  778. sum += *( ( ( uint8_t * ) dpi ) + i );
  779. dpi->checksum -= sum;
  780. return 0;
  781. }
  782. /**
  783. * INT 13, 48 - Get extended parameters
  784. *
  785. * @v int13 Emulated drive
  786. * @v ds:si Drive parameter table
  787. * @ret status Status code
  788. */
  789. static int int13_get_extended_parameters ( struct int13_drive *int13,
  790. struct i386_all_regs *ix86 ) {
  791. struct int13_disk_parameters params;
  792. struct segoff address;
  793. size_t len = sizeof ( params );
  794. uint16_t bufsize;
  795. int rc;
  796. /* Get buffer size */
  797. get_real ( bufsize, ix86->segs.ds,
  798. ( ix86->regs.si + offsetof ( typeof ( params ), bufsize )));
  799. DBGC2 ( int13, "Get extended drive parameters to %04x:%04x+%02x\n",
  800. ix86->segs.ds, ix86->regs.si, bufsize );
  801. /* Build drive parameters */
  802. memset ( &params, 0, sizeof ( params ) );
  803. params.flags = INT13_FL_DMA_TRANSPARENT;
  804. if ( ( int13->cylinders < 1024 ) &&
  805. ( int13->capacity.blocks <= INT13_MAX_CHS_SECTORS ) ) {
  806. params.flags |= INT13_FL_CHS_VALID;
  807. }
  808. params.cylinders = int13->cylinders;
  809. params.heads = int13->heads;
  810. params.sectors_per_track = int13->sectors_per_track;
  811. params.sectors = int13->capacity.blocks;
  812. params.sector_size = int13->capacity.blksize;
  813. memset ( &params.dpte, 0xff, sizeof ( params.dpte ) );
  814. if ( ( rc = int13_device_path_info ( int13, &params.dpi ) ) != 0 ) {
  815. DBGC ( int13, "INT13 drive %02x could not provide device "
  816. "path information: %s\n",
  817. int13->drive, strerror ( rc ) );
  818. len = offsetof ( typeof ( params ), dpi );
  819. }
  820. /* Calculate returned "buffer size" (which will be less than
  821. * the length actually copied if device path information is
  822. * present).
  823. */
  824. if ( bufsize < offsetof ( typeof ( params ), dpte ) )
  825. return -INT13_STATUS_INVALID;
  826. if ( bufsize < offsetof ( typeof ( params ), dpi ) ) {
  827. params.bufsize = offsetof ( typeof ( params ), dpte );
  828. } else {
  829. params.bufsize = offsetof ( typeof ( params ), dpi );
  830. }
  831. DBGC ( int13, "INT 13 drive %02x described using extended "
  832. "parameters:\n", int13->drive );
  833. address.segment = ix86->segs.ds;
  834. address.offset = ix86->regs.si;
  835. DBGC_HDA ( int13, address, &params, len );
  836. /* Return drive parameters */
  837. if ( len > bufsize )
  838. len = bufsize;
  839. copy_to_real ( ix86->segs.ds, ix86->regs.si, &params, len );
  840. return 0;
  841. }
  842. /**
  843. * INT 13 handler
  844. *
  845. */
  846. static __asmcall void int13 ( struct i386_all_regs *ix86 ) {
  847. int command = ix86->regs.ah;
  848. unsigned int bios_drive = ix86->regs.dl;
  849. struct int13_drive *int13;
  850. int status;
  851. /* Check BIOS hasn't killed off our drive */
  852. int13_check_num_drives();
  853. list_for_each_entry ( int13, &int13s, list ) {
  854. if ( bios_drive != int13->drive ) {
  855. /* Remap any accesses to this drive's natural number */
  856. if ( bios_drive == int13->natural_drive ) {
  857. DBGC2 ( int13, "INT13,%02x (%02x) remapped to "
  858. "(%02x)\n", ix86->regs.ah,
  859. bios_drive, int13->drive );
  860. ix86->regs.dl = int13->drive;
  861. return;
  862. }
  863. continue;
  864. }
  865. DBGC2 ( int13, "INT13,%02x (%02x): ",
  866. ix86->regs.ah, int13->drive );
  867. switch ( command ) {
  868. case INT13_RESET:
  869. status = int13_reset ( int13, ix86 );
  870. break;
  871. case INT13_GET_LAST_STATUS:
  872. status = int13_get_last_status ( int13, ix86 );
  873. break;
  874. case INT13_READ_SECTORS:
  875. status = int13_read_sectors ( int13, ix86 );
  876. break;
  877. case INT13_WRITE_SECTORS:
  878. status = int13_write_sectors ( int13, ix86 );
  879. break;
  880. case INT13_GET_PARAMETERS:
  881. status = int13_get_parameters ( int13, ix86 );
  882. break;
  883. case INT13_GET_DISK_TYPE:
  884. status = int13_get_disk_type ( int13, ix86 );
  885. break;
  886. case INT13_EXTENSION_CHECK:
  887. status = int13_extension_check ( int13, ix86 );
  888. break;
  889. case INT13_EXTENDED_READ:
  890. status = int13_extended_read ( int13, ix86 );
  891. break;
  892. case INT13_EXTENDED_WRITE:
  893. status = int13_extended_write ( int13, ix86 );
  894. break;
  895. case INT13_EXTENDED_VERIFY:
  896. status = int13_extended_verify ( int13, ix86 );
  897. break;
  898. case INT13_EXTENDED_SEEK:
  899. status = int13_extended_seek ( int13, ix86 );
  900. break;
  901. case INT13_GET_EXTENDED_PARAMETERS:
  902. status = int13_get_extended_parameters ( int13, ix86 );
  903. break;
  904. default:
  905. DBGC2 ( int13, "*** Unrecognised INT13 ***\n" );
  906. status = -INT13_STATUS_INVALID;
  907. break;
  908. }
  909. /* Store status for INT 13,01 */
  910. int13->last_status = status;
  911. /* Negative status indicates an error */
  912. if ( status < 0 ) {
  913. status = -status;
  914. DBGC ( int13, "INT13,%02x (%02x) failed with status "
  915. "%02x\n", ix86->regs.ah, int13->drive, status );
  916. } else {
  917. ix86->flags &= ~CF;
  918. }
  919. ix86->regs.ah = status;
  920. /* Set OF to indicate to wrapper not to chain this call */
  921. ix86->flags |= OF;
  922. return;
  923. }
  924. }
  925. /**
  926. * Hook INT 13 handler
  927. *
  928. */
  929. static void int13_hook_vector ( void ) {
  930. /* Assembly wrapper to call int13(). int13() sets OF if we
  931. * should not chain to the previous handler. (The wrapper
  932. * clears CF and OF before calling int13()).
  933. */
  934. __asm__ __volatile__ (
  935. TEXT16_CODE ( "\nint13_wrapper:\n\t"
  936. /* Preserve %ax and %dx for future reference */
  937. "pushw %%bp\n\t"
  938. "movw %%sp, %%bp\n\t"
  939. "pushw %%ax\n\t"
  940. "pushw %%dx\n\t"
  941. /* Clear OF, set CF, call int13() */
  942. "orb $0, %%al\n\t"
  943. "stc\n\t"
  944. "pushl %0\n\t"
  945. "pushw %%cs\n\t"
  946. "call prot_call\n\t"
  947. /* Chain if OF not set */
  948. "jo 1f\n\t"
  949. "pushfw\n\t"
  950. "lcall *%%cs:int13_vector\n\t"
  951. "\n1:\n\t"
  952. /* Overwrite flags for iret */
  953. "pushfw\n\t"
  954. "popw 6(%%bp)\n\t"
  955. /* Fix up %dl:
  956. *
  957. * INT 13,15 : do nothing
  958. * INT 13,08 : load with number of drives
  959. * all others: restore original value
  960. */
  961. "cmpb $0x15, -1(%%bp)\n\t"
  962. "je 2f\n\t"
  963. "movb -4(%%bp), %%dl\n\t"
  964. "cmpb $0x08, -1(%%bp)\n\t"
  965. "jne 2f\n\t"
  966. "pushw %%ds\n\t"
  967. "pushw %1\n\t"
  968. "popw %%ds\n\t"
  969. "movb %c2, %%dl\n\t"
  970. "popw %%ds\n\t"
  971. /* Return */
  972. "\n2:\n\t"
  973. "movw %%bp, %%sp\n\t"
  974. "popw %%bp\n\t"
  975. "iret\n\t" )
  976. : : "i" ( int13 ), "i" ( BDA_SEG ), "i" ( BDA_NUM_DRIVES ) );
  977. hook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
  978. &int13_vector );
  979. }
  980. /**
  981. * Unhook INT 13 handler
  982. */
  983. static void int13_unhook_vector ( void ) {
  984. unhook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
  985. &int13_vector );
  986. }
  987. /**
  988. * Handle INT 13 emulated drive underlying block device closing
  989. *
  990. * @v int13 Emulated drive
  991. * @v rc Reason for close
  992. */
  993. static void int13_block_close ( struct int13_drive *int13, int rc ) {
  994. /* Any closing is an error from our point of view */
  995. if ( rc == 0 )
  996. rc = -ENOTCONN;
  997. DBGC ( int13, "INT13 drive %02x went away: %s\n",
  998. int13->drive, strerror ( rc ) );
  999. /* Record block device error code */
  1000. int13->block_rc = rc;
  1001. /* Shut down interfaces */
  1002. intf_restart ( &int13->block, rc );
  1003. /* Further INT 13 calls will fail immediately. The caller may
  1004. * use INT 13,00 to reset the drive.
  1005. */
  1006. }
  1007. /** INT 13 drive interface operations */
  1008. static struct interface_operation int13_block_op[] = {
  1009. INTF_OP ( intf_close, struct int13_drive *, int13_block_close ),
  1010. };
  1011. /** INT 13 drive interface descriptor */
  1012. static struct interface_descriptor int13_block_desc =
  1013. INTF_DESC ( struct int13_drive, block, int13_block_op );
  1014. /**
  1015. * Free INT 13 emulated drive
  1016. *
  1017. * @v refcnt Reference count
  1018. */
  1019. static void int13_free ( struct refcnt *refcnt ) {
  1020. struct int13_drive *int13 =
  1021. container_of ( refcnt, struct int13_drive, refcnt );
  1022. uri_put ( int13->uri );
  1023. free ( int13 );
  1024. }
  1025. /**
  1026. * Hook INT 13 emulated drive
  1027. *
  1028. * @v uri URI
  1029. * @v drive Requested drive number
  1030. * @ret drive Assigned drive number, or negative error
  1031. *
  1032. * Registers the drive with the INT 13 emulation subsystem, and hooks
  1033. * the INT 13 interrupt vector (if not already hooked).
  1034. */
  1035. static int int13_hook ( struct uri *uri, unsigned int drive ) {
  1036. struct int13_drive *int13;
  1037. uint8_t num_drives;
  1038. unsigned int natural_drive;
  1039. int rc;
  1040. /* Calculate drive number */
  1041. get_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
  1042. natural_drive = ( num_drives | 0x80 );
  1043. if ( drive == INT13_USE_NATURAL_DRIVE )
  1044. drive = natural_drive;
  1045. drive |= 0x80;
  1046. /* Check that drive number is not in use */
  1047. list_for_each_entry ( int13, &int13s, list ) {
  1048. if ( int13->drive == drive ) {
  1049. rc = -EADDRINUSE;
  1050. goto err_in_use;
  1051. }
  1052. }
  1053. /* Allocate and initialise structure */
  1054. int13 = zalloc ( sizeof ( *int13 ) );
  1055. if ( ! int13 ) {
  1056. rc = -ENOMEM;
  1057. goto err_zalloc;
  1058. }
  1059. ref_init ( &int13->refcnt, int13_free );
  1060. intf_init ( &int13->block, &int13_block_desc, &int13->refcnt );
  1061. int13->uri = uri_get ( uri );
  1062. int13->drive = drive;
  1063. int13->natural_drive = natural_drive;
  1064. /* Open block device interface */
  1065. if ( ( rc = int13_reopen_block ( int13 ) ) != 0 )
  1066. goto err_reopen_block;
  1067. /* Give drive a default geometry */
  1068. if ( ( rc = int13_guess_geometry ( int13 ) ) != 0 )
  1069. goto err_guess_geometry;
  1070. DBGC ( int13, "INT13 drive %02x (naturally %02x) registered with C/H/S "
  1071. "geometry %d/%d/%d\n", int13->drive, int13->natural_drive,
  1072. int13->cylinders, int13->heads, int13->sectors_per_track );
  1073. /* Hook INT 13 vector if not already hooked */
  1074. if ( list_empty ( &int13s ) )
  1075. int13_hook_vector();
  1076. /* Add to list of emulated drives */
  1077. list_add ( &int13->list, &int13s );
  1078. /* Update BIOS drive count */
  1079. int13_set_num_drives();
  1080. return int13->drive;
  1081. err_guess_geometry:
  1082. err_reopen_block:
  1083. intf_shutdown ( &int13->block, rc );
  1084. ref_put ( &int13->refcnt );
  1085. err_zalloc:
  1086. err_in_use:
  1087. return rc;
  1088. }
  1089. /**
  1090. * Find INT 13 emulated drive by drive number
  1091. *
  1092. * @v drive Drive number
  1093. * @ret int13 Emulated drive, or NULL
  1094. */
  1095. static struct int13_drive * int13_find ( unsigned int drive ) {
  1096. struct int13_drive *int13;
  1097. list_for_each_entry ( int13, &int13s, list ) {
  1098. if ( int13->drive == drive )
  1099. return int13;
  1100. }
  1101. return NULL;
  1102. }
  1103. /**
  1104. * Unhook INT 13 emulated drive
  1105. *
  1106. * @v drive Drive number
  1107. *
  1108. * Unregisters the drive from the INT 13 emulation subsystem. If this
  1109. * is the last emulated drive, the INT 13 vector is unhooked (if
  1110. * possible).
  1111. */
  1112. static void int13_unhook ( unsigned int drive ) {
  1113. struct int13_drive *int13;
  1114. /* Find drive */
  1115. int13 = int13_find ( drive );
  1116. if ( ! int13 ) {
  1117. DBG ( "INT13 cannot find emulated drive %02x\n", drive );
  1118. return;
  1119. }
  1120. /* Shut down interfaces */
  1121. intf_shutdown ( &int13->block, 0 );
  1122. /* Remove from list of emulated drives */
  1123. list_del ( &int13->list );
  1124. /* Should adjust BIOS drive count, but it's difficult
  1125. * to do so reliably.
  1126. */
  1127. DBGC ( int13, "INT13 drive %02x unregsitered\n", int13->drive );
  1128. /* Unhook INT 13 vector if no more drives */
  1129. if ( list_empty ( &int13s ) )
  1130. int13_unhook_vector();
  1131. /* Drop list's reference to drive */
  1132. ref_put ( &int13->refcnt );
  1133. }
  1134. /**
  1135. * Attempt to boot from an INT 13 drive
  1136. *
  1137. * @v drive Drive number
  1138. * @ret rc Return status code
  1139. *
  1140. * This boots from the specified INT 13 drive by loading the Master
  1141. * Boot Record to 0000:7c00 and jumping to it. INT 18 is hooked to
  1142. * capture an attempt by the MBR to boot the next device. (This is
  1143. * the closest thing to a return path from an MBR).
  1144. *
  1145. * Note that this function can never return success, by definition.
  1146. */
  1147. static int int13_boot ( unsigned int drive ) {
  1148. struct memory_map memmap;
  1149. int status, signature;
  1150. int discard_c, discard_d;
  1151. int rc;
  1152. DBG ( "INT13 drive %02x booting\n", drive );
  1153. /* Use INT 13 to read the boot sector */
  1154. __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
  1155. "pushw $0\n\t"
  1156. "popw %%es\n\t"
  1157. "stc\n\t"
  1158. "sti\n\t"
  1159. "int $0x13\n\t"
  1160. "sti\n\t" /* BIOS bugs */
  1161. "jc 1f\n\t"
  1162. "xorl %%eax, %%eax\n\t"
  1163. "\n1:\n\t"
  1164. "movzwl %%es:0x7dfe, %%ebx\n\t"
  1165. "popw %%es\n\t" )
  1166. : "=a" ( status ), "=b" ( signature ),
  1167. "=c" ( discard_c ), "=d" ( discard_d )
  1168. : "a" ( 0x0201 ), "b" ( 0x7c00 ),
  1169. "c" ( 1 ), "d" ( drive ) );
  1170. if ( status )
  1171. return -EIO;
  1172. /* Check signature is correct */
  1173. if ( signature != be16_to_cpu ( 0x55aa ) ) {
  1174. DBG ( "INT13 drive %02x invalid disk signature %#04x (should "
  1175. "be 0x55aa)\n", drive, cpu_to_be16 ( signature ) );
  1176. return -ENOEXEC;
  1177. }
  1178. /* Dump out memory map prior to boot, if memmap debugging is
  1179. * enabled. Not required for program flow, but we have so
  1180. * many problems that turn out to be memory-map related that
  1181. * it's worth doing.
  1182. */
  1183. get_memmap ( &memmap );
  1184. /* Jump to boot sector */
  1185. if ( ( rc = call_bootsector ( 0x0, 0x7c00, drive ) ) != 0 ) {
  1186. DBG ( "INT13 drive %02x boot returned: %s\n",
  1187. drive, strerror ( rc ) );
  1188. return rc;
  1189. }
  1190. return -ECANCELED; /* -EIMPOSSIBLE */
  1191. }
  1192. /** A boot firmware table generated by iPXE */
  1193. union xbft_table {
  1194. /** ACPI header */
  1195. struct acpi_description_header acpi;
  1196. /** Padding */
  1197. char pad[768];
  1198. };
  1199. /** The boot firmware table generated by iPXE */
  1200. static union xbft_table __bss16 ( xbftab ) __attribute__ (( aligned ( 16 ) ));
  1201. #define xbftab __use_data16 ( xbftab )
  1202. /**
  1203. * Describe INT 13 emulated drive for SAN-booted operating system
  1204. *
  1205. * @v drive Drive number
  1206. * @ret rc Return status code
  1207. */
  1208. static int int13_describe ( unsigned int drive ) {
  1209. struct int13_drive *int13;
  1210. struct segoff xbft_address;
  1211. int rc;
  1212. /* Find drive */
  1213. int13 = int13_find ( drive );
  1214. if ( ! int13 ) {
  1215. DBG ( "INT13 cannot find emulated drive %02x\n", drive );
  1216. return -ENODEV;
  1217. }
  1218. /* Clear table */
  1219. memset ( &xbftab, 0, sizeof ( xbftab ) );
  1220. /* Fill in common parameters */
  1221. strncpy ( xbftab.acpi.oem_id, "FENSYS",
  1222. sizeof ( xbftab.acpi.oem_id ) );
  1223. strncpy ( xbftab.acpi.oem_table_id, "iPXE",
  1224. sizeof ( xbftab.acpi.oem_table_id ) );
  1225. /* Fill in remaining parameters */
  1226. if ( ( rc = acpi_describe ( &int13->block, &xbftab.acpi,
  1227. sizeof ( xbftab ) ) ) != 0 ) {
  1228. DBGC ( int13, "INT13 drive %02x could not create ACPI "
  1229. "description: %s\n", int13->drive, strerror ( rc ) );
  1230. return rc;
  1231. }
  1232. /* Fix up ACPI checksum */
  1233. acpi_fix_checksum ( &xbftab.acpi );
  1234. xbft_address.segment = rm_ds;
  1235. xbft_address.offset = __from_data16 ( &xbftab );
  1236. DBGC ( int13, "INT13 drive %02x described using boot firmware "
  1237. "table:\n", int13->drive );
  1238. DBGC_HDA ( int13, xbft_address, &xbftab,
  1239. le32_to_cpu ( xbftab.acpi.length ) );
  1240. return 0;
  1241. }
  1242. PROVIDE_SANBOOT ( pcbios, san_hook, int13_hook );
  1243. PROVIDE_SANBOOT ( pcbios, san_unhook, int13_unhook );
  1244. PROVIDE_SANBOOT ( pcbios, san_boot, int13_boot );
  1245. PROVIDE_SANBOOT ( pcbios, san_describe, int13_describe );