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