Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

int13.c 55KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  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. VIRT_CALL ( int13 )
  1324. /* Chain if OF not set */
  1325. "jo 1f\n\t"
  1326. "pushfw\n\t"
  1327. "lcall *%%cs:int13_vector\n\t"
  1328. "\n1:\n\t"
  1329. /* Overwrite flags for iret */
  1330. "pushfw\n\t"
  1331. "popw 6(%%bp)\n\t"
  1332. /* Fix up %dl:
  1333. *
  1334. * INT 13,15 : do nothing if hard disk
  1335. * INT 13,08 : load with number of drives
  1336. * all others: restore original value
  1337. */
  1338. "cmpb $0x15, -1(%%bp)\n\t"
  1339. "jne 2f\n\t"
  1340. "testb $0x80, -4(%%bp)\n\t"
  1341. "jnz 3f\n\t"
  1342. "\n2:\n\t"
  1343. "movb -4(%%bp), %%dl\n\t"
  1344. "cmpb $0x08, -1(%%bp)\n\t"
  1345. "jne 3f\n\t"
  1346. "testb $0x80, %%dl\n\t"
  1347. "movb %%cs:num_drives, %%dl\n\t"
  1348. "jnz 3f\n\t"
  1349. "movb %%cs:num_fdds, %%dl\n\t"
  1350. /* Return */
  1351. "\n3:\n\t"
  1352. "movw %%bp, %%sp\n\t"
  1353. "popw %%bp\n\t"
  1354. "iret\n\t" ) : : );
  1355. hook_bios_interrupt ( 0x13, ( intptr_t ) int13_wrapper, &int13_vector );
  1356. }
  1357. /**
  1358. * Unhook INT 13 handler
  1359. */
  1360. static void int13_unhook_vector ( void ) {
  1361. unhook_bios_interrupt ( 0x13, ( intptr_t ) int13_wrapper,
  1362. &int13_vector );
  1363. }
  1364. /**
  1365. * Check INT13 emulated drive flow control window
  1366. *
  1367. * @v int13 Emulated drive
  1368. */
  1369. static size_t int13_block_window ( struct int13_drive *int13 __unused ) {
  1370. /* We are never ready to receive data via this interface.
  1371. * This prevents objects that support both block and stream
  1372. * interfaces from attempting to send us stream data.
  1373. */
  1374. return 0;
  1375. }
  1376. /**
  1377. * Handle INT 13 emulated drive underlying block device closing
  1378. *
  1379. * @v int13 Emulated drive
  1380. * @v rc Reason for close
  1381. */
  1382. static void int13_block_close ( struct int13_drive *int13, int rc ) {
  1383. /* Any closing is an error from our point of view */
  1384. if ( rc == 0 )
  1385. rc = -ENOTCONN;
  1386. DBGC ( int13, "INT13 drive %02x went away: %s\n",
  1387. int13->drive, strerror ( rc ) );
  1388. /* Record block device error code */
  1389. int13->block_rc = rc;
  1390. /* Shut down interfaces */
  1391. intf_restart ( &int13->block, rc );
  1392. }
  1393. /** INT 13 drive interface operations */
  1394. static struct interface_operation int13_block_op[] = {
  1395. INTF_OP ( xfer_window, struct int13_drive *, int13_block_window ),
  1396. INTF_OP ( intf_close, struct int13_drive *, int13_block_close ),
  1397. };
  1398. /** INT 13 drive interface descriptor */
  1399. static struct interface_descriptor int13_block_desc =
  1400. INTF_DESC ( struct int13_drive, block, int13_block_op );
  1401. /**
  1402. * Free INT 13 emulated drive
  1403. *
  1404. * @v refcnt Reference count
  1405. */
  1406. static void int13_free ( struct refcnt *refcnt ) {
  1407. struct int13_drive *int13 =
  1408. container_of ( refcnt, struct int13_drive, refcnt );
  1409. uri_put ( int13->uri );
  1410. free ( int13 );
  1411. }
  1412. /**
  1413. * Hook INT 13 emulated drive
  1414. *
  1415. * @v uri URI
  1416. * @v drive Drive number
  1417. * @ret rc Return status code
  1418. *
  1419. * Registers the drive with the INT 13 emulation subsystem, and hooks
  1420. * the INT 13 interrupt vector (if not already hooked).
  1421. */
  1422. static int int13_hook ( struct uri *uri, unsigned int drive ) {
  1423. struct int13_drive *int13;
  1424. unsigned int natural_drive;
  1425. void *scratch;
  1426. int rc;
  1427. /* Calculate natural drive number */
  1428. int13_sync_num_drives();
  1429. natural_drive = ( ( drive & 0x80 ) ? ( num_drives | 0x80 ) : num_fdds );
  1430. /* Check that drive number is not in use */
  1431. list_for_each_entry ( int13, &int13s, list ) {
  1432. if ( int13->drive == drive ) {
  1433. rc = -EADDRINUSE;
  1434. goto err_in_use;
  1435. }
  1436. }
  1437. /* Allocate and initialise structure */
  1438. int13 = zalloc ( sizeof ( *int13 ) );
  1439. if ( ! int13 ) {
  1440. rc = -ENOMEM;
  1441. goto err_zalloc;
  1442. }
  1443. ref_init ( &int13->refcnt, int13_free );
  1444. intf_init ( &int13->block, &int13_block_desc, &int13->refcnt );
  1445. int13->uri = uri_get ( uri );
  1446. int13->drive = drive;
  1447. int13->natural_drive = natural_drive;
  1448. /* Open block device interface */
  1449. if ( ( rc = int13_reopen_block ( int13 ) ) != 0 )
  1450. goto err_reopen_block;
  1451. /* Read device capacity */
  1452. if ( ( rc = int13_read_capacity ( int13 ) ) != 0 )
  1453. goto err_read_capacity;
  1454. /* Allocate scratch area */
  1455. scratch = malloc ( int13_blksize ( int13 ) );
  1456. if ( ! scratch )
  1457. goto err_alloc_scratch;
  1458. /* Parse parameters, if present */
  1459. if ( ( rc = int13_parse_iso9660 ( int13, scratch ) ) != 0 )
  1460. goto err_parse_iso9660;
  1461. /* Give drive a default geometry */
  1462. if ( ( rc = int13_guess_geometry ( int13, scratch ) ) != 0 )
  1463. goto err_guess_geometry;
  1464. DBGC ( int13, "INT13 drive %02x (naturally %02x) registered with C/H/S "
  1465. "geometry %d/%d/%d\n", int13->drive, int13->natural_drive,
  1466. int13->cylinders, int13->heads, int13->sectors_per_track );
  1467. /* Hook INT 13 vector if not already hooked */
  1468. if ( list_empty ( &int13s ) ) {
  1469. int13_hook_vector();
  1470. devices_get();
  1471. }
  1472. /* Add to list of emulated drives */
  1473. list_add ( &int13->list, &int13s );
  1474. /* Update BIOS drive count */
  1475. int13_sync_num_drives();
  1476. free ( scratch );
  1477. return 0;
  1478. err_guess_geometry:
  1479. err_parse_iso9660:
  1480. free ( scratch );
  1481. err_alloc_scratch:
  1482. err_read_capacity:
  1483. err_reopen_block:
  1484. intf_shutdown ( &int13->block, rc );
  1485. ref_put ( &int13->refcnt );
  1486. err_zalloc:
  1487. err_in_use:
  1488. return rc;
  1489. }
  1490. /**
  1491. * Find INT 13 emulated drive by drive number
  1492. *
  1493. * @v drive Drive number
  1494. * @ret int13 Emulated drive, or NULL
  1495. */
  1496. static struct int13_drive * int13_find ( unsigned int drive ) {
  1497. struct int13_drive *int13;
  1498. list_for_each_entry ( int13, &int13s, list ) {
  1499. if ( int13->drive == drive )
  1500. return int13;
  1501. }
  1502. return NULL;
  1503. }
  1504. /**
  1505. * Unhook INT 13 emulated drive
  1506. *
  1507. * @v drive Drive number
  1508. *
  1509. * Unregisters the drive from the INT 13 emulation subsystem. If this
  1510. * is the last emulated drive, the INT 13 vector is unhooked (if
  1511. * possible).
  1512. */
  1513. static void int13_unhook ( unsigned int drive ) {
  1514. struct int13_drive *int13;
  1515. /* Find drive */
  1516. int13 = int13_find ( drive );
  1517. if ( ! int13 ) {
  1518. DBG ( "INT13 cannot find emulated drive %02x\n", drive );
  1519. return;
  1520. }
  1521. /* Shut down interfaces */
  1522. intf_shutdown ( &int13->block, 0 );
  1523. /* Remove from list of emulated drives */
  1524. list_del ( &int13->list );
  1525. /* Should adjust BIOS drive count, but it's difficult
  1526. * to do so reliably.
  1527. */
  1528. DBGC ( int13, "INT13 drive %02x unregistered\n", int13->drive );
  1529. /* Unhook INT 13 vector if no more drives */
  1530. if ( list_empty ( &int13s ) ) {
  1531. devices_put();
  1532. int13_unhook_vector();
  1533. }
  1534. /* Drop list's reference to drive */
  1535. ref_put ( &int13->refcnt );
  1536. }
  1537. /**
  1538. * Load and verify master boot record from INT 13 drive
  1539. *
  1540. * @v drive Drive number
  1541. * @v address Boot code address to fill in
  1542. * @ret rc Return status code
  1543. */
  1544. static int int13_load_mbr ( unsigned int drive, struct segoff *address ) {
  1545. uint8_t status;
  1546. int discard_b, discard_c, discard_d;
  1547. uint16_t magic;
  1548. /* Use INT 13, 02 to read the MBR */
  1549. address->segment = 0;
  1550. address->offset = 0x7c00;
  1551. __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
  1552. "pushl %%ebx\n\t"
  1553. "popw %%bx\n\t"
  1554. "popw %%es\n\t"
  1555. "stc\n\t"
  1556. "sti\n\t"
  1557. "int $0x13\n\t"
  1558. "sti\n\t" /* BIOS bugs */
  1559. "jc 1f\n\t"
  1560. "xorw %%ax, %%ax\n\t"
  1561. "\n1:\n\t"
  1562. "popw %%es\n\t" )
  1563. : "=a" ( status ), "=b" ( discard_b ),
  1564. "=c" ( discard_c ), "=d" ( discard_d )
  1565. : "a" ( 0x0201 ), "b" ( *address ),
  1566. "c" ( 1 ), "d" ( drive ) );
  1567. if ( status ) {
  1568. DBG ( "INT13 drive %02x could not read MBR (status %02x)\n",
  1569. drive, status );
  1570. return -EIO;
  1571. }
  1572. /* Check magic signature */
  1573. get_real ( magic, address->segment,
  1574. ( address->offset +
  1575. offsetof ( struct master_boot_record, magic ) ) );
  1576. if ( magic != INT13_MBR_MAGIC ) {
  1577. DBG ( "INT13 drive %02x does not contain a valid MBR\n",
  1578. drive );
  1579. return -ENOEXEC;
  1580. }
  1581. return 0;
  1582. }
  1583. /** El Torito boot catalog command packet */
  1584. static struct int13_cdrom_boot_catalog_command __data16 ( eltorito_cmd ) = {
  1585. .size = sizeof ( struct int13_cdrom_boot_catalog_command ),
  1586. .count = 1,
  1587. .buffer = 0x7c00,
  1588. .start = 0,
  1589. };
  1590. #define eltorito_cmd __use_data16 ( eltorito_cmd )
  1591. /** El Torito disk address packet */
  1592. static struct int13_disk_address __bss16 ( eltorito_address );
  1593. #define eltorito_address __use_data16 ( eltorito_address )
  1594. /**
  1595. * Load and verify El Torito boot record from INT 13 drive
  1596. *
  1597. * @v drive Drive number
  1598. * @v address Boot code address to fill in
  1599. * @ret rc Return status code
  1600. */
  1601. static int int13_load_eltorito ( unsigned int drive, struct segoff *address ) {
  1602. struct {
  1603. struct eltorito_validation_entry valid;
  1604. struct eltorito_boot_entry boot;
  1605. } __attribute__ (( packed )) catalog;
  1606. uint8_t status;
  1607. /* Use INT 13, 4d to read the boot catalog */
  1608. __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
  1609. "sti\n\t"
  1610. "int $0x13\n\t"
  1611. "sti\n\t" /* BIOS bugs */
  1612. "jc 1f\n\t"
  1613. "xorw %%ax, %%ax\n\t"
  1614. "\n1:\n\t" )
  1615. : "=a" ( status )
  1616. : "a" ( 0x4d00 ), "d" ( drive ),
  1617. "S" ( __from_data16 ( &eltorito_cmd ) ) );
  1618. if ( status ) {
  1619. DBG ( "INT13 drive %02x could not read El Torito boot catalog "
  1620. "(status %02x)\n", drive, status );
  1621. return -EIO;
  1622. }
  1623. copy_from_user ( &catalog, phys_to_user ( eltorito_cmd.buffer ), 0,
  1624. sizeof ( catalog ) );
  1625. /* Sanity checks */
  1626. if ( catalog.valid.platform_id != ELTORITO_PLATFORM_X86 ) {
  1627. DBG ( "INT13 drive %02x El Torito specifies unknown platform "
  1628. "%02x\n", drive, catalog.valid.platform_id );
  1629. return -ENOEXEC;
  1630. }
  1631. if ( catalog.boot.indicator != ELTORITO_BOOTABLE ) {
  1632. DBG ( "INT13 drive %02x El Torito is not bootable\n", drive );
  1633. return -ENOEXEC;
  1634. }
  1635. if ( catalog.boot.media_type != ELTORITO_NO_EMULATION ) {
  1636. DBG ( "INT13 drive %02x El Torito requires emulation "
  1637. "type %02x\n", drive, catalog.boot.media_type );
  1638. return -ENOTSUP;
  1639. }
  1640. DBG ( "INT13 drive %02x El Torito boot image at LBA %08x (count %d)\n",
  1641. drive, catalog.boot.start, catalog.boot.length );
  1642. address->segment = ( catalog.boot.load_segment ?
  1643. catalog.boot.load_segment : 0x7c0 );
  1644. address->offset = 0;
  1645. DBG ( "INT13 drive %02x El Torito boot image loads at %04x:%04x\n",
  1646. drive, address->segment, address->offset );
  1647. /* Use INT 13, 42 to read the boot image */
  1648. eltorito_address.bufsize =
  1649. offsetof ( typeof ( eltorito_address ), buffer_phys );
  1650. eltorito_address.count = catalog.boot.length;
  1651. eltorito_address.buffer = *address;
  1652. eltorito_address.lba = catalog.boot.start;
  1653. __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
  1654. "sti\n\t"
  1655. "int $0x13\n\t"
  1656. "sti\n\t" /* BIOS bugs */
  1657. "jc 1f\n\t"
  1658. "xorw %%ax, %%ax\n\t"
  1659. "\n1:\n\t" )
  1660. : "=a" ( status )
  1661. : "a" ( 0x4200 ), "d" ( drive ),
  1662. "S" ( __from_data16 ( &eltorito_address ) ) );
  1663. if ( status ) {
  1664. DBG ( "INT13 drive %02x could not read El Torito boot image "
  1665. "(status %02x)\n", drive, status );
  1666. return -EIO;
  1667. }
  1668. return 0;
  1669. }
  1670. /**
  1671. * Attempt to boot from an INT 13 drive
  1672. *
  1673. * @v drive Drive number
  1674. * @ret rc Return status code
  1675. *
  1676. * This boots from the specified INT 13 drive by loading the Master
  1677. * Boot Record to 0000:7c00 and jumping to it. INT 18 is hooked to
  1678. * capture an attempt by the MBR to boot the next device. (This is
  1679. * the closest thing to a return path from an MBR).
  1680. *
  1681. * Note that this function can never return success, by definition.
  1682. */
  1683. static int int13_boot ( unsigned int drive ) {
  1684. struct memory_map memmap;
  1685. struct segoff address;
  1686. int rc;
  1687. /* Look for a usable boot sector */
  1688. if ( ( ( rc = int13_load_mbr ( drive, &address ) ) != 0 ) &&
  1689. ( ( rc = int13_load_eltorito ( drive, &address ) ) != 0 ) )
  1690. return rc;
  1691. /* Dump out memory map prior to boot, if memmap debugging is
  1692. * enabled. Not required for program flow, but we have so
  1693. * many problems that turn out to be memory-map related that
  1694. * it's worth doing.
  1695. */
  1696. get_memmap ( &memmap );
  1697. /* Jump to boot sector */
  1698. if ( ( rc = call_bootsector ( address.segment, address.offset,
  1699. drive ) ) != 0 ) {
  1700. DBG ( "INT13 drive %02x boot returned: %s\n",
  1701. drive, strerror ( rc ) );
  1702. return rc;
  1703. }
  1704. return -ECANCELED; /* -EIMPOSSIBLE */
  1705. }
  1706. /** A boot firmware table generated by iPXE */
  1707. union xbft_table {
  1708. /** ACPI header */
  1709. struct acpi_description_header acpi;
  1710. /** Padding */
  1711. char pad[768];
  1712. };
  1713. /** The boot firmware table generated by iPXE */
  1714. static union xbft_table __bss16 ( xbftab ) __attribute__ (( aligned ( 16 ) ));
  1715. #define xbftab __use_data16 ( xbftab )
  1716. /**
  1717. * Describe INT 13 emulated drive for SAN-booted operating system
  1718. *
  1719. * @v drive Drive number
  1720. * @ret rc Return status code
  1721. */
  1722. static int int13_describe ( unsigned int drive ) {
  1723. struct int13_drive *int13;
  1724. struct segoff xbft_address;
  1725. int rc;
  1726. /* Find drive */
  1727. int13 = int13_find ( drive );
  1728. if ( ! int13 ) {
  1729. DBG ( "INT13 cannot find emulated drive %02x\n", drive );
  1730. return -ENODEV;
  1731. }
  1732. /* Reopen block device if necessary */
  1733. if ( ( int13->block_rc != 0 ) &&
  1734. ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) )
  1735. return rc;
  1736. /* Clear table */
  1737. memset ( &xbftab, 0, sizeof ( xbftab ) );
  1738. /* Fill in common parameters */
  1739. strncpy ( xbftab.acpi.oem_id, "FENSYS",
  1740. sizeof ( xbftab.acpi.oem_id ) );
  1741. strncpy ( xbftab.acpi.oem_table_id, "iPXE",
  1742. sizeof ( xbftab.acpi.oem_table_id ) );
  1743. /* Fill in remaining parameters */
  1744. if ( ( rc = acpi_describe ( &int13->block, &xbftab.acpi,
  1745. sizeof ( xbftab ) ) ) != 0 ) {
  1746. DBGC ( int13, "INT13 drive %02x could not create ACPI "
  1747. "description: %s\n", int13->drive, strerror ( rc ) );
  1748. return rc;
  1749. }
  1750. /* Fix up ACPI checksum */
  1751. acpi_fix_checksum ( &xbftab.acpi );
  1752. xbft_address.segment = rm_ds;
  1753. xbft_address.offset = __from_data16 ( &xbftab );
  1754. DBGC ( int13, "INT13 drive %02x described using boot firmware "
  1755. "table:\n", int13->drive );
  1756. DBGC_HDA ( int13, xbft_address, &xbftab,
  1757. le32_to_cpu ( xbftab.acpi.length ) );
  1758. return 0;
  1759. }
  1760. PROVIDE_SANBOOT_INLINE ( pcbios, san_default_drive );
  1761. PROVIDE_SANBOOT ( pcbios, san_hook, int13_hook );
  1762. PROVIDE_SANBOOT ( pcbios, san_unhook, int13_unhook );
  1763. PROVIDE_SANBOOT ( pcbios, san_boot, int13_boot );
  1764. PROVIDE_SANBOOT ( pcbios, san_describe, int13_describe );