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

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