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