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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <limits.h>
  19. #include <assert.h>
  20. #include <gpxe/list.h>
  21. #include <gpxe/blockdev.h>
  22. #include <realmode.h>
  23. #include <bios.h>
  24. #include <biosint.h>
  25. #include <int13.h>
  26. /** @file
  27. *
  28. * INT 13 emulation
  29. *
  30. * This module provides a mechanism for exporting block devices via
  31. * the BIOS INT 13 disk interrupt interface.
  32. *
  33. */
  34. /** Vector for chaining to other INT 13 handlers */
  35. static struct segoff __text16 ( int13_vector );
  36. #define int13_vector __use_text16 ( int13_vector )
  37. /** Assembly wrapper */
  38. extern void int13_wrapper ( void );
  39. /** List of registered emulated drives */
  40. static LIST_HEAD ( drives );
  41. /**
  42. * Convert CHS address to linear address
  43. *
  44. * @v drive Emulated drive
  45. * @v ch Low bits of cylinder number
  46. * @v cl (bits 7:6) High bits of cylinder number
  47. * @v cl (bits 5:0) Sector number
  48. * @v dh Head number
  49. * @ret lba LBA address
  50. *
  51. */
  52. static unsigned long chs_to_lba ( struct int13_drive *drive,
  53. struct i386_all_regs *ix86 ) {
  54. unsigned int cylinder;
  55. unsigned int head;
  56. unsigned int sector;
  57. unsigned long lba;
  58. cylinder = ( ( ( ix86->regs.cl & 0xc0 ) << 8 ) | ix86->regs.ch );
  59. head = ix86->regs.dh;
  60. sector = ( ix86->regs.cl & 0x3f );
  61. assert ( cylinder < drive->cylinders );
  62. assert ( head < drive->heads );
  63. assert ( ( sector >= 1 ) && ( sector <= drive->sectors_per_track ) );
  64. lba = ( ( ( ( cylinder * drive->heads ) + head )
  65. * drive->sectors_per_track ) + sector - 1 );
  66. DBG ( "C/H/S address %x/%x/%x -> LBA %x\n",
  67. cylinder, head, sector, lba );
  68. return lba;
  69. }
  70. /**
  71. * Read from drive to real-mode data buffer
  72. *
  73. * @v drive Emulated drive
  74. * @v lba LBA starting sector number
  75. * @v data Data buffer
  76. * @v count Number of sectors to read
  77. * @ret status Status code
  78. */
  79. static int int13_read ( struct int13_drive *drive, uint64_t lba,
  80. struct segoff data, unsigned long count ) {
  81. struct block_device *blockdev = drive->blockdev;
  82. size_t blksize = blockdev->blksize;
  83. uint8_t buffer[blksize];
  84. int rc;
  85. DBG ( "Read %lx sectors from %llx to %04x:%04x\n", count,
  86. ( unsigned long long ) lba, data.segment, data.offset );
  87. while ( count-- ) {
  88. if ( ( rc = blockdev->read ( blockdev, lba, buffer ) ) != 0 )
  89. return INT13_STATUS_READ_ERROR;
  90. copy_to_real ( data.segment, data.offset, buffer, blksize );
  91. data.offset += blksize;
  92. lba++;
  93. }
  94. return 0;
  95. }
  96. /**
  97. * Write from real-mode data buffer to drive
  98. *
  99. * @v drive Emulated drive
  100. * @v lba LBA starting sector number
  101. * @v data Data buffer
  102. * @v count Number of sectors to read
  103. * @ret status Status code
  104. */
  105. static int int13_write ( struct int13_drive *drive, uint64_t lba,
  106. struct segoff data, unsigned long count ) {
  107. struct block_device *blockdev = drive->blockdev;
  108. size_t blksize = blockdev->blksize;
  109. uint8_t buffer[blksize];
  110. int rc;
  111. DBG ( "Write %lx sectors from %04x:%04x to %llx\n", count,
  112. data.segment, data.offset, ( unsigned long long ) lba );
  113. while ( count-- ) {
  114. copy_from_real ( buffer, data.segment, data.offset, blksize );
  115. if ( ( rc = blockdev->write ( blockdev, lba, buffer ) ) != 0 )
  116. return INT13_STATUS_WRITE_ERROR;
  117. data.offset += blksize;
  118. lba++;
  119. }
  120. return 0;
  121. }
  122. /**
  123. * INT 13, 00 - Reset disk system
  124. *
  125. * @v drive Emulated drive
  126. * @ret status Status code
  127. */
  128. static int int13_reset ( struct int13_drive *drive __unused,
  129. struct i386_all_regs *ix86 __unused ) {
  130. DBG ( "Reset drive\n" );
  131. return 0;
  132. }
  133. /**
  134. * INT 13, 01 - Get status of last operation
  135. *
  136. * @v drive Emulated drive
  137. * @ret status Status code
  138. */
  139. static int int13_get_last_status ( struct int13_drive *drive,
  140. struct i386_all_regs *ix86 __unused ) {
  141. DBG ( "Get status of last operation\n" );
  142. return drive->last_status;
  143. }
  144. /**
  145. * INT 13, 02 - Read sectors
  146. *
  147. * @v drive Emulated drive
  148. * @v al Number of sectors to read (must be nonzero)
  149. * @v ch Low bits of cylinder number
  150. * @v cl (bits 7:6) High bits of cylinder number
  151. * @v cl (bits 5:0) Sector number
  152. * @v dh Head number
  153. * @v es:bx Data buffer
  154. * @ret status Status code
  155. * @ret al Number of sectors read
  156. */
  157. static int int13_read_sectors ( struct int13_drive *drive,
  158. struct i386_all_regs *ix86 ) {
  159. unsigned long lba = chs_to_lba ( drive, ix86 );
  160. unsigned int count = ix86->regs.al;
  161. struct segoff data = {
  162. .segment = ix86->segs.es,
  163. .offset = ix86->regs.bx,
  164. };
  165. if ( drive->blockdev->blksize != INT13_BLKSIZE ) {
  166. DBG ( "Invalid blocksize (%d) for non-extended read\n",
  167. drive->blockdev->blksize );
  168. return INT13_STATUS_INVALID;
  169. }
  170. return int13_read ( drive, lba, data, count );
  171. }
  172. /**
  173. * INT 13, 03 - Write sectors
  174. *
  175. * @v drive Emulated drive
  176. * @v al Number of sectors to write (must be nonzero)
  177. * @v ch Low bits of cylinder number
  178. * @v cl (bits 7:6) High bits of cylinder number
  179. * @v cl (bits 5:0) Sector number
  180. * @v dh Head number
  181. * @v es:bx Data buffer
  182. * @ret status Status code
  183. * @ret al Number of sectors written
  184. */
  185. static int int13_write_sectors ( struct int13_drive *drive,
  186. struct i386_all_regs *ix86 ) {
  187. unsigned long lba = chs_to_lba ( drive, ix86 );
  188. unsigned int count = ix86->regs.al;
  189. struct segoff data = {
  190. .segment = ix86->segs.es,
  191. .offset = ix86->regs.bx,
  192. };
  193. if ( drive->blockdev->blksize != INT13_BLKSIZE ) {
  194. DBG ( "Invalid blocksize (%d) for non-extended write\n",
  195. drive->blockdev->blksize );
  196. return INT13_STATUS_INVALID;
  197. }
  198. return int13_write ( drive, lba, data, count );
  199. }
  200. /**
  201. * INT 13, 08 - Get drive parameters
  202. *
  203. * @v drive Emulated drive
  204. * @ret status Status code
  205. * @ret ch Low bits of maximum cylinder number
  206. * @ret cl (bits 7:6) High bits of maximum cylinder number
  207. * @ret cl (bits 5:0) Maximum sector number
  208. * @ret dh Maximum head number
  209. * @ret dl Number of drives
  210. */
  211. static int int13_get_parameters ( struct int13_drive *drive,
  212. struct i386_all_regs *ix86 ) {
  213. unsigned int max_cylinder = drive->cylinders - 1;
  214. unsigned int max_head = drive->heads - 1;
  215. unsigned int max_sector = drive->sectors_per_track; /* sic */
  216. DBG ( "Get drive parameters\n" );
  217. ix86->regs.ch = ( max_cylinder & 0xff );
  218. ix86->regs.cl = ( ( ( max_cylinder >> 8 ) << 6 ) | max_sector );
  219. ix86->regs.dh = max_head;
  220. get_real ( ix86->regs.dl, BDA_SEG, BDA_NUM_DRIVES );
  221. return 0;
  222. }
  223. /**
  224. * INT 13, 42 - Extended read
  225. *
  226. * @v drive Emulated drive
  227. * @v ds:si Disk address packet
  228. * @ret status Status code
  229. */
  230. static int int13_extended_read ( struct int13_drive *drive,
  231. struct i386_all_regs *ix86 ) {
  232. struct int13_disk_address addr;
  233. copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si,
  234. sizeof ( addr ) );
  235. return int13_read ( drive, addr.lba, addr.buffer, addr.count );
  236. }
  237. /**
  238. * INT 13, 43 - Extended write
  239. *
  240. * @v drive Emulated drive
  241. * @v ds:si Disk address packet
  242. * @ret status Status code
  243. */
  244. static int int13_extended_write ( struct int13_drive *drive,
  245. struct i386_all_regs *ix86 ) {
  246. struct int13_disk_address addr;
  247. copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si,
  248. sizeof ( addr ) );
  249. return int13_write ( drive, addr.lba, addr.buffer, addr.count );
  250. }
  251. /**
  252. * INT 13, 48 - Get extended parameters
  253. *
  254. * @v drive Emulated drive
  255. * @v ds:si Drive parameter table
  256. * @ret status Status code
  257. */
  258. static int int13_get_extended_parameters ( struct int13_drive *drive,
  259. struct i386_all_regs *ix86 ) {
  260. struct int13_disk_parameters params = {
  261. .bufsize = sizeof ( params ),
  262. .flags = INT13_FL_DMA_TRANSPARENT,
  263. .cylinders = drive->cylinders,
  264. .heads = drive->heads,
  265. .sectors_per_track = drive->sectors_per_track,
  266. .sectors = drive->blockdev->blocks,
  267. .sector_size = drive->blockdev->blksize,
  268. };
  269. DBG ( "Get extended drive parameters to %04x:%04x\n",
  270. ix86->segs.ds, ix86->regs.si );
  271. copy_to_real ( ix86->segs.ds, ix86->regs.si, &params,
  272. sizeof ( params ) );
  273. return 0;
  274. }
  275. /**
  276. * INT 13 handler
  277. *
  278. */
  279. static void int13 ( struct i386_all_regs *ix86 ) {
  280. struct int13_drive *drive;
  281. int status;
  282. list_for_each_entry ( drive, &drives, list ) {
  283. if ( drive->drive != ix86->regs.dl )
  284. continue;
  285. DBG ( "INT 13, %02x on drive %02x\n", ix86->regs.ah,
  286. ix86->regs.dl );
  287. switch ( ix86->regs.ah ) {
  288. case INT13_RESET:
  289. status = int13_reset ( drive, ix86 );
  290. break;
  291. case INT13_GET_LAST_STATUS:
  292. status = int13_get_last_status ( drive, ix86 );
  293. break;
  294. case INT13_READ_SECTORS:
  295. status = int13_read_sectors ( drive, ix86 );
  296. break;
  297. case INT13_WRITE_SECTORS:
  298. status = int13_write_sectors ( drive, ix86 );
  299. break;
  300. case INT13_GET_PARAMETERS:
  301. status = int13_get_parameters ( drive, ix86 );
  302. break;
  303. case INT13_EXTENDED_READ:
  304. status = int13_extended_read ( drive, ix86 );
  305. break;
  306. case INT13_EXTENDED_WRITE:
  307. status = int13_extended_write ( drive, ix86 );
  308. break;
  309. case INT13_GET_EXTENDED_PARAMETERS:
  310. status = int13_get_extended_parameters ( drive, ix86 );
  311. break;
  312. default:
  313. DBG ( "Unrecognised INT 13\n" );
  314. status = INT13_STATUS_INVALID;
  315. break;
  316. }
  317. /* Store status for INT 13,01 */
  318. drive->last_status = status;
  319. /* All functions return status via %ah and CF */
  320. ix86->regs.ah = status;
  321. if ( status ) {
  322. ix86->flags |= CF;
  323. DBG ( "INT13 failed with status %x\n", status );
  324. }
  325. /* Set OF to indicate to wrapper not to chain this call */
  326. ix86->flags |= OF;
  327. }
  328. }
  329. /**
  330. * Hook INT 13 handler
  331. *
  332. */
  333. static void hook_int13 ( void ) {
  334. /* Assembly wrapper to call int13(). int13() sets OF if we
  335. * should not chain to the previous handler. (The wrapper
  336. * clears CF and OF before calling int13()).
  337. */
  338. __asm__ __volatile__ ( ".section \".text16\", \"ax\", @progbits\n\t"
  339. ".code16\n\t"
  340. "\nint13_wrapper:\n\t"
  341. "orb $0, %%al\n\t" /* clear CF and OF */
  342. "pushl %0\n\t" /* call int13() */
  343. "data32 call prot_call\n\t"
  344. "jo 1f\n\t" /* chain if OF not set */
  345. "pushfw\n\t"
  346. "lcall *%%cs:int13_vector\n\t"
  347. "\n1:\n\t"
  348. "call 2f\n\t" /* return with flags intact */
  349. "lret $2\n\t"
  350. "\n2:\n\t"
  351. "ret $4\n\t"
  352. ".previous\n\t"
  353. ".code32\n\t" : :
  354. "i" ( int13 ) );
  355. hook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
  356. &int13_vector );
  357. }
  358. /**
  359. * Unhook INT 13 handler
  360. */
  361. static void unhook_int13 ( void ) {
  362. unhook_bios_interrupt ( 0x13, ( unsigned int ) int13_wrapper,
  363. &int13_vector );
  364. }
  365. /**
  366. * Register INT 13 emulated drive
  367. *
  368. * @v drive Emulated drive
  369. *
  370. * Registers the drive with the INT 13 emulation subsystem, and hooks
  371. * the INT 13 interrupt vector (if not already hooked).
  372. *
  373. * The underlying block device must be valid. A drive number and
  374. * geometry will be assigned if left blank.
  375. */
  376. void register_int13_drive ( struct int13_drive *drive ) {
  377. uint8_t num_drives;
  378. unsigned long blocks;
  379. unsigned long blocks_per_cyl;
  380. /* Give drive a default geometry if none specified */
  381. if ( ! drive->heads )
  382. drive->heads = 255;
  383. if ( ! drive->sectors_per_track )
  384. drive->sectors_per_track = 63;
  385. if ( ! drive->cylinders ) {
  386. /* Avoid attempting a 64-bit divide on a 32-bit system */
  387. blocks = ( ( drive->blockdev->blocks <= ULONG_MAX ) ?
  388. drive->blockdev->blocks : ULONG_MAX );
  389. blocks_per_cyl = ( drive->heads * drive->sectors_per_track );
  390. assert ( blocks_per_cyl != 0 );
  391. drive->cylinders = ( blocks / blocks_per_cyl );
  392. }
  393. /* Assign drive number if none specified, update BIOS drive count */
  394. get_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
  395. if ( ! drive->drive )
  396. drive->drive = ( num_drives | 0x80 );
  397. if ( num_drives <= ( drive->drive & 0x7f ) )
  398. num_drives = ( ( drive->drive & 0x7f ) + 1 );
  399. put_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES );
  400. DBG ( "Registered INT13 drive %02x with C/H/S geometry %d/%d/%d\n",
  401. drive->drive, drive->cylinders, drive->heads,
  402. drive->sectors_per_track );
  403. /* Hook INT 13 vector if not already hooked */
  404. if ( list_empty ( &drives ) )
  405. hook_int13();
  406. /* Add to list of emulated drives */
  407. list_add ( &drive->list, &drives );
  408. }
  409. /**
  410. * Unregister INT 13 emulated drive
  411. *
  412. * @v drive Emulated drive
  413. *
  414. * Unregisters the drive from the INT 13 emulation subsystem. If this
  415. * is the last emulated drive, the INT 13 vector is unhooked (if
  416. * possible).
  417. */
  418. void unregister_int13_drive ( struct int13_drive *drive ) {
  419. /* Remove from list of emulated drives */
  420. list_del ( &drive->list );
  421. DBG ( "Unregistered INT13 drive %02x\n", drive->drive );
  422. /* Unhook INT 13 vector if no more drives */
  423. if ( list_empty ( &drives ) )
  424. unhook_int13();
  425. }