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