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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. struct idt_register com32_external_idtr = {
  41. .limit = COM32_NUM_IDT_ENTRIES * sizeof ( struct idt_descriptor ) - 1,
  42. .base = COM32_IDT
  43. };
  44. struct idt_register com32_internal_idtr;
  45. /**
  46. * Execute COMBOOT image
  47. *
  48. * @v image COM32 image
  49. * @ret rc Return status code
  50. */
  51. static int com32_exec_loop ( struct image *image ) {
  52. struct memory_map memmap;
  53. unsigned int i;
  54. int state;
  55. uint32_t avail_mem_top;
  56. state = rmsetjmp ( comboot_return );
  57. switch ( state ) {
  58. case 0: /* First time through; invoke COM32 program */
  59. /* Get memory map */
  60. get_memmap ( &memmap );
  61. /* Find end of block covering COM32 image loading area */
  62. for ( i = 0, avail_mem_top = 0 ; i < memmap.count ; i++ ) {
  63. if ( (memmap.regions[i].start <= COM32_START_PHYS) &&
  64. (memmap.regions[i].end > COM32_START_PHYS + image->len) ) {
  65. avail_mem_top = memmap.regions[i].end;
  66. break;
  67. }
  68. }
  69. DBGC ( image, "COM32 %p: available memory top = 0x%x\n",
  70. image, avail_mem_top );
  71. assert ( avail_mem_top != 0 );
  72. com32_external_esp = phys_to_virt ( avail_mem_top );
  73. /* Hook COMBOOT API interrupts */
  74. hook_comboot_interrupts();
  75. /* Unregister image, so that a "boot" command doesn't
  76. * throw us into an execution loop. We never
  77. * reregister ourselves; COMBOOT images expect to be
  78. * removed on exit.
  79. */
  80. unregister_image ( image );
  81. __asm__ __volatile__ (
  82. "sidt com32_internal_idtr\n\t"
  83. "lidt com32_external_idtr\n\t" /* Set up IDT */
  84. "movl %%esp, (com32_internal_esp)\n\t" /* Save internal virtual address space ESP */
  85. "movl (com32_external_esp), %%esp\n\t" /* Switch to COM32 ESP (top of available memory) */
  86. "call _virt_to_phys\n\t" /* Switch to flat physical address space */
  87. "sti\n\t" /* Enable interrupts */
  88. "pushl %0\n\t" /* Pointer to CDECL helper function */
  89. "pushl %1\n\t" /* Pointer to FAR call helper function */
  90. "pushl %2\n\t" /* Size of low memory bounce buffer */
  91. "pushl %3\n\t" /* Pointer to low memory bounce buffer */
  92. "pushl %4\n\t" /* Pointer to INT call helper function */
  93. "pushl %5\n\t" /* Pointer to the command line arguments */
  94. "pushl $6\n\t" /* Number of additional arguments */
  95. "call *%6\n\t" /* Execute image */
  96. "cli\n\t" /* Disable interrupts */
  97. "call _phys_to_virt\n\t" /* Switch back to internal virtual address space */
  98. "lidt com32_internal_idtr\n\t" /* Switch back to internal IDT (for debugging) */
  99. "movl (com32_internal_esp), %%esp\n\t" /* Switch back to internal stack */
  100. :
  101. :
  102. /* %0 */ "r" ( virt_to_phys ( com32_cfarcall_wrapper ) ),
  103. /* %1 */ "r" ( virt_to_phys ( com32_farcall_wrapper ) ),
  104. /* %2 */ "r" ( get_fbms() * 1024 - (COM32_BOUNCE_SEG << 4) ),
  105. /* %3 */ "i" ( COM32_BOUNCE_SEG << 4 ),
  106. /* %4 */ "r" ( virt_to_phys ( com32_intcall_wrapper ) ),
  107. /* %5 */ "r" ( virt_to_phys ( image->cmdline ?
  108. image->cmdline : "" ) ),
  109. /* %6 */ "r" ( COM32_START_PHYS )
  110. :
  111. "memory" );
  112. DBGC ( image, "COM32 %p: returned\n", image );
  113. break;
  114. case COMBOOT_EXIT:
  115. DBGC ( image, "COM32 %p: exited\n", image );
  116. break;
  117. case COMBOOT_EXIT_RUN_KERNEL:
  118. assert ( image->replacement );
  119. DBGC ( image, "COM32 %p: exited to run kernel %s\n",
  120. image, image->replacement->name );
  121. break;
  122. case COMBOOT_EXIT_COMMAND:
  123. DBGC ( image, "COM32 %p: exited after executing command\n",
  124. image );
  125. break;
  126. default:
  127. assert ( 0 );
  128. break;
  129. }
  130. unhook_comboot_interrupts();
  131. comboot_force_text_mode();
  132. return 0;
  133. }
  134. /**
  135. * Check image name extension
  136. *
  137. * @v image COM32 image
  138. * @ret rc Return status code
  139. */
  140. static int com32_identify ( struct image *image ) {
  141. const char *ext;
  142. static const uint8_t magic[] = { 0xB8, 0xFF, 0x4C, 0xCD, 0x21 };
  143. uint8_t buf[5];
  144. if ( image->len >= 5 ) {
  145. /* Check for magic number
  146. * mov eax,21cd4cffh
  147. * B8 FF 4C CD 21
  148. */
  149. copy_from_user ( buf, image->data, 0, sizeof(buf) );
  150. if ( ! memcmp ( buf, magic, sizeof(buf) ) ) {
  151. DBGC ( image, "COM32 %p: found magic number\n",
  152. image );
  153. return 0;
  154. }
  155. }
  156. /* Magic number not found; check filename extension */
  157. ext = strrchr( image->name, '.' );
  158. if ( ! ext ) {
  159. DBGC ( image, "COM32 %p: no extension\n",
  160. image );
  161. return -ENOEXEC;
  162. }
  163. ++ext;
  164. if ( strcasecmp( ext, "c32" ) ) {
  165. DBGC ( image, "COM32 %p: unrecognized extension %s\n",
  166. image, ext );
  167. return -ENOEXEC;
  168. }
  169. return 0;
  170. }
  171. /**
  172. * Load COM32 image into memory and set up the IDT
  173. * @v image COM32 image
  174. * @ret rc Return status code
  175. */
  176. static int com32_load_image ( struct image *image ) {
  177. physaddr_t com32_irq_wrapper_phys;
  178. struct idt_descriptor *idt;
  179. struct ijb_entry *ijb;
  180. size_t filesz, memsz;
  181. userptr_t buffer;
  182. int rc, i;
  183. /* The interrupt descriptor table, interrupt jump buffer, and
  184. * image data are all contiguous in memory. Prepare them all at once.
  185. */
  186. filesz = image->len +
  187. COM32_NUM_IDT_ENTRIES * sizeof ( struct idt_descriptor ) +
  188. COM32_NUM_IDT_ENTRIES * sizeof ( struct ijb_entry );
  189. memsz = filesz;
  190. buffer = phys_to_user ( COM32_IDT );
  191. if ( ( rc = prep_segment ( buffer, filesz, memsz ) ) != 0 ) {
  192. DBGC ( image, "COM32 %p: could not prepare segment: %s\n",
  193. image, strerror ( rc ) );
  194. return rc;
  195. }
  196. /* Write the IDT and IJB */
  197. idt = phys_to_virt ( COM32_IDT );
  198. ijb = phys_to_virt ( COM32_IJB );
  199. com32_irq_wrapper_phys = virt_to_phys ( com32_irq_wrapper );
  200. for ( i = 0; i < COM32_NUM_IDT_ENTRIES; i++ ) {
  201. uint32_t ijb_address = virt_to_phys ( &ijb[i] );
  202. idt[i].offset_low = ijb_address & 0xFFFF;
  203. idt[i].selector = PHYSICAL_CS;
  204. idt[i].flags = IDT_INTERRUPT_GATE_FLAGS;
  205. idt[i].offset_high = ijb_address >> 16;
  206. ijb[i].pusha_instruction = IJB_PUSHA;
  207. ijb[i].mov_instruction = IJB_MOV_AL_IMM8;
  208. ijb[i].mov_value = i;
  209. ijb[i].jump_instruction = IJB_JMP_REL32;
  210. ijb[i].jump_destination = com32_irq_wrapper_phys -
  211. virt_to_phys ( &ijb[i + 1] );
  212. }
  213. /* Copy image to segment */
  214. buffer = phys_to_user ( COM32_START_PHYS );
  215. memcpy_user ( buffer, 0, image->data, 0, filesz );
  216. return 0;
  217. }
  218. /**
  219. * Prepare COM32 low memory bounce buffer
  220. * @v image COM32 image
  221. * @ret rc Return status code
  222. */
  223. static int com32_prepare_bounce_buffer ( struct image * image ) {
  224. unsigned int seg;
  225. userptr_t seg_userptr;
  226. size_t filesz, memsz;
  227. int rc;
  228. seg = COM32_BOUNCE_SEG;
  229. seg_userptr = real_to_user ( seg, 0 );
  230. /* Ensure the entire 64k segment is free */
  231. memsz = 0xFFFF;
  232. filesz = 0;
  233. /* Prepare, verify, and load the real-mode segment */
  234. if ( ( rc = prep_segment ( seg_userptr, filesz, memsz ) ) != 0 ) {
  235. DBGC ( image, "COM32 %p: could not prepare bounce buffer segment: %s\n",
  236. image, strerror ( rc ) );
  237. return rc;
  238. }
  239. return 0;
  240. }
  241. /**
  242. * Probe COM32 image
  243. *
  244. * @v image COM32 image
  245. * @ret rc Return status code
  246. */
  247. static int com32_probe ( struct image *image ) {
  248. int rc;
  249. DBGC ( image, "COM32 %p: name '%s'\n", image, image->name );
  250. /* Check if this is a COMBOOT image */
  251. if ( ( rc = com32_identify ( image ) ) != 0 ) {
  252. return rc;
  253. }
  254. return 0;
  255. }
  256. /**
  257. * Execute COMBOOT image
  258. *
  259. * @v image COM32 image
  260. * @ret rc Return status code
  261. */
  262. static int com32_exec ( struct image *image ) {
  263. int rc;
  264. /* Load image */
  265. if ( ( rc = com32_load_image ( image ) ) != 0 ) {
  266. return rc;
  267. }
  268. /* Prepare bounce buffer segment */
  269. if ( ( rc = com32_prepare_bounce_buffer ( image ) ) != 0 ) {
  270. return rc;
  271. }
  272. return com32_exec_loop ( image );
  273. }
  274. /** SYSLINUX COM32 image type */
  275. struct image_type com32_image_type __image_type ( PROBE_NORMAL ) = {
  276. .name = "COM32",
  277. .probe = com32_probe,
  278. .exec = com32_exec,
  279. };