environ.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2008 by egnite Software GmbH
  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. #include <dev/nvmem.h>
  33. #include <stdint.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <memdebug.h>
  37. #include <sys/nutdebug.h>
  38. #include <sys/environ.h>
  39. NUTENVIRONMENT *nut_environ;
  40. /*
  41. * Read string from non-volatile memory.
  42. */
  43. static size_t read_string(int *addr, char *buf, size_t siz)
  44. {
  45. size_t rc = 0;
  46. char ch;
  47. while (rc <= siz) {
  48. if (NutNvMemLoad(*addr, &ch, 1)) {
  49. rc = 0;
  50. break;
  51. }
  52. (*addr)++;
  53. if (ch == 0) {
  54. break;
  55. }
  56. if (buf) {
  57. *buf++ = ch;
  58. }
  59. rc++;
  60. }
  61. if (buf) {
  62. *buf = 0;
  63. }
  64. return rc;
  65. }
  66. /*!
  67. * \brief Find an NUTENVIRONMENT entry by name.
  68. *
  69. * \param name Points to a string, which is the name of the entry.
  70. *
  71. * \return A pointer to the entry with the specified name. If the name
  72. * cannot be found, then a null pointer is returned.
  73. */
  74. NUTENVIRONMENT *findenv(const char *name)
  75. {
  76. NUTENVIRONMENT *envp;
  77. size_t len;
  78. size_t max_len = 0;
  79. char *buf;
  80. NUTASSERT(name != NULL);
  81. if (nut_environ == NULL) {
  82. /* Load environment from non-volatile memory. */
  83. uint32_t magic;
  84. int addr = ENVIRON_EE_OFFSET;
  85. NUTENVIRONMENT *prvp = NULL;
  86. if (NutNvMemLoad(addr, &magic, sizeof(magic)) == 0 && magic == ENVIRON_MAGIC) {
  87. addr += 4;
  88. /* Determine the maximum string length. */
  89. while ((len = read_string(&addr, NULL, MAX_ENVIRON_ITEM_SIZE)) > 0) {
  90. if (len > max_len) {
  91. max_len = len;
  92. }
  93. len = read_string(&addr, NULL, MAX_ENVIRON_ITEM_SIZE);
  94. if (len > max_len) {
  95. max_len = len;
  96. }
  97. }
  98. /* Read the environment. */
  99. addr = ENVIRON_EE_OFFSET + sizeof(magic);
  100. if (max_len && (buf = malloc(max_len + 1)) != NULL) {
  101. while ((len = read_string(&addr, buf, max_len)) > 0) {
  102. if ((envp = malloc(sizeof(NUTENVIRONMENT))) == NULL) {
  103. break;
  104. }
  105. memset(envp, 0, sizeof(NUTENVIRONMENT));
  106. if ((envp->env_name = malloc(len + 1)) == NULL) {
  107. free(envp);
  108. break;
  109. }
  110. strcpy(envp->env_name, buf);
  111. len = read_string(&addr, buf, max_len);
  112. if ((envp->env_value = malloc(len + 1)) == NULL) {
  113. break;
  114. }
  115. strcpy(envp->env_value, buf);
  116. if (prvp) {
  117. prvp->env_next = envp;
  118. envp->env_prev = prvp;
  119. } else {
  120. nut_environ = envp;
  121. }
  122. prvp = envp;
  123. }
  124. free(buf);
  125. }
  126. }
  127. }
  128. for (envp = nut_environ; envp; envp = envp->env_next) {
  129. if (strcmp(envp->env_name, name) == 0) {
  130. break;
  131. }
  132. }
  133. return envp;
  134. }