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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. #include <gpxe/image.h>
  37. #include <usr/imgmgmt.h>
  38. /** The "SYSLINUX" version string */
  39. static char __data16_array ( syslinux_version, [] ) = "gPXE " VERSION;
  40. #define syslinux_version __use_data16 ( syslinux_version )
  41. /** The "SYSLINUX" copyright string */
  42. static char __data16_array ( syslinux_copyright, [] ) = "http://etherboot.org";
  43. #define syslinux_copyright __use_data16 ( syslinux_copyright )
  44. static char __data16_array ( syslinux_configuration_file, [] ) = "";
  45. #define syslinux_configuration_file __use_data16 ( syslinux_configuration_file )
  46. /** Feature flags */
  47. static uint8_t __data16 ( comboot_feature_flags ) = COMBOOT_FEATURE_IDLE_LOOP;
  48. #define comboot_feature_flags __use_data16 ( comboot_feature_flags )
  49. static struct segoff __text16 ( int20_vector );
  50. #define int20_vector __use_text16 ( int20_vector )
  51. static struct segoff __text16 ( int21_vector );
  52. #define int21_vector __use_text16 ( int21_vector )
  53. static struct segoff __text16 ( int22_vector );
  54. #define int22_vector __use_text16 ( int22_vector )
  55. extern void int20_wrapper ( void );
  56. extern void int21_wrapper ( void );
  57. extern void int22_wrapper ( void );
  58. /* setjmp/longjmp context buffer used to return after loading an image */
  59. jmp_buf comboot_return;
  60. /* Replacement image when exiting with COMBOOT_EXIT_RUN_KERNEL */
  61. struct image *comboot_replacement_image;
  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. * Fetch kernel and optional initrd
  133. */
  134. static int comboot_fetch_kernel ( char *kernel_file, char *cmdline ) {
  135. struct image *kernel = NULL;
  136. struct image *initrd = NULL;
  137. char *initrd_file;
  138. int rc;
  139. /* Find initrd= parameter, if any */
  140. if ( ( initrd_file = strstr ( cmdline, "initrd=" ) ) != NULL ) {
  141. char *initrd_end;
  142. /* skip "initrd=" */
  143. initrd_file += 7;
  144. /* Find terminating space, if any, and replace with NUL */
  145. initrd_end = strchr ( initrd_file, ' ' );
  146. if ( initrd_end )
  147. *initrd_end = '\0';
  148. DBG ( "COMBOOT: fetching initrd '%s'\n", initrd_file );
  149. /* Allocate and fetch initrd */
  150. initrd = alloc_image();
  151. if ( ! initrd ) {
  152. DBG ( "COMBOOT: could not allocate initrd\n" );
  153. rc = -ENOMEM;
  154. goto out;
  155. }
  156. if ( ( rc = imgfetch ( initrd, initrd_file,
  157. register_image ) ) != 0 ) {
  158. DBG ( "COMBOOT: could not fetch initrd: %s\n",
  159. strerror ( rc ) );
  160. goto out;
  161. }
  162. /* Restore space after initrd name, if applicable */
  163. if ( initrd_end )
  164. *initrd_end = ' ';
  165. }
  166. DBG ( "COMBOOT: fetching kernel '%s'\n", kernel_file );
  167. /* Allocate and fetch kernel */
  168. kernel = alloc_image();
  169. if ( ! kernel ) {
  170. DBG ( "COMBOOT: could not allocate kernel\n" );
  171. rc = -ENOMEM;
  172. goto out;
  173. }
  174. if ( ( rc = imgfetch ( kernel, kernel_file,
  175. register_image ) ) != 0 ) {
  176. DBG ( "COMBOOT: could not fetch kernel: %s\n",
  177. strerror ( rc ) );
  178. goto out;
  179. }
  180. if ( ( rc = image_set_cmdline ( kernel, cmdline ) ) != 0 ) {
  181. DBG ( "COMBOOT: could not set kernel command line: %s\n",
  182. strerror ( rc ) );
  183. goto out;
  184. }
  185. /* Store kernel as replacement image */
  186. assert ( comboot_replacement_image == NULL );
  187. comboot_replacement_image = image_get ( kernel );
  188. out:
  189. /* Drop image references unconditionally; either we want to
  190. * discard them, or they have been registered and we should
  191. * drop out local reference.
  192. */
  193. image_put ( kernel );
  194. image_put ( initrd );
  195. return rc;
  196. }
  197. /**
  198. * Terminate program interrupt handler
  199. */
  200. static __asmcall void int20 ( struct i386_all_regs *ix86 __unused ) {
  201. longjmp ( comboot_return, COMBOOT_EXIT );
  202. }
  203. /**
  204. * DOS-compatible API
  205. */
  206. static __asmcall void int21 ( struct i386_all_regs *ix86 ) {
  207. ix86->flags |= CF;
  208. switch ( ix86->regs.ah ) {
  209. case 0x00:
  210. case 0x4C: /* Terminate program */
  211. longjmp ( comboot_return, COMBOOT_EXIT );
  212. break;
  213. case 0x01: /* Get Key with Echo */
  214. case 0x08: /* Get Key without Echo */
  215. /* TODO: handle extended characters? */
  216. ix86->regs.al = getchar( );
  217. /* Enter */
  218. if ( ix86->regs.al == 0x0A )
  219. ix86->regs.al = 0x0D;
  220. if ( ix86->regs.ah == 0x01 )
  221. putchar ( ix86->regs.al );
  222. ix86->flags &= ~CF;
  223. break;
  224. case 0x02: /* Write Character */
  225. putchar ( ix86->regs.dl );
  226. ix86->flags &= ~CF;
  227. break;
  228. case 0x04: /* Write Character to Serial Port */
  229. serial_putc ( ix86->regs.dl );
  230. ix86->flags &= ~CF;
  231. break;
  232. case 0x09: /* Write DOS String to Console */
  233. print_user_string ( ix86->segs.ds, ix86->regs.dx, '$' );
  234. ix86->flags &= ~CF;
  235. break;
  236. case 0x0B: /* Check Keyboard */
  237. if ( iskey() )
  238. ix86->regs.al = 0xFF;
  239. else
  240. ix86->regs.al = 0x00;
  241. ix86->flags &= ~CF;
  242. break;
  243. case 0x30: /* Check DOS Version */
  244. /* Bottom halves all 0; top halves spell "SYSLINUX" */
  245. ix86->regs.eax = 0x59530000;
  246. ix86->regs.ebx = 0x4C530000;
  247. ix86->regs.ecx = 0x4E490000;
  248. ix86->regs.edx = 0x58550000;
  249. ix86->flags &= ~CF;
  250. break;
  251. default:
  252. DBG ( "COMBOOT unknown int21 function %02x\n", ix86->regs.ah );
  253. break;
  254. }
  255. }
  256. /**
  257. * SYSLINUX API
  258. */
  259. static __asmcall void int22 ( struct i386_all_regs *ix86 ) {
  260. ix86->flags |= CF;
  261. switch ( ix86->regs.ax ) {
  262. case 0x0001: /* Get Version */
  263. /* Number of INT 22h API functions available */
  264. ix86->regs.ax = 0x0018;
  265. /* SYSLINUX version number */
  266. ix86->regs.ch = 0; /* major */
  267. ix86->regs.cl = 0; /* minor */
  268. /* SYSLINUX derivative ID */
  269. ix86->regs.dl = BZI_LOADER_TYPE_GPXE;
  270. /* SYSLINUX version and copyright strings */
  271. ix86->segs.es = rm_ds;
  272. ix86->regs.si = ( ( unsigned ) __from_data16 ( syslinux_version ) );
  273. ix86->regs.di = ( ( unsigned ) __from_data16 ( syslinux_copyright ) );
  274. ix86->flags &= ~CF;
  275. break;
  276. case 0x0002: /* Write String */
  277. print_user_string ( ix86->segs.es, ix86->regs.bx, '\0' );
  278. ix86->flags &= ~CF;
  279. break;
  280. case 0x0003: /* Run command */
  281. {
  282. userptr_t cmd_u = real_to_user ( ix86->segs.es, ix86->regs.bx );
  283. int len = strlen_user ( cmd_u, 0 );
  284. char cmd[len + 1];
  285. copy_from_user ( cmd, cmd_u, 0, len + 1 );
  286. DBG ( "COMBOOT: executing command '%s'\n", cmd );
  287. system ( cmd );
  288. DBG ( "COMBOOT: exiting after executing command...\n" );
  289. longjmp ( comboot_return, COMBOOT_EXIT_COMMAND );
  290. }
  291. break;
  292. case 0x0004: /* Run default command */
  293. /* FIXME: just exit for now */
  294. longjmp ( comboot_return, COMBOOT_EXIT_COMMAND );
  295. break;
  296. case 0x0005: /* Force text mode */
  297. comboot_force_text_mode ( );
  298. ix86->flags &= ~CF;
  299. break;
  300. case 0x0006: /* Open file */
  301. {
  302. int fd;
  303. userptr_t file_u = real_to_user ( ix86->segs.es, ix86->regs.si );
  304. int len = strlen_user ( file_u, 0 );
  305. char file[len + 1];
  306. copy_from_user ( file, file_u, 0, len + 1 );
  307. if ( file[0] == '\0' ) {
  308. DBG ( "COMBOOT: attempted open with empty file name\n" );
  309. break;
  310. }
  311. DBG ( "COMBOOT: opening file '%s'\n", file );
  312. fd = open ( file );
  313. if ( fd < 0 ) {
  314. DBG ( "COMBOOT: error opening file %s\n", file );
  315. break;
  316. }
  317. /* This relies on the fact that a gPXE POSIX fd will
  318. * always fit in 16 bits.
  319. */
  320. #if (POSIX_FD_MAX > 65535)
  321. #error POSIX_FD_MAX too large
  322. #endif
  323. ix86->regs.si = (uint16_t) fd;
  324. ix86->regs.cx = COMBOOT_FILE_BLOCKSZ;
  325. ix86->regs.eax = fsize ( fd );
  326. ix86->flags &= ~CF;
  327. }
  328. break;
  329. case 0x0007: /* Read file */
  330. {
  331. int fd = ix86->regs.si;
  332. int len = ix86->regs.cx * COMBOOT_FILE_BLOCKSZ;
  333. int rc;
  334. fd_set fds;
  335. userptr_t buf = real_to_user ( ix86->segs.es, ix86->regs.bx );
  336. /* Wait for data ready to read */
  337. FD_ZERO ( &fds );
  338. FD_SET ( fd, &fds );
  339. select ( &fds, 1 );
  340. rc = read_user ( fd, buf, 0, len );
  341. if ( rc < 0 ) {
  342. DBG ( "COMBOOT: read failed\n" );
  343. ix86->regs.si = 0;
  344. break;
  345. }
  346. ix86->regs.ecx = rc;
  347. ix86->flags &= ~CF;
  348. }
  349. break;
  350. case 0x0008: /* Close file */
  351. {
  352. int fd = ix86->regs.si;
  353. close ( fd );
  354. ix86->flags &= ~CF;
  355. }
  356. break;
  357. case 0x0009: /* Call PXE Stack */
  358. pxe_api_call ( ix86 );
  359. ix86->flags &= ~CF;
  360. break;
  361. case 0x000A: /* Get Derivative-Specific Information */
  362. /* gPXE has its own derivative ID, so there is no defined
  363. * output here; just return AL for now */
  364. ix86->regs.al = BZI_LOADER_TYPE_GPXE;
  365. ix86->flags &= ~CF;
  366. break;
  367. case 0x000B: /* Get Serial Console Configuration */
  368. /* FIXME: stub */
  369. ix86->regs.dx = 0;
  370. ix86->flags &= ~CF;
  371. break;
  372. case 0x000E: /* Get configuration file name */
  373. /* FIXME: stub */
  374. ix86->segs.es = rm_ds;
  375. ix86->regs.bx = ( ( unsigned ) __from_data16 ( syslinux_configuration_file ) );
  376. ix86->flags &= ~CF;
  377. break;
  378. case 0x000F: /* Get IPAPPEND strings */
  379. /* FIXME: stub */
  380. ix86->regs.cx = 0;
  381. ix86->segs.es = 0;
  382. ix86->regs.bx = 0;
  383. ix86->flags &= ~CF;
  384. break;
  385. case 0x0010: /* Resolve hostname */
  386. {
  387. userptr_t hostname_u = real_to_user ( ix86->segs.es, ix86->regs.bx );
  388. int len = strlen_user ( hostname_u, 0 );
  389. char hostname[len];
  390. struct in_addr addr;
  391. copy_from_user ( hostname, hostname_u, 0, len + 1 );
  392. /* TODO:
  393. * "If the hostname does not contain a dot (.), the
  394. * local domain name is automatically appended."
  395. */
  396. comboot_resolv ( hostname, &addr );
  397. ix86->regs.eax = addr.s_addr;
  398. ix86->flags &= ~CF;
  399. }
  400. break;
  401. case 0x0011: /* Maximum number of shuffle descriptors */
  402. ix86->regs.cx = COMBOOT_MAX_SHUFFLE_DESCRIPTORS;
  403. ix86->flags &= ~CF;
  404. break;
  405. case 0x0012: /* Cleanup, shuffle and boot */
  406. if ( ix86->regs.cx > COMBOOT_MAX_SHUFFLE_DESCRIPTORS )
  407. break;
  408. /* Perform final cleanup */
  409. shutdown ( SHUTDOWN_BOOT );
  410. /* Perform sequence of copies */
  411. shuffle ( ix86->segs.es, ix86->regs.di, ix86->regs.cx );
  412. /* Jump to real-mode entry point */
  413. __asm__ __volatile__ (
  414. REAL_CODE (
  415. "pushw %0\n\t"
  416. "popw %%ds\n\t"
  417. "pushl %1\n\t"
  418. "lret\n\t"
  419. )
  420. :
  421. : "r" ( ix86->segs.ds ),
  422. "r" ( ix86->regs.ebp ),
  423. "d" ( ix86->regs.ebx ),
  424. "S" ( ix86->regs.esi ) );
  425. assert ( 0 ); /* Execution should never reach this point */
  426. break;
  427. case 0x0013: /* Idle loop call */
  428. step ( );
  429. ix86->flags &= ~CF;
  430. break;
  431. case 0x0015: /* Get feature flags */
  432. ix86->segs.es = rm_ds;
  433. ix86->regs.bx = ( ( unsigned ) __from_data16 ( &comboot_feature_flags ) );
  434. ix86->regs.cx = 1; /* Number of feature flag bytes */
  435. ix86->flags &= ~CF;
  436. break;
  437. case 0x0016: /* Run kernel image */
  438. {
  439. userptr_t file_u = real_to_user ( ix86->segs.ds, ix86->regs.si );
  440. userptr_t cmd_u = real_to_user ( ix86->segs.es, ix86->regs.bx );
  441. int file_len = strlen_user ( file_u, 0 );
  442. int cmd_len = strlen_user ( cmd_u, 0 );
  443. char file[file_len + 1];
  444. char cmd[cmd_len + 1];
  445. copy_from_user ( file, file_u, 0, file_len + 1 );
  446. copy_from_user ( cmd, cmd_u, 0, cmd_len + 1 );
  447. DBG ( "COMBOOT: run kernel %s %s\n", file, cmd );
  448. comboot_fetch_kernel ( file, cmd );
  449. /* Technically, we should return if we
  450. * couldn't load the kernel, but it's not safe
  451. * to do that since we have just overwritten
  452. * part of the COMBOOT program's memory space.
  453. */
  454. DBG ( "COMBOOT: exiting to run kernel...\n" );
  455. longjmp ( comboot_return, COMBOOT_EXIT_RUN_KERNEL );
  456. }
  457. break;
  458. case 0x0017: /* Report video mode change */
  459. comboot_graphics_mode = ix86->regs.bx;
  460. ix86->flags &= ~CF;
  461. break;
  462. case 0x0018: /* Query custom font */
  463. /* FIXME: stub */
  464. ix86->regs.al = 0;
  465. ix86->segs.es = 0;
  466. ix86->regs.bx = 0;
  467. ix86->flags &= ~CF;
  468. break;
  469. default:
  470. DBG ( "COMBOOT unknown int22 function %04x\n", ix86->regs.ax );
  471. break;
  472. }
  473. }
  474. /**
  475. * Hook BIOS interrupts related to COMBOOT API (INT 20h, 21h, 22h)
  476. */
  477. void hook_comboot_interrupts ( ) {
  478. __asm__ __volatile__ (
  479. TEXT16_CODE ( "\nint20_wrapper:\n\t"
  480. "pushl %0\n\t"
  481. "pushw %%cs\n\t"
  482. "call prot_call\n\t"
  483. "addw $4, %%sp\n\t"
  484. "iret\n\t" )
  485. : : "i" ( int20 ) );
  486. hook_bios_interrupt ( 0x20, ( unsigned int ) int20_wrapper,
  487. &int20_vector );
  488. __asm__ __volatile__ (
  489. TEXT16_CODE ( "\nint21_wrapper:\n\t"
  490. "pushl %0\n\t"
  491. "pushw %%cs\n\t"
  492. "call prot_call\n\t"
  493. "addw $4, %%sp\n\t"
  494. "iret\n\t" )
  495. : : "i" ( int21 ) );
  496. hook_bios_interrupt ( 0x21, ( unsigned int ) int21_wrapper,
  497. &int21_vector );
  498. __asm__ __volatile__ (
  499. TEXT16_CODE ( "\nint22_wrapper:\n\t"
  500. "pushl %0\n\t"
  501. "pushw %%cs\n\t"
  502. "call prot_call\n\t"
  503. "addw $4, %%sp\n\t"
  504. "iret\n\t" )
  505. : : "i" ( int22) );
  506. hook_bios_interrupt ( 0x22, ( unsigned int ) int22_wrapper,
  507. &int22_vector );
  508. }