blob: 6169899c1b175af96e5b78b92c689ef287804a73 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Feras Aldahlawi735ef6d2021-03-19 14:01:46 -07003#pragma once
4
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08005#include "app.hpp"
6#include "async_resp.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08007#include "dbus_singleton.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08008#include "dbus_utility.hpp"
9#include "error_messages.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080010#include "http_request.hpp"
11#include "logging.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080012#include "utils/collection.hpp"
13#include "utils/hex_utils.hpp"
14#include "utils/json_utils.hpp"
15
Ed Tanousd7857202025-01-28 15:32:26 -080016#include <boost/beast/http/verb.hpp>
George Liue99073f2022-12-09 11:06:16 +080017#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070018#include <boost/url/format.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080019#include <boost/url/url.hpp>
Feras Aldahlawi735ef6d2021-03-19 14:01:46 -070020#include <nlohmann/json.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080021#include <sdbusplus/message/native_types.hpp>
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -070022
George Liu7a1dbc42022-12-07 16:03:22 +080023#include <array>
Ed Tanousd7857202025-01-28 15:32:26 -080024#include <cstdint>
25#include <functional>
26#include <memory>
George Liu7a1dbc42022-12-07 16:03:22 +080027#include <string_view>
Ed Tanousd7857202025-01-28 15:32:26 -080028#include <utility>
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -070029#include <vector>
Feras Aldahlawi735ef6d2021-03-19 14:01:46 -070030
31namespace crow
32{
33namespace google_api
34{
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -070035
Nan Zhou30aacdd2022-07-03 05:02:36 +000036inline void
37 handleGoogleV1Get(const crow::Request& /*req*/,
38 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -070039{
40 asyncResp->res.jsonValue["@odata.type"] =
41 "#GoogleServiceRoot.v1_0_0.GoogleServiceRoot";
42 asyncResp->res.jsonValue["@odata.id"] = "/google/v1";
43 asyncResp->res.jsonValue["Id"] = "Google Rest RootService";
44 asyncResp->res.jsonValue["Name"] = "Google Service Root";
45 asyncResp->res.jsonValue["Version"] = "1.0.0";
46 asyncResp->res.jsonValue["RootOfTrustCollection"]["@odata.id"] =
Nan Zhoucd027592022-07-03 05:18:29 +000047 "/google/v1/RootOfTrustCollection";
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -070048}
49
Nan Zhou30aacdd2022-07-03 05:02:36 +000050inline void handleRootOfTrustCollectionGet(
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -070051 const crow::Request& /*req*/,
52 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
53{
Nan Zhoucd027592022-07-03 05:18:29 +000054 asyncResp->res.jsonValue["@odata.id"] = "/google/v1/RootOfTrustCollection";
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -070055 asyncResp->res.jsonValue["@odata.type"] =
56 "#RootOfTrustCollection.RootOfTrustCollection";
George Liu7a1dbc42022-12-07 16:03:22 +080057 const std::array<std::string_view, 1> interfaces{
58 "xyz.openbmc_project.Control.Hoth"};
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -070059 redfish::collection_util::getCollectionMembers(
Willy Tuae9031f2022-09-27 05:48:07 +000060 asyncResp, boost::urls::url("/google/v1/RootOfTrustCollection"),
George Liu7a1dbc42022-12-07 16:03:22 +080061 interfaces, "/xyz/openbmc_project");
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -070062}
63
64// Helper struct to identify a resolved D-Bus object interface
65struct ResolvedEntity
66{
67 std::string id;
68 std::string service;
69 std::string object;
Nan Zhou16a55352022-07-03 05:06:08 +000070 std::string interface;
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -070071};
72
73using ResolvedEntityHandler = std::function<void(
74 const std::string&, const std::shared_ptr<bmcweb::AsyncResp>&,
75 const ResolvedEntity&)>;
76
Nan Zhou30aacdd2022-07-03 05:02:36 +000077inline void hothGetSubtreeCallback(
78 const std::string& command,
79 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
80 const std::string& rotId, const ResolvedEntityHandler& entityHandler,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080081 const boost::system::error_code& ec,
Nan Zhou30aacdd2022-07-03 05:02:36 +000082 const dbus::utility::MapperGetSubTreeResponse& subtree)
83{
84 if (ec)
85 {
86 redfish::messages::internalError(asyncResp->res);
87 return;
88 }
Nan Zhou5600f022022-07-03 16:22:38 +000089 for (const auto& [path, services] : subtree)
Nan Zhou30aacdd2022-07-03 05:02:36 +000090 {
Nan Zhou5600f022022-07-03 16:22:38 +000091 sdbusplus::message::object_path objPath(path);
92 if (objPath.filename() != rotId || services.empty())
Nan Zhou30aacdd2022-07-03 05:02:36 +000093 {
94 continue;
95 }
96
Nan Zhoucd027592022-07-03 05:18:29 +000097 ResolvedEntity resolvedEntity = {
98 .id = rotId,
Nan Zhou5600f022022-07-03 16:22:38 +000099 .service = services[0].first,
100 .object = path,
Nan Zhoucd027592022-07-03 05:18:29 +0000101 .interface = "xyz.openbmc_project.Control.Hoth"};
Nan Zhou30aacdd2022-07-03 05:02:36 +0000102 entityHandler(command, asyncResp, resolvedEntity);
103 return;
104 }
105
106 // Couldn't find an object with that name. return an error
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800107 redfish::messages::resourceNotFound(asyncResp->res, "RootOfTrust", rotId);
Nan Zhou30aacdd2022-07-03 05:02:36 +0000108}
109
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -0700110inline void resolveRoT(const std::string& command,
111 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
112 const std::string& rotId,
113 ResolvedEntityHandler&& entityHandler)
114{
George Liu7a1dbc42022-12-07 16:03:22 +0800115 constexpr std::array<std::string_view, 1> hothIfaces = {
Nan Zhoucd027592022-07-03 05:18:29 +0000116 "xyz.openbmc_project.Control.Hoth"};
George Liue99073f2022-12-09 11:06:16 +0800117 dbus::utility::getSubTree(
118 "/xyz/openbmc_project", 0, hothIfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -0700119 [command, asyncResp, rotId, entityHandler{std::move(entityHandler)}](
George Liue99073f2022-12-09 11:06:16 +0800120 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -0700121 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400122 hothGetSubtreeCallback(command, asyncResp, rotId, entityHandler, ec,
123 subtree);
124 });
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -0700125}
126
127inline void populateRootOfTrustEntity(
128 const std::string& /*unused*/,
129 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
130 const ResolvedEntity& resolvedEntity)
131{
132 asyncResp->res.jsonValue["@odata.type"] = "#RootOfTrust.v1_0_0.RootOfTrust";
Ed Tanousef4c65b2023-04-24 15:28:50 -0700133 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
134 "/google/v1/RootOfTrustCollection/{}", resolvedEntity.id);
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -0700135
136 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
137 asyncResp->res.jsonValue["Id"] = resolvedEntity.id;
138 // Need to fix this later to a stabler property.
139 asyncResp->res.jsonValue["Name"] = resolvedEntity.id;
140 asyncResp->res.jsonValue["Description"] = "Google Root Of Trust";
141 asyncResp->res.jsonValue["Actions"]["#RootOfTrust.SendCommand"]["target"] =
142 "/google/v1/RootOfTrustCollection/" + resolvedEntity.id +
143 "/Actions/RootOfTrust.SendCommand";
144
145 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
146 resolvedEntity.id;
147 asyncResp->res.jsonValue["Location"]["PartLocation"]["LocationType"] =
148 "Embedded";
149}
150
Nan Zhou30aacdd2022-07-03 05:02:36 +0000151inline void
152 handleRootOfTrustGet(const crow::Request& /*req*/,
153 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
154 const std::string& param)
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -0700155{
Nan Zhou30aacdd2022-07-03 05:02:36 +0000156 std::string emptyCommand;
157 resolveRoT(emptyCommand, asyncResp, param, populateRootOfTrustEntity);
158}
159
160inline void
161 invocationCallback(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800162 const boost::system::error_code& ec,
Nan Zhou30aacdd2022-07-03 05:02:36 +0000163 const std::vector<uint8_t>& responseBytes)
164{
165 if (ec)
166 {
Ed Tanous62598e32023-07-17 17:06:25 -0700167 BMCWEB_LOG_ERROR("RootOfTrust.Actions.SendCommand failed: {}",
168 ec.message());
Nan Zhou30aacdd2022-07-03 05:02:36 +0000169 redfish::messages::internalError(asyncResp->res);
170 return;
171 }
172
173 asyncResp->res.jsonValue["CommandResponse"] =
174 bytesToHexString(responseBytes);
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -0700175}
176
177inline void
178 invokeRoTCommand(const std::string& command,
179 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
180 const ResolvedEntity& resolvedEntity)
181{
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -0700182 std::vector<uint8_t> bytes = hexStringToBytes(command);
183 if (bytes.empty())
184 {
Ed Tanous62598e32023-07-17 17:06:25 -0700185 BMCWEB_LOG_DEBUG("Invalid command: {}", command);
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -0700186 redfish::messages::actionParameterValueTypeError(command, "Command",
187 "SendCommand");
188 return;
189 }
190
191 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800192 [asyncResp{asyncResp}](const boost::system::error_code& ec,
Nan Zhou30aacdd2022-07-03 05:02:36 +0000193 const std::vector<uint8_t>& responseBytes) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400194 invocationCallback(asyncResp, ec, responseBytes);
195 },
Nan Zhou30aacdd2022-07-03 05:02:36 +0000196 resolvedEntity.service, resolvedEntity.object, resolvedEntity.interface,
197 "SendHostCommand", bytes);
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -0700198}
199
Nan Zhou30aacdd2022-07-03 05:02:36 +0000200inline void handleRoTSendCommandPost(
201 const crow::Request& request,
202 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
203 const std::string& rotId)
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -0700204{
205 std::string command;
206 if (!redfish::json_util::readJsonAction(request, asyncResp->res, "Command",
207 command))
208 {
Ed Tanous62598e32023-07-17 17:06:25 -0700209 BMCWEB_LOG_DEBUG("Missing property Command.");
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -0700210 redfish::messages::actionParameterMissing(asyncResp->res, "SendCommand",
211 "Command");
212 return;
213 }
214
215 resolveRoT(command, asyncResp, rotId, invokeRoTCommand);
216}
Feras Aldahlawi735ef6d2021-03-19 14:01:46 -0700217
218inline void requestRoutes(App& app)
219{
220 BMCWEB_ROUTE(app, "/google/v1/")
Nan Zhou30aacdd2022-07-03 05:02:36 +0000221 .methods(boost::beast::http::verb::get)(handleGoogleV1Get);
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -0700222
223 BMCWEB_ROUTE(app, "/google/v1/RootOfTrustCollection")
224 .privileges({{"ConfigureManager"}})
Nan Zhou30aacdd2022-07-03 05:02:36 +0000225 .methods(boost::beast::http::verb::get)(handleRootOfTrustCollectionGet);
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -0700226
227 BMCWEB_ROUTE(app, "/google/v1/RootOfTrustCollection/<str>")
228 .privileges({{"ConfigureManager"}})
Nan Zhou30aacdd2022-07-03 05:02:36 +0000229 .methods(boost::beast::http::verb::get)(handleRootOfTrustGet);
Vidya Satyamsetti4cee35e2022-04-21 14:53:44 -0700230
231 BMCWEB_ROUTE(
232 app,
233 "/google/v1/RootOfTrustCollection/<str>/Actions/RootOfTrust.SendCommand")
234 .privileges({{"ConfigureManager"}})
Nan Zhou30aacdd2022-07-03 05:02:36 +0000235 .methods(boost::beast::http::verb::post)(handleRoTSendCommandPost);
Feras Aldahlawi735ef6d2021-03-19 14:01:46 -0700236}
237
238} // namespace google_api
239} // namespace crow