blob: 71b6ec3691f02f99843cd314018956cfe766e054 [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 Williams6da4f912023-05-10 07:50:53 -0500175 auto method1 = bus.new_method_call(mapperService, activeObjPath,
176 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 Williams6da4f912023-05-10 07:50:53 -0500204 DbusChangedProps props;
205 std::string iface;
206 msg.read(iface, props);
207 processPriorityChangeNotification(props);
Patrick Williamsa6756622023-10-20 11:19:15 -0500208 }));
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 Williams6da4f912023-05-10 07:50:53 -0500214 DBusInterfaceAdded interfaces;
215 sdbusplus::message::object_path path;
216 msg.read(path, interfaces);
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600217
Patrick Williams6da4f912023-05-10 07:50:53 -0500218 for (auto& interface : interfaces)
219 {
220 if (interface.first == "xyz.openbmc_project.Software.Activation")
Sampa Misraaea5dde2020-08-31 08:33:47 -0500221 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500222 auto imageInterface = "xyz.openbmc_project.Software.Activation";
223 auto imageObjPath = path.str.c_str();
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600224
Patrick Williams6da4f912023-05-10 07:50:53 -0500225 try
226 {
227 auto propVal = dBusIntf->getDbusPropertyVariant(
228 imageObjPath, "Activation", imageInterface);
229 const auto& imageProp = std::get<std::string>(propVal);
230 if (imageProp == "xyz.openbmc_project.Software."
231 "Activation.Activations.Ready" &&
232 isCodeUpdateInProgress())
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500233 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500234 newImageId = path.str;
235 if (!imageActivationMatch)
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500236 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500237 imageActivationMatch =
238 std::make_unique<sdbusplus::bus::match_t>(
Sagar Srinivas9a64b4a2021-02-09 07:55:38 -0600239 pldm::utils::DBusHandler::getBus(),
240 propertiesChanged(newImageId,
241 "xyz.openbmc_project."
242 "Software.Activation"),
Patrick Williams84b790c2022-07-22 19:26:56 -0500243 [this](sdbusplus::message_t& msg) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500244 DbusChangedProps props;
245 std::string iface;
246 msg.read(iface, props);
247 const auto itr = props.find("Activation");
248 if (itr != props.end())
249 {
250 PropertyValue value = itr->second;
251 auto propVal = std::get<std::string>(value);
252 if (propVal ==
253 "xyz.openbmc_project.Software."
254 "Activation.Activations.Active")
255 {
256 CodeUpdateState state =
257 CodeUpdateState::END;
258 setCodeUpdateProgress(false);
259 auto sensorId =
260 getFirmwareUpdateSensor();
261 sendStateSensorEvent(
262 sensorId, PLDM_STATE_SENSOR_STATE,
263 0, uint8_t(state),
264 uint8_t(CodeUpdateState::START));
265 newImageId.clear();
266 }
267 else if (propVal == "xyz.openbmc_project."
268 "Software.Activation."
269 "Activations.Failed" ||
270 propVal == "xyz.openbmc_"
271 "project.Software."
272 "Activation."
273 "Activations."
274 "Invalid")
275 {
276 CodeUpdateState state =
277 CodeUpdateState::FAIL;
278 setCodeUpdateProgress(false);
279 auto sensorId =
280 getFirmwareUpdateSensor();
281 sendStateSensorEvent(
282 sensorId, PLDM_STATE_SENSOR_STATE,
283 0, uint8_t(state),
284 uint8_t(CodeUpdateState::START));
285 newImageId.clear();
286 }
287 }
Patrick Williamsa6756622023-10-20 11:19:15 -0500288 });
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500289 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500290 auto rc = setRequestedActivation();
291 if (rc != PLDM_SUCCESS)
292 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500293 error("Could not set Requested Activation");
Patrick Williams6da4f912023-05-10 07:50:53 -0500294 CodeUpdateState state = CodeUpdateState::FAIL;
295 setCodeUpdateProgress(false);
296 auto sensorId = getFirmwareUpdateSensor();
297 sendStateSensorEvent(
298 sensorId, PLDM_STATE_SENSOR_STATE, 0,
299 uint8_t(state),
300 uint8_t(CodeUpdateState::START));
Patrick Williams6da4f912023-05-10 07:50:53 -0500301 }
302 break;
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500303 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500304 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500305 catch (const sdbusplus::exception_t& e)
306 {
307 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500308 "Failed to get activation status for interface '{INTERFACE}' and object path '{PATH}', error - {ERROR}",
309 "ERROR", e, "INTERFACE", imageInterface, "PATH",
310 imageObjPath);
Patrick Williams6da4f912023-05-10 07:50:53 -0500311 }
Sampa Misraaea5dde2020-08-31 08:33:47 -0500312 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500313 }
Patrick Williamsa6756622023-10-20 11:19:15 -0500314 }));
Sampa Misraaea5dde2020-08-31 08:33:47 -0500315}
316
317void CodeUpdate::processPriorityChangeNotification(
318 const DbusChangedProps& chProperties)
319{
320 static constexpr auto propName = "Priority";
321 const auto it = chProperties.find(propName);
322 if (it == chProperties.end())
323 {
324 return;
325 }
326 uint8_t newVal = std::get<uint8_t>(it->second);
327 nextBootSide = (newVal == 0) ? currBootSide
328 : ((currBootSide == Tside) ? Pside : Tside);
329}
330
331void CodeUpdate::setOemPlatformHandler(
332 pldm::responder::oem_platform::Handler* handler)
333{
334 oemPlatformHandler = handler;
335}
336
Varsha Kaverappa3ca29df2020-09-27 12:39:22 -0500337void CodeUpdate::clearDirPath(const std::string& dirPath)
338{
Manojkiran Eda78124cf2022-08-29 11:22:43 +0530339 if (!fs::is_directory(dirPath))
Varsha Kaverappa3ca29df2020-09-27 12:39:22 -0500340 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500341 error("The directory '{PATH}' does not exist", "PATH", dirPath);
Manojkiran Eda78124cf2022-08-29 11:22:43 +0530342 return;
Varsha Kaverappa3ca29df2020-09-27 12:39:22 -0500343 }
Manojkiran Eda78124cf2022-08-29 11:22:43 +0530344 for (const auto& iter : fs::directory_iterator(dirPath))
345 {
346 fs::remove_all(iter);
347 }
Varsha Kaverappa3ca29df2020-09-27 12:39:22 -0500348}
349
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500350void CodeUpdate::sendStateSensorEvent(
351 uint16_t sensorId, enum sensor_event_class_states sensorEventClass,
352 uint8_t sensorOffset, uint8_t eventState, uint8_t prevEventState)
353{
354 pldm::responder::oem_ibm_platform::Handler* oemIbmPlatformHandler =
355 dynamic_cast<pldm::responder::oem_ibm_platform::Handler*>(
356 oemPlatformHandler);
357 oemIbmPlatformHandler->sendStateSensorEvent(
358 sensorId, sensorEventClass, sensorOffset, eventState, prevEventState);
359}
360
361void CodeUpdate::deleteImage()
362{
363 static constexpr auto UPDATER_SERVICE =
364 "xyz.openbmc_project.Software.BMC.Updater";
365 static constexpr auto SW_OBJ_PATH = "/xyz/openbmc_project/software";
366 static constexpr auto DELETE_INTF =
367 "xyz.openbmc_project.Collection.DeleteAll";
368
369 auto& bus = dBusIntf->getBus();
370 try
371 {
372 auto method = bus.new_method_call(UPDATER_SERVICE, SW_OBJ_PATH,
373 DELETE_INTF, "DeleteAll");
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +0000374 bus.call_noreply(method, dbusTimeout);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500375 }
376 catch (const std::exception& e)
377 {
Sagar Srinivas90403472023-08-21 06:08:55 -0500378 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500379 "Failed to delete image at path '{PATH}' and interface '{INTERFACE}', error - {ERROR}",
380 "PATH", SW_OBJ_PATH, "INTERFACE", DELETE_INTF, "ERROR", e);
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500381 return;
382 }
383}
384
Sampa Misraaea5dde2020-08-31 08:33:47 -0500385uint8_t fetchBootSide(uint16_t entityInstance, CodeUpdate* codeUpdate)
386{
387 uint8_t sensorOpState = tSideNum;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500388 if (entityInstance == 0)
389 {
390 auto currSide = codeUpdate->fetchCurrentBootSide();
391 if (currSide == Pside)
392 {
393 sensorOpState = pSideNum;
394 }
395 }
396 else if (entityInstance == 1)
397 {
398 auto nextSide = codeUpdate->fetchNextBootSide();
399 if (nextSide == Pside)
400 {
401 sensorOpState = pSideNum;
402 }
403 }
404 else
405 {
406 sensorOpState = PLDM_SENSOR_UNKNOWN;
407 }
408
409 return sensorOpState;
410}
411
412int setBootSide(uint16_t entityInstance, uint8_t currState,
413 const std::vector<set_effecter_state_field>& stateField,
414 CodeUpdate* codeUpdate)
415{
416 int rc = PLDM_SUCCESS;
417 auto side = (stateField[currState].effecter_state == pSideNum) ? "P" : "T";
418
419 if (entityInstance == 0)
420 {
421 rc = codeUpdate->setCurrentBootSide(side);
422 }
423 else if (entityInstance == 1)
424 {
425 rc = codeUpdate->setNextBootSide(side);
426 }
427 else
428 {
429 rc = PLDM_PLATFORM_INVALID_STATE_VALUE;
430 }
431 return rc;
432}
433
Adriana Kobylak837fb472020-10-16 16:53:42 -0500434template <typename... T>
Patrick Williams6da4f912023-05-10 07:50:53 -0500435int executeCmd(const T&... t)
Adriana Kobylak837fb472020-10-16 16:53:42 -0500436{
437 std::stringstream cmd;
438 ((cmd << t << " "), ...) << std::endl;
439 FILE* pipe = popen(cmd.str().c_str(), "r");
440 if (!pipe)
441 {
442 throw std::runtime_error("popen() failed!");
443 }
444 int rc = pclose(pipe);
445 if (WEXITSTATUS(rc))
446 {
447 std::cerr << "Error executing: ";
448 ((std::cerr << " " << t), ...);
449 std::cerr << "\n";
450 return -1;
451 }
452
453 return 0;
454}
455
Adriana Kobylak727f7382020-09-01 14:38:25 -0500456int processCodeUpdateLid(const std::string& filePath)
457{
458 struct LidHeader
459 {
460 uint16_t magicNumber;
Adriana Kobylak86d14182020-10-16 16:11:08 -0500461 uint16_t headerVersion;
462 uint32_t lidNumber;
463 uint32_t lidDate;
464 uint16_t lidTime;
465 uint16_t lidClass;
466 uint32_t lidCrc;
467 uint32_t lidSize;
468 uint32_t headerSize;
Adriana Kobylak727f7382020-09-01 14:38:25 -0500469 };
470 LidHeader header;
471
472 std::ifstream ifs(filePath, std::ios::in | std::ios::binary);
473 if (!ifs)
474 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500475 error("Failed to opening file '{FILE}' ifstream", "PATH", filePath);
Adriana Kobylak727f7382020-09-01 14:38:25 -0500476 return PLDM_ERROR;
477 }
478 ifs.seekg(0);
479 ifs.read(reinterpret_cast<char*>(&header), sizeof(header));
Adriana Kobylak727f7382020-09-01 14:38:25 -0500480
Adriana Kobylak86d14182020-10-16 16:11:08 -0500481 // File size should be the value of lid size minus the header size
482 auto fileSize = fs::file_size(filePath);
483 fileSize -= htonl(header.headerSize);
484 if (fileSize < htonl(header.lidSize))
485 {
486 // File is not completely written yet
Adriana Kobylakfa810d72020-10-16 16:27:28 -0500487 ifs.close();
Adriana Kobylak86d14182020-10-16 16:11:08 -0500488 return PLDM_SUCCESS;
489 }
490
Adriana Kobylak727f7382020-09-01 14:38:25 -0500491 constexpr auto magicNumber = 0x0222;
492 if (htons(header.magicNumber) != magicNumber)
493 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500494 error("Invalid magic number for file '{PATH}'", "PATH", filePath);
Adriana Kobylakfa810d72020-10-16 16:27:28 -0500495 ifs.close();
Adriana Kobylak727f7382020-09-01 14:38:25 -0500496 return PLDM_ERROR;
497 }
498
Adriana Kobylaka1f158c2020-11-09 12:47:29 -0600499 fs::create_directories(imageDirPath);
Adriana Kobylakfa810d72020-10-16 16:27:28 -0500500 fs::create_directories(lidDirPath);
501
Adriana Kobylaka1f158c2020-11-09 12:47:29 -0600502 constexpr auto bmcClass = 0x2000;
503 if (htons(header.lidClass) == bmcClass)
504 {
505 // Skip the header and concatenate the BMC LIDs into a tar file
506 std::ofstream ofs(tarImagePath,
507 std::ios::out | std::ios::binary | std::ios::app);
508 ifs.seekg(htonl(header.headerSize));
509 ofs << ifs.rdbuf();
510 ofs.flush();
511 ofs.close();
512 }
513 else
514 {
515 std::stringstream lidFileName;
516 lidFileName << std::hex << htonl(header.lidNumber) << ".lid";
517 auto lidNoHeaderPath = fs::path(lidDirPath) / lidFileName.str();
518 std::ofstream ofs(lidNoHeaderPath,
519 std::ios::out | std::ios::binary | std::ios::trunc);
520 ifs.seekg(htonl(header.headerSize));
521 ofs << ifs.rdbuf();
522 ofs.flush();
523 ofs.close();
524 }
Adriana Kobylakfa810d72020-10-16 16:27:28 -0500525
526 ifs.close();
527 fs::remove(filePath);
Adriana Kobylak727f7382020-09-01 14:38:25 -0500528 return PLDM_SUCCESS;
529}
530
Adriana Kobylak9296f242021-09-22 15:52:00 +0000531int CodeUpdate::assembleCodeUpdateImage()
Adriana Kobylak837fb472020-10-16 16:53:42 -0500532{
Adriana Kobylak9296f242021-09-22 15:52:00 +0000533 pid_t pid = fork();
534
535 if (pid == 0)
Adriana Kobylak837fb472020-10-16 16:53:42 -0500536 {
Adriana Kobylak9296f242021-09-22 15:52:00 +0000537 pid_t nextPid = fork();
538 if (nextPid == 0)
539 {
540 // Create the hostfw squashfs image from the LID files without
541 // header
542 auto rc = executeCmd("/usr/sbin/mksquashfs", lidDirPath.c_str(),
543 hostfwImagePath.c_str(), "-all-root",
544 "-no-recovery");
545 if (rc < 0)
546 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600547 error("Error occurred during the mksqusquashfs call");
Adriana Kobylak9296f242021-09-22 15:52:00 +0000548 setCodeUpdateProgress(false);
549 auto sensorId = getFirmwareUpdateSensor();
550 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
551 uint8_t(CodeUpdateState::FAIL),
552 uint8_t(CodeUpdateState::START));
553 exit(EXIT_FAILURE);
554 }
555
556 fs::create_directories(updateDirPath);
557
558 // Extract the BMC tarball content
559 rc = executeCmd("/bin/tar", "-xf", tarImagePath.c_str(), "-C",
560 updateDirPath);
561 if (rc < 0)
562 {
563 setCodeUpdateProgress(false);
564 auto sensorId = getFirmwareUpdateSensor();
565 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
566 uint8_t(CodeUpdateState::FAIL),
567 uint8_t(CodeUpdateState::START));
568 exit(EXIT_FAILURE);
569 }
570
571 // Add the hostfw image to the directory where the contents were
572 // extracted
573 fs::copy_file(hostfwImagePath,
574 fs::path(updateDirPath) / hostfwImageName,
575 fs::copy_options::overwrite_existing);
576
577 // Remove the tarball file, then re-generate it with so that the
578 // hostfw image becomes part of the tarball
579 fs::remove(tarImagePath);
580 rc = executeCmd("/bin/tar", "-cf", tarImagePath, ".", "-C",
581 updateDirPath);
582 if (rc < 0)
583 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600584 error("Error occurred during the generation of the tarball");
Adriana Kobylak9296f242021-09-22 15:52:00 +0000585 setCodeUpdateProgress(false);
586 auto sensorId = getFirmwareUpdateSensor();
587 sendStateSensorEvent(sensorId, PLDM_STATE_SENSOR_STATE, 0,
588 uint8_t(CodeUpdateState::FAIL),
589 uint8_t(CodeUpdateState::START));
590 exit(EXIT_FAILURE);
591 }
592
593 // Copy the tarball to the update directory to trigger the phosphor
594 // software manager to create a version interface
595 fs::copy_file(tarImagePath, updateImagePath,
596 fs::copy_options::overwrite_existing);
597
598 // Cleanup
599 fs::remove_all(updateDirPath);
600 fs::remove_all(lidDirPath);
601 fs::remove_all(imageDirPath);
602
603 exit(EXIT_SUCCESS);
604 }
605 else if (nextPid < 0)
606 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500607 error("Failure occurred during fork, error number - {ERROR_NUM}",
608 "ERROR_NUM", errno);
Adriana Kobylak9296f242021-09-22 15:52:00 +0000609 exit(EXIT_FAILURE);
610 }
611
612 // Do nothing as parent. When parent exits, child will be reparented
613 // under init and be reaped properly.
614 exit(0);
615 }
616 else if (pid > 0)
617 {
618 int status;
619 if (waitpid(pid, &status, 0) < 0)
620 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500621 error("Error occurred during waitpid, error number - {ERROR_NUM}",
622 "ERROR_NUM", errno);
Riya Dixit49cfb132023-03-02 04:26:53 -0600623
Adriana Kobylak9296f242021-09-22 15:52:00 +0000624 return PLDM_ERROR;
625 }
626 else if (WEXITSTATUS(status) != 0)
627 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600628 error(
Riya Dixitfc84f632024-04-06 14:00:02 -0500629 "Failed to execute the assembling of the image, status is {IMG_STATUS}",
Riya Dixit49cfb132023-03-02 04:26:53 -0600630 "IMG_STATUS", status);
Adriana Kobylak9296f242021-09-22 15:52:00 +0000631 return PLDM_ERROR;
632 }
633 }
634 else
635 {
Riya Dixitfc84f632024-04-06 14:00:02 -0500636 error("Error occurred during fork, error number - {ERROR_NUM}}",
637 "ERROR_NUM", errno);
Adriana Kobylak837fb472020-10-16 16:53:42 -0500638 return PLDM_ERROR;
639 }
640
Adriana Kobylak837fb472020-10-16 16:53:42 -0500641 return PLDM_SUCCESS;
642}
643
Sampa Misraaea5dde2020-08-31 08:33:47 -0500644} // namespace responder
645} // namespace pldm