Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

int13.c 56KB

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