blob: 426038ea27ba0678657ccde9f0d14fd561ed5efe [file] [log] [blame]
AKASHI Takahirofab430b2020-11-30 18:12:15 +09001// 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
17typedef __u8 u8;
18typedef __u16 u16;
19typedef __u32 u32;
20typedef __u64 u64;
21typedef __s16 s16;
22typedef __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
33static const char *tool_name = "mkeficapsule";
34
35efi_guid_t efi_guid_fm_capsule = EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
36efi_guid_t efi_guid_image_type_uboot_fit =
37 EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID;
38efi_guid_t efi_guid_image_type_uboot_raw =
39 EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID;
40
41static 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
50static 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
62static 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 header.flags = CAPSULE_FLAGS_PERSIST_ACROSS_RESET; /* TODO */
102 header.capsule_image_size = sizeof(header)
103 + sizeof(capsule) + sizeof(u64)
104 + sizeof(image)
105 + bin_stat.st_size;
106
107 size = fwrite(&header, 1, sizeof(header), f);
108 if (size < sizeof(header)) {
109 printf("write failed (%lx)\n", size);
110 goto err_3;
111 }
112
113 capsule.version = 0x00000001;
114 capsule.embedded_driver_count = 0;
115 capsule.payload_item_count = 1;
116 size = fwrite(&capsule, 1, sizeof(capsule), f);
117 if (size < (sizeof(capsule))) {
118 printf("write failed (%lx)\n", size);
119 goto err_3;
120 }
121 offset = sizeof(capsule) + sizeof(u64);
122 size = fwrite(&offset, 1, sizeof(offset), f);
123 if (size < sizeof(offset)) {
124 printf("write failed (%lx)\n", size);
125 goto err_3;
126 }
127
128 image.version = 0x00000003;
129 memcpy(&image.update_image_type_id, guid, sizeof(*guid));
130 image.update_image_index = index;
131 image.update_image_size = bin_stat.st_size;
132 image.update_vendor_code_size = 0; /* none */
133 image.update_hardware_instance = instance;
134 image.image_capsule_support = 0;
135
136 size = fwrite(&image, 1, sizeof(image), f);
137 if (size < sizeof(image)) {
138 printf("write failed (%lx)\n", size);
139 goto err_3;
140 }
141 size = fread(data, 1, bin_stat.st_size, g);
142 if (size < bin_stat.st_size) {
143 printf("read failed (%lx)\n", size);
144 goto err_3;
145 }
146 size = fwrite(data, 1, bin_stat.st_size, f);
147 if (size < bin_stat.st_size) {
148 printf("write failed (%lx)\n", size);
149 goto err_3;
150 }
151
152 fclose(f);
153 fclose(g);
154 free(data);
155
156 return 0;
157
158err_3:
159 fclose(f);
160err_2:
161 free(data);
162err_1:
163 fclose(g);
164
165 return -1;
166}
167
168/*
169 * Usage:
170 * $ mkeficapsule -f <firmware binary> <output file>
171 */
172int main(int argc, char **argv)
173{
174 char *file;
175 efi_guid_t *guid;
176 unsigned long index, instance;
177 int c, idx;
178
179 file = NULL;
180 guid = NULL;
181 index = 0;
182 instance = 0;
183 for (;;) {
184 c = getopt_long(argc, argv, "f:r:i:I:v:h", options, &idx);
185 if (c == -1)
186 break;
187
188 switch (c) {
189 case 'f':
190 if (file) {
191 printf("Image already specified\n");
192 return -1;
193 }
194 file = optarg;
195 guid = &efi_guid_image_type_uboot_fit;
196 break;
197 case 'r':
198 if (file) {
199 printf("Image already specified\n");
200 return -1;
201 }
202 file = optarg;
203 guid = &efi_guid_image_type_uboot_raw;
204 break;
205 case 'i':
206 index = strtoul(optarg, NULL, 0);
207 break;
208 case 'I':
209 instance = strtoul(optarg, NULL, 0);
210 break;
211 case 'h':
212 print_usage();
213 return 0;
214 }
215 }
216
217 /* need a output file */
218 if (argc != optind + 1) {
219 print_usage();
220 return -1;
221 }
222
223 /* need a fit image file or raw image file */
224 if (!file) {
225 print_usage();
226 return -1;
227 }
228
229 if (create_fwbin(argv[optind], file, guid, index, instance)
230 < 0) {
231 printf("Creating firmware capsule failed\n");
232 return -1;
233 }
234
235 return 0;
236}