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