/src/fwupd/plugins/cros-ec/fu-cros-ec-common.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2020 Benson Leung <bleung@chromium.org> |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | */ |
6 | | |
7 | | #include "config.h" |
8 | | |
9 | | #include "fu-cros-ec-common.h" |
10 | | #include "fu-cros-ec-struct.h" |
11 | | |
12 | | void |
13 | | fu_cros_ec_version_free(FuCrosEcVersion *version) |
14 | 0 | { |
15 | 0 | g_free(version->boardname); |
16 | 0 | g_free(version->triplet); |
17 | 0 | g_free(version->sha1); |
18 | 0 | g_free(version); |
19 | 0 | } |
20 | | |
21 | | FuCrosEcVersion * |
22 | | fu_cros_ec_version_parse(const gchar *version_raw, GError **error) |
23 | 0 | { |
24 | 0 | gchar *ver = NULL; |
25 | 0 | g_autofree gchar *board = g_strdup(version_raw); |
26 | 0 | g_auto(GStrv) marker_split = NULL; |
27 | 0 | g_auto(GStrv) triplet_split = NULL; |
28 | 0 | g_autoptr(FuCrosEcVersion) version = g_new0(FuCrosEcVersion, 1); |
29 | |
|
30 | 0 | if (version_raw == NULL || strlen(version_raw) == 0) { |
31 | 0 | g_set_error_literal(error, |
32 | 0 | FWUPD_ERROR, |
33 | 0 | FWUPD_ERROR_INTERNAL, |
34 | 0 | "no version string to parse"); |
35 | 0 | return NULL; |
36 | 0 | } |
37 | | |
38 | | /* sample version string: cheese_v1.1.1755-4da9520 */ |
39 | 0 | ver = g_strrstr(board, "_v"); |
40 | 0 | if (ver == NULL) { |
41 | 0 | g_set_error_literal(error, |
42 | 0 | FWUPD_ERROR, |
43 | 0 | FWUPD_ERROR_INTERNAL, |
44 | 0 | "version marker not found"); |
45 | 0 | return NULL; |
46 | 0 | } |
47 | 0 | *ver = '\0'; |
48 | 0 | ver += 2; |
49 | 0 | marker_split = g_strsplit_set(ver, "-+", 2); |
50 | 0 | if (g_strv_length(marker_split) < 2) { |
51 | 0 | g_set_error(error, |
52 | 0 | FWUPD_ERROR, |
53 | 0 | FWUPD_ERROR_INTERNAL, |
54 | 0 | "hash marker not found: %s", |
55 | 0 | ver); |
56 | 0 | return NULL; |
57 | 0 | } |
58 | 0 | triplet_split = g_strsplit_set(marker_split[0], ".", 3); |
59 | 0 | if (g_strv_length(triplet_split) < 3) { |
60 | 0 | g_set_error(error, |
61 | 0 | FWUPD_ERROR, |
62 | 0 | FWUPD_ERROR_INTERNAL, |
63 | 0 | "improper version triplet: %s", |
64 | 0 | marker_split[0]); |
65 | 0 | return NULL; |
66 | 0 | } |
67 | | |
68 | 0 | version->triplet = |
69 | 0 | fu_strsafe(marker_split[0], FU_STRUCT_CROS_EC_FIRST_RESPONSE_PDU_SIZE_VERSION); |
70 | 0 | version->boardname = fu_strsafe(board, FU_STRUCT_CROS_EC_FIRST_RESPONSE_PDU_SIZE_VERSION); |
71 | 0 | if (version->boardname == NULL) { |
72 | 0 | g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, "empty board name"); |
73 | 0 | return NULL; |
74 | 0 | } |
75 | 0 | version->sha1 = |
76 | 0 | fu_strsafe(marker_split[1], FU_STRUCT_CROS_EC_FIRST_RESPONSE_PDU_SIZE_VERSION); |
77 | 0 | if (version->sha1 == NULL) { |
78 | 0 | g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, "empty SHA"); |
79 | 0 | return NULL; |
80 | 0 | } |
81 | 0 | version->dirty = (g_strrstr(ver, "+") != NULL); |
82 | 0 | return g_steal_pointer(&version); |
83 | 0 | } |