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.

pxe_call.c 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <ipxe/uaccess.h>
  21. #include <ipxe/init.h>
  22. #include <setjmp.h>
  23. #include <registers.h>
  24. #include <biosint.h>
  25. #include <pxe.h>
  26. #include <pxe_call.h>
  27. /** @file
  28. *
  29. * PXE API entry point
  30. */
  31. /** Vector for chaining INT 1A */
  32. extern struct segoff __text16 ( pxe_int_1a_vector );
  33. #define pxe_int_1a_vector __use_text16 ( pxe_int_1a_vector )
  34. /** INT 1A handler */
  35. extern void pxe_int_1a ( void );
  36. /** INT 1A hooked flag */
  37. static int int_1a_hooked = 0;
  38. /**
  39. * Handle an unknown PXE API call
  40. *
  41. * @v pxenv_unknown Pointer to a struct s_PXENV_UNKNOWN
  42. * @ret #PXENV_EXIT_FAILURE Always
  43. * @err #PXENV_STATUS_UNSUPPORTED Always
  44. */
  45. static PXENV_EXIT_t pxenv_unknown ( struct s_PXENV_UNKNOWN *pxenv_unknown ) {
  46. pxenv_unknown->Status = PXENV_STATUS_UNSUPPORTED;
  47. return PXENV_EXIT_FAILURE;
  48. }
  49. /** Unknown PXE API call list */
  50. struct pxe_api_call pxenv_unknown_api __pxe_api_call =
  51. PXE_API_CALL ( PXENV_UNKNOWN, pxenv_unknown, struct s_PXENV_UNKNOWN );
  52. /**
  53. * Locate PXE API call
  54. *
  55. * @v opcode Opcode
  56. * @ret call PXE API call, or NULL
  57. */
  58. static struct pxe_api_call * find_pxe_api_call ( uint16_t opcode ) {
  59. struct pxe_api_call *call;
  60. for_each_table_entry ( call, PXE_API_CALLS ) {
  61. if ( call->opcode == opcode )
  62. return call;
  63. }
  64. return NULL;
  65. }
  66. /**
  67. * Dispatch PXE API call
  68. *
  69. * @v bx PXE opcode
  70. * @v es:di Address of PXE parameter block
  71. * @ret ax PXE exit code
  72. */
  73. __asmcall void pxe_api_call ( struct i386_all_regs *ix86 ) {
  74. uint16_t opcode = ix86->regs.bx;
  75. userptr_t uparams = real_to_user ( ix86->segs.es, ix86->regs.di );
  76. struct pxe_api_call *call;
  77. union u_PXENV_ANY params;
  78. PXENV_EXIT_t ret;
  79. /* Locate API call */
  80. call = find_pxe_api_call ( opcode );
  81. if ( ! call ) {
  82. DBGC ( &pxe_netdev, "PXENV_UNKNOWN_%04x\n", opcode );
  83. call = &pxenv_unknown_api;
  84. }
  85. /* Copy parameter block from caller */
  86. copy_from_user ( &params, uparams, 0, call->params_len );
  87. /* Set default status in case child routine fails to do so */
  88. params.Status = PXENV_STATUS_FAILURE;
  89. /* Hand off to relevant API routine */
  90. ret = call->entry ( &params );
  91. /* Copy modified parameter block back to caller and return */
  92. copy_to_user ( uparams, 0, &params, call->params_len );
  93. ix86->regs.ax = ret;
  94. }
  95. /**
  96. * Dispatch weak PXE API call with PXE stack available
  97. *
  98. * @v ix86 Registers for PXE call
  99. * @ret present Zero (PXE stack present)
  100. */
  101. int pxe_api_call_weak ( struct i386_all_regs *ix86 ) {
  102. pxe_api_call ( ix86 );
  103. return 0;
  104. }
  105. /**
  106. * Dispatch PXE loader call
  107. *
  108. * @v es:di Address of PXE parameter block
  109. * @ret ax PXE exit code
  110. */
  111. __asmcall void pxe_loader_call ( struct i386_all_regs *ix86 ) {
  112. userptr_t uparams = real_to_user ( ix86->segs.es, ix86->regs.di );
  113. struct s_UNDI_LOADER params;
  114. PXENV_EXIT_t ret;
  115. /* Copy parameter block from caller */
  116. copy_from_user ( &params, uparams, 0, sizeof ( params ) );
  117. /* Fill in ROM segment address */
  118. ppxe.UNDIROMID.segment = ix86->segs.ds;
  119. /* Set default status in case child routine fails to do so */
  120. params.Status = PXENV_STATUS_FAILURE;
  121. /* Call UNDI loader */
  122. ret = undi_loader ( &params );
  123. /* Copy modified parameter block back to caller and return */
  124. copy_to_user ( uparams, 0, &params, sizeof ( params ) );
  125. ix86->regs.ax = ret;
  126. }
  127. /**
  128. * Calculate byte checksum as used by PXE
  129. *
  130. * @v data Data
  131. * @v size Length of data
  132. * @ret sum Checksum
  133. */
  134. static uint8_t pxe_checksum ( void *data, size_t size ) {
  135. uint8_t *bytes = data;
  136. uint8_t sum = 0;
  137. while ( size-- ) {
  138. sum += *bytes++;
  139. }
  140. return sum;
  141. }
  142. /**
  143. * Initialise !PXE and PXENV+ structures
  144. *
  145. */
  146. static void pxe_init_structures ( void ) {
  147. uint32_t rm_cs_phys = ( rm_cs << 4 );
  148. uint32_t rm_ds_phys = ( rm_ds << 4 );
  149. /* Fill in missing segment fields */
  150. ppxe.EntryPointSP.segment = rm_cs;
  151. ppxe.EntryPointESP.segment = rm_cs;
  152. ppxe.Stack.segment_address = rm_ds;
  153. ppxe.Stack.Physical_address = rm_ds_phys;
  154. ppxe.UNDIData.segment_address = rm_ds;
  155. ppxe.UNDIData.Physical_address = rm_ds_phys;
  156. ppxe.UNDICode.segment_address = rm_cs;
  157. ppxe.UNDICode.Physical_address = rm_cs_phys;
  158. ppxe.UNDICodeWrite.segment_address = rm_cs;
  159. ppxe.UNDICodeWrite.Physical_address = rm_cs_phys;
  160. pxenv.RMEntry.segment = rm_cs;
  161. pxenv.StackSeg = rm_ds;
  162. pxenv.UNDIDataSeg = rm_ds;
  163. pxenv.UNDICodeSeg = rm_cs;
  164. pxenv.PXEPtr.segment = rm_cs;
  165. /* Update checksums */
  166. ppxe.StructCksum -= pxe_checksum ( &ppxe, sizeof ( ppxe ) );
  167. pxenv.Checksum -= pxe_checksum ( &pxenv, sizeof ( pxenv ) );
  168. }
  169. /** PXE structure initialiser */
  170. struct init_fn pxe_init_fn __init_fn ( INIT_NORMAL ) = {
  171. .initialise = pxe_init_structures,
  172. };
  173. /**
  174. * Activate PXE stack
  175. *
  176. * @v netdev Net device to use as PXE net device
  177. */
  178. void pxe_activate ( struct net_device *netdev ) {
  179. /* Ensure INT 1A is hooked */
  180. if ( ! int_1a_hooked ) {
  181. hook_bios_interrupt ( 0x1a, ( unsigned int ) pxe_int_1a,
  182. &pxe_int_1a_vector );
  183. devices_get();
  184. int_1a_hooked = 1;
  185. }
  186. /* Set PXE network device */
  187. pxe_set_netdev ( netdev );
  188. }
  189. /**
  190. * Deactivate PXE stack
  191. *
  192. * @ret rc Return status code
  193. */
  194. int pxe_deactivate ( void ) {
  195. int rc;
  196. /* Clear PXE network device */
  197. pxe_set_netdev ( NULL );
  198. /* Ensure INT 1A is unhooked, if possible */
  199. if ( int_1a_hooked ) {
  200. if ( ( rc = unhook_bios_interrupt ( 0x1a,
  201. (unsigned int) pxe_int_1a,
  202. &pxe_int_1a_vector ))!= 0){
  203. DBG ( "Could not unhook INT 1A: %s\n",
  204. strerror ( rc ) );
  205. return rc;
  206. }
  207. devices_put();
  208. int_1a_hooked = 0;
  209. }
  210. return 0;
  211. }
  212. /** Jump buffer for PXENV_RESTART_TFTP */
  213. rmjmp_buf pxe_restart_nbp;
  214. /**
  215. * Start PXE NBP at 0000:7c00
  216. *
  217. * @ret rc Return status code
  218. */
  219. int pxe_start_nbp ( void ) {
  220. int jmp;
  221. int discard_b, discard_c, discard_d, discard_D;
  222. uint16_t rc;
  223. /* Allow restarting NBP via PXENV_RESTART_TFTP */
  224. jmp = rmsetjmp ( pxe_restart_nbp );
  225. if ( jmp )
  226. DBG ( "Restarting NBP (%x)\n", jmp );
  227. /* Far call to PXE NBP */
  228. __asm__ __volatile__ ( REAL_CODE ( "movw %%cx, %%es\n\t"
  229. "pushw %%es\n\t"
  230. "pushw %%di\n\t"
  231. "sti\n\t"
  232. "lcall $0, $0x7c00\n\t"
  233. "addw $4, %%sp\n\t" )
  234. : "=a" ( rc ), "=b" ( discard_b ),
  235. "=c" ( discard_c ), "=d" ( discard_d ),
  236. "=D" ( discard_D )
  237. : "a" ( 0 ), "b" ( __from_text16 ( &pxenv ) ),
  238. "c" ( rm_cs ),
  239. "d" ( virt_to_phys ( &pxenv ) ),
  240. "D" ( __from_text16 ( &ppxe ) )
  241. : "esi", "ebp", "memory" );
  242. return rc;
  243. }
  244. REQUIRE_OBJECT ( pxe_preboot );
  245. REQUIRE_OBJECT ( pxe_undi );
  246. REQUIRE_OBJECT ( pxe_udp );
  247. REQUIRE_OBJECT ( pxe_tftp );
  248. REQUIRE_OBJECT ( pxe_file );