blob: 36dc8753ebba9a7b68f533641c18654ac843881c [file] [log] [blame]
Prithvi Pai627c99d2025-02-08 14:05:25 +05301#include "secureboot.hpp"
2
3#include <cereal/archives/binary.hpp>
4
5#include <fstream>
6
7// Register class version with Cereal
8CEREAL_CLASS_VERSION(bios_config::SecureBoot, 0)
9
10namespace bios_config
11{
12
13SecureBoot::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
26SecureBootBase::CurrentBootType SecureBoot::currentBoot(
27 SecureBootBase::CurrentBootType value)
28{
29 auto ret = SecureBootBase::currentBoot(value);
30 serialize();
31 return ret;
32}
33
34bool SecureBoot::pendingEnable(bool value)
35{
36 auto ret = SecureBootBase::pendingEnable(value);
37 serialize();
38 return ret;
39}
40
41SecureBootBase::ModeType SecureBoot::mode(SecureBootBase::ModeType value)
42{
43 auto ret = SecureBootBase::mode(value);
44 serialize();
45 return ret;
46}
47
48void 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
64bool 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