blob: 12377db1fbe83dc74399332dccb9a045e6884fd5 [file] [log] [blame]
David Brown63902772017-07-12 09:47:49 -06001// Build mcuboot as a library, based on the requested features.
2
Fabio Utzig455cad52018-10-15 14:36:33 -07003extern crate cc;
David Brown63902772017-07-12 09:47:49 -06004
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();
Fabio Utzig9b97b132018-12-18 17:21:51 -020018 let bootstrap = env::var("CARGO_FEATURE_BOOTSTRAP").is_ok();
David Brown63902772017-07-12 09:47:49 -060019
Fabio Utzig455cad52018-10-15 14:36:33 -070020 let mut conf = cc::Build::new();
David Brown63902772017-07-12 09:47:49 -060021 conf.define("__BOOTSIM__", None);
Fabio Utzig08fcfe92018-11-26 10:18:18 -020022 conf.define("MCUBOOT_HAVE_LOGGING", None);
David Brown63902772017-07-12 09:47:49 -060023 conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
Marti Bolivar248da082018-04-24 15:11:39 -040024 conf.define("MCUBOOT_HAVE_ASSERT_H", None);
Marti Bolivarf9bfddd2018-04-24 14:28:33 -040025 conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128"));
Fabio Utzigebdc9692017-11-23 16:28:25 -020026
Fabio Utzig9b97b132018-12-18 17:21:51 -020027 if bootstrap {
28 conf.define("MCUBOOT_BOOTSTRAP", None);
29 }
30
Fabio Utzigebdc9692017-11-23 16:28:25 -020031 if validate_slot0 {
32 conf.define("MCUBOOT_VALIDATE_SLOT0", None);
33 }
David Brown63902772017-07-12 09:47:49 -060034
David Brown704ac6f2017-07-12 10:14:47 -060035 // Currently, mbed TLS cannot build with both RSA and ECDSA.
36 if sig_rsa && sig_ecdsa {
37 panic!("mcuboot does not support RSA and ECDSA at the same time");
38 }
David Brown63902772017-07-12 09:47:49 -060039
David Brown704ac6f2017-07-12 10:14:47 -060040 if sig_rsa {
David Brown63902772017-07-12 09:47:49 -060041 conf.define("MCUBOOT_SIGN_RSA", None);
42 conf.define("MCUBOOT_USE_MBED_TLS", None);
43
David Brown82bf7c22017-07-12 09:49:31 -060044 conf.include("mbedtls/include");
45 conf.file("mbedtls/library/sha256.c");
Fabio Utzig806af0e2018-04-26 10:53:54 -030046 conf.file("csupport/keys.c");
David Brown63902772017-07-12 09:47:49 -060047
David Brown82bf7c22017-07-12 09:49:31 -060048 conf.file("mbedtls/library/rsa.c");
49 conf.file("mbedtls/library/bignum.c");
Fabio Utzigb04afa92018-09-12 15:27:04 -030050 conf.file("mbedtls/library/platform.c");
51 conf.file("mbedtls/library/platform_util.c");
David Brown82bf7c22017-07-12 09:49:31 -060052 conf.file("mbedtls/library/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060053 } else if sig_ecdsa {
Fabio Utzigc7865402017-12-05 08:50:52 -020054 conf.define("MCUBOOT_SIGN_EC256", None);
David Brown63902772017-07-12 09:47:49 -060055 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -020056
Fabio Utzigba05f2a2017-12-05 11:00:41 -020057 conf.include("../../ext/mbedtls/include");
Fabio Utzigc7865402017-12-05 08:50:52 -020058 conf.include("../../ext/tinycrypt/lib/include");
59
Fabio Utzig806af0e2018-04-26 10:53:54 -030060 conf.file("csupport/keys.c");
Fabio Utzigc7865402017-12-05 08:50:52 -020061
62 conf.file("../../ext/tinycrypt/lib/source/utils.c");
63 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
64 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
65 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
66 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
67
Fabio Utzigba05f2a2017-12-05 11:00:41 -020068 conf.file("../../ext/mbedtls/src/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060069 } else {
Marti Bolivara4818a52018-04-12 13:02:38 -040070 // Neither signature type, only verify sha256. The default
71 // configuration file bundled with mbedTLS is sufficient.
David Brown704ac6f2017-07-12 10:14:47 -060072 conf.define("MCUBOOT_USE_MBED_TLS", None);
David Brown704ac6f2017-07-12 10:14:47 -060073 conf.include("mbedtls/include");
74 conf.file("mbedtls/library/sha256.c");
David Brown63902772017-07-12 09:47:49 -060075 }
76
77 if overwrite_only {
78 conf.define("MCUBOOT_OVERWRITE_ONLY", None);
Fabio Utzig13d9e352017-10-05 20:32:31 -030079 conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
David Brown63902772017-07-12 09:47:49 -060080 }
81
Fabio Utzig1e48b912018-09-18 09:04:18 -030082 if enc_rsa {
83 conf.define("MCUBOOT_ENCRYPT_RSA", None);
84 conf.define("MCUBOOT_ENC_IMAGES", None);
85 conf.define("MCUBOOT_USE_MBED_TLS", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -030086
87 conf.file("../../boot/bootutil/src/encrypted.c");
88 conf.file("csupport/keys.c");
89
90 conf.include("mbedtls/include");
91 conf.file("mbedtls/library/sha256.c");
92
93 conf.file("mbedtls/library/platform.c");
94 conf.file("mbedtls/library/platform_util.c");
95 conf.file("mbedtls/library/rsa.c");
96 conf.file("mbedtls/library/rsa_internal.c");
97 conf.file("mbedtls/library/md.c");
98 conf.file("mbedtls/library/md_wrap.c");
99 conf.file("mbedtls/library/aes.c");
100 conf.file("mbedtls/library/bignum.c");
101 conf.file("mbedtls/library/asn1parse.c");
102 }
103
104 if enc_kw {
105 conf.define("MCUBOOT_ENCRYPT_KW", None);
106 conf.define("MCUBOOT_ENC_IMAGES", None);
107 conf.define("MCUBOOT_USE_MBED_TLS", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300108
109 conf.file("../../boot/bootutil/src/encrypted.c");
110 conf.file("csupport/keys.c");
111
112 conf.include("mbedtls/include");
113 conf.file("mbedtls/library/sha256.c");
114
115 conf.file("mbedtls/library/platform.c");
116 conf.file("mbedtls/library/platform_util.c");
117 conf.file("mbedtls/library/nist_kw.c");
118 conf.file("mbedtls/library/cipher.c");
119 conf.file("mbedtls/library/cipher_wrap.c");
120 conf.file("mbedtls/library/aes.c");
121 }
122
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200123 if sig_rsa && enc_kw {
124 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>"));
125 } else if sig_rsa || enc_rsa {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200126 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
127 } else if sig_ecdsa {
128 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
129 } else if enc_kw {
130 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
131 }
132
David Brown704ac6f2017-07-12 10:14:47 -0600133 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200134 if sig_rsa {
135 conf.file("../../boot/bootutil/src/image_rsa.c");
136 } else if sig_ecdsa {
137 conf.file("../../boot/bootutil/src/image_ec256.c");
138 }
David Brown63902772017-07-12 09:47:49 -0600139 conf.file("../../boot/bootutil/src/loader.c");
140 conf.file("../../boot/bootutil/src/caps.c");
141 conf.file("../../boot/bootutil/src/bootutil_misc.c");
David Brownd2b18532017-07-12 09:51:31 -0600142 conf.file("csupport/run.c");
David Brown63902772017-07-12 09:47:49 -0600143 conf.include("../../boot/bootutil/include");
Fabio Utzig57c40f72017-12-12 21:48:30 -0200144 conf.include("csupport");
Fabio Utzig9a4b9ba2018-05-07 08:31:27 -0300145 conf.include("../../boot/zephyr/include");
David Brown63902772017-07-12 09:47:49 -0600146 conf.debug(true);
147 conf.flag("-Wall");
David Brown0b693c02017-07-12 12:34:33 -0600148 conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -0600149
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200150 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
151 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
152 // to build correctly so leaving it here to updated in the future...
153 conf.flag("-std=c99");
154
David Brown63902772017-07-12 09:47:49 -0600155 conf.compile("libbootutil.a");
156
157 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -0200158 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
Fabio Utzigd32fd642017-12-18 15:19:47 -0200159 walk_dir("../../ext/mbedtls").unwrap();
David Brownd2b18532017-07-12 09:51:31 -0600160 walk_dir("csupport").unwrap();
David Brown82bf7c22017-07-12 09:49:31 -0600161 walk_dir("mbedtls/include").unwrap();
162 walk_dir("mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -0600163}
164
165// Output the names of all files within a directory so that Cargo knows when to rebuild.
166fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
167 for ent in fs::read_dir(path.as_ref())? {
168 let ent = ent?;
169 let p = ent.path();
170 if p.is_dir() {
171 walk_dir(p)?;
172 } else {
173 // Note that non-utf8 names will fail.
174 let name = p.to_str().unwrap();
175 if name.ends_with(".c") || name.ends_with(".h") {
176 println!("cargo:rerun-if-changed={}", name);
177 }
178 }
179 }
180
181 Ok(())
182}