1 /* -----------------------------------------------------------------------
3 * Copyright 1999-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009-2010 Intel Corporation; author: H. Peter Anvin
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 * Boston MA 02111-1307, USA; either version 2 of the License, or
10 * (at your option) any later version; incorporated herein by reference.
12 * ----------------------------------------------------------------------- */
23 #include <syslinux/pxe_api.h>
24 #include "fs.h" /* For MAX_OPEN, should go away */
27 * Some basic defines...
29 #define TFTP_PORT htons(69) /* Default TFTP port */
30 #define TFTP_BLOCKSIZE_LG2 9
31 #define TFTP_BLOCKSIZE (1 << TFTP_BLOCKSIZE_LG2)
32 #define PKTBUF_SIZE 2048 /* */
34 #define is_digit(c) (((c) >= '0') && ((c) <= '9'))
36 static inline bool is_hex(char c
)
38 return (c
>= '0' && c
<= '9') ||
39 (c
>= 'A' && c
<= 'F') ||
40 (c
>= 'a' && c
<= 'f');
43 static inline int hexval(char c
)
45 return (c
>= 'A') ? (c
& ~0x20) - 'A' + 10 : (c
- '0');
49 * TFTP operation codes
51 #define TFTP_RRQ htons(1) // Read rest
52 #define TFTP_WRQ htons(2) // Write rest
53 #define TFTP_DATA htons(3) // Data packet
54 #define TFTP_ACK htons(4) // ACK packet
55 #define TFTP_ERROR htons(5) // ERROR packet
56 #define TFTP_OACK htons(6) // OACK packet
61 #define TFTP_EUNDEF htons(0) // Unspecified error
62 #define TFTP_ENOTFOUND htons(1) // File not found
63 #define TFTP_EACCESS htons(2) // Access violation
64 #define TFTP_ENOSPACE htons(3) // Disk full
65 #define TFTP_EBADOP htons(4) // Invalid TFTP operation
66 #define TFTP_EBADID htons(5) // Unknown transfer
67 #define TFTP_EEXISTS htons(6) // File exists
68 #define TFTP_ENOUSER htons(7) // No such user
69 #define TFTP_EOPTNEG htons(8) // Option negotiation failure
72 #define BOOTP_OPTION_MAGIC htonl(0x63825363)
76 #define DNS_PORT htons(53) /* Default DNS port */
77 #define DNS_MAX_PACKET 512 /* Defined by protocol */
78 #define DNS_MAX_SERVERS 4 /* Max no of DNS servers */
86 uint8_t signature
[6]; /* PXENV+ */
100 uint16_t undidatasize
;
101 uint16_t undicodeseg
;
102 uint16_t undicodesize
;
107 uint8_t signature
[4]; /* !PXE */
108 uint8_t structlength
;
112 segoff16_t undiromid
;
113 segoff16_t baseromid
;
114 segoff16_t entrypointsp
;
115 segoff16_t entrypointesp
;
116 segoff16_t statuscallout
;
119 uint16_t firstselector
;
120 pxe_segdesc_t seg
[7];
125 PXE_Seg_UNDIData
= 1,
126 PXE_Seg_UNDICode
= 2,
127 PXE_Seg_UNDICodeWrite
= 3,
130 PXE_Seg_BC_CodeWrite
= 6
134 uint8_t opcode
; /* BOOTP/DHCP "opcode" */
135 uint8_t hardware
; /* ARP hreadware type */
136 uint8_t hardlen
; /* Hardware address length */
137 uint8_t gatehops
; /* Used by forwarders */
138 uint32_t ident
; /* Transaction ID */
139 uint16_t seconds
; /* Seconds elapsed */
140 uint16_t flags
; /* Broadcast flags */
141 uint32_t cip
; /* Cient IP */
142 uint32_t yip
; /* "Your" IP */
143 uint32_t sip
; /* Next Server IP */
144 uint32_t gip
; /* Relay agent IP */
145 uint8_t macaddr
[16]; /* Client MAC address */
146 uint8_t sname
[64]; /* Server name (optional) */
147 char bootfile
[128]; /* Boot file name */
148 uint32_t option_magic
; /* Vendor option magic cookie */
149 uint8_t options
[1260]; /* Vendor options */
150 } __attribute__ ((packed
));
153 * Our inode private information -- this includes the packet buffer!
155 struct pxe_pvt_inode
{
156 uint16_t tftp_localport
; /* Local port number (0=not in us)*/
157 uint16_t tftp_remoteport
; /* Remote port number */
158 uint32_t tftp_remoteip
; /* Remote IP address */
159 uint32_t tftp_filepos
; /* bytes downloaded (includeing buffer) */
160 uint32_t tftp_blksize
; /* Block size for this connection(*) */
161 uint16_t tftp_bytesleft
; /* Unclaimed data bytes */
162 uint16_t tftp_lastpkt
; /* Sequence number of last packet (NBO) */
163 char *tftp_dataptr
; /* Pointer to available data */
164 uint8_t tftp_goteof
; /* 1 if the EOF packet received */
165 uint8_t tftp_unused
[3]; /* Currently unused */
166 char tftp_pktbuf
[PKTBUF_SIZE
];
167 } __attribute__ ((packed
));
169 #define PVT(i) ((struct pxe_pvt_inode *)((i)->pvt))
172 * Network boot information
185 extern struct ip_info IPInfo
;
187 extern uint8_t MAC
[];
188 extern char BOOTIFStr
[];
189 extern uint8_t MAC_len
;
190 extern uint8_t MAC_type
;
192 extern uint8_t DHCPMagic
;
193 extern uint32_t RebootTime
;
195 extern char boot_file
[];
196 extern char path_prefix
[];
197 extern char LocalDomain
[];
199 extern char IPOption
[];
200 extern char dot_quad_buf
[];
202 extern uint32_t dns_server
[];
204 extern uint16_t APIVer
;
205 extern far_ptr_t PXEEntry
;
206 extern uint8_t KeepPXE
;
208 extern far_ptr_t InitStack
;
210 extern bool have_uuid
;
211 extern uint8_t uuid_type
;
212 extern uint8_t uuid
[];
214 extern uint16_t BIOS_fbm
;
215 extern const uint8_t TimeoutTable
[];
218 * Compute the suitable gateway for a specific route -- too many
219 * vendor PXE stacks don't do this correctly...
221 static inline uint32_t gateway(uint32_t ip
)
223 if ((ip
^ IPInfo
.myip
) & IPInfo
.netmask
)
224 return IPInfo
.gateway
;
234 bool ip_ok(uint32_t);
235 int pxe_call(int, void *);
238 void parse_dhcp(int);
241 int dns_mangle(char **, const char *);
242 uint32_t dns_resolv(const char *);
245 void pxe_idle_init(void);
246 void pxe_idle_cleanup(void);
249 uint16_t get_port(void);
250 void free_port(uint16_t port
);