AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright 2018 Linaro Limited |
| 4 | * Author: AKASHI Takahiro |
| 5 | */ |
| 6 | |
| 7 | #include <getopt.h> |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 8 | #include <pe.h> |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 9 | #include <stdbool.h> |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 10 | #include <stdint.h> |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <linux/types.h> |
Sughosh Ganu | 079fcf2 | 2020-12-30 19:26:59 +0530 | [diff] [blame] | 15 | |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 16 | #include <sys/stat.h> |
| 17 | #include <sys/types.h> |
AKASHI Takahiro | ba21243 | 2022-02-09 19:10:39 +0900 | [diff] [blame] | 18 | #include <uuid/uuid.h> |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 19 | #include <linux/kconfig.h> |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 20 | |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 21 | #include <gnutls/gnutls.h> |
| 22 | #include <gnutls/pkcs7.h> |
| 23 | #include <gnutls/abstract.h> |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 24 | |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 25 | #include "eficapsule.h" |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 26 | |
| 27 | static const char *tool_name = "mkeficapsule"; |
| 28 | |
| 29 | efi_guid_t efi_guid_fm_capsule = EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID; |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 30 | efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID; |
| 31 | |
Sughosh Ganu | c067638 | 2022-10-21 18:16:07 +0530 | [diff] [blame] | 32 | static const char *opts_short = "g:i:I:v:p:c:m:o:dhAR"; |
Sughosh Ganu | b39405d | 2022-10-21 18:16:06 +0530 | [diff] [blame] | 33 | |
| 34 | enum { |
| 35 | CAPSULE_NORMAL_BLOB = 0, |
| 36 | CAPSULE_ACCEPT, |
| 37 | CAPSULE_REVERT, |
| 38 | } capsule_type; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 39 | |
| 40 | static struct option options[] = { |
AKASHI Takahiro | ba21243 | 2022-02-09 19:10:39 +0900 | [diff] [blame] | 41 | {"guid", required_argument, NULL, 'g'}, |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 42 | {"index", required_argument, NULL, 'i'}, |
| 43 | {"instance", required_argument, NULL, 'I'}, |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 44 | {"private-key", required_argument, NULL, 'p'}, |
| 45 | {"certificate", required_argument, NULL, 'c'}, |
| 46 | {"monotonic-count", required_argument, NULL, 'm'}, |
| 47 | {"dump-sig", no_argument, NULL, 'd'}, |
Sughosh Ganu | b39405d | 2022-10-21 18:16:06 +0530 | [diff] [blame] | 48 | {"fw-accept", no_argument, NULL, 'A'}, |
| 49 | {"fw-revert", no_argument, NULL, 'R'}, |
Sughosh Ganu | c067638 | 2022-10-21 18:16:07 +0530 | [diff] [blame] | 50 | {"capoemflag", required_argument, NULL, 'o'}, |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 51 | {"help", no_argument, NULL, 'h'}, |
| 52 | {NULL, 0, NULL, 0}, |
| 53 | }; |
| 54 | |
| 55 | static void print_usage(void) |
| 56 | { |
AKASHI Takahiro | ba21243 | 2022-02-09 19:10:39 +0900 | [diff] [blame] | 57 | fprintf(stderr, "Usage: %s [options] <image blob> <output file>\n" |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 58 | "Options:\n" |
Sughosh Ganu | 079fcf2 | 2020-12-30 19:26:59 +0530 | [diff] [blame] | 59 | |
AKASHI Takahiro | ba21243 | 2022-02-09 19:10:39 +0900 | [diff] [blame] | 60 | "\t-g, --guid <guid string> guid for image blob type\n" |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 61 | "\t-i, --index <index> update image index\n" |
| 62 | "\t-I, --instance <instance> update hardware instance\n" |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 63 | "\t-p, --private-key <privkey file> private key file\n" |
| 64 | "\t-c, --certificate <cert file> signer's certificate file\n" |
| 65 | "\t-m, --monotonic-count <count> monotonic count\n" |
| 66 | "\t-d, --dump_sig dump signature (*.p7)\n" |
Sughosh Ganu | b39405d | 2022-10-21 18:16:06 +0530 | [diff] [blame] | 67 | "\t-A, --fw-accept firmware accept capsule, requires GUID, no image blob\n" |
| 68 | "\t-R, --fw-revert firmware revert capsule, takes no GUID, no image blob\n" |
Sughosh Ganu | c067638 | 2022-10-21 18:16:07 +0530 | [diff] [blame] | 69 | "\t-o, --capoemflag Capsule OEM Flag, an integer between 0x0000 and 0xffff\n" |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 70 | "\t-h, --help print a help message\n", |
| 71 | tool_name); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 72 | } |
| 73 | |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 74 | /** |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 75 | * auth_context - authentication context |
| 76 | * @key_file: Path to a private key file |
| 77 | * @cert_file: Path to a certificate file |
| 78 | * @image_data: Pointer to firmware data |
| 79 | * @image_size: Size of firmware data |
| 80 | * @auth: Authentication header |
| 81 | * @sig_data: Signature data |
| 82 | * @sig_size: Size of signature data |
| 83 | * |
| 84 | * Data structure used in create_auth_data(). @key_file through |
| 85 | * @image_size are input parameters. @auth, @sig_data and @sig_size |
| 86 | * are filled in by create_auth_data(). |
| 87 | */ |
| 88 | struct auth_context { |
| 89 | char *key_file; |
| 90 | char *cert_file; |
| 91 | uint8_t *image_data; |
| 92 | size_t image_size; |
| 93 | struct efi_firmware_image_authentication auth; |
| 94 | uint8_t *sig_data; |
| 95 | size_t sig_size; |
| 96 | }; |
| 97 | |
| 98 | static int dump_sig; |
| 99 | |
| 100 | /** |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 101 | * read_bin_file - read a firmware binary file |
| 102 | * @bin: Path to a firmware binary file |
| 103 | * @data: Pointer to pointer of allocated buffer |
| 104 | * @bin_size: Size of allocated buffer |
| 105 | * |
| 106 | * Read out a content of binary, @bin, into @data. |
| 107 | * A caller should free @data. |
| 108 | * |
| 109 | * Return: |
| 110 | * * 0 - on success |
| 111 | * * -1 - on failure |
| 112 | */ |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 113 | static int read_bin_file(char *bin, uint8_t **data, off_t *bin_size) |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 114 | { |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 115 | FILE *g; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 116 | struct stat bin_stat; |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 117 | void *buf; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 118 | size_t size; |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 119 | int ret = 0; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 120 | |
| 121 | g = fopen(bin, "r"); |
| 122 | if (!g) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame] | 123 | fprintf(stderr, "cannot open %s\n", bin); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 124 | return -1; |
| 125 | } |
| 126 | if (stat(bin, &bin_stat) < 0) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame] | 127 | fprintf(stderr, "cannot determine the size of %s\n", bin); |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 128 | ret = -1; |
| 129 | goto err; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 130 | } |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 131 | if (bin_stat.st_size > SIZE_MAX) { |
| 132 | fprintf(stderr, "file size is too large for malloc: %s\n", bin); |
| 133 | ret = -1; |
| 134 | goto err; |
| 135 | } |
| 136 | buf = malloc(bin_stat.st_size); |
| 137 | if (!buf) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame] | 138 | fprintf(stderr, "cannot allocate memory: %zx\n", |
| 139 | (size_t)bin_stat.st_size); |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 140 | ret = -1; |
| 141 | goto err; |
| 142 | } |
| 143 | |
| 144 | size = fread(buf, 1, bin_stat.st_size, g); |
| 145 | if (size < bin_stat.st_size) { |
| 146 | fprintf(stderr, "read failed (%zx)\n", size); |
| 147 | ret = -1; |
| 148 | goto err; |
| 149 | } |
| 150 | |
| 151 | *data = buf; |
| 152 | *bin_size = bin_stat.st_size; |
| 153 | err: |
| 154 | fclose(g); |
| 155 | |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * write_capsule_file - write a capsule file |
| 161 | * @bin: FILE stream |
| 162 | * @data: Pointer to data |
| 163 | * @bin_size: Size of data |
| 164 | * |
| 165 | * Write out data, @data, with the size @bin_size. |
| 166 | * |
| 167 | * Return: |
| 168 | * * 0 - on success |
| 169 | * * -1 - on failure |
| 170 | */ |
| 171 | static int write_capsule_file(FILE *f, void *data, size_t size, const char *msg) |
| 172 | { |
| 173 | size_t size_written; |
| 174 | |
| 175 | size_written = fwrite(data, 1, size, f); |
| 176 | if (size_written < size) { |
| 177 | fprintf(stderr, "%s: write failed (%zx != %zx)\n", msg, |
| 178 | size_written, size); |
| 179 | return -1; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 180 | } |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 181 | |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * create_auth_data - compose authentication data in capsule |
| 187 | * @auth_context: Pointer to authentication context |
| 188 | * |
| 189 | * Fill up an authentication header (.auth) and signature data (.sig_data) |
| 190 | * in @auth_context, using library functions from openssl. |
| 191 | * All the parameters in @auth_context must be filled in by a caller. |
| 192 | * |
| 193 | * Return: |
| 194 | * * 0 - on success |
| 195 | * * -1 - on failure |
| 196 | */ |
| 197 | static int create_auth_data(struct auth_context *ctx) |
| 198 | { |
| 199 | gnutls_datum_t cert; |
| 200 | gnutls_datum_t key; |
| 201 | off_t file_size; |
| 202 | gnutls_privkey_t pkey; |
| 203 | gnutls_x509_crt_t x509; |
| 204 | gnutls_pkcs7_t pkcs7; |
| 205 | gnutls_datum_t data; |
| 206 | gnutls_datum_t signature; |
| 207 | int ret; |
| 208 | |
| 209 | ret = read_bin_file(ctx->cert_file, &cert.data, &file_size); |
| 210 | if (ret < 0) |
| 211 | return -1; |
| 212 | if (file_size > UINT_MAX) |
| 213 | return -1; |
| 214 | cert.size = file_size; |
| 215 | |
| 216 | ret = read_bin_file(ctx->key_file, &key.data, &file_size); |
| 217 | if (ret < 0) |
| 218 | return -1; |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 219 | if (file_size > UINT_MAX) |
| 220 | return -1; |
| 221 | key.size = file_size; |
| 222 | |
| 223 | /* |
| 224 | * For debugging, |
| 225 | * gnutls_global_set_time_function(mytime); |
| 226 | * gnutls_global_set_log_function(tls_log_func); |
| 227 | * gnutls_global_set_log_level(6); |
| 228 | */ |
| 229 | |
| 230 | ret = gnutls_privkey_init(&pkey); |
| 231 | if (ret < 0) { |
| 232 | fprintf(stderr, "error in gnutls_privkey_init(): %s\n", |
| 233 | gnutls_strerror(ret)); |
| 234 | return -1; |
| 235 | } |
| 236 | |
| 237 | ret = gnutls_x509_crt_init(&x509); |
| 238 | if (ret < 0) { |
| 239 | fprintf(stderr, "error in gnutls_x509_crt_init(): %s\n", |
| 240 | gnutls_strerror(ret)); |
| 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | /* load a private key */ |
| 245 | ret = gnutls_privkey_import_x509_raw(pkey, &key, GNUTLS_X509_FMT_PEM, |
| 246 | 0, 0); |
| 247 | if (ret < 0) { |
| 248 | fprintf(stderr, |
| 249 | "error in gnutls_privkey_import_x509_raw(): %s\n", |
| 250 | gnutls_strerror(ret)); |
| 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | /* load x509 certificate */ |
| 255 | ret = gnutls_x509_crt_import(x509, &cert, GNUTLS_X509_FMT_PEM); |
| 256 | if (ret < 0) { |
| 257 | fprintf(stderr, "error in gnutls_x509_crt_import(): %s\n", |
| 258 | gnutls_strerror(ret)); |
| 259 | return -1; |
| 260 | } |
| 261 | |
| 262 | /* generate a PKCS #7 structure */ |
| 263 | ret = gnutls_pkcs7_init(&pkcs7); |
| 264 | if (ret < 0) { |
| 265 | fprintf(stderr, "error in gnutls_pkcs7_init(): %s\n", |
| 266 | gnutls_strerror(ret)); |
| 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | /* sign */ |
| 271 | /* |
| 272 | * Data should have |
| 273 | * * firmware image |
| 274 | * * monotonic count |
| 275 | * in this order! |
| 276 | * See EDK2's FmpAuthenticatedHandlerRsa2048Sha256() |
| 277 | */ |
| 278 | data.size = ctx->image_size + sizeof(ctx->auth.monotonic_count); |
| 279 | data.data = malloc(data.size); |
| 280 | if (!data.data) { |
| 281 | fprintf(stderr, "allocating memory (0x%x) failed\n", data.size); |
| 282 | return -1; |
| 283 | } |
| 284 | memcpy(data.data, ctx->image_data, ctx->image_size); |
| 285 | memcpy(data.data + ctx->image_size, &ctx->auth.monotonic_count, |
| 286 | sizeof(ctx->auth.monotonic_count)); |
| 287 | |
| 288 | ret = gnutls_pkcs7_sign(pkcs7, x509, pkey, &data, NULL, NULL, |
| 289 | GNUTLS_DIG_SHA256, |
| 290 | /* GNUTLS_PKCS7_EMBED_DATA? */ |
| 291 | GNUTLS_PKCS7_INCLUDE_CERT | |
| 292 | GNUTLS_PKCS7_INCLUDE_TIME); |
| 293 | if (ret < 0) { |
| 294 | fprintf(stderr, "error in gnutls_pkcs7)sign(): %s\n", |
| 295 | gnutls_strerror(ret)); |
| 296 | return -1; |
| 297 | } |
| 298 | |
| 299 | /* export */ |
| 300 | ret = gnutls_pkcs7_export2(pkcs7, GNUTLS_X509_FMT_DER, &signature); |
| 301 | if (ret < 0) { |
| 302 | fprintf(stderr, "error in gnutls_pkcs7_export2: %s\n", |
| 303 | gnutls_strerror(ret)); |
| 304 | return -1; |
| 305 | } |
| 306 | ctx->sig_data = signature.data; |
| 307 | ctx->sig_size = signature.size; |
| 308 | |
| 309 | /* fill auth_info */ |
| 310 | ctx->auth.auth_info.hdr.dwLength = sizeof(ctx->auth.auth_info) |
| 311 | + ctx->sig_size; |
| 312 | ctx->auth.auth_info.hdr.wRevision = WIN_CERT_REVISION_2_0; |
| 313 | ctx->auth.auth_info.hdr.wCertificateType = WIN_CERT_TYPE_EFI_GUID; |
| 314 | memcpy(&ctx->auth.auth_info.cert_type, &efi_guid_cert_type_pkcs7, |
| 315 | sizeof(efi_guid_cert_type_pkcs7)); |
| 316 | |
| 317 | /* |
| 318 | * For better clean-ups, |
| 319 | * gnutls_pkcs7_deinit(pkcs7); |
| 320 | * gnutls_privkey_deinit(pkey); |
| 321 | * gnutls_x509_crt_deinit(x509); |
| 322 | * free(cert.data); |
| 323 | * free(key.data); |
| 324 | * if error |
| 325 | * gnutls_free(signature.data); |
| 326 | */ |
| 327 | |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /** |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 332 | * dump_signature - dump out a signature |
| 333 | * @path: Path to a capsule file |
| 334 | * @signature: Signature data |
| 335 | * @sig_size: Size of signature data |
| 336 | * |
| 337 | * Signature data pointed to by @signature will be saved into |
| 338 | * a file whose file name is @path with ".p7" suffix. |
| 339 | * |
| 340 | * Return: |
| 341 | * * 0 - on success |
| 342 | * * -1 - on failure |
| 343 | */ |
| 344 | static int dump_signature(const char *path, uint8_t *signature, size_t sig_size) |
| 345 | { |
| 346 | char *sig_path; |
| 347 | FILE *f; |
| 348 | size_t size; |
| 349 | int ret = -1; |
| 350 | |
| 351 | sig_path = malloc(strlen(path) + 3 + 1); |
| 352 | if (!sig_path) |
| 353 | return ret; |
| 354 | |
| 355 | sprintf(sig_path, "%s.p7", path); |
| 356 | f = fopen(sig_path, "w"); |
| 357 | if (!f) |
| 358 | goto err; |
| 359 | |
| 360 | size = fwrite(signature, 1, sig_size, f); |
| 361 | if (size == sig_size) |
| 362 | ret = 0; |
| 363 | |
| 364 | fclose(f); |
| 365 | err: |
| 366 | free(sig_path); |
| 367 | return ret; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * free_sig_data - free out signature data |
| 372 | * @ctx: Pointer to authentication context |
| 373 | * |
| 374 | * Free signature data allocated in create_auth_data(). |
| 375 | */ |
| 376 | static void free_sig_data(struct auth_context *ctx) |
| 377 | { |
| 378 | if (ctx->sig_size) |
| 379 | gnutls_free(ctx->sig_data); |
| 380 | } |
| 381 | |
| 382 | /** |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 383 | * create_fwbin - create an uefi capsule file |
| 384 | * @path: Path to a created capsule file |
| 385 | * @bin: Path to a firmware binary to encapsulate |
| 386 | * @guid: GUID of related FMP driver |
| 387 | * @index: Index number in capsule |
| 388 | * @instance: Instance number in capsule |
| 389 | * @mcount: Monotonic count in authentication information |
| 390 | * @private_file: Path to a private key file |
| 391 | * @cert_file: Path to a certificate file |
Sughosh Ganu | c067638 | 2022-10-21 18:16:07 +0530 | [diff] [blame] | 392 | * @oemflags: Capsule OEM Flags, bits 0-15 |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 393 | * |
| 394 | * This function actually does the job of creating an uefi capsule file. |
| 395 | * All the arguments must be supplied. |
| 396 | * If either @private_file ror @cert_file is NULL, the capsule file |
| 397 | * won't be signed. |
| 398 | * |
| 399 | * Return: |
| 400 | * * 0 - on success |
| 401 | * * -1 - on failure |
| 402 | */ |
| 403 | static int create_fwbin(char *path, char *bin, efi_guid_t *guid, |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 404 | unsigned long index, unsigned long instance, |
Sughosh Ganu | c067638 | 2022-10-21 18:16:07 +0530 | [diff] [blame] | 405 | uint64_t mcount, char *privkey_file, char *cert_file, |
| 406 | uint16_t oemflags) |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 407 | { |
| 408 | struct efi_capsule_header header; |
| 409 | struct efi_firmware_management_capsule_header capsule; |
| 410 | struct efi_firmware_management_capsule_image_header image; |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 411 | struct auth_context auth_context; |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 412 | FILE *f; |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 413 | uint8_t *data; |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 414 | off_t bin_size; |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 415 | uint64_t offset; |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 416 | int ret; |
| 417 | |
| 418 | #ifdef DEBUG |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 419 | fprintf(stderr, "For output: %s\n", path); |
| 420 | fprintf(stderr, "\tbin: %s\n\ttype: %pUl\n", bin, guid); |
| 421 | fprintf(stderr, "\tindex: %lu\n\tinstance: %lu\n", index, instance); |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 422 | #endif |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 423 | auth_context.sig_size = 0; |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 424 | f = NULL; |
| 425 | data = NULL; |
| 426 | ret = -1; |
| 427 | |
| 428 | /* |
| 429 | * read a firmware binary |
| 430 | */ |
| 431 | if (read_bin_file(bin, &data, &bin_size)) |
| 432 | goto err; |
| 433 | |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 434 | /* first, calculate signature to determine its size */ |
| 435 | if (privkey_file && cert_file) { |
| 436 | auth_context.key_file = privkey_file; |
| 437 | auth_context.cert_file = cert_file; |
| 438 | auth_context.auth.monotonic_count = mcount; |
| 439 | auth_context.image_data = data; |
| 440 | auth_context.image_size = bin_size; |
| 441 | |
| 442 | if (create_auth_data(&auth_context)) { |
| 443 | fprintf(stderr, "Signing firmware image failed\n"); |
| 444 | goto err; |
| 445 | } |
| 446 | |
| 447 | if (dump_sig && |
| 448 | dump_signature(path, auth_context.sig_data, |
| 449 | auth_context.sig_size)) { |
| 450 | fprintf(stderr, "Creating signature file failed\n"); |
| 451 | goto err; |
| 452 | } |
| 453 | } |
| 454 | |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 455 | /* |
| 456 | * write a capsule file |
| 457 | */ |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 458 | f = fopen(path, "w"); |
| 459 | if (!f) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame] | 460 | fprintf(stderr, "cannot open %s\n", path); |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 461 | goto err; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 462 | } |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 463 | |
| 464 | /* |
| 465 | * capsule file header |
| 466 | */ |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 467 | header.capsule_guid = efi_guid_fm_capsule; |
| 468 | header.header_size = sizeof(header); |
AKASHI Takahiro | 0f626ce | 2020-11-30 18:12:16 +0900 | [diff] [blame] | 469 | /* TODO: The current implementation ignores flags */ |
| 470 | header.flags = CAPSULE_FLAGS_PERSIST_ACROSS_RESET; |
Sughosh Ganu | c067638 | 2022-10-21 18:16:07 +0530 | [diff] [blame] | 471 | if (oemflags) |
| 472 | header.flags |= oemflags; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 473 | header.capsule_image_size = sizeof(header) |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 474 | + sizeof(capsule) + sizeof(uint64_t) |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 475 | + sizeof(image) |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 476 | + bin_size; |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 477 | if (auth_context.sig_size) |
| 478 | header.capsule_image_size += sizeof(auth_context.auth) |
| 479 | + auth_context.sig_size; |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 480 | if (write_capsule_file(f, &header, sizeof(header), |
| 481 | "Capsule header")) |
| 482 | goto err; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 483 | |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 484 | /* |
| 485 | * firmware capsule header |
| 486 | * This capsule has only one firmware capsule image. |
| 487 | */ |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 488 | capsule.version = 0x00000001; |
| 489 | capsule.embedded_driver_count = 0; |
| 490 | capsule.payload_item_count = 1; |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 491 | if (write_capsule_file(f, &capsule, sizeof(capsule), |
| 492 | "Firmware capsule header")) |
| 493 | goto err; |
| 494 | |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 495 | offset = sizeof(capsule) + sizeof(uint64_t); |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 496 | if (write_capsule_file(f, &offset, sizeof(offset), |
| 497 | "Offset to capsule image")) |
| 498 | goto err; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 499 | |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 500 | /* |
| 501 | * firmware capsule image header |
| 502 | */ |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 503 | image.version = 0x00000003; |
| 504 | memcpy(&image.update_image_type_id, guid, sizeof(*guid)); |
| 505 | image.update_image_index = index; |
AKASHI Takahiro | a4c14aa | 2021-01-22 10:43:49 +0900 | [diff] [blame] | 506 | image.reserved[0] = 0; |
| 507 | image.reserved[1] = 0; |
| 508 | image.reserved[2] = 0; |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 509 | image.update_image_size = bin_size; |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 510 | if (auth_context.sig_size) |
| 511 | image.update_image_size += sizeof(auth_context.auth) |
| 512 | + auth_context.sig_size; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 513 | image.update_vendor_code_size = 0; /* none */ |
| 514 | image.update_hardware_instance = instance; |
| 515 | image.image_capsule_support = 0; |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 516 | if (auth_context.sig_size) |
| 517 | image.image_capsule_support |= CAPSULE_SUPPORT_AUTHENTICATION; |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 518 | if (write_capsule_file(f, &image, sizeof(image), |
| 519 | "Firmware capsule image header")) |
| 520 | goto err; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 521 | |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 522 | /* |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 523 | * signature |
| 524 | */ |
| 525 | if (auth_context.sig_size) { |
| 526 | if (write_capsule_file(f, &auth_context.auth, |
| 527 | sizeof(auth_context.auth), |
| 528 | "Authentication header")) |
| 529 | goto err; |
| 530 | |
| 531 | if (write_capsule_file(f, auth_context.sig_data, |
| 532 | auth_context.sig_size, "Signature")) |
| 533 | goto err; |
| 534 | } |
| 535 | |
| 536 | /* |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 537 | * firmware binary |
| 538 | */ |
| 539 | if (write_capsule_file(f, data, bin_size, "Firmware binary")) |
| 540 | goto err; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 541 | |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 542 | ret = 0; |
| 543 | err: |
| 544 | if (f) |
| 545 | fclose(f); |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 546 | free_sig_data(&auth_context); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 547 | free(data); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 548 | |
AKASHI Takahiro | 30fcea2 | 2022-01-18 13:39:45 +0900 | [diff] [blame] | 549 | return ret; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 550 | } |
| 551 | |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 552 | /** |
AKASHI Takahiro | ba21243 | 2022-02-09 19:10:39 +0900 | [diff] [blame] | 553 | * convert_uuid_to_guid() - convert UUID to GUID |
| 554 | * @buf: UUID binary |
| 555 | * |
| 556 | * UUID and GUID have the same data structure, but their binary |
| 557 | * formats are different due to the endianness. See lib/uuid.c. |
| 558 | * Since uuid_parse() can handle only UUID, this function must |
| 559 | * be called to get correct data for GUID when parsing a string. |
| 560 | * |
| 561 | * The correct data will be returned in @buf. |
| 562 | */ |
| 563 | void convert_uuid_to_guid(unsigned char *buf) |
| 564 | { |
| 565 | unsigned char c; |
| 566 | |
| 567 | c = buf[0]; |
| 568 | buf[0] = buf[3]; |
| 569 | buf[3] = c; |
| 570 | c = buf[1]; |
| 571 | buf[1] = buf[2]; |
| 572 | buf[2] = c; |
| 573 | |
| 574 | c = buf[4]; |
| 575 | buf[4] = buf[5]; |
| 576 | buf[5] = c; |
| 577 | |
| 578 | c = buf[6]; |
| 579 | buf[6] = buf[7]; |
| 580 | buf[7] = c; |
| 581 | } |
| 582 | |
Sughosh Ganu | b39405d | 2022-10-21 18:16:06 +0530 | [diff] [blame] | 583 | static int create_empty_capsule(char *path, efi_guid_t *guid, bool fw_accept) |
| 584 | { |
| 585 | struct efi_capsule_header header = { 0 }; |
| 586 | FILE *f = NULL; |
| 587 | int ret = -1; |
| 588 | efi_guid_t fw_accept_guid = FW_ACCEPT_OS_GUID; |
| 589 | efi_guid_t fw_revert_guid = FW_REVERT_OS_GUID; |
| 590 | efi_guid_t capsule_guid; |
| 591 | |
| 592 | f = fopen(path, "w"); |
| 593 | if (!f) { |
| 594 | fprintf(stderr, "cannot open %s\n", path); |
| 595 | goto err; |
| 596 | } |
| 597 | |
| 598 | capsule_guid = fw_accept ? fw_accept_guid : fw_revert_guid; |
| 599 | |
| 600 | memcpy(&header.capsule_guid, &capsule_guid, sizeof(efi_guid_t)); |
| 601 | header.header_size = sizeof(header); |
| 602 | header.flags = 0; |
| 603 | |
| 604 | header.capsule_image_size = fw_accept ? |
| 605 | sizeof(header) + sizeof(efi_guid_t) : sizeof(header); |
| 606 | |
| 607 | if (write_capsule_file(f, &header, sizeof(header), |
| 608 | "Capsule header")) |
| 609 | goto err; |
| 610 | |
| 611 | if (fw_accept) { |
| 612 | if (write_capsule_file(f, guid, sizeof(*guid), |
| 613 | "FW Accept Capsule Payload")) |
| 614 | goto err; |
| 615 | } |
| 616 | |
| 617 | ret = 0; |
| 618 | |
| 619 | err: |
| 620 | if (f) |
| 621 | fclose(f); |
| 622 | |
| 623 | return ret; |
| 624 | } |
| 625 | |
AKASHI Takahiro | ba21243 | 2022-02-09 19:10:39 +0900 | [diff] [blame] | 626 | /** |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 627 | * main - main entry function of mkeficapsule |
| 628 | * @argc: Number of arguments |
| 629 | * @argv: Array of pointers to arguments |
| 630 | * |
| 631 | * Create an uefi capsule file, optionally signing it. |
| 632 | * Parse all the arguments and pass them on to create_fwbin(). |
| 633 | * |
| 634 | * Return: |
| 635 | * * 0 - on success |
| 636 | * * -1 - on failure |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 637 | */ |
| 638 | int main(int argc, char **argv) |
| 639 | { |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 640 | efi_guid_t *guid; |
AKASHI Takahiro | ba21243 | 2022-02-09 19:10:39 +0900 | [diff] [blame] | 641 | unsigned char uuid_buf[16]; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 642 | unsigned long index, instance; |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 643 | uint64_t mcount; |
Sughosh Ganu | c067638 | 2022-10-21 18:16:07 +0530 | [diff] [blame] | 644 | unsigned long oemflags; |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 645 | char *privkey_file, *cert_file; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 646 | int c, idx; |
| 647 | |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 648 | guid = NULL; |
| 649 | index = 0; |
| 650 | instance = 0; |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 651 | mcount = 0; |
| 652 | privkey_file = NULL; |
| 653 | cert_file = NULL; |
| 654 | dump_sig = 0; |
Sughosh Ganu | b39405d | 2022-10-21 18:16:06 +0530 | [diff] [blame] | 655 | capsule_type = CAPSULE_NORMAL_BLOB; |
Sughosh Ganu | c067638 | 2022-10-21 18:16:07 +0530 | [diff] [blame] | 656 | oemflags = 0; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 657 | for (;;) { |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 658 | c = getopt_long(argc, argv, opts_short, options, &idx); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 659 | if (c == -1) |
| 660 | break; |
| 661 | |
| 662 | switch (c) { |
AKASHI Takahiro | ba21243 | 2022-02-09 19:10:39 +0900 | [diff] [blame] | 663 | case 'g': |
| 664 | if (guid) { |
| 665 | fprintf(stderr, |
| 666 | "Image type already specified\n"); |
| 667 | exit(EXIT_FAILURE); |
| 668 | } |
| 669 | if (uuid_parse(optarg, uuid_buf)) { |
| 670 | fprintf(stderr, "Wrong guid format\n"); |
| 671 | exit(EXIT_FAILURE); |
| 672 | } |
| 673 | convert_uuid_to_guid(uuid_buf); |
| 674 | guid = (efi_guid_t *)uuid_buf; |
| 675 | break; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 676 | case 'i': |
| 677 | index = strtoul(optarg, NULL, 0); |
| 678 | break; |
| 679 | case 'I': |
| 680 | instance = strtoul(optarg, NULL, 0); |
| 681 | break; |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 682 | case 'p': |
| 683 | if (privkey_file) { |
| 684 | fprintf(stderr, |
| 685 | "Private Key already specified\n"); |
| 686 | exit(EXIT_FAILURE); |
| 687 | } |
| 688 | privkey_file = optarg; |
| 689 | break; |
| 690 | case 'c': |
| 691 | if (cert_file) { |
| 692 | fprintf(stderr, |
| 693 | "Certificate file already specified\n"); |
| 694 | exit(EXIT_FAILURE); |
| 695 | } |
| 696 | cert_file = optarg; |
| 697 | break; |
| 698 | case 'm': |
| 699 | mcount = strtoul(optarg, NULL, 0); |
| 700 | break; |
| 701 | case 'd': |
| 702 | dump_sig = 1; |
| 703 | break; |
Sughosh Ganu | b39405d | 2022-10-21 18:16:06 +0530 | [diff] [blame] | 704 | case 'A': |
| 705 | if (capsule_type) { |
| 706 | fprintf(stderr, |
| 707 | "Select either of Accept or Revert capsule generation\n"); |
| 708 | exit(1); |
| 709 | } |
| 710 | capsule_type = CAPSULE_ACCEPT; |
| 711 | break; |
| 712 | case 'R': |
| 713 | if (capsule_type) { |
| 714 | fprintf(stderr, |
| 715 | "Select either of Accept or Revert capsule generation\n"); |
| 716 | exit(1); |
| 717 | } |
| 718 | capsule_type = CAPSULE_REVERT; |
| 719 | break; |
Sughosh Ganu | c067638 | 2022-10-21 18:16:07 +0530 | [diff] [blame] | 720 | case 'o': |
| 721 | oemflags = strtoul(optarg, NULL, 0); |
| 722 | if (oemflags > 0xffff) { |
| 723 | fprintf(stderr, |
| 724 | "oemflags must be between 0x0 and 0xffff\n"); |
| 725 | exit(1); |
| 726 | } |
| 727 | break; |
Sughosh Ganu | b39405d | 2022-10-21 18:16:06 +0530 | [diff] [blame] | 728 | default: |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 729 | print_usage(); |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 730 | exit(EXIT_SUCCESS); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
AKASHI Takahiro | c246b76 | 2022-02-09 19:10:35 +0900 | [diff] [blame] | 734 | /* check necessary parameters */ |
Sughosh Ganu | b39405d | 2022-10-21 18:16:06 +0530 | [diff] [blame] | 735 | if ((capsule_type == CAPSULE_NORMAL_BLOB && |
| 736 | ((argc != optind + 2) || !guid || |
| 737 | ((privkey_file && !cert_file) || |
| 738 | (!privkey_file && cert_file)))) || |
| 739 | (capsule_type != CAPSULE_NORMAL_BLOB && |
| 740 | ((argc != optind + 1) || |
| 741 | ((capsule_type == CAPSULE_ACCEPT) && !guid) || |
| 742 | ((capsule_type == CAPSULE_REVERT) && guid)))) { |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 743 | print_usage(); |
Sughosh Ganu | 14b4420 | 2021-01-22 20:34:56 +0530 | [diff] [blame] | 744 | exit(EXIT_FAILURE); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 745 | } |
| 746 | |
Sughosh Ganu | b39405d | 2022-10-21 18:16:06 +0530 | [diff] [blame] | 747 | if (capsule_type != CAPSULE_NORMAL_BLOB) { |
| 748 | if (create_empty_capsule(argv[argc - 1], guid, |
| 749 | capsule_type == CAPSULE_ACCEPT) < 0) { |
| 750 | fprintf(stderr, "Creating empty capsule failed\n"); |
| 751 | exit(EXIT_FAILURE); |
| 752 | } |
| 753 | } else if (create_fwbin(argv[argc - 1], argv[argc - 2], guid, |
| 754 | index, instance, mcount, privkey_file, |
Sughosh Ganu | c067638 | 2022-10-21 18:16:07 +0530 | [diff] [blame] | 755 | cert_file, (uint16_t)oemflags) < 0) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame] | 756 | fprintf(stderr, "Creating firmware capsule failed\n"); |
Sughosh Ganu | 14b4420 | 2021-01-22 20:34:56 +0530 | [diff] [blame] | 757 | exit(EXIT_FAILURE); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 758 | } |
| 759 | |
Sughosh Ganu | 14b4420 | 2021-01-22 20:34:56 +0530 | [diff] [blame] | 760 | exit(EXIT_SUCCESS); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 761 | } |