blob: 0922e53a76c43ff53211761b82c38afe93868bbf [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
SimonB60f2cf92016-04-03 14:16:08 +01002#
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
Simon Butcher4ae86912016-06-21 10:09:25 +010010# 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.
SimonB60f2cf92016-04-03 14:16:08 +010013#
14# Usage: config.pl [-f <file> | --file <file>] [-o | --force]
Simon Butcher4ae86912016-06-21 10:09:25 +010015# [set <symbol> <value> | unset <symbol> | get <symbol> |
16# full | realfull]
SimonB60f2cf92016-04-03 14:16:08 +010017#
18# Full usage description provided below.
19#
Hanno Becker7c0f17d2017-09-28 11:49:46 +010020# The following options are disabled instead of enabled with "full".
SimonB60f2cf92016-04-03 14:16:08 +010021#
Simon Butcherab5df402016-06-11 02:31:21 +010022# MBEDTLS_TEST_NULL_ENTROPY
SimonB60f2cf92016-04-03 14:16:08 +010023# 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_NO_DEFAULT_ENTROPY_SOURCES
30# MBEDTLS_NO_PLATFORM_ENTROPY
31# MBEDTLS_REMOVE_ARC4_CIPHERSUITES
Andres Amaya Garcia5d8aade2018-10-30 18:21:41 +000032# MBEDTLS_REMOVE_3DES_CIPHERSUITES
SimonB60f2cf92016-04-03 14:16:08 +010033# MBEDTLS_SSL_HW_RECORD_ACCEL
Manuel Pégourié-Gonnardba8b1eb2019-06-17 15:21:07 +020034# MBEDTLS_SSL_PROTO_NO_DTLS
Manuel Pégourié-Gonnard26ac9c42019-07-01 10:07:28 +020035# MBEDTLS_SSL_NO_SESSION_CACHE
36# MBEDTLS_SSL_NO_SESSION_RESUMPTION
Hanno Becker7c0f17d2017-09-28 11:49:46 +010037# MBEDTLS_RSA_NO_CRT
Hanno Becker8239fad2019-08-27 15:45:44 +010038# MBEDTLS_USE_TINYCRYPT
SimonB60f2cf92016-04-03 14:16:08 +010039# MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
40# MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
41# - this could be enabled if the respective tests were adapted
Hanno Beckerb4d967a2019-06-12 13:59:14 +010042# MBEDTLS_X509_REMOVE_INFO
Hanno Becker843b71a2019-06-25 09:39:21 +010043# MBEDTLS_X509_CRT_REMOVE_TIME
Hanno Beckerd07614c2019-06-25 10:19:58 +010044# MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID
Teppo Järvelin4009d8f2019-08-19 14:48:09 +030045# MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION
Hanno Becker9ec3fe02019-07-01 17:36:12 +010046# MBEDTLS_X509_REMOVE_VERIFY_CALLBACK
SimonB60f2cf92016-04-03 14:16:08 +010047# MBEDTLS_ZLIB_SUPPORT
48# MBEDTLS_PKCS11_C
49# and any symbol beginning _ALT
50#
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020051
52use warnings;
53use strict;
54
SimonB60f2cf92016-04-03 14:16:08 +010055my $config_file = "include/mbedtls/config.h";
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020056my $usage = <<EOU;
SimonB60f2cf92016-04-03 14:16:08 +010057$0 [-f <file> | --file <file>] [-o | --force]
Simon Butcher4ae86912016-06-21 10:09:25 +010058 [set <symbol> <value> | unset <symbol> | get <symbol> |
Azim Khanc3c3a682017-12-21 15:19:53 +000059 full | realfull | baremetal]
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020060
SimonB60f2cf92016-04-03 14:16:08 +010061Commands
Simon Butcher4ae86912016-06-21 10:09:25 +010062 set <symbol> [<value>] - Uncomments or adds a #define for the <symbol> to
SimonB60f2cf92016-04-03 14:16:08 +010063 the configuration file, and optionally making it
64 of <value>.
65 If the symbol isn't present in the file an error
66 is returned.
Simon Butcher4ae86912016-06-21 10:09:25 +010067 unset <symbol> - Comments out the #define for the given symbol if
68 present in the configuration file.
69 get <symbol> - Finds the #define for the given symbol, returning
Gilles Peskined98e9e82017-10-09 16:56:18 +020070 an exitcode of 0 if the symbol is found, and 1 if
Simon Butcher4ae86912016-06-21 10:09:25 +010071 not. The value of the symbol is output if one is
72 specified in the configuration file.
SimonB60f2cf92016-04-03 14:16:08 +010073 full - Uncomments all #define's in the configuration file
Simon Butcher4ae86912016-06-21 10:09:25 +010074 excluding some reserved symbols, until the
SimonB60f2cf92016-04-03 14:16:08 +010075 'Module configuration options' section
76 realfull - Uncomments all #define's with no exclusions
Azim Khanc3c3a682017-12-21 15:19:53 +000077 baremetal - Sets full configuration suitable for baremetal build.
SimonB60f2cf92016-04-03 14:16:08 +010078
79Options
80 -f | --file <filename> - The file or file path for the configuration file
81 to edit. When omitted, the following default is
82 used:
83 $config_file
84 -o | --force - If the symbol isn't present in the configuration
Brian J Murray2adecba2016-11-06 04:45:15 -080085 file when setting its value, a #define is
SimonB60f2cf92016-04-03 14:16:08 +010086 appended to the end of the file.
87
88EOU
89
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020090my @excluded = qw(
Simon Butcherab5df402016-06-11 02:31:21 +010091MBEDTLS_TEST_NULL_ENTROPY
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092MBEDTLS_DEPRECATED_REMOVED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093MBEDTLS_HAVE_SSE2
94MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
95MBEDTLS_ECP_DP_M221_ENABLED
96MBEDTLS_ECP_DP_M383_ENABLED
97MBEDTLS_ECP_DP_M511_ENABLED
98MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
99MBEDTLS_NO_PLATFORM_ENTROPY
Hanno Becker7c0f17d2017-09-28 11:49:46 +0100100MBEDTLS_RSA_NO_CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101MBEDTLS_REMOVE_ARC4_CIPHERSUITES
Andres Amaya Garcia5d8aade2018-10-30 18:21:41 +0000102MBEDTLS_REMOVE_3DES_CIPHERSUITES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103MBEDTLS_SSL_HW_RECORD_ACCEL
Manuel Pégourié-Gonnardba8b1eb2019-06-17 15:21:07 +0200104MBEDTLS_SSL_PROTO_NO_TLS
Manuel Pégourié-Gonnard26ac9c42019-07-01 10:07:28 +0200105MBEDTLS_SSL_NO_SESSION_CACHE
106MBEDTLS_SSL_NO_SESSION_RESUMPTION
Hanno Becker8239fad2019-08-27 15:45:44 +0100107MBEDTLS_USE_TINYCRYPT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
109MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
Hanno Beckerb4d967a2019-06-12 13:59:14 +0100110MBEDTLS_X509_REMOVE_INFO
Hanno Becker843b71a2019-06-25 09:39:21 +0100111MBEDTLS_X509_CRT_REMOVE_TIME
Hanno Beckerd07614c2019-06-25 10:19:58 +0100112MBEDTLS_X509_CRT_REMOVE_SUBJECT_ISSUER_ID
Teppo Järvelin4009d8f2019-08-19 14:48:09 +0300113MBEDTLS_X509_REMOVE_HOSTNAME_VERIFICATION
Hanno Becker9ec3fe02019-07-01 17:36:12 +0100114MBEDTLS_X509_REMOVE_VERIFY_CALLBACK
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115MBEDTLS_ZLIB_SUPPORT
116MBEDTLS_PKCS11_C
Azim Khanc4e96942017-12-21 15:22:37 +0000117MBEDTLS_NO_UDBL_DIVISION
Manuel Pégourié-Gonnard2adb3752018-06-07 10:51:44 +0200118MBEDTLS_NO_64BIT_MULTIPLICATION
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +0200119MBEDTLS_USE_TINYCRYPT
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200120_ALT\s*$
121);
122
Azim Khan0d445732017-12-21 09:28:39 +0000123# Things that should be disabled in "baremetal"
124my @excluded_baremetal = qw(
125MBEDTLS_NET_C
126MBEDTLS_TIMING_C
127MBEDTLS_FS_IO
128MBEDTLS_ENTROPY_NV_SEED
129MBEDTLS_HAVE_TIME
130MBEDTLS_HAVE_TIME_DATE
131MBEDTLS_DEPRECATED_WARNING
132MBEDTLS_HAVEGE_C
133MBEDTLS_THREADING_C
134MBEDTLS_THREADING_PTHREAD
135MBEDTLS_MEMORY_BACKTRACE
136MBEDTLS_MEMORY_BUFFER_ALLOC_C
137MBEDTLS_PLATFORM_TIME_ALT
138MBEDTLS_PLATFORM_FPRINTF_ALT
139);
140
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +0100141# Things that should be enabled in "full" even if they match @excluded
142my @non_excluded = qw(
143PLATFORM_[A-Z0-9]+_ALT
144);
145
Azim Khan0d445732017-12-21 09:28:39 +0000146# Things that should be enabled in "baremetal"
147my @non_excluded_baremetal = qw(
148MBEDTLS_NO_PLATFORM_ENTROPY
149);
150
SimonB60f2cf92016-04-03 14:16:08 +0100151# Process the command line arguments
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200152
SimonB60f2cf92016-04-03 14:16:08 +0100153my $force_option = 0;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200154
SimonB60f2cf92016-04-03 14:16:08 +0100155my ($arg, $name, $value, $action);
156
SimonB73883c12016-04-04 13:49:10 +0100157while ($arg = shift) {
SimonB60f2cf92016-04-03 14:16:08 +0100158
159 # Check if the argument is an option
SimonB73883c12016-04-04 13:49:10 +0100160 if ($arg eq "-f" || $arg eq "--file") {
SimonB60f2cf92016-04-03 14:16:08 +0100161 $config_file = shift;
162
163 -f $config_file or die "No such file: $config_file\n";
164
165 }
SimonB73883c12016-04-04 13:49:10 +0100166 elsif ($arg eq "-o" || $arg eq "--force") {
SimonB60f2cf92016-04-03 14:16:08 +0100167 $force_option = 1;
168
169 }
170 else
171 {
172 # ...else assume it's a command
173 $action = $arg;
174
Azim Khan0d445732017-12-21 09:28:39 +0000175 if ($action eq "full" || $action eq "realfull" || $action eq "baremetal" ) {
SimonB60f2cf92016-04-03 14:16:08 +0100176 # No additional parameters
177 die $usage if @ARGV;
178
179 }
Simon Butcher4ae86912016-06-21 10:09:25 +0100180 elsif ($action eq "unset" || $action eq "get") {
SimonB60f2cf92016-04-03 14:16:08 +0100181 die $usage unless @ARGV;
182 $name = shift;
183
184 }
185 elsif ($action eq "set") {
186 die $usage unless @ARGV;
187 $name = shift;
188 $value = shift if @ARGV;
189
190 }
191 else {
192 die "Command '$action' not recognised.\n\n".$usage;
193 }
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200194 }
195}
196
Simon Butcher3d265132016-06-23 21:57:06 +0100197# If no command was specified, exit...
198if ( not defined($action) ){ die $usage; }
199
SimonB60f2cf92016-04-03 14:16:08 +0100200# Check the config file is present
201if (! -f $config_file) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200202
SimonB60f2cf92016-04-03 14:16:08 +0100203 chdir '..' or die;
204
205 # Confirm this is the project root directory and try again
206 if ( !(-d 'scripts' && -d 'include' && -d 'library' && -f $config_file) ) {
207 die "If no file specified, must be run from the project root or scripts directory.\n";
208 }
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200209}
SimonB60f2cf92016-04-03 14:16:08 +0100210
211
212# Now read the file and process the contents
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200213
214open my $config_read, '<', $config_file or die "read $config_file: $!\n";
215my @config_lines = <$config_read>;
216close $config_read;
217
Azim Khan0d445732017-12-21 09:28:39 +0000218# Add required baremetal symbols to the list that is included.
219if ( $action eq "baremetal" ) {
220 @non_excluded = ( @non_excluded, @non_excluded_baremetal );
221}
222
223my ($exclude_re, $no_exclude_re, $exclude_baremetal_re);
Manuel Pégourié-Gonnard1989caf2016-01-04 12:57:32 +0100224if ($action eq "realfull") {
225 $exclude_re = qr/^$/;
226 $no_exclude_re = qr/./;
227} else {
228 $exclude_re = join '|', @excluded;
229 $no_exclude_re = join '|', @non_excluded;
230}
Azim Khan0d445732017-12-21 09:28:39 +0000231if ( $action eq "baremetal" ) {
232 $exclude_baremetal_re = join '|', @excluded_baremetal;
233}
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200234
Gilles Peskine01f57e32017-10-09 16:54:28 +0200235my $config_write = undef;
236if ($action ne "get") {
237 open $config_write, '>', $config_file or die "write $config_file: $!\n";
238}
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200239
240my $done;
241for my $line (@config_lines) {
Azim Khan0d445732017-12-21 09:28:39 +0000242 if ($action eq "full" || $action eq "realfull" || $action eq "baremetal" ) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200243 if ($line =~ /name SECTION: Module configuration options/) {
244 $done = 1;
245 }
246
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +0100247 if (!$done && $line =~ m!^//\s?#define! &&
Azim Khan0d445732017-12-21 09:28:39 +0000248 ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) &&
249 ( $action ne "baremetal" || ( $line !~ /$exclude_baremetal_re/ ) ) ) {
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +0100250 $line =~ s!^//\s?!!;
251 }
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100252 if (!$done && $line =~ m!^\s?#define! &&
Azim Khan0d445732017-12-21 09:28:39 +0000253 ! ( ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) &&
254 ( $action ne "baremetal" || ( $line !~ /$exclude_baremetal_re/ ) ) ) ) {
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +0100255 $line =~ s!^!//!;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200256 }
257 } elsif ($action eq "unset") {
Manuel Pégourié-Gonnard7f9049b2015-06-23 17:42:51 +0200258 if (!$done && $line =~ /^\s*#define\s*$name\b/) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200259 $line = '//' . $line;
260 $done = 1;
261 }
262 } elsif (!$done && $action eq "set") {
Manuel Pégourié-Gonnard7f9049b2015-06-23 17:42:51 +0200263 if ($line =~ m!^(?://)?\s*#define\s*$name\b!) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200264 $line = "#define $name";
265 $line .= " $value" if defined $value && $value ne "";
266 $line .= "\n";
267 $done = 1;
268 }
Simon Butcher4ae86912016-06-21 10:09:25 +0100269 } elsif (!$done && $action eq "get") {
Gilles Peskinef0f55cc2017-10-09 16:51:24 +0200270 if ($line =~ /^\s*#define\s*$name(?:\s+(.*?))\s*(?:$|\/\*|\/\/)/) {
Simon Butcher4ae86912016-06-21 10:09:25 +0100271 $value = $1;
272 $done = 1;
273 }
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200274 }
275
Gilles Peskine01f57e32017-10-09 16:54:28 +0200276 if (defined $config_write) {
Gilles Peskine8ca0e8f2017-10-10 11:26:45 +0200277 print $config_write $line or die "write $config_file: $!\n";
Gilles Peskine01f57e32017-10-09 16:54:28 +0200278 }
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200279}
280
SimonB60f2cf92016-04-03 14:16:08 +0100281# Did the set command work?
Gilles Peskined98e9e82017-10-09 16:56:18 +0200282if ($action eq "set" && $force_option && !$done) {
SimonB60f2cf92016-04-03 14:16:08 +0100283
284 # If the force option was set, append the symbol to the end of the file
285 my $line = "#define $name";
286 $line .= " $value" if defined $value && $value ne "";
287 $line .= "\n";
288 $done = 1;
289
Gilles Peskine01f57e32017-10-09 16:54:28 +0200290 print $config_write $line or die "write $config_file: $!\n";
SimonB60f2cf92016-04-03 14:16:08 +0100291}
292
Gilles Peskine01f57e32017-10-09 16:54:28 +0200293if (defined $config_write) {
294 close $config_write or die "close $config_file: $!\n";
295}
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200296
Simon Butcherdd9895d2016-06-21 15:12:00 +0100297if ($action eq "get") {
Gilles Peskined98e9e82017-10-09 16:56:18 +0200298 if ($done) {
Simon Butcherdd9895d2016-06-21 15:12:00 +0100299 if ($value ne '') {
Gilles Peskined98e9e82017-10-09 16:56:18 +0200300 print "$value\n";
Simon Butcherdd9895d2016-06-21 15:12:00 +0100301 }
302 exit 0;
303 } else {
304 # If the symbol was not found, return an error
Gilles Peskined98e9e82017-10-09 16:56:18 +0200305 exit 1;
Simon Butcher4ae86912016-06-21 10:09:25 +0100306 }
Simon Butcher4ae86912016-06-21 10:09:25 +0100307}
308
SimonB60f2cf92016-04-03 14:16:08 +0100309if ($action eq "full" && !$done) {
310 die "Configuration section was not found in $config_file\n";
311
312}
313
314if ($action ne "full" && $action ne "unset" && !$done) {
315 die "A #define for the symbol $name was not found in $config_file\n";
316}
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200317
318__END__