/src/fwupd/libfwupdplugin/fu-linear-firmware.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2022 Richard Hughes <richard@hughsie.com> |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | */ |
6 | | |
7 | | #include "config.h" |
8 | | |
9 | | #include "fu-byte-array.h" |
10 | | #include "fu-bytes.h" |
11 | | #include "fu-common.h" |
12 | | #include "fu-input-stream.h" |
13 | | #include "fu-linear-firmware.h" |
14 | | #include "fu-partial-input-stream.h" |
15 | | |
16 | | /** |
17 | | * FuLinearFirmware: |
18 | | * |
19 | | * A firmware made up of concatenated blobs of a different firmware type. |
20 | | * |
21 | | * Parsed firmware images can set `FU_FIRMWARE_FLAG_IS_LAST_IMAGE` to abort processing. |
22 | | * |
23 | | * NOTE: All the child images will be of the specified `GType`. |
24 | | * |
25 | | * See also: [class@FuFirmware] |
26 | | */ |
27 | | |
28 | | typedef struct { |
29 | | GType image_gtype; |
30 | | } FuLinearFirmwarePrivate; |
31 | | |
32 | | G_DEFINE_TYPE_WITH_PRIVATE(FuLinearFirmware, fu_linear_firmware, FU_TYPE_FIRMWARE) |
33 | 0 | #define GET_PRIVATE(o) (fu_linear_firmware_get_instance_private(o)) |
34 | | |
35 | | enum { PROP_0, PROP_IMAGE_GTYPE, PROP_LAST }; |
36 | | |
37 | | static void |
38 | | fu_linear_firmware_export(FuFirmware *firmware, FuFirmwareExportFlags flags, XbBuilderNode *bn) |
39 | 0 | { |
40 | 0 | FuLinearFirmware *self = FU_LINEAR_FIRMWARE(firmware); |
41 | 0 | FuLinearFirmwarePrivate *priv = GET_PRIVATE(self); |
42 | 0 | fu_xmlb_builder_insert_kv(bn, "image_gtype", g_type_name(priv->image_gtype)); |
43 | 0 | } |
44 | | |
45 | | /** |
46 | | * fu_linear_firmware_get_image_gtype: |
47 | | * @self: a #FuLinearFirmware |
48 | | * |
49 | | * Gets the image #GType to use when parsing a byte buffer. |
50 | | * |
51 | | * Returns: integer |
52 | | * |
53 | | * Since: 1.8.2 |
54 | | **/ |
55 | | GType |
56 | | fu_linear_firmware_get_image_gtype(FuLinearFirmware *self) |
57 | 0 | { |
58 | 0 | FuLinearFirmwarePrivate *priv = GET_PRIVATE(self); |
59 | 0 | g_return_val_if_fail(FU_IS_LINEAR_FIRMWARE(self), G_TYPE_INVALID); |
60 | 0 | return priv->image_gtype; |
61 | 0 | } |
62 | | |
63 | | static gboolean |
64 | | fu_linear_firmware_build(FuFirmware *firmware, XbNode *n, GError **error) |
65 | 0 | { |
66 | 0 | FuLinearFirmware *self = FU_LINEAR_FIRMWARE(firmware); |
67 | 0 | FuLinearFirmwarePrivate *priv = GET_PRIVATE(self); |
68 | 0 | const gchar *tmp; |
69 | | |
70 | | /* simple properties */ |
71 | 0 | tmp = xb_node_query_text(n, "image_gtype", NULL); |
72 | 0 | if (tmp != NULL) { |
73 | 0 | priv->image_gtype = g_type_from_name(tmp); |
74 | 0 | if (priv->image_gtype == G_TYPE_INVALID) { |
75 | 0 | g_set_error(error, |
76 | 0 | FWUPD_ERROR, |
77 | 0 | FWUPD_ERROR_NOT_FOUND, |
78 | 0 | "GType %s not registered", |
79 | 0 | tmp); |
80 | 0 | return FALSE; |
81 | 0 | } |
82 | 0 | } |
83 | | |
84 | | /* success */ |
85 | 0 | return TRUE; |
86 | 0 | } |
87 | | |
88 | | static gboolean |
89 | | fu_linear_firmware_parse(FuFirmware *firmware, |
90 | | GInputStream *stream, |
91 | | FuFirmwareParseFlags flags, |
92 | | GError **error) |
93 | 0 | { |
94 | 0 | FuLinearFirmware *self = FU_LINEAR_FIRMWARE(firmware); |
95 | 0 | FuLinearFirmwarePrivate *priv = GET_PRIVATE(self); |
96 | 0 | gsize offset = 0; |
97 | 0 | gsize streamsz = 0; |
98 | |
|
99 | 0 | if (!fu_input_stream_size(stream, &streamsz, error)) |
100 | 0 | return FALSE; |
101 | 0 | while (offset < streamsz) { |
102 | 0 | g_autoptr(FuFirmware) img = g_object_new(priv->image_gtype, NULL); |
103 | 0 | g_autoptr(GInputStream) stream_tmp = NULL; |
104 | |
|
105 | 0 | stream_tmp = fu_partial_input_stream_new(stream, offset, streamsz - offset, error); |
106 | 0 | if (stream_tmp == NULL) { |
107 | 0 | g_prefix_error_literal(error, "failed to cut linear image: "); |
108 | 0 | return FALSE; |
109 | 0 | } |
110 | 0 | if (!fu_firmware_parse_stream(img, |
111 | 0 | stream_tmp, |
112 | 0 | 0x0, |
113 | 0 | flags | FU_FIRMWARE_PARSE_FLAG_NO_SEARCH, |
114 | 0 | error)) { |
115 | 0 | g_prefix_error(error, "failed to parse at 0x%x: ", (guint)offset); |
116 | 0 | return FALSE; |
117 | 0 | } |
118 | 0 | fu_firmware_set_offset(firmware, offset); |
119 | 0 | if (!fu_firmware_add_image_full(firmware, img, error)) |
120 | 0 | return FALSE; |
121 | | |
122 | | /* skip any padding */ |
123 | 0 | if (fu_firmware_has_flag(img, FU_FIRMWARE_FLAG_IS_LAST_IMAGE)) |
124 | 0 | break; |
125 | | |
126 | | /* next! */ |
127 | 0 | offset += fu_firmware_get_size(img); |
128 | 0 | } |
129 | | |
130 | | /* success */ |
131 | 0 | return TRUE; |
132 | 0 | } |
133 | | |
134 | | static GByteArray * |
135 | | fu_linear_firmware_write(FuFirmware *firmware, GError **error) |
136 | 0 | { |
137 | 0 | g_autoptr(GByteArray) buf = g_byte_array_new(); |
138 | 0 | g_autoptr(GPtrArray) images = fu_firmware_get_images(firmware); |
139 | | |
140 | | /* add each file */ |
141 | 0 | for (guint i = 0; i < images->len; i++) { |
142 | 0 | FuFirmware *img = g_ptr_array_index(images, i); |
143 | 0 | g_autoptr(GBytes) blob = NULL; |
144 | 0 | if (i == images->len - 1) |
145 | 0 | fu_firmware_add_flag(img, FU_FIRMWARE_FLAG_IS_LAST_IMAGE); |
146 | 0 | fu_firmware_set_offset(img, buf->len); |
147 | 0 | blob = fu_firmware_write(img, error); |
148 | 0 | if (blob == NULL) |
149 | 0 | return NULL; |
150 | 0 | fu_byte_array_append_bytes(buf, blob); |
151 | 0 | } |
152 | | |
153 | | /* success */ |
154 | 0 | return g_steal_pointer(&buf); |
155 | 0 | } |
156 | | |
157 | | static void |
158 | | fu_linear_firmware_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) |
159 | 0 | { |
160 | 0 | FuLinearFirmware *self = FU_LINEAR_FIRMWARE(object); |
161 | 0 | FuLinearFirmwarePrivate *priv = GET_PRIVATE(self); |
162 | 0 | switch (prop_id) { |
163 | 0 | case PROP_IMAGE_GTYPE: |
164 | 0 | g_value_set_gtype(value, priv->image_gtype); |
165 | 0 | break; |
166 | 0 | default: |
167 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); |
168 | 0 | break; |
169 | 0 | } |
170 | 0 | } |
171 | | |
172 | | static void |
173 | | fu_linear_firmware_set_property(GObject *object, |
174 | | guint prop_id, |
175 | | const GValue *value, |
176 | | GParamSpec *pspec) |
177 | 0 | { |
178 | 0 | FuLinearFirmware *self = FU_LINEAR_FIRMWARE(object); |
179 | 0 | FuLinearFirmwarePrivate *priv = GET_PRIVATE(self); |
180 | 0 | switch (prop_id) { |
181 | 0 | case PROP_IMAGE_GTYPE: |
182 | 0 | priv->image_gtype = g_value_get_gtype(value); |
183 | 0 | break; |
184 | 0 | default: |
185 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); |
186 | 0 | break; |
187 | 0 | } |
188 | 0 | } |
189 | | |
190 | | static void |
191 | | fu_linear_firmware_init(FuLinearFirmware *self) |
192 | 0 | { |
193 | 0 | fu_firmware_set_images_max(FU_FIRMWARE(self), 1024); |
194 | 0 | fu_firmware_add_flag(FU_FIRMWARE(self), FU_FIRMWARE_FLAG_NO_AUTO_DETECTION); |
195 | 0 | } |
196 | | |
197 | | static void |
198 | | fu_linear_firmware_class_init(FuLinearFirmwareClass *klass) |
199 | 0 | { |
200 | 0 | FuFirmwareClass *firmware_class = FU_FIRMWARE_CLASS(klass); |
201 | 0 | GObjectClass *object_class = G_OBJECT_CLASS(klass); |
202 | 0 | GParamSpec *pspec; |
203 | |
|
204 | 0 | object_class->get_property = fu_linear_firmware_get_property; |
205 | 0 | object_class->set_property = fu_linear_firmware_set_property; |
206 | 0 | firmware_class->parse = fu_linear_firmware_parse; |
207 | 0 | firmware_class->write = fu_linear_firmware_write; |
208 | 0 | firmware_class->export = fu_linear_firmware_export; |
209 | 0 | firmware_class->build = fu_linear_firmware_build; |
210 | | |
211 | | /** |
212 | | * FuLinearFirmware:image-gtype: |
213 | | * |
214 | | * The image #GType |
215 | | * |
216 | | * Since: 1.8.2 |
217 | | */ |
218 | 0 | pspec = |
219 | 0 | g_param_spec_gtype("image-gtype", |
220 | 0 | NULL, |
221 | 0 | NULL, |
222 | 0 | FU_TYPE_FIRMWARE, |
223 | 0 | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME); |
224 | 0 | g_object_class_install_property(object_class, PROP_IMAGE_GTYPE, pspec); |
225 | 0 | } |
226 | | |
227 | | /** |
228 | | * fu_linear_firmware_new: |
229 | | * @image_gtype: a #GType, e.g. %FU_TYPE_OPROM_FIRMWARE |
230 | | * |
231 | | * Creates a new #FuFirmware made up of concatenated images. |
232 | | * |
233 | | * Since: 1.8.2 |
234 | | **/ |
235 | | FuFirmware * |
236 | | fu_linear_firmware_new(GType image_gtype) |
237 | 0 | { |
238 | 0 | return g_object_new(FU_TYPE_LINEAR_FIRMWARE, "image-gtype", image_gtype, NULL); |
239 | 0 | } |