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