Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 3 | #include "app.hpp" |
| 4 | #include "dbus_singleton.hpp" |
| 5 | #include "dbus_utility.hpp" |
Ed Tanous | 2c6ffdb | 2023-06-28 11:28:38 -0700 | [diff] [blame] | 6 | #include "ossl_random.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 7 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 8 | #include <sdbusplus/bus/match.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 9 | |
| 10 | #include <cstdio> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 11 | #include <fstream> |
| 12 | #include <memory> |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 13 | #include <ranges> |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 14 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 15 | namespace crow |
| 16 | { |
| 17 | namespace image_upload |
| 18 | { |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 19 | |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 20 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 21 | static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateMatcher; |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 22 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 23 | inline void |
| 24 | uploadImageHandler(const crow::Request& req, |
| 25 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 26 | { |
| 27 | // Only allow one FW update at a time |
| 28 | if (fwUpdateMatcher != nullptr) |
| 29 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 30 | asyncResp->res.addHeader("Retry-After", "30"); |
| 31 | asyncResp->res.result(boost::beast::http::status::service_unavailable); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 32 | return; |
| 33 | } |
Ed Tanous | 8e8245d | 2024-04-11 22:21:38 -0700 | [diff] [blame] | 34 | if (req.ioService == nullptr) |
| 35 | { |
| 36 | asyncResp->res.result( |
| 37 | boost::beast::http::status::internal_server_error); |
| 38 | return; |
| 39 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 40 | // Make this const static so it survives outside this method |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 41 | static boost::asio::steady_timer timeout(*req.ioService, |
| 42 | std::chrono::seconds(5)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 43 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 44 | timeout.expires_after(std::chrono::seconds(15)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 45 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 46 | auto timeoutHandler = [asyncResp](const boost::system::error_code& ec) { |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 47 | fwUpdateMatcher = nullptr; |
Ed Tanous | 23e6420 | 2020-09-15 19:21:30 -0700 | [diff] [blame] | 48 | if (ec == boost::asio::error::operation_aborted) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 49 | { |
| 50 | // expected, we were canceled before the timer completed. |
| 51 | return; |
| 52 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 53 | BMCWEB_LOG_ERROR("Timed out waiting for Version interface"); |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 54 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | if (ec) |
| 56 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 57 | BMCWEB_LOG_ERROR("Async_wait failed {}", ec); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 58 | return; |
| 59 | } |
| 60 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 61 | asyncResp->res.result(boost::beast::http::status::bad_request); |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 62 | asyncResp->res.jsonValue["data"]["description"] = |
| 63 | "Version already exists or failed to be extracted"; |
| 64 | asyncResp->res.jsonValue["message"] = "400 Bad Request"; |
| 65 | asyncResp->res.jsonValue["status"] = "error"; |
Lei YU | 9f898f8 | 2019-03-08 16:52:10 +0800 | [diff] [blame] | 66 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 67 | |
Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 68 | std::function<void(sdbusplus::message_t&)> callback = |
| 69 | [asyncResp](sdbusplus::message_t& m) { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 70 | BMCWEB_LOG_DEBUG("Match fired"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 71 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 72 | sdbusplus::message::object_path path; |
Michael Shen | 80f79a4 | 2023-08-24 13:41:53 +0000 | [diff] [blame] | 73 | dbus::utility::DBusInterfacesMap interfaces; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 74 | m.read(path, interfaces); |
Matt Spinler | c900850 | 2019-01-21 12:21:25 -0600 | [diff] [blame] | 75 | |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 76 | if (std::ranges::find_if(interfaces, [](const auto& i) { |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 77 | return i.first == "xyz.openbmc_project.Software.Version"; |
| 78 | }) != interfaces.end()) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 79 | { |
| 80 | timeout.cancel(); |
| 81 | std::string leaf = path.filename(); |
| 82 | if (leaf.empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 83 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 84 | leaf = path.str; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 85 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 86 | |
| 87 | asyncResp->res.jsonValue["data"] = leaf; |
| 88 | asyncResp->res.jsonValue["message"] = "200 OK"; |
| 89 | asyncResp->res.jsonValue["status"] = "ok"; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 90 | BMCWEB_LOG_DEBUG("ending response"); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 91 | fwUpdateMatcher = nullptr; |
| 92 | } |
| 93 | }; |
Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 94 | fwUpdateMatcher = std::make_unique<sdbusplus::bus::match_t>( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 95 | *crow::connections::systemBus, |
| 96 | "interface='org.freedesktop.DBus.ObjectManager',type='signal'," |
Matt Spinler | c900850 | 2019-01-21 12:21:25 -0600 | [diff] [blame] | 97 | "member='InterfacesAdded',path='/xyz/openbmc_project/software'", |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 98 | callback); |
| 99 | |
Ed Tanous | 2c6ffdb | 2023-06-28 11:28:38 -0700 | [diff] [blame] | 100 | std::string filepath("/tmp/images/" + bmcweb::getRandomUUID()); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 101 | BMCWEB_LOG_DEBUG("Writing file to {}", filepath); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 102 | std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary | |
| 103 | std::ofstream::trunc); |
Ed Tanous | 33c6b58 | 2023-02-14 15:05:48 -0800 | [diff] [blame] | 104 | out << req.body(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 105 | out.close(); |
Lei YU | 9f898f8 | 2019-03-08 16:52:10 +0800 | [diff] [blame] | 106 | timeout.async_wait(timeoutHandler); |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 109 | inline void requestRoutes(App& app) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 110 | { |
| 111 | BMCWEB_ROUTE(app, "/upload/image/<str>") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 112 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 113 | .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 114 | [](const crow::Request& req, |
| 115 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 116 | const std::string&) { uploadImageHandler(req, asyncResp); }); |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 117 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 118 | BMCWEB_ROUTE(app, "/upload/image") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 119 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 120 | .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 121 | [](const crow::Request& req, |
| 122 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 123 | uploadImageHandler(req, asyncResp); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 124 | }); |
Ed Tanous | c3ee522 | 2018-05-01 12:58:27 -0700 | [diff] [blame] | 125 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 126 | } // namespace image_upload |
| 127 | } // namespace crow |