blob: 87933fa2d6a451ff332fb419af74dfb71b2505f2 [file] [log] [blame]
Jennifer Lee729dae72018-04-24 15:59:34 -07001/*
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 "node.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -070019
Jennifer Lee729dae72018-04-24 15:59:34 -070020#include <boost/container/flat_map.hpp>
Andrew Geissler87d84722019-02-28 14:28:39 -060021#include <utils/fw_utils.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050022
Ed Tanousabf2add2019-01-22 16:40:12 -080023#include <variant>
Jennifer Lee729dae72018-04-24 15:59:34 -070024
Ed Tanous1abe55e2018-09-05 08:30:59 -070025namespace redfish
26{
Ed Tanous27826b52018-10-29 11:40:58 -070027
Andrew Geissler0e7de462019-03-04 19:11:54 -060028// Match signals added on software path
Jennifer Leeacb7cfb2018-06-07 16:08:15 -070029static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher;
James Feist4cde5d92020-06-11 10:39:55 -070030static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateErrorMatcher;
Andrew Geissler0e7de462019-03-04 19:11:54 -060031// Only allow one update at a time
32static bool fwUpdateInProgress = false;
Andrew Geissler86adcd62019-04-18 10:58:05 -050033// Timer for software available
Ed Tanous271584a2019-07-09 16:24:22 -070034static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer;
Andrew Geissler86adcd62019-04-18 10:58:05 -050035
36static void cleanUp()
37{
38 fwUpdateInProgress = false;
39 fwUpdateMatcher = nullptr;
James Feist4cde5d92020-06-11 10:39:55 -070040 fwUpdateErrorMatcher = nullptr;
Andrew Geissler86adcd62019-04-18 10:58:05 -050041}
Gunnar Mills1214b7e2020-06-04 10:11:30 -050042static void activateImage(const std::string& objPath,
43 const std::string& service)
Andrew Geissler86adcd62019-04-18 10:58:05 -050044{
45 BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service;
46 crow::connections::systemBus->async_method_call(
Ed Tanous81ce6092020-12-17 16:54:55 +000047 [](const boost::system::error_code errorCode) {
48 if (errorCode)
Andrew Geissler86adcd62019-04-18 10:58:05 -050049 {
Ed Tanous81ce6092020-12-17 16:54:55 +000050 BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
51 BMCWEB_LOG_DEBUG << "error msg = " << errorCode.message();
Andrew Geissler86adcd62019-04-18 10:58:05 -050052 }
53 },
54 service, objPath, "org.freedesktop.DBus.Properties", "Set",
55 "xyz.openbmc_project.Software.Activation", "RequestedActivation",
56 std::variant<std::string>(
57 "xyz.openbmc_project.Software.Activation.RequestedActivations."
58 "Active"));
59}
Andrew Geissler0554c982019-04-23 14:40:12 -050060
61// Note that asyncResp can be either a valid pointer or nullptr. If nullptr
62// then no asyncResp updates will occur
Ed Tanousb5a76932020-09-29 16:16:58 -070063static void softwareInterfaceAdded(const std::shared_ptr<AsyncResp>& asyncResp,
Gunnar Mills1214b7e2020-06-04 10:11:30 -050064 sdbusplus::message::message& m,
65 const crow::Request& req)
Andrew Geissler86adcd62019-04-18 10:58:05 -050066{
67 std::vector<std::pair<
68 std::string,
69 std::vector<std::pair<std::string, std::variant<std::string>>>>>
70 interfacesProperties;
71
72 sdbusplus::message::object_path objPath;
73
74 m.read(objPath, interfacesProperties);
75
76 BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
Gunnar Mills1214b7e2020-06-04 10:11:30 -050077 for (auto& interface : interfacesProperties)
Andrew Geissler86adcd62019-04-18 10:58:05 -050078 {
79 BMCWEB_LOG_DEBUG << "interface = " << interface.first;
80
81 if (interface.first == "xyz.openbmc_project.Software.Activation")
82 {
Andrew Geissler86adcd62019-04-18 10:58:05 -050083 // Retrieve service and activate
84 crow::connections::systemBus->async_method_call(
James Feistfe306722020-03-12 16:32:08 -070085 [objPath, asyncResp,
Ed Tanous81ce6092020-12-17 16:54:55 +000086 req](const boost::system::error_code errorCode,
James Feistfe306722020-03-12 16:32:08 -070087 const std::vector<std::pair<
Gunnar Mills1214b7e2020-06-04 10:11:30 -050088 std::string, std::vector<std::string>>>& objInfo) {
Ed Tanous81ce6092020-12-17 16:54:55 +000089 if (errorCode)
Andrew Geissler86adcd62019-04-18 10:58:05 -050090 {
Ed Tanous81ce6092020-12-17 16:54:55 +000091 BMCWEB_LOG_DEBUG << "error_code = " << errorCode;
Andrew Geissler86adcd62019-04-18 10:58:05 -050092 BMCWEB_LOG_DEBUG << "error msg = "
Ed Tanous81ce6092020-12-17 16:54:55 +000093 << errorCode.message();
Andrew Geissler0554c982019-04-23 14:40:12 -050094 if (asyncResp)
95 {
96 messages::internalError(asyncResp->res);
97 }
Andrew Geissler86adcd62019-04-18 10:58:05 -050098 cleanUp();
99 return;
100 }
101 // Ensure we only got one service back
102 if (objInfo.size() != 1)
103 {
104 BMCWEB_LOG_ERROR << "Invalid Object Size "
105 << objInfo.size();
Andrew Geissler0554c982019-04-23 14:40:12 -0500106 if (asyncResp)
107 {
108 messages::internalError(asyncResp->res);
109 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500110 cleanUp();
111 return;
112 }
113 // cancel timer only when
114 // xyz.openbmc_project.Software.Activation interface
115 // is added
116 fwAvailableTimer = nullptr;
117
118 activateImage(objPath.str, objInfo[0].first);
Andrew Geissler0554c982019-04-23 14:40:12 -0500119 if (asyncResp)
120 {
James Feist32898ce2020-03-10 16:16:52 -0700121 std::shared_ptr<task::TaskData> task =
122 task::TaskData::createTask(
123 [](boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500124 sdbusplus::message::message& msg,
125 const std::shared_ptr<task::TaskData>&
126 taskData) {
James Feist32898ce2020-03-10 16:16:52 -0700127 if (ec)
128 {
129 return task::completed;
130 }
131
132 std::string iface;
133 boost::container::flat_map<
James Feistfd9ab9e2020-05-19 13:48:07 -0700134 std::string,
135 std::variant<std::string, uint8_t>>
James Feist32898ce2020-03-10 16:16:52 -0700136 values;
James Feist32898ce2020-03-10 16:16:52 -0700137
James Feiste5d50062020-05-11 17:29:00 -0700138 std::string index =
139 std::to_string(taskData->index);
James Feistfd9ab9e2020-05-19 13:48:07 -0700140 msg.read(iface, values);
James Feiste5d50062020-05-11 17:29:00 -0700141
James Feistfd9ab9e2020-05-19 13:48:07 -0700142 if (iface == "xyz.openbmc_project.Software."
143 "Activation")
James Feist32898ce2020-03-10 16:16:52 -0700144 {
James Feistfd9ab9e2020-05-19 13:48:07 -0700145 auto findActivation =
146 values.find("Activation");
147 if (findActivation == values.end())
148 {
149 return !task::completed;
150 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500151 std::string* state =
James Feistfd9ab9e2020-05-19 13:48:07 -0700152 std::get_if<std::string>(
153 &(findActivation->second));
154
155 if (state == nullptr)
156 {
157 taskData->messages.emplace_back(
158 messages::internalError());
159 return task::completed;
160 }
161
162 if (boost::ends_with(*state,
163 "Invalid") ||
164 boost::ends_with(*state, "Failed"))
165 {
166 taskData->state = "Exception";
167 taskData->status = "Warning";
168 taskData->messages.emplace_back(
169 messages::taskAborted(index));
170 return task::completed;
171 }
172
173 if (boost::ends_with(*state, "Staged"))
174 {
175 taskData->state = "Stopping";
176 taskData->messages.emplace_back(
177 messages::taskPaused(index));
178
179 // its staged, set a long timer to
180 // allow them time to complete the
181 // update (probably cycle the
182 // system) if this expires then
183 // task will be cancelled
184 taskData->extendTimer(
185 std::chrono::hours(5));
186 return !task::completed;
187 }
188
189 if (boost::ends_with(*state, "Active"))
190 {
191 taskData->messages.emplace_back(
192 messages::taskCompletedOK(
193 index));
194 taskData->state = "Completed";
195 return task::completed;
196 }
James Feist32898ce2020-03-10 16:16:52 -0700197 }
James Feistfd9ab9e2020-05-19 13:48:07 -0700198 else if (iface ==
199 "xyz.openbmc_project.Software."
200 "ActivationProgress")
James Feist32898ce2020-03-10 16:16:52 -0700201 {
James Feistfd9ab9e2020-05-19 13:48:07 -0700202 auto findProgress =
203 values.find("Progress");
204 if (findProgress == values.end())
205 {
206 return !task::completed;
207 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500208 uint8_t* progress =
James Feistfd9ab9e2020-05-19 13:48:07 -0700209 std::get_if<uint8_t>(
210 &(findProgress->second));
James Feist32898ce2020-03-10 16:16:52 -0700211
James Feistfd9ab9e2020-05-19 13:48:07 -0700212 if (progress == nullptr)
213 {
214 taskData->messages.emplace_back(
215 messages::internalError());
216 return task::completed;
217 }
James Feist32898ce2020-03-10 16:16:52 -0700218 taskData->messages.emplace_back(
James Feistfd9ab9e2020-05-19 13:48:07 -0700219 messages::taskProgressChanged(
220 index, static_cast<size_t>(
221 *progress)));
222
223 // if we're getting status updates it's
224 // still alive, update timer
225 taskData->extendTimer(
226 std::chrono::minutes(5));
James Feist32898ce2020-03-10 16:16:52 -0700227 }
228
229 // as firmware update often results in a
230 // reboot, the task may never "complete"
231 // unless it is an error
232
233 return !task::completed;
234 },
235 "type='signal',interface='org.freedesktop.DBus."
236 "Properties',"
James Feistfd9ab9e2020-05-19 13:48:07 -0700237 "member='PropertiesChanged',path='" +
James Feist32898ce2020-03-10 16:16:52 -0700238 objPath.str + "'");
239 task->startTimer(std::chrono::minutes(5));
240 task->populateResp(asyncResp->res);
James Feistfe306722020-03-12 16:32:08 -0700241 task->payload.emplace(req);
Andrew Geissler0554c982019-04-23 14:40:12 -0500242 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500243 fwUpdateInProgress = false;
244 },
245 "xyz.openbmc_project.ObjectMapper",
246 "/xyz/openbmc_project/object_mapper",
247 "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500248 std::array<const char*, 1>{
Andrew Geissler86adcd62019-04-18 10:58:05 -0500249 "xyz.openbmc_project.Software.Activation"});
250 }
251 }
252}
253
Andrew Geissler0554c982019-04-23 14:40:12 -0500254// Note that asyncResp can be either a valid pointer or nullptr. If nullptr
255// then no asyncResp updates will occur
Ed Tanousb5a76932020-09-29 16:16:58 -0700256static void monitorForSoftwareAvailable(
257 const std::shared_ptr<AsyncResp>& asyncResp, const crow::Request& req,
258 const std::string& url, int timeoutTimeSeconds = 10)
Andrew Geissler86adcd62019-04-18 10:58:05 -0500259{
260 // Only allow one FW update at a time
261 if (fwUpdateInProgress != false)
262 {
Andrew Geissler0554c982019-04-23 14:40:12 -0500263 if (asyncResp)
264 {
Andrew Geissler0554c982019-04-23 14:40:12 -0500265 messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
266 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500267 return;
268 }
269
Andrew Geissler0554c982019-04-23 14:40:12 -0500270 fwAvailableTimer =
Ed Tanous271584a2019-07-09 16:24:22 -0700271 std::make_unique<boost::asio::steady_timer>(*req.ioService);
Andrew Geissler86adcd62019-04-18 10:58:05 -0500272
Ed Tanous271584a2019-07-09 16:24:22 -0700273 fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
Andrew Geissler86adcd62019-04-18 10:58:05 -0500274
275 fwAvailableTimer->async_wait(
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500276 [asyncResp](const boost::system::error_code& ec) {
Andrew Geissler86adcd62019-04-18 10:58:05 -0500277 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
284 << "Timed out waiting for firmware object being created";
285 BMCWEB_LOG_ERROR
286 << "FW image may has already been uploaded to server";
287 if (ec)
288 {
289 BMCWEB_LOG_ERROR << "Async_wait failed" << ec;
290 return;
291 }
Andrew Geissler0554c982019-04-23 14:40:12 -0500292 if (asyncResp)
293 {
294 redfish::messages::internalError(asyncResp->res);
295 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500296 });
297
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500298 auto callback = [asyncResp, req](sdbusplus::message::message& m) {
Andrew Geissler86adcd62019-04-18 10:58:05 -0500299 BMCWEB_LOG_DEBUG << "Match fired";
James Feistfe306722020-03-12 16:32:08 -0700300 softwareInterfaceAdded(asyncResp, m, req);
Andrew Geissler86adcd62019-04-18 10:58:05 -0500301 };
302
303 fwUpdateInProgress = true;
304
305 fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
306 *crow::connections::systemBus,
307 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
308 "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
309 callback);
James Feist4cde5d92020-06-11 10:39:55 -0700310
311 fwUpdateErrorMatcher = std::make_unique<sdbusplus::bus::match::match>(
312 *crow::connections::systemBus,
313 "type='signal',member='PropertiesChanged',path_namespace='/xyz/"
314 "openbmc_project/logging/entry',"
315 "arg0='xyz.openbmc_project.Logging.Entry'",
316 [asyncResp, url](sdbusplus::message::message& m) {
317 BMCWEB_LOG_DEBUG << "Error Match fired";
318 boost::container::flat_map<std::string, std::variant<std::string>>
319 values;
320 std::string objName;
321 m.read(objName, values);
322 auto find = values.find("Message");
323 if (find == values.end())
324 {
325 return;
326 }
327 std::string* type = std::get_if<std::string>(&(find->second));
328 if (type == nullptr)
329 {
330 return; // if this was our message, timeout will cover it
331 }
Gunnar Mills88b3dd12020-11-20 14:26:04 -0600332 if (!boost::starts_with(*type, "xyz.openbmc_project.Software"))
James Feist4cde5d92020-06-11 10:39:55 -0700333 {
334 return;
335 }
336 if (*type ==
337 "xyz.openbmc_project.Software.Image.Error.UnTarFailure")
338 {
339 redfish::messages::invalidUpload(asyncResp->res, url,
340 "Invalid archive");
341 }
342 else if (*type == "xyz.openbmc_project.Software.Image.Error."
343 "ManifestFileFailure")
344 {
345 redfish::messages::invalidUpload(asyncResp->res, url,
346 "Invalid manifest");
347 }
348 else if (*type ==
349 "xyz.openbmc_project.Software.Image.Error.ImageFailure")
350 {
351 redfish::messages::invalidUpload(asyncResp->res, url,
352 "Invalid image format");
353 }
Gunnar Mills88b3dd12020-11-20 14:26:04 -0600354 else if (*type == "xyz.openbmc_project.Software.Version.Error."
355 "AlreadyExists")
356 {
357
358 redfish::messages::invalidUpload(
359 asyncResp->res, url, "Image version already exists");
360
361 redfish::messages::resourceAlreadyExists(
362 asyncResp->res, "UpdateService.v1_4_0.UpdateService",
363 "Version", "uploaded version");
364 }
James Feist4cde5d92020-06-11 10:39:55 -0700365 else if (*type ==
366 "xyz.openbmc_project.Software.Image.Error.BusyFailure")
367 {
368 redfish::messages::resourceExhaustion(asyncResp->res, url);
369 }
370 else
371 {
372 redfish::messages::internalError(asyncResp->res);
373 }
374 fwAvailableTimer = nullptr;
375 });
Andrew Geissler86adcd62019-04-18 10:58:05 -0500376}
Jennifer Lee729dae72018-04-24 15:59:34 -0700377
Andrew Geissler0554c982019-04-23 14:40:12 -0500378/**
379 * UpdateServiceActionsSimpleUpdate class supports handle POST method for
380 * SimpleUpdate action.
381 */
382class UpdateServiceActionsSimpleUpdate : public Node
383{
384 public:
Ed Tanous52cc1122020-07-18 13:51:21 -0700385 UpdateServiceActionsSimpleUpdate(App& app) :
Andrew Geissler0554c982019-04-23 14:40:12 -0500386 Node(app,
387 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
388 {
389 entityPrivileges = {
390 {boost::beast::http::verb::get, {{"Login"}}},
391 {boost::beast::http::verb::head, {{"Login"}}},
392 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
393 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
394 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
395 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
396 }
397
398 private:
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500399 void doPost(crow::Response& res, const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +0000400 const std::vector<std::string>&) override
Andrew Geissler0554c982019-04-23 14:40:12 -0500401 {
402 std::optional<std::string> transferProtocol;
403 std::string imageURI;
404 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
405
406 BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
407
408 // User can pass in both TransferProtocol and ImageURI parameters or
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500409 // they can pass in just the ImageURI with the transfer protocol
410 // embedded within it.
Andrew Geissler0554c982019-04-23 14:40:12 -0500411 // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
412 // 2) ImageURI:tftp://1.1.1.1/myfile.bin
413
414 if (!json_util::readJson(req, asyncResp->res, "TransferProtocol",
415 transferProtocol, "ImageURI", imageURI))
416 {
417 BMCWEB_LOG_DEBUG
418 << "Missing TransferProtocol or ImageURI parameter";
419 return;
420 }
421 if (!transferProtocol)
422 {
423 // Must be option 2
424 // Verify ImageURI has transfer protocol in it
Ed Tanousf23b7292020-10-15 09:41:17 -0700425 size_t separator = imageURI.find(':');
Andrew Geissler0554c982019-04-23 14:40:12 -0500426 if ((separator == std::string::npos) ||
427 ((separator + 1) > imageURI.size()))
428 {
429 messages::actionParameterValueTypeError(
430 asyncResp->res, imageURI, "ImageURI",
431 "UpdateService.SimpleUpdate");
432 BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
433 << imageURI;
434 return;
435 }
436 transferProtocol = imageURI.substr(0, separator);
437 // Ensure protocol is upper case for a common comparison path below
438 boost::to_upper(*transferProtocol);
439 BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
440 << *transferProtocol;
441
442 // Adjust imageURI to not have the protocol on it for parsing
443 // below
444 // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
445 imageURI = imageURI.substr(separator + 3);
446 BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
447 }
448
449 // OpenBMC currently only supports TFTP
450 if (*transferProtocol != "TFTP")
451 {
452 messages::actionParameterNotSupported(asyncResp->res,
453 "TransferProtocol",
454 "UpdateService.SimpleUpdate");
455 BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
456 << *transferProtocol;
457 return;
458 }
459
460 // Format should be <IP or Hostname>/<file> for imageURI
Ed Tanousf23b7292020-10-15 09:41:17 -0700461 size_t separator = imageURI.find('/');
Andrew Geissler0554c982019-04-23 14:40:12 -0500462 if ((separator == std::string::npos) ||
463 ((separator + 1) > imageURI.size()))
464 {
465 messages::actionParameterValueTypeError(
466 asyncResp->res, imageURI, "ImageURI",
467 "UpdateService.SimpleUpdate");
468 BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
469 return;
470 }
471
472 std::string tftpServer = imageURI.substr(0, separator);
473 std::string fwFile = imageURI.substr(separator + 1);
474 BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
475
476 // Setup callback for when new software detected
Gunnar Mills2618d5e2020-08-18 13:04:27 -0500477 // Give TFTP 10 minutes to complete
James Feist4cde5d92020-06-11 10:39:55 -0700478 monitorForSoftwareAvailable(
479 nullptr, req,
480 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate",
Gunnar Mills2618d5e2020-08-18 13:04:27 -0500481 600);
Andrew Geissler0554c982019-04-23 14:40:12 -0500482
Gunnar Mills2618d5e2020-08-18 13:04:27 -0500483 // TFTP can take up to 10 minutes depending on image size and
Andrew Geissler0554c982019-04-23 14:40:12 -0500484 // connection speed. Return to caller as soon as the TFTP operation
485 // has been started. The callback above will ensure the activate
486 // is started once the download has completed
487 redfish::messages::success(asyncResp->res);
488
489 // Call TFTP service
490 crow::connections::systemBus->async_method_call(
491 [](const boost::system::error_code ec) {
492 if (ec)
493 {
494 // messages::internalError(asyncResp->res);
495 cleanUp();
496 BMCWEB_LOG_DEBUG << "error_code = " << ec;
497 BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
498 }
499 else
500 {
501 BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
502 }
503 },
504 "xyz.openbmc_project.Software.Download",
505 "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP",
506 "DownloadViaTFTP", fwFile, tftpServer);
507
508 BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
509 }
510};
511
Ed Tanous1abe55e2018-09-05 08:30:59 -0700512class UpdateService : public Node
513{
514 public:
Ed Tanous52cc1122020-07-18 13:51:21 -0700515 UpdateService(App& app) : Node(app, "/redfish/v1/UpdateService/")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700516 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700517 entityPrivileges = {
518 {boost::beast::http::verb::get, {{"Login"}}},
519 {boost::beast::http::verb::head, {{"Login"}}},
520 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
521 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
522 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
523 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Jennifer Leeacb7cfb2018-06-07 16:08:15 -0700524 }
Jennifer Leeacb7cfb2018-06-07 16:08:15 -0700525
Ed Tanous1abe55e2018-09-05 08:30:59 -0700526 private:
Ed Tanouscb13a392020-07-25 19:02:03 +0000527 void doGet(crow::Response& res, const crow::Request&,
528 const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700529 {
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530530 std::shared_ptr<AsyncResp> aResp = std::make_shared<AsyncResp>(res);
531 res.jsonValue["@odata.type"] = "#UpdateService.v1_4_0.UpdateService";
Ed Tanous0f74e642018-11-12 15:17:05 -0800532 res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
Ed Tanous0f74e642018-11-12 15:17:05 -0800533 res.jsonValue["Id"] = "UpdateService";
534 res.jsonValue["Description"] = "Service for Software Update";
535 res.jsonValue["Name"] = "Update Service";
536 res.jsonValue["HttpPushUri"] = "/redfish/v1/UpdateService";
537 // UpdateService cannot be disabled
538 res.jsonValue["ServiceEnabled"] = true;
539 res.jsonValue["FirmwareInventory"] = {
540 {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}};
Andrew Geissler0554c982019-04-23 14:40:12 -0500541#ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
542 // Update Actions object.
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500543 nlohmann::json& updateSvcSimpleUpdate =
Andrew Geissler0554c982019-04-23 14:40:12 -0500544 res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
545 updateSvcSimpleUpdate["target"] =
546 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
547 updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = {
548 "TFTP"};
549#endif
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530550 // Get the current ApplyTime value
551 crow::connections::systemBus->async_method_call(
552 [aResp](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500553 const std::variant<std::string>& applyTime) {
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530554 if (ec)
555 {
556 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
557 messages::internalError(aResp->res);
558 return;
559 }
560
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500561 const std::string* s = std::get_if<std::string>(&applyTime);
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530562 if (s == nullptr)
563 {
564 return;
565 }
566 // Store the ApplyTime Value
567 if (*s == "xyz.openbmc_project.Software.ApplyTime."
568 "RequestedApplyTimes.Immediate")
569 {
570 aResp->res.jsonValue["HttpPushUriOptions"]
571 ["HttpPushUriApplyTime"]["ApplyTime"] =
572 "Immediate";
573 }
574 else if (*s == "xyz.openbmc_project.Software.ApplyTime."
575 "RequestedApplyTimes.OnReset")
576 {
577 aResp->res.jsonValue["HttpPushUriOptions"]
578 ["HttpPushUriApplyTime"]["ApplyTime"] =
579 "OnReset";
580 }
581 },
582 "xyz.openbmc_project.Settings",
583 "/xyz/openbmc_project/software/apply_time",
584 "org.freedesktop.DBus.Properties", "Get",
585 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700586 }
Andrew Geissler0e7de462019-03-04 19:11:54 -0600587
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500588 void doPatch(crow::Response& res, const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +0000589 const std::vector<std::string>&) override
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530590 {
591 BMCWEB_LOG_DEBUG << "doPatch...";
592
593 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530594
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530595 std::optional<nlohmann::json> pushUriOptions;
596 if (!json_util::readJson(req, res, "HttpPushUriOptions",
597 pushUriOptions))
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530598 {
599 return;
600 }
601
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530602 if (pushUriOptions)
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530603 {
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530604 std::optional<nlohmann::json> pushUriApplyTime;
605 if (!json_util::readJson(*pushUriOptions, res,
606 "HttpPushUriApplyTime", pushUriApplyTime))
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530607 {
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530608 return;
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530609 }
610
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530611 if (pushUriApplyTime)
612 {
613 std::optional<std::string> applyTime;
614 if (!json_util::readJson(*pushUriApplyTime, res, "ApplyTime",
615 applyTime))
616 {
617 return;
618 }
619
620 if (applyTime)
621 {
622 std::string applyTimeNewVal;
623 if (applyTime == "Immediate")
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530624 {
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530625 applyTimeNewVal =
626 "xyz.openbmc_project.Software.ApplyTime."
627 "RequestedApplyTimes.Immediate";
628 }
629 else if (applyTime == "OnReset")
630 {
631 applyTimeNewVal =
632 "xyz.openbmc_project.Software.ApplyTime."
633 "RequestedApplyTimes.OnReset";
634 }
635 else
636 {
637 BMCWEB_LOG_INFO
638 << "ApplyTime value is not in the list of "
639 "acceptable values";
640 messages::propertyValueNotInList(
641 asyncResp->res, *applyTime, "ApplyTime");
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530642 return;
643 }
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530644
645 // Set the requested image apply time value
646 crow::connections::systemBus->async_method_call(
647 [asyncResp](const boost::system::error_code ec) {
648 if (ec)
649 {
650 BMCWEB_LOG_ERROR << "D-Bus responses error: "
651 << ec;
652 messages::internalError(asyncResp->res);
653 return;
654 }
655 messages::success(asyncResp->res);
656 },
657 "xyz.openbmc_project.Settings",
658 "/xyz/openbmc_project/software/apply_time",
659 "org.freedesktop.DBus.Properties", "Set",
660 "xyz.openbmc_project.Software.ApplyTime",
661 "RequestedApplyTime",
662 std::variant<std::string>{applyTimeNewVal});
663 }
664 }
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530665 }
666 }
667
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500668 void doPost(crow::Response& res, const crow::Request& req,
Ed Tanouscb13a392020-07-25 19:02:03 +0000669 const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700670 {
671 BMCWEB_LOG_DEBUG << "doPost...";
Jennifer Leeacb7cfb2018-06-07 16:08:15 -0700672
Andrew Geissler0e7de462019-03-04 19:11:54 -0600673 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700674
Andrew Geissler86adcd62019-04-18 10:58:05 -0500675 // Setup callback for when new software detected
James Feist4cde5d92020-06-11 10:39:55 -0700676 monitorForSoftwareAvailable(asyncResp, req,
677 "/redfish/v1/UpdateService");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700678
679 std::string filepath(
680 "/tmp/images/" +
681 boost::uuids::to_string(boost::uuids::random_generator()()));
682 BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
683 std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
684 std::ofstream::trunc);
685 out << req.body;
686 out.close();
687 BMCWEB_LOG_DEBUG << "file upload complete!!";
688 }
Jennifer Lee729dae72018-04-24 15:59:34 -0700689};
Ed Tanousc711bf82018-07-30 16:31:33 -0700690
Ed Tanous1abe55e2018-09-05 08:30:59 -0700691class SoftwareInventoryCollection : public Node
692{
693 public:
Ed Tanous52cc1122020-07-18 13:51:21 -0700694 SoftwareInventoryCollection(App& app) :
Ed Tanous1abe55e2018-09-05 08:30:59 -0700695 Node(app, "/redfish/v1/UpdateService/FirmwareInventory/")
696 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700697 entityPrivileges = {
698 {boost::beast::http::verb::get, {{"Login"}}},
699 {boost::beast::http::verb::head, {{"Login"}}},
700 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
701 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
702 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
703 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Jennifer Lee729dae72018-04-24 15:59:34 -0700704 }
Jennifer Lee729dae72018-04-24 15:59:34 -0700705
Ed Tanous1abe55e2018-09-05 08:30:59 -0700706 private:
Ed Tanouscb13a392020-07-25 19:02:03 +0000707 void doGet(crow::Response& res, const crow::Request&,
708 const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700709 {
710 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous0f74e642018-11-12 15:17:05 -0800711 res.jsonValue["@odata.type"] =
712 "#SoftwareInventoryCollection.SoftwareInventoryCollection";
713 res.jsonValue["@odata.id"] =
714 "/redfish/v1/UpdateService/FirmwareInventory";
Ed Tanous0f74e642018-11-12 15:17:05 -0800715 res.jsonValue["Name"] = "Software Inventory Collection";
Ed Tanousc711bf82018-07-30 16:31:33 -0700716
Ed Tanous1abe55e2018-09-05 08:30:59 -0700717 crow::connections::systemBus->async_method_call(
718 [asyncResp](
719 const boost::system::error_code ec,
720 const std::vector<std::pair<
721 std::string, std::vector<std::pair<
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500722 std::string, std::vector<std::string>>>>>&
723 subtree) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700724 if (ec)
725 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700726 messages::internalError(asyncResp->res);
Ed Tanousc711bf82018-07-30 16:31:33 -0700727 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700728 }
729 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
730 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jennifer Lee6c4eb9d2018-05-22 10:58:31 -0700731
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500732 for (auto& obj : subtree)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700733 {
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700734 // if can't parse fw id then return
Ed Tanous27826b52018-10-29 11:40:58 -0700735 std::size_t idPos;
Ed Tanousf23b7292020-10-15 09:41:17 -0700736 if ((idPos = obj.first.rfind('/')) == std::string::npos)
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700737 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700738 messages::internalError(asyncResp->res);
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700739 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
740 return;
741 }
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700742 std::string swId = obj.first.substr(idPos + 1);
743
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500744 nlohmann::json& members =
Andrew Geisslere0dd8052019-06-18 16:05:10 -0500745 asyncResp->res.jsonValue["Members"];
746 members.push_back(
747 {{"@odata.id", "/redfish/v1/UpdateService/"
748 "FirmwareInventory/" +
749 swId}});
750 asyncResp->res.jsonValue["Members@odata.count"] =
751 members.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700752 }
753 },
Andrew Geissler2830a9c2020-01-06 10:18:11 -0600754 // Note that only firmware levels associated with a device are
755 // stored under /xyz/openbmc_project/software therefore to ensure
756 // only real FirmwareInventory items are returned, this full object
757 // path must be used here as input to mapper
Ed Tanous1abe55e2018-09-05 08:30:59 -0700758 "xyz.openbmc_project.ObjectMapper",
759 "/xyz/openbmc_project/object_mapper",
Andrew Geissler2830a9c2020-01-06 10:18:11 -0600760 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
761 "/xyz/openbmc_project/software", static_cast<int32_t>(0),
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500762 std::array<const char*, 1>{"xyz.openbmc_project.Software.Version"});
Ed Tanous1abe55e2018-09-05 08:30:59 -0700763 }
Jennifer Lee729dae72018-04-24 15:59:34 -0700764};
765
Ed Tanous1abe55e2018-09-05 08:30:59 -0700766class SoftwareInventory : public Node
767{
768 public:
Ed Tanous52cc1122020-07-18 13:51:21 -0700769 SoftwareInventory(App& app) :
Ed Tanous1abe55e2018-09-05 08:30:59 -0700770 Node(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/",
771 std::string())
772 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700773 entityPrivileges = {
774 {boost::beast::http::verb::get, {{"Login"}}},
775 {boost::beast::http::verb::head, {{"Login"}}},
776 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
777 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
778 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
779 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
780 }
781
782 private:
Andrew Geissler87d84722019-02-28 14:28:39 -0600783 /* Fill related item links (i.e. bmc, bios) in for inventory */
Ed Tanousb5a76932020-09-29 16:16:58 -0700784 static void getRelatedItems(const std::shared_ptr<AsyncResp>& aResp,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500785 const std::string& purpose)
Andrew Geissler87d84722019-02-28 14:28:39 -0600786 {
787 if (purpose == fw_util::bmcPurpose)
788 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500789 nlohmann::json& members = aResp->res.jsonValue["RelatedItem"];
Andrew Geissler87d84722019-02-28 14:28:39 -0600790 members.push_back({{"@odata.id", "/redfish/v1/Managers/bmc"}});
791 aResp->res.jsonValue["Members@odata.count"] = members.size();
792 }
793 else if (purpose == fw_util::biosPurpose)
794 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500795 nlohmann::json& members = aResp->res.jsonValue["RelatedItem"];
Gunnar Millsf723d732020-02-26 11:20:49 -0600796 members.push_back(
797 {{"@odata.id", "/redfish/v1/Systems/system/Bios"}});
798 aResp->res.jsonValue["Members@odata.count"] = members.size();
Andrew Geissler87d84722019-02-28 14:28:39 -0600799 }
800 else
801 {
802 BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
803 }
804 }
805
Ed Tanouscb13a392020-07-25 19:02:03 +0000806 void doGet(crow::Response& res, const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500807 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700808 {
809 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700810
811 if (params.size() != 1)
812 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700813 messages::internalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700814 res.end();
815 return;
816 }
817
Ed Tanous3ae837c2018-08-07 14:41:19 -0700818 std::shared_ptr<std::string> swId =
Ed Tanous1abe55e2018-09-05 08:30:59 -0700819 std::make_shared<std::string>(params[0]);
820
821 res.jsonValue["@odata.id"] =
Ed Tanous3ae837c2018-08-07 14:41:19 -0700822 "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700823
824 crow::connections::systemBus->async_method_call(
Ed Tanous3ae837c2018-08-07 14:41:19 -0700825 [asyncResp, swId](
Ed Tanous1abe55e2018-09-05 08:30:59 -0700826 const boost::system::error_code ec,
827 const std::vector<std::pair<
828 std::string, std::vector<std::pair<
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500829 std::string, std::vector<std::string>>>>>&
830 subtree) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700831 BMCWEB_LOG_DEBUG << "doGet callback...";
832 if (ec)
833 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700834 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700835 return;
836 }
837
Andrew Geissler69132282019-07-01 11:00:35 -0500838 // Ensure we find our input swId, otherwise return an error
839 bool found = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700840 for (const std::pair<
841 std::string,
842 std::vector<
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500843 std::pair<std::string, std::vector<std::string>>>>&
844 obj : subtree)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700845 {
Ed Tanous3ae837c2018-08-07 14:41:19 -0700846 if (boost::ends_with(obj.first, *swId) != true)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700847 {
848 continue;
849 }
850
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700851 if (obj.second.size() < 1)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700852 {
853 continue;
854 }
855
Andrew Geissler69132282019-07-01 11:00:35 -0500856 found = true;
Andrew Geisslere0dd8052019-06-18 16:05:10 -0500857 fw_util::getFwStatus(asyncResp, swId, obj.second[0].first);
858
Ed Tanous1abe55e2018-09-05 08:30:59 -0700859 crow::connections::systemBus->async_method_call(
860 [asyncResp,
Ed Tanous81ce6092020-12-17 16:54:55 +0000861 swId](const boost::system::error_code errorCode,
Ed Tanous3ae837c2018-08-07 14:41:19 -0700862 const boost::container::flat_map<
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500863 std::string, VariantType>& propertiesList) {
Ed Tanous81ce6092020-12-17 16:54:55 +0000864 if (errorCode)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700865 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700866 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700867 return;
868 }
869 boost::container::flat_map<
870 std::string, VariantType>::const_iterator it =
871 propertiesList.find("Purpose");
872 if (it == propertiesList.end())
873 {
874 BMCWEB_LOG_DEBUG
875 << "Can't find property \"Purpose\"!";
Jason M. Billsf12894f2018-10-09 12:45:45 -0700876 messages::propertyMissing(asyncResp->res,
877 "Purpose");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700878 return;
879 }
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500880 const std::string* swInvPurpose =
Ed Tanousabf2add2019-01-22 16:40:12 -0800881 std::get_if<std::string>(&it->second);
Ed Tanous3ae837c2018-08-07 14:41:19 -0700882 if (swInvPurpose == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700883 {
884 BMCWEB_LOG_DEBUG
885 << "wrong types for property\"Purpose\"!";
Jason M. Billsf12894f2018-10-09 12:45:45 -0700886 messages::propertyValueTypeError(asyncResp->res,
887 "", "Purpose");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700888 return;
889 }
890
Ed Tanous3ae837c2018-08-07 14:41:19 -0700891 BMCWEB_LOG_DEBUG << "swInvPurpose = "
892 << *swInvPurpose;
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700893 it = propertiesList.find("Version");
894 if (it == propertiesList.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700895 {
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700896 BMCWEB_LOG_DEBUG
897 << "Can't find property \"Version\"!";
Jason M. Billsf12894f2018-10-09 12:45:45 -0700898 messages::propertyMissing(asyncResp->res,
899 "Version");
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700900 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700901 }
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700902
903 BMCWEB_LOG_DEBUG << "Version found!";
904
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500905 const std::string* version =
Ed Tanousabf2add2019-01-22 16:40:12 -0800906 std::get_if<std::string>(&it->second);
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700907
908 if (version == nullptr)
909 {
910 BMCWEB_LOG_DEBUG
911 << "Can't find property \"Version\"!";
Jason M. Billsf12894f2018-10-09 12:45:45 -0700912
913 messages::propertyValueTypeError(asyncResp->res,
914 "", "Version");
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700915 return;
916 }
917 asyncResp->res.jsonValue["Version"] = *version;
918 asyncResp->res.jsonValue["Id"] = *swId;
Andrew Geissler54daabe2019-02-13 13:54:15 -0600919
920 // swInvPurpose is of format:
921 // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
James Feiste2e96772019-10-03 10:51:43 -0700922 // Translate this to "ABC image"
Ed Tanousf23b7292020-10-15 09:41:17 -0700923 size_t endDesc = swInvPurpose->rfind('.');
Andrew Geissler54daabe2019-02-13 13:54:15 -0600924 if (endDesc == std::string::npos)
925 {
926 messages::internalError(asyncResp->res);
927 return;
928 }
929 endDesc++;
930 if (endDesc >= swInvPurpose->size())
931 {
932 messages::internalError(asyncResp->res);
933 return;
934 }
935
936 std::string formatDesc =
937 swInvPurpose->substr(endDesc);
938 asyncResp->res.jsonValue["Description"] =
James Feiste2e96772019-10-03 10:51:43 -0700939 formatDesc + " image";
Andrew Geissler87d84722019-02-28 14:28:39 -0600940 getRelatedItems(asyncResp, *swInvPurpose);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700941 },
942 obj.second[0].first, obj.first,
943 "org.freedesktop.DBus.Properties", "GetAll",
944 "xyz.openbmc_project.Software.Version");
945 }
Andrew Geissler69132282019-07-01 11:00:35 -0500946 if (!found)
947 {
948 BMCWEB_LOG_ERROR << "Input swID " + *swId + " not found!";
949 messages::resourceMissingAtURI(
950 asyncResp->res,
951 "/redfish/v1/UpdateService/FirmwareInventory/" + *swId);
952 return;
953 }
Ayushi Smriti4e68c452019-09-04 14:37:55 +0530954 asyncResp->res.jsonValue["@odata.type"] =
955 "#SoftwareInventory.v1_1_0.SoftwareInventory";
Ayushi Smriti4e68c452019-09-04 14:37:55 +0530956 asyncResp->res.jsonValue["Name"] = "Software Inventory";
Ayushi Smriti4e68c452019-09-04 14:37:55 +0530957 asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
AppaRao Puli3f8a7432020-01-29 02:36:32 +0530958
959 asyncResp->res.jsonValue["Updateable"] = false;
960 fw_util::getFwUpdateableStatus(asyncResp, swId);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700961 },
962 "xyz.openbmc_project.ObjectMapper",
963 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700964 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
965 static_cast<int32_t>(0),
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500966 std::array<const char*, 1>{"xyz.openbmc_project.Software.Version"});
Ed Tanous1abe55e2018-09-05 08:30:59 -0700967 }
968};
969
970} // namespace redfish