blob: d5f029c37ccf617d298719e0c15d929c493eb67f [file] [log] [blame]
Patrick Venture298930a2019-07-03 11:44:52 -07001/*
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 */
Jason Lingc893f432020-10-24 19:31:44 -070016#include "firmware_handlers_builder.hpp"
Patrick Venture298930a2019-07-03 11:44:52 -070017
18#include "file_handler.hpp"
Patrick Venturea6b4abd2019-07-19 10:58:55 -070019#include "fs.hpp"
Patrick Ventured53d60a2020-04-07 09:01:34 -070020#include "skip_action.hpp"
Patrick Venture298930a2019-07-03 11:44:52 -070021
Patrick Venture9b37b092020-05-28 20:58:57 -070022#include <nlohmann/json.hpp>
Patrick Venture9b37b092020-05-28 20:58:57 -070023
Patrick Venturea6b4abd2019-07-19 10:58:55 -070024#include <algorithm>
Patrick Venture298930a2019-07-03 11:44:52 -070025#include <cstdio>
26#include <exception>
Patrick Venturea6b4abd2019-07-19 10:58:55 -070027#include <fstream>
Patrick Venture097435f2019-08-15 07:39:48 -070028#include <regex>
Patrick Venture298930a2019-07-03 11:44:52 -070029#include <string>
30#include <vector>
31
32namespace ipmi_flash
33{
Jason Lingc893f432020-10-24 19:31:44 -070034std::vector<HandlerConfig<ActionPack>>
35 FirmwareHandlersBuilder::buildHandlerFromJson(const nlohmann::json& data)
Patrick Venture9cce5a22019-08-06 09:24:46 -070036{
Jason Lingc893f432020-10-24 19:31:44 -070037 std::vector<HandlerConfig<ActionPack>> handlers;
Patrick Venture298930a2019-07-03 11:44:52 -070038
39 for (const auto& item : data)
40 {
41 try
42 {
Jason Lingc893f432020-10-24 19:31:44 -070043 HandlerConfig<ActionPack> output;
Patrick Venture298930a2019-07-03 11:44:52 -070044
45 /* at() throws an exception when the key is not present. */
46 item.at("blob").get_to(output.blobId);
47
Patrick Venture097435f2019-08-15 07:39:48 -070048 /* name must be: /flash/... */
49 if (!std::regex_match(output.blobId, std::regex("^\\/flash\\/.+")))
50 {
51 throw std::runtime_error("Invalid blob name: '" +
52 output.blobId +
53 "' must start with /flash/");
54 }
55
Patrick Venture298930a2019-07-03 11:44:52 -070056 /* handler is required. */
57 const auto& h = item.at("handler");
58 const std::string handlerType = h.at("type");
59 if (handlerType == "file")
60 {
61 const auto& path = h.at("path");
62 output.handler = std::make_unique<FileHandler>(path);
63 }
64 else
65 {
66 throw std::runtime_error("Invalid handler type: " +
67 handlerType);
68 }
69
70 /* actions are required (presently). */
71 const auto& a = item.at("actions");
72 std::unique_ptr<ActionPack> pack = std::make_unique<ActionPack>();
73
Patrick Ventured53d60a2020-04-07 09:01:34 -070074 /* to make an action optional, assign type "skip" */
Patrick Venture298930a2019-07-03 11:44:52 -070075 const auto& prep = a.at("preparation");
76 const std::string prepareType = prep.at("type");
77 if (prepareType == "systemd")
78 {
Patrick Venturee0216d22019-08-21 10:17:39 -070079 pack->preparation = std::move(buildSystemd(prep));
Patrick Venture298930a2019-07-03 11:44:52 -070080 }
Patrick Ventured53d60a2020-04-07 09:01:34 -070081 else if (prepareType == "skip")
82 {
83 pack->preparation = SkipAction::CreateSkipAction();
84 }
Patrick Venture298930a2019-07-03 11:44:52 -070085 else
86 {
87 throw std::runtime_error("Invalid preparation type: " +
88 prepareType);
89 }
90
91 const auto& verify = a.at("verification");
92 const std::string verifyType = verify.at("type");
93 if (verifyType == "fileSystemdVerify")
94 {
Patrick Venture9cce5a22019-08-06 09:24:46 -070095 pack->verification = std::move(buildFileSystemd(verify));
Patrick Venture298930a2019-07-03 11:44:52 -070096 }
William A. Kennington III2d434c82019-11-22 18:42:35 -080097 else if (verifyType == "systemd")
98 {
99 pack->verification = std::move(buildSystemd(verify));
100 }
Patrick Ventured53d60a2020-04-07 09:01:34 -0700101 else if (verifyType == "skip")
102 {
103 pack->verification = SkipAction::CreateSkipAction();
104 }
Patrick Venture298930a2019-07-03 11:44:52 -0700105 else
106 {
107 throw std::runtime_error("Invalid verification type:" +
108 verifyType);
109 }
110
111 const auto& update = a.at("update");
112 const std::string updateType = update.at("type");
113 if (updateType == "reboot")
114 {
Patrick Venturee0216d22019-08-21 10:17:39 -0700115 pack->update = SystemdNoFile::CreateSystemdNoFile(
Patrick Venture44564902019-08-21 09:46:32 -0700116 sdbusplus::bus::new_default(), "reboot.target",
117 "replace-irreversibly");
Patrick Venture298930a2019-07-03 11:44:52 -0700118 }
Patrick Venturec2baac92019-08-05 13:30:38 -0700119 else if (updateType == "fileSystemdUpdate")
120 {
Patrick Venture9cce5a22019-08-06 09:24:46 -0700121 pack->update = std::move(buildFileSystemd(update));
Patrick Venturec2baac92019-08-05 13:30:38 -0700122 }
Patrick Venture298930a2019-07-03 11:44:52 -0700123 else if (updateType == "systemd")
124 {
Patrick Venturee0216d22019-08-21 10:17:39 -0700125 pack->update = std::move(buildSystemd(update));
Patrick Venture298930a2019-07-03 11:44:52 -0700126 }
Patrick Ventured53d60a2020-04-07 09:01:34 -0700127 else if (updateType == "skip")
128 {
129 pack->update = SkipAction::CreateSkipAction();
130 }
Patrick Venture298930a2019-07-03 11:44:52 -0700131 else
132 {
133 throw std::runtime_error("Invalid update type: " + updateType);
134 }
135
136 output.actions = std::move(pack);
137 handlers.push_back(std::move(output));
138 }
139 catch (const std::exception& e)
140 {
141 /* TODO: Once phosphor-logging supports unit-test injection, fix
142 * this to log.
143 */
144 std::fprintf(stderr,
145 "Excepted building HandlerConfig from json: %s\n",
146 e.what());
147 }
148 }
149
150 return handlers;
151}
Patrick Venture298930a2019-07-03 11:44:52 -0700152} // namespace ipmi_flash