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