Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

com32.c 7.4KB

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