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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /**
  41. * Execute COMBOOT image
  42. *
  43. * @v image COM32 image
  44. * @ret rc Return status code
  45. */
  46. static int com32_exec_loop ( struct image *image ) {
  47. struct memory_map memmap;
  48. unsigned int i;
  49. int state;
  50. uint32_t avail_mem_top;
  51. state = rmsetjmp ( comboot_return );
  52. switch ( state ) {
  53. case 0: /* First time through; invoke COM32 program */
  54. /* Get memory map */
  55. get_memmap ( &memmap );
  56. /* Find end of block covering COM32 image loading area */
  57. for ( i = 0, avail_mem_top = 0 ; i < memmap.count ; i++ ) {
  58. if ( (memmap.regions[i].start <= COM32_START_PHYS) &&
  59. (memmap.regions[i].end > COM32_START_PHYS + image->len) ) {
  60. avail_mem_top = memmap.regions[i].end;
  61. break;
  62. }
  63. }
  64. DBGC ( image, "COM32 %p: available memory top = 0x%x\n",
  65. image, avail_mem_top );
  66. assert ( avail_mem_top != 0 );
  67. com32_external_esp = phys_to_virt ( avail_mem_top );
  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__ (
  77. "movl %%esp, (com32_internal_esp)\n\t" /* Save internal virtual address space ESP */
  78. "movl (com32_external_esp), %%esp\n\t" /* Switch to COM32 ESP (top of available memory) */
  79. "call _virt_to_phys\n\t" /* Switch to flat physical address space */
  80. "sti\n\t" /* Enable interrupts */
  81. "pushl %0\n\t" /* Pointer to CDECL helper function */
  82. "pushl %1\n\t" /* Pointer to FAR call helper function */
  83. "pushl %2\n\t" /* Size of low memory bounce buffer */
  84. "pushl %3\n\t" /* Pointer to low memory bounce buffer */
  85. "pushl %4\n\t" /* Pointer to INT call helper function */
  86. "pushl %5\n\t" /* Pointer to the command line arguments */
  87. "pushl $6\n\t" /* Number of additional arguments */
  88. "call *%6\n\t" /* Execute image */
  89. "cli\n\t" /* Disable interrupts */
  90. "call _phys_to_virt\n\t" /* Switch back to internal virtual address space */
  91. "movl (com32_internal_esp), %%esp\n\t" /* Switch back to internal stack */
  92. :
  93. :
  94. /* %0 */ "r" ( virt_to_phys ( com32_cfarcall_wrapper ) ),
  95. /* %1 */ "r" ( virt_to_phys ( com32_farcall_wrapper ) ),
  96. /* %2 */ "r" ( get_fbms() * 1024 - (COM32_BOUNCE_SEG << 4) ),
  97. /* %3 */ "i" ( COM32_BOUNCE_SEG << 4 ),
  98. /* %4 */ "r" ( virt_to_phys ( com32_intcall_wrapper ) ),
  99. /* %5 */ "r" ( virt_to_phys ( image->cmdline ?
  100. image->cmdline : "" ) ),
  101. /* %6 */ "r" ( COM32_START_PHYS )
  102. :
  103. "memory" );
  104. DBGC ( image, "COM32 %p: returned\n", image );
  105. break;
  106. case COMBOOT_EXIT:
  107. DBGC ( image, "COM32 %p: exited\n", image );
  108. break;
  109. case COMBOOT_EXIT_RUN_KERNEL:
  110. assert ( image->replacement );
  111. DBGC ( image, "COM32 %p: exited to run kernel %s\n",
  112. image, image->replacement->name );
  113. break;
  114. case COMBOOT_EXIT_COMMAND:
  115. DBGC ( image, "COM32 %p: exited after executing command\n",
  116. image );
  117. break;
  118. default:
  119. assert ( 0 );
  120. break;
  121. }
  122. unhook_comboot_interrupts();
  123. comboot_force_text_mode();
  124. return 0;
  125. }
  126. /**
  127. * Check image name extension
  128. *
  129. * @v image COM32 image
  130. * @ret rc Return status code
  131. */
  132. static int com32_identify ( struct image *image ) {
  133. const char *ext;
  134. static const uint8_t magic[] = { 0xB8, 0xFF, 0x4C, 0xCD, 0x21 };
  135. uint8_t buf[5];
  136. if ( image->len >= 5 ) {
  137. /* Check for magic number
  138. * mov eax,21cd4cffh
  139. * B8 FF 4C CD 21
  140. */
  141. copy_from_user ( buf, image->data, 0, sizeof(buf) );
  142. if ( ! memcmp ( buf, magic, sizeof(buf) ) ) {
  143. DBGC ( image, "COM32 %p: found magic number\n",
  144. image );
  145. return 0;
  146. }
  147. }
  148. /* Magic number not found; check filename extension */
  149. ext = strrchr( image->name, '.' );
  150. if ( ! ext ) {
  151. DBGC ( image, "COM32 %p: no extension\n",
  152. image );
  153. return -ENOEXEC;
  154. }
  155. ++ext;
  156. if ( strcasecmp( ext, "c32" ) ) {
  157. DBGC ( image, "COM32 %p: unrecognized extension %s\n",
  158. image, ext );
  159. return -ENOEXEC;
  160. }
  161. return 0;
  162. }
  163. /**
  164. * Load COM32 image into memory
  165. * @v image COM32 image
  166. * @ret rc Return status code
  167. */
  168. static int com32_load_image ( struct image *image ) {
  169. size_t filesz, memsz;
  170. userptr_t buffer;
  171. int rc;
  172. filesz = image->len;
  173. memsz = filesz;
  174. buffer = phys_to_user ( COM32_START_PHYS );
  175. if ( ( rc = prep_segment ( buffer, filesz, memsz ) ) != 0 ) {
  176. DBGC ( image, "COM32 %p: could not prepare segment: %s\n",
  177. image, strerror ( rc ) );
  178. return rc;
  179. }
  180. /* Copy image to segment */
  181. memcpy_user ( buffer, 0, image->data, 0, filesz );
  182. return 0;
  183. }
  184. /**
  185. * Prepare COM32 low memory bounce buffer
  186. * @v image COM32 image
  187. * @ret rc Return status code
  188. */
  189. static int com32_prepare_bounce_buffer ( struct image * image ) {
  190. unsigned int seg;
  191. userptr_t seg_userptr;
  192. size_t filesz, memsz;
  193. int rc;
  194. seg = COM32_BOUNCE_SEG;
  195. seg_userptr = real_to_user ( seg, 0 );
  196. /* Ensure the entire 64k segment is free */
  197. memsz = 0xFFFF;
  198. filesz = 0;
  199. /* Prepare, verify, and load the real-mode segment */
  200. if ( ( rc = prep_segment ( seg_userptr, filesz, memsz ) ) != 0 ) {
  201. DBGC ( image, "COM32 %p: could not prepare bounce buffer segment: %s\n",
  202. image, strerror ( rc ) );
  203. return rc;
  204. }
  205. return 0;
  206. }
  207. /**
  208. * Probe COM32 image
  209. *
  210. * @v image COM32 image
  211. * @ret rc Return status code
  212. */
  213. static int com32_probe ( struct image *image ) {
  214. int rc;
  215. DBGC ( image, "COM32 %p: name '%s'\n", image, image->name );
  216. /* Check if this is a COMBOOT image */
  217. if ( ( rc = com32_identify ( image ) ) != 0 ) {
  218. return rc;
  219. }
  220. return 0;
  221. }
  222. /**
  223. * Execute COMBOOT image
  224. *
  225. * @v image COM32 image
  226. * @ret rc Return status code
  227. */
  228. static int com32_exec ( struct image *image ) {
  229. int rc;
  230. /* Load image */
  231. if ( ( rc = com32_load_image ( image ) ) != 0 ) {
  232. return rc;
  233. }
  234. /* Prepare bounce buffer segment */
  235. if ( ( rc = com32_prepare_bounce_buffer ( image ) ) != 0 ) {
  236. return rc;
  237. }
  238. return com32_exec_loop ( image );
  239. }
  240. /** SYSLINUX COM32 image type */
  241. struct image_type com32_image_type __image_type ( PROBE_NORMAL ) = {
  242. .name = "COM32",
  243. .probe = com32_probe,
  244. .exec = com32_exec,
  245. };