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

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