blob: e5b6fe8e1c3a0b83592be09a89212a91828517e5 [file] [log] [blame]
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11001/*
2 * Mailbox Daemon Implementation
3 *
4 * Copyright 2016 IBM
Cyril Burc85e34d2016-11-15 11:50:41 +11005 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110010 * http://www.apache.org/licenses/LICENSE-2.0
Cyril Burc85e34d2016-11-15 11:50:41 +110011 *
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110012 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
Cyril Burc85e34d2016-11-15 11:50:41 +110017 *
18 */
19
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110020#define _GNU_SOURCE
Cyril Burc85e34d2016-11-15 11:50:41 +110021#include <assert.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <getopt.h>
25#include <limits.h>
26#include <poll.h>
27#include <stdbool.h>
28#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <syslog.h>
Michael Neuling899ebac2017-01-14 11:20:26 -060033#include <signal.h>
Cyril Burc85e34d2016-11-15 11:50:41 +110034#include <sys/ioctl.h>
35#include <sys/mman.h>
36#include <sys/stat.h>
37#include <sys/timerfd.h>
38#include <sys/types.h>
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110039#include <sys/signalfd.h>
Cyril Burc85e34d2016-11-15 11:50:41 +110040#include <time.h>
41#include <unistd.h>
Andrew Jeffery78210b92017-01-13 13:06:09 +103042#include <inttypes.h>
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110043#include <systemd/sd-bus.h>
Cyril Burc85e34d2016-11-15 11:50:41 +110044
45#include "mbox.h"
46#include "common.h"
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110047#include "dbus.h"
48#include "mboxd_dbus.h"
49#include "mboxd_flash.h"
50#include "mboxd_lpc.h"
51#include "mboxd_msg.h"
52#include "mboxd_windows.h"
Cyril Burc85e34d2016-11-15 11:50:41 +110053
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110054#define USAGE \
55"\nUsage: %s [-V | --version] [-h | --help] [-v[v] | --verbose] [-s | --syslog]\n" \
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +100056"\t\t[-n | --window-num <num>]\n" \
57"\t\t[-w | --window-size <size>M]\n" \
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110058"\t\t-f | --flash <size>[K|M]\n\n" \
59"\t-v | --verbose\t\tBe [more] verbose\n" \
60"\t-s | --syslog\t\tLog output to syslog (pointless without -v)\n" \
61"\t-n | --window-num\tThe number of windows\n" \
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +100062"\t\t\t\t(default: fill the reserved memory region)\n" \
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110063"\t-w | --window-size\tThe window size (power of 2) in MB\n" \
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +100064"\t\t\t\t(default: 1MB)\n" \
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110065"\t-f | --flash\t\tSize of flash in [K|M] bytes\n\n"
Cyril Burc85e34d2016-11-15 11:50:41 +110066
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110067static int poll_loop(struct mbox_context *context)
Cyril Burc85e34d2016-11-15 11:50:41 +110068{
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110069 int rc = 0, i;
Cyril Bur46233672017-01-16 13:33:26 +110070
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110071 /* Set POLLIN on polling file descriptors */
72 for (i = 0; i < POLL_FDS; i++) {
73 context->fds[i].events = POLLIN;
Cyril Bur46233672017-01-16 13:33:26 +110074 }
75
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110076 while (1) {
77 rc = poll(context->fds, POLL_FDS, -1);
78
79 if (rc < 0) { /* Error */
80 MSG_ERR("Error from poll(): %s\n", strerror(errno));
81 break; /* This should mean we clean up nicely */
82 }
83
84 /* Event on Polled File Descriptor - Handle It */
85 if (context->fds[SIG_FD].revents & POLLIN) { /* Signal */
86 struct signalfd_siginfo info = { 0 };
87
88 rc = read(context->fds[SIG_FD].fd, (void *) &info,
89 sizeof(info));
90 if (rc != sizeof(info)) {
91 MSG_ERR("Error reading signal event: %s\n",
92 strerror(errno));
93 }
94
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100095 MSG_DBG("Received signal: %d\n", info.ssi_signo);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110096 switch (info.ssi_signo) {
97 case SIGINT:
98 case SIGTERM:
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100099 MSG_INFO("Caught Signal - Exiting...\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100100 context->terminate = true;
101 break;
102 case SIGHUP:
103 /* Host didn't request reset -> Notify it */
104 reset_all_windows(context, SET_BMC_EVENT);
105 rc = point_to_flash(context);
106 if (rc < 0) {
107 MSG_ERR("WARNING: Failed to point the "
108 "LPC bus back to flash on "
109 "SIGHUP\nIf the host requires "
110 "this expect problems...\n");
111 }
112 break;
113 default:
114 MSG_ERR("Unhandled Signal: %d\n",
115 info.ssi_signo);
116 break;
117 }
118 }
119 if (context->fds[DBUS_FD].revents & POLLIN) { /* DBUS */
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000120 while ((rc = sd_bus_process(context->bus, NULL)) > 0) {
121 MSG_DBG("DBUS Event\n");
122 }
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100123 if (rc < 0) {
124 MSG_ERR("Error handling DBUS event: %s\n",
125 strerror(-rc));
126 }
127 }
128 if (context->terminate) {
129 break; /* This should mean we clean up nicely */
130 }
131 if (context->fds[MBOX_FD].revents & POLLIN) { /* MBOX */
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000132 MSG_DBG("MBOX Event\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100133 rc = dispatch_mbox(context);
134 if (rc < 0) {
135 MSG_ERR("Error handling MBOX event\n");
136 }
137 }
138 }
139
140 /* Best to reset windows and point back to flash for safety */
141 /* Host didn't request reset -> Notify it */
142 reset_all_windows(context, SET_BMC_EVENT);
143 rc = point_to_flash(context);
144 /* Not much we can do if this fails */
145 if (rc < 0) {
146 MSG_ERR("WARNING: Failed to point the LPC bus back to flash\n"
147 "If the host requires this expect problems...\n");
148 }
149
150 return rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100151}
152
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100153static int init_signals(struct mbox_context *context, sigset_t *set)
Cyril Burc85e34d2016-11-15 11:50:41 +1100154{
155 int rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100156
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100157 /* Block SIGHUPs, SIGTERMs and SIGINTs */
158 sigemptyset(set);
159 sigaddset(set, SIGHUP);
160 sigaddset(set, SIGINT);
161 sigaddset(set, SIGTERM);
162 rc = sigprocmask(SIG_BLOCK, set, NULL);
163 if (rc < 0) {
164 MSG_ERR("Failed to set SIG_BLOCK mask %s\n", strerror(errno));
165 return rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100166 }
167
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100168 /* Get Signal File Descriptor */
169 rc = signalfd(-1, set, SFD_NONBLOCK);
170 if (rc < 0) {
171 MSG_ERR("Failed to get signalfd %s\n", strerror(errno));
172 return rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100173 }
174
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100175 context->fds[SIG_FD].fd = rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100176 return 0;
177}
178
Cyril Burc85e34d2016-11-15 11:50:41 +1100179static void usage(const char *name)
180{
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100181 printf(USAGE, name);
Cyril Burc85e34d2016-11-15 11:50:41 +1100182}
183
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100184static bool parse_cmdline(int argc, char **argv,
185 struct mbox_context *context)
Cyril Burc85e34d2016-11-15 11:50:41 +1100186{
Cyril Bure8f2de12017-01-17 16:56:02 +1100187 char *endptr;
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +1000188 int opt;
Cyril Burc85e34d2016-11-15 11:50:41 +1100189
190 static const struct option long_options[] = {
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100191 { "flash", required_argument, 0, 'f' },
192 { "window-size", optional_argument, 0, 'w' },
193 { "window-num", optional_argument, 0, 'n' },
194 { "verbose", no_argument, 0, 'v' },
195 { "syslog", no_argument, 0, 's' },
196 { "version", no_argument, 0, 'V' },
197 { "help", no_argument, 0, 'h' },
198 { 0, 0, 0, 0 }
Cyril Burc85e34d2016-11-15 11:50:41 +1100199 };
200
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100201 verbosity = MBOX_LOG_NONE;
Cyril Burc85e34d2016-11-15 11:50:41 +1100202 mbox_vlog = &mbox_log_console;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100203
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100204 context->current = NULL; /* No current window */
205
206 while ((opt = getopt_long(argc, argv, "f:w::n::vsVh", long_options, NULL))
207 != -1) {
Cyril Burc85e34d2016-11-15 11:50:41 +1100208 switch (opt) {
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100209 case 0:
210 break;
211 case 'f':
212 context->flash_size = strtol(optarg, &endptr, 10);
213 if (optarg == endptr) {
214 fprintf(stderr, "Unparseable flash size\n");
215 return false;
216 }
217 switch (*endptr) {
218 case '\0':
Cyril Burc85e34d2016-11-15 11:50:41 +1100219 break;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100220 case 'M':
221 context->flash_size <<= 10;
222 case 'K':
223 context->flash_size <<= 10;
Cyril Burc85e34d2016-11-15 11:50:41 +1100224 break;
225 default:
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100226 fprintf(stderr, "Unknown units '%c'\n",
227 *endptr);
228 return false;
229 }
230 break;
231 case 'n':
232 context->windows.num = strtol(argv[optind], &endptr,
233 10);
234 if (optarg == endptr || *endptr != '\0') {
235 fprintf(stderr, "Unparseable window num\n");
236 return false;
237 }
238 break;
239 case 'w':
240 context->windows.default_size = strtol(argv[optind],
241 &endptr, 10);
242 context->windows.default_size <<= 20; /* Given in MB */
243 if (optarg == endptr || (*endptr != '\0' &&
244 *endptr != 'M')) {
245 fprintf(stderr, "Unparseable window size\n");
246 return false;
247 }
Suraj Jitindar Singh0aff80c2017-04-12 14:37:24 +1000248 if (!is_power_of_2(context->windows.default_size)) {
249 fprintf(stderr, "Window size not power of 2\n");
250 return false;
251 }
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100252 break;
253 case 'v':
254 verbosity++;
255 break;
256 case 's':
257 /* Avoid a double openlog() */
258 if (mbox_vlog != &vsyslog) {
259 openlog(PREFIX, LOG_ODELAY, LOG_DAEMON);
260 mbox_vlog = &vsyslog;
261 }
262 break;
263 case 'V':
264 printf("%s v%d.%.2d\n", THIS_NAME, API_MAX_VERSION,
265 SUB_VERSION);
266 exit(0);
267 case 'h':
268 return false; /* This will print the usage message */
269 default:
270 return false;
Cyril Burc85e34d2016-11-15 11:50:41 +1100271 }
272 }
273
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100274 if (!context->flash_size) {
Cyril Bure8f2de12017-01-17 16:56:02 +1100275 fprintf(stderr, "Must specify a non-zero flash size\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100276 return false;
Cyril Bure8f2de12017-01-17 16:56:02 +1100277 }
278
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000279 MSG_INFO("Flash size: 0x%.8x\n", context->flash_size);
Cyril Burc85e34d2016-11-15 11:50:41 +1100280
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100281 if (verbosity) {
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000282 MSG_INFO("%s logging\n", verbosity == MBOX_LOG_DEBUG ? "Debug" :
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100283 "Verbose");
Cyril Burc85e34d2016-11-15 11:50:41 +1100284 }
285
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100286 return true;
Cyril Burc85e34d2016-11-15 11:50:41 +1100287}
288
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100289int main(int argc, char **argv)
290{
291 struct mbox_context *context;
292 char *name = argv[0];
293 sigset_t set;
294 int rc, i;
295
296 context = calloc(1, sizeof(*context));
297 if (!context) {
298 fprintf(stderr, "Memory allocation failed\n");
299 exit(1);
300 }
301
302 if (!parse_cmdline(argc, argv, context)) {
303 usage(name);
304 free(context);
305 exit(0);
306 }
307
308 for (i = 0; i < TOTAL_FDS; i++) {
309 context->fds[i].fd = -1;
310 }
311
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000312 MSG_INFO("Starting Daemon\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100313
314 rc = init_signals(context, &set);
315 if (rc) {
316 goto finish;
317 }
318
319 rc = init_mbox_dev(context);
320 if (rc) {
321 goto finish;
322 }
323
324 rc = init_lpc_dev(context);
325 if (rc) {
326 goto finish;
327 }
328
329 /* We've found the reserved memory region -> we can assign to windows */
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +1000330 rc = init_windows(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100331 if (rc) {
332 goto finish;
333 }
334
335 rc = init_flash_dev(context);
336 if (rc) {
337 goto finish;
338 }
339
340 rc = init_mboxd_dbus(context);
341 if (rc) {
342 goto finish;
343 }
344
345 /* Set the LPC bus mapping to point to the physical flash device */
346 rc = point_to_flash(context);
347 if (rc) {
348 goto finish;
349 }
350
351 rc = set_bmc_events(context, BMC_EVENT_DAEMON_READY, SET_BMC_EVENT);
352 if (rc) {
353 goto finish;
354 }
355
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000356 MSG_INFO("Entering Polling Loop\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100357 rc = poll_loop(context);
358
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000359 MSG_INFO("Exiting Poll Loop: %d\n", rc);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100360
361finish:
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000362 MSG_INFO("Daemon Exiting...\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100363 clr_bmc_events(context, BMC_EVENT_DAEMON_READY, SET_BMC_EVENT);
364
365 free_mboxd_dbus(context);
366 free_flash_dev(context);
367 free_lpc_dev(context);
368 free_mbox_dev(context);
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +1000369 free_windows(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100370 free(context);
371
372 return rc;
373}