summaryrefslogtreecommitdiff
path: root/src/util/virfirmware.c
blob: 33d2789ca71ae949b7fb55fc4654c8f7e86bfba4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * virfirmware.c: Definition of firmware object and supporting functions
 *
 * Copyright (C) 2016 SUSE LINUX Products GmbH, Nuernberg, Germany.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "virerror.h"
#include "virfirmware.h"
#include "virlog.h"
#include "virstring.h"

#define VIR_FROM_THIS VIR_FROM_NONE

VIR_LOG_INIT("util.firmware");


void
virFirmwareFree(virFirmware *firmware)
{
    if (!firmware)
        return;

    g_free(firmware->name);
    g_free(firmware->nvram);
    g_free(firmware);
}


void
virFirmwareFreeList(virFirmware **firmwares, size_t nfirmwares)
{
    size_t i;

    for (i = 0; i < nfirmwares; i++)
        virFirmwareFree(firmwares[i]);

    g_free(firmwares);
}


int
virFirmwareParse(const char *str, virFirmware *firmware)
{
    g_auto(GStrv) token = NULL;

    if (!(token = g_strsplit(str, ":", 0)))
        return -1;

    if (token[0]) {
        virSkipSpaces((const char **) &token[0]);
        if (token[1])
            virSkipSpaces((const char **) &token[1]);
    }

    /* Exactly two tokens are expected */
    if (!token[0] || !token[1] || token[2] ||
        STREQ(token[0], "") || STREQ(token[1], "")) {
        virReportError(VIR_ERR_CONF_SYNTAX,
                       _("Invalid nvram format: '%1$s'"),
                       str);
        return -1;
    }

    firmware->name = g_strdup(token[0]);
    firmware->nvram = g_strdup(token[1]);

    return 0;
}


int
virFirmwareParseList(const char *list,
                     virFirmware ***firmwares,
                     size_t *nfirmwares)
{
    g_auto(GStrv) token = NULL;
    size_t i, j;

    if (!(token = g_strsplit(list, ":", 0)))
        return -1;

    for (i = 0; token[i]; i += 2) {
        if (!token[i] || !token[i + 1] ||
            STREQ(token[i], "") || STREQ(token[i + 1], "")) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Invalid --with-loader-nvram list: %1$s"),
                           list);
            return -1;
        }
    }

    if (i) {
        *firmwares = g_new0(virFirmware *, i / 2);
        *nfirmwares = i / 2;

        for (j = 0; j < i / 2; j++) {
            virFirmware **fws = *firmwares;

            fws[j] = g_new0(virFirmware, 1);
            fws[j]->name = g_strdup(token[2 * j]);
            fws[j]->nvram = g_strdup(token[2 * j + 1]);
        }
    }

    return 0;
}