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