blob: 064a2d988950ba294cb1feae6831263a3aed11bf [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
Andrew Jeffery48595402018-02-21 16:16:14 +103077static inline void writeSizes(pnor_partition& part, size_t start, size_t end,
78 size_t blockSize)
Deepak Kodihalli393821d2017-04-28 04:44:38 -050079{
80 size_t size = end - start;
Adriana Kobylak2ad21322017-11-28 14:59:19 -060081 part.data.base = align_up(start, blockSize) / blockSize;
Deepak Kodihallid1d79302017-07-11 23:01:39 -050082 size_t sizeInBlocks = align_up(size, blockSize) / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050083 part.data.size = sizeInBlocks;
Adriana Kobylakeb083552017-08-04 14:03:32 -050084
85 // If a a patch partition file exists, populate actual size with its file
86 // size if it is smaller than the total size.
87 fs::path patchFile(PARTITION_FILES_PATCH_LOC);
88 patchFile /= part.data.name;
89 if (fs::is_regular_file(patchFile))
90 {
Andrew Jefferyf34db312018-03-09 15:27:03 +103091 part.data.actual =
92 std::min(size, static_cast<size_t>(fs::file_size(patchFile)));
Adriana Kobylakeb083552017-08-04 14:03:32 -050093 }
94 else
95 {
96 part.data.actual = size;
97 }
Deepak Kodihalli393821d2017-04-28 04:44:38 -050098}
99
Andrew Jefferya76199b2018-02-21 16:17:20 +1030100static inline void writeUserdata(pnor_partition& part, uint32_t version,
Deepak Kodihallie8b0e8a2017-07-12 10:01:31 -0500101 const std::string& data)
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500102{
Adriana Kobylakacb32ef2017-12-05 16:12:06 -0600103 std::istringstream stream(data);
Andrew Jefferyf34db312018-03-09 15:27:03 +1030104 std::string flag{};
Adriana Kobylakacb32ef2017-12-05 16:12:06 -0600105 auto perms = 0;
106
107 while (std::getline(stream, flag, ','))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500108 {
Adriana Kobylakacb32ef2017-12-05 16:12:06 -0600109 if (flag == "ECC")
110 {
111 part.data.user.data[0] = PARTITION_ECC_PROTECTED;
112 }
113 else if (flag == "READONLY")
114 {
115 perms |= PARTITION_READONLY;
116 }
117 else if (flag == "PRESERVED")
118 {
119 perms |= PARTITION_PRESERVED;
120 }
121 else if (flag == "REPROVISION")
122 {
123 perms |= PARTITION_REPROVISION;
124 }
125 else if (flag == "VOLATILE")
126 {
127 perms |= PARTITION_VOLATILE;
128 }
129 else if (flag == "CLEARECC")
130 {
131 perms |= PARTITION_CLEARECC;
132 }
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500133 }
Deepak Kodihallie8b0e8a2017-07-12 10:01:31 -0500134
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500135 part.data.user.data[1] = perms;
Deepak Kodihallie8b0e8a2017-07-12 10:01:31 -0500136
137 part.data.user.data[1] |= version;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500138}
139
Andrew Jeffery4cc2e8f2018-02-21 16:16:14 +1030140static inline void writeDefaults(pnor_partition& part)
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500141{
142 part.data.pid = PARENT_PATITION_ID;
143 part.data.type = PARTITION_TYPE_DATA;
144 part.data.flags = 0; // flags unused
145}
146
Andrew Jeffery54e91222018-02-21 16:14:44 +1030147static inline void writeNameAndId(pnor_partition& part, std::string&& name,
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500148 const std::string& id)
149{
150 name.resize(PARTITION_NAME_MAX);
Andrew Jefferyf34db312018-03-09 15:27:03 +1030151 memcpy(part.data.name, name.c_str(), sizeof(part.data.name));
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500152 part.data.id = std::stoul(id);
153}
154
Andrew Jefferyfaaa71c2018-02-21 16:29:48 +1030155bool Table::parseTocLine(const std::string& line, 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 writeNameAndId(part, match[NAME_MATCH].str(), match[ID_MATCH].str());
179 writeDefaults(part);
180
181 unsigned long start =
182 std::stoul(match[START_ADDR_MATCH].str(), nullptr, 16);
183 unsigned long end = std::stoul(match[END_ADDR_MATCH].str(), nullptr, 16);
Andrew Jeffery48595402018-02-21 16:16:14 +1030184 writeSizes(part, start, end, blockSize);
Andrew Jeffery62de1aa2018-02-21 16:01:42 +1030185
186 // Use the shift to convert "80" to 0x80000000
187 unsigned long version = std::stoul(match[VERSION_MATCH].str(), nullptr, 16);
188 writeUserdata(part, version << versionShift, match.suffix().str());
189 part.checksum = details::checksum(part.data);
190
Andrew Jeffery581a4f22018-02-21 15:29:38 +1030191 return true;
192}
193
194void Table::preparePartitions()
195{
196 fs::path tocFile = directory;
197 tocFile /= PARTITION_TOC_FILE;
198 allocateMemory(tocFile);
199
200 std::ifstream file(tocFile.c_str());
201 std::string line;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500202 decltype(auto) table = getNativeTable();
203
204 while (std::getline(file, line))
205 {
Andrew Jefferyfaaa71c2018-02-21 16:29:48 +1030206 pnor_partition& part = table.partitions[numParts];
207 fs::path file;
208
209 if (!parseTocLine(line, part))
210 {
211 continue;
212 }
213
214 file = directory;
215 file /= part.data.name;
216 if (fs::exists(file))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500217 {
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500218 ++numParts;
219 }
Andrew Jefferyfaaa71c2018-02-21 16:29:48 +1030220 else
221 {
222 MSG_ERR("Partition file %s does not exist", file.c_str());
223 }
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500224 }
225}
226
227const pnor_partition& Table::partition(size_t offset) const
228{
229 const decltype(auto) table = getNativeTable();
Deepak Kodihallid1d79302017-07-11 23:01:39 -0500230 size_t offt = offset / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500231
232 for (decltype(numParts) i{}; i < numParts; ++i)
233 {
234 if ((offt >= table.partitions[i].data.base) &&
Andrew Jefferyf34db312018-03-09 15:27:03 +1030235 (offt <
236 (table.partitions[i].data.base + table.partitions[i].data.size)))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500237 {
238 return table.partitions[i];
239 }
240 }
241
Andrew Jefferye5707402018-02-02 14:39:36 +1030242 MSG_ERR("Partition corresponding to offset %zu not found", offset);
Jayanth Othayoth59ce0992017-10-27 00:45:53 -0500243 elog<InternalFailure>();
244
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500245 static pnor_partition p{};
246 return p;
247}
248
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500249const pnor_partition& Table::partition(const std::string& name) const
250{
251 const decltype(auto) table = getNativeTable();
252
253 for (decltype(numParts) i{}; i < numParts; ++i)
254 {
255 if (name == table.partitions[i].data.name)
256 {
257 return table.partitions[i];
258 }
259 }
260
261 MSG_ERR("Partition %s not found", name.c_str());
Marri Devender Rao08b0a892017-11-08 03:38:27 -0600262 log<level::ERR>("Table::partition partition not found ",
Andrew Jefferyf34db312018-03-09 15:27:03 +1030263 entry("PARTITION_NAME=%s", name.c_str()));
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500264 elog<InternalFailure>();
265 static pnor_partition p{};
266 return p;
267}
268
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500269} // namespace partition
270
271PartitionTable endianFixup(const PartitionTable& in)
272{
273 PartitionTable out;
274 out.resize(in.size());
275 auto src = reinterpret_cast<const pnor_partition_table*>(in.data());
276 auto dst = reinterpret_cast<pnor_partition_table*>(out.data());
277
278 dst->data.magic = htobe32(src->data.magic);
279 dst->data.version = htobe32(src->data.version);
280 dst->data.size = htobe32(src->data.size);
281 dst->data.entry_size = htobe32(src->data.entry_size);
282 dst->data.entry_count = htobe32(src->data.entry_count);
283 dst->data.block_size = htobe32(src->data.block_size);
284 dst->data.block_count = htobe32(src->data.block_count);
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500285 dst->checksum = details::checksum(dst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500286
287 for (decltype(src->data.entry_count) i{}; i < src->data.entry_count; ++i)
288 {
289 auto psrc = &src->partitions[i];
290 auto pdst = &dst->partitions[i];
291 strncpy(pdst->data.name, psrc->data.name, PARTITION_NAME_MAX);
292 // Just to be safe
293 pdst->data.name[PARTITION_NAME_MAX] = '\0';
294 pdst->data.base = htobe32(psrc->data.base);
295 pdst->data.size = htobe32(psrc->data.size);
296 pdst->data.pid = htobe32(psrc->data.pid);
297 pdst->data.id = htobe32(psrc->data.id);
298 pdst->data.type = htobe32(psrc->data.type);
299 pdst->data.flags = htobe32(psrc->data.flags);
300 pdst->data.actual = htobe32(psrc->data.actual);
301 for (size_t j = 0; j < PARTITION_USER_WORDS; ++j)
302 {
303 pdst->data.user.data[j] = htobe32(psrc->data.user.data[j]);
304 }
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500305 pdst->checksum = details::checksum(pdst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500306 }
307
308 return out;
309}
310
311} // namespace virtual_pnor
312} // namespace openpower