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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  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/blockdev.h>
  31. #include <ipxe/io.h>
  32. #include <ipxe/acpi.h>
  33. #include <ipxe/sanboot.h>
  34. #include <ipxe/device.h>
  35. #include <ipxe/pci.h>
  36. #include <ipxe/eltorito.h>
  37. #include <realmode.h>
  38. #include <bios.h>
  39. #include <biosint.h>
  40. #include <bootsector.h>
  41. #include <int13.h>
  42. /** @file
  43. *
  44. * INT 13 emulation
  45. *
  46. * This module provides a mechanism for exporting block devices via
  47. * the BIOS INT 13 disk interrupt interface.
  48. *
  49. */
  50. /** INT 13 SAN device private data */
  51. struct int13_data {
  52. /** BIOS natural drive number (0x00-0xff)
  53. *
  54. * This is the drive number that would have been assigned by
  55. * 'naturally' appending the drive to the end of the BIOS
  56. * drive list.
  57. *
  58. * If the emulated drive replaces a preexisting drive, this is
  59. * the drive number that the preexisting drive gets remapped
  60. * to.
  61. */
  62. unsigned int natural_drive;
  63. /** Number of cylinders
  64. *
  65. * The cylinder number field in an INT 13 call is ten bits
  66. * wide, giving a maximum of 1024 cylinders. Conventionally,
  67. * when the 7.8GB limit of a CHS address is exceeded, it is
  68. * the number of cylinders that is increased beyond the
  69. * addressable limit.
  70. */
  71. unsigned int cylinders;
  72. /** Number of heads
  73. *
  74. * The head number field in an INT 13 call is eight bits wide,
  75. * giving a maximum of 256 heads. However, apparently all
  76. * versions of MS-DOS up to and including Win95 fail with 256
  77. * heads, so the maximum encountered in practice is 255.
  78. */
  79. unsigned int heads;
  80. /** Number of sectors per track
  81. *
  82. * The sector number field in an INT 13 call is six bits wide,
  83. * giving a maximum of 63 sectors, since sector numbering
  84. * (unlike head and cylinder numbering) starts at 1, not 0.
  85. */
  86. unsigned int sectors_per_track;
  87. /** Address of El Torito boot catalog (if any) */
  88. unsigned int boot_catalog;
  89. /** Status of last operation */
  90. int last_status;
  91. };
  92. /** Vector for chaining to other INT 13 handlers */
  93. static struct segoff __text16 ( int13_vector );
  94. #define int13_vector __use_text16 ( int13_vector )
  95. /** Assembly wrapper */
  96. extern void int13_wrapper ( void );
  97. /** Dummy floppy disk parameter table */
  98. static struct int13_fdd_parameters __data16 ( int13_fdd_params ) = {
  99. /* 512 bytes per sector */
  100. .bytes_per_sector = 0x02,
  101. /* Highest sectors per track that we ever return */
  102. .sectors_per_track = 48,
  103. };
  104. #define int13_fdd_params __use_data16 ( int13_fdd_params )
  105. /**
  106. * Equipment word
  107. *
  108. * This is a cached copy of the BIOS Data Area equipment word at
  109. * 40:10.
  110. */
  111. static uint16_t equipment_word;
  112. /**
  113. * Number of BIOS floppy disk drives
  114. *
  115. * This is derived from the equipment word. It is held in .text16 to
  116. * allow for easy access by the INT 13,08 wrapper.
  117. */
  118. static uint8_t __text16 ( num_fdds );
  119. #define num_fdds __use_text16 ( num_fdds )
  120. /**
  121. * Number of BIOS hard disk drives
  122. *
  123. * This is a cached copy of the BIOS Data Area number of hard disk
  124. * drives at 40:75. It is held in .text16 to allow for easy access by
  125. * the INT 13,08 wrapper.
  126. */
  127. static uint8_t __text16 ( num_drives );
  128. #define num_drives __use_text16 ( num_drives )
  129. /**
  130. * Calculate SAN device capacity (limited to 32 bits)
  131. *
  132. * @v sandev SAN device
  133. * @ret blocks Number of blocks
  134. */
  135. static inline uint32_t int13_capacity32 ( struct san_device *sandev ) {
  136. uint64_t capacity = sandev_capacity ( sandev );
  137. return ( ( capacity <= 0xffffffffUL ) ? capacity : 0xffffffff );
  138. }
  139. /**
  140. * Test if SAN device is a floppy disk drive
  141. *
  142. * @v sandev SAN device
  143. * @ret is_fdd SAN device is a floppy disk drive
  144. */
  145. static inline int int13_is_fdd ( struct san_device *sandev ) {
  146. return ( ! ( sandev->drive & 0x80 ) );
  147. }
  148. /**
  149. * Parse El Torito parameters
  150. *
  151. * @v sandev SAN device
  152. * @v scratch Scratch area for single-sector reads
  153. * @ret rc Return status code
  154. *
  155. * Reads and parses El Torito parameters, if present.
  156. */
  157. static int int13_parse_eltorito ( struct san_device *sandev, void *scratch ) {
  158. struct int13_data *int13 = sandev->priv;
  159. static const struct eltorito_descriptor_fixed boot_check = {
  160. .type = ISO9660_TYPE_BOOT,
  161. .id = ISO9660_ID,
  162. .version = 1,
  163. .system_id = "EL TORITO SPECIFICATION",
  164. };
  165. struct eltorito_descriptor *boot = scratch;
  166. int rc;
  167. /* Read boot record volume descriptor */
  168. if ( ( rc = sandev_read ( sandev, ELTORITO_LBA, 1,
  169. virt_to_user ( boot ) ) ) != 0 ) {
  170. DBGC ( sandev, "INT13 drive %02x could not read El Torito boot "
  171. "record volume descriptor: %s\n",
  172. sandev->drive, strerror ( rc ) );
  173. return rc;
  174. }
  175. /* Check for an El Torito boot catalog */
  176. if ( memcmp ( boot, &boot_check, sizeof ( boot_check ) ) == 0 ) {
  177. int13->boot_catalog = boot->sector;
  178. DBGC ( sandev, "INT13 drive %02x has an El Torito boot catalog "
  179. "at LBA %08x\n", sandev->drive, int13->boot_catalog );
  180. } else {
  181. DBGC ( sandev, "INT13 drive %02x has no El Torito boot "
  182. "catalog\n", sandev->drive );
  183. }
  184. return 0;
  185. }
  186. /**
  187. * Guess INT 13 hard disk drive geometry
  188. *
  189. * @v sandev SAN device
  190. * @v scratch Scratch area for single-sector reads
  191. * @ret heads Guessed number of heads
  192. * @ret sectors Guessed number of sectors per track
  193. * @ret rc Return status code
  194. *
  195. * Guesses the drive geometry by inspecting the partition table.
  196. */
  197. static int int13_guess_geometry_hdd ( struct san_device *sandev, void *scratch,
  198. unsigned int *heads,
  199. unsigned int *sectors ) {
  200. struct master_boot_record *mbr = scratch;
  201. struct partition_table_entry *partition;
  202. unsigned int i;
  203. unsigned int start_cylinder;
  204. unsigned int start_head;
  205. unsigned int start_sector;
  206. unsigned int end_head;
  207. unsigned int end_sector;
  208. int rc;
  209. /* Read partition table */
  210. if ( ( rc = sandev_read ( sandev, 0, 1, virt_to_user ( mbr ) ) ) != 0 ) {
  211. DBGC ( sandev, "INT13 drive %02x could not read "
  212. "partition table to guess geometry: %s\n",
  213. sandev->drive, strerror ( rc ) );
  214. return rc;
  215. }
  216. DBGC2 ( sandev, "INT13 drive %02x has MBR:\n", sandev->drive );
  217. DBGC2_HDA ( sandev, 0, mbr, sizeof ( *mbr ) );
  218. DBGC ( sandev, "INT13 drive %02x has signature %08x\n",
  219. sandev->drive, mbr->signature );
  220. /* Scan through partition table and modify guesses for
  221. * heads and sectors_per_track if we find any used
  222. * partitions.
  223. */
  224. *heads = 0;
  225. *sectors = 0;
  226. for ( i = 0 ; i < 4 ; i++ ) {
  227. /* Skip empty partitions */
  228. partition = &mbr->partitions[i];
  229. if ( ! partition->type )
  230. continue;
  231. /* If partition starts on cylinder 0 then we can
  232. * unambiguously determine the number of sectors.
  233. */
  234. start_cylinder = PART_CYLINDER ( partition->chs_start );
  235. start_head = PART_HEAD ( partition->chs_start );
  236. start_sector = PART_SECTOR ( partition->chs_start );
  237. if ( ( start_cylinder == 0 ) && ( start_head != 0 ) ) {
  238. *sectors = ( ( partition->start + 1 - start_sector ) /
  239. start_head );
  240. DBGC ( sandev, "INT13 drive %02x guessing C/H/S "
  241. "xx/xx/%d based on partition %d\n",
  242. sandev->drive, *sectors, ( i + 1 ) );
  243. }
  244. /* If partition ends on a higher head or sector number
  245. * than our current guess, then increase the guess.
  246. */
  247. end_head = PART_HEAD ( partition->chs_end );
  248. end_sector = PART_SECTOR ( partition->chs_end );
  249. if ( ( end_head + 1 ) > *heads ) {
  250. *heads = ( end_head + 1 );
  251. DBGC ( sandev, "INT13 drive %02x guessing C/H/S "
  252. "xx/%d/xx based on partition %d\n",
  253. sandev->drive, *heads, ( i + 1 ) );
  254. }
  255. if ( end_sector > *sectors ) {
  256. *sectors = end_sector;
  257. DBGC ( sandev, "INT13 drive %02x guessing C/H/S "
  258. "xx/xx/%d based on partition %d\n",
  259. sandev->drive, *sectors, ( i + 1 ) );
  260. }
  261. }
  262. /* Default guess is xx/255/63 */
  263. if ( ! *heads )
  264. *heads = 255;
  265. if ( ! *sectors )
  266. *sectors = 63;
  267. return 0;
  268. }
  269. /** Recognised floppy disk geometries */
  270. static const struct int13_fdd_geometry int13_fdd_geometries[] = {
  271. INT13_FDD_GEOMETRY ( 40, 1, 8 ),
  272. INT13_FDD_GEOMETRY ( 40, 1, 9 ),
  273. INT13_FDD_GEOMETRY ( 40, 2, 8 ),
  274. INT13_FDD_GEOMETRY ( 40, 1, 9 ),
  275. INT13_FDD_GEOMETRY ( 80, 2, 8 ),
  276. INT13_FDD_GEOMETRY ( 80, 2, 9 ),
  277. INT13_FDD_GEOMETRY ( 80, 2, 15 ),
  278. INT13_FDD_GEOMETRY ( 80, 2, 18 ),
  279. INT13_FDD_GEOMETRY ( 80, 2, 20 ),
  280. INT13_FDD_GEOMETRY ( 80, 2, 21 ),
  281. INT13_FDD_GEOMETRY ( 82, 2, 21 ),
  282. INT13_FDD_GEOMETRY ( 83, 2, 21 ),
  283. INT13_FDD_GEOMETRY ( 80, 2, 22 ),
  284. INT13_FDD_GEOMETRY ( 80, 2, 23 ),
  285. INT13_FDD_GEOMETRY ( 80, 2, 24 ),
  286. INT13_FDD_GEOMETRY ( 80, 2, 36 ),
  287. INT13_FDD_GEOMETRY ( 80, 2, 39 ),
  288. INT13_FDD_GEOMETRY ( 80, 2, 40 ),
  289. INT13_FDD_GEOMETRY ( 80, 2, 44 ),
  290. INT13_FDD_GEOMETRY ( 80, 2, 48 ),
  291. };
  292. /**
  293. * Guess INT 13 floppy disk drive geometry
  294. *
  295. * @v sandev SAN device
  296. * @ret heads Guessed number of heads
  297. * @ret sectors Guessed number of sectors per track
  298. * @ret rc Return status code
  299. *
  300. * Guesses the drive geometry by inspecting the disk size.
  301. */
  302. static int int13_guess_geometry_fdd ( struct san_device *sandev,
  303. unsigned int *heads,
  304. unsigned int *sectors ) {
  305. unsigned int blocks = sandev_capacity ( sandev );
  306. const struct int13_fdd_geometry *geometry;
  307. unsigned int cylinders;
  308. unsigned int i;
  309. /* Look for a match against a known geometry */
  310. for ( i = 0 ; i < ( sizeof ( int13_fdd_geometries ) /
  311. sizeof ( int13_fdd_geometries[0] ) ) ; i++ ) {
  312. geometry = &int13_fdd_geometries[i];
  313. cylinders = INT13_FDD_CYLINDERS ( geometry );
  314. *heads = INT13_FDD_HEADS ( geometry );
  315. *sectors = INT13_FDD_SECTORS ( geometry );
  316. if ( ( cylinders * (*heads) * (*sectors) ) == blocks ) {
  317. DBGC ( sandev, "INT13 drive %02x guessing C/H/S "
  318. "%d/%d/%d based on size %dK\n", sandev->drive,
  319. cylinders, *heads, *sectors, ( blocks / 2 ) );
  320. return 0;
  321. }
  322. }
  323. /* Otherwise, assume a partial disk image in the most common
  324. * format (1440K, 80/2/18).
  325. */
  326. *heads = 2;
  327. *sectors = 18;
  328. DBGC ( sandev, "INT13 drive %02x guessing C/H/S xx/%d/%d based on size "
  329. "%dK\n", sandev->drive, *heads, *sectors, ( blocks / 2 ) );
  330. return 0;
  331. }
  332. /**
  333. * Guess INT 13 drive geometry
  334. *
  335. * @v sandev SAN device
  336. * @v scratch Scratch area for single-sector reads
  337. * @ret rc Return status code
  338. */
  339. static int int13_guess_geometry ( struct san_device *sandev, void *scratch ) {
  340. struct int13_data *int13 = sandev->priv;
  341. unsigned int guessed_heads;
  342. unsigned int guessed_sectors;
  343. unsigned int blocks;
  344. unsigned int blocks_per_cyl;
  345. int rc;
  346. /* Guess geometry according to drive type */
  347. if ( int13_is_fdd ( sandev ) ) {
  348. if ( ( rc = int13_guess_geometry_fdd ( sandev, &guessed_heads,
  349. &guessed_sectors )) != 0)
  350. return rc;
  351. } else {
  352. if ( ( rc = int13_guess_geometry_hdd ( sandev, scratch,
  353. &guessed_heads,
  354. &guessed_sectors )) != 0)
  355. return rc;
  356. }
  357. /* Apply guesses if no geometry already specified */
  358. if ( ! int13->heads )
  359. int13->heads = guessed_heads;
  360. if ( ! int13->sectors_per_track )
  361. int13->sectors_per_track = guessed_sectors;
  362. if ( ! int13->cylinders ) {
  363. /* Avoid attempting a 64-bit divide on a 32-bit system */
  364. blocks = int13_capacity32 ( sandev );
  365. blocks_per_cyl = ( int13->heads * int13->sectors_per_track );
  366. assert ( blocks_per_cyl != 0 );
  367. int13->cylinders = ( blocks / blocks_per_cyl );
  368. if ( int13->cylinders > 1024 )
  369. int13->cylinders = 1024;
  370. }
  371. return 0;
  372. }
  373. /**
  374. * Update BIOS drive count
  375. */
  376. static void int13_sync_num_drives ( void ) {
  377. struct san_device *sandev;
  378. struct int13_data *int13;
  379. uint8_t *counter;
  380. uint8_t max_drive;
  381. uint8_t required;
  382. /* Get current drive counts */
  383. get_real ( equipment_word, BDA_SEG, BDA_EQUIPMENT_WORD );
  384. get_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
  385. num_fdds = ( ( equipment_word & 0x0001 ) ?
  386. ( ( ( equipment_word >> 6 ) & 0x3 ) + 1 ) : 0 );
  387. /* Ensure count is large enough to cover all of our SAN devices */
  388. for_each_sandev ( sandev ) {
  389. int13 = sandev->priv;
  390. counter = ( int13_is_fdd ( sandev ) ? &num_fdds : &num_drives );
  391. max_drive = sandev->drive;
  392. if ( max_drive < int13->natural_drive )
  393. max_drive = int13->natural_drive;
  394. required = ( ( max_drive & 0x7f ) + 1 );
  395. if ( *counter < required ) {
  396. *counter = required;
  397. DBGC ( sandev, "INT13 drive %02x added to drive count: "
  398. "%d HDDs, %d FDDs\n",
  399. sandev->drive, num_drives, num_fdds );
  400. }
  401. }
  402. /* Update current drive count */
  403. equipment_word &= ~( ( 0x3 << 6 ) | 0x0001 );
  404. if ( num_fdds ) {
  405. equipment_word |= ( 0x0001 |
  406. ( ( ( num_fdds - 1 ) & 0x3 ) << 6 ) );
  407. }
  408. put_real ( equipment_word, BDA_SEG, BDA_EQUIPMENT_WORD );
  409. put_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
  410. }
  411. /**
  412. * Check number of drives
  413. */
  414. static void int13_check_num_drives ( void ) {
  415. uint16_t check_equipment_word;
  416. uint8_t check_num_drives;
  417. get_real ( check_equipment_word, BDA_SEG, BDA_EQUIPMENT_WORD );
  418. get_real ( check_num_drives, BDA_SEG, BDA_NUM_DRIVES );
  419. if ( ( check_equipment_word != equipment_word ) ||
  420. ( check_num_drives != num_drives ) ) {
  421. int13_sync_num_drives();
  422. }
  423. }
  424. /**
  425. * INT 13, 00 - Reset disk system
  426. *
  427. * @v sandev SAN device
  428. * @ret status Status code
  429. */
  430. static int int13_reset ( struct san_device *sandev,
  431. struct i386_all_regs *ix86 __unused ) {
  432. int rc;
  433. DBGC2 ( sandev, "Reset drive\n" );
  434. /* Reset SAN device */
  435. if ( ( rc = sandev_reset ( sandev ) ) != 0 )
  436. return -INT13_STATUS_RESET_FAILED;
  437. return 0;
  438. }
  439. /**
  440. * INT 13, 01 - Get status of last operation
  441. *
  442. * @v sandev SAN device
  443. * @ret status Status code
  444. */
  445. static int int13_get_last_status ( struct san_device *sandev,
  446. struct i386_all_regs *ix86 __unused ) {
  447. struct int13_data *int13 = sandev->priv;
  448. DBGC2 ( sandev, "Get status of last operation\n" );
  449. return int13->last_status;
  450. }
  451. /**
  452. * Read / write sectors
  453. *
  454. * @v sandev SAN device
  455. * @v al Number of sectors to read or write (must be nonzero)
  456. * @v ch Low bits of cylinder number
  457. * @v cl (bits 7:6) High bits of cylinder number
  458. * @v cl (bits 5:0) Sector number
  459. * @v dh Head number
  460. * @v es:bx Data buffer
  461. * @v sandev_rw SAN device read/write method
  462. * @ret status Status code
  463. * @ret al Number of sectors read or written
  464. */
  465. static int int13_rw_sectors ( struct san_device *sandev,
  466. struct i386_all_regs *ix86,
  467. int ( * sandev_rw ) ( struct san_device *sandev,
  468. uint64_t lba,
  469. unsigned int count,
  470. userptr_t buffer ) ) {
  471. struct int13_data *int13 = sandev->priv;
  472. unsigned int cylinder, head, sector;
  473. unsigned long lba;
  474. unsigned int count;
  475. userptr_t buffer;
  476. int rc;
  477. /* Validate blocksize */
  478. if ( sandev_blksize ( sandev ) != INT13_BLKSIZE ) {
  479. DBGC ( sandev, "\nINT 13 drive %02x invalid blocksize (%zd) "
  480. "for non-extended read/write\n",
  481. sandev->drive, sandev_blksize ( sandev ) );
  482. return -INT13_STATUS_INVALID;
  483. }
  484. /* Calculate parameters */
  485. cylinder = ( ( ( ix86->regs.cl & 0xc0 ) << 2 ) | ix86->regs.ch );
  486. head = ix86->regs.dh;
  487. sector = ( ix86->regs.cl & 0x3f );
  488. if ( ( cylinder >= int13->cylinders ) ||
  489. ( head >= int13->heads ) ||
  490. ( sector < 1 ) || ( sector > int13->sectors_per_track ) ) {
  491. DBGC ( sandev, "C/H/S %d/%d/%d out of range for geometry "
  492. "%d/%d/%d\n", cylinder, head, sector, int13->cylinders,
  493. int13->heads, int13->sectors_per_track );
  494. return -INT13_STATUS_INVALID;
  495. }
  496. lba = ( ( ( ( cylinder * int13->heads ) + head )
  497. * int13->sectors_per_track ) + sector - 1 );
  498. count = ix86->regs.al;
  499. buffer = real_to_user ( ix86->segs.es, ix86->regs.bx );
  500. DBGC2 ( sandev, "C/H/S %d/%d/%d = LBA %08lx <-> %04x:%04x (count %d)\n",
  501. cylinder, head, sector, lba, ix86->segs.es, ix86->regs.bx,
  502. count );
  503. /* Read from / write to block device */
  504. if ( ( rc = sandev_rw ( sandev, lba, count, buffer ) ) != 0 ){
  505. DBGC ( sandev, "INT13 drive %02x I/O failed: %s\n",
  506. sandev->drive, strerror ( rc ) );
  507. return -INT13_STATUS_READ_ERROR;
  508. }
  509. return 0;
  510. }
  511. /**
  512. * INT 13, 02 - Read sectors
  513. *
  514. * @v sandev SAN device
  515. * @v al Number of sectors to read (must be nonzero)
  516. * @v ch Low bits of cylinder number
  517. * @v cl (bits 7:6) High bits of cylinder number
  518. * @v cl (bits 5:0) Sector number
  519. * @v dh Head number
  520. * @v es:bx Data buffer
  521. * @ret status Status code
  522. * @ret al Number of sectors read
  523. */
  524. static int int13_read_sectors ( struct san_device *sandev,
  525. struct i386_all_regs *ix86 ) {
  526. DBGC2 ( sandev, "Read: " );
  527. return int13_rw_sectors ( sandev, ix86, sandev_read );
  528. }
  529. /**
  530. * INT 13, 03 - Write sectors
  531. *
  532. * @v sandev SAN device
  533. * @v al Number of sectors to write (must be nonzero)
  534. * @v ch Low bits of cylinder number
  535. * @v cl (bits 7:6) High bits of cylinder number
  536. * @v cl (bits 5:0) Sector number
  537. * @v dh Head number
  538. * @v es:bx Data buffer
  539. * @ret status Status code
  540. * @ret al Number of sectors written
  541. */
  542. static int int13_write_sectors ( struct san_device *sandev,
  543. struct i386_all_regs *ix86 ) {
  544. DBGC2 ( sandev, "Write: " );
  545. return int13_rw_sectors ( sandev, ix86, sandev_write );
  546. }
  547. /**
  548. * INT 13, 08 - Get drive parameters
  549. *
  550. * @v sandev SAN device
  551. * @ret status Status code
  552. * @ret ch Low bits of maximum cylinder number
  553. * @ret cl (bits 7:6) High bits of maximum cylinder number
  554. * @ret cl (bits 5:0) Maximum sector number
  555. * @ret dh Maximum head number
  556. * @ret dl Number of drives
  557. */
  558. static int int13_get_parameters ( struct san_device *sandev,
  559. struct i386_all_regs *ix86 ) {
  560. struct int13_data *int13 = sandev->priv;
  561. unsigned int max_cylinder = int13->cylinders - 1;
  562. unsigned int max_head = int13->heads - 1;
  563. unsigned int max_sector = int13->sectors_per_track; /* sic */
  564. DBGC2 ( sandev, "Get drive parameters\n" );
  565. /* Validate blocksize */
  566. if ( sandev_blksize ( sandev ) != INT13_BLKSIZE ) {
  567. DBGC ( sandev, "\nINT 13 drive %02x invalid blocksize (%zd) "
  568. "for non-extended parameters\n",
  569. sandev->drive, sandev_blksize ( sandev ) );
  570. return -INT13_STATUS_INVALID;
  571. }
  572. /* Common parameters */
  573. ix86->regs.ch = ( max_cylinder & 0xff );
  574. ix86->regs.cl = ( ( ( max_cylinder >> 8 ) << 6 ) | max_sector );
  575. ix86->regs.dh = max_head;
  576. ix86->regs.dl = ( int13_is_fdd ( sandev ) ? num_fdds : num_drives );
  577. /* Floppy-specific parameters */
  578. if ( int13_is_fdd ( sandev ) ) {
  579. ix86->regs.bl = INT13_FDD_TYPE_1M44;
  580. ix86->segs.es = rm_ds;
  581. ix86->regs.di = __from_data16 ( &int13_fdd_params );
  582. }
  583. return 0;
  584. }
  585. /**
  586. * INT 13, 15 - Get disk type
  587. *
  588. * @v sandev SAN device
  589. * @ret ah Type code
  590. * @ret cx:dx Sector count
  591. * @ret status Status code / disk type
  592. */
  593. static int int13_get_disk_type ( struct san_device *sandev,
  594. struct i386_all_regs *ix86 ) {
  595. uint32_t blocks;
  596. DBGC2 ( sandev, "Get disk type\n" );
  597. if ( int13_is_fdd ( sandev ) ) {
  598. return INT13_DISK_TYPE_FDD;
  599. } else {
  600. blocks = int13_capacity32 ( sandev );
  601. ix86->regs.cx = ( blocks >> 16 );
  602. ix86->regs.dx = ( blocks & 0xffff );
  603. return INT13_DISK_TYPE_HDD;
  604. }
  605. }
  606. /**
  607. * INT 13, 41 - Extensions installation check
  608. *
  609. * @v sandev SAN device
  610. * @v bx 0x55aa
  611. * @ret bx 0xaa55
  612. * @ret cx Extensions API support bitmap
  613. * @ret status Status code / API version
  614. */
  615. static int int13_extension_check ( struct san_device *sandev __unused,
  616. struct i386_all_regs *ix86 ) {
  617. if ( ix86->regs.bx == 0x55aa ) {
  618. DBGC2 ( sandev, "INT13 extensions installation check\n" );
  619. ix86->regs.bx = 0xaa55;
  620. ix86->regs.cx = ( INT13_EXTENSION_LINEAR |
  621. INT13_EXTENSION_EDD |
  622. INT13_EXTENSION_64BIT );
  623. return INT13_EXTENSION_VER_3_0;
  624. } else {
  625. return -INT13_STATUS_INVALID;
  626. }
  627. }
  628. /**
  629. * Extended read / write
  630. *
  631. * @v sandev SAN device
  632. * @v ds:si Disk address packet
  633. * @v sandev_rw SAN device read/write method
  634. * @ret status Status code
  635. */
  636. static int int13_extended_rw ( struct san_device *sandev,
  637. struct i386_all_regs *ix86,
  638. int ( * sandev_rw ) ( struct san_device *sandev,
  639. uint64_t lba,
  640. unsigned int count,
  641. userptr_t buffer ) ) {
  642. struct int13_disk_address addr;
  643. uint8_t bufsize;
  644. uint64_t lba;
  645. unsigned long count;
  646. userptr_t buffer;
  647. int rc;
  648. /* Extended reads are not allowed on floppy drives.
  649. * ELTORITO.SYS seems to assume that we are really a CD-ROM if
  650. * we support extended reads for a floppy drive.
  651. */
  652. if ( int13_is_fdd ( sandev ) )
  653. return -INT13_STATUS_INVALID;
  654. /* Get buffer size */
  655. get_real ( bufsize, ix86->segs.ds,
  656. ( ix86->regs.si + offsetof ( typeof ( addr ), bufsize ) ) );
  657. if ( bufsize < offsetof ( typeof ( addr ), buffer_phys ) ) {
  658. DBGC2 ( sandev, "<invalid buffer size %#02x\n>\n", bufsize );
  659. return -INT13_STATUS_INVALID;
  660. }
  661. /* Read parameters from disk address structure */
  662. memset ( &addr, 0, sizeof ( addr ) );
  663. copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si, bufsize );
  664. lba = addr.lba;
  665. DBGC2 ( sandev, "LBA %08llx <-> ", ( ( unsigned long long ) lba ) );
  666. if ( ( addr.count == 0xff ) ||
  667. ( ( addr.buffer.segment == 0xffff ) &&
  668. ( addr.buffer.offset == 0xffff ) ) ) {
  669. buffer = phys_to_user ( addr.buffer_phys );
  670. DBGC2 ( sandev, "%08llx",
  671. ( ( unsigned long long ) addr.buffer_phys ) );
  672. } else {
  673. buffer = real_to_user ( addr.buffer.segment,
  674. addr.buffer.offset );
  675. DBGC2 ( sandev, "%04x:%04x", addr.buffer.segment,
  676. addr.buffer.offset );
  677. }
  678. if ( addr.count <= 0x7f ) {
  679. count = addr.count;
  680. } else if ( addr.count == 0xff ) {
  681. count = addr.long_count;
  682. } else {
  683. DBGC2 ( sandev, " <invalid count %#02x>\n", addr.count );
  684. return -INT13_STATUS_INVALID;
  685. }
  686. DBGC2 ( sandev, " (count %ld)\n", count );
  687. /* Read from / write to block device */
  688. if ( ( rc = sandev_rw ( sandev, lba, count, buffer ) ) != 0 ) {
  689. DBGC ( sandev, "INT13 drive %02x extended I/O failed: %s\n",
  690. sandev->drive, strerror ( rc ) );
  691. /* Record that no blocks were transferred successfully */
  692. addr.count = 0;
  693. put_real ( addr.count, ix86->segs.ds,
  694. ( ix86->regs.si +
  695. offsetof ( typeof ( addr ), count ) ) );
  696. return -INT13_STATUS_READ_ERROR;
  697. }
  698. return 0;
  699. }
  700. /**
  701. * INT 13, 42 - Extended read
  702. *
  703. * @v sandev SAN device
  704. * @v ds:si Disk address packet
  705. * @ret status Status code
  706. */
  707. static int int13_extended_read ( struct san_device *sandev,
  708. struct i386_all_regs *ix86 ) {
  709. DBGC2 ( sandev, "Extended read: " );
  710. return int13_extended_rw ( sandev, ix86, sandev_read );
  711. }
  712. /**
  713. * INT 13, 43 - Extended write
  714. *
  715. * @v sandev SAN device
  716. * @v ds:si Disk address packet
  717. * @ret status Status code
  718. */
  719. static int int13_extended_write ( struct san_device *sandev,
  720. struct i386_all_regs *ix86 ) {
  721. DBGC2 ( sandev, "Extended write: " );
  722. return int13_extended_rw ( sandev, ix86, sandev_write );
  723. }
  724. /**
  725. * INT 13, 44 - Verify sectors
  726. *
  727. * @v sandev SAN device
  728. * @v ds:si Disk address packet
  729. * @ret status Status code
  730. */
  731. static int int13_extended_verify ( struct san_device *sandev,
  732. struct i386_all_regs *ix86 ) {
  733. struct int13_disk_address addr;
  734. uint64_t lba;
  735. unsigned long count;
  736. /* Read parameters from disk address structure */
  737. if ( DBG_EXTRA ) {
  738. copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si,
  739. sizeof ( addr ));
  740. lba = addr.lba;
  741. count = addr.count;
  742. DBGC2 ( sandev, "Verify: LBA %08llx (count %ld)\n",
  743. ( ( unsigned long long ) lba ), count );
  744. }
  745. /* We have no mechanism for verifying sectors */
  746. return -INT13_STATUS_INVALID;
  747. }
  748. /**
  749. * INT 13, 44 - Extended seek
  750. *
  751. * @v sandev SAN device
  752. * @v ds:si Disk address packet
  753. * @ret status Status code
  754. */
  755. static int int13_extended_seek ( struct san_device *sandev,
  756. struct i386_all_regs *ix86 ) {
  757. struct int13_disk_address addr;
  758. uint64_t lba;
  759. unsigned long count;
  760. /* Read parameters from disk address structure */
  761. if ( DBG_EXTRA ) {
  762. copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si,
  763. sizeof ( addr ));
  764. lba = addr.lba;
  765. count = addr.count;
  766. DBGC2 ( sandev, "Seek: LBA %08llx (count %ld)\n",
  767. ( ( unsigned long long ) lba ), count );
  768. }
  769. /* Ignore and return success */
  770. return 0;
  771. }
  772. /**
  773. * Build device path information
  774. *
  775. * @v sandev SAN device
  776. * @v dpi Device path information
  777. * @ret rc Return status code
  778. */
  779. static int int13_device_path_info ( struct san_device *sandev,
  780. struct edd_device_path_information *dpi ) {
  781. struct san_path *sanpath;
  782. struct device *device;
  783. struct device_description *desc;
  784. unsigned int i;
  785. uint8_t sum = 0;
  786. int rc;
  787. /* Reopen block device if necessary */
  788. if ( sandev_needs_reopen ( sandev ) &&
  789. ( ( rc = sandev_reopen ( sandev ) ) != 0 ) )
  790. return rc;
  791. sanpath = sandev->active;
  792. assert ( sanpath != NULL );
  793. /* Get underlying hardware device */
  794. device = identify_device ( &sanpath->block );
  795. if ( ! device ) {
  796. DBGC ( sandev, "INT13 drive %02x cannot identify hardware "
  797. "device\n", sandev->drive );
  798. return -ENODEV;
  799. }
  800. /* Fill in bus type and interface path */
  801. desc = &device->desc;
  802. switch ( desc->bus_type ) {
  803. case BUS_TYPE_PCI:
  804. dpi->host_bus_type.type = EDD_BUS_TYPE_PCI;
  805. dpi->interface_path.pci.bus = PCI_BUS ( desc->location );
  806. dpi->interface_path.pci.slot = PCI_SLOT ( desc->location );
  807. dpi->interface_path.pci.function = PCI_FUNC ( desc->location );
  808. dpi->interface_path.pci.channel = 0xff; /* unused */
  809. break;
  810. default:
  811. DBGC ( sandev, "INT13 drive %02x unrecognised bus type %d\n",
  812. sandev->drive, desc->bus_type );
  813. return -ENOTSUP;
  814. }
  815. /* Get EDD block device description */
  816. if ( ( rc = edd_describe ( &sanpath->block, &dpi->interface_type,
  817. &dpi->device_path ) ) != 0 ) {
  818. DBGC ( sandev, "INT13 drive %02x cannot identify block device: "
  819. "%s\n", sandev->drive, strerror ( rc ) );
  820. return rc;
  821. }
  822. /* Fill in common fields and fix checksum */
  823. dpi->key = EDD_DEVICE_PATH_INFO_KEY;
  824. dpi->len = sizeof ( *dpi );
  825. for ( i = 0 ; i < sizeof ( *dpi ) ; i++ )
  826. sum += *( ( ( uint8_t * ) dpi ) + i );
  827. dpi->checksum -= sum;
  828. return 0;
  829. }
  830. /**
  831. * INT 13, 48 - Get extended parameters
  832. *
  833. * @v sandev SAN device
  834. * @v ds:si Drive parameter table
  835. * @ret status Status code
  836. */
  837. static int int13_get_extended_parameters ( struct san_device *sandev,
  838. struct i386_all_regs *ix86 ) {
  839. struct int13_data *int13 = sandev->priv;
  840. struct int13_disk_parameters params;
  841. struct segoff address;
  842. size_t len = sizeof ( params );
  843. uint16_t bufsize;
  844. int rc;
  845. /* Get buffer size */
  846. get_real ( bufsize, ix86->segs.ds,
  847. ( ix86->regs.si + offsetof ( typeof ( params ), bufsize )));
  848. DBGC2 ( sandev, "Get extended drive parameters to %04x:%04x+%02x\n",
  849. ix86->segs.ds, ix86->regs.si, bufsize );
  850. /* Build drive parameters */
  851. memset ( &params, 0, sizeof ( params ) );
  852. params.flags = INT13_FL_DMA_TRANSPARENT;
  853. if ( ( int13->cylinders < 1024 ) &&
  854. ( sandev_capacity ( sandev ) <= INT13_MAX_CHS_SECTORS ) ) {
  855. params.flags |= INT13_FL_CHS_VALID;
  856. }
  857. params.cylinders = int13->cylinders;
  858. params.heads = int13->heads;
  859. params.sectors_per_track = int13->sectors_per_track;
  860. params.sectors = sandev_capacity ( sandev );
  861. params.sector_size = sandev_blksize ( sandev );
  862. memset ( &params.dpte, 0xff, sizeof ( params.dpte ) );
  863. if ( ( rc = int13_device_path_info ( sandev, &params.dpi ) ) != 0 ) {
  864. DBGC ( sandev, "INT13 drive %02x could not provide device "
  865. "path information: %s\n",
  866. sandev->drive, strerror ( rc ) );
  867. len = offsetof ( typeof ( params ), dpi );
  868. }
  869. /* Calculate returned "buffer size" (which will be less than
  870. * the length actually copied if device path information is
  871. * present).
  872. */
  873. if ( bufsize < offsetof ( typeof ( params ), dpte ) )
  874. return -INT13_STATUS_INVALID;
  875. if ( bufsize < offsetof ( typeof ( params ), dpi ) ) {
  876. params.bufsize = offsetof ( typeof ( params ), dpte );
  877. } else {
  878. params.bufsize = offsetof ( typeof ( params ), dpi );
  879. }
  880. DBGC ( sandev, "INT 13 drive %02x described using extended "
  881. "parameters:\n", sandev->drive );
  882. address.segment = ix86->segs.ds;
  883. address.offset = ix86->regs.si;
  884. DBGC_HDA ( sandev, address, &params, len );
  885. /* Return drive parameters */
  886. if ( len > bufsize )
  887. len = bufsize;
  888. copy_to_real ( ix86->segs.ds, ix86->regs.si, &params, len );
  889. return 0;
  890. }
  891. /**
  892. * INT 13, 4b - Get status or terminate CD-ROM emulation
  893. *
  894. * @v sandev SAN device
  895. * @v ds:si Specification packet
  896. * @ret status Status code
  897. */
  898. static int int13_cdrom_status_terminate ( struct san_device *sandev,
  899. struct i386_all_regs *ix86 ) {
  900. struct int13_cdrom_specification specification;
  901. DBGC2 ( sandev, "Get CD-ROM emulation status to %04x:%04x%s\n",
  902. ix86->segs.ds, ix86->regs.si,
  903. ( ix86->regs.al ? "" : " and terminate" ) );
  904. /* Fail if we are not a CD-ROM */
  905. if ( ! sandev->is_cdrom ) {
  906. DBGC ( sandev, "INT13 drive %02x is not a CD-ROM\n",
  907. sandev->drive );
  908. return -INT13_STATUS_INVALID;
  909. }
  910. /* Build specification packet */
  911. memset ( &specification, 0, sizeof ( specification ) );
  912. specification.size = sizeof ( specification );
  913. specification.drive = sandev->drive;
  914. /* Return specification packet */
  915. copy_to_real ( ix86->segs.ds, ix86->regs.si, &specification,
  916. sizeof ( specification ) );
  917. return 0;
  918. }
  919. /**
  920. * INT 13, 4d - Read CD-ROM boot catalog
  921. *
  922. * @v sandev SAN device
  923. * @v ds:si Command packet
  924. * @ret status Status code
  925. */
  926. static int int13_cdrom_read_boot_catalog ( struct san_device *sandev,
  927. struct i386_all_regs *ix86 ) {
  928. struct int13_data *int13 = sandev->priv;
  929. struct int13_cdrom_boot_catalog_command command;
  930. unsigned int start;
  931. int rc;
  932. /* Read parameters from command packet */
  933. copy_from_real ( &command, ix86->segs.ds, ix86->regs.si,
  934. sizeof ( command ) );
  935. DBGC2 ( sandev, "Read CD-ROM boot catalog to %08x\n", command.buffer );
  936. /* Fail if we have no boot catalog */
  937. if ( ! int13->boot_catalog ) {
  938. DBGC ( sandev, "INT13 drive %02x has no boot catalog\n",
  939. sandev->drive );
  940. return -INT13_STATUS_INVALID;
  941. }
  942. start = ( int13->boot_catalog + command.start );
  943. /* Read from boot catalog */
  944. if ( ( rc = sandev_read ( sandev, start, command.count,
  945. phys_to_user ( command.buffer ) ) ) != 0 ) {
  946. DBGC ( sandev, "INT13 drive %02x could not read boot catalog: "
  947. "%s\n", sandev->drive, strerror ( rc ) );
  948. return -INT13_STATUS_READ_ERROR;
  949. }
  950. return 0;
  951. }
  952. /**
  953. * INT 13 handler
  954. *
  955. */
  956. static __asmcall void int13 ( struct i386_all_regs *ix86 ) {
  957. int command = ix86->regs.ah;
  958. unsigned int bios_drive = ix86->regs.dl;
  959. struct san_device *sandev;
  960. struct int13_data *int13;
  961. int status;
  962. /* Check BIOS hasn't killed off our drive */
  963. int13_check_num_drives();
  964. for_each_sandev ( sandev ) {
  965. int13 = sandev->priv;
  966. if ( bios_drive != sandev->drive ) {
  967. /* Remap any accesses to this drive's natural number */
  968. if ( bios_drive == int13->natural_drive ) {
  969. DBGC2 ( sandev, "INT13,%02x (%02x) remapped to "
  970. "(%02x)\n", ix86->regs.ah,
  971. bios_drive, sandev->drive );
  972. ix86->regs.dl = sandev->drive;
  973. return;
  974. } else if ( ( ( bios_drive & 0x7f ) == 0x7f ) &&
  975. ( command == INT13_CDROM_STATUS_TERMINATE )
  976. && sandev->is_cdrom ) {
  977. /* Catch non-drive-specific CD-ROM calls */
  978. } else {
  979. continue;
  980. }
  981. }
  982. DBGC2 ( sandev, "INT13,%02x (%02x): ",
  983. ix86->regs.ah, bios_drive );
  984. switch ( command ) {
  985. case INT13_RESET:
  986. status = int13_reset ( sandev, ix86 );
  987. break;
  988. case INT13_GET_LAST_STATUS:
  989. status = int13_get_last_status ( sandev, ix86 );
  990. break;
  991. case INT13_READ_SECTORS:
  992. status = int13_read_sectors ( sandev, ix86 );
  993. break;
  994. case INT13_WRITE_SECTORS:
  995. status = int13_write_sectors ( sandev, ix86 );
  996. break;
  997. case INT13_GET_PARAMETERS:
  998. status = int13_get_parameters ( sandev, ix86 );
  999. break;
  1000. case INT13_GET_DISK_TYPE:
  1001. status = int13_get_disk_type ( sandev, ix86 );
  1002. break;
  1003. case INT13_EXTENSION_CHECK:
  1004. status = int13_extension_check ( sandev, ix86 );
  1005. break;
  1006. case INT13_EXTENDED_READ:
  1007. status = int13_extended_read ( sandev, ix86 );
  1008. break;
  1009. case INT13_EXTENDED_WRITE:
  1010. status = int13_extended_write ( sandev, ix86 );
  1011. break;
  1012. case INT13_EXTENDED_VERIFY:
  1013. status = int13_extended_verify ( sandev, ix86 );
  1014. break;
  1015. case INT13_EXTENDED_SEEK:
  1016. status = int13_extended_seek ( sandev, ix86 );
  1017. break;
  1018. case INT13_GET_EXTENDED_PARAMETERS:
  1019. status = int13_get_extended_parameters ( sandev, ix86 );
  1020. break;
  1021. case INT13_CDROM_STATUS_TERMINATE:
  1022. status = int13_cdrom_status_terminate ( sandev, ix86 );
  1023. break;
  1024. case INT13_CDROM_READ_BOOT_CATALOG:
  1025. status = int13_cdrom_read_boot_catalog ( sandev, ix86 );
  1026. break;
  1027. default:
  1028. DBGC2 ( sandev, "*** Unrecognised INT13 ***\n" );
  1029. status = -INT13_STATUS_INVALID;
  1030. break;
  1031. }
  1032. /* Store status for INT 13,01 */
  1033. int13->last_status = status;
  1034. /* Negative status indicates an error */
  1035. if ( status < 0 ) {
  1036. status = -status;
  1037. DBGC ( sandev, "INT13,%02x (%02x) failed with status "
  1038. "%02x\n", ix86->regs.ah, sandev->drive, status );
  1039. } else {
  1040. ix86->flags &= ~CF;
  1041. }
  1042. ix86->regs.ah = status;
  1043. /* Set OF to indicate to wrapper not to chain this call */
  1044. ix86->flags |= OF;
  1045. return;
  1046. }
  1047. }
  1048. /**
  1049. * Hook INT 13 handler
  1050. *
  1051. */
  1052. static void int13_hook_vector ( void ) {
  1053. /* Assembly wrapper to call int13(). int13() sets OF if we
  1054. * should not chain to the previous handler. (The wrapper
  1055. * clears CF and OF before calling int13()).
  1056. */
  1057. __asm__ __volatile__ (
  1058. TEXT16_CODE ( "\nint13_wrapper:\n\t"
  1059. /* Preserve %ax and %dx for future reference */
  1060. "pushw %%bp\n\t"
  1061. "movw %%sp, %%bp\n\t"
  1062. "pushw %%ax\n\t"
  1063. "pushw %%dx\n\t"
  1064. /* Clear OF, set CF, call int13() */
  1065. "orb $0, %%al\n\t"
  1066. "stc\n\t"
  1067. VIRT_CALL ( int13 )
  1068. /* Chain if OF not set */
  1069. "jo 1f\n\t"
  1070. "pushfw\n\t"
  1071. "lcall *%%cs:int13_vector\n\t"
  1072. "\n1:\n\t"
  1073. /* Overwrite flags for iret */
  1074. "pushfw\n\t"
  1075. "popw 6(%%bp)\n\t"
  1076. /* Fix up %dl:
  1077. *
  1078. * INT 13,15 : do nothing if hard disk
  1079. * INT 13,08 : load with number of drives
  1080. * all others: restore original value
  1081. */
  1082. "cmpb $0x15, -1(%%bp)\n\t"
  1083. "jne 2f\n\t"
  1084. "testb $0x80, -4(%%bp)\n\t"
  1085. "jnz 3f\n\t"
  1086. "\n2:\n\t"
  1087. "movb -4(%%bp), %%dl\n\t"
  1088. "cmpb $0x08, -1(%%bp)\n\t"
  1089. "jne 3f\n\t"
  1090. "testb $0x80, %%dl\n\t"
  1091. "movb %%cs:num_drives, %%dl\n\t"
  1092. "jnz 3f\n\t"
  1093. "movb %%cs:num_fdds, %%dl\n\t"
  1094. /* Return */
  1095. "\n3:\n\t"
  1096. "movw %%bp, %%sp\n\t"
  1097. "popw %%bp\n\t"
  1098. "iret\n\t" ) : : );
  1099. hook_bios_interrupt ( 0x13, ( intptr_t ) int13_wrapper, &int13_vector );
  1100. }
  1101. /**
  1102. * Unhook INT 13 handler
  1103. */
  1104. static void int13_unhook_vector ( void ) {
  1105. unhook_bios_interrupt ( 0x13, ( intptr_t ) int13_wrapper,
  1106. &int13_vector );
  1107. }
  1108. /**
  1109. * Hook INT 13 SAN device
  1110. *
  1111. * @v drive Drive number
  1112. * @v uris List of URIs
  1113. * @v count Number of URIs
  1114. * @v flags Flags
  1115. * @ret drive Drive number, or negative error
  1116. *
  1117. * Registers the drive with the INT 13 emulation subsystem, and hooks
  1118. * the INT 13 interrupt vector (if not already hooked).
  1119. */
  1120. static int int13_hook ( unsigned int drive, struct uri **uris,
  1121. unsigned int count, unsigned int flags ) {
  1122. struct san_device *sandev;
  1123. struct int13_data *int13;
  1124. unsigned int natural_drive;
  1125. void *scratch;
  1126. int need_hook = ( ! have_sandevs() );
  1127. int rc;
  1128. /* Calculate natural drive number */
  1129. int13_sync_num_drives();
  1130. natural_drive = ( ( drive & 0x80 ) ? ( num_drives | 0x80 ) : num_fdds );
  1131. /* Use natural drive number if directed to do so */
  1132. if ( ( drive & 0x7f ) == 0x7f )
  1133. drive = natural_drive;
  1134. /* Allocate SAN device */
  1135. sandev = alloc_sandev ( uris, count, sizeof ( *int13 ) );
  1136. if ( ! sandev ) {
  1137. rc = -ENOMEM;
  1138. goto err_alloc;
  1139. }
  1140. int13 = sandev->priv;
  1141. int13->natural_drive = natural_drive;
  1142. /* Register SAN device */
  1143. if ( ( rc = register_sandev ( sandev, drive, flags ) ) != 0 ) {
  1144. DBGC ( sandev, "INT13 drive %02x could not register: %s\n",
  1145. drive, strerror ( rc ) );
  1146. goto err_register;
  1147. }
  1148. /* Allocate scratch area */
  1149. scratch = malloc ( sandev_blksize ( sandev ) );
  1150. if ( ! scratch )
  1151. goto err_alloc_scratch;
  1152. /* Parse parameters, if present */
  1153. if ( sandev->is_cdrom &&
  1154. ( ( rc = int13_parse_eltorito ( sandev, scratch ) ) != 0 ) )
  1155. goto err_parse_eltorito;
  1156. /* Give drive a default geometry, if applicable */
  1157. if ( ( sandev_blksize ( sandev ) == INT13_BLKSIZE ) &&
  1158. ( ( rc = int13_guess_geometry ( sandev, scratch ) ) != 0 ) )
  1159. goto err_guess_geometry;
  1160. DBGC ( sandev, "INT13 drive %02x (naturally %02x) registered with "
  1161. "C/H/S geometry %d/%d/%d\n",
  1162. sandev->drive, int13->natural_drive, int13->cylinders,
  1163. int13->heads, int13->sectors_per_track );
  1164. /* Hook INT 13 vector if not already hooked */
  1165. if ( need_hook ) {
  1166. int13_hook_vector();
  1167. devices_get();
  1168. }
  1169. /* Update BIOS drive count */
  1170. int13_sync_num_drives();
  1171. free ( scratch );
  1172. return drive;
  1173. err_guess_geometry:
  1174. err_parse_eltorito:
  1175. free ( scratch );
  1176. err_alloc_scratch:
  1177. unregister_sandev ( sandev );
  1178. err_register:
  1179. sandev_put ( sandev );
  1180. err_alloc:
  1181. return rc;
  1182. }
  1183. /**
  1184. * Unhook INT 13 SAN device
  1185. *
  1186. * @v drive Drive number
  1187. *
  1188. * Unregisters the drive from the INT 13 emulation subsystem. If this
  1189. * is the last SAN device, the INT 13 vector is unhooked (if
  1190. * possible).
  1191. */
  1192. static void int13_unhook ( unsigned int drive ) {
  1193. struct san_device *sandev;
  1194. /* Find drive */
  1195. sandev = sandev_find ( drive );
  1196. if ( ! sandev ) {
  1197. DBG ( "INT13 cannot find drive %02x\n", drive );
  1198. return;
  1199. }
  1200. /* Unregister SAN device */
  1201. unregister_sandev ( sandev );
  1202. /* Should adjust BIOS drive count, but it's difficult
  1203. * to do so reliably.
  1204. */
  1205. DBGC ( sandev, "INT13 drive %02x unregistered\n", sandev->drive );
  1206. /* Unhook INT 13 vector if no more drives */
  1207. if ( ! have_sandevs() ) {
  1208. devices_put();
  1209. int13_unhook_vector();
  1210. }
  1211. /* Drop reference to drive */
  1212. sandev_put ( sandev );
  1213. }
  1214. /**
  1215. * Load and verify master boot record from INT 13 drive
  1216. *
  1217. * @v drive Drive number
  1218. * @v address Boot code address to fill in
  1219. * @ret rc Return status code
  1220. */
  1221. static int int13_load_mbr ( unsigned int drive, struct segoff *address ) {
  1222. uint16_t status;
  1223. int discard_b, discard_c, discard_d;
  1224. uint16_t magic;
  1225. /* Use INT 13, 02 to read the MBR */
  1226. address->segment = 0;
  1227. address->offset = 0x7c00;
  1228. __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
  1229. "pushl %%ebx\n\t"
  1230. "popw %%bx\n\t"
  1231. "popw %%es\n\t"
  1232. "stc\n\t"
  1233. "sti\n\t"
  1234. "int $0x13\n\t"
  1235. "sti\n\t" /* BIOS bugs */
  1236. "jc 1f\n\t"
  1237. "xorw %%ax, %%ax\n\t"
  1238. "\n1:\n\t"
  1239. "popw %%es\n\t" )
  1240. : "=a" ( status ), "=b" ( discard_b ),
  1241. "=c" ( discard_c ), "=d" ( discard_d )
  1242. : "a" ( 0x0201 ), "b" ( *address ),
  1243. "c" ( 1 ), "d" ( drive ) );
  1244. if ( status ) {
  1245. DBG ( "INT13 drive %02x could not read MBR (status %04x)\n",
  1246. drive, status );
  1247. return -EIO;
  1248. }
  1249. /* Check magic signature */
  1250. get_real ( magic, address->segment,
  1251. ( address->offset +
  1252. offsetof ( struct master_boot_record, magic ) ) );
  1253. if ( magic != INT13_MBR_MAGIC ) {
  1254. DBG ( "INT13 drive %02x does not contain a valid MBR\n",
  1255. drive );
  1256. return -ENOEXEC;
  1257. }
  1258. return 0;
  1259. }
  1260. /** El Torito boot catalog command packet */
  1261. static struct int13_cdrom_boot_catalog_command __data16 ( eltorito_cmd ) = {
  1262. .size = sizeof ( struct int13_cdrom_boot_catalog_command ),
  1263. .count = 1,
  1264. .buffer = 0x7c00,
  1265. .start = 0,
  1266. };
  1267. #define eltorito_cmd __use_data16 ( eltorito_cmd )
  1268. /** El Torito disk address packet */
  1269. static struct int13_disk_address __bss16 ( eltorito_address );
  1270. #define eltorito_address __use_data16 ( eltorito_address )
  1271. /**
  1272. * Load and verify El Torito boot record from INT 13 drive
  1273. *
  1274. * @v drive Drive number
  1275. * @v address Boot code address to fill in
  1276. * @ret rc Return status code
  1277. */
  1278. static int int13_load_eltorito ( unsigned int drive, struct segoff *address ) {
  1279. struct {
  1280. struct eltorito_validation_entry valid;
  1281. struct eltorito_boot_entry boot;
  1282. } __attribute__ (( packed )) catalog;
  1283. uint16_t status;
  1284. /* Use INT 13, 4d to read the boot catalog */
  1285. __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
  1286. "sti\n\t"
  1287. "int $0x13\n\t"
  1288. "sti\n\t" /* BIOS bugs */
  1289. "jc 1f\n\t"
  1290. "xorw %%ax, %%ax\n\t"
  1291. "\n1:\n\t" )
  1292. : "=a" ( status )
  1293. : "a" ( 0x4d00 ), "d" ( drive ),
  1294. "S" ( __from_data16 ( &eltorito_cmd ) ) );
  1295. if ( status ) {
  1296. DBG ( "INT13 drive %02x could not read El Torito boot catalog "
  1297. "(status %04x)\n", drive, status );
  1298. return -EIO;
  1299. }
  1300. copy_from_user ( &catalog, phys_to_user ( eltorito_cmd.buffer ), 0,
  1301. sizeof ( catalog ) );
  1302. /* Sanity checks */
  1303. if ( catalog.valid.platform_id != ELTORITO_PLATFORM_X86 ) {
  1304. DBG ( "INT13 drive %02x El Torito specifies unknown platform "
  1305. "%02x\n", drive, catalog.valid.platform_id );
  1306. return -ENOEXEC;
  1307. }
  1308. if ( catalog.boot.indicator != ELTORITO_BOOTABLE ) {
  1309. DBG ( "INT13 drive %02x El Torito is not bootable\n", drive );
  1310. return -ENOEXEC;
  1311. }
  1312. if ( catalog.boot.media_type != ELTORITO_NO_EMULATION ) {
  1313. DBG ( "INT13 drive %02x El Torito requires emulation "
  1314. "type %02x\n", drive, catalog.boot.media_type );
  1315. return -ENOTSUP;
  1316. }
  1317. DBG ( "INT13 drive %02x El Torito boot image at LBA %08x (count %d)\n",
  1318. drive, catalog.boot.start, catalog.boot.length );
  1319. address->segment = ( catalog.boot.load_segment ?
  1320. catalog.boot.load_segment : 0x7c0 );
  1321. address->offset = 0;
  1322. DBG ( "INT13 drive %02x El Torito boot image loads at %04x:%04x\n",
  1323. drive, address->segment, address->offset );
  1324. /* Use INT 13, 42 to read the boot image */
  1325. eltorito_address.bufsize =
  1326. offsetof ( typeof ( eltorito_address ), buffer_phys );
  1327. eltorito_address.count = catalog.boot.length;
  1328. eltorito_address.buffer = *address;
  1329. eltorito_address.lba = catalog.boot.start;
  1330. __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
  1331. "sti\n\t"
  1332. "int $0x13\n\t"
  1333. "sti\n\t" /* BIOS bugs */
  1334. "jc 1f\n\t"
  1335. "xorw %%ax, %%ax\n\t"
  1336. "\n1:\n\t" )
  1337. : "=a" ( status )
  1338. : "a" ( 0x4200 ), "d" ( drive ),
  1339. "S" ( __from_data16 ( &eltorito_address ) ) );
  1340. if ( status ) {
  1341. DBG ( "INT13 drive %02x could not read El Torito boot image "
  1342. "(status %04x)\n", drive, status );
  1343. return -EIO;
  1344. }
  1345. return 0;
  1346. }
  1347. /**
  1348. * Attempt to boot from an INT 13 drive
  1349. *
  1350. * @v drive Drive number
  1351. * @v filename Filename (or NULL to use default)
  1352. * @ret rc Return status code
  1353. *
  1354. * This boots from the specified INT 13 drive by loading the Master
  1355. * Boot Record to 0000:7c00 and jumping to it. INT 18 is hooked to
  1356. * capture an attempt by the MBR to boot the next device. (This is
  1357. * the closest thing to a return path from an MBR).
  1358. *
  1359. * Note that this function can never return success, by definition.
  1360. */
  1361. static int int13_boot ( unsigned int drive, const char *filename __unused ) {
  1362. struct memory_map memmap;
  1363. struct segoff address;
  1364. int rc;
  1365. /* Look for a usable boot sector */
  1366. if ( ( ( rc = int13_load_mbr ( drive, &address ) ) != 0 ) &&
  1367. ( ( rc = int13_load_eltorito ( drive, &address ) ) != 0 ) )
  1368. return rc;
  1369. /* Dump out memory map prior to boot, if memmap debugging is
  1370. * enabled. Not required for program flow, but we have so
  1371. * many problems that turn out to be memory-map related that
  1372. * it's worth doing.
  1373. */
  1374. get_memmap ( &memmap );
  1375. /* Jump to boot sector */
  1376. if ( ( rc = call_bootsector ( address.segment, address.offset,
  1377. drive ) ) != 0 ) {
  1378. DBG ( "INT13 drive %02x boot returned: %s\n",
  1379. drive, strerror ( rc ) );
  1380. return rc;
  1381. }
  1382. return -ECANCELED; /* -EIMPOSSIBLE */
  1383. }
  1384. /** Maximum size of boot firmware table(s) */
  1385. #define XBFTAB_SIZE 768
  1386. /** Alignment of boot firmware table entries */
  1387. #define XBFTAB_ALIGN 16
  1388. /** The boot firmware table(s) generated by iPXE */
  1389. static uint8_t __bss16_array ( xbftab, [XBFTAB_SIZE] )
  1390. __attribute__ (( aligned ( XBFTAB_ALIGN ) ));
  1391. #define xbftab __use_data16 ( xbftab )
  1392. /** Total used length of boot firmware tables */
  1393. static size_t xbftab_used;
  1394. /**
  1395. * Install ACPI table
  1396. *
  1397. * @v acpi ACPI description header
  1398. * @ret rc Return status code
  1399. */
  1400. static int int13_install ( struct acpi_header *acpi ) {
  1401. struct segoff xbft_address;
  1402. struct acpi_header *installed;
  1403. size_t len;
  1404. /* Check length */
  1405. len = acpi->length;
  1406. if ( len > ( sizeof ( xbftab ) - xbftab_used ) ) {
  1407. DBGC ( acpi, "INT13 out of space for %s table\n",
  1408. acpi_name ( acpi->signature ) );
  1409. return -ENOSPC;
  1410. }
  1411. /* Install table */
  1412. installed = ( ( ( void * ) xbftab ) + xbftab_used );
  1413. memcpy ( installed, acpi, len );
  1414. xbft_address.segment = rm_ds;
  1415. xbft_address.offset = __from_data16 ( installed );
  1416. /* Fill in common parameters */
  1417. strncpy ( installed->oem_id, "FENSYS",
  1418. sizeof ( installed->oem_id ) );
  1419. strncpy ( installed->oem_table_id, "iPXE",
  1420. sizeof ( installed->oem_table_id ) );
  1421. /* Fix checksum */
  1422. acpi_fix_checksum ( installed );
  1423. /* Update used length */
  1424. xbftab_used = ( ( xbftab_used + len + XBFTAB_ALIGN - 1 ) &
  1425. ~( XBFTAB_ALIGN - 1 ) );
  1426. DBGC ( acpi, "INT13 installed %s:\n",
  1427. acpi_name ( installed->signature ) );
  1428. DBGC_HDA ( acpi, xbft_address, installed, len );
  1429. return 0;
  1430. }
  1431. /**
  1432. * Describe SAN devices for SAN-booted operating system
  1433. *
  1434. * @ret rc Return status code
  1435. */
  1436. static int int13_describe ( void ) {
  1437. int rc;
  1438. /* Clear tables */
  1439. memset ( &xbftab, 0, sizeof ( xbftab ) );
  1440. xbftab_used = 0;
  1441. /* Install ACPI tables */
  1442. if ( ( rc = acpi_install ( int13_install ) ) != 0 ) {
  1443. DBG ( "INT13 could not install ACPI tables: %s\n",
  1444. strerror ( rc ) );
  1445. return rc;
  1446. }
  1447. return 0;
  1448. }
  1449. PROVIDE_SANBOOT ( pcbios, san_hook, int13_hook );
  1450. PROVIDE_SANBOOT ( pcbios, san_unhook, int13_unhook );
  1451. PROVIDE_SANBOOT ( pcbios, san_boot, int13_boot );
  1452. PROVIDE_SANBOOT ( pcbios, san_describe, int13_describe );