fw_env.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. char *lockname;
  58. };
  59. int parse_aes_key(char *key, uint8_t *bin_key);
  60. /**
  61. * fw_printenv() - print one or several environment variables
  62. *
  63. * @argc: number of variables names to be printed, prints all if 0
  64. * @argv: array of variable names to be printed, if argc != 0
  65. * @value_only: do not repeat the variable name, print the bare value,
  66. * only one variable allowed with this option, argc must be 1
  67. * @opts: encryption key, configuration file, defaults are used if NULL
  68. *
  69. * Description:
  70. * Uses fw_env_open, fw_getenv
  71. *
  72. * Return:
  73. * 0 on success, -1 on failure (modifies errno)
  74. */
  75. int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts);
  76. /**
  77. * fw_setenv() - adds or removes one variable to the environment
  78. *
  79. * @argc: number of strings in argv, argv[0] is variable name,
  80. * argc==1 means erase variable, argc > 1 means add a variable
  81. * @argv: argv[0] is variable name, argv[1..argc-1] are concatenated separated
  82. * by single blank and set as the new value of the variable
  83. * @opts: how to retrieve environment from flash, defaults are used if NULL
  84. *
  85. * Description:
  86. * Uses fw_env_open, fw_env_write, fw_env_close
  87. *
  88. * Return:
  89. * 0 on success, -1 on failure (modifies errno)
  90. *
  91. * ERRORS:
  92. * EROFS - some variables ("ethaddr", "serial#") cannot be modified
  93. */
  94. int fw_setenv(int argc, char *argv[], struct env_opts *opts);
  95. /**
  96. * fw_parse_script() - adds or removes multiple variables with a batch script
  97. *
  98. * @fname: batch script file name
  99. * @opts: encryption key, configuration file, defaults are used if NULL
  100. *
  101. * Description:
  102. * Uses fw_env_open, fw_env_write, fw_env_close
  103. *
  104. * Return:
  105. * 0 success, -1 on failure (modifies errno)
  106. *
  107. * Script Syntax:
  108. *
  109. * key [ [space]+ value]
  110. *
  111. * lines starting with '#' treated as comment
  112. *
  113. * A variable without value will be deleted. Any number of spaces are allowed
  114. * between key and value. The value starts with the first non-space character
  115. * and ends with newline. No comments allowed on these lines. Spaces inside
  116. * the value are preserved verbatim.
  117. *
  118. * Script Example:
  119. *
  120. * netdev eth0
  121. *
  122. * kernel_addr 400000
  123. *
  124. * foo spaces are copied verbatim
  125. *
  126. * # delete variable bar
  127. *
  128. * bar
  129. */
  130. int fw_parse_script(char *fname, struct env_opts *opts);
  131. /**
  132. * fw_env_open() - read enviroment from flash into RAM cache
  133. *
  134. * @opts: encryption key, configuration file, defaults are used if NULL
  135. *
  136. * Return:
  137. * 0 on success, -1 on failure (modifies errno)
  138. */
  139. int fw_env_open(struct env_opts *opts);
  140. /**
  141. * fw_getenv() - lookup variable in the RAM cache
  142. *
  143. * @name: variable to be searched
  144. * Return:
  145. * pointer to start of value, NULL if not found
  146. */
  147. char *fw_getenv(char *name);
  148. /**
  149. * fw_env_write() - modify a variable held in the RAM cache
  150. *
  151. * @name: variable
  152. * @value: delete variable if NULL, otherwise create or overwrite the variable
  153. *
  154. * This is called in sequence to update the environment in RAM without updating
  155. * the copy in flash after each set
  156. *
  157. * Return:
  158. * 0 on success, -1 on failure (modifies errno)
  159. *
  160. * ERRORS:
  161. * EROFS - some variables ("ethaddr", "serial#") cannot be modified
  162. */
  163. int fw_env_write(char *name, char *value);
  164. /**
  165. * fw_env_close - write the environment from RAM cache back to flash
  166. *
  167. * @opts: encryption key, configuration file, defaults are used if NULL
  168. *
  169. * Return:
  170. * 0 on success, -1 on failure (modifies errno)
  171. */
  172. int fw_env_close(struct env_opts *opts);
  173. unsigned long crc32(unsigned long, const unsigned char *, unsigned);