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",
64 static int create_fwbin(char *path, char *bin, efi_guid_t *guid,
65 unsigned long index, unsigned long instance)
67 struct efi_capsule_header header;
68 struct efi_firmware_management_capsule_header capsule;
69 struct efi_firmware_management_capsule_image_header image;
77 printf("For output: %s\n", path);
78 printf("\tbin: %s\n\ttype: %pUl\n", bin, guid);
79 printf("\tindex: %ld\n\tinstance: %ld\n", index, instance);
84 printf("cannot open %s\n", bin);
87 if (stat(bin, &bin_stat) < 0) {
88 printf("cannot determine the size of %s\n", bin);
91 data = malloc(bin_stat.st_size);
93 printf("cannot allocate memory: %zx\n", (size_t)bin_stat.st_size);
98 printf("cannot open %s\n", path);
101 header.capsule_guid = efi_guid_fm_capsule;
102 header.header_size = sizeof(header);
103 /* TODO: The current implementation ignores flags */
104 header.flags = CAPSULE_FLAGS_PERSIST_ACROSS_RESET;
105 header.capsule_image_size = sizeof(header)
106 + sizeof(capsule) + sizeof(u64)
110 size = fwrite(&header, 1, sizeof(header), f);
111 if (size < sizeof(header)) {
112 printf("write failed (%zx)\n", size);
116 capsule.version = 0x00000001;
117 capsule.embedded_driver_count = 0;
118 capsule.payload_item_count = 1;
119 size = fwrite(&capsule, 1, sizeof(capsule), f);
120 if (size < (sizeof(capsule))) {
121 printf("write failed (%zx)\n", size);
124 offset = sizeof(capsule) + sizeof(u64);
125 size = fwrite(&offset, 1, sizeof(offset), f);
126 if (size < sizeof(offset)) {
127 printf("write failed (%zx)\n", size);
131 image.version = 0x00000003;
132 memcpy(&image.update_image_type_id, guid, sizeof(*guid));
133 image.update_image_index = index;
134 image.reserved[0] = 0;
135 image.reserved[1] = 0;
136 image.reserved[2] = 0;
137 image.update_image_size = bin_stat.st_size;
138 image.update_vendor_code_size = 0; /* none */
139 image.update_hardware_instance = instance;
140 image.image_capsule_support = 0;
142 size = fwrite(&image, 1, sizeof(image), f);
143 if (size < sizeof(image)) {
144 printf("write failed (%zx)\n", size);
147 size = fread(data, 1, bin_stat.st_size, g);
148 if (size < bin_stat.st_size) {
149 printf("read failed (%zx)\n", size);
152 size = fwrite(data, 1, bin_stat.st_size, f);
153 if (size < bin_stat.st_size) {
154 printf("write failed (%zx)\n", size);
176 * $ mkeficapsule -f <firmware binary> <output file>
178 int main(int argc, char **argv)
182 unsigned long index, instance;
190 c = getopt_long(argc, argv, "f:r:i:I:v:h", options, &idx);
197 printf("Image already specified\n");
201 guid = &efi_guid_image_type_uboot_fit;
205 printf("Image already specified\n");
209 guid = &efi_guid_image_type_uboot_raw;
212 index = strtoul(optarg, NULL, 0);
215 instance = strtoul(optarg, NULL, 0);
223 /* need an output file */
224 if (argc != optind + 1) {
229 /* need a fit image file or raw image file */
235 if (create_fwbin(argv[optind], file, guid, index, instance)
237 printf("Creating firmware capsule failed\n");