| Alexander Hansen | 40fb549 | 2025-10-28 17:56:12 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2019 IBM Corporation |
| 3 | |
| Matt Spinler | a906c94 | 2019-10-08 13:42:05 -0500 | [diff] [blame] | 4 | #include "fru_identity.hpp" |
| 5 | |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 6 | #include "pel_values.hpp" |
| 7 | |
| Arya K Padman | 5bc2653 | 2024-04-10 06:19:25 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/lg2.hpp> |
| Matt Spinler | a27e2e5 | 2020-04-09 11:06:11 -0500 | [diff] [blame] | 9 | |
| Matt Spinler | a906c94 | 2019-10-08 13:42:05 -0500 | [diff] [blame] | 10 | namespace openpower |
| 11 | { |
| 12 | namespace pels |
| 13 | { |
| 14 | namespace src |
| 15 | { |
| 16 | |
| Matt Spinler | 18f4b6e | 2020-05-21 11:36:24 -0500 | [diff] [blame] | 17 | namespace |
| 18 | { |
| 19 | |
| 20 | /** |
| 21 | * @brief Fills in the std::array from the string value |
| 22 | * |
| 23 | * If the string is shorter than the array, it will be padded with |
| 24 | * '\0's. |
| 25 | * |
| 26 | * @param[in] source - The string to fill in the array with |
| 27 | * @param[out] target - The input object that supports [] and size() |
| 28 | */ |
| 29 | template <typename T> |
| 30 | void fillArray(const std::string& source, T& target) |
| 31 | { |
| 32 | for (size_t i = 0; i < target.size(); i++) |
| 33 | { |
| 34 | target[i] = (source.size() > i) ? source[i] : '\0'; |
| 35 | } |
| 36 | } |
| 37 | |
| Patrick Williams | d26fa3e | 2021-04-21 15:22:23 -0500 | [diff] [blame] | 38 | } // namespace |
| Matt Spinler | 18f4b6e | 2020-05-21 11:36:24 -0500 | [diff] [blame] | 39 | |
| Matt Spinler | a906c94 | 2019-10-08 13:42:05 -0500 | [diff] [blame] | 40 | FRUIdentity::FRUIdentity(Stream& pel) |
| 41 | { |
| 42 | pel >> _type >> _size >> _flags; |
| 43 | |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 44 | if (hasPN() || hasMP()) |
| Matt Spinler | a906c94 | 2019-10-08 13:42:05 -0500 | [diff] [blame] | 45 | { |
| 46 | pel.read(_pnOrProcedureID.data(), _pnOrProcedureID.size()); |
| 47 | } |
| 48 | |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 49 | if (hasCCIN()) |
| Matt Spinler | a906c94 | 2019-10-08 13:42:05 -0500 | [diff] [blame] | 50 | { |
| 51 | pel.read(_ccin.data(), _ccin.size()); |
| 52 | } |
| 53 | |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 54 | if (hasSN()) |
| Matt Spinler | a906c94 | 2019-10-08 13:42:05 -0500 | [diff] [blame] | 55 | { |
| 56 | pel.read(_sn.data(), _sn.size()); |
| 57 | } |
| 58 | } |
| 59 | |
| Matt Spinler | a09354b | 2020-03-13 10:24:52 -0500 | [diff] [blame] | 60 | size_t FRUIdentity::flattenedSize() const |
| 61 | { |
| 62 | size_t size = sizeof(_type) + sizeof(_size) + sizeof(_flags); |
| 63 | |
| 64 | if (hasPN() || hasMP()) |
| 65 | { |
| 66 | size += _pnOrProcedureID.size(); |
| 67 | } |
| 68 | |
| 69 | if (hasCCIN()) |
| 70 | { |
| 71 | size += _ccin.size(); |
| 72 | } |
| 73 | |
| 74 | if (hasSN()) |
| 75 | { |
| 76 | size += _sn.size(); |
| 77 | } |
| 78 | |
| 79 | return size; |
| 80 | } |
| 81 | |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 82 | FRUIdentity::FRUIdentity(const std::string& partNumber, const std::string& ccin, |
| 83 | const std::string& serialNumber) |
| 84 | { |
| 85 | _type = substructureType; |
| 86 | _flags = hardwareFRU; |
| 87 | |
| 88 | setPartNumber(partNumber); |
| 89 | setCCIN(ccin); |
| 90 | setSerialNumber(serialNumber); |
| 91 | |
| 92 | _size = flattenedSize(); |
| 93 | } |
| 94 | |
| Matt Spinler | 468aab5 | 2020-08-13 11:04:31 -0500 | [diff] [blame] | 95 | FRUIdentity::FRUIdentity(const std::string& procedure, CalloutValueType type) |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 96 | { |
| 97 | _type = substructureType; |
| 98 | _flags = maintenanceProc; |
| 99 | |
| Matt Spinler | 468aab5 | 2020-08-13 11:04:31 -0500 | [diff] [blame] | 100 | setMaintenanceProcedure(procedure, type); |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 101 | |
| 102 | _size = flattenedSize(); |
| 103 | } |
| 104 | |
| Matt Spinler | 468aab5 | 2020-08-13 11:04:31 -0500 | [diff] [blame] | 105 | FRUIdentity::FRUIdentity(const std::string& fru, CalloutValueType type, |
| Matt Spinler | 2b6dfa0 | 2020-04-09 11:39:05 -0500 | [diff] [blame] | 106 | bool trustedLocationCode) |
| 107 | { |
| 108 | _type = substructureType; |
| 109 | _flags = (trustedLocationCode) ? symbolicFRUTrustedLocCode : symbolicFRU; |
| 110 | |
| Matt Spinler | 468aab5 | 2020-08-13 11:04:31 -0500 | [diff] [blame] | 111 | setSymbolicFRU(fru, type); |
| Matt Spinler | 2b6dfa0 | 2020-04-09 11:39:05 -0500 | [diff] [blame] | 112 | |
| 113 | _size = flattenedSize(); |
| 114 | } |
| 115 | |
| Matt Spinler | a906c94 | 2019-10-08 13:42:05 -0500 | [diff] [blame] | 116 | std::optional<std::string> FRUIdentity::getPN() const |
| 117 | { |
| 118 | if (hasPN()) |
| 119 | { |
| 120 | // NULL terminated |
| 121 | std::string pn{_pnOrProcedureID.data()}; |
| 122 | return pn; |
| 123 | } |
| 124 | |
| 125 | return std::nullopt; |
| 126 | } |
| 127 | |
| 128 | std::optional<std::string> FRUIdentity::getMaintProc() const |
| 129 | { |
| 130 | if (hasMP()) |
| 131 | { |
| 132 | // NULL terminated |
| 133 | std::string mp{_pnOrProcedureID.data()}; |
| 134 | return mp; |
| 135 | } |
| 136 | |
| 137 | return std::nullopt; |
| 138 | } |
| 139 | |
| 140 | std::optional<std::string> FRUIdentity::getCCIN() const |
| 141 | { |
| 142 | if (hasCCIN()) |
| 143 | { |
| 144 | std::string ccin{_ccin.begin(), _ccin.begin() + _ccin.size()}; |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 145 | |
| 146 | // Don't leave any NULLs in the string (not there usually) |
| 147 | if (auto pos = ccin.find('\0'); pos != std::string::npos) |
| 148 | { |
| 149 | ccin.resize(pos); |
| 150 | } |
| Matt Spinler | a906c94 | 2019-10-08 13:42:05 -0500 | [diff] [blame] | 151 | return ccin; |
| 152 | } |
| 153 | |
| 154 | return std::nullopt; |
| 155 | } |
| 156 | |
| 157 | std::optional<std::string> FRUIdentity::getSN() const |
| 158 | { |
| 159 | if (hasSN()) |
| 160 | { |
| 161 | std::string sn{_sn.begin(), _sn.begin() + _sn.size()}; |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 162 | |
| 163 | // Don't leave any NULLs in the string (not there usually) |
| 164 | if (auto pos = sn.find('\0'); pos != std::string::npos) |
| 165 | { |
| 166 | sn.resize(pos); |
| 167 | } |
| Matt Spinler | a906c94 | 2019-10-08 13:42:05 -0500 | [diff] [blame] | 168 | return sn; |
| 169 | } |
| 170 | |
| 171 | return std::nullopt; |
| 172 | } |
| 173 | |
| Matt Spinler | 724d0d8 | 2019-11-06 10:05:36 -0600 | [diff] [blame] | 174 | void FRUIdentity::flatten(Stream& pel) const |
| Matt Spinler | a906c94 | 2019-10-08 13:42:05 -0500 | [diff] [blame] | 175 | { |
| 176 | pel << _type << _size << _flags; |
| 177 | |
| 178 | if (hasPN() || hasMP()) |
| 179 | { |
| 180 | pel.write(_pnOrProcedureID.data(), _pnOrProcedureID.size()); |
| 181 | } |
| 182 | |
| 183 | if (hasCCIN()) |
| 184 | { |
| 185 | pel.write(_ccin.data(), _ccin.size()); |
| 186 | } |
| 187 | |
| 188 | if (hasSN()) |
| 189 | { |
| 190 | pel.write(_sn.data(), _sn.size()); |
| 191 | } |
| 192 | } |
| 193 | |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 194 | void FRUIdentity::setPartNumber(const std::string& partNumber) |
| 195 | { |
| 196 | _flags |= pnSupplied; |
| 197 | _flags &= ~maintProcSupplied; |
| 198 | |
| 199 | auto pn = partNumber; |
| 200 | |
| 201 | // Strip leading whitespace on this one. |
| Patrick Williams | 66f3675 | 2024-09-30 21:47:57 -0400 | [diff] [blame] | 202 | while (!pn.empty() && (' ' == pn.front())) |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 203 | { |
| Patrick Williams | 66f3675 | 2024-09-30 21:47:57 -0400 | [diff] [blame] | 204 | pn.erase(0, 1); |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 205 | } |
| 206 | |
| Matt Spinler | 18f4b6e | 2020-05-21 11:36:24 -0500 | [diff] [blame] | 207 | fillArray(pn, _pnOrProcedureID); |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 208 | |
| 209 | // ensure null terminated |
| 210 | _pnOrProcedureID.back() = 0; |
| 211 | } |
| 212 | |
| 213 | void FRUIdentity::setCCIN(const std::string& ccin) |
| 214 | { |
| 215 | _flags |= ccinSupplied; |
| 216 | |
| Matt Spinler | 18f4b6e | 2020-05-21 11:36:24 -0500 | [diff] [blame] | 217 | fillArray(ccin, _ccin); |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void FRUIdentity::setSerialNumber(const std::string& serialNumber) |
| 221 | { |
| 222 | _flags |= snSupplied; |
| 223 | |
| Matt Spinler | 18f4b6e | 2020-05-21 11:36:24 -0500 | [diff] [blame] | 224 | fillArray(serialNumber, _sn); |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 225 | } |
| 226 | |
| Matt Spinler | 468aab5 | 2020-08-13 11:04:31 -0500 | [diff] [blame] | 227 | void FRUIdentity::setMaintenanceProcedure(const std::string& procedure, |
| 228 | CalloutValueType type) |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 229 | { |
| 230 | _flags |= maintProcSupplied; |
| 231 | _flags &= ~pnSupplied; |
| 232 | |
| Matt Spinler | 468aab5 | 2020-08-13 11:04:31 -0500 | [diff] [blame] | 233 | if (type == CalloutValueType::registryName) |
| Matt Spinler | a27e2e5 | 2020-04-09 11:06:11 -0500 | [diff] [blame] | 234 | { |
| Matt Spinler | 468aab5 | 2020-08-13 11:04:31 -0500 | [diff] [blame] | 235 | if (pel_values::maintenanceProcedures.count(procedure)) |
| 236 | { |
| 237 | fillArray(pel_values::maintenanceProcedures.at(procedure), |
| 238 | _pnOrProcedureID); |
| 239 | } |
| 240 | else |
| 241 | { |
| Arya K Padman | 5bc2653 | 2024-04-10 06:19:25 -0500 | [diff] [blame] | 242 | lg2::error("Invalid maintenance procedure {PROCEDURE}", "PROCEDURE", |
| 243 | procedure); |
| Matt Spinler | 468aab5 | 2020-08-13 11:04:31 -0500 | [diff] [blame] | 244 | strncpy(_pnOrProcedureID.data(), "INVALID", |
| 245 | _pnOrProcedureID.size()); |
| 246 | } |
| Matt Spinler | a27e2e5 | 2020-04-09 11:06:11 -0500 | [diff] [blame] | 247 | } |
| 248 | else |
| 249 | { |
| Matt Spinler | 468aab5 | 2020-08-13 11:04:31 -0500 | [diff] [blame] | 250 | fillArray(procedure, _pnOrProcedureID); |
| Matt Spinler | a27e2e5 | 2020-04-09 11:06:11 -0500 | [diff] [blame] | 251 | } |
| Matt Spinler | ba0ee00 | 2020-03-13 11:24:14 -0500 | [diff] [blame] | 252 | |
| 253 | // ensure null terminated |
| 254 | _pnOrProcedureID.back() = 0; |
| 255 | } |
| 256 | |
| Matt Spinler | 468aab5 | 2020-08-13 11:04:31 -0500 | [diff] [blame] | 257 | void FRUIdentity::setSymbolicFRU(const std::string& symbolicFRU, |
| 258 | CalloutValueType type) |
| Matt Spinler | 2b6dfa0 | 2020-04-09 11:39:05 -0500 | [diff] [blame] | 259 | { |
| Matt Spinler | 2b6dfa0 | 2020-04-09 11:39:05 -0500 | [diff] [blame] | 260 | // Treat this has a HW callout. |
| 261 | _flags |= pnSupplied; |
| 262 | _flags &= ~maintProcSupplied; |
| 263 | |
| Matt Spinler | 468aab5 | 2020-08-13 11:04:31 -0500 | [diff] [blame] | 264 | if (type == CalloutValueType::registryName) |
| Matt Spinler | 2b6dfa0 | 2020-04-09 11:39:05 -0500 | [diff] [blame] | 265 | { |
| Matt Spinler | 468aab5 | 2020-08-13 11:04:31 -0500 | [diff] [blame] | 266 | if (pel_values::symbolicFRUs.count(symbolicFRU)) |
| 267 | { |
| 268 | fillArray(pel_values::symbolicFRUs.at(symbolicFRU), |
| 269 | _pnOrProcedureID); |
| 270 | } |
| 271 | else |
| 272 | { |
| Arya K Padman | 5bc2653 | 2024-04-10 06:19:25 -0500 | [diff] [blame] | 273 | lg2::error("Invalid symbolic FRU {FRU}", "FRU", symbolicFRU); |
| Matt Spinler | 468aab5 | 2020-08-13 11:04:31 -0500 | [diff] [blame] | 274 | strncpy(_pnOrProcedureID.data(), "INVALID", |
| 275 | _pnOrProcedureID.size()); |
| 276 | } |
| Matt Spinler | 2b6dfa0 | 2020-04-09 11:39:05 -0500 | [diff] [blame] | 277 | } |
| 278 | else |
| 279 | { |
| Matt Spinler | 468aab5 | 2020-08-13 11:04:31 -0500 | [diff] [blame] | 280 | fillArray(symbolicFRU, _pnOrProcedureID); |
| Matt Spinler | 2b6dfa0 | 2020-04-09 11:39:05 -0500 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | // ensure null terminated |
| 284 | _pnOrProcedureID.back() = 0; |
| 285 | } |
| 286 | |
| Matt Spinler | a906c94 | 2019-10-08 13:42:05 -0500 | [diff] [blame] | 287 | } // namespace src |
| 288 | } // namespace pels |
| 289 | } // namespace openpower |