3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 // Client architecture types
23 #define SYS_ARCH_EFI32 6
28 #define UDP_FILTER_MASK (EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP | \
29 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT | \
30 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP | \
31 EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT | \
32 EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER \
35 #define PXE_BOOT_LAYER_MASK 0x7FFF
36 #define PXE_BOOT_LAYER_INITIAL 0x0000
37 #define PXE_BOOT_LAYER_CREDENTIAL_FLAG 0x8000
38 #define MAX_BOOT_SERVERS 32
41 // macro to evaluate IP address as TRUE if it is a multicast IP address
43 #define IS_MULTICAST(ptr) ((*((UINT8 *) ptr) & 0xf0) == 0xe0)
48 #define IP_ADDRESS_LENGTH(qp) (((qp)->UsingIpv6) ? sizeof (EFI_IPv6_ADDRESS) : sizeof (EFI_IPv4_ADDRESS))
50 #define MAX_FRAME_DATA_SIZE 1488
51 #define ALLOCATE_SIZE(X) (((X) + 7) & 0xfff8)
52 #define MODE_ALLOCATE_SIZE ALLOCATE_SIZE (sizeof (EFI_PXE_BASE_CODE_MODE))
53 #define BUFFER_ALLOCATE_SIZE (8192 + 512)
54 #define ROUTER_ALLOCATE_SIZE ALLOCATE_SIZE ((sizeof (EFI_PXE_BASE_CODE_ROUTE_ENTRY) * PXE_ROUTER_TABLE_SIZE))
55 #define ARP_ALLOCATE_SIZE ALLOCATE_SIZE ((sizeof (EFI_PXE_BASE_CODE_ARP_ENTRY) * PXE_ARP_CACHE_SIZE))
56 #define FILTER_ALLOCATE_SIZE ALLOCATE_SIZE ((sizeof (EFI_IP_ADDRESS) * PXE_IP_FILTER_SIZE))
57 #define PXE_ARP_CACHE_SIZE 8
58 #define PXE_ROUTER_TABLE_SIZE 8
59 #define PXE_IP_FILTER_SIZE 8
60 #define ICMP_ALLOCATE_SIZE ALLOCATE_SIZE (sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR))
61 #define TFTP_ERR_ALLOCATE_SIZE ALLOCATE_SIZE (sizeof (EFI_PXE_BASE_CODE_TFTP_ERROR))
64 // DHCP discover/request packets are sent to this UDP port. ProxyDHCP
65 // servers listen on this port for DHCP discover packets that have a
66 // class identifier (option 60) with 'PXEClient' in the first 9 bytes.
67 // Bootservers also listen on this port for PXE broadcast discover
68 // requests from PXE clients.
70 #define DHCP_SERVER_PORT 67
73 // When DHCP, proxyDHCP and Bootservers respond to DHCP and PXE broadcast
74 // discover requests by broadcasting the reply packet, the packet is
75 // broadcast to this port.
77 #define DHCP_CLIENT_PORT 68
80 // TFTP servers listen for TFTP open requests on this port.
82 #define TFTP_OPEN_PORT 69
85 // proxyDHCP and Bootservers listen on this port for a PXE unicast and/or
86 // multicast discover requests from PXE clients. A PXE discover request
87 // looks like a DHCP discover or DHCP request packet.
89 #define PXE_DISCOVERY_PORT 4011
92 // This port is used by the PXE client/server protocol tests.
94 #define PXE_PORT_PXETEST_PORT 0x8080
97 // Definitions for Ethertype protocol numbers and interface types
100 #define PXE_PROTOCOL_ETHERNET_IP 0x0800
101 #define PXE_PROTOCOL_ETHERNET_ARP 0x0806
102 #define PXE_PROTOCOL_ETHERNET_RARP 0x8035
104 #define PXE_IFTYPE_ETHERNET 0x01
105 #define PXE_IFTYPE_TOKENRING 0x04
106 #define PXE_IFTYPE_FIBRE_CHANNEL 0x12
109 // Definitions for internet protocol version 4 header
110 // Per RFC 791, September 1981.
114 #pragma pack(1) // make network structures packed byte alignment
120 #define IPV4_HEADER_LENGTH(IpHeaderPtr) (((IpHeaderPtr)->VersionIhl & 0xf) << 2)
122 #define SET_IPV4_VER_HDL(IpHeaderPtr, IpHeaderLen) { \
123 (IpHeaderPtr)->VersionIhl = (UINT8) ((IPVER4 << 4) | ((IpHeaderLen) >> 2)); \
131 UINT16 FragmentFields
;
134 UINT16 HeaderChecksum
;
138 // options are not implemented
142 #define IP_FRAG_RSVD 0x8000 // reserved bit - must be zero
143 #define IP_NO_FRAG 0x4000 // do not fragment bit
144 #define IP_MORE_FRAG 0x2000 // not last fragment
145 #define IP_FRAG_OFF_MSK 0x1fff // fragment offset in 8 byte chunks
146 #define DEFAULT_RFC_TTL 64
154 * Definitions for internet control message protocol version 4 message
155 * structure. Per RFC 792, September 1981.
159 // icmp header for all icmp messages
162 UINT8 Type
; // message type
163 UINT8 Code
; // type specific - 0 for types we implement
164 UINT16 Checksum
; // ones complement of ones complement sum of 16 bit words of message
167 #define ICMP_DEST_UNREACHABLE 3
168 #define ICMP_SOURCE_QUENCH 4
169 #define ICMP_REDIRECT 5
171 #define ICMP_ECHO_REPLY 0
172 #define ICMP_ROUTER_ADV 9
173 #define ICMP_ROUTER_SOLICIT 10
174 #define ICMP_TIME_EXCEEDED 11
175 #define ICMP_PARAMETER_PROBLEM 12
176 #define ICMP_TIMESTAMP 13
177 #define ICMP_TIMESTAMP_REPLY 14
178 #define ICMP_INFO_REQ 15
179 #define ICMP_INFO_REQ_REPLY 16
180 #define ICMP_SUBNET_MASK_REQ 17
181 #define ICMP_SUBNET_MASK_REPLY 18
183 // other ICMP message types ignored in this implementation
185 // icmp general messages
188 ICMPV4_HEADER Header
;
190 // generally unused except byte [0] for
191 // parameter problem message
193 UINT8 GenerallyUnused
[4];
195 // original message ip header of plus 64
198 IPV4_HEADER IpHeader
;
199 } ICMPV4_GENERAL_MESSAGE
;
202 // icmp req/rply message header
205 ICMPV4_HEADER Header
;
207 UINT16 SequenceNumber
;
208 } ICMPV4_REQUEST_REPLY_HEADER
;
214 ICMPV4_REQUEST_REPLY_HEADER Header
;
215 UINT8 EchoData
[1]; // variable length data to be echoed
216 } ICMPV4_ECHO_MESSAGE
;
219 // icmp timestamp message - times are milliseconds since midnight UT -
220 // if non std, set high order bit
223 ICMPV4_REQUEST_REPLY_HEADER Header
;
224 UINT32 OriginalTime
; // originating timestamp
225 UINT32 ReceiveTime
; // receiving timestamp
226 UINT32 TransmitTime
; // transmitting timestamp
227 } ICMPV4_TIMESTAMP_MESSAGE
;
230 // icmp info request structure - fill in source and dest net ip address on reply
233 ICMPV4_REQUEST_REPLY_HEADER Header
;
234 } ICMPV4_INFO_MESSAGE
;
237 // Definitions for internet control message protocol version 4 message structure
239 // Per RFC 1256, September 1991.
242 // icmp router advertisement message
245 ICMPV4_HEADER Header
;
246 UINT8 NumberEntries
; // number of address entries
247 UINT8 EntrySize
; // number of 32 bit words per address entry
248 UINT16 Lifetime
; // seconds to consider info valid
251 } ICMPV4_ROUTER_ADVERTISE_MESSAGE
;
254 // icmp router solicitation message
257 ICMPV4_HEADER Header
;
259 } ICMPV4_ROUTER_SOLICIT_MESSAGE
;
261 #define MAX_SOLICITATION_DELAY 1 // 1 second
262 #define SOLICITATION_INTERVAL 3 // 3 seconds
263 #define MAX_SOLICITATIONS 3 // 3 transmissions
264 #define V1ROUTER_PRESENT_TIMEOUT 400 // 400 second timeout until v2 reports can be sent
265 #define UNSOLICITED_REPORT_INTERVAL 10 // 10 seconds between unsolicited reports
266 #define BROADCAST_IPv4 0xffffffff
269 // Definitions for address resolution protocol message structure
270 // Per RFC 826, November 1982
273 UINT16 HwType
; // hardware type - e.g. ethernet (1)
274 UINT16 ProtType
; // protocol type - for ethernet, 0x800 for IP
275 UINT8 HwAddLen
; // byte length of a hardware address (e.g. 6 for ethernet)
276 UINT8 ProtAddLen
; // byte length of a protocol address (e.g. 4 for ipv4)
279 // source and dest hw and prot addresses follow - see example below
283 #define ETHERNET_ADD_SPC 1
285 #define ETHER_TYPE_IP 0x800
287 #define ARP_REQUEST 1
291 // generic ARP packet
294 ARP_HEADER ArpHeader
;
295 EFI_MAC_ADDRESS SrcHardwareAddr
;
296 EFI_IP_ADDRESS SrcProtocolAddr
;
297 EFI_MAC_ADDRESS DestHardwareAddr
;
298 EFI_IP_ADDRESS DestProtocolAddr
;
301 #define ENET_HWADDLEN 6
302 #define IPV4_PROTADDLEN 4
305 // Definitions for user datagram protocol version 4 pseudo header & header
306 // Per RFC 768, 28 August 1980
309 IPV4_ADDR SrcAddr
; // source ip address
310 IPV4_ADDR DestAddr
; // dest ip address
312 UINT8 Protocol
; // protocol
313 UINT16 TotalLength
; // UDP length - sizeof udpv4hdr + data length
314 } UDPV4_PSEUDO_HEADER
;
317 UINT16 SrcPort
; // source port identifier
318 UINT16 DestPort
; // destination port identifier
319 UINT16 TotalLength
; // total length header plus data
321 // ones complement of ones complement sum of 16 bit
322 // words of pseudo header, UDP header, and data
323 // zero checksum is transmitted as -0 (ones comp)
324 // zero transmitted means checksum not computed
331 UDPV4_PSEUDO_HEADER Udpv4PseudoHeader
;
332 UDPV4_HEADER Udpv4Header
;
336 // Definitions for transmission control protocol header
337 // Per RFC 793, September, 1981
340 IPV4_ADDR SrcAddr
; // source ip address
341 IPV4_ADDR DestAddr
; // dest ip address
343 UINT8 Protocol
; // protocol
344 UINT16 TotalLength
; // TCP length - TCP header length + data length
345 } TCPV4_PSEUDO_HEADER
;
348 UINT16 SrcPort
; // source port identifier
349 UINT16 DestPort
; // destination port identifier
350 UINT32 SeqNumber
; // Sequence number
351 UINT32 AckNumber
; // Acknowledgement Number
353 // Nibble of HLEN (length of header in 32-bit multiples)
355 // Nibble of Code Bits
358 UINT16 Window
; // Software buffer size (sliding window size) in network-standard byte order
360 // ones complement of ones complement sum of 16 bit words of
361 // pseudo header, TCP header, and data
362 // zero checksum is transmitted as -0 (ones comp)
363 // zero transmitted means checksum not computed
366 UINT16 UrgentPointer
; // pointer to urgent data (allows sender to specify urgent data)
370 TCPV4_PSEUDO_HEADER Tcpv4PseudoHeader
;
371 TCPV4_HEADER Tcpv4Header
;
375 UINT8 Kind
; // one of the following:
376 UINT8 Length
; // total option length including Kind and Lth
377 UINT8 Data
[1]; // length = Lth - 2
380 #define TCP_OP_END 0 // only used to pad to end of TCP header
381 #define TCP_NOP 1 // optional - may be used to pad between options to get alignment
382 #define TCP_MAX_SEG 2 // maximum receive segment size - only send at initial connection request
383 #define MAX_MEDIA_HDR_SIZE 64
384 #define MIN_ENET_DATA_SIZE 64
385 #define MAX_ENET_DATA_SIZE 1500 // temp def - make a network based var
386 #define MAX_IPV4_PKT_SIZE 65535 // maximum IP packet size
387 #define MAX_IPV4_DATA_SIZE (MAX_IPV4_PKT_SIZE - sizeof (IPV4_HEADER))
388 #define MAX_IPV4_FRAME_DATA_SIZE (MAX_FRAME_DATA_SIZE - sizeof (IPV4_HEADER))
389 #define REAS_IPV4_PKT_SIZE 576 // minimum IP packet size all IP host can handle
390 #define REAS_IPV4_DATA_SIZE (REAS_IPV4_PKT_SIZE - sizeof (IPV4_HEADER))
396 UINT8 Data
[MAX_ENET_DATA_SIZE
];
397 ICMPV4_HEADER IcmpHeader
;
398 IGMPV2_MESSAGE IgmpMessage
;
400 UDPV4_HEADER UdpHeader
;
404 TCPV4_HEADER TcpHeader
;
410 // out buffer structure
413 UINT8 MediaHeader
[MAX_MEDIA_HDR_SIZE
];
414 IPV4_HEADER IpHeader
;
416 // following union placement only valid if no option IP header
422 IPV4_HEADER IpHeader
;
424 // following union placement only valid if no option IP header
429 #pragma pack() // reset to default
431 ////////////////////////////////////////////////////////////
433 // BC IP Filter Routine
437 PXE_BASECODE_DEVICE
*Private
,
438 IN EFI_PXE_BASE_CODE_IP_FILTER
*Filter
443 // //////////////////////////////////////////////////////////////////////
445 // Udp Write Routine - called by base code - e.g. TFTP - already locked
449 IN PXE_BASECODE_DEVICE
*Private
,
451 IN EFI_IP_ADDRESS
*DestIpPtr
,
452 IN EFI_PXE_BASE_CODE_UDP_PORT
*DestPortptr
,
453 IN EFI_IP_ADDRESS
*GatewayIpPtr
, OPTIONAL
454 IN EFI_IP_ADDRESS
*SrcIpPtr
, OPTIONAL
455 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*SrcPortPtr
, OPTIONAL
456 IN UINTN
*HeaderSizePtr
, OPTIONAL
457 IN VOID
*HeaderPtr
, OPTIONAL
458 IN UINTN
*BufferSizePtr
,
464 // /////////////////////////////////////////////////////////////////////
466 // Udp Read Routine - called by base code - e.g. TFTP - already locked
470 IN PXE_BASECODE_DEVICE
*Private
,
472 IN OUT EFI_IP_ADDRESS
*DestIpPtr
, OPTIONAL
473 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*DestPorPtrt
, OPTIONAL
474 IN OUT EFI_IP_ADDRESS
*SrcIpPtr
, OPTIONAL
475 IN OUT EFI_PXE_BASE_CODE_UDP_PORT
*SrcPortPtr
, OPTIONAL
476 IN UINTN
*HeaderSizePtr
, OPTIONAL
477 IN VOID
*HeaderPtr
, OPTIONAL
478 IN OUT UINTN
*BufferSizePtr
,
480 IN EFI_EVENT TimeoutEvent
486 PXE_BASECODE_DEVICE
*Private
,
493 PXE_BASECODE_DEVICE
*Private
,
499 // convert number to zero filled ascii value of length lth
510 // convert number to ascii string at ptr
520 // convert ascii numeric string to UINTN
535 // calculate the internet checksum (RFC 1071)
536 // return 16 bit ones complement of ones complement sum of 16 bit words
546 // do checksum on non contiguous header and data
558 // update checksum when only a single word changes
570 IN PXE_BASECODE_DEVICE
*Private
,
571 IN UINT16 InitialSeed
577 IN PXE_BASECODE_DEVICE
*Private
583 PXE_BASECODE_DEVICE
*Private
,
587 VOID
*HardwareAddress
,
588 UINT16 MediaProtocol
,
589 IN EFI_PXE_BASE_CODE_FUNCTION Function
595 PXE_BASECODE_DEVICE
*Private
,
596 ARP_PACKET
*ArpPacketPtr
,
603 PXE_BASECODE_DEVICE
*Private
,
604 IGMPV2_MESSAGE
*IgmpMessageptr
,
611 PXE_BASECODE_DEVICE
*Private
613 ; // poll when doing a receive
614 // return hw add of IP and TRUE if available, otherwise FALSE
618 IN PXE_BASECODE_DEVICE
*Private
,
619 EFI_IP_ADDRESS
*ProtocolAddressPtr
,
620 EFI_MAC_ADDRESS
*HardwareAddressPtr
626 IN PXE_BASECODE_DEVICE
*Private
,
627 IN EFI_IP_ADDRESS
*ProtocolAddressPtr
,
628 OUT EFI_MAC_ADDRESS
*HardwareAddressptr
637 EFI_IP_ADDRESS
*SubnetMask
643 PXE_BASECODE_DEVICE
*Private
,
644 EFI_IP_ADDRESS
*RouterIp
648 #define Ip4AddRouter(Private, Ipv4Ptr) IpAddRouter (Private, (EFI_IP_ADDRESS *) Ipv4Ptr)
651 // routine to send ipv4 packet
652 // ipv4 + upper protocol header for length TotHdrLth in xmtbuf, ipv4 header length IpHdrLth
653 // routine fills in ipv4hdr Ver_Hdl, TotLth, and Checksum, moves in Data, and gets dest MAC address
657 PXE_BASECODE_DEVICE
*Private
,
660 UINTN TotalHeaderLen
,
663 EFI_PXE_BASE_CODE_FUNCTION Function
668 // send ipv4 packet with ipv4 option
672 PXE_BASECODE_DEVICE
*Private
,
680 EFI_PXE_BASE_CODE_FUNCTION Function
685 // send MsgLth message at MsgPtr - higher level protocol header already in xmtbuf, length HdrSize
689 IN PXE_BASECODE_DEVICE
*Private
, // pointer to instance data
690 IN UINTN MayFragment
, //
691 IN UINT8 Protocol
, // protocol
692 IN UINT32 SrcIp
, // Source IP address
693 IN UINT32 DestIp
, // Destination IP address
694 IN UINT32 GatewayIp
, // used if not NULL and needed
695 IN UINTN HeaderSize
, // protocol header byte length
696 IN UINT8
*MsgPtr
, // pointer to data
699 ; // data byte length
700 // receive up to MsgLth message into MsgPtr for protocol Prot
701 // return message length, src/dest ips if select any, and pointer to protocol header
705 IN PXE_BASECODE_DEVICE
*Private
, // pointer to instance data
706 UINT16 OpFlags
, // Flags to determine if filtering on IP addresses
707 EFI_IP_ADDRESS
*SrcIpPtr
, // if filtering, O if accept any
708 EFI_IP_ADDRESS
*DstIpPtr
, // if filtering, O if accept any
709 UINT8 Protocol
, // protocol
710 VOID
*HeaderPtr
, // address of where to put protocol header
711 UINTN HeaderSize
, // protocol header byte length
712 UINT8
*MsgPtr
, // pointer to data buffer
713 UINTN
*MsgLenPtr
, // pointer to data buffer length/ O - returned data length
714 IN EFI_EVENT TimeoutEvent
721 IN PXE_BASECODE_DEVICE
*Private
726 // routine to cycle waiting for a receive or timeout
730 IN PXE_BASECODE_DEVICE
*Private
,
731 IN EFI_PXE_BASE_CODE_FUNCTION Function
,
732 IN EFI_EVENT TimeoutEvent
,
733 IN OUT UINTN
*HeaderSizePtr
,
734 IN OUT UINTN
*BufferSizePtr
,
735 IN OUT UINT16
*ProtocolPtr