David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 1 | // Build mcuboot as a library, based on the requested features. |
| 2 | |
Fabio Utzig | 455cad5 | 2018-10-15 14:36:33 -0700 | [diff] [blame] | 3 | extern crate cc; |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 4 | |
| 5 | use std::env; |
| 6 | use std::fs; |
| 7 | use std::io; |
| 8 | use std::path::Path; |
| 9 | |
| 10 | fn 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(); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 15 | let validate_primary_slot = |
| 16 | env::var("CARGO_FEATURE_VALIDATE_PRIMARY_SLOT").is_ok(); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 17 | let enc_rsa = env::var("CARGO_FEATURE_ENC_RSA").is_ok(); |
| 18 | let enc_kw = env::var("CARGO_FEATURE_ENC_KW").is_ok(); |
Fabio Utzig | 9b97b13 | 2018-12-18 17:21:51 -0200 | [diff] [blame] | 19 | let bootstrap = env::var("CARGO_FEATURE_BOOTSTRAP").is_ok(); |
David Brown | 5e6f5e0 | 2019-04-04 10:50:05 +0700 | [diff] [blame^] | 20 | let multiimage = env::var("CARGO_FEATURE_MULTIIMAGE").is_ok(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 21 | |
Fabio Utzig | 455cad5 | 2018-10-15 14:36:33 -0700 | [diff] [blame] | 22 | let mut conf = cc::Build::new(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 23 | conf.define("__BOOTSIM__", None); |
Fabio Utzig | 08fcfe9 | 2018-11-26 10:18:18 -0200 | [diff] [blame] | 24 | conf.define("MCUBOOT_HAVE_LOGGING", None); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 25 | conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None); |
Marti Bolivar | 248da08 | 2018-04-24 15:11:39 -0400 | [diff] [blame] | 26 | conf.define("MCUBOOT_HAVE_ASSERT_H", None); |
Marti Bolivar | f9bfddd | 2018-04-24 14:28:33 -0400 | [diff] [blame] | 27 | conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128")); |
David Brown | 5e6f5e0 | 2019-04-04 10:50:05 +0700 | [diff] [blame^] | 28 | conf.define("MCUBOOT_IMAGE_NUMBER", Some(if multiimage { "2" } else { "1" })); |
Fabio Utzig | ebdc969 | 2017-11-23 16:28:25 -0200 | [diff] [blame] | 29 | |
Fabio Utzig | 9b97b13 | 2018-12-18 17:21:51 -0200 | [diff] [blame] | 30 | if bootstrap { |
| 31 | conf.define("MCUBOOT_BOOTSTRAP", None); |
| 32 | } |
| 33 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 34 | if validate_primary_slot { |
| 35 | conf.define("MCUBOOT_VALIDATE_PRIMARY_SLOT", None); |
Fabio Utzig | ebdc969 | 2017-11-23 16:28:25 -0200 | [diff] [blame] | 36 | } |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 37 | |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 38 | // Currently, mbed TLS cannot build with both RSA and ECDSA. |
| 39 | if sig_rsa && sig_ecdsa { |
| 40 | panic!("mcuboot does not support RSA and ECDSA at the same time"); |
| 41 | } |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 42 | |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 43 | if sig_rsa { |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 44 | conf.define("MCUBOOT_SIGN_RSA", None); |
| 45 | conf.define("MCUBOOT_USE_MBED_TLS", None); |
| 46 | |
David Brown | 82bf7c2 | 2017-07-12 09:49:31 -0600 | [diff] [blame] | 47 | conf.include("mbedtls/include"); |
| 48 | conf.file("mbedtls/library/sha256.c"); |
Fabio Utzig | 806af0e | 2018-04-26 10:53:54 -0300 | [diff] [blame] | 49 | conf.file("csupport/keys.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 50 | |
David Brown | 82bf7c2 | 2017-07-12 09:49:31 -0600 | [diff] [blame] | 51 | conf.file("mbedtls/library/rsa.c"); |
| 52 | conf.file("mbedtls/library/bignum.c"); |
Fabio Utzig | b04afa9 | 2018-09-12 15:27:04 -0300 | [diff] [blame] | 53 | conf.file("mbedtls/library/platform.c"); |
| 54 | conf.file("mbedtls/library/platform_util.c"); |
David Brown | 82bf7c2 | 2017-07-12 09:49:31 -0600 | [diff] [blame] | 55 | conf.file("mbedtls/library/asn1parse.c"); |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 56 | } else if sig_ecdsa { |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 57 | conf.define("MCUBOOT_SIGN_EC256", None); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 58 | conf.define("MCUBOOT_USE_TINYCRYPT", None); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 59 | |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 60 | if !enc_kw { |
| 61 | conf.include("../../ext/mbedtls/include"); |
| 62 | } |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 63 | conf.include("../../ext/tinycrypt/lib/include"); |
| 64 | |
Fabio Utzig | 806af0e | 2018-04-26 10:53:54 -0300 | [diff] [blame] | 65 | conf.file("csupport/keys.c"); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 66 | |
| 67 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
| 68 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 69 | conf.file("../../ext/tinycrypt/lib/source/ecc.c"); |
| 70 | conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c"); |
| 71 | conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c"); |
| 72 | |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 73 | conf.file("../../ext/mbedtls/src/platform_util.c"); |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 74 | conf.file("../../ext/mbedtls/src/asn1parse.c"); |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 75 | } else { |
Marti Bolivar | a4818a5 | 2018-04-12 13:02:38 -0400 | [diff] [blame] | 76 | // Neither signature type, only verify sha256. The default |
| 77 | // configuration file bundled with mbedTLS is sufficient. |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 78 | conf.define("MCUBOOT_USE_MBED_TLS", None); |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 79 | conf.include("mbedtls/include"); |
| 80 | conf.file("mbedtls/library/sha256.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | if overwrite_only { |
| 84 | conf.define("MCUBOOT_OVERWRITE_ONLY", None); |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 85 | conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 86 | } |
| 87 | |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 88 | if enc_rsa { |
| 89 | conf.define("MCUBOOT_ENCRYPT_RSA", None); |
| 90 | conf.define("MCUBOOT_ENC_IMAGES", None); |
| 91 | conf.define("MCUBOOT_USE_MBED_TLS", None); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 92 | |
| 93 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 94 | conf.file("csupport/keys.c"); |
| 95 | |
| 96 | conf.include("mbedtls/include"); |
| 97 | conf.file("mbedtls/library/sha256.c"); |
| 98 | |
| 99 | conf.file("mbedtls/library/platform.c"); |
| 100 | conf.file("mbedtls/library/platform_util.c"); |
| 101 | conf.file("mbedtls/library/rsa.c"); |
| 102 | conf.file("mbedtls/library/rsa_internal.c"); |
| 103 | conf.file("mbedtls/library/md.c"); |
| 104 | conf.file("mbedtls/library/md_wrap.c"); |
| 105 | conf.file("mbedtls/library/aes.c"); |
| 106 | conf.file("mbedtls/library/bignum.c"); |
| 107 | conf.file("mbedtls/library/asn1parse.c"); |
| 108 | } |
| 109 | |
| 110 | if enc_kw { |
| 111 | conf.define("MCUBOOT_ENCRYPT_KW", None); |
| 112 | conf.define("MCUBOOT_ENC_IMAGES", None); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 113 | |
| 114 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 115 | conf.file("csupport/keys.c"); |
| 116 | |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 117 | if sig_rsa { |
| 118 | conf.file("mbedtls/library/sha256.c"); |
| 119 | } |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 120 | |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 121 | /* Simulator uses Mbed-TLS to wrap keys */ |
| 122 | conf.include("mbedtls/include"); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 123 | conf.file("mbedtls/library/platform.c"); |
| 124 | conf.file("mbedtls/library/platform_util.c"); |
| 125 | conf.file("mbedtls/library/nist_kw.c"); |
| 126 | conf.file("mbedtls/library/cipher.c"); |
| 127 | conf.file("mbedtls/library/cipher_wrap.c"); |
| 128 | conf.file("mbedtls/library/aes.c"); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 129 | |
| 130 | if sig_ecdsa { |
| 131 | conf.define("MCUBOOT_USE_TINYCRYPT", None); |
| 132 | |
| 133 | conf.include("../../ext/tinycrypt/lib/include"); |
| 134 | |
| 135 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
| 136 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 137 | conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c"); |
| 138 | conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c"); |
| 139 | } |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 140 | } |
| 141 | |
Fabio Utzig | 251ef1d | 2018-12-18 17:20:19 -0200 | [diff] [blame] | 142 | if sig_rsa && enc_kw { |
| 143 | conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>")); |
| 144 | } else if sig_rsa || enc_rsa { |
Fabio Utzig | 04fd63e | 2018-12-14 06:43:31 -0200 | [diff] [blame] | 145 | conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>")); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 146 | } else if sig_ecdsa && !enc_kw { |
Fabio Utzig | 04fd63e | 2018-12-14 06:43:31 -0200 | [diff] [blame] | 147 | conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>")); |
| 148 | } else if enc_kw { |
| 149 | conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>")); |
| 150 | } |
| 151 | |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 152 | conf.file("../../boot/bootutil/src/image_validate.c"); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 153 | if sig_rsa { |
| 154 | conf.file("../../boot/bootutil/src/image_rsa.c"); |
| 155 | } else if sig_ecdsa { |
| 156 | conf.file("../../boot/bootutil/src/image_ec256.c"); |
| 157 | } |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 158 | conf.file("../../boot/bootutil/src/loader.c"); |
| 159 | conf.file("../../boot/bootutil/src/caps.c"); |
| 160 | conf.file("../../boot/bootutil/src/bootutil_misc.c"); |
David Brown | d2b1853 | 2017-07-12 09:51:31 -0600 | [diff] [blame] | 161 | conf.file("csupport/run.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 162 | conf.include("../../boot/bootutil/include"); |
Fabio Utzig | 57c40f7 | 2017-12-12 21:48:30 -0200 | [diff] [blame] | 163 | conf.include("csupport"); |
Fabio Utzig | 9a4b9ba | 2018-05-07 08:31:27 -0300 | [diff] [blame] | 164 | conf.include("../../boot/zephyr/include"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 165 | conf.debug(true); |
| 166 | conf.flag("-Wall"); |
David Brown | 0b693c0 | 2017-07-12 12:34:33 -0600 | [diff] [blame] | 167 | conf.flag("-Werror"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 168 | |
Fabio Utzig | 0bccf9d | 2017-12-07 12:13:57 -0200 | [diff] [blame] | 169 | // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90. |
| 170 | // It has incomplete std=c11 and std=c99 support but std=c99 was checked |
| 171 | // to build correctly so leaving it here to updated in the future... |
| 172 | conf.flag("-std=c99"); |
| 173 | |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 174 | conf.compile("libbootutil.a"); |
| 175 | |
| 176 | walk_dir("../../boot").unwrap(); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 177 | walk_dir("../../ext/tinycrypt/lib/source").unwrap(); |
Fabio Utzig | d32fd64 | 2017-12-18 15:19:47 -0200 | [diff] [blame] | 178 | walk_dir("../../ext/mbedtls").unwrap(); |
David Brown | d2b1853 | 2017-07-12 09:51:31 -0600 | [diff] [blame] | 179 | walk_dir("csupport").unwrap(); |
David Brown | 82bf7c2 | 2017-07-12 09:49:31 -0600 | [diff] [blame] | 180 | walk_dir("mbedtls/include").unwrap(); |
| 181 | walk_dir("mbedtls/library").unwrap(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // Output the names of all files within a directory so that Cargo knows when to rebuild. |
| 185 | fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> { |
| 186 | for ent in fs::read_dir(path.as_ref())? { |
| 187 | let ent = ent?; |
| 188 | let p = ent.path(); |
| 189 | if p.is_dir() { |
| 190 | walk_dir(p)?; |
| 191 | } else { |
| 192 | // Note that non-utf8 names will fail. |
| 193 | let name = p.to_str().unwrap(); |
| 194 | if name.ends_with(".c") || name.ends_with(".h") { |
| 195 | println!("cargo:rerun-if-changed={}", name); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | Ok(()) |
| 201 | } |