blob: b944998d49dfd0f7aa95ee7c1bf1e27dfe711754 [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"
Cyril Burc85e34d2016-11-15 11:50:41 +110030#include "mbox.h"
31#include "common.h"
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110032#include "dbus.h"
33#include "mboxd_dbus.h"
34#include "mboxd_flash.h"
35#include "mboxd_lpc.h"
36#include "mboxd_msg.h"
37#include "mboxd_windows.h"
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -050038#include "mboxd_pnor_partition_table.h"
Cyril Burc85e34d2016-11-15 11:50:41 +110039
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110040#define USAGE \
41"\nUsage: %s [-V | --version] [-h | --help] [-v[v] | --verbose] [-s | --syslog]\n" \
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +100042"\t\t[-n | --window-num <num>]\n" \
43"\t\t[-w | --window-size <size>M]\n" \
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110044"\t\t-f | --flash <size>[K|M]\n\n" \
45"\t-v | --verbose\t\tBe [more] verbose\n" \
46"\t-s | --syslog\t\tLog output to syslog (pointless without -v)\n" \
47"\t-n | --window-num\tThe number of windows\n" \
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +100048"\t\t\t\t(default: fill the reserved memory region)\n" \
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110049"\t-w | --window-size\tThe window size (power of 2) in MB\n" \
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +100050"\t\t\t\t(default: 1MB)\n" \
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110051"\t-f | --flash\t\tSize of flash in [K|M] bytes\n\n"
Cyril Burc85e34d2016-11-15 11:50:41 +110052
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110053static int poll_loop(struct mbox_context *context)
Cyril Burc85e34d2016-11-15 11:50:41 +110054{
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110055 int rc = 0, i;
Cyril Bur46233672017-01-16 13:33:26 +110056
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110057 /* Set POLLIN on polling file descriptors */
58 for (i = 0; i < POLL_FDS; i++) {
59 context->fds[i].events = POLLIN;
Cyril Bur46233672017-01-16 13:33:26 +110060 }
61
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110062 while (1) {
63 rc = poll(context->fds, POLL_FDS, -1);
64
65 if (rc < 0) { /* Error */
66 MSG_ERR("Error from poll(): %s\n", strerror(errno));
67 break; /* This should mean we clean up nicely */
68 }
69
70 /* Event on Polled File Descriptor - Handle It */
71 if (context->fds[SIG_FD].revents & POLLIN) { /* Signal */
72 struct signalfd_siginfo info = { 0 };
73
74 rc = read(context->fds[SIG_FD].fd, (void *) &info,
75 sizeof(info));
76 if (rc != sizeof(info)) {
77 MSG_ERR("Error reading signal event: %s\n",
78 strerror(errno));
79 }
80
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100081 MSG_DBG("Received signal: %d\n", info.ssi_signo);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110082 switch (info.ssi_signo) {
83 case SIGINT:
84 case SIGTERM:
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100085 MSG_INFO("Caught Signal - Exiting...\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110086 context->terminate = true;
87 break;
88 case SIGHUP:
89 /* Host didn't request reset -> Notify it */
90 reset_all_windows(context, SET_BMC_EVENT);
Deepak Kodihalli017e45c2017-07-12 01:06:30 -050091 rc = reset_lpc(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110092 if (rc < 0) {
93 MSG_ERR("WARNING: Failed to point the "
94 "LPC bus back to flash on "
95 "SIGHUP\nIf the host requires "
96 "this expect problems...\n");
97 }
98 break;
99 default:
100 MSG_ERR("Unhandled Signal: %d\n",
101 info.ssi_signo);
102 break;
103 }
104 }
105 if (context->fds[DBUS_FD].revents & POLLIN) { /* DBUS */
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000106 while ((rc = sd_bus_process(context->bus, NULL)) > 0) {
107 MSG_DBG("DBUS Event\n");
108 }
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100109 if (rc < 0) {
110 MSG_ERR("Error handling DBUS event: %s\n",
111 strerror(-rc));
112 }
113 }
114 if (context->terminate) {
115 break; /* This should mean we clean up nicely */
116 }
117 if (context->fds[MBOX_FD].revents & POLLIN) { /* MBOX */
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000118 MSG_DBG("MBOX Event\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100119 rc = dispatch_mbox(context);
120 if (rc < 0) {
121 MSG_ERR("Error handling MBOX event\n");
122 }
123 }
124 }
125
Deepak Kodihalli017e45c2017-07-12 01:06:30 -0500126 /* Best to reset windows and the lpc mapping for safety */
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100127 /* Host didn't request reset -> Notify it */
128 reset_all_windows(context, SET_BMC_EVENT);
Deepak Kodihalli017e45c2017-07-12 01:06:30 -0500129 rc = reset_lpc(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100130 /* Not much we can do if this fails */
131 if (rc < 0) {
132 MSG_ERR("WARNING: Failed to point the LPC bus back to flash\n"
133 "If the host requires this expect problems...\n");
134 }
135
136 return rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100137}
138
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100139static int init_signals(struct mbox_context *context, sigset_t *set)
Cyril Burc85e34d2016-11-15 11:50:41 +1100140{
141 int rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100142
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100143 /* Block SIGHUPs, SIGTERMs and SIGINTs */
144 sigemptyset(set);
145 sigaddset(set, SIGHUP);
146 sigaddset(set, SIGINT);
147 sigaddset(set, SIGTERM);
148 rc = sigprocmask(SIG_BLOCK, set, NULL);
149 if (rc < 0) {
150 MSG_ERR("Failed to set SIG_BLOCK mask %s\n", strerror(errno));
151 return rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100152 }
153
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100154 /* Get Signal File Descriptor */
155 rc = signalfd(-1, set, SFD_NONBLOCK);
156 if (rc < 0) {
157 MSG_ERR("Failed to get signalfd %s\n", strerror(errno));
158 return rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100159 }
160
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100161 context->fds[SIG_FD].fd = rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100162 return 0;
163}
164
Cyril Burc85e34d2016-11-15 11:50:41 +1100165static void usage(const char *name)
166{
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100167 printf(USAGE, name);
Cyril Burc85e34d2016-11-15 11:50:41 +1100168}
169
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100170static bool parse_cmdline(int argc, char **argv,
171 struct mbox_context *context)
Cyril Burc85e34d2016-11-15 11:50:41 +1100172{
Cyril Bure8f2de12017-01-17 16:56:02 +1100173 char *endptr;
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +1000174 int opt;
Cyril Burc85e34d2016-11-15 11:50:41 +1100175
176 static const struct option long_options[] = {
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100177 { "flash", required_argument, 0, 'f' },
178 { "window-size", optional_argument, 0, 'w' },
179 { "window-num", optional_argument, 0, 'n' },
180 { "verbose", no_argument, 0, 'v' },
181 { "syslog", no_argument, 0, 's' },
182 { "version", no_argument, 0, 'V' },
183 { "help", no_argument, 0, 'h' },
184 { 0, 0, 0, 0 }
Cyril Burc85e34d2016-11-15 11:50:41 +1100185 };
186
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100187 verbosity = MBOX_LOG_NONE;
Cyril Burc85e34d2016-11-15 11:50:41 +1100188 mbox_vlog = &mbox_log_console;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100189
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100190 context->current = NULL; /* No current window */
191
192 while ((opt = getopt_long(argc, argv, "f:w::n::vsVh", long_options, NULL))
193 != -1) {
Cyril Burc85e34d2016-11-15 11:50:41 +1100194 switch (opt) {
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100195 case 0:
196 break;
197 case 'f':
198 context->flash_size = strtol(optarg, &endptr, 10);
199 if (optarg == endptr) {
200 fprintf(stderr, "Unparseable flash size\n");
201 return false;
202 }
203 switch (*endptr) {
204 case '\0':
Cyril Burc85e34d2016-11-15 11:50:41 +1100205 break;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100206 case 'M':
207 context->flash_size <<= 10;
208 case 'K':
209 context->flash_size <<= 10;
Cyril Burc85e34d2016-11-15 11:50:41 +1100210 break;
211 default:
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100212 fprintf(stderr, "Unknown units '%c'\n",
213 *endptr);
214 return false;
215 }
216 break;
217 case 'n':
218 context->windows.num = strtol(argv[optind], &endptr,
219 10);
220 if (optarg == endptr || *endptr != '\0') {
221 fprintf(stderr, "Unparseable window num\n");
222 return false;
223 }
224 break;
225 case 'w':
226 context->windows.default_size = strtol(argv[optind],
227 &endptr, 10);
228 context->windows.default_size <<= 20; /* Given in MB */
229 if (optarg == endptr || (*endptr != '\0' &&
230 *endptr != 'M')) {
231 fprintf(stderr, "Unparseable window size\n");
232 return false;
233 }
Suraj Jitindar Singh0aff80c2017-04-12 14:37:24 +1000234 if (!is_power_of_2(context->windows.default_size)) {
235 fprintf(stderr, "Window size not power of 2\n");
236 return false;
237 }
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100238 break;
239 case 'v':
240 verbosity++;
241 break;
242 case 's':
243 /* Avoid a double openlog() */
244 if (mbox_vlog != &vsyslog) {
245 openlog(PREFIX, LOG_ODELAY, LOG_DAEMON);
246 mbox_vlog = &vsyslog;
247 }
248 break;
249 case 'V':
Suraj Jitindar Singh8d65bb42017-05-01 16:05:17 +1000250 printf("%s V%s\n", THIS_NAME, PACKAGE_VERSION);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100251 exit(0);
252 case 'h':
253 return false; /* This will print the usage message */
254 default:
255 return false;
Cyril Burc85e34d2016-11-15 11:50:41 +1100256 }
257 }
258
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100259 if (!context->flash_size) {
Cyril Bure8f2de12017-01-17 16:56:02 +1100260 fprintf(stderr, "Must specify a non-zero flash size\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100261 return false;
Cyril Bure8f2de12017-01-17 16:56:02 +1100262 }
263
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000264 MSG_INFO("Flash size: 0x%.8x\n", context->flash_size);
Cyril Burc85e34d2016-11-15 11:50:41 +1100265
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100266 if (verbosity) {
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000267 MSG_INFO("%s logging\n", verbosity == MBOX_LOG_DEBUG ? "Debug" :
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100268 "Verbose");
Cyril Burc85e34d2016-11-15 11:50:41 +1100269 }
270
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100271 return true;
Cyril Burc85e34d2016-11-15 11:50:41 +1100272}
273
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100274int main(int argc, char **argv)
275{
276 struct mbox_context *context;
277 char *name = argv[0];
278 sigset_t set;
279 int rc, i;
280
281 context = calloc(1, sizeof(*context));
282 if (!context) {
283 fprintf(stderr, "Memory allocation failed\n");
284 exit(1);
285 }
286
287 if (!parse_cmdline(argc, argv, context)) {
288 usage(name);
289 free(context);
290 exit(0);
291 }
292
293 for (i = 0; i < TOTAL_FDS; i++) {
294 context->fds[i].fd = -1;
295 }
296
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000297 MSG_INFO("Starting Daemon\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100298
299 rc = init_signals(context, &set);
300 if (rc) {
301 goto finish;
302 }
303
304 rc = init_mbox_dev(context);
305 if (rc) {
306 goto finish;
307 }
308
309 rc = init_lpc_dev(context);
310 if (rc) {
311 goto finish;
312 }
313
314 /* We've found the reserved memory region -> we can assign to windows */
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +1000315 rc = init_windows(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100316 if (rc) {
317 goto finish;
318 }
319
320 rc = init_flash_dev(context);
321 if (rc) {
322 goto finish;
323 }
324
325 rc = init_mboxd_dbus(context);
326 if (rc) {
327 goto finish;
328 }
329
Deepak Kodihalli017e45c2017-07-12 01:06:30 -0500330#ifdef VIRTUAL_PNOR_ENABLED
Deepak Kodihalli64ec3e42017-07-17 06:15:16 -0500331 init_vpnor(context);
Deepak Kodihalli017e45c2017-07-12 01:06:30 -0500332#endif
333
334 /* Set the LPC bus mapping */
335 rc = reset_lpc(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100336 if (rc) {
337 goto finish;
338 }
339
340 rc = set_bmc_events(context, BMC_EVENT_DAEMON_READY, SET_BMC_EVENT);
341 if (rc) {
342 goto finish;
343 }
344
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000345 MSG_INFO("Entering Polling Loop\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100346 rc = poll_loop(context);
347
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000348 MSG_INFO("Exiting Poll Loop: %d\n", rc);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100349
350finish:
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000351 MSG_INFO("Daemon Exiting...\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100352 clr_bmc_events(context, BMC_EVENT_DAEMON_READY, SET_BMC_EVENT);
353
354 free_mboxd_dbus(context);
355 free_flash_dev(context);
356 free_lpc_dev(context);
357 free_mbox_dev(context);
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +1000358 free_windows(context);
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500359#ifdef VIRTUAL_PNOR_ENABLED
Deepak Kodihalli64ec3e42017-07-17 06:15:16 -0500360 destroy_vpnor(context);
Deepak Kodihalli6c2fa902017-05-01 06:36:02 -0500361#endif
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100362 free(context);
363
364 return rc;
365}