Merge tag 'efi-next' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi into...
[platform/kernel/u-boot.git] / tools / mkeficapsule.c
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>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16
17 typedef __u8 u8;
18 typedef __u16 u16;
19 typedef __u32 u32;
20 typedef __u64 u64;
21 typedef __s16 s16;
22 typedef __s32 s32;
23
24 #define aligned_u64 __aligned_u64
25
26 #ifndef __packed
27 #define __packed __attribute__((packed))
28 #endif
29
30 #include <efi.h>
31 #include <efi_api.h>
32
33 static const char *tool_name = "mkeficapsule";
34
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;
40
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'},
47         {NULL, 0, NULL, 0},
48 };
49
50 static void print_usage(void)
51 {
52         printf("Usage: %s [options] <output file>\n"
53                "Options:\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",
59                tool_name);
60 }
61
62 static int create_fwbin(char *path, char *bin, efi_guid_t *guid,
63                         unsigned long index, unsigned long instance)
64 {
65         struct efi_capsule_header header;
66         struct efi_firmware_management_capsule_header capsule;
67         struct efi_firmware_management_capsule_image_header image;
68         FILE *f, *g;
69         struct stat bin_stat;
70         u8 *data;
71         size_t size;
72         u64 offset;
73
74 #ifdef DEBUG
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);
78 #endif
79
80         g = fopen(bin, "r");
81         if (!g) {
82                 printf("cannot open %s\n", bin);
83                 return -1;
84         }
85         if (stat(bin, &bin_stat) < 0) {
86                 printf("cannot determine the size of %s\n", bin);
87                 goto err_1;
88         }
89         data = malloc(bin_stat.st_size);
90         if (!data) {
91                 printf("cannot allocate memory: %lx\n", bin_stat.st_size);
92                 goto err_1;
93         }
94         f = fopen(path, "w");
95         if (!f) {
96                 printf("cannot open %s\n", path);
97                 goto err_2;
98         }
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)
105                                         + sizeof(image)
106                                         + bin_stat.st_size;
107
108         size = fwrite(&header, 1, sizeof(header), f);
109         if (size < sizeof(header)) {
110                 printf("write failed (%lx)\n", size);
111                 goto err_3;
112         }
113
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);
120                 goto err_3;
121         }
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);
126                 goto err_3;
127         }
128
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;
136
137         size = fwrite(&image, 1, sizeof(image), f);
138         if (size < sizeof(image)) {
139                 printf("write failed (%lx)\n", size);
140                 goto err_3;
141         }
142         size = fread(data, 1, bin_stat.st_size, g);
143         if (size < bin_stat.st_size) {
144                 printf("read failed (%lx)\n", size);
145                 goto err_3;
146         }
147         size = fwrite(data, 1, bin_stat.st_size, f);
148         if (size < bin_stat.st_size) {
149                 printf("write failed (%lx)\n", size);
150                 goto err_3;
151         }
152
153         fclose(f);
154         fclose(g);
155         free(data);
156
157         return 0;
158
159 err_3:
160         fclose(f);
161 err_2:
162         free(data);
163 err_1:
164         fclose(g);
165
166         return -1;
167 }
168
169 /*
170  * Usage:
171  *   $ mkeficapsule -f <firmware binary> <output file>
172  */
173 int main(int argc, char **argv)
174 {
175         char *file;
176         efi_guid_t *guid;
177         unsigned long index, instance;
178         int c, idx;
179
180         file = NULL;
181         guid = NULL;
182         index = 0;
183         instance = 0;
184         for (;;) {
185                 c = getopt_long(argc, argv, "f:r:i:I:v:h", options, &idx);
186                 if (c == -1)
187                         break;
188
189                 switch (c) {
190                 case 'f':
191                         if (file) {
192                                 printf("Image already specified\n");
193                                 return -1;
194                         }
195                         file = optarg;
196                         guid = &efi_guid_image_type_uboot_fit;
197                         break;
198                 case 'r':
199                         if (file) {
200                                 printf("Image already specified\n");
201                                 return -1;
202                         }
203                         file = optarg;
204                         guid = &efi_guid_image_type_uboot_raw;
205                         break;
206                 case 'i':
207                         index = strtoul(optarg, NULL, 0);
208                         break;
209                 case 'I':
210                         instance = strtoul(optarg, NULL, 0);
211                         break;
212                 case 'h':
213                         print_usage();
214                         return 0;
215                 }
216         }
217
218         /* need a output file */
219         if (argc != optind + 1) {
220                 print_usage();
221                 return -1;
222         }
223
224         /* need a fit image file or raw image file */
225         if (!file) {
226                 print_usage();
227                 return -1;
228         }
229
230         if (create_fwbin(argv[optind], file, guid, index, instance)
231                         < 0) {
232                 printf("Creating firmware capsule failed\n");
233                 return -1;
234         }
235
236         return 0;
237 }