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