xref: /openbmc/bmcweb/redfish-core/lib/bios.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4 
5 #include "bmcweb_config.h"
6 
7 #include "app.hpp"
8 #include "async_resp.hpp"
9 #include "dbus_singleton.hpp"
10 #include "error_messages.hpp"
11 #include "http_request.hpp"
12 #include "logging.hpp"
13 #include "query.hpp"
14 #include "registries/privilege_registry.hpp"
15 #include "utils/sw_utils.hpp"
16 
17 #include <boost/beast/http/verb.hpp>
18 
19 #include <format>
20 #include <functional>
21 #include <memory>
22 #include <string>
23 
24 namespace redfish
25 {
26 /**
27  * BiosService class supports handle get method for bios.
28  */
29 inline void
handleBiosServiceGet(crow::App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & systemName)30     handleBiosServiceGet(crow::App& app, const crow::Request& req,
31                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
32                          const std::string& systemName)
33 {
34     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
35     {
36         return;
37     }
38     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
39     {
40         // Option currently returns no systems.  TBD
41         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
42                                    systemName);
43         return;
44     }
45     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
46     {
47         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
48                                    systemName);
49         return;
50     }
51     asyncResp->res.jsonValue["@odata.id"] = std::format(
52         "/redfish/v1/Systems/{}/Bios", BMCWEB_REDFISH_SYSTEM_URI_NAME);
53     asyncResp->res.jsonValue["@odata.type"] = "#Bios.v1_1_0.Bios";
54     asyncResp->res.jsonValue["Name"] = "BIOS Configuration";
55     asyncResp->res.jsonValue["Description"] = "BIOS Configuration Service";
56     asyncResp->res.jsonValue["Id"] = "BIOS";
57     asyncResp->res.jsonValue["Actions"]["#Bios.ResetBios"]["target"] =
58         std::format("/redfish/v1/Systems/{}/Bios/Actions/Bios.ResetBios",
59                     BMCWEB_REDFISH_SYSTEM_URI_NAME);
60 
61     // Get the ActiveSoftwareImage and SoftwareImages
62     sw_util::populateSoftwareInformation(asyncResp, sw_util::biosPurpose, "",
63                                          true);
64 }
65 
requestRoutesBiosService(App & app)66 inline void requestRoutesBiosService(App& app)
67 {
68     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Bios/")
69         .privileges(redfish::privileges::getBios)
70         .methods(boost::beast::http::verb::get)(
71             std::bind_front(handleBiosServiceGet, std::ref(app)));
72 }
73 
74 /**
75  * BiosReset class supports handle POST method for Reset bios.
76  * The class retrieves and sends data directly to D-Bus.
77  *
78  * Function handles POST method request.
79  * Analyzes POST body message before sends Reset request data to D-Bus.
80  */
81 inline void
handleBiosResetPost(crow::App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & systemName)82     handleBiosResetPost(crow::App& app, const crow::Request& req,
83                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
84                         const std::string& systemName)
85 {
86     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
87     {
88         return;
89     }
90 
91     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
92     {
93         // Option currently returns no systems.  TBD
94         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
95                                    systemName);
96         return;
97     }
98 
99     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
100     {
101         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
102                                    systemName);
103         return;
104     }
105 
106     crow::connections::systemBus->async_method_call(
107         [asyncResp](const boost::system::error_code& ec) {
108             if (ec)
109             {
110                 BMCWEB_LOG_ERROR("Failed to reset bios: {}", ec);
111                 messages::internalError(asyncResp->res);
112                 return;
113             }
114         },
115         "org.open_power.Software.Host.Updater", "/xyz/openbmc_project/software",
116         "xyz.openbmc_project.Common.FactoryReset", "Reset");
117 }
118 
requestRoutesBiosReset(App & app)119 inline void requestRoutesBiosReset(App& app)
120 {
121     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Bios/Actions/Bios.ResetBios/")
122         .privileges(redfish::privileges::postBios)
123         .methods(boost::beast::http::verb::post)(
124             std::bind_front(handleBiosResetPost, std::ref(app)));
125 }
126 
127 } // namespace redfish
128