Commit | Line | Data |
---|---|---|
db4d0741 MR |
1 | |
2 | /************************************************************************** | |
3 | NETBOOT - BOOTP/TFTP Bootstrap Program | |
4 | ||
5 | Author: Martin Renters | |
6 | Date: Dec/93 | |
7 | ||
8 | **************************************************************************/ | |
9 | ||
10 | #include <a.out.h> | |
11 | #include <netdb.h> | |
12 | #include <sys/param.h> | |
13 | #include <sys/socket.h> | |
14 | #include <sys/mount.h> | |
15 | #include <net/if.h> | |
16 | #include <nfs/nfsv2.h> | |
17 | #include <nfs/nfsdiskless.h> | |
18 | ||
19 | #ifndef DEFAULT_BOOTFILE | |
20 | #define DEFAULT_BOOTFILE "386bsd" | |
21 | #endif | |
22 | ||
23 | #ifndef MAX_TFTP_RETRIES | |
24 | #define MAX_TFTP_RETRIES 20 | |
25 | #endif | |
26 | ||
27 | #ifndef MAX_BOOTP_RETRIES | |
28 | #define MAX_BOOTP_RETRIES 20 | |
29 | #endif | |
30 | ||
31 | #ifndef MAX_ARP_RETRIES | |
32 | #define MAX_ARP_RETRIES 20 | |
33 | #endif | |
34 | ||
35 | #ifndef TIMEOUT /* Inter-packet retry in ticks 18/sec */ | |
36 | #define TIMEOUT 20 | |
37 | #endif | |
38 | ||
39 | #ifndef NULL | |
40 | #define NULL ((void *)0) | |
41 | #endif | |
42 | ||
43 | #define ETHER_ADDR_SIZE 6 /* Size of Ethernet address */ | |
44 | #define ETHER_HDR_SIZE 14 /* Size of ethernet header */ | |
45 | ||
46 | #define ARP_CLIENT 0 | |
47 | #define ARP_SERVER 1 | |
48 | #define ARP_GATEWAY 2 | |
49 | #define ARP_NS 3 | |
50 | #define MAX_ARP ARP_NS+1 | |
51 | ||
52 | #define IP 0x0800 | |
53 | #define ARP 0x0806 | |
54 | ||
55 | #define BOOTP_SERVER 67 | |
56 | #define BOOTP_CLIENT 68 | |
57 | #define TFTP 69 | |
58 | ||
59 | #define IP_UDP 17 | |
60 | #define IP_BROADCAST 0xFFFFFFFF | |
61 | ||
62 | #define ARP_REQUEST 1 | |
63 | #define ARP_REPLY 2 | |
64 | ||
65 | #define BOOTP_REQUEST 1 | |
66 | #define BOOTP_REPLY 2 | |
67 | ||
68 | #define TFTP_RRQ 1 | |
69 | #define TFTP_WRQ 2 | |
70 | #define TFTP_DATA 3 | |
71 | #define TFTP_ACK 4 | |
72 | #define TFTP_ERROR 5 | |
73 | ||
74 | #define TFTP_CODE_EOF 1 | |
75 | #define TFTP_CODE_MORE 2 | |
76 | #define TFTP_CODE_ERROR 3 | |
77 | #define TFTP_CODE_BOOT 4 | |
78 | #define TFTP_CODE_CFG 5 | |
79 | ||
80 | struct arptable_t { | |
81 | unsigned long ipaddr; | |
82 | unsigned char node[6]; | |
83 | } arptable[MAX_ARP]; | |
84 | ||
85 | struct arprequest { | |
86 | unsigned short hwtype; | |
87 | unsigned short protocol; | |
88 | char hwlen; | |
89 | char protolen; | |
90 | unsigned short opcode; | |
91 | char shwaddr[6]; | |
92 | char sipaddr[4]; | |
93 | char thwaddr[6]; | |
94 | char tipaddr[4]; | |
95 | }; | |
96 | ||
97 | struct iphdr { | |
98 | char verhdrlen; | |
99 | char service; | |
100 | unsigned short len; | |
101 | unsigned short ident; | |
102 | unsigned short frags; | |
103 | char ttl; | |
104 | char protocol; | |
105 | unsigned short chksum; | |
106 | char src[4]; | |
107 | char dest[4]; | |
108 | }; | |
109 | ||
110 | struct udphdr { | |
111 | unsigned short src; | |
112 | unsigned short dest; | |
113 | unsigned short len; | |
114 | unsigned short chksum; | |
115 | }; | |
116 | ||
117 | struct bootp_t { | |
118 | struct iphdr ip; | |
119 | struct udphdr udp; | |
120 | char bp_op; | |
121 | char bp_htype; | |
122 | char bp_hlen; | |
123 | char bp_hops; | |
124 | unsigned long bp_xid; | |
125 | unsigned short bp_secs; | |
126 | unsigned short unused; | |
127 | char bp_ciaddr[4]; | |
128 | char bp_yiaddr[4]; | |
129 | char bp_siaddr[4]; | |
130 | char bp_giaddr[4]; | |
131 | char bp_hwaddr[16]; | |
132 | char bp_sname[64]; | |
133 | char bp_file[128]; | |
134 | char bp_vend[64]; | |
135 | }; | |
136 | ||
137 | struct tftp_t { | |
138 | struct iphdr ip; | |
139 | struct udphdr udp; | |
140 | unsigned short opcode; | |
141 | union { | |
142 | char rrq[512]; | |
143 | struct { | |
144 | unsigned short block; | |
145 | char download[512]; | |
146 | } data; | |
147 | struct { | |
148 | unsigned short block; | |
149 | } ack; | |
150 | struct { | |
151 | unsigned short errcode; | |
152 | char errmsg[512]; | |
153 | } err; | |
154 | } u; | |
155 | }; | |
156 | #define TFTP_MIN_PACKET_SIZE (sizeof(struct iphdr) + sizeof(struct udphdr) + 4) | |
157 | ||
158 | #ifdef oldstuff | |
159 | static inline unsigned short htons(unsigned short in) | |
160 | { | |
161 | return((in >> 8) | (in << 8)); | |
162 | } | |
163 | #define ntohs htons | |
164 | ||
165 | static inline unsigned long htonl(unsigned long in) | |
166 | { | |
167 | return((in >> 24) | ((in >> 16) & 0x0000FF00) | | |
168 | ((in << 16) & 0x00FF0000) | (in << 24)); | |
169 | } | |
170 | #define ntohl htonl | |
171 | #endif | |
172 | ||
173 | static inline unsigned char inb(a) | |
174 | unsigned short a; | |
175 | { | |
176 | unsigned char d; | |
177 | asm volatile( "inb %1, %0" : "=a" (d) : "d" (a)); | |
178 | return d; | |
179 | } | |
180 | ||
181 | static inline void outb(a,d) | |
182 | unsigned short a; | |
183 | unsigned char d; | |
184 | { | |
185 | asm volatile( "outb %0, %1" : : "a" (d), "d" (a)); | |
186 | } |