blob: d3ea1a81912f9c0af4ca27b63591a9d956fc5be7 [file] [log] [blame]
Sampa Misraaea5dde2020-08-31 08:33:47 -05001#include "inband_code_update.hpp"
2
Sagar Srinivas78a225a2020-08-27 00:52:20 -05003#include "libpldmresponder/pdr.hpp"
Sampa Misraaea5dde2020-08-31 08:33:47 -05004#include "oem_ibm_handler.hpp"
5#include "xyz/openbmc_project/Common/error.hpp"
6
Adriana Kobylak727f7382020-09-01 14:38:25 -05007#include <arpa/inet.h>
George Liuc453e162022-12-21 17:16:23 +08008#include <libpldm/entity.h>
Adriana Kobylak727f7382020-09-01 14:38:25 -05009
Riya Dixit49cfb132023-03-02 04:26:53 -060010#include <phosphor-logging/lg2.hpp>
Sampa Misraaea5dde2020-08-31 08:33:47 -050011#include <sdbusplus/server.hpp>
12#include <xyz/openbmc_project/Dump/NewDump/server.hpp>
13
14#include <exception>
Adriana Kobylak727f7382020-09-01 14:38:25 -050015#include <fstream>
Riya Dixit49cfb132023-03-02 04:26:53 -060016
17PHOSPHOR_LOG2_USING;
18
Sampa Misraaea5dde2020-08-31 08:33:47 -050019namespace pldm
20{
Brad Bishop5079ac42021-08-19 18:35:06 -040021using namespace utils;
22
Sampa Misraaea5dde2020-08-31 08:33:47 -050023namespace responder
24{
25using namespace oem_ibm_platform;
26
Adriana Kobylakfa810d72020-10-16 16:27:28 -050027/** @brief Directory where the lid files without a header are stored */
28auto lidDirPath = fs::path(LID_STAGING_DIR) / "lid";
29
Adriana Kobylaka1f158c2020-11-09 12:47:29 -060030/** @brief Directory where the image files are stored as they are built */
31auto imageDirPath = fs::path(LID_STAGING_DIR) / "image";
32
Adriana Kobylak837fb472020-10-16 16:53:42 -050033/** @brief Directory where the code update tarball files are stored */
34auto updateDirPath = fs::path(LID_STAGING_DIR) / "update";
35
Adriana Kobylaka1f158c2020-11-09 12:47:29 -060036/** @brief The file name of the code update tarball */
37constexpr auto tarImageName = "image.tar";
38
Adriana Kobylak837fb472020-10-16 16:53:42 -050039/** @brief The file name of the hostfw image */
Adriana Kobylak131327e2021-03-10 18:45:24 +000040constexpr auto hostfwImageName = "image-hostfw";
Adriana Kobylak837fb472020-10-16 16:53:42 -050041
Adriana Kobylaka1f158c2020-11-09 12:47:29 -060042/** @brief The path to the code update tarball file */
43auto tarImagePath = fs::path(imageDirPath) / tarImageName;
44
Adriana Kobylak837fb472020-10-16 16:53:42 -050045/** @brief The path to the hostfw image */
46auto hostfwImagePath = fs::path(imageDirPath) / hostfwImageName;
47
48/** @brief The path to the tarball file expected by the phosphor software
49 * manager */
50auto updateImagePath = fs::path("/tmp/images") / tarImageName;
51
Sampa Misraaea5dde2020-08-31 08:33:47 -050052std::string CodeUpdate::fetchCurrentBootSide()
53{
54 return currBootSide;
55}
56
57std::string CodeUpdate::fetchNextBootSide()
58{
59 return nextBootSide;
60}
61
62int CodeUpdate::setCurrentBootSide(const std::string& currSide)
63{
64 currBootSide = currSide;
65 return PLDM_SUCCESS;
66}
67
68int CodeUpdate::setNextBootSide(const std::string& nextSide)
69{
70 nextBootSide = nextSide;
71 std::string objPath{};
72 if (nextBootSide == currBootSide)
73 {
74 objPath = runningVersion;
75 }
76 else
77 {
78 objPath = nonRunningVersion;
79 }
80 if (objPath.empty())
81 {
Riya Dixit49cfb132023-03-02 04:26:53 -060082 error("no nonRunningVersion present");
Sampa Misraaea5dde2020-08-31 08:33:47 -050083 return PLDM_PLATFORM_INVALID_STATE_VALUE;
84 }
85
86 pldm::utils::DBusMapping dbusMapping{objPath, redundancyIntf, "Priority",
87 "uint8_t"};
88 uint8_t val = 0;
89 pldm::utils::PropertyValue value = static_cast<uint8_t>(val);
90 try
91 {
92 dBusIntf->setDbusProperty(dbusMapping, value);
93 }
94 catch (const std::exception& e)
95 {
Riya Dixitfc84f632024-04-06 14:00:02 -050096 error("Failed to set the next boot side to {PATH} , error - {ERROR}",
97 "PATH", objPath, "ERROR", e);
Sampa Misraaea5dde2020-08-31 08:33:47 -050098 return PLDM_ERROR;
99 }
100 return PLDM_SUCCESS;
101}
102
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500103int CodeUpdate::setRequestedApplyTime()
104{
105 int rc = PLDM_SUCCESS;
106 pldm::utils::PropertyValue value =
107 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
108 DBusMapping dbusMapping;
109 dbusMapping.objectPath = "/xyz/openbmc_project/software/apply_time";
110 dbusMapping.interface = "xyz.openbmc_project.Software.ApplyTime";
111 dbusMapping.propertyName = "RequestedApplyTime";
112 dbusMapping.propertyType = "string";
113 try
114 {
115 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
116 }
117 catch (const std::exception& e)
118 {
Sagar Srinivas90403472023-08-21 06:08:55 -0500119 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500120 "Failed to set property '{PROPERTY}' at path '{PATH}' and interface '{INTERFACE}', error - {ERROR}",
121 "PATH", dbusMapping.objectPath, "INTERFACE", dbusMapping.interface,
122 "PROPERTY", dbusMapping.propertyName, "ERROR", e);
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500123 rc = PLDM_ERROR;
124 }
125 return rc;
126}
127
128int CodeUpdate::setRequestedActivation()
129{
130 int rc = PLDM_SUCCESS;
131 pldm::utils::PropertyValue value =
132 "xyz.openbmc_project.Software.Activation.RequestedActivations.Active";
133 DBusMapping dbusMapping;
134 dbusMapping.objectPath = newImageId;
135 dbusMapping.interface = "xyz.openbmc_project.Software.Activation";
136 dbusMapping.propertyName = "RequestedActivation";
137 dbusMapping.propertyType = "string";
138 try
139 {
140 pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value);
141 }
142 catch (const std::exception& e)
143 {
Sagar Srinivas90403472023-08-21 06:08:55 -0500144 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500145 "Failed to set property {PROPERTY} at path '{PATH}' and interface '{INTERFACE}', error - {ERROR}",
146 "PATH", dbusMapping.objectPath, "INTERFACE", dbusMapping.interface,
147 "PROPERTY", dbusMapping.propertyName, "ERROR", e);
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500148 rc = PLDM_ERROR;
149 }
Sagar Srinivascfdbca72020-09-22 10:03:35 -0500150 return rc;
151}
152
Sampa Misraaea5dde2020-08-31 08:33:47 -0500153void CodeUpdate::setVersions()
154{
155 static constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
156 static constexpr auto functionalObjPath =
157 "/xyz/openbmc_project/software/functional";
158 static constexpr auto activeObjPath =
159 "/xyz/openbmc_project/software/active";
160 static constexpr auto propIntf = "org.freedesktop.DBus.Properties";
161
162 auto& bus = dBusIntf->getBus();
Sampa Misraaea5dde2020-08-31 08:33:47 -0500163 try
164 {
165 auto method = bus.new_method_call(mapperService, functionalObjPath,
166 propIntf, "Get");
167 method.append("xyz.openbmc_project.Association", "endpoints");
168 std::variant<std::vector<std::string>> paths;
169
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500170 auto reply = bus.call(method, dbusTimeout);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500171 reply.read(paths);
172
173 runningVersion = std::get<std::vector<std::string>>(paths)[0];
174
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400175 auto method1 =
176 bus.new_method_call(mapperService, activeObjPath, propIntf, "Get");
Sampa Misraaea5dde2020-08-31 08:33:47 -0500177 method1.append("xyz.openbmc_project.Association", "endpoints");
178
vkaverap@in.ibm.com91a092f2023-09-18 23:39:44 -0500179 auto reply1 = bus.call(method1, dbusTimeout);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500180 reply1.read(paths);
181 for (const auto& path : std::get<std::vector<std::string>>(paths))
182 {
183 if (path != runningVersion)
184 {
185 nonRunningVersion = path;
186 break;
187 }
188 }
189 }
190 catch (const std::exception& e)
191 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600192 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500193 "Failed to make a d-bus call to Object Mapper Association, error - {ERROR}",
194 "ERROR", e);
Sampa Misraaea5dde2020-08-31 08:33:47 -0500195 return;
196 }
197
198 using namespace sdbusplus::bus::match::rules;
199 captureNextBootSideChange.push_back(
Patrick Williams84b790c2022-07-22 19:26:56 -0500200 std::make_unique<sdbusplus::bus::match_t>(
Sampa Misraaea5dde2020-08-31 08:33:47 -0500201 pldm::utils::DBusHandler::getBus(),
202 propertiesChanged(runningVersion, redundancyIntf),
Patrick Williams84b790c2022-07-22 19:26:56 -0500203 [this](sdbusplus::message_t& msg) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400204 DbusChangedProps props;
205 std::string iface;
206 msg.read(iface, props);
207 processPriorityChangeNotification(props);
208 }));
Patrick Williams84b790c2022-07-22 19:26:56 -0500209 fwUpdateMatcher.push_back(std::make_unique<sdbusplus::bus::match_t>(
Sampa Misraaea5dde2020-08-31 08:33:47 -0500210 pldm::utils::DBusHandler::getBus(),
211 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
212 "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
Patrick Williams84b790c2022-07-22 19:26:56 -0500213 [this](sdbusplus::message_t& msg) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400214 DBusInterfaceAdded interfaces;
215 sdbusplus::message::object_path path;
216 msg.read(path, interfaces);
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600217
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400218 for (auto& interface : interfaces)
Sampa Misraaea5dde2020-08-31 08:33:47 -0500219 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400220 if (interface.first ==
221 "xyz.openbmc_project.Software.Activation")
Patrick Williams6da4f912023-05-10 07:50:53 -0500222 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400223 auto imageInterface =
224 "xyz.openbmc_project.Software.Activation";
225 auto imageObjPath = path.str.c_str();
226
227 try
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500228 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400229 auto propVal = dBusIntf->getDbusPropertyVariant(
230 imageObjPath, "Activation", imageInterface);
231 const auto& imageProp = std::get<std::string>(propVal);
232 if (imageProp == "xyz.openbmc_project.Software."
233 "Activation.Activations.Ready" &&
234 isCodeUpdateInProgress())
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500235 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400236 newImageId = path.str;
237 if (!imageActivationMatch)
238 {
239 imageActivationMatch = std::make_unique<
240 sdbusplus::bus::match_t>(
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600241 pldm::utils::DBusHandler::getBus(),
242 propertiesChanged(newImageId,
243 "xyz.openbmc_project."
244 "Software.Activation"),
Patrick Williams84b790c2022-07-22 19:26:56 -0500245 [this](sdbusplus::message_t& msg) {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400246 DbusChangedProps props;
247 std::string iface;
248 msg.read(iface, props);
249 const auto itr =
250 props.find("Activation");
251 if (itr != props.end())
252 {
253 PropertyValue value = itr->second;
254 auto propVal =
255 std::get<std::string>(value);
256 if (propVal ==
257 "xyz.openbmc_project.Software."
258 "Activation.Activations.Active")
259 {
260 CodeUpdateState state =
261 CodeUpdateState::END;
262 setCodeUpdateProgress(false);
263 auto sensorId =
264 getFirmwareUpdateSensor();
265 sendStateSensorEvent(
266 sensorId,
267 PLDM_STATE_SENSOR_STATE, 0,
268 uint8_t(state),
269 uint8_t(CodeUpdateState::
270 START));
271 newImageId.clear();
272 }
273 else if (propVal ==
274 "xyz.openbmc_project."
275 "Software.Activation."
276 "Activations.Failed" ||
277 propVal ==
278 "xyz.openbmc_"
279 "project.Software."
280 "Activation."
281 "Activations."
282 "Invalid")
283 {
284 CodeUpdateState state =
285 CodeUpdateState::FAIL;
286 setCodeUpdateProgress(false);
287 auto sensorId =
288 getFirmwareUpdateSensor();
289 sendStateSensorEvent(
290 sensorId,
291 PLDM_STATE_SENSOR_STATE, 0,
292 uint8_t(state),
293 uint8_t(CodeUpdateState::
294 START));
295 newImageId.clear();
296 }
297 }
298 });
299 }
300 auto rc = setRequestedActivation();
301 if (rc != PLDM_SUCCESS)
302 {
303 error("Could not set Requested Activation");
304 CodeUpdateState state = CodeUpdateState::FAIL;
305 setCodeUpdateProgress(false);
306 auto sensorId = getFirmwareUpdateSensor();
307 sendStateSensorEvent(
308 sensorId, PLDM_STATE_SENSOR_STATE, 0,
309 uint8_t(state),
310 uint8_t(CodeUpdateState::START));
311 }
312 break;
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500313 }
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400314 }
315 catch (const sdbusplus::exception_t& e)
316 {
317 error(
318 "Failed to get activation status for interface '{INTERFACE}' and object path '{PATH}', error - {ERROR}",
319 "ERROR", e, "INTERFACE", imageInterface, "PATH",
320 imageObjPath);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500321 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500322 }
323 }
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400324 }));
Sampa Misraaea5dde2020-08-31 08:33:47 -0500325}
326
327void CodeUpdate::processPriorityChangeNotification(
328 const DbusChangedProps& chProperties)
329{
330 static constexpr auto propName = "Priority";
331 const auto it = chProperties.find(propName);
332 if (it == chProperties.end())
333 {
334 return;
335 }
336 uint8_t newVal = std::get<uint8_t>(it->second);
337 nextBootSide = (newVal == 0) ? currBootSide
338 : ((currBootSide == Tside) ? Pside : Tside);
339}
340
341void CodeUpdate::setOemPlatformHandler(
342 pldm::responder::oem_platform::Handler* handler)
343{
344 oemPlatformHandler = handler;
345}
346
Varsha Kaverappa3ca29df2020-09-27 12:39:22 -0500347void CodeUpdate::clearDirPath(const std::string& dirPath)
348{
Manojkiran Eda78124cf2022-08-29 11:22:43 +0530349 if (!fs::is_directory(dirPath))
Varsha Kaverappa3ca29df2020-09-27 12:39:22 -0500350 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500351 error("The directory '{PATH}' does not exist", "PATH", dirPath);
Manojkiran Eda78124cf2022-08-29 11:22:43 +0530352 return;
Varsha Kaverappa3ca29df2020-09-27 12:39:22 -0500353 }
Manojkiran Eda78124cf2022-08-29 11:22:43 +0530354 for (const auto& iter : fs::directory_iterator(dirPath))
355 {
356 fs::remove_all(iter);
357 }
Varsha Kaverappa3ca29df2020-09-27 12:39:22 -0500358}
359
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500360void CodeUpdate::sendStateSensorEvent(
361 uint16_t sensorId, enum sensor_event_class_states sensorEventClass,
362 uint8_t sensorOffset, uint8_t eventState, uint8_t prevEventState)
363{
364 pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler =
365 dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>(
366 oemPlatformHandler);
367 oemIbmPlatformHandler->sendStateSensorEvent(
368 sensorId, sensorEventClass, sensorOffset, eventState, prevEventState);
369}
370
371void CodeUpdate::deleteImage()
372{
373 static constexpr auto UPDATER_SERVICE =
374 "xyz.openbmc_project.Software.BMC.Updater";
375 static constexpr auto SW_OBJ_PATH = "/xyz/openbmc_project/software";
376 static constexpr auto DELETE_INTF =
377 "xyz.openbmc_project.Collection.DeleteAll";
378
379 auto& bus = dBusIntf->getBus();
380 try
381 {
382 auto method = bus.new_method_call(UPDATER_SERVICE, SW_OBJ_PATH,
383 DELETE_INTF, "DeleteAll");
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000384 bus.call_noreply(method, dbusTimeout);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500385 }
386 catch (const std::exception& e)
387 {
Sagar Srinivas90403472023-08-21 06:08:55 -0500388 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500389 "Failed to delete image at path '{PATH}' and interface '{INTERFACE}', error - {ERROR}",
390 "PATH", SW_OBJ_PATH, "INTERFACE", DELETE_INTF, "ERROR", e);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500391 return;
392 }
393}
394
Sampa Misraaea5dde2020-08-31 08:33:47 -0500395uint8_t fetchBootSide(uint16_t entityInstance, CodeUpdate* codeUpdate)
396{
397 uint8_t sensorOpState = tSideNum;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500398 if (entityInstance == 0)
399 {
400 auto currSide = codeUpdate->fetchCurrentBootSide();
401 if (currSide == Pside)
402 {
403 sensorOpState = pSideNum;
404 }
405 }
406 else if (entityInstance == 1)
407 {
408 auto nextSide = codeUpdate->fetchNextBootSide();
409 if (nextSide == Pside)
410 {
411 sensorOpState = pSideNum;
412 }
413 }
414 else
415 {
416 sensorOpState = PLDM_SENSOR_UNKNOWN;
417 }
418
419 return sensorOpState;
420}
421
422int setBootSide(uint16_t entityInstance, uint8_t currState,
423 const std::vector<set_effecter_state_field>& stateField,
424 CodeUpdate* codeUpdate)
425{
426 int rc = PLDM_SUCCESS;
427 auto side = (stateField[currState].effecter_state == pSideNum) ? "P" : "T";
428
429 if (entityInstance == 0)
430 {
431 rc = codeUpdate->setCurrentBootSide(side);
432 }
433 else if (entityInstance == 1)
434 {
435 rc = codeUpdate->setNextBootSide(side);
436 }
437 else
438 {
439 rc = PLDM_PLATFORM_INVALID_STATE_VALUE;
440 }
441 return rc;
442}
443
Adriana Kobylak837fb472020-10-16 16:53:42 -0500444template <typename... T>
Patrick Williams6da4f912023-05-10 07:50:53 -0500445int executeCmd(const T&... t)
Adriana Kobylak837fb472020-10-16 16:53:42 -0500446{
447 std::stringstream cmd;
448 ((cmd << t << " "), ...) << std::endl;
449 FILE* pipe = popen(cmd.str().c_str(), "r");
450 if (!pipe)
451 {
452 throw std::runtime_error("popen() failed!");
453 }
454 int rc = pclose(pipe);
455 if (WEXITSTATUS(rc))
456 {
457 std::cerr << "Error executing: ";
458 ((std::cerr << " " << t), ...);
459 std::cerr << "\n";
460 return -1;
461 }
462
463 return 0;
464}
465
Adriana Kobylak727f7382020-09-01 14:38:25 -0500466int processCodeUpdateLid(const std::string& filePath)
467{
468 struct LidHeader
469 {
470 uint16_t magicNumber;
Adriana Kobylak86d14182020-10-16 16:11:08 -0500471 uint16_t headerVersion;
472 uint32_t lidNumber;
473 uint32_t lidDate;
474 uint16_t lidTime;
475 uint16_t lidClass;
476 uint32_t lidCrc;
477 uint32_t lidSize;
478 uint32_t headerSize;
Adriana Kobylak727f7382020-09-01 14:38:25 -0500479 };
480 LidHeader header;
481
482 std::ifstream ifs(filePath, std::ios::in | std::ios::binary);
483 if (!ifs)
484 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500485 error("Failed to opening file '{FILE}' ifstream", "PATH", filePath);
Adriana Kobylak727f7382020-09-01 14:38:25 -0500486 return PLDM_ERROR;
487 }
488 ifs.seekg(0);
489 ifs.read(reinterpret_cast<char*>(&header), sizeof(header));
Adriana Kobylak727f7382020-09-01 14:38:25 -0500490
Adriana Kobylak86d14182020-10-16 16:11:08 -0500491 // File size should be the value of lid size minus the header size
492 auto fileSize = fs::file_size(filePath);
493 fileSize -= htonl(header.headerSize);
494 if (fileSize < htonl(header.lidSize))
495 {
496 // File is not completely written yet
Adriana Kobylakfa810d72020-10-16 16:27:28 -0500497 ifs.close();
Adriana Kobylak86d14182020-10-16 16:11:08 -0500498 return PLDM_SUCCESS;
499 }
500
Adriana Kobylak727f7382020-09-01 14:38:25 -0500501 constexpr auto magicNumber = 0x0222;
502 if (htons(header.magicNumber) != magicNumber)
503 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500504 error("Invalid magic number for file '{PATH}'", "PATH", filePath);
Adriana Kobylakfa810d72020-10-16 16:27:28 -0500505 ifs.close();
Adriana Kobylak727f7382020-09-01 14:38:25 -0500506 return PLDM_ERROR;
507 }
508
Adriana Kobylaka1f158c2020-11-09 12:47:29 -0600509 fs::create_directories(imageDirPath);
Adriana Kobylakfa810d72020-10-16 16:27:28 -0500510 fs::create_directories(lidDirPath);
511
Adriana Kobylaka1f158c2020-11-09 12:47:29 -0600512 constexpr auto bmcClass = 0x2000;
513 if (htons(header.lidClass) == bmcClass)
514 {
515 // Skip the header and concatenate the BMC LIDs into a tar file
516 std::ofstream ofs(tarImagePath,
517 std::ios::out | std::ios::binary | std::ios::app);
518 ifs.seekg(htonl(header.headerSize));
519 ofs << ifs.rdbuf();
520 ofs.flush();
521 ofs.close();
522 }
523 else
524 {
525 std::stringstream lidFileName;
526 lidFileName << std::hex << htonl(header.lidNumber) << ".lid";
527 auto lidNoHeaderPath = fs::path(lidDirPath) / lidFileName.str();
528 std::ofstream ofs(lidNoHeaderPath,
529 std::ios::out | std::ios::binary | std::ios::trunc);
530 ifs.seekg(htonl(header.headerSize));
531 ofs << ifs.rdbuf();
532 ofs.flush();
533 ofs.close();
534 }
Adriana Kobylakfa810d72020-10-16 16:27:28 -0500535
536 ifs.close();
537 fs::remove(filePath);
Adriana Kobylak727f7382020-09-01 14:38:25 -0500538 return PLDM_SUCCESS;
539}
540
Adriana Kobylak9296f242021-09-22 15:52:00 +0000541int CodeUpdate::assembleCodeUpdateImage()
Adriana Kobylak837fb472020-10-16 16:53:42 -0500542{
Adriana Kobylak9296f242021-09-22 15:52:00 +0000543 pid_t pid = fork();
544
545 if (pid == 0)
Adriana Kobylak837fb472020-10-16 16:53:42 -0500546 {
Adriana Kobylak9296f242021-09-22 15:52:00 +0000547 pid_t nextPid = fork();
548 if (nextPid == 0)
549 {
550 // Create the hostfw squashfs image from the LID files without
551 // header
552 auto rc = executeCmd("/usr/sbin/mksquashfs", lidDirPath.c_str(),
553 hostfwImagePath.c_str(), "-all-root",
554 "-no-recovery");
555 if (rc < 0)
556 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600557 error("Error occurred during the mksqusquashfs call");
Adriana Kobylak9296f242021-09-22 15:52:00 +0000558 setCodeUpdateProgress(false);
559 auto sensorId = getFirmwareUpdateSensor();
560 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
561 uint8_t(CodeUpdateState::FAIL),
562 uint8_t(CodeUpdateState::START));
563 exit(EXIT_FAILURE);
564 }
565
566 fs::create_directories(updateDirPath);
567
568 // Extract the BMC tarball content
569 rc = executeCmd("/bin/tar", "-xf", tarImagePath.c_str(), "-C",
570 updateDirPath);
571 if (rc < 0)
572 {
573 setCodeUpdateProgress(false);
574 auto sensorId = getFirmwareUpdateSensor();
575 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
576 uint8_t(CodeUpdateState::FAIL),
577 uint8_t(CodeUpdateState::START));
578 exit(EXIT_FAILURE);
579 }
580
581 // Add the hostfw image to the directory where the contents were
582 // extracted
583 fs::copy_file(hostfwImagePath,
584 fs::path(updateDirPath) / hostfwImageName,
585 fs::copy_options::overwrite_existing);
586
587 // Remove the tarball file, then re-generate it with so that the
588 // hostfw image becomes part of the tarball
589 fs::remove(tarImagePath);
590 rc = executeCmd("/bin/tar", "-cf", tarImagePath, ".", "-C",
591 updateDirPath);
592 if (rc < 0)
593 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600594 error("Error occurred during the generation of the tarball");
Adriana Kobylak9296f242021-09-22 15:52:00 +0000595 setCodeUpdateProgress(false);
596 auto sensorId = getFirmwareUpdateSensor();
597 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
598 uint8_t(CodeUpdateState::FAIL),
599 uint8_t(CodeUpdateState::START));
600 exit(EXIT_FAILURE);
601 }
602
603 // Copy the tarball to the update directory to trigger the phosphor
604 // software manager to create a version interface
605 fs::copy_file(tarImagePath, updateImagePath,
606 fs::copy_options::overwrite_existing);
607
608 // Cleanup
609 fs::remove_all(updateDirPath);
610 fs::remove_all(lidDirPath);
611 fs::remove_all(imageDirPath);
612
613 exit(EXIT_SUCCESS);
614 }
615 else if (nextPid < 0)
616 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500617 error("Failure occurred during fork, error number - {ERROR_NUM}",
618 "ERROR_NUM", errno);
Adriana Kobylak9296f242021-09-22 15:52:00 +0000619 exit(EXIT_FAILURE);
620 }
621
622 // Do nothing as parent. When parent exits, child will be reparented
623 // under init and be reaped properly.
624 exit(0);
625 }
626 else if (pid > 0)
627 {
628 int status;
629 if (waitpid(pid, &status, 0) < 0)
630 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500631 error("Error occurred during waitpid, error number - {ERROR_NUM}",
632 "ERROR_NUM", errno);
Riya Dixit49cfb132023-03-02 04:26:53 -0600633
Adriana Kobylak9296f242021-09-22 15:52:00 +0000634 return PLDM_ERROR;
635 }
636 else if (WEXITSTATUS(status) != 0)
637 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600638 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500639 "Failed to execute the assembling of the image, status is {IMG_STATUS}",
Riya Dixit49cfb132023-03-02 04:26:53 -0600640 "IMG_STATUS", status);
Adriana Kobylak9296f242021-09-22 15:52:00 +0000641 return PLDM_ERROR;
642 }
643 }
644 else
645 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500646 error("Error occurred during fork, error number - {ERROR_NUM}}",
647 "ERROR_NUM", errno);
Adriana Kobylak837fb472020-10-16 16:53:42 -0500648 return PLDM_ERROR;
649 }
650
Adriana Kobylak837fb472020-10-16 16:53:42 -0500651 return PLDM_SUCCESS;
652}
653
Sampa Misraaea5dde2020-08-31 08:33:47 -0500654} // namespace responder
655} // namespace pldm