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