tools: blob: add exceptions
Add exceptions on failures where it's a clean failure path.
Change-Id: Iaa8b6c7a0914367866092a7e31899453183fd7b2
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/tools/blob_handler.cpp b/tools/blob_handler.cpp
index 43fc891..62bbb4d 100644
--- a/tools/blob_handler.cpp
+++ b/tools/blob_handler.cpp
@@ -56,8 +56,7 @@
}
catch (const IpmiException& e)
{
- std::fprintf(stderr, "Received exception: %s\n", e.what());
- return {};
+ throw BlobException(e.what());
}
/* IPMI_CC was OK, and it returned no bytes, so let's be happy with that for
@@ -75,16 +74,14 @@
*/
if (reply.size() < headerSize)
{
- std::fprintf(stderr, "Invalid response length\n");
- return {};
+ throw BlobException("Invalid response length");
}
/* Verify the OEN. */
if (std::memcmp(ipmiPhosphorOen.data(), reply.data(),
ipmiPhosphorOen.size()) != 0)
{
- std::fprintf(stderr, "Invalid OEN received\n");
- return {};
+ throw BlobException("Invalid OEN received");
}
/* Validate CRC. */
@@ -106,7 +103,7 @@
{
std::fprintf(stderr, "Invalid CRC, received: 0x%x, computed: 0x%x\n",
crc, computed);
- return {};
+ throw BlobException("Invalid CRC on received data.");
}
return bytes;
@@ -115,14 +112,23 @@
int BlobHandler::getBlobCount()
{
std::uint32_t count;
- auto resp = sendIpmiPayload(BlobOEMCommands::bmcBlobGetCount, {});
- if (resp.size() != sizeof(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;
}
- /* LE to LE (need to make this portable as some point. */
- std::memcpy(&count, resp.data(), sizeof(count));
+ std::fprintf(stderr, "BLOB Count: %d\n", count);
return count;
}
@@ -130,11 +136,19 @@
{
std::vector<std::uint8_t> payload;
std::uint8_t* data = reinterpret_cast<std::uint8_t*>(&index);
+
std::copy(data, data + sizeof(std::uint32_t), std::back_inserter(payload));
- auto resp = sendIpmiPayload(BlobOEMCommands::bmcBlobEnumerate, payload);
- return (resp.size() > 0) ? std::string(&resp[0], &resp[resp.size() - 1])
- : "";
+ try
+ {
+ auto resp = sendIpmiPayload(BlobOEMCommands::bmcBlobEnumerate, payload);
+ return (resp.size() > 0) ? std::string(&resp[0], &resp[resp.size() - 1])
+ : "";
+ }
+ catch (const BlobException& b)
+ {
+ return "";
+ }
}
std::vector<std::string> BlobHandler::getBlobList()
@@ -158,11 +172,20 @@
StatResponse BlobHandler::getStat(const std::string& id)
{
StatResponse meta;
- std::vector<std::uint8_t> name;
+ 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. */
- auto resp = sendIpmiPayload(BlobOEMCommands::bmcBlobStat, name);
+ 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);
@@ -181,16 +204,25 @@
blobs::FirmwareBlobHandler::UpdateFlags handlerFlags)
{
std::uint16_t session;
- std::vector<std::uint8_t> request;
+ std::vector<std::uint8_t> request, resp;
std::uint16_t flags =
blobs::FirmwareBlobHandler::UpdateFlags::openWrite | handlerFlags;
auto addrFlags = reinterpret_cast<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. */
- auto resp = sendIpmiPayload(BlobOEMCommands::bmcBlobOpen, request);
+ try
+ {
+ resp = sendIpmiPayload(BlobOEMCommands::bmcBlobOpen, request);
+ }
+ catch (const BlobException& b)
+ {
+ throw;
+ }
+
if (resp.size() != sizeof(session))
{
throw BlobException("Did not receive session.");