bmc: implement search for json files
Tested: Verified it prints out the blobs expected.
Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I1ba6af8b2ca066e6eb1699cb4214ce32f148005d
diff --git a/bmc/Makefile.am b/bmc/Makefile.am
index ef1a455..797de8d 100644
--- a/bmc/Makefile.am
+++ b/bmc/Makefile.am
@@ -37,6 +37,7 @@
noinst_LTLIBRARIES = libfirmwareblob_common.la
libfirmwareblob_common_la_SOURCES = \
buildjson.cpp \
+ fs.cpp \
firmware_handler.cpp \
file_handler.cpp \
prepare_systemd.cpp \
diff --git a/bmc/buildjson.cpp b/bmc/buildjson.cpp
index 5c14e61..a359add 100644
--- a/bmc/buildjson.cpp
+++ b/bmc/buildjson.cpp
@@ -16,13 +16,17 @@
#include "buildjson.hpp"
#include "file_handler.hpp"
+#include "fs.hpp"
#include "prepare_systemd.hpp"
#include "update_systemd.hpp"
#include "verify_systemd.hpp"
+#include <algorithm>
#include <cstdio>
#include <exception>
+#include <fstream>
#include <nlohmann/json.hpp>
+#include <phosphor-logging/log.hpp>
#include <sdbusplus/bus.hpp>
#include <string>
#include <vector>
@@ -133,4 +137,37 @@
return handlers;
}
+std::vector<HandlerConfig> BuildHandlerConfigs(const std::string& directory)
+{
+ using namespace phosphor::logging;
+
+ std::vector<HandlerConfig> output;
+
+ std::vector<std::string> jsonPaths = GetJsonList(directory);
+
+ for (const auto& path : jsonPaths)
+ {
+ std::ifstream jsonFile(path);
+ if (!jsonFile.is_open())
+ {
+ log<level::ERR>("Unable to open json file",
+ entry("PATH=%s", path.c_str()));
+ continue;
+ }
+
+ auto data = nlohmann::json::parse(jsonFile, nullptr, false);
+ if (data.is_discarded())
+ {
+ log<level::ERR>("Parsing json failed",
+ entry("PATH=%s", path.c_str()));
+ continue;
+ }
+
+ std::vector<HandlerConfig> configs = buildHandlerFromJson(data);
+ std::move(configs.begin(), configs.end(), std::back_inserter(output));
+ }
+
+ return output;
+}
+
} // namespace ipmi_flash
diff --git a/bmc/buildjson.hpp b/bmc/buildjson.hpp
index 5f9affd..8e333f6 100644
--- a/bmc/buildjson.hpp
+++ b/bmc/buildjson.hpp
@@ -54,4 +54,12 @@
*/
std::vector<HandlerConfig> buildHandlerFromJson(const nlohmann::json& data);
+/**
+ * Given a folder of json configs, build the configurations.
+ *
+ * @param[in] directory - the directory to search (recurisvely).
+ * @return list of HandlerConfig objects.
+ */
+std::vector<HandlerConfig> BuildHandlerConfigs(const std::string& directory);
+
} // namespace ipmi_flash
diff --git a/bmc/fs.cpp b/bmc/fs.cpp
new file mode 100644
index 0000000..c43e310
--- /dev/null
+++ b/bmc/fs.cpp
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2019 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "fs.hpp"
+
+#include <filesystem>
+#include <regex>
+#include <string>
+#include <vector>
+
+namespace ipmi_flash
+{
+namespace fs = std::filesystem;
+
+std::vector<std::string> GetJsonList(const std::string& directory)
+{
+ std::vector<std::string> output;
+
+ for (const auto& p : fs::recursive_directory_iterator(directory))
+ {
+ auto ps = p.path().string();
+
+ /** TODO: openbmc/phosphor-ipmi-blobs/blob/de8a16e2e8/fs.cpp#L27 is
+ * nicer, may be worth finding a way to make this into a util.
+ */
+ if (std::regex_match(ps, std::regex(".+.json$")))
+ {
+ output.push_back(ps);
+ }
+ }
+
+ return output;
+}
+
+} // namespace ipmi_flash
diff --git a/bmc/fs.hpp b/bmc/fs.hpp
new file mode 100644
index 0000000..d54af2a
--- /dev/null
+++ b/bmc/fs.hpp
@@ -0,0 +1,17 @@
+#pragma once
+
+#include <string>
+#include <vector>
+
+namespace ipmi_flash
+{
+
+/**
+ * Given a directory, return the list of json file paths (full paths).
+ *
+ * @param[in] directory - the full path to the directory to search.
+ * @return list of full paths to json files found in that directory.
+ */
+std::vector<std::string> GetJsonList(const std::string& directory);
+
+} // namespace ipmi_flash
diff --git a/bmc/main.cpp b/bmc/main.cpp
index d22348d..2120107 100644
--- a/bmc/main.cpp
+++ b/bmc/main.cpp
@@ -16,6 +16,7 @@
#include "config.h"
+#include "buildjson.hpp"
#include "file_handler.hpp"
#include "firmware_handler.hpp"
#include "flags.hpp"
@@ -34,16 +35,22 @@
#include <memory>
#include <phosphor-logging/log.hpp>
#include <sdbusplus/bus.hpp>
+#include <string>
#include <unordered_map>
+#include <vector>
namespace ipmi_flash
{
+
namespace
{
/* The maximum external buffer size we expect is 64KB. */
static constexpr std::size_t memoryRegionSize = 64 * 1024UL;
+static constexpr const char* jsonConfigurationPath =
+ "/usr/share/phosphor-ipmi-flash/";
+
#ifdef ENABLE_LPC_BRIDGE
#if defined(ASPEED_LPC)
LpcDataHandler lpcDataHandler(
@@ -96,6 +103,17 @@
std::unique_ptr<blobs::GenericBlobInterface> createHandler()
{
+ /* NOTE: This is unused presently. */
+ {
+ std::vector<ipmi_flash::HandlerConfig> configsFromJson =
+ ipmi_flash::BuildHandlerConfigs(ipmi_flash::jsonConfigurationPath);
+
+ for (const auto& config : configsFromJson)
+ {
+ std::fprintf(stderr, "config loaded: %s\n", config.blobId.c_str());
+ }
+ }
+
ipmi_flash::ActionMap actionPacks = {};
{