bootmon.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2005-2007 by egnite Software GmbH
  3. * Copyright (C) 2010 by egnite GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*
  36. * $Id: bootmon.c 3422 2011-05-18 09:46:57Z haraldkipp $
  37. */
  38. #include "utils.h"
  39. #include "uart.h"
  40. #include "dialog.h"
  41. #include "ether.h"
  42. #include "dhcp.h"
  43. #include "bootmon.h"
  44. unsigned long random_id;
  45. ETHERHDR sheader;
  46. ETHERFRAME sframe;
  47. ETHERHDR rheader;
  48. ETHERFRAME rframe;
  49. static unsigned char my_mac[32];
  50. static char my_ip[32];
  51. static char my_mask[32];
  52. static char my_gate[32];
  53. static char my_tftpd[32];
  54. static char my_image[64];
  55. #ifdef USRLED
  56. #include "npluled.h"
  57. void NplUledCntl(int status)
  58. {
  59. if (status == ULED_ON) {
  60. /*! \brief Activate negated chip select. */
  61. outb(NPL_XER, inb(NPL_XER) & ~NPL_USRLED);
  62. } else {
  63. if (status == ULED_OFF) {
  64. /*! \brief Deactivate negated chip select. */
  65. outb(NPL_XER, inb(NPL_XER) | NPL_USRLED);
  66. }
  67. }
  68. }
  69. #else
  70. #define NplUledCntl(x)
  71. #endif
  72. static int UserEntry(void)
  73. {
  74. PutString("\nBootMon 1.2.1\n");
  75. NplUledCntl(ULED_OFF);
  76. memcpy_(my_mac, confnet.cdn_mac, 6);
  77. strcpy_(my_ip, inet_ntoa(confnet.cdn_cip_addr));
  78. strcpy_(my_mask, inet_ntoa(confnet.cdn_ip_mask));
  79. strcpy_(my_gate, inet_ntoa(confnet.cdn_gateway));
  80. strcpy_(my_tftpd, inet_ntoa(confboot.cb_tftp_ip));
  81. strcpy_(my_image, (char*)confboot.cb_image);
  82. GetMac(my_mac);
  83. GetIP("IP address", my_ip);
  84. if (inet_addr(my_ip)) {
  85. GetIP("Net mask", my_mask);
  86. GetIP("Default route", my_gate);
  87. }
  88. GetIP("TFTP IP", my_tftpd);
  89. if (inet_addr(my_tftpd)) {
  90. PutString("TFTP Image: ");
  91. GetLine(my_image, 31);
  92. }
  93. PutString("\n");
  94. memcpy_(confnet.cdn_mac, my_mac, sizeof(confnet.cdn_mac));
  95. confnet.cdn_cip_addr = inet_addr(my_ip);
  96. confnet.cdn_ip_mask = inet_addr(my_mask);
  97. confnet.cdn_gateway = inet_addr(my_gate);
  98. confboot.cb_tftp_ip = inet_addr(my_tftpd);
  99. DEBUG("Store '");
  100. DEBUG(my_image);
  101. DEBUG("'\n");
  102. strcpy_((char*)confboot.cb_image, my_image);
  103. BootConfigWrite();
  104. return 0;
  105. }
  106. /*
  107. * Bootloader entry point.
  108. */
  109. int main(void)
  110. {
  111. int i;
  112. unsigned long *flash_app = (unsigned long *)0x10020000;
  113. NplUledCntl(ULED_ON);
  114. /* Initialize RS232 port. */
  115. UartInit();
  116. /* Read configuration from EEPROM. */
  117. if (BootConfigRead()) {
  118. /* First trial failed, retry once more. */
  119. BootConfigRead();
  120. }
  121. /* Set default netmask, if configured one is zero. */
  122. if (confnet.cdn_ip_mask == 0) {
  123. confnet.cdn_ip_mask = 0xFFFFFF;
  124. }
  125. /* Initialize random ID with MAC address. */
  126. memcpy_((unsigned char *)&random_id, &confnet.cdn_mac[2], sizeof(random_id));
  127. /*
  128. * Loop until a valid image is loaded.
  129. */
  130. for (;;) {
  131. /*
  132. * Give user a chance to enter a space and a new configuration.
  133. */
  134. for (i = 0; i < 100; i++) {
  135. if (UartRxWait(6000) == 0) {
  136. if (UartRx() == ' ') {
  137. UserEntry();
  138. break;
  139. }
  140. }
  141. }
  142. NplUledCntl(ULED_OFF);
  143. if (confboot.cb_tftp_ip == 0 && *flash_app == 0xE59FF018) {
  144. DEBUG("\nStarting Flashed Application\n");
  145. asm volatile ("@ Start Application" "\n\t" /* */
  146. "ldr r0, =0x10020000" "\n\t" /* */
  147. "bx r0" "\n\t" /* */
  148. :::"r0" /* */
  149. );
  150. }
  151. /*
  152. * Initialize the network interface controller hardware.
  153. */
  154. DEBUG("\nETHERNET ");
  155. if (EtherInit() == 0) {
  156. DEBUG("OK\n");
  157. /*
  158. * DHCP query and TFTP download.
  159. */
  160. if (DhcpQuery() == 0) {
  161. if (TftpRecv() == 0) {
  162. break;
  163. }
  164. }
  165. } else {
  166. DEBUG("No\n");
  167. }
  168. NplUledCntl(ULED_ON);
  169. }
  170. #ifdef NUTDEBUG
  171. /* Avoids UART garbage. */
  172. Delay(5);
  173. #endif
  174. asm volatile ("@ Start Application" "\n\t" /* */
  175. "ldr r0, =0" "\n\t" /* */
  176. "bx r0" "\n\t" /* */
  177. :::"r0" /* */
  178. );
  179. return 0;
  180. }