blob: 36f07655b27b40455a3593857152a1f9321b5635 [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),
31 imgBlocks(0),
32 directory(std::move(directory)),
Deepak Kodihallid1d79302017-07-11 23:01:39 -050033 numParts(0),
34 blockSize(blockSize),
35 pnorSize(pnorSize)
Deepak Kodihalli393821d2017-04-28 04:44:38 -050036{
37 preparePartitions();
38 prepareHeader();
39 hostTbl = endianFixup(tbl);
40}
41
42void Table::prepareHeader()
43{
44 decltype(auto) table = getNativeTable();
45 table.data.magic = PARTITION_HEADER_MAGIC;
46 table.data.version = PARTITION_VERSION_1;
47 table.data.size = szBlocks;
48 table.data.entry_size = sizeof(pnor_partition);
49 table.data.entry_count = numParts;
Deepak Kodihallid1d79302017-07-11 23:01:39 -050050 table.data.block_size = blockSize;
51 table.data.block_count = pnorSize / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050052 table.checksum = details::checksum(table.data);
53}
54
55inline void Table::allocateMemory(const fs::path& tocFile)
56{
57 size_t num = 0;
58 std::string line;
59 std::ifstream file(tocFile.c_str());
60
61 // Find number of lines in partition file - this will help
62 // determine the number of partitions and hence also how much
63 // memory to allocate for the partitions array.
64 // The actual number of partitions may turn out to be lesser than this,
65 // in case of errors.
66 while (std::getline(file, line))
67 {
68 // Check if line starts with "partition"
69 if (std::string::npos != line.find("partition", 0))
70 {
71 ++num;
72 }
73 }
74
75 size_t totalSizeBytes = sizeof(pnor_partition_table) +
76 (num * sizeof(pnor_partition));
Deepak Kodihallid1d79302017-07-11 23:01:39 -050077 size_t totalSizeAligned = align_up(totalSizeBytes, blockSize);
78 szBlocks = totalSizeAligned / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050079 imgBlocks = szBlocks;
80 tbl.resize(totalSizeAligned);
81}
82
83inline void Table::writeSizes(pnor_partition& part, size_t start, size_t end)
84{
85 size_t size = end - start;
86 part.data.base = imgBlocks;
Deepak Kodihallid1d79302017-07-11 23:01:39 -050087 size_t sizeInBlocks = align_up(size, blockSize) / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050088 imgBlocks += sizeInBlocks;
89 part.data.size = sizeInBlocks;
90 part.data.actual = size;
91}
92
93inline void Table::writeUserdata(pnor_partition& part, const std::string& data)
94{
95 if (std::string::npos != data.find("ECC"))
96 {
97 part.data.user.data[0] = PARTITION_ECC_PROTECTED;
98 }
99 auto perms = 0;
100 if (std::string::npos != data.find("READONLY"))
101 {
102 perms |= PARTITION_READONLY;
103 }
104 if (std::string::npos != data.find("PRESERVED"))
105 {
106 perms |= PARTITION_PRESERVED;
107 }
108 part.data.user.data[1] = perms;
109}
110
111inline void Table::writeDefaults(pnor_partition& part)
112{
113 part.data.pid = PARENT_PATITION_ID;
114 part.data.type = PARTITION_TYPE_DATA;
115 part.data.flags = 0; // flags unused
116}
117
118inline void Table::writeNameAndId(pnor_partition& part, std::string&& name,
119 const std::string& id)
120{
121 name.resize(PARTITION_NAME_MAX);
122 memcpy(part.data.name,
123 name.c_str(),
124 sizeof(part.data.name));
125 part.data.id = std::stoul(id);
126}
127
128void Table::preparePartitions()
129{
130 fs::path tocFile = directory;
131 tocFile /= PARTITION_TOC_FILE;
132 allocateMemory(tocFile);
133
134 std::ifstream file(tocFile.c_str());
135 static constexpr auto ID_MATCH = 1;
136 static constexpr auto NAME_MATCH = 2;
137 static constexpr auto START_ADDR_MATCH = 3;
138 static constexpr auto END_ADDR_MATCH = 4;
139 // Parse PNOR toc (table of contents) file, which has lines like :
140 // partition01=HBB,00010000,000a0000,ECC,PRESERVED, to indicate partitions
141 std::regex regex
142 {
143 "^partition([0-9]+)=([A-Za-z0-9_]+),"
144 "([0-9a-fA-F]+),([0-9a-fA-F]+)",
145 std::regex::extended
146 };
147 std::smatch match;
148 std::string line;
149
150 decltype(auto) table = getNativeTable();
151
152 while (std::getline(file, line))
153 {
154 if (std::regex_search(line, match, regex))
155 {
156 fs::path partitionFile = directory;
157 partitionFile /= match[NAME_MATCH].str();
158 if (!fs::exists(partitionFile))
159 {
160 MSG_ERR("Partition file %s does not exist",
161 partitionFile.c_str());
162 continue;
163 }
164
165 writeNameAndId(table.partitions[numParts],
166 match[NAME_MATCH].str(),
167 match[ID_MATCH].str());
168 writeDefaults(table.partitions[numParts]);
169 writeSizes(table.partitions[numParts],
170 std::stoul(match[START_ADDR_MATCH].str(), nullptr, 16),
171 std::stoul(match[END_ADDR_MATCH].str(), nullptr, 16));
172 writeUserdata(table.partitions[numParts], match.suffix().str());
173 table.partitions[numParts].checksum =
174 details::checksum(table.partitions[numParts].data);
175
176 ++numParts;
177 }
178 }
179}
180
181const pnor_partition& Table::partition(size_t offset) const
182{
183 const decltype(auto) table = getNativeTable();
Deepak Kodihallid1d79302017-07-11 23:01:39 -0500184 size_t offt = offset / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500185
186 for (decltype(numParts) i{}; i < numParts; ++i)
187 {
188 if ((offt >= table.partitions[i].data.base) &&
189 (offt < (table.partitions[i].data.base +
190 table.partitions[i].data.size)))
191 {
192 return table.partitions[i];
193 }
194 }
195
196 static pnor_partition p{};
197 return p;
198}
199
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500200const pnor_partition& Table::partition(const std::string& name) const
201{
202 const decltype(auto) table = getNativeTable();
203
204 for (decltype(numParts) i{}; i < numParts; ++i)
205 {
206 if (name == table.partitions[i].data.name)
207 {
208 return table.partitions[i];
209 }
210 }
211
212 MSG_ERR("Partition %s not found", name.c_str());
213 elog<InternalFailure>();
214 static pnor_partition p{};
215 return p;
216}
217
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500218} // namespace partition
219
220PartitionTable endianFixup(const PartitionTable& in)
221{
222 PartitionTable out;
223 out.resize(in.size());
224 auto src = reinterpret_cast<const pnor_partition_table*>(in.data());
225 auto dst = reinterpret_cast<pnor_partition_table*>(out.data());
226
227 dst->data.magic = htobe32(src->data.magic);
228 dst->data.version = htobe32(src->data.version);
229 dst->data.size = htobe32(src->data.size);
230 dst->data.entry_size = htobe32(src->data.entry_size);
231 dst->data.entry_count = htobe32(src->data.entry_count);
232 dst->data.block_size = htobe32(src->data.block_size);
233 dst->data.block_count = htobe32(src->data.block_count);
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500234 dst->checksum = details::checksum(dst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500235
236 for (decltype(src->data.entry_count) i{}; i < src->data.entry_count; ++i)
237 {
238 auto psrc = &src->partitions[i];
239 auto pdst = &dst->partitions[i];
240 strncpy(pdst->data.name, psrc->data.name, PARTITION_NAME_MAX);
241 // Just to be safe
242 pdst->data.name[PARTITION_NAME_MAX] = '\0';
243 pdst->data.base = htobe32(psrc->data.base);
244 pdst->data.size = htobe32(psrc->data.size);
245 pdst->data.pid = htobe32(psrc->data.pid);
246 pdst->data.id = htobe32(psrc->data.id);
247 pdst->data.type = htobe32(psrc->data.type);
248 pdst->data.flags = htobe32(psrc->data.flags);
249 pdst->data.actual = htobe32(psrc->data.actual);
250 for (size_t j = 0; j < PARTITION_USER_WORDS; ++j)
251 {
252 pdst->data.user.data[j] = htobe32(psrc->data.user.data[j]);
253 }
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500254 pdst->checksum = details::checksum(pdst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500255 }
256
257 return out;
258}
259
260} // namespace virtual_pnor
261} // namespace openpower