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

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