1 /*
2  * Copyright 2018 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "config.h"
18 
19 #include "file_handler.hpp"
20 #include "general_systemd.hpp"
21 #include "image_handler.hpp"
22 #include "status.hpp"
23 #include "version_handler.hpp"
24 #include "version_handlers_builder.hpp"
25 
26 #include <phosphor-logging/log.hpp>
27 #include <sdbusplus/bus.hpp>
28 
29 #include <cstdint>
30 #include <memory>
31 #include <string>
32 #include <unordered_map>
33 #include <vector>
34 
35 namespace ipmi_flash
36 {
37 
38 namespace
39 {
40 
41 static constexpr const char* jsonConfigurationPath =
42     "/usr/share/phosphor-ipmi-flash/";
43 } // namespace
44 
45 std::unique_ptr<blobs::GenericBlobInterface>
46     createHandlerFromJsons(VersionHandlersBuilder& builder,
47                            const char* configPath)
48 {
49     std::vector<HandlerConfig<VersionActionPack>> configsFromJson =
50         builder.buildHandlerConfigs(configPath);
51 
52     VersionInfoMap handlerMap;
53     for (auto& config : configsFromJson)
54     {
55         auto [it, inserted] = handlerMap.try_emplace(
56             config.blobId, config.blobId, std::move(config.actions),
57             std::move(config.handler));
58         if (inserted == false)
59         {
60             std::fprintf(stderr, "duplicate blob id %s, discarding\n",
61                          config.blobId.c_str());
62         }
63         else
64         {
65             std::fprintf(stderr, "config loaded: %s\n", config.blobId.c_str());
66         }
67     }
68     auto handler = VersionBlobHandler::create(std::move(handlerMap));
69     if (!handler)
70     {
71         std::fprintf(stderr, "Version Handler has an invalid configuration");
72         return nullptr;
73     }
74 
75     return handler;
76 }
77 } // namespace ipmi_flash
78 extern "C"
79 {
80     std::unique_ptr<blobs::GenericBlobInterface> createHandler();
81 }
82 
83 std::unique_ptr<blobs::GenericBlobInterface> createHandler()
84 {
85     ipmi_flash::VersionHandlersBuilder builder;
86     return ipmi_flash::createHandlerFromJsons(
87         builder, ipmi_flash::jsonConfigurationPath);
88 }
89