blob: 871adbecc86fcae8572efa65773361411dd31a6b [file] [log] [blame]
Deepak Kodihalli393821d2017-04-28 04:44:38 -05001#include "pnor_partition_table.hpp"
2#include "common.h"
3#include "config.h"
Deepak Kodihalli8a899692017-07-11 23:17:19 -05004#include "xyz/openbmc_project/Common/error.hpp"
5#include <phosphor-logging/elog-errors.hpp>
Deepak Kodihalli393821d2017-04-28 04:44:38 -05006#include <syslog.h>
7#include <endian.h>
8#include <regex>
9#include <fstream>
10#include <algorithm>
11
12namespace openpower
13{
14namespace virtual_pnor
15{
16
Deepak Kodihalli8a899692017-07-11 23:17:19 -050017using namespace phosphor::logging;
18using namespace sdbusplus::xyz::openbmc_project::Common::Error;
19
Deepak Kodihalli393821d2017-04-28 04:44:38 -050020namespace partition
21{
Deepak Kodihalli393821d2017-04-28 04:44:38 -050022
Andrew Jefferyf34db312018-03-09 15:27:03 +103023Table::Table(size_t blockSize, size_t pnorSize) :
Deepak Kodihallid1d79302017-07-11 23:01:39 -050024 Table(fs::path(PARTITION_FILES_RO_LOC), blockSize, pnorSize)
Deepak Kodihalli393821d2017-04-28 04:44:38 -050025{
26}
27
Andrew Jefferyf34db312018-03-09 15:27:03 +103028Table::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 Kodihalli393821d2017-04-28 04:44:38 -050031{
32 preparePartitions();
33 prepareHeader();
34 hostTbl = endianFixup(tbl);
35}
36
37void 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 Kodihallid1d79302017-07-11 23:01:39 -050045 table.data.block_size = blockSize;
46 table.data.block_count = pnorSize / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050047 table.checksum = details::checksum(table.data);
48}
49
50inline 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 Jefferyf34db312018-03-09 15:27:03 +103070 size_t totalSizeBytes =
71 sizeof(pnor_partition_table) + (num * sizeof(pnor_partition));
Deepak Kodihallid1d79302017-07-11 23:01:39 -050072 size_t totalSizeAligned = align_up(totalSizeBytes, blockSize);
73 szBlocks = totalSizeAligned / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050074 tbl.resize(totalSizeAligned);
75}
76
77inline void Table::writeSizes(pnor_partition& part, size_t start, size_t end)
78{
79 size_t size = end - start;
Adriana Kobylak2ad21322017-11-28 14:59:19 -060080 part.data.base = align_up(start, blockSize) / blockSize;
Deepak Kodihallid1d79302017-07-11 23:01:39 -050081 size_t sizeInBlocks = align_up(size, blockSize) / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050082 part.data.size = sizeInBlocks;
Adriana Kobylakeb083552017-08-04 14:03:32 -050083
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 Jefferyf34db312018-03-09 15:27:03 +103090 part.data.actual =
91 std::min(size, static_cast<size_t>(fs::file_size(patchFile)));
Adriana Kobylakeb083552017-08-04 14:03:32 -050092 }
93 else
94 {
95 part.data.actual = size;
96 }
Deepak Kodihalli393821d2017-04-28 04:44:38 -050097}
98
Andrew Jefferyf34db312018-03-09 15:27:03 +103099inline void Table::writeUserdata(pnor_partition& part, uint32_t version,
Deepak Kodihallie8b0e8a2017-07-12 10:01:31 -0500100 const std::string& data)
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500101{
Adriana Kobylakacb32ef2017-12-05 16:12:06 -0600102 std::istringstream stream(data);
Andrew Jefferyf34db312018-03-09 15:27:03 +1030103 std::string flag{};
Adriana Kobylakacb32ef2017-12-05 16:12:06 -0600104 auto perms = 0;
105
106 while (std::getline(stream, flag, ','))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500107 {
Adriana Kobylakacb32ef2017-12-05 16:12:06 -0600108 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 Kodihalli393821d2017-04-28 04:44:38 -0500132 }
Deepak Kodihallie8b0e8a2017-07-12 10:01:31 -0500133
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500134 part.data.user.data[1] = perms;
Deepak Kodihallie8b0e8a2017-07-12 10:01:31 -0500135
136 part.data.user.data[1] |= version;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500137}
138
139inline void Table::writeDefaults(pnor_partition& part)
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 Jeffery54e91222018-02-21 16:14:44 +1030146static inline void writeNameAndId(pnor_partition& part, std::string&& name,
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500147 const std::string& id)
148{
149 name.resize(PARTITION_NAME_MAX);
Andrew Jefferyf34db312018-03-09 15:27:03 +1030150 memcpy(part.data.name, name.c_str(), sizeof(part.data.name));
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500151 part.data.id = std::stoul(id);
152}
153
Andrew Jeffery581a4f22018-02-21 15:29:38 +1030154bool Table::parseTocLine(fs::path& dir, const std::string& line,
155 pnor_partition& part)
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500156{
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500157 static constexpr auto ID_MATCH = 1;
158 static constexpr auto NAME_MATCH = 2;
Adriana Kobylak1e1bdc72017-08-16 16:30:08 -0500159 static constexpr auto START_ADDR_MATCH = 4;
160 static constexpr auto END_ADDR_MATCH = 6;
161 static constexpr auto VERSION_MATCH = 8;
Andrew Jeffery581a4f22018-02-21 15:29:38 +1030162 constexpr auto versionShift = 24;
163
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500164 // Parse PNOR toc (table of contents) file, which has lines like :
Adriana Kobylak1e1bdc72017-08-16 16:30:08 -0500165 // partition01=HBB,0x00010000,0x000a0000,0x80,ECC,PRESERVED, to indicate
Deepak Kodihallie8b0e8a2017-07-12 10:01:31 -0500166 // partition information
Andrew Jefferyf34db312018-03-09 15:27:03 +1030167 std::regex regex{
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500168 "^partition([0-9]+)=([A-Za-z0-9_]+),"
Adriana Kobylak1e1bdc72017-08-16 16:30:08 -0500169 "(0x)?([0-9a-fA-F]+),(0x)?([0-9a-fA-F]+),(0x)?([A-Fa-f0-9]{2})",
Andrew Jefferyf34db312018-03-09 15:27:03 +1030170 std::regex::extended};
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500171
Andrew Jeffery581a4f22018-02-21 15:29:38 +1030172 std::smatch match;
Andrew Jeffery62de1aa2018-02-21 16:01:42 +1030173 if (!std::regex_search(line, match, regex))
Andrew Jeffery581a4f22018-02-21 15:29:38 +1030174 {
Andrew Jeffery62de1aa2018-02-21 16:01:42 +1030175 return false;
Andrew Jeffery581a4f22018-02-21 15:29:38 +1030176 }
177
Andrew Jeffery62de1aa2018-02-21 16:01:42 +1030178 fs::path partitionFile = dir;
179 partitionFile /= match[NAME_MATCH].str();
180 if (!fs::exists(partitionFile))
181 {
182 MSG_ERR("Partition file %s does not exist", partitionFile.c_str());
183 return false;
184 }
185
186 writeNameAndId(part, match[NAME_MATCH].str(), match[ID_MATCH].str());
187 writeDefaults(part);
188
189 unsigned long start =
190 std::stoul(match[START_ADDR_MATCH].str(), nullptr, 16);
191 unsigned long end = std::stoul(match[END_ADDR_MATCH].str(), nullptr, 16);
192 writeSizes(part, start, end);
193
194 // Use the shift to convert "80" to 0x80000000
195 unsigned long version = std::stoul(match[VERSION_MATCH].str(), nullptr, 16);
196 writeUserdata(part, version << versionShift, match.suffix().str());
197 part.checksum = details::checksum(part.data);
198
Andrew Jeffery581a4f22018-02-21 15:29:38 +1030199 return true;
200}
201
202void Table::preparePartitions()
203{
204 fs::path tocFile = directory;
205 tocFile /= PARTITION_TOC_FILE;
206 allocateMemory(tocFile);
207
208 std::ifstream file(tocFile.c_str());
209 std::string line;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500210 decltype(auto) table = getNativeTable();
211
212 while (std::getline(file, line))
213 {
Andrew Jeffery581a4f22018-02-21 15:29:38 +1030214 if (parseTocLine(directory, line, table.partitions[numParts]))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500215 {
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500216 ++numParts;
217 }
218 }
219}
220
221const pnor_partition& Table::partition(size_t offset) const
222{
223 const decltype(auto) table = getNativeTable();
Deepak Kodihallid1d79302017-07-11 23:01:39 -0500224 size_t offt = offset / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500225
226 for (decltype(numParts) i{}; i < numParts; ++i)
227 {
228 if ((offt >= table.partitions[i].data.base) &&
Andrew Jefferyf34db312018-03-09 15:27:03 +1030229 (offt <
230 (table.partitions[i].data.base + table.partitions[i].data.size)))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500231 {
232 return table.partitions[i];
233 }
234 }
235
Andrew Jefferye5707402018-02-02 14:39:36 +1030236 MSG_ERR("Partition corresponding to offset %zu not found", offset);
Jayanth Othayoth59ce0992017-10-27 00:45:53 -0500237 elog<InternalFailure>();
238
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500239 static pnor_partition p{};
240 return p;
241}
242
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500243const 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 Rao08b0a892017-11-08 03:38:27 -0600256 log<level::ERR>("Table::partition partition not found ",
Andrew Jefferyf34db312018-03-09 15:27:03 +1030257 entry("PARTITION_NAME=%s", name.c_str()));
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500258 elog<InternalFailure>();
259 static pnor_partition p{};
260 return p;
261}
262
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500263} // namespace partition
264
265PartitionTable 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 Kodihallib9cdcd22017-07-12 08:14:48 -0500279 dst->checksum = details::checksum(dst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500280
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 Kodihallib9cdcd22017-07-12 08:14:48 -0500299 pdst->checksum = details::checksum(pdst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500300 }
301
302 return out;
303}
304
305} // namespace virtual_pnor
306} // namespace openpower