2014-07-12 03:19:18 +02:00
|
|
|
#!/usr/bin/perl
|
2016-04-03 15:16:08 +02:00
|
|
|
#
|
|
|
|
# This file is part of mbed TLS (https://tls.mbed.org)
|
|
|
|
#
|
|
|
|
# Copyright (c) 2014-2016, ARM Limited, All Rights Reserved
|
|
|
|
#
|
|
|
|
# Purpose
|
|
|
|
#
|
|
|
|
# Comments and uncomments #define lines in the given header file and optionally
|
|
|
|
# sets their value. This is to provide scripting control of what preprocessor
|
|
|
|
# symbols, and therefore what build time configuration flags are set in the
|
|
|
|
# 'config.h' file.
|
|
|
|
#
|
|
|
|
# Usage: config.pl [-f <file> | --file <file>] [-o | --force]
|
|
|
|
# [set <symbol> <value> | unset <symbol> | full | realfull]
|
|
|
|
#
|
|
|
|
# Full usage description provided below.
|
|
|
|
#
|
|
|
|
# Things that shouldn't be enabled with "full".
|
|
|
|
#
|
2016-06-11 03:31:21 +02:00
|
|
|
# MBEDTLS_TEST_NULL_ENTROPY
|
2016-04-03 15:16:08 +02:00
|
|
|
# MBEDTLS_DEPRECATED_REMOVED
|
|
|
|
# MBEDTLS_HAVE_SSE2
|
|
|
|
# MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
|
|
|
|
# MBEDTLS_ECP_DP_M221_ENABLED
|
|
|
|
# MBEDTLS_ECP_DP_M383_ENABLED
|
|
|
|
# MBEDTLS_ECP_DP_M511_ENABLED
|
|
|
|
# MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
|
|
|
|
# MBEDTLS_NO_PLATFORM_ENTROPY
|
|
|
|
# MBEDTLS_REMOVE_ARC4_CIPHERSUITES
|
|
|
|
# MBEDTLS_SSL_HW_RECORD_ACCEL
|
|
|
|
# MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
|
|
|
|
# MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
|
|
|
|
# - this could be enabled if the respective tests were adapted
|
|
|
|
# MBEDTLS_ZLIB_SUPPORT
|
|
|
|
# MBEDTLS_PKCS11_C
|
|
|
|
# and any symbol beginning _ALT
|
|
|
|
#
|
2014-07-12 03:19:18 +02:00
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2016-04-03 15:16:08 +02:00
|
|
|
my $config_file = "include/mbedtls/config.h";
|
2014-07-12 03:19:18 +02:00
|
|
|
my $usage = <<EOU;
|
2016-04-03 15:16:08 +02:00
|
|
|
$0 [-f <file> | --file <file>] [-o | --force]
|
|
|
|
[set <symbol> <value> | unset <symbol> | full | realfull]
|
|
|
|
|
|
|
|
Commands
|
|
|
|
set <symbol> [<value] - Uncomments or adds a #define for the <symnol> to
|
|
|
|
the configuration file, and optionally making it
|
|
|
|
of <value>.
|
|
|
|
If the symbol isn't present in the file an error
|
|
|
|
is returned.
|
|
|
|
unset <symbol> - Comments out any #define present in the
|
|
|
|
configuration file.
|
|
|
|
full - Uncomments all #define's in the configuration file
|
|
|
|
excluding some reserved symbols, until the
|
|
|
|
'Module configuration options' section
|
|
|
|
realfull - Uncomments all #define's with no exclusions
|
|
|
|
|
|
|
|
Options
|
|
|
|
-f | --file <filename> - The file or file path for the configuration file
|
|
|
|
to edit. When omitted, the following default is
|
|
|
|
used:
|
|
|
|
$config_file
|
|
|
|
-o | --force - If the symbol isn't present in the configuration
|
|
|
|
file when setting it's value, a #define is
|
|
|
|
appended to the end of the file.
|
|
|
|
|
2014-07-12 03:19:18 +02:00
|
|
|
EOU
|
|
|
|
|
|
|
|
my @excluded = qw(
|
2016-06-11 03:31:21 +02:00
|
|
|
MBEDTLS_TEST_NULL_ENTROPY
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_DEPRECATED_REMOVED
|
|
|
|
MBEDTLS_HAVE_SSE2
|
|
|
|
MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
|
|
|
|
MBEDTLS_ECP_DP_M221_ENABLED
|
|
|
|
MBEDTLS_ECP_DP_M383_ENABLED
|
|
|
|
MBEDTLS_ECP_DP_M511_ENABLED
|
|
|
|
MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
|
|
|
|
MBEDTLS_NO_PLATFORM_ENTROPY
|
|
|
|
MBEDTLS_REMOVE_ARC4_CIPHERSUITES
|
|
|
|
MBEDTLS_SSL_HW_RECORD_ACCEL
|
|
|
|
MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
|
|
|
|
MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
|
|
|
|
MBEDTLS_ZLIB_SUPPORT
|
|
|
|
MBEDTLS_PKCS11_C
|
2014-07-12 03:19:18 +02:00
|
|
|
_ALT\s*$
|
|
|
|
);
|
|
|
|
|
2015-06-03 10:59:06 +02:00
|
|
|
# Things that should be enabled in "full" even if they match @excluded
|
|
|
|
my @non_excluded = qw(
|
|
|
|
PLATFORM_[A-Z0-9]+_ALT
|
|
|
|
);
|
|
|
|
|
2016-04-03 15:16:08 +02:00
|
|
|
# Process the command line arguments
|
2014-07-12 03:19:18 +02:00
|
|
|
|
2016-04-03 15:16:08 +02:00
|
|
|
my $force_option = 0;
|
|
|
|
|
|
|
|
my ($arg, $name, $value, $action);
|
|
|
|
|
2016-04-04 14:49:10 +02:00
|
|
|
while ($arg = shift) {
|
2016-04-03 15:16:08 +02:00
|
|
|
|
|
|
|
# Check if the argument is an option
|
2016-04-04 14:49:10 +02:00
|
|
|
if ($arg eq "-f" || $arg eq "--file") {
|
2016-04-03 15:16:08 +02:00
|
|
|
$config_file = shift;
|
|
|
|
|
|
|
|
-f $config_file or die "No such file: $config_file\n";
|
|
|
|
|
|
|
|
}
|
2016-04-04 14:49:10 +02:00
|
|
|
elsif ($arg eq "-o" || $arg eq "--force") {
|
2016-04-03 15:16:08 +02:00
|
|
|
$force_option = 1;
|
2014-07-12 03:19:18 +02:00
|
|
|
|
2016-04-03 15:16:08 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# ...else assume it's a command
|
|
|
|
$action = $arg;
|
|
|
|
|
|
|
|
if ($action eq "full" || $action eq "realfull") {
|
|
|
|
# No additional parameters
|
|
|
|
die $usage if @ARGV;
|
|
|
|
|
|
|
|
}
|
|
|
|
elsif ($action eq "unset") {
|
|
|
|
die $usage unless @ARGV;
|
|
|
|
$name = shift;
|
|
|
|
|
|
|
|
}
|
|
|
|
elsif ($action eq "set") {
|
|
|
|
die $usage unless @ARGV;
|
|
|
|
$name = shift;
|
|
|
|
$value = shift if @ARGV;
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
die "Command '$action' not recognised.\n\n".$usage;
|
|
|
|
}
|
2014-07-12 03:19:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 15:16:08 +02:00
|
|
|
# Check the config file is present
|
|
|
|
if (! -f $config_file) {
|
|
|
|
|
|
|
|
chdir '..' or die;
|
|
|
|
|
|
|
|
# Confirm this is the project root directory and try again
|
|
|
|
if ( !(-d 'scripts' && -d 'include' && -d 'library' && -f $config_file) ) {
|
|
|
|
die "If no file specified, must be run from the project root or scripts directory.\n";
|
|
|
|
}
|
2014-07-12 03:19:18 +02:00
|
|
|
}
|
2016-04-03 15:16:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Now read the file and process the contents
|
2014-07-12 03:19:18 +02:00
|
|
|
|
|
|
|
open my $config_read, '<', $config_file or die "read $config_file: $!\n";
|
|
|
|
my @config_lines = <$config_read>;
|
|
|
|
close $config_read;
|
|
|
|
|
2016-01-04 12:57:32 +01:00
|
|
|
my ($exclude_re, $no_exclude_re);
|
|
|
|
if ($action eq "realfull") {
|
|
|
|
$exclude_re = qr/^$/;
|
|
|
|
$no_exclude_re = qr/./;
|
|
|
|
} else {
|
|
|
|
$exclude_re = join '|', @excluded;
|
|
|
|
$no_exclude_re = join '|', @non_excluded;
|
|
|
|
}
|
2014-07-12 03:19:18 +02:00
|
|
|
|
|
|
|
open my $config_write, '>', $config_file or die "write $config_file: $!\n";
|
|
|
|
|
|
|
|
my $done;
|
|
|
|
for my $line (@config_lines) {
|
2016-01-04 12:57:32 +01:00
|
|
|
if ($action eq "full" || $action eq "realfull") {
|
2014-07-12 03:19:18 +02:00
|
|
|
if ($line =~ /name SECTION: Module configuration options/) {
|
|
|
|
$done = 1;
|
|
|
|
}
|
|
|
|
|
2015-06-03 10:59:06 +02:00
|
|
|
if (!$done && $line =~ m!^//\s?#define! &&
|
|
|
|
( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
|
2015-03-24 09:50:15 +01:00
|
|
|
$line =~ s!^//\s?!!;
|
|
|
|
}
|
2015-06-03 11:33:55 +02:00
|
|
|
if (!$done && $line =~ m!^\s?#define! &&
|
|
|
|
! ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
|
2015-03-24 09:50:15 +01:00
|
|
|
$line =~ s!^!//!;
|
2014-07-12 03:19:18 +02:00
|
|
|
}
|
|
|
|
} elsif ($action eq "unset") {
|
2015-06-23 17:42:51 +02:00
|
|
|
if (!$done && $line =~ /^\s*#define\s*$name\b/) {
|
2014-07-12 03:19:18 +02:00
|
|
|
$line = '//' . $line;
|
|
|
|
$done = 1;
|
|
|
|
}
|
|
|
|
} elsif (!$done && $action eq "set") {
|
2015-06-23 17:42:51 +02:00
|
|
|
if ($line =~ m!^(?://)?\s*#define\s*$name\b!) {
|
2014-07-12 03:19:18 +02:00
|
|
|
$line = "#define $name";
|
|
|
|
$line .= " $value" if defined $value && $value ne "";
|
|
|
|
$line .= "\n";
|
|
|
|
$done = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print $config_write $line;
|
|
|
|
}
|
|
|
|
|
2016-04-03 15:16:08 +02:00
|
|
|
# Did the set command work?
|
|
|
|
if ($action eq "set"&& $force_option && !$done) {
|
|
|
|
|
|
|
|
# If the force option was set, append the symbol to the end of the file
|
|
|
|
my $line = "#define $name";
|
|
|
|
$line .= " $value" if defined $value && $value ne "";
|
|
|
|
$line .= "\n";
|
|
|
|
$done = 1;
|
|
|
|
|
|
|
|
print $config_write $line;
|
|
|
|
}
|
|
|
|
|
2014-07-12 03:19:18 +02:00
|
|
|
close $config_write;
|
|
|
|
|
2016-04-03 15:16:08 +02:00
|
|
|
if ($action eq "full" && !$done) {
|
|
|
|
die "Configuration section was not found in $config_file\n";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($action ne "full" && $action ne "unset" && !$done) {
|
|
|
|
die "A #define for the symbol $name was not found in $config_file\n";
|
|
|
|
}
|
2014-07-12 03:19:18 +02:00
|
|
|
|
|
|
|
__END__
|