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" |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 34 | #include "backend.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 | f4bc335 | 2019-03-18 12:09:48 +1030 | [diff] [blame] | 39 | #include "vpnor/backend.h" |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 40 | |
Evan Lojewski | a042978 | 2019-03-13 15:25:44 +1030 | [diff] [blame] | 41 | const char* 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" |
| 46 | #ifdef VIRTUAL_PNOR_ENABLED |
Andrew Jeffery | 5320f6e | 2019-03-15 12:40:41 +1030 | [diff] [blame] | 47 | "\t\t-b | --backend <vpnor|mtd[:PATH]|file:PATH>\n" |
Evan Lojewski | a042978 | 2019-03-13 15:25:44 +1030 | [diff] [blame] | 48 | #else |
Andrew Jeffery | 5320f6e | 2019-03-15 12:40:41 +1030 | [diff] [blame] | 49 | "\t\t-b | --backend <mtd[:PATH]|file:PATH>\n" |
Evan Lojewski | a042978 | 2019-03-13 15:25:44 +1030 | [diff] [blame] | 50 | #endif |
| 51 | "\t-v | --verbose\t\tBe [more] verbose\n" |
| 52 | "\t-s | --syslog\t\tLog output to syslog (pointless without -v)\n" |
| 53 | "\t-n | --window-num\tThe number of windows\n" |
| 54 | "\t\t\t\t(default: fill the reserved memory region)\n" |
| 55 | "\t-w | --window-size\tThe window size (power of 2) in MB\n" |
| 56 | "\t\t\t\t(default: 1MB)\n" |
Stewart Smith | ef0c836 | 2018-11-19 13:49:46 +1100 | [diff] [blame] | 57 | "\t-f | --flash\t\tSize of flash in [K|M] bytes\n\n" |
| 58 | "\t-t | --trace\t\tFile to write trace data to (in blktrace format)\n\n"; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 59 | |
Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 60 | static int dbus_init(struct mbox_context *context, |
| 61 | const struct transport_ops **ops) |
Andrew Jeffery | ef9e62d | 2018-08-08 15:48:27 +0930 | [diff] [blame] | 62 | { |
| 63 | int rc; |
| 64 | |
| 65 | rc = sd_bus_default_system(&context->bus); |
| 66 | if (rc < 0) { |
| 67 | MSG_ERR("Failed to connect to the system bus: %s\n", |
| 68 | strerror(-rc)); |
| 69 | return rc; |
| 70 | } |
| 71 | |
| 72 | rc = control_legacy_init(context); |
| 73 | if (rc < 0) { |
| 74 | MSG_ERR("Failed to initialise legacy DBus interface: %s\n", |
| 75 | strerror(-rc)); |
| 76 | return rc; |
| 77 | } |
| 78 | |
| 79 | rc = control_dbus_init(context); |
| 80 | if (rc < 0) { |
| 81 | MSG_ERR("Failed to initialise DBus control interface: %s\n", |
| 82 | strerror(-rc)); |
| 83 | return rc; |
| 84 | } |
| 85 | |
Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 86 | rc = transport_dbus_init(context, ops); |
Andrew Jeffery | 23140be | 2018-09-05 14:15:07 +0930 | [diff] [blame] | 87 | if (rc < 0) { |
| 88 | MSG_ERR("Failed to initialise DBus protocol interface: %s\n", |
| 89 | strerror(-rc)); |
| 90 | return rc; |
| 91 | } |
| 92 | |
Andrew Jeffery | ef9e62d | 2018-08-08 15:48:27 +0930 | [diff] [blame] | 93 | rc = sd_bus_request_name(context->bus, MBOX_DBUS_NAME, |
| 94 | SD_BUS_NAME_ALLOW_REPLACEMENT | |
| 95 | SD_BUS_NAME_REPLACE_EXISTING); |
| 96 | if (rc < 0) { |
Andrew Jeffery | 23140be | 2018-09-05 14:15:07 +0930 | [diff] [blame] | 97 | MSG_ERR("Failed to request DBus name: %s\n", strerror(-rc)); |
Andrew Jeffery | ef9e62d | 2018-08-08 15:48:27 +0930 | [diff] [blame] | 98 | return rc; |
| 99 | } |
| 100 | |
| 101 | rc = sd_bus_get_fd(context->bus); |
| 102 | if (rc < 0) { |
| 103 | MSG_ERR("Failed to get bus fd: %s\n", strerror(-rc)); |
| 104 | return rc; |
| 105 | } |
| 106 | |
| 107 | context->fds[DBUS_FD].fd = rc; |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static void dbus_free(struct mbox_context *context) |
| 113 | { |
Andrew Jeffery | 23140be | 2018-09-05 14:15:07 +0930 | [diff] [blame] | 114 | transport_dbus_free(context); |
Andrew Jeffery | ef9e62d | 2018-08-08 15:48:27 +0930 | [diff] [blame] | 115 | control_dbus_free(context); |
| 116 | control_legacy_free(context); |
| 117 | sd_bus_unref(context->bus); |
| 118 | } |
Andrew Jeffery | 55f4d6f | 2018-08-06 12:26:44 +0930 | [diff] [blame] | 119 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 120 | static int poll_loop(struct mbox_context *context) |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 121 | { |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 122 | int rc = 0, i; |
Cyril Bur | 4623367 | 2017-01-16 13:33:26 +1100 | [diff] [blame] | 123 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 124 | /* Set POLLIN on polling file descriptors */ |
| 125 | for (i = 0; i < POLL_FDS; i++) { |
| 126 | context->fds[i].events = POLLIN; |
Cyril Bur | 4623367 | 2017-01-16 13:33:26 +1100 | [diff] [blame] | 127 | } |
| 128 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 129 | while (1) { |
| 130 | rc = poll(context->fds, POLL_FDS, -1); |
| 131 | |
| 132 | if (rc < 0) { /* Error */ |
| 133 | MSG_ERR("Error from poll(): %s\n", strerror(errno)); |
| 134 | break; /* This should mean we clean up nicely */ |
| 135 | } |
| 136 | |
| 137 | /* Event on Polled File Descriptor - Handle It */ |
| 138 | if (context->fds[SIG_FD].revents & POLLIN) { /* Signal */ |
| 139 | struct signalfd_siginfo info = { 0 }; |
| 140 | |
| 141 | rc = read(context->fds[SIG_FD].fd, (void *) &info, |
| 142 | sizeof(info)); |
| 143 | if (rc != sizeof(info)) { |
| 144 | MSG_ERR("Error reading signal event: %s\n", |
| 145 | strerror(errno)); |
| 146 | } |
| 147 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 148 | MSG_DBG("Received signal: %d\n", info.ssi_signo); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 149 | switch (info.ssi_signo) { |
| 150 | case SIGINT: |
| 151 | case SIGTERM: |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 152 | MSG_INFO("Caught Signal - Exiting...\n"); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 153 | context->terminate = true; |
| 154 | break; |
| 155 | case SIGHUP: |
Andrew Jeffery | f69760d | 2019-03-14 16:54:13 +1030 | [diff] [blame] | 156 | rc = protocol_reset(context); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 157 | if (rc < 0) { |
Andrew Jeffery | f69760d | 2019-03-14 16:54:13 +1030 | [diff] [blame] | 158 | MSG_ERR("Failed to reset on SIGHUP\n"); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 159 | } |
| 160 | break; |
| 161 | default: |
| 162 | MSG_ERR("Unhandled Signal: %d\n", |
| 163 | info.ssi_signo); |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | if (context->fds[DBUS_FD].revents & POLLIN) { /* DBUS */ |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 168 | while ((rc = sd_bus_process(context->bus, NULL)) > 0) { |
| 169 | MSG_DBG("DBUS Event\n"); |
| 170 | } |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 171 | if (rc < 0) { |
| 172 | MSG_ERR("Error handling DBUS event: %s\n", |
| 173 | strerror(-rc)); |
| 174 | } |
| 175 | } |
| 176 | if (context->terminate) { |
| 177 | break; /* This should mean we clean up nicely */ |
| 178 | } |
| 179 | if (context->fds[MBOX_FD].revents & POLLIN) { /* MBOX */ |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 180 | MSG_DBG("MBOX Event\n"); |
Andrew Jeffery | d86141b | 2018-08-09 14:58:53 +0930 | [diff] [blame] | 181 | rc = transport_mbox_dispatch(context); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 182 | if (rc < 0) { |
| 183 | MSG_ERR("Error handling MBOX event\n"); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
Andrew Jeffery | f69760d | 2019-03-14 16:54:13 +1030 | [diff] [blame] | 188 | rc = protocol_reset(context); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 189 | if (rc < 0) { |
Andrew Jeffery | f69760d | 2019-03-14 16:54:13 +1030 | [diff] [blame] | 190 | MSG_ERR("Failed to reset during poll loop cleanup\n"); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | return rc; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 194 | } |
| 195 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 196 | static int init_signals(struct mbox_context *context, sigset_t *set) |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 197 | { |
| 198 | int rc; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 199 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 200 | /* Block SIGHUPs, SIGTERMs and SIGINTs */ |
| 201 | sigemptyset(set); |
| 202 | sigaddset(set, SIGHUP); |
| 203 | sigaddset(set, SIGINT); |
| 204 | sigaddset(set, SIGTERM); |
| 205 | rc = sigprocmask(SIG_BLOCK, set, NULL); |
| 206 | if (rc < 0) { |
| 207 | MSG_ERR("Failed to set SIG_BLOCK mask %s\n", strerror(errno)); |
| 208 | return rc; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 209 | } |
| 210 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 211 | /* Get Signal File Descriptor */ |
| 212 | rc = signalfd(-1, set, SFD_NONBLOCK); |
| 213 | if (rc < 0) { |
| 214 | MSG_ERR("Failed to get signalfd %s\n", strerror(errno)); |
| 215 | return rc; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 216 | } |
| 217 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 218 | context->fds[SIG_FD].fd = rc; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 222 | static void usage(const char *name) |
| 223 | { |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 224 | printf(USAGE, name); |
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 | static bool parse_cmdline(int argc, char **argv, |
| 228 | struct mbox_context *context) |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 229 | { |
Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 230 | char *endptr; |
Suraj Jitindar Singh | c29172e | 2017-04-12 14:26:47 +1000 | [diff] [blame] | 231 | int opt; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 232 | |
| 233 | static const struct option long_options[] = { |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 234 | { "flash", required_argument, 0, 'f' }, |
Evan Lojewski | a042978 | 2019-03-13 15:25:44 +1030 | [diff] [blame] | 235 | { "backend", required_argument, 0, 'b' }, |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 236 | { "window-size", optional_argument, 0, 'w' }, |
| 237 | { "window-num", optional_argument, 0, 'n' }, |
| 238 | { "verbose", no_argument, 0, 'v' }, |
| 239 | { "syslog", no_argument, 0, 's' }, |
Stewart Smith | ef0c836 | 2018-11-19 13:49:46 +1100 | [diff] [blame] | 240 | { "trace", optional_argument, 0, 't' }, |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 241 | { "version", no_argument, 0, 'V' }, |
| 242 | { "help", no_argument, 0, 'h' }, |
| 243 | { 0, 0, 0, 0 } |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 244 | }; |
| 245 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 246 | verbosity = MBOX_LOG_NONE; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 247 | mbox_vlog = &mbox_log_console; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 248 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 249 | context->current = NULL; /* No current window */ |
| 250 | |
Stewart Smith | ef0c836 | 2018-11-19 13:49:46 +1100 | [diff] [blame] | 251 | while ((opt = getopt_long(argc, argv, "f:b:w::n::vst::Vh", long_options, NULL)) |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 252 | != -1) { |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 253 | switch (opt) { |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 254 | case 0: |
| 255 | break; |
| 256 | case 'f': |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 257 | context->backend.flash_size = strtol(optarg, &endptr, 10); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 258 | if (optarg == endptr) { |
| 259 | fprintf(stderr, "Unparseable flash size\n"); |
| 260 | return false; |
| 261 | } |
| 262 | switch (*endptr) { |
| 263 | case '\0': |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 264 | break; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 265 | case 'M': |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 266 | context->backend.flash_size <<= 10; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 267 | case 'K': |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 268 | context->backend.flash_size <<= 10; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 269 | break; |
| 270 | default: |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 271 | fprintf(stderr, "Unknown units '%c'\n", |
| 272 | *endptr); |
| 273 | return false; |
| 274 | } |
| 275 | break; |
Evan Lojewski | a042978 | 2019-03-13 15:25:44 +1030 | [diff] [blame] | 276 | case 'b': |
Andrew Jeffery | 5320f6e | 2019-03-15 12:40:41 +1030 | [diff] [blame] | 277 | context->source = optarg; |
Evan Lojewski | a042978 | 2019-03-13 15:25:44 +1030 | [diff] [blame] | 278 | break; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 279 | case 'n': |
| 280 | context->windows.num = strtol(argv[optind], &endptr, |
| 281 | 10); |
| 282 | if (optarg == endptr || *endptr != '\0') { |
| 283 | fprintf(stderr, "Unparseable window num\n"); |
| 284 | return false; |
| 285 | } |
| 286 | break; |
| 287 | case 'w': |
| 288 | context->windows.default_size = strtol(argv[optind], |
| 289 | &endptr, 10); |
| 290 | context->windows.default_size <<= 20; /* Given in MB */ |
| 291 | if (optarg == endptr || (*endptr != '\0' && |
| 292 | *endptr != 'M')) { |
| 293 | fprintf(stderr, "Unparseable window size\n"); |
| 294 | return false; |
| 295 | } |
Suraj Jitindar Singh | 0aff80c | 2017-04-12 14:37:24 +1000 | [diff] [blame] | 296 | if (!is_power_of_2(context->windows.default_size)) { |
| 297 | fprintf(stderr, "Window size not power of 2\n"); |
| 298 | return false; |
| 299 | } |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 300 | break; |
| 301 | case 'v': |
| 302 | verbosity++; |
| 303 | break; |
| 304 | case 's': |
| 305 | /* Avoid a double openlog() */ |
| 306 | if (mbox_vlog != &vsyslog) { |
| 307 | openlog(PREFIX, LOG_ODELAY, LOG_DAEMON); |
| 308 | mbox_vlog = &vsyslog; |
| 309 | } |
| 310 | break; |
| 311 | case 'V': |
Suraj Jitindar Singh | 8d65bb4 | 2017-05-01 16:05:17 +1000 | [diff] [blame] | 312 | printf("%s V%s\n", THIS_NAME, PACKAGE_VERSION); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 313 | exit(0); |
Stewart Smith | ef0c836 | 2018-11-19 13:49:46 +1100 | [diff] [blame] | 314 | case 't': |
| 315 | context->blktracefd = open(argv[optind], |
| 316 | O_CREAT|O_TRUNC|O_WRONLY, |
| 317 | 0666); |
| 318 | printf("Recording blktrace output to %s\n", |
| 319 | argv[optind]); |
| 320 | if (context->blktracefd == -1) { |
| 321 | perror("Couldn't open blktrace file for writing"); |
| 322 | exit(2); |
| 323 | } |
| 324 | break; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 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 | |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 332 | if (!context->backend.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 | |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 337 | MSG_INFO("Flash size: 0x%.8x\n", context->backend.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 | |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 347 | static int mboxd_backend_init(struct mbox_context *context) |
| 348 | { |
Andrew Jeffery | 5320f6e | 2019-03-15 12:40:41 +1030 | [diff] [blame] | 349 | const char *delim; |
| 350 | const char *path; |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 351 | int rc; |
| 352 | |
Andrew Jeffery | 5320f6e | 2019-03-15 12:40:41 +1030 | [diff] [blame] | 353 | if (!context->source) { |
| 354 | struct vpnor_partition_paths paths; |
| 355 | vpnor_default_paths(&paths); |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 356 | |
Andrew Jeffery | 5320f6e | 2019-03-15 12:40:41 +1030 | [diff] [blame] | 357 | rc = backend_probe_vpnor(&context->backend, &paths); |
| 358 | if(rc < 0) |
| 359 | rc = backend_probe_mtd(&context->backend, NULL); |
| 360 | |
| 361 | return rc; |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 362 | } |
| 363 | |
Andrew Jeffery | 5320f6e | 2019-03-15 12:40:41 +1030 | [diff] [blame] | 364 | delim = strchr(context->source, ':'); |
| 365 | path = delim ? delim + 1 : NULL; |
| 366 | |
| 367 | if (!strncmp(context->source, "vpnor", strlen("vpnor"))) { |
| 368 | struct vpnor_partition_paths paths; |
| 369 | |
| 370 | if (path) { |
| 371 | rc = -EINVAL; |
| 372 | } else { |
| 373 | vpnor_default_paths(&paths); |
| 374 | rc = backend_probe_vpnor(&context->backend, &paths); |
| 375 | } |
| 376 | } else if (!strncmp(context->source, "mtd", strlen("mtd"))) { |
| 377 | rc = backend_probe_mtd(&context->backend, path); |
| 378 | } else if (!strncmp(context->source, "file", strlen("file"))) { |
| 379 | rc = backend_probe_file(&context->backend, path); |
| 380 | } else { |
| 381 | rc = -EINVAL; |
| 382 | } |
| 383 | |
| 384 | if (rc < 0) |
| 385 | MSG_ERR("Invalid backend argument: %s\n", context->source); |
| 386 | |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 387 | return rc; |
| 388 | } |
| 389 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 390 | int main(int argc, char **argv) |
| 391 | { |
Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 392 | const struct transport_ops *mbox_ops, *dbus_ops; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 393 | struct mbox_context *context; |
Andrew Jeffery | 4b8203d | 2019-05-06 14:36:16 +0930 | [diff] [blame] | 394 | bool have_transport_mbox; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 395 | char *name = argv[0]; |
| 396 | sigset_t set; |
| 397 | int rc, i; |
| 398 | |
| 399 | context = calloc(1, sizeof(*context)); |
| 400 | if (!context) { |
| 401 | fprintf(stderr, "Memory allocation failed\n"); |
| 402 | exit(1); |
| 403 | } |
| 404 | |
| 405 | if (!parse_cmdline(argc, argv, context)) { |
| 406 | usage(name); |
| 407 | free(context); |
| 408 | exit(0); |
| 409 | } |
| 410 | |
| 411 | for (i = 0; i < TOTAL_FDS; i++) { |
| 412 | context->fds[i].fd = -1; |
| 413 | } |
| 414 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 415 | MSG_INFO("Starting Daemon\n"); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 416 | |
| 417 | rc = init_signals(context, &set); |
| 418 | if (rc) { |
Andrew Jeffery | 4ef0c13 | 2019-03-19 15:14:13 +1030 | [diff] [blame] | 419 | goto cleanup_context; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 420 | } |
| 421 | |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 422 | rc = mboxd_backend_init(context); |
| 423 | if (rc) { |
Andrew Jeffery | 4ef0c13 | 2019-03-19 15:14:13 +1030 | [diff] [blame] | 424 | goto cleanup_context; |
Evan Lojewski | f1e547c | 2019-03-14 14:34:33 +1030 | [diff] [blame] | 425 | } |
| 426 | |
Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 427 | rc = protocol_init(context); |
| 428 | if (rc) { |
Andrew Jeffery | 4ef0c13 | 2019-03-19 15:14:13 +1030 | [diff] [blame] | 429 | goto cleanup_backend; |
Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 430 | } |
| 431 | |
Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 432 | rc = transport_mbox_init(context, &mbox_ops); |
Andrew Jeffery | 4b8203d | 2019-05-06 14:36:16 +0930 | [diff] [blame] | 433 | /* TODO: Think about whether we could use a less branch-y strategy */ |
| 434 | have_transport_mbox = rc == 0; |
| 435 | if (!have_transport_mbox) { |
| 436 | /* Disable MBOX for poll()ing purposes */ |
| 437 | context->fds[MBOX_FD].fd = -1; |
| 438 | MSG_DBG("Failed to initialise MBOX transport: %d\n", rc); |
| 439 | MSG_INFO("MBOX transport unavailable\n"); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 440 | } |
| 441 | |
Andrew Jeffery | cb9b210 | 2018-08-08 16:31:07 +0930 | [diff] [blame] | 442 | rc = lpc_dev_init(context); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 443 | if (rc) { |
Andrew Jeffery | 4ef0c13 | 2019-03-19 15:14:13 +1030 | [diff] [blame] | 444 | goto cleanup_mbox; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* We've found the reserved memory region -> we can assign to windows */ |
Andrew Jeffery | c1a67fa | 2018-08-08 17:07:38 +0930 | [diff] [blame] | 448 | rc = windows_init(context); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 449 | if (rc) { |
Andrew Jeffery | 4ef0c13 | 2019-03-19 15:14:13 +1030 | [diff] [blame] | 450 | goto cleanup_lpc; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 451 | } |
| 452 | |
Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 453 | rc = dbus_init(context, &dbus_ops); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 454 | if (rc) { |
Andrew Jeffery | 4ef0c13 | 2019-03-19 15:14:13 +1030 | [diff] [blame] | 455 | goto cleanup_windows; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 456 | } |
| 457 | |
Deepak Kodihalli | 017e45c | 2017-07-12 01:06:30 -0500 | [diff] [blame] | 458 | /* Set the LPC bus mapping */ |
Andrew Jeffery | f69760d | 2019-03-14 16:54:13 +1030 | [diff] [blame] | 459 | __protocol_reset(context); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 460 | |
Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 461 | /* We're ready to go, alert the host */ |
| 462 | context->bmc_events |= BMC_EVENT_DAEMON_READY; |
Andrew Jeffery | 4c15bb1 | 2018-12-05 14:15:28 +1030 | [diff] [blame] | 463 | context->bmc_events |= BMC_EVENT_PROTOCOL_RESET; |
Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 464 | |
Andrew Jeffery | 4b8203d | 2019-05-06 14:36:16 +0930 | [diff] [blame] | 465 | /* Alert on all supported transports, as required */ |
| 466 | if (have_transport_mbox) { |
| 467 | rc = protocol_events_put(context, mbox_ops); |
| 468 | if (rc) { |
| 469 | goto cleanup; |
| 470 | } |
Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | rc = protocol_events_put(context, dbus_ops); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 474 | if (rc) { |
Andrew Jeffery | 4ef0c13 | 2019-03-19 15:14:13 +1030 | [diff] [blame] | 475 | goto cleanup; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 476 | } |
| 477 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 478 | MSG_INFO("Entering Polling Loop\n"); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 479 | rc = poll_loop(context); |
| 480 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 481 | MSG_INFO("Exiting Poll Loop: %d\n", rc); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 482 | |
Suraj Jitindar Singh | 2851959 | 2017-04-27 14:48:58 +1000 | [diff] [blame] | 483 | MSG_INFO("Daemon Exiting...\n"); |
Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 484 | context->bmc_events &= ~BMC_EVENT_DAEMON_READY; |
Andrew Jeffery | fab672b | 2018-11-01 17:12:09 +1030 | [diff] [blame] | 485 | context->bmc_events |= BMC_EVENT_PROTOCOL_RESET; |
Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 486 | |
Andrew Jeffery | 4b8203d | 2019-05-06 14:36:16 +0930 | [diff] [blame] | 487 | /* Alert on all supported transports, as required */ |
| 488 | if (have_transport_mbox) { |
| 489 | protocol_events_put(context, mbox_ops); |
| 490 | } |
| 491 | |
Andrew Jeffery | fe0c9e8 | 2018-11-01 14:02:17 +1030 | [diff] [blame] | 492 | protocol_events_put(context, dbus_ops); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 493 | |
Andrew Jeffery | 4ef0c13 | 2019-03-19 15:14:13 +1030 | [diff] [blame] | 494 | cleanup: |
Andrew Jeffery | ef9e62d | 2018-08-08 15:48:27 +0930 | [diff] [blame] | 495 | dbus_free(context); |
Andrew Jeffery | 4ef0c13 | 2019-03-19 15:14:13 +1030 | [diff] [blame] | 496 | cleanup_windows: |
Andrew Jeffery | f5f5142 | 2018-08-08 17:08:33 +0930 | [diff] [blame] | 497 | windows_free(context); |
Andrew Jeffery | 4ef0c13 | 2019-03-19 15:14:13 +1030 | [diff] [blame] | 498 | cleanup_lpc: |
| 499 | lpc_dev_free(context); |
| 500 | cleanup_mbox: |
Andrew Jeffery | 4b8203d | 2019-05-06 14:36:16 +0930 | [diff] [blame] | 501 | if (have_transport_mbox) { |
| 502 | transport_mbox_free(context); |
| 503 | } |
Andrew Jeffery | 1e531af | 2018-08-07 13:32:57 +0930 | [diff] [blame] | 504 | protocol_free(context); |
Andrew Jeffery | 4ef0c13 | 2019-03-19 15:14:13 +1030 | [diff] [blame] | 505 | cleanup_backend: |
| 506 | backend_free(&context->backend); |
| 507 | cleanup_context: |
Stewart Smith | ef0c836 | 2018-11-19 13:49:46 +1100 | [diff] [blame] | 508 | if (context->blktracefd) |
| 509 | close(context->blktracefd); |
| 510 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 511 | free(context); |
| 512 | |
| 513 | return rc; |
| 514 | } |