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> |
| 8 | #include <malloc.h> |
| 9 | #include <stdbool.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <linux/types.h> |
Sughosh Ganu | 079fcf2 | 2020-12-30 19:26:59 +0530 | [diff] [blame] | 14 | |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 15 | #include <sys/stat.h> |
| 16 | #include <sys/types.h> |
| 17 | |
| 18 | typedef __u8 u8; |
| 19 | typedef __u16 u16; |
| 20 | typedef __u32 u32; |
| 21 | typedef __u64 u64; |
| 22 | typedef __s16 s16; |
| 23 | typedef __s32 s32; |
| 24 | |
| 25 | #define aligned_u64 __aligned_u64 |
| 26 | |
| 27 | #ifndef __packed |
| 28 | #define __packed __attribute__((packed)) |
| 29 | #endif |
| 30 | |
| 31 | #include <efi.h> |
| 32 | #include <efi_api.h> |
| 33 | |
| 34 | static const char *tool_name = "mkeficapsule"; |
| 35 | |
| 36 | efi_guid_t efi_guid_fm_capsule = EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID; |
| 37 | efi_guid_t efi_guid_image_type_uboot_fit = |
| 38 | EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID; |
| 39 | efi_guid_t efi_guid_image_type_uboot_raw = |
| 40 | EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID; |
| 41 | |
| 42 | static struct option options[] = { |
| 43 | {"fit", required_argument, NULL, 'f'}, |
| 44 | {"raw", required_argument, NULL, 'r'}, |
| 45 | {"index", required_argument, NULL, 'i'}, |
| 46 | {"instance", required_argument, NULL, 'I'}, |
| 47 | {"help", no_argument, NULL, 'h'}, |
| 48 | {NULL, 0, NULL, 0}, |
| 49 | }; |
| 50 | |
| 51 | static void print_usage(void) |
| 52 | { |
| 53 | printf("Usage: %s [options] <output file>\n" |
| 54 | "Options:\n" |
Sughosh Ganu | 079fcf2 | 2020-12-30 19:26:59 +0530 | [diff] [blame] | 55 | |
Heinrich Schuchardt | 455219f | 2021-04-08 22:02:29 +0200 | [diff] [blame] | 56 | "\t-f, --fit <fit image> new FIT image file\n" |
| 57 | "\t-r, --raw <raw image> new raw image file\n" |
| 58 | "\t-i, --index <index> update image index\n" |
| 59 | "\t-I, --instance <instance> update hardware instance\n" |
Heinrich Schuchardt | 455219f | 2021-04-08 22:02:29 +0200 | [diff] [blame] | 60 | "\t-h, --help print a help message\n", |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 61 | tool_name); |
| 62 | } |
| 63 | |
| 64 | static int create_fwbin(char *path, char *bin, efi_guid_t *guid, |
| 65 | unsigned long index, unsigned long instance) |
| 66 | { |
| 67 | struct efi_capsule_header header; |
| 68 | struct efi_firmware_management_capsule_header capsule; |
| 69 | struct efi_firmware_management_capsule_image_header image; |
| 70 | FILE *f, *g; |
| 71 | struct stat bin_stat; |
| 72 | u8 *data; |
| 73 | size_t size; |
| 74 | u64 offset; |
| 75 | |
| 76 | #ifdef DEBUG |
| 77 | printf("For output: %s\n", path); |
Klaus Heinrich Kiwi | 2748fb6 | 2021-02-20 17:40:45 -0300 | [diff] [blame] | 78 | printf("\tbin: %s\n\ttype: %pUl\n", bin, guid); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 79 | printf("\tindex: %ld\n\tinstance: %ld\n", index, instance); |
| 80 | #endif |
| 81 | |
| 82 | g = fopen(bin, "r"); |
| 83 | if (!g) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame^] | 84 | fprintf(stderr, "cannot open %s\n", bin); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 85 | return -1; |
| 86 | } |
| 87 | if (stat(bin, &bin_stat) < 0) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame^] | 88 | fprintf(stderr, "cannot determine the size of %s\n", bin); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 89 | goto err_1; |
| 90 | } |
| 91 | data = malloc(bin_stat.st_size); |
| 92 | if (!data) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame^] | 93 | fprintf(stderr, "cannot allocate memory: %zx\n", |
| 94 | (size_t)bin_stat.st_size); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 95 | goto err_1; |
| 96 | } |
| 97 | f = fopen(path, "w"); |
| 98 | if (!f) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame^] | 99 | fprintf(stderr, "cannot open %s\n", path); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 100 | goto err_2; |
| 101 | } |
| 102 | header.capsule_guid = efi_guid_fm_capsule; |
| 103 | header.header_size = sizeof(header); |
AKASHI Takahiro | 0f626ce | 2020-11-30 18:12:16 +0900 | [diff] [blame] | 104 | /* TODO: The current implementation ignores flags */ |
| 105 | header.flags = CAPSULE_FLAGS_PERSIST_ACROSS_RESET; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 106 | header.capsule_image_size = sizeof(header) |
| 107 | + sizeof(capsule) + sizeof(u64) |
| 108 | + sizeof(image) |
| 109 | + bin_stat.st_size; |
| 110 | |
| 111 | size = fwrite(&header, 1, sizeof(header), f); |
| 112 | if (size < sizeof(header)) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame^] | 113 | fprintf(stderr, "write failed (%zx)\n", size); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 114 | goto err_3; |
| 115 | } |
| 116 | |
| 117 | capsule.version = 0x00000001; |
| 118 | capsule.embedded_driver_count = 0; |
| 119 | capsule.payload_item_count = 1; |
| 120 | size = fwrite(&capsule, 1, sizeof(capsule), f); |
| 121 | if (size < (sizeof(capsule))) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame^] | 122 | fprintf(stderr, "write failed (%zx)\n", size); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 123 | goto err_3; |
| 124 | } |
| 125 | offset = sizeof(capsule) + sizeof(u64); |
| 126 | size = fwrite(&offset, 1, sizeof(offset), f); |
| 127 | if (size < sizeof(offset)) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame^] | 128 | fprintf(stderr, "write failed (%zx)\n", size); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 129 | goto err_3; |
| 130 | } |
| 131 | |
| 132 | image.version = 0x00000003; |
| 133 | memcpy(&image.update_image_type_id, guid, sizeof(*guid)); |
| 134 | image.update_image_index = index; |
AKASHI Takahiro | a4c14aa | 2021-01-22 10:43:49 +0900 | [diff] [blame] | 135 | image.reserved[0] = 0; |
| 136 | image.reserved[1] = 0; |
| 137 | image.reserved[2] = 0; |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 138 | image.update_image_size = bin_stat.st_size; |
| 139 | image.update_vendor_code_size = 0; /* none */ |
| 140 | image.update_hardware_instance = instance; |
| 141 | image.image_capsule_support = 0; |
| 142 | |
| 143 | size = fwrite(&image, 1, sizeof(image), f); |
| 144 | if (size < sizeof(image)) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame^] | 145 | fprintf(stderr, "write failed (%zx)\n", size); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 146 | goto err_3; |
| 147 | } |
| 148 | size = fread(data, 1, bin_stat.st_size, g); |
| 149 | if (size < bin_stat.st_size) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame^] | 150 | fprintf(stderr, "read failed (%zx)\n", size); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 151 | goto err_3; |
| 152 | } |
| 153 | size = fwrite(data, 1, bin_stat.st_size, f); |
| 154 | if (size < bin_stat.st_size) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame^] | 155 | fprintf(stderr, "write failed (%zx)\n", size); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 156 | goto err_3; |
| 157 | } |
| 158 | |
| 159 | fclose(f); |
| 160 | fclose(g); |
| 161 | free(data); |
| 162 | |
| 163 | return 0; |
| 164 | |
| 165 | err_3: |
| 166 | fclose(f); |
| 167 | err_2: |
| 168 | free(data); |
| 169 | err_1: |
| 170 | fclose(g); |
| 171 | |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Usage: |
| 177 | * $ mkeficapsule -f <firmware binary> <output file> |
| 178 | */ |
| 179 | int main(int argc, char **argv) |
| 180 | { |
| 181 | char *file; |
| 182 | efi_guid_t *guid; |
| 183 | unsigned long index, instance; |
| 184 | int c, idx; |
| 185 | |
| 186 | file = NULL; |
| 187 | guid = NULL; |
| 188 | index = 0; |
| 189 | instance = 0; |
| 190 | for (;;) { |
AKASHI Takahiro | 5b03cab | 2021-10-07 15:23:30 +0900 | [diff] [blame] | 191 | c = getopt_long(argc, argv, "f:r:i:I:v:h", options, &idx); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 192 | if (c == -1) |
| 193 | break; |
| 194 | |
| 195 | switch (c) { |
| 196 | case 'f': |
| 197 | if (file) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame^] | 198 | fprintf(stderr, "Image already specified\n"); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 199 | return -1; |
| 200 | } |
| 201 | file = optarg; |
| 202 | guid = &efi_guid_image_type_uboot_fit; |
| 203 | break; |
| 204 | case 'r': |
| 205 | if (file) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame^] | 206 | fprintf(stderr, "Image already specified\n"); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 207 | return -1; |
| 208 | } |
| 209 | file = optarg; |
| 210 | guid = &efi_guid_image_type_uboot_raw; |
| 211 | break; |
| 212 | case 'i': |
| 213 | index = strtoul(optarg, NULL, 0); |
| 214 | break; |
| 215 | case 'I': |
| 216 | instance = strtoul(optarg, NULL, 0); |
| 217 | break; |
| 218 | case 'h': |
| 219 | print_usage(); |
| 220 | return 0; |
| 221 | } |
| 222 | } |
| 223 | |
AKASHI Takahiro | 5b03cab | 2021-10-07 15:23:30 +0900 | [diff] [blame] | 224 | /* need an output file */ |
| 225 | if (argc != optind + 1) { |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 226 | print_usage(); |
Sughosh Ganu | 14b4420 | 2021-01-22 20:34:56 +0530 | [diff] [blame] | 227 | exit(EXIT_FAILURE); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 228 | } |
| 229 | |
AKASHI Takahiro | 5b03cab | 2021-10-07 15:23:30 +0900 | [diff] [blame] | 230 | /* need a fit image file or raw image file */ |
| 231 | if (!file) { |
| 232 | print_usage(); |
| 233 | exit(EXIT_SUCCESS); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | if (create_fwbin(argv[optind], file, guid, index, instance) |
| 237 | < 0) { |
AKASHI Takahiro | 6cf40d4 | 2022-01-18 13:39:44 +0900 | [diff] [blame^] | 238 | fprintf(stderr, "Creating firmware capsule failed\n"); |
Sughosh Ganu | 14b4420 | 2021-01-22 20:34:56 +0530 | [diff] [blame] | 239 | exit(EXIT_FAILURE); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 240 | } |
| 241 | |
Sughosh Ganu | 14b4420 | 2021-01-22 20:34:56 +0530 | [diff] [blame] | 242 | exit(EXIT_SUCCESS); |
AKASHI Takahiro | 19122aa | 2020-11-30 18:12:15 +0900 | [diff] [blame] | 243 | } |