blob: 0aacc47d11210e76d5559cfdd787b42b0c3a2cc9 [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"
Ed Tanousd98a2f92025-02-06 17:36:31 -080010#include "io_context_singleton.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080011#include "logging.hpp"
Ed Tanous2c6ffdb2023-06-28 11:28:38 -070012#include "ossl_random.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080013
Ed Tanousd7857202025-01-28 15:32:26 -080014#include <boost/asio/error.hpp>
15#include <boost/asio/steady_timer.hpp>
16#include <boost/beast/http/status.hpp>
17#include <boost/beast/http/verb.hpp>
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include <sdbusplus/bus/match.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080019#include <sdbusplus/message.hpp>
20#include <sdbusplus/message/native_types.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050021
Ed Tanousd7857202025-01-28 15:32:26 -080022#include <algorithm>
23#include <chrono>
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
Patrick Williams504af5a2025-02-03 14:29:03 -050038inline void uploadImageHandler(
39 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 }
49 // Make this const static so it survives outside this method
Ed Tanousd98a2f92025-02-06 17:36:31 -080050 static boost::asio::steady_timer timeout(getIoContext(),
Ed Tanous271584a2019-07-09 16:24:22 -070051 std::chrono::seconds(5));
Ed Tanous1abe55e2018-09-05 08:30:59 -070052
Ed Tanous271584a2019-07-09 16:24:22 -070053 timeout.expires_after(std::chrono::seconds(15));
Ed Tanous1abe55e2018-09-05 08:30:59 -070054
zhanghch058d1b46d2021-04-01 11:18:24 +080055 auto timeoutHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanousc3ee5222018-05-01 12:58:27 -070056 fwUpdateMatcher = nullptr;
Ed Tanous23e64202020-09-15 19:21:30 -070057 if (ec == boost::asio::error::operation_aborted)
Ed Tanous1abe55e2018-09-05 08:30:59 -070058 {
59 // expected, we were canceled before the timer completed.
60 return;
61 }
Ed Tanous62598e32023-07-17 17:06:25 -070062 BMCWEB_LOG_ERROR("Timed out waiting for Version interface");
Ed Tanousc3ee5222018-05-01 12:58:27 -070063
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 if (ec)
65 {
Ed Tanous62598e32023-07-17 17:06:25 -070066 BMCWEB_LOG_ERROR("Async_wait failed {}", ec);
Ed Tanous1abe55e2018-09-05 08:30:59 -070067 return;
68 }
69
zhanghch058d1b46d2021-04-01 11:18:24 +080070 asyncResp->res.result(boost::beast::http::status::bad_request);
Ed Tanous14766872022-03-15 10:44:42 -070071 asyncResp->res.jsonValue["data"]["description"] =
72 "Version already exists or failed to be extracted";
73 asyncResp->res.jsonValue["message"] = "400 Bad Request";
74 asyncResp->res.jsonValue["status"] = "error";
Lei YU9f898f82019-03-08 16:52:10 +080075 };
Ed Tanous1abe55e2018-09-05 08:30:59 -070076
Patrick Williams59d494e2022-07-22 19:26:55 -050077 std::function<void(sdbusplus::message_t&)> callback =
78 [asyncResp](sdbusplus::message_t& m) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040079 BMCWEB_LOG_DEBUG("Match fired");
Ed Tanous1abe55e2018-09-05 08:30:59 -070080
Patrick Williamsbd79bce2024-08-16 15:22:20 -040081 sdbusplus::message::object_path path;
82 dbus::utility::DBusInterfacesMap interfaces;
83 m.read(path, interfaces);
Matt Spinlerc9008502019-01-21 12:21:25 -060084
Patrick Williamsbd79bce2024-08-16 15:22:20 -040085 if (std::ranges::find_if(interfaces, [](const auto& i) {
86 return i.first == "xyz.openbmc_project.Software.Version";
87 }) != interfaces.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -070088 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040089 timeout.cancel();
90 std::string leaf = path.filename();
91 if (leaf.empty())
92 {
93 leaf = path.str;
94 }
Ed Tanous002d39b2022-05-31 08:59:27 -070095
Patrick Williamsbd79bce2024-08-16 15:22:20 -040096 asyncResp->res.jsonValue["data"] = leaf;
97 asyncResp->res.jsonValue["message"] = "200 OK";
98 asyncResp->res.jsonValue["status"] = "ok";
99 BMCWEB_LOG_DEBUG("ending response");
100 fwUpdateMatcher = nullptr;
101 }
102 };
Patrick Williams59d494e2022-07-22 19:26:55 -0500103 fwUpdateMatcher = std::make_unique<sdbusplus::bus::match_t>(
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104 *crow::connections::systemBus,
105 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
Matt Spinlerc9008502019-01-21 12:21:25 -0600106 "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
Ed Tanous1abe55e2018-09-05 08:30:59 -0700107 callback);
108
Ed Tanous2c6ffdb2023-06-28 11:28:38 -0700109 std::string filepath("/tmp/images/" + bmcweb::getRandomUUID());
Ed Tanous62598e32023-07-17 17:06:25 -0700110 BMCWEB_LOG_DEBUG("Writing file to {}", filepath);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111 std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
112 std::ofstream::trunc);
Ed Tanous33c6b582023-02-14 15:05:48 -0800113 out << req.body();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700114 out.close();
Lei YU9f898f82019-03-08 16:52:10 +0800115 timeout.async_wait(timeoutHandler);
Ed Tanousc3ee5222018-05-01 12:58:27 -0700116}
117
Ed Tanous23a21a12020-07-25 04:45:05 +0000118inline void requestRoutes(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119{
120 BMCWEB_ROUTE(app, "/upload/image/<str>")
Ed Tanous432a8902021-06-14 15:28:56 -0700121 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700122 .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800123 [](const crow::Request& req,
124 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
125 const std::string&) { uploadImageHandler(req, asyncResp); });
Ed Tanousc3ee5222018-05-01 12:58:27 -0700126
Ed Tanous1abe55e2018-09-05 08:30:59 -0700127 BMCWEB_ROUTE(app, "/upload/image")
Ed Tanous432a8902021-06-14 15:28:56 -0700128 .privileges({{"ConfigureComponents", "ConfigureManager"}})
Ed Tanousb41187f2019-10-24 16:30:02 -0700129 .methods(boost::beast::http::verb::post, boost::beast::http::verb::put)(
zhanghch058d1b46d2021-04-01 11:18:24 +0800130 [](const crow::Request& req,
131 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400132 uploadImageHandler(req, asyncResp);
133 });
Ed Tanousc3ee5222018-05-01 12:58:27 -0700134}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700135} // namespace image_upload
136} // namespace crow