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 "flash.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->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->flash_size <<= 10; 259 case 'K': 260 context->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->flash_size) { 311 fprintf(stderr, "Must specify a non-zero flash size\n"); 312 return false; 313 } 314 315 MSG_INFO("Flash size: 0x%.8x\n", context->flash_size); 316 317 if (verbosity) { 318 MSG_INFO("%s logging\n", verbosity == MBOX_LOG_DEBUG ? "Debug" : 319 "Verbose"); 320 } 321 322 return true; 323 } 324 325 int main(int argc, char **argv) 326 { 327 const struct transport_ops *mbox_ops, *dbus_ops; 328 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 349 MSG_INFO("Starting Daemon\n"); 350 351 rc = init_signals(context, &set); 352 if (rc) { 353 goto finish; 354 } 355 356 rc = protocol_init(context); 357 if (rc) { 358 goto finish; 359 } 360 361 rc = transport_mbox_init(context, &mbox_ops); 362 if (rc) { 363 goto finish; 364 } 365 366 rc = lpc_dev_init(context); 367 if (rc) { 368 goto finish; 369 } 370 371 /* We've found the reserved memory region -> we can assign to windows */ 372 rc = windows_init(context); 373 if (rc) { 374 goto finish; 375 } 376 377 rc = flash_dev_init(context); 378 if (rc) { 379 goto finish; 380 } 381 382 rc = dbus_init(context, &dbus_ops); 383 if (rc) { 384 goto finish; 385 } 386 387 #ifdef VIRTUAL_PNOR_ENABLED 388 init_vpnor(context); 389 #endif 390 391 /* Set the LPC bus mapping */ 392 __protocol_reset(context); 393 394 /* We're ready to go, alert the host */ 395 context->bmc_events |= BMC_EVENT_DAEMON_READY; 396 context->bmc_events |= BMC_EVENT_PROTOCOL_RESET; 397 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); 405 if (rc) { 406 goto finish; 407 } 408 409 MSG_INFO("Entering Polling Loop\n"); 410 rc = poll_loop(context); 411 412 MSG_INFO("Exiting Poll Loop: %d\n", rc); 413 414 finish: 415 MSG_INFO("Daemon Exiting...\n"); 416 context->bmc_events &= ~BMC_EVENT_DAEMON_READY; 417 context->bmc_events |= BMC_EVENT_PROTOCOL_RESET; 418 419 /* Alert on all supported transports */ 420 protocol_events_put(context, mbox_ops); 421 protocol_events_put(context, dbus_ops); 422 423 #ifdef VIRTUAL_PNOR_ENABLED 424 destroy_vpnor(context); 425 #endif 426 dbus_free(context); 427 flash_dev_free(context); 428 lpc_dev_free(context); 429 transport_mbox_free(context); 430 windows_free(context); 431 protocol_free(context); 432 free(context); 433 434 return rc; 435 } 436