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.

com32.c 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 COM32 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/io.h>
  40. #include <ipxe/console.h>
  41. /**
  42. * Execute COMBOOT image
  43. *
  44. * @v image COM32 image
  45. * @ret rc Return status code
  46. */
  47. static int com32_exec_loop ( struct image *image ) {
  48. struct memory_map memmap;
  49. unsigned int i;
  50. int state;
  51. uint32_t avail_mem_top;
  52. state = rmsetjmp ( comboot_return );
  53. switch ( state ) {
  54. case 0: /* First time through; invoke COM32 program */
  55. /* Get memory map */
  56. get_memmap ( &memmap );
  57. /* Find end of block covering COM32 image loading area */
  58. for ( i = 0, avail_mem_top = 0 ; i < memmap.count ; i++ ) {
  59. if ( (memmap.regions[i].start <= COM32_START_PHYS) &&
  60. (memmap.regions[i].end > COM32_START_PHYS + image->len) ) {
  61. avail_mem_top = memmap.regions[i].end;
  62. break;
  63. }
  64. }
  65. DBGC ( image, "COM32 %p: available memory top = 0x%x\n",
  66. image, avail_mem_top );
  67. assert ( avail_mem_top != 0 );
  68. /* Hook COMBOOT API interrupts */
  69. hook_comboot_interrupts();
  70. /* Unregister image, so that a "boot" command doesn't
  71. * throw us into an execution loop. We never
  72. * reregister ourselves; COMBOOT images expect to be
  73. * removed on exit.
  74. */
  75. unregister_image ( image );
  76. __asm__ __volatile__ ( PHYS_CODE (
  77. /* Preserve registers */
  78. "pushal\n\t"
  79. /* Preserve stack pointer */
  80. "subl $4, %k0\n\t"
  81. "movl %%esp, (%k0)\n\t"
  82. /* Switch to COM32 stack */
  83. "movl %k0, %%esp\n\t"
  84. /* Enable interrupts */
  85. "sti\n\t"
  86. /* Construct stack frame */
  87. "pushl %k1\n\t"
  88. "pushl %k2\n\t"
  89. "pushl %k3\n\t"
  90. "pushl %k4\n\t"
  91. "pushl %k5\n\t"
  92. "pushl %k6\n\t"
  93. "pushl $6\n\t"
  94. /* Call COM32 entry point */
  95. "movl %k7, %k0\n\t"
  96. "call *%k0\n\t"
  97. /* Disable interrupts */
  98. "cli\n\t"
  99. /* Restore stack pointer */
  100. "movl 24(%%esp), %%esp\n\t"
  101. /* Restore registers */
  102. "popal\n\t" )
  103. :
  104. : "r" ( avail_mem_top ),
  105. "r" ( virt_to_phys ( com32_cfarcall_wrapper ) ),
  106. "r" ( virt_to_phys ( com32_farcall_wrapper ) ),
  107. "r" ( get_fbms() * 1024 - ( COM32_BOUNCE_SEG << 4 ) ),
  108. "i" ( COM32_BOUNCE_SEG << 4 ),
  109. "r" ( virt_to_phys ( com32_intcall_wrapper ) ),
  110. "r" ( virt_to_phys ( image->cmdline ?
  111. image->cmdline : "" ) ),
  112. "i" ( COM32_START_PHYS )
  113. : "memory" );
  114. DBGC ( image, "COM32 %p: returned\n", image );
  115. break;
  116. case COMBOOT_EXIT:
  117. DBGC ( image, "COM32 %p: exited\n", image );
  118. break;
  119. case COMBOOT_EXIT_RUN_KERNEL:
  120. assert ( image->replacement );
  121. DBGC ( image, "COM32 %p: exited to run kernel %s\n",
  122. image, image->replacement->name );
  123. break;
  124. case COMBOOT_EXIT_COMMAND:
  125. DBGC ( image, "COM32 %p: exited after executing command\n",
  126. image );
  127. break;
  128. default:
  129. assert ( 0 );
  130. break;
  131. }
  132. unhook_comboot_interrupts();
  133. comboot_force_text_mode();
  134. return 0;
  135. }
  136. /**
  137. * Check image name extension
  138. *
  139. * @v image COM32 image
  140. * @ret rc Return status code
  141. */
  142. static int com32_identify ( struct image *image ) {
  143. const char *ext;
  144. static const uint8_t magic[] = { 0xB8, 0xFF, 0x4C, 0xCD, 0x21 };
  145. uint8_t buf[5];
  146. if ( image->len >= 5 ) {
  147. /* Check for magic number
  148. * mov eax,21cd4cffh
  149. * B8 FF 4C CD 21
  150. */
  151. copy_from_user ( buf, image->data, 0, sizeof(buf) );
  152. if ( ! memcmp ( buf, magic, sizeof(buf) ) ) {
  153. DBGC ( image, "COM32 %p: found magic number\n",
  154. image );
  155. return 0;
  156. }
  157. }
  158. /* Magic number not found; check filename extension */
  159. ext = strrchr( image->name, '.' );
  160. if ( ! ext ) {
  161. DBGC ( image, "COM32 %p: no extension\n",
  162. image );
  163. return -ENOEXEC;
  164. }
  165. ++ext;
  166. if ( strcasecmp( ext, "c32" ) ) {
  167. DBGC ( image, "COM32 %p: unrecognized extension %s\n",
  168. image, ext );
  169. return -ENOEXEC;
  170. }
  171. return 0;
  172. }
  173. /**
  174. * Load COM32 image into memory
  175. * @v image COM32 image
  176. * @ret rc Return status code
  177. */
  178. static int com32_load_image ( struct image *image ) {
  179. size_t filesz, memsz;
  180. userptr_t buffer;
  181. int rc;
  182. filesz = image->len;
  183. memsz = filesz;
  184. buffer = phys_to_user ( COM32_START_PHYS );
  185. if ( ( rc = prep_segment ( buffer, filesz, memsz ) ) != 0 ) {
  186. DBGC ( image, "COM32 %p: could not prepare segment: %s\n",
  187. image, strerror ( rc ) );
  188. return rc;
  189. }
  190. /* Copy image to segment */
  191. memcpy_user ( buffer, 0, image->data, 0, filesz );
  192. return 0;
  193. }
  194. /**
  195. * Prepare COM32 low memory bounce buffer
  196. * @v image COM32 image
  197. * @ret rc Return status code
  198. */
  199. static int com32_prepare_bounce_buffer ( struct image * image ) {
  200. unsigned int seg;
  201. userptr_t seg_userptr;
  202. size_t filesz, memsz;
  203. int rc;
  204. seg = COM32_BOUNCE_SEG;
  205. seg_userptr = real_to_user ( seg, 0 );
  206. /* Ensure the entire 64k segment is free */
  207. memsz = 0xFFFF;
  208. filesz = 0;
  209. /* Prepare, verify, and load the real-mode segment */
  210. if ( ( rc = prep_segment ( seg_userptr, filesz, memsz ) ) != 0 ) {
  211. DBGC ( image, "COM32 %p: could not prepare bounce buffer segment: %s\n",
  212. image, strerror ( rc ) );
  213. return rc;
  214. }
  215. return 0;
  216. }
  217. /**
  218. * Probe COM32 image
  219. *
  220. * @v image COM32 image
  221. * @ret rc Return status code
  222. */
  223. static int com32_probe ( struct image *image ) {
  224. int rc;
  225. DBGC ( image, "COM32 %p: name '%s'\n", image, image->name );
  226. /* Check if this is a COMBOOT image */
  227. if ( ( rc = com32_identify ( image ) ) != 0 ) {
  228. return rc;
  229. }
  230. return 0;
  231. }
  232. /**
  233. * Execute COMBOOT image
  234. *
  235. * @v image COM32 image
  236. * @ret rc Return status code
  237. */
  238. static int com32_exec ( struct image *image ) {
  239. int rc;
  240. /* Load image */
  241. if ( ( rc = com32_load_image ( image ) ) != 0 ) {
  242. return rc;
  243. }
  244. /* Prepare bounce buffer segment */
  245. if ( ( rc = com32_prepare_bounce_buffer ( image ) ) != 0 ) {
  246. return rc;
  247. }
  248. /* Reset console */
  249. console_reset();
  250. return com32_exec_loop ( image );
  251. }
  252. /** SYSLINUX COM32 image type */
  253. struct image_type com32_image_type __image_type ( PROBE_NORMAL ) = {
  254. .name = "COM32",
  255. .probe = com32_probe,
  256. .exec = com32_exec,
  257. };