1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2018 Linaro Limited
4 * Author: AKASHI Takahiro
13 #include <linux/types.h>
15 #include <sys/types.h>
24 #define aligned_u64 __aligned_u64
27 #define __packed __attribute__((packed))
33 static const char *tool_name = "mkeficapsule";
35 efi_guid_t efi_guid_fm_capsule = EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
36 efi_guid_t efi_guid_image_type_uboot_fit =
37 EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID;
38 efi_guid_t efi_guid_image_type_uboot_raw =
39 EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID;
41 static struct option options[] = {
42 {"fit", required_argument, NULL, 'f'},
43 {"raw", required_argument, NULL, 'r'},
44 {"index", required_argument, NULL, 'i'},
45 {"instance", required_argument, NULL, 'I'},
46 {"help", no_argument, NULL, 'h'},
50 static void print_usage(void)
52 printf("Usage: %s [options] <output file>\n"
54 "\t--fit <fit image> new FIT image file\n"
55 "\t--raw <raw image> new raw image file\n"
56 "\t--index <index> update image index\n"
57 "\t--instance <instance> update hardware instance\n"
58 "\t--help print a help message\n",
62 static int create_fwbin(char *path, char *bin, efi_guid_t *guid,
63 unsigned long index, unsigned long instance)
65 struct efi_capsule_header header;
66 struct efi_firmware_management_capsule_header capsule;
67 struct efi_firmware_management_capsule_image_header image;
75 printf("For output: %s\n", path);
76 printf("\tbin: %s\n\ttype: %pUl\n" bin, guid);
77 printf("\tindex: %ld\n\tinstance: %ld\n", index, instance);
82 printf("cannot open %s\n", bin);
85 if (stat(bin, &bin_stat) < 0) {
86 printf("cannot determine the size of %s\n", bin);
89 data = malloc(bin_stat.st_size);
91 printf("cannot allocate memory: %lx\n", bin_stat.st_size);
96 printf("cannot open %s\n", path);
99 header.capsule_guid = efi_guid_fm_capsule;
100 header.header_size = sizeof(header);
101 /* TODO: The current implementation ignores flags */
102 header.flags = CAPSULE_FLAGS_PERSIST_ACROSS_RESET;
103 header.capsule_image_size = sizeof(header)
104 + sizeof(capsule) + sizeof(u64)
108 size = fwrite(&header, 1, sizeof(header), f);
109 if (size < sizeof(header)) {
110 printf("write failed (%lx)\n", size);
114 capsule.version = 0x00000001;
115 capsule.embedded_driver_count = 0;
116 capsule.payload_item_count = 1;
117 size = fwrite(&capsule, 1, sizeof(capsule), f);
118 if (size < (sizeof(capsule))) {
119 printf("write failed (%lx)\n", size);
122 offset = sizeof(capsule) + sizeof(u64);
123 size = fwrite(&offset, 1, sizeof(offset), f);
124 if (size < sizeof(offset)) {
125 printf("write failed (%lx)\n", size);
129 image.version = 0x00000003;
130 memcpy(&image.update_image_type_id, guid, sizeof(*guid));
131 image.update_image_index = index;
132 image.update_image_size = bin_stat.st_size;
133 image.update_vendor_code_size = 0; /* none */
134 image.update_hardware_instance = instance;
135 image.image_capsule_support = 0;
137 size = fwrite(&image, 1, sizeof(image), f);
138 if (size < sizeof(image)) {
139 printf("write failed (%lx)\n", size);
142 size = fread(data, 1, bin_stat.st_size, g);
143 if (size < bin_stat.st_size) {
144 printf("read failed (%lx)\n", size);
147 size = fwrite(data, 1, bin_stat.st_size, f);
148 if (size < bin_stat.st_size) {
149 printf("write failed (%lx)\n", size);
171 * $ mkeficapsule -f <firmware binary> <output file>
173 int main(int argc, char **argv)
177 unsigned long index, instance;
185 c = getopt_long(argc, argv, "f:r:i:I:v:h", options, &idx);
192 printf("Image already specified\n");
196 guid = &efi_guid_image_type_uboot_fit;
200 printf("Image already specified\n");
204 guid = &efi_guid_image_type_uboot_raw;
207 index = strtoul(optarg, NULL, 0);
210 instance = strtoul(optarg, NULL, 0);
218 /* need a output file */
219 if (argc != optind + 1) {
224 /* need a fit image file or raw image file */
230 if (create_fwbin(argv[optind], file, guid, index, instance)
232 printf("Creating firmware capsule failed\n");