Andrew Jeffery | 4fe996c | 2018-02-27 12:16:48 +1030 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 */ |
| 2 | /* Copyright (C) 2018 IBM Corp. */ |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 3 | #pragma once |
| 4 | |
| 5 | #include <vector> |
| 6 | #include <memory> |
| 7 | #include <numeric> |
| 8 | #include <experimental/filesystem> |
Andrew Jeffery | 7f9c343 | 2018-03-01 12:07:13 +1030 | [diff] [blame] | 9 | #include "common.h" |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 10 | #include "pnor_partition_defs.h" |
| 11 | |
Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame^] | 12 | struct mbox_context; |
| 13 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 14 | namespace openpower |
| 15 | { |
| 16 | namespace virtual_pnor |
| 17 | { |
| 18 | |
| 19 | namespace fs = std::experimental::filesystem; |
| 20 | |
| 21 | using PartitionTable = std::vector<uint8_t>; |
| 22 | using checksum_t = uint32_t; |
| 23 | |
| 24 | /** @brief Convert the input partition table to big endian. |
| 25 | * |
| 26 | * @param[in] src - reference to the pnor partition table |
| 27 | * |
| 28 | * @returns converted partition table |
| 29 | */ |
| 30 | PartitionTable endianFixup(const PartitionTable& src); |
| 31 | |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 32 | /** @brief Parse a ToC line (entry) into the corresponding FFS partition |
| 33 | * object. |
| 34 | * |
| 35 | * @param[in] line - The ToC line to parse |
| 36 | * @param[in] blockSize - The flash block size in bytes |
| 37 | * @param[out] part - The partition object to populate with the information |
| 38 | * parsed from the provided ToC line |
| 39 | * |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 40 | * Throws: MalformedTocEntry, InvalidTocEntry |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 41 | */ |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 42 | void parseTocLine(const std::string& line, size_t blockSize, |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 43 | pnor_partition& part); |
| 44 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 45 | namespace details |
| 46 | { |
| 47 | |
| 48 | /** @brief Compute XOR-based checksum, by XORing consecutive words |
| 49 | * in the input data. Input must be aligned to word boundary. |
| 50 | * |
| 51 | * @param[in] data - input data on which checksum is computed |
| 52 | * |
| 53 | * @returns computed checksum |
| 54 | */ |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 55 | template <class T> checksum_t checksum(const T& data) |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 56 | { |
| 57 | static_assert(sizeof(decltype(data)) % sizeof(checksum_t) == 0, |
| 58 | "sizeof(data) is not aligned to sizeof(checksum_t) boundary"); |
| 59 | |
| 60 | auto begin = reinterpret_cast<const checksum_t*>(&data); |
| 61 | auto end = begin + (sizeof(decltype(data)) / sizeof(checksum_t)); |
| 62 | |
| 63 | return std::accumulate(begin, end, 0, std::bit_xor<checksum_t>()); |
| 64 | } |
| 65 | |
| 66 | } // namespace details |
| 67 | |
| 68 | namespace partition |
| 69 | { |
| 70 | |
| 71 | /** @class Table |
| 72 | * @brief Generates virtual PNOR partition table. |
| 73 | * |
| 74 | * Generates virtual PNOR partition table upon construction. Reads |
| 75 | * the PNOR information generated by this tool : |
| 76 | * github.com/openbmc/openpower-pnor-code-mgmt/blob/master/generate-squashfs, |
| 77 | * which generates a minimalistic table-of-contents (toc) file and |
| 78 | * individual files to represent various partitions that are of interest - |
| 79 | * these help form the "virtual" PNOR, which is typically a subset of the full |
| 80 | * PNOR image. |
| 81 | * These files are stored in a well-known location on the PNOR. |
| 82 | * Based on this information, this class prepares the partition table whose |
| 83 | * structure is as outlined in pnor_partition.h. |
| 84 | * |
| 85 | * The virtual PNOR supports 4KB erase blocks - partitions must be aligned to |
| 86 | * this size. |
| 87 | */ |
| 88 | class Table |
| 89 | { |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 90 | public: |
| 91 | /** @brief Constructor accepting the path of the directory |
| 92 | * that houses the PNOR partition files. |
| 93 | * |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 94 | * @param[in] ctx - Acquire sizes and paths relevant to the table |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 95 | * |
| 96 | * Throws MalformedTocEntry, InvalidTocEntry |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 97 | */ |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 98 | Table(const struct mbox_context* ctx); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 99 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 100 | Table(const Table&) = delete; |
| 101 | Table& operator=(const Table&) = delete; |
| 102 | Table(Table&&) = delete; |
| 103 | Table& operator=(Table&&) = delete; |
| 104 | ~Table() = default; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 105 | |
Andrew Jeffery | 7f9c343 | 2018-03-01 12:07:13 +1030 | [diff] [blame] | 106 | /** @brief Return the exact size of partition table in bytes |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 107 | * |
Andrew Jeffery | 7f9c343 | 2018-03-01 12:07:13 +1030 | [diff] [blame] | 108 | * @returns size_t - size of partition table in bytes |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 109 | */ |
| 110 | size_t size() const |
| 111 | { |
Andrew Jeffery | 7f9c343 | 2018-03-01 12:07:13 +1030 | [diff] [blame] | 112 | return szBytes; |
| 113 | } |
| 114 | |
| 115 | /** @brief Return aligned size of partition table in bytes |
| 116 | * |
| 117 | * The value returned will be greater-than or equal to size(), and |
| 118 | * aligned to blockSize. |
| 119 | * |
| 120 | * @returns size_t - capacity of partition table in bytes |
| 121 | */ |
| 122 | size_t capacity() const |
| 123 | { |
| 124 | return align_up(szBytes, blockSize); |
| 125 | } |
| 126 | |
| 127 | /** @brief Return the size of partition table in blocks |
| 128 | * |
| 129 | * @returns size_t - size of partition table in blocks |
| 130 | */ |
| 131 | size_t blocks() const |
| 132 | { |
| 133 | return capacity() / blockSize; |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 134 | } |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 135 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 136 | /** @brief Return a partition table having byte-ordering |
| 137 | * that the host expects. |
| 138 | * |
| 139 | * The host needs the partion table in big-endian. |
| 140 | * |
| 141 | * @returns const reference to host partition table. |
| 142 | */ |
| 143 | const pnor_partition_table& getHostTable() const |
| 144 | { |
| 145 | return *(reinterpret_cast<const pnor_partition_table*>(hostTbl.data())); |
| 146 | } |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 147 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 148 | /** @brief Return a little-endian partition table |
| 149 | * |
| 150 | * @returns const reference to native partition table |
| 151 | */ |
| 152 | const pnor_partition_table& getNativeTable() const |
| 153 | { |
| 154 | return *(reinterpret_cast<const pnor_partition_table*>(tbl.data())); |
| 155 | } |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 156 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 157 | /** @brief Return partition corresponding to PNOR offset, the offset |
| 158 | * is within returned partition. |
| 159 | * |
| 160 | * @param[in] offset - PNOR offset in bytes |
| 161 | * |
| 162 | * @returns const reference to pnor_partition, if found, else an |
| 163 | * exception will be thrown. |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame] | 164 | * |
| 165 | * Throws: UnmappedOffset |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 166 | */ |
| 167 | const pnor_partition& partition(size_t offset) const; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 168 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 169 | /** @brief Return partition corresponding to input partition name. |
| 170 | * |
| 171 | * @param[in] name - PNOR partition name |
| 172 | * |
| 173 | * @returns const reference to pnor_partition, if found, else an |
| 174 | * exception will be thrown. |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame] | 175 | * |
| 176 | * Throws: UnknownPartition |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 177 | */ |
| 178 | const pnor_partition& partition(const std::string& name) const; |
Deepak Kodihalli | 8a89969 | 2017-07-11 23:17:19 -0500 | [diff] [blame] | 179 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 180 | private: |
| 181 | /** @brief Prepares a vector of PNOR partition structures. |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 182 | * |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 183 | * @param[in] ctx - An mbox context providing partition locations |
| 184 | * |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 185 | * Throws: MalformedTocEntry, InvalidTocEntry |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 186 | */ |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 187 | void preparePartitions(const struct mbox_context* ctx); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 188 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 189 | /** @brief Prepares the PNOR header. |
| 190 | */ |
| 191 | void prepareHeader(); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 192 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 193 | /** @brief Allocate memory to hold the partion table. Determine the |
| 194 | * amount needed based on the partition files in the toc file. |
| 195 | * |
| 196 | * @param[in] tocFile - Table of contents file path. |
| 197 | */ |
| 198 | void allocateMemory(const fs::path& tocFile); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 199 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 200 | /** @brief Return a little-endian partition table |
| 201 | * |
| 202 | * @returns reference to native partition table |
| 203 | */ |
| 204 | pnor_partition_table& getNativeTable() |
| 205 | { |
| 206 | return *(reinterpret_cast<pnor_partition_table*>(tbl.data())); |
| 207 | } |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 208 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 209 | /** @brief Size of the PNOR partition table - |
| 210 | * sizeof(pnor_partition_table) + |
| 211 | * (no. of partitions * sizeof(pnor_partition)), |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 212 | */ |
Andrew Jeffery | 7f9c343 | 2018-03-01 12:07:13 +1030 | [diff] [blame] | 213 | size_t szBytes; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 214 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 215 | /** @brief Partition table */ |
| 216 | PartitionTable tbl; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 217 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 218 | /** @brief Partition table with host byte ordering */ |
| 219 | PartitionTable hostTbl; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 220 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 221 | /** @brief Directory housing generated PNOR partition files */ |
| 222 | fs::path directory; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 223 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 224 | /** @brief Number of partitions */ |
| 225 | size_t numParts; |
Deepak Kodihalli | d1d7930 | 2017-07-11 23:01:39 -0500 | [diff] [blame] | 226 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 227 | /** @brief PNOR block size, in bytes */ |
| 228 | size_t blockSize; |
Deepak Kodihalli | d1d7930 | 2017-07-11 23:01:39 -0500 | [diff] [blame] | 229 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 230 | /** @brief PNOR size, in bytes */ |
| 231 | size_t pnorSize; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 232 | }; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 233 | } // namespace partition |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 234 | |
| 235 | /** @brief An exception type storing a reason string. |
| 236 | * |
| 237 | * This looks a lot like how std::runtime_error might be implemented however |
| 238 | * we want to avoid extending it, as exceptions extending ReasonedError have |
| 239 | * an expectation of being handled (can be predicted and are inside the scope |
| 240 | * of the program). |
| 241 | * |
| 242 | * From std::runtime_error documentation[1]: |
| 243 | * |
| 244 | * > Defines a type of object to be thrown as exception. It reports errors |
| 245 | * > that are due to events beyond the scope of the program and can not be |
| 246 | * > easily predicted. |
| 247 | * |
| 248 | * [1] http://en.cppreference.com/w/cpp/error/runtime_error |
| 249 | * |
| 250 | * We need to keep the inheritance hierarchy separate: This avoids the |
| 251 | * introduction of code that overzealously catches std::runtime_error to |
| 252 | * handle exceptions that would otherwise derive ReasonedError, and in the |
| 253 | * process swallows genuine runtime failures. |
| 254 | */ |
| 255 | class ReasonedError : public std::exception |
| 256 | { |
| 257 | public: |
| 258 | ReasonedError(const std::string&& what) : _what(what) |
| 259 | { |
| 260 | } |
| 261 | const char* what() const noexcept |
| 262 | { |
| 263 | return _what.c_str(); |
| 264 | }; |
| 265 | |
| 266 | private: |
| 267 | const std::string _what; |
| 268 | }; |
| 269 | |
| 270 | /** @brief Base exception type for errors related to ToC entry parsing. |
| 271 | * |
| 272 | * Callers of parseTocEntry() may not be concerned with the specifics and |
| 273 | * rather just want to extract and log what(). |
| 274 | */ |
| 275 | class TocEntryError : public ReasonedError |
| 276 | { |
| 277 | public: |
| 278 | TocEntryError(const std::string&& reason) : ReasonedError(std::move(reason)) |
| 279 | { |
| 280 | } |
| 281 | }; |
| 282 | |
| 283 | /** @brief The exception thrown on finding a syntax error in the ToC entry |
| 284 | * |
| 285 | * If the syntax is wrong, or expected values are missing, the ToC entry is |
| 286 | * malformed |
| 287 | */ |
| 288 | class MalformedTocEntry : public TocEntryError |
| 289 | { |
| 290 | public: |
| 291 | MalformedTocEntry(const std::string&& reason) : |
| 292 | TocEntryError(std::move(reason)) |
| 293 | { |
| 294 | } |
| 295 | }; |
| 296 | |
| 297 | /** @brief The exception thrown on finding a semantic error in the ToC entry |
| 298 | * |
| 299 | * If the syntax of the ToC entry is correct but the semantics are broken, |
| 300 | * then we have an invalid ToC entry. |
| 301 | */ |
| 302 | class InvalidTocEntry : public TocEntryError |
| 303 | { |
| 304 | public: |
| 305 | InvalidTocEntry(const std::string&& reason) : |
| 306 | TocEntryError(std::move(reason)) |
| 307 | { |
| 308 | } |
| 309 | }; |
| 310 | |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame] | 311 | class UnmappedOffset : public std::exception |
| 312 | { |
| 313 | public: |
| 314 | UnmappedOffset(size_t base, size_t next) : base(base), next(next) |
| 315 | { |
| 316 | } |
| 317 | |
| 318 | const size_t base; |
| 319 | const size_t next; |
| 320 | }; |
| 321 | |
| 322 | class OutOfBoundsOffset : public ReasonedError |
| 323 | { |
| 324 | public: |
| 325 | OutOfBoundsOffset(const std::string&& reason) : |
| 326 | ReasonedError(std::move(reason)) |
| 327 | { |
| 328 | } |
| 329 | }; |
| 330 | |
| 331 | class UnknownPartition : public ReasonedError |
| 332 | { |
| 333 | public: |
| 334 | UnknownPartition(const std::string&& reason) : |
| 335 | ReasonedError(std::move(reason)) |
| 336 | { |
| 337 | } |
| 338 | }; |
| 339 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 340 | } // namespace virtual_pnor |
| 341 | } // namespace openpower |
Andrew Jeffery | 2ceba89 | 2018-02-28 17:44:54 +1030 | [diff] [blame] | 342 | |
| 343 | struct vpnor_partition_table |
| 344 | { |
| 345 | openpower::virtual_pnor::partition::Table* table; |
| 346 | }; |