Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. /**
  20. * @file
  21. *
  22. * SYSLINUX COMBOOT (16-bit) image format
  23. *
  24. */
  25. FILE_LICENCE ( GPL2_OR_LATER );
  26. #include <stdint.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <strings.h>
  30. #include <errno.h>
  31. #include <assert.h>
  32. #include <realmode.h>
  33. #include <basemem.h>
  34. #include <comboot.h>
  35. #include <ipxe/uaccess.h>
  36. #include <ipxe/image.h>
  37. #include <ipxe/segment.h>
  38. #include <ipxe/init.h>
  39. #include <ipxe/features.h>
  40. #include <ipxe/console.h>
  41. FEATURE ( FEATURE_IMAGE, "COMBOOT", DHCP_EB_FEATURE_COMBOOT, 1 );
  42. /**
  43. * COMBOOT PSP, copied to offset 0 of code segment
  44. */
  45. struct comboot_psp {
  46. /** INT 20 instruction, executed if COMBOOT image returns with RET */
  47. uint16_t int20;
  48. /** Segment of first non-free paragraph of memory */
  49. uint16_t first_non_free_para;
  50. };
  51. /** Offset in PSP of command line */
  52. #define COMBOOT_PSP_CMDLINE_OFFSET 0x81
  53. /** Maximum length of command line in PSP
  54. * (127 bytes minus space and CR) */
  55. #define COMBOOT_MAX_CMDLINE_LEN 125
  56. /**
  57. * Copy command line to PSP
  58. *
  59. * @v image COMBOOT image
  60. */
  61. static void comboot_copy_cmdline ( struct image * image, userptr_t seg_userptr ) {
  62. const char *cmdline = ( image->cmdline ? image->cmdline : "" );
  63. int cmdline_len = strlen ( cmdline );
  64. if( cmdline_len > COMBOOT_MAX_CMDLINE_LEN )
  65. cmdline_len = COMBOOT_MAX_CMDLINE_LEN;
  66. uint8_t len_byte = cmdline_len;
  67. char spc = ' ', cr = '\r';
  68. /* Copy length to byte before command line */
  69. copy_to_user ( seg_userptr, COMBOOT_PSP_CMDLINE_OFFSET - 1,
  70. &len_byte, 1 );
  71. /* Command line starts with space */
  72. copy_to_user ( seg_userptr,
  73. COMBOOT_PSP_CMDLINE_OFFSET,
  74. &spc, 1 );
  75. /* Copy command line */
  76. copy_to_user ( seg_userptr,
  77. COMBOOT_PSP_CMDLINE_OFFSET + 1,
  78. cmdline, cmdline_len );
  79. /* Command line ends with CR */
  80. copy_to_user ( seg_userptr,
  81. COMBOOT_PSP_CMDLINE_OFFSET + cmdline_len + 1,
  82. &cr, 1 );
  83. }
  84. /**
  85. * Initialize PSP
  86. *
  87. * @v image COMBOOT image
  88. * @v seg_userptr segment to initialize
  89. */
  90. static void comboot_init_psp ( struct image * image, userptr_t seg_userptr ) {
  91. struct comboot_psp psp;
  92. /* Fill PSP */
  93. /* INT 20h instruction, byte order reversed */
  94. psp.int20 = 0x20CD;
  95. /* get_fbms() returns BIOS free base memory counter, which is in
  96. * kilobytes; x * 1024 / 16 == x * 64 == x << 6 */
  97. psp.first_non_free_para = get_fbms() << 6;
  98. DBGC ( image, "COMBOOT %p: first non-free paragraph = 0x%x\n",
  99. image, psp.first_non_free_para );
  100. /* Copy the PSP to offset 0 of segment.
  101. * The rest of the PSP was already zeroed by
  102. * comboot_prepare_segment. */
  103. copy_to_user ( seg_userptr, 0, &psp, sizeof( psp ) );
  104. /* Copy the command line to the PSP */
  105. comboot_copy_cmdline ( image, seg_userptr );
  106. }
  107. /**
  108. * Execute COMBOOT image
  109. *
  110. * @v image COMBOOT image
  111. * @ret rc Return status code
  112. */
  113. static int comboot_exec_loop ( struct image *image ) {
  114. userptr_t seg_userptr = real_to_user ( COMBOOT_PSP_SEG, 0 );
  115. int state;
  116. state = rmsetjmp ( comboot_return );
  117. switch ( state ) {
  118. case 0: /* First time through; invoke COMBOOT program */
  119. /* Initialize PSP */
  120. comboot_init_psp ( image, seg_userptr );
  121. /* Hook COMBOOT API interrupts */
  122. hook_comboot_interrupts();
  123. DBGC ( image, "executing 16-bit COMBOOT image at %4x:0100\n",
  124. COMBOOT_PSP_SEG );
  125. /* Unregister image, so that a "boot" command doesn't
  126. * throw us into an execution loop. We never
  127. * reregister ourselves; COMBOOT images expect to be
  128. * removed on exit.
  129. */
  130. unregister_image ( image );
  131. /* Store stack segment at 0x38 and stack pointer at 0x3A
  132. * in the PSP and jump to the image */
  133. __asm__ __volatile__ (
  134. REAL_CODE ( /* Save return address with segment on old stack */
  135. "popw %%ax\n\t"
  136. "pushw %%cs\n\t"
  137. "pushw %%ax\n\t"
  138. /* Set DS=ES=segment with image */
  139. "movw %w0, %%ds\n\t"
  140. "movw %w0, %%es\n\t"
  141. /* Set SS:SP to new stack (end of image segment) */
  142. "movw %w0, %%ss\n\t"
  143. "xor %%sp, %%sp\n\t"
  144. "pushw $0\n\t"
  145. "pushw %w0\n\t"
  146. "pushw $0x100\n\t"
  147. /* Zero registers (some COM files assume GP regs are 0) */
  148. "xorw %%ax, %%ax\n\t"
  149. "xorw %%bx, %%bx\n\t"
  150. "xorw %%cx, %%cx\n\t"
  151. "xorw %%dx, %%dx\n\t"
  152. "xorw %%si, %%si\n\t"
  153. "xorw %%di, %%di\n\t"
  154. "xorw %%bp, %%bp\n\t"
  155. "lret\n\t" )
  156. : : "r" ( COMBOOT_PSP_SEG ) : "eax" );
  157. DBGC ( image, "COMBOOT %p: returned\n", image );
  158. break;
  159. case COMBOOT_EXIT:
  160. DBGC ( image, "COMBOOT %p: exited\n", image );
  161. break;
  162. case COMBOOT_EXIT_RUN_KERNEL:
  163. assert ( image->replacement );
  164. DBGC ( image, "COMBOOT %p: exited to run kernel %s\n",
  165. image, image->replacement->name );
  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, "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. * Probe COMBOOT image
  231. *
  232. * @v image COMBOOT image
  233. * @ret rc Return status code
  234. */
  235. static int comboot_probe ( 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. return 0;
  244. }
  245. /**
  246. * Execute COMBOOT image
  247. *
  248. * @v image COMBOOT image
  249. * @ret rc Return status code
  250. */
  251. static int comboot_exec ( struct image *image ) {
  252. int rc;
  253. /* Sanity check for filesize */
  254. if( image->len >= 0xFF00 ) {
  255. DBGC( image, "COMBOOT %p: image too large\n",
  256. image );
  257. return -ENOEXEC;
  258. }
  259. /* Prepare segment and load image */
  260. if ( ( rc = comboot_prepare_segment ( image ) ) != 0 ) {
  261. return rc;
  262. }
  263. /* Reset console */
  264. console_reset();
  265. return comboot_exec_loop ( image );
  266. }
  267. /** SYSLINUX COMBOOT (16-bit) image type */
  268. struct image_type comboot_image_type __image_type ( PROBE_NORMAL ) = {
  269. .name = "COMBOOT",
  270. .probe = comboot_probe,
  271. .exec = comboot_exec,
  272. };