xref: /openbmc/phosphor-bmc-code-mgmt/bmc/static/flash.cpp (revision cab87e9cdeeb3e166d6d577511f6be4dc7721aca)
1*cab87e9cSJagpal Singh Gill #include "config.h"
2*cab87e9cSJagpal Singh Gill 
3*cab87e9cSJagpal Singh Gill #include "flash.hpp"
4*cab87e9cSJagpal Singh Gill 
5*cab87e9cSJagpal Singh Gill #include "activation.hpp"
6*cab87e9cSJagpal Singh Gill #include "images.hpp"
7*cab87e9cSJagpal Singh Gill #include "item_updater.hpp"
8*cab87e9cSJagpal Singh Gill 
9*cab87e9cSJagpal Singh Gill #include <phosphor-logging/lg2.hpp>
10*cab87e9cSJagpal Singh Gill 
11*cab87e9cSJagpal Singh Gill #include <filesystem>
12*cab87e9cSJagpal Singh Gill #include <system_error>
13*cab87e9cSJagpal Singh Gill 
14*cab87e9cSJagpal Singh Gill namespace
15*cab87e9cSJagpal Singh Gill {
16*cab87e9cSJagpal Singh Gill constexpr auto PATH_INITRAMFS = "/run/initramfs";
17*cab87e9cSJagpal Singh Gill constexpr auto FLASH_ALT_SERVICE_TMPL = "obmc-flash-bmc-alt@";
18*cab87e9cSJagpal Singh Gill } // namespace
19*cab87e9cSJagpal Singh Gill 
20*cab87e9cSJagpal Singh Gill namespace phosphor
21*cab87e9cSJagpal Singh Gill {
22*cab87e9cSJagpal Singh Gill namespace software
23*cab87e9cSJagpal Singh Gill {
24*cab87e9cSJagpal Singh Gill namespace updater
25*cab87e9cSJagpal Singh Gill {
26*cab87e9cSJagpal Singh Gill 
27*cab87e9cSJagpal Singh Gill PHOSPHOR_LOG2_USING;
28*cab87e9cSJagpal Singh Gill 
29*cab87e9cSJagpal Singh Gill namespace fs = std::filesystem;
30*cab87e9cSJagpal Singh Gill using namespace phosphor::software::image;
31*cab87e9cSJagpal Singh Gill 
flashWrite()32*cab87e9cSJagpal Singh Gill void Activation::flashWrite()
33*cab87e9cSJagpal Singh Gill {
34*cab87e9cSJagpal Singh Gill #ifdef BMC_STATIC_DUAL_IMAGE
35*cab87e9cSJagpal Singh Gill     if (parent.runningImageSlot != 0)
36*cab87e9cSJagpal Singh Gill     {
37*cab87e9cSJagpal Singh Gill         // It's running on the secondary chip, update the primary one
38*cab87e9cSJagpal Singh Gill         info("Flashing primary flash from secondary, id: {ID}", "ID",
39*cab87e9cSJagpal Singh Gill              versionId);
40*cab87e9cSJagpal Singh Gill         auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
41*cab87e9cSJagpal Singh Gill                                           SYSTEMD_INTERFACE, "StartUnit");
42*cab87e9cSJagpal Singh Gill         auto serviceFile = FLASH_ALT_SERVICE_TMPL + versionId + ".service";
43*cab87e9cSJagpal Singh Gill         method.append(serviceFile, "replace");
44*cab87e9cSJagpal Singh Gill         bus.call_noreply(method);
45*cab87e9cSJagpal Singh Gill         return;
46*cab87e9cSJagpal Singh Gill     }
47*cab87e9cSJagpal Singh Gill #endif
48*cab87e9cSJagpal Singh Gill     // For static layout code update, just put images in /run/initramfs.
49*cab87e9cSJagpal Singh Gill     // It expects user to trigger a reboot and an updater script will program
50*cab87e9cSJagpal Singh Gill     // the image to flash during reboot.
51*cab87e9cSJagpal Singh Gill     fs::path uploadDir(IMG_UPLOAD_DIR);
52*cab87e9cSJagpal Singh Gill     fs::path toPath(PATH_INITRAMFS);
53*cab87e9cSJagpal Singh Gill 
54*cab87e9cSJagpal Singh Gill     for (const auto& bmcImage : parent.imageUpdateList)
55*cab87e9cSJagpal Singh Gill     {
56*cab87e9cSJagpal Singh Gill         std::error_code ec;
57*cab87e9cSJagpal Singh Gill         fs::copy_file(uploadDir / versionId / bmcImage, toPath / bmcImage,
58*cab87e9cSJagpal Singh Gill                       fs::copy_options::overwrite_existing, ec);
59*cab87e9cSJagpal Singh Gill     }
60*cab87e9cSJagpal Singh Gill }
61*cab87e9cSJagpal Singh Gill 
onStateChanges(sdbusplus::message_t & msg)62*cab87e9cSJagpal Singh Gill void Activation::onStateChanges([[maybe_unused]] sdbusplus::message_t& msg)
63*cab87e9cSJagpal Singh Gill {
64*cab87e9cSJagpal Singh Gill #ifdef BMC_STATIC_DUAL_IMAGE
65*cab87e9cSJagpal Singh Gill     uint32_t newStateID;
66*cab87e9cSJagpal Singh Gill     auto serviceFile = FLASH_ALT_SERVICE_TMPL + versionId + ".service";
67*cab87e9cSJagpal Singh Gill     sdbusplus::message::object_path newStateObjPath;
68*cab87e9cSJagpal Singh Gill     std::string newStateUnit{};
69*cab87e9cSJagpal Singh Gill     std::string newStateResult{};
70*cab87e9cSJagpal Singh Gill     msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
71*cab87e9cSJagpal Singh Gill 
72*cab87e9cSJagpal Singh Gill     if (newStateUnit != serviceFile)
73*cab87e9cSJagpal Singh Gill     {
74*cab87e9cSJagpal Singh Gill         return;
75*cab87e9cSJagpal Singh Gill     }
76*cab87e9cSJagpal Singh Gill     if (newStateResult == "done")
77*cab87e9cSJagpal Singh Gill     {
78*cab87e9cSJagpal Singh Gill         activationProgress->progress(90);
79*cab87e9cSJagpal Singh Gill         onFlashWriteSuccess();
80*cab87e9cSJagpal Singh Gill     }
81*cab87e9cSJagpal Singh Gill     else
82*cab87e9cSJagpal Singh Gill     {
83*cab87e9cSJagpal Singh Gill         Activation::activation(sdbusplus::server::xyz::openbmc_project::
84*cab87e9cSJagpal Singh Gill                                    software::Activation::Activations::Failed);
85*cab87e9cSJagpal Singh Gill     }
86*cab87e9cSJagpal Singh Gill #endif
87*cab87e9cSJagpal Singh Gill }
88*cab87e9cSJagpal Singh Gill 
89*cab87e9cSJagpal Singh Gill } // namespace updater
90*cab87e9cSJagpal Singh Gill } // namespace software
91*cab87e9cSJagpal Singh Gill } // namespace phosphor
92