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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <ipxe/uaccess.h>
  25. #include <ipxe/init.h>
  26. #include <ipxe/profile.h>
  27. #include <setjmp.h>
  28. #include <registers.h>
  29. #include <biosint.h>
  30. #include <pxe.h>
  31. #include <pxe_call.h>
  32. /** @file
  33. *
  34. * PXE API entry point
  35. */
  36. /* Disambiguate the various error causes */
  37. #define EINFO_EPXENBP \
  38. __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
  39. "External PXE NBP error" )
  40. #define EPXENBP( status ) EPLATFORM ( EINFO_EPXENBP, status )
  41. /** Vector for chaining INT 1A */
  42. extern struct segoff __text16 ( pxe_int_1a_vector );
  43. #define pxe_int_1a_vector __use_text16 ( pxe_int_1a_vector )
  44. /** INT 1A handler */
  45. extern void pxe_int_1a ( void );
  46. /** INT 1A hooked flag */
  47. static int int_1a_hooked = 0;
  48. /** PXENV_UNDI_TRANSMIT API call profiler */
  49. static struct profiler pxe_api_tx_profiler __profiler =
  50. { .name = "pxeapi.tx" };
  51. /** PXENV_UNDI_ISR API call profiler */
  52. static struct profiler pxe_api_isr_profiler __profiler =
  53. { .name = "pxeapi.isr" };
  54. /** PXE unknown API call profiler
  55. *
  56. * This profiler can be used to measure the overhead of a dummy PXE
  57. * API call.
  58. */
  59. static struct profiler pxe_api_unknown_profiler __profiler =
  60. { .name = "pxeapi.unknown" };
  61. /** Miscellaneous PXE API call profiler */
  62. static struct profiler pxe_api_misc_profiler __profiler =
  63. { .name = "pxeapi.misc" };
  64. /**
  65. * Handle an unknown PXE API call
  66. *
  67. * @v pxenv_unknown Pointer to a struct s_PXENV_UNKNOWN
  68. * @ret #PXENV_EXIT_FAILURE Always
  69. * @err #PXENV_STATUS_UNSUPPORTED Always
  70. */
  71. static PXENV_EXIT_t pxenv_unknown ( struct s_PXENV_UNKNOWN *pxenv_unknown ) {
  72. pxenv_unknown->Status = PXENV_STATUS_UNSUPPORTED;
  73. return PXENV_EXIT_FAILURE;
  74. }
  75. /** Unknown PXE API call list */
  76. struct pxe_api_call pxenv_unknown_api __pxe_api_call =
  77. PXE_API_CALL ( PXENV_UNKNOWN, pxenv_unknown, struct s_PXENV_UNKNOWN );
  78. /**
  79. * Locate PXE API call
  80. *
  81. * @v opcode Opcode
  82. * @ret call PXE API call, or NULL
  83. */
  84. static struct pxe_api_call * find_pxe_api_call ( uint16_t opcode ) {
  85. struct pxe_api_call *call;
  86. for_each_table_entry ( call, PXE_API_CALLS ) {
  87. if ( call->opcode == opcode )
  88. return call;
  89. }
  90. return NULL;
  91. }
  92. /**
  93. * Determine applicable profiler (for debugging)
  94. *
  95. * @v opcode PXE opcode
  96. * @ret profiler Profiler
  97. */
  98. static struct profiler * pxe_api_profiler ( unsigned int opcode ) {
  99. /* Determine applicable profiler */
  100. switch ( opcode ) {
  101. case PXENV_UNDI_TRANSMIT:
  102. return &pxe_api_tx_profiler;
  103. case PXENV_UNDI_ISR:
  104. return &pxe_api_isr_profiler;
  105. case PXENV_UNKNOWN:
  106. return &pxe_api_unknown_profiler;
  107. default:
  108. return &pxe_api_misc_profiler;
  109. }
  110. }
  111. /**
  112. * Dispatch PXE API call
  113. *
  114. * @v bx PXE opcode
  115. * @v es:di Address of PXE parameter block
  116. * @ret ax PXE exit code
  117. */
  118. __asmcall void pxe_api_call ( struct i386_all_regs *ix86 ) {
  119. uint16_t opcode = ix86->regs.bx;
  120. userptr_t uparams = real_to_user ( ix86->segs.es, ix86->regs.di );
  121. struct profiler *profiler = pxe_api_profiler ( opcode );
  122. struct pxe_api_call *call;
  123. union u_PXENV_ANY params;
  124. PXENV_EXIT_t ret;
  125. /* Start profiling */
  126. profile_start ( profiler );
  127. /* Locate API call */
  128. call = find_pxe_api_call ( opcode );
  129. if ( ! call ) {
  130. DBGC ( &pxe_netdev, "PXENV_UNKNOWN_%04x\n", opcode );
  131. call = &pxenv_unknown_api;
  132. }
  133. /* Copy parameter block from caller */
  134. copy_from_user ( &params, uparams, 0, call->params_len );
  135. /* Set default status in case child routine fails to do so */
  136. params.Status = PXENV_STATUS_FAILURE;
  137. /* Hand off to relevant API routine */
  138. ret = call->entry ( &params );
  139. /* Copy modified parameter block back to caller and return */
  140. copy_to_user ( uparams, 0, &params, call->params_len );
  141. ix86->regs.ax = ret;
  142. /* Stop profiling, if applicable */
  143. profile_stop ( profiler );
  144. }
  145. /**
  146. * Dispatch weak PXE API call with PXE stack available
  147. *
  148. * @v ix86 Registers for PXE call
  149. * @ret present Zero (PXE stack present)
  150. */
  151. int pxe_api_call_weak ( struct i386_all_regs *ix86 ) {
  152. pxe_api_call ( ix86 );
  153. return 0;
  154. }
  155. /**
  156. * Dispatch PXE loader call
  157. *
  158. * @v es:di Address of PXE parameter block
  159. * @ret ax PXE exit code
  160. */
  161. __asmcall void pxe_loader_call ( struct i386_all_regs *ix86 ) {
  162. userptr_t uparams = real_to_user ( ix86->segs.es, ix86->regs.di );
  163. struct s_UNDI_LOADER params;
  164. PXENV_EXIT_t ret;
  165. /* Copy parameter block from caller */
  166. copy_from_user ( &params, uparams, 0, sizeof ( params ) );
  167. /* Fill in ROM segment address */
  168. ppxe.UNDIROMID.segment = ix86->segs.ds;
  169. /* Set default status in case child routine fails to do so */
  170. params.Status = PXENV_STATUS_FAILURE;
  171. /* Call UNDI loader */
  172. ret = undi_loader ( &params );
  173. /* Copy modified parameter block back to caller and return */
  174. copy_to_user ( uparams, 0, &params, sizeof ( params ) );
  175. ix86->regs.ax = ret;
  176. }
  177. /**
  178. * Calculate byte checksum as used by PXE
  179. *
  180. * @v data Data
  181. * @v size Length of data
  182. * @ret sum Checksum
  183. */
  184. static uint8_t pxe_checksum ( void *data, size_t size ) {
  185. uint8_t *bytes = data;
  186. uint8_t sum = 0;
  187. while ( size-- ) {
  188. sum += *bytes++;
  189. }
  190. return sum;
  191. }
  192. /**
  193. * Initialise !PXE and PXENV+ structures
  194. *
  195. */
  196. static void pxe_init_structures ( void ) {
  197. uint32_t rm_cs_phys = ( rm_cs << 4 );
  198. uint32_t rm_ds_phys = ( rm_ds << 4 );
  199. /* Fill in missing segment fields */
  200. ppxe.EntryPointSP.segment = rm_cs;
  201. ppxe.EntryPointESP.segment = rm_cs;
  202. ppxe.Stack.segment_address = rm_ds;
  203. ppxe.Stack.Physical_address = rm_ds_phys;
  204. ppxe.UNDIData.segment_address = rm_ds;
  205. ppxe.UNDIData.Physical_address = rm_ds_phys;
  206. ppxe.UNDICode.segment_address = rm_cs;
  207. ppxe.UNDICode.Physical_address = rm_cs_phys;
  208. ppxe.UNDICodeWrite.segment_address = rm_cs;
  209. ppxe.UNDICodeWrite.Physical_address = rm_cs_phys;
  210. pxenv.RMEntry.segment = rm_cs;
  211. pxenv.StackSeg = rm_ds;
  212. pxenv.UNDIDataSeg = rm_ds;
  213. pxenv.UNDICodeSeg = rm_cs;
  214. pxenv.PXEPtr.segment = rm_cs;
  215. /* Update checksums */
  216. ppxe.StructCksum -= pxe_checksum ( &ppxe, sizeof ( ppxe ) );
  217. pxenv.Checksum -= pxe_checksum ( &pxenv, sizeof ( pxenv ) );
  218. }
  219. /** PXE structure initialiser */
  220. struct init_fn pxe_init_fn __init_fn ( INIT_NORMAL ) = {
  221. .initialise = pxe_init_structures,
  222. };
  223. /**
  224. * Activate PXE stack
  225. *
  226. * @v netdev Net device to use as PXE net device
  227. */
  228. void pxe_activate ( struct net_device *netdev ) {
  229. /* Ensure INT 1A is hooked */
  230. if ( ! int_1a_hooked ) {
  231. hook_bios_interrupt ( 0x1a, ( unsigned int ) pxe_int_1a,
  232. &pxe_int_1a_vector );
  233. devices_get();
  234. int_1a_hooked = 1;
  235. }
  236. /* Set PXE network device */
  237. pxe_set_netdev ( netdev );
  238. }
  239. /**
  240. * Deactivate PXE stack
  241. *
  242. * @ret rc Return status code
  243. */
  244. int pxe_deactivate ( void ) {
  245. int rc;
  246. /* Clear PXE network device */
  247. pxe_set_netdev ( NULL );
  248. /* Ensure INT 1A is unhooked, if possible */
  249. if ( int_1a_hooked ) {
  250. if ( ( rc = unhook_bios_interrupt ( 0x1a,
  251. (unsigned int) pxe_int_1a,
  252. &pxe_int_1a_vector ))!= 0){
  253. DBG ( "Could not unhook INT 1A: %s\n",
  254. strerror ( rc ) );
  255. return rc;
  256. }
  257. devices_put();
  258. int_1a_hooked = 0;
  259. }
  260. return 0;
  261. }
  262. /** Jump buffer for PXENV_RESTART_TFTP */
  263. rmjmp_buf pxe_restart_nbp;
  264. /**
  265. * Start PXE NBP at 0000:7c00
  266. *
  267. * @ret rc Return status code
  268. */
  269. int pxe_start_nbp ( void ) {
  270. int jmp;
  271. int discard_b, discard_c, discard_d, discard_D;
  272. uint16_t status;
  273. /* Allow restarting NBP via PXENV_RESTART_TFTP */
  274. jmp = rmsetjmp ( pxe_restart_nbp );
  275. if ( jmp )
  276. DBG ( "Restarting NBP (%x)\n", jmp );
  277. /* Far call to PXE NBP */
  278. __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
  279. "movw %%cx, %%es\n\t"
  280. "pushw %%es\n\t"
  281. "pushw %%di\n\t"
  282. "sti\n\t"
  283. "lcall $0, $0x7c00\n\t"
  284. "popl %%ebp\n\t" /* discard */
  285. "popl %%ebp\n\t" /* gcc bug */ )
  286. : "=a" ( status ), "=b" ( discard_b ),
  287. "=c" ( discard_c ), "=d" ( discard_d ),
  288. "=D" ( discard_D )
  289. : "a" ( 0 ), "b" ( __from_text16 ( &pxenv ) ),
  290. "c" ( rm_cs ),
  291. "d" ( virt_to_phys ( &pxenv ) ),
  292. "D" ( __from_text16 ( &ppxe ) )
  293. : "esi", "memory" );
  294. if ( status )
  295. return -EPXENBP ( status );
  296. return 0;
  297. }
  298. REQUIRE_OBJECT ( pxe_preboot );
  299. REQUIRE_OBJECT ( pxe_undi );
  300. REQUIRE_OBJECT ( pxe_udp );
  301. REQUIRE_OBJECT ( pxe_tftp );
  302. REQUIRE_OBJECT ( pxe_file );