blob: 6ef8951ed6004ace49cee094328ffb197af309f0 [file] [log] [blame]
Alexander Hansencc372352025-01-14 14:15:39 +01001#include "software_update.hpp"
2
3#include "device.hpp"
4#include "software.hpp"
5
6#include <phosphor-logging/elog-errors.hpp>
7#include <phosphor-logging/elog.hpp>
8#include <phosphor-logging/lg2.hpp>
9#include <sdbusplus/async/context.hpp>
10#include <xyz/openbmc_project/Software/Update/aserver.hpp>
11
12PHOSPHOR_LOG2_USING;
13
14using Unavailable = sdbusplus::xyz::openbmc_project::Common::Error::Unavailable;
15
16using namespace phosphor::logging;
17using namespace phosphor::software::update;
18using namespace phosphor::software::device;
19using namespace phosphor::software;
20
21namespace SoftwareLogging = phosphor::logging::xyz::openbmc_project::software;
22namespace SoftwareErrors =
23 sdbusplus::error::xyz::openbmc_project::software::image;
24
25SoftwareUpdate::SoftwareUpdate(
26 sdbusplus::async::context& ctx, const char* path, Software& software,
27 const std::set<RequestedApplyTimes>& allowedApplyTimes) :
28 sdbusplus::aserver::xyz::openbmc_project::software::Update<SoftwareUpdate>(
29 ctx, path),
30 software(software), allowedApplyTimes(allowedApplyTimes)
Alexander Hansen8b4ab1e2025-04-10 14:25:53 +020031{
32 emit_added();
33}
34
35SoftwareUpdate::~SoftwareUpdate()
36{
37 emit_removed();
38}
Alexander Hansencc372352025-01-14 14:15:39 +010039
40auto SoftwareUpdate::method_call(start_update_t /*unused*/, auto image,
41 auto applyTime)
42 -> sdbusplus::async::task<start_update_t::return_type>
43{
44 debug("Requesting Image update with {FD}", "FD", image.fd);
45
46 Device& device = software.parentDevice;
47
48 if (device.updateInProgress)
49 {
50 error("An update is already in progress, cannot update.");
Alexander Hansenf9cfdca2025-08-08 09:39:00 +020051 elog<Unavailable>();
Alexander Hansencc372352025-01-14 14:15:39 +010052 }
53
54 device.updateInProgress = true;
55
56 // check if the apply time is allowed by our device
57 if (!allowedApplyTimes.contains(applyTime))
58 {
59 error(
60 "the selected apply time {APPLYTIME} is not allowed by the device",
61 "APPLYTIME", applyTime);
62 device.updateInProgress = false;
Alexander Hansenf9cfdca2025-08-08 09:39:00 +020063 elog<sdbusplus::error::xyz::openbmc_project::software::update::
64 Incompatible>();
Alexander Hansencc372352025-01-14 14:15:39 +010065 }
66
67 debug("started asynchronous update with fd {FD}", "FD", image.fd);
68
69 int imageDup = dup(image.fd);
70
71 if (imageDup < 0)
72 {
73 error("ERROR calling dup on fd: {ERR}", "ERR", strerror(errno));
74 device.updateInProgress = false;
75 co_return software.objectPath;
76 }
77
78 debug("starting async update with FD: {FD}\n", "FD", imageDup);
79
80 std::unique_ptr<Software> softwareInstance =
81 std::make_unique<Software>(ctx, device);
82
83 softwareInstance->setActivation(ActivationInterface::Activations::NotReady);
84
85 std::string newObjPath = softwareInstance->objectPath;
86
Alexander Hansencc372352025-01-14 14:15:39 +010087 ctx.spawn(
88 [](Device& device, int imageDup, RequestedApplyTimes applyTime,
89 std::unique_ptr<Software> swupdate) -> sdbusplus::async::task<> {
90 co_await device.startUpdateAsync(imageDup, applyTime,
91 std::move(swupdate));
92 device.updateInProgress = false;
93 close(imageDup);
94 co_return;
95 }(device, imageDup, applyTime, std::move(softwareInstance)));
Alexander Hansencc372352025-01-14 14:15:39 +010096
97 // We need the object path for the new software here.
98 // It must be the same as constructed during the update process.
99 // This is so that bmcweb and redfish clients can keep track of the update
100 // process.
101 co_return newObjPath;
102}
103
104auto SoftwareUpdate::get_property(allowed_apply_times_t /*unused*/) const
105{
106 return allowedApplyTimes;
107}