Coverage Report

Created: 2026-12-01 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fwupd/libfwupdplugin/fu-sbatlevel-section.c
Line
Count
Source
1
/*
2
 * Copyright 2023 Canonical Ltd.
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 */
6
7
#define G_LOG_DOMAIN "FuFirmware"
8
9
#include "config.h"
10
11
#include "fu-byte-array.h"
12
#include "fu-common.h"
13
#include "fu-csv-firmware.h"
14
#include "fu-input-stream.h"
15
#include "fu-partial-input-stream.h"
16
#include "fu-sbatlevel-section-struct.h"
17
#include "fu-sbatlevel-section.h"
18
19
172
G_DEFINE_TYPE(FuSbatlevelSection, fu_sbatlevel_section, FU_TYPE_FIRMWARE);
20
172
21
172
static gboolean
22
172
fu_sbatlevel_section_add_entry(FuSbatlevelSection *self,
23
172
             FuInputStream *stream,
24
172
             gsize offset,
25
172
             const gchar *entry_name,
26
172
             guint64 entry_idx,
27
172
             FuFirmwareParseFlags flags,
28
172
             GError **error)
29
172
{
30
154
  gsize streamsz = 0;
31
154
  g_autoptr(FuFirmware) entry_fw = NULL;
32
154
  g_autoptr(FuInputStream) partial_stream = NULL;
33
34
  /* stop at the null terminator */
35
154
  if (!fu_input_stream_size(stream, &streamsz, error))
36
0
    return FALSE;
37
154
  if (offset >= streamsz) {
38
48
    g_set_error(error,
39
48
          FWUPD_ERROR,
40
48
          FWUPD_ERROR_INVALID_DATA,
41
48
          "offset 0x%x exceeds stream size 0x%x",
42
48
          (guint)offset,
43
48
          (guint)streamsz);
44
48
    return FALSE;
45
48
  }
46
24.0k
  for (guint i = offset; i < streamsz; i++) {
47
23.9k
    guint8 value = 0;
48
23.9k
    if (!fu_input_stream_read_u8(stream, i, &value, error))
49
0
      return FALSE;
50
23.9k
    if (value == 0x0) {
51
66
      streamsz = i;
52
66
      break;
53
66
    }
54
23.9k
  }
55
56
106
  entry_fw = fu_csv_firmware_new();
57
106
  fu_csv_firmware_add_column_id(FU_CSV_FIRMWARE(entry_fw), "$id");
58
106
  fu_csv_firmware_add_column_id(FU_CSV_FIRMWARE(entry_fw), "component_generation");
59
106
  fu_csv_firmware_add_column_id(FU_CSV_FIRMWARE(entry_fw), "date_stamp");
60
106
  fu_csv_firmware_set_write_column_ids(FU_CSV_FIRMWARE(entry_fw), FALSE);
61
62
106
  fu_firmware_set_idx(entry_fw, entry_idx);
63
106
  fu_firmware_set_id(entry_fw, entry_name);
64
106
  fu_firmware_set_offset(entry_fw, offset);
65
106
  partial_stream = fu_partial_input_stream_new(stream, offset, streamsz - offset, error);
66
106
  if (partial_stream == NULL) {
67
0
    g_prefix_error_literal(error, "failed to cut CSV section: ");
68
0
    return FALSE;
69
0
  }
70
106
  if (!fu_firmware_parse_stream(entry_fw, partial_stream, 0, flags, error)) {
71
2
    g_prefix_error(error, "failed to parse %s: ", entry_name);
72
2
    return FALSE;
73
2
  }
74
104
  if (!fu_firmware_add_image(FU_FIRMWARE(self), entry_fw, error))
75
0
    return FALSE;
76
77
  /* success */
78
104
  return TRUE;
79
104
}
80
81
static gboolean
82
fu_sbatlevel_section_parse(FuFirmware *firmware,
83
         FuInputStream *stream,
84
         FuFirmwareParseFlags flags,
85
         GError **error)
86
144
{
87
144
  FuSbatlevelSection *self = FU_SBATLEVEL_SECTION(firmware);
88
144
  g_autoptr(FuStructSbatLevelSectionHeader) st = NULL;
89
90
144
  st = fu_struct_sbat_level_section_header_parse_stream(stream, 0x0, error);
91
144
  if (st == NULL)
92
48
    return FALSE;
93
96
  if (!fu_sbatlevel_section_add_entry(
94
96
    self,
95
96
    stream,
96
96
    sizeof(guint32) + fu_struct_sbat_level_section_header_get_previous(st),
97
96
    "previous",
98
96
    0,
99
96
    flags,
100
96
    error))
101
38
    return FALSE;
102
58
  if (!fu_sbatlevel_section_add_entry(self,
103
58
              stream,
104
58
              sizeof(guint32) +
105
58
            fu_struct_sbat_level_section_header_get_latest(st),
106
58
              "latest",
107
58
              1,
108
58
              flags,
109
58
              error))
110
12
    return FALSE;
111
46
  return TRUE;
112
58
}
113
114
static GByteArray *
115
fu_sbatlevel_section_write(FuFirmware *firmware, GError **error)
116
13
{
117
13
  g_autoptr(FuFirmware) img_ltst = NULL;
118
13
  g_autoptr(FuFirmware) img_prev = NULL;
119
13
  g_autoptr(FuStructSbatLevelSectionHeader) st = fu_struct_sbat_level_section_header_new();
120
13
  g_autoptr(GByteArray) buf_ltst = NULL;
121
13
  g_autoptr(GByteArray) buf_prev = NULL;
122
123
  /* previous */
124
13
  fu_struct_sbat_level_section_header_set_previous(st, sizeof(guint32) * 2);
125
13
  img_prev = fu_firmware_get_image_by_id(firmware, "previous", error);
126
13
  if (img_prev == NULL)
127
2
    return NULL;
128
11
  buf_prev = fu_firmware_write_array(img_prev, error);
129
11
  if (buf_prev == NULL)
130
0
    return NULL;
131
11
  fu_byte_array_append_array(st->buf, buf_prev);
132
11
  fu_byte_array_append_uint8(st->buf, 0x0);
133
134
  /* latest */
135
11
  fu_struct_sbat_level_section_header_set_latest(st,
136
11
                   (sizeof(guint32) * 2) + buf_prev->len + 1);
137
11
  img_ltst = fu_firmware_get_image_by_id(firmware, "latest", error);
138
11
  if (img_ltst == NULL)
139
0
    return NULL;
140
11
  buf_ltst = fu_firmware_write_array(img_ltst, error);
141
11
  if (buf_ltst == NULL)
142
0
    return NULL;
143
11
  fu_byte_array_append_array(st->buf, buf_ltst);
144
11
  fu_byte_array_append_uint8(st->buf, 0x0);
145
146
  /* success */
147
11
  return g_steal_pointer(&st->buf);
148
11
}
149
150
static void
151
fu_sbatlevel_section_init(FuSbatlevelSection *self)
152
171
{
153
171
}
154
155
static void
156
fu_sbatlevel_section_class_init(FuSbatlevelSectionClass *klass)
157
1
{
158
1
  FuFirmwareClass *firmware_class = FU_FIRMWARE_CLASS(klass);
159
1
  fu_firmware_add_image_gtype(firmware_class, FU_TYPE_CSV_FIRMWARE);
160
161
1
  fu_firmware_set_size_max(firmware_class, 10 * FU_KB);
162
1
  firmware_class->parse = fu_sbatlevel_section_parse;
163
1
  firmware_class->write = fu_sbatlevel_section_write;
164
1
  fu_firmware_set_images_max(firmware_class, 2);
165
1
}
166
167
FuFirmware *
168
fu_sbatlevel_section_new(void)
169
171
{
170
171
  return FU_FIRMWARE(g_object_new(FU_TYPE_SBATLEVEL_SECTION, NULL));
171
171
}