blob: 04b2cb61b42a19743efbf0d98538e4041769057a [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Cyril Burc85e34d2016-11-15 11:50:41 +11003
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11004#define _GNU_SOURCE
Cyril Burc85e34d2016-11-15 11:50:41 +11005#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 Neuling899ebac2017-01-14 11:20:26 -060017#include <signal.h>
Cyril Burc85e34d2016-11-15 11:50:41 +110018#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 Singhe39c9162017-03-28 10:47:43 +110023#include <sys/signalfd.h>
Cyril Burc85e34d2016-11-15 11:50:41 +110024#include <time.h>
25#include <unistd.h>
Andrew Jeffery78210b92017-01-13 13:06:09 +103026#include <inttypes.h>
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110027#include <systemd/sd-bus.h>
Cyril Burc85e34d2016-11-15 11:50:41 +110028
Suraj Jitindar Singh8d65bb42017-05-01 16:05:17 +100029#include "config.h"
Andrew Jeffery26558db2018-08-10 00:22:38 +093030#include "mboxd.h"
Cyril Burc85e34d2016-11-15 11:50:41 +110031#include "common.h"
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110032#include "dbus.h"
Andrew Jeffery55f4d6f2018-08-06 12:26:44 +093033#include "control_dbus.h"
Evan Lojewskif1e547c2019-03-14 14:34:33 +103034#include "backend.h"
Andrew Jefferycd186112018-08-08 10:47:55 +093035#include "lpc.h"
Andrew Jeffery23140be2018-09-05 14:15:07 +093036#include "transport_dbus.h"
Andrew Jefferyf593b1b2018-08-08 11:01:04 +093037#include "windows.h"
Andrew Jefferyf4bc3352019-03-18 12:09:48 +103038#include "vpnor/backend.h"
Cyril Burc85e34d2016-11-15 11:50:41 +110039
Evan Lojewskia0429782019-03-13 15:25:44 +103040const char* USAGE =
41 "\nUsage: %s [-V | --version] [-h | --help] [-v[v] | --verbose] [-s | --syslog]\n"
42 "\t\t[-n | --window-num <num>]\n"
43 "\t\t[-w | --window-size <size>M]\n"
44 "\t\t-f | --flash <size>[K|M]\n"
45#ifdef VIRTUAL_PNOR_ENABLED
Andrew Jeffery5320f6e2019-03-15 12:40:41 +103046 "\t\t-b | --backend <vpnor|mtd[:PATH]|file:PATH>\n"
Evan Lojewskia0429782019-03-13 15:25:44 +103047#else
Andrew Jeffery5320f6e2019-03-15 12:40:41 +103048 "\t\t-b | --backend <mtd[:PATH]|file:PATH>\n"
Evan Lojewskia0429782019-03-13 15:25:44 +103049#endif
50 "\t-v | --verbose\t\tBe [more] verbose\n"
51 "\t-s | --syslog\t\tLog output to syslog (pointless without -v)\n"
52 "\t-n | --window-num\tThe number of windows\n"
53 "\t\t\t\t(default: fill the reserved memory region)\n"
54 "\t-w | --window-size\tThe window size (power of 2) in MB\n"
55 "\t\t\t\t(default: 1MB)\n"
Stewart Smithef0c8362018-11-19 13:49:46 +110056 "\t-f | --flash\t\tSize of flash in [K|M] bytes\n\n"
57 "\t-t | --trace\t\tFile to write trace data to (in blktrace format)\n\n";
Cyril Burc85e34d2016-11-15 11:50:41 +110058
Andrew Jefferyfe0c9e82018-11-01 14:02:17 +103059static int dbus_init(struct mbox_context *context,
60 const struct transport_ops **ops)
Andrew Jefferyef9e62d2018-08-08 15:48:27 +093061{
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 Jefferyfe0c9e82018-11-01 14:02:17 +103085 rc = transport_dbus_init(context, ops);
Andrew Jeffery23140be2018-09-05 14:15:07 +093086 if (rc < 0) {
87 MSG_ERR("Failed to initialise DBus protocol interface: %s\n",
88 strerror(-rc));
89 return rc;
90 }
91
Andrew Jefferyef9e62d2018-08-08 15:48:27 +093092 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 Jeffery23140be2018-09-05 14:15:07 +093096 MSG_ERR("Failed to request DBus name: %s\n", strerror(-rc));
Andrew Jefferyef9e62d2018-08-08 15:48:27 +093097 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
111static void dbus_free(struct mbox_context *context)
112{
Andrew Jeffery23140be2018-09-05 14:15:07 +0930113 transport_dbus_free(context);
Andrew Jefferyef9e62d2018-08-08 15:48:27 +0930114 control_dbus_free(context);
115 control_legacy_free(context);
116 sd_bus_unref(context->bus);
117}
Andrew Jeffery55f4d6f2018-08-06 12:26:44 +0930118
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100119static int poll_loop(struct mbox_context *context)
Cyril Burc85e34d2016-11-15 11:50:41 +1100120{
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100121 int rc = 0, i;
Cyril Bur46233672017-01-16 13:33:26 +1100122
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100123 /* Set POLLIN on polling file descriptors */
124 for (i = 0; i < POLL_FDS; i++) {
125 context->fds[i].events = POLLIN;
Cyril Bur46233672017-01-16 13:33:26 +1100126 }
127
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100128 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 Singh28519592017-04-27 14:48:58 +1000147 MSG_DBG("Received signal: %d\n", info.ssi_signo);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100148 switch (info.ssi_signo) {
149 case SIGINT:
150 case SIGTERM:
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000151 MSG_INFO("Caught Signal - Exiting...\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100152 context->terminate = true;
153 break;
154 case SIGHUP:
Andrew Jefferyf69760d2019-03-14 16:54:13 +1030155 rc = protocol_reset(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100156 if (rc < 0) {
Andrew Jefferyf69760d2019-03-14 16:54:13 +1030157 MSG_ERR("Failed to reset on SIGHUP\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100158 }
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 Singh28519592017-04-27 14:48:58 +1000167 while ((rc = sd_bus_process(context->bus, NULL)) > 0) {
168 MSG_DBG("DBUS Event\n");
169 }
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100170 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 }
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100178 }
179
Andrew Jefferyf69760d2019-03-14 16:54:13 +1030180 rc = protocol_reset(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100181 if (rc < 0) {
Andrew Jefferyf69760d2019-03-14 16:54:13 +1030182 MSG_ERR("Failed to reset during poll loop cleanup\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100183 }
184
185 return rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100186}
187
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100188static int init_signals(struct mbox_context *context, sigset_t *set)
Cyril Burc85e34d2016-11-15 11:50:41 +1100189{
190 int rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100191
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100192 /* Block SIGHUPs, SIGTERMs and SIGINTs */
193 sigemptyset(set);
194 sigaddset(set, SIGHUP);
195 sigaddset(set, SIGINT);
196 sigaddset(set, SIGTERM);
197 rc = sigprocmask(SIG_BLOCK, set, NULL);
198 if (rc < 0) {
199 MSG_ERR("Failed to set SIG_BLOCK mask %s\n", strerror(errno));
200 return rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100201 }
202
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100203 /* Get Signal File Descriptor */
204 rc = signalfd(-1, set, SFD_NONBLOCK);
205 if (rc < 0) {
206 MSG_ERR("Failed to get signalfd %s\n", strerror(errno));
207 return rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100208 }
209
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100210 context->fds[SIG_FD].fd = rc;
Cyril Burc85e34d2016-11-15 11:50:41 +1100211 return 0;
212}
213
Cyril Burc85e34d2016-11-15 11:50:41 +1100214static void usage(const char *name)
215{
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100216 printf(USAGE, name);
Cyril Burc85e34d2016-11-15 11:50:41 +1100217}
218
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100219static bool parse_cmdline(int argc, char **argv,
220 struct mbox_context *context)
Cyril Burc85e34d2016-11-15 11:50:41 +1100221{
Cyril Bure8f2de12017-01-17 16:56:02 +1100222 char *endptr;
Suraj Jitindar Singhc29172e2017-04-12 14:26:47 +1000223 int opt;
Cyril Burc85e34d2016-11-15 11:50:41 +1100224
225 static const struct option long_options[] = {
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100226 { "flash", required_argument, 0, 'f' },
Evan Lojewskia0429782019-03-13 15:25:44 +1030227 { "backend", required_argument, 0, 'b' },
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100228 { "window-size", optional_argument, 0, 'w' },
229 { "window-num", optional_argument, 0, 'n' },
230 { "verbose", no_argument, 0, 'v' },
231 { "syslog", no_argument, 0, 's' },
Stewart Smithef0c8362018-11-19 13:49:46 +1100232 { "trace", optional_argument, 0, 't' },
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100233 { "version", no_argument, 0, 'V' },
234 { "help", no_argument, 0, 'h' },
235 { 0, 0, 0, 0 }
Cyril Burc85e34d2016-11-15 11:50:41 +1100236 };
237
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100238 verbosity = MBOX_LOG_NONE;
Cyril Burc85e34d2016-11-15 11:50:41 +1100239 mbox_vlog = &mbox_log_console;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100240
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100241 context->current = NULL; /* No current window */
242
Stewart Smithef0c8362018-11-19 13:49:46 +1100243 while ((opt = getopt_long(argc, argv, "f:b:w::n::vst::Vh", long_options, NULL))
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100244 != -1) {
Cyril Burc85e34d2016-11-15 11:50:41 +1100245 switch (opt) {
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100246 case 0:
247 break;
248 case 'f':
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030249 context->backend.flash_size = strtol(optarg, &endptr, 10);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100250 if (optarg == endptr) {
251 fprintf(stderr, "Unparseable flash size\n");
252 return false;
253 }
254 switch (*endptr) {
255 case '\0':
Cyril Burc85e34d2016-11-15 11:50:41 +1100256 break;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100257 case 'M':
Patrick Williams68a24c92023-07-25 12:02:16 -0500258 context->backend.flash_size <<= 20;
259 break;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100260 case 'K':
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030261 context->backend.flash_size <<= 10;
Cyril Burc85e34d2016-11-15 11:50:41 +1100262 break;
263 default:
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100264 fprintf(stderr, "Unknown units '%c'\n",
265 *endptr);
266 return false;
267 }
268 break;
Evan Lojewskia0429782019-03-13 15:25:44 +1030269 case 'b':
Andrew Jeffery5320f6e2019-03-15 12:40:41 +1030270 context->source = optarg;
Evan Lojewskia0429782019-03-13 15:25:44 +1030271 break;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100272 case 'n':
273 context->windows.num = strtol(argv[optind], &endptr,
274 10);
275 if (optarg == endptr || *endptr != '\0') {
276 fprintf(stderr, "Unparseable window num\n");
277 return false;
278 }
279 break;
280 case 'w':
281 context->windows.default_size = strtol(argv[optind],
282 &endptr, 10);
283 context->windows.default_size <<= 20; /* Given in MB */
284 if (optarg == endptr || (*endptr != '\0' &&
285 *endptr != 'M')) {
286 fprintf(stderr, "Unparseable window size\n");
287 return false;
288 }
Suraj Jitindar Singh0aff80c2017-04-12 14:37:24 +1000289 if (!is_power_of_2(context->windows.default_size)) {
290 fprintf(stderr, "Window size not power of 2\n");
291 return false;
292 }
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100293 break;
294 case 'v':
295 verbosity++;
296 break;
297 case 's':
298 /* Avoid a double openlog() */
299 if (mbox_vlog != &vsyslog) {
300 openlog(PREFIX, LOG_ODELAY, LOG_DAEMON);
301 mbox_vlog = &vsyslog;
302 }
303 break;
304 case 'V':
Suraj Jitindar Singh8d65bb42017-05-01 16:05:17 +1000305 printf("%s V%s\n", THIS_NAME, PACKAGE_VERSION);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100306 exit(0);
Stewart Smithef0c8362018-11-19 13:49:46 +1100307 case 't':
308 context->blktracefd = open(argv[optind],
309 O_CREAT|O_TRUNC|O_WRONLY,
310 0666);
311 printf("Recording blktrace output to %s\n",
312 argv[optind]);
313 if (context->blktracefd == -1) {
314 perror("Couldn't open blktrace file for writing");
315 exit(2);
316 }
317 break;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100318 case 'h':
319 return false; /* This will print the usage message */
320 default:
321 return false;
Cyril Burc85e34d2016-11-15 11:50:41 +1100322 }
323 }
324
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030325 if (!context->backend.flash_size) {
Cyril Bure8f2de12017-01-17 16:56:02 +1100326 fprintf(stderr, "Must specify a non-zero flash size\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100327 return false;
Cyril Bure8f2de12017-01-17 16:56:02 +1100328 }
329
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030330 MSG_INFO("Flash size: 0x%.8x\n", context->backend.flash_size);
Cyril Burc85e34d2016-11-15 11:50:41 +1100331
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100332 if (verbosity) {
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000333 MSG_INFO("%s logging\n", verbosity == MBOX_LOG_DEBUG ? "Debug" :
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100334 "Verbose");
Cyril Burc85e34d2016-11-15 11:50:41 +1100335 }
336
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100337 return true;
Cyril Burc85e34d2016-11-15 11:50:41 +1100338}
339
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030340static int mboxd_backend_init(struct mbox_context *context)
341{
Andrew Jeffery5320f6e2019-03-15 12:40:41 +1030342 const char *delim;
343 const char *path;
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030344 int rc;
345
Andrew Jeffery5320f6e2019-03-15 12:40:41 +1030346 if (!context->source) {
347 struct vpnor_partition_paths paths;
348 vpnor_default_paths(&paths);
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030349
Andrew Jeffery5320f6e2019-03-15 12:40:41 +1030350 rc = backend_probe_vpnor(&context->backend, &paths);
351 if(rc < 0)
352 rc = backend_probe_mtd(&context->backend, NULL);
353
354 return rc;
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030355 }
356
Andrew Jeffery5320f6e2019-03-15 12:40:41 +1030357 delim = strchr(context->source, ':');
358 path = delim ? delim + 1 : NULL;
359
360 if (!strncmp(context->source, "vpnor", strlen("vpnor"))) {
361 struct vpnor_partition_paths paths;
362
363 if (path) {
364 rc = -EINVAL;
365 } else {
366 vpnor_default_paths(&paths);
367 rc = backend_probe_vpnor(&context->backend, &paths);
368 }
369 } else if (!strncmp(context->source, "mtd", strlen("mtd"))) {
370 rc = backend_probe_mtd(&context->backend, path);
371 } else if (!strncmp(context->source, "file", strlen("file"))) {
372 rc = backend_probe_file(&context->backend, path);
373 } else {
374 rc = -EINVAL;
375 }
376
377 if (rc < 0)
378 MSG_ERR("Invalid backend argument: %s\n", context->source);
379
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030380 return rc;
381}
382
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100383int main(int argc, char **argv)
384{
Joel Stanleydfbeae22020-03-12 16:11:10 +1030385 const struct transport_ops *dbus_ops;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100386 struct mbox_context *context;
387 char *name = argv[0];
388 sigset_t set;
389 int rc, i;
390
391 context = calloc(1, sizeof(*context));
392 if (!context) {
393 fprintf(stderr, "Memory allocation failed\n");
394 exit(1);
395 }
396
397 if (!parse_cmdline(argc, argv, context)) {
398 usage(name);
399 free(context);
400 exit(0);
401 }
402
403 for (i = 0; i < TOTAL_FDS; i++) {
404 context->fds[i].fd = -1;
405 }
406
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000407 MSG_INFO("Starting Daemon\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100408
409 rc = init_signals(context, &set);
410 if (rc) {
Andrew Jeffery4ef0c132019-03-19 15:14:13 +1030411 goto cleanup_context;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100412 }
413
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030414 rc = mboxd_backend_init(context);
415 if (rc) {
Andrew Jeffery4ef0c132019-03-19 15:14:13 +1030416 goto cleanup_context;
Evan Lojewskif1e547c2019-03-14 14:34:33 +1030417 }
418
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930419 rc = protocol_init(context);
420 if (rc) {
Andrew Jeffery4ef0c132019-03-19 15:14:13 +1030421 goto cleanup_backend;
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930422 }
423
Andrew Jefferycb9b2102018-08-08 16:31:07 +0930424 rc = lpc_dev_init(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100425 if (rc) {
Joel Stanleydfbeae22020-03-12 16:11:10 +1030426 goto cleanup_protocol;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100427 }
428
429 /* We've found the reserved memory region -> we can assign to windows */
Andrew Jefferyc1a67fa2018-08-08 17:07:38 +0930430 rc = windows_init(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100431 if (rc) {
Andrew Jeffery4ef0c132019-03-19 15:14:13 +1030432 goto cleanup_lpc;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100433 }
434
Andrew Jefferyfe0c9e82018-11-01 14:02:17 +1030435 rc = dbus_init(context, &dbus_ops);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100436 if (rc) {
Andrew Jeffery4ef0c132019-03-19 15:14:13 +1030437 goto cleanup_windows;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100438 }
439
Deepak Kodihalli017e45c2017-07-12 01:06:30 -0500440 /* Set the LPC bus mapping */
Andrew Jefferyf69760d2019-03-14 16:54:13 +1030441 __protocol_reset(context);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100442
Andrew Jefferyfe0c9e82018-11-01 14:02:17 +1030443 /* We're ready to go, alert the host */
444 context->bmc_events |= BMC_EVENT_DAEMON_READY;
Andrew Jeffery4c15bb12018-12-05 14:15:28 +1030445 context->bmc_events |= BMC_EVENT_PROTOCOL_RESET;
Andrew Jefferyfe0c9e82018-11-01 14:02:17 +1030446
Andrew Jeffery4b8203d2019-05-06 14:36:16 +0930447 /* Alert on all supported transports, as required */
Andrew Jefferyfe0c9e82018-11-01 14:02:17 +1030448 rc = protocol_events_put(context, dbus_ops);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100449 if (rc) {
Andrew Jeffery4ef0c132019-03-19 15:14:13 +1030450 goto cleanup;
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100451 }
452
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000453 MSG_INFO("Entering Polling Loop\n");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100454 rc = poll_loop(context);
455
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000456 MSG_INFO("Exiting Poll Loop: %d\n", rc);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100457
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000458 MSG_INFO("Daemon Exiting...\n");
Andrew Jefferyfe0c9e82018-11-01 14:02:17 +1030459 context->bmc_events &= ~BMC_EVENT_DAEMON_READY;
Andrew Jefferyfab672b2018-11-01 17:12:09 +1030460 context->bmc_events |= BMC_EVENT_PROTOCOL_RESET;
Andrew Jefferyfe0c9e82018-11-01 14:02:17 +1030461
Andrew Jeffery4b8203d2019-05-06 14:36:16 +0930462 /* Alert on all supported transports, as required */
Andrew Jefferyfe0c9e82018-11-01 14:02:17 +1030463 protocol_events_put(context, dbus_ops);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100464
Andrew Jeffery4ef0c132019-03-19 15:14:13 +1030465cleanup:
Andrew Jefferyef9e62d2018-08-08 15:48:27 +0930466 dbus_free(context);
Andrew Jeffery4ef0c132019-03-19 15:14:13 +1030467cleanup_windows:
Andrew Jefferyf5f51422018-08-08 17:08:33 +0930468 windows_free(context);
Andrew Jeffery4ef0c132019-03-19 15:14:13 +1030469cleanup_lpc:
470 lpc_dev_free(context);
Joel Stanleydfbeae22020-03-12 16:11:10 +1030471cleanup_protocol:
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930472 protocol_free(context);
Andrew Jeffery4ef0c132019-03-19 15:14:13 +1030473cleanup_backend:
474 backend_free(&context->backend);
475cleanup_context:
Stewart Smithef0c8362018-11-19 13:49:46 +1100476 if (context->blktracefd)
477 close(context->blktracefd);
478
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100479 free(context);
480
481 return rc;
482}