demo-app.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /*
  33. * This demo demonstrates the usage of the bootloader
  34. *
  35. */
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <io.h>
  39. #include <dev/board.h>
  40. #include <dev/nvmem.h>
  41. #include <dev/watchdog.h>
  42. #include <sys/timer.h>
  43. #define TRUE 1
  44. #define FALSE 0
  45. #ifndef EE_CONFBOOT
  46. #define EE_CONFBOOT 0x80 /* Offset in eeprom we store the boot info structure */
  47. #endif
  48. /*
  49. * Boot configuration structure.
  50. */
  51. typedef struct __attribute__ ((packed)) _CONFBOOT {
  52. unsigned char cb_size; /* Size of this structure. */
  53. unsigned char cb_flags; /* Currently unused */
  54. unsigned long cb_tftp_ip; /* IP address of the TFTP server */
  55. char cb_image[58]; /* Name of the image file to load */
  56. u_char digest[16]; /* MD5 digest */
  57. int size; /* Size of the image */
  58. } CONFBOOT;
  59. CONFBOOT confboot;
  60. /* Reset the processor using the reset controller */
  61. void boot(void)
  62. {
  63. printf("Booting...\r\n");
  64. NutSleep(100);
  65. outr(RSTC_CR, RSTC_KEY | RSTC_EXTRST | RSTC_PERRST | RSTC_PROCRST);
  66. }
  67. /* Read boot configuration from non-volatile memory. */
  68. static int read_boot_config(void)
  69. {
  70. int rc;
  71. rc = NutNvMemLoad(EE_CONFBOOT, &confboot, sizeof(CONFBOOT));
  72. if (confboot.cb_size != sizeof(CONFBOOT)) {
  73. rc = -1;
  74. }
  75. return rc;
  76. }
  77. /* Store boot configuration in non-volatile memory. */
  78. static void write_boot_config(void)
  79. {
  80. confboot.cb_size = sizeof(CONFBOOT);
  81. NutNvMemSave(EE_CONFBOOT, &confboot, sizeof(CONFBOOT));
  82. }
  83. /* Init the uart */
  84. FILE *init_uart(void)
  85. {
  86. u_long baud = 115200;
  87. u_long cookedmode = FALSE;
  88. FILE *uart0;
  89. /* Init the debug UART */
  90. NutRegisterDevice(&DEV_UART, 0, 0);
  91. uart0 = fopen(DEV_UART_NAME, "r+");
  92. _ioctl(_fileno(uart0), UART_SETSPEED, &baud);
  93. _ioctl(_fileno(uart0), UART_SETCOOKEDMODE, &cookedmode);
  94. freopen(DEV_UART_NAME, "w", stdout);
  95. freopen(DEV_UART_NAME, "r", stdin);
  96. freopen(DEV_UART_NAME, "w", stderr);
  97. return uart0;
  98. }
  99. static char inbuf[128];
  100. int main(void)
  101. {
  102. char *cp;
  103. int i;
  104. FILE *uart = init_uart();
  105. printf("Demo application for at91sam7x_bootloader started...\r\n");
  106. for (i = 0;; i++) {
  107. printf("\nEnter \"reset\" to reset or \"boot\" to reboot from tftp server:\r\n");
  108. fflush(uart);
  109. fgets(inbuf, 10, uart);
  110. /*
  111. * Chop off trailing linefeed.
  112. */
  113. cp = strchr(inbuf, '\n');
  114. if (cp) {
  115. *cp = 0;
  116. }
  117. /* Check if the string is "boot" or "load" */
  118. if (strcmp(inbuf, "reset") == 0) {
  119. printf("Reset...\r\n");
  120. boot();
  121. } else
  122. if (strcmp(inbuf, "boot") == 0) {
  123. printf("Reboot from tftp server\r\n");
  124. read_boot_config();
  125. memset(confboot.digest, 0, sizeof(confboot.digest));
  126. write_boot_config();
  127. boot();
  128. } else {
  129. printf("You entered: %s\r\n", inbuf);
  130. }
  131. }
  132. return 0;
  133. }