You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

int13.c 55KB

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