Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 1 | #include "config.h" |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 2 | |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 3 | #include "serialize.hpp" |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 4 | |
| 5 | #include <cereal/archives/json.hpp> |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 6 | #include <phosphor-logging/lg2.hpp> |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 7 | #include <sdbusplus/server.hpp> |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 8 | |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 9 | #include <filesystem> |
| 10 | #include <fstream> |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 11 | #include <system_error> |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 12 | |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 13 | namespace phosphor |
| 14 | { |
| 15 | namespace software |
| 16 | { |
| 17 | namespace updater |
| 18 | { |
| 19 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 20 | PHOSPHOR_LOG2_USING; |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame] | 21 | namespace fs = std::filesystem; |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 22 | |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 23 | const std::string priorityName = "priority"; |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 24 | const std::string purposeName = "purpose"; |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 25 | |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 26 | void storePriority(const std::string& flashId, uint8_t priority) |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 27 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 28 | std::error_code ec; |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 29 | auto path = fs::path(PERSIST_DIR) / flashId; |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 30 | if (!fs::is_directory(path, ec)) |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 31 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 32 | if (fs::exists(path, ec)) |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 33 | { |
| 34 | // Delete if it's a non-directory file |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 35 | warning("Removing non-directory file: {PATH}", "PATH", path); |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 36 | fs::remove_all(path, ec); |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 37 | } |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 38 | fs::create_directories(path, ec); |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 39 | } |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 40 | path = path / priorityName; |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 41 | |
| 42 | std::ofstream os(path.c_str()); |
| 43 | cereal::JSONOutputArchive oarchive(os); |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 44 | oarchive(cereal::make_nvp(priorityName, priority)); |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 45 | } |
| 46 | |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 47 | void storePurpose(const std::string& flashId, VersionPurpose purpose) |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 48 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 49 | std::error_code ec; |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 50 | auto path = fs::path(PERSIST_DIR) / flashId; |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 51 | if (!fs::is_directory(path, ec)) |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 52 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 53 | if (fs::exists(path, ec)) |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 54 | { |
| 55 | // Delete if it's a non-directory file |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 56 | warning("Removing non-directory file: {PATH}", "PATH", path); |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 57 | fs::remove_all(path, ec); |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 58 | } |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 59 | fs::create_directories(path, ec); |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 60 | } |
| 61 | path = path / purposeName; |
| 62 | |
| 63 | std::ofstream os(path.c_str()); |
| 64 | cereal::JSONOutputArchive oarchive(os); |
| 65 | oarchive(cereal::make_nvp(purposeName, purpose)); |
| 66 | } |
| 67 | |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 68 | bool restorePriority(const std::string& flashId, uint8_t& priority) |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 69 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 70 | std::error_code ec; |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 71 | auto path = fs::path(PERSIST_DIR) / flashId / priorityName; |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 72 | if (fs::exists(path, ec)) |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 73 | { |
| 74 | std::ifstream is(path.c_str(), std::ios::in); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 75 | try |
| 76 | { |
| 77 | cereal::JSONInputArchive iarchive(is); |
Adriana Kobylak | 687e75e | 2019-11-07 11:23:25 -0600 | [diff] [blame] | 78 | iarchive(cereal::make_nvp(priorityName, priority)); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 79 | return true; |
| 80 | } |
Patrick Williams | 58e1897 | 2021-10-06 12:25:58 -0500 | [diff] [blame] | 81 | catch (const cereal::Exception& e) |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 82 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 83 | fs::remove_all(path, ec); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 84 | } |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 85 | } |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 86 | |
| 87 | // Find the mtd device "u-boot-env" to retrieve the environment variables |
| 88 | std::ifstream mtdDevices("/proc/mtd"); |
Pavithra Barithaya | 7b7fb30 | 2024-06-26 01:37:41 -0500 | [diff] [blame] | 89 | std::string device; |
| 90 | std::string devicePath; |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 91 | |
| 92 | try |
| 93 | { |
Gunnar Mills | ffe0492 | 2017-10-06 13:20:43 -0500 | [diff] [blame] | 94 | while (std::getline(mtdDevices, device)) |
| 95 | { |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 96 | if (device.find("u-boot-env") != std::string::npos) |
| 97 | { |
| 98 | devicePath = "/dev/" + device.substr(0, device.find(':')); |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (!devicePath.empty()) |
| 104 | { |
| 105 | std::ifstream input(devicePath.c_str()); |
| 106 | std::string envVars; |
| 107 | std::getline(input, envVars); |
| 108 | |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 109 | std::string versionVar = flashId + "="; |
Michael Tritz | 6273efd | 2017-12-14 15:21:18 -0600 | [diff] [blame] | 110 | auto varPosition = envVars.find(versionVar); |
| 111 | |
| 112 | if (varPosition != std::string::npos) |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 113 | { |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 114 | // Grab the environment variable for this flashId. These |
| 115 | // variables follow the format "flashId=priority\0" |
Michael Tritz | 6273efd | 2017-12-14 15:21:18 -0600 | [diff] [blame] | 116 | auto var = envVars.substr(varPosition); |
| 117 | priority = std::stoi(var.substr(versionVar.length())); |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | } |
| 121 | } |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 122 | catch (const std::exception& e) |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 123 | { |
| 124 | error("Error during processing: {ERROR}", "ERROR", e); |
| 125 | } |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 126 | |
| 127 | return false; |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 130 | bool restorePurpose(const std::string& flashId, VersionPurpose& purpose) |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 131 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 132 | std::error_code ec; |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 133 | auto path = fs::path(PERSIST_DIR) / flashId / purposeName; |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 134 | if (fs::exists(path, ec)) |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 135 | { |
| 136 | std::ifstream is(path.c_str(), std::ios::in); |
| 137 | try |
| 138 | { |
| 139 | cereal::JSONInputArchive iarchive(is); |
| 140 | iarchive(cereal::make_nvp(purposeName, purpose)); |
| 141 | return true; |
| 142 | } |
Patrick Williams | 58e1897 | 2021-10-06 12:25:58 -0500 | [diff] [blame] | 143 | catch (const cereal::Exception& e) |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 144 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 145 | fs::remove_all(path, ec); |
Adriana Kobylak | ec4eec3 | 2019-11-13 14:28:35 -0600 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | return false; |
| 150 | } |
| 151 | |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 152 | void removePersistDataDirectory(const std::string& flashId) |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 153 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 154 | std::error_code ec; |
Adriana Kobylak | 780220f | 2022-01-18 20:01:53 +0000 | [diff] [blame] | 155 | auto path = fs::path(PERSIST_DIR) / flashId; |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 156 | if (fs::exists(path, ec)) |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 157 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 158 | fs::remove_all(path, ec); |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Gunnar Mills | fa34e02 | 2018-09-04 10:05:45 -0500 | [diff] [blame] | 162 | } // namespace updater |
Saqib Khan | 5d53267 | 2017-08-09 10:44:50 -0500 | [diff] [blame] | 163 | } // namespace software |
Gunnar Mills | fa34e02 | 2018-09-04 10:05:45 -0500 | [diff] [blame] | 164 | } // namespace phosphor |