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