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