1 /** @file
2   This EFI_PXE_BASE_CODE_PROTOCOL and EFI_LOAD_FILE_PROTOCOL.
3   interfaces declaration.
4 
5   Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.<BR>
6 
7   This program and the accompanying materials
8   are licensed and made available under the terms and conditions of the BSD License
9   which accompanies this distribution.  The full text of the license may be found at
10   http://opensource.org/licenses/bsd-license.php.
11 
12   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 
15 **/
16 
17 #ifndef __EFI_PXEBC_IMPL_H__
18 #define __EFI_PXEBC_IMPL_H__
19 
20 #include <Uefi.h>
21 
22 #include <Guid/SmBios.h>
23 #include <IndustryStandard/SmBios.h>
24 #include <Protocol/NetworkInterfaceIdentifier.h>
25 #include <Protocol/Arp.h>
26 #include <Protocol/Ip4.h>
27 #include <Protocol/Ip4Config2.h>
28 #include <Protocol/Ip6.h>
29 #include <Protocol/Ip6Config.h>
30 #include <Protocol/Udp4.h>
31 #include <Protocol/Udp6.h>
32 #include <Protocol/Dhcp4.h>
33 #include <Protocol/Dhcp6.h>
34 #include <Protocol/Mtftp4.h>
35 #include <Protocol/Mtftp6.h>
36 #include <Protocol/PxeBaseCode.h>
37 #include <Protocol/LoadFile.h>
38 #include <Protocol/PxeBaseCodeCallBack.h>
39 #include <Protocol/ServiceBinding.h>
40 #include <Protocol/DriverBinding.h>
41 #include <Protocol/AdapterInformation.h>
42 
43 #include <Library/DebugLib.h>
44 #include <Library/BaseMemoryLib.h>
45 #include <Library/MemoryAllocationLib.h>
46 #include <Library/UefiDriverEntryPoint.h>
47 #include <Library/UefiBootServicesTableLib.h>
48 #include <Library/UefiRuntimeServicesTableLib.h>
49 #include <Library/UefiLib.h>
50 #include <Library/BaseLib.h>
51 #include <Library/NetLib.h>
52 #include <Library/DpcLib.h>
53 #include <Library/DevicePathLib.h>
54 #include <Library/PcdLib.h>
55 
56 typedef struct _PXEBC_PRIVATE_DATA  PXEBC_PRIVATE_DATA;
57 typedef struct _PXEBC_PRIVATE_PROTOCOL PXEBC_PRIVATE_PROTOCOL;
58 typedef struct _PXEBC_VIRTUAL_NIC   PXEBC_VIRTUAL_NIC;
59 
60 #include "PxeBcDriver.h"
61 #include "PxeBcDhcp4.h"
62 #include "PxeBcDhcp6.h"
63 #include "PxeBcMtftp.h"
64 #include "PxeBcBoot.h"
65 #include "PxeBcSupport.h"
66 
67 #define PXEBC_DEFAULT_HOPLIMIT        64
68 #define PXEBC_DEFAULT_LIFETIME        50000    // 50 ms, unit is microsecond
69 #define PXEBC_UDP_TIMEOUT             30000000 // 3 seconds, unit is 100nanosecond
70 #define PXEBC_DAD_ADDITIONAL_DELAY    30000000 // 3 seconds
71 #define PXEBC_MTFTP_TIMEOUT           4
72 #define PXEBC_MTFTP_RETRIES           6
73 #define PXEBC_DHCP_RETRIES            4        // refers to mPxeDhcpTimeout, also by PXE2.1 spec.
74 #define PXEBC_MENU_MAX_NUM            24
75 #define PXEBC_OFFER_MAX_NUM           16
76 
77 #define PXEBC_PRIVATE_DATA_SIGNATURE          SIGNATURE_32 ('P', 'X', 'E', 'P')
78 #define PXEBC_VIRTUAL_NIC_SIGNATURE           SIGNATURE_32 ('P', 'X', 'E', 'V')
79 #define PXEBC_PRIVATE_DATA_FROM_PXEBC(a)      CR (a, PXEBC_PRIVATE_DATA, PxeBc, PXEBC_PRIVATE_DATA_SIGNATURE)
80 #define PXEBC_PRIVATE_DATA_FROM_ID(a)         CR (a, PXEBC_PRIVATE_DATA, Id, PXEBC_PRIVATE_DATA_SIGNATURE)
81 #define PXEBC_VIRTUAL_NIC_FROM_LOADFILE(a)    CR (a, PXEBC_VIRTUAL_NIC, LoadFile, PXEBC_VIRTUAL_NIC_SIGNATURE)
82 
83 typedef union {
84   PXEBC_DHCP4_PACKET_CACHE            Dhcp4;
85   PXEBC_DHCP6_PACKET_CACHE            Dhcp6;
86 } PXEBC_DHCP_PACKET_CACHE;
87 
88 struct _PXEBC_PRIVATE_PROTOCOL {
89   UINT64                                    Reserved;
90 };
91 
92 struct _PXEBC_VIRTUAL_NIC {
93   UINT32                                    Signature;
94   EFI_HANDLE                                Controller;
95   EFI_LOAD_FILE_PROTOCOL                    LoadFile;
96   EFI_DEVICE_PATH_PROTOCOL                  *DevicePath;
97   PXEBC_PRIVATE_DATA                        *Private;
98 };
99 
100 struct _PXEBC_PRIVATE_DATA {
101   UINT32                                    Signature;
102   EFI_HANDLE                                Controller;
103   EFI_HANDLE                                Image;
104 
105   PXEBC_PRIVATE_PROTOCOL                    Id;
106   EFI_SIMPLE_NETWORK_PROTOCOL               *Snp;
107 
108   PXEBC_VIRTUAL_NIC                         *Ip4Nic;
109   PXEBC_VIRTUAL_NIC                         *Ip6Nic;
110 
111   EFI_HANDLE                                ArpChild;
112   EFI_HANDLE                                Ip4Child;
113   EFI_HANDLE                                Dhcp4Child;
114   EFI_HANDLE                                Mtftp4Child;
115   EFI_HANDLE                                Udp4ReadChild;
116   EFI_HANDLE                                Udp4WriteChild;
117 
118   EFI_ARP_PROTOCOL                          *Arp;
119   EFI_IP4_PROTOCOL                          *Ip4;
120   EFI_IP4_CONFIG2_PROTOCOL                  *Ip4Config2;
121   EFI_DHCP4_PROTOCOL                        *Dhcp4;
122   EFI_MTFTP4_PROTOCOL                       *Mtftp4;
123   EFI_UDP4_PROTOCOL                         *Udp4Read;
124   EFI_UDP4_PROTOCOL                         *Udp4Write;
125 
126   EFI_HANDLE                                Ip6Child;
127   EFI_HANDLE                                Dhcp6Child;
128   EFI_HANDLE                                Mtftp6Child;
129   EFI_HANDLE                                Udp6ReadChild;
130   EFI_HANDLE                                Udp6WriteChild;
131 
132   EFI_IP6_PROTOCOL                          *Ip6;
133   EFI_IP6_CONFIG_PROTOCOL                   *Ip6Cfg;
134   EFI_DHCP6_PROTOCOL                        *Dhcp6;
135   EFI_MTFTP6_PROTOCOL                       *Mtftp6;
136   EFI_UDP6_PROTOCOL                         *Udp6Read;
137   EFI_UDP6_PROTOCOL                         *Udp6Write;
138 
139   EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL *Nii;
140   EFI_PXE_BASE_CODE_PROTOCOL                PxeBc;
141   EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL       LoadFileCallback;
142   EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL       *PxeBcCallback;
143   EFI_DEVICE_PATH_PROTOCOL                  *DevicePath;
144 
145   EFI_PXE_BASE_CODE_MODE                    Mode;
146   EFI_PXE_BASE_CODE_FUNCTION                Function;
147   UINT32                                    Ip6Policy;
148   UINT32                                    SolicitTimes;
149   UINT64                                    ElapsedTime;
150 
151   EFI_UDP4_CONFIG_DATA                      Udp4CfgData;
152   EFI_UDP6_CONFIG_DATA                      Udp6CfgData;
153   EFI_IP4_CONFIG_DATA                       Ip4CfgData;
154   EFI_IP6_CONFIG_DATA                       Ip6CfgData;
155 
156   EFI_EVENT                                 UdpTimeOutEvent;
157   EFI_EVENT                                 ArpUpdateEvent;
158   EFI_IP4_COMPLETION_TOKEN                  IcmpToken;
159   EFI_IP6_COMPLETION_TOKEN                  Icmp6Token;
160 
161   BOOLEAN                                   IsAddressOk;
162   BOOLEAN                                   IsOfferSorted;
163   BOOLEAN                                   IsProxyRecved;
164   BOOLEAN                                   IsDoDiscover;
165 
166   EFI_IP_ADDRESS                            TmpStationIp;
167   EFI_IP_ADDRESS                            StationIp;
168   EFI_IP_ADDRESS                            SubnetMask;
169   EFI_IP_ADDRESS                            GatewayIp;
170   EFI_IP_ADDRESS                            ServerIp;
171   UINT16                                    CurSrcPort;
172   UINT32                                    IaId;
173 
174   UINT32                                    Ip4MaxPacketSize;
175   UINT32                                    Ip6MaxPacketSize;
176   UINT8                                     *BootFileName;
177   UINTN                                     BootFileSize;
178   UINTN                                     BlockSize;
179 
180   PXEBC_DHCP_PACKET_CACHE                   ProxyOffer;
181   PXEBC_DHCP_PACKET_CACHE                   DhcpAck;
182   PXEBC_DHCP_PACKET_CACHE                   PxeReply;
183   EFI_DHCP6_PACKET                          *Dhcp6Request;
184   EFI_DHCP4_PACKET                          SeedPacket;
185 
186   //
187   // OfferIndex records the index of DhcpOffer[] buffer, and OfferCount records the num of each type of offer.
188   //
189   // It supposed that
190   //
191   //   OfferNum:    8
192   //   OfferBuffer: [ProxyBinl, ProxyBinl, DhcpOnly, ProxyPxe10, DhcpOnly, DhcpPxe10, DhcpBinl, ProxyBinl]
193   //   (OfferBuffer is 0-based.)
194   //
195   // And assume that (DhcpPxe10 is the first priority actually.)
196   //
197   //   SelectIndex:     2
198   //   SelectProxyType: PXEBC_OFFER_TYPE_PROXY_BINL
199   //   (SelectIndex is 1-based, and 0 means no one is selected.)
200   //
201   // So it should be
202   //
203   //                 DhcpOnly  DhcpPxe10  DhcpWfm11a  DhcpBinl  ProxyPxe10  ProxyWfm11a  ProxyBinl  Bootp
204   //   OfferCount:  [    2(n),      1(n),       0(n),     1(n),       1(1),        0(1),      3(n),  1(1)]
205   //
206   //   OfferIndex: {[       2,         5,          0,        6,          3,           0,        *0,     0]
207   //                [       4,         0,          0,        0,          0,           0,         1,     0]
208   //                [       0,         0,          0,        0,          0,           0,         7,     0]
209   //                ...                                                                                  ]}
210   //   (OfferIndex is 0-based.)
211   //
212   //
213   UINT32                                    SelectIndex;
214   UINT32                                    SelectProxyType;
215   PXEBC_DHCP_PACKET_CACHE                   OfferBuffer[PXEBC_OFFER_MAX_NUM];
216   UINT32                                    OfferNum;
217   UINT32                                    OfferCount[PxeOfferTypeMax];
218   UINT32                                    OfferIndex[PxeOfferTypeMax][PXEBC_OFFER_MAX_NUM];
219 };
220 
221 extern EFI_PXE_BASE_CODE_PROTOCOL           gPxeBcProtocolTemplate;
222 extern EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL  gPxeBcCallBackTemplate;
223 extern EFI_LOAD_FILE_PROTOCOL               gLoadFileProtocolTemplate;
224 
225 #endif
226