patman: Rename Color() method to build()
[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 <stdbool.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <linux/types.h>
14
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"
55
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",
61                 tool_name);
62 }
63
64 /**
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
69  *
70  * Read out a content of binary, @bin, into @data.
71  * A caller should free @data.
72  *
73  * Return:
74  * * 0  - on success
75  * * -1 - on failure
76  */
77 static int read_bin_file(char *bin, void **data, off_t *bin_size)
78 {
79         FILE *g;
80         struct stat bin_stat;
81         void *buf;
82         size_t size;
83         int ret = 0;
84
85         g = fopen(bin, "r");
86         if (!g) {
87                 fprintf(stderr, "cannot open %s\n", bin);
88                 return -1;
89         }
90         if (stat(bin, &bin_stat) < 0) {
91                 fprintf(stderr, "cannot determine the size of %s\n", bin);
92                 ret = -1;
93                 goto err;
94         }
95         if (bin_stat.st_size > SIZE_MAX) {
96                 fprintf(stderr, "file size is too large for malloc: %s\n", bin);
97                 ret = -1;
98                 goto err;
99         }
100         buf = malloc(bin_stat.st_size);
101         if (!buf) {
102                 fprintf(stderr, "cannot allocate memory: %zx\n",
103                         (size_t)bin_stat.st_size);
104                 ret = -1;
105                 goto err;
106         }
107
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);
111                 ret = -1;
112                 goto err;
113         }
114
115         *data = buf;
116         *bin_size = bin_stat.st_size;
117 err:
118         fclose(g);
119
120         return ret;
121 }
122
123 /**
124  * write_capsule_file - write a capsule file
125  * @bin:        FILE stream
126  * @data:       Pointer to data
127  * @bin_size:   Size of data
128  *
129  * Write out data, @data, with the size @bin_size.
130  *
131  * Return:
132  * * 0  - on success
133  * * -1 - on failure
134  */
135 static int write_capsule_file(FILE *f, void *data, size_t size, const char *msg)
136 {
137         size_t size_written;
138
139         size_written = fwrite(data, 1, size, f);
140         if (size_written < size) {
141                 fprintf(stderr, "%s: write failed (%zx != %zx)\n", msg,
142                         size_written, size);
143                 return -1;
144         }
145
146         return 0;
147 }
148
149 /**
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
159  *
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
163  * won't be signed.
164  *
165  * Return:
166  * * 0  - on success
167  * * -1 - on failure
168  */
169 static int create_fwbin(char *path, char *bin, efi_guid_t *guid,
170                         unsigned long index, unsigned long instance)
171 {
172         struct efi_capsule_header header;
173         struct efi_firmware_management_capsule_header capsule;
174         struct efi_firmware_management_capsule_image_header image;
175         FILE *f;
176         void *data;
177         off_t bin_size;
178         u64 offset;
179         int ret;
180
181 #ifdef DEBUG
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);
185 #endif
186
187         f = NULL;
188         data = NULL;
189         ret = -1;
190
191         /*
192          * read a firmware binary
193          */
194         if (read_bin_file(bin, &data, &bin_size))
195                 goto err;
196
197         /*
198          * write a capsule file
199          */
200         f = fopen(path, "w");
201         if (!f) {
202                 fprintf(stderr, "cannot open %s\n", path);
203                 goto err;
204         }
205
206         /*
207          * capsule file header
208          */
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)
215                                         + sizeof(image)
216                                         + bin_size;
217         if (write_capsule_file(f, &header, sizeof(header),
218                                "Capsule header"))
219                 goto err;
220
221         /*
222          * firmware capsule header
223          * This capsule has only one firmware capsule image.
224          */
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"))
230                 goto err;
231
232         offset = sizeof(capsule) + sizeof(u64);
233         if (write_capsule_file(f, &offset, sizeof(offset),
234                                "Offset to capsule image"))
235                 goto err;
236
237         /*
238          * firmware capsule image header
239          */
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"))
252                 goto err;
253
254         /*
255          * firmware binary
256          */
257         if (write_capsule_file(f, data, bin_size, "Firmware binary"))
258                 goto err;
259
260         ret = 0;
261 err:
262         if (f)
263                 fclose(f);
264         free(data);
265
266         return ret;
267 }
268
269 /*
270  * Usage:
271  *   $ mkeficapsule -f <firmware binary> <output file>
272  */
273 int main(int argc, char **argv)
274 {
275         char *file;
276         efi_guid_t *guid;
277         unsigned long index, instance;
278         int c, idx;
279
280         file = NULL;
281         guid = NULL;
282         index = 0;
283         instance = 0;
284         for (;;) {
285                 c = getopt_long(argc, argv, "f:r:i:I:v:h", options, &idx);
286                 if (c == -1)
287                         break;
288
289                 switch (c) {
290                 case 'f':
291                         if (file) {
292                                 fprintf(stderr, "Image already specified\n");
293                                 return -1;
294                         }
295                         file = optarg;
296                         guid = &efi_guid_image_type_uboot_fit;
297                         break;
298                 case 'r':
299                         if (file) {
300                                 fprintf(stderr, "Image already specified\n");
301                                 return -1;
302                         }
303                         file = optarg;
304                         guid = &efi_guid_image_type_uboot_raw;
305                         break;
306                 case 'i':
307                         index = strtoul(optarg, NULL, 0);
308                         break;
309                 case 'I':
310                         instance = strtoul(optarg, NULL, 0);
311                         break;
312                 case 'h':
313                         print_usage();
314                         return 0;
315                 }
316         }
317
318         /* need an output file */
319         if (argc != optind + 1) {
320                 print_usage();
321                 exit(EXIT_FAILURE);
322         }
323
324         /* need a fit image file or raw image file */
325         if (!file) {
326                 print_usage();
327                 exit(EXIT_SUCCESS);
328         }
329
330         if (create_fwbin(argv[optind], file, guid, index, instance)
331                         < 0) {
332                 fprintf(stderr, "Creating firmware capsule failed\n");
333                 exit(EXIT_FAILURE);
334         }
335
336         exit(EXIT_SUCCESS);
337 }