xref: /openbmc/bmcweb/redfish-core/lib/bios.hpp (revision a529a6aa)
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"]["target"] =
44         std::format("/redfish/v1/Systems/{}/Bios/Actions/Bios.ResetBios",
45                     BMCWEB_REDFISH_SYSTEM_URI_NAME);
46 
47     // Get the ActiveSoftwareImage and SoftwareImages
48     sw_util::populateSoftwareInformation(asyncResp, sw_util::biosPurpose, "",
49                                          true);
50 }
51 
52 inline void requestRoutesBiosService(App& app)
53 {
54     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Bios/")
55         .privileges(redfish::privileges::getBios)
56         .methods(boost::beast::http::verb::get)(
57             std::bind_front(handleBiosServiceGet, std::ref(app)));
58 }
59 
60 /**
61  * BiosReset class supports handle POST method for Reset bios.
62  * The class retrieves and sends data directly to D-Bus.
63  *
64  * Function handles POST method request.
65  * Analyzes POST body message before sends Reset request data to D-Bus.
66  */
67 inline void
68     handleBiosResetPost(crow::App& app, const crow::Request& req,
69                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
70                         const std::string& systemName)
71 {
72     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
73     {
74         return;
75     }
76 
77     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
78     {
79         // Option currently returns no systems.  TBD
80         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
81                                    systemName);
82         return;
83     }
84 
85     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
86     {
87         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
88                                    systemName);
89         return;
90     }
91 
92     crow::connections::systemBus->async_method_call(
93         [asyncResp](const boost::system::error_code& ec) {
94         if (ec)
95         {
96             BMCWEB_LOG_ERROR("Failed to reset bios: {}", ec);
97             messages::internalError(asyncResp->res);
98             return;
99         }
100     },
101         "org.open_power.Software.Host.Updater", "/xyz/openbmc_project/software",
102         "xyz.openbmc_project.Common.FactoryReset", "Reset");
103 }
104 
105 inline void requestRoutesBiosReset(App& app)
106 {
107     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Bios/Actions/Bios.ResetBios/")
108         .privileges(redfish::privileges::postBios)
109         .methods(boost::beast::http::verb::post)(
110             std::bind_front(handleBiosResetPost, std::ref(app)));
111 }
112 
113 } // namespace redfish
114