config.pl 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #!/usr/bin/env perl
  2. #
  3. # This file is part of mbed TLS (https://tls.mbed.org)
  4. #
  5. # Copyright (c) 2014-2016, ARM Limited, All Rights Reserved
  6. #
  7. # Purpose
  8. #
  9. # Comments and uncomments #define lines in the given header file and optionally
  10. # sets their value or can get the value. This is to provide scripting control of
  11. # what preprocessor symbols, and therefore what build time configuration flags
  12. # are set in the 'config.h' file.
  13. #
  14. # Usage: config.pl [-f <file> | --file <file>] [-o | --force]
  15. # [set <symbol> <value> | unset <symbol> | get <symbol> |
  16. # full | realfull]
  17. #
  18. # Full usage description provided below.
  19. #
  20. # The following options are disabled instead of enabled with "full".
  21. #
  22. # MBEDTLS_TEST_NULL_ENTROPY
  23. # MBEDTLS_DEPRECATED_REMOVED
  24. # MBEDTLS_HAVE_SSE2
  25. # MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
  26. # MBEDTLS_ECP_DP_M221_ENABLED
  27. # MBEDTLS_ECP_DP_M383_ENABLED
  28. # MBEDTLS_ECP_DP_M511_ENABLED
  29. # MBEDTLS_MEMORY_BACKTRACE
  30. # MBEDTLS_MEMORY_BUFFER_ALLOC_C
  31. # MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
  32. # MBEDTLS_NO_PLATFORM_ENTROPY
  33. # MBEDTLS_REMOVE_ARC4_CIPHERSUITES
  34. # MBEDTLS_REMOVE_3DES_CIPHERSUITES
  35. # MBEDTLS_SSL_HW_RECORD_ACCEL
  36. # MBEDTLS_RSA_NO_CRT
  37. # MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
  38. # MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
  39. # - this could be enabled if the respective tests were adapted
  40. # MBEDTLS_ZLIB_SUPPORT
  41. # MBEDTLS_PKCS11_C
  42. # MBEDTLS_NO_UDBL_DIVISION
  43. # MBEDTLS_NO_64BIT_MULTIPLICATION
  44. # and any symbol beginning _ALT
  45. #
  46. use warnings;
  47. use strict;
  48. my $config_file = "include/mbedtls/config.h";
  49. my $usage = <<EOU;
  50. $0 [-f <file> | --file <file>] [-o | --force]
  51. [set <symbol> <value> | unset <symbol> | get <symbol> |
  52. full | realfull | baremetal]
  53. Commands
  54. set <symbol> [<value>] - Uncomments or adds a #define for the <symbol> to
  55. the configuration file, and optionally making it
  56. of <value>.
  57. If the symbol isn't present in the file an error
  58. is returned.
  59. unset <symbol> - Comments out the #define for the given symbol if
  60. present in the configuration file.
  61. get <symbol> - Finds the #define for the given symbol, returning
  62. an exitcode of 0 if the symbol is found, and 1 if
  63. not. The value of the symbol is output if one is
  64. specified in the configuration file.
  65. full - Uncomments all #define's in the configuration file
  66. excluding some reserved symbols, until the
  67. 'Module configuration options' section
  68. realfull - Uncomments all #define's with no exclusions
  69. baremetal - Sets full configuration suitable for baremetal build.
  70. Options
  71. -f | --file <filename> - The file or file path for the configuration file
  72. to edit. When omitted, the following default is
  73. used:
  74. $config_file
  75. -o | --force - If the symbol isn't present in the configuration
  76. file when setting its value, a #define is
  77. appended to the end of the file.
  78. EOU
  79. my @excluded = qw(
  80. MBEDTLS_TEST_NULL_ENTROPY
  81. MBEDTLS_DEPRECATED_REMOVED
  82. MBEDTLS_HAVE_SSE2
  83. MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
  84. MBEDTLS_CTR_DRBG_USE_128_BIT_KEY
  85. MBEDTLS_ECP_DP_M221_ENABLED
  86. MBEDTLS_ECP_DP_M383_ENABLED
  87. MBEDTLS_ECP_DP_M511_ENABLED
  88. MBEDTLS_MEMORY_DEBUG
  89. MBEDTLS_MEMORY_BACKTRACE
  90. MBEDTLS_MEMORY_BUFFER_ALLOC_C
  91. MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
  92. MBEDTLS_NO_PLATFORM_ENTROPY
  93. MBEDTLS_RSA_NO_CRT
  94. MBEDTLS_REMOVE_ARC4_CIPHERSUITES
  95. MBEDTLS_REMOVE_3DES_CIPHERSUITES
  96. MBEDTLS_SSL_HW_RECORD_ACCEL
  97. MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
  98. MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
  99. MBEDTLS_ZLIB_SUPPORT
  100. MBEDTLS_PKCS11_C
  101. MBEDTLS_NO_UDBL_DIVISION
  102. MBEDTLS_NO_64BIT_MULTIPLICATION
  103. _ALT\s*$
  104. );
  105. # Things that should be disabled in "baremetal"
  106. my @excluded_baremetal = qw(
  107. MBEDTLS_NET_C
  108. MBEDTLS_TIMING_C
  109. MBEDTLS_FS_IO
  110. MBEDTLS_ENTROPY_NV_SEED
  111. MBEDTLS_HAVE_TIME
  112. MBEDTLS_HAVE_TIME_DATE
  113. MBEDTLS_DEPRECATED_WARNING
  114. MBEDTLS_HAVEGE_C
  115. MBEDTLS_THREADING_C
  116. MBEDTLS_THREADING_PTHREAD
  117. MBEDTLS_MEMORY_BACKTRACE
  118. MBEDTLS_MEMORY_BUFFER_ALLOC_C
  119. MBEDTLS_PLATFORM_TIME_ALT
  120. MBEDTLS_PLATFORM_FPRINTF_ALT
  121. );
  122. # Things that should be enabled in "full" even if they match @excluded
  123. my @non_excluded = qw(
  124. PLATFORM_[A-Z0-9]+_ALT
  125. );
  126. # Things that should be enabled in "baremetal"
  127. my @non_excluded_baremetal = qw(
  128. MBEDTLS_NO_PLATFORM_ENTROPY
  129. );
  130. # Process the command line arguments
  131. my $force_option = 0;
  132. my ($arg, $name, $value, $action);
  133. while ($arg = shift) {
  134. # Check if the argument is an option
  135. if ($arg eq "-f" || $arg eq "--file") {
  136. $config_file = shift;
  137. -f $config_file or die "No such file: $config_file\n";
  138. }
  139. elsif ($arg eq "-o" || $arg eq "--force") {
  140. $force_option = 1;
  141. }
  142. else
  143. {
  144. # ...else assume it's a command
  145. $action = $arg;
  146. if ($action eq "full" || $action eq "realfull" || $action eq "baremetal" ) {
  147. # No additional parameters
  148. die $usage if @ARGV;
  149. }
  150. elsif ($action eq "unset" || $action eq "get") {
  151. die $usage unless @ARGV;
  152. $name = shift;
  153. }
  154. elsif ($action eq "set") {
  155. die $usage unless @ARGV;
  156. $name = shift;
  157. $value = shift if @ARGV;
  158. }
  159. else {
  160. die "Command '$action' not recognised.\n\n".$usage;
  161. }
  162. }
  163. }
  164. # If no command was specified, exit...
  165. if ( not defined($action) ){ die $usage; }
  166. # Check the config file is present
  167. if (! -f $config_file) {
  168. chdir '..' or die;
  169. # Confirm this is the project root directory and try again
  170. if ( !(-d 'scripts' && -d 'include' && -d 'library' && -f $config_file) ) {
  171. die "If no file specified, must be run from the project root or scripts directory.\n";
  172. }
  173. }
  174. # Now read the file and process the contents
  175. open my $config_read, '<', $config_file or die "read $config_file: $!\n";
  176. my @config_lines = <$config_read>;
  177. close $config_read;
  178. # Add required baremetal symbols to the list that is included.
  179. if ( $action eq "baremetal" ) {
  180. @non_excluded = ( @non_excluded, @non_excluded_baremetal );
  181. }
  182. my ($exclude_re, $no_exclude_re, $exclude_baremetal_re);
  183. if ($action eq "realfull") {
  184. $exclude_re = qr/^$/;
  185. $no_exclude_re = qr/./;
  186. } else {
  187. $exclude_re = join '|', @excluded;
  188. $no_exclude_re = join '|', @non_excluded;
  189. }
  190. if ( $action eq "baremetal" ) {
  191. $exclude_baremetal_re = join '|', @excluded_baremetal;
  192. }
  193. my $config_write = undef;
  194. if ($action ne "get") {
  195. open $config_write, '>', $config_file or die "write $config_file: $!\n";
  196. }
  197. my $done;
  198. for my $line (@config_lines) {
  199. if ($action eq "full" || $action eq "realfull" || $action eq "baremetal" ) {
  200. if ($line =~ /name SECTION: Module configuration options/) {
  201. $done = 1;
  202. }
  203. if (!$done && $line =~ m!^//\s?#define! &&
  204. ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) &&
  205. ( $action ne "baremetal" || ( $line !~ /$exclude_baremetal_re/ ) ) ) {
  206. $line =~ s!^//\s?!!;
  207. }
  208. if (!$done && $line =~ m!^\s?#define! &&
  209. ! ( ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) &&
  210. ( $action ne "baremetal" || ( $line !~ /$exclude_baremetal_re/ ) ) ) ) {
  211. $line =~ s!^!//!;
  212. }
  213. } elsif ($action eq "unset") {
  214. if (!$done && $line =~ /^\s*#define\s*$name\b/) {
  215. $line = '//' . $line;
  216. $done = 1;
  217. }
  218. } elsif (!$done && $action eq "set") {
  219. if ($line =~ m!^(?://)?\s*#define\s*$name\b!) {
  220. $line = "#define $name";
  221. $line .= " $value" if defined $value && $value ne "";
  222. $line .= "\n";
  223. $done = 1;
  224. }
  225. } elsif (!$done && $action eq "get") {
  226. if ($line =~ /^\s*#define\s*$name(?:\s+(.*?))\s*(?:$|\/\*|\/\/)/) {
  227. $value = $1;
  228. $done = 1;
  229. }
  230. }
  231. if (defined $config_write) {
  232. print $config_write $line or die "write $config_file: $!\n";
  233. }
  234. }
  235. # Did the set command work?
  236. if ($action eq "set" && $force_option && !$done) {
  237. # If the force option was set, append the symbol to the end of the file
  238. my $line = "#define $name";
  239. $line .= " $value" if defined $value && $value ne "";
  240. $line .= "\n";
  241. $done = 1;
  242. print $config_write $line or die "write $config_file: $!\n";
  243. }
  244. if (defined $config_write) {
  245. close $config_write or die "close $config_file: $!\n";
  246. }
  247. if ($action eq "get") {
  248. if ($done) {
  249. if ($value ne '') {
  250. print "$value\n";
  251. }
  252. exit 0;
  253. } else {
  254. # If the symbol was not found, return an error
  255. exit 1;
  256. }
  257. }
  258. if ($action eq "full" && !$done) {
  259. die "Configuration section was not found in $config_file\n";
  260. }
  261. if ($action ne "full" && $action ne "unset" && !$done) {
  262. die "A #define for the symbol $name was not found in $config_file\n";
  263. }
  264. __END__