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