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

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