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 "config.h" |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 4 | |
| 5 | #include "pnor_partition_table.hpp" |
| 6 | |
Deepak Kodihalli | 8a89969 | 2017-07-11 23:17:19 -0500 | [diff] [blame] | 7 | #include "xyz/openbmc_project/Common/error.hpp" |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 8 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 9 | #include <endian.h> |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 10 | #include <syslog.h> |
| 11 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 12 | #include <algorithm> |
William A. Kennington III | d5f1d40 | 2018-10-11 13:55:04 -0700 | [diff] [blame] | 13 | #include <fstream> |
| 14 | #include <phosphor-logging/elog-errors.hpp> |
| 15 | #include <regex> |
| 16 | |
| 17 | #include "common.h" |
| 18 | #include "mboxd.h" |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 19 | |
| 20 | namespace openpower |
| 21 | { |
| 22 | namespace virtual_pnor |
| 23 | { |
| 24 | |
Deepak Kodihalli | 8a89969 | 2017-07-11 23:17:19 -0500 | [diff] [blame] | 25 | using namespace phosphor::logging; |
| 26 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 27 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 28 | namespace partition |
| 29 | { |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 30 | |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 31 | Table::Table(const struct mbox_context* ctx) : |
| 32 | szBytes(sizeof(pnor_partition_table)), numParts(0), |
| 33 | blockSize(1 << ctx->erase_size_shift), pnorSize(ctx->flash_size) |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 34 | { |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 35 | preparePartitions(ctx); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 36 | prepareHeader(); |
| 37 | hostTbl = endianFixup(tbl); |
| 38 | } |
| 39 | |
| 40 | void Table::prepareHeader() |
| 41 | { |
| 42 | decltype(auto) table = getNativeTable(); |
| 43 | table.data.magic = PARTITION_HEADER_MAGIC; |
| 44 | table.data.version = PARTITION_VERSION_1; |
Andrew Jeffery | 7f9c343 | 2018-03-01 12:07:13 +1030 | [diff] [blame] | 45 | table.data.size = blocks(); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 46 | table.data.entry_size = sizeof(pnor_partition); |
| 47 | table.data.entry_count = numParts; |
Deepak Kodihalli | d1d7930 | 2017-07-11 23:01:39 -0500 | [diff] [blame] | 48 | table.data.block_size = blockSize; |
| 49 | table.data.block_count = pnorSize / blockSize; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 50 | table.checksum = details::checksum(table.data); |
| 51 | } |
| 52 | |
| 53 | inline void Table::allocateMemory(const fs::path& tocFile) |
| 54 | { |
| 55 | size_t num = 0; |
| 56 | std::string line; |
| 57 | std::ifstream file(tocFile.c_str()); |
| 58 | |
| 59 | // Find number of lines in partition file - this will help |
| 60 | // determine the number of partitions and hence also how much |
| 61 | // memory to allocate for the partitions array. |
| 62 | // The actual number of partitions may turn out to be lesser than this, |
| 63 | // in case of errors. |
| 64 | while (std::getline(file, line)) |
| 65 | { |
| 66 | // Check if line starts with "partition" |
| 67 | if (std::string::npos != line.find("partition", 0)) |
| 68 | { |
| 69 | ++num; |
| 70 | } |
| 71 | } |
| 72 | |
Andrew Jeffery | 7f9c343 | 2018-03-01 12:07:13 +1030 | [diff] [blame] | 73 | szBytes = sizeof(pnor_partition_table) + (num * sizeof(pnor_partition)); |
| 74 | tbl.resize(capacity()); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 77 | void Table::preparePartitions(const struct mbox_context* ctx) |
Andrew Jeffery | 581a4f2 | 2018-02-21 15:29:38 +1030 | [diff] [blame] | 78 | { |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 79 | const fs::path roDir = ctx->paths.ro_loc; |
| 80 | const fs::path patchDir = ctx->paths.patch_loc; |
| 81 | fs::path tocFile = roDir / PARTITION_TOC_FILE; |
Andrew Jeffery | 581a4f2 | 2018-02-21 15:29:38 +1030 | [diff] [blame] | 82 | allocateMemory(tocFile); |
| 83 | |
| 84 | std::ifstream file(tocFile.c_str()); |
| 85 | std::string line; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 86 | decltype(auto) table = getNativeTable(); |
| 87 | |
| 88 | while (std::getline(file, line)) |
| 89 | { |
Andrew Jeffery | faaa71c | 2018-02-21 16:29:48 +1030 | [diff] [blame] | 90 | pnor_partition& part = table.partitions[numParts]; |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 91 | fs::path patch; |
Andrew Jeffery | faaa71c | 2018-02-21 16:29:48 +1030 | [diff] [blame] | 92 | fs::path file; |
| 93 | |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 94 | // The ToC file presented in the vpnor squashfs looks like: |
| 95 | // |
| 96 | // version=IBM-witherspoon-ibm-OP9_v1.19_1.135 |
| 97 | // 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 |
| 98 | // partition00=part,0x00000000,0x00002000,00,READWRITE |
| 99 | // partition01=HBEL,0x00008000,0x0002c000,00,ECC,REPROVISION,CLEARECC,READWRITE |
| 100 | // ... |
| 101 | // |
| 102 | // As such we want to skip any lines that don't begin with 'partition' |
| 103 | if (std::string::npos == line.find("partition", 0)) |
Andrew Jeffery | faaa71c | 2018-02-21 16:29:48 +1030 | [diff] [blame] | 104 | { |
| 105 | continue; |
| 106 | } |
| 107 | |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 108 | parseTocLine(line, blockSize, part); |
| 109 | |
Andrew Jeffery | 4ca1aa5 | 2018-03-02 12:08:29 +1030 | [diff] [blame] | 110 | if (numParts > 0) |
| 111 | { |
| 112 | struct pnor_partition& prev = table.partitions[numParts - 1]; |
| 113 | uint32_t prev_end = prev.data.base + prev.data.size; |
| 114 | |
| 115 | if (part.data.id == prev.data.id) |
| 116 | { |
| 117 | MSG_ERR("ID for previous partition '%s' at block 0x%" PRIx32 |
| 118 | "matches current partition '%s' at block 0x%" PRIx32 |
| 119 | ": %" PRId32 "\n", |
| 120 | prev.data.name, prev.data.base, part.data.name, |
| 121 | part.data.base, part.data.id); |
| 122 | } |
| 123 | |
| 124 | if (part.data.base < prev_end) |
| 125 | { |
| 126 | std::stringstream err; |
| 127 | err << "Partition '" << part.data.name << "' start block 0x" |
| 128 | << std::hex << part.data.base << "is less than the end " |
| 129 | << "block 0x" << std::hex << prev_end << " of '" |
| 130 | << prev.data.name << "'"; |
| 131 | throw InvalidTocEntry(err.str()); |
| 132 | } |
| 133 | } |
| 134 | |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 135 | file = roDir / part.data.name; |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 136 | if (!fs::exists(file)) |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 137 | { |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 138 | std::stringstream err; |
| 139 | err << "Partition file " << file.native() << " does not exist"; |
| 140 | throw InvalidTocEntry(err.str()); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 141 | } |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 142 | |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 143 | patch = patchDir / part.data.name; |
| 144 | if (fs::is_regular_file(patch)) |
| 145 | { |
| 146 | const size_t size = part.data.size * blockSize; |
| 147 | part.data.actual = |
| 148 | std::min(size, static_cast<size_t>(fs::file_size(patch))); |
| 149 | } |
| 150 | |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 151 | ++numParts; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | const pnor_partition& Table::partition(size_t offset) const |
| 156 | { |
| 157 | const decltype(auto) table = getNativeTable(); |
Andrew Jeffery | bd38c21 | 2018-02-27 17:44:33 +1030 | [diff] [blame] | 158 | size_t blockOffset = offset / blockSize; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 159 | |
| 160 | for (decltype(numParts) i{}; i < numParts; ++i) |
| 161 | { |
Andrew Jeffery | cd92851 | 2018-02-28 22:33:48 +1030 | [diff] [blame] | 162 | const struct pnor_partition& part = table.partitions[i]; |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 163 | size_t len = part.data.size; |
| 164 | |
Andrew Jeffery | cd92851 | 2018-02-28 22:33:48 +1030 | [diff] [blame] | 165 | if ((blockOffset >= part.data.base) && |
Andrew Jeffery | 1a3f843 | 2018-03-02 10:18:02 +1030 | [diff] [blame] | 166 | (blockOffset < (part.data.base + len))) |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 167 | { |
Andrew Jeffery | cd92851 | 2018-02-28 22:33:48 +1030 | [diff] [blame] | 168 | return part; |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 169 | } |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame] | 170 | |
| 171 | /* Are we in a hole between partitions? */ |
| 172 | if (blockOffset < part.data.base) |
| 173 | { |
| 174 | throw UnmappedOffset(offset, part.data.base * blockSize); |
| 175 | } |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 176 | } |
| 177 | |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame] | 178 | throw UnmappedOffset(offset, pnorSize); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Deepak Kodihalli | 8a89969 | 2017-07-11 23:17:19 -0500 | [diff] [blame] | 181 | const pnor_partition& Table::partition(const std::string& name) const |
| 182 | { |
| 183 | const decltype(auto) table = getNativeTable(); |
| 184 | |
| 185 | for (decltype(numParts) i{}; i < numParts; ++i) |
| 186 | { |
| 187 | if (name == table.partitions[i].data.name) |
| 188 | { |
| 189 | return table.partitions[i]; |
| 190 | } |
| 191 | } |
| 192 | |
Andrew Jeffery | ae1edb9 | 2018-02-28 23:16:48 +1030 | [diff] [blame] | 193 | std::stringstream err; |
| 194 | err << "Partition " << name << " is not listed in the table of contents"; |
| 195 | throw UnknownPartition(err.str()); |
Deepak Kodihalli | 8a89969 | 2017-07-11 23:17:19 -0500 | [diff] [blame] | 196 | } |
| 197 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 198 | } // namespace partition |
| 199 | |
| 200 | PartitionTable endianFixup(const PartitionTable& in) |
| 201 | { |
| 202 | PartitionTable out; |
| 203 | out.resize(in.size()); |
| 204 | auto src = reinterpret_cast<const pnor_partition_table*>(in.data()); |
| 205 | auto dst = reinterpret_cast<pnor_partition_table*>(out.data()); |
| 206 | |
| 207 | dst->data.magic = htobe32(src->data.magic); |
| 208 | dst->data.version = htobe32(src->data.version); |
| 209 | dst->data.size = htobe32(src->data.size); |
| 210 | dst->data.entry_size = htobe32(src->data.entry_size); |
| 211 | dst->data.entry_count = htobe32(src->data.entry_count); |
| 212 | dst->data.block_size = htobe32(src->data.block_size); |
| 213 | dst->data.block_count = htobe32(src->data.block_count); |
Deepak Kodihalli | b9cdcd2 | 2017-07-12 08:14:48 -0500 | [diff] [blame] | 214 | dst->checksum = details::checksum(dst->data); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 215 | |
| 216 | for (decltype(src->data.entry_count) i{}; i < src->data.entry_count; ++i) |
| 217 | { |
| 218 | auto psrc = &src->partitions[i]; |
| 219 | auto pdst = &dst->partitions[i]; |
| 220 | strncpy(pdst->data.name, psrc->data.name, PARTITION_NAME_MAX); |
| 221 | // Just to be safe |
| 222 | pdst->data.name[PARTITION_NAME_MAX] = '\0'; |
| 223 | pdst->data.base = htobe32(psrc->data.base); |
| 224 | pdst->data.size = htobe32(psrc->data.size); |
| 225 | pdst->data.pid = htobe32(psrc->data.pid); |
| 226 | pdst->data.id = htobe32(psrc->data.id); |
| 227 | pdst->data.type = htobe32(psrc->data.type); |
| 228 | pdst->data.flags = htobe32(psrc->data.flags); |
| 229 | pdst->data.actual = htobe32(psrc->data.actual); |
| 230 | for (size_t j = 0; j < PARTITION_USER_WORDS; ++j) |
| 231 | { |
| 232 | pdst->data.user.data[j] = htobe32(psrc->data.user.data[j]); |
| 233 | } |
Deepak Kodihalli | b9cdcd2 | 2017-07-12 08:14:48 -0500 | [diff] [blame] | 234 | pdst->checksum = details::checksum(pdst->data); |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | return out; |
| 238 | } |
| 239 | |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 240 | static inline void writeSizes(pnor_partition& part, size_t start, size_t end, |
| 241 | size_t blockSize) |
| 242 | { |
| 243 | size_t size = end - start; |
| 244 | part.data.base = align_up(start, blockSize) / blockSize; |
| 245 | size_t sizeInBlocks = align_up(size, blockSize) / blockSize; |
| 246 | part.data.size = sizeInBlocks; |
Andrew Jeffery | 742a1f6 | 2018-03-02 09:26:03 +1030 | [diff] [blame] | 247 | part.data.actual = size; |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static inline void writeUserdata(pnor_partition& part, uint32_t version, |
| 251 | const std::string& data) |
| 252 | { |
| 253 | std::istringstream stream(data); |
| 254 | std::string flag{}; |
| 255 | auto perms = 0; |
Andrew Jeffery | 0f9fbd6 | 2018-03-02 12:00:42 +1030 | [diff] [blame] | 256 | auto state = 0; |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 257 | |
Andrew Jeffery | 0f9fbd6 | 2018-03-02 12:00:42 +1030 | [diff] [blame] | 258 | MSG_DBG("Parsing ToC flags '%s'\n", data.c_str()); |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 259 | while (std::getline(stream, flag, ',')) |
| 260 | { |
Andrew Jeffery | 0f9fbd6 | 2018-03-02 12:00:42 +1030 | [diff] [blame] | 261 | if (flag == "") |
| 262 | continue; |
| 263 | |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 264 | if (flag == "ECC") |
| 265 | { |
Andrew Jeffery | 0f9fbd6 | 2018-03-02 12:00:42 +1030 | [diff] [blame] | 266 | state |= PARTITION_ECC_PROTECTED; |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 267 | } |
| 268 | else if (flag == "READONLY") |
| 269 | { |
| 270 | perms |= PARTITION_READONLY; |
| 271 | } |
Andrew Jeffery | 0f9fbd6 | 2018-03-02 12:00:42 +1030 | [diff] [blame] | 272 | else if (flag == "READWRITE") |
| 273 | { |
| 274 | perms &= ~PARTITION_READONLY; |
| 275 | } |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 276 | else if (flag == "PRESERVED") |
| 277 | { |
| 278 | perms |= PARTITION_PRESERVED; |
| 279 | } |
| 280 | else if (flag == "REPROVISION") |
| 281 | { |
| 282 | perms |= PARTITION_REPROVISION; |
| 283 | } |
| 284 | else if (flag == "VOLATILE") |
| 285 | { |
| 286 | perms |= PARTITION_VOLATILE; |
| 287 | } |
| 288 | else if (flag == "CLEARECC") |
| 289 | { |
| 290 | perms |= PARTITION_CLEARECC; |
| 291 | } |
Andrew Jeffery | 0f9fbd6 | 2018-03-02 12:00:42 +1030 | [diff] [blame] | 292 | else |
| 293 | { |
| 294 | MSG_INFO("Found unimplemented partition property: %s\n", |
| 295 | flag.c_str()); |
| 296 | } |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 297 | } |
| 298 | |
Andrew Jeffery | 7a85d22 | 2018-11-13 17:15:18 +1030 | [diff] [blame] | 299 | // Awful hack: Detect the TOC partition and force it read-only. |
| 300 | // |
| 301 | // Note that as it stands in the caller code we populate the critical |
| 302 | // elements before the user data. These tests make doing so a requirement. |
| 303 | if (part.data.id == 0 && !part.data.base && part.data.size) |
| 304 | { |
| 305 | perms |= PARTITION_READONLY; |
| 306 | } |
| 307 | |
Andrew Jeffery | 0f9fbd6 | 2018-03-02 12:00:42 +1030 | [diff] [blame] | 308 | part.data.user.data[0] = state; |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 309 | part.data.user.data[1] = perms; |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 310 | part.data.user.data[1] |= version; |
| 311 | } |
| 312 | |
| 313 | static inline void writeDefaults(pnor_partition& part) |
| 314 | { |
| 315 | part.data.pid = PARENT_PATITION_ID; |
| 316 | part.data.type = PARTITION_TYPE_DATA; |
| 317 | part.data.flags = 0; // flags unused |
| 318 | } |
| 319 | |
| 320 | static inline void writeNameAndId(pnor_partition& part, std::string&& name, |
| 321 | const std::string& id) |
| 322 | { |
| 323 | name.resize(PARTITION_NAME_MAX); |
| 324 | memcpy(part.data.name, name.c_str(), sizeof(part.data.name)); |
| 325 | part.data.id = std::stoul(id); |
| 326 | } |
| 327 | |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 328 | void parseTocLine(const std::string& line, size_t blockSize, |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 329 | pnor_partition& part) |
| 330 | { |
| 331 | static constexpr auto ID_MATCH = 1; |
| 332 | static constexpr auto NAME_MATCH = 2; |
| 333 | static constexpr auto START_ADDR_MATCH = 4; |
| 334 | static constexpr auto END_ADDR_MATCH = 6; |
| 335 | static constexpr auto VERSION_MATCH = 8; |
| 336 | constexpr auto versionShift = 24; |
| 337 | |
| 338 | // Parse PNOR toc (table of contents) file, which has lines like : |
| 339 | // partition01=HBB,0x00010000,0x000a0000,0x80,ECC,PRESERVED, to indicate |
| 340 | // partition information |
| 341 | std::regex regex{ |
| 342 | "^partition([0-9]+)=([A-Za-z0-9_]+)," |
| 343 | "(0x)?([0-9a-fA-F]+),(0x)?([0-9a-fA-F]+),(0x)?([A-Fa-f0-9]{2})", |
| 344 | std::regex::extended}; |
| 345 | |
| 346 | std::smatch match; |
| 347 | if (!std::regex_search(line, match, regex)) |
| 348 | { |
Andrew Jeffery | f96bd16 | 2018-02-26 13:05:00 +1030 | [diff] [blame] | 349 | std::stringstream err; |
| 350 | err << "Malformed partition description: " << line.c_str() << "\n"; |
| 351 | throw MalformedTocEntry(err.str()); |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | writeNameAndId(part, match[NAME_MATCH].str(), match[ID_MATCH].str()); |
| 355 | writeDefaults(part); |
| 356 | |
| 357 | unsigned long start = |
| 358 | std::stoul(match[START_ADDR_MATCH].str(), nullptr, 16); |
Andrew Jeffery | b87aa32 | 2018-02-27 16:27:02 +1030 | [diff] [blame] | 359 | if (start & (blockSize - 1)) |
| 360 | { |
| 361 | MSG_ERR("Start offset 0x%lx for partition '%s' is not aligned to block " |
| 362 | "size 0x%zx\n", |
| 363 | start, match[NAME_MATCH].str().c_str(), blockSize); |
| 364 | } |
| 365 | |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 366 | unsigned long end = std::stoul(match[END_ADDR_MATCH].str(), nullptr, 16); |
Andrew Jeffery | b87aa32 | 2018-02-27 16:27:02 +1030 | [diff] [blame] | 367 | if ((end - start) & (blockSize - 1)) |
| 368 | { |
| 369 | MSG_ERR("Partition '%s' has a size 0x%lx that is not aligned to block " |
| 370 | "size 0x%zx\n", |
| 371 | match[NAME_MATCH].str().c_str(), (end - start), blockSize); |
| 372 | } |
| 373 | |
Andrew Jeffery | db49341 | 2018-03-02 11:54:22 +1030 | [diff] [blame] | 374 | if (start >= end) |
| 375 | { |
| 376 | std::stringstream err; |
| 377 | err << "Partition " << match[NAME_MATCH].str() |
| 378 | << " has an invalid range: start offset (0x" << std::hex << start |
| 379 | << " is beyond open end (0x" << std::hex << end << ")\n"; |
| 380 | throw InvalidTocEntry(err.str()); |
| 381 | } |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 382 | writeSizes(part, start, end, blockSize); |
| 383 | |
| 384 | // Use the shift to convert "80" to 0x80000000 |
| 385 | unsigned long version = std::stoul(match[VERSION_MATCH].str(), nullptr, 16); |
Andrew Jeffery | 7a85d22 | 2018-11-13 17:15:18 +1030 | [diff] [blame] | 386 | // Note that we must have written the partition ID and sizes prior to |
| 387 | // populating the userdata. See the note about awful hacks in |
| 388 | // writeUserdata() |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 389 | writeUserdata(part, version << versionShift, match.suffix().str()); |
| 390 | part.checksum = details::checksum(part.data); |
Andrew Jeffery | d394a78 | 2018-02-21 16:16:14 +1030 | [diff] [blame] | 391 | } |
| 392 | |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 393 | } // namespace virtual_pnor |
| 394 | } // namespace openpower |