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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <strings.h>
  28. #include <errno.h>
  29. #include <assert.h>
  30. #include <realmode.h>
  31. #include <basemem.h>
  32. #include <comboot.h>
  33. #include <gpxe/uaccess.h>
  34. #include <gpxe/image.h>
  35. #include <gpxe/segment.h>
  36. #include <gpxe/init.h>
  37. #include <gpxe/memmap.h>
  38. struct image_type com32_image_type __image_type ( PROBE_NORMAL );
  39. /**
  40. * Execute COMBOOT image
  41. *
  42. * @v image COM32 image
  43. * @ret rc Return status code
  44. */
  45. static int com32_exec ( struct image *image ) {
  46. struct memory_map memmap;
  47. unsigned int i;
  48. int state;
  49. uint32_t avail_mem_top;
  50. state = setjmp ( comboot_return );
  51. switch ( state ) {
  52. case 0: /* First time through; invoke COM32 program */
  53. /* Get memory map */
  54. get_memmap ( &memmap );
  55. /* Find end of block covering COM32 image loading area */
  56. for ( i = 0, avail_mem_top = 0 ; i < memmap.count ; i++ ) {
  57. if ( (memmap.regions[i].start <= COM32_START_PHYS) &&
  58. (memmap.regions[i].end > COM32_START_PHYS + image->len) ) {
  59. avail_mem_top = memmap.regions[i].end;
  60. break;
  61. }
  62. }
  63. DBGC ( image, "COM32 %p: available memory top = 0x%x\n",
  64. image, (int)avail_mem_top );
  65. assert ( avail_mem_top != 0 );
  66. com32_external_esp = phys_to_virt ( avail_mem_top );
  67. /* Hook COMBOOT API interrupts */
  68. hook_comboot_interrupts( );
  69. /* Temporarily de-register image, so that a "boot" command
  70. * doesn't throw us into an execution loop. Hold a reference
  71. * to avoid the image's being freed.
  72. */
  73. image_get ( image );
  74. unregister_image ( image );
  75. __asm__ __volatile__ (
  76. "movl %%esp, (com32_internal_esp)\n\t" /* Save internal virtual address space ESP */
  77. "movl (com32_external_esp), %%esp\n\t" /* Switch to COM32 ESP (top of available memory) */
  78. "call _virt_to_phys\n\t" /* Switch to flat physical address space */
  79. "pushl %0\n\t" /* Pointer to CDECL helper function */
  80. "pushl %1\n\t" /* Pointer to FAR call helper function */
  81. "pushl %2\n\t" /* Size of low memory bounce buffer */
  82. "pushl %3\n\t" /* Pointer to low memory bounce buffer */
  83. "pushl %4\n\t" /* Pointer to INT call helper function */
  84. "pushl %5\n\t" /* Pointer to the command line arguments */
  85. "pushl $6\n\t" /* Number of additional arguments */
  86. "call *%6\n\t" /* Execute image */
  87. "call _phys_to_virt\n\t" /* Switch back to internal virtual address space */
  88. "movl (com32_internal_esp), %%esp\n\t" /* Switch back to internal stack */
  89. :
  90. :
  91. /* %0 */ "r" ( virt_to_phys ( com32_cfarcall_wrapper ) ),
  92. /* %1 */ "r" ( virt_to_phys ( com32_farcall_wrapper ) ),
  93. /* %2 */ "r" ( get_fbms() * 1024 - (COM32_BOUNCE_SEG << 4) ),
  94. /* %3 */ "i" ( COM32_BOUNCE_SEG << 4 ),
  95. /* %4 */ "r" ( virt_to_phys ( com32_intcall_wrapper ) ),
  96. /* %5 */ "r" ( virt_to_phys ( image->cmdline ) ),
  97. /* %6 */ "r" ( COM32_START_PHYS )
  98. :
  99. "memory" );
  100. break;
  101. case COMBOOT_RETURN_RUN_KERNEL:
  102. DBGC ( image, "COM32 %p: returned to run kernel...\n", image );
  103. comboot_run_kernel ( );
  104. break;
  105. case COMBOOT_RETURN_EXIT:
  106. break;
  107. }
  108. comboot_force_text_mode ( );
  109. DBGC ( image, "COM32 %p returned\n", image );
  110. /* Re-register image and return */
  111. register_image ( image );
  112. image_put ( image );
  113. return 0;
  114. }
  115. /**
  116. * Check image name extension
  117. *
  118. * @v image COM32 image
  119. * @ret rc Return status code
  120. */
  121. static int com32_identify ( struct image *image ) {
  122. const char *ext;
  123. static const uint8_t magic[] = { 0xB8, 0xFF, 0x4C, 0xCD, 0x21 };
  124. uint8_t buf[5];
  125. if ( image->len >= 5 ) {
  126. /* Check for magic number
  127. * mov eax,21cd4cffh
  128. * B8 FF 4C CD 21
  129. */
  130. copy_from_user ( buf, image->data, 0, sizeof(buf) );
  131. if ( ! memcmp ( buf, magic, sizeof(buf) ) ) {
  132. DBGC ( image, "COM32 %p: found magic number\n",
  133. image );
  134. return 0;
  135. }
  136. }
  137. /* Magic number not found; check filename extension */
  138. ext = strrchr( image->name, '.' );
  139. if ( ! ext ) {
  140. DBGC ( image, "COM32 %p: no extension\n",
  141. image );
  142. return -ENOEXEC;
  143. }
  144. ++ext;
  145. if ( strcasecmp( ext, "c32" ) ) {
  146. DBGC ( image, "COM32 %p: unrecognized extension %s\n",
  147. image, ext );
  148. return -ENOEXEC;
  149. }
  150. return 0;
  151. }
  152. /**
  153. * Load COM32 image into memory
  154. * @v image COM32 image
  155. * @ret rc Return status code
  156. */
  157. static int comboot_load_image ( struct image *image ) {
  158. size_t filesz, memsz;
  159. userptr_t buffer;
  160. int rc;
  161. filesz = image->len;
  162. memsz = filesz;
  163. buffer = phys_to_user ( COM32_START_PHYS );
  164. if ( ( rc = prep_segment ( buffer, filesz, memsz ) ) != 0 ) {
  165. DBGC ( image, "COM32 %p: could not prepare segment: %s\n",
  166. image, strerror ( rc ) );
  167. return rc;
  168. }
  169. /* Copy image to segment */
  170. memcpy_user ( buffer, 0, image->data, 0, filesz );
  171. return 0;
  172. }
  173. /**
  174. * Prepare COM32 low memory bounce buffer
  175. * @v image COM32 image
  176. * @ret rc Return status code
  177. */
  178. static int comboot_prepare_bounce_buffer ( struct image * image ) {
  179. unsigned int seg;
  180. userptr_t seg_userptr;
  181. size_t filesz, memsz;
  182. int rc;
  183. seg = COM32_BOUNCE_SEG;
  184. seg_userptr = real_to_user ( seg, 0 );
  185. /* Ensure the entire 64k segment is free */
  186. memsz = 0xFFFF;
  187. filesz = 0;
  188. /* Prepare, verify, and load the real-mode segment */
  189. if ( ( rc = prep_segment ( seg_userptr, filesz, memsz ) ) != 0 ) {
  190. DBGC ( image, "COM32 %p: could not prepare bounce buffer segment: %s\n",
  191. image, strerror ( rc ) );
  192. return rc;
  193. }
  194. return 0;
  195. }
  196. /**
  197. * Load COM32 image into memory
  198. *
  199. * @v image COM32 image
  200. * @ret rc Return status code
  201. */
  202. static int com32_load ( struct image *image ) {
  203. int rc;
  204. DBGC ( image, "COM32 %p: name '%s', cmdline '%s'\n",
  205. image, image->name, image->cmdline );
  206. /* Check if this is a COMBOOT image */
  207. if ( ( rc = com32_identify ( image ) ) != 0 ) {
  208. return rc;
  209. }
  210. /* This is a COM32 image, valid or otherwise */
  211. if ( ! image->type )
  212. image->type = &com32_image_type;
  213. /* Load image */
  214. if ( ( rc = comboot_load_image ( image ) ) != 0 ) {
  215. return rc;
  216. }
  217. /* Prepare bounce buffer segment */
  218. if ( ( rc = comboot_prepare_bounce_buffer ( image ) ) != 0 ) {
  219. return rc;
  220. }
  221. return 0;
  222. }
  223. /** SYSLINUX COM32 image type */
  224. struct image_type com32_image_type __image_type ( PROBE_NORMAL ) = {
  225. .name = "COM32",
  226. .load = com32_load,
  227. .exec = com32_exec,
  228. };