blob: c2f2623a8a99dde377df44306470f89e0f44ff4e [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 Jefferyfe0c9e82018-11-01 14:02:17 +103054static int dbus_init(struct mbox_context *context,
55 const struct transport_ops **ops)
Andrew Jefferyef9e62d2018-08-08 15:48:27 +093056{
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
Andrew Jefferyfe0c9e82018-11-01 14:02:17 +103080 rc = transport_dbus_init(context, ops);
Andrew Jeffery23140be2018-09-05 14:15:07 +093081 if (rc < 0) {
82 MSG_ERR("Failed to initialise DBus protocol interface: %s\n",
83 strerror(-rc));
84 return rc;
85 }
86
Andrew Jefferyef9e62d2018-08-08 15:48:27 +093087 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) {
Andrew Jeffery23140be2018-09-05 14:15:07 +093091 MSG_ERR("Failed to request DBus name: %s\n", strerror(-rc));
Andrew Jefferyef9e62d2018-08-08 15:48:27 +093092 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
106static void dbus_free(struct mbox_context *context)
107{
Andrew Jeffery23140be2018-09-05 14:15:07 +0930108 transport_dbus_free(context);
Andrew Jefferyef9e62d2018-08-08 15:48:27 +0930109 control_dbus_free(context);
110 control_legacy_free(context);
111 sd_bus_unref(context->bus);
112}
Andrew Jeffery55f4d6f2018-08-06 12:26:44 +0930113
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100114static int poll_loop(struct mbox_context *context)
Cyril Burc85e34d2016-11-15 11:50:41 +1100115{
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100116 int rc = 0, i;
Cyril Bur46233672017-01-16 13:33:26 +1100117
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100118 /* Set POLLIN on polling file descriptors */
119 for (i = 0; i < POLL_FDS; i++) {
120 context->fds[i].events = POLLIN;
Cyril Bur46233672017-01-16 13:33:26 +1100121 }
122
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100123 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
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000142 MSG_DBG("Received signal: %d\n", info.ssi_signo);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100143 switch (info.ssi_signo) {
144 case SIGINT:
145 case SIGTERM:
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000146 MSG_INFO("Caught Signal - Exiting...\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100147 context->terminate = true;
148 break;
149 case SIGHUP:
Andrew Jefferyf69760d2019-03-14 16:54:13 +1030150 rc = protocol_reset(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100151 if (rc < 0) {
Andrew Jefferyf69760d2019-03-14 16:54:13 +1030152 MSG_ERR("Failed to reset on SIGHUP\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100153 }
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 */
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000162 while ((rc = sd_bus_process(context->bus, NULL)) > 0) {
163 MSG_DBG("DBUS Event\n");
164 }
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100165 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 */
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000174 MSG_DBG("MBOX Event\n");
Andrew Jefferyd86141b2018-08-09 14:58:53 +0930175 rc = transport_mbox_dispatch(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100176 if (rc < 0) {
177 MSG_ERR("Error handling MBOX event\n");
178 }
179 }
180 }
181
Andrew Jefferyf69760d2019-03-14 16:54:13 +1030182 rc = protocol_reset(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100183 if (rc < 0) {
Andrew Jefferyf69760d2019-03-14 16:54:13 +1030184 MSG_ERR("Failed to reset during poll loop cleanup\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100185 }
186
187 return rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100188}
189
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100190static int init_signals(struct mbox_context *context, sigset_t *set)
Cyril Burc85e34d2016-11-15 11:50:41 +1100191{
192 int rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100193
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100194 /* 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;
Cyril Burc85e34d2016-11-15 11:50:41 +1100203 }
204
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100205 /* 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;
Cyril Burc85e34d2016-11-15 11:50:41 +1100210 }
211
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100212 context->fds[SIG_FD].fd = rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100213 return 0;
214}
215
Cyril Burc85e34d2016-11-15 11:50:41 +1100216static void usage(const char *name)
217{
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100218 printf(USAGE, name);
Cyril Burc85e34d2016-11-15 11:50:41 +1100219}
220
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100221static bool parse_cmdline(int argc, char **argv,
222 struct mbox_context *context)
Cyril Burc85e34d2016-11-15 11:50:41 +1100223{
Cyril Bure8f2de12017-01-17 16:56:02 +1100224 char *endptr;
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +1000225 int opt;
Cyril Burc85e34d2016-11-15 11:50:41 +1100226
227 static const struct option long_options[] = {
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100228 { "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 }
Cyril Burc85e34d2016-11-15 11:50:41 +1100236 };
237
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100238 verbosity = MBOX_LOG_NONE;
Cyril Burc85e34d2016-11-15 11:50:41 +1100239 mbox_vlog = &mbox_log_console;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100240
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100241 context->current = NULL; /* No current window */
242
243 while ((opt = getopt_long(argc, argv, "f:w::n::vsVh", long_options, NULL))
244 != -1) {
Cyril Burc85e34d2016-11-15 11:50:41 +1100245 switch (opt) {
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100246 case 0:
247 break;
248 case 'f':
249 context->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':
Cyril Burc85e34d2016-11-15 11:50:41 +1100256 break;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100257 case 'M':
258 context->flash_size <<= 10;
259 case 'K':
260 context->flash_size <<= 10;
Cyril Burc85e34d2016-11-15 11:50:41 +1100261 break;
262 default:
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100263 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 }
Suraj Jitindar Singh0aff80c2017-04-12 14:37:24 +1000285 if (!is_power_of_2(context->windows.default_size)) {
286 fprintf(stderr, "Window size not power of 2\n");
287 return false;
288 }
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100289 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':
Suraj Jitindar Singh8d65bb42017-05-01 16:05:17 +1000301 printf("%s V%s\n", THIS_NAME, PACKAGE_VERSION);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100302 exit(0);
303 case 'h':
304 return false; /* This will print the usage message */
305 default:
306 return false;
Cyril Burc85e34d2016-11-15 11:50:41 +1100307 }
308 }
309
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100310 if (!context->flash_size) {
Cyril Bure8f2de12017-01-17 16:56:02 +1100311 fprintf(stderr, "Must specify a non-zero flash size\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100312 return false;
Cyril Bure8f2de12017-01-17 16:56:02 +1100313 }
314
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000315 MSG_INFO("Flash size: 0x%.8x\n", context->flash_size);
Cyril Burc85e34d2016-11-15 11:50:41 +1100316
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100317 if (verbosity) {
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000318 MSG_INFO("%s logging\n", verbosity == MBOX_LOG_DEBUG ? "Debug" :
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100319 "Verbose");
Cyril Burc85e34d2016-11-15 11:50:41 +1100320 }
321
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100322 return true;
Cyril Burc85e34d2016-11-15 11:50:41 +1100323}
324
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100325int main(int argc, char **argv)
326{
Andrew Jefferyfe0c9e82018-11-01 14:02:17 +1030327 const struct transport_ops *mbox_ops, *dbus_ops;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100328 struct mbox_context *context;
329 char *name = argv[0];
330 sigset_t set;
331 int rc, i;
332
333 context = calloc(1, sizeof(*context));
334 if (!context) {
335 fprintf(stderr, "Memory allocation failed\n");
336 exit(1);
337 }
338
339 if (!parse_cmdline(argc, argv, context)) {
340 usage(name);
341 free(context);
342 exit(0);
343 }
344
345 for (i = 0; i < TOTAL_FDS; i++) {
346 context->fds[i].fd = -1;
347 }
348
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000349 MSG_INFO("Starting Daemon\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100350
351 rc = init_signals(context, &set);
352 if (rc) {
353 goto finish;
354 }
355
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930356 rc = protocol_init(context);
357 if (rc) {
358 goto finish;
359 }
360
Andrew Jefferyfe0c9e82018-11-01 14:02:17 +1030361 rc = transport_mbox_init(context, &mbox_ops);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100362 if (rc) {
363 goto finish;
364 }
365
Andrew Jefferycb9b2102018-08-08 16:31:07 +0930366 rc = lpc_dev_init(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100367 if (rc) {
368 goto finish;
369 }
370
371 /* We've found the reserved memory region -> we can assign to windows */
Andrew Jefferyc1a67fa2018-08-08 17:07:38 +0930372 rc = windows_init(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100373 if (rc) {
374 goto finish;
375 }
376
Andrew Jefferyd6b09bc2018-08-08 16:47:54 +0930377 rc = flash_dev_init(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100378 if (rc) {
379 goto finish;
380 }
381
Andrew Jefferyfe0c9e82018-11-01 14:02:17 +1030382 rc = dbus_init(context, &dbus_ops);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100383 if (rc) {
384 goto finish;
385 }
386
Deepak Kodihalli017e45c2017-07-12 01:06:30 -0500387#ifdef VIRTUAL_PNOR_ENABLED
Deepak Kodihalli64ec3e42017-07-17 06:15:16 -0500388 init_vpnor(context);
Deepak Kodihalli017e45c2017-07-12 01:06:30 -0500389#endif
390
391 /* Set the LPC bus mapping */
Andrew Jefferyf69760d2019-03-14 16:54:13 +1030392 __protocol_reset(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100393
Andrew Jefferyfe0c9e82018-11-01 14:02:17 +1030394 /* We're ready to go, alert the host */
395 context->bmc_events |= BMC_EVENT_DAEMON_READY;
Andrew Jeffery4c15bb12018-12-05 14:15:28 +1030396 context->bmc_events |= BMC_EVENT_PROTOCOL_RESET;
Andrew Jefferyfe0c9e82018-11-01 14:02:17 +1030397
398 /* Alert on all supported transports */
399 rc = protocol_events_put(context, mbox_ops);
400 if (rc) {
401 goto finish;
402 }
403
404 rc = protocol_events_put(context, dbus_ops);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100405 if (rc) {
406 goto finish;
407 }
408
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000409 MSG_INFO("Entering Polling Loop\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100410 rc = poll_loop(context);
411
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000412 MSG_INFO("Exiting Poll Loop: %d\n", rc);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100413
414finish:
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000415 MSG_INFO("Daemon Exiting...\n");
Andrew Jefferyfe0c9e82018-11-01 14:02:17 +1030416 context->bmc_events &= ~BMC_EVENT_DAEMON_READY;
Andrew Jefferyfab672b2018-11-01 17:12:09 +1030417 context->bmc_events |= BMC_EVENT_PROTOCOL_RESET;
Andrew Jefferyfe0c9e82018-11-01 14:02:17 +1030418
419 /* Alert on all supported transports */
420 protocol_events_put(context, mbox_ops);
421 protocol_events_put(context, dbus_ops);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100422
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930423#ifdef VIRTUAL_PNOR_ENABLED
424 destroy_vpnor(context);
425#endif
Andrew Jefferyef9e62d2018-08-08 15:48:27 +0930426 dbus_free(context);
Andrew Jefferydec6ca62018-08-08 16:49:43 +0930427 flash_dev_free(context);
Andrew Jeffery2e2df282018-08-08 16:32:22 +0930428 lpc_dev_free(context);
Andrew Jeffery55260ce2018-08-09 15:05:59 +0930429 transport_mbox_free(context);
Andrew Jefferyf5f51422018-08-08 17:08:33 +0930430 windows_free(context);
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930431 protocol_free(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100432 free(context);
433
434 return rc;
435}