bootloader.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2008 by Thermotemp GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THERMOTEMP GMBH AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THERMOTEMP
  21. * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. */
  32. #include <cfg/crt.h>
  33. #include <string.h>
  34. #include <stdio.h>
  35. #include <io.h>
  36. #include <dev/board.h>
  37. #include <dev/npl.h>
  38. #include <dev/nvmem.h>
  39. #include <sys/timer.h>
  40. #include <sys/confnet.h>
  41. #include <net/if_var.h>
  42. #include <net/route.h>
  43. #include <arpa/inet.h>
  44. #include "default.h"
  45. #include "debug.h"
  46. #include "loader.h"
  47. void init_network(void)
  48. {
  49. NutRegisterDevice(&DEV_ETHER, 0, 0);
  50. /* Read Standard Network Parameters */
  51. if (NutNetLoadConfig(DEV_ETHER_NAME) != 0) { /* EEPROM net configuration is NOT valid */
  52. u_char mac[] = DEFAULT_MAC;
  53. /* Try to load the last valid mac address from confnet structure */
  54. if (NutNvMemLoad(MAC_POSITION, mac, 6) != 0) {
  55. ERROR("Could not load MAC address\r\n");
  56. }
  57. /* Check if it's a valid mac. The first three bytes needs to be the same like the default MAC */
  58. if ((mac[0] != (u_char) DEFAULT_MAC[0]) || (mac[1] == (u_char) DEFAULT_MAC[1]) || (mac[2] == (u_char) DEFAULT_MAC[2])) {
  59. memcpy(mac, DEFAULT_MAC, sizeof(mac));
  60. }
  61. confnet.cdn_ip_addr = inet_addr(DEFAULT_IP);
  62. confnet.cdn_gateway = inet_addr(DEFAULT_GATWAY);
  63. confnet.cdn_ip_mask = inet_addr(DEFAULT_NETMASK);
  64. NutNetSaveConfig();
  65. NutNetIfConfig(DEV_ETHER_NAME, mac, confnet.cdn_ip_addr, confnet.cdn_ip_mask);
  66. } else {
  67. /* Check of the MAC address is valid. Check for the first three bytes of the default MAC address */
  68. if ((confnet.cdn_mac[0] == (u_char) DEFAULT_MAC[0]) && (confnet.cdn_mac[1] == (u_char) DEFAULT_MAC[1]) && (confnet.cdn_mac[2] == (u_char) DEFAULT_MAC[2])) {
  69. NutNetIfConfig(DEV_ETHER_NAME, confnet.cdn_mac, confnet.cdn_ip_addr, confnet.cdn_ip_mask);
  70. } else {
  71. u_char mac[] = DEFAULT_MAC;
  72. NutNetIfConfig(DEV_ETHER_NAME, mac, confnet.cdn_ip_addr, confnet.cdn_ip_mask);
  73. }
  74. }
  75. NutIpRouteAdd(0, 0, confnet.cdn_gateway, &DEV_ETHER);
  76. INFO("MAC address: %02X:%02X:%02X:%02X:%02X:%02X\r\n", confnet.cdn_mac[0], confnet.cdn_mac[1], confnet.cdn_mac[2],
  77. confnet.cdn_mac[3], confnet.cdn_mac[4], confnet.cdn_mac[5]);
  78. INFO("IP:%s\r\n", inet_ntoa(confnet.cdn_ip_addr));
  79. INFO("Netmask: %s\r\n", inet_ntoa(confnet.cdn_ip_mask));
  80. INFO("Gateway: %s\r\n", inet_ntoa(confnet.cdn_gateway));
  81. }
  82. FILE *init_uart(void)
  83. {
  84. u_long baud = DEFAULT_RS232_BAUD;
  85. u_long cookedmode = FALSE;
  86. FILE *uart0;
  87. /* Init the debug UART */
  88. NutRegisterDevice(&DEV_UART0, 0, 0);
  89. uart0 = fopen(DEV_UART0_NAME, "r+");
  90. _ioctl(_fileno(uart0), UART_SETSPEED, &baud);
  91. _ioctl(_fileno(uart0), UART_SETCOOKEDMODE, &cookedmode);
  92. freopen(DEV_UART0_NAME, "w", stdout);
  93. freopen(DEV_UART0_NAME, "r", stdin);
  94. freopen(DEV_UART0_NAME, "w", stderr);
  95. return uart0;
  96. }
  97. int main(void)
  98. {
  99. FILE *uart0;
  100. uart0 = init_uart();
  101. INFO("At91sam7X Bootloader started.\r\n\r\n");
  102. init_loader();
  103. if (check_or_save_md5(confboot.size, FALSE) != 0) {
  104. init_network();
  105. loader(uart0);
  106. }
  107. INFO("Booting...\r\n");
  108. NutSleep(100);
  109. boot();
  110. while (1);
  111. }