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