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

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