Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 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 | */ |
Jason Ling | c893f43 | 2020-10-24 19:31:44 -0700 | [diff] [blame] | 16 | #include "firmware_handlers_builder.hpp" |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 17 | |
| 18 | #include "file_handler.hpp" |
Patrick Venture | a6b4abd | 2019-07-19 10:58:55 -0700 | [diff] [blame] | 19 | #include "fs.hpp" |
William A. Kennington III | bec2318 | 2021-01-21 18:10:52 -0800 | [diff] [blame] | 20 | #include "general_systemd.hpp" |
Patrick Venture | d53d60a | 2020-04-07 09:01:34 -0700 | [diff] [blame] | 21 | #include "skip_action.hpp" |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 22 | |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 23 | #include <nlohmann/json.hpp> |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 24 | |
Patrick Venture | a6b4abd | 2019-07-19 10:58:55 -0700 | [diff] [blame] | 25 | #include <algorithm> |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 26 | #include <cstdio> |
| 27 | #include <exception> |
Patrick Venture | a6b4abd | 2019-07-19 10:58:55 -0700 | [diff] [blame] | 28 | #include <fstream> |
Patrick Venture | 097435f | 2019-08-15 07:39:48 -0700 | [diff] [blame] | 29 | #include <regex> |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <vector> |
| 32 | |
| 33 | namespace ipmi_flash |
| 34 | { |
Jason Ling | c893f43 | 2020-10-24 19:31:44 -0700 | [diff] [blame] | 35 | std::vector<HandlerConfig<ActionPack>> |
| 36 | FirmwareHandlersBuilder::buildHandlerFromJson(const nlohmann::json& data) |
Patrick Venture | 9cce5a2 | 2019-08-06 09:24:46 -0700 | [diff] [blame] | 37 | { |
Jason Ling | c893f43 | 2020-10-24 19:31:44 -0700 | [diff] [blame] | 38 | std::vector<HandlerConfig<ActionPack>> handlers; |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 39 | |
| 40 | for (const auto& item : data) |
| 41 | { |
| 42 | try |
| 43 | { |
Jason Ling | c893f43 | 2020-10-24 19:31:44 -0700 | [diff] [blame] | 44 | HandlerConfig<ActionPack> output; |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 45 | |
| 46 | /* at() throws an exception when the key is not present. */ |
| 47 | item.at("blob").get_to(output.blobId); |
| 48 | |
Patrick Venture | 097435f | 2019-08-15 07:39:48 -0700 | [diff] [blame] | 49 | /* name must be: /flash/... */ |
| 50 | if (!std::regex_match(output.blobId, std::regex("^\\/flash\\/.+"))) |
| 51 | { |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 52 | throw std::runtime_error( |
| 53 | "Invalid blob name: '" + output.blobId + |
| 54 | "' must start with /flash/"); |
Patrick Venture | 097435f | 2019-08-15 07:39:48 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 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 | { |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 67 | throw std::runtime_error( |
| 68 | "Invalid handler type: " + handlerType); |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 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 | |
Patrick Venture | d53d60a | 2020-04-07 09:01:34 -0700 | [diff] [blame] | 75 | /* to make an action optional, assign type "skip" */ |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 76 | const auto& prep = a.at("preparation"); |
| 77 | const std::string prepareType = prep.at("type"); |
| 78 | if (prepareType == "systemd") |
| 79 | { |
Patrick Venture | e0216d2 | 2019-08-21 10:17:39 -0700 | [diff] [blame] | 80 | pack->preparation = std::move(buildSystemd(prep)); |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 81 | } |
Patrick Venture | d53d60a | 2020-04-07 09:01:34 -0700 | [diff] [blame] | 82 | else if (prepareType == "skip") |
| 83 | { |
| 84 | pack->preparation = SkipAction::CreateSkipAction(); |
| 85 | } |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 86 | else |
| 87 | { |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 88 | throw std::runtime_error( |
| 89 | "Invalid preparation type: " + prepareType); |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | const auto& verify = a.at("verification"); |
| 93 | const std::string verifyType = verify.at("type"); |
| 94 | if (verifyType == "fileSystemdVerify") |
| 95 | { |
Patrick Venture | 9cce5a2 | 2019-08-06 09:24:46 -0700 | [diff] [blame] | 96 | pack->verification = std::move(buildFileSystemd(verify)); |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 97 | } |
William A. Kennington III | 2d434c8 | 2019-11-22 18:42:35 -0800 | [diff] [blame] | 98 | else if (verifyType == "systemd") |
| 99 | { |
| 100 | pack->verification = std::move(buildSystemd(verify)); |
| 101 | } |
Patrick Venture | d53d60a | 2020-04-07 09:01:34 -0700 | [diff] [blame] | 102 | else if (verifyType == "skip") |
| 103 | { |
| 104 | pack->verification = SkipAction::CreateSkipAction(); |
| 105 | } |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 106 | else |
| 107 | { |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 108 | throw std::runtime_error( |
| 109 | "Invalid verification type:" + verifyType); |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | const auto& update = a.at("update"); |
| 113 | const std::string updateType = update.at("type"); |
| 114 | if (updateType == "reboot") |
| 115 | { |
Patrick Venture | e0216d2 | 2019-08-21 10:17:39 -0700 | [diff] [blame] | 116 | pack->update = SystemdNoFile::CreateSystemdNoFile( |
Patrick Venture | 4456490 | 2019-08-21 09:46:32 -0700 | [diff] [blame] | 117 | sdbusplus::bus::new_default(), "reboot.target", |
| 118 | "replace-irreversibly"); |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 119 | } |
Patrick Venture | c2baac9 | 2019-08-05 13:30:38 -0700 | [diff] [blame] | 120 | else if (updateType == "fileSystemdUpdate") |
| 121 | { |
Patrick Venture | 9cce5a2 | 2019-08-06 09:24:46 -0700 | [diff] [blame] | 122 | pack->update = std::move(buildFileSystemd(update)); |
Patrick Venture | c2baac9 | 2019-08-05 13:30:38 -0700 | [diff] [blame] | 123 | } |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 124 | else if (updateType == "systemd") |
| 125 | { |
Patrick Venture | e0216d2 | 2019-08-21 10:17:39 -0700 | [diff] [blame] | 126 | pack->update = std::move(buildSystemd(update)); |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 127 | } |
Patrick Venture | d53d60a | 2020-04-07 09:01:34 -0700 | [diff] [blame] | 128 | else if (updateType == "skip") |
| 129 | { |
| 130 | pack->update = SkipAction::CreateSkipAction(); |
| 131 | } |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 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 | } |
Patrick Venture | 298930a | 2019-07-03 11:44:52 -0700 | [diff] [blame] | 153 | } // namespace ipmi_flash |