discover/platform-powerpc: Read and write password hash from NVRAM
[petitboot] / lib / param_list / param_list.c
1
2 #define _GNU_SOURCE
3
4 #include <assert.h>
5 #include <string.h>
6
7 #include <log/log.h>
8 #include <talloc/talloc.h>
9 #include <param_list/param_list.h>
10
11 const char **common_known_params(void)
12 {
13         static const char *common[] = {
14                 "auto-boot?",
15                 "petitboot,network",
16                 "petitboot,timeout",
17                 "petitboot,bootdevs",
18                 "petitboot,language",
19                 "petitboot,debug?",
20                 "petitboot,write?",
21                 "petitboot,snapshots?",
22                 "petitboot,console",
23                 "petitboot,http_proxy",
24                 "petitboot,https_proxy",
25                 "petitboot,password",
26                 NULL,
27         };
28
29         return common;
30 }
31
32 void param_list_init(struct param_list *pl, const char *known_params[])
33 {
34         assert(known_params);
35         list_init(&pl->params);
36         pl->known_params = known_params;
37 }
38
39 bool param_list_is_known_n(const struct param_list *pl, const char *name,
40         unsigned int name_len)
41 {
42         const char **known;
43
44         assert(pl->known_params);
45
46         param_list_for_each_known_param(pl, known) {
47                 if (name_len == strlen(*known) && !strncmp(name, *known, name_len)) {
48                         return true;
49                 }
50         }
51
52         return false;
53 }
54
55 bool param_list_is_known(const struct param_list *pl, const char *name)
56 {
57         const char **known;
58
59         assert(pl->known_params);
60
61         param_list_for_each_known_param(pl, known) {
62                 if (!strcmp(name, *known)) {
63                         return true;
64                 }
65         }
66
67         return false;
68 }
69
70 struct param *param_list_get_param(struct param_list *pl, const char *name)
71 {
72         struct param *param;
73
74         param_list_for_each(pl, param) {
75                 if (!strcmp(param->name, name))
76                         return param;
77         }
78         return NULL;
79 }
80
81 void param_list_set(struct param_list *pl, const char *name, const char *value,
82         bool modified_on_create)
83 {
84         struct param *param;
85
86         param_list_for_each(pl, param) {
87                 if (strcmp(param->name, name))
88                         continue;
89
90                 if (!strcmp(param->value, value))
91                         return;
92
93                 /* Update existing list entry. */
94                 talloc_free(param->value);
95                 param->value = talloc_strdup(param, value);
96                 param->modified = true;
97                 pb_debug_fn("Updated: %s:%s\n", name, value);
98                 return;
99         }
100
101         /* Add new entry to list. */
102         param = talloc(pl, struct param);
103         param->modified = modified_on_create;
104         param->name = talloc_strdup(pl, name);
105         param->value = talloc_strdup(pl, value);
106         list_add(&pl->params, &param->list);
107         pb_debug_fn("Created: %s:%s\n", name, value);
108 }
109
110 void param_list_set_non_empty(struct param_list *pl, const char *name, const char *value,
111         bool modified_on_create)
112 {
113         if (!param_list_get_value(pl, name) && !strlen(value)) {
114                 return;
115         }
116
117         param_list_set(pl, name, value, modified_on_create);
118 }
119