summaryrefslogtreecommitdiffstats
path: root/mboxctl.c
blob: ccb58482bdf019cf055f5b41a74692a111305921 (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2018 IBM Corp.
#include "config.h"

#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <systemd/sd-bus.h>

#include "dbus.h"

#define USAGE \
"\nUsage: %s [--silent | -s] <command> [args]\n\n" \
"\t\t--silent\t\t- no output on the command line\n\n" \
"\tCommands: (num args)\n" \
"\t\t--ping\t\t\t- ping the daemon (0)\n" \
"\t\t--daemon-state\t\t- check state of the daemon (0)\n" \
"\t\t--lpc-state\t\t- check the state of the lpc mapping (0)\n" \
"\t\t--kill\t\t\t- stop the daemon [no flush] (0)\n" \
"\t\t--reset\t\t\t- hard reset the daemon state (0)\n" \
"\t\t--point-to-flash\t- point the lpc mapping back to flash (0)\n" \
"\t\t--suspend\t\t- suspend the daemon to inhibit flash accesses (0)\n" \
"\t\t--resume\t\t- resume the daemon (1)\n" \
"\t\t\targ[0]: < \"clean\" | \"modified\" >\n" \
"\t\t--clear-cache\t- tell the daemon to discard any caches (0)\n" \
"\t\t--backend <vpnor|mtd[:PATH]|file:PATH>\n"

#define NAME		"Mailbox Control"

static bool silent;

#define MSG_OUT(...)	do { if (!silent) { \
				fprintf(stdout, __VA_ARGS__); } \
			} while (0)
#define MSG_ERR(...)	do { if (!silent) { \
				fprintf(stderr, __VA_ARGS__); } \
			} while (0)

struct mboxctl_context {
	sd_bus *bus;
};

static void usage(char *name)
{
	MSG_OUT(USAGE, name);
	exit(0);
}

static int init_mboxctl_dbus(struct mboxctl_context *context)
{
	int rc;

	rc = sd_bus_default_system(&context->bus);
	if (rc < 0) {
		MSG_ERR("Failed to connect to the system bus: %s\n",
			strerror(-rc));
	}

	return rc;
}

static int mboxctl_directive(struct mboxctl_context *context, const char *cmd)
{
	sd_bus_error error = SD_BUS_ERROR_NULL;
	sd_bus_message *m = NULL;
	int rc;

	rc = sd_bus_message_new_method_call(context->bus, &m,
					    MBOX_DBUS_NAME,
					    MBOX_DBUS_OBJECT,
					    MBOX_DBUS_CONTROL_IFACE,
					    cmd);
	if (rc < 0) {
		MSG_ERR("Failed to init method call: %s\n",
			strerror(-rc));
		goto out;
	}

	rc = sd_bus_call(context->bus, m, 0, &error, NULL);
	if (rc < 0) {
		MSG_ERR("Failed to post message: %s\n", strerror(-rc));
	}

out:
	sd_bus_error_free(&error);
	sd_bus_message_unref(m);

	return rc < 0 ? rc : 0;
}

static int mboxctl_getter(struct mboxctl_context *context,
			  const char *property, uint8_t *resp)
{
	sd_bus_error error = SD_BUS_ERROR_NULL;

	return sd_bus_get_property_trivial(context->bus, MBOX_DBUS_NAME,
					   MBOX_DBUS_OBJECT,
					   MBOX_DBUS_CONTROL_IFACE,
					   property, &error, 'y', resp);
}

static int handle_cmd_ping(struct mboxctl_context *context)
{
	int rc;

	rc = mboxctl_directive(context, "Ping");
	MSG_OUT("Ping: %s\n", rc ? strerror(-rc) : "Success");

	return rc;
}

static int handle_cmd_daemon_state(struct mboxctl_context *context)
{
	uint8_t resp;
	int rc;

	rc = mboxctl_getter(context, "DaemonState", &resp);
	if (rc < 0)
		return rc;

	MSG_OUT("Daemon State: %s\n", resp == DAEMON_STATE_ACTIVE ?
				      "Active" : "Suspended");
	return 0;
}

static int handle_cmd_lpc_state(struct mboxctl_context *context)
{
	uint8_t resp;
	int rc;

	rc = mboxctl_getter(context, "LpcState", &resp);
	if (rc < 0)
		return rc;

	MSG_OUT("LPC Bus Maps: %s\n", resp == LPC_STATE_MEM ?
				      "BMC Memory" :
				      (resp == LPC_STATE_FLASH ?
				       "Flash Device" :
				       "Invalid System State"));

	return 0;
}

static int handle_cmd_kill(struct mboxctl_context *context)
{
	int rc;

	rc = mboxctl_directive(context, "Kill");
	MSG_OUT("Kill: %s\n", rc ? strerror(-rc) : "Success");

	return rc;
}

static int handle_cmd_reset(struct mboxctl_context *context)
{
	int rc;

	rc = mboxctl_directive(context, "Reset");
	MSG_OUT("Reset: %s\n", rc ? strerror(-rc) : "Success");

	return rc;
}

static int handle_cmd_suspend(struct mboxctl_context *context)
{
	int rc;

	rc = mboxctl_directive(context, "Suspend");
	MSG_OUT("Suspend: %s\n", rc ? strerror(-rc) : "Success");

	return rc;
}

static int handle_cmd_resume(struct mboxctl_context *context, char *sarg)
{
	sd_bus_error error = SD_BUS_ERROR_NULL;
	sd_bus_message *m = NULL, *n = NULL;
	uint8_t arg;
	int rc;

	if (!sarg) {
		MSG_ERR("Resume command takes an argument\n");
		return -EINVAL;
	}

	rc = sd_bus_message_new_method_call(context->bus, &m,
					    MBOX_DBUS_NAME,
					    MBOX_DBUS_OBJECT,
					    MBOX_DBUS_CONTROL_IFACE,
					    "Resume");
	if (rc < 0) {
		MSG_ERR("Failed to init method call: %s\n",
			strerror(-rc));
		goto out;
	}

	if (!strncmp(sarg, "clean", strlen("clean"))) {
		arg = RESUME_NOT_MODIFIED;
	} else if (!strncmp(sarg, "modified", strlen("modified"))) {
		arg = RESUME_FLASH_MODIFIED;
	} else {
		MSG_ERR("Resume command takes argument < \"clean\" | "
			"\"modified\" >\n");
		rc = -EINVAL;
		goto out;
	}

	rc = sd_bus_message_append(m, "b", arg);
	if (rc < 0) {
		MSG_ERR("Failed to add args to message: %s\n",
			strerror(-rc));
		goto out;
	}

	rc = sd_bus_call(context->bus, m, 0, &error, &n);
	if (rc < 0) {
		MSG_ERR("Failed to post message: %s\n", strerror(-rc));
		goto out;
	}

	MSG_OUT("Resume: %s\n", rc < 0 ? strerror(-rc) : "Success");

out:
	sd_bus_error_free(&error);
	sd_bus_message_unref(m);
	sd_bus_message_unref(n);

	return rc < 0 ? rc : 0;
}

static int handle_cmd_modified(struct mboxctl_context *context)
{
	int rc;

	rc = mboxctl_directive(context, "MarkFlashModified");
	MSG_OUT("Clear Cache: %s\n", rc ? strerror(-rc) : "Success");

	return rc;
}

static int handle_cmd_backend(struct mboxctl_context *context, char *sarg)
{
	sd_bus_error error = SD_BUS_ERROR_NULL;
	sd_bus_message *m = NULL, *n = NULL;
	char *delim = NULL;
	char *strv[2];
	int rc;

	if (!sarg) {
		MSG_ERR("Backend command takes an argument\n");
		return -EINVAL;
	}

	rc = sd_bus_message_new_method_call(context->bus, &m,
					    MBOX_DBUS_NAME,
					    MBOX_DBUS_OBJECT,
					    MBOX_DBUS_CONTROL_IFACE,
					    "SetBackend");
	if (rc < 0) {
		MSG_ERR("Failed to init method call: %s\n",
			strerror(-rc));
		goto out;
	}

	if (!strncmp(sarg, "vpnor", strlen("vpnor"))) {
		if (strchr(sarg, ':')) {
			MSG_ERR("Path parameter not supported for vpnor\n");
			rc = -EINVAL;
			goto out;
		}

		rc = sd_bus_message_append(m, "s", "vpnor");
		if (rc < 0)
			goto out;
	} else if (!strncmp(sarg, "mtd", strlen("mtd"))) {
		rc = sd_bus_message_append(m, "s", "mtd");
		if (rc < 0)
			goto out;
	} else if (!strncmp(sarg, "file", strlen("file"))) {
		rc = sd_bus_message_append(m, "s", "file");
		if (rc < 0)
			goto out;
	} else {
		rc = -EINVAL;
		goto out;
	}

	delim = strchr(sarg, ':');
	if (delim) {
		char *path;

		path = realpath(delim + 1, NULL);
		if (!path) {
			MSG_ERR("Failed to resolve path: %s\n",
					strerror(errno));
			rc = -errno;
			goto out;
		}

		strv[0] = path;
		strv[1] = NULL;

		rc = sd_bus_message_append_strv(m, &strv[0]);
		free(path);
		if (rc < 0)
			goto out;
	} else {
		strv[0] = NULL;
		strv[1] = NULL;
		rc = sd_bus_message_append_strv(m, &strv[0]);
		if (rc < 0)
			goto out;
	}

	rc = sd_bus_call(context->bus, m, 0, &error, &n);
	if (rc < 0) {
		MSG_ERR("Failed to post message: %s\n", strerror(-rc));
		goto out;
	}

	MSG_OUT("SetBackend: %s\n", rc < 0 ? strerror(-rc) : "Success");

out:
	sd_bus_error_free(&error);
	sd_bus_message_unref(m);

	return rc < 0 ? rc : 0;
}

static int parse_cmdline(struct mboxctl_context *context, int argc, char **argv)
{
	int opt, rc = -1;

	static const struct option long_options[] = {
		{ "silent",		no_argument,		0, 's' },
		{ "ping",		no_argument,		0, 'p' },
		{ "daemon-state",	no_argument,		0, 'd' },
		{ "lpc-state",		no_argument,		0, 'l' },
		{ "kill",		no_argument,		0, 'k' },
		{ "reset",		no_argument,		0, 'r' },
		{ "point-to-flash",	no_argument,		0, 'f' },
		{ "suspend",		no_argument,		0, 'u' },
		{ "resume",		required_argument,	0, 'e' },
		{ "clear-cache",	no_argument,		0, 'c' },
		{ "backend",		required_argument,	0, 'b' },
		{ "version",		no_argument,		0, 'v' },
		{ "help",		no_argument,		0, 'h' },
		{ 0,			0,			0, 0   }
	};

	if (argc <= 1) {
		usage(argv[0]);
		return -EINVAL;
	}

	while ((opt = getopt_long(argc, argv, "spdlkrfue:cvh", long_options,
				  NULL)) != -1) {
		switch (opt) {
		case 's':
			silent = true;
			continue;
		case 'p':
			rc = handle_cmd_ping(context);
			break;
		case 'd':
			rc = handle_cmd_daemon_state(context);
			break;
		case 'l':
			rc = handle_cmd_lpc_state(context);
			break;
		case 'k':
			rc = handle_cmd_kill(context);
			break;
		case 'r': /* These are the same for now (reset may change) */
		case 'f':
			rc = handle_cmd_reset(context);
			break;
		case 'u':
			rc = handle_cmd_suspend(context);
			break;
		case 'e':
			rc = handle_cmd_resume(context, optarg);
			break;
		case 'c':
			rc = handle_cmd_modified(context);
			break;
		case 'b':
			rc = handle_cmd_backend(context, optarg);
			break;
		case 'v':
			MSG_OUT("%s V%s\n", NAME, PACKAGE_VERSION);
			rc = 0;
			break;
		case 'h':
			usage(argv[0]);
			rc = 0;
			break;
		default:
			usage(argv[0]);
			rc = -EINVAL;
			break;
		}
	}

	return rc;
}

int main(int argc, char **argv)
{
	struct mboxctl_context context;
	int rc;

	silent = false;

	rc = init_mboxctl_dbus(&context);
	if (rc < 0) {
		MSG_ERR("Failed to init dbus\n");
		return rc;
	}

	rc = parse_cmdline(&context, argc, argv);

	return rc;
}
OpenPOWER on IntegriCloud