File: parser.c

package info (click to toggle)
swupdate 2025.12%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,004 kB
  • sloc: ansic: 66,621; python: 6,291; makefile: 791; sh: 538; javascript: 229
file content (317 lines) | stat: -rw-r--r-- 7,387 bytes parent folder | download | duplicates (3)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * (C) Copyright 2013
 * Stefano Babic, stefano.babic@swupdate.org.
 *
 * SPDX-License-Identifier:     GPL-2.0-only
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include "swupdate.h"
#include "parsers.h"
#include "util.h"
#include "progress.h"
#include "handler.h"
#include "swupdate_crypto.h"

static parser_fn parsers[] = {
	parse_cfg,
	parse_json,
	parse_external
};

typedef enum {
	IS_IMAGE_FILE,
	IS_SCRIPT,
	IS_PARTITION,
	IS_UNKNOWN
} IMGTYPE;

static inline IMGTYPE get_entry_type(struct img_type *img)
{
	if (!img->is_script && !img->is_partitioner)
		return IS_IMAGE_FILE;
	if (img->is_script)
		return IS_SCRIPT;
	if (img->is_partitioner)
		return IS_PARTITION;
	return IS_UNKNOWN;
}


#ifndef CONFIG_HASH_VERIFY
static int check_hash_absent(struct imglist *list)
{
	struct img_type *image;
	LIST_FOREACH(image, list, next) {
		if (strnlen((const char *)image->sha256, SHA256_HASH_LENGTH) > 0) {
			ERROR("hash verification not enabled but hash supplied for %s",
				  image->fname);
			return -EINVAL;
		}
	}
	return 0;
}
#endif

#ifdef CONFIG_SIGNED_IMAGES
/*
 * Check that all images in a list have a valid hash
 */
static int check_missing_hash(struct imglist *list)
{
	struct img_type *image;

	LIST_FOREACH(image, list, next) {
		/*
		 * Skip handler with no data because there is no image
		 * associated for this type
		 */
		if ( !(get_handler_mask(image) & NO_DATA_HANDLER) &&
				(!IsValidHash(image->sha256))) {
			ERROR("Hash not set for %s Type %s",
				image->fname,
				image->type);
			return -EINVAL;
		}
	}

	return 0;
}
#endif

static int check_handler(struct img_type *item, unsigned int mask, const char *desc)
{
	struct installer_handler *hnd;

	hnd = find_handler(item);
	if (!hnd) {
		ERROR("feature '%s' required for "
		      "'%s' in %s is absent!",
		      item->type, item->fname,
		      SW_DESCRIPTION_FILENAME);
		return -EINVAL;
	}

	if (!(hnd->mask & mask)) {
		ERROR("feature '%s' is not allowed for "
		      "'%s' in %s is absent!",
		      item->type, desc,
		      SW_DESCRIPTION_FILENAME);
		return -EINVAL;
	}

	return 0;
}

static int check_handler_list(struct imglist *list,
				unsigned int allowedmask,
				IMGTYPE type,
				const char *desc)
{
	struct img_type *item;
	int ret;
	if (!LIST_EMPTY(list)) {
		LIST_FOREACH(item, list, next)
		{
			if (get_entry_type(item) != type)
				continue;
			ret = check_handler(item, allowedmask, desc);

			if (ret < 0)
				return ret;
		}
	}

	return 0;
}

struct swupdate_type_cfg *swupdate_find_update_type(struct swupdate_type_list *list, const char *name)
{
	struct swupdate_type_cfg *type_cfg = NULL;

	LIST_FOREACH(type_cfg, list, next) {
		if (!strcmp(type_cfg->type_name, name)) {
			return type_cfg;
		}
	}
	return NULL;
}

int parse(struct swupdate_cfg *sw, const char *descfile)
{
	int ret = -1;
	parser_fn current;
#ifdef CONFIG_SIGNED_IMAGES
	char *sigfile;

	sigfile = malloc(strlen(descfile) + strlen(".sig") + 1);
	if (!sigfile)
		return -ENOMEM;
	strcpy(sigfile, descfile);
	strcat(sigfile, ".sig");

	ret = swupdate_verify_file(sw->dgst, sigfile, descfile,
				   sw->forced_signer_name);
	free(sigfile);

	if (ret)
		return ret;

#endif
	char *errors[ARRAY_SIZE(parsers)] = {0};

	for (unsigned int i = 0; i < ARRAY_SIZE(parsers); i++) {
		current = parsers[i];

		ret = current(sw, descfile, &errors[i]);

		if (ret == 0)
			break;
	}

	if (ret != 0) {
		for (unsigned int i = 0; i < ARRAY_SIZE(parsers); i++) {
			if (errors[i] != NULL) {
				ERROR("%s", errors[i]);
				free(errors[i]);
			}
		}
		ERROR("no parser available to parse " SW_DESCRIPTION_FILENAME "!");
		return ret;
	}

	for (unsigned int i = 0; i < ARRAY_SIZE(parsers); i++)
		if (errors[i] != NULL)
			free(errors[i]);

	ret = check_handler_list(&sw->scripts, SCRIPT_HANDLER, IS_SCRIPT, "scripts");
	ret |= check_handler_list(&sw->images, IMAGE_HANDLER | FILE_HANDLER, IS_IMAGE_FILE,
					"images / files");
	ret |= check_handler_list(&sw->images, PARTITION_HANDLER, IS_PARTITION,
					"partitions");
	if (ret)
		return -EINVAL;

	/*
	 *  Bootloader is slightly different, it has no image
	 *  but a list of variables
	 */
	struct img_type item_uboot = {.type = "uboot"};
	struct img_type item_bootloader = {.type = "bootenv"};
	if (!LIST_EMPTY(&sw->bootloader) &&
			(!find_handler(&item_uboot) &&
			 !find_handler(&item_bootloader))) {
		ERROR("bootloader support absent but %s has bootloader section!",
		      SW_DESCRIPTION_FILENAME);
		return -EINVAL;
	}

#ifdef CONFIG_SIGNED_IMAGES

	/*
	 * If the software must be verified, all images
	 * must have a valid hash to be checked
	 */
	if (check_missing_hash(&sw->images) ||
		check_missing_hash(&sw->scripts))
		ret = -EINVAL;
#else
#ifndef CONFIG_HASH_VERIFY
	if (check_hash_absent(&sw->images) ||
		check_hash_absent(&sw->scripts))
		ret = -EINVAL;
#endif
#endif

	/*
	 * Check if a an Update Type is set and
	 * load the configuration
	 */
	struct swupdate_type_cfg *update_type;
	if (!strnlen(sw->update_type_name, sizeof(sw->update_type_name) - 1)) {
		if (sw->update_type_required) {
			ERROR("Update Type is mandatory but it was not set");
			return -EINVAL;
		} else
			strlcpy(sw->update_type_name,
				"default",
				sizeof(sw->update_type_name));
	}
	update_type = swupdate_find_update_type(&sw->swupdate_types, sw->update_type_name);
	if (!update_type) {
		ERROR("Requested Update of Type %s but it is not configured", sw->update_type_name);
		return -EINVAL;
	}

	sw->update_type = update_type;

	/*
	 * If downgrading is not allowed, convert
	 * versions in numbers to be compared and check to get a
	 * newer version
	 */
	if (sw->update_type->no_downgrading) {
		if (compare_versions(sw->version, sw->update_type->minimum_version) < 0) {
			ERROR("No downgrading allowed: new version %s < installed %s",
				sw->version, sw->update_type->minimum_version);
			return -EPERM;
		}
	}

	/*
	 * Check if update is allowed until a chosen version, convert
	 * versions in numbers to be compared and check to get a
	 * newer version
	 */
	if (sw->update_type->check_max_version) {
		if (compare_versions(sw->version, sw->update_type->maximum_version) > 0) {
			ERROR("Max version set: new version %s > max allowed %s",
				sw->version, sw->update_type->maximum_version);
			return -EPERM;
		}
	}

	/*
	 * If reinstalling is not allowed, compare
	 * version strings
	 */
	if (sw->update_type->no_reinstalling) {

		if (strcmp(sw->version, sw->update_type->current_version) == 0) {
			ERROR("No reinstalling allowed: new version %s == installed %s",
				sw->version, sw->update_type->current_version);
			return -EPERM;
		}
	}

	/*
	 * Compute the total number of installer
	 * to initialize the progress bar
	 */
	unsigned int totalsteps = count_elem_list(&sw->images) +
					2 * count_elem_list(&sw->scripts);
	swupdate_progress_init(totalsteps);

	TRACE("Number of found artifacts: %d", count_elem_list(&sw->images));
	TRACE("Number of scripts: %d", count_elem_list(&sw->scripts));
	TRACE("Number of steps to be run: %d", totalsteps);


	/*
	 * Send the version string as first message to progress interface
	 */
	char *versioninfo;
	if (asprintf(&versioninfo, "{\"VERSION\" : \"%s\"}", sw->version) == ENOMEM_ASPRINTF)
		ERROR("OOM sending version info");
	else {
		swupdate_progress_info(RUN, NONE, versioninfo);
		free(versioninfo);
	}

	return ret;
}