confos.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 2001-2006 by egnite Software 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 THE COPYRIGHT HOLDERS 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 THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT 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. /*
  34. * $Log$
  35. * Revision 1.10 2009/01/16 17:03:02 haraldkipp
  36. * Configurable host name length. The *nix conditional is
  37. * no longer required as this will be handled in the nvmem
  38. * routines. NutLoadConfig will now set the virgin host
  39. * name, if no valid configuration is available. Cookie
  40. * and virgin host name are configurable too, but disabled
  41. * in the Configurator until we fixed the string value
  42. * problem. You may use UserConf.mk instead.
  43. *
  44. * Revision 1.9 2006/05/25 09:18:28 haraldkipp
  45. * API documentation updated and corrected.
  46. *
  47. * Revision 1.8 2006/01/23 19:50:48 haraldkipp
  48. * Dummy NVMEM configuration for GBA added.
  49. *
  50. * Revision 1.7 2006/01/23 17:26:17 haraldkipp
  51. * Platform independant routines added, which provide generic access to
  52. * non-volatile memory.
  53. *
  54. * Revision 1.6 2005/07/26 15:49:59 haraldkipp
  55. * Cygwin support added.
  56. *
  57. * Revision 1.5 2004/09/08 10:24:34 haraldkipp
  58. * No EEPROM support for AT91
  59. *
  60. * Revision 1.4 2004/04/07 12:13:58 haraldkipp
  61. * Matthias Ringwald's *nix emulation added
  62. *
  63. * Revision 1.3 2004/03/16 16:48:45 haraldkipp
  64. * Added Jan Dubiec's H8/300 port.
  65. *
  66. * Revision 1.2 2004/03/03 17:42:19 drsung
  67. * Bugfix in NutSaveConfig. Write only to eeprom if actual byte in
  68. * eeprom differs from byte to write.
  69. *
  70. * Revision 1.1.1.1 2003/05/09 14:41:46 haraldkipp
  71. * Initial using 3.2.1
  72. *
  73. * Revision 1.1 2003/02/04 18:17:07 harald
  74. * Version 3 released
  75. *
  76. */
  77. #include <cfg/arch.h>
  78. #include <sys/nutdebug.h>
  79. #include <sys/confos.h>
  80. #include <dev/nvmem.h>
  81. #include <string.h>
  82. /*!
  83. * \addtogroup xgConfOs
  84. */
  85. /*@{*/
  86. /*!
  87. * \brief Global system configuration structure.
  88. *
  89. * Contains the current system configuration. Nut/OS will load
  90. * this structure from non-volatile memory during initialization.
  91. */
  92. CONFOS confos;
  93. /*!
  94. * \brief Load Nut/OS configuration from non-volatile memory.
  95. *
  96. * This routine is automatically called during system initialization.
  97. * It tries to read the OS configuration structure \ref CONFOS from
  98. * non-volatile memory, starting at /ref CONFOS_EE_OFFSET.
  99. *
  100. * The routine may fail, if
  101. * - non-volatile memory is not supported,
  102. * - non-volatile memory contains invalid data,
  103. * - the configuration structure has changed.
  104. *
  105. * \return 0 if OK, -1 on failures, in which case the hostname will
  106. * be set to \ref CONFOS_VIRGIN_HOSTNAME.
  107. */
  108. int NutLoadConfig(void)
  109. {
  110. /* Sanity check. */
  111. NUTASSERT(sizeof(CONFOS) <= 255);
  112. if (NutNvMemLoad(CONFOS_EE_OFFSET, &confos, sizeof(CONFOS)) == 0 /* If loaded... */
  113. && confos.size == sizeof(CONFOS) /* ...check magic size and cookie. */
  114. && memcmp(confos.magic, CONFOS_EE_MAGIC, sizeof(CONFOS_EE_MAGIC) - 1) == 0) {
  115. return 0;
  116. }
  117. /* No valid configuration, set virgin hostname. */
  118. NUTASSERT(sizeof(confos.hostname) > strlen(CONFOS_VIRGIN_HOSTNAME));
  119. strcpy(confos.hostname, CONFOS_VIRGIN_HOSTNAME);
  120. return -1;
  121. }
  122. /*!
  123. * \brief Save Nut/OS configuration in non-volatile memory.
  124. *
  125. * Since Nut/OS version 4.7.5, this routine is no longer called automatically
  126. * during system initialization. Thus, if NutLoadConfig() fails, then the
  127. * non-volatile memory remains invalid and NutLoadConfig() will again fail
  128. * during the next system start.
  129. *
  130. * \return 0 if OK, -1 on failures.
  131. */
  132. int NutSaveConfig(void)
  133. {
  134. /* Sanity checks. */
  135. NUTASSERT(sizeof(CONFOS) <= 255);
  136. NUTASSERT(strlen(confos.hostname) <= MAX_HOSTNAME_LEN);
  137. /* Update the magic part. */
  138. confos.size = sizeof(CONFOS);
  139. memcpy(confos.magic, CONFOS_EE_MAGIC, sizeof(CONFOS_EE_MAGIC) - 1);
  140. return NutNvMemSave(CONFOS_EE_OFFSET, &confos, sizeof(CONFOS));
  141. }
  142. /*@}*/