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