mboxd: Don't require mbox

With the DBus interface to the daemon in place it's possible to
implement any hardware interface in terms of the DBus interface. This is
the strategy used to support the IPMI HIOMAP transport. Further, the use
of MBOX is deprecated due to security concerns. We want to drop the
driver from the kernel, which failed to get traction upstream, so make
support optional.

As a consequence, switch the default transport to DBus.

Change-Id: I9f16ca053ce48943dce59b83ca991ec5494580d8
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/mboxd.c b/mboxd.c
index 637f416..c7f7d33 100644
--- a/mboxd.c
+++ b/mboxd.c
@@ -391,6 +391,7 @@
 {
 	const struct transport_ops *mbox_ops, *dbus_ops;
 	struct mbox_context *context;
+	bool have_transport_mbox;
 	char *name = argv[0];
 	sigset_t set;
 	int rc, i;
@@ -429,8 +430,13 @@
 	}
 
 	rc = transport_mbox_init(context, &mbox_ops);
-	if (rc) {
-		goto cleanup_protocol;
+	/* TODO: Think about whether we could use a less branch-y strategy */
+	have_transport_mbox = rc == 0;
+	if (!have_transport_mbox) {
+		/* Disable MBOX for poll()ing purposes */
+		context->fds[MBOX_FD].fd = -1;
+		MSG_DBG("Failed to initialise MBOX transport: %d\n", rc);
+		MSG_INFO("MBOX transport unavailable\n");
 	}
 
 	rc = lpc_dev_init(context);
@@ -456,10 +462,12 @@
 	context->bmc_events |= BMC_EVENT_DAEMON_READY;
 	context->bmc_events |= BMC_EVENT_PROTOCOL_RESET;
 
-	/* Alert on all supported transports */
-	rc = protocol_events_put(context, mbox_ops);
-	if (rc) {
-		goto cleanup;
+	/* Alert on all supported transports, as required */
+	if (have_transport_mbox) {
+		rc = protocol_events_put(context, mbox_ops);
+		if (rc) {
+			goto cleanup;
+		}
 	}
 
 	rc = protocol_events_put(context, dbus_ops);
@@ -476,8 +484,11 @@
 	context->bmc_events &= ~BMC_EVENT_DAEMON_READY;
 	context->bmc_events |= BMC_EVENT_PROTOCOL_RESET;
 
-	/* Alert on all supported transports */
-	protocol_events_put(context, mbox_ops);
+	/* Alert on all supported transports, as required */
+	if (have_transport_mbox) {
+		protocol_events_put(context, mbox_ops);
+	}
+
 	protocol_events_put(context, dbus_ops);
 
 cleanup:
@@ -487,8 +498,9 @@
 cleanup_lpc:
 	lpc_dev_free(context);
 cleanup_mbox:
-	transport_mbox_free(context);
-cleanup_protocol:
+	if (have_transport_mbox) {
+		transport_mbox_free(context);
+	}
 	protocol_free(context);
 cleanup_backend:
 	backend_free(&context->backend);
diff --git a/test/mbox.c b/test/mbox.c
index 55af00c..c224b84 100644
--- a/test/mbox.c
+++ b/test/mbox.c
@@ -211,11 +211,13 @@
 	tmpf_destroy(&test.lpc);
 }
 
-int __transport_mbox_init(struct mbox_context *context, const char *path);
+int __transport_mbox_init(struct mbox_context *context, const char *path,
+			  const struct transport_ops **ops);
 int __lpc_dev_init(struct mbox_context *context, const char *path);
 
 struct mbox_context *mbox_create_frontend_context(int n_windows, size_t len)
 {
+	const struct transport_ops *ops;
 	struct mtd_info_user mtd_info;
 	int rc;
 
@@ -242,7 +244,8 @@
 	 * /dev/null and replace it with our own fd for mbox device emulation
 	 * by the test framework.
 	 */
-	__transport_mbox_init(&test.context, "/dev/null");
+	__transport_mbox_init(&test.context, "/dev/null", &ops);
+	test.context.transport = ops;
 	rc = close(test.context.fds[MBOX_FD].fd);
 	assert(rc == 0);
 	test.context.fds[MBOX_FD].fd = test.mbox.fd;
diff --git a/transport_dbus.c b/transport_dbus.c
index b6eb0e1..18cd0d0 100644
--- a/transport_dbus.c
+++ b/transport_dbus.c
@@ -526,6 +526,8 @@
 		return rc;
 	}
 
+	context->transport = &transport_dbus_ops;
+
 	if (ops) {
 		*ops = &transport_dbus_ops;
 	}
diff --git a/transport_mbox.c b/transport_mbox.c
index 3d71820..2759fa5 100644
--- a/transport_mbox.c
+++ b/transport_mbox.c
@@ -646,16 +646,15 @@
 	return handle_mbox_req(context, &req);
 }
 
-int __transport_mbox_init(struct mbox_context *context, const char *path)
+int __transport_mbox_init(struct mbox_context *context, const char *path,
+			  const struct transport_ops **ops)
 {
 	int fd;
 
-	context->transport = &transport_mbox_ops;
-
 	/* Open MBOX Device */
 	fd = open(path, O_RDWR | O_NONBLOCK);
 	if (fd < 0) {
-		MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
+		MSG_INFO("Couldn't open %s with flags O_RDWR: %s\n",
 			path, strerror(errno));
 		return -errno;
 	}
@@ -663,6 +662,10 @@
 
 	context->fds[MBOX_FD].fd = fd;
 
+	if (ops) {
+		*ops = &transport_mbox_ops;
+	}
+
 	return 0;
 }
 
@@ -671,14 +674,10 @@
 {
 	int rc;
 
-	rc = __transport_mbox_init(context, MBOX_HOST_PATH);
+	rc = __transport_mbox_init(context, MBOX_HOST_PATH, ops);
 	if (rc)
 		return rc;
 
-	if (ops) {
-		*ops = &transport_mbox_ops;
-	}
-
 	return 0;
 }