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.

comboot_call.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * Copyright (C) 2008 Daniel Verkamp <daniel@drv.nu>.
  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. /**
  19. * @file SYSLINUX COMBOOT API
  20. *
  21. */
  22. #include <errno.h>
  23. #include <realmode.h>
  24. #include <biosint.h>
  25. #include <console.h>
  26. #include <stdlib.h>
  27. #include <comboot.h>
  28. #include <bzimage.h>
  29. #include <pxe_call.h>
  30. #include <setjmp.h>
  31. #include <string.h>
  32. #include <gpxe/posix_io.h>
  33. #include <gpxe/process.h>
  34. #include <gpxe/serial.h>
  35. #include <gpxe/init.h>
  36. /** The "SYSLINUX" version string */
  37. static char __data16_array ( syslinux_version, [] ) = "gPXE " VERSION;
  38. #define syslinux_version __use_data16 ( syslinux_version )
  39. /** The "SYSLINUX" copyright string */
  40. static char __data16_array ( syslinux_copyright, [] ) = "http://etherboot.org";
  41. #define syslinux_copyright __use_data16 ( syslinux_copyright )
  42. static char __data16_array ( syslinux_configuration_file, [] ) = "";
  43. #define syslinux_configuration_file __use_data16 ( syslinux_configuration_file )
  44. /** Feature flags */
  45. static uint8_t __data16 ( comboot_feature_flags ) = COMBOOT_FEATURE_IDLE_LOOP;
  46. #define comboot_feature_flags __use_data16 ( comboot_feature_flags )
  47. static struct segoff __text16 ( int20_vector );
  48. #define int20_vector __use_text16 ( int20_vector )
  49. static struct segoff __text16 ( int21_vector );
  50. #define int21_vector __use_text16 ( int21_vector )
  51. static struct segoff __text16 ( int22_vector );
  52. #define int22_vector __use_text16 ( int22_vector )
  53. extern void int20_wrapper ( void );
  54. extern void int21_wrapper ( void );
  55. extern void int22_wrapper ( void );
  56. /* setjmp/longjmp context buffer used to return after loading an image */
  57. jmp_buf comboot_return;
  58. /* Command line to execute when returning via comboot_return
  59. * with COMBOOT_RETURN_RUN_KERNEL
  60. */
  61. char *comboot_kernel_cmdline;
  62. /* Mode flags set by INT 22h AX=0017h */
  63. static uint16_t comboot_graphics_mode = 0;
  64. /**
  65. * Print a string with a particular terminator
  66. */
  67. static void print_user_string ( unsigned int segment, unsigned int offset, char terminator ) {
  68. int i = 0;
  69. char c;
  70. userptr_t str = real_to_user ( segment, offset );
  71. for ( ; ; ) {
  72. copy_from_user ( &c, str, i, 1 );
  73. if ( c == terminator ) break;
  74. putchar ( c );
  75. i++;
  76. }
  77. }
  78. /**
  79. * Perform a series of memory copies from a list in low memory
  80. */
  81. static void shuffle ( unsigned int list_segment, unsigned int list_offset, unsigned int count )
  82. {
  83. comboot_shuffle_descriptor shuf[COMBOOT_MAX_SHUFFLE_DESCRIPTORS];
  84. unsigned int i;
  85. /* Copy shuffle descriptor list so it doesn't get overwritten */
  86. copy_from_user ( shuf, real_to_user ( list_segment, list_offset ), 0,
  87. count * sizeof( comboot_shuffle_descriptor ) );
  88. /* Do the copies */
  89. for ( i = 0; i < count; i++ ) {
  90. userptr_t src_u = phys_to_user ( shuf[ i ].src );
  91. userptr_t dest_u = phys_to_user ( shuf[ i ].dest );
  92. if ( shuf[ i ].src == 0xFFFFFFFF ) {
  93. /* Fill with 0 instead of copying */
  94. memset_user ( dest_u, 0, 0, shuf[ i ].len );
  95. } else if ( shuf[ i ].dest == 0xFFFFFFFF ) {
  96. /* Copy new list of descriptors */
  97. count = shuf[ i ].len / sizeof( comboot_shuffle_descriptor );
  98. assert ( count <= COMBOOT_MAX_SHUFFLE_DESCRIPTORS );
  99. copy_from_user ( shuf, src_u, 0, shuf[ i ].len );
  100. i = -1;
  101. } else {
  102. /* Regular copy */
  103. memmove_user ( dest_u, 0, src_u, 0, shuf[ i ].len );
  104. }
  105. }
  106. }
  107. /**
  108. * Set default text mode
  109. */
  110. void comboot_force_text_mode ( void ) {
  111. if ( comboot_graphics_mode & COMBOOT_VIDEO_VESA ) {
  112. /* Set VGA mode 3 via VESA VBE mode set */
  113. __asm__ __volatile__ (
  114. REAL_CODE (
  115. "mov $0x4F02, %%ax\n\t"
  116. "mov $0x03, %%bx\n\t"
  117. "int $0x10\n\t"
  118. )
  119. : : );
  120. } else if ( comboot_graphics_mode & COMBOOT_VIDEO_GRAPHICS ) {
  121. /* Set VGA mode 3 via standard VGA mode set */
  122. __asm__ __volatile__ (
  123. REAL_CODE (
  124. "mov $0x03, %%ax\n\t"
  125. "int $0x10\n\t"
  126. )
  127. : : );
  128. }
  129. comboot_graphics_mode = 0;
  130. }
  131. /**
  132. * Run the kernel specified in comboot_kernel_cmdline
  133. */
  134. void comboot_run_kernel ( )
  135. {
  136. char *initrd;
  137. comboot_force_text_mode ( );
  138. DBG ( "COMBOOT: executing image '%s'\n", comboot_kernel_cmdline );
  139. /* Find initrd= parameter, if any */
  140. if ( ( initrd = strstr ( comboot_kernel_cmdline, "initrd=" ) ) ) {
  141. char old_char = '\0';
  142. char *initrd_end = strchr( initrd, ' ' );
  143. /* Replace space after end of parameter
  144. * with a nul terminator if this is not
  145. * the last parameter
  146. */
  147. if ( initrd_end ) {
  148. old_char = *initrd_end;
  149. *initrd_end = '\0';
  150. }
  151. /* Replace = with space to get 'initrd filename'
  152. * command suitable for system()
  153. */
  154. initrd[6] = ' ';
  155. DBG( "COMBOOT: loading initrd '%s'\n", initrd );
  156. system ( initrd );
  157. /* Restore space after parameter */
  158. if ( initrd_end ) {
  159. *initrd_end = old_char;
  160. }
  161. /* Restore = */
  162. initrd[6] = '=';
  163. }
  164. /* Load kernel */
  165. DBG ( "COMBOOT: loading kernel '%s'\n", comboot_kernel_cmdline );
  166. system ( comboot_kernel_cmdline );
  167. free ( comboot_kernel_cmdline );
  168. /* Boot */
  169. system ( "boot" );
  170. DBG ( "COMBOOT: back from executing command\n" );
  171. }
  172. /**
  173. * Terminate program interrupt handler
  174. */
  175. static __cdecl void int20 ( struct i386_all_regs *ix86 __unused ) {
  176. longjmp ( comboot_return, COMBOOT_RETURN_EXIT );
  177. }
  178. /**
  179. * DOS-compatible API
  180. */
  181. static __cdecl void int21 ( struct i386_all_regs *ix86 ) {
  182. ix86->flags |= CF;
  183. switch ( ix86->regs.ah ) {
  184. case 0x00:
  185. case 0x4C: /* Terminate program */
  186. longjmp ( comboot_return, COMBOOT_RETURN_EXIT );
  187. break;
  188. case 0x01: /* Get Key with Echo */
  189. case 0x08: /* Get Key without Echo */
  190. /* TODO: handle extended characters? */
  191. ix86->regs.al = getchar( );
  192. /* Enter */
  193. if ( ix86->regs.al == 0x0A )
  194. ix86->regs.al = 0x0D;
  195. if ( ix86->regs.ah == 0x01 )
  196. putchar ( ix86->regs.al );
  197. ix86->flags &= ~CF;
  198. break;
  199. case 0x02: /* Write Character */
  200. putchar ( ix86->regs.dl );
  201. ix86->flags &= ~CF;
  202. break;
  203. case 0x04: /* Write Character to Serial Port */
  204. serial_putc ( ix86->regs.dl );
  205. ix86->flags &= ~CF;
  206. break;
  207. case 0x09: /* Write DOS String to Console */
  208. print_user_string ( ix86->segs.ds, ix86->regs.dx, '$' );
  209. ix86->flags &= ~CF;
  210. break;
  211. case 0x0B: /* Check Keyboard */
  212. if ( iskey() )
  213. ix86->regs.al = 0xFF;
  214. else
  215. ix86->regs.al = 0x00;
  216. ix86->flags &= ~CF;
  217. break;
  218. case 0x30: /* Check DOS Version */
  219. /* Bottom halves all 0; top halves spell "SYSLINUX" */
  220. ix86->regs.eax = 0x59530000;
  221. ix86->regs.ebx = 0x4C530000;
  222. ix86->regs.ecx = 0x4E490000;
  223. ix86->regs.edx = 0x58550000;
  224. ix86->flags &= ~CF;
  225. break;
  226. default:
  227. DBG ( "COMBOOT unknown int21 function %02x\n", ix86->regs.ah );
  228. break;
  229. }
  230. }
  231. /**
  232. * SYSLINUX API
  233. */
  234. static __cdecl void int22 ( struct i386_all_regs *ix86 ) {
  235. ix86->flags |= CF;
  236. switch ( ix86->regs.ax ) {
  237. case 0x0001: /* Get Version */
  238. /* Number of INT 22h API functions available */
  239. ix86->regs.ax = 0x0018;
  240. /* SYSLINUX version number */
  241. ix86->regs.ch = 0; /* major */
  242. ix86->regs.cl = 0; /* minor */
  243. /* SYSLINUX derivative ID */
  244. ix86->regs.dl = BZI_LOADER_TYPE_GPXE;
  245. /* SYSLINUX version and copyright strings */
  246. ix86->segs.es = rm_ds;
  247. ix86->regs.si = ( ( unsigned ) __from_data16 ( syslinux_version ) );
  248. ix86->regs.di = ( ( unsigned ) __from_data16 ( syslinux_copyright ) );
  249. ix86->flags &= ~CF;
  250. break;
  251. case 0x0002: /* Write String */
  252. print_user_string ( ix86->segs.es, ix86->regs.bx, '\0' );
  253. ix86->flags &= ~CF;
  254. break;
  255. case 0x0003: /* Run command */
  256. {
  257. userptr_t cmd_u = real_to_user ( ix86->segs.es, ix86->regs.bx );
  258. int len = strlen_user ( cmd_u, 0 );
  259. char cmd[len + 1];
  260. copy_from_user ( cmd, cmd_u, 0, len + 1 );
  261. DBG ( "COMBOOT: executing command '%s'\n", cmd );
  262. comboot_kernel_cmdline = strdup ( cmd );
  263. DBG ( "COMBOOT: returning to run image...\n" );
  264. longjmp ( comboot_return, COMBOOT_RETURN_RUN_KERNEL );
  265. }
  266. break;
  267. case 0x0004: /* Run default command */
  268. /* FIXME: just exit for now */
  269. longjmp ( comboot_return, COMBOOT_RETURN_EXIT );
  270. break;
  271. case 0x0005: /* Force text mode */
  272. comboot_force_text_mode ( );
  273. ix86->flags &= ~CF;
  274. break;
  275. case 0x0006: /* Open file */
  276. {
  277. int fd;
  278. userptr_t file_u = real_to_user ( ix86->segs.es, ix86->regs.si );
  279. int len = strlen_user ( file_u, 0 );
  280. char file[len + 1];
  281. copy_from_user ( file, file_u, 0, len + 1 );
  282. if ( file[0] == '\0' ) {
  283. DBG ( "COMBOOT: attempted open with empty file name\n" );
  284. break;
  285. }
  286. DBG ( "COMBOOT: opening file '%s'\n", file );
  287. fd = open ( file );
  288. if ( fd < 0 ) {
  289. DBG ( "COMBOOT: error opening file %s\n", file );
  290. break;
  291. }
  292. /* This relies on the fact that a gPXE POSIX fd will
  293. * always fit in 16 bits.
  294. */
  295. #if (POSIX_FD_MAX > 65535)
  296. #error POSIX_FD_MAX too large
  297. #endif
  298. ix86->regs.si = (uint16_t) fd;
  299. ix86->regs.cx = COMBOOT_FILE_BLOCKSZ;
  300. ix86->regs.eax = fsize ( fd );
  301. ix86->flags &= ~CF;
  302. }
  303. break;
  304. case 0x0007: /* Read file */
  305. {
  306. int fd = ix86->regs.si;
  307. int len = ix86->regs.cx * COMBOOT_FILE_BLOCKSZ;
  308. int rc;
  309. fd_set fds;
  310. userptr_t buf = real_to_user ( ix86->segs.es, ix86->regs.bx );
  311. /* Wait for data ready to read */
  312. FD_ZERO ( &fds );
  313. FD_SET ( fd, &fds );
  314. select ( &fds, 1 );
  315. rc = read_user ( fd, buf, 0, len );
  316. if ( rc < 0 ) {
  317. DBG ( "COMBOOT: read failed\n" );
  318. ix86->regs.si = 0;
  319. break;
  320. }
  321. ix86->regs.ecx = rc;
  322. ix86->flags &= ~CF;
  323. }
  324. break;
  325. case 0x0008: /* Close file */
  326. {
  327. int fd = ix86->regs.si;
  328. close ( fd );
  329. ix86->flags &= ~CF;
  330. }
  331. break;
  332. case 0x0009: /* Call PXE Stack */
  333. pxe_api_call ( ix86 );
  334. ix86->flags &= ~CF;
  335. break;
  336. case 0x000A: /* Get Derivative-Specific Information */
  337. /* gPXE has its own derivative ID, so there is no defined
  338. * output here; just return AL for now */
  339. ix86->regs.al = BZI_LOADER_TYPE_GPXE;
  340. ix86->flags &= ~CF;
  341. break;
  342. case 0x000B: /* Get Serial Console Configuration */
  343. /* FIXME: stub */
  344. ix86->regs.dx = 0;
  345. ix86->flags &= ~CF;
  346. break;
  347. case 0x000E: /* Get configuration file name */
  348. /* FIXME: stub */
  349. ix86->segs.es = rm_ds;
  350. ix86->regs.bx = ( ( unsigned ) __from_data16 ( syslinux_configuration_file ) );
  351. ix86->flags &= ~CF;
  352. break;
  353. case 0x000F: /* Get IPAPPEND strings */
  354. /* FIXME: stub */
  355. ix86->regs.cx = 0;
  356. ix86->segs.es = 0;
  357. ix86->regs.bx = 0;
  358. ix86->flags &= ~CF;
  359. break;
  360. case 0x0010: /* Resolve hostname */
  361. {
  362. userptr_t hostname_u = real_to_user ( ix86->segs.es, ix86->regs.bx );
  363. int len = strlen_user ( hostname_u, 0 );
  364. char hostname[len];
  365. struct in_addr addr;
  366. copy_from_user ( hostname, hostname_u, 0, len + 1 );
  367. /* TODO:
  368. * "If the hostname does not contain a dot (.), the
  369. * local domain name is automatically appended."
  370. */
  371. comboot_resolv ( hostname, &addr );
  372. ix86->regs.eax = addr.s_addr;
  373. ix86->flags &= ~CF;
  374. }
  375. break;
  376. case 0x0011: /* Maximum number of shuffle descriptors */
  377. ix86->regs.cx = COMBOOT_MAX_SHUFFLE_DESCRIPTORS;
  378. ix86->flags &= ~CF;
  379. break;
  380. case 0x0012: /* Cleanup, shuffle and boot */
  381. if ( ix86->regs.cx > COMBOOT_MAX_SHUFFLE_DESCRIPTORS )
  382. break;
  383. /* Perform final cleanup */
  384. shutdown ( SHUTDOWN_BOOT );
  385. /* Perform sequence of copies */
  386. shuffle ( ix86->segs.es, ix86->regs.di, ix86->regs.cx );
  387. /* Jump to real-mode entry point */
  388. __asm__ __volatile__ (
  389. REAL_CODE (
  390. "pushw %0\n\t"
  391. "popw %%ds\n\t"
  392. "pushl %1\n\t"
  393. "lret\n\t"
  394. )
  395. :
  396. : "r" ( ix86->segs.ds ),
  397. "r" ( ix86->regs.ebp ),
  398. "d" ( ix86->regs.ebx ),
  399. "S" ( ix86->regs.esi ) );
  400. assert ( 0 ); /* Execution should never reach this point */
  401. break;
  402. case 0x0013: /* Idle loop call */
  403. step ( );
  404. ix86->flags &= ~CF;
  405. break;
  406. case 0x0015: /* Get feature flags */
  407. ix86->segs.es = rm_ds;
  408. ix86->regs.bx = ( ( unsigned ) __from_data16 ( &comboot_feature_flags ) );
  409. ix86->regs.cx = 1; /* Number of feature flag bytes */
  410. ix86->flags &= ~CF;
  411. break;
  412. case 0x0016: /* Run kernel image */
  413. {
  414. userptr_t file_u = real_to_user ( ix86->segs.ds, ix86->regs.si );
  415. userptr_t cmd_u = real_to_user ( ix86->segs.es, ix86->regs.bx );
  416. int file_len = strlen_user ( file_u, 0 );
  417. int cmd_len = strlen_user ( cmd_u, 0 );
  418. char file[file_len + 1 + cmd_len + 7 + 1];
  419. char cmd[cmd_len + 1];
  420. memcpy( file, "kernel ", 7 );
  421. copy_from_user ( file + 7, file_u, 0, file_len + 1 );
  422. copy_from_user ( cmd, cmd_u, 0, cmd_len + 1 );
  423. strcat ( file, " " );
  424. strcat ( file, cmd );
  425. DBG ( "COMBOOT: run kernel image '%s'\n", file );
  426. comboot_kernel_cmdline = strdup ( file );
  427. DBG ( "COMBOOT: returning to run image...\n" );
  428. longjmp ( comboot_return, COMBOOT_RETURN_RUN_KERNEL );
  429. }
  430. break;
  431. case 0x0017: /* Report video mode change */
  432. comboot_graphics_mode = ix86->regs.bx;
  433. ix86->flags &= ~CF;
  434. break;
  435. case 0x0018: /* Query custom font */
  436. /* FIXME: stub */
  437. ix86->regs.al = 0;
  438. ix86->segs.es = 0;
  439. ix86->regs.bx = 0;
  440. ix86->flags &= ~CF;
  441. break;
  442. default:
  443. DBG ( "COMBOOT unknown int22 function %04x\n", ix86->regs.ax );
  444. break;
  445. }
  446. }
  447. /**
  448. * Hook BIOS interrupts related to COMBOOT API (INT 20h, 21h, 22h)
  449. */
  450. void hook_comboot_interrupts ( ) {
  451. __asm__ __volatile__ (
  452. TEXT16_CODE ( "\nint20_wrapper:\n\t"
  453. "pushl %0\n\t"
  454. "pushw %%cs\n\t"
  455. "call prot_call\n\t"
  456. "addw $4, %%sp\n\t"
  457. "iret\n\t" )
  458. : : "i" ( int20 ) );
  459. hook_bios_interrupt ( 0x20, ( unsigned int ) int20_wrapper,
  460. &int20_vector );
  461. __asm__ __volatile__ (
  462. TEXT16_CODE ( "\nint21_wrapper:\n\t"
  463. "pushl %0\n\t"
  464. "pushw %%cs\n\t"
  465. "call prot_call\n\t"
  466. "addw $4, %%sp\n\t"
  467. "iret\n\t" )
  468. : : "i" ( int21 ) );
  469. hook_bios_interrupt ( 0x21, ( unsigned int ) int21_wrapper,
  470. &int21_vector );
  471. __asm__ __volatile__ (
  472. TEXT16_CODE ( "\nint22_wrapper:\n\t"
  473. "pushl %0\n\t"
  474. "pushw %%cs\n\t"
  475. "call prot_call\n\t"
  476. "addw $4, %%sp\n\t"
  477. "iret\n\t" )
  478. : : "i" ( int22) );
  479. hook_bios_interrupt ( 0x22, ( unsigned int ) int22_wrapper,
  480. &int22_vector );
  481. }