1 /*
2 // Copyright (c) 2018 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 #pragma once
17 
18 #include "bmcweb_config.h"
19 
20 #include "app.hpp"
21 #include "dbus_utility.hpp"
22 #include "error_messages.hpp"
23 #include "generated/enums/update_service.hpp"
24 #include "multipart_parser.hpp"
25 #include "ossl_random.hpp"
26 #include "query.hpp"
27 #include "registries/privilege_registry.hpp"
28 #include "task.hpp"
29 #include "task_messages.hpp"
30 #include "utils/collection.hpp"
31 #include "utils/dbus_utils.hpp"
32 #include "utils/json_utils.hpp"
33 #include "utils/sw_utils.hpp"
34 
35 #include <boost/system/error_code.hpp>
36 #include <boost/url/format.hpp>
37 #include <sdbusplus/asio/property.hpp>
38 #include <sdbusplus/bus/match.hpp>
39 #include <sdbusplus/unpack_properties.hpp>
40 
41 #include <array>
42 #include <filesystem>
43 #include <optional>
44 #include <string>
45 #include <string_view>
46 
47 namespace redfish
48 {
49 
50 // Match signals added on software path
51 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
52 static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateMatcher;
53 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
54 static std::unique_ptr<sdbusplus::bus::match_t> fwUpdateErrorMatcher;
55 // Only allow one update at a time
56 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
57 static bool fwUpdateInProgress = false;
58 // Timer for software available
59 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
60 static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer;
61 
62 inline void cleanUp()
63 {
64     fwUpdateInProgress = false;
65     fwUpdateMatcher = nullptr;
66     fwUpdateErrorMatcher = nullptr;
67 }
68 
69 inline void activateImage(const std::string& objPath,
70                           const std::string& service)
71 {
72     BMCWEB_LOG_DEBUG("Activate image for {} {}", objPath, service);
73     sdbusplus::asio::setProperty(
74         *crow::connections::systemBus, service, objPath,
75         "xyz.openbmc_project.Software.Activation", "RequestedActivation",
76         "xyz.openbmc_project.Software.Activation.RequestedActivations.Active",
77         [](const boost::system::error_code& ec) {
78         if (ec)
79         {
80             BMCWEB_LOG_DEBUG("error_code = {}", ec);
81             BMCWEB_LOG_DEBUG("error msg = {}", ec.message());
82         }
83     });
84 }
85 
86 // Note that asyncResp can be either a valid pointer or nullptr. If nullptr
87 // then no asyncResp updates will occur
88 static void
89     softwareInterfaceAdded(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
90                            sdbusplus::message_t& m, task::Payload&& payload)
91 {
92     dbus::utility::DBusInterfacesMap interfacesProperties;
93 
94     sdbusplus::message::object_path objPath;
95 
96     m.read(objPath, interfacesProperties);
97 
98     BMCWEB_LOG_DEBUG("obj path = {}", objPath.str);
99     for (const auto& interface : interfacesProperties)
100     {
101         BMCWEB_LOG_DEBUG("interface = {}", interface.first);
102 
103         if (interface.first == "xyz.openbmc_project.Software.Activation")
104         {
105             // Retrieve service and activate
106             constexpr std::array<std::string_view, 1> interfaces = {
107                 "xyz.openbmc_project.Software.Activation"};
108             dbus::utility::getDbusObject(
109                 objPath.str, interfaces,
110                 [objPath, asyncResp, payload(std::move(payload))](
111                     const boost::system::error_code& ec,
112                     const std::vector<
113                         std::pair<std::string, std::vector<std::string>>>&
114                         objInfo) mutable {
115                 if (ec)
116                 {
117                     BMCWEB_LOG_DEBUG("error_code = {}", ec);
118                     BMCWEB_LOG_DEBUG("error msg = {}", ec.message());
119                     if (asyncResp)
120                     {
121                         messages::internalError(asyncResp->res);
122                     }
123                     cleanUp();
124                     return;
125                 }
126                 // Ensure we only got one service back
127                 if (objInfo.size() != 1)
128                 {
129                     BMCWEB_LOG_ERROR("Invalid Object Size {}", objInfo.size());
130                     if (asyncResp)
131                     {
132                         messages::internalError(asyncResp->res);
133                     }
134                     cleanUp();
135                     return;
136                 }
137                 // cancel timer only when
138                 // xyz.openbmc_project.Software.Activation interface
139                 // is added
140                 fwAvailableTimer = nullptr;
141 
142                 activateImage(objPath.str, objInfo[0].first);
143                 if (asyncResp)
144                 {
145                     std::shared_ptr<task::TaskData> task =
146                         task::TaskData::createTask(
147                             [](const boost::system::error_code& ec2,
148                                sdbusplus::message_t& msg,
149                                const std::shared_ptr<task::TaskData>&
150                                    taskData) {
151                         if (ec2)
152                         {
153                             return task::completed;
154                         }
155 
156                         std::string iface;
157                         dbus::utility::DBusPropertiesMap values;
158 
159                         std::string index = std::to_string(taskData->index);
160                         msg.read(iface, values);
161 
162                         if (iface == "xyz.openbmc_project.Software.Activation")
163                         {
164                             const std::string* state = nullptr;
165                             for (const auto& property : values)
166                             {
167                                 if (property.first == "Activation")
168                                 {
169                                     state = std::get_if<std::string>(
170                                         &property.second);
171                                     if (state == nullptr)
172                                     {
173                                         taskData->messages.emplace_back(
174                                             messages::internalError());
175                                         return task::completed;
176                                     }
177                                 }
178                             }
179 
180                             if (state == nullptr)
181                             {
182                                 return !task::completed;
183                             }
184 
185                             if (state->ends_with("Invalid") ||
186                                 state->ends_with("Failed"))
187                             {
188                                 taskData->state = "Exception";
189                                 taskData->status = "Warning";
190                                 taskData->messages.emplace_back(
191                                     messages::taskAborted(index));
192                                 return task::completed;
193                             }
194 
195                             if (state->ends_with("Staged"))
196                             {
197                                 taskData->state = "Stopping";
198                                 taskData->messages.emplace_back(
199                                     messages::taskPaused(index));
200 
201                                 // its staged, set a long timer to
202                                 // allow them time to complete the
203                                 // update (probably cycle the
204                                 // system) if this expires then
205                                 // task will be canceled
206                                 taskData->extendTimer(std::chrono::hours(5));
207                                 return !task::completed;
208                             }
209 
210                             if (state->ends_with("Active"))
211                             {
212                                 taskData->messages.emplace_back(
213                                     messages::taskCompletedOK(index));
214                                 taskData->state = "Completed";
215                                 return task::completed;
216                             }
217                         }
218                         else if (
219                             iface ==
220                             "xyz.openbmc_project.Software.ActivationProgress")
221                         {
222                             const uint8_t* progress = nullptr;
223                             for (const auto& property : values)
224                             {
225                                 if (property.first == "Progress")
226                                 {
227                                     progress =
228                                         std::get_if<uint8_t>(&property.second);
229                                     if (progress == nullptr)
230                                     {
231                                         taskData->messages.emplace_back(
232                                             messages::internalError());
233                                         return task::completed;
234                                     }
235                                 }
236                             }
237 
238                             if (progress == nullptr)
239                             {
240                                 return !task::completed;
241                             }
242                             taskData->percentComplete = *progress;
243                             taskData->messages.emplace_back(
244                                 messages::taskProgressChanged(index,
245                                                               *progress));
246 
247                             // if we're getting status updates it's
248                             // still alive, update timer
249                             taskData->extendTimer(std::chrono::minutes(5));
250                         }
251 
252                         // as firmware update often results in a
253                         // reboot, the task  may never "complete"
254                         // unless it is an error
255 
256                         return !task::completed;
257                     },
258                             "type='signal',interface='org.freedesktop.DBus.Properties',"
259                             "member='PropertiesChanged',path='" +
260                                 objPath.str + "'");
261                     task->startTimer(std::chrono::minutes(5));
262                     task->populateResp(asyncResp->res);
263                     task->payload.emplace(std::move(payload));
264                 }
265                 fwUpdateInProgress = false;
266             });
267 
268             break;
269         }
270     }
271 }
272 
273 inline void afterAvailbleTimerAsyncWait(
274     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
275     const boost::system::error_code& ec)
276 {
277     cleanUp();
278     if (ec == boost::asio::error::operation_aborted)
279     {
280         // expected, we were canceled before the timer completed.
281         return;
282     }
283     BMCWEB_LOG_ERROR("Timed out waiting for firmware object being created");
284     BMCWEB_LOG_ERROR("FW image may has already been uploaded to server");
285     if (ec)
286     {
287         BMCWEB_LOG_ERROR("Async_wait failed{}", ec);
288         return;
289     }
290     if (asyncResp)
291     {
292         redfish::messages::internalError(asyncResp->res);
293     }
294 }
295 
296 inline void
297     handleUpdateErrorType(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
298                           const std::string& url, const std::string& type)
299 {
300     if (type == "xyz.openbmc_project.Software.Image.Error.UnTarFailure")
301     {
302         redfish::messages::invalidUpload(asyncResp->res, url,
303                                          "Invalid archive");
304     }
305     else if (type ==
306              "xyz.openbmc_project.Software.Image.Error.ManifestFileFailure")
307     {
308         redfish::messages::invalidUpload(asyncResp->res, url,
309                                          "Invalid manifest");
310     }
311     else if (type == "xyz.openbmc_project.Software.Image.Error.ImageFailure")
312     {
313         redfish::messages::invalidUpload(asyncResp->res, url,
314                                          "Invalid image format");
315     }
316     else if (type == "xyz.openbmc_project.Software.Version.Error.AlreadyExists")
317     {
318         redfish::messages::invalidUpload(asyncResp->res, url,
319                                          "Image version already exists");
320 
321         redfish::messages::resourceAlreadyExists(
322             asyncResp->res, "UpdateService", "Version", "uploaded version");
323     }
324     else if (type == "xyz.openbmc_project.Software.Image.Error.BusyFailure")
325     {
326         redfish::messages::resourceExhaustion(asyncResp->res, url);
327     }
328     else if (type == "xyz.openbmc_project.Software.Version.Error.Incompatible")
329     {
330         redfish::messages::invalidUpload(asyncResp->res, url,
331                                          "Incompatible image version");
332     }
333     else if (type ==
334              "xyz.openbmc_project.Software.Version.Error.ExpiredAccessKey")
335     {
336         redfish::messages::invalidUpload(asyncResp->res, url,
337                                          "Update Access Key Expired");
338     }
339     else if (type ==
340              "xyz.openbmc_project.Software.Version.Error.InvalidSignature")
341     {
342         redfish::messages::invalidUpload(asyncResp->res, url,
343                                          "Invalid image signature");
344     }
345     else if (type ==
346                  "xyz.openbmc_project.Software.Image.Error.InternalFailure" ||
347              type == "xyz.openbmc_project.Software.Version.Error.HostFile")
348     {
349         BMCWEB_LOG_ERROR("Software Image Error type={}", type);
350         redfish::messages::internalError(asyncResp->res);
351     }
352     else
353     {
354         // Unrelated error types. Ignored
355         BMCWEB_LOG_INFO("Non-Software-related Error type={}. Ignored", type);
356         return;
357     }
358     // Clear the timer
359     fwAvailableTimer = nullptr;
360 }
361 
362 inline void
363     afterUpdateErrorMatcher(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
364                             const std::string& url, sdbusplus::message_t& m)
365 {
366     dbus::utility::DBusInterfacesMap interfacesProperties;
367     sdbusplus::message::object_path objPath;
368     m.read(objPath, interfacesProperties);
369     BMCWEB_LOG_DEBUG("obj path = {}", objPath.str);
370     for (const std::pair<std::string, dbus::utility::DBusPropertiesMap>&
371              interface : interfacesProperties)
372     {
373         if (interface.first == "xyz.openbmc_project.Logging.Entry")
374         {
375             for (const std::pair<std::string, dbus::utility::DbusVariantType>&
376                      value : interface.second)
377             {
378                 if (value.first != "Message")
379                 {
380                     continue;
381                 }
382                 const std::string* type =
383                     std::get_if<std::string>(&value.second);
384                 if (type == nullptr)
385                 {
386                     // if this was our message, timeout will cover it
387                     return;
388                 }
389                 handleUpdateErrorType(asyncResp, url, *type);
390             }
391         }
392     }
393 }
394 
395 // Note that asyncResp can be either a valid pointer or nullptr. If nullptr
396 // then no asyncResp updates will occur
397 inline void monitorForSoftwareAvailable(
398     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
399     const crow::Request& req, const std::string& url,
400     int timeoutTimeSeconds = 25)
401 {
402     // Only allow one FW update at a time
403     if (fwUpdateInProgress)
404     {
405         if (asyncResp)
406         {
407             messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
408         }
409         return;
410     }
411 
412     if (req.ioService == nullptr)
413     {
414         messages::internalError(asyncResp->res);
415         return;
416     }
417 
418     fwAvailableTimer =
419         std::make_unique<boost::asio::steady_timer>(*req.ioService);
420 
421     fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
422 
423     fwAvailableTimer->async_wait(
424         std::bind_front(afterAvailbleTimerAsyncWait, asyncResp));
425 
426     task::Payload payload(req);
427     auto callback = [asyncResp, payload](sdbusplus::message_t& m) mutable {
428         BMCWEB_LOG_DEBUG("Match fired");
429         softwareInterfaceAdded(asyncResp, m, std::move(payload));
430     };
431 
432     fwUpdateInProgress = true;
433 
434     fwUpdateMatcher = std::make_unique<sdbusplus::bus::match_t>(
435         *crow::connections::systemBus,
436         "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
437         "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
438         callback);
439 
440     fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match_t>(
441         *crow::connections::systemBus,
442         "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
443         "member='InterfacesAdded',"
444         "path='/xyz/openbmc_project/logging'",
445         std::bind_front(afterUpdateErrorMatcher, asyncResp, url));
446 }
447 
448 inline std::optional<boost::urls::url>
449     parseSimpleUpdateUrl(std::string imageURI,
450                          std::optional<std::string> transferProtocol,
451                          crow::Response& res)
452 {
453     if (imageURI.find("://") == std::string::npos)
454     {
455         if (imageURI.starts_with("/"))
456         {
457             messages::actionParameterValueTypeError(
458                 res, imageURI, "ImageURI", "UpdateService.SimpleUpdate");
459             return std::nullopt;
460         }
461         if (!transferProtocol)
462         {
463             messages::actionParameterValueTypeError(
464                 res, imageURI, "ImageURI", "UpdateService.SimpleUpdate");
465             return std::nullopt;
466         }
467         // OpenBMC currently only supports TFTP
468         if (*transferProtocol == "TFTP")
469         {
470             imageURI = "tftp://" + imageURI;
471         }
472         else
473         {
474             messages::actionParameterNotSupported(res, "TransferProtocol",
475                                                   *transferProtocol);
476             BMCWEB_LOG_ERROR("Request incorrect protocol parameter: {}",
477                              *transferProtocol);
478             return std::nullopt;
479         }
480     }
481 
482     boost::system::result<boost::urls::url> url =
483         boost::urls::parse_absolute_uri(imageURI);
484     if (!url)
485     {
486         messages::actionParameterValueTypeError(res, imageURI, "ImageURI",
487                                                 "UpdateService.SimpleUpdate");
488 
489         return std::nullopt;
490     }
491     url->normalize();
492 
493     if (url->scheme() == "tftp")
494     {
495         if (url->encoded_path().size() < 2)
496         {
497             messages::actionParameterNotSupported(res, "ImageURI",
498                                                   url->buffer());
499             return std::nullopt;
500         }
501     }
502     else
503     {
504         messages::actionParameterNotSupported(res, "ImageURI", imageURI);
505         return std::nullopt;
506     }
507 
508     if (url->encoded_path().empty())
509     {
510         messages::actionParameterValueTypeError(res, imageURI, "ImageURI",
511                                                 "UpdateService.SimpleUpdate");
512         return std::nullopt;
513     }
514 
515     return *url;
516 }
517 
518 inline void doTftpUpdate(const crow::Request& req,
519                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
520                          const boost::urls::url_view_base& url)
521 {
522 #ifndef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
523     messages::actionParameterNotSupported(asyncResp->res, "ImageURI",
524                                           url.buffer());
525     return;
526 #endif
527 
528     std::string path(url.encoded_path());
529     if (path.size() < 2)
530     {
531         messages::actionParameterNotSupported(asyncResp->res, "ImageURI",
532                                               url.buffer());
533         return;
534     }
535     // TFTP expects a path without a /
536     path.erase(0, 1);
537     std::string host(url.encoded_host_and_port());
538     BMCWEB_LOG_DEBUG("Server: {} File: {}", host, path);
539 
540     // Setup callback for when new software detected
541     // Give TFTP 10 minutes to complete
542     monitorForSoftwareAvailable(
543         asyncResp, req,
544         "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate", 600);
545 
546     // TFTP can take up to 10 minutes depending on image size and
547     // connection speed. Return to caller as soon as the TFTP operation
548     // has been started. The callback above will ensure the activate
549     // is started once the download has completed
550     redfish::messages::success(asyncResp->res);
551 
552     // Call TFTP service
553     crow::connections::systemBus->async_method_call(
554         [](const boost::system::error_code& ec) {
555         if (ec)
556         {
557             // messages::internalError(asyncResp->res);
558             cleanUp();
559             BMCWEB_LOG_DEBUG("error_code = {}", ec);
560             BMCWEB_LOG_DEBUG("error msg = {}", ec.message());
561         }
562         else
563         {
564             BMCWEB_LOG_DEBUG("Call to DownloaViaTFTP Success");
565         }
566     },
567         "xyz.openbmc_project.Software.Download",
568         "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP",
569         "DownloadViaTFTP", path, host);
570 }
571 
572 inline void handleUpdateServiceSimpleUpdateAction(
573     crow::App& app, const crow::Request& req,
574     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
575 {
576     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
577     {
578         return;
579     }
580 
581     std::optional<std::string> transferProtocol;
582     std::string imageURI;
583 
584     BMCWEB_LOG_DEBUG("Enter UpdateService.SimpleUpdate doPost");
585 
586     // User can pass in both TransferProtocol and ImageURI parameters or
587     // they can pass in just the ImageURI with the transfer protocol
588     // embedded within it.
589     // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
590     // 2) ImageURI:tftp://1.1.1.1/myfile.bin
591 
592     if (!json_util::readJsonAction(req, asyncResp->res, "TransferProtocol",
593                                    transferProtocol, "ImageURI", imageURI))
594     {
595         BMCWEB_LOG_DEBUG("Missing TransferProtocol or ImageURI parameter");
596         return;
597     }
598 
599     std::optional<boost::urls::url> url =
600         parseSimpleUpdateUrl(imageURI, transferProtocol, asyncResp->res);
601     if (!url)
602     {
603         return;
604     }
605     if (url->scheme() == "tftp")
606     {
607         doTftpUpdate(req, asyncResp, *url);
608     }
609     else
610     {
611         messages::actionParameterNotSupported(asyncResp->res, "ImageURI",
612                                               url->buffer());
613         return;
614     }
615 
616     BMCWEB_LOG_DEBUG("Exit UpdateService.SimpleUpdate doPost");
617 }
618 
619 inline void uploadImageFile(crow::Response& res, std::string_view body)
620 {
621     std::filesystem::path filepath("/tmp/images/" + bmcweb::getRandomUUID());
622 
623     BMCWEB_LOG_DEBUG("Writing file to {}", filepath.string());
624     std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
625                                     std::ofstream::trunc);
626     // set the permission of the file to 640
627     std::filesystem::perms permission = std::filesystem::perms::owner_read |
628                                         std::filesystem::perms::group_read;
629     std::filesystem::permissions(filepath, permission);
630     out << body;
631 
632     if (out.bad())
633     {
634         messages::internalError(res);
635         cleanUp();
636     }
637 }
638 
639 inline void setApplyTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
640                          const std::string& applyTime)
641 {
642     std::string applyTimeNewVal;
643     if (applyTime == "Immediate")
644     {
645         applyTimeNewVal =
646             "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.Immediate";
647     }
648     else if (applyTime == "OnReset")
649     {
650         applyTimeNewVal =
651             "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
652     }
653     else
654     {
655         BMCWEB_LOG_INFO(
656             "ApplyTime value is not in the list of acceptable values");
657         messages::propertyValueNotInList(asyncResp->res, applyTime,
658                                          "ApplyTime");
659         return;
660     }
661 
662     setDbusProperty(asyncResp, "xyz.openbmc_project.Settings",
663                     sdbusplus::message::object_path(
664                         "/xyz/openbmc_project/software/apply_time"),
665                     "xyz.openbmc_project.Software.ApplyTime",
666                     "RequestedApplyTime", "ApplyTime", applyTimeNewVal);
667 }
668 
669 inline void
670     updateMultipartContext(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
671                            const MultipartParser& parser)
672 {
673     const std::string* uploadData = nullptr;
674     std::optional<std::string> applyTime = "OnReset";
675     bool targetFound = false;
676     for (const FormPart& formpart : parser.mime_fields)
677     {
678         boost::beast::http::fields::const_iterator it =
679             formpart.fields.find("Content-Disposition");
680         if (it == formpart.fields.end())
681         {
682             BMCWEB_LOG_ERROR("Couldn't find Content-Disposition");
683             return;
684         }
685         BMCWEB_LOG_INFO("Parsing value {}", it->value());
686 
687         // The construction parameters of param_list must start with `;`
688         size_t index = it->value().find(';');
689         if (index == std::string::npos)
690         {
691             continue;
692         }
693 
694         for (const auto& param :
695              boost::beast::http::param_list{it->value().substr(index)})
696         {
697             if (param.first != "name" || param.second.empty())
698             {
699                 continue;
700             }
701 
702             if (param.second == "UpdateParameters")
703             {
704                 std::vector<std::string> targets;
705                 nlohmann::json content =
706                     nlohmann::json::parse(formpart.content);
707                 nlohmann::json::object_t* obj =
708                     content.get_ptr<nlohmann::json::object_t*>();
709                 if (obj == nullptr)
710                 {
711                     messages::propertyValueFormatError(asyncResp->res, targets,
712                                                        "UpdateParameters");
713                     return;
714                 }
715 
716                 if (!json_util::readJsonObject(
717                         *obj, asyncResp->res, "Targets", targets,
718                         "@Redfish.OperationApplyTime", applyTime))
719                 {
720                     return;
721                 }
722                 if (targets.size() != 1)
723                 {
724                     messages::propertyValueFormatError(asyncResp->res, targets,
725                                                        "Targets");
726                     return;
727                 }
728                 if (targets[0] != "/redfish/v1/Managers/bmc")
729                 {
730                     messages::propertyValueNotInList(asyncResp->res, targets[0],
731                                                      "Targets/0");
732                     return;
733                 }
734                 targetFound = true;
735             }
736             else if (param.second == "UpdateFile")
737             {
738                 uploadData = &(formpart.content);
739             }
740         }
741     }
742 
743     if (uploadData == nullptr)
744     {
745         BMCWEB_LOG_ERROR("Upload data is NULL");
746         messages::propertyMissing(asyncResp->res, "UpdateFile");
747         return;
748     }
749     if (!targetFound)
750     {
751         messages::propertyMissing(asyncResp->res, "targets");
752         return;
753     }
754 
755     setApplyTime(asyncResp, *applyTime);
756 
757     uploadImageFile(asyncResp->res, *uploadData);
758 }
759 
760 inline void
761     handleUpdateServicePost(App& app, const crow::Request& req,
762                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
763 {
764     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
765     {
766         return;
767     }
768     std::string_view contentType = req.getHeaderValue("Content-Type");
769 
770     BMCWEB_LOG_DEBUG("doPost: contentType={}", contentType);
771 
772     // Make sure that content type is application/octet-stream or
773     // multipart/form-data
774     if (bmcweb::asciiIEquals(contentType, "application/octet-stream"))
775     {
776         // Setup callback for when new software detected
777         monitorForSoftwareAvailable(asyncResp, req,
778                                     "/redfish/v1/UpdateService");
779 
780         uploadImageFile(asyncResp->res, req.body());
781     }
782     else if (contentType.starts_with("multipart/form-data"))
783     {
784         MultipartParser parser;
785 
786         // Setup callback for when new software detected
787         monitorForSoftwareAvailable(asyncResp, req,
788                                     "/redfish/v1/UpdateService");
789 
790         ParserError ec = parser.parse(req);
791         if (ec != ParserError::PARSER_SUCCESS)
792         {
793             // handle error
794             BMCWEB_LOG_ERROR("MIME parse failed, ec : {}",
795                              static_cast<int>(ec));
796             messages::internalError(asyncResp->res);
797             return;
798         }
799         updateMultipartContext(asyncResp, parser);
800     }
801     else
802     {
803         BMCWEB_LOG_DEBUG("Bad content type specified:{}", contentType);
804         asyncResp->res.result(boost::beast::http::status::bad_request);
805     }
806 }
807 
808 inline void
809     handleUpdateServiceGet(App& app, const crow::Request& req,
810                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
811 {
812     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
813     {
814         return;
815     }
816     asyncResp->res.jsonValue["@odata.type"] =
817         "#UpdateService.v1_11_1.UpdateService";
818     asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
819     asyncResp->res.jsonValue["Id"] = "UpdateService";
820     asyncResp->res.jsonValue["Description"] = "Service for Software Update";
821     asyncResp->res.jsonValue["Name"] = "Update Service";
822 
823     asyncResp->res.jsonValue["HttpPushUri"] =
824         "/redfish/v1/UpdateService/update";
825     asyncResp->res.jsonValue["MultipartHttpPushUri"] =
826         "/redfish/v1/UpdateService/update";
827 
828     // UpdateService cannot be disabled
829     asyncResp->res.jsonValue["ServiceEnabled"] = true;
830     asyncResp->res.jsonValue["FirmwareInventory"]["@odata.id"] =
831         "/redfish/v1/UpdateService/FirmwareInventory";
832     // Get the MaxImageSizeBytes
833     asyncResp->res.jsonValue["MaxImageSizeBytes"] = bmcwebHttpReqBodyLimitMb *
834                                                     1024 * 1024;
835 
836     // Update Actions object.
837     nlohmann::json& updateSvcSimpleUpdate =
838         asyncResp->res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
839     updateSvcSimpleUpdate["target"] =
840         "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
841 
842     nlohmann::json::array_t allowed;
843 
844 #ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
845     allowed.emplace_back(update_service::TransferProtocolType::TFTP);
846 #endif
847 
848     updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] =
849         std::move(allowed);
850 
851     // Get the current ApplyTime value
852     sdbusplus::asio::getProperty<std::string>(
853         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
854         "/xyz/openbmc_project/software/apply_time",
855         "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime",
856         [asyncResp](const boost::system::error_code& ec,
857                     const std::string& applyTime) {
858         if (ec)
859         {
860             BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
861             messages::internalError(asyncResp->res);
862             return;
863         }
864 
865         // Store the ApplyTime Value
866         if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
867                          "RequestedApplyTimes.Immediate")
868         {
869             asyncResp->res.jsonValue["HttpPushUriOptions"]
870                                     ["HttpPushUriApplyTime"]["ApplyTime"] =
871                 "Immediate";
872         }
873         else if (applyTime == "xyz.openbmc_project.Software.ApplyTime."
874                               "RequestedApplyTimes.OnReset")
875         {
876             asyncResp->res.jsonValue["HttpPushUriOptions"]
877                                     ["HttpPushUriApplyTime"]["ApplyTime"] =
878                 "OnReset";
879         }
880     });
881 }
882 
883 inline void handleUpdateServicePatch(
884     App& app, const crow::Request& req,
885     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
886 {
887     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
888     {
889         return;
890     }
891     BMCWEB_LOG_DEBUG("doPatch...");
892 
893     std::optional<std::string> applyTime;
894     if (!json_util::readJsonPatch(
895             req, asyncResp->res,
896             "HttpPushUriOptions/HttpPushUriApplyTime/ApplyTime", applyTime))
897     {
898         return;
899     }
900 
901     if (applyTime)
902     {
903         setApplyTime(asyncResp, *applyTime);
904     }
905 }
906 
907 inline void handleUpdateServiceFirmwareInventoryCollectionGet(
908     App& app, const crow::Request& req,
909     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
910 {
911     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
912     {
913         return;
914     }
915     asyncResp->res.jsonValue["@odata.type"] =
916         "#SoftwareInventoryCollection.SoftwareInventoryCollection";
917     asyncResp->res.jsonValue["@odata.id"] =
918         "/redfish/v1/UpdateService/FirmwareInventory";
919     asyncResp->res.jsonValue["Name"] = "Software Inventory Collection";
920     const std::array<const std::string_view, 1> iface = {
921         "xyz.openbmc_project.Software.Version"};
922 
923     redfish::collection_util::getCollectionMembers(
924         asyncResp,
925         boost::urls::url("/redfish/v1/UpdateService/FirmwareInventory"), iface,
926         "/xyz/openbmc_project/software");
927 }
928 
929 /* Fill related item links (i.e. bmc, bios) in for inventory */
930 inline void getRelatedItems(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
931                             const std::string& purpose)
932 {
933     if (purpose == sw_util::bmcPurpose)
934     {
935         nlohmann::json& relatedItem = asyncResp->res.jsonValue["RelatedItem"];
936         nlohmann::json::object_t item;
937         item["@odata.id"] = "/redfish/v1/Managers/bmc";
938         relatedItem.emplace_back(std::move(item));
939         asyncResp->res.jsonValue["RelatedItem@odata.count"] =
940             relatedItem.size();
941     }
942     else if (purpose == sw_util::biosPurpose)
943     {
944         nlohmann::json& relatedItem = asyncResp->res.jsonValue["RelatedItem"];
945         nlohmann::json::object_t item;
946         item["@odata.id"] = "/redfish/v1/Systems/system/Bios";
947         relatedItem.emplace_back(std::move(item));
948         asyncResp->res.jsonValue["RelatedItem@odata.count"] =
949             relatedItem.size();
950     }
951     else
952     {
953         BMCWEB_LOG_DEBUG("Unknown software purpose {}", purpose);
954     }
955 }
956 
957 inline void
958     getSoftwareVersion(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
959                        const std::string& service, const std::string& path,
960                        const std::string& swId)
961 {
962     sdbusplus::asio::getAllProperties(
963         *crow::connections::systemBus, service, path,
964         "xyz.openbmc_project.Software.Version",
965         [asyncResp,
966          swId](const boost::system::error_code& ec,
967                const dbus::utility::DBusPropertiesMap& propertiesList) {
968         if (ec)
969         {
970             messages::internalError(asyncResp->res);
971             return;
972         }
973 
974         const std::string* swInvPurpose = nullptr;
975         const std::string* version = nullptr;
976 
977         const bool success = sdbusplus::unpackPropertiesNoThrow(
978             dbus_utils::UnpackErrorPrinter(), propertiesList, "Purpose",
979             swInvPurpose, "Version", version);
980 
981         if (!success)
982         {
983             messages::internalError(asyncResp->res);
984             return;
985         }
986 
987         if (swInvPurpose == nullptr)
988         {
989             BMCWEB_LOG_DEBUG("Can't find property \"Purpose\"!");
990             messages::internalError(asyncResp->res);
991             return;
992         }
993 
994         BMCWEB_LOG_DEBUG("swInvPurpose = {}", *swInvPurpose);
995 
996         if (version == nullptr)
997         {
998             BMCWEB_LOG_DEBUG("Can't find property \"Version\"!");
999 
1000             messages::internalError(asyncResp->res);
1001 
1002             return;
1003         }
1004         asyncResp->res.jsonValue["Version"] = *version;
1005         asyncResp->res.jsonValue["Id"] = swId;
1006 
1007         // swInvPurpose is of format:
1008         // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
1009         // Translate this to "ABC image"
1010         size_t endDesc = swInvPurpose->rfind('.');
1011         if (endDesc == std::string::npos)
1012         {
1013             messages::internalError(asyncResp->res);
1014             return;
1015         }
1016         endDesc++;
1017         if (endDesc >= swInvPurpose->size())
1018         {
1019             messages::internalError(asyncResp->res);
1020             return;
1021         }
1022 
1023         std::string formatDesc = swInvPurpose->substr(endDesc);
1024         asyncResp->res.jsonValue["Description"] = formatDesc + " image";
1025         getRelatedItems(asyncResp, *swInvPurpose);
1026     });
1027 }
1028 
1029 inline void handleUpdateServiceFirmwareInventoryGet(
1030     App& app, const crow::Request& req,
1031     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1032     const std::string& param)
1033 {
1034     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1035     {
1036         return;
1037     }
1038     std::shared_ptr<std::string> swId = std::make_shared<std::string>(param);
1039 
1040     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1041         "/redfish/v1/UpdateService/FirmwareInventory/{}", *swId);
1042 
1043     constexpr std::array<std::string_view, 1> interfaces = {
1044         "xyz.openbmc_project.Software.Version"};
1045     dbus::utility::getSubTree(
1046         "/", 0, interfaces,
1047         [asyncResp,
1048          swId](const boost::system::error_code& ec,
1049                const dbus::utility::MapperGetSubTreeResponse& subtree) {
1050         BMCWEB_LOG_DEBUG("doGet callback...");
1051         if (ec)
1052         {
1053             messages::internalError(asyncResp->res);
1054             return;
1055         }
1056 
1057         // Ensure we find our input swId, otherwise return an error
1058         bool found = false;
1059         for (const std::pair<
1060                  std::string,
1061                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
1062                  obj : subtree)
1063         {
1064             if (!obj.first.ends_with(*swId))
1065             {
1066                 continue;
1067             }
1068 
1069             if (obj.second.empty())
1070             {
1071                 continue;
1072             }
1073 
1074             found = true;
1075             sw_util::getSwStatus(asyncResp, swId, obj.second[0].first);
1076             getSoftwareVersion(asyncResp, obj.second[0].first, obj.first,
1077                                *swId);
1078         }
1079         if (!found)
1080         {
1081             BMCWEB_LOG_WARNING("Input swID {} not found!", *swId);
1082             messages::resourceMissingAtURI(
1083                 asyncResp->res,
1084                 boost::urls::format(
1085                     "/redfish/v1/UpdateService/FirmwareInventory/{}", *swId));
1086             return;
1087         }
1088         asyncResp->res.jsonValue["@odata.type"] =
1089             "#SoftwareInventory.v1_1_0.SoftwareInventory";
1090         asyncResp->res.jsonValue["Name"] = "Software Inventory";
1091         asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
1092 
1093         asyncResp->res.jsonValue["Updateable"] = false;
1094         sw_util::getSwUpdatableStatus(asyncResp, swId);
1095     });
1096 }
1097 
1098 inline void requestRoutesUpdateService(App& app)
1099 {
1100     BMCWEB_ROUTE(
1101         app, "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
1102         .privileges(redfish::privileges::postUpdateService)
1103         .methods(boost::beast::http::verb::post)(std::bind_front(
1104             handleUpdateServiceSimpleUpdateAction, std::ref(app)));
1105 
1106     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/")
1107         .privileges(redfish::privileges::getSoftwareInventory)
1108         .methods(boost::beast::http::verb::get)(std::bind_front(
1109             handleUpdateServiceFirmwareInventoryGet, std::ref(app)));
1110 
1111     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
1112         .privileges(redfish::privileges::getUpdateService)
1113         .methods(boost::beast::http::verb::get)(
1114             std::bind_front(handleUpdateServiceGet, std::ref(app)));
1115 
1116     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/")
1117         .privileges(redfish::privileges::patchUpdateService)
1118         .methods(boost::beast::http::verb::patch)(
1119             std::bind_front(handleUpdateServicePatch, std::ref(app)));
1120 
1121     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/update/")
1122         .privileges(redfish::privileges::postUpdateService)
1123         .methods(boost::beast::http::verb::post)(
1124             std::bind_front(handleUpdateServicePost, std::ref(app)));
1125 
1126     BMCWEB_ROUTE(app, "/redfish/v1/UpdateService/FirmwareInventory/")
1127         .privileges(redfish::privileges::getSoftwareInventoryCollection)
1128         .methods(boost::beast::http::verb::get)(std::bind_front(
1129             handleUpdateServiceFirmwareInventoryCollectionGet, std::ref(app)));
1130 }
1131 
1132 } // namespace redfish
1133