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