fw_env.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * (C) Copyright 2002-2008
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <aes.h>
  8. #include <stdint.h>
  9. /* Pull in the current config to define the default environment */
  10. #include <linux/kconfig.h>
  11. #ifndef __ASSEMBLY__
  12. #define __ASSEMBLY__ /* get only #defines from config.h */
  13. #include <config.h>
  14. #undef __ASSEMBLY__
  15. #else
  16. #include <config.h>
  17. #endif
  18. /*
  19. * To build the utility with the static configuration
  20. * comment out the next line.
  21. * See included "fw_env.config" sample file
  22. * for notes on configuration.
  23. */
  24. #define CONFIG_FILE "/etc/fw_env.config"
  25. #ifndef CONFIG_FILE
  26. #define HAVE_REDUND /* For systems with 2 env sectors */
  27. #define DEVICE1_NAME "/dev/mtd1"
  28. #define DEVICE2_NAME "/dev/mtd2"
  29. #define DEVICE1_OFFSET 0x0000
  30. #define ENV1_SIZE 0x4000
  31. #define DEVICE1_ESIZE 0x4000
  32. #define DEVICE1_ENVSECTORS 2
  33. #define DEVICE2_OFFSET 0x0000
  34. #define ENV2_SIZE 0x4000
  35. #define DEVICE2_ESIZE 0x4000
  36. #define DEVICE2_ENVSECTORS 2
  37. #endif
  38. #ifndef CONFIG_BAUDRATE
  39. #define CONFIG_BAUDRATE 115200
  40. #endif
  41. #ifndef CONFIG_BOOTDELAY
  42. #define CONFIG_BOOTDELAY 5 /* autoboot after 5 seconds */
  43. #endif
  44. #ifndef CONFIG_BOOTCOMMAND
  45. #define CONFIG_BOOTCOMMAND \
  46. "bootp; " \
  47. "setenv bootargs root=/dev/nfs nfsroot=${serverip}:${rootpath} " \
  48. "ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}::off; " \
  49. "bootm"
  50. #endif
  51. struct env_opts {
  52. #ifdef CONFIG_FILE
  53. char *config_file;
  54. #endif
  55. int aes_flag; /* Is AES encryption used? */
  56. uint8_t aes_key[AES_KEY_LENGTH];
  57. };
  58. int parse_aes_key(char *key, uint8_t *bin_key);
  59. /**
  60. * fw_printenv() - print one or several environment variables
  61. *
  62. * @argc: number of variables names to be printed, prints all if 0
  63. * @argv: array of variable names to be printed, if argc != 0
  64. * @value_only: do not repeat the variable name, print the bare value,
  65. * only one variable allowed with this option, argc must be 1
  66. * @opts: encryption key, configuration file, defaults are used if NULL
  67. *
  68. * Description:
  69. * Uses fw_env_open, fw_getenv
  70. *
  71. * Return:
  72. * 0 on success, -1 on failure (modifies errno)
  73. */
  74. int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts);
  75. /**
  76. * fw_setenv() - adds or removes one variable to the environment
  77. *
  78. * @argc: number of strings in argv, argv[0] is variable name,
  79. * argc==1 means erase variable, argc > 1 means add a variable
  80. * @argv: argv[0] is variable name, argv[1..argc-1] are concatenated separated
  81. * by single blank and set as the new value of the variable
  82. * @opts: how to retrieve environment from flash, defaults are used if NULL
  83. *
  84. * Description:
  85. * Uses fw_env_open, fw_env_write, fw_env_close
  86. *
  87. * Return:
  88. * 0 on success, -1 on failure (modifies errno)
  89. *
  90. * ERRORS:
  91. * EROFS - some variables ("ethaddr", "serial#") cannot be modified
  92. */
  93. int fw_setenv(int argc, char *argv[], struct env_opts *opts);
  94. /**
  95. * fw_parse_script() - adds or removes multiple variables with a batch script
  96. *
  97. * @fname: batch script file name
  98. * @opts: encryption key, configuration file, defaults are used if NULL
  99. *
  100. * Description:
  101. * Uses fw_env_open, fw_env_write, fw_env_close
  102. *
  103. * Return:
  104. * 0 success, -1 on failure (modifies errno)
  105. *
  106. * Script Syntax:
  107. *
  108. * key [ [space]+ value]
  109. *
  110. * lines starting with '#' treated as comment
  111. *
  112. * A variable without value will be deleted. Any number of spaces are allowed
  113. * between key and value. The value starts with the first non-space character
  114. * and ends with newline. No comments allowed on these lines. Spaces inside
  115. * the value are preserved verbatim.
  116. *
  117. * Script Example:
  118. *
  119. * netdev eth0
  120. *
  121. * kernel_addr 400000
  122. *
  123. * foo spaces are copied verbatim
  124. *
  125. * # delete variable bar
  126. *
  127. * bar
  128. */
  129. int fw_parse_script(char *fname, struct env_opts *opts);
  130. /**
  131. * fw_env_open() - read enviroment from flash into RAM cache
  132. *
  133. * @opts: encryption key, configuration file, defaults are used if NULL
  134. *
  135. * Return:
  136. * 0 on success, -1 on failure (modifies errno)
  137. */
  138. int fw_env_open(struct env_opts *opts);
  139. /**
  140. * fw_getenv() - lookup variable in the RAM cache
  141. *
  142. * @name: variable to be searched
  143. * Return:
  144. * pointer to start of value, NULL if not found
  145. */
  146. char *fw_getenv(char *name);
  147. /**
  148. * fw_env_write() - modify a variable held in the RAM cache
  149. *
  150. * @name: variable
  151. * @value: delete variable if NULL, otherwise create or overwrite the variable
  152. *
  153. * This is called in sequence to update the environment in RAM without updating
  154. * the copy in flash after each set
  155. *
  156. * Return:
  157. * 0 on success, -1 on failure (modifies errno)
  158. *
  159. * ERRORS:
  160. * EROFS - some variables ("ethaddr", "serial#") cannot be modified
  161. */
  162. int fw_env_write(char *name, char *value);
  163. /**
  164. * fw_env_close - write the environment from RAM cache back to flash
  165. *
  166. * @opts: encryption key, configuration file, defaults are used if NULL
  167. *
  168. * Return:
  169. * 0 on success, -1 on failure (modifies errno)
  170. */
  171. int fw_env_close(struct env_opts *opts);
  172. unsigned long crc32(unsigned long, const unsigned char *, unsigned);