Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / image / pxe_image.c
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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /**
23  * @file
24  *
25  * PXE image format
26  *
27  */
28
29 #include <pxe.h>
30 #include <pxe_call.h>
31 #include <ipxe/uaccess.h>
32 #include <ipxe/image.h>
33 #include <ipxe/segment.h>
34 #include <ipxe/netdevice.h>
35 #include <ipxe/features.h>
36 #include <ipxe/console.h>
37
38 FEATURE ( FEATURE_IMAGE, "PXE", DHCP_EB_FEATURE_PXE, 1 );
39
40 /** PXE command line */
41 const char *pxe_cmdline;
42
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
54         /* Verify and prepare segment */
55         if ( ( rc = prep_segment ( buffer, image->len, image->len ) ) != 0 ) {
56                 DBGC ( image, "IMAGE %p could not prepare segment: %s\n",
57                        image, strerror ( rc ) );
58                 return rc;
59         }
60
61         /* Copy image to segment */
62         memcpy_user ( buffer, 0, image->data, 0, image->len );
63
64         /* Arbitrarily pick the most recently opened network device */
65         if ( ( netdev = last_opened_netdev() ) == NULL ) {
66                 DBGC ( image, "IMAGE %p could not locate PXE net device\n",
67                        image );
68                 return -ENODEV;
69         }
70         netdev_get ( netdev );
71
72         /* Activate PXE */
73         pxe_activate ( netdev );
74
75         /* Set PXE command line */
76         pxe_cmdline = image->cmdline;
77
78         /* Reset console since PXE NBP will probably use it */
79         console_reset();
80
81         /* Start PXE NBP */
82         rc = pxe_start_nbp();
83
84         /* Clear PXE command line */
85         pxe_cmdline = NULL;
86
87         /* Deactivate PXE */
88         pxe_deactivate();
89
90         /* Try to reopen network device.  Ignore errors, since the NBP
91          * may have called PXENV_STOP_UNDI.
92          */
93         netdev_open ( netdev );
94         netdev_put ( netdev );
95
96         return rc;
97 }
98
99 /**
100  * Probe PXE image
101  *
102  * @v image             PXE file
103  * @ret rc              Return status code
104  */
105 int pxe_probe ( struct image *image ) {
106
107         /* Images too large to fit in base memory cannot be PXE
108          * images.  We include this check to help prevent unrecognised
109          * images from being marked as PXE images, since PXE images
110          * have no signature we can check against.
111          */
112         if ( image->len > ( 0xa0000 - 0x7c00 ) )
113                 return -ENOEXEC;
114
115         /* Rejecting zero-length images is also useful, since these
116          * end up looking to the user like bugs in iPXE.
117          */
118         if ( ! image->len )
119                 return -ENOEXEC;
120
121         return 0;
122 }
123
124 /** PXE image type */
125 struct image_type pxe_image_type __image_type ( PROBE_PXE ) = {
126         .name = "PXE",
127         .probe = pxe_probe,
128         .exec = pxe_exec,
129 };