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.c 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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
  20. *
  21. * SYSLINUX COMBOOT (16-bit) image format
  22. *
  23. */
  24. FILE_LICENCE ( GPL2_OR_LATER );
  25. #include <stdint.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <strings.h>
  29. #include <errno.h>
  30. #include <assert.h>
  31. #include <realmode.h>
  32. #include <basemem.h>
  33. #include <comboot.h>
  34. #include <ipxe/uaccess.h>
  35. #include <ipxe/image.h>
  36. #include <ipxe/segment.h>
  37. #include <ipxe/init.h>
  38. #include <ipxe/features.h>
  39. FEATURE ( FEATURE_IMAGE, "COMBOOT", DHCP_EB_FEATURE_COMBOOT, 1 );
  40. /**
  41. * COMBOOT PSP, copied to offset 0 of code segment
  42. */
  43. struct comboot_psp {
  44. /** INT 20 instruction, executed if COMBOOT image returns with RET */
  45. uint16_t int20;
  46. /** Segment of first non-free paragraph of memory */
  47. uint16_t first_non_free_para;
  48. };
  49. /** Offset in PSP of command line */
  50. #define COMBOOT_PSP_CMDLINE_OFFSET 0x81
  51. /** Maximum length of command line in PSP
  52. * (127 bytes minus space and CR) */
  53. #define COMBOOT_MAX_CMDLINE_LEN 125
  54. /**
  55. * Copy command line to PSP
  56. *
  57. * @v image COMBOOT image
  58. */
  59. static void comboot_copy_cmdline ( struct image * image, userptr_t seg_userptr ) {
  60. const char *cmdline = ( image->cmdline ? image->cmdline : "" );
  61. int cmdline_len = strlen ( cmdline );
  62. if( cmdline_len > COMBOOT_MAX_CMDLINE_LEN )
  63. cmdline_len = COMBOOT_MAX_CMDLINE_LEN;
  64. uint8_t len_byte = cmdline_len;
  65. char spc = ' ', cr = '\r';
  66. /* Copy length to byte before command line */
  67. copy_to_user ( seg_userptr, COMBOOT_PSP_CMDLINE_OFFSET - 1,
  68. &len_byte, 1 );
  69. /* Command line starts with space */
  70. copy_to_user ( seg_userptr,
  71. COMBOOT_PSP_CMDLINE_OFFSET,
  72. &spc, 1 );
  73. /* Copy command line */
  74. copy_to_user ( seg_userptr,
  75. COMBOOT_PSP_CMDLINE_OFFSET + 1,
  76. cmdline, cmdline_len );
  77. /* Command line ends with CR */
  78. copy_to_user ( seg_userptr,
  79. COMBOOT_PSP_CMDLINE_OFFSET + cmdline_len + 1,
  80. &cr, 1 );
  81. }
  82. /**
  83. * Initialize PSP
  84. *
  85. * @v image COMBOOT image
  86. * @v seg_userptr segment to initialize
  87. */
  88. static void comboot_init_psp ( struct image * image, userptr_t seg_userptr ) {
  89. struct comboot_psp psp;
  90. /* Fill PSP */
  91. /* INT 20h instruction, byte order reversed */
  92. psp.int20 = 0x20CD;
  93. /* get_fbms() returns BIOS free base memory counter, which is in
  94. * kilobytes; x * 1024 / 16 == x * 64 == x << 6 */
  95. psp.first_non_free_para = get_fbms() << 6;
  96. DBGC ( image, "COMBOOT %p: first non-free paragraph = 0x%x\n",
  97. image, psp.first_non_free_para );
  98. /* Copy the PSP to offset 0 of segment.
  99. * The rest of the PSP was already zeroed by
  100. * comboot_prepare_segment. */
  101. copy_to_user ( seg_userptr, 0, &psp, sizeof( psp ) );
  102. /* Copy the command line to the PSP */
  103. comboot_copy_cmdline ( image, seg_userptr );
  104. }
  105. /**
  106. * Execute COMBOOT image
  107. *
  108. * @v image COMBOOT image
  109. * @ret rc Return status code
  110. */
  111. static int comboot_exec_loop ( struct image *image ) {
  112. userptr_t seg_userptr = real_to_user ( COMBOOT_PSP_SEG, 0 );
  113. int state;
  114. state = rmsetjmp ( comboot_return );
  115. switch ( state ) {
  116. case 0: /* First time through; invoke COMBOOT program */
  117. /* Initialize PSP */
  118. comboot_init_psp ( image, seg_userptr );
  119. /* Hook COMBOOT API interrupts */
  120. hook_comboot_interrupts();
  121. DBGC ( image, "executing 16-bit COMBOOT image at %4x:0100\n",
  122. COMBOOT_PSP_SEG );
  123. /* Unregister image, so that a "boot" command doesn't
  124. * throw us into an execution loop. We never
  125. * reregister ourselves; COMBOOT images expect to be
  126. * removed on exit.
  127. */
  128. unregister_image ( image );
  129. /* Store stack segment at 0x38 and stack pointer at 0x3A
  130. * in the PSP and jump to the image */
  131. __asm__ __volatile__ (
  132. REAL_CODE ( /* Save return address with segment on old stack */
  133. "popw %%ax\n\t"
  134. "pushw %%cs\n\t"
  135. "pushw %%ax\n\t"
  136. /* Set DS=ES=segment with image */
  137. "movw %w0, %%ds\n\t"
  138. "movw %w0, %%es\n\t"
  139. /* Set SS:SP to new stack (end of image segment) */
  140. "movw %w0, %%ss\n\t"
  141. "xor %%sp, %%sp\n\t"
  142. "pushw $0\n\t"
  143. "pushw %w0\n\t"
  144. "pushw $0x100\n\t"
  145. /* Zero registers (some COM files assume GP regs are 0) */
  146. "xorw %%ax, %%ax\n\t"
  147. "xorw %%bx, %%bx\n\t"
  148. "xorw %%cx, %%cx\n\t"
  149. "xorw %%dx, %%dx\n\t"
  150. "xorw %%si, %%si\n\t"
  151. "xorw %%di, %%di\n\t"
  152. "xorw %%bp, %%bp\n\t"
  153. "lret\n\t" )
  154. : : "r" ( COMBOOT_PSP_SEG ) : "eax" );
  155. DBGC ( image, "COMBOOT %p: returned\n", image );
  156. break;
  157. case COMBOOT_EXIT:
  158. DBGC ( image, "COMBOOT %p: exited\n", image );
  159. break;
  160. case COMBOOT_EXIT_RUN_KERNEL:
  161. assert ( image->replacement );
  162. DBGC ( image, "COMBOOT %p: exited to run kernel %s\n",
  163. image, image->replacement->name );
  164. break;
  165. case COMBOOT_EXIT_COMMAND:
  166. DBGC ( image, "COMBOOT %p: exited after executing command\n",
  167. image );
  168. break;
  169. default:
  170. assert ( 0 );
  171. break;
  172. }
  173. unhook_comboot_interrupts();
  174. comboot_force_text_mode();
  175. return 0;
  176. }
  177. /**
  178. * Check image name extension
  179. *
  180. * @v image COMBOOT image
  181. * @ret rc Return status code
  182. */
  183. static int comboot_identify ( struct image *image ) {
  184. const char *ext;
  185. ext = strrchr( image->name, '.' );
  186. if ( ! ext ) {
  187. DBGC ( image, "COMBOOT %p: no extension\n",
  188. image );
  189. return -ENOEXEC;
  190. }
  191. ++ext;
  192. if ( strcasecmp( ext, "com" ) && strcasecmp( ext, "cbt" ) ) {
  193. DBGC ( image, "COMBOOT %p: unrecognized extension %s\n",
  194. image, ext );
  195. return -ENOEXEC;
  196. }
  197. return 0;
  198. }
  199. /**
  200. * Load COMBOOT image into memory, preparing a segment and returning it
  201. * @v image COMBOOT image
  202. * @ret rc Return status code
  203. */
  204. static int comboot_prepare_segment ( struct image *image )
  205. {
  206. userptr_t seg_userptr;
  207. size_t filesz, memsz;
  208. int rc;
  209. /* Load image in segment */
  210. seg_userptr = real_to_user ( COMBOOT_PSP_SEG, 0 );
  211. /* Allow etra 0x100 bytes before image for PSP */
  212. filesz = image->len + 0x100;
  213. /* Ensure the entire 64k segment is free */
  214. memsz = 0xFFFF;
  215. /* Prepare, verify, and load the real-mode segment */
  216. if ( ( rc = prep_segment ( seg_userptr, filesz, memsz ) ) != 0 ) {
  217. DBGC ( image, "COMBOOT %p: could not prepare segment: %s\n",
  218. image, strerror ( rc ) );
  219. return rc;
  220. }
  221. /* Zero PSP */
  222. memset_user ( seg_userptr, 0, 0, 0x100 );
  223. /* Copy image to segment:0100 */
  224. memcpy_user ( seg_userptr, 0x100, image->data, 0, image->len );
  225. return 0;
  226. }
  227. /**
  228. * Probe COMBOOT image
  229. *
  230. * @v image COMBOOT image
  231. * @ret rc Return status code
  232. */
  233. static int comboot_probe ( struct image *image ) {
  234. int rc;
  235. DBGC ( image, "COMBOOT %p: name '%s'\n",
  236. image, image->name );
  237. /* Check if this is a COMBOOT image */
  238. if ( ( rc = comboot_identify ( image ) ) != 0 ) {
  239. return rc;
  240. }
  241. return 0;
  242. }
  243. /**
  244. * Execute COMBOOT image
  245. *
  246. * @v image COMBOOT image
  247. * @ret rc Return status code
  248. */
  249. static int comboot_exec ( struct image *image ) {
  250. int rc;
  251. /* Sanity check for filesize */
  252. if( image->len >= 0xFF00 ) {
  253. DBGC( image, "COMBOOT %p: image too large\n",
  254. image );
  255. return -ENOEXEC;
  256. }
  257. /* Prepare segment and load image */
  258. if ( ( rc = comboot_prepare_segment ( image ) ) != 0 ) {
  259. return rc;
  260. }
  261. return comboot_exec_loop ( image );
  262. }
  263. /** SYSLINUX COMBOOT (16-bit) image type */
  264. struct image_type comboot_image_type __image_type ( PROBE_NORMAL ) = {
  265. .name = "COMBOOT",
  266. .probe = comboot_probe,
  267. .exec = comboot_exec,
  268. };