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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <limits.h>
  27. #include <byteswap.h>
  28. #include <errno.h>
  29. #include <assert.h>
  30. #include <ipxe/list.h>
  31. #include <ipxe/blockdev.h>
  32. #include <ipxe/io.h>
  33. #include <ipxe/open.h>
  34. #include <ipxe/uri.h>
  35. #include <ipxe/process.h>
  36. #include <ipxe/xfer.h>
  37. #include <ipxe/retry.h>
  38. #include <ipxe/timer.h>
  39. #include <ipxe/acpi.h>
  40. #include <ipxe/sanboot.h>
  41. #include <ipxe/device.h>
  42. #include <ipxe/pci.h>
  43. #include <ipxe/iso9660.h>
  44. #include <ipxe/eltorito.h>
  45. #include <realmode.h>
  46. #include <bios.h>
  47. #include <biosint.h>
  48. #include <bootsector.h>
  49. #include <int13.h>
  50. /** @file
  51. *
  52. * INT 13 emulation
  53. *
  54. * This module provides a mechanism for exporting block devices via
  55. * the BIOS INT 13 disk interrupt interface.
  56. *
  57. */
  58. /**
  59. * Overall timeout for INT 13 commands (independent of underlying device
  60. *
  61. * Underlying devices should ideally never become totally stuck.
  62. * However, if they do, then the INT 13 mechanism provides no means
  63. * for the caller to cancel the operation, and the machine appears to
  64. * hang. Use an overall timeout for all commands to avoid this
  65. * problem and bounce timeout failures to the caller.
  66. */
  67. #define INT13_COMMAND_TIMEOUT ( 15 * TICKS_PER_SEC )
  68. /** An INT 13 emulated drive */
  69. struct int13_drive {
  70. /** Reference count */
  71. struct refcnt refcnt;
  72. /** List of all registered drives */
  73. struct list_head list;
  74. /** Block device URI */
  75. struct uri *uri;
  76. /** Underlying block device interface */
  77. struct interface block;
  78. /** BIOS in-use drive number (0x00-0xff) */
  79. unsigned int drive;
  80. /** BIOS natural drive number (0x00-0xff)
  81. *
  82. * This is the drive number that would have been assigned by
  83. * 'naturally' appending the drive to the end of the BIOS
  84. * drive list.
  85. *
  86. * If the emulated drive replaces a preexisting drive, this is
  87. * the drive number that the preexisting drive gets remapped
  88. * to.
  89. */
  90. unsigned int natural_drive;
  91. /** Block device capacity */
  92. struct block_device_capacity capacity;
  93. /** INT 13 emulated blocksize shift
  94. *
  95. * To allow for emulation of CD-ROM access, this represents
  96. * the left-shift required to translate from INT 13 blocks to
  97. * underlying blocks.
  98. */
  99. unsigned int blksize_shift;
  100. /** Number of cylinders
  101. *
  102. * The cylinder number field in an INT 13 call is ten bits
  103. * wide, giving a maximum of 1024 cylinders. Conventionally,
  104. * when the 7.8GB limit of a CHS address is exceeded, it is
  105. * the number of cylinders that is increased beyond the
  106. * addressable limit.
  107. */
  108. unsigned int cylinders;
  109. /** Number of heads
  110. *
  111. * The head number field in an INT 13 call is eight bits wide,
  112. * giving a maximum of 256 heads. However, apparently all
  113. * versions of MS-DOS up to and including Win95 fail with 256
  114. * heads, so the maximum encountered in practice is 255.
  115. */
  116. unsigned int heads;
  117. /** Number of sectors per track
  118. *
  119. * The sector number field in an INT 13 call is six bits wide,
  120. * giving a maximum of 63 sectors, since sector numbering
  121. * (unlike head and cylinder numbering) starts at 1, not 0.
  122. */
  123. unsigned int sectors_per_track;
  124. /** Drive is a CD-ROM */
  125. int is_cdrom;
  126. /** Address of El Torito boot catalog (if any) */
  127. unsigned int boot_catalog;
  128. /** Underlying device status, if in error */
  129. int block_rc;
  130. /** Status of last operation */
  131. int last_status;
  132. };
  133. /** Vector for chaining to other INT 13 handlers */
  134. static struct segoff __text16 ( int13_vector );
  135. #define int13_vector __use_text16 ( int13_vector )
  136. /** Assembly wrapper */
  137. extern void int13_wrapper ( void );
  138. /** Dummy floppy disk parameter table */
  139. static struct int13_fdd_parameters __data16 ( int13_fdd_params ) = {
  140. /* 512 bytes per sector */
  141. .bytes_per_sector = 0x02,
  142. /* Highest sectors per track that we ever return */
  143. .sectors_per_track = 48,
  144. };
  145. #define int13_fdd_params __use_data16 ( int13_fdd_params )
  146. /** List of registered emulated drives */
  147. static LIST_HEAD ( int13s );
  148. /**
  149. * Equipment word
  150. *
  151. * This is a cached copy of the BIOS Data Area equipment word at
  152. * 40:10.
  153. */
  154. static uint16_t equipment_word;
  155. /**
  156. * Number of BIOS floppy disk drives
  157. *
  158. * This is derived from the equipment word. It is held in .text16 to
  159. * allow for easy access by the INT 13,08 wrapper.
  160. */
  161. static uint8_t __text16 ( num_fdds );
  162. #define num_fdds __use_text16 ( num_fdds )
  163. /**
  164. * Number of BIOS hard disk drives
  165. *
  166. * This is a cached copy of the BIOS Data Area number of hard disk
  167. * drives at 40:75. It is held in .text16 to allow for easy access by
  168. * the INT 13,08 wrapper.
  169. */
  170. static uint8_t __text16 ( num_drives );
  171. #define num_drives __use_text16 ( num_drives )
  172. /**
  173. * Calculate INT 13 drive sector size
  174. *
  175. * @v int13 Emulated drive
  176. * @ret blksize Sector size
  177. */
  178. static inline size_t int13_blksize ( struct int13_drive *int13 ) {
  179. return ( int13->capacity.blksize << int13->blksize_shift );
  180. }
  181. /**
  182. * Calculate INT 13 drive capacity
  183. *
  184. * @v int13 Emulated drive
  185. * @ret blocks Number of blocks
  186. */
  187. static inline uint64_t int13_capacity ( struct int13_drive *int13 ) {
  188. return ( int13->capacity.blocks >> int13->blksize_shift );
  189. }
  190. /**
  191. * Calculate INT 13 drive capacity (limited to 32 bits)
  192. *
  193. * @v int13 Emulated drive
  194. * @ret blocks Number of blocks
  195. */
  196. static inline uint32_t int13_capacity32 ( struct int13_drive *int13 ) {
  197. uint64_t capacity = int13_capacity ( int13 );
  198. return ( ( capacity <= 0xffffffffUL ) ? capacity : 0xffffffff );
  199. }
  200. /**
  201. * Test if INT 13 drive is a floppy disk drive
  202. *
  203. * @v int13 Emulated drive
  204. * @ret is_fdd Emulated drive is a floppy disk
  205. */
  206. static inline int int13_is_fdd ( struct int13_drive *int13 ) {
  207. return ( ! ( int13->drive & 0x80 ) );
  208. }
  209. /** An INT 13 command */
  210. struct int13_command {
  211. /** Status */
  212. int rc;
  213. /** INT 13 drive */
  214. struct int13_drive *int13;
  215. /** Underlying block device interface */
  216. struct interface block;
  217. /** Command timeout timer */
  218. struct retry_timer timer;
  219. };
  220. /**
  221. * Record INT 13 drive capacity
  222. *
  223. * @v command INT 13 command
  224. * @v capacity Block device capacity
  225. */
  226. static void int13_command_capacity ( struct int13_command *command,
  227. struct block_device_capacity *capacity ) {
  228. memcpy ( &command->int13->capacity, capacity,
  229. sizeof ( command->int13->capacity ) );
  230. }
  231. /**
  232. * Close INT 13 command
  233. *
  234. * @v command INT 13 command
  235. * @v rc Reason for close
  236. */
  237. static void int13_command_close ( struct int13_command *command, int rc ) {
  238. intf_restart ( &command->block, rc );
  239. stop_timer ( &command->timer );
  240. command->rc = rc;
  241. }
  242. /**
  243. * Handle INT 13 command timer expiry
  244. *
  245. * @v timer Timer
  246. */
  247. static void int13_command_expired ( struct retry_timer *timer,
  248. int over __unused ) {
  249. struct int13_command *command =
  250. container_of ( timer, struct int13_command, timer );
  251. int13_command_close ( command, -ETIMEDOUT );
  252. }
  253. /** INT 13 command interface operations */
  254. static struct interface_operation int13_command_op[] = {
  255. INTF_OP ( intf_close, struct int13_command *, int13_command_close ),
  256. INTF_OP ( block_capacity, struct int13_command *,
  257. int13_command_capacity ),
  258. };
  259. /** INT 13 command interface descriptor */
  260. static struct interface_descriptor int13_command_desc =
  261. INTF_DESC ( struct int13_command, block, int13_command_op );
  262. /**
  263. * Open (or reopen) INT 13 emulated drive underlying block device
  264. *
  265. * @v int13 Emulated drive
  266. * @ret rc Return status code
  267. */
  268. static int int13_reopen_block ( struct int13_drive *int13 ) {
  269. int rc;
  270. /* Close any existing block device */
  271. intf_restart ( &int13->block, -ECONNRESET );
  272. /* Open block device */
  273. if ( ( rc = xfer_open_uri ( &int13->block, int13->uri ) ) != 0 ) {
  274. DBGC ( int13, "INT13 drive %02x could not reopen block "
  275. "device: %s\n", int13->drive, strerror ( rc ) );
  276. int13->block_rc = rc;
  277. return rc;
  278. }
  279. /* Clear block device error status */
  280. int13->block_rc = 0;
  281. return 0;
  282. }
  283. /**
  284. * Prepare to issue INT 13 command
  285. *
  286. * @v command INT 13 command
  287. * @v int13 Emulated drive
  288. * @ret rc Return status code
  289. */
  290. static int int13_command_start ( struct int13_command *command,
  291. struct int13_drive *int13 ) {
  292. int rc;
  293. /* Sanity check */
  294. assert ( command->int13 == NULL );
  295. assert ( ! timer_running ( &command->timer ) );
  296. /* Reopen block device if necessary */
  297. if ( ( int13->block_rc != 0 ) &&
  298. ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) )
  299. return rc;
  300. /* Initialise command */
  301. command->rc = -EINPROGRESS;
  302. command->int13 = int13;
  303. start_timer_fixed ( &command->timer, INT13_COMMAND_TIMEOUT );
  304. /* Wait for block control interface to become ready */
  305. while ( ( command->rc == -EINPROGRESS ) &&
  306. ( xfer_window ( &int13->block ) == 0 ) ) {
  307. step();
  308. }
  309. return ( ( command->rc == -EINPROGRESS ) ?
  310. int13->block_rc : command->rc );
  311. }
  312. /**
  313. * Wait for INT 13 command to complete
  314. *
  315. * @v command INT 13 command
  316. * @ret rc Return status code
  317. */
  318. static int int13_command_wait ( struct int13_command *command ) {
  319. /* Sanity check */
  320. assert ( timer_running ( &command->timer ) );
  321. /* Wait for command to complete */
  322. while ( command->rc == -EINPROGRESS )
  323. step();
  324. assert ( ! timer_running ( &command->timer ) );
  325. return command->rc;
  326. }
  327. /**
  328. * Terminate INT 13 command
  329. *
  330. * @v command INT 13 command
  331. */
  332. static void int13_command_stop ( struct int13_command *command ) {
  333. stop_timer ( &command->timer );
  334. command->int13 = NULL;
  335. }
  336. /** The single active INT 13 command */
  337. static struct int13_command int13_command = {
  338. .block = INTF_INIT ( int13_command_desc ),
  339. .timer = TIMER_INIT ( int13_command_expired ),
  340. };
  341. /**
  342. * Read from or write to INT 13 drive
  343. *
  344. * @v int13 Emulated drive
  345. * @v lba Starting logical block address
  346. * @v count Number of logical blocks
  347. * @v buffer Data buffer
  348. * @v block_rw Block read/write method
  349. * @ret rc Return status code
  350. */
  351. static int int13_rw ( struct int13_drive *int13, uint64_t lba,
  352. unsigned int count, userptr_t buffer,
  353. int ( * block_rw ) ( struct interface *control,
  354. struct interface *data,
  355. uint64_t lba, unsigned int count,
  356. userptr_t buffer, size_t len ) ) {
  357. struct int13_command *command = &int13_command;
  358. unsigned int frag_count;
  359. size_t frag_len;
  360. int rc;
  361. /* Translate to underlying blocksize */
  362. lba <<= int13->blksize_shift;
  363. count <<= int13->blksize_shift;
  364. while ( count ) {
  365. /* Determine fragment length */
  366. frag_count = count;
  367. if ( frag_count > int13->capacity.max_count )
  368. frag_count = int13->capacity.max_count;
  369. frag_len = ( int13->capacity.blksize * frag_count );
  370. /* Issue command */
  371. if ( ( ( rc = int13_command_start ( command, int13 ) ) != 0 ) ||
  372. ( ( rc = block_rw ( &int13->block, &command->block, lba,
  373. frag_count, buffer,
  374. frag_len ) ) != 0 ) ||
  375. ( ( rc = int13_command_wait ( command ) ) != 0 ) ) {
  376. int13_command_stop ( command );
  377. return rc;
  378. }
  379. int13_command_stop ( command );
  380. /* Move to next fragment */
  381. lba += frag_count;
  382. count -= frag_count;
  383. buffer = userptr_add ( buffer, frag_len );
  384. }
  385. return 0;
  386. }
  387. /**
  388. * Read INT 13 drive capacity
  389. *
  390. * @v int13 Emulated drive
  391. * @ret rc Return status code
  392. */
  393. static int int13_read_capacity ( struct int13_drive *int13 ) {
  394. struct int13_command *command = &int13_command;
  395. int rc;
  396. /* Issue command */
  397. if ( ( ( rc = int13_command_start ( command, int13 ) ) != 0 ) ||
  398. ( ( rc = block_read_capacity ( &int13->block,
  399. &command->block ) ) != 0 ) ||
  400. ( ( rc = int13_command_wait ( command ) ) != 0 ) ) {
  401. int13_command_stop ( command );
  402. return rc;
  403. }
  404. int13_command_stop ( command );
  405. return 0;
  406. }
  407. /**
  408. * Parse ISO9660 parameters
  409. *
  410. * @v int13 Emulated drive
  411. * @v scratch Scratch area for single-sector reads
  412. * @ret rc Return status code
  413. *
  414. * Reads and parses ISO9660 parameters, if present.
  415. */
  416. static int int13_parse_iso9660 ( struct int13_drive *int13, void *scratch ) {
  417. static const struct iso9660_primary_descriptor_fixed primary_check = {
  418. .type = ISO9660_TYPE_PRIMARY,
  419. .id = ISO9660_ID,
  420. };
  421. struct iso9660_primary_descriptor *primary = scratch;
  422. static const struct eltorito_descriptor_fixed boot_check = {
  423. .type = ISO9660_TYPE_BOOT,
  424. .id = ISO9660_ID,
  425. .version = 1,
  426. .system_id = "EL TORITO SPECIFICATION",
  427. };
  428. struct eltorito_descriptor *boot = scratch;
  429. unsigned int blksize;
  430. unsigned int blksize_shift;
  431. int rc;
  432. /* Calculate required blocksize shift */
  433. blksize = int13_blksize ( int13 );
  434. blksize_shift = 0;
  435. while ( blksize < ISO9660_BLKSIZE ) {
  436. blksize <<= 1;
  437. blksize_shift++;
  438. }
  439. if ( blksize > ISO9660_BLKSIZE ) {
  440. /* Do nothing if the blksize is invalid for CD-ROM access */
  441. return 0;
  442. }
  443. /* Read primary volume descriptor */
  444. if ( ( rc = int13_rw ( int13,
  445. ( ISO9660_PRIMARY_LBA << blksize_shift ), 1,
  446. virt_to_user ( primary ), block_read ) ) != 0 ){
  447. DBGC ( int13, "INT13 drive %02x could not read ISO9660 "
  448. "primary volume descriptor: %s\n",
  449. int13->drive, strerror ( rc ) );
  450. return rc;
  451. }
  452. /* Do nothing unless this is an ISO image */
  453. if ( memcmp ( primary, &primary_check, sizeof ( primary_check ) ) != 0 )
  454. return 0;
  455. DBGC ( int13, "INT13 drive %02x contains an ISO9660 filesystem; "
  456. "treating as CD-ROM\n", int13->drive );
  457. int13->is_cdrom = 1;
  458. /* Read boot record volume descriptor */
  459. if ( ( rc = int13_rw ( int13,
  460. ( ELTORITO_LBA << blksize_shift ), 1,
  461. virt_to_user ( boot ), block_read ) ) != 0 ) {
  462. DBGC ( int13, "INT13 drive %02x could not read El Torito boot "
  463. "record volume descriptor: %s\n",
  464. int13->drive, strerror ( rc ) );
  465. return rc;
  466. }
  467. /* Check for an El Torito boot catalog */
  468. if ( memcmp ( boot, &boot_check, sizeof ( boot_check ) ) == 0 ) {
  469. int13->boot_catalog = boot->sector;
  470. DBGC ( int13, "INT13 drive %02x has an El Torito boot catalog "
  471. "at LBA %08x\n", int13->drive, int13->boot_catalog );
  472. } else {
  473. DBGC ( int13, "INT13 drive %02x has no El Torito boot "
  474. "catalog\n", int13->drive );
  475. }
  476. /* Configure drive for no-emulation CD-ROM access */
  477. int13->blksize_shift += blksize_shift;
  478. return 0;
  479. }
  480. /**
  481. * Guess INT 13 hard disk drive geometry
  482. *
  483. * @v int13 Emulated drive
  484. * @v scratch Scratch area for single-sector reads
  485. * @ret heads Guessed number of heads
  486. * @ret sectors Guessed number of sectors per track
  487. * @ret rc Return status code
  488. *
  489. * Guesses the drive geometry by inspecting the partition table.
  490. */
  491. static int int13_guess_geometry_hdd ( struct int13_drive *int13, void *scratch,
  492. unsigned int *heads,
  493. unsigned int *sectors ) {
  494. struct master_boot_record *mbr = scratch;
  495. struct partition_table_entry *partition;
  496. unsigned int i;
  497. int rc;
  498. /* Default guess is xx/255/63 */
  499. *heads = 255;
  500. *sectors = 63;
  501. /* Read partition table */
  502. if ( ( rc = int13_rw ( int13, 0, 1, virt_to_user ( mbr ),
  503. block_read ) ) != 0 ) {
  504. DBGC ( int13, "INT13 drive %02x could not read "
  505. "partition table to guess geometry: %s\n",
  506. int13->drive, strerror ( rc ) );
  507. return rc;
  508. }
  509. DBGC2 ( int13, "INT13 drive %02x has MBR:\n", int13->drive );
  510. DBGC2_HDA ( int13, 0, mbr, sizeof ( *mbr ) );
  511. DBGC ( int13, "INT13 drive %02x has signature %08x\n",
  512. int13->drive, mbr->signature );
  513. /* Scan through partition table and modify guesses for
  514. * heads and sectors_per_track if we find any used
  515. * partitions.
  516. */
  517. for ( i = 0 ; i < 4 ; i++ ) {
  518. partition = &mbr->partitions[i];
  519. if ( ! partition->type )
  520. continue;
  521. *heads = ( PART_HEAD ( partition->chs_end ) + 1 );
  522. *sectors = PART_SECTOR ( partition->chs_end );
  523. DBGC ( int13, "INT13 drive %02x guessing C/H/S xx/%d/%d based "
  524. "on partition %d\n",
  525. int13->drive, *heads, *sectors, ( i + 1 ) );
  526. }
  527. return 0;
  528. }
  529. /** Recognised floppy disk geometries */
  530. static const struct int13_fdd_geometry int13_fdd_geometries[] = {
  531. INT13_FDD_GEOMETRY ( 40, 1, 8 ),
  532. INT13_FDD_GEOMETRY ( 40, 1, 9 ),
  533. INT13_FDD_GEOMETRY ( 40, 2, 8 ),
  534. INT13_FDD_GEOMETRY ( 40, 1, 9 ),
  535. INT13_FDD_GEOMETRY ( 80, 2, 8 ),
  536. INT13_FDD_GEOMETRY ( 80, 2, 9 ),
  537. INT13_FDD_GEOMETRY ( 80, 2, 15 ),
  538. INT13_FDD_GEOMETRY ( 80, 2, 18 ),
  539. INT13_FDD_GEOMETRY ( 80, 2, 20 ),
  540. INT13_FDD_GEOMETRY ( 80, 2, 21 ),
  541. INT13_FDD_GEOMETRY ( 82, 2, 21 ),
  542. INT13_FDD_GEOMETRY ( 83, 2, 21 ),
  543. INT13_FDD_GEOMETRY ( 80, 2, 22 ),
  544. INT13_FDD_GEOMETRY ( 80, 2, 23 ),
  545. INT13_FDD_GEOMETRY ( 80, 2, 24 ),
  546. INT13_FDD_GEOMETRY ( 80, 2, 36 ),
  547. INT13_FDD_GEOMETRY ( 80, 2, 39 ),
  548. INT13_FDD_GEOMETRY ( 80, 2, 40 ),
  549. INT13_FDD_GEOMETRY ( 80, 2, 44 ),
  550. INT13_FDD_GEOMETRY ( 80, 2, 48 ),
  551. };
  552. /**
  553. * Guess INT 13 floppy disk drive geometry
  554. *
  555. * @v int13 Emulated drive
  556. * @ret heads Guessed number of heads
  557. * @ret sectors Guessed number of sectors per track
  558. * @ret rc Return status code
  559. *
  560. * Guesses the drive geometry by inspecting the disk size.
  561. */
  562. static int int13_guess_geometry_fdd ( struct int13_drive *int13,
  563. unsigned int *heads,
  564. unsigned int *sectors ) {
  565. unsigned int blocks = int13_capacity ( int13 );
  566. const struct int13_fdd_geometry *geometry;
  567. unsigned int cylinders;
  568. unsigned int i;
  569. /* Look for a match against a known geometry */
  570. for ( i = 0 ; i < ( sizeof ( int13_fdd_geometries ) /
  571. sizeof ( int13_fdd_geometries[0] ) ) ; i++ ) {
  572. geometry = &int13_fdd_geometries[i];
  573. cylinders = INT13_FDD_CYLINDERS ( geometry );
  574. *heads = INT13_FDD_HEADS ( geometry );
  575. *sectors = INT13_FDD_SECTORS ( geometry );
  576. if ( ( cylinders * (*heads) * (*sectors) ) == blocks ) {
  577. DBGC ( int13, "INT13 drive %02x guessing C/H/S "
  578. "%d/%d/%d based on size %dK\n", int13->drive,
  579. cylinders, *heads, *sectors, ( blocks / 2 ) );
  580. return 0;
  581. }
  582. }
  583. /* Otherwise, assume a partial disk image in the most common
  584. * format (1440K, 80/2/18).
  585. */
  586. *heads = 2;
  587. *sectors = 18;
  588. DBGC ( int13, "INT13 drive %02x guessing C/H/S xx/%d/%d based on size "
  589. "%dK\n", int13->drive, *heads, *sectors, ( blocks / 2 ) );
  590. return 0;
  591. }
  592. /**
  593. * Guess INT 13 drive geometry
  594. *
  595. * @v int13 Emulated drive
  596. * @v scratch Scratch area for single-sector reads
  597. * @ret rc Return status code
  598. */
  599. static int int13_guess_geometry ( struct int13_drive *int13, void *scratch ) {
  600. unsigned int guessed_heads;
  601. unsigned int guessed_sectors;
  602. unsigned int blocks;
  603. unsigned int blocks_per_cyl;
  604. int rc;
  605. /* Don't even try when the blksize is invalid for C/H/S access */
  606. if ( int13_blksize ( int13 ) != INT13_BLKSIZE )
  607. return 0;
  608. /* Guess geometry according to drive type */
  609. if ( int13_is_fdd ( int13 ) ) {
  610. if ( ( rc = int13_guess_geometry_fdd ( int13, &guessed_heads,
  611. &guessed_sectors )) != 0)
  612. return rc;
  613. } else {
  614. if ( ( rc = int13_guess_geometry_hdd ( int13, scratch,
  615. &guessed_heads,
  616. &guessed_sectors )) != 0)
  617. return rc;
  618. }
  619. /* Apply guesses if no geometry already specified */
  620. if ( ! int13->heads )
  621. int13->heads = guessed_heads;
  622. if ( ! int13->sectors_per_track )
  623. int13->sectors_per_track = guessed_sectors;
  624. if ( ! int13->cylinders ) {
  625. /* Avoid attempting a 64-bit divide on a 32-bit system */
  626. blocks = int13_capacity32 ( int13 );
  627. blocks_per_cyl = ( int13->heads * int13->sectors_per_track );
  628. assert ( blocks_per_cyl != 0 );
  629. int13->cylinders = ( blocks / blocks_per_cyl );
  630. if ( int13->cylinders > 1024 )
  631. int13->cylinders = 1024;
  632. }
  633. return 0;
  634. }
  635. /**
  636. * Update BIOS drive count
  637. */
  638. static void int13_sync_num_drives ( void ) {
  639. struct int13_drive *int13;
  640. uint8_t *counter;
  641. uint8_t max_drive;
  642. uint8_t required;
  643. /* Get current drive counts */
  644. get_real ( equipment_word, BDA_SEG, BDA_EQUIPMENT_WORD );
  645. get_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
  646. num_fdds = ( ( equipment_word & 0x0001 ) ?
  647. ( ( ( equipment_word >> 6 ) & 0x3 ) + 1 ) : 0 );
  648. /* Ensure count is large enough to cover all of our emulated drives */
  649. list_for_each_entry ( int13, &int13s, list ) {
  650. counter = ( int13_is_fdd ( int13 ) ? &num_fdds : &num_drives );
  651. max_drive = int13->drive;
  652. if ( max_drive < int13->natural_drive )
  653. max_drive = int13->natural_drive;
  654. required = ( ( max_drive & 0x7f ) + 1 );
  655. if ( *counter < required ) {
  656. *counter = required;
  657. DBGC ( int13, "INT13 drive %02x added to drive count: "
  658. "%d HDDs, %d FDDs\n",
  659. int13->drive, num_drives, num_fdds );
  660. }
  661. }
  662. /* Update current drive count */
  663. equipment_word &= ~( ( 0x3 << 6 ) | 0x0001 );
  664. if ( num_fdds ) {
  665. equipment_word |= ( 0x0001 |
  666. ( ( ( num_fdds - 1 ) & 0x3 ) << 6 ) );
  667. }
  668. put_real ( equipment_word, BDA_SEG, BDA_EQUIPMENT_WORD );
  669. put_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
  670. }
  671. /**
  672. * Check number of drives
  673. */
  674. static void int13_check_num_drives ( void ) {
  675. uint16_t check_equipment_word;
  676. uint8_t check_num_drives;
  677. get_real ( check_equipment_word, BDA_SEG, BDA_EQUIPMENT_WORD );
  678. get_real ( check_num_drives, BDA_SEG, BDA_NUM_DRIVES );
  679. if ( ( check_equipment_word != equipment_word ) ||
  680. ( check_num_drives != num_drives ) ) {
  681. int13_sync_num_drives();
  682. }
  683. }
  684. /**
  685. * INT 13, 00 - Reset disk system
  686. *
  687. * @v int13 Emulated drive
  688. * @ret status Status code
  689. */
  690. static int int13_reset ( struct int13_drive *int13,
  691. struct i386_all_regs *ix86 __unused ) {
  692. int rc;
  693. DBGC2 ( int13, "Reset drive\n" );
  694. /* Reopen underlying block device */
  695. if ( ( rc = int13_reopen_block ( int13 ) ) != 0 )
  696. return -INT13_STATUS_RESET_FAILED;
  697. /* Check that block device is functional */
  698. if ( ( rc = int13_read_capacity ( int13 ) ) != 0 )
  699. return -INT13_STATUS_RESET_FAILED;
  700. return 0;
  701. }
  702. /**
  703. * INT 13, 01 - Get status of last operation
  704. *
  705. * @v int13 Emulated drive
  706. * @ret status Status code
  707. */
  708. static int int13_get_last_status ( struct int13_drive *int13,
  709. struct i386_all_regs *ix86 __unused ) {
  710. DBGC2 ( int13, "Get status of last operation\n" );
  711. return int13->last_status;
  712. }
  713. /**
  714. * Read / write sectors
  715. *
  716. * @v int13 Emulated drive
  717. * @v al Number of sectors to read or write (must be nonzero)
  718. * @v ch Low bits of cylinder number
  719. * @v cl (bits 7:6) High bits of cylinder number
  720. * @v cl (bits 5:0) Sector number
  721. * @v dh Head number
  722. * @v es:bx Data buffer
  723. * @v block_rw Block read/write method
  724. * @ret status Status code
  725. * @ret al Number of sectors read or written
  726. */
  727. static int int13_rw_sectors ( struct int13_drive *int13,
  728. struct i386_all_regs *ix86,
  729. int ( * block_rw ) ( struct interface *control,
  730. struct interface *data,
  731. uint64_t lba,
  732. unsigned int count,
  733. userptr_t buffer,
  734. size_t len ) ) {
  735. unsigned int cylinder, head, sector;
  736. unsigned long lba;
  737. unsigned int count;
  738. userptr_t buffer;
  739. int rc;
  740. /* Validate blocksize */
  741. if ( int13_blksize ( int13 ) != INT13_BLKSIZE ) {
  742. DBGC ( int13, "\nINT 13 drive %02x invalid blocksize (%zd) "
  743. "for non-extended read/write\n",
  744. int13->drive, int13_blksize ( int13 ) );
  745. return -INT13_STATUS_INVALID;
  746. }
  747. /* Calculate parameters */
  748. cylinder = ( ( ( ix86->regs.cl & 0xc0 ) << 2 ) | ix86->regs.ch );
  749. head = ix86->regs.dh;
  750. sector = ( ix86->regs.cl & 0x3f );
  751. if ( ( cylinder >= int13->cylinders ) ||
  752. ( head >= int13->heads ) ||
  753. ( sector < 1 ) || ( sector > int13->sectors_per_track ) ) {
  754. DBGC ( int13, "C/H/S %d/%d/%d out of range for geometry "
  755. "%d/%d/%d\n", cylinder, head, sector, int13->cylinders,
  756. int13->heads, int13->sectors_per_track );
  757. return -INT13_STATUS_INVALID;
  758. }
  759. lba = ( ( ( ( cylinder * int13->heads ) + head )
  760. * int13->sectors_per_track ) + sector - 1 );
  761. count = ix86->regs.al;
  762. buffer = real_to_user ( ix86->segs.es, ix86->regs.bx );
  763. DBGC2 ( int13, "C/H/S %d/%d/%d = LBA %08lx <-> %04x:%04x (count %d)\n",
  764. cylinder, head, sector, lba, ix86->segs.es, ix86->regs.bx,
  765. count );
  766. /* Read from / write to block device */
  767. if ( ( rc = int13_rw ( int13, lba, count, buffer, block_rw ) ) != 0 ) {
  768. DBGC ( int13, "INT13 drive %02x I/O failed: %s\n",
  769. int13->drive, strerror ( rc ) );
  770. return -INT13_STATUS_READ_ERROR;
  771. }
  772. return 0;
  773. }
  774. /**
  775. * INT 13, 02 - Read sectors
  776. *
  777. * @v int13 Emulated drive
  778. * @v al Number of sectors to read (must be nonzero)
  779. * @v ch Low bits of cylinder number
  780. * @v cl (bits 7:6) High bits of cylinder number
  781. * @v cl (bits 5:0) Sector number
  782. * @v dh Head number
  783. * @v es:bx Data buffer
  784. * @ret status Status code
  785. * @ret al Number of sectors read
  786. */
  787. static int int13_read_sectors ( struct int13_drive *int13,
  788. struct i386_all_regs *ix86 ) {
  789. DBGC2 ( int13, "Read: " );
  790. return int13_rw_sectors ( int13, ix86, block_read );
  791. }
  792. /**
  793. * INT 13, 03 - Write sectors
  794. *
  795. * @v int13 Emulated drive
  796. * @v al Number of sectors to write (must be nonzero)
  797. * @v ch Low bits of cylinder number
  798. * @v cl (bits 7:6) High bits of cylinder number
  799. * @v cl (bits 5:0) Sector number
  800. * @v dh Head number
  801. * @v es:bx Data buffer
  802. * @ret status Status code
  803. * @ret al Number of sectors written
  804. */
  805. static int int13_write_sectors ( struct int13_drive *int13,
  806. struct i386_all_regs *ix86 ) {
  807. DBGC2 ( int13, "Write: " );
  808. return int13_rw_sectors ( int13, ix86, block_write );
  809. }
  810. /**
  811. * INT 13, 08 - Get drive parameters
  812. *
  813. * @v int13 Emulated drive
  814. * @ret status Status code
  815. * @ret ch Low bits of maximum cylinder number
  816. * @ret cl (bits 7:6) High bits of maximum cylinder number
  817. * @ret cl (bits 5:0) Maximum sector number
  818. * @ret dh Maximum head number
  819. * @ret dl Number of drives
  820. */
  821. static int int13_get_parameters ( struct int13_drive *int13,
  822. struct i386_all_regs *ix86 ) {
  823. unsigned int max_cylinder = int13->cylinders - 1;
  824. unsigned int max_head = int13->heads - 1;
  825. unsigned int max_sector = int13->sectors_per_track; /* sic */
  826. DBGC2 ( int13, "Get drive parameters\n" );
  827. /* Validate blocksize */
  828. if ( int13_blksize ( int13 ) != INT13_BLKSIZE ) {
  829. DBGC ( int13, "\nINT 13 drive %02x invalid blocksize (%zd) "
  830. "for non-extended parameters\n",
  831. int13->drive, int13_blksize ( int13 ) );
  832. return -INT13_STATUS_INVALID;
  833. }
  834. /* Common parameters */
  835. ix86->regs.ch = ( max_cylinder & 0xff );
  836. ix86->regs.cl = ( ( ( max_cylinder >> 8 ) << 6 ) | max_sector );
  837. ix86->regs.dh = max_head;
  838. ix86->regs.dl = ( int13_is_fdd ( int13 ) ? num_fdds : num_drives );
  839. /* Floppy-specific parameters */
  840. if ( int13_is_fdd ( int13 ) ) {
  841. ix86->regs.bl = INT13_FDD_TYPE_1M44;
  842. ix86->segs.es = rm_ds;
  843. ix86->regs.di = __from_data16 ( &int13_fdd_params );
  844. }
  845. return 0;
  846. }
  847. /**
  848. * INT 13, 15 - Get disk type
  849. *
  850. * @v int13 Emulated drive
  851. * @ret ah Type code
  852. * @ret cx:dx Sector count
  853. * @ret status Status code / disk type
  854. */
  855. static int int13_get_disk_type ( struct int13_drive *int13,
  856. struct i386_all_regs *ix86 ) {
  857. uint32_t blocks;
  858. DBGC2 ( int13, "Get disk type\n" );
  859. if ( int13_is_fdd ( int13 ) ) {
  860. return INT13_DISK_TYPE_FDD;
  861. } else {
  862. blocks = int13_capacity32 ( int13 );
  863. ix86->regs.cx = ( blocks >> 16 );
  864. ix86->regs.dx = ( blocks & 0xffff );
  865. return INT13_DISK_TYPE_HDD;
  866. }
  867. }
  868. /**
  869. * INT 13, 41 - Extensions installation check
  870. *
  871. * @v int13 Emulated drive
  872. * @v bx 0x55aa
  873. * @ret bx 0xaa55
  874. * @ret cx Extensions API support bitmap
  875. * @ret status Status code / API version
  876. */
  877. static int int13_extension_check ( struct int13_drive *int13 __unused,
  878. struct i386_all_regs *ix86 ) {
  879. if ( ix86->regs.bx == 0x55aa ) {
  880. DBGC2 ( int13, "INT13 extensions installation check\n" );
  881. ix86->regs.bx = 0xaa55;
  882. ix86->regs.cx = ( INT13_EXTENSION_LINEAR |
  883. INT13_EXTENSION_EDD |
  884. INT13_EXTENSION_64BIT );
  885. return INT13_EXTENSION_VER_3_0;
  886. } else {
  887. return -INT13_STATUS_INVALID;
  888. }
  889. }
  890. /**
  891. * Extended read / write
  892. *
  893. * @v int13 Emulated drive
  894. * @v ds:si Disk address packet
  895. * @v block_rw Block read/write method
  896. * @ret status Status code
  897. */
  898. static int int13_extended_rw ( struct int13_drive *int13,
  899. struct i386_all_regs *ix86,
  900. int ( * block_rw ) ( struct interface *control,
  901. struct interface *data,
  902. uint64_t lba,
  903. unsigned int count,
  904. userptr_t buffer,
  905. size_t len ) ) {
  906. struct int13_disk_address addr;
  907. uint8_t bufsize;
  908. uint64_t lba;
  909. unsigned long count;
  910. userptr_t buffer;
  911. int rc;
  912. /* Extended reads are not allowed on floppy drives.
  913. * ELTORITO.SYS seems to assume that we are really a CD-ROM if
  914. * we support extended reads for a floppy drive.
  915. */
  916. if ( int13_is_fdd ( int13 ) )
  917. return -INT13_STATUS_INVALID;
  918. /* Get buffer size */
  919. get_real ( bufsize, ix86->segs.ds,
  920. ( ix86->regs.si + offsetof ( typeof ( addr ), bufsize ) ) );
  921. if ( bufsize < offsetof ( typeof ( addr ), buffer_phys ) ) {
  922. DBGC2 ( int13, "<invalid buffer size %#02x\n>\n", bufsize );
  923. return -INT13_STATUS_INVALID;
  924. }
  925. /* Read parameters from disk address structure */
  926. memset ( &addr, 0, sizeof ( addr ) );
  927. copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si, bufsize );
  928. lba = addr.lba;
  929. DBGC2 ( int13, "LBA %08llx <-> ", ( ( unsigned long long ) lba ) );
  930. if ( ( addr.count == 0xff ) ||
  931. ( ( addr.buffer.segment == 0xffff ) &&
  932. ( addr.buffer.offset == 0xffff ) ) ) {
  933. buffer = phys_to_user ( addr.buffer_phys );
  934. DBGC2 ( int13, "%08llx",
  935. ( ( unsigned long long ) addr.buffer_phys ) );
  936. } else {
  937. buffer = real_to_user ( addr.buffer.segment,
  938. addr.buffer.offset );
  939. DBGC2 ( int13, "%04x:%04x", addr.buffer.segment,
  940. addr.buffer.offset );
  941. }
  942. if ( addr.count <= 0x7f ) {
  943. count = addr.count;
  944. } else if ( addr.count == 0xff ) {
  945. count = addr.long_count;
  946. } else {
  947. DBGC2 ( int13, " <invalid count %#02x>\n", addr.count );
  948. return -INT13_STATUS_INVALID;
  949. }
  950. DBGC2 ( int13, " (count %ld)\n", count );
  951. /* Read from / write to block device */
  952. if ( ( rc = int13_rw ( int13, lba, count, buffer, block_rw ) ) != 0 ) {
  953. DBGC ( int13, "INT13 drive %02x extended I/O failed: %s\n",
  954. int13->drive, strerror ( rc ) );
  955. /* Record that no blocks were transferred successfully */
  956. addr.count = 0;
  957. put_real ( addr.count, ix86->segs.ds,
  958. ( ix86->regs.si +
  959. offsetof ( typeof ( addr ), count ) ) );
  960. return -INT13_STATUS_READ_ERROR;
  961. }
  962. return 0;
  963. }
  964. /**
  965. * INT 13, 42 - Extended read
  966. *
  967. * @v int13 Emulated drive
  968. * @v ds:si Disk address packet
  969. * @ret status Status code
  970. */
  971. static int int13_extended_read ( struct int13_drive *int13,
  972. struct i386_all_regs *ix86 ) {
  973. DBGC2 ( int13, "Extended read: " );
  974. return int13_extended_rw ( int13, ix86, block_read );
  975. }
  976. /**
  977. * INT 13, 43 - Extended write
  978. *
  979. * @v int13 Emulated drive
  980. * @v ds:si Disk address packet
  981. * @ret status Status code
  982. */
  983. static int int13_extended_write ( struct int13_drive *int13,
  984. struct i386_all_regs *ix86 ) {
  985. DBGC2 ( int13, "Extended write: " );
  986. return int13_extended_rw ( int13, ix86, block_write );
  987. }
  988. /**
  989. * INT 13, 44 - Verify sectors
  990. *
  991. * @v int13 Emulated drive
  992. * @v ds:si Disk address packet
  993. * @ret status Status code
  994. */
  995. static int int13_extended_verify ( struct int13_drive *int13,
  996. struct i386_all_regs *ix86 ) {
  997. struct int13_disk_address addr;
  998. uint64_t lba;
  999. unsigned long count;
  1000. /* Read parameters from disk address structure */
  1001. if ( DBG_EXTRA ) {
  1002. copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si,
  1003. sizeof ( addr ));
  1004. lba = addr.lba;
  1005. count = addr.count;
  1006. DBGC2 ( int13, "Verify: LBA %08llx (count %ld)\n",
  1007. ( ( unsigned long long ) lba ), count );
  1008. }
  1009. /* We have no mechanism for verifying sectors */
  1010. return -INT13_STATUS_INVALID;
  1011. }
  1012. /**
  1013. * INT 13, 44 - Extended seek
  1014. *
  1015. * @v int13 Emulated drive
  1016. * @v ds:si Disk address packet
  1017. * @ret status Status code
  1018. */
  1019. static int int13_extended_seek ( struct int13_drive *int13,
  1020. struct i386_all_regs *ix86 ) {
  1021. struct int13_disk_address addr;
  1022. uint64_t lba;
  1023. unsigned long count;
  1024. /* Read parameters from disk address structure */
  1025. if ( DBG_EXTRA ) {
  1026. copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si,
  1027. sizeof ( addr ));
  1028. lba = addr.lba;
  1029. count = addr.count;
  1030. DBGC2 ( int13, "Seek: LBA %08llx (count %ld)\n",
  1031. ( ( unsigned long long ) lba ), count );
  1032. }
  1033. /* Ignore and return success */
  1034. return 0;
  1035. }
  1036. /**
  1037. * Build device path information
  1038. *
  1039. * @v int13 Emulated drive
  1040. * @v dpi Device path information
  1041. * @ret rc Return status code
  1042. */
  1043. static int int13_device_path_info ( struct int13_drive *int13,
  1044. struct edd_device_path_information *dpi ) {
  1045. struct device *device;
  1046. struct device_description *desc;
  1047. unsigned int i;
  1048. uint8_t sum = 0;
  1049. int rc;
  1050. /* Reopen block device if necessary */
  1051. if ( ( int13->block_rc != 0 ) &&
  1052. ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) )
  1053. return rc;
  1054. /* Get underlying hardware device */
  1055. device = identify_device ( &int13->block );
  1056. if ( ! device ) {
  1057. DBGC ( int13, "INT13 drive %02x cannot identify hardware "
  1058. "device\n", int13->drive );
  1059. return -ENODEV;
  1060. }
  1061. /* Fill in bus type and interface path */
  1062. desc = &device->desc;
  1063. switch ( desc->bus_type ) {
  1064. case BUS_TYPE_PCI:
  1065. dpi->host_bus_type.type = EDD_BUS_TYPE_PCI;
  1066. dpi->interface_path.pci.bus = PCI_BUS ( desc->location );
  1067. dpi->interface_path.pci.slot = PCI_SLOT ( desc->location );
  1068. dpi->interface_path.pci.function = PCI_FUNC ( desc->location );
  1069. dpi->interface_path.pci.channel = 0xff; /* unused */
  1070. break;
  1071. default:
  1072. DBGC ( int13, "INT13 drive %02x unrecognised bus type %d\n",
  1073. int13->drive, desc->bus_type );
  1074. return -ENOTSUP;
  1075. }
  1076. /* Get EDD block device description */
  1077. if ( ( rc = edd_describe ( &int13->block, &dpi->interface_type,
  1078. &dpi->device_path ) ) != 0 ) {
  1079. DBGC ( int13, "INT13 drive %02x cannot identify block device: "
  1080. "%s\n", int13->drive, strerror ( rc ) );
  1081. return rc;
  1082. }
  1083. /* Fill in common fields and fix checksum */
  1084. dpi->key = EDD_DEVICE_PATH_INFO_KEY;
  1085. dpi->len = sizeof ( *dpi );
  1086. for ( i = 0 ; i < sizeof ( *dpi ) ; i++ )
  1087. sum += *( ( ( uint8_t * ) dpi ) + i );
  1088. dpi->checksum -= sum;
  1089. return 0;
  1090. }
  1091. /**
  1092. * INT 13, 48 - Get extended parameters
  1093. *
  1094. * @v int13 Emulated drive
  1095. * @v ds:si Drive parameter table
  1096. * @ret status Status code
  1097. */
  1098. static int int13_get_extended_parameters ( struct int13_drive *int13,
  1099. struct i386_all_regs *ix86 ) {
  1100. struct int13_disk_parameters params;
  1101. struct segoff address;
  1102. size_t len = sizeof ( params );
  1103. uint16_t bufsize;
  1104. int rc;
  1105. /* Get buffer size */
  1106. get_real ( bufsize, ix86->segs.ds,
  1107. ( ix86->regs.si + offsetof ( typeof ( params ), bufsize )));
  1108. DBGC2 ( int13, "Get extended drive parameters to %04x:%04x+%02x\n",
  1109. ix86->segs.ds, ix86->regs.si, bufsize );
  1110. /* Build drive parameters */
  1111. memset ( &params, 0, sizeof ( params ) );
  1112. params.flags = INT13_FL_DMA_TRANSPARENT;
  1113. if ( ( int13->cylinders < 1024 ) &&
  1114. ( int13_capacity ( int13 ) <= INT13_MAX_CHS_SECTORS ) ) {
  1115. params.flags |= INT13_FL_CHS_VALID;
  1116. }
  1117. params.cylinders = int13->cylinders;
  1118. params.heads = int13->heads;
  1119. params.sectors_per_track = int13->sectors_per_track;
  1120. params.sectors = int13_capacity ( int13 );
  1121. params.sector_size = int13_blksize ( int13 );
  1122. memset ( &params.dpte, 0xff, sizeof ( params.dpte ) );
  1123. if ( ( rc = int13_device_path_info ( int13, &params.dpi ) ) != 0 ) {
  1124. DBGC ( int13, "INT13 drive %02x could not provide device "
  1125. "path information: %s\n",
  1126. int13->drive, strerror ( rc ) );
  1127. len = offsetof ( typeof ( params ), dpi );
  1128. }
  1129. /* Calculate returned "buffer size" (which will be less than
  1130. * the length actually copied if device path information is
  1131. * present).
  1132. */
  1133. if ( bufsize < offsetof ( typeof ( params ), dpte ) )
  1134. return -INT13_STATUS_INVALID;
  1135. if ( bufsize < offsetof ( typeof ( params ), dpi ) ) {
  1136. params.bufsize = offsetof ( typeof ( params ), dpte );
  1137. } else {
  1138. params.bufsize = offsetof ( typeof ( params ), dpi );
  1139. }
  1140. DBGC ( int13, "INT 13 drive %02x described using extended "
  1141. "parameters:\n", int13->drive );
  1142. address.segment = ix86->segs.ds;
  1143. address.offset = ix86->regs.si;
  1144. DBGC_HDA ( int13, address, &params, len );
  1145. /* Return drive parameters */
  1146. if ( len > bufsize )
  1147. len = bufsize;
  1148. copy_to_real ( ix86->segs.ds, ix86->regs.si, &params, len );
  1149. return 0;
  1150. }
  1151. /**
  1152. * INT 13, 4b - Get status or terminate CD-ROM emulation
  1153. *
  1154. * @v int13 Emulated drive
  1155. * @v ds:si Specification packet
  1156. * @ret status Status code
  1157. */
  1158. static int int13_cdrom_status_terminate ( struct int13_drive *int13,
  1159. struct i386_all_regs *ix86 ) {
  1160. struct int13_cdrom_specification specification;
  1161. DBGC2 ( int13, "Get CD-ROM emulation status to %04x:%04x%s\n",
  1162. ix86->segs.ds, ix86->regs.si,
  1163. ( ix86->regs.al ? "" : " and terminate" ) );
  1164. /* Fail if we are not a CD-ROM */
  1165. if ( ! int13->is_cdrom ) {
  1166. DBGC ( int13, "INT13 drive %02x is not a CD-ROM\n",
  1167. int13->drive );
  1168. return -INT13_STATUS_INVALID;
  1169. }
  1170. /* Build specification packet */
  1171. memset ( &specification, 0, sizeof ( specification ) );
  1172. specification.size = sizeof ( specification );
  1173. specification.drive = int13->drive;
  1174. /* Return specification packet */
  1175. copy_to_real ( ix86->segs.ds, ix86->regs.si, &specification,
  1176. sizeof ( specification ) );
  1177. return 0;
  1178. }
  1179. /**
  1180. * INT 13, 4d - Read CD-ROM boot catalog
  1181. *
  1182. * @v int13 Emulated drive
  1183. * @v ds:si Command packet
  1184. * @ret status Status code
  1185. */
  1186. static int int13_cdrom_read_boot_catalog ( struct int13_drive *int13,
  1187. struct i386_all_regs *ix86 ) {
  1188. struct int13_cdrom_boot_catalog_command command;
  1189. int rc;
  1190. /* Read parameters from command packet */
  1191. copy_from_real ( &command, ix86->segs.ds, ix86->regs.si,
  1192. sizeof ( command ) );
  1193. DBGC2 ( int13, "Read CD-ROM boot catalog to %08x\n", command.buffer );
  1194. /* Fail if we have no boot catalog */
  1195. if ( ! int13->boot_catalog ) {
  1196. DBGC ( int13, "INT13 drive %02x has no boot catalog\n",
  1197. int13->drive );
  1198. return -INT13_STATUS_INVALID;
  1199. }
  1200. /* Read from boot catalog */
  1201. if ( ( rc = int13_rw ( int13, ( int13->boot_catalog + command.start ),
  1202. command.count, phys_to_user ( command.buffer ),
  1203. block_read ) ) != 0 ) {
  1204. DBGC ( int13, "INT13 drive %02x could not read boot catalog: "
  1205. "%s\n", int13->drive, strerror ( rc ) );
  1206. return -INT13_STATUS_READ_ERROR;
  1207. }
  1208. return 0;
  1209. }
  1210. /**
  1211. * INT 13 handler
  1212. *
  1213. */
  1214. static __asmcall void int13 ( struct i386_all_regs *ix86 ) {
  1215. int command = ix86->regs.ah;
  1216. unsigned int bios_drive = ix86->regs.dl;
  1217. struct int13_drive *int13;
  1218. int status;
  1219. /* Check BIOS hasn't killed off our drive */
  1220. int13_check_num_drives();
  1221. list_for_each_entry ( int13, &int13s, list ) {
  1222. if ( bios_drive != int13->drive ) {
  1223. /* Remap any accesses to this drive's natural number */
  1224. if ( bios_drive == int13->natural_drive ) {
  1225. DBGC2 ( int13, "INT13,%02x (%02x) remapped to "
  1226. "(%02x)\n", ix86->regs.ah,
  1227. bios_drive, int13->drive );
  1228. ix86->regs.dl = int13->drive;
  1229. return;
  1230. } else if ( ( ( bios_drive & 0x7f ) == 0x7f ) &&
  1231. ( command == INT13_CDROM_STATUS_TERMINATE )
  1232. && int13->is_cdrom ) {
  1233. /* Catch non-drive-specific CD-ROM calls */
  1234. } else {
  1235. continue;
  1236. }
  1237. }
  1238. DBGC2 ( int13, "INT13,%02x (%02x): ",
  1239. ix86->regs.ah, bios_drive );
  1240. switch ( command ) {
  1241. case INT13_RESET:
  1242. status = int13_reset ( int13, ix86 );
  1243. break;
  1244. case INT13_GET_LAST_STATUS:
  1245. status = int13_get_last_status ( int13, ix86 );
  1246. break;
  1247. case INT13_READ_SECTORS:
  1248. status = int13_read_sectors ( int13, ix86 );
  1249. break;
  1250. case INT13_WRITE_SECTORS:
  1251. status = int13_write_sectors ( int13, ix86 );
  1252. break;
  1253. case INT13_GET_PARAMETERS:
  1254. status = int13_get_parameters ( int13, ix86 );
  1255. break;
  1256. case INT13_GET_DISK_TYPE:
  1257. status = int13_get_disk_type ( int13, ix86 );
  1258. break;
  1259. case INT13_EXTENSION_CHECK:
  1260. status = int13_extension_check ( int13, ix86 );
  1261. break;
  1262. case INT13_EXTENDED_READ:
  1263. status = int13_extended_read ( int13, ix86 );
  1264. break;
  1265. case INT13_EXTENDED_WRITE:
  1266. status = int13_extended_write ( int13, ix86 );
  1267. break;
  1268. case INT13_EXTENDED_VERIFY:
  1269. status = int13_extended_verify ( int13, ix86 );
  1270. break;
  1271. case INT13_EXTENDED_SEEK:
  1272. status = int13_extended_seek ( int13, ix86 );
  1273. break;
  1274. case INT13_GET_EXTENDED_PARAMETERS:
  1275. status = int13_get_extended_parameters ( int13, ix86 );
  1276. break;
  1277. case INT13_CDROM_STATUS_TERMINATE:
  1278. status = int13_cdrom_status_terminate ( int13, ix86 );
  1279. break;
  1280. case INT13_CDROM_READ_BOOT_CATALOG:
  1281. status = int13_cdrom_read_boot_catalog ( int13, ix86 );
  1282. break;
  1283. default:
  1284. DBGC2 ( int13, "*** Unrecognised INT13 ***\n" );
  1285. status = -INT13_STATUS_INVALID;
  1286. break;
  1287. }
  1288. /* Store status for INT 13,01 */
  1289. int13->last_status = status;
  1290. /* Negative status indicates an error */
  1291. if ( status < 0 ) {
  1292. status = -status;
  1293. DBGC ( int13, "INT13,%02x (%02x) failed with status "
  1294. "%02x\n", ix86->regs.ah, int13->drive, status );
  1295. } else {
  1296. ix86->flags &= ~CF;
  1297. }
  1298. ix86->regs.ah = status;
  1299. /* Set OF to indicate to wrapper not to chain this call */
  1300. ix86->flags |= OF;
  1301. return;
  1302. }
  1303. }
  1304. /**
  1305. * Hook INT 13 handler
  1306. *
  1307. */
  1308. static void int13_hook_vector ( void ) {
  1309. /* Assembly wrapper to call int13(). int13() sets OF if we
  1310. * should not chain to the previous handler. (The wrapper
  1311. * clears CF and OF before calling int13()).
  1312. */
  1313. __asm__ __volatile__ (
  1314. TEXT16_CODE ( "\nint13_wrapper:\n\t"
  1315. /* Preserve %ax and %dx for future reference */
  1316. "pushw %%bp\n\t"
  1317. "movw %%sp, %%bp\n\t"
  1318. "pushw %%ax\n\t"
  1319. "pushw %%dx\n\t"
  1320. /* Clear OF, set CF, call int13() */
  1321. "orb $0, %%al\n\t"
  1322. "stc\n\t"
  1323. "pushl %0\n\t"
  1324. "call prot_call\n\t"
  1325. /* Chain if OF not set */
  1326. "jo 1f\n\t"
  1327. "pushfw\n\t"
  1328. "lcall *%%cs:int13_vector\n\t"
  1329. "\n1:\n\t"
  1330. /* Overwrite flags for iret */
  1331. "pushfw\n\t"
  1332. "popw 6(%%bp)\n\t"
  1333. /* Fix up %dl:
  1334. *
  1335. * INT 13,15 : do nothing if hard disk
  1336. * INT 13,08 : load with number of drives
  1337. * all others: restore original value
  1338. */
  1339. "cmpb $0x15, -1(%%bp)\n\t"
  1340. "jne 2f\n\t"
  1341. "testb $0x80, -4(%%bp)\n\t"
  1342. "jnz 3f\n\t"
  1343. "\n2:\n\t"
  1344. "movb -4(%%bp), %%dl\n\t"
  1345. "cmpb $0x08, -1(%%bp)\n\t"
  1346. "jne 3f\n\t"
  1347. "testb $0x80, %%dl\n\t"
  1348. "movb %%cs:num_drives, %%dl\n\t"
  1349. "jnz 3f\n\t"
  1350. "movb %%cs:num_fdds, %%dl\n\t"
  1351. /* Return */
  1352. "\n3:\n\t"
  1353. "movw %%bp, %%sp\n\t"
  1354. "popw %%bp\n\t"
  1355. "iret\n\t" )
  1356. : : "i" ( int13 ) );
  1357. hook_bios_interrupt ( 0x13, ( intptr_t ) int13_wrapper, &int13_vector );
  1358. }
  1359. /**
  1360. * Unhook INT 13 handler
  1361. */
  1362. static void int13_unhook_vector ( void ) {
  1363. unhook_bios_interrupt ( 0x13, ( intptr_t ) int13_wrapper,
  1364. &int13_vector );
  1365. }
  1366. /**
  1367. * Check INT13 emulated drive flow control window
  1368. *
  1369. * @v int13 Emulated drive
  1370. */
  1371. static size_t int13_block_window ( struct int13_drive *int13 __unused ) {
  1372. /* We are never ready to receive data via this interface.
  1373. * This prevents objects that support both block and stream
  1374. * interfaces from attempting to send us stream data.
  1375. */
  1376. return 0;
  1377. }
  1378. /**
  1379. * Handle INT 13 emulated drive underlying block device closing
  1380. *
  1381. * @v int13 Emulated drive
  1382. * @v rc Reason for close
  1383. */
  1384. static void int13_block_close ( struct int13_drive *int13, int rc ) {
  1385. /* Any closing is an error from our point of view */
  1386. if ( rc == 0 )
  1387. rc = -ENOTCONN;
  1388. DBGC ( int13, "INT13 drive %02x went away: %s\n",
  1389. int13->drive, strerror ( rc ) );
  1390. /* Record block device error code */
  1391. int13->block_rc = rc;
  1392. /* Shut down interfaces */
  1393. intf_restart ( &int13->block, rc );
  1394. }
  1395. /** INT 13 drive interface operations */
  1396. static struct interface_operation int13_block_op[] = {
  1397. INTF_OP ( xfer_window, struct int13_drive *, int13_block_window ),
  1398. INTF_OP ( intf_close, struct int13_drive *, int13_block_close ),
  1399. };
  1400. /** INT 13 drive interface descriptor */
  1401. static struct interface_descriptor int13_block_desc =
  1402. INTF_DESC ( struct int13_drive, block, int13_block_op );
  1403. /**
  1404. * Free INT 13 emulated drive
  1405. *
  1406. * @v refcnt Reference count
  1407. */
  1408. static void int13_free ( struct refcnt *refcnt ) {
  1409. struct int13_drive *int13 =
  1410. container_of ( refcnt, struct int13_drive, refcnt );
  1411. uri_put ( int13->uri );
  1412. free ( int13 );
  1413. }
  1414. /**
  1415. * Hook INT 13 emulated drive
  1416. *
  1417. * @v uri URI
  1418. * @v drive Drive number
  1419. * @ret rc Return status code
  1420. *
  1421. * Registers the drive with the INT 13 emulation subsystem, and hooks
  1422. * the INT 13 interrupt vector (if not already hooked).
  1423. */
  1424. static int int13_hook ( struct uri *uri, unsigned int drive ) {
  1425. struct int13_drive *int13;
  1426. unsigned int natural_drive;
  1427. void *scratch;
  1428. int rc;
  1429. /* Calculate natural drive number */
  1430. int13_sync_num_drives();
  1431. natural_drive = ( ( drive & 0x80 ) ? ( num_drives | 0x80 ) : num_fdds );
  1432. /* Check that drive number is not in use */
  1433. list_for_each_entry ( int13, &int13s, list ) {
  1434. if ( int13->drive == drive ) {
  1435. rc = -EADDRINUSE;
  1436. goto err_in_use;
  1437. }
  1438. }
  1439. /* Allocate and initialise structure */
  1440. int13 = zalloc ( sizeof ( *int13 ) );
  1441. if ( ! int13 ) {
  1442. rc = -ENOMEM;
  1443. goto err_zalloc;
  1444. }
  1445. ref_init ( &int13->refcnt, int13_free );
  1446. intf_init ( &int13->block, &int13_block_desc, &int13->refcnt );
  1447. int13->uri = uri_get ( uri );
  1448. int13->drive = drive;
  1449. int13->natural_drive = natural_drive;
  1450. /* Open block device interface */
  1451. if ( ( rc = int13_reopen_block ( int13 ) ) != 0 )
  1452. goto err_reopen_block;
  1453. /* Read device capacity */
  1454. if ( ( rc = int13_read_capacity ( int13 ) ) != 0 )
  1455. goto err_read_capacity;
  1456. /* Allocate scratch area */
  1457. scratch = malloc ( int13_blksize ( int13 ) );
  1458. if ( ! scratch )
  1459. goto err_alloc_scratch;
  1460. /* Parse parameters, if present */
  1461. if ( ( rc = int13_parse_iso9660 ( int13, scratch ) ) != 0 )
  1462. goto err_parse_iso9660;
  1463. /* Give drive a default geometry */
  1464. if ( ( rc = int13_guess_geometry ( int13, scratch ) ) != 0 )
  1465. goto err_guess_geometry;
  1466. DBGC ( int13, "INT13 drive %02x (naturally %02x) registered with C/H/S "
  1467. "geometry %d/%d/%d\n", int13->drive, int13->natural_drive,
  1468. int13->cylinders, int13->heads, int13->sectors_per_track );
  1469. /* Hook INT 13 vector if not already hooked */
  1470. if ( list_empty ( &int13s ) ) {
  1471. int13_hook_vector();
  1472. devices_get();
  1473. }
  1474. /* Add to list of emulated drives */
  1475. list_add ( &int13->list, &int13s );
  1476. /* Update BIOS drive count */
  1477. int13_sync_num_drives();
  1478. free ( scratch );
  1479. return 0;
  1480. err_guess_geometry:
  1481. err_parse_iso9660:
  1482. free ( scratch );
  1483. err_alloc_scratch:
  1484. err_read_capacity:
  1485. err_reopen_block:
  1486. intf_shutdown ( &int13->block, rc );
  1487. ref_put ( &int13->refcnt );
  1488. err_zalloc:
  1489. err_in_use:
  1490. return rc;
  1491. }
  1492. /**
  1493. * Find INT 13 emulated drive by drive number
  1494. *
  1495. * @v drive Drive number
  1496. * @ret int13 Emulated drive, or NULL
  1497. */
  1498. static struct int13_drive * int13_find ( unsigned int drive ) {
  1499. struct int13_drive *int13;
  1500. list_for_each_entry ( int13, &int13s, list ) {
  1501. if ( int13->drive == drive )
  1502. return int13;
  1503. }
  1504. return NULL;
  1505. }
  1506. /**
  1507. * Unhook INT 13 emulated drive
  1508. *
  1509. * @v drive Drive number
  1510. *
  1511. * Unregisters the drive from the INT 13 emulation subsystem. If this
  1512. * is the last emulated drive, the INT 13 vector is unhooked (if
  1513. * possible).
  1514. */
  1515. static void int13_unhook ( unsigned int drive ) {
  1516. struct int13_drive *int13;
  1517. /* Find drive */
  1518. int13 = int13_find ( drive );
  1519. if ( ! int13 ) {
  1520. DBG ( "INT13 cannot find emulated drive %02x\n", drive );
  1521. return;
  1522. }
  1523. /* Shut down interfaces */
  1524. intf_shutdown ( &int13->block, 0 );
  1525. /* Remove from list of emulated drives */
  1526. list_del ( &int13->list );
  1527. /* Should adjust BIOS drive count, but it's difficult
  1528. * to do so reliably.
  1529. */
  1530. DBGC ( int13, "INT13 drive %02x unregistered\n", int13->drive );
  1531. /* Unhook INT 13 vector if no more drives */
  1532. if ( list_empty ( &int13s ) ) {
  1533. devices_put();
  1534. int13_unhook_vector();
  1535. }
  1536. /* Drop list's reference to drive */
  1537. ref_put ( &int13->refcnt );
  1538. }
  1539. /**
  1540. * Load and verify master boot record from INT 13 drive
  1541. *
  1542. * @v drive Drive number
  1543. * @v address Boot code address to fill in
  1544. * @ret rc Return status code
  1545. */
  1546. static int int13_load_mbr ( unsigned int drive, struct segoff *address ) {
  1547. uint8_t status;
  1548. int discard_b, discard_c, discard_d;
  1549. uint16_t magic;
  1550. /* Use INT 13, 02 to read the MBR */
  1551. address->segment = 0;
  1552. address->offset = 0x7c00;
  1553. __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
  1554. "pushl %%ebx\n\t"
  1555. "popw %%bx\n\t"
  1556. "popw %%es\n\t"
  1557. "stc\n\t"
  1558. "sti\n\t"
  1559. "int $0x13\n\t"
  1560. "sti\n\t" /* BIOS bugs */
  1561. "jc 1f\n\t"
  1562. "xorw %%ax, %%ax\n\t"
  1563. "\n1:\n\t"
  1564. "popw %%es\n\t" )
  1565. : "=a" ( status ), "=b" ( discard_b ),
  1566. "=c" ( discard_c ), "=d" ( discard_d )
  1567. : "a" ( 0x0201 ), "b" ( *address ),
  1568. "c" ( 1 ), "d" ( drive ) );
  1569. if ( status ) {
  1570. DBG ( "INT13 drive %02x could not read MBR (status %02x)\n",
  1571. drive, status );
  1572. return -EIO;
  1573. }
  1574. /* Check magic signature */
  1575. get_real ( magic, address->segment,
  1576. ( address->offset +
  1577. offsetof ( struct master_boot_record, magic ) ) );
  1578. if ( magic != INT13_MBR_MAGIC ) {
  1579. DBG ( "INT13 drive %02x does not contain a valid MBR\n",
  1580. drive );
  1581. return -ENOEXEC;
  1582. }
  1583. return 0;
  1584. }
  1585. /** El Torito boot catalog command packet */
  1586. static struct int13_cdrom_boot_catalog_command __data16 ( eltorito_cmd ) = {
  1587. .size = sizeof ( struct int13_cdrom_boot_catalog_command ),
  1588. .count = 1,
  1589. .buffer = 0x7c00,
  1590. .start = 0,
  1591. };
  1592. #define eltorito_cmd __use_data16 ( eltorito_cmd )
  1593. /** El Torito disk address packet */
  1594. static struct int13_disk_address __bss16 ( eltorito_address );
  1595. #define eltorito_address __use_data16 ( eltorito_address )
  1596. /**
  1597. * Load and verify El Torito boot record from INT 13 drive
  1598. *
  1599. * @v drive Drive number
  1600. * @v address Boot code address to fill in
  1601. * @ret rc Return status code
  1602. */
  1603. static int int13_load_eltorito ( unsigned int drive, struct segoff *address ) {
  1604. struct {
  1605. struct eltorito_validation_entry valid;
  1606. struct eltorito_boot_entry boot;
  1607. } __attribute__ (( packed )) catalog;
  1608. uint8_t status;
  1609. /* Use INT 13, 4d to read the boot catalog */
  1610. __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
  1611. "sti\n\t"
  1612. "int $0x13\n\t"
  1613. "sti\n\t" /* BIOS bugs */
  1614. "jc 1f\n\t"
  1615. "xorw %%ax, %%ax\n\t"
  1616. "\n1:\n\t" )
  1617. : "=a" ( status )
  1618. : "a" ( 0x4d00 ), "d" ( drive ),
  1619. "S" ( __from_data16 ( &eltorito_cmd ) ) );
  1620. if ( status ) {
  1621. DBG ( "INT13 drive %02x could not read El Torito boot catalog "
  1622. "(status %02x)\n", drive, status );
  1623. return -EIO;
  1624. }
  1625. copy_from_user ( &catalog, phys_to_user ( eltorito_cmd.buffer ), 0,
  1626. sizeof ( catalog ) );
  1627. /* Sanity checks */
  1628. if ( catalog.valid.platform_id != ELTORITO_PLATFORM_X86 ) {
  1629. DBG ( "INT13 drive %02x El Torito specifies unknown platform "
  1630. "%02x\n", drive, catalog.valid.platform_id );
  1631. return -ENOEXEC;
  1632. }
  1633. if ( catalog.boot.indicator != ELTORITO_BOOTABLE ) {
  1634. DBG ( "INT13 drive %02x El Torito is not bootable\n", drive );
  1635. return -ENOEXEC;
  1636. }
  1637. if ( catalog.boot.media_type != ELTORITO_NO_EMULATION ) {
  1638. DBG ( "INT13 drive %02x El Torito requires emulation "
  1639. "type %02x\n", drive, catalog.boot.media_type );
  1640. return -ENOTSUP;
  1641. }
  1642. DBG ( "INT13 drive %02x El Torito boot image at LBA %08x (count %d)\n",
  1643. drive, catalog.boot.start, catalog.boot.length );
  1644. address->segment = ( catalog.boot.load_segment ?
  1645. catalog.boot.load_segment : 0x7c0 );
  1646. address->offset = 0;
  1647. DBG ( "INT13 drive %02x El Torito boot image loads at %04x:%04x\n",
  1648. drive, address->segment, address->offset );
  1649. /* Use INT 13, 42 to read the boot image */
  1650. eltorito_address.bufsize =
  1651. offsetof ( typeof ( eltorito_address ), buffer_phys );
  1652. eltorito_address.count = catalog.boot.length;
  1653. eltorito_address.buffer = *address;
  1654. eltorito_address.lba = catalog.boot.start;
  1655. __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
  1656. "sti\n\t"
  1657. "int $0x13\n\t"
  1658. "sti\n\t" /* BIOS bugs */
  1659. "jc 1f\n\t"
  1660. "xorw %%ax, %%ax\n\t"
  1661. "\n1:\n\t" )
  1662. : "=a" ( status )
  1663. : "a" ( 0x4200 ), "d" ( drive ),
  1664. "S" ( __from_data16 ( &eltorito_address ) ) );
  1665. if ( status ) {
  1666. DBG ( "INT13 drive %02x could not read El Torito boot image "
  1667. "(status %02x)\n", drive, status );
  1668. return -EIO;
  1669. }
  1670. return 0;
  1671. }
  1672. /**
  1673. * Attempt to boot from an INT 13 drive
  1674. *
  1675. * @v drive Drive number
  1676. * @ret rc Return status code
  1677. *
  1678. * This boots from the specified INT 13 drive by loading the Master
  1679. * Boot Record to 0000:7c00 and jumping to it. INT 18 is hooked to
  1680. * capture an attempt by the MBR to boot the next device. (This is
  1681. * the closest thing to a return path from an MBR).
  1682. *
  1683. * Note that this function can never return success, by definition.
  1684. */
  1685. static int int13_boot ( unsigned int drive ) {
  1686. struct memory_map memmap;
  1687. struct segoff address;
  1688. int rc;
  1689. /* Look for a usable boot sector */
  1690. if ( ( ( rc = int13_load_mbr ( drive, &address ) ) != 0 ) &&
  1691. ( ( rc = int13_load_eltorito ( drive, &address ) ) != 0 ) )
  1692. return rc;
  1693. /* Dump out memory map prior to boot, if memmap debugging is
  1694. * enabled. Not required for program flow, but we have so
  1695. * many problems that turn out to be memory-map related that
  1696. * it's worth doing.
  1697. */
  1698. get_memmap ( &memmap );
  1699. /* Jump to boot sector */
  1700. if ( ( rc = call_bootsector ( address.segment, address.offset,
  1701. drive ) ) != 0 ) {
  1702. DBG ( "INT13 drive %02x boot returned: %s\n",
  1703. drive, strerror ( rc ) );
  1704. return rc;
  1705. }
  1706. return -ECANCELED; /* -EIMPOSSIBLE */
  1707. }
  1708. /** A boot firmware table generated by iPXE */
  1709. union xbft_table {
  1710. /** ACPI header */
  1711. struct acpi_description_header acpi;
  1712. /** Padding */
  1713. char pad[768];
  1714. };
  1715. /** The boot firmware table generated by iPXE */
  1716. static union xbft_table __bss16 ( xbftab ) __attribute__ (( aligned ( 16 ) ));
  1717. #define xbftab __use_data16 ( xbftab )
  1718. /**
  1719. * Describe INT 13 emulated drive for SAN-booted operating system
  1720. *
  1721. * @v drive Drive number
  1722. * @ret rc Return status code
  1723. */
  1724. static int int13_describe ( unsigned int drive ) {
  1725. struct int13_drive *int13;
  1726. struct segoff xbft_address;
  1727. int rc;
  1728. /* Find drive */
  1729. int13 = int13_find ( drive );
  1730. if ( ! int13 ) {
  1731. DBG ( "INT13 cannot find emulated drive %02x\n", drive );
  1732. return -ENODEV;
  1733. }
  1734. /* Reopen block device if necessary */
  1735. if ( ( int13->block_rc != 0 ) &&
  1736. ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) )
  1737. return rc;
  1738. /* Clear table */
  1739. memset ( &xbftab, 0, sizeof ( xbftab ) );
  1740. /* Fill in common parameters */
  1741. strncpy ( xbftab.acpi.oem_id, "FENSYS",
  1742. sizeof ( xbftab.acpi.oem_id ) );
  1743. strncpy ( xbftab.acpi.oem_table_id, "iPXE",
  1744. sizeof ( xbftab.acpi.oem_table_id ) );
  1745. /* Fill in remaining parameters */
  1746. if ( ( rc = acpi_describe ( &int13->block, &xbftab.acpi,
  1747. sizeof ( xbftab ) ) ) != 0 ) {
  1748. DBGC ( int13, "INT13 drive %02x could not create ACPI "
  1749. "description: %s\n", int13->drive, strerror ( rc ) );
  1750. return rc;
  1751. }
  1752. /* Fix up ACPI checksum */
  1753. acpi_fix_checksum ( &xbftab.acpi );
  1754. xbft_address.segment = rm_ds;
  1755. xbft_address.offset = __from_data16 ( &xbftab );
  1756. DBGC ( int13, "INT13 drive %02x described using boot firmware "
  1757. "table:\n", int13->drive );
  1758. DBGC_HDA ( int13, xbft_address, &xbftab,
  1759. le32_to_cpu ( xbftab.acpi.length ) );
  1760. return 0;
  1761. }
  1762. PROVIDE_SANBOOT_INLINE ( pcbios, san_default_drive );
  1763. PROVIDE_SANBOOT ( pcbios, san_hook, int13_hook );
  1764. PROVIDE_SANBOOT ( pcbios, san_unhook, int13_unhook );
  1765. PROVIDE_SANBOOT ( pcbios, san_boot, int13_boot );
  1766. PROVIDE_SANBOOT ( pcbios, san_describe, int13_describe );