2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
35 #include <ipxe/uaccess.h>
36 #include <ipxe/image.h>
37 #include <ipxe/segment.h>
38 #include <ipxe/netdevice.h>
39 #include <ipxe/features.h>
40 #include <ipxe/console.h>
41 #include <ipxe/efi/efi.h>
42 #include <ipxe/efi/IndustryStandard/PeImage.h>
44 FEATURE ( FEATURE_IMAGE, "PXE", DHCP_EB_FEATURE_PXE, 1 );
46 /** PXE command line */
47 const char *pxe_cmdline;
53 * @ret rc Return status code
55 static int pxe_exec ( struct image *image ) {
56 userptr_t buffer = real_to_user ( 0, 0x7c00 );
57 struct net_device *netdev;
60 /* Verify and prepare segment */
61 if ( ( rc = prep_segment ( buffer, image->len, image->len ) ) != 0 ) {
62 DBGC ( image, "IMAGE %p could not prepare segment: %s\n",
63 image, strerror ( rc ) );
67 /* Copy image to segment */
68 memcpy_user ( buffer, 0, image->data, 0, image->len );
70 /* Arbitrarily pick the most recently opened network device */
71 if ( ( netdev = last_opened_netdev() ) == NULL ) {
72 DBGC ( image, "IMAGE %p could not locate PXE net device\n",
76 netdev_get ( netdev );
79 pxe_activate ( netdev );
81 /* Set PXE command line */
82 pxe_cmdline = image->cmdline;
84 /* Reset console since PXE NBP will probably use it */
90 /* Clear PXE command line */
96 /* Try to reopen network device. Ignore errors, since the NBP
97 * may have called PXENV_STOP_UNDI.
99 netdev_open ( netdev );
100 netdev_put ( netdev );
109 * @ret rc Return status code
111 int pxe_probe ( struct image *image ) {
113 /* Images too large to fit in base memory cannot be PXE
114 * images. We include this check to help prevent unrecognised
115 * images from being marked as PXE images, since PXE images
116 * have no signature we can check against.
118 if ( image->len > ( 0xa0000 - 0x7c00 ) )
121 /* Rejecting zero-length images is also useful, since these
122 * end up looking to the user like bugs in iPXE.
131 * Probe PXE image (with rejection of potential EFI images)
134 * @ret rc Return status code
136 int pxe_probe_no_mz ( struct image *image ) {
140 /* Probe PXE image */
141 if ( ( rc = pxe_probe ( image ) ) != 0 )
144 /* Reject image with an "MZ" signature which may indicate an
145 * EFI image incorrectly handed out to a BIOS system.
147 if ( image->len >= sizeof ( magic ) ) {
148 copy_from_user ( &magic, image->data, 0, sizeof ( magic ) );
149 if ( magic == cpu_to_le16 ( EFI_IMAGE_DOS_SIGNATURE ) ) {
150 DBGC ( image, "IMAGE %p may be an EFI image\n",
159 /** PXE image type */
160 struct image_type pxe_image_type[] __image_type ( PROBE_PXE ) = {
163 .probe = pxe_probe_no_mz,
167 .name = "PXE-NBP (may be EFI?)",