blob: 930b4a9f5129806b992b2df9d9476a8aef80aa68 [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 Jefferyf96bd162018-02-26 13:05:00 +103087 // The ToC file presented in the vpnor squashfs looks like:
88 //
89 // version=IBM-witherspoon-ibm-OP9_v1.19_1.135
90 // extended_version=op-build-v1.19-571-g04f4690-dirty,buildroot-2017.11-5-g65679be,skiboot-v5.10-rc4,hostboot-4c46d66,linux-4.14.20-openpower1-p4a6b675,petitboot-v1.6.6-pe5aaec2,machine-xml-0fea226,occ-3286b6b,hostboot-binaries-3d1af8f,capp-ucode-p9-dd2-v3,sbe-99e2fe2
91 // partition00=part,0x00000000,0x00002000,00,READWRITE
92 // partition01=HBEL,0x00008000,0x0002c000,00,ECC,REPROVISION,CLEARECC,READWRITE
93 // ...
94 //
95 // As such we want to skip any lines that don't begin with 'partition'
96 if (std::string::npos == line.find("partition", 0))
Andrew Jefferyfaaa71c2018-02-21 16:29:48 +103097 {
98 continue;
99 }
100
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030101 parseTocLine(line, blockSize, part);
102
103 file = directory / part.data.name;
104 if (!fs::exists(file))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500105 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030106 std::stringstream err;
107 err << "Partition file " << file.native() << " does not exist";
108 throw InvalidTocEntry(err.str());
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500109 }
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030110
111 ++numParts;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500112 }
113}
114
115const pnor_partition& Table::partition(size_t offset) const
116{
117 const decltype(auto) table = getNativeTable();
Deepak Kodihallid1d79302017-07-11 23:01:39 -0500118 size_t offt = offset / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500119
120 for (decltype(numParts) i{}; i < numParts; ++i)
121 {
122 if ((offt >= table.partitions[i].data.base) &&
Andrew Jefferyf34db312018-03-09 15:27:03 +1030123 (offt <
124 (table.partitions[i].data.base + table.partitions[i].data.size)))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500125 {
126 return table.partitions[i];
127 }
128 }
129
Andrew Jefferye8a79ff2018-02-27 10:56:32 +1030130 MSG_ERR("Partition corresponding to offset 0x%zx not found", offset);
Jayanth Othayoth59ce0992017-10-27 00:45:53 -0500131 elog<InternalFailure>();
132
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500133 static pnor_partition p{};
134 return p;
135}
136
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500137const pnor_partition& Table::partition(const std::string& name) const
138{
139 const decltype(auto) table = getNativeTable();
140
141 for (decltype(numParts) i{}; i < numParts; ++i)
142 {
143 if (name == table.partitions[i].data.name)
144 {
145 return table.partitions[i];
146 }
147 }
148
Andrew Jefferye8a79ff2018-02-27 10:56:32 +1030149 MSG_ERR("Partition '%s' not found", name.c_str());
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500150 elog<InternalFailure>();
151 static pnor_partition p{};
152 return p;
153}
154
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500155} // namespace partition
156
157PartitionTable endianFixup(const PartitionTable& in)
158{
159 PartitionTable out;
160 out.resize(in.size());
161 auto src = reinterpret_cast<const pnor_partition_table*>(in.data());
162 auto dst = reinterpret_cast<pnor_partition_table*>(out.data());
163
164 dst->data.magic = htobe32(src->data.magic);
165 dst->data.version = htobe32(src->data.version);
166 dst->data.size = htobe32(src->data.size);
167 dst->data.entry_size = htobe32(src->data.entry_size);
168 dst->data.entry_count = htobe32(src->data.entry_count);
169 dst->data.block_size = htobe32(src->data.block_size);
170 dst->data.block_count = htobe32(src->data.block_count);
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500171 dst->checksum = details::checksum(dst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500172
173 for (decltype(src->data.entry_count) i{}; i < src->data.entry_count; ++i)
174 {
175 auto psrc = &src->partitions[i];
176 auto pdst = &dst->partitions[i];
177 strncpy(pdst->data.name, psrc->data.name, PARTITION_NAME_MAX);
178 // Just to be safe
179 pdst->data.name[PARTITION_NAME_MAX] = '\0';
180 pdst->data.base = htobe32(psrc->data.base);
181 pdst->data.size = htobe32(psrc->data.size);
182 pdst->data.pid = htobe32(psrc->data.pid);
183 pdst->data.id = htobe32(psrc->data.id);
184 pdst->data.type = htobe32(psrc->data.type);
185 pdst->data.flags = htobe32(psrc->data.flags);
186 pdst->data.actual = htobe32(psrc->data.actual);
187 for (size_t j = 0; j < PARTITION_USER_WORDS; ++j)
188 {
189 pdst->data.user.data[j] = htobe32(psrc->data.user.data[j]);
190 }
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500191 pdst->checksum = details::checksum(pdst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500192 }
193
194 return out;
195}
196
Andrew Jefferyd394a782018-02-21 16:16:14 +1030197static inline void writeSizes(pnor_partition& part, size_t start, size_t end,
198 size_t blockSize)
199{
200 size_t size = end - start;
201 part.data.base = align_up(start, blockSize) / blockSize;
202 size_t sizeInBlocks = align_up(size, blockSize) / blockSize;
203 part.data.size = sizeInBlocks;
204
205 // If a a patch partition file exists, populate actual size with its file
206 // size if it is smaller than the total size.
207 fs::path patchFile(PARTITION_FILES_PATCH_LOC);
208 patchFile /= part.data.name;
209 if (fs::is_regular_file(patchFile))
210 {
211 part.data.actual =
212 std::min(size, static_cast<size_t>(fs::file_size(patchFile)));
213 }
214 else
215 {
216 part.data.actual = size;
217 }
218}
219
220static inline void writeUserdata(pnor_partition& part, uint32_t version,
221 const std::string& data)
222{
223 std::istringstream stream(data);
224 std::string flag{};
225 auto perms = 0;
226
227 while (std::getline(stream, flag, ','))
228 {
229 if (flag == "ECC")
230 {
231 part.data.user.data[0] = PARTITION_ECC_PROTECTED;
232 }
233 else if (flag == "READONLY")
234 {
235 perms |= PARTITION_READONLY;
236 }
237 else if (flag == "PRESERVED")
238 {
239 perms |= PARTITION_PRESERVED;
240 }
241 else if (flag == "REPROVISION")
242 {
243 perms |= PARTITION_REPROVISION;
244 }
245 else if (flag == "VOLATILE")
246 {
247 perms |= PARTITION_VOLATILE;
248 }
249 else if (flag == "CLEARECC")
250 {
251 perms |= PARTITION_CLEARECC;
252 }
253 }
254
255 part.data.user.data[1] = perms;
256
257 part.data.user.data[1] |= version;
258}
259
260static inline void writeDefaults(pnor_partition& part)
261{
262 part.data.pid = PARENT_PATITION_ID;
263 part.data.type = PARTITION_TYPE_DATA;
264 part.data.flags = 0; // flags unused
265}
266
267static inline void writeNameAndId(pnor_partition& part, std::string&& name,
268 const std::string& id)
269{
270 name.resize(PARTITION_NAME_MAX);
271 memcpy(part.data.name, name.c_str(), sizeof(part.data.name));
272 part.data.id = std::stoul(id);
273}
274
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030275void parseTocLine(const std::string& line, size_t blockSize,
Andrew Jefferyd394a782018-02-21 16:16:14 +1030276 pnor_partition& part)
277{
278 static constexpr auto ID_MATCH = 1;
279 static constexpr auto NAME_MATCH = 2;
280 static constexpr auto START_ADDR_MATCH = 4;
281 static constexpr auto END_ADDR_MATCH = 6;
282 static constexpr auto VERSION_MATCH = 8;
283 constexpr auto versionShift = 24;
284
285 // Parse PNOR toc (table of contents) file, which has lines like :
286 // partition01=HBB,0x00010000,0x000a0000,0x80,ECC,PRESERVED, to indicate
287 // partition information
288 std::regex regex{
289 "^partition([0-9]+)=([A-Za-z0-9_]+),"
290 "(0x)?([0-9a-fA-F]+),(0x)?([0-9a-fA-F]+),(0x)?([A-Fa-f0-9]{2})",
291 std::regex::extended};
292
293 std::smatch match;
294 if (!std::regex_search(line, match, regex))
295 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030296 std::stringstream err;
297 err << "Malformed partition description: " << line.c_str() << "\n";
298 throw MalformedTocEntry(err.str());
Andrew Jefferyd394a782018-02-21 16:16:14 +1030299 }
300
301 writeNameAndId(part, match[NAME_MATCH].str(), match[ID_MATCH].str());
302 writeDefaults(part);
303
304 unsigned long start =
305 std::stoul(match[START_ADDR_MATCH].str(), nullptr, 16);
306 unsigned long end = std::stoul(match[END_ADDR_MATCH].str(), nullptr, 16);
307 writeSizes(part, start, end, blockSize);
308
309 // Use the shift to convert "80" to 0x80000000
310 unsigned long version = std::stoul(match[VERSION_MATCH].str(), nullptr, 16);
311 writeUserdata(part, version << versionShift, match.suffix().str());
312 part.checksum = details::checksum(part.data);
Andrew Jefferyd394a782018-02-21 16:16:14 +1030313}
314
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500315} // namespace virtual_pnor
316} // namespace openpower