Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 1 | #include "handler.hpp" |
Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 2 | #include "parse_config.hpp" |
Patrick Venture | 15f0f94 | 2020-07-09 09:38:18 -0700 | [diff] [blame] | 3 | #include "sys_file_impl.hpp" |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 4 | |
| 5 | #include <blobs-ipmid/blobs.hpp> |
Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 6 | #include <exception> |
| 7 | #include <fstream> |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 8 | #include <memory> |
Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 9 | #include <phosphor-logging/elog.hpp> |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 10 | |
| 11 | #ifdef __cplusplus |
| 12 | extern "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 | */ |
| 19 | std::unique_ptr<blobs::GenericBlobInterface> createHandler(); |
| 20 | |
| 21 | #ifdef __cplusplus |
| 22 | } |
| 23 | #endif |
| 24 | |
Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 25 | /* Configuration file path */ |
| 26 | constexpr auto blobConfigPath = "/usr/share/binaryblob/config.json"; |
| 27 | |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 28 | std::unique_ptr<blobs::GenericBlobInterface> createHandler() |
| 29 | { |
Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 30 | 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 Venture | e496b2b | 2020-07-09 13:49:05 -0700 | [diff] [blame] | 74 | config.blobBaseId, std::move(file))); |
Kun Yi | 4dc7648 | 2019-03-05 16:06:19 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | return std::move(handler); |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 78 | } |