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

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