xref: /openbmc/hiomapd/mboxd.c (revision a042978b)
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright (C) 2018 IBM Corp.
3 
4 #define _GNU_SOURCE
5 #include <assert.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <getopt.h>
9 #include <limits.h>
10 #include <poll.h>
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <syslog.h>
17 #include <signal.h>
18 #include <sys/ioctl.h>
19 #include <sys/mman.h>
20 #include <sys/stat.h>
21 #include <sys/timerfd.h>
22 #include <sys/types.h>
23 #include <sys/signalfd.h>
24 #include <time.h>
25 #include <unistd.h>
26 #include <inttypes.h>
27 #include <systemd/sd-bus.h>
28 
29 #include "config.h"
30 #include "mboxd.h"
31 #include "common.h"
32 #include "dbus.h"
33 #include "control_dbus.h"
34 #include "backend.h"
35 #include "lpc.h"
36 #include "transport_mbox.h"
37 #include "transport_dbus.h"
38 #include "windows.h"
39 #include "vpnor/mboxd_pnor_partition_table.h"
40 
41 const char* USAGE =
42 	"\nUsage: %s [-V | --version] [-h | --help] [-v[v] | --verbose] [-s | --syslog]\n"
43 	"\t\t[-n | --window-num <num>]\n"
44 	"\t\t[-w | --window-size <size>M]\n"
45 	"\t\t-f | --flash <size>[K|M]\n"
46 #ifdef VIRTUAL_PNOR_ENABLED
47 	"\t\t-s | --source <vpnor|path>\n\n"
48 #else
49 	"\t\t-s | --source <path>\n\n"
50 #endif
51 	"\t-v | --verbose\t\tBe [more] verbose\n"
52 	"\t-s | --syslog\t\tLog output to syslog (pointless without -v)\n"
53 	"\t-n | --window-num\tThe number of windows\n"
54 	"\t\t\t\t(default: fill the reserved memory region)\n"
55 	"\t-w | --window-size\tThe window size (power of 2) in MB\n"
56 	"\t\t\t\t(default: 1MB)\n"
57 	"\t-f | --flash\t\tSize of flash in [K|M] bytes\n\n";
58 
59 static int dbus_init(struct mbox_context *context,
60 		     const struct transport_ops **ops)
61 {
62 	int rc;
63 
64 	rc = sd_bus_default_system(&context->bus);
65 	if (rc < 0) {
66 		MSG_ERR("Failed to connect to the system bus: %s\n",
67 			strerror(-rc));
68 		return rc;
69 	}
70 
71 	rc = control_legacy_init(context);
72 	if (rc < 0) {
73 		MSG_ERR("Failed to initialise legacy DBus interface: %s\n",
74 			strerror(-rc));
75 		return rc;
76 	}
77 
78 	rc = control_dbus_init(context);
79 	if (rc < 0) {
80 		MSG_ERR("Failed to initialise DBus control interface: %s\n",
81 			strerror(-rc));
82 		return rc;
83 	}
84 
85 	rc = transport_dbus_init(context, ops);
86 	if (rc < 0) {
87 		MSG_ERR("Failed to initialise DBus protocol interface: %s\n",
88 			strerror(-rc));
89 		return rc;
90 	}
91 
92 	rc = sd_bus_request_name(context->bus, MBOX_DBUS_NAME,
93 				 SD_BUS_NAME_ALLOW_REPLACEMENT |
94 				 SD_BUS_NAME_REPLACE_EXISTING);
95 	if (rc < 0) {
96 		MSG_ERR("Failed to request DBus name: %s\n", strerror(-rc));
97 		return rc;
98 	}
99 
100 	rc = sd_bus_get_fd(context->bus);
101 	if (rc < 0) {
102 		MSG_ERR("Failed to get bus fd: %s\n", strerror(-rc));
103 		return rc;
104 	}
105 
106 	context->fds[DBUS_FD].fd = rc;
107 
108 	return 0;
109 }
110 
111 static void dbus_free(struct mbox_context *context)
112 {
113 	transport_dbus_free(context);
114 	control_dbus_free(context);
115 	control_legacy_free(context);
116 	sd_bus_unref(context->bus);
117 }
118 
119 static int poll_loop(struct mbox_context *context)
120 {
121 	int rc = 0, i;
122 
123 	/* Set POLLIN on polling file descriptors */
124 	for (i = 0; i < POLL_FDS; i++) {
125 		context->fds[i].events = POLLIN;
126 	}
127 
128 	while (1) {
129 		rc = poll(context->fds, POLL_FDS, -1);
130 
131 		if (rc < 0) { /* Error */
132 			MSG_ERR("Error from poll(): %s\n", strerror(errno));
133 			break; /* This should mean we clean up nicely */
134 		}
135 
136 		/* Event on Polled File Descriptor - Handle It */
137 		if (context->fds[SIG_FD].revents & POLLIN) { /* Signal */
138 			struct signalfd_siginfo info = { 0 };
139 
140 			rc = read(context->fds[SIG_FD].fd, (void *) &info,
141 				  sizeof(info));
142 			if (rc != sizeof(info)) {
143 				MSG_ERR("Error reading signal event: %s\n",
144 					strerror(errno));
145 			}
146 
147 			MSG_DBG("Received signal: %d\n", info.ssi_signo);
148 			switch (info.ssi_signo) {
149 			case SIGINT:
150 			case SIGTERM:
151 				MSG_INFO("Caught Signal - Exiting...\n");
152 				context->terminate = true;
153 				break;
154 			case SIGHUP:
155 				rc = protocol_reset(context);
156 				if (rc < 0) {
157 					MSG_ERR("Failed to reset on SIGHUP\n");
158 				}
159 				break;
160 			default:
161 				MSG_ERR("Unhandled Signal: %d\n",
162 					info.ssi_signo);
163 				break;
164 			}
165 		}
166 		if (context->fds[DBUS_FD].revents & POLLIN) { /* DBUS */
167 			while ((rc = sd_bus_process(context->bus, NULL)) > 0) {
168 				MSG_DBG("DBUS Event\n");
169 			}
170 			if (rc < 0) {
171 				MSG_ERR("Error handling DBUS event: %s\n",
172 						strerror(-rc));
173 			}
174 		}
175 		if (context->terminate) {
176 			break; /* This should mean we clean up nicely */
177 		}
178 		if (context->fds[MBOX_FD].revents & POLLIN) { /* MBOX */
179 			MSG_DBG("MBOX Event\n");
180 			rc = transport_mbox_dispatch(context);
181 			if (rc < 0) {
182 				MSG_ERR("Error handling MBOX event\n");
183 			}
184 		}
185 	}
186 
187 	rc = protocol_reset(context);
188 	if (rc < 0) {
189 		MSG_ERR("Failed to reset during poll loop cleanup\n");
190 	}
191 
192 	return rc;
193 }
194 
195 static int init_signals(struct mbox_context *context, sigset_t *set)
196 {
197 	int rc;
198 
199 	/* Block SIGHUPs, SIGTERMs and SIGINTs */
200 	sigemptyset(set);
201 	sigaddset(set, SIGHUP);
202 	sigaddset(set, SIGINT);
203 	sigaddset(set, SIGTERM);
204 	rc = sigprocmask(SIG_BLOCK, set, NULL);
205 	if (rc < 0) {
206 		MSG_ERR("Failed to set SIG_BLOCK mask %s\n", strerror(errno));
207 		return rc;
208 	}
209 
210 	/* Get Signal File Descriptor */
211 	rc = signalfd(-1, set, SFD_NONBLOCK);
212 	if (rc < 0) {
213 		MSG_ERR("Failed to get signalfd %s\n", strerror(errno));
214 		return rc;
215 	}
216 
217 	context->fds[SIG_FD].fd = rc;
218 	return 0;
219 }
220 
221 static void usage(const char *name)
222 {
223 	printf(USAGE, name);
224 }
225 
226 static bool parse_cmdline(int argc, char **argv,
227 			  struct mbox_context *context)
228 {
229 	char *endptr;
230 	int opt;
231 
232 	static const struct option long_options[] = {
233 		{ "flash",		required_argument,	0, 'f' },
234 		{ "backend",		required_argument,	0, 'b' },
235 		{ "window-size",	optional_argument,	0, 'w' },
236 		{ "window-num",		optional_argument,	0, 'n' },
237 		{ "verbose",		no_argument,		0, 'v' },
238 		{ "syslog",		no_argument,		0, 's' },
239 		{ "version",		no_argument,		0, 'V' },
240 		{ "help",		no_argument,		0, 'h' },
241 		{ 0,			0,			0, 0   }
242 	};
243 
244 	verbosity = MBOX_LOG_NONE;
245 	mbox_vlog = &mbox_log_console;
246 
247 	context->current = NULL; /* No current window */
248 
249 	while ((opt = getopt_long(argc, argv, "f:b:w::n::vsVh", long_options, NULL))
250 			!= -1) {
251 		switch (opt) {
252 		case 0:
253 			break;
254 		case 'f':
255 			context->backend.flash_size = strtol(optarg, &endptr, 10);
256 			if (optarg == endptr) {
257 				fprintf(stderr, "Unparseable flash size\n");
258 				return false;
259 			}
260 			switch (*endptr) {
261 			case '\0':
262 				break;
263 			case 'M':
264 				context->backend.flash_size <<= 10;
265 			case 'K':
266 				context->backend.flash_size <<= 10;
267 				break;
268 			default:
269 				fprintf(stderr, "Unknown units '%c'\n",
270 					*endptr);
271 				return false;
272 			}
273 			break;
274 		case 'b':
275 			context->path = optarg;
276 			break;
277 		case 'n':
278 			context->windows.num = strtol(argv[optind], &endptr,
279 						      10);
280 			if (optarg == endptr || *endptr != '\0') {
281 				fprintf(stderr, "Unparseable window num\n");
282 				return false;
283 			}
284 			break;
285 		case 'w':
286 			context->windows.default_size = strtol(argv[optind],
287 							       &endptr, 10);
288 			context->windows.default_size <<= 20; /* Given in MB */
289 			if (optarg == endptr || (*endptr != '\0' &&
290 						 *endptr != 'M')) {
291 				fprintf(stderr, "Unparseable window size\n");
292 				return false;
293 			}
294 			if (!is_power_of_2(context->windows.default_size)) {
295 				fprintf(stderr, "Window size not power of 2\n");
296 				return false;
297 			}
298 			break;
299 		case 'v':
300 			verbosity++;
301 			break;
302 		case 's':
303 			/* Avoid a double openlog() */
304 			if (mbox_vlog != &vsyslog) {
305 				openlog(PREFIX, LOG_ODELAY, LOG_DAEMON);
306 				mbox_vlog = &vsyslog;
307 			}
308 			break;
309 		case 'V':
310 			printf("%s V%s\n", THIS_NAME, PACKAGE_VERSION);
311 			exit(0);
312 		case 'h':
313 			return false; /* This will print the usage message */
314 		default:
315 			return false;
316 		}
317 	}
318 
319 	if (!context->path) {
320 		context->path = get_dev_mtd();
321 	}
322 	if (!context->backend.flash_size) {
323 		fprintf(stderr, "Must specify a non-zero flash size\n");
324 		return false;
325 	}
326 
327 	MSG_INFO("Flash size: 0x%.8x\n", context->backend.flash_size);
328 
329 	if (verbosity) {
330 		MSG_INFO("%s logging\n", verbosity == MBOX_LOG_DEBUG ? "Debug" :
331 					"Verbose");
332 	}
333 
334 	return true;
335 }
336 
337 static int mboxd_backend_init(struct mbox_context *context)
338 {
339 	int rc;
340 
341 #ifdef VIRTUAL_PNOR_ENABLED
342 	struct vpnor_partition_paths paths;
343 	vpnor_default_paths(&paths);
344 
345 	rc = backend_probe_vpnor(&context->backend, &paths);
346 	if(rc)
347 #endif
348 	{
349 		rc = backend_probe_mtd(&context->backend, context->path);
350 		if (rc) {
351 			rc = backend_probe_file(&context->backend,
352 						context->path);
353 		}
354 	}
355 
356 	return rc;
357 }
358 
359 int main(int argc, char **argv)
360 {
361 	const struct transport_ops *mbox_ops, *dbus_ops;
362 	struct mbox_context *context;
363 	char *name = argv[0];
364 	sigset_t set;
365 	int rc, i;
366 
367 	context = calloc(1, sizeof(*context));
368 	if (!context) {
369 		fprintf(stderr, "Memory allocation failed\n");
370 		exit(1);
371 	}
372 
373 	if (!parse_cmdline(argc, argv, context)) {
374 		usage(name);
375 		free(context);
376 		exit(0);
377 	}
378 
379 	for (i = 0; i < TOTAL_FDS; i++) {
380 		context->fds[i].fd = -1;
381 	}
382 
383 	MSG_INFO("Starting Daemon\n");
384 
385 	rc = init_signals(context, &set);
386 	if (rc) {
387 		goto finish;
388 	}
389 
390 	rc = mboxd_backend_init(context);
391 	if (rc) {
392 		goto finish;
393 	}
394 
395 	rc = protocol_init(context);
396 	if (rc) {
397 		goto finish;
398 	}
399 
400 	rc = transport_mbox_init(context, &mbox_ops);
401 	if (rc) {
402 		goto finish;
403 	}
404 
405 	rc = lpc_dev_init(context);
406 	if (rc) {
407 		goto finish;
408 	}
409 
410 	/* We've found the reserved memory region -> we can assign to windows */
411 	rc = windows_init(context);
412 	if (rc) {
413 		goto finish;
414 	}
415 
416 	rc = dbus_init(context, &dbus_ops);
417 	if (rc) {
418 		goto finish;
419 	}
420 
421 	/* Set the LPC bus mapping */
422 	__protocol_reset(context);
423 
424 	/* We're ready to go, alert the host */
425 	context->bmc_events |= BMC_EVENT_DAEMON_READY;
426 	context->bmc_events |= BMC_EVENT_PROTOCOL_RESET;
427 
428 	/* Alert on all supported transports */
429 	rc = protocol_events_put(context, mbox_ops);
430 	if (rc) {
431 		goto finish;
432 	}
433 
434 	rc = protocol_events_put(context, dbus_ops);
435 	if (rc) {
436 		goto finish;
437 	}
438 
439 	MSG_INFO("Entering Polling Loop\n");
440 	rc = poll_loop(context);
441 
442 	MSG_INFO("Exiting Poll Loop: %d\n", rc);
443 
444 finish:
445 	MSG_INFO("Daemon Exiting...\n");
446 	context->bmc_events &= ~BMC_EVENT_DAEMON_READY;
447 	context->bmc_events |= BMC_EVENT_PROTOCOL_RESET;
448 
449 	/* Alert on all supported transports */
450 	protocol_events_put(context, mbox_ops);
451 	protocol_events_put(context, dbus_ops);
452 
453 	dbus_free(context);
454 	backend_free(&context->backend);
455 	lpc_dev_free(context);
456 	transport_mbox_free(context);
457 	windows_free(context);
458 	protocol_free(context);
459 	free(context);
460 
461 	return rc;
462 }
463