Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

comboot.c 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. /* Temporarily de-register image, so that a "boot" command
  124. * doesn't throw us into an execution loop. Hold a reference
  125. * to avoid the image's being freed.
  126. */
  127. image_get ( image );
  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. break;
  156. case COMBOOT_RETURN_RUN_KERNEL:
  157. DBGC ( image, "COMBOOT %p: returned to run kernel...\n", image );
  158. comboot_run_kernel ( );
  159. break;
  160. case COMBOOT_RETURN_EXIT:
  161. break;
  162. }
  163. comboot_force_text_mode ( );
  164. DBGC ( image, "COMBOOT %p returned\n", image );
  165. /* Re-register image and return */
  166. register_image ( image );
  167. image_put ( image );
  168. return 0;
  169. }
  170. /**
  171. * Check image name extension
  172. *
  173. * @v image COMBOOT image
  174. * @ret rc Return status code
  175. */
  176. static int comboot_identify ( struct image *image ) {
  177. const char *ext;
  178. ext = strrchr( image->name, '.' );
  179. if ( ! ext ) {
  180. DBGC ( image, "COMBOOT %p: no extension\n",
  181. image );
  182. return -ENOEXEC;
  183. }
  184. ++ext;
  185. if ( strcasecmp( ext, "com" ) && strcasecmp( ext, "cbt" ) ) {
  186. DBGC ( image, "COMBOOT %p: unrecognized extension %s\n",
  187. image, ext );
  188. return -ENOEXEC;
  189. }
  190. return 0;
  191. }
  192. /**
  193. * Load COMBOOT image into memory, preparing a segment and returning it
  194. * @v image COMBOOT image
  195. * @ret rc Return status code
  196. */
  197. static int comboot_prepare_segment ( struct image *image )
  198. {
  199. userptr_t seg_userptr;
  200. size_t filesz, memsz;
  201. int rc;
  202. /* Load image in segment */
  203. seg_userptr = real_to_user ( COMBOOT_PSP_SEG, 0 );
  204. /* Allow etra 0x100 bytes before image for PSP */
  205. filesz = image->len + 0x100;
  206. /* Ensure the entire 64k segment is free */
  207. memsz = 0xFFFF;
  208. /* Prepare, verify, and load the real-mode segment */
  209. if ( ( rc = prep_segment ( seg_userptr, filesz, memsz ) ) != 0 ) {
  210. DBGC ( image, "COMBOOT %p: could not prepare segment: %s\n",
  211. image, strerror ( rc ) );
  212. return rc;
  213. }
  214. /* Zero PSP */
  215. memset_user ( seg_userptr, 0, 0, 0x100 );
  216. /* Copy image to segment:0100 */
  217. memcpy_user ( seg_userptr, 0x100, image->data, 0, image->len );
  218. return 0;
  219. }
  220. /**
  221. * Load COMBOOT image into memory
  222. *
  223. * @v image COMBOOT image
  224. * @ret rc Return status code
  225. */
  226. static int comboot_load ( struct image *image ) {
  227. int rc;
  228. DBGC ( image, "COMBOOT %p: name '%s'\n",
  229. image, image->name );
  230. /* Check if this is a COMBOOT image */
  231. if ( ( rc = comboot_identify ( image ) ) != 0 ) {
  232. return rc;
  233. }
  234. /* This is a 16-bit COMBOOT image, valid or otherwise */
  235. if ( ! image->type )
  236. image->type = &comboot_image_type;
  237. /* Sanity check for filesize */
  238. if( image->len >= 0xFF00 ) {
  239. DBGC( image, "COMBOOT %p: image too large\n",
  240. image );
  241. return -ENOEXEC;
  242. }
  243. /* Prepare segment and load image */
  244. if ( ( rc = comboot_prepare_segment ( image ) ) != 0 ) {
  245. return rc;
  246. }
  247. return 0;
  248. }
  249. /** SYSLINUX COMBOOT (16-bit) image type */
  250. struct image_type comboot_image_type __image_type ( PROBE_NORMAL ) = {
  251. .name = "COMBOOT",
  252. .load = comboot_load,
  253. .exec = comboot_exec,
  254. };