1f6ed5897SGunnar Mills #include "config.h"
2f6ed5897SGunnar Mills
3f3ce4337SLei YU #ifdef UBIFS_LAYOUT
4f3ce4337SLei YU #include "ubi/item_updater_ubi.hpp"
5da6ab6cbSLei YU #include "ubi/watch.hpp"
68bc2ab4eSAdriana Kobylak #elif defined MMC_LAYOUT
78bc2ab4eSAdriana Kobylak #include "mmc/item_updater_mmc.hpp"
8322f3f47SLei YU #else
9322f3f47SLei YU #include "static/item_updater_static.hpp"
10f3ce4337SLei YU #endif
11099543e4SBrad Bishop #include "functions.hpp"
12f6ed5897SGunnar Mills
13099543e4SBrad Bishop #include <CLI/CLI.hpp>
14f6ed5897SGunnar Mills #include <phosphor-logging/log.hpp>
152d8fa225SAdriana Kobylak #include <sdbusplus/bus.hpp>
162d8fa225SAdriana Kobylak #include <sdbusplus/server/manager.hpp>
170283f7cbSBrad Bishop #include <sdeventplus/event.hpp>
188facccfaSBrad Bishop
19099543e4SBrad Bishop #include <map>
20099543e4SBrad Bishop #include <memory>
21099543e4SBrad Bishop #include <string>
226bd6d7bbSGunnar Mills #include <system_error>
23099543e4SBrad Bishop #include <vector>
242d8fa225SAdriana Kobylak
251ee9095bSBrad Bishop namespace openpower
261ee9095bSBrad Bishop {
271ee9095bSBrad Bishop namespace software
281ee9095bSBrad Bishop {
291ee9095bSBrad Bishop namespace updater
301ee9095bSBrad Bishop {
initializeService(sdbusplus::bus_t & bus)310dea1992SPatrick Williams void initializeService(sdbusplus::bus_t& bus)
321ee9095bSBrad Bishop {
330dea1992SPatrick Williams static sdbusplus::server::manager_t objManager(bus, SOFTWARE_OBJPATH);
341ee9095bSBrad Bishop #ifdef UBIFS_LAYOUT
351ee9095bSBrad Bishop static ItemUpdaterUbi updater(bus, SOFTWARE_OBJPATH);
361ee9095bSBrad Bishop static Watch watch(
371ee9095bSBrad Bishop bus.get_event(),
381ee9095bSBrad Bishop std::bind(std::mem_fn(&ItemUpdater::updateFunctionalAssociation),
391ee9095bSBrad Bishop &updater, std::placeholders::_1));
401ee9095bSBrad Bishop #elif defined MMC_LAYOUT
411ee9095bSBrad Bishop static ItemUpdaterMMC updater(bus, SOFTWARE_OBJPATH);
421ee9095bSBrad Bishop #else
431ee9095bSBrad Bishop static ItemUpdaterStatic updater(bus, SOFTWARE_OBJPATH);
441ee9095bSBrad Bishop #endif
451ee9095bSBrad Bishop bus.request_name(BUSNAME_UPDATER);
461ee9095bSBrad Bishop }
471ee9095bSBrad Bishop } // namespace updater
481ee9095bSBrad Bishop } // namespace software
491ee9095bSBrad Bishop } // namespace openpower
501ee9095bSBrad Bishop
main(int argc,char * argv[])51099543e4SBrad Bishop int main(int argc, char* argv[])
522d8fa225SAdriana Kobylak {
536bd6d7bbSGunnar Mills using namespace openpower::software::updater;
546bd6d7bbSGunnar Mills using namespace phosphor::logging;
552d8fa225SAdriana Kobylak auto bus = sdbusplus::bus::new_default();
560283f7cbSBrad Bishop auto loop = sdeventplus::Event::get_default();
576bd6d7bbSGunnar Mills
581ee9095bSBrad Bishop bus.attach_event(loop.get(), SD_EVENT_PRIORITY_NORMAL);
592d8fa225SAdriana Kobylak
60099543e4SBrad Bishop CLI::App app{"OpenPOWER host firmware manager"};
61099543e4SBrad Bishop
62099543e4SBrad Bishop using namespace std::string_literals;
63099543e4SBrad Bishop std::map<std::string, std::vector<std::string>> extensionMap{{
64b4813bdfSAdriana Kobylak {"com.ibm.Hardware.Chassis.Model.BlueRidge2U"s,
65b4813bdfSAdriana Kobylak {".BLUERIDGE_2U_XML"s, ".P10"s}},
66b4813bdfSAdriana Kobylak {"com.ibm.Hardware.Chassis.Model.BlueRidge4U"s,
67b4813bdfSAdriana Kobylak {".BLUERIDGE_4U_XML"s, ".P10"s}},
68b4813bdfSAdriana Kobylak {"com.ibm.Hardware.Chassis.Model.BlueRidge1S4U"s,
69b4813bdfSAdriana Kobylak {".BLUERIDGE_4U_XML"s, ".P10"s}},
70749bdcc9SAdriana Kobylak {"com.ibm.Hardware.Chassis.Model.Bonnell"s, {".BONNELL_XML"s, ".P10"s}},
71749bdcc9SAdriana Kobylak {"com.ibm.Hardware.Chassis.Model.Everest"s, {".EVEREST_XML"s, ".P10"s}},
72b4813bdfSAdriana Kobylak {"com.ibm.Hardware.Chassis.Model.Fuji"s, {".FUJI_XML"s, ".P10"s}},
73749bdcc9SAdriana Kobylak {"com.ibm.Hardware.Chassis.Model.Rainier2U"s,
74749bdcc9SAdriana Kobylak {".RAINIER_2U_XML"s, ".P10"s}},
75749bdcc9SAdriana Kobylak {"com.ibm.Hardware.Chassis.Model.Rainier4U"s,
76749bdcc9SAdriana Kobylak {".RAINIER_4U_XML"s, ".P10"s}},
77749bdcc9SAdriana Kobylak {"com.ibm.Hardware.Chassis.Model.Rainier1S4U"s,
78749bdcc9SAdriana Kobylak {".RAINIER_4U_XML"s, ".P10"s}},
79099543e4SBrad Bishop }};
8053a27395SAdriana Kobylak
8153a27395SAdriana Kobylak // subcommandContext allows program subcommand callbacks to add loop event
8253a27395SAdriana Kobylak // callbacks (e.g. reception of a dbus signal) and then return to main,
8353a27395SAdriana Kobylak // without the loop event callback being destroyed until the loop event
8453a27395SAdriana Kobylak // callback has a chance to run and instruct the loop to exit.
8553a27395SAdriana Kobylak std::vector<std::shared_ptr<void>> subcommandContext;
8653a27395SAdriana Kobylak static_cast<void>(
8753a27395SAdriana Kobylak app.add_subcommand("process-host-firmware",
8853a27395SAdriana Kobylak "Point the host firmware at its data.")
8953a27395SAdriana Kobylak ->callback([&bus, &loop, &subcommandContext, extensionMap]() {
90099543e4SBrad Bishop auto hostFirmwareDirectory = "/media/hostfw/running"s;
91099543e4SBrad Bishop auto logCallback = [](const auto& path, auto& ec) {
92099543e4SBrad Bishop std::cerr << path << ": " << ec.message() << "\n";
93099543e4SBrad Bishop };
9453a27395SAdriana Kobylak subcommandContext.push_back(
95099543e4SBrad Bishop functions::process_hostfirmware::processHostFirmware(
9653a27395SAdriana Kobylak bus, extensionMap, std::move(hostFirmwareDirectory),
9753a27395SAdriana Kobylak std::move(logCallback), loop));
98099543e4SBrad Bishop }));
9953a27395SAdriana Kobylak static_cast<void>(
10053a27395SAdriana Kobylak app.add_subcommand("update-bios-attr-table",
10153a27395SAdriana Kobylak "Update the bios attribute table with the host "
10253a27395SAdriana Kobylak "firmware data details.")
103af29f723SPatrick Williams ->callback([&bus, &loop, &subcommandContext, extensionMap]() {
104ae0998f1SAdriana Kobylak auto elementsJsonFilePath = "/usr/share/hostfw/elements.json"s;
105*f8e02429SPatrick Williams auto subcommands =
106*f8e02429SPatrick Williams functions::process_hostfirmware::updateBiosAttrTable(
107*f8e02429SPatrick Williams bus, extensionMap, std::move(elementsJsonFilePath),
108*f8e02429SPatrick Williams loop);
109ebf67bf7SAdriana Kobylak for (const auto& subcommand : subcommands)
110ebf67bf7SAdriana Kobylak {
111ebf67bf7SAdriana Kobylak subcommandContext.push_back(subcommand);
112ebf67bf7SAdriana Kobylak }
11353a27395SAdriana Kobylak }));
11453a27395SAdriana Kobylak
115099543e4SBrad Bishop CLI11_PARSE(app, argc, argv);
116099543e4SBrad Bishop
117099543e4SBrad Bishop if (app.get_subcommands().size() == 0)
118099543e4SBrad Bishop {
1191ee9095bSBrad Bishop initializeService(bus);
120099543e4SBrad Bishop }
1212d8fa225SAdriana Kobylak
1226bd6d7bbSGunnar Mills try
1232d8fa225SAdriana Kobylak {
1240283f7cbSBrad Bishop auto rc = loop.loop();
1256bd6d7bbSGunnar Mills if (rc < 0)
1266bd6d7bbSGunnar Mills {
1276bd6d7bbSGunnar Mills log<level::ERR>("Error occurred during the sd_event_loop",
1286bd6d7bbSGunnar Mills entry("RC=%d", rc));
1296bd6d7bbSGunnar Mills return -1;
1302d8fa225SAdriana Kobylak }
1316bd6d7bbSGunnar Mills }
1326bd6d7bbSGunnar Mills catch (const std::system_error& e)
1336bd6d7bbSGunnar Mills {
1346bd6d7bbSGunnar Mills log<level::ERR>(e.what());
1356bd6d7bbSGunnar Mills return -1;
1366bd6d7bbSGunnar Mills }
1376bd6d7bbSGunnar Mills
1382d8fa225SAdriana Kobylak return 0;
1392d8fa225SAdriana Kobylak }
140