blob: 061fc4cf8aa7062a03ae0414151ab26a3a723529 [file] [log] [blame]
Kun Yi91beea62018-11-26 15:23:14 -08001#include "handler.hpp"
Kun Yi4dc76482019-03-05 16:06:19 -08002#include "parse_config.hpp"
Patrick Venture15f0f942020-07-09 09:38:18 -07003#include "sys_file_impl.hpp"
Kun Yi91beea62018-11-26 15:23:14 -08004
5#include <blobs-ipmid/blobs.hpp>
Kun Yi4dc76482019-03-05 16:06:19 -08006#include <exception>
7#include <fstream>
Kun Yi91beea62018-11-26 15:23:14 -08008#include <memory>
Kun Yi4dc76482019-03-05 16:06:19 -08009#include <phosphor-logging/elog.hpp>
Kun Yi91beea62018-11-26 15:23:14 -080010
11#ifdef __cplusplus
12extern "C" {
13#endif
14
15/**
16 * This is required by the blob manager.
17 * TODO: move the declaration to blobs.hpp since all handlers need it
18 */
19std::unique_ptr<blobs::GenericBlobInterface> createHandler();
20
21#ifdef __cplusplus
22}
23#endif
24
Kun Yi4dc76482019-03-05 16:06:19 -080025/* Configuration file path */
26constexpr auto blobConfigPath = "/usr/share/binaryblob/config.json";
27
Kun Yi91beea62018-11-26 15:23:14 -080028std::unique_ptr<blobs::GenericBlobInterface> createHandler()
29{
Kun Yi4dc76482019-03-05 16:06:19 -080030 using namespace phosphor::logging;
31 using nlohmann::json;
32
33 std::ifstream input(blobConfigPath);
34 json j;
35
36 try
37 {
38 input >> j;
39 }
40 catch (const std::exception& e)
41 {
42 log<level::ERR>("Failed to parse config into json",
43 entry("ERR=%s", e.what()));
44 return nullptr;
45 }
46
47 // Construct binary blobs from config and add to handler
48 auto handler = std::make_unique<blobs::BinaryStoreBlobHandler>();
49
50 for (const auto& element : j)
51 {
52 conf::BinaryBlobConfig config;
53 try
54 {
55 conf::parseFromConfigFile(element, config);
56 }
57 catch (const std::exception& e)
58 {
59 log<level::ERR>("Encountered error when parsing config file",
60 entry("ERR=%s", e.what()));
61 return nullptr;
62 }
63
64 log<level::INFO>("Loading from config with",
65 entry("BASE_ID=%s", config.blobBaseId.c_str()),
66 entry("FILE=%s", config.sysFilePath.c_str()),
67 entry("MAX_SIZE=%llx", static_cast<unsigned long long>(
68 config.maxSizeBytes)));
69
70 auto file = std::make_unique<binstore::SysFileImpl>(config.sysFilePath,
71 config.offsetBytes);
72
73 handler->addNewBinaryStore(binstore::BinaryStore::createFromConfig(
Patrick Venturee496b2b2020-07-09 13:49:05 -070074 config.blobBaseId, std::move(file)));
Kun Yi4dc76482019-03-05 16:06:19 -080075 }
76
77 return std::move(handler);
Kun Yi91beea62018-11-26 15:23:14 -080078}