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