Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 1 | #include "config.h" |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 2 | |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 3 | #include "serialize.hpp" |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 4 | |
| 5 | #include <cereal/archives/json.hpp> |
Brad Bishop | 8facccf | 2020-11-04 09:44:58 -0500 | [diff] [blame] | 6 | #include <sdbusplus/server.hpp> |
| 7 | |
Brad Bishop | 9f44c99 | 2020-11-06 14:48:46 -0500 | [diff] [blame] | 8 | #include <filesystem> |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 9 | #include <fstream> |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 10 | |
| 11 | namespace openpower |
| 12 | { |
| 13 | namespace software |
| 14 | { |
| 15 | namespace updater |
| 16 | { |
| 17 | |
Lei YU | 1db9adf | 2019-03-05 16:02:31 +0800 | [diff] [blame] | 18 | void storeToFile(const std::string& versionId, uint8_t priority) |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 19 | { |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 20 | auto bus = sdbusplus::bus::new_default(); |
| 21 | |
Brad Bishop | 9f44c99 | 2020-11-06 14:48:46 -0500 | [diff] [blame] | 22 | if (!std::filesystem::is_directory(PERSIST_DIR)) |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 23 | { |
Brad Bishop | 9f44c99 | 2020-11-06 14:48:46 -0500 | [diff] [blame] | 24 | std::filesystem::create_directories(PERSIST_DIR); |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 25 | } |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 26 | |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 27 | // store one copy in /var/lib/obmc/openpower-pnor-code-mgmt/[versionId] |
| 28 | auto varPath = PERSIST_DIR + versionId; |
| 29 | std::ofstream varOutput(varPath.c_str()); |
| 30 | cereal::JSONOutputArchive varArchive(varOutput); |
| 31 | varArchive(cereal::make_nvp("priority", priority)); |
| 32 | |
Brad Bishop | 9f44c99 | 2020-11-06 14:48:46 -0500 | [diff] [blame] | 33 | if (std::filesystem::is_directory(PNOR_RW_PREFIX + versionId)) |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 34 | { |
| 35 | // store another copy in /media/pnor-rw-[versionId]/[versionId] |
| 36 | auto rwPath = PNOR_RW_PREFIX + versionId + "/" + versionId; |
| 37 | std::ofstream rwOutput(rwPath.c_str()); |
| 38 | cereal::JSONOutputArchive rwArchive(rwOutput); |
| 39 | rwArchive(cereal::make_nvp("priority", priority)); |
| 40 | } |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 41 | |
| 42 | // lastly, store the priority as an environment variable pnor-[versionId] |
| 43 | std::string serviceFile = "obmc-flash-bmc-setenv@pnor\\x2d" + versionId + |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 44 | "\\x3d" + std::to_string(priority) + ".service"; |
| 45 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 46 | SYSTEMD_INTERFACE, "StartUnit"); |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 47 | method.append(serviceFile, "replace"); |
| 48 | bus.call_noreply(method); |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 49 | } |
| 50 | |
Lei YU | 1db9adf | 2019-03-05 16:02:31 +0800 | [diff] [blame] | 51 | bool restoreFromFile(const std::string& versionId, uint8_t& priority) |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 52 | { |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 53 | auto varPath = PERSIST_DIR + versionId; |
Brad Bishop | 9f44c99 | 2020-11-06 14:48:46 -0500 | [diff] [blame] | 54 | if (std::filesystem::exists(varPath)) |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 55 | { |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 56 | std::ifstream varInput(varPath.c_str(), std::ios::in); |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 57 | try |
| 58 | { |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 59 | cereal::JSONInputArchive varArchive(varInput); |
| 60 | varArchive(cereal::make_nvp("priority", priority)); |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 61 | return true; |
| 62 | } |
Patrick Williams | 97a709b | 2021-10-06 13:05:40 -0500 | [diff] [blame] | 63 | catch (const cereal::RapidJSONException& e) |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 64 | { |
Brad Bishop | 9f44c99 | 2020-11-06 14:48:46 -0500 | [diff] [blame] | 65 | std::filesystem::remove(varPath); |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | auto rwPath = PNOR_RW_PREFIX + versionId + "/" + versionId; |
Brad Bishop | 9f44c99 | 2020-11-06 14:48:46 -0500 | [diff] [blame] | 70 | if (std::filesystem::exists(rwPath)) |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 71 | { |
| 72 | std::ifstream rwInput(rwPath.c_str(), std::ios::in); |
| 73 | try |
| 74 | { |
| 75 | cereal::JSONInputArchive rwArchive(rwInput); |
| 76 | rwArchive(cereal::make_nvp("priority", priority)); |
| 77 | return true; |
| 78 | } |
Patrick Williams | 97a709b | 2021-10-06 13:05:40 -0500 | [diff] [blame] | 79 | catch (const cereal::RapidJSONException& e) |
Michael Tritz | 4fb952c | 2017-08-13 14:55:04 -0500 | [diff] [blame] | 80 | { |
Brad Bishop | 9f44c99 | 2020-11-06 14:48:46 -0500 | [diff] [blame] | 81 | std::filesystem::remove(rwPath); |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 82 | } |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 83 | } |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 84 | |
| 85 | try |
| 86 | { |
| 87 | std::string devicePath = "/dev/mtd/u-boot-env"; |
| 88 | |
Brad Bishop | 9f44c99 | 2020-11-06 14:48:46 -0500 | [diff] [blame] | 89 | if (std::filesystem::exists(devicePath) && !devicePath.empty()) |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 90 | { |
| 91 | std::ifstream input(devicePath.c_str()); |
| 92 | std::string envVars; |
| 93 | std::getline(input, envVars); |
| 94 | |
| 95 | std::string versionVar = "pnor-" + versionId + "="; |
| 96 | auto varPosition = envVars.find(versionVar); |
| 97 | |
| 98 | if (varPosition != std::string::npos) |
| 99 | { |
| 100 | // Grab the environment variable for this versionId. These |
| 101 | // variables follow the format "pnor-[versionId]=[priority]\0" |
| 102 | auto var = envVars.substr(varPosition); |
| 103 | priority = std::stoi(var.substr(versionVar.length())); |
| 104 | return true; |
| 105 | } |
| 106 | } |
| 107 | } |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 108 | catch (const std::exception& e) |
Brad Bishop | 8facccf | 2020-11-04 09:44:58 -0500 | [diff] [blame] | 109 | {} |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 110 | |
Michael Tritz | 3641792 | 2017-08-04 14:00:29 -0500 | [diff] [blame] | 111 | return false; |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Lei YU | 1db9adf | 2019-03-05 16:02:31 +0800 | [diff] [blame] | 114 | void removeFile(const std::string& versionId) |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 115 | { |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 116 | auto bus = sdbusplus::bus::new_default(); |
| 117 | |
| 118 | // Clear the environment variable pnor-[versionId]. |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 119 | std::string serviceFile = |
| 120 | "obmc-flash-bmc-setenv@pnor\\x2d" + versionId + ".service"; |
| 121 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 122 | SYSTEMD_INTERFACE, "StartUnit"); |
Michael Tritz | 40ecdb6 | 2017-11-20 16:56:41 -0600 | [diff] [blame] | 123 | method.append(serviceFile, "replace"); |
| 124 | bus.call_noreply(method); |
| 125 | |
| 126 | // Delete the file /var/lib/obmc/openpower-pnor-code-mgmt/[versionId]. |
| 127 | // Note that removeFile() is called in the case of a version being deleted, |
| 128 | // so the file /media/pnor-rw-[versionId]/[versionId] will also be deleted |
| 129 | // along with its surrounding directory. |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 130 | std::string path = PERSIST_DIR + versionId; |
Brad Bishop | 9f44c99 | 2020-11-06 14:48:46 -0500 | [diff] [blame] | 131 | if (std::filesystem::exists(path)) |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 132 | { |
Brad Bishop | 9f44c99 | 2020-11-06 14:48:46 -0500 | [diff] [blame] | 133 | std::filesystem::remove(path); |
Michael Tritz | 60bc20f | 2017-07-29 23:32:21 -0500 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | } // namespace updater |
| 138 | } // namespace software |
| 139 | } // namespace openpower |