blob: ef615cb3b8f0184008f668852b10097b0b787930 [file] [log] [blame]
Ed Tanousc3ee5222018-05-01 12:58:27 -07001#pragma once
2
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08003#include "app.hpp"
4#include "dbus_singleton.hpp"
5#include "dbus_utility.hpp"
Ed Tanous2c6ffdb2023-06-28 11:28:38 -07006#include "ossl_random.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08007
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08008#include <sdbusplus/bus/match.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05009
10#include <cstdio>
Ed Tanous1abe55e2018-09-05 08:30:59 -070011#include <fstream>
12#include <memory>
Ed Tanous3544d2a2023-08-06 18:12:20 -070013#include <ranges>
Ed Tanousc3ee5222018-05-01 12:58:27 -070014
Ed Tanous1abe55e2018-09-05 08:30:59 -070015namespace crow
16{
17namespace image_upload
18{
Ed Tanousc3ee5222018-05-01 12:58:27 -070019
Ed Tanouscf9e4172022-12-21 09:30:16 -080020// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Patrick Williams59d494e2022-07-22 19:26:55 -050021static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateMatcher;
Ed Tanousc3ee5222018-05-01 12:58:27 -070022
zhanghch058d1b46d2021-04-01 11:18:24 +080023inline void
24 uploadImageHandler(const crow::Request& req,
25 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -070026{
27 // Only allow one FW update at a time
28 if (fwUpdateMatcher != nullptr)
29 {
zhanghch058d1b46d2021-04-01 11:18:24 +080030 asyncResp->res.addHeader("Retry-After", "30");
31 asyncResp->res.result(boost::beast::http::status::service_unavailable);
Ed Tanous1abe55e2018-09-05 08:30:59 -070032 return;
33 }
34 // Make this const static so it survives outside this method
Ed Tanous271584a2019-07-09 16:24:22 -070035 static boost::asio::steady_timer timeout(*req.ioService,
36 std::chrono::seconds(5));
Ed Tanous1abe55e2018-09-05 08:30:59 -070037
Ed Tanous271584a2019-07-09 16:24:22 -070038 timeout.expires_after(std::chrono::seconds(15));
Ed Tanous1abe55e2018-09-05 08:30:59 -070039
zhanghch058d1b46d2021-04-01 11:18:24 +080040 auto timeoutHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanousc3ee5222018-05-01 12:58:27 -070041 fwUpdateMatcher = nullptr;
Ed Tanous23e64202020-09-15 19:21:30 -070042 if (ec == boost::asio::error::operation_aborted)
Ed Tanous1abe55e2018-09-05 08:30:59 -070043 {
44 // expected, we were canceled before the timer completed.
45 return;
46 }
Ed Tanous62598e32023-07-17 17:06:25 -070047 BMCWEB_LOG_ERROR("Timed out waiting for Version interface");
Ed Tanousc3ee5222018-05-01 12:58:27 -070048
Ed Tanous1abe55e2018-09-05 08:30:59 -070049 if (ec)
50 {
Ed Tanous62598e32023-07-17 17:06:25 -070051 BMCWEB_LOG_ERROR("Async_wait failed {}", ec);
Ed Tanous1abe55e2018-09-05 08:30:59 -070052 return;
53 }
54
zhanghch058d1b46d2021-04-01 11:18:24 +080055 asyncResp->res.result(boost::beast::http::status::bad_request);
Ed Tanous14766872022-03-15 10:44:42 -070056 asyncResp->res.jsonValue["data"]["description"] =
57 "Version already exists or failed to be extracted";
58 asyncResp->res.jsonValue["message"] = "400 Bad Request";
59 asyncResp->res.jsonValue["status"] = "error";
Lei YU9f898f82019-03-08 16:52:10 +080060 };
Ed Tanous1abe55e2018-09-05 08:30:59 -070061
Patrick Williams59d494e2022-07-22 19:26:55 -050062 std::function<void(sdbusplus::message_t&)> callback =
63 [asyncResp](sdbusplus::message_t& m) {
Ed Tanous62598e32023-07-17 17:06:25 -070064 BMCWEB_LOG_DEBUG("Match fired");
Ed Tanous1abe55e2018-09-05 08:30:59 -070065
Ed Tanous002d39b2022-05-31 08:59:27 -070066 sdbusplus::message::object_path path;
Michael Shen80f79a42023-08-24 13:41:53 +000067 dbus::utility::DBusInterfacesMap interfaces;
Ed Tanous002d39b2022-05-31 08:59:27 -070068 m.read(path, interfaces);
Matt Spinlerc9008502019-01-21 12:21:25 -060069
Ed Tanous3544d2a2023-08-06 18:12:20 -070070 if (std::ranges::find_if(interfaces, [](const auto& i) {
Patrick Williams5a39f772023-10-20 11:20:21 -050071 return i.first == "xyz.openbmc_project.Software.Version";
72 }) != interfaces.end())
Ed Tanous002d39b2022-05-31 08:59:27 -070073 {
74 timeout.cancel();
75 std::string leaf = path.filename();
76 if (leaf.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -070077 {
Ed Tanous002d39b2022-05-31 08:59:27 -070078 leaf = path.str;
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 }
Ed Tanous002d39b2022-05-31 08:59:27 -070080
81 asyncResp->res.jsonValue["data"] = leaf;
82 asyncResp->res.jsonValue["message"] = "200 OK";
83 asyncResp->res.jsonValue["status"] = "ok";
Ed Tanous62598e32023-07-17 17:06:25 -070084 BMCWEB_LOG_DEBUG("ending response");
Ed Tanous002d39b2022-05-31 08:59:27 -070085 fwUpdateMatcher = nullptr;
86 }
87 };
Patrick Williams59d494e2022-07-22 19:26:55 -050088 fwUpdateMatcher = std::make_unique<sdbusplus::bus::match_t>(
Ed Tanous1abe55e2018-09-05 08:30:59 -070089 *crow::connections::systemBus,
90 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
Matt Spinlerc9008502019-01-21 12:21:25 -060091 "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
Ed Tanous1abe55e2018-09-05 08:30:59 -070092 callback);
93
Ed Tanous2c6ffdb2023-06-28 11:28:38 -070094 std::string filepath("/tmp/images/" + bmcweb::getRandomUUID());
Ed Tanous62598e32023-07-17 17:06:25 -070095 BMCWEB_LOG_DEBUG("Writing file to {}", filepath);
Ed Tanous1abe55e2018-09-05 08:30:59 -070096 std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
97 std::ofstream::trunc);
Ed Tanous33c6b582023-02-14 15:05:48 -080098 out << req.body();
Ed Tanous1abe55e2018-09-05 08:30:59 -070099 out.close();
Lei YU9f898f82019-03-08 16:52:10 +0800100 timeout.async_wait(timeoutHandler);
Ed Tanousc3ee5222018-05-01 12:58:27 -0700101}
102
Ed Tanous23a21a12020-07-25 04:45:05 +0000103inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104{
105 BMCWEB_ROUTE(app, "/upload/image/<str>")
Ed Tanous432a8902021-06-14 15:28:56 -0700106 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700107 .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800108 [](const crow::Request& req,
109 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
110 const std::string&) { uploadImageHandler(req, asyncResp); });
Ed Tanousc3ee5222018-05-01 12:58:27 -0700111
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112 BMCWEB_ROUTE(app, "/upload/image")
Ed Tanous432a8902021-06-14 15:28:56 -0700113 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700114 .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800115 [](const crow::Request& req,
116 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700117 uploadImageHandler(req, asyncResp);
Patrick Williams5a39f772023-10-20 11:20:21 -0500118 });
Ed Tanousc3ee5222018-05-01 12:58:27 -0700119}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700120} // namespace image_upload
121} // namespace crow