etherboot.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2000,2001,2002 Free Software Foundation, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /* RULE: You must define the macro ``GRUB'' when including this header
  20. file in GRUB code. */
  21. /* Based on "src/etherboot.h" in etherboot-5.0.5. */
  22. /**************************************************************************
  23. ETHERBOOT - BOOTP/TFTP Bootstrap Program
  24. Author: Martin Renters
  25. Date: Dec/93
  26. **************************************************************************/
  27. /* Include GRUB-specific macros and prototypes here. */
  28. #include <shared.h>
  29. /* FIXME: For now, enable the DHCP support. Perhaps I should segregate
  30. the DHCP support from the BOOTP support, and permit both to
  31. co-exist. */
  32. #undef NO_DHCP_SUPPORT
  33. /* In GRUB, the relocated address in Etherboot doesn't have any sense.
  34. Just define it as a bogus value. */
  35. #define RELOC 0
  36. /* FIXME: Should be an option. */
  37. #define BACKOFF_LIMIT 7
  38. #include <osdep.h>
  39. #define CTRL_C 3
  40. #ifndef MAX_TFTP_RETRIES
  41. # define MAX_TFTP_RETRIES 20
  42. #endif
  43. #ifndef MAX_BOOTP_RETRIES
  44. # define MAX_BOOTP_RETRIES 20
  45. #endif
  46. #define MAX_BOOTP_EXTLEN (ETH_FRAME_LEN - ETH_HLEN - \
  47. sizeof (struct bootp_t))
  48. #ifndef MAX_ARP_RETRIES
  49. # define MAX_ARP_RETRIES 20
  50. #endif
  51. #ifndef MAX_RPC_RETRIES
  52. # define MAX_RPC_RETRIES 20
  53. #endif
  54. #define TICKS_PER_SEC 18
  55. /* Inter-packet retry in ticks */
  56. #define TIMEOUT (10 * TICKS_PER_SEC)
  57. /* These settings have sense only if compiled with -DCONGESTED */
  58. /* total retransmission timeout in ticks */
  59. #define TFTP_TIMEOUT (30 * TICKS_PER_SEC)
  60. /* packet retransmission timeout in ticks */
  61. #define TFTP_REXMT (3 * TICKS_PER_SEC)
  62. #ifndef NULL
  63. # define NULL ((void *) 0)
  64. #endif
  65. /*
  66. I'm moving towards the defined names in linux/if_ether.h for clarity.
  67. The confusion between 60/64 and 1514/1518 arose because the NS8390
  68. counts the 4 byte frame checksum in the incoming packet, but not
  69. in the outgoing packet. 60/1514 are the correct numbers for most
  70. if not all of the other NIC controllers. I will be retiring the
  71. 64/1518 defines in the lead-up to 5.0.
  72. */
  73. #define ETH_ALEN 6 /* Size of Ethernet address */
  74. #define ETH_HLEN 14 /* Size of ethernet header */
  75. #define ETH_ZLEN 60 /* Minimum packet */
  76. /*#define ETH_MIN_PACKET 64*/
  77. #define ETH_FRAME_LEN 1514 /* Maximum packet */
  78. /*#define ETH_MAX_PACKET 1518*/
  79. /* Because some DHCP/BOOTP servers don't treat the maximum length the same
  80. as Etherboot, subtract the size of an IP header and that of an UDP
  81. header. */
  82. #define ETH_MAX_MTU (ETH_FRAME_LEN - ETH_HLEN \
  83. - sizeof (struct iphdr) \
  84. - sizeof (struct udphdr))
  85. #define ARP_CLIENT 0
  86. #define ARP_SERVER 1
  87. #define ARP_GATEWAY 2
  88. #define ARP_ROOTSERVER 3
  89. #define ARP_SWAPSERVER 4
  90. #define MAX_ARP ARP_SWAPSERVER+1
  91. #define RARP_REQUEST 3
  92. #define RARP_REPLY 4
  93. #define IP 0x0800
  94. #define ARP 0x0806
  95. #define RARP 0x8035
  96. #define BOOTP_SERVER 67
  97. #define BOOTP_CLIENT 68
  98. #define TFTP_PORT 69
  99. #define SUNRPC_PORT 111
  100. #define IP_UDP 17
  101. /* Same after going through htonl */
  102. #define IP_BROADCAST 0xFFFFFFFF
  103. #define ARP_REQUEST 1
  104. #define ARP_REPLY 2
  105. #define BOOTP_REQUEST 1
  106. #define BOOTP_REPLY 2
  107. #define TAG_LEN(p) (*((p) + 1))
  108. #define RFC1533_COOKIE 99, 130, 83, 99
  109. #define RFC1533_PAD 0
  110. #define RFC1533_NETMASK 1
  111. #define RFC1533_TIMEOFFSET 2
  112. #define RFC1533_GATEWAY 3
  113. #define RFC1533_TIMESERVER 4
  114. #define RFC1533_IEN116NS 5
  115. #define RFC1533_DNS 6
  116. #define RFC1533_LOGSERVER 7
  117. #define RFC1533_COOKIESERVER 8
  118. #define RFC1533_LPRSERVER 9
  119. #define RFC1533_IMPRESSSERVER 10
  120. #define RFC1533_RESOURCESERVER 11
  121. #define RFC1533_HOSTNAME 12
  122. #define RFC1533_BOOTFILESIZE 13
  123. #define RFC1533_MERITDUMPFILE 14
  124. #define RFC1533_DOMAINNAME 15
  125. #define RFC1533_SWAPSERVER 16
  126. #define RFC1533_ROOTPATH 17
  127. #define RFC1533_EXTENSIONPATH 18
  128. #define RFC1533_IPFORWARDING 19
  129. #define RFC1533_IPSOURCEROUTING 20
  130. #define RFC1533_IPPOLICYFILTER 21
  131. #define RFC1533_IPMAXREASSEMBLY 22
  132. #define RFC1533_IPTTL 23
  133. #define RFC1533_IPMTU 24
  134. #define RFC1533_IPMTUPLATEAU 25
  135. #define RFC1533_INTMTU 26
  136. #define RFC1533_INTLOCALSUBNETS 27
  137. #define RFC1533_INTBROADCAST 28
  138. #define RFC1533_INTICMPDISCOVER 29
  139. #define RFC1533_INTICMPRESPOND 30
  140. #define RFC1533_INTROUTEDISCOVER 31
  141. #define RFC1533_INTROUTESOLICIT 32
  142. #define RFC1533_INTSTATICROUTES 33
  143. #define RFC1533_LLTRAILERENCAP 34
  144. #define RFC1533_LLARPCACHETMO 35
  145. #define RFC1533_LLETHERNETENCAP 36
  146. #define RFC1533_TCPTTL 37
  147. #define RFC1533_TCPKEEPALIVETMO 38
  148. #define RFC1533_TCPKEEPALIVEGB 39
  149. #define RFC1533_NISDOMAIN 40
  150. #define RFC1533_NISSERVER 41
  151. #define RFC1533_NTPSERVER 42
  152. #define RFC1533_VENDOR 43
  153. #define RFC1533_NBNS 44
  154. #define RFC1533_NBDD 45
  155. #define RFC1533_NBNT 46
  156. #define RFC1533_NBSCOPE 47
  157. #define RFC1533_XFS 48
  158. #define RFC1533_XDM 49
  159. #ifndef NO_DHCP_SUPPORT
  160. #define RFC2132_REQ_ADDR 50
  161. #define RFC2132_MSG_TYPE 53
  162. #define RFC2132_SRV_ID 54
  163. #define RFC2132_PARAM_LIST 55
  164. #define RFC2132_MAX_SIZE 57
  165. #define RFC2132_VENDOR_CLASS_ID 60
  166. #define DHCPDISCOVER 1
  167. #define DHCPOFFER 2
  168. #define DHCPREQUEST 3
  169. #define DHCPACK 5
  170. #endif /* NO_DHCP_SUPPORT */
  171. #define RFC1533_VENDOR_MAJOR 0
  172. #define RFC1533_VENDOR_MINOR 0
  173. #define RFC1533_VENDOR_MAGIC 128
  174. #define RFC1533_VENDOR_ADDPARM 129
  175. #define RFC1533_VENDOR_MNUOPTS 160
  176. #define RFC1533_VENDOR_SELECTION 176
  177. #define RFC1533_VENDOR_MOTD 184
  178. #define RFC1533_VENDOR_NUMOFMOTD 8
  179. #define RFC1533_VENDOR_IMG 192
  180. #define RFC1533_VENDOR_NUMOFIMG 16
  181. #define RFC1533_VENDOR_CONFIGFILE 150
  182. #define RFC1533_END 255
  183. #define BOOTP_VENDOR_LEN 64
  184. #ifndef NO_DHCP_SUPPORT
  185. #define DHCP_OPT_LEN 312
  186. #endif /* NO_DHCP_SUPPORT */
  187. #define TFTP_DEFAULTSIZE_PACKET 512
  188. #define TFTP_MAX_PACKET 1432 /* 512 */
  189. #define TFTP_RRQ 1
  190. #define TFTP_WRQ 2
  191. #define TFTP_DATA 3
  192. #define TFTP_ACK 4
  193. #define TFTP_ERROR 5
  194. #define TFTP_OACK 6
  195. #define TFTP_CODE_EOF 1
  196. #define TFTP_CODE_MORE 2
  197. #define TFTP_CODE_ERROR 3
  198. #define TFTP_CODE_BOOT 4
  199. #define TFTP_CODE_CFG 5
  200. #define AWAIT_ARP 0
  201. #define AWAIT_BOOTP 1
  202. #define AWAIT_TFTP 2
  203. #define AWAIT_RARP 3
  204. #define AWAIT_RPC 4
  205. #define AWAIT_QDRAIN 5 /* drain queue, process ARP requests */
  206. typedef struct
  207. {
  208. unsigned long s_addr;
  209. }
  210. in_addr;
  211. struct arptable_t
  212. {
  213. in_addr ipaddr;
  214. unsigned char node[6];
  215. };
  216. /*
  217. * A pity sipaddr and tipaddr are not longword aligned or we could use
  218. * in_addr. No, I don't want to use #pragma packed.
  219. */
  220. struct arprequest
  221. {
  222. unsigned short hwtype;
  223. unsigned short protocol;
  224. char hwlen;
  225. char protolen;
  226. unsigned short opcode;
  227. char shwaddr[6];
  228. char sipaddr[4];
  229. char thwaddr[6];
  230. char tipaddr[4];
  231. };
  232. struct iphdr
  233. {
  234. char verhdrlen;
  235. char service;
  236. unsigned short len;
  237. unsigned short ident;
  238. unsigned short frags;
  239. char ttl;
  240. char protocol;
  241. unsigned short chksum;
  242. in_addr src;
  243. in_addr dest;
  244. };
  245. struct udphdr
  246. {
  247. unsigned short src;
  248. unsigned short dest;
  249. unsigned short len;
  250. unsigned short chksum;
  251. };
  252. /* Format of a bootp packet. */
  253. struct bootp_t
  254. {
  255. char bp_op;
  256. char bp_htype;
  257. char bp_hlen;
  258. char bp_hops;
  259. unsigned long bp_xid;
  260. unsigned short bp_secs;
  261. unsigned short unused;
  262. in_addr bp_ciaddr;
  263. in_addr bp_yiaddr;
  264. in_addr bp_siaddr;
  265. in_addr bp_giaddr;
  266. char bp_hwaddr[16];
  267. char bp_sname[64];
  268. char bp_file[128];
  269. #ifdef NO_DHCP_SUPPORT
  270. char bp_vend[BOOTP_VENDOR_LEN];
  271. #else
  272. char bp_vend[DHCP_OPT_LEN];
  273. #endif /* NO_DHCP_SUPPORT */
  274. };
  275. /* Format of a bootp IP packet. */
  276. struct bootpip_t
  277. {
  278. struct iphdr ip;
  279. struct udphdr udp;
  280. struct bootp_t bp;
  281. };
  282. /* Format of bootp packet with extensions. */
  283. struct bootpd_t
  284. {
  285. struct bootp_t bootp_reply;
  286. unsigned char bootp_extension[MAX_BOOTP_EXTLEN];
  287. };
  288. struct tftp_t
  289. {
  290. struct iphdr ip;
  291. struct udphdr udp;
  292. unsigned short opcode;
  293. union
  294. {
  295. char rrq[TFTP_DEFAULTSIZE_PACKET];
  296. struct
  297. {
  298. unsigned short block;
  299. char download[TFTP_MAX_PACKET];
  300. }
  301. data;
  302. struct
  303. {
  304. unsigned short block;
  305. }
  306. ack;
  307. struct
  308. {
  309. unsigned short errcode;
  310. char errmsg[TFTP_DEFAULTSIZE_PACKET];
  311. }
  312. err;
  313. struct
  314. {
  315. char data[TFTP_DEFAULTSIZE_PACKET+2];
  316. }
  317. oack;
  318. }
  319. u;
  320. };
  321. /* Define a smaller tftp packet solely for making requests to conserve stack
  322. 512 bytes should be enough. */
  323. struct tftpreq_t
  324. {
  325. struct iphdr ip;
  326. struct udphdr udp;
  327. unsigned short opcode;
  328. union
  329. {
  330. char rrq[512];
  331. struct
  332. {
  333. unsigned short block;
  334. }
  335. ack;
  336. struct
  337. {
  338. unsigned short errcode;
  339. char errmsg[512-2];
  340. }
  341. err;
  342. }
  343. u;
  344. };
  345. #define TFTP_MIN_PACKET (sizeof(struct iphdr) + sizeof(struct udphdr) + 4)
  346. struct rpc_t
  347. {
  348. struct iphdr ip;
  349. struct udphdr udp;
  350. union
  351. {
  352. char data[300]; /* longest RPC call must fit!!!! */
  353. struct
  354. {
  355. long id;
  356. long type;
  357. long rpcvers;
  358. long prog;
  359. long vers;
  360. long proc;
  361. long data[1];
  362. }
  363. call;
  364. struct
  365. {
  366. long id;
  367. long type;
  368. long rstatus;
  369. long verifier;
  370. long v2;
  371. long astatus;
  372. long data[1];
  373. }
  374. reply;
  375. }
  376. u;
  377. };
  378. #define PROG_PORTMAP 100000
  379. #define PROG_NFS 100003
  380. #define PROG_MOUNT 100005
  381. #define MSG_CALL 0
  382. #define MSG_REPLY 1
  383. #define PORTMAP_GETPORT 3
  384. #define MOUNT_ADDENTRY 1
  385. #define MOUNT_UMOUNTALL 4
  386. #define NFS_LOOKUP 4
  387. #define NFS_READ 6
  388. #define NFS_FHSIZE 32
  389. #define NFSERR_PERM 1
  390. #define NFSERR_NOENT 2
  391. #define NFSERR_ACCES 13
  392. /* Block size used for NFS read accesses. A RPC reply packet (including all
  393. * headers) must fit within a single Ethernet frame to avoid fragmentation.
  394. * Chosen to be a power of two, as most NFS servers are optimized for this. */
  395. #define NFS_READ_SIZE 1024
  396. #define FLOPPY_BOOT_LOCATION 0x7c00
  397. /* Must match offsets in loader.S */
  398. #define ROM_SEGMENT 0x1fa
  399. #define ROM_LENGTH 0x1fc
  400. #define ROM_INFO_LOCATION (FLOPPY_BOOT_LOCATION + ROM_SEGMENT)
  401. /* at end of floppy boot block */
  402. struct rom_info
  403. {
  404. unsigned short rom_segment;
  405. unsigned short rom_length;
  406. };
  407. static inline int
  408. rom_address_ok (struct rom_info *rom, int assigned_rom_segment)
  409. {
  410. return (assigned_rom_segment < 0xC000
  411. || assigned_rom_segment == rom->rom_segment);
  412. }
  413. /* Define a type for passing info to a loaded program. */
  414. struct ebinfo
  415. {
  416. unsigned char major, minor; /* Version */
  417. unsigned short flags; /* Bit flags */
  418. };
  419. /***************************************************************************
  420. External prototypes
  421. ***************************************************************************/
  422. /* main.c */
  423. extern void print_network_configuration (void);
  424. extern int ifconfig (char *ip, char *sm, char *gw, char *svr);
  425. extern int udp_transmit (unsigned long destip, unsigned int srcsock,
  426. unsigned int destsock, int len, const void *buf);
  427. extern int await_reply (int type, int ival, void *ptr, int timeout);
  428. extern int decode_rfc1533 (unsigned char *, int, int, int);
  429. extern long rfc2131_sleep_interval (int base, int exp);
  430. extern void cleanup (void);
  431. extern int rarp (void);
  432. extern int bootp (void);
  433. extern void cleanup_net (void);
  434. /* config.c */
  435. extern void print_config (void);
  436. extern void eth_reset (void);
  437. extern int eth_probe (void);
  438. extern int eth_poll (void);
  439. extern void eth_transmit (const char *d, unsigned int t,
  440. unsigned int s, const void *p);
  441. extern void eth_disable (void);
  442. /* misc.c */
  443. extern void twiddle (void);
  444. extern void sleep (int secs);
  445. extern int getdec (char **s);
  446. extern void etherboot_printf (const char *, ...);
  447. extern int etherboot_sprintf (char *, const char *, ...);
  448. extern int inet_aton (char *p, in_addr *i);
  449. /***************************************************************************
  450. External variables
  451. ***************************************************************************/
  452. /* main.c */
  453. extern int ip_abort;
  454. extern int network_ready;
  455. extern struct rom_info rom;
  456. extern struct arptable_t arptable[MAX_ARP];
  457. /* config.c */
  458. extern struct nic nic;
  459. /* Local hack - define some macros to use etherboot source files "as is". */
  460. #ifndef GRUB
  461. # undef printf
  462. # define printf etherboot_printf
  463. # undef sprintf
  464. # define sprintf etherboot_sprintf
  465. #endif /* GRUB */