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" |
| 4 | #include <syslog.h> |
| 5 | #include <endian.h> |
| 6 | #include <regex> |
| 7 | #include <fstream> |
| 8 | #include <algorithm> |
| 9 | |
| 10 | namespace openpower |
| 11 | { |
| 12 | namespace virtual_pnor |
| 13 | { |
| 14 | |
| 15 | namespace partition |
| 16 | { |
| 17 | namespace block |
| 18 | { |
| 19 | |
| 20 | // The PNOR erase-block size is 4 KB |
| 21 | constexpr size_t size = 4096; |
| 22 | |
| 23 | } // namespace block |
| 24 | |
| 25 | Table::Table(): |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 26 | Table(fs::path(PARTITION_FILES_RO_LOC)) |
Deepak Kodihalli | 393821d | 2017-04-28 04:44:38 -0500 | [diff] [blame] | 27 | { |
| 28 | } |
| 29 | |
| 30 | Table::Table(fs::path&& directory): |
| 31 | szBlocks(0), |
| 32 | imgBlocks(0), |
| 33 | directory(std::move(directory)), |
| 34 | numParts(0) |
| 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; |
| 49 | table.data.block_size = block::size; |
| 50 | table.data.block_count = imgBlocks; |
| 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)); |
| 76 | size_t totalSizeAligned = align_up(totalSizeBytes, block::size); |
| 77 | szBlocks = totalSizeAligned / block::size; |
| 78 | imgBlocks = szBlocks; |
| 79 | tbl.resize(totalSizeAligned); |
| 80 | } |
| 81 | |
| 82 | inline void Table::writeSizes(pnor_partition& part, size_t start, size_t end) |
| 83 | { |
| 84 | size_t size = end - start; |
| 85 | part.data.base = imgBlocks; |
| 86 | size_t sizeInBlocks = align_up(size, block::size) / block::size; |
| 87 | imgBlocks += sizeInBlocks; |
| 88 | part.data.size = sizeInBlocks; |
| 89 | part.data.actual = size; |
| 90 | } |
| 91 | |
| 92 | inline void Table::writeUserdata(pnor_partition& part, const std::string& data) |
| 93 | { |
| 94 | if (std::string::npos != data.find("ECC")) |
| 95 | { |
| 96 | part.data.user.data[0] = PARTITION_ECC_PROTECTED; |
| 97 | } |
| 98 | auto perms = 0; |
| 99 | if (std::string::npos != data.find("READONLY")) |
| 100 | { |
| 101 | perms |= PARTITION_READONLY; |
| 102 | } |
| 103 | if (std::string::npos != data.find("PRESERVED")) |
| 104 | { |
| 105 | perms |= PARTITION_PRESERVED; |
| 106 | } |
| 107 | part.data.user.data[1] = perms; |
| 108 | } |
| 109 | |
| 110 | inline void Table::writeDefaults(pnor_partition& part) |
| 111 | { |
| 112 | part.data.pid = PARENT_PATITION_ID; |
| 113 | part.data.type = PARTITION_TYPE_DATA; |
| 114 | part.data.flags = 0; // flags unused |
| 115 | } |
| 116 | |
| 117 | inline void Table::writeNameAndId(pnor_partition& part, std::string&& name, |
| 118 | const std::string& id) |
| 119 | { |
| 120 | name.resize(PARTITION_NAME_MAX); |
| 121 | memcpy(part.data.name, |
| 122 | name.c_str(), |
| 123 | sizeof(part.data.name)); |
| 124 | part.data.id = std::stoul(id); |
| 125 | } |
| 126 | |
| 127 | void Table::preparePartitions() |
| 128 | { |
| 129 | fs::path tocFile = directory; |
| 130 | tocFile /= PARTITION_TOC_FILE; |
| 131 | allocateMemory(tocFile); |
| 132 | |
| 133 | std::ifstream file(tocFile.c_str()); |
| 134 | static constexpr auto ID_MATCH = 1; |
| 135 | static constexpr auto NAME_MATCH = 2; |
| 136 | static constexpr auto START_ADDR_MATCH = 3; |
| 137 | static constexpr auto END_ADDR_MATCH = 4; |
| 138 | // Parse PNOR toc (table of contents) file, which has lines like : |
| 139 | // partition01=HBB,00010000,000a0000,ECC,PRESERVED, to indicate partitions |
| 140 | std::regex regex |
| 141 | { |
| 142 | "^partition([0-9]+)=([A-Za-z0-9_]+)," |
| 143 | "([0-9a-fA-F]+),([0-9a-fA-F]+)", |
| 144 | std::regex::extended |
| 145 | }; |
| 146 | std::smatch match; |
| 147 | std::string line; |
| 148 | |
| 149 | decltype(auto) table = getNativeTable(); |
| 150 | |
| 151 | while (std::getline(file, line)) |
| 152 | { |
| 153 | if (std::regex_search(line, match, regex)) |
| 154 | { |
| 155 | fs::path partitionFile = directory; |
| 156 | partitionFile /= match[NAME_MATCH].str(); |
| 157 | if (!fs::exists(partitionFile)) |
| 158 | { |
| 159 | MSG_ERR("Partition file %s does not exist", |
| 160 | partitionFile.c_str()); |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | writeNameAndId(table.partitions[numParts], |
| 165 | match[NAME_MATCH].str(), |
| 166 | match[ID_MATCH].str()); |
| 167 | writeDefaults(table.partitions[numParts]); |
| 168 | writeSizes(table.partitions[numParts], |
| 169 | std::stoul(match[START_ADDR_MATCH].str(), nullptr, 16), |
| 170 | std::stoul(match[END_ADDR_MATCH].str(), nullptr, 16)); |
| 171 | writeUserdata(table.partitions[numParts], match.suffix().str()); |
| 172 | table.partitions[numParts].checksum = |
| 173 | details::checksum(table.partitions[numParts].data); |
| 174 | |
| 175 | ++numParts; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | const pnor_partition& Table::partition(size_t offset) const |
| 181 | { |
| 182 | const decltype(auto) table = getNativeTable(); |
| 183 | size_t offt = offset / block::size; |
| 184 | |
| 185 | for (decltype(numParts) i{}; i < numParts; ++i) |
| 186 | { |
| 187 | if ((offt >= table.partitions[i].data.base) && |
| 188 | (offt < (table.partitions[i].data.base + |
| 189 | table.partitions[i].data.size))) |
| 190 | { |
| 191 | return table.partitions[i]; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | static pnor_partition p{}; |
| 196 | return p; |
| 197 | } |
| 198 | |
| 199 | } // namespace partition |
| 200 | |
| 201 | PartitionTable endianFixup(const PartitionTable& in) |
| 202 | { |
| 203 | PartitionTable out; |
| 204 | out.resize(in.size()); |
| 205 | auto src = reinterpret_cast<const pnor_partition_table*>(in.data()); |
| 206 | auto dst = reinterpret_cast<pnor_partition_table*>(out.data()); |
| 207 | |
| 208 | dst->data.magic = htobe32(src->data.magic); |
| 209 | dst->data.version = htobe32(src->data.version); |
| 210 | dst->data.size = htobe32(src->data.size); |
| 211 | dst->data.entry_size = htobe32(src->data.entry_size); |
| 212 | dst->data.entry_count = htobe32(src->data.entry_count); |
| 213 | dst->data.block_size = htobe32(src->data.block_size); |
| 214 | dst->data.block_count = htobe32(src->data.block_count); |
| 215 | dst->checksum = htobe32(src->checksum); |
| 216 | |
| 217 | for (decltype(src->data.entry_count) i{}; i < src->data.entry_count; ++i) |
| 218 | { |
| 219 | auto psrc = &src->partitions[i]; |
| 220 | auto pdst = &dst->partitions[i]; |
| 221 | strncpy(pdst->data.name, psrc->data.name, PARTITION_NAME_MAX); |
| 222 | // Just to be safe |
| 223 | pdst->data.name[PARTITION_NAME_MAX] = '\0'; |
| 224 | pdst->data.base = htobe32(psrc->data.base); |
| 225 | pdst->data.size = htobe32(psrc->data.size); |
| 226 | pdst->data.pid = htobe32(psrc->data.pid); |
| 227 | pdst->data.id = htobe32(psrc->data.id); |
| 228 | pdst->data.type = htobe32(psrc->data.type); |
| 229 | pdst->data.flags = htobe32(psrc->data.flags); |
| 230 | pdst->data.actual = htobe32(psrc->data.actual); |
| 231 | for (size_t j = 0; j < PARTITION_USER_WORDS; ++j) |
| 232 | { |
| 233 | pdst->data.user.data[j] = htobe32(psrc->data.user.data[j]); |
| 234 | } |
| 235 | pdst->checksum = htobe32(psrc->checksum); |
| 236 | } |
| 237 | |
| 238 | return out; |
| 239 | } |
| 240 | |
| 241 | } // namespace virtual_pnor |
| 242 | } // namespace openpower |