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