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