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 | #include "pnor_partition_table.hpp" |
| 4 | #include "common.h" |
| 5 | #include "config.h" |
Deepak Kodihalli | 8a89969 | 2017-07-11 23:17:19 -0500 | [diff] [blame] | 6 | #include "xyz/openbmc_project/Common/error.hpp" |
| 7 | #include <phosphor-logging/elog-errors.hpp> |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 8 | #include <syslog.h> |
| 9 | #include <endian.h> |
| 10 | #include <regex> |
| 11 | #include <fstream> |
| 12 | #include <algorithm> |
| 13 | |
| 14 | namespace openpower |
| 15 | { |
| 16 | namespace virtual_pnor |
| 17 | { |
| 18 | |
Deepak Kodihalli | 8a89969 | 2017-07-11 23:17:19 -0500 | [diff] [blame] | 19 | using namespace phosphor::logging; |
| 20 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 21 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 22 | namespace partition |
| 23 | { |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 24 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 25 | Table::Table(fs::path&& directory, size_t blockSize, size_t pnorSize) : |
Andrew Jeffery | 7f9c343 | 2018-03-01 12:07:13 +1030 | [diff] [blame] | 26 | szBytes(sizeof(pnor_partition_table)), directory(std::move(directory)), |
| 27 | numParts(0), blockSize(blockSize), pnorSize(pnorSize) |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 28 | { |
| 29 | preparePartitions(); |
| 30 | prepareHeader(); |
| 31 | hostTbl = endianFixup(tbl); |
| 32 | } |
| 33 | |
| 34 | void Table::prepareHeader() |
| 35 | { |
| 36 | decltype(auto) table = getNativeTable(); |
| 37 | table.data.magic = PARTITION_HEADER_MAGIC; |
| 38 | table.data.version = PARTITION_VERSION_1; |
Andrew Jeffery | 7f9c343 | 2018-03-01 12:07:13 +1030 | [diff] [blame] | 39 | table.data.size = blocks(); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 40 | table.data.entry_size = sizeof(pnor_partition); |
| 41 | table.data.entry_count = numParts; |
Deepak Kodihalli | d1d7930 | 2017-07-11 23:01:39 -0500 | [diff] [blame] | 42 | table.data.block_size = blockSize; |
| 43 | table.data.block_count = pnorSize / blockSize; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 44 | table.checksum = details::checksum(table.data); |
| 45 | } |
| 46 | |
| 47 | inline void Table::allocateMemory(const fs::path& tocFile) |
| 48 | { |
| 49 | size_t num = 0; |
| 50 | std::string line; |
| 51 | std::ifstream file(tocFile.c_str()); |
| 52 | |
| 53 | // Find number of lines in partition file - this will help |
| 54 | // determine the number of partitions and hence also how much |
| 55 | // memory to allocate for the partitions array. |
| 56 | // The actual number of partitions may turn out to be lesser than this, |
| 57 | // in case of errors. |
| 58 | while (std::getline(file, line)) |
| 59 | { |
| 60 | // Check if line starts with "partition" |
| 61 | if (std::string::npos != line.find("partition", 0)) |
| 62 | { |
| 63 | ++num; |
| 64 | } |
| 65 | } |
| 66 | |
Andrew Jeffery | 7f9c343 | 2018-03-01 12:07:13 +1030 | [diff] [blame] | 67 | szBytes = sizeof(pnor_partition_table) + (num * sizeof(pnor_partition)); |
| 68 | tbl.resize(capacity()); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Andrew Jeffery | 581a4f2 | 2018-02-21 15:29:38 +1030 | [diff] [blame] | 71 | void Table::preparePartitions() |
| 72 | { |
| 73 | fs::path tocFile = directory; |
| 74 | tocFile /= PARTITION_TOC_FILE; |
| 75 | allocateMemory(tocFile); |
| 76 | |
| 77 | std::ifstream file(tocFile.c_str()); |
| 78 | std::string line; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 79 | decltype(auto) table = getNativeTable(); |
| 80 | |
| 81 | while (std::getline(file, line)) |
| 82 | { |
Andrew Jeffery | faaa71c | 2018-02-21 16:29:48 +1030 | [diff] [blame] | 83 | pnor_partition& part = table.partitions[numParts]; |
| 84 | fs::path file; |
| 85 | |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 86 | // The ToC file presented in the vpnor squashfs looks like: |
| 87 | // |
| 88 | // version=IBM-witherspoon-ibm-OP9_v1.19_1.135 |
| 89 | // extended_version=op-build-v1.19-571-g04f4690-dirty,buildroot-2017.11-5-g65679be,skiboot-v5.10-rc4,hostboot-4c46d66,linux-4.14.20-openpower1-p4a6b675,petitboot-v1.6.6-pe5aaec2,machine-xml-0fea226,occ-3286b6b,hostboot-binaries-3d1af8f,capp-ucode-p9-dd2-v3,sbe-99e2fe2 |
| 90 | // partition00=part,0x00000000,0x00002000,00,READWRITE |
| 91 | // partition01=HBEL,0x00008000,0x0002c000,00,ECC,REPROVISION,CLEARECC,READWRITE |
| 92 | // ... |
| 93 | // |
| 94 | // As such we want to skip any lines that don't begin with 'partition' |
| 95 | if (std::string::npos == line.find("partition", 0)) |
Andrew Jeffery | faaa71c | 2018-02-21 16:29:48 +1030 | [diff] [blame] | 96 | { |
| 97 | continue; |
| 98 | } |
| 99 | |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 100 | parseTocLine(line, blockSize, part); |
| 101 | |
| 102 | file = directory / part.data.name; |
| 103 | if (!fs::exists(file)) |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 104 | { |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 105 | std::stringstream err; |
| 106 | err << "Partition file " << file.native() << " does not exist"; |
| 107 | throw InvalidTocEntry(err.str()); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 108 | } |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 109 | |
| 110 | ++numParts; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | const pnor_partition& Table::partition(size_t offset) const |
| 115 | { |
| 116 | const decltype(auto) table = getNativeTable(); |
Deepak Kodihalli | d1d7930 | 2017-07-11 23:01:39 -0500 | [diff] [blame] | 117 | size_t offt = offset / blockSize; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 118 | |
| 119 | for (decltype(numParts) i{}; i < numParts; ++i) |
| 120 | { |
| 121 | if ((offt >= table.partitions[i].data.base) && |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 122 | (offt < |
| 123 | (table.partitions[i].data.base + table.partitions[i].data.size))) |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 124 | { |
| 125 | return table.partitions[i]; |
| 126 | } |
| 127 | } |
| 128 | |
Andrew Jeffery | e8a79ff | 2018-02-27 10:56:32 +1030 | [diff] [blame] | 129 | MSG_ERR("Partition corresponding to offset 0x%zx not found", offset); |
Jayanth Othayoth | 59ce099 | 2017-10-27 00:45:53 -0500 | [diff] [blame] | 130 | elog<InternalFailure>(); |
| 131 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 132 | static pnor_partition p{}; |
| 133 | return p; |
| 134 | } |
| 135 | |
Deepak Kodihalli | 8a89969 | 2017-07-11 23:17:19 -0500 | [diff] [blame] | 136 | const pnor_partition& Table::partition(const std::string& name) const |
| 137 | { |
| 138 | const decltype(auto) table = getNativeTable(); |
| 139 | |
| 140 | for (decltype(numParts) i{}; i < numParts; ++i) |
| 141 | { |
| 142 | if (name == table.partitions[i].data.name) |
| 143 | { |
| 144 | return table.partitions[i]; |
| 145 | } |
| 146 | } |
| 147 | |
Andrew Jeffery | e8a79ff | 2018-02-27 10:56:32 +1030 | [diff] [blame] | 148 | MSG_ERR("Partition '%s' not found", name.c_str()); |
Deepak Kodihalli | 8a89969 | 2017-07-11 23:17:19 -0500 | [diff] [blame] | 149 | elog<InternalFailure>(); |
| 150 | static pnor_partition p{}; |
| 151 | return p; |
| 152 | } |
| 153 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 154 | } // namespace partition |
| 155 | |
| 156 | PartitionTable endianFixup(const PartitionTable& in) |
| 157 | { |
| 158 | PartitionTable out; |
| 159 | out.resize(in.size()); |
| 160 | auto src = reinterpret_cast<const pnor_partition_table*>(in.data()); |
| 161 | auto dst = reinterpret_cast<pnor_partition_table*>(out.data()); |
| 162 | |
| 163 | dst->data.magic = htobe32(src->data.magic); |
| 164 | dst->data.version = htobe32(src->data.version); |
| 165 | dst->data.size = htobe32(src->data.size); |
| 166 | dst->data.entry_size = htobe32(src->data.entry_size); |
| 167 | dst->data.entry_count = htobe32(src->data.entry_count); |
| 168 | dst->data.block_size = htobe32(src->data.block_size); |
| 169 | dst->data.block_count = htobe32(src->data.block_count); |
Deepak Kodihalli | b9cdcd2 | 2017-07-12 08:14:48 -0500 | [diff] [blame] | 170 | dst->checksum = details::checksum(dst->data); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 171 | |
| 172 | for (decltype(src->data.entry_count) i{}; i < src->data.entry_count; ++i) |
| 173 | { |
| 174 | auto psrc = &src->partitions[i]; |
| 175 | auto pdst = &dst->partitions[i]; |
| 176 | strncpy(pdst->data.name, psrc->data.name, PARTITION_NAME_MAX); |
| 177 | // Just to be safe |
| 178 | pdst->data.name[PARTITION_NAME_MAX] = '\0'; |
| 179 | pdst->data.base = htobe32(psrc->data.base); |
| 180 | pdst->data.size = htobe32(psrc->data.size); |
| 181 | pdst->data.pid = htobe32(psrc->data.pid); |
| 182 | pdst->data.id = htobe32(psrc->data.id); |
| 183 | pdst->data.type = htobe32(psrc->data.type); |
| 184 | pdst->data.flags = htobe32(psrc->data.flags); |
| 185 | pdst->data.actual = htobe32(psrc->data.actual); |
| 186 | for (size_t j = 0; j < PARTITION_USER_WORDS; ++j) |
| 187 | { |
| 188 | pdst->data.user.data[j] = htobe32(psrc->data.user.data[j]); |
| 189 | } |
Deepak Kodihalli | b9cdcd2 | 2017-07-12 08:14:48 -0500 | [diff] [blame] | 190 | pdst->checksum = details::checksum(pdst->data); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | return out; |
| 194 | } |
| 195 | |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 196 | static inline void writeSizes(pnor_partition& part, size_t start, size_t end, |
| 197 | size_t blockSize) |
| 198 | { |
| 199 | size_t size = end - start; |
| 200 | part.data.base = align_up(start, blockSize) / blockSize; |
| 201 | size_t sizeInBlocks = align_up(size, blockSize) / blockSize; |
| 202 | part.data.size = sizeInBlocks; |
| 203 | |
| 204 | // If a a patch partition file exists, populate actual size with its file |
| 205 | // size if it is smaller than the total size. |
| 206 | fs::path patchFile(PARTITION_FILES_PATCH_LOC); |
| 207 | patchFile /= part.data.name; |
| 208 | if (fs::is_regular_file(patchFile)) |
| 209 | { |
| 210 | part.data.actual = |
| 211 | std::min(size, static_cast<size_t>(fs::file_size(patchFile))); |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | part.data.actual = size; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | static inline void writeUserdata(pnor_partition& part, uint32_t version, |
| 220 | const std::string& data) |
| 221 | { |
| 222 | std::istringstream stream(data); |
| 223 | std::string flag{}; |
| 224 | auto perms = 0; |
| 225 | |
| 226 | while (std::getline(stream, flag, ',')) |
| 227 | { |
| 228 | if (flag == "ECC") |
| 229 | { |
| 230 | part.data.user.data[0] = PARTITION_ECC_PROTECTED; |
| 231 | } |
| 232 | else if (flag == "READONLY") |
| 233 | { |
| 234 | perms |= PARTITION_READONLY; |
| 235 | } |
| 236 | else if (flag == "PRESERVED") |
| 237 | { |
| 238 | perms |= PARTITION_PRESERVED; |
| 239 | } |
| 240 | else if (flag == "REPROVISION") |
| 241 | { |
| 242 | perms |= PARTITION_REPROVISION; |
| 243 | } |
| 244 | else if (flag == "VOLATILE") |
| 245 | { |
| 246 | perms |= PARTITION_VOLATILE; |
| 247 | } |
| 248 | else if (flag == "CLEARECC") |
| 249 | { |
| 250 | perms |= PARTITION_CLEARECC; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | part.data.user.data[1] = perms; |
| 255 | |
| 256 | part.data.user.data[1] |= version; |
| 257 | } |
| 258 | |
| 259 | static inline void writeDefaults(pnor_partition& part) |
| 260 | { |
| 261 | part.data.pid = PARENT_PATITION_ID; |
| 262 | part.data.type = PARTITION_TYPE_DATA; |
| 263 | part.data.flags = 0; // flags unused |
| 264 | } |
| 265 | |
| 266 | static inline void writeNameAndId(pnor_partition& part, std::string&& name, |
| 267 | const std::string& id) |
| 268 | { |
| 269 | name.resize(PARTITION_NAME_MAX); |
| 270 | memcpy(part.data.name, name.c_str(), sizeof(part.data.name)); |
| 271 | part.data.id = std::stoul(id); |
| 272 | } |
| 273 | |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 274 | void parseTocLine(const std::string& line, size_t blockSize, |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 275 | pnor_partition& part) |
| 276 | { |
| 277 | static constexpr auto ID_MATCH = 1; |
| 278 | static constexpr auto NAME_MATCH = 2; |
| 279 | static constexpr auto START_ADDR_MATCH = 4; |
| 280 | static constexpr auto END_ADDR_MATCH = 6; |
| 281 | static constexpr auto VERSION_MATCH = 8; |
| 282 | constexpr auto versionShift = 24; |
| 283 | |
| 284 | // Parse PNOR toc (table of contents) file, which has lines like : |
| 285 | // partition01=HBB,0x00010000,0x000a0000,0x80,ECC,PRESERVED, to indicate |
| 286 | // partition information |
| 287 | std::regex regex{ |
| 288 | "^partition([0-9]+)=([A-Za-z0-9_]+)," |
| 289 | "(0x)?([0-9a-fA-F]+),(0x)?([0-9a-fA-F]+),(0x)?([A-Fa-f0-9]{2})", |
| 290 | std::regex::extended}; |
| 291 | |
| 292 | std::smatch match; |
| 293 | if (!std::regex_search(line, match, regex)) |
| 294 | { |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 295 | std::stringstream err; |
| 296 | err << "Malformed partition description: " << line.c_str() << "\n"; |
| 297 | throw MalformedTocEntry(err.str()); |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | writeNameAndId(part, match[NAME_MATCH].str(), match[ID_MATCH].str()); |
| 301 | writeDefaults(part); |
| 302 | |
| 303 | unsigned long start = |
| 304 | std::stoul(match[START_ADDR_MATCH].str(), nullptr, 16); |
Andrew Jeffery | b87aa32 | 2018-02-27 16:27:02 +1030 | [diff] [blame] | 305 | if (start & (blockSize - 1)) |
| 306 | { |
| 307 | MSG_ERR("Start offset 0x%lx for partition '%s' is not aligned to block " |
| 308 | "size 0x%zx\n", |
| 309 | start, match[NAME_MATCH].str().c_str(), blockSize); |
| 310 | } |
| 311 | |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 312 | unsigned long end = std::stoul(match[END_ADDR_MATCH].str(), nullptr, 16); |
Andrew Jeffery | b87aa32 | 2018-02-27 16:27:02 +1030 | [diff] [blame] | 313 | if ((end - start) & (blockSize - 1)) |
| 314 | { |
| 315 | MSG_ERR("Partition '%s' has a size 0x%lx that is not aligned to block " |
| 316 | "size 0x%zx\n", |
| 317 | match[NAME_MATCH].str().c_str(), (end - start), blockSize); |
| 318 | } |
| 319 | |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 320 | writeSizes(part, start, end, blockSize); |
| 321 | |
| 322 | // Use the shift to convert "80" to 0x80000000 |
| 323 | unsigned long version = std::stoul(match[VERSION_MATCH].str(), nullptr, 16); |
| 324 | writeUserdata(part, version << versionShift, match.suffix().str()); |
| 325 | part.checksum = details::checksum(part.data); |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 326 | } |
| 327 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 328 | } // namespace virtual_pnor |
| 329 | } // namespace openpower |