1 /*
2  * Copyright 2019 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 #include "firmware_handlers_builder.hpp"
17 
18 #include "file_handler.hpp"
19 #include "fs.hpp"
20 #include "general_systemd.hpp"
21 #include "skip_action.hpp"
22 
23 #include <nlohmann/json.hpp>
24 
25 #include <algorithm>
26 #include <cstdio>
27 #include <exception>
28 #include <fstream>
29 #include <regex>
30 #include <string>
31 #include <vector>
32 
33 namespace ipmi_flash
34 {
35 std::vector<HandlerConfig<ActionPack>>
36     FirmwareHandlersBuilder::buildHandlerFromJson(const nlohmann::json& data)
37 {
38     std::vector<HandlerConfig<ActionPack>> handlers;
39 
40     for (const auto& item : data)
41     {
42         try
43         {
44             HandlerConfig<ActionPack> output;
45 
46             /* at() throws an exception when the key is not present. */
47             item.at("blob").get_to(output.blobId);
48 
49             /* name must be: /flash/... */
50             if (!std::regex_match(output.blobId, std::regex("^\\/flash\\/.+")))
51             {
52                 throw std::runtime_error("Invalid blob name: '" +
53                                          output.blobId +
54                                          "' must start with /flash/");
55             }
56 
57             /* handler is required. */
58             const auto& h = item.at("handler");
59             const std::string handlerType = h.at("type");
60             if (handlerType == "file")
61             {
62                 const auto& path = h.at("path");
63                 output.handler = std::make_unique<FileHandler>(path);
64             }
65             else
66             {
67                 throw std::runtime_error("Invalid handler type: " +
68                                          handlerType);
69             }
70 
71             /* actions are required (presently). */
72             const auto& a = item.at("actions");
73             std::unique_ptr<ActionPack> pack = std::make_unique<ActionPack>();
74 
75             /* to make an action optional, assign type "skip" */
76             const auto& prep = a.at("preparation");
77             const std::string prepareType = prep.at("type");
78             if (prepareType == "systemd")
79             {
80                 pack->preparation = std::move(buildSystemd(prep));
81             }
82             else if (prepareType == "skip")
83             {
84                 pack->preparation = SkipAction::CreateSkipAction();
85             }
86             else
87             {
88                 throw std::runtime_error("Invalid preparation type: " +
89                                          prepareType);
90             }
91 
92             const auto& verify = a.at("verification");
93             const std::string verifyType = verify.at("type");
94             if (verifyType == "fileSystemdVerify")
95             {
96                 pack->verification = std::move(buildFileSystemd(verify));
97             }
98             else if (verifyType == "systemd")
99             {
100                 pack->verification = std::move(buildSystemd(verify));
101             }
102             else if (verifyType == "skip")
103             {
104                 pack->verification = SkipAction::CreateSkipAction();
105             }
106             else
107             {
108                 throw std::runtime_error("Invalid verification type:" +
109                                          verifyType);
110             }
111 
112             const auto& update = a.at("update");
113             const std::string updateType = update.at("type");
114             if (updateType == "reboot")
115             {
116                 pack->update = SystemdNoFile::CreateSystemdNoFile(
117                     sdbusplus::bus::new_default(), "reboot.target",
118                     "replace-irreversibly");
119             }
120             else if (updateType == "fileSystemdUpdate")
121             {
122                 pack->update = std::move(buildFileSystemd(update));
123             }
124             else if (updateType == "systemd")
125             {
126                 pack->update = std::move(buildSystemd(update));
127             }
128             else if (updateType == "skip")
129             {
130                 pack->update = SkipAction::CreateSkipAction();
131             }
132             else
133             {
134                 throw std::runtime_error("Invalid update type: " + updateType);
135             }
136 
137             output.actions = std::move(pack);
138             handlers.push_back(std::move(output));
139         }
140         catch (const std::exception& e)
141         {
142             /* TODO: Once phosphor-logging supports unit-test injection, fix
143              * this to log.
144              */
145             std::fprintf(stderr,
146                          "Excepted building HandlerConfig from json: %s\n",
147                          e.what());
148         }
149     }
150 
151     return handlers;
152 }
153 } // namespace ipmi_flash
154