blob: eafdca5dcd1fbdfc1ac79d44f7626b747c11d087 [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
Deepak Kodihallid1d79302017-07-11 23:01:39 -050023Table::Table(size_t blockSize, size_t pnorSize):
24 Table(fs::path(PARTITION_FILES_RO_LOC), blockSize, pnorSize)
Deepak Kodihalli393821d2017-04-28 04:44:38 -050025{
26}
27
Deepak Kodihallid1d79302017-07-11 23:01:39 -050028Table::Table(fs::path&& directory,
29 size_t blockSize, size_t pnorSize):
Deepak Kodihalli393821d2017-04-28 04:44:38 -050030 szBlocks(0),
Deepak Kodihalli393821d2017-04-28 04:44:38 -050031 directory(std::move(directory)),
Deepak Kodihallid1d79302017-07-11 23:01:39 -050032 numParts(0),
33 blockSize(blockSize),
34 pnorSize(pnorSize)
Deepak Kodihalli393821d2017-04-28 04:44:38 -050035{
36 preparePartitions();
37 prepareHeader();
38 hostTbl = endianFixup(tbl);
39}
40
41void 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;
Deepak Kodihallid1d79302017-07-11 23:01:39 -050049 table.data.block_size = blockSize;
50 table.data.block_count = pnorSize / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050051 table.checksum = details::checksum(table.data);
52}
53
54inline 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));
Deepak Kodihallid1d79302017-07-11 23:01:39 -050076 size_t totalSizeAligned = align_up(totalSizeBytes, blockSize);
77 szBlocks = totalSizeAligned / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050078 tbl.resize(totalSizeAligned);
79}
80
81inline void Table::writeSizes(pnor_partition& part, size_t start, size_t end)
82{
83 size_t size = end - start;
Adriana Kobylak2ad21322017-11-28 14:59:19 -060084 part.data.base = align_up(start, blockSize) / blockSize;
Deepak Kodihallid1d79302017-07-11 23:01:39 -050085 size_t sizeInBlocks = align_up(size, blockSize) / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050086 part.data.size = sizeInBlocks;
Adriana Kobylakeb083552017-08-04 14:03:32 -050087
88 // If a a patch partition file exists, populate actual size with its file
89 // size if it is smaller than the total size.
90 fs::path patchFile(PARTITION_FILES_PATCH_LOC);
91 patchFile /= part.data.name;
92 if (fs::is_regular_file(patchFile))
93 {
94 part.data.actual = std::min(
95 size, static_cast<size_t>(fs::file_size(patchFile)));
96 }
97 else
98 {
99 part.data.actual = size;
100 }
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500101}
102
Deepak Kodihallie8b0e8a2017-07-12 10:01:31 -0500103inline void Table::writeUserdata(pnor_partition& part,
104 uint32_t version,
105 const std::string& data)
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500106{
107 if (std::string::npos != data.find("ECC"))
108 {
109 part.data.user.data[0] = PARTITION_ECC_PROTECTED;
110 }
Deepak Kodihallie8b0e8a2017-07-12 10:01:31 -0500111
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500112 auto perms = 0;
113 if (std::string::npos != data.find("READONLY"))
114 {
115 perms |= PARTITION_READONLY;
116 }
117 if (std::string::npos != data.find("PRESERVED"))
118 {
119 perms |= PARTITION_PRESERVED;
120 }
121 part.data.user.data[1] = perms;
Deepak Kodihallie8b0e8a2017-07-12 10:01:31 -0500122
123 part.data.user.data[1] |= version;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500124}
125
126inline void Table::writeDefaults(pnor_partition& part)
127{
128 part.data.pid = PARENT_PATITION_ID;
129 part.data.type = PARTITION_TYPE_DATA;
130 part.data.flags = 0; // flags unused
131}
132
133inline void Table::writeNameAndId(pnor_partition& part, std::string&& name,
134 const std::string& id)
135{
136 name.resize(PARTITION_NAME_MAX);
137 memcpy(part.data.name,
138 name.c_str(),
139 sizeof(part.data.name));
140 part.data.id = std::stoul(id);
141}
142
143void Table::preparePartitions()
144{
145 fs::path tocFile = directory;
146 tocFile /= PARTITION_TOC_FILE;
147 allocateMemory(tocFile);
148
149 std::ifstream file(tocFile.c_str());
150 static constexpr auto ID_MATCH = 1;
151 static constexpr auto NAME_MATCH = 2;
Adriana Kobylak1e1bdc72017-08-16 16:30:08 -0500152 static constexpr auto START_ADDR_MATCH = 4;
153 static constexpr auto END_ADDR_MATCH = 6;
154 static constexpr auto VERSION_MATCH = 8;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500155 // Parse PNOR toc (table of contents) file, which has lines like :
Adriana Kobylak1e1bdc72017-08-16 16:30:08 -0500156 // partition01=HBB,0x00010000,0x000a0000,0x80,ECC,PRESERVED, to indicate
Deepak Kodihallie8b0e8a2017-07-12 10:01:31 -0500157 // partition information
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500158 std::regex regex
159 {
160 "^partition([0-9]+)=([A-Za-z0-9_]+),"
Adriana Kobylak1e1bdc72017-08-16 16:30:08 -0500161 "(0x)?([0-9a-fA-F]+),(0x)?([0-9a-fA-F]+),(0x)?([A-Fa-f0-9]{2})",
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500162 std::regex::extended
163 };
164 std::smatch match;
165 std::string line;
Deepak Kodihallie8b0e8a2017-07-12 10:01:31 -0500166 constexpr auto versionShift = 24;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500167
168 decltype(auto) table = getNativeTable();
169
170 while (std::getline(file, line))
171 {
172 if (std::regex_search(line, match, regex))
173 {
174 fs::path partitionFile = directory;
175 partitionFile /= match[NAME_MATCH].str();
176 if (!fs::exists(partitionFile))
177 {
178 MSG_ERR("Partition file %s does not exist",
179 partitionFile.c_str());
180 continue;
181 }
182
183 writeNameAndId(table.partitions[numParts],
184 match[NAME_MATCH].str(),
185 match[ID_MATCH].str());
186 writeDefaults(table.partitions[numParts]);
187 writeSizes(table.partitions[numParts],
188 std::stoul(match[START_ADDR_MATCH].str(), nullptr, 16),
189 std::stoul(match[END_ADDR_MATCH].str(), nullptr, 16));
Deepak Kodihallie8b0e8a2017-07-12 10:01:31 -0500190 writeUserdata(
191 table.partitions[numParts],
192 std::stoul(match[VERSION_MATCH].str(), nullptr, 16) <<
193 versionShift, // For eg, convert "80" to 0x80000000
194 match.suffix().str());
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500195 table.partitions[numParts].checksum =
196 details::checksum(table.partitions[numParts].data);
197
198 ++numParts;
199 }
200 }
201}
202
203const pnor_partition& Table::partition(size_t offset) const
204{
205 const decltype(auto) table = getNativeTable();
Deepak Kodihallid1d79302017-07-11 23:01:39 -0500206 size_t offt = offset / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500207
208 for (decltype(numParts) i{}; i < numParts; ++i)
209 {
210 if ((offt >= table.partitions[i].data.base) &&
211 (offt < (table.partitions[i].data.base +
212 table.partitions[i].data.size)))
213 {
214 return table.partitions[i];
215 }
216 }
217
Jayanth Othayoth59ce0992017-10-27 00:45:53 -0500218 MSG_ERR("Partition corresponding to offset %x not found", offset);
219 elog<InternalFailure>();
220
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500221 static pnor_partition p{};
222 return p;
223}
224
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500225const pnor_partition& Table::partition(const std::string& name) const
226{
227 const decltype(auto) table = getNativeTable();
228
229 for (decltype(numParts) i{}; i < numParts; ++i)
230 {
231 if (name == table.partitions[i].data.name)
232 {
233 return table.partitions[i];
234 }
235 }
236
237 MSG_ERR("Partition %s not found", name.c_str());
Marri Devender Rao08b0a892017-11-08 03:38:27 -0600238 log<level::ERR>("Table::partition partition not found ",
239 entry("PARTITION_NAME=%s", name.c_str()));
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500240 elog<InternalFailure>();
241 static pnor_partition p{};
242 return p;
243}
244
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500245} // namespace partition
246
247PartitionTable endianFixup(const PartitionTable& in)
248{
249 PartitionTable out;
250 out.resize(in.size());
251 auto src = reinterpret_cast<const pnor_partition_table*>(in.data());
252 auto dst = reinterpret_cast<pnor_partition_table*>(out.data());
253
254 dst->data.magic = htobe32(src->data.magic);
255 dst->data.version = htobe32(src->data.version);
256 dst->data.size = htobe32(src->data.size);
257 dst->data.entry_size = htobe32(src->data.entry_size);
258 dst->data.entry_count = htobe32(src->data.entry_count);
259 dst->data.block_size = htobe32(src->data.block_size);
260 dst->data.block_count = htobe32(src->data.block_count);
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500261 dst->checksum = details::checksum(dst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500262
263 for (decltype(src->data.entry_count) i{}; i < src->data.entry_count; ++i)
264 {
265 auto psrc = &src->partitions[i];
266 auto pdst = &dst->partitions[i];
267 strncpy(pdst->data.name, psrc->data.name, PARTITION_NAME_MAX);
268 // Just to be safe
269 pdst->data.name[PARTITION_NAME_MAX] = '\0';
270 pdst->data.base = htobe32(psrc->data.base);
271 pdst->data.size = htobe32(psrc->data.size);
272 pdst->data.pid = htobe32(psrc->data.pid);
273 pdst->data.id = htobe32(psrc->data.id);
274 pdst->data.type = htobe32(psrc->data.type);
275 pdst->data.flags = htobe32(psrc->data.flags);
276 pdst->data.actual = htobe32(psrc->data.actual);
277 for (size_t j = 0; j < PARTITION_USER_WORDS; ++j)
278 {
279 pdst->data.user.data[j] = htobe32(psrc->data.user.data[j]);
280 }
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500281 pdst->checksum = details::checksum(pdst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500282 }
283
284 return out;
285}
286
287} // namespace virtual_pnor
288} // namespace openpower