/src/fwupd/libfwupdplugin/fu-oprom-firmware.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2022 Richard Hughes <richard@hughsie.com> |
3 | | * Copyright 2022 Intel |
4 | | * |
5 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
6 | | */ |
7 | | |
8 | 0 | #define G_LOG_DOMAIN "FuFirmware" |
9 | | |
10 | | #include "config.h" |
11 | | |
12 | | #include <string.h> |
13 | | |
14 | | #include "fu-byte-array.h" |
15 | | #include "fu-common.h" |
16 | | #include "fu-ifwi-cpd-firmware.h" |
17 | | #include "fu-input-stream.h" |
18 | | #include "fu-oprom-firmware.h" |
19 | | #include "fu-string.h" |
20 | | |
21 | | /** |
22 | | * FuOpromFirmware: |
23 | | * |
24 | | * An OptionROM can be found in nearly every PCI device. Multiple OptionROM images may be appended. |
25 | | * |
26 | | * See also: [class@FuFirmware] |
27 | | */ |
28 | | |
29 | | typedef struct { |
30 | | FuOpromMachineType machine_type; |
31 | | FuOpromSubsystem subsystem; |
32 | | FuOpromCompressionType compression_type; |
33 | | guint16 vendor_id; |
34 | | guint16 device_id; |
35 | | } FuOpromFirmwarePrivate; |
36 | | |
37 | 939 | G_DEFINE_TYPE_WITH_PRIVATE(FuOpromFirmware, fu_oprom_firmware, FU_TYPE_FIRMWARE) |
38 | 1.67k | #define GET_PRIVATE(o) (fu_oprom_firmware_get_instance_private(o)) |
39 | | |
40 | 3.13k | #define FU_OPROM_FIRMWARE_ALIGN_LEN 512u |
41 | | |
42 | | /** |
43 | | * fu_oprom_firmware_get_machine_type: |
44 | | * @self: a #FuFirmware |
45 | | * |
46 | | * Gets the machine type. |
47 | | * |
48 | | * Returns: a #FuOpromMachineType |
49 | | **/ |
50 | | FuOpromMachineType |
51 | | fu_oprom_firmware_get_machine_type(FuOpromFirmware *self) |
52 | 0 | { |
53 | 0 | FuOpromFirmwarePrivate *priv = GET_PRIVATE(self); |
54 | 0 | g_return_val_if_fail(FU_IS_OPROM_FIRMWARE(self), G_MAXUINT16); |
55 | 0 | return priv->machine_type; |
56 | 0 | } |
57 | | |
58 | | /** |
59 | | * fu_oprom_firmware_get_subsystem: |
60 | | * @self: a #FuFirmware |
61 | | * |
62 | | * Gets the machine type. |
63 | | * |
64 | | * Returns: a #FuOpromSubsystem |
65 | | **/ |
66 | | FuOpromSubsystem |
67 | | fu_oprom_firmware_get_subsystem(FuOpromFirmware *self) |
68 | 0 | { |
69 | 0 | FuOpromFirmwarePrivate *priv = GET_PRIVATE(self); |
70 | 0 | g_return_val_if_fail(FU_IS_OPROM_FIRMWARE(self), G_MAXUINT16); |
71 | 0 | return priv->subsystem; |
72 | 0 | } |
73 | | |
74 | | /** |
75 | | * fu_oprom_firmware_get_compression_type: |
76 | | * @self: a #FuFirmware |
77 | | * |
78 | | * Gets the machine type. |
79 | | * |
80 | | * Returns: a #FuOpromCompressionType |
81 | | **/ |
82 | | FuOpromCompressionType |
83 | | fu_oprom_firmware_get_compression_type(FuOpromFirmware *self) |
84 | 0 | { |
85 | 0 | FuOpromFirmwarePrivate *priv = GET_PRIVATE(self); |
86 | 0 | g_return_val_if_fail(FU_IS_OPROM_FIRMWARE(self), G_MAXUINT16); |
87 | 0 | return priv->compression_type; |
88 | 0 | } |
89 | | |
90 | | static void |
91 | | fu_oprom_firmware_export(FuFirmware *firmware, |
92 | | FuFirmwareExportFlags flags, |
93 | | FwupdJsonObject *json_object) |
94 | 0 | { |
95 | 0 | FuOpromFirmware *self = FU_OPROM_FIRMWARE(firmware); |
96 | 0 | FuOpromFirmwarePrivate *priv = GET_PRIVATE(self); |
97 | 0 | if (priv->machine_type != 0) |
98 | 0 | fwupd_json_object_add_integer(json_object, "machine_type", priv->machine_type); |
99 | 0 | if (priv->subsystem != 0) |
100 | 0 | fwupd_json_object_add_integer(json_object, "subsystem", priv->subsystem); |
101 | 0 | if (priv->compression_type != 0) |
102 | 0 | fwupd_json_object_add_integer(json_object, |
103 | 0 | "compression_type", |
104 | 0 | priv->compression_type); |
105 | 0 | if (priv->vendor_id != 0) |
106 | 0 | fwupd_json_object_add_integer(json_object, "vendor_id", priv->vendor_id); |
107 | 0 | if (priv->device_id != 0) |
108 | 0 | fwupd_json_object_add_integer(json_object, "device_id", priv->device_id); |
109 | 0 | } |
110 | | |
111 | | static gboolean |
112 | | fu_oprom_firmware_validate(FuFirmware *firmware, |
113 | | FuInputStream *stream, |
114 | | gsize offset, |
115 | | GError **error) |
116 | 937 | { |
117 | 937 | return fu_struct_oprom_validate_stream(stream, offset, error); |
118 | 937 | } |
119 | | |
120 | | static gboolean |
121 | | fu_oprom_firmware_parse(FuFirmware *firmware, |
122 | | FuInputStream *stream, |
123 | | FuFirmwareParseFlags flags, |
124 | | GError **error) |
125 | 887 | { |
126 | 887 | FuOpromFirmware *self = FU_OPROM_FIRMWARE(firmware); |
127 | 887 | FuOpromFirmwarePrivate *priv = GET_PRIVATE(self); |
128 | 887 | guint16 expansion_header_offset = 0; |
129 | 887 | guint16 pci_header_offset; |
130 | 887 | guint16 image_length = 0; |
131 | 887 | gsize image_size = 0; |
132 | 887 | g_autoptr(FuStructOprom) st_hdr = NULL; |
133 | 887 | g_autoptr(FuStructOpromPci) st_pci = NULL; |
134 | | |
135 | | /* parse header */ |
136 | 887 | st_hdr = fu_struct_oprom_parse_stream(stream, 0x0, error); |
137 | 887 | if (st_hdr == NULL) |
138 | 0 | return FALSE; |
139 | 887 | priv->subsystem = fu_struct_oprom_get_subsystem(st_hdr); |
140 | 887 | priv->compression_type = fu_struct_oprom_get_compression_type(st_hdr); |
141 | 887 | priv->machine_type = fu_struct_oprom_get_machine_type(st_hdr); |
142 | | |
143 | | /* get PCI offset */ |
144 | 887 | pci_header_offset = fu_struct_oprom_get_pci_header_offset(st_hdr); |
145 | 887 | if (pci_header_offset == 0x0) { |
146 | 2 | g_set_error_literal(error, |
147 | 2 | FWUPD_ERROR, |
148 | 2 | FWUPD_ERROR_INVALID_DATA, |
149 | 2 | "no PCI data structure offset provided"); |
150 | 2 | return FALSE; |
151 | 2 | } |
152 | | |
153 | | /* verify signature */ |
154 | 885 | st_pci = fu_struct_oprom_pci_parse_stream(stream, pci_header_offset, error); |
155 | 885 | if (st_pci == NULL) |
156 | 73 | return FALSE; |
157 | 812 | priv->vendor_id = fu_struct_oprom_pci_get_vendor_id(st_pci); |
158 | 812 | priv->device_id = fu_struct_oprom_pci_get_device_id(st_pci); |
159 | | |
160 | | /* get length */ |
161 | 812 | image_length = fu_struct_oprom_pci_get_image_length(st_pci); |
162 | 812 | if (image_length == 0x0) { |
163 | 2 | g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_INVALID_DATA, "invalid image"); |
164 | 2 | return FALSE; |
165 | 2 | } |
166 | 810 | if (!fu_size_checked_inc_product(&image_size, |
167 | 810 | image_length, |
168 | 810 | FU_OPROM_FIRMWARE_ALIGN_LEN, |
169 | 810 | error)) { |
170 | 0 | g_prefix_error_literal(error, "image size overflow: "); |
171 | 0 | return FALSE; |
172 | 0 | } |
173 | 810 | fu_firmware_set_size(firmware, image_size); |
174 | 810 | fu_firmware_set_idx(firmware, fu_struct_oprom_pci_get_code_type(st_pci)); |
175 | | |
176 | | /* is last image */ |
177 | 810 | if (fu_struct_oprom_pci_get_indicator(st_pci) & FU_OPROM_INDICATOR_FLAG_LAST) |
178 | 286 | fu_firmware_add_flag(firmware, FU_FIRMWARE_FLAG_IS_LAST_IMAGE); |
179 | | |
180 | | /* get CPD offset */ |
181 | 810 | expansion_header_offset = fu_struct_oprom_get_expansion_header_offset(st_hdr); |
182 | 810 | if (expansion_header_offset != 0x0) { |
183 | 808 | g_autoptr(FuFirmware) img = NULL; |
184 | 808 | img = fu_firmware_new_from_gtypes(stream, |
185 | 808 | expansion_header_offset, |
186 | 808 | flags, |
187 | 808 | error, |
188 | 808 | FU_TYPE_IFWI_CPD_FIRMWARE, |
189 | 808 | FU_TYPE_FIRMWARE, |
190 | 808 | G_TYPE_INVALID); |
191 | 808 | if (img == NULL) { |
192 | 23 | g_prefix_error_literal(error, "failed to build firmware: "); |
193 | 23 | return FALSE; |
194 | 23 | } |
195 | 785 | fu_firmware_set_id(img, "cpd"); |
196 | 785 | fu_firmware_set_offset(img, expansion_header_offset); |
197 | 785 | if (!fu_firmware_add_image(firmware, img, error)) |
198 | 0 | return FALSE; |
199 | 785 | } |
200 | | |
201 | | /* success */ |
202 | 787 | return TRUE; |
203 | 810 | } |
204 | | |
205 | | static GByteArray * |
206 | | fu_oprom_firmware_write(FuFirmware *firmware, GError **error) |
207 | 787 | { |
208 | 787 | FuOpromFirmware *self = FU_OPROM_FIRMWARE(firmware); |
209 | 787 | FuOpromFirmwarePrivate *priv = GET_PRIVATE(self); |
210 | 787 | gsize image_size = 0; |
211 | 787 | g_autoptr(GByteArray) buf = g_byte_array_new(); |
212 | 787 | g_autoptr(FuStructOprom) st_hdr = fu_struct_oprom_new(); |
213 | 787 | g_autoptr(FuStructOpromPci) st_pci = fu_struct_oprom_pci_new(); |
214 | 787 | g_autoptr(GBytes) blob_cpd = NULL; |
215 | | |
216 | | /* the smallest each image (and header) can be is 512 bytes */ |
217 | 787 | image_size = fu_common_align_up(st_hdr->buf->len, FU_FIRMWARE_ALIGNMENT_512); |
218 | 787 | blob_cpd = fu_firmware_get_image_by_id_bytes(firmware, "cpd", NULL); |
219 | 787 | if (blob_cpd != NULL) { |
220 | 753 | gsize cpd_size = |
221 | 753 | fu_common_align_up(g_bytes_get_size(blob_cpd), FU_FIRMWARE_ALIGNMENT_512); |
222 | 753 | if (!fu_size_checked_inc(&image_size, cpd_size, error)) { |
223 | 0 | g_prefix_error_literal(error, "overflow calculating OptionROM size: "); |
224 | 0 | return NULL; |
225 | 0 | } |
226 | 753 | } |
227 | | |
228 | | /* write the header */ |
229 | 787 | fu_struct_oprom_set_image_size(st_hdr, image_size / FU_OPROM_FIRMWARE_ALIGN_LEN); |
230 | 787 | fu_struct_oprom_set_subsystem(st_hdr, priv->subsystem); |
231 | 787 | fu_struct_oprom_set_machine_type(st_hdr, priv->machine_type); |
232 | 787 | fu_struct_oprom_set_compression_type(st_hdr, priv->compression_type); |
233 | 787 | if (blob_cpd != NULL) { |
234 | 753 | fu_struct_oprom_set_expansion_header_offset(st_hdr, |
235 | 753 | image_size - |
236 | 753 | FU_OPROM_FIRMWARE_ALIGN_LEN); |
237 | 753 | } |
238 | 787 | g_byte_array_append(buf, st_hdr->buf->data, st_hdr->buf->len); |
239 | | |
240 | | /* add PCI section */ |
241 | 787 | fu_struct_oprom_pci_set_vendor_id(st_pci, priv->vendor_id); |
242 | 787 | fu_struct_oprom_pci_set_device_id(st_pci, priv->device_id); |
243 | 787 | fu_struct_oprom_pci_set_image_length(st_pci, image_size / FU_OPROM_FIRMWARE_ALIGN_LEN); |
244 | 787 | fu_struct_oprom_pci_set_code_type(st_pci, fu_firmware_get_idx(firmware)); |
245 | 787 | if (fu_firmware_has_flag(firmware, FU_FIRMWARE_FLAG_IS_LAST_IMAGE)) |
246 | 274 | fu_struct_oprom_pci_set_indicator(st_pci, FU_OPROM_INDICATOR_FLAG_LAST); |
247 | 787 | fu_byte_array_append_array(buf, st_pci->buf); |
248 | 787 | fu_byte_array_align_up(buf, FU_FIRMWARE_ALIGNMENT_512, 0xFF); |
249 | | |
250 | | /* add CPD */ |
251 | 787 | if (blob_cpd != NULL) { |
252 | 753 | fu_byte_array_append_bytes(buf, blob_cpd); |
253 | 753 | fu_byte_array_align_up(buf, FU_FIRMWARE_ALIGNMENT_512, 0xFF); |
254 | 753 | } |
255 | | |
256 | | /* success */ |
257 | 787 | return g_steal_pointer(&buf); |
258 | 787 | } |
259 | | |
260 | | static gboolean |
261 | | fu_oprom_firmware_build(FuFirmware *firmware, FwupdJsonObject *json_object, GError **error) |
262 | 0 | { |
263 | 0 | FuOpromFirmware *self = FU_OPROM_FIRMWARE(firmware); |
264 | 0 | FuOpromFirmwarePrivate *priv = GET_PRIVATE(self); |
265 | | |
266 | | /* simple properties */ |
267 | 0 | if (fwupd_json_object_has_node(json_object, "machine_type")) { |
268 | 0 | gint64 val = G_MAXINT64; |
269 | 0 | if (!fwupd_json_object_get_integer(json_object, "machine_type", &val, error)) |
270 | 0 | return FALSE; |
271 | 0 | if (val < 0 || val > G_MAXUINT16) { |
272 | 0 | g_set_error_literal(error, |
273 | 0 | FWUPD_ERROR, |
274 | 0 | FWUPD_ERROR_INVALID_DATA, |
275 | 0 | "invalid machine_type"); |
276 | 0 | return FALSE; |
277 | 0 | } |
278 | 0 | priv->machine_type = val; |
279 | 0 | } |
280 | 0 | if (fwupd_json_object_has_node(json_object, "subsystem")) { |
281 | 0 | gint64 val = G_MAXINT64; |
282 | 0 | if (!fwupd_json_object_get_integer(json_object, "subsystem", &val, error)) |
283 | 0 | return FALSE; |
284 | 0 | if (val < 0 || val > G_MAXUINT16) { |
285 | 0 | g_set_error_literal(error, |
286 | 0 | FWUPD_ERROR, |
287 | 0 | FWUPD_ERROR_INVALID_DATA, |
288 | 0 | "invalid subsystem"); |
289 | 0 | return FALSE; |
290 | 0 | } |
291 | 0 | priv->subsystem = val; |
292 | 0 | } |
293 | 0 | if (fwupd_json_object_has_node(json_object, "compression_type")) { |
294 | 0 | gint64 val = G_MAXINT64; |
295 | 0 | if (!fwupd_json_object_get_integer(json_object, "compression_type", &val, error)) |
296 | 0 | return FALSE; |
297 | 0 | if (val < 0 || val > G_MAXUINT16) { |
298 | 0 | g_set_error_literal(error, |
299 | 0 | FWUPD_ERROR, |
300 | 0 | FWUPD_ERROR_INVALID_DATA, |
301 | 0 | "invalid compression_type"); |
302 | 0 | return FALSE; |
303 | 0 | } |
304 | 0 | priv->compression_type = val; |
305 | 0 | } |
306 | 0 | if (fwupd_json_object_has_node(json_object, "vendor_id")) { |
307 | 0 | gint64 val = G_MAXINT64; |
308 | 0 | if (!fwupd_json_object_get_integer(json_object, "vendor_id", &val, error)) |
309 | 0 | return FALSE; |
310 | 0 | if (val < 0 || val > G_MAXUINT16) { |
311 | 0 | g_set_error_literal(error, |
312 | 0 | FWUPD_ERROR, |
313 | 0 | FWUPD_ERROR_INVALID_DATA, |
314 | 0 | "invalid vendor_id"); |
315 | 0 | return FALSE; |
316 | 0 | } |
317 | 0 | priv->vendor_id = val; |
318 | 0 | } |
319 | 0 | if (fwupd_json_object_has_node(json_object, "device_id")) { |
320 | 0 | gint64 val = G_MAXINT64; |
321 | 0 | if (!fwupd_json_object_get_integer(json_object, "device_id", &val, error)) |
322 | 0 | return FALSE; |
323 | 0 | if (val < 0 || val > G_MAXUINT16) { |
324 | 0 | g_set_error_literal(error, |
325 | 0 | FWUPD_ERROR, |
326 | 0 | FWUPD_ERROR_INVALID_DATA, |
327 | 0 | "invalid device_id"); |
328 | 0 | return FALSE; |
329 | 0 | } |
330 | 0 | priv->device_id = val; |
331 | 0 | } |
332 | | |
333 | | /* success */ |
334 | 0 | return TRUE; |
335 | 0 | } |
336 | | |
337 | | static void |
338 | | fu_oprom_firmware_init(FuOpromFirmware *self) |
339 | 937 | { |
340 | 937 | fu_firmware_add_flag(FU_FIRMWARE(self), FU_FIRMWARE_FLAG_HAS_STORED_SIZE); |
341 | 937 | fu_firmware_add_flag(FU_FIRMWARE(self), FU_FIRMWARE_FLAG_ALLOW_LINEAR); |
342 | 937 | } |
343 | | |
344 | | static void |
345 | | fu_oprom_firmware_class_init(FuOpromFirmwareClass *klass) |
346 | 1 | { |
347 | 1 | FuFirmwareClass *firmware_class = FU_FIRMWARE_CLASS(klass); |
348 | 1 | fu_firmware_add_image_gtype(firmware_class, FU_TYPE_FIRMWARE); |
349 | 1 | fu_firmware_add_image_gtype(firmware_class, FU_TYPE_IFWI_CPD_FIRMWARE); |
350 | 1 | fu_firmware_set_size_max(firmware_class, 128 * FU_MB); |
351 | 1 | firmware_class->validate = fu_oprom_firmware_validate; |
352 | 1 | firmware_class->export = fu_oprom_firmware_export; |
353 | 1 | firmware_class->parse = fu_oprom_firmware_parse; |
354 | 1 | firmware_class->write = fu_oprom_firmware_write; |
355 | 1 | firmware_class->build = fu_oprom_firmware_build; |
356 | 1 | } |
357 | | |
358 | | /** |
359 | | * fu_oprom_firmware_new: |
360 | | * |
361 | | * Creates a new #FuFirmware of OptionROM format |
362 | | **/ |
363 | | FuFirmware * |
364 | | fu_oprom_firmware_new(void) |
365 | 0 | { |
366 | 0 | return FU_FIRMWARE(g_object_new(FU_TYPE_OPROM_FIRMWARE, NULL)); |
367 | 0 | } |