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