summaryrefslogtreecommitdiff
path: root/libappstream-builder/plugins/asb-plugin-firmware.c
blob: d0da39949ac8d439316c3f0ccaab9fc481bb1b1e (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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2015 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <config.h>
#include <fnmatch.h>
#include <string.h>
#include <stdlib.h>

#include <asb-plugin.h>

/**
 * asb_plugin_get_name:
 */
const gchar *
asb_plugin_get_name (void)
{
	return "firmware";
}

/**
 * asb_plugin_add_globs:
 */
void
asb_plugin_add_globs (AsbPlugin *plugin, GPtrArray *globs)
{
	asb_plugin_add_glob (globs, "*.inf");
}

/**
 * _asb_plugin_check_filename:
 */
static gboolean
_asb_plugin_check_filename (const gchar *filename)
{
	if (asb_plugin_match_glob ("*.inf", filename))
		return TRUE;
	return FALSE;
}

/**
 * asb_plugin_check_filename:
 */
gboolean
asb_plugin_check_filename (AsbPlugin *plugin, const gchar *filename)
{
	return _asb_plugin_check_filename (filename);
}

/**
 * asb_plugin_firmware_sanitize_guid:
 */
static gchar *
asb_plugin_firmware_sanitize_guid (const gchar *id)
{
	GString *id_new;
	guint i;

	id_new = g_string_sized_new (strlen (id));

	for (i = 0; id[i] != '\0'; i++) {
		if (g_ascii_isalnum (id[i]) || id[i] == '-')
			g_string_append_c (id_new, id[i]);
	}
	return g_string_free (id_new, FALSE);
}

/**
 * asb_plugin_firmware_parse_date:
 */
static guint64
asb_plugin_firmware_parse_date (const gchar *date, GError **error)
{
	_cleanup_date_time_unref_ GDateTime *dt = NULL;
	_cleanup_strv_free_ gchar **split = NULL;

	/* split up into MM/DD/YYYY, because America */
	split = g_strsplit (date, "/", -1);
	if (g_strv_length (split) != 3) {
		g_set_error (error,
			     ASB_PLUGIN_ERROR,
			     ASB_PLUGIN_ERROR_NOT_SUPPORTED,
			     "DriverVer date invalid: %s", date);
		return 0;
	}
	dt = g_date_time_new_local (atoi (split[2]),
				    atoi (split[0]),
				    atoi (split[1]),
				    0, 0, 0);
	if (dt == NULL) {
		g_set_error (error,
			     ASB_PLUGIN_ERROR,
			     ASB_PLUGIN_ERROR_NOT_SUPPORTED,
			     "DriverVer date invalid: %s", date);
		return 0;
	}
	return g_date_time_to_unix (dt);
}

/**
 * asb_plugin_firmware_get_source_package:
 */
static gchar *
asb_plugin_firmware_get_source_package (const gchar *filename)
{
	gchar *basename;
	gchar *tmp;

	basename = g_path_get_basename (filename);
	tmp = g_strrstr (basename, ".inf");
	if (tmp != NULL)
		*tmp = '\0';
	return basename;
}

/**
 * asb_plugin_firmware_get_checksum:
 */
static gchar *
asb_plugin_firmware_get_checksum (const gchar *filename,
				  GChecksumType checksum_type,
				  GError **error)
{
	gsize len;
	_cleanup_free_ gchar *data = NULL;

	if (!g_file_get_contents (filename, &data, &len, error))
		return NULL;
	return g_compute_checksum_for_data (checksum_type, (const guchar *)data, len);
}

/**
 * asb_plugin_process_filename:
 */
static gboolean
asb_plugin_process_filename (AsbPlugin *plugin,
			     AsbPackage *pkg,
			     const gchar *filename,
			     GList **apps,
			     const gchar *tmpdir,
			     GError **error)
{
	guint64 timestamp;
	guint i;
	_cleanup_free_ gchar *checksum = NULL;
	_cleanup_free_ gchar *class = NULL;
	_cleanup_free_ gchar *comment = NULL;
	_cleanup_free_ gchar *date_version = NULL;
	_cleanup_free_ gchar *filename_full = NULL;
	_cleanup_free_ gchar *id_new = NULL;
	_cleanup_free_ gchar *id = NULL;
	_cleanup_free_ gchar *location_checksum = NULL;
	_cleanup_free_ gchar *location_url = NULL;
	_cleanup_free_ gchar *name = NULL;
	_cleanup_free_ gchar *srcpkg = NULL;
	_cleanup_free_ gchar *vendor = NULL;
	_cleanup_keyfile_unref_ GKeyFile *kf = NULL;
	_cleanup_object_unref_ AsbApp *app = NULL;
	_cleanup_object_unref_ AsIcon *icon = NULL;
	_cleanup_object_unref_ AsProvide *provide = NULL;
	_cleanup_object_unref_ AsRelease *release = NULL;
	_cleanup_strv_free_ gchar **date_version_split = NULL;

	/* fix up the inf file */
	filename_full = g_build_filename (tmpdir, filename, NULL);
	kf = as_utils_load_inf_file (filename_full, error);
	if (kf == NULL)
		return FALSE;

	/* parse */
	class = g_key_file_get_string (kf, "Version", "Class", NULL);
	if (g_strcmp0 (class, "Firmware") != 0) {
		g_set_error (error,
			     ASB_PLUGIN_ERROR,
			     ASB_PLUGIN_ERROR_NOT_SUPPORTED,
			     "Driver class is '%s', not 'Firmware'", class);
		return FALSE;
	}

	/* get the GUID */
	id = g_key_file_get_string (kf, "Version", "ClassGuid", NULL);
	if (id == NULL) {
		g_set_error_literal (error,
				     ASB_PLUGIN_ERROR,
				     ASB_PLUGIN_ERROR_NOT_SUPPORTED,
				     "Driver ID is missing");
		return FALSE;
	}

	/* strip any curley brackets */
	id_new = asb_plugin_firmware_sanitize_guid (id);
	app = asb_app_new (pkg, id_new);
	as_app_set_id_kind (AS_APP (app), AS_ID_KIND_FIRMWARE);

	/* add localizable strings */
	vendor = g_key_file_get_string (kf, "Strings", "MfgName", NULL);
	if (vendor == NULL)
		vendor = g_key_file_get_string (kf, "Strings", "Provider", NULL);
	if (vendor != NULL)
		as_app_set_developer_name (AS_APP (app), NULL, vendor, -1);
	name = g_key_file_get_string (kf, "Strings", "FirmwareDesc", NULL);
	if (name != NULL)
		as_app_set_name (AS_APP (app), NULL, name, -1);
	comment = g_key_file_get_string (kf, "Strings", "DiskName", NULL);
	if (comment != NULL)
		as_app_set_comment (AS_APP (app), NULL, comment, -1);

	/* get the release date and the version in case there's no metainfo */
	date_version = g_key_file_get_string (kf, "Version", "DriverVer", NULL);
	if (date_version == NULL) {
		g_set_error_literal (error,
				     ASB_PLUGIN_ERROR,
				     ASB_PLUGIN_ERROR_NOT_SUPPORTED,
				     "DriverVer is missing");
		return FALSE;
	}

	/* parse the DriverVer */
	date_version_split = g_strsplit (date_version, ",", -1);
	if (g_strv_length (date_version_split) != 2) {
		g_set_error (error,
			     ASB_PLUGIN_ERROR,
			     ASB_PLUGIN_ERROR_NOT_SUPPORTED,
			     "DriverVer is invalid: %s", date_version);
		return FALSE;
	}
	timestamp = asb_plugin_firmware_parse_date (date_version_split[0], error);
	if (timestamp == 0)
		return FALSE;

	/* add a release with no real description */
	release = as_release_new ();
	as_release_set_version (release, date_version_split[1], -1);
	as_release_set_timestamp (release, timestamp);
	as_app_add_release (AS_APP (app), release);

	/* this is a Linux extension */
	location_url = g_key_file_get_string (kf, "Location", "URLs", NULL);
	if (location_url != NULL) {
		_cleanup_strv_free_ gchar **location_urls = NULL;
		location_urls = g_strsplit (location_url, ",", -1);
		for (i = 0; location_urls[i] != NULL; i++)
			as_release_add_location (release, location_urls[i], -1);
	}

	/* checksum the .cab file */
	checksum = asb_plugin_firmware_get_checksum (asb_package_get_filename (pkg),
						     G_CHECKSUM_SHA1,
						     error);
	if (checksum == NULL)
		return FALSE;
	as_release_set_checksum (release, G_CHECKSUM_SHA1, checksum, -1);

	/* this is a hack */
	srcpkg = asb_plugin_firmware_get_source_package (filename);
	if (srcpkg != NULL)
		asb_package_set_source_pkgname (pkg, srcpkg);

	/* add icon */
	icon = as_icon_new ();
	as_icon_set_kind (icon, AS_ICON_KIND_STOCK);
	as_icon_set_name (icon, "application-x-executable", -1);
	as_app_add_icon (AS_APP (app), icon);

	asb_plugin_add_app (apps, AS_APP (app));

	/* add provide */
	return TRUE;
}

/**
 * asb_plugin_process:
 */
GList *
asb_plugin_process (AsbPlugin *plugin,
		    AsbPackage *pkg,
		    const gchar *tmpdir,
		    GError **error)
{
	gboolean ret;
	GError *error_local = NULL;
	GList *apps = NULL;
	guint i;
	gchar **filelist;

	filelist = asb_package_get_filelist (pkg);
	for (i = 0; filelist[i] != NULL; i++) {
		if (!_asb_plugin_check_filename (filelist[i]))
			continue;
		ret = asb_plugin_process_filename (plugin,
						   pkg,
						   filelist[i],
						   &apps,
						   tmpdir,
						   &error_local);
		if (!ret) {
			asb_package_log (pkg,
					 ASB_PACKAGE_LOG_LEVEL_INFO,
					 "Failed to process %s: %s",
					 filelist[i],
					 error_local->message);
			g_clear_error (&error_local);
		}
	}

	/* no desktop files we care about */
	if (apps == NULL) {
		g_set_error (error,
			     ASB_PLUGIN_ERROR,
			     ASB_PLUGIN_ERROR_FAILED,
			     "nothing interesting in %s",
			     asb_package_get_basename (pkg));
		return NULL;
	}
	return apps;
}

/**
 * asb_plugin_merge:
 */
void
asb_plugin_merge (AsbPlugin *plugin, GList *list)
{
	AsApp *app;
	GList *l;

	/* remove the source package name, which doesn't make sense */
	for (l = list; l != NULL; l = l->next) {
		app = AS_APP (l->data);
		if (as_app_get_id_kind (app) == AS_ID_KIND_FIRMWARE)
			as_app_set_source_pkgname (app, NULL, -1);
	}
}