| Andrew Jeffery | 4fe996c | 2018-02-27 12:16:48 +1030 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 | 
|  | 2 | // Copyright (C) 2018 IBM Corp. | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 3 |  | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 4 | #define _GNU_SOURCE | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 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> | 
| Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 17 | #include <signal.h> | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 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> | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 23 | #include <sys/signalfd.h> | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 24 | #include <time.h> | 
|  | 25 | #include <unistd.h> | 
| Andrew Jeffery | 78210b9 | 2017-01-13 13:06:09 +1030 | [diff] [blame] | 26 | #include <inttypes.h> | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 27 | #include <systemd/sd-bus.h> | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 28 |  | 
| Suraj Jitindar Singh | 8d65bb4 | 2017-05-01 16:05:17 +1000 | [diff] [blame] | 29 | #include "config.h" | 
| Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 30 | #include "mboxd.h" | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 31 | #include "common.h" | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 32 | #include "dbus.h" | 
| Andrew Jeffery | 55f4d6f | 2018-08-06 12:26:44 +0930 | [diff] [blame] | 33 | #include "control_dbus.h" | 
| Andrew Jeffery | eebc6bd | 2018-08-08 10:38:19 +0930 | [diff] [blame] | 34 | #include "flash.h" | 
| Andrew Jeffery | cd18611 | 2018-08-08 10:47:55 +0930 | [diff] [blame] | 35 | #include "lpc.h" | 
| Andrew Jeffery | 457a6e5 | 2018-08-08 11:21:08 +0930 | [diff] [blame] | 36 | #include "transport_mbox.h" | 
| Andrew Jeffery | 23140be | 2018-09-05 14:15:07 +0930 | [diff] [blame] | 37 | #include "transport_dbus.h" | 
| Andrew Jeffery | f593b1b | 2018-08-08 11:01:04 +0930 | [diff] [blame] | 38 | #include "windows.h" | 
| Andrew Jeffery | 53c21aa | 2018-03-26 11:56:16 +1030 | [diff] [blame] | 39 | #include "vpnor/mboxd_pnor_partition_table.h" | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 40 |  | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 41 | #define USAGE \ | 
|  | 42 | "\nUsage: %s [-V | --version] [-h | --help] [-v[v] | --verbose] [-s | --syslog]\n" \ | 
| Suraj Jitindar Singh | c29172e | 2017-04-12 14:26:47 +1000 | [diff] [blame] | 43 | "\t\t[-n | --window-num <num>]\n" \ | 
|  | 44 | "\t\t[-w | --window-size <size>M]\n" \ | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 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" \ | 
| Suraj Jitindar Singh | c29172e | 2017-04-12 14:26:47 +1000 | [diff] [blame] | 49 | "\t\t\t\t(default: fill the reserved memory region)\n" \ | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 50 | "\t-w | --window-size\tThe window size (power of 2) in MB\n" \ | 
| Suraj Jitindar Singh | c29172e | 2017-04-12 14:26:47 +1000 | [diff] [blame] | 51 | "\t\t\t\t(default: 1MB)\n" \ | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 52 | "\t-f | --flash\t\tSize of flash in [K|M] bytes\n\n" | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 53 |  | 
| Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 54 | static int dbus_init(struct mbox_context *context, | 
|  | 55 | const struct transport_ops **ops) | 
| Andrew Jeffery | ef9e62d | 2018-08-08 15:48:27 +0930 | [diff] [blame] | 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 |  | 
| Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 80 | rc = transport_dbus_init(context, ops); | 
| Andrew Jeffery | 23140be | 2018-09-05 14:15:07 +0930 | [diff] [blame] | 81 | if (rc < 0) { | 
|  | 82 | MSG_ERR("Failed to initialise DBus protocol interface: %s\n", | 
|  | 83 | strerror(-rc)); | 
|  | 84 | return rc; | 
|  | 85 | } | 
|  | 86 |  | 
| Andrew Jeffery | ef9e62d | 2018-08-08 15:48:27 +0930 | [diff] [blame] | 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) { | 
| Andrew Jeffery | 23140be | 2018-09-05 14:15:07 +0930 | [diff] [blame] | 91 | MSG_ERR("Failed to request DBus name: %s\n", strerror(-rc)); | 
| Andrew Jeffery | ef9e62d | 2018-08-08 15:48:27 +0930 | [diff] [blame] | 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 | { | 
| Andrew Jeffery | 23140be | 2018-09-05 14:15:07 +0930 | [diff] [blame] | 108 | transport_dbus_free(context); | 
| Andrew Jeffery | ef9e62d | 2018-08-08 15:48:27 +0930 | [diff] [blame] | 109 | control_dbus_free(context); | 
|  | 110 | control_legacy_free(context); | 
|  | 111 | sd_bus_unref(context->bus); | 
|  | 112 | } | 
| Andrew Jeffery | 55f4d6f | 2018-08-06 12:26:44 +0930 | [diff] [blame] | 113 |  | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 114 | static int poll_loop(struct mbox_context *context) | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 115 | { | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 116 | int rc = 0, i; | 
| Cyril Bur | 4623367 | 2017-01-16 13:33:26 +1100 | [diff] [blame] | 117 |  | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 118 | /* Set POLLIN on polling file descriptors */ | 
|  | 119 | for (i = 0; i < POLL_FDS; i++) { | 
|  | 120 | context->fds[i].events = POLLIN; | 
| Cyril Bur | 4623367 | 2017-01-16 13:33:26 +1100 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 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 |  | 
| Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 142 | MSG_DBG("Received signal: %d\n", info.ssi_signo); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 143 | switch (info.ssi_signo) { | 
|  | 144 | case SIGINT: | 
|  | 145 | case SIGTERM: | 
| Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 146 | MSG_INFO("Caught Signal - Exiting...\n"); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 147 | context->terminate = true; | 
|  | 148 | break; | 
|  | 149 | case SIGHUP: | 
|  | 150 | /* Host didn't request reset -> Notify it */ | 
| Andrew Jeffery | 2ebfd20 | 2018-08-20 11:46:28 +0930 | [diff] [blame] | 151 | if (windows_reset_all(context)) { | 
|  | 152 | rc = protocol_events_set(context, | 
|  | 153 | BMC_EVENT_WINDOW_RESET); | 
|  | 154 | if (rc < 0) { | 
|  | 155 | MSG_ERR("Failed to notify host of reset, expect host-side corruption\n"); | 
|  | 156 | break; | 
|  | 157 | } | 
|  | 158 | } | 
| Andrew Jeffery | 17971e4 | 2018-08-08 16:36:10 +0930 | [diff] [blame] | 159 | rc = lpc_reset(context); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 160 | if (rc < 0) { | 
|  | 161 | MSG_ERR("WARNING: Failed to point the " | 
|  | 162 | "LPC bus back to flash on " | 
|  | 163 | "SIGHUP\nIf the host requires " | 
|  | 164 | "this expect problems...\n"); | 
|  | 165 | } | 
|  | 166 | break; | 
|  | 167 | default: | 
|  | 168 | MSG_ERR("Unhandled Signal: %d\n", | 
|  | 169 | info.ssi_signo); | 
|  | 170 | break; | 
|  | 171 | } | 
|  | 172 | } | 
|  | 173 | if (context->fds[DBUS_FD].revents & POLLIN) { /* DBUS */ | 
| Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 174 | while ((rc = sd_bus_process(context->bus, NULL)) > 0) { | 
|  | 175 | MSG_DBG("DBUS Event\n"); | 
|  | 176 | } | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 177 | if (rc < 0) { | 
|  | 178 | MSG_ERR("Error handling DBUS event: %s\n", | 
|  | 179 | strerror(-rc)); | 
|  | 180 | } | 
|  | 181 | } | 
|  | 182 | if (context->terminate) { | 
|  | 183 | break; /* This should mean we clean up nicely */ | 
|  | 184 | } | 
|  | 185 | if (context->fds[MBOX_FD].revents & POLLIN) { /* MBOX */ | 
| Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 186 | MSG_DBG("MBOX Event\n"); | 
| Andrew Jeffery | d86141b | 2018-08-09 14:58:53 +0930 | [diff] [blame] | 187 | rc = transport_mbox_dispatch(context); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 188 | if (rc < 0) { | 
|  | 189 | MSG_ERR("Error handling MBOX event\n"); | 
|  | 190 | } | 
|  | 191 | } | 
|  | 192 | } | 
|  | 193 |  | 
| Deepak Kodihalli | 017e45c | 2017-07-12 01:06:30 -0500 | [diff] [blame] | 194 | /* Best to reset windows and the lpc mapping for safety */ | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 195 | /* Host didn't request reset -> Notify it */ | 
| Andrew Jeffery | fd4fa34 | 2018-11-23 08:33:37 +1100 | [diff] [blame^] | 196 | windows_reset_all(context); | 
|  | 197 |  | 
| Andrew Jeffery | 17971e4 | 2018-08-08 16:36:10 +0930 | [diff] [blame] | 198 | rc = lpc_reset(context); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 199 | /* Not much we can do if this fails */ | 
|  | 200 | if (rc < 0) { | 
|  | 201 | MSG_ERR("WARNING: Failed to point the LPC bus back to flash\n" | 
|  | 202 | "If the host requires this expect problems...\n"); | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | return rc; | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 206 | } | 
|  | 207 |  | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 208 | static int init_signals(struct mbox_context *context, sigset_t *set) | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 209 | { | 
|  | 210 | int rc; | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 211 |  | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 212 | /* Block SIGHUPs, SIGTERMs and SIGINTs */ | 
|  | 213 | sigemptyset(set); | 
|  | 214 | sigaddset(set, SIGHUP); | 
|  | 215 | sigaddset(set, SIGINT); | 
|  | 216 | sigaddset(set, SIGTERM); | 
|  | 217 | rc = sigprocmask(SIG_BLOCK, set, NULL); | 
|  | 218 | if (rc < 0) { | 
|  | 219 | MSG_ERR("Failed to set SIG_BLOCK mask %s\n", strerror(errno)); | 
|  | 220 | return rc; | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 221 | } | 
|  | 222 |  | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 223 | /* Get Signal File Descriptor */ | 
|  | 224 | rc = signalfd(-1, set, SFD_NONBLOCK); | 
|  | 225 | if (rc < 0) { | 
|  | 226 | MSG_ERR("Failed to get signalfd %s\n", strerror(errno)); | 
|  | 227 | return rc; | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 228 | } | 
|  | 229 |  | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 230 | context->fds[SIG_FD].fd = rc; | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 231 | return 0; | 
|  | 232 | } | 
|  | 233 |  | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 234 | static void usage(const char *name) | 
|  | 235 | { | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 236 | printf(USAGE, name); | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 237 | } | 
|  | 238 |  | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 239 | static bool parse_cmdline(int argc, char **argv, | 
|  | 240 | struct mbox_context *context) | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 241 | { | 
| Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 242 | char *endptr; | 
| Suraj Jitindar Singh | c29172e | 2017-04-12 14:26:47 +1000 | [diff] [blame] | 243 | int opt; | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 244 |  | 
|  | 245 | static const struct option long_options[] = { | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 246 | { "flash",		required_argument,	0, 'f' }, | 
|  | 247 | { "window-size",	optional_argument,	0, 'w' }, | 
|  | 248 | { "window-num",		optional_argument,	0, 'n' }, | 
|  | 249 | { "verbose",		no_argument,		0, 'v' }, | 
|  | 250 | { "syslog",		no_argument,		0, 's' }, | 
|  | 251 | { "version",		no_argument,		0, 'V' }, | 
|  | 252 | { "help",		no_argument,		0, 'h' }, | 
|  | 253 | { 0,			0,			0, 0   } | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 254 | }; | 
|  | 255 |  | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 256 | verbosity = MBOX_LOG_NONE; | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 257 | mbox_vlog = &mbox_log_console; | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 258 |  | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 259 | context->current = NULL; /* No current window */ | 
|  | 260 |  | 
|  | 261 | while ((opt = getopt_long(argc, argv, "f:w::n::vsVh", long_options, NULL)) | 
|  | 262 | != -1) { | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 263 | switch (opt) { | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 264 | case 0: | 
|  | 265 | break; | 
|  | 266 | case 'f': | 
|  | 267 | context->flash_size = strtol(optarg, &endptr, 10); | 
|  | 268 | if (optarg == endptr) { | 
|  | 269 | fprintf(stderr, "Unparseable flash size\n"); | 
|  | 270 | return false; | 
|  | 271 | } | 
|  | 272 | switch (*endptr) { | 
|  | 273 | case '\0': | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 274 | break; | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 275 | case 'M': | 
|  | 276 | context->flash_size <<= 10; | 
|  | 277 | case 'K': | 
|  | 278 | context->flash_size <<= 10; | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 279 | break; | 
|  | 280 | default: | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 281 | fprintf(stderr, "Unknown units '%c'\n", | 
|  | 282 | *endptr); | 
|  | 283 | return false; | 
|  | 284 | } | 
|  | 285 | break; | 
|  | 286 | case 'n': | 
|  | 287 | context->windows.num = strtol(argv[optind], &endptr, | 
|  | 288 | 10); | 
|  | 289 | if (optarg == endptr || *endptr != '\0') { | 
|  | 290 | fprintf(stderr, "Unparseable window num\n"); | 
|  | 291 | return false; | 
|  | 292 | } | 
|  | 293 | break; | 
|  | 294 | case 'w': | 
|  | 295 | context->windows.default_size = strtol(argv[optind], | 
|  | 296 | &endptr, 10); | 
|  | 297 | context->windows.default_size <<= 20; /* Given in MB */ | 
|  | 298 | if (optarg == endptr || (*endptr != '\0' && | 
|  | 299 | *endptr != 'M')) { | 
|  | 300 | fprintf(stderr, "Unparseable window size\n"); | 
|  | 301 | return false; | 
|  | 302 | } | 
| Suraj Jitindar Singh | 0aff80c | 2017-04-12 14:37:24 +1000 | [diff] [blame] | 303 | if (!is_power_of_2(context->windows.default_size)) { | 
|  | 304 | fprintf(stderr, "Window size not power of 2\n"); | 
|  | 305 | return false; | 
|  | 306 | } | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 307 | break; | 
|  | 308 | case 'v': | 
|  | 309 | verbosity++; | 
|  | 310 | break; | 
|  | 311 | case 's': | 
|  | 312 | /* Avoid a double openlog() */ | 
|  | 313 | if (mbox_vlog != &vsyslog) { | 
|  | 314 | openlog(PREFIX, LOG_ODELAY, LOG_DAEMON); | 
|  | 315 | mbox_vlog = &vsyslog; | 
|  | 316 | } | 
|  | 317 | break; | 
|  | 318 | case 'V': | 
| Suraj Jitindar Singh | 8d65bb4 | 2017-05-01 16:05:17 +1000 | [diff] [blame] | 319 | printf("%s V%s\n", THIS_NAME, PACKAGE_VERSION); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 320 | exit(0); | 
|  | 321 | case 'h': | 
|  | 322 | return false; /* This will print the usage message */ | 
|  | 323 | default: | 
|  | 324 | return false; | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 325 | } | 
|  | 326 | } | 
|  | 327 |  | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 328 | if (!context->flash_size) { | 
| Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 329 | fprintf(stderr, "Must specify a non-zero flash size\n"); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 330 | return false; | 
| Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 331 | } | 
|  | 332 |  | 
| Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 333 | MSG_INFO("Flash size: 0x%.8x\n", context->flash_size); | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 334 |  | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 335 | if (verbosity) { | 
| Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 336 | MSG_INFO("%s logging\n", verbosity == MBOX_LOG_DEBUG ? "Debug" : | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 337 | "Verbose"); | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 338 | } | 
|  | 339 |  | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 340 | return true; | 
| Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 341 | } | 
|  | 342 |  | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 343 | int main(int argc, char **argv) | 
|  | 344 | { | 
| Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 345 | const struct transport_ops *mbox_ops, *dbus_ops; | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 346 | struct mbox_context *context; | 
|  | 347 | char *name = argv[0]; | 
|  | 348 | sigset_t set; | 
|  | 349 | int rc, i; | 
|  | 350 |  | 
|  | 351 | context = calloc(1, sizeof(*context)); | 
|  | 352 | if (!context) { | 
|  | 353 | fprintf(stderr, "Memory allocation failed\n"); | 
|  | 354 | exit(1); | 
|  | 355 | } | 
|  | 356 |  | 
|  | 357 | if (!parse_cmdline(argc, argv, context)) { | 
|  | 358 | usage(name); | 
|  | 359 | free(context); | 
|  | 360 | exit(0); | 
|  | 361 | } | 
|  | 362 |  | 
|  | 363 | for (i = 0; i < TOTAL_FDS; i++) { | 
|  | 364 | context->fds[i].fd = -1; | 
|  | 365 | } | 
|  | 366 |  | 
| Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 367 | MSG_INFO("Starting Daemon\n"); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 368 |  | 
|  | 369 | rc = init_signals(context, &set); | 
|  | 370 | if (rc) { | 
|  | 371 | goto finish; | 
|  | 372 | } | 
|  | 373 |  | 
| Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 374 | rc = protocol_init(context); | 
|  | 375 | if (rc) { | 
|  | 376 | goto finish; | 
|  | 377 | } | 
|  | 378 |  | 
| Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 379 | rc = transport_mbox_init(context, &mbox_ops); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 380 | if (rc) { | 
|  | 381 | goto finish; | 
|  | 382 | } | 
|  | 383 |  | 
| Andrew Jeffery | cb9b210 | 2018-08-08 16:31:07 +0930 | [diff] [blame] | 384 | rc = lpc_dev_init(context); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 385 | if (rc) { | 
|  | 386 | goto finish; | 
|  | 387 | } | 
|  | 388 |  | 
|  | 389 | /* We've found the reserved memory region -> we can assign to windows */ | 
| Andrew Jeffery | c1a67fa | 2018-08-08 17:07:38 +0930 | [diff] [blame] | 390 | rc = windows_init(context); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 391 | if (rc) { | 
|  | 392 | goto finish; | 
|  | 393 | } | 
|  | 394 |  | 
| Andrew Jeffery | d6b09bc | 2018-08-08 16:47:54 +0930 | [diff] [blame] | 395 | rc = flash_dev_init(context); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 396 | if (rc) { | 
|  | 397 | goto finish; | 
|  | 398 | } | 
|  | 399 |  | 
| Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 400 | rc = dbus_init(context, &dbus_ops); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 401 | if (rc) { | 
|  | 402 | goto finish; | 
|  | 403 | } | 
|  | 404 |  | 
| Deepak Kodihalli | 017e45c | 2017-07-12 01:06:30 -0500 | [diff] [blame] | 405 | #ifdef VIRTUAL_PNOR_ENABLED | 
| Deepak Kodihalli | 64ec3e4 | 2017-07-17 06:15:16 -0500 | [diff] [blame] | 406 | init_vpnor(context); | 
| Deepak Kodihalli | 017e45c | 2017-07-12 01:06:30 -0500 | [diff] [blame] | 407 | #endif | 
|  | 408 |  | 
|  | 409 | /* Set the LPC bus mapping */ | 
| Andrew Jeffery | 17971e4 | 2018-08-08 16:36:10 +0930 | [diff] [blame] | 410 | rc = lpc_reset(context); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 411 | if (rc) { | 
| Andrew Jeffery | 8fe809e | 2018-05-17 09:54:32 +0930 | [diff] [blame] | 412 | MSG_ERR("LPC configuration failed, RESET required: %d\n", rc); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 413 | } | 
|  | 414 |  | 
| Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 415 | /* We're ready to go, alert the host */ | 
|  | 416 | context->bmc_events |= BMC_EVENT_DAEMON_READY; | 
|  | 417 |  | 
|  | 418 | /* Alert on all supported transports */ | 
|  | 419 | rc = protocol_events_put(context, mbox_ops); | 
|  | 420 | if (rc) { | 
|  | 421 | goto finish; | 
|  | 422 | } | 
|  | 423 |  | 
|  | 424 | rc = protocol_events_put(context, dbus_ops); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 425 | if (rc) { | 
|  | 426 | goto finish; | 
|  | 427 | } | 
|  | 428 |  | 
| Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 429 | MSG_INFO("Entering Polling Loop\n"); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 430 | rc = poll_loop(context); | 
|  | 431 |  | 
| Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 432 | MSG_INFO("Exiting Poll Loop: %d\n", rc); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 433 |  | 
|  | 434 | finish: | 
| Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 435 | MSG_INFO("Daemon Exiting...\n"); | 
| Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 436 | context->bmc_events &= ~BMC_EVENT_DAEMON_READY; | 
| Andrew Jeffery | fab672b | 2018-11-01 17:12:09 +1030 | [diff] [blame] | 437 | context->bmc_events |= BMC_EVENT_PROTOCOL_RESET; | 
| Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 438 |  | 
|  | 439 | /* Alert on all supported transports */ | 
|  | 440 | protocol_events_put(context, mbox_ops); | 
|  | 441 | protocol_events_put(context, dbus_ops); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 442 |  | 
| Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 443 | #ifdef VIRTUAL_PNOR_ENABLED | 
|  | 444 | destroy_vpnor(context); | 
|  | 445 | #endif | 
| Andrew Jeffery | ef9e62d | 2018-08-08 15:48:27 +0930 | [diff] [blame] | 446 | dbus_free(context); | 
| Andrew Jeffery | dec6ca6 | 2018-08-08 16:49:43 +0930 | [diff] [blame] | 447 | flash_dev_free(context); | 
| Andrew Jeffery | 2e2df28 | 2018-08-08 16:32:22 +0930 | [diff] [blame] | 448 | lpc_dev_free(context); | 
| Andrew Jeffery | 55260ce | 2018-08-09 15:05:59 +0930 | [diff] [blame] | 449 | transport_mbox_free(context); | 
| Andrew Jeffery | f5f5142 | 2018-08-08 17:08:33 +0930 | [diff] [blame] | 450 | windows_free(context); | 
| Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 451 | protocol_free(context); | 
| Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 452 | free(context); | 
|  | 453 |  | 
|  | 454 | return rc; | 
|  | 455 | } |