1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2018 Linaro Limited
4 * Author: AKASHI Takahiro
13 #include <linux/types.h>
16 #include <sys/types.h>
25 #define aligned_u64 __aligned_u64
28 #define __packed __attribute__((packed))
34 static const char *tool_name = "mkeficapsule";
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;
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'},
51 static void print_usage(void)
53 printf("Usage: %s [options] <output file>\n"
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"
60 "\t-h, --help print a help message\n",
65 * read_bin_file - read a firmware binary file
66 * @bin: Path to a firmware binary file
67 * @data: Pointer to pointer of allocated buffer
68 * @bin_size: Size of allocated buffer
70 * Read out a content of binary, @bin, into @data.
71 * A caller should free @data.
77 static int read_bin_file(char *bin, void **data, off_t *bin_size)
87 fprintf(stderr, "cannot open %s\n", bin);
90 if (stat(bin, &bin_stat) < 0) {
91 fprintf(stderr, "cannot determine the size of %s\n", bin);
95 if (bin_stat.st_size > SIZE_MAX) {
96 fprintf(stderr, "file size is too large for malloc: %s\n", bin);
100 buf = malloc(bin_stat.st_size);
102 fprintf(stderr, "cannot allocate memory: %zx\n",
103 (size_t)bin_stat.st_size);
108 size = fread(buf, 1, bin_stat.st_size, g);
109 if (size < bin_stat.st_size) {
110 fprintf(stderr, "read failed (%zx)\n", size);
116 *bin_size = bin_stat.st_size;
124 * write_capsule_file - write a capsule file
126 * @data: Pointer to data
127 * @bin_size: Size of data
129 * Write out data, @data, with the size @bin_size.
135 static int write_capsule_file(FILE *f, void *data, size_t size, const char *msg)
139 size_written = fwrite(data, 1, size, f);
140 if (size_written < size) {
141 fprintf(stderr, "%s: write failed (%zx != %zx)\n", msg,
150 * create_fwbin - create an uefi capsule file
151 * @path: Path to a created capsule file
152 * @bin: Path to a firmware binary to encapsulate
153 * @guid: GUID of related FMP driver
154 * @index: Index number in capsule
155 * @instance: Instance number in capsule
156 * @mcount: Monotonic count in authentication information
157 * @private_file: Path to a private key file
158 * @cert_file: Path to a certificate file
160 * This function actually does the job of creating an uefi capsule file.
161 * All the arguments must be supplied.
162 * If either @private_file ror @cert_file is NULL, the capsule file
169 static int create_fwbin(char *path, char *bin, efi_guid_t *guid,
170 unsigned long index, unsigned long instance)
172 struct efi_capsule_header header;
173 struct efi_firmware_management_capsule_header capsule;
174 struct efi_firmware_management_capsule_image_header image;
182 printf("For output: %s\n", path);
183 printf("\tbin: %s\n\ttype: %pUl\n", bin, guid);
184 printf("\tindex: %ld\n\tinstance: %ld\n", index, instance);
192 * read a firmware binary
194 if (read_bin_file(bin, &data, &bin_size))
198 * write a capsule file
200 f = fopen(path, "w");
202 fprintf(stderr, "cannot open %s\n", path);
207 * capsule file header
209 header.capsule_guid = efi_guid_fm_capsule;
210 header.header_size = sizeof(header);
211 /* TODO: The current implementation ignores flags */
212 header.flags = CAPSULE_FLAGS_PERSIST_ACROSS_RESET;
213 header.capsule_image_size = sizeof(header)
214 + sizeof(capsule) + sizeof(u64)
217 if (write_capsule_file(f, &header, sizeof(header),
222 * firmware capsule header
223 * This capsule has only one firmware capsule image.
225 capsule.version = 0x00000001;
226 capsule.embedded_driver_count = 0;
227 capsule.payload_item_count = 1;
228 if (write_capsule_file(f, &capsule, sizeof(capsule),
229 "Firmware capsule header"))
232 offset = sizeof(capsule) + sizeof(u64);
233 if (write_capsule_file(f, &offset, sizeof(offset),
234 "Offset to capsule image"))
238 * firmware capsule image header
240 image.version = 0x00000003;
241 memcpy(&image.update_image_type_id, guid, sizeof(*guid));
242 image.update_image_index = index;
243 image.reserved[0] = 0;
244 image.reserved[1] = 0;
245 image.reserved[2] = 0;
246 image.update_image_size = bin_size;
247 image.update_vendor_code_size = 0; /* none */
248 image.update_hardware_instance = instance;
249 image.image_capsule_support = 0;
250 if (write_capsule_file(f, &image, sizeof(image),
251 "Firmware capsule image header"))
257 if (write_capsule_file(f, data, bin_size, "Firmware binary"))
271 * $ mkeficapsule -f <firmware binary> <output file>
273 int main(int argc, char **argv)
277 unsigned long index, instance;
285 c = getopt_long(argc, argv, "f:r:i:I:v:h", options, &idx);
292 fprintf(stderr, "Image already specified\n");
296 guid = &efi_guid_image_type_uboot_fit;
300 fprintf(stderr, "Image already specified\n");
304 guid = &efi_guid_image_type_uboot_raw;
307 index = strtoul(optarg, NULL, 0);
310 instance = strtoul(optarg, NULL, 0);
318 /* need an output file */
319 if (argc != optind + 1) {
324 /* need a fit image file or raw image file */
330 if (create_fwbin(argv[optind], file, guid, index, instance)
332 fprintf(stderr, "Creating firmware capsule failed\n");