xref: /openbmc/bios-settings-mgr/src/secureboot.cpp (revision 627c99dec58b6c5012b6c271935ad7902def9e64)
1*627c99deSPrithvi Pai #include "secureboot.hpp"
2*627c99deSPrithvi Pai 
3*627c99deSPrithvi Pai #include <cereal/archives/binary.hpp>
4*627c99deSPrithvi Pai 
5*627c99deSPrithvi Pai #include <fstream>
6*627c99deSPrithvi Pai 
7*627c99deSPrithvi Pai // Register class version with Cereal
8*627c99deSPrithvi Pai CEREAL_CLASS_VERSION(bios_config::SecureBoot, 0)
9*627c99deSPrithvi Pai 
10*627c99deSPrithvi Pai namespace bios_config
11*627c99deSPrithvi Pai {
12*627c99deSPrithvi Pai 
SecureBoot(sdbusplus::asio::object_server & objectServer,std::shared_ptr<sdbusplus::asio::connection> & systemBus,std::string persistPath)13*627c99deSPrithvi Pai SecureBoot::SecureBoot(sdbusplus::asio::object_server& objectServer,
14*627c99deSPrithvi Pai                        std::shared_ptr<sdbusplus::asio::connection>& systemBus,
15*627c99deSPrithvi Pai                        std::string persistPath) :
16*627c99deSPrithvi Pai     sdbusplus::xyz::openbmc_project::BIOSConfig::server::SecureBoot(
17*627c99deSPrithvi Pai         *systemBus, secureBootObjectPath),
18*627c99deSPrithvi Pai     objServer(objectServer), systemBus(systemBus)
19*627c99deSPrithvi Pai {
20*627c99deSPrithvi Pai     fs::path secureBootDir(persistPath);
21*627c99deSPrithvi Pai     fs::create_directories(secureBootDir);
22*627c99deSPrithvi Pai     secureBootFile = secureBootDir / secureBootPersistFile;
23*627c99deSPrithvi Pai     deserialize();
24*627c99deSPrithvi Pai }
25*627c99deSPrithvi Pai 
currentBoot(SecureBootBase::CurrentBootType value)26*627c99deSPrithvi Pai SecureBootBase::CurrentBootType SecureBoot::currentBoot(
27*627c99deSPrithvi Pai     SecureBootBase::CurrentBootType value)
28*627c99deSPrithvi Pai {
29*627c99deSPrithvi Pai     auto ret = SecureBootBase::currentBoot(value);
30*627c99deSPrithvi Pai     serialize();
31*627c99deSPrithvi Pai     return ret;
32*627c99deSPrithvi Pai }
33*627c99deSPrithvi Pai 
pendingEnable(bool value)34*627c99deSPrithvi Pai bool SecureBoot::pendingEnable(bool value)
35*627c99deSPrithvi Pai {
36*627c99deSPrithvi Pai     auto ret = SecureBootBase::pendingEnable(value);
37*627c99deSPrithvi Pai     serialize();
38*627c99deSPrithvi Pai     return ret;
39*627c99deSPrithvi Pai }
40*627c99deSPrithvi Pai 
mode(SecureBootBase::ModeType value)41*627c99deSPrithvi Pai SecureBootBase::ModeType SecureBoot::mode(SecureBootBase::ModeType value)
42*627c99deSPrithvi Pai {
43*627c99deSPrithvi Pai     auto ret = SecureBootBase::mode(value);
44*627c99deSPrithvi Pai     serialize();
45*627c99deSPrithvi Pai     return ret;
46*627c99deSPrithvi Pai }
47*627c99deSPrithvi Pai 
serialize()48*627c99deSPrithvi Pai void SecureBoot::serialize()
49*627c99deSPrithvi Pai {
50*627c99deSPrithvi Pai     try
51*627c99deSPrithvi Pai     {
52*627c99deSPrithvi Pai         std::filesystem::create_directories(secureBootFile.parent_path());
53*627c99deSPrithvi Pai         std::ofstream os(secureBootFile.c_str(),
54*627c99deSPrithvi Pai                          std::ios::out | std::ios::binary);
55*627c99deSPrithvi Pai         cereal::BinaryOutputArchive oarchive(os);
56*627c99deSPrithvi Pai         oarchive(*this);
57*627c99deSPrithvi Pai     }
58*627c99deSPrithvi Pai     catch (const std::exception& e)
59*627c99deSPrithvi Pai     {
60*627c99deSPrithvi Pai         lg2::error("Failed to serialize SecureBoot: {ERROR}", "ERROR", e);
61*627c99deSPrithvi Pai     }
62*627c99deSPrithvi Pai }
63*627c99deSPrithvi Pai 
deserialize()64*627c99deSPrithvi Pai bool SecureBoot::deserialize()
65*627c99deSPrithvi Pai {
66*627c99deSPrithvi Pai     try
67*627c99deSPrithvi Pai     {
68*627c99deSPrithvi Pai         if (std::filesystem::exists(secureBootFile))
69*627c99deSPrithvi Pai         {
70*627c99deSPrithvi Pai             std::ifstream is(secureBootFile.c_str(),
71*627c99deSPrithvi Pai                              std::ios::in | std::ios::binary);
72*627c99deSPrithvi Pai             cereal::BinaryInputArchive iarchive(is);
73*627c99deSPrithvi Pai             iarchive(*this);
74*627c99deSPrithvi Pai             return true;
75*627c99deSPrithvi Pai         }
76*627c99deSPrithvi Pai         return false;
77*627c99deSPrithvi Pai     }
78*627c99deSPrithvi Pai     catch (const std::exception& e)
79*627c99deSPrithvi Pai     {
80*627c99deSPrithvi Pai         lg2::error("Failed to deserialize SecureBoot: {ERROR}", "ERROR", e);
81*627c99deSPrithvi Pai         return false;
82*627c99deSPrithvi Pai     }
83*627c99deSPrithvi Pai }
84*627c99deSPrithvi Pai } // namespace bios_config
85