blob: ea958f6ada7eeac8a0498322f5592b6cf3296e8f [file] [log] [blame]
David Brown63902772017-07-12 09:47:49 -06001// Build mcuboot as a library, based on the requested features.
2
3extern crate gcc;
4
5use std::env;
6use std::fs;
7use std::io;
8use std::path::Path;
9
10fn main() {
11 // Feature flags.
12 let sig_rsa = env::var("CARGO_FEATURE_SIG_RSA").is_ok();
13 let sig_ecdsa = env::var("CARGO_FEATURE_SIG_ECDSA").is_ok();
14 let overwrite_only = env::var("CARGO_FEATURE_OVERWRITE_ONLY").is_ok();
Fabio Utzigebdc9692017-11-23 16:28:25 -020015 let validate_slot0 = env::var("CARGO_FEATURE_VALIDATE_SLOT0").is_ok();
Fabio Utzig1e48b912018-09-18 09:04:18 -030016 let enc_rsa = env::var("CARGO_FEATURE_ENC_RSA").is_ok();
17 let enc_kw = env::var("CARGO_FEATURE_ENC_KW").is_ok();
David Brown63902772017-07-12 09:47:49 -060018
Fabio Utzigc7865402017-12-05 08:50:52 -020019 let mut conf = gcc::Build::new();
David Brown63902772017-07-12 09:47:49 -060020 conf.define("__BOOTSIM__", None);
21 conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
Marti Bolivar248da082018-04-24 15:11:39 -040022 conf.define("MCUBOOT_HAVE_ASSERT_H", None);
Marti Bolivarf9bfddd2018-04-24 14:28:33 -040023 conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128"));
Fabio Utzigebdc9692017-11-23 16:28:25 -020024
25 if validate_slot0 {
26 conf.define("MCUBOOT_VALIDATE_SLOT0", None);
27 }
David Brown63902772017-07-12 09:47:49 -060028
David Brown704ac6f2017-07-12 10:14:47 -060029 // Currently, mbed TLS cannot build with both RSA and ECDSA.
30 if sig_rsa && sig_ecdsa {
31 panic!("mcuboot does not support RSA and ECDSA at the same time");
32 }
David Brown63902772017-07-12 09:47:49 -060033
David Brown704ac6f2017-07-12 10:14:47 -060034 if sig_rsa {
David Brown63902772017-07-12 09:47:49 -060035 conf.define("MCUBOOT_SIGN_RSA", None);
36 conf.define("MCUBOOT_USE_MBED_TLS", None);
37
Marti Bolivara4818a52018-04-12 13:02:38 -040038 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
David Brown82bf7c22017-07-12 09:49:31 -060039 conf.include("mbedtls/include");
40 conf.file("mbedtls/library/sha256.c");
Fabio Utzig806af0e2018-04-26 10:53:54 -030041 conf.file("csupport/keys.c");
David Brown63902772017-07-12 09:47:49 -060042
David Brown82bf7c22017-07-12 09:49:31 -060043 conf.file("mbedtls/library/rsa.c");
44 conf.file("mbedtls/library/bignum.c");
Fabio Utzigb04afa92018-09-12 15:27:04 -030045 conf.file("mbedtls/library/platform.c");
46 conf.file("mbedtls/library/platform_util.c");
David Brown82bf7c22017-07-12 09:49:31 -060047 conf.file("mbedtls/library/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060048 } else if sig_ecdsa {
Fabio Utzigc7865402017-12-05 08:50:52 -020049 conf.define("MCUBOOT_SIGN_EC256", None);
David Brown63902772017-07-12 09:47:49 -060050 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -020051
Marti Bolivara4818a52018-04-12 13:02:38 -040052 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Fabio Utzigba05f2a2017-12-05 11:00:41 -020053 conf.include("../../ext/mbedtls/include");
Fabio Utzigc7865402017-12-05 08:50:52 -020054 conf.include("../../ext/tinycrypt/lib/include");
55
Fabio Utzig806af0e2018-04-26 10:53:54 -030056 conf.file("csupport/keys.c");
Fabio Utzigc7865402017-12-05 08:50:52 -020057
58 conf.file("../../ext/tinycrypt/lib/source/utils.c");
59 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
60 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
61 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
62 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
63
Fabio Utzigba05f2a2017-12-05 11:00:41 -020064 conf.file("../../ext/mbedtls/src/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060065 } else {
Marti Bolivara4818a52018-04-12 13:02:38 -040066 // Neither signature type, only verify sha256. The default
67 // configuration file bundled with mbedTLS is sufficient.
David Brown704ac6f2017-07-12 10:14:47 -060068 conf.define("MCUBOOT_USE_MBED_TLS", None);
David Brown704ac6f2017-07-12 10:14:47 -060069 conf.include("mbedtls/include");
70 conf.file("mbedtls/library/sha256.c");
David Brown63902772017-07-12 09:47:49 -060071 }
72
73 if overwrite_only {
74 conf.define("MCUBOOT_OVERWRITE_ONLY", None);
Fabio Utzig13d9e352017-10-05 20:32:31 -030075 conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
David Brown63902772017-07-12 09:47:49 -060076 }
77
Fabio Utzig1e48b912018-09-18 09:04:18 -030078 if enc_rsa {
79 conf.define("MCUBOOT_ENCRYPT_RSA", None);
80 conf.define("MCUBOOT_ENC_IMAGES", None);
81 conf.define("MCUBOOT_USE_MBED_TLS", None);
82 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
83
84 conf.file("../../boot/bootutil/src/encrypted.c");
85 conf.file("csupport/keys.c");
86
87 conf.include("mbedtls/include");
88 conf.file("mbedtls/library/sha256.c");
89
90 conf.file("mbedtls/library/platform.c");
91 conf.file("mbedtls/library/platform_util.c");
92 conf.file("mbedtls/library/rsa.c");
93 conf.file("mbedtls/library/rsa_internal.c");
94 conf.file("mbedtls/library/md.c");
95 conf.file("mbedtls/library/md_wrap.c");
96 conf.file("mbedtls/library/aes.c");
97 conf.file("mbedtls/library/bignum.c");
98 conf.file("mbedtls/library/asn1parse.c");
99 }
100
101 if enc_kw {
102 conf.define("MCUBOOT_ENCRYPT_KW", None);
103 conf.define("MCUBOOT_ENC_IMAGES", None);
104 conf.define("MCUBOOT_USE_MBED_TLS", None);
105 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
106
107 conf.file("../../boot/bootutil/src/encrypted.c");
108 conf.file("csupport/keys.c");
109
110 conf.include("mbedtls/include");
111 conf.file("mbedtls/library/sha256.c");
112
113 conf.file("mbedtls/library/platform.c");
114 conf.file("mbedtls/library/platform_util.c");
115 conf.file("mbedtls/library/nist_kw.c");
116 conf.file("mbedtls/library/cipher.c");
117 conf.file("mbedtls/library/cipher_wrap.c");
118 conf.file("mbedtls/library/aes.c");
119 }
120
David Brown704ac6f2017-07-12 10:14:47 -0600121 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200122 if sig_rsa {
123 conf.file("../../boot/bootutil/src/image_rsa.c");
124 } else if sig_ecdsa {
125 conf.file("../../boot/bootutil/src/image_ec256.c");
126 }
David Brown63902772017-07-12 09:47:49 -0600127 conf.file("../../boot/bootutil/src/loader.c");
128 conf.file("../../boot/bootutil/src/caps.c");
129 conf.file("../../boot/bootutil/src/bootutil_misc.c");
David Brownd2b18532017-07-12 09:51:31 -0600130 conf.file("csupport/run.c");
David Brown63902772017-07-12 09:47:49 -0600131 conf.include("../../boot/bootutil/include");
Fabio Utzig57c40f72017-12-12 21:48:30 -0200132 conf.include("csupport");
Fabio Utzig9a4b9ba2018-05-07 08:31:27 -0300133 conf.include("../../boot/zephyr/include");
David Brown63902772017-07-12 09:47:49 -0600134 conf.debug(true);
135 conf.flag("-Wall");
David Brown0b693c02017-07-12 12:34:33 -0600136 conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -0600137
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200138 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
139 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
140 // to build correctly so leaving it here to updated in the future...
141 conf.flag("-std=c99");
142
David Brown63902772017-07-12 09:47:49 -0600143 conf.compile("libbootutil.a");
144
145 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -0200146 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
Fabio Utzigd32fd642017-12-18 15:19:47 -0200147 walk_dir("../../ext/mbedtls").unwrap();
David Brownd2b18532017-07-12 09:51:31 -0600148 walk_dir("csupport").unwrap();
David Brown82bf7c22017-07-12 09:49:31 -0600149 walk_dir("mbedtls/include").unwrap();
150 walk_dir("mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -0600151}
152
153// Output the names of all files within a directory so that Cargo knows when to rebuild.
154fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
155 for ent in fs::read_dir(path.as_ref())? {
156 let ent = ent?;
157 let p = ent.path();
158 if p.is_dir() {
159 walk_dir(p)?;
160 } else {
161 // Note that non-utf8 names will fail.
162 let name = p.to_str().unwrap();
163 if name.ends_with(".c") || name.ends_with(".h") {
164 println!("cargo:rerun-if-changed={}", name);
165 }
166 }
167 }
168
169 Ok(())
170}