fix error handling for CryptHandle.get
The old code calls CryptHandle once to make sure it is valid, then
calls it again to use it.
Tested:
busctl call xyz.openbmc_project.eStoraged.mmcblk0 \
> /xyz/openbmc_project/inventory/storage/mmcblk0 \
> xyz.openbmc_project.Inventory.Item.Volume FormatLuks ays 3 1 2 3 \
> xyz.openbmc_project.Inventory.Item.Volume.FilesystemType.ext4 \
> --timeout=60
Signed-off-by: John Edward Broadbent <jebr@google.com>
Change-Id: I21c02315c365e74ead3d0b5b2578c62503376756
diff --git a/include/cryptsetupInterface.hpp b/include/cryptsetupInterface.hpp
index 49076dd..719e98b 100644
--- a/include/cryptsetupInterface.hpp
+++ b/include/cryptsetupInterface.hpp
@@ -2,11 +2,17 @@
#include <libcryptsetup.h>
+#include <phosphor-logging/lg2.hpp>
#include <stdplus/handle/managed.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
+
+#include <string>
namespace estoraged
{
+using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound;
+
/** @class CryptsetupInterface
* @brief Interface to the cryptsetup functions used to manage a LUKS device.
* @details This class is used to mock out the cryptsetup functions.
@@ -222,6 +228,14 @@
/** @brief Get a pointer to the crypt_device struct. */
struct crypt_device* get()
{
+ if (*handle == nullptr)
+ {
+ lg2::error("Failed to get crypt device handle",
+ "REDFISH_MESSAGE_ID",
+ std::string("OpenBMC.0.1.HandleGetFail"));
+ throw ResourceNotFound();
+ }
+
return *handle;
}
diff --git a/src/erase/cryptoErase.cpp b/src/erase/cryptoErase.cpp
index edc71d9..426ba01 100644
--- a/src/erase/cryptoErase.cpp
+++ b/src/erase/cryptoErase.cpp
@@ -27,12 +27,6 @@
{
/* get cryptHandle */
CryptHandle cryptHandle(std::string(devPath).c_str());
- if (cryptHandle.get() == nullptr)
- {
- lg2::error("Failed to initialize crypt device", "REDFISH_MESSAGE_ID",
- std::string("OpenBMC.0.1.EraseFailure"));
- throw ResourceNotFound();
- }
/* cryptLoad */
if (cryptIface->cryptLoad(cryptHandle.get(), CRYPT_LUKS2, nullptr) != 0)
{
diff --git a/src/estoraged.cpp b/src/estoraged.cpp
index 9646139..c1ac2fd 100644
--- a/src/estoraged.cpp
+++ b/src/estoraged.cpp
@@ -27,7 +27,6 @@
{
using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
-using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound;
using sdbusplus::xyz::openbmc_project::Common::Error::UnsupportedRequest;
using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume;
@@ -104,12 +103,6 @@
}
CryptHandle cryptHandle(devPath.c_str());
- if (cryptHandle.get() == nullptr)
- {
- lg2::error("Failed to initialize crypt device", "REDFISH_MESSAGE_ID",
- std::string("OpenBMC.0.1.FormatFail"));
- throw ResourceNotFound();
- }
formatLuksDev(cryptHandle.get(), password);
activateLuksDev(cryptHandle.get(), password);
@@ -195,12 +188,6 @@
lg2::info("Starting unlock", "REDFISH_MESSAGE_ID", msg);
CryptHandle cryptHandle(devPath.c_str());
- if (cryptHandle.get() == nullptr)
- {
- lg2::error("Failed to initialize crypt device", "REDFISH_MESSAGE_ID",
- std::string("OpenBMC.0.1.UnlockFail"));
- throw ResourceNotFound();
- }
activateLuksDev(cryptHandle.get(), std::move(password));
mountFilesystem();