rarp.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * (C) Copyright 2000-2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <net.h>
  26. #include "nfs.h"
  27. #include "bootp.h"
  28. #include "rarp.h"
  29. #include "tftp.h"
  30. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  31. #define TIMEOUT 5 /* Seconds before trying BOOTP again */
  32. #ifndef CONFIG_NET_RETRY_COUNT
  33. # define TIMEOUT_COUNT 5 /* # of timeouts before giving up */
  34. #else
  35. # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
  36. #endif
  37. int RarpTry;
  38. /*
  39. * Handle a RARP received packet.
  40. */
  41. static void
  42. RarpHandler(uchar * dummi0, unsigned dummi1, unsigned dummi2, unsigned dummi3)
  43. {
  44. char *s;
  45. #ifdef DEBUG
  46. puts ("Got good RARP\n");
  47. #endif
  48. if ((s = getenv("autoload")) != NULL) {
  49. if (*s == 'n') {
  50. /*
  51. * Just use RARP to configure system;
  52. * Do not use TFTP/NFS to to load the bootfile.
  53. */
  54. NetState = NETLOOP_SUCCESS;
  55. return;
  56. #if (CONFIG_COMMANDS & CFG_CMD_NFS)
  57. } else if ((s != NULL) && !strcmp(s, "NFS")) {
  58. NfsStart();
  59. return;
  60. #endif
  61. }
  62. }
  63. TftpStart ();
  64. }
  65. /*
  66. * Timeout on BOOTP request.
  67. */
  68. static void
  69. RarpTimeout(void)
  70. {
  71. if (RarpTry >= TIMEOUT_COUNT) {
  72. puts ("\nRetry count exceeded; starting again\n");
  73. NetStartAgain ();
  74. } else {
  75. NetSetTimeout (TIMEOUT * CFG_HZ, RarpTimeout);
  76. RarpRequest ();
  77. }
  78. }
  79. void
  80. RarpRequest (void)
  81. {
  82. int i;
  83. volatile uchar *pkt;
  84. ARP_t * rarp;
  85. printf("RARP broadcast %d\n", ++RarpTry);
  86. pkt = NetTxPacket;
  87. pkt += NetSetEther(pkt, NetBcastAddr, PROT_RARP);
  88. rarp = (ARP_t *)pkt;
  89. rarp->ar_hrd = htons (ARP_ETHER);
  90. rarp->ar_pro = htons (PROT_IP);
  91. rarp->ar_hln = 6;
  92. rarp->ar_pln = 4;
  93. rarp->ar_op = htons (RARPOP_REQUEST);
  94. memcpy (&rarp->ar_data[0], NetOurEther, 6); /* source ET addr */
  95. memcpy (&rarp->ar_data[6], &NetOurIP, 4); /* source IP addr */
  96. memcpy (&rarp->ar_data[10], NetOurEther, 6); /* dest ET addr = source ET addr ??*/
  97. /* dest. IP addr set to broadcast */
  98. for (i = 0; i <= 3; i++) {
  99. rarp->ar_data[16 + i] = 0xff;
  100. }
  101. NetSendPacket(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
  102. NetSetTimeout(TIMEOUT * CFG_HZ, RarpTimeout);
  103. NetSetHandler(RarpHandler);
  104. }
  105. #endif /* CFG_CMD_NET */