blob: 3ca7721cbdd43bb34271e8a8a3e70dcd40f5e260 [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 {
James Feist32898ce2020-03-10 16:16:52 -0700120 std::shared_ptr<task::TaskData> task =
121 task::TaskData::createTask(
122 [](boost::system::error_code ec,
123 sdbusplus::message::message &msg,
124 const std::shared_ptr<task::TaskData>
125 &taskData) {
126 if (ec)
127 {
128 return task::completed;
129 }
130
131 std::string iface;
132 boost::container::flat_map<
133 std::string, std::variant<std::string>>
134 values;
135 msg.read(iface, values);
136 auto findActivation =
137 values.find("Activation");
138 if (findActivation == values.end())
139 {
140 return !task::completed;
141 }
142 std::string *state =
143 std::get_if<std::string>(
144 &(findActivation->second));
145
146 if (state == nullptr)
147 {
148 taskData->messages.emplace_back(
149 messages::internalError());
150 return task::completed;
151 }
152
153 if (boost::ends_with(*state, "Invalid") ||
154 boost::ends_with(*state, "Failed"))
155 {
156 taskData->state = "Exception";
157 taskData->status = "Warning";
158 taskData->messages.emplace_back(
159 messages::invalidObject(
160 "/redfish/v1/UpdateService/"));
161 return task::completed;
162 }
163
164 if (boost::ends_with(*state, "Staged"))
165 {
166 taskData->state = "Pending";
167 return !task::completed;
168 }
169
170 if (boost::ends_with(*state, "Active"))
171 {
172 taskData->messages.emplace_back(
173 messages::success());
174 taskData->state = "Completed";
175 return task::completed;
176 }
177
178 // as firmware update often results in a
179 // reboot, the task may never "complete"
180 // unless it is an error
181
182 return !task::completed;
183 },
184 "type='signal',interface='org.freedesktop.DBus."
185 "Properties',"
186 "member='PropertiesChanged',arg0='xyz.openbmc_"
187 "project.Software.Activation',path='" +
188 objPath.str + "'");
189 task->startTimer(std::chrono::minutes(5));
190 task->populateResp(asyncResp->res);
Andrew Geissler0554c982019-04-23 14:40:12 -0500191 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500192 fwUpdateInProgress = false;
193 },
194 "xyz.openbmc_project.ObjectMapper",
195 "/xyz/openbmc_project/object_mapper",
196 "xyz.openbmc_project.ObjectMapper", "GetObject", objPath.str,
197 std::array<const char *, 1>{
198 "xyz.openbmc_project.Software.Activation"});
199 }
200 }
201}
202
Andrew Geissler0554c982019-04-23 14:40:12 -0500203// Note that asyncResp can be either a valid pointer or nullptr. If nullptr
204// then no asyncResp updates will occur
Andrew Geissler86adcd62019-04-18 10:58:05 -0500205static void monitorForSoftwareAvailable(std::shared_ptr<AsyncResp> asyncResp,
Andrew Geissler0554c982019-04-23 14:40:12 -0500206 const crow::Request &req,
207 int timeoutTimeSeconds = 5)
Andrew Geissler86adcd62019-04-18 10:58:05 -0500208{
209 // Only allow one FW update at a time
210 if (fwUpdateInProgress != false)
211 {
Andrew Geissler0554c982019-04-23 14:40:12 -0500212 if (asyncResp)
213 {
Andrew Geissler0554c982019-04-23 14:40:12 -0500214 messages::serviceTemporarilyUnavailable(asyncResp->res, "30");
215 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500216 return;
217 }
218
Andrew Geissler0554c982019-04-23 14:40:12 -0500219 fwAvailableTimer =
Ed Tanous271584a2019-07-09 16:24:22 -0700220 std::make_unique<boost::asio::steady_timer>(*req.ioService);
Andrew Geissler86adcd62019-04-18 10:58:05 -0500221
Ed Tanous271584a2019-07-09 16:24:22 -0700222 fwAvailableTimer->expires_after(std::chrono::seconds(timeoutTimeSeconds));
Andrew Geissler86adcd62019-04-18 10:58:05 -0500223
224 fwAvailableTimer->async_wait(
225 [asyncResp](const boost::system::error_code &ec) {
226 cleanUp();
227 if (ec == boost::asio::error::operation_aborted)
228 {
229 // expected, we were canceled before the timer completed.
230 return;
231 }
232 BMCWEB_LOG_ERROR
233 << "Timed out waiting for firmware object being created";
234 BMCWEB_LOG_ERROR
235 << "FW image may has already been uploaded to server";
236 if (ec)
237 {
238 BMCWEB_LOG_ERROR << "Async_wait failed" << ec;
239 return;
240 }
Andrew Geissler0554c982019-04-23 14:40:12 -0500241 if (asyncResp)
242 {
243 redfish::messages::internalError(asyncResp->res);
244 }
Andrew Geissler86adcd62019-04-18 10:58:05 -0500245 });
246
247 auto callback = [asyncResp](sdbusplus::message::message &m) {
248 BMCWEB_LOG_DEBUG << "Match fired";
249 softwareInterfaceAdded(asyncResp, m);
250 };
251
252 fwUpdateInProgress = true;
253
254 fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
255 *crow::connections::systemBus,
256 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
257 "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
258 callback);
259}
Jennifer Lee729dae72018-04-24 15:59:34 -0700260
Andrew Geissler0554c982019-04-23 14:40:12 -0500261/**
262 * UpdateServiceActionsSimpleUpdate class supports handle POST method for
263 * SimpleUpdate action.
264 */
265class UpdateServiceActionsSimpleUpdate : public Node
266{
267 public:
268 UpdateServiceActionsSimpleUpdate(CrowApp &app) :
269 Node(app,
270 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate/")
271 {
272 entityPrivileges = {
273 {boost::beast::http::verb::get, {{"Login"}}},
274 {boost::beast::http::verb::head, {{"Login"}}},
275 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
276 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
277 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
278 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
279 }
280
281 private:
282 void doPost(crow::Response &res, const crow::Request &req,
283 const std::vector<std::string> &params) override
284 {
285 std::optional<std::string> transferProtocol;
286 std::string imageURI;
287 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
288
289 BMCWEB_LOG_DEBUG << "Enter UpdateService.SimpleUpdate doPost";
290
291 // User can pass in both TransferProtocol and ImageURI parameters or
292 // they can pass in just the ImageURI with the transfer protocl embedded
293 // within it.
294 // 1) TransferProtocol:TFTP ImageURI:1.1.1.1/myfile.bin
295 // 2) ImageURI:tftp://1.1.1.1/myfile.bin
296
297 if (!json_util::readJson(req, asyncResp->res, "TransferProtocol",
298 transferProtocol, "ImageURI", imageURI))
299 {
300 BMCWEB_LOG_DEBUG
301 << "Missing TransferProtocol or ImageURI parameter";
302 return;
303 }
304 if (!transferProtocol)
305 {
306 // Must be option 2
307 // Verify ImageURI has transfer protocol in it
308 size_t separator = imageURI.find(":");
309 if ((separator == std::string::npos) ||
310 ((separator + 1) > imageURI.size()))
311 {
312 messages::actionParameterValueTypeError(
313 asyncResp->res, imageURI, "ImageURI",
314 "UpdateService.SimpleUpdate");
315 BMCWEB_LOG_ERROR << "ImageURI missing transfer protocol: "
316 << imageURI;
317 return;
318 }
319 transferProtocol = imageURI.substr(0, separator);
320 // Ensure protocol is upper case for a common comparison path below
321 boost::to_upper(*transferProtocol);
322 BMCWEB_LOG_DEBUG << "Encoded transfer protocol "
323 << *transferProtocol;
324
325 // Adjust imageURI to not have the protocol on it for parsing
326 // below
327 // ex. tftp://1.1.1.1/myfile.bin -> 1.1.1.1/myfile.bin
328 imageURI = imageURI.substr(separator + 3);
329 BMCWEB_LOG_DEBUG << "Adjusted imageUri " << imageURI;
330 }
331
332 // OpenBMC currently only supports TFTP
333 if (*transferProtocol != "TFTP")
334 {
335 messages::actionParameterNotSupported(asyncResp->res,
336 "TransferProtocol",
337 "UpdateService.SimpleUpdate");
338 BMCWEB_LOG_ERROR << "Request incorrect protocol parameter: "
339 << *transferProtocol;
340 return;
341 }
342
343 // Format should be <IP or Hostname>/<file> for imageURI
344 size_t separator = imageURI.find("/");
345 if ((separator == std::string::npos) ||
346 ((separator + 1) > imageURI.size()))
347 {
348 messages::actionParameterValueTypeError(
349 asyncResp->res, imageURI, "ImageURI",
350 "UpdateService.SimpleUpdate");
351 BMCWEB_LOG_ERROR << "Invalid ImageURI: " << imageURI;
352 return;
353 }
354
355 std::string tftpServer = imageURI.substr(0, separator);
356 std::string fwFile = imageURI.substr(separator + 1);
357 BMCWEB_LOG_DEBUG << "Server: " << tftpServer + " File: " << fwFile;
358
359 // Setup callback for when new software detected
360 // Give TFTP 2 minutes to complete
361 monitorForSoftwareAvailable(nullptr, req, 120);
362
363 // TFTP can take up to 2 minutes depending on image size and
364 // connection speed. Return to caller as soon as the TFTP operation
365 // has been started. The callback above will ensure the activate
366 // is started once the download has completed
367 redfish::messages::success(asyncResp->res);
368
369 // Call TFTP service
370 crow::connections::systemBus->async_method_call(
371 [](const boost::system::error_code ec) {
372 if (ec)
373 {
374 // messages::internalError(asyncResp->res);
375 cleanUp();
376 BMCWEB_LOG_DEBUG << "error_code = " << ec;
377 BMCWEB_LOG_DEBUG << "error msg = " << ec.message();
378 }
379 else
380 {
381 BMCWEB_LOG_DEBUG << "Call to DownloaViaTFTP Success";
382 }
383 },
384 "xyz.openbmc_project.Software.Download",
385 "/xyz/openbmc_project/software", "xyz.openbmc_project.Common.TFTP",
386 "DownloadViaTFTP", fwFile, tftpServer);
387
388 BMCWEB_LOG_DEBUG << "Exit UpdateService.SimpleUpdate doPost";
389 }
390};
391
Ed Tanous1abe55e2018-09-05 08:30:59 -0700392class UpdateService : public Node
393{
394 public:
395 UpdateService(CrowApp &app) : Node(app, "/redfish/v1/UpdateService/")
396 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700397 entityPrivileges = {
398 {boost::beast::http::verb::get, {{"Login"}}},
399 {boost::beast::http::verb::head, {{"Login"}}},
400 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
401 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
402 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
403 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Jennifer Leeacb7cfb2018-06-07 16:08:15 -0700404 }
Jennifer Leeacb7cfb2018-06-07 16:08:15 -0700405
Ed Tanous1abe55e2018-09-05 08:30:59 -0700406 private:
407 void doGet(crow::Response &res, const crow::Request &req,
408 const std::vector<std::string> &params) override
409 {
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530410 std::shared_ptr<AsyncResp> aResp = std::make_shared<AsyncResp>(res);
411 res.jsonValue["@odata.type"] = "#UpdateService.v1_4_0.UpdateService";
Ed Tanous0f74e642018-11-12 15:17:05 -0800412 res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
Ed Tanous0f74e642018-11-12 15:17:05 -0800413 res.jsonValue["Id"] = "UpdateService";
414 res.jsonValue["Description"] = "Service for Software Update";
415 res.jsonValue["Name"] = "Update Service";
416 res.jsonValue["HttpPushUri"] = "/redfish/v1/UpdateService";
417 // UpdateService cannot be disabled
418 res.jsonValue["ServiceEnabled"] = true;
419 res.jsonValue["FirmwareInventory"] = {
420 {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}};
Andrew Geissler0554c982019-04-23 14:40:12 -0500421#ifdef BMCWEB_INSECURE_ENABLE_REDFISH_FW_TFTP_UPDATE
422 // Update Actions object.
423 nlohmann::json &updateSvcSimpleUpdate =
424 res.jsonValue["Actions"]["#UpdateService.SimpleUpdate"];
425 updateSvcSimpleUpdate["target"] =
426 "/redfish/v1/UpdateService/Actions/UpdateService.SimpleUpdate";
427 updateSvcSimpleUpdate["TransferProtocol@Redfish.AllowableValues"] = {
428 "TFTP"};
429#endif
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530430 // Get the current ApplyTime value
431 crow::connections::systemBus->async_method_call(
432 [aResp](const boost::system::error_code ec,
433 const std::variant<std::string> &applyTime) {
434 if (ec)
435 {
436 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
437 messages::internalError(aResp->res);
438 return;
439 }
440
441 const std::string *s = std::get_if<std::string>(&applyTime);
442 if (s == nullptr)
443 {
444 return;
445 }
446 // Store the ApplyTime Value
447 if (*s == "xyz.openbmc_project.Software.ApplyTime."
448 "RequestedApplyTimes.Immediate")
449 {
450 aResp->res.jsonValue["HttpPushUriOptions"]
451 ["HttpPushUriApplyTime"]["ApplyTime"] =
452 "Immediate";
453 }
454 else if (*s == "xyz.openbmc_project.Software.ApplyTime."
455 "RequestedApplyTimes.OnReset")
456 {
457 aResp->res.jsonValue["HttpPushUriOptions"]
458 ["HttpPushUriApplyTime"]["ApplyTime"] =
459 "OnReset";
460 }
461 },
462 "xyz.openbmc_project.Settings",
463 "/xyz/openbmc_project/software/apply_time",
464 "org.freedesktop.DBus.Properties", "Get",
465 "xyz.openbmc_project.Software.ApplyTime", "RequestedApplyTime");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700466 }
Andrew Geissler0e7de462019-03-04 19:11:54 -0600467
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530468 void doPatch(crow::Response &res, const crow::Request &req,
469 const std::vector<std::string> &params) override
470 {
471 BMCWEB_LOG_DEBUG << "doPatch...";
472
473 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530474
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530475 std::optional<nlohmann::json> pushUriOptions;
476 if (!json_util::readJson(req, res, "HttpPushUriOptions",
477 pushUriOptions))
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530478 {
479 return;
480 }
481
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530482 if (pushUriOptions)
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530483 {
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530484 std::optional<nlohmann::json> pushUriApplyTime;
485 if (!json_util::readJson(*pushUriOptions, res,
486 "HttpPushUriApplyTime", pushUriApplyTime))
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530487 {
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530488 return;
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530489 }
490
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530491 if (pushUriApplyTime)
492 {
493 std::optional<std::string> applyTime;
494 if (!json_util::readJson(*pushUriApplyTime, res, "ApplyTime",
495 applyTime))
496 {
497 return;
498 }
499
500 if (applyTime)
501 {
502 std::string applyTimeNewVal;
503 if (applyTime == "Immediate")
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530504 {
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530505 applyTimeNewVal =
506 "xyz.openbmc_project.Software.ApplyTime."
507 "RequestedApplyTimes.Immediate";
508 }
509 else if (applyTime == "OnReset")
510 {
511 applyTimeNewVal =
512 "xyz.openbmc_project.Software.ApplyTime."
513 "RequestedApplyTimes.OnReset";
514 }
515 else
516 {
517 BMCWEB_LOG_INFO
518 << "ApplyTime value is not in the list of "
519 "acceptable values";
520 messages::propertyValueNotInList(
521 asyncResp->res, *applyTime, "ApplyTime");
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530522 return;
523 }
Jayashankar Padath274dfe62019-08-23 12:30:57 +0530524
525 // Set the requested image apply time value
526 crow::connections::systemBus->async_method_call(
527 [asyncResp](const boost::system::error_code ec) {
528 if (ec)
529 {
530 BMCWEB_LOG_ERROR << "D-Bus responses error: "
531 << ec;
532 messages::internalError(asyncResp->res);
533 return;
534 }
535 messages::success(asyncResp->res);
536 },
537 "xyz.openbmc_project.Settings",
538 "/xyz/openbmc_project/software/apply_time",
539 "org.freedesktop.DBus.Properties", "Set",
540 "xyz.openbmc_project.Software.ApplyTime",
541 "RequestedApplyTime",
542 std::variant<std::string>{applyTimeNewVal});
543 }
544 }
Jayashankar Padathfa1a5a32019-05-28 23:54:37 +0530545 }
546 }
547
Ed Tanous1abe55e2018-09-05 08:30:59 -0700548 void doPost(crow::Response &res, const crow::Request &req,
549 const std::vector<std::string> &params) override
550 {
551 BMCWEB_LOG_DEBUG << "doPost...";
Jennifer Leeacb7cfb2018-06-07 16:08:15 -0700552
Andrew Geissler0e7de462019-03-04 19:11:54 -0600553 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700554
Andrew Geissler86adcd62019-04-18 10:58:05 -0500555 // Setup callback for when new software detected
556 monitorForSoftwareAvailable(asyncResp, req);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700557
558 std::string filepath(
559 "/tmp/images/" +
560 boost::uuids::to_string(boost::uuids::random_generator()()));
561 BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
562 std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
563 std::ofstream::trunc);
564 out << req.body;
565 out.close();
566 BMCWEB_LOG_DEBUG << "file upload complete!!";
567 }
Jennifer Lee729dae72018-04-24 15:59:34 -0700568};
Ed Tanousc711bf82018-07-30 16:31:33 -0700569
Ed Tanous1abe55e2018-09-05 08:30:59 -0700570class SoftwareInventoryCollection : public Node
571{
572 public:
573 template <typename CrowApp>
574 SoftwareInventoryCollection(CrowApp &app) :
575 Node(app, "/redfish/v1/UpdateService/FirmwareInventory/")
576 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700577 entityPrivileges = {
578 {boost::beast::http::verb::get, {{"Login"}}},
579 {boost::beast::http::verb::head, {{"Login"}}},
580 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
581 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
582 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
583 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Jennifer Lee729dae72018-04-24 15:59:34 -0700584 }
Jennifer Lee729dae72018-04-24 15:59:34 -0700585
Ed Tanous1abe55e2018-09-05 08:30:59 -0700586 private:
587 void doGet(crow::Response &res, const crow::Request &req,
588 const std::vector<std::string> &params) override
589 {
590 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous0f74e642018-11-12 15:17:05 -0800591 res.jsonValue["@odata.type"] =
592 "#SoftwareInventoryCollection.SoftwareInventoryCollection";
593 res.jsonValue["@odata.id"] =
594 "/redfish/v1/UpdateService/FirmwareInventory";
Ed Tanous0f74e642018-11-12 15:17:05 -0800595 res.jsonValue["Name"] = "Software Inventory Collection";
Ed Tanousc711bf82018-07-30 16:31:33 -0700596
Ed Tanous1abe55e2018-09-05 08:30:59 -0700597 crow::connections::systemBus->async_method_call(
598 [asyncResp](
599 const boost::system::error_code ec,
600 const std::vector<std::pair<
601 std::string, std::vector<std::pair<
602 std::string, std::vector<std::string>>>>>
603 &subtree) {
604 if (ec)
605 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700606 messages::internalError(asyncResp->res);
Ed Tanousc711bf82018-07-30 16:31:33 -0700607 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700608 }
609 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
610 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jennifer Lee6c4eb9d2018-05-22 10:58:31 -0700611
Ed Tanous1abe55e2018-09-05 08:30:59 -0700612 for (auto &obj : subtree)
613 {
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700614 // if can't parse fw id then return
Ed Tanous27826b52018-10-29 11:40:58 -0700615 std::size_t idPos;
616 if ((idPos = obj.first.rfind("/")) == std::string::npos)
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700617 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700618 messages::internalError(asyncResp->res);
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700619 BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
620 return;
621 }
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700622 std::string swId = obj.first.substr(idPos + 1);
623
Andrew Geisslere0dd8052019-06-18 16:05:10 -0500624 nlohmann::json &members =
625 asyncResp->res.jsonValue["Members"];
626 members.push_back(
627 {{"@odata.id", "/redfish/v1/UpdateService/"
628 "FirmwareInventory/" +
629 swId}});
630 asyncResp->res.jsonValue["Members@odata.count"] =
631 members.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700632 }
633 },
Andrew Geissler2830a9c2020-01-06 10:18:11 -0600634 // Note that only firmware levels associated with a device are
635 // stored under /xyz/openbmc_project/software therefore to ensure
636 // only real FirmwareInventory items are returned, this full object
637 // path must be used here as input to mapper
Ed Tanous1abe55e2018-09-05 08:30:59 -0700638 "xyz.openbmc_project.ObjectMapper",
639 "/xyz/openbmc_project/object_mapper",
Andrew Geissler2830a9c2020-01-06 10:18:11 -0600640 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
641 "/xyz/openbmc_project/software", static_cast<int32_t>(0),
Ed Tanous1abe55e2018-09-05 08:30:59 -0700642 std::array<const char *, 1>{
643 "xyz.openbmc_project.Software.Version"});
644 }
Jennifer Lee729dae72018-04-24 15:59:34 -0700645};
646
Ed Tanous1abe55e2018-09-05 08:30:59 -0700647class SoftwareInventory : public Node
648{
649 public:
650 template <typename CrowApp>
651 SoftwareInventory(CrowApp &app) :
652 Node(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/",
653 std::string())
654 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700655 entityPrivileges = {
656 {boost::beast::http::verb::get, {{"Login"}}},
657 {boost::beast::http::verb::head, {{"Login"}}},
658 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
659 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
660 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
661 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
662 }
663
664 private:
Andrew Geissler87d84722019-02-28 14:28:39 -0600665 /* Fill related item links (i.e. bmc, bios) in for inventory */
666 static void getRelatedItems(std::shared_ptr<AsyncResp> aResp,
667 const std::string &purpose)
668 {
669 if (purpose == fw_util::bmcPurpose)
670 {
671 nlohmann::json &members = aResp->res.jsonValue["RelatedItem"];
672 members.push_back({{"@odata.id", "/redfish/v1/Managers/bmc"}});
673 aResp->res.jsonValue["Members@odata.count"] = members.size();
674 }
675 else if (purpose == fw_util::biosPurpose)
676 {
Gunnar Millsf723d732020-02-26 11:20:49 -0600677 nlohmann::json &members = aResp->res.jsonValue["RelatedItem"];
678 members.push_back(
679 {{"@odata.id", "/redfish/v1/Systems/system/Bios"}});
680 aResp->res.jsonValue["Members@odata.count"] = members.size();
Andrew Geissler87d84722019-02-28 14:28:39 -0600681 }
682 else
683 {
684 BMCWEB_LOG_ERROR << "Unknown software purpose " << purpose;
685 }
686 }
687
Ed Tanous1abe55e2018-09-05 08:30:59 -0700688 void doGet(crow::Response &res, const crow::Request &req,
689 const std::vector<std::string> &params) override
690 {
691 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700692
693 if (params.size() != 1)
694 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700695 messages::internalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700696 res.end();
697 return;
698 }
699
Ed Tanous3ae837c2018-08-07 14:41:19 -0700700 std::shared_ptr<std::string> swId =
Ed Tanous1abe55e2018-09-05 08:30:59 -0700701 std::make_shared<std::string>(params[0]);
702
703 res.jsonValue["@odata.id"] =
Ed Tanous3ae837c2018-08-07 14:41:19 -0700704 "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700705
706 crow::connections::systemBus->async_method_call(
Ed Tanous3ae837c2018-08-07 14:41:19 -0700707 [asyncResp, swId](
Ed Tanous1abe55e2018-09-05 08:30:59 -0700708 const boost::system::error_code ec,
709 const std::vector<std::pair<
710 std::string, std::vector<std::pair<
711 std::string, std::vector<std::string>>>>>
712 &subtree) {
713 BMCWEB_LOG_DEBUG << "doGet callback...";
714 if (ec)
715 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700716 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700717 return;
718 }
719
Andrew Geissler69132282019-07-01 11:00:35 -0500720 // Ensure we find our input swId, otherwise return an error
721 bool found = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700722 for (const std::pair<
723 std::string,
724 std::vector<
725 std::pair<std::string, std::vector<std::string>>>>
726 &obj : subtree)
727 {
Ed Tanous3ae837c2018-08-07 14:41:19 -0700728 if (boost::ends_with(obj.first, *swId) != true)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700729 {
730 continue;
731 }
732
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700733 if (obj.second.size() < 1)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700734 {
735 continue;
736 }
737
Andrew Geissler69132282019-07-01 11:00:35 -0500738 found = true;
Andrew Geisslere0dd8052019-06-18 16:05:10 -0500739 fw_util::getFwStatus(asyncResp, swId, obj.second[0].first);
740
Ed Tanous1abe55e2018-09-05 08:30:59 -0700741 crow::connections::systemBus->async_method_call(
742 [asyncResp,
Ed Tanous3ae837c2018-08-07 14:41:19 -0700743 swId](const boost::system::error_code error_code,
744 const boost::container::flat_map<
745 std::string, VariantType> &propertiesList) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700746 if (error_code)
747 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700748 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700749 return;
750 }
751 boost::container::flat_map<
752 std::string, VariantType>::const_iterator it =
753 propertiesList.find("Purpose");
754 if (it == propertiesList.end())
755 {
756 BMCWEB_LOG_DEBUG
757 << "Can't find property \"Purpose\"!";
Jason M. Billsf12894f2018-10-09 12:45:45 -0700758 messages::propertyMissing(asyncResp->res,
759 "Purpose");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700760 return;
761 }
Ed Tanous3ae837c2018-08-07 14:41:19 -0700762 const std::string *swInvPurpose =
Ed Tanousabf2add2019-01-22 16:40:12 -0800763 std::get_if<std::string>(&it->second);
Ed Tanous3ae837c2018-08-07 14:41:19 -0700764 if (swInvPurpose == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700765 {
766 BMCWEB_LOG_DEBUG
767 << "wrong types for property\"Purpose\"!";
Jason M. Billsf12894f2018-10-09 12:45:45 -0700768 messages::propertyValueTypeError(asyncResp->res,
769 "", "Purpose");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700770 return;
771 }
772
Ed Tanous3ae837c2018-08-07 14:41:19 -0700773 BMCWEB_LOG_DEBUG << "swInvPurpose = "
774 << *swInvPurpose;
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700775 it = propertiesList.find("Version");
776 if (it == propertiesList.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700777 {
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700778 BMCWEB_LOG_DEBUG
779 << "Can't find property \"Version\"!";
Jason M. Billsf12894f2018-10-09 12:45:45 -0700780 messages::propertyMissing(asyncResp->res,
781 "Version");
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700782 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700783 }
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700784
785 BMCWEB_LOG_DEBUG << "Version found!";
786
787 const std::string *version =
Ed Tanousabf2add2019-01-22 16:40:12 -0800788 std::get_if<std::string>(&it->second);
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700789
790 if (version == nullptr)
791 {
792 BMCWEB_LOG_DEBUG
793 << "Can't find property \"Version\"!";
Jason M. Billsf12894f2018-10-09 12:45:45 -0700794
795 messages::propertyValueTypeError(asyncResp->res,
796 "", "Version");
Jennifer Leef4b65ab2018-09-18 12:00:13 -0700797 return;
798 }
799 asyncResp->res.jsonValue["Version"] = *version;
800 asyncResp->res.jsonValue["Id"] = *swId;
Andrew Geissler54daabe2019-02-13 13:54:15 -0600801
802 // swInvPurpose is of format:
803 // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
James Feiste2e96772019-10-03 10:51:43 -0700804 // Translate this to "ABC image"
Andrew Geissler54daabe2019-02-13 13:54:15 -0600805 size_t endDesc = swInvPurpose->rfind(".");
806 if (endDesc == std::string::npos)
807 {
808 messages::internalError(asyncResp->res);
809 return;
810 }
811 endDesc++;
812 if (endDesc >= swInvPurpose->size())
813 {
814 messages::internalError(asyncResp->res);
815 return;
816 }
817
818 std::string formatDesc =
819 swInvPurpose->substr(endDesc);
820 asyncResp->res.jsonValue["Description"] =
James Feiste2e96772019-10-03 10:51:43 -0700821 formatDesc + " image";
Andrew Geissler87d84722019-02-28 14:28:39 -0600822 getRelatedItems(asyncResp, *swInvPurpose);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700823 },
824 obj.second[0].first, obj.first,
825 "org.freedesktop.DBus.Properties", "GetAll",
826 "xyz.openbmc_project.Software.Version");
827 }
Andrew Geissler69132282019-07-01 11:00:35 -0500828 if (!found)
829 {
830 BMCWEB_LOG_ERROR << "Input swID " + *swId + " not found!";
831 messages::resourceMissingAtURI(
832 asyncResp->res,
833 "/redfish/v1/UpdateService/FirmwareInventory/" + *swId);
834 return;
835 }
Ayushi Smriti4e68c452019-09-04 14:37:55 +0530836 asyncResp->res.jsonValue["@odata.type"] =
837 "#SoftwareInventory.v1_1_0.SoftwareInventory";
Ayushi Smriti4e68c452019-09-04 14:37:55 +0530838 asyncResp->res.jsonValue["Name"] = "Software Inventory";
Ayushi Smriti4e68c452019-09-04 14:37:55 +0530839 asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
AppaRao Puli3f8a7432020-01-29 02:36:32 +0530840
841 asyncResp->res.jsonValue["Updateable"] = false;
842 fw_util::getFwUpdateableStatus(asyncResp, swId);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700843 },
844 "xyz.openbmc_project.ObjectMapper",
845 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700846 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/",
847 static_cast<int32_t>(0),
Ed Tanous1abe55e2018-09-05 08:30:59 -0700848 std::array<const char *, 1>{
849 "xyz.openbmc_project.Software.Version"});
850 }
851};
852
853} // namespace redfish