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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <strings.h>
  28. #include <errno.h>
  29. #include <assert.h>
  30. #include <realmode.h>
  31. #include <basemem.h>
  32. #include <comboot.h>
  33. #include <gpxe/uaccess.h>
  34. #include <gpxe/image.h>
  35. #include <gpxe/segment.h>
  36. #include <gpxe/init.h>
  37. #include <gpxe/features.h>
  38. FEATURE ( FEATURE_IMAGE, "COMBOOT", DHCP_EB_FEATURE_COMBOOT, 1 );
  39. struct image_type comboot_image_type __image_type ( PROBE_NORMAL );
  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 ( struct image *image ) {
  112. userptr_t seg_userptr = real_to_user ( COMBOOT_PSP_SEG, 0 );
  113. int state;
  114. state = setjmp ( 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. DBGC ( image, "COMBOOT %p: exited to run kernel %p\n",
  162. image, comboot_replacement_image );
  163. image->replacement = comboot_replacement_image;
  164. comboot_replacement_image = NULL;
  165. image_autoload ( image->replacement );
  166. break;
  167. case COMBOOT_EXIT_COMMAND:
  168. DBGC ( image, "COMBOOT %p: exited after executing command\n",
  169. image );
  170. break;
  171. default:
  172. assert ( 0 );
  173. break;
  174. }
  175. unhook_comboot_interrupts();
  176. comboot_force_text_mode();
  177. return 0;
  178. }
  179. /**
  180. * Check image name extension
  181. *
  182. * @v image COMBOOT image
  183. * @ret rc Return status code
  184. */
  185. static int comboot_identify ( struct image *image ) {
  186. const char *ext;
  187. ext = strrchr( image->name, '.' );
  188. if ( ! ext ) {
  189. DBGC ( image, "COMBOOT %p: no extension\n",
  190. image );
  191. return -ENOEXEC;
  192. }
  193. ++ext;
  194. if ( strcasecmp( ext, "com" ) && strcasecmp( ext, "cbt" ) ) {
  195. DBGC ( image, "COMBOOT %p: unrecognized extension %s\n",
  196. image, ext );
  197. return -ENOEXEC;
  198. }
  199. return 0;
  200. }
  201. /**
  202. * Load COMBOOT image into memory, preparing a segment and returning it
  203. * @v image COMBOOT image
  204. * @ret rc Return status code
  205. */
  206. static int comboot_prepare_segment ( struct image *image )
  207. {
  208. userptr_t seg_userptr;
  209. size_t filesz, memsz;
  210. int rc;
  211. /* Load image in segment */
  212. seg_userptr = real_to_user ( COMBOOT_PSP_SEG, 0 );
  213. /* Allow etra 0x100 bytes before image for PSP */
  214. filesz = image->len + 0x100;
  215. /* Ensure the entire 64k segment is free */
  216. memsz = 0xFFFF;
  217. /* Prepare, verify, and load the real-mode segment */
  218. if ( ( rc = prep_segment ( seg_userptr, filesz, memsz ) ) != 0 ) {
  219. DBGC ( image, "COMBOOT %p: could not prepare segment: %s\n",
  220. image, strerror ( rc ) );
  221. return rc;
  222. }
  223. /* Zero PSP */
  224. memset_user ( seg_userptr, 0, 0, 0x100 );
  225. /* Copy image to segment:0100 */
  226. memcpy_user ( seg_userptr, 0x100, image->data, 0, image->len );
  227. return 0;
  228. }
  229. /**
  230. * Load COMBOOT image into memory
  231. *
  232. * @v image COMBOOT image
  233. * @ret rc Return status code
  234. */
  235. static int comboot_load ( struct image *image ) {
  236. int rc;
  237. DBGC ( image, "COMBOOT %p: name '%s'\n",
  238. image, image->name );
  239. /* Check if this is a COMBOOT image */
  240. if ( ( rc = comboot_identify ( image ) ) != 0 ) {
  241. return rc;
  242. }
  243. /* This is a 16-bit COMBOOT image, valid or otherwise */
  244. if ( ! image->type )
  245. image->type = &comboot_image_type;
  246. /* Sanity check for filesize */
  247. if( image->len >= 0xFF00 ) {
  248. DBGC( image, "COMBOOT %p: image too large\n",
  249. image );
  250. return -ENOEXEC;
  251. }
  252. /* Prepare segment and load image */
  253. if ( ( rc = comboot_prepare_segment ( image ) ) != 0 ) {
  254. return rc;
  255. }
  256. return 0;
  257. }
  258. /** SYSLINUX COMBOOT (16-bit) image type */
  259. struct image_type comboot_image_type __image_type ( PROBE_NORMAL ) = {
  260. .name = "COMBOOT",
  261. .load = comboot_load,
  262. .exec = comboot_exec,
  263. };