blob: 048430d0633779a63cd4de5db320ff2ea3b272dd [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>
Ed Tanousabf2add2019-01-22 16:40:12 -080022#include <variant>
Jennifer Lee729dae72018-04-24 15:59:34 -070023
Ed Tanous1abe55e2018-09-05 08:30:59 -070024namespace redfish
25{
Ed Tanous27826b52018-10-29 11:40:58 -070026
Andrew Geissler0e7de462019-03-04 19:11:54 -060027// Match signals added on software path
Jennifer Leeacb7cfb2018-06-07 16:08:15 -070028static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher;
Andrew Geissler0e7de462019-03-04 19:11:54 -060029// Only allow one update at a time
30static bool fwUpdateInProgress = false;
Andrew Geissler86adcd62019-04-18 10:58:05 -050031// Timer for software available
Ed Tanous271584a2019-07-09 16:24:22 -070032static std::unique_ptr<boost::asio::steady_timer> fwAvailableTimer;
Andrew Geissler86adcd62019-04-18 10:58:05 -050033
34static void cleanUp()
35{
36 fwUpdateInProgress = false;
37 fwUpdateMatcher = nullptr;
38}
39static void activateImage(const std::string &objPath,
40 const std::string &service)
41{
42 BMCWEB_LOG_DEBUG << "Activate image for " << objPath << " " << service;
43 crow::connections::systemBus->async_method_call(
44 [](const boost::system::error_code error_code) {
45 if (error_code)
46 {
47 BMCWEB_LOG_DEBUG << "error_code = " << error_code;
48 BMCWEB_LOG_DEBUG << "error msg = " << error_code.message();
49 }
50 },
51 service, objPath, "org.freedesktop.DBus.Properties", "Set",
52 "xyz.openbmc_project.Software.Activation", "RequestedActivation",
53 std::variant<std::string>(
54 "xyz.openbmc_project.Software.Activation.RequestedActivations."
55 "Active"));
56}
Andrew Geissler0554c982019-04-23 14:40:12 -050057
58// Note that asyncResp can be either a valid pointer or nullptr. If nullptr
59// then no asyncResp updates will occur
Andrew Geissler86adcd62019-04-18 10:58:05 -050060static void softwareInterfaceAdded(std::shared_ptr<AsyncResp> asyncResp,
61 sdbusplus::message::message &m)
62{
63 std::vector<std::pair<
64 std::string,
65 std::vector<std::pair<std::string, std::variant<std::string>>>>>
66 interfacesProperties;
67
68 sdbusplus::message::object_path objPath;
69
70 m.read(objPath, interfacesProperties);
71
72 BMCWEB_LOG_DEBUG << "obj path = " << objPath.str;
73 for (auto &interface : interfacesProperties)
74 {
75 BMCWEB_LOG_DEBUG << "interface = " << interface.first;
76
77 if (interface.first == "xyz.openbmc_project.Software.Activation")
78 {
79 // Found our interface, disable callbacks
80 fwUpdateMatcher = nullptr;
81
82 // Retrieve service and activate
83 crow::connections::systemBus->async_method_call(
84 [objPath, asyncResp](
85 const boost::system::error_code error_code,
86 const std::vector<std::pair<
87 std::string, std::vector<std::string>>> &objInfo) {
88 if (error_code)
89 {
90 BMCWEB_LOG_DEBUG << "error_code = " << error_code;
91 BMCWEB_LOG_DEBUG << "error msg = "
92 << error_code.message();
Andrew Geissler0554c982019-04-23 14:40:12 -050093 if (asyncResp)
94 {
95 messages::internalError(asyncResp->res);
96 }
Andrew Geissler86adcd62019-04-18 10:58:05 -050097 cleanUp();
98 return;
99 }
100 // Ensure we only got one service back
101 if (objInfo.size() != 1)
102 {
103 BMCWEB_LOG_ERROR << "Invalid Object Size "
104 << objInfo.size();
Andrew Geissler0554c982019-04-23 14:40:12 -0500105 if (asyncResp)
106 {
107 messages::internalError(asyncResp->res);
108 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500109 cleanUp();
110 return;
111 }
112 // cancel timer only when
113 // xyz.openbmc_project.Software.Activation interface
114 // is added
115 fwAvailableTimer = nullptr;
116
117 activateImage(objPath.str, objInfo[0].first);
Andrew Geissler0554c982019-04-23 14:40:12 -0500118 if (asyncResp)
119 {
120 redfish::messages::success(asyncResp->res);
121 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500122 fwUpdateInProgress = false;
123 },
124 "xyz.openbmc_project.ObjectMapper",
125 "/xyz/openbmc_project/object_mapper",
126 "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str,
127 std::array<const char *, 1>{
128 "xyz.openbmc_project.Software.Activation"});
129 }
130 }
131}
132
Andrew Geissler0554c982019-04-23 14:40:12 -0500133// Note that asyncResp can be either a valid pointer or nullptr. If nullptr
134// then no asyncResp updates will occur
Andrew Geissler86adcd62019-04-18 10:58:05 -0500135static void monitorForSoftwareAvailable(std::shared_ptr<AsyncResp> asyncResp,
Andrew Geissler0554c982019-04-23 14:40:12 -0500136 const crow::Request &req,
137 int timeoutTimeSeconds = 5)
Andrew Geissler86adcd62019-04-18 10:58:05 -0500138{
139 // Only allow one FW update at a time
140 if (fwUpdateInProgress != false)
141 {
Andrew Geissler0554c982019-04-23 14:40:12 -0500142 if (asyncResp)
143 {
Andrew Geissler0554c982019-04-23 14:40:12 -0500144 messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
145 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500146 return;
147 }
148
Andrew Geissler0554c982019-04-23 14:40:12 -0500149 fwAvailableTimer =
Ed Tanous271584a2019-07-09 16:24:22 -0700150 std::make_unique<boost::asio::steady_timer>(*req.ioService);
Andrew Geissler86adcd62019-04-18 10:58:05 -0500151
Ed Tanous271584a2019-07-09 16:24:22 -0700152 fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
Andrew Geissler86adcd62019-04-18 10:58:05 -0500153
154 fwAvailableTimer->async_wait(
155 [asyncResp](const boost::system::error_code &ec) {
156 cleanUp();
157 if (ec == boost::asio::error::operation_aborted)
158 {
159 // expected, we were canceled before the timer completed.
160 return;
161 }
162 BMCWEB_LOG_ERROR
163 << "Timed out waiting for firmware object being created";
164 BMCWEB_LOG_ERROR
165 << "FW image may has already been uploaded to server";
166 if (ec)
167 {
168 BMCWEB_LOG_ERROR << "Async_wait failed" << ec;
169 return;
170 }
Andrew Geissler0554c982019-04-23 14:40:12 -0500171 if (asyncResp)
172 {
173 redfish::messages::internalError(asyncResp->res);
174 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500175 });
176
177 auto callback = [asyncResp](sdbusplus::message::message &m) {
178 BMCWEB_LOG_DEBUG << "Match fired";
179 softwareInterfaceAdded(asyncResp, m);
180 };
181
182 fwUpdateInProgress = true;
183
184 fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
185 *crow::connections::systemBus,
186 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
187 "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
188 callback);
189}
Jennifer Lee729dae72018-04-24 15:59:34 -0700190
Andrew Geissler0554c982019-04-23 14:40:12 -0500191/**
192 * UpdateServiceActionsSimpleUpdate class supports handle POST method for
193 * SimpleUpdate action.
194 */
195class UpdateServiceActionsSimpleUpdate : public Node
196{
197 public:
198 UpdateServiceActionsSimpleUpdate(CrowApp &app) :
199 Node(app,
200 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
201 {
202 entityPrivileges = {
203 {boost::beast::http::verb::get, {{"Login"}}},
204 {boost::beast::http::verb::head, {{"Login"}}},
205 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
206 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
207 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
208 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
209 }
210
211 private:
212 void doPost(crow::Response &res, const crow::Request &req,
213 const std::vector<std::string> &params) override
214 {
215 std::optional<std::string> transferProtocol;
216 std::string imageURI;
217 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
218
219 BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
220
221 // User can pass in both TransferProtocol and ImageURI parameters or
222 // they can pass in just the ImageURI with the transfer protocl embedded
223 // within it.
224 // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
225 // 2) ImageURI:tftp://1.1.1.1/myfile.bin
226
227 if (!json_util::readJson(req, asyncResp->res, "TransferProtocol",
228 transferProtocol, "ImageURI", imageURI))
229 {
230 BMCWEB_LOG_DEBUG
231 << "Missing TransferProtocol or ImageURI parameter";
232 return;
233 }
234 if (!transferProtocol)
235 {
236 // Must be option 2
237 // Verify ImageURI has transfer protocol in it
238 size_t separator = imageURI.find(":");
239 if ((separator == std::string::npos) ||
240 ((separator + 1) > imageURI.size()))
241 {
242 messages::actionParameterValueTypeError(
243 asyncResp->res, imageURI, "ImageURI",
244 "UpdateService.SimpleUpdate");
245 BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
246 << imageURI;
247 return;
248 }
249 transferProtocol = imageURI.substr(0, separator);
250 // Ensure protocol is upper case for a common comparison path below
251 boost::to_upper(*transferProtocol);
252 BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
253 << *transferProtocol;
254
255 // Adjust imageURI to not have the protocol on it for parsing
256 // below
257 // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
258 imageURI = imageURI.substr(separator + 3);
259 BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
260 }
261
262 // OpenBMC currently only supports TFTP
263 if (*transferProtocol != "TFTP")
264 {
265 messages::actionParameterNotSupported(asyncResp->res,
266 "TransferProtocol",
267 "UpdateService.SimpleUpdate");
268 BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
269 << *transferProtocol;
270 return;
271 }
272
273 // Format should be <IP or Hostname>/<file> for imageURI
274 size_t separator = imageURI.find("/");
275 if ((separator == std::string::npos) ||
276 ((separator + 1) > imageURI.size()))
277 {
278 messages::actionParameterValueTypeError(
279 asyncResp->res, imageURI, "ImageURI",
280 "UpdateService.SimpleUpdate");
281 BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
282 return;
283 }
284
285 std::string tftpServer = imageURI.substr(0, separator);
286 std::string fwFile = imageURI.substr(separator + 1);
287 BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
288
289 // Setup callback for when new software detected
290 // Give TFTP 2 minutes to complete
291 monitorForSoftwareAvailable(nullptr, req, 120);
292
293 // TFTP can take up to 2 minutes depending on image size and
294 // connection speed. Return to caller as soon as the TFTP operation
295 // has been started. The callback above will ensure the activate
296 // is started once the download has completed
297 redfish::messages::success(asyncResp->res);
298
299 // Call TFTP service
300 crow::connections::systemBus->async_method_call(
301 [](const boost::system::error_code ec) {
302 if (ec)
303 {
304 // messages::internalError(asyncResp->res);
305 cleanUp();
306 BMCWEB_LOG_DEBUG << "error_code = " << ec;
307 BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
308 }
309 else
310 {
311 BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
312 }
313 },
314 "xyz.openbmc_project.Software.Download",
315 "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP",
316 "DownloadViaTFTP", fwFile, tftpServer);
317
318 BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
319 }
320};
321
Ed Tanous1abe55e2018-09-05 08:30:59 -0700322class UpdateService : public Node
323{
324 public:
325 UpdateService(CrowApp &app) : Node(app, "/redfish/v1/UpdateService/")
326 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700327 entityPrivileges = {
328 {boost::beast::http::verb::get, {{"Login"}}},
329 {boost::beast::http::verb::head, {{"Login"}}},
330 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
331 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
332 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
333 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Jennifer Leeacb7cfb2018-06-07 16:08:15 -0700334 }
Jennifer Leeacb7cfb2018-06-07 16:08:15 -0700335
Ed Tanous1abe55e2018-09-05 08:30:59 -0700336 private:
337 void doGet(crow::Response &res, const crow::Request &req,
338 const std::vector<std::string> &params) override
339 {
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530340 std::shared_ptr<AsyncResp> aResp = std::make_shared<AsyncResp>(res);
341 res.jsonValue["@odata.type"] = "#UpdateService.v1_4_0.UpdateService";
Ed Tanous0f74e642018-11-12 15:17:05 -0800342 res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
343 res.jsonValue["@odata.context"] =
344 "/redfish/v1/$metadata#UpdateService.UpdateService";
345 res.jsonValue["Id"] = "UpdateService";
346 res.jsonValue["Description"] = "Service for Software Update";
347 res.jsonValue["Name"] = "Update Service";
348 res.jsonValue["HttpPushUri"] = "/redfish/v1/UpdateService";
349 // UpdateService cannot be disabled
350 res.jsonValue["ServiceEnabled"] = true;
351 res.jsonValue["FirmwareInventory"] = {
352 {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}};
Andrew Geissler0554c982019-04-23 14:40:12 -0500353#ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
354 // Update Actions object.
355 nlohmann::json &updateSvcSimpleUpdate =
356 res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
357 updateSvcSimpleUpdate["target"] =
358 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
359 updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = {
360 "TFTP"};
361#endif
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530362 // Get the current ApplyTime value
363 crow::connections::systemBus->async_method_call(
364 [aResp](const boost::system::error_code ec,
365 const std::variant<std::string> &applyTime) {
366 if (ec)
367 {
368 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
369 messages::internalError(aResp->res);
370 return;
371 }
372
373 const std::string *s = std::get_if<std::string>(&applyTime);
374 if (s == nullptr)
375 {
376 return;
377 }
378 // Store the ApplyTime Value
379 if (*s == "xyz.openbmc_project.Software.ApplyTime."
380 "RequestedApplyTimes.Immediate")
381 {
382 aResp->res.jsonValue["HttpPushUriOptions"]
383 ["HttpPushUriApplyTime"]["ApplyTime"] =
384 "Immediate";
385 }
386 else if (*s == "xyz.openbmc_project.Software.ApplyTime."
387 "RequestedApplyTimes.OnReset")
388 {
389 aResp->res.jsonValue["HttpPushUriOptions"]
390 ["HttpPushUriApplyTime"]["ApplyTime"] =
391 "OnReset";
392 }
393 },
394 "xyz.openbmc_project.Settings",
395 "/xyz/openbmc_project/software/apply_time",
396 "org.freedesktop.DBus.Properties", "Get",
397 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700398 }
Andrew Geissler0e7de462019-03-04 19:11:54 -0600399
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530400 void doPatch(crow::Response &res, const crow::Request &req,
401 const std::vector<std::string> &params) override
402 {
403 BMCWEB_LOG_DEBUG << "doPatch...";
404
405 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530406
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530407 std::optional<nlohmann::json> pushUriOptions;
408 if (!json_util::readJson(req, res, "HttpPushUriOptions",
409 pushUriOptions))
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530410 {
411 return;
412 }
413
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530414 if (pushUriOptions)
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530415 {
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530416 std::optional<nlohmann::json> pushUriApplyTime;
417 if (!json_util::readJson(*pushUriOptions, res,
418 "HttpPushUriApplyTime", pushUriApplyTime))
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530419 {
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530420 return;
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530421 }
422
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530423 if (pushUriApplyTime)
424 {
425 std::optional<std::string> applyTime;
426 if (!json_util::readJson(*pushUriApplyTime, res, "ApplyTime",
427 applyTime))
428 {
429 return;
430 }
431
432 if (applyTime)
433 {
434 std::string applyTimeNewVal;
435 if (applyTime == "Immediate")
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530436 {
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530437 applyTimeNewVal =
438 "xyz.openbmc_project.Software.ApplyTime."
439 "RequestedApplyTimes.Immediate";
440 }
441 else if (applyTime == "OnReset")
442 {
443 applyTimeNewVal =
444 "xyz.openbmc_project.Software.ApplyTime."
445 "RequestedApplyTimes.OnReset";
446 }
447 else
448 {
449 BMCWEB_LOG_INFO
450 << "ApplyTime value is not in the list of "
451 "acceptable values";
452 messages::propertyValueNotInList(
453 asyncResp->res, *applyTime, "ApplyTime");
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530454 return;
455 }
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530456
457 // Set the requested image apply time value
458 crow::connections::systemBus->async_method_call(
459 [asyncResp](const boost::system::error_code ec) {
460 if (ec)
461 {
462 BMCWEB_LOG_ERROR << "D-Bus responses error: "
463 << ec;
464 messages::internalError(asyncResp->res);
465 return;
466 }
467 messages::success(asyncResp->res);
468 },
469 "xyz.openbmc_project.Settings",
470 "/xyz/openbmc_project/software/apply_time",
471 "org.freedesktop.DBus.Properties", "Set",
472 "xyz.openbmc_project.Software.ApplyTime",
473 "RequestedApplyTime",
474 std::variant<std::string>{applyTimeNewVal});
475 }
476 }
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530477 }
478 }
479
Ed Tanous1abe55e2018-09-05 08:30:59 -0700480 void doPost(crow::Response &res, const crow::Request &req,
481 const std::vector<std::string> &params) override
482 {
483 BMCWEB_LOG_DEBUG << "doPost...";
Jennifer Leeacb7cfb2018-06-07 16:08:15 -0700484
Andrew Geissler0e7de462019-03-04 19:11:54 -0600485 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700486
Andrew Geissler86adcd62019-04-18 10:58:05 -0500487 // Setup callback for when new software detected
488 monitorForSoftwareAvailable(asyncResp, req);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700489
490 std::string filepath(
491 "/tmp/images/" +
492 boost::uuids::to_string(boost::uuids::random_generator()()));
493 BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
494 std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
495 std::ofstream::trunc);
496 out << req.body;
497 out.close();
498 BMCWEB_LOG_DEBUG << "file upload complete!!";
499 }
Jennifer Lee729dae72018-04-24 15:59:34 -0700500};
Ed Tanousc711bf82018-07-30 16:31:33 -0700501
Ed Tanous1abe55e2018-09-05 08:30:59 -0700502class SoftwareInventoryCollection : public Node
503{
504 public:
505 template <typename CrowApp>
506 SoftwareInventoryCollection(CrowApp &app) :
507 Node(app, "/redfish/v1/UpdateService/FirmwareInventory/")
508 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700509 entityPrivileges = {
510 {boost::beast::http::verb::get, {{"Login"}}},
511 {boost::beast::http::verb::head, {{"Login"}}},
512 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
513 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
514 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
515 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Jennifer Lee729dae72018-04-24 15:59:34 -0700516 }
Jennifer Lee729dae72018-04-24 15:59:34 -0700517
Ed Tanous1abe55e2018-09-05 08:30:59 -0700518 private:
519 void doGet(crow::Response &res, const crow::Request &req,
520 const std::vector<std::string> &params) override
521 {
522 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous0f74e642018-11-12 15:17:05 -0800523 res.jsonValue["@odata.type"] =
524 "#SoftwareInventoryCollection.SoftwareInventoryCollection";
525 res.jsonValue["@odata.id"] =
526 "/redfish/v1/UpdateService/FirmwareInventory";
527 res.jsonValue["@odata.context"] =
528 "/redfish/v1/"
529 "$metadata#SoftwareInventoryCollection.SoftwareInventoryCollection";
530 res.jsonValue["Name"] = "Software Inventory Collection";
Ed Tanousc711bf82018-07-30 16:31:33 -0700531
Ed Tanous1abe55e2018-09-05 08:30:59 -0700532 crow::connections::systemBus->async_method_call(
533 [asyncResp](
534 const boost::system::error_code ec,
535 const std::vector<std::pair<
536 std::string, std::vector<std::pair<
537 std::string, std::vector<std::string>>>>>
538 &subtree) {
539 if (ec)
540 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700541 messages::internalError(asyncResp->res);
Ed Tanousc711bf82018-07-30 16:31:33 -0700542 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700543 }
544 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
545 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jennifer Lee6c4eb9d2018-05-22 10:58:31 -0700546
Ed Tanous1abe55e2018-09-05 08:30:59 -0700547 for (auto &obj : subtree)
548 {
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700549 // if can't parse fw id then return
Ed Tanous27826b52018-10-29 11:40:58 -0700550 std::size_t idPos;
551 if ((idPos = obj.first.rfind("/")) == std::string::npos)
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700552 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700553 messages::internalError(asyncResp->res);
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700554 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
555 return;
556 }
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700557 std::string swId = obj.first.substr(idPos + 1);
558
Andrew Geisslere0dd8052019-06-18 16:05:10 -0500559 nlohmann::json &members =
560 asyncResp->res.jsonValue["Members"];
561 members.push_back(
562 {{"@odata.id", "/redfish/v1/UpdateService/"
563 "FirmwareInventory/" +
564 swId}});
565 asyncResp->res.jsonValue["Members@odata.count"] =
566 members.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700567 }
568 },
Andrew Geissler2830a9c2020-01-06 10:18:11 -0600569 // Note that only firmware levels associated with a device are
570 // stored under /xyz/openbmc_project/software therefore to ensure
571 // only real FirmwareInventory items are returned, this full object
572 // path must be used here as input to mapper
Ed Tanous1abe55e2018-09-05 08:30:59 -0700573 "xyz.openbmc_project.ObjectMapper",
574 "/xyz/openbmc_project/object_mapper",
Andrew Geissler2830a9c2020-01-06 10:18:11 -0600575 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
576 "/xyz/openbmc_project/software", static_cast<int32_t>(0),
Ed Tanous1abe55e2018-09-05 08:30:59 -0700577 std::array<const char *, 1>{
578 "xyz.openbmc_project.Software.Version"});
579 }
Jennifer Lee729dae72018-04-24 15:59:34 -0700580};
581
Ed Tanous1abe55e2018-09-05 08:30:59 -0700582class SoftwareInventory : public Node
583{
584 public:
585 template <typename CrowApp>
586 SoftwareInventory(CrowApp &app) :
587 Node(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/",
588 std::string())
589 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700590 entityPrivileges = {
591 {boost::beast::http::verb::get, {{"Login"}}},
592 {boost::beast::http::verb::head, {{"Login"}}},
593 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
594 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
595 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
596 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
597 }
598
599 private:
Andrew Geissler87d84722019-02-28 14:28:39 -0600600 /* Fill related item links (i.e. bmc, bios) in for inventory */
601 static void getRelatedItems(std::shared_ptr<AsyncResp> aResp,
602 const std::string &purpose)
603 {
604 if (purpose == fw_util::bmcPurpose)
605 {
606 nlohmann::json &members = aResp->res.jsonValue["RelatedItem"];
607 members.push_back({{"@odata.id", "/redfish/v1/Managers/bmc"}});
608 aResp->res.jsonValue["Members@odata.count"] = members.size();
609 }
610 else if (purpose == fw_util::biosPurpose)
611 {
612 // TODO(geissonator) Need BIOS schema support added for this
613 // to be valid
614 // nlohmann::json &members = aResp->res.jsonValue["RelatedItem"];
615 // members.push_back(
616 // {{"@odata.id", "/redfish/v1/Systems/system/BIOS"}});
617 // aResp->res.jsonValue["Members@odata.count"] = members.size();
618 }
619 else
620 {
621 BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
622 }
623 }
624
Ed Tanous1abe55e2018-09-05 08:30:59 -0700625 void doGet(crow::Response &res, const crow::Request &req,
626 const std::vector<std::string> &params) override
627 {
628 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700629
630 if (params.size() != 1)
631 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700632 messages::internalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700633 res.end();
634 return;
635 }
636
Ed Tanous3ae837c2018-08-07 14:41:19 -0700637 std::shared_ptr<std::string> swId =
Ed Tanous1abe55e2018-09-05 08:30:59 -0700638 std::make_shared<std::string>(params[0]);
639
640 res.jsonValue["@odata.id"] =
Ed Tanous3ae837c2018-08-07 14:41:19 -0700641 "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700642
643 crow::connections::systemBus->async_method_call(
Ed Tanous3ae837c2018-08-07 14:41:19 -0700644 [asyncResp, swId](
Ed Tanous1abe55e2018-09-05 08:30:59 -0700645 const boost::system::error_code ec,
646 const std::vector<std::pair<
647 std::string, std::vector<std::pair<
648 std::string, std::vector<std::string>>>>>
649 &subtree) {
650 BMCWEB_LOG_DEBUG << "doGet callback...";
651 if (ec)
652 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700653 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700654 return;
655 }
656
Andrew Geissler69132282019-07-01 11:00:35 -0500657 // Ensure we find our input swId, otherwise return an error
658 bool found = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700659 for (const std::pair<
660 std::string,
661 std::vector<
662 std::pair<std::string, std::vector<std::string>>>>
663 &obj : subtree)
664 {
Ed Tanous3ae837c2018-08-07 14:41:19 -0700665 if (boost::ends_with(obj.first, *swId) != true)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700666 {
667 continue;
668 }
669
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700670 if (obj.second.size() < 1)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700671 {
672 continue;
673 }
674
Andrew Geissler69132282019-07-01 11:00:35 -0500675 found = true;
Andrew Geisslere0dd8052019-06-18 16:05:10 -0500676 fw_util::getFwStatus(asyncResp, swId, obj.second[0].first);
677
Ed Tanous1abe55e2018-09-05 08:30:59 -0700678 crow::connections::systemBus->async_method_call(
679 [asyncResp,
Ed Tanous3ae837c2018-08-07 14:41:19 -0700680 swId](const boost::system::error_code error_code,
681 const boost::container::flat_map<
682 std::string, VariantType> &propertiesList) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700683 if (error_code)
684 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700685 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700686 return;
687 }
688 boost::container::flat_map<
689 std::string, VariantType>::const_iterator it =
690 propertiesList.find("Purpose");
691 if (it == propertiesList.end())
692 {
693 BMCWEB_LOG_DEBUG
694 << "Can't find property \"Purpose\"!";
Jason M. Billsf12894f2018-10-09 12:45:45 -0700695 messages::propertyMissing(asyncResp->res,
696 "Purpose");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700697 return;
698 }
Ed Tanous3ae837c2018-08-07 14:41:19 -0700699 const std::string *swInvPurpose =
Ed Tanousabf2add2019-01-22 16:40:12 -0800700 std::get_if<std::string>(&it->second);
Ed Tanous3ae837c2018-08-07 14:41:19 -0700701 if (swInvPurpose == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700702 {
703 BMCWEB_LOG_DEBUG
704 << "wrong types for property\"Purpose\"!";
Jason M. Billsf12894f2018-10-09 12:45:45 -0700705 messages::propertyValueTypeError(asyncResp->res,
706 "", "Purpose");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700707 return;
708 }
709
Ed Tanous3ae837c2018-08-07 14:41:19 -0700710 BMCWEB_LOG_DEBUG << "swInvPurpose = "
711 << *swInvPurpose;
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700712 it = propertiesList.find("Version");
713 if (it == propertiesList.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700714 {
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700715 BMCWEB_LOG_DEBUG
716 << "Can't find property \"Version\"!";
Jason M. Billsf12894f2018-10-09 12:45:45 -0700717 messages::propertyMissing(asyncResp->res,
718 "Version");
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700719 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700720 }
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700721
722 BMCWEB_LOG_DEBUG << "Version found!";
723
724 const std::string *version =
Ed Tanousabf2add2019-01-22 16:40:12 -0800725 std::get_if<std::string>(&it->second);
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700726
727 if (version == nullptr)
728 {
729 BMCWEB_LOG_DEBUG
730 << "Can't find property \"Version\"!";
Jason M. Billsf12894f2018-10-09 12:45:45 -0700731
732 messages::propertyValueTypeError(asyncResp->res,
733 "", "Version");
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700734 return;
735 }
736 asyncResp->res.jsonValue["Version"] = *version;
737 asyncResp->res.jsonValue["Id"] = *swId;
Andrew Geissler54daabe2019-02-13 13:54:15 -0600738
739 // swInvPurpose is of format:
740 // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
James Feiste2e96772019-10-03 10:51:43 -0700741 // Translate this to "ABC image"
Andrew Geissler54daabe2019-02-13 13:54:15 -0600742 size_t endDesc = swInvPurpose->rfind(".");
743 if (endDesc == std::string::npos)
744 {
745 messages::internalError(asyncResp->res);
746 return;
747 }
748 endDesc++;
749 if (endDesc >= swInvPurpose->size())
750 {
751 messages::internalError(asyncResp->res);
752 return;
753 }
754
755 std::string formatDesc =
756 swInvPurpose->substr(endDesc);
757 asyncResp->res.jsonValue["Description"] =
James Feiste2e96772019-10-03 10:51:43 -0700758 formatDesc + " image";
Andrew Geissler87d84722019-02-28 14:28:39 -0600759 getRelatedItems(asyncResp, *swInvPurpose);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700760 },
761 obj.second[0].first, obj.first,
762 "org.freedesktop.DBus.Properties", "GetAll",
763 "xyz.openbmc_project.Software.Version");
764 }
Andrew Geissler69132282019-07-01 11:00:35 -0500765 if (!found)
766 {
767 BMCWEB_LOG_ERROR << "Input swID " + *swId + " not found!";
768 messages::resourceMissingAtURI(
769 asyncResp->res,
770 "/redfish/v1/UpdateService/FirmwareInventory/" + *swId);
771 return;
772 }
Ayushi Smriti4e68c452019-09-04 14:37:55 +0530773 asyncResp->res.jsonValue["@odata.type"] =
774 "#SoftwareInventory.v1_1_0.SoftwareInventory";
775 asyncResp->res.jsonValue["@odata.context"] =
776 "/redfish/v1/$metadata#SoftwareInventory.SoftwareInventory";
777 asyncResp->res.jsonValue["Name"] = "Software Inventory";
Ayushi Smriti4e68c452019-09-04 14:37:55 +0530778 asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
AppaRao Puli3f8a7432020-01-29 02:36:32 +0530779
780 asyncResp->res.jsonValue["Updateable"] = false;
781 fw_util::getFwUpdateableStatus(asyncResp, swId);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700782 },
783 "xyz.openbmc_project.ObjectMapper",
784 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700785 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
786 static_cast<int32_t>(0),
Ed Tanous1abe55e2018-09-05 08:30:59 -0700787 std::array<const char *, 1>{
788 "xyz.openbmc_project.Software.Version"});
789 }
790};
791
792} // namespace redfish