use ipmiblob library from ipmi-blob-tool

Drop all code that is now handled by the ipmiblob library provided by
the new ipmi-blob-tool.  This is a library that can be included on the
BMC if necessary, but relies on nothing that is strictly meant for the
BMC.

Change-Id: Iadbf0bd89c58cafc436fba05ea43e21c49e2b669
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/tools/Makefile.am b/tools/Makefile.am
index 11ec485..d8a593d 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -5,14 +5,11 @@
 burn_my_bmc_CXXFLAGS = -I$(top_srcdir) $(CODE_COVERAGE_CXXFLAGS)
 
 noinst_LTLIBRARIES = libupdater.la
-libupdater_la_LDFLAGS = -static $(CODE_COVERAGE_LIBS)
-libupdater_la_CXXFLAGS = -I$(top_srcdir) $(CODE_COVERAGE_CXXFLAGS)
+libupdater_la_LDFLAGS = -static $(CODE_COVERAGE_LIBS) $(IPMIBLOB_LIBS)
+libupdater_la_CXXFLAGS = -I$(top_srcdir) $(CODE_COVERAGE_CXXFLAGS) $(IPMIBLOB_CFLAGS)
 libupdater_la_SOURCES = \
 	updater.cpp \
 	bt.cpp \
 	lpc.cpp \
-	blob_handler.cpp \
-	ipmi_handler.cpp \
-	crc.cpp \
 	io.cpp \
 	../internal/sys.cpp
diff --git a/tools/blob_errors.hpp b/tools/blob_errors.hpp
deleted file mode 100644
index 45f0e46..0000000
--- a/tools/blob_errors.hpp
+++ /dev/null
@@ -1,23 +0,0 @@
-#pragma once
-
-#include <exception>
-#include <string>
-
-namespace host_tool
-{
-
-class BlobException : public std::exception
-{
-  public:
-    explicit BlobException(const std::string& message) : message(message){};
-
-    virtual const char* what() const noexcept override
-    {
-        return message.c_str();
-    }
-
-  private:
-    std::string message;
-};
-
-} // namespace host_tool
diff --git a/tools/blob_handler.cpp b/tools/blob_handler.cpp
deleted file mode 100644
index 12fbaaa..0000000
--- a/tools/blob_handler.cpp
+++ /dev/null
@@ -1,318 +0,0 @@
-/*
- * Copyright 2018 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "blob_handler.hpp"
-
-#include "blob_errors.hpp"
-#include "crc.hpp"
-#include "ipmi_errors.hpp"
-
-#include <array>
-#include <cstring>
-
-namespace host_tool
-{
-
-namespace
-{
-const std::array<std::uint8_t, 3> ipmiPhosphorOen = {0xcf, 0xc2, 0x00};
-}
-
-std::vector<std::uint8_t>
-    BlobHandler::sendIpmiPayload(BlobOEMCommands command,
-                                 const std::vector<std::uint8_t>& payload)
-{
-    std::vector<std::uint8_t> request, reply, bytes;
-
-    std::copy(ipmiPhosphorOen.begin(), ipmiPhosphorOen.end(),
-              std::back_inserter(request));
-    request.push_back(command);
-
-    if (payload.size() > 0)
-    {
-        /* Grow the vector to hold the bytes. */
-        request.reserve(request.size() + sizeof(std::uint16_t));
-
-        /* CRC required. */
-        std::uint16_t crc = generateCrc(payload);
-        auto src = reinterpret_cast<const std::uint8_t*>(&crc);
-
-        std::copy(src, src + sizeof(crc), std::back_inserter(request));
-
-        /* Copy the payload. */
-        std::copy(payload.begin(), payload.end(), std::back_inserter(request));
-    }
-
-    try
-    {
-        reply = ipmi->sendPacket(request);
-    }
-    catch (const IpmiException& e)
-    {
-        throw BlobException(e.what());
-    }
-
-    /* IPMI_CC was OK, and it returned no bytes, so let's be happy with that for
-     * now.
-     */
-    if (reply.size() == 0)
-    {
-        return reply;
-    }
-
-    /* This cannot be a response because it's smaller than the smallest
-     * response.
-     */
-    if (reply.size() < ipmiPhosphorOen.size())
-    {
-        throw BlobException("Invalid response length");
-    }
-
-    /* Verify the OEN. */
-    if (std::memcmp(ipmiPhosphorOen.data(), reply.data(),
-                    ipmiPhosphorOen.size()) != 0)
-    {
-        throw BlobException("Invalid OEN received");
-    }
-
-    /* In this case there was no data, as there was no CRC. */
-    std::size_t headerSize = ipmiPhosphorOen.size() + sizeof(std::uint16_t);
-    if (reply.size() < headerSize)
-    {
-        return {};
-    }
-
-    /* Validate CRC. */
-    std::uint16_t crc;
-    auto ptr = reinterpret_cast<std::uint8_t*>(&crc);
-    std::memcpy(ptr, &reply[ipmiPhosphorOen.size()], sizeof(crc));
-
-    for (const auto& byte : reply)
-    {
-        std::fprintf(stderr, "0x%02x ", byte);
-    }
-    std::fprintf(stderr, "\n");
-
-    bytes.insert(bytes.begin(), reply.begin() + headerSize, reply.end());
-
-    auto computed = generateCrc(bytes);
-    if (crc != computed)
-    {
-        std::fprintf(stderr, "Invalid CRC, received: 0x%x, computed: 0x%x\n",
-                     crc, computed);
-        throw BlobException("Invalid CRC on received data.");
-    }
-
-    return bytes;
-}
-
-int BlobHandler::getBlobCount()
-{
-    std::uint32_t count;
-    try
-    {
-        auto resp = sendIpmiPayload(BlobOEMCommands::bmcBlobGetCount, {});
-        if (resp.size() != sizeof(count))
-        {
-            return 0;
-        }
-
-        /* LE to LE (need to make this portable as some point. */
-        std::memcpy(&count, resp.data(), sizeof(count));
-    }
-    catch (const BlobException& b)
-    {
-        return 0;
-    }
-
-    std::fprintf(stderr, "BLOB Count: %d\n", count);
-    return count;
-}
-
-std::string BlobHandler::enumerateBlob(std::uint32_t index)
-{
-    std::vector<std::uint8_t> payload;
-    auto data = reinterpret_cast<const std::uint8_t*>(&index);
-
-    std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
-
-    try
-    {
-        auto resp = sendIpmiPayload(BlobOEMCommands::bmcBlobEnumerate, payload);
-        return (resp.size() > 0) ? std::string(&resp[0], &resp[resp.size() - 1])
-                                 : "";
-    }
-    catch (const BlobException& b)
-    {
-        return "";
-    }
-}
-
-void BlobHandler::writeGeneric(BlobOEMCommands command, std::uint16_t session,
-                               std::uint32_t offset,
-                               const std::vector<std::uint8_t>& bytes)
-{
-    std::vector<std::uint8_t> payload;
-
-    payload.reserve(sizeof(std::uint16_t) + sizeof(std::uint32_t) +
-                    bytes.size());
-
-    auto data = reinterpret_cast<const std::uint8_t*>(&session);
-    std::copy(data, data + sizeof(std::uint16_t), std::back_inserter(payload));
-
-    data = reinterpret_cast<const std::uint8_t*>(&offset);
-    std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
-
-    std::copy(bytes.begin(), bytes.end(), std::back_inserter(payload));
-
-    auto resp = sendIpmiPayload(command, payload);
-}
-
-void BlobHandler::writeMeta(std::uint16_t session, std::uint32_t offset,
-                            const std::vector<std::uint8_t>& bytes)
-{
-    return writeGeneric(BlobOEMCommands::bmcBlobWriteMeta, session, offset,
-                        bytes);
-}
-
-void BlobHandler::writeBytes(std::uint16_t session, std::uint32_t offset,
-                             const std::vector<std::uint8_t>& bytes)
-{
-    return writeGeneric(BlobOEMCommands::bmcBlobWrite, session, offset, bytes);
-}
-
-std::vector<std::string> BlobHandler::getBlobList()
-{
-    std::vector<std::string> list;
-    int blobCount = getBlobCount();
-
-    for (int i = 0; i < blobCount; i++)
-    {
-        auto name = enumerateBlob(i);
-        /* Currently ignore failures. */
-        if (!name.empty())
-        {
-            list.push_back(name);
-        }
-    }
-
-    return list;
-}
-
-StatResponse BlobHandler::getStat(const std::string& id)
-{
-    StatResponse meta;
-    std::vector<std::uint8_t> name, resp;
-
-    std::copy(id.begin(), id.end(), std::back_inserter(name));
-    name.push_back(0x00); /* need to add nul-terminator. */
-
-    try
-    {
-        resp = sendIpmiPayload(BlobOEMCommands::bmcBlobStat, name);
-    }
-    catch (const BlobException& b)
-    {
-        throw;
-    }
-
-    std::memcpy(&meta.blob_state, &resp[0], sizeof(meta.blob_state));
-    std::memcpy(&meta.size, &resp[sizeof(meta.blob_state)], sizeof(meta.size));
-    int offset = sizeof(meta.blob_state) + sizeof(meta.size);
-    std::uint8_t len = resp[offset];
-    if (len > 0)
-    {
-        std::copy(&resp[offset + 1], &resp[resp.size()],
-                  std::back_inserter(meta.metadata));
-    }
-
-    return meta;
-}
-
-std::uint16_t
-    BlobHandler::openBlob(const std::string& id,
-                          blobs::FirmwareBlobHandler::UpdateFlags handlerFlags)
-{
-    std::uint16_t session;
-    std::vector<std::uint8_t> request, resp;
-    std::uint16_t flags =
-        blobs::FirmwareBlobHandler::UpdateFlags::openWrite | handlerFlags;
-    auto addrFlags = reinterpret_cast<const std::uint8_t*>(&flags);
-
-    std::copy(addrFlags, addrFlags + sizeof(flags),
-              std::back_inserter(request));
-    std::copy(id.begin(), id.end(), std::back_inserter(request));
-    request.push_back(0x00); /* need to add nul-terminator. */
-
-    try
-    {
-        resp = sendIpmiPayload(BlobOEMCommands::bmcBlobOpen, request);
-    }
-    catch (const BlobException& b)
-    {
-        throw;
-    }
-
-    if (resp.size() != sizeof(session))
-    {
-        throw BlobException("Did not receive session.");
-    }
-
-    std::memcpy(&session, resp.data(), sizeof(session));
-    return session;
-}
-
-void BlobHandler::closeBlob(std::uint16_t session)
-{
-    std::vector<std::uint8_t> request, resp;
-    auto addrSession = reinterpret_cast<const std::uint8_t*>(&session);
-    std::copy(addrSession, addrSession + sizeof(session),
-              std::back_inserter(request));
-
-    try
-    {
-        resp = sendIpmiPayload(BlobOEMCommands::bmcBlobClose, request);
-    }
-    catch (const BlobException& b)
-    {
-        std::fprintf(stderr, "Received failure on close: %s\n", b.what());
-    }
-
-    return;
-}
-
-std::vector<std::uint8_t> BlobHandler::readBytes(std::uint16_t session,
-                                                 std::uint32_t offset,
-                                                 std::uint32_t length)
-{
-    std::vector<std::uint8_t> payload;
-
-    payload.reserve(sizeof(std::uint16_t) + sizeof(std::uint32_t) +
-                    sizeof(std::uint32_t));
-
-    auto data = reinterpret_cast<const std::uint8_t*>(&session);
-    std::copy(data, data + sizeof(std::uint16_t), std::back_inserter(payload));
-
-    data = reinterpret_cast<const std::uint8_t*>(&offset);
-    std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
-
-    data = reinterpret_cast<const std::uint8_t*>(&length);
-    std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
-
-    return sendIpmiPayload(BlobOEMCommands::bmcBlobRead, payload);
-}
-
-} // namespace host_tool
diff --git a/tools/blob_handler.hpp b/tools/blob_handler.hpp
deleted file mode 100644
index 1cbc963..0000000
--- a/tools/blob_handler.hpp
+++ /dev/null
@@ -1,109 +0,0 @@
-#pragma once
-
-#include "blob_interface.hpp"
-#include "ipmi_interface.hpp"
-
-namespace host_tool
-{
-
-class BlobHandler : public BlobInterface
-{
-  public:
-    enum BlobOEMCommands
-    {
-        bmcBlobGetCount = 0,
-        bmcBlobEnumerate = 1,
-        bmcBlobOpen = 2,
-        bmcBlobRead = 3,
-        bmcBlobWrite = 4,
-        bmcBlobCommit = 5,
-        bmcBlobClose = 6,
-        bmcBlobDelete = 7,
-        bmcBlobStat = 8,
-        bmcBlobSessionStat = 9,
-        bmcBlobWriteMeta = 10,
-    };
-
-    explicit BlobHandler(IpmiInterface* ipmi) : ipmi(ipmi){};
-
-    /**
-     * Retrieve the blob count.
-     *
-     * @return the number of blob_ids found (0 on failure).
-     */
-    int getBlobCount();
-
-    /**
-     * Given an index into the list of blobs, return the name.
-     *
-     * @param[in] index - the index into the list of blob ids.
-     * @return the name as a string or empty on failure.
-     */
-    std::string enumerateBlob(std::uint32_t index);
-
-    /**
-     * @throws BlobException.
-     */
-    void writeMeta(std::uint16_t session, std::uint32_t offset,
-                   const std::vector<std::uint8_t>& bytes) override;
-
-    /**
-     * @throw BlobException.
-     */
-    void writeBytes(std::uint16_t session, std::uint32_t offset,
-                    const std::vector<std::uint8_t>& bytes) override;
-
-    std::vector<std::string> getBlobList() override;
-
-    /**
-     * @throws BlobException.
-     */
-    StatResponse getStat(const std::string& id) override;
-
-    /**
-     * @throws BlobException.
-     */
-    std::uint16_t
-        openBlob(const std::string& id,
-                 blobs::FirmwareBlobHandler::UpdateFlags handlerFlags) override;
-
-    void closeBlob(std::uint16_t session) override;
-
-    /**
-     * @throws BlobException.
-     */
-    std::vector<std::uint8_t> readBytes(std::uint16_t session,
-                                        std::uint32_t offset,
-                                        std::uint32_t length) override;
-
-  private:
-    /**
-     * Send the contents of the payload to IPMI, this method handles wrapping
-     * with the OEN, subcommand and CRC.
-     *
-     * @param[in] command - the blob command.
-     * @param[in] payload - the payload bytes.
-     * @return the bytes returned from the ipmi interface.
-     * @throws BlobException.
-     */
-    std::vector<std::uint8_t>
-        sendIpmiPayload(BlobOEMCommands command,
-                        const std::vector<std::uint8_t>& payload);
-
-    /**
-     * Generic blob byte writer.
-     *
-     * @param[in] command - the command associated with this write.
-     * @param[in] session - the session id.
-     * @param[in] offset - the offset for the metadata to write.
-     * @param[in] bytes - the bytes to send.
-     * @throws BlobException on failure.
-     */
-    void writeGeneric(BlobOEMCommands command, std::uint16_t session,
-                      std::uint32_t offset,
-                      const std::vector<std::uint8_t>& bytes);
-
-    IpmiInterface* ipmi;
-};
-
-} // namespace host_tool
diff --git a/tools/blob_interface.hpp b/tools/blob_interface.hpp
deleted file mode 100644
index 61ee5eb..0000000
--- a/tools/blob_interface.hpp
+++ /dev/null
@@ -1,94 +0,0 @@
-#pragma once
-
-#include "firmware_handler.hpp"
-
-#include <cstdint>
-#include <string>
-#include <vector>
-
-namespace host_tool
-{
-
-struct StatResponse
-{
-    std::uint16_t blob_state;
-    std::uint32_t size;
-    std::vector<std::uint8_t> metadata;
-};
-
-class BlobInterface
-{
-  public:
-    virtual ~BlobInterface() = default;
-
-    /**
-     * Write metadata to a blob.
-     *
-     * @param[in] session - the session id.
-     * @param[in] offset - the offset for the metadata to write.
-     * @param[in] bytes - the bytes to send.
-     * @throws BlobException on failure.
-     */
-    virtual void writeMeta(std::uint16_t session, std::uint32_t offset,
-                           const std::vector<std::uint8_t>& bytes) = 0;
-
-    /**
-     * Write bytes to a blob.
-     *
-     * @param[in] session - the session id.
-     * @param[in] offset - the offset to which to write the bytes.
-     * @param[in] bytes - the bytes to send.
-     * @throws BlobException on failure.
-     */
-    virtual void writeBytes(std::uint16_t session, std::uint32_t offset,
-                            const std::vector<std::uint8_t>& bytes) = 0;
-
-    /**
-     * Get a list of the blob_ids provided by the BMC.
-     *
-     * @return list of strings, each representing a blob_id returned.
-     */
-    virtual std::vector<std::string> getBlobList() = 0;
-
-    /**
-     * Get the stat() on the blob_id.
-     *
-     * @param[in] id - the blob_id.
-     * @return metadata structure.
-     */
-    virtual StatResponse getStat(const std::string& id) = 0;
-
-    /**
-     * Attempt to open the file using the specific data interface flag.
-     *
-     * @param[in] blob - the blob_id to open.
-     * @param[in] handlerFlags - the data interface flag, if relevant.
-     * @return the session id on success.
-     * @throws BlobException on failure.
-     */
-    virtual std::uint16_t
-        openBlob(const std::string& id,
-                 blobs::FirmwareBlobHandler::UpdateFlags handlerFlags) = 0;
-
-    /**
-     * Attempt to close the open session.
-     *
-     * @param[in] session - the session to close.
-     */
-    virtual void closeBlob(std::uint16_t session) = 0;
-
-    /**
-     * Read bytes from a blob.
-     *
-     * @param[in] session - the session id.
-     * @param[in] offset - the offset to which to write the bytes.
-     * @param[in] length - the number of bytes to read.
-     * @return the bytes read
-     * @throws BlobException on failure.
-     */
-    virtual std::vector<std::uint8_t> readBytes(std::uint16_t session,
-                                                std::uint32_t offset,
-                                                std::uint32_t length) = 0;
-};
-
-} // namespace host_tool
diff --git a/tools/bt.cpp b/tools/bt.cpp
index 297bc30..c49aecf 100644
--- a/tools/bt.cpp
+++ b/tools/bt.cpp
@@ -1,8 +1,7 @@
 #include "bt.hpp"
 
-#include "blob_errors.hpp"
-
 #include <cstdint>
+#include <ipmiblob/blob_errors.hpp>
 #include <vector>
 
 namespace host_tool
@@ -37,7 +36,7 @@
             }
         } while (bytesRead > 0);
     }
-    catch (const BlobException& b)
+    catch (const ipmiblob::BlobException& b)
     {
         sys->close(inputFd);
         return false;
diff --git a/tools/bt.hpp b/tools/bt.hpp
index 0b0187f..811c81d 100644
--- a/tools/bt.hpp
+++ b/tools/bt.hpp
@@ -1,16 +1,17 @@
 #pragma once
 
-#include "blob_interface.hpp"
 #include "interface.hpp"
 #include "internal/sys.hpp"
 
+#include <ipmiblob/blob_interface.hpp>
+
 namespace host_tool
 {
 
 class BtDataHandler : public DataInterface
 {
   public:
-    BtDataHandler(BlobInterface* blob,
+    BtDataHandler(ipmiblob::BlobInterface* blob,
                   const internal::Sys* sys = &internal::sys_impl) :
         blob(blob),
         sys(sys){};
@@ -22,7 +23,7 @@
     }
 
   private:
-    BlobInterface* blob;
+    ipmiblob::BlobInterface* blob;
     const internal::Sys* sys;
 };
 
diff --git a/tools/crc.cpp b/tools/crc.cpp
deleted file mode 100644
index d6f59ef..0000000
--- a/tools/crc.cpp
+++ /dev/null
@@ -1,44 +0,0 @@
-#include "crc.hpp"
-
-namespace host_tool
-{
-
-/*
- * This implementation tracks the specification given at
- * http://srecord.sourceforge.net/crc16-ccitt.html
- * Code copied from internal portable sources.
- */
-std::uint16_t generateCrc(const std::vector<std::uint8_t>& data)
-{
-    const std::uint16_t kPoly = 0x1021;
-    const std::uint16_t kLeftBit = 0x8000;
-    const int kExtraRounds = 2;
-    const std::uint8_t* bytes = data.data();
-    std::uint16_t crc = 0xFFFF;
-    std::size_t i;
-    std::size_t j;
-    std::size_t size = data.size();
-
-    for (i = 0; i < size + kExtraRounds; ++i)
-    {
-        for (j = 0; j < 8; ++j)
-        {
-            bool xor_flag = (crc & kLeftBit) ? 1 : 0;
-            crc <<= 1;
-            // If this isn't an extra round and the current byte's j'th bit from
-            // the left is set, increment the CRC.
-            if (i < size && (bytes[i] & (1 << (7 - j))))
-            {
-                crc++;
-            }
-            if (xor_flag)
-            {
-                crc ^= kPoly;
-            }
-        }
-    }
-
-    return crc;
-}
-
-} // namespace host_tool
diff --git a/tools/crc.hpp b/tools/crc.hpp
deleted file mode 100644
index c335ed2..0000000
--- a/tools/crc.hpp
+++ /dev/null
@@ -1,20 +0,0 @@
-#pragma once
-
-#include <cstdint>
-#include <vector>
-
-namespace host_tool
-{
-
-/**
- * Generate the CRC for a payload (really any bytes).
- *
- * This is meant to only be called on the payload and not the CRC or the OEM
- * header, etc.
- *
- * @param[in] data - the bytes against to run the CRC
- * @return the CRC value
- */
-std::uint16_t generateCrc(const std::vector<std::uint8_t>& data);
-
-} // namespace host_tool
diff --git a/tools/ipmi_errors.hpp b/tools/ipmi_errors.hpp
deleted file mode 100644
index 9f1a9f9..0000000
--- a/tools/ipmi_errors.hpp
+++ /dev/null
@@ -1,47 +0,0 @@
-#pragma once
-
-#include <exception>
-#include <map>
-#include <sstream>
-#include <string>
-
-namespace host_tool
-{
-
-class IpmiException : public std::exception
-{
-  public:
-    const std::map<int, std::string> commonFailures = {
-        {0xc0, "busy"},
-        {0xc1, "invalid"},
-        {0xc3, "timeout"},
-    };
-
-    explicit IpmiException(int cc)
-    {
-        std::ostringstream smessage;
-
-        auto search = commonFailures.find(cc);
-        if (search != commonFailures.end())
-        {
-            smessage << "Received IPMI_CC: " << search->second;
-        }
-        else
-        {
-            smessage << "Received IPMI_CC: " << cc;
-        }
-
-        message = smessage.str();
-    }
-    explicit IpmiException(const std::string& message) : message(message){};
-
-    virtual const char* what() const noexcept override
-    {
-        return message.c_str();
-    }
-
-  private:
-    std::string message;
-};
-
-} // namespace host_tool
diff --git a/tools/ipmi_handler.cpp b/tools/ipmi_handler.cpp
deleted file mode 100644
index 9278338..0000000
--- a/tools/ipmi_handler.cpp
+++ /dev/null
@@ -1,165 +0,0 @@
-/*
- * Copyright 2018 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "ipmi_handler.hpp"
-
-#include "ipmi_errors.hpp"
-
-#include <fcntl.h>
-#include <linux/ipmi.h>
-#include <linux/ipmi_msgdefs.h>
-#include <sys/ioctl.h>
-
-#include <array>
-#include <cstdint>
-#include <cstring>
-#include <sstream>
-#include <string>
-#include <vector>
-
-namespace host_tool
-{
-
-void IpmiHandler::open()
-{
-    const int device = 0;
-    const std::vector<std::string> formats = {"/dev/ipmi", "/dev/ipmi/",
-                                              "/dev/ipmidev/"};
-
-    for (const auto& format : formats)
-    {
-        std::ostringstream path;
-        path << format << device;
-
-        fd = sys->open(path.str().c_str(), O_RDWR);
-        if (fd < 0)
-        {
-            continue;
-        }
-        break;
-    }
-
-    if (fd < 0)
-    {
-        throw IpmiException("Unable to open any ipmi devices");
-    }
-}
-
-std::vector<std::uint8_t>
-    IpmiHandler::sendPacket(std::vector<std::uint8_t>& data)
-{
-    if (fd < 0)
-    {
-        open();
-    }
-
-    constexpr int ipmiOEMNetFn = 46;
-    constexpr int ipmiOEMLun = 0;
-    /* /openbmc/phosphor-host-ipmid/blob/master/host-ipmid/oemopenbmc.hpp */
-    constexpr int ipmiOEMBlobCmd = 128;
-    constexpr int fifteenMs = 15 * 1000;
-    constexpr int ipmiReadTimeout = fifteenMs;
-    constexpr int ipmiResponseBufferLen = IPMI_MAX_MSG_LENGTH;
-    constexpr int ipmiOk = 0;
-
-    /* We have a handle to the IPMI device. */
-    std::array<std::uint8_t, ipmiResponseBufferLen> responseBuffer;
-
-    /* Build address. */
-    struct ipmi_system_interface_addr systemAddress;
-    systemAddress.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
-    systemAddress.channel = IPMI_BMC_CHANNEL;
-    systemAddress.lun = ipmiOEMLun;
-
-    /* Build request. */
-    struct ipmi_req request;
-    std::memset(&request, 0, sizeof(request));
-    request.addr = reinterpret_cast<unsigned char*>(&systemAddress);
-    request.addr_len = sizeof(systemAddress);
-    request.msgid = sequence++;
-    request.msg.data = reinterpret_cast<unsigned char*>(data.data());
-    request.msg.data_len = data.size();
-    request.msg.netfn = ipmiOEMNetFn;
-    request.msg.cmd = ipmiOEMBlobCmd;
-
-    struct ipmi_recv reply;
-    reply.addr = reinterpret_cast<unsigned char*>(&systemAddress);
-    reply.addr_len = sizeof(systemAddress);
-    reply.msg.data = reinterpret_cast<unsigned char*>(responseBuffer.data());
-    reply.msg.data_len = responseBuffer.size();
-
-    /* Try to send request. */
-    int rc = sys->ioctl(fd, IPMICTL_SEND_COMMAND, &request);
-    if (rc < 0)
-    {
-        throw IpmiException("Unable to send IPMI request.");
-    }
-
-    /* Could use sdeventplus, but for only one type of event is it worth it? */
-    struct pollfd pfd;
-    pfd.fd = fd;
-    pfd.events = POLLIN;
-
-    do
-    {
-        rc = sys->poll(&pfd, 1, ipmiReadTimeout);
-        if (rc < 0)
-        {
-            if (errno == EINTR)
-            {
-                continue;
-            }
-            throw IpmiException("Error occurred.");
-        }
-        else if (rc == 0)
-        {
-            throw IpmiException("Timeout waiting for reply.");
-        }
-
-        /* Yay, happy case! */
-        rc = sys->ioctl(fd, IPMICTL_RECEIVE_MSG_TRUNC, &reply);
-        if (rc < 0)
-        {
-            throw IpmiException("Unable to read reply.");
-        }
-
-        if (request.msgid != reply.msgid)
-        {
-            std::fprintf(stderr, "Received wrong message, trying again.\n");
-        }
-    } while (request.msgid != reply.msgid);
-
-    if (responseBuffer[0] != ipmiOk)
-    {
-        throw IpmiException(static_cast<int>(responseBuffer[0]));
-    }
-
-    std::vector<std::uint8_t> returning;
-    auto dataLen = reply.msg.data_len - 1;
-
-    returning.insert(returning.begin(), responseBuffer.begin() + 1,
-                     responseBuffer.begin() + dataLen + 1);
-
-    for (const auto& byte : returning)
-    {
-        std::fprintf(stderr, "0x%02x ", byte);
-    }
-    std::fprintf(stderr, "\n");
-
-    return returning;
-}
-
-} // namespace host_tool
diff --git a/tools/ipmi_handler.hpp b/tools/ipmi_handler.hpp
deleted file mode 100644
index 1c91bff..0000000
--- a/tools/ipmi_handler.hpp
+++ /dev/null
@@ -1,46 +0,0 @@
-#pragma once
-
-#include "internal/sys.hpp"
-#include "ipmi_interface.hpp"
-
-#include <vector>
-
-namespace host_tool
-{
-
-class IpmiHandler : public IpmiInterface
-{
-  public:
-    explicit IpmiHandler(const internal::Sys* sys = &internal::sys_impl) :
-        sys(sys){};
-
-    ~IpmiHandler() = default;
-    IpmiHandler(const IpmiHandler&) = delete;
-    IpmiHandler& operator=(const IpmiHandler&) = delete;
-    IpmiHandler(IpmiHandler&&) = default;
-    IpmiHandler& operator=(IpmiHandler&&) = default;
-
-    /**
-     * Attempt to open the device node.
-     *
-     * @throws IpmiException on failure.
-     */
-    void open();
-
-    /**
-     * @throws IpmiException on failure.
-     */
-    std::vector<std::uint8_t>
-        sendPacket(std::vector<std::uint8_t>& data) override;
-
-  private:
-    const internal::Sys* sys;
-    /** TODO: Use a smart file descriptor when it's ready.  Until then only
-     * allow moving this object.
-     */
-    int fd = -1;
-    /* The last IPMI sequence number we used. */
-    int sequence = 0;
-};
-
-} // namespace host_tool
diff --git a/tools/ipmi_interface.hpp b/tools/ipmi_interface.hpp
deleted file mode 100644
index 6bad7db..0000000
--- a/tools/ipmi_interface.hpp
+++ /dev/null
@@ -1,25 +0,0 @@
-#pragma once
-
-#include <cstdint>
-#include <vector>
-
-namespace host_tool
-{
-
-class IpmiInterface
-{
-  public:
-    virtual ~IpmiInterface() = default;
-
-    /**
-     * Send an IPMI packet to the BMC.
-     *
-     * @param[in] data - a vector of the IPMI packet contents.
-     * @return the bytes returned.
-     * @throws IpmiException on failure.
-     */
-    virtual std::vector<std::uint8_t>
-        sendPacket(std::vector<std::uint8_t>& data) = 0;
-};
-
-} // namespace host_tool
diff --git a/tools/lpc.hpp b/tools/lpc.hpp
index 1ccecc2..fc8ca4b 100644
--- a/tools/lpc.hpp
+++ b/tools/lpc.hpp
@@ -1,11 +1,11 @@
 #pragma once
 
-#include "blob_interface.hpp"
 #include "interface.hpp"
 #include "internal/sys.hpp"
 #include "io.hpp"
 
 #include <cstdint>
+#include <ipmiblob/blob_interface.hpp>
 
 namespace host_tool
 {
@@ -21,7 +21,7 @@
 class LpcDataHandler : public DataInterface
 {
   public:
-    LpcDataHandler(BlobInterface* blob, HostIoInterface* io,
+    LpcDataHandler(ipmiblob::BlobInterface* blob, HostIoInterface* io,
                    const internal::Sys* sys = &internal::sys_impl) :
         blob(blob),
         io(io), sys(sys){};
@@ -33,7 +33,7 @@
     }
 
   private:
-    BlobInterface* blob;
+    ipmiblob::BlobInterface* blob;
     HostIoInterface* io;
     const internal::Sys* sys;
 };
diff --git a/tools/main.cpp b/tools/main.cpp
index ada34bb..f676f6a 100644
--- a/tools/main.cpp
+++ b/tools/main.cpp
@@ -14,10 +14,8 @@
  * limitations under the License.
  */
 
-#include "blob_handler.hpp"
 #include "bt.hpp"
 #include "io.hpp"
-#include "ipmi_handler.hpp"
 #include "lpc.hpp"
 #include "tool_errors.hpp"
 #include "updater.hpp"
@@ -28,6 +26,8 @@
 #include <algorithm>
 #include <cstdio>
 #include <iostream>
+#include <ipmiblob/blob_handler.hpp>
+#include <ipmiblob/ipmi_handler.hpp>
 #include <iterator>
 #include <memory>
 #include <string>
@@ -137,8 +137,8 @@
             exit(EXIT_FAILURE);
         }
 
-        host_tool::IpmiHandler ipmi;
-        host_tool::BlobHandler blob(&ipmi);
+        ipmiblob::IpmiHandler ipmi;
+        ipmiblob::BlobHandler blob(&ipmi);
         host_tool::DevMemDevice devmem;
 
         std::unique_ptr<host_tool::DataInterface> handler;
diff --git a/tools/updater.cpp b/tools/updater.cpp
index 2620715..d7e5f96 100644
--- a/tools/updater.cpp
+++ b/tools/updater.cpp
@@ -16,18 +16,19 @@
 
 #include "updater.hpp"
 
-#include "blob_errors.hpp"
 #include "tool_errors.hpp"
 
 #include <algorithm>
+#include <blobs-ipmid/blobs.hpp>
 #include <cstring>
+#include <ipmiblob/blob_errors.hpp>
 #include <memory>
 #include <string>
 
 namespace host_tool
 {
 
-void updaterMain(BlobInterface* blob, DataInterface* handler,
+void updaterMain(ipmiblob::BlobInterface* blob, DataInterface* handler,
                  const std::string& imagePath, const std::string& signaturePath)
 {
     /* TODO(venture): Add optional parameter to specify the flash type, default
@@ -58,12 +59,12 @@
     /* Call stat on /flash/image (or /flash/tarball) and check if data interface
      * is supported.
      */
-    StatResponse stat;
+    ipmiblob::StatResponse stat;
     try
     {
         stat = blob->getStat(goalFirmware);
     }
-    catch (const BlobException& b)
+    catch (const ipmiblob::BlobException& b)
     {
         throw ToolException("blob exception received: " +
                             std::string(b.what()));
@@ -79,9 +80,12 @@
     std::uint16_t session;
     try
     {
-        session = blob->openBlob(goalFirmware, supported);
+        session = blob->openBlob(
+            goalFirmware,
+            static_cast<std::uint16_t>(supported) |
+                static_cast<std::uint16_t>(blobs::OpenFlags::write));
     }
-    catch (const BlobException& b)
+    catch (const ipmiblob::BlobException& b)
     {
         throw ToolException("blob exception received: " +
                             std::string(b.what()));
diff --git a/tools/updater.hpp b/tools/updater.hpp
index adee496..0a66f34 100644
--- a/tools/updater.hpp
+++ b/tools/updater.hpp
@@ -1,8 +1,8 @@
 #pragma once
 
-#include "blob_interface.hpp"
 #include "interface.hpp"
 
+#include <ipmiblob/blob_interface.hpp>
 #include <string>
 
 namespace host_tool
@@ -17,7 +17,7 @@
  * @param[in] signaturePath - the path to the signature file.
  * @throws ToolException on failures.
  */
-void updaterMain(BlobInterface* blob, DataInterface* handler,
+void updaterMain(ipmiblob::BlobInterface* blob, DataInterface* handler,
                  const std::string& imagePath,
                  const std::string& signaturePath);