build: install three targets to handle bmc updates

Transition from three service files to three targets installed by the
BMC library:
 - phosphor-ipmi-flash-bmc-prepare.target
 - phosphor-ipmi-flash-bmc-verify.target
 - phosphor-ipmi-flash-bmc-update.target

The prepare target is started each time the firmware update state
machine changes from notYetStarted to uploadInProgress and can be
leveraged to free memory (flush caches) or anything else required.

The verify target is started when the verify blob is committed.  The
implementation of services within this target are such that they are
expected to output a status value that's readable (typically via a
file).

The update target is started when the update blob is committed.

Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I1e838de954a3cf8956ff4b72d090e253b832efe7
diff --git a/.gitignore b/.gitignore
index dd057dd..2b70727 100644
--- a/.gitignore
+++ b/.gitignore
@@ -48,6 +48,7 @@
 test-suite.log
 
 # Custom generated files
+*.target
 
 # Output binaries
 burn_my_bmc
diff --git a/README.md b/README.md
index 9620e2b..b9c5e06 100644
--- a/README.md
+++ b/README.md
@@ -180,10 +180,10 @@
 `STATIC_HANDLER_STAGED_NAME` | `/run/initramfs/bmc-image` | The filename where to write the staged firmware image for static updates.
 `TARBALL_STAGED_NAME`        | `/tmp/image-update.tar`    | The filename where to write the UBI update tarball.
 `HASH_FILENAME`              | `/tmp/bmc.sig`             | The file to use for the hash provided.
-`PREPARATION_DBUS_SERVICE`   | `prepare_update.service`   | The systemd service started when the host starts to send an update.
+`PREPARATION_DBUS_SERVICE`   | `phosphor-ipmi-flash-bmc-prepare.target` | The systemd service started when the host starts to send an update.
 `VERIFY_STATUS_FILENAME`     | `/tmp/bmc.verify`          | The file checked for the verification status.
-`VERIFY_DBUS_SERVICE`        | `verify_image.service`     | The systemd service started for verification.
-`UPDATE_DBUS_SERVICE`        | `update_bmc.service`       | The systemd service started for updating the BMC.
+`VERIFY_DBUS_SERVICE`        | `phosphor-ipmi-flash-bmc-verify.target`  | The systemd service started for verification.
+`UPDATE_DBUS_SERVICE`        | `phosphor-ipmi-flash-bmc-update.target`  | The systemd service started for updating the BMC.
 
 ## Flash State Machine Details
 
diff --git a/bmc/Makefile.am b/bmc/Makefile.am
index 7e03384..6378ffb 100644
--- a/bmc/Makefile.am
+++ b/bmc/Makefile.am
@@ -1,5 +1,12 @@
 AM_DEFAULT_SOURCE_EXT = .cpp
 
+if HAVE_SYSTEMD
+systemdsystemunit_DATA = \
+	phosphor-ipmi-flash-bmc-prepare.target \
+	phosphor-ipmi-flash-bmc-verify.target \
+	phosphor-ipmi-flash-bmc-update.target
+endif
+
 noinst_LTLIBRARIES = libfirmwareblob_common.la
 libfirmwareblob_common_la_SOURCES = \
 	firmware_handler.cpp \
diff --git a/bmc/phosphor-ipmi-flash-bmc-prepare.target.in b/bmc/phosphor-ipmi-flash-bmc-prepare.target.in
new file mode 100644
index 0000000..7f0cbd9
--- /dev/null
+++ b/bmc/phosphor-ipmi-flash-bmc-prepare.target.in
@@ -0,0 +1,2 @@
+[Unit]
+Description=Phosphor-ipmi-flash Prepare BMC to receive update
diff --git a/bmc/phosphor-ipmi-flash-bmc-update.target.in b/bmc/phosphor-ipmi-flash-bmc-update.target.in
new file mode 100644
index 0000000..a7335b0
--- /dev/null
+++ b/bmc/phosphor-ipmi-flash-bmc-update.target.in
@@ -0,0 +1,2 @@
+[Unit]
+Description=Phosphor-ipmi-flash update the BMC image
diff --git a/bmc/phosphor-ipmi-flash-bmc-verify.target.in b/bmc/phosphor-ipmi-flash-bmc-verify.target.in
new file mode 100644
index 0000000..d45da94
--- /dev/null
+++ b/bmc/phosphor-ipmi-flash-bmc-verify.target.in
@@ -0,0 +1,2 @@
+[Unit]
+Description=Phosphor-ipmi-flash verify the image contents
diff --git a/configure.ac b/configure.ac
index 2334814..e6a1a8b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -44,7 +44,7 @@
 AX_APPEND_COMPILE_FLAGS([-Wall -Werror], [CXXFLAGS])
 
 # Checks for library functions.
-LT_INIT # Required for systemd linking
+LT_INIT
 
 # Enable building the host tool (default: yes)
 AC_ARG_ENABLE([build-host-tool],
@@ -60,6 +60,8 @@
 
 # Build the BMC by default, so check if set to no.
 AS_IF([test "x$enable_build_bmc_blob_handler" != "xno"], [
+    PKG_CHECK_MODULES([SYSTEMD], [libsystemd >= 221])
+
     PKG_CHECK_MODULES(
         [SDBUSPLUS],
         [sdbusplus],
@@ -74,6 +76,27 @@
         [],
         [AC_MSG_ERROR(["phosphor-ipmi-blobs required and not found."])]
     )
+
+    PKG_PROG_PKG_CONFIG
+    AC_ARG_WITH([systemdsystemunitdir],
+        [AS_HELP_STRING([--with-systemdsystemunitdir=DIR], [Directory for systemd service files])],
+        [],
+        [with_systemdsystemunitdir=auto]
+    )
+    AS_IF([test "x$with_systemdsystemunitdir" = "xyes" -o "x$with_systemdsystemunitdir" = "xauto"],
+        [def_systemdsystemunitdir=$($PKG_CONFIG --variable=systemdsystemunitdir systemd)
+         AS_IF([test "x$def_systemdsystemunitdir" = "x"],
+               [AS_IF([test "x$with_systemdsystemunitdir" = "xyes"],
+                    [AC_MSG_ERROR([systemd support requested but pkg-config unable to query systemd package])]
+               )
+               with_systemdsystemunitdir=no],
+               [with_systemdsystemunitdir="$def_systemdsystemunitdir"]
+         )]
+    )
+    AS_IF([test "x$with_systemdsystemunitdir" != "xno"],
+        [AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir])]
+    )
+    AM_CONDITIONAL([HAVE_SYSTEMD], [test "x$with_systemdsystemunitdir" != "xno"])
 ])
 
 # If not building the host-tool, we're building the BMC.
@@ -246,16 +269,16 @@
 
 AC_ARG_VAR(
     PREPARATION_DBUS_SERVICE,
-    [The systemd service started when the host starts to send an update.]
+    [The systemd target started when the host starts to send an update.]
 )
 AS_IF(
     [test "x$PREPARATION_DBUS_SERVICE" == "x"],
-    [PREPARATION_DBUS_SERVICE="prepare_update.service"]
+    [PREPARATION_DBUS_SERVICE="phosphor-ipmi-flash-bmc-prepare.target"]
 )
 AC_DEFINE_UNQUOTED(
     [PREPARATION_DBUS_SERVICE],
     ["$PREPARATION_DBUS_SERVICE"],
-    [The systemd service started when the host starts to send an update.]
+    [The systemd target started when the host starts to send an update.]
 )
 
 AC_ARG_VAR(
@@ -274,30 +297,30 @@
 
 AC_ARG_VAR(
     VERIFY_DBUS_SERVICE,
-    [The systemd service started for verification.]
+    [The systemd target started for verification.]
 )
 AS_IF(
     [test "x$VERIFY_DBUS_SERVICE" == "x"],
-    [VERIFY_DBUS_SERVICE="verify_image.service"]
+    [VERIFY_DBUS_SERVICE="phosphor-ipmi-flash-bmc-verify.target"]
 )
 AC_DEFINE_UNQUOTED(
     [VERIFY_DBUS_SERVICE],
     ["$VERIFY_DBUS_SERVICE"],
-    [The systemd service started for verification.]
+    [The systemd target started for verification.]
 )
 
 AC_ARG_VAR(
     UPDATE_DBUS_SERVICE,
-    [The systemd service started for updating the BMC.]
+    [The systemd target started for updating the BMC.]
 )
 AS_IF(
     [test "x$UPDATE_DBUS_SERVICE" == "x"],
-    [UPDATE_DBUS_SERVICE="update_bmc.service"]
+    [UPDATE_DBUS_SERVICE="phosphor-ipmi-flash-bmc-update.target"]
 )
 AC_DEFINE_UNQUOTED(
     [UPDATE_DBUS_SERVICE],
     ["$UPDATE_DBUS_SERVICE"],
-    [The systemd service started for updating the BMC.]
+    [The systemd target started for updating the BMC.]
 )
 
 AC_CHECK_HEADER(linux/ipmi.h, [HAVE_LINUX_IPMI_H=""], [HAVE_LINUX_IPMI_H="-I linux/ipmi.h"])
@@ -432,4 +455,7 @@
 AC_CONFIG_FILES([bmc/Makefile bmc/test/Makefile])
 AC_CONFIG_FILES([tools/Makefile tools/test/Makefile])
 AC_CONFIG_FILES([cleanup/Makefile cleanup/test/Makefile])
+AC_CONFIG_FILES([bmc/phosphor-ipmi-flash-bmc-prepare.target])
+AC_CONFIG_FILES([bmc/phosphor-ipmi-flash-bmc-verify.target])
+AC_CONFIG_FILES([bmc/phosphor-ipmi-flash-bmc-update.target])
 AC_OUTPUT