blob: dece231106aeddebbe5e8e00f1b8c4b64718ecf3 [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Cyril Burc85e34d2016-11-15 11:50:41 +11003
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11004#define _GNU_SOURCE
Cyril Burc85e34d2016-11-15 11:50:41 +11005#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>
Michael Neuling899ebac2017-01-14 11:20:26 -060017#include <signal.h>
Cyril Burc85e34d2016-11-15 11:50:41 +110018#include <sys/ioctl.h>
19#include <sys/mman.h>
20#include <sys/stat.h>
21#include <sys/timerfd.h>
22#include <sys/types.h>
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110023#include <sys/signalfd.h>
Cyril Burc85e34d2016-11-15 11:50:41 +110024#include <time.h>
25#include <unistd.h>
Andrew Jeffery78210b92017-01-13 13:06:09 +103026#include <inttypes.h>
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110027#include <systemd/sd-bus.h>
Cyril Burc85e34d2016-11-15 11:50:41 +110028
Suraj Jitindar Singh8d65bb42017-05-01 16:05:17 +100029#include "config.h"
Andrew Jeffery26558db2018-08-10 00:22:38 +093030#include "mboxd.h"
Cyril Burc85e34d2016-11-15 11:50:41 +110031#include "common.h"
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110032#include "dbus.h"
Andrew Jeffery55f4d6f2018-08-06 12:26:44 +093033#include "control_dbus.h"
Andrew Jefferyeebc6bd2018-08-08 10:38:19 +093034#include "flash.h"
Andrew Jefferycd186112018-08-08 10:47:55 +093035#include "lpc.h"
Andrew Jeffery457a6e52018-08-08 11:21:08 +093036#include "transport_mbox.h"
Andrew Jeffery23140be2018-09-05 14:15:07 +093037#include "transport_dbus.h"
Andrew Jefferyf593b1b2018-08-08 11:01:04 +093038#include "windows.h"
Andrew Jeffery53c21aa2018-03-26 11:56:16 +103039#include "vpnor/mboxd_pnor_partition_table.h"
Cyril Burc85e34d2016-11-15 11:50:41 +110040
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110041#define USAGE \
42"\nUsage: %s [-V | --version] [-h | --help] [-v[v] | --verbose] [-s | --syslog]\n" \
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +100043"\t\t[-n | --window-num <num>]\n" \
44"\t\t[-w | --window-size <size>M]\n" \
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110045"\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" \
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +100049"\t\t\t\t(default: fill the reserved memory region)\n" \
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110050"\t-w | --window-size\tThe window size (power of 2) in MB\n" \
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +100051"\t\t\t\t(default: 1MB)\n" \
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110052"\t-f | --flash\t\tSize of flash in [K|M] bytes\n\n"
Cyril Burc85e34d2016-11-15 11:50:41 +110053
Andrew Jefferyef9e62d2018-08-08 15:48:27 +093054static int dbus_init(struct mbox_context *context)
55{
56 int rc;
57
58 rc = sd_bus_default_system(&context->bus);
59 if (rc < 0) {
60 MSG_ERR("Failed to connect to the system bus: %s\n",
61 strerror(-rc));
62 return rc;
63 }
64
65 rc = control_legacy_init(context);
66 if (rc < 0) {
67 MSG_ERR("Failed to initialise legacy DBus interface: %s\n",
68 strerror(-rc));
69 return rc;
70 }
71
72 rc = control_dbus_init(context);
73 if (rc < 0) {
74 MSG_ERR("Failed to initialise DBus control interface: %s\n",
75 strerror(-rc));
76 return rc;
77 }
78
Andrew Jeffery23140be2018-09-05 14:15:07 +093079 rc = transport_dbus_init(context);
80 if (rc < 0) {
81 MSG_ERR("Failed to initialise DBus protocol interface: %s\n",
82 strerror(-rc));
83 return rc;
84 }
85
Andrew Jefferyef9e62d2018-08-08 15:48:27 +093086 rc = sd_bus_request_name(context->bus, MBOX_DBUS_NAME,
87 SD_BUS_NAME_ALLOW_REPLACEMENT |
88 SD_BUS_NAME_REPLACE_EXISTING);
89 if (rc < 0) {
Andrew Jeffery23140be2018-09-05 14:15:07 +093090 MSG_ERR("Failed to request DBus name: %s\n", strerror(-rc));
Andrew Jefferyef9e62d2018-08-08 15:48:27 +093091 return rc;
92 }
93
94 rc = sd_bus_get_fd(context->bus);
95 if (rc < 0) {
96 MSG_ERR("Failed to get bus fd: %s\n", strerror(-rc));
97 return rc;
98 }
99
100 context->fds[DBUS_FD].fd = rc;
101
102 return 0;
103}
104
105static void dbus_free(struct mbox_context *context)
106{
Andrew Jeffery23140be2018-09-05 14:15:07 +0930107 transport_dbus_free(context);
Andrew Jefferyef9e62d2018-08-08 15:48:27 +0930108 control_dbus_free(context);
109 control_legacy_free(context);
110 sd_bus_unref(context->bus);
111}
Andrew Jeffery55f4d6f2018-08-06 12:26:44 +0930112
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100113static int poll_loop(struct mbox_context *context)
Cyril Burc85e34d2016-11-15 11:50:41 +1100114{
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100115 int rc = 0, i;
Cyril Bur46233672017-01-16 13:33:26 +1100116
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100117 /* Set POLLIN on polling file descriptors */
118 for (i = 0; i < POLL_FDS; i++) {
119 context->fds[i].events = POLLIN;
Cyril Bur46233672017-01-16 13:33:26 +1100120 }
121
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100122 while (1) {
123 rc = poll(context->fds, POLL_FDS, -1);
124
125 if (rc < 0) { /* Error */
126 MSG_ERR("Error from poll(): %s\n", strerror(errno));
127 break; /* This should mean we clean up nicely */
128 }
129
130 /* Event on Polled File Descriptor - Handle It */
131 if (context->fds[SIG_FD].revents & POLLIN) { /* Signal */
132 struct signalfd_siginfo info = { 0 };
133
134 rc = read(context->fds[SIG_FD].fd, (void *) &info,
135 sizeof(info));
136 if (rc != sizeof(info)) {
137 MSG_ERR("Error reading signal event: %s\n",
138 strerror(errno));
139 }
140
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000141 MSG_DBG("Received signal: %d\n", info.ssi_signo);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100142 switch (info.ssi_signo) {
143 case SIGINT:
144 case SIGTERM:
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000145 MSG_INFO("Caught Signal - Exiting...\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100146 context->terminate = true;
147 break;
148 case SIGHUP:
149 /* Host didn't request reset -> Notify it */
Andrew Jeffery5335f092018-08-09 14:56:08 +0930150 windows_reset_all(context, EVENT_TRIGGER);
Andrew Jeffery17971e42018-08-08 16:36:10 +0930151 rc = lpc_reset(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100152 if (rc < 0) {
153 MSG_ERR("WARNING: Failed to point the "
154 "LPC bus back to flash on "
155 "SIGHUP\nIf the host requires "
156 "this expect problems...\n");
157 }
158 break;
159 default:
160 MSG_ERR("Unhandled Signal: %d\n",
161 info.ssi_signo);
162 break;
163 }
164 }
165 if (context->fds[DBUS_FD].revents & POLLIN) { /* DBUS */
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000166 while ((rc = sd_bus_process(context->bus, NULL)) > 0) {
167 MSG_DBG("DBUS Event\n");
168 }
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100169 if (rc < 0) {
170 MSG_ERR("Error handling DBUS event: %s\n",
171 strerror(-rc));
172 }
173 }
174 if (context->terminate) {
175 break; /* This should mean we clean up nicely */
176 }
177 if (context->fds[MBOX_FD].revents & POLLIN) { /* MBOX */
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000178 MSG_DBG("MBOX Event\n");
Andrew Jefferyd86141b2018-08-09 14:58:53 +0930179 rc = transport_mbox_dispatch(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100180 if (rc < 0) {
181 MSG_ERR("Error handling MBOX event\n");
182 }
183 }
184 }
185
Deepak Kodihalli017e45c2017-07-12 01:06:30 -0500186 /* Best to reset windows and the lpc mapping for safety */
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100187 /* Host didn't request reset -> Notify it */
Andrew Jeffery5335f092018-08-09 14:56:08 +0930188 windows_reset_all(context, EVENT_TRIGGER);
Andrew Jeffery17971e42018-08-08 16:36:10 +0930189 rc = lpc_reset(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100190 /* Not much we can do if this fails */
191 if (rc < 0) {
192 MSG_ERR("WARNING: Failed to point the LPC bus back to flash\n"
193 "If the host requires this expect problems...\n");
194 }
195
196 return rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100197}
198
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100199static int init_signals(struct mbox_context *context, sigset_t *set)
Cyril Burc85e34d2016-11-15 11:50:41 +1100200{
201 int rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100202
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100203 /* Block SIGHUPs, SIGTERMs and SIGINTs */
204 sigemptyset(set);
205 sigaddset(set, SIGHUP);
206 sigaddset(set, SIGINT);
207 sigaddset(set, SIGTERM);
208 rc = sigprocmask(SIG_BLOCK, set, NULL);
209 if (rc < 0) {
210 MSG_ERR("Failed to set SIG_BLOCK mask %s\n", strerror(errno));
211 return rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100212 }
213
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100214 /* Get Signal File Descriptor */
215 rc = signalfd(-1, set, SFD_NONBLOCK);
216 if (rc < 0) {
217 MSG_ERR("Failed to get signalfd %s\n", strerror(errno));
218 return rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100219 }
220
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100221 context->fds[SIG_FD].fd = rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100222 return 0;
223}
224
Cyril Burc85e34d2016-11-15 11:50:41 +1100225static void usage(const char *name)
226{
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100227 printf(USAGE, name);
Cyril Burc85e34d2016-11-15 11:50:41 +1100228}
229
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100230static bool parse_cmdline(int argc, char **argv,
231 struct mbox_context *context)
Cyril Burc85e34d2016-11-15 11:50:41 +1100232{
Cyril Bure8f2de12017-01-17 16:56:02 +1100233 char *endptr;
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +1000234 int opt;
Cyril Burc85e34d2016-11-15 11:50:41 +1100235
236 static const struct option long_options[] = {
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100237 { "flash", required_argument, 0, 'f' },
238 { "window-size", optional_argument, 0, 'w' },
239 { "window-num", optional_argument, 0, 'n' },
240 { "verbose", no_argument, 0, 'v' },
241 { "syslog", no_argument, 0, 's' },
242 { "version", no_argument, 0, 'V' },
243 { "help", no_argument, 0, 'h' },
244 { 0, 0, 0, 0 }
Cyril Burc85e34d2016-11-15 11:50:41 +1100245 };
246
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100247 verbosity = MBOX_LOG_NONE;
Cyril Burc85e34d2016-11-15 11:50:41 +1100248 mbox_vlog = &mbox_log_console;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100249
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100250 context->current = NULL; /* No current window */
251
252 while ((opt = getopt_long(argc, argv, "f:w::n::vsVh", long_options, NULL))
253 != -1) {
Cyril Burc85e34d2016-11-15 11:50:41 +1100254 switch (opt) {
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100255 case 0:
256 break;
257 case 'f':
258 context->flash_size = strtol(optarg, &endptr, 10);
259 if (optarg == endptr) {
260 fprintf(stderr, "Unparseable flash size\n");
261 return false;
262 }
263 switch (*endptr) {
264 case '\0':
Cyril Burc85e34d2016-11-15 11:50:41 +1100265 break;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100266 case 'M':
267 context->flash_size <<= 10;
268 case 'K':
269 context->flash_size <<= 10;
Cyril Burc85e34d2016-11-15 11:50:41 +1100270 break;
271 default:
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100272 fprintf(stderr, "Unknown units '%c'\n",
273 *endptr);
274 return false;
275 }
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 }
Suraj Jitindar Singh0aff80c2017-04-12 14:37:24 +1000294 if (!is_power_of_2(context->windows.default_size)) {
295 fprintf(stderr, "Window size not power of 2\n");
296 return false;
297 }
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100298 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':
Suraj Jitindar Singh8d65bb42017-05-01 16:05:17 +1000310 printf("%s V%s\n", THIS_NAME, PACKAGE_VERSION);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100311 exit(0);
312 case 'h':
313 return false; /* This will print the usage message */
314 default:
315 return false;
Cyril Burc85e34d2016-11-15 11:50:41 +1100316 }
317 }
318
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100319 if (!context->flash_size) {
Cyril Bure8f2de12017-01-17 16:56:02 +1100320 fprintf(stderr, "Must specify a non-zero flash size\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100321 return false;
Cyril Bure8f2de12017-01-17 16:56:02 +1100322 }
323
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000324 MSG_INFO("Flash size: 0x%.8x\n", context->flash_size);
Cyril Burc85e34d2016-11-15 11:50:41 +1100325
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100326 if (verbosity) {
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000327 MSG_INFO("%s logging\n", verbosity == MBOX_LOG_DEBUG ? "Debug" :
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100328 "Verbose");
Cyril Burc85e34d2016-11-15 11:50:41 +1100329 }
330
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100331 return true;
Cyril Burc85e34d2016-11-15 11:50:41 +1100332}
333
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100334int main(int argc, char **argv)
335{
336 struct mbox_context *context;
337 char *name = argv[0];
338 sigset_t set;
339 int rc, i;
340
341 context = calloc(1, sizeof(*context));
342 if (!context) {
343 fprintf(stderr, "Memory allocation failed\n");
344 exit(1);
345 }
346
347 if (!parse_cmdline(argc, argv, context)) {
348 usage(name);
349 free(context);
350 exit(0);
351 }
352
353 for (i = 0; i < TOTAL_FDS; i++) {
354 context->fds[i].fd = -1;
355 }
356
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000357 MSG_INFO("Starting Daemon\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100358
359 rc = init_signals(context, &set);
360 if (rc) {
361 goto finish;
362 }
363
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930364 rc = protocol_init(context);
365 if (rc) {
366 goto finish;
367 }
368
Andrew Jefferyb2466ee2018-08-09 15:03:27 +0930369 rc = transport_mbox_init(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100370 if (rc) {
371 goto finish;
372 }
373
Andrew Jefferycb9b2102018-08-08 16:31:07 +0930374 rc = lpc_dev_init(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100375 if (rc) {
376 goto finish;
377 }
378
379 /* We've found the reserved memory region -> we can assign to windows */
Andrew Jefferyc1a67fa2018-08-08 17:07:38 +0930380 rc = windows_init(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100381 if (rc) {
382 goto finish;
383 }
384
Andrew Jefferyd6b09bc2018-08-08 16:47:54 +0930385 rc = flash_dev_init(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100386 if (rc) {
387 goto finish;
388 }
389
Andrew Jefferyef9e62d2018-08-08 15:48:27 +0930390 rc = dbus_init(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100391 if (rc) {
392 goto finish;
393 }
394
Deepak Kodihalli017e45c2017-07-12 01:06:30 -0500395#ifdef VIRTUAL_PNOR_ENABLED
Deepak Kodihalli64ec3e42017-07-17 06:15:16 -0500396 init_vpnor(context);
Deepak Kodihalli017e45c2017-07-12 01:06:30 -0500397#endif
398
399 /* Set the LPC bus mapping */
Andrew Jeffery17971e42018-08-08 16:36:10 +0930400 rc = lpc_reset(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100401 if (rc) {
Andrew Jeffery8fe809e2018-05-17 09:54:32 +0930402 MSG_ERR("LPC configuration failed, RESET required: %d\n", rc);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100403 }
404
Andrew Jeffery5335f092018-08-09 14:56:08 +0930405 rc = protocol_events_set(context, BMC_EVENT_DAEMON_READY, EVENT_TRIGGER);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100406 if (rc) {
407 goto finish;
408 }
409
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000410 MSG_INFO("Entering Polling Loop\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100411 rc = poll_loop(context);
412
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000413 MSG_INFO("Exiting Poll Loop: %d\n", rc);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100414
415finish:
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000416 MSG_INFO("Daemon Exiting...\n");
Andrew Jeffery5335f092018-08-09 14:56:08 +0930417 protocol_events_clear(context, BMC_EVENT_DAEMON_READY, EVENT_TRIGGER);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100418
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930419#ifdef VIRTUAL_PNOR_ENABLED
420 destroy_vpnor(context);
421#endif
Andrew Jefferyef9e62d2018-08-08 15:48:27 +0930422 dbus_free(context);
Andrew Jefferydec6ca62018-08-08 16:49:43 +0930423 flash_dev_free(context);
Andrew Jeffery2e2df282018-08-08 16:32:22 +0930424 lpc_dev_free(context);
Andrew Jeffery55260ce2018-08-09 15:05:59 +0930425 transport_mbox_free(context);
Andrew Jefferyf5f51422018-08-08 17:08:33 +0930426 windows_free(context);
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930427 protocol_free(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100428 free(context);
429
430 return rc;
431}