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_image.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2007 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. /**
  25. * @file
  26. *
  27. * PXE image format
  28. *
  29. */
  30. #include <pxe.h>
  31. #include <pxe_call.h>
  32. #include <ipxe/uaccess.h>
  33. #include <ipxe/image.h>
  34. #include <ipxe/segment.h>
  35. #include <ipxe/netdevice.h>
  36. #include <ipxe/features.h>
  37. #include <ipxe/console.h>
  38. #include <ipxe/efi/efi.h>
  39. #include <ipxe/efi/IndustryStandard/PeImage.h>
  40. FEATURE ( FEATURE_IMAGE, "PXE", DHCP_EB_FEATURE_PXE, 1 );
  41. /** PXE command line */
  42. const char *pxe_cmdline;
  43. /**
  44. * Execute PXE image
  45. *
  46. * @v image PXE image
  47. * @ret rc Return status code
  48. */
  49. static int pxe_exec ( struct image *image ) {
  50. userptr_t buffer = real_to_user ( 0, 0x7c00 );
  51. struct net_device *netdev;
  52. int rc;
  53. /* Verify and prepare segment */
  54. if ( ( rc = prep_segment ( buffer, image->len, image->len ) ) != 0 ) {
  55. DBGC ( image, "IMAGE %p could not prepare segment: %s\n",
  56. image, strerror ( rc ) );
  57. return rc;
  58. }
  59. /* Copy image to segment */
  60. memcpy_user ( buffer, 0, image->data, 0, image->len );
  61. /* Arbitrarily pick the most recently opened network device */
  62. if ( ( netdev = last_opened_netdev() ) == NULL ) {
  63. DBGC ( image, "IMAGE %p could not locate PXE net device\n",
  64. image );
  65. return -ENODEV;
  66. }
  67. netdev_get ( netdev );
  68. /* Activate PXE */
  69. pxe_activate ( netdev );
  70. /* Construct fake DHCP packets */
  71. pxe_fake_cached_info();
  72. /* Set PXE command line */
  73. pxe_cmdline = image->cmdline;
  74. /* Reset console since PXE NBP will probably use it */
  75. console_reset();
  76. /* Start PXE NBP */
  77. rc = pxe_start_nbp();
  78. /* Clear PXE command line */
  79. pxe_cmdline = NULL;
  80. /* Deactivate PXE */
  81. pxe_deactivate();
  82. /* Try to reopen network device. Ignore errors, since the NBP
  83. * may have called PXENV_STOP_UNDI.
  84. */
  85. netdev_open ( netdev );
  86. netdev_put ( netdev );
  87. return rc;
  88. }
  89. /**
  90. * Probe PXE image
  91. *
  92. * @v image PXE file
  93. * @ret rc Return status code
  94. */
  95. int pxe_probe ( struct image *image ) {
  96. /* Images too large to fit in base memory cannot be PXE
  97. * images. We include this check to help prevent unrecognised
  98. * images from being marked as PXE images, since PXE images
  99. * have no signature we can check against.
  100. */
  101. if ( image->len > ( 0xa0000 - 0x7c00 ) )
  102. return -ENOEXEC;
  103. /* Rejecting zero-length images is also useful, since these
  104. * end up looking to the user like bugs in iPXE.
  105. */
  106. if ( ! image->len )
  107. return -ENOEXEC;
  108. return 0;
  109. }
  110. /**
  111. * Probe PXE image (with rejection of potential EFI images)
  112. *
  113. * @v image PXE file
  114. * @ret rc Return status code
  115. */
  116. int pxe_probe_no_mz ( struct image *image ) {
  117. uint16_t magic;
  118. int rc;
  119. /* Probe PXE image */
  120. if ( ( rc = pxe_probe ( image ) ) != 0 )
  121. return rc;
  122. /* Reject image with an "MZ" signature which may indicate an
  123. * EFI image incorrectly handed out to a BIOS system.
  124. */
  125. if ( image->len >= sizeof ( magic ) ) {
  126. copy_from_user ( &magic, image->data, 0, sizeof ( magic ) );
  127. if ( magic == cpu_to_le16 ( EFI_IMAGE_DOS_SIGNATURE ) ) {
  128. DBGC ( image, "IMAGE %p may be an EFI image\n",
  129. image );
  130. return -ENOTTY;
  131. }
  132. }
  133. return 0;
  134. }
  135. /** PXE image type */
  136. struct image_type pxe_image_type[] __image_type ( PROBE_PXE ) = {
  137. {
  138. .name = "PXE-NBP",
  139. .probe = pxe_probe_no_mz,
  140. .exec = pxe_exec,
  141. },
  142. {
  143. .name = "PXE-NBP (may be EFI?)",
  144. .probe = pxe_probe,
  145. .exec = pxe_exec,
  146. },
  147. };