blob: 128b384682ad4bf1296e10fcb74fac8245a519c9 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanousc3ee5222018-05-01 12:58:27 -07003#pragma once
4
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08005#include "app.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08006#include "async_resp.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08007#include "dbus_singleton.hpp"
8#include "dbus_utility.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08009#include "http_request.hpp"
10#include "logging.hpp"
Ed Tanous2c6ffdb2023-06-28 11:28:38 -070011#include "ossl_random.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080012
Ed Tanousd7857202025-01-28 15:32:26 -080013#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 Tanous3ccb3ad2023-01-13 17:40:03 -080017#include <sdbusplus/bus/match.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080018#include <sdbusplus/message.hpp>
19#include <sdbusplus/message/native_types.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050020
Ed Tanousd7857202025-01-28 15:32:26 -080021#include <algorithm>
22#include <chrono>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050023#include <cstdio>
Ed Tanous1abe55e2018-09-05 08:30:59 -070024#include <fstream>
Ed Tanousd7857202025-01-28 15:32:26 -080025#include <functional>
Ed Tanous1abe55e2018-09-05 08:30:59 -070026#include <memory>
Ed Tanous3544d2a2023-08-06 18:12:20 -070027#include <ranges>
Ed Tanousd7857202025-01-28 15:32:26 -080028#include <string>
Ed Tanousc3ee5222018-05-01 12:58:27 -070029
Ed Tanous1abe55e2018-09-05 08:30:59 -070030namespace crow
31{
32namespace image_upload
33{
Ed Tanousc3ee5222018-05-01 12:58:27 -070034
Ed Tanouscf9e4172022-12-21 09:30:16 -080035// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Patrick Williams59d494e2022-07-22 19:26:55 -050036static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateMatcher;
Ed Tanousc3ee5222018-05-01 12:58:27 -070037
zhanghch058d1b46d2021-04-01 11:18:24 +080038inline void
39 uploadImageHandler(const crow::Request& req,
40 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -070041{
42 // Only allow one FW update at a time
43 if (fwUpdateMatcher != nullptr)
44 {
zhanghch058d1b46d2021-04-01 11:18:24 +080045 asyncResp->res.addHeader("Retry-After", "30");
46 asyncResp->res.result(boost::beast::http::status::service_unavailable);
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 return;
48 }
Ed Tanous8e8245d2024-04-11 22:21:38 -070049 if (req.ioService == nullptr)
50 {
51 asyncResp->res.result(
52 boost::beast::http::status::internal_server_error);
53 return;
54 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070055 // Make this const static so it survives outside this method
Ed Tanous271584a2019-07-09 16:24:22 -070056 static boost::asio::steady_timer timeout(*req.ioService,
57 std::chrono::seconds(5));
Ed Tanous1abe55e2018-09-05 08:30:59 -070058
Ed Tanous271584a2019-07-09 16:24:22 -070059 timeout.expires_after(std::chrono::seconds(15));
Ed Tanous1abe55e2018-09-05 08:30:59 -070060
zhanghch058d1b46d2021-04-01 11:18:24 +080061 auto timeoutHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanousc3ee5222018-05-01 12:58:27 -070062 fwUpdateMatcher = nullptr;
Ed Tanous23e64202020-09-15 19:21:30 -070063 if (ec == boost::asio::error::operation_aborted)
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 {
65 // expected, we were canceled before the timer completed.
66 return;
67 }
Ed Tanous62598e32023-07-17 17:06:25 -070068 BMCWEB_LOG_ERROR("Timed out waiting for Version interface");
Ed Tanousc3ee5222018-05-01 12:58:27 -070069
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 if (ec)
71 {
Ed Tanous62598e32023-07-17 17:06:25 -070072 BMCWEB_LOG_ERROR("Async_wait failed {}", ec);
Ed Tanous1abe55e2018-09-05 08:30:59 -070073 return;
74 }
75
zhanghch058d1b46d2021-04-01 11:18:24 +080076 asyncResp->res.result(boost::beast::http::status::bad_request);
Ed Tanous14766872022-03-15 10:44:42 -070077 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 YU9f898f82019-03-08 16:52:10 +080081 };
Ed Tanous1abe55e2018-09-05 08:30:59 -070082
Patrick Williams59d494e2022-07-22 19:26:55 -050083 std::function<void(sdbusplus::message_t&)> callback =
84 [asyncResp](sdbusplus::message_t& m) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040085 BMCWEB_LOG_DEBUG("Match fired");
Ed Tanous1abe55e2018-09-05 08:30:59 -070086
Patrick Williamsbd79bce2024-08-16 15:22:20 -040087 sdbusplus::message::object_path path;
88 dbus::utility::DBusInterfacesMap interfaces;
89 m.read(path, interfaces);
Matt Spinlerc9008502019-01-21 12:21:25 -060090
Patrick Williamsbd79bce2024-08-16 15:22:20 -040091 if (std::ranges::find_if(interfaces, [](const auto& i) {
92 return i.first == "xyz.openbmc_project.Software.Version";
93 }) != interfaces.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -070094 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040095 timeout.cancel();
96 std::string leaf = path.filename();
97 if (leaf.empty())
98 {
99 leaf = path.str;
100 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700101
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400102 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 Williams59d494e2022-07-22 19:26:55 -0500109 fwUpdateMatcher = std::make_unique<sdbusplus::bus::match_t>(
Ed Tanous1abe55e2018-09-05 08:30:59 -0700110 *crow::connections::systemBus,
111 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
Matt Spinlerc9008502019-01-21 12:21:25 -0600112 "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
Ed Tanous1abe55e2018-09-05 08:30:59 -0700113 callback);
114
Ed Tanous2c6ffdb2023-06-28 11:28:38 -0700115 std::string filepath("/tmp/images/" + bmcweb::getRandomUUID());
Ed Tanous62598e32023-07-17 17:06:25 -0700116 BMCWEB_LOG_DEBUG("Writing file to {}", filepath);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700117 std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
118 std::ofstream::trunc);
Ed Tanous33c6b582023-02-14 15:05:48 -0800119 out << req.body();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700120 out.close();
Lei YU9f898f82019-03-08 16:52:10 +0800121 timeout.async_wait(timeoutHandler);
Ed Tanousc3ee5222018-05-01 12:58:27 -0700122}
123
Ed Tanous23a21a12020-07-25 04:45:05 +0000124inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700125{
126 BMCWEB_ROUTE(app, "/upload/image/<str>")
Ed Tanous432a8902021-06-14 15:28:56 -0700127 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700128 .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800129 [](const crow::Request& req,
130 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
131 const std::string&) { uploadImageHandler(req, asyncResp); });
Ed Tanousc3ee5222018-05-01 12:58:27 -0700132
Ed Tanous1abe55e2018-09-05 08:30:59 -0700133 BMCWEB_ROUTE(app, "/upload/image")
Ed Tanous432a8902021-06-14 15:28:56 -0700134 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700135 .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800136 [](const crow::Request& req,
137 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400138 uploadImageHandler(req, asyncResp);
139 });
Ed Tanousc3ee5222018-05-01 12:58:27 -0700140}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700141} // namespace image_upload
142} // namespace crow