blob: 983cddd3f762a9a1ac08723d2e8c1dba55dee48b [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 Jefferye5707402018-02-02 14:39:36 +1030130 MSG_ERR("Partition corresponding to offset %zu 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
149 MSG_ERR("Partition %s not found", name.c_str());
Marri Devender Rao08b0a892017-11-08 03:38:27 -0600150 log<level::ERR>("Table::partition partition not found ",
Andrew Jefferyf34db312018-03-09 15:27:03 +1030151 entry("PARTITION_NAME=%s", name.c_str()));
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500152 elog<InternalFailure>();
153 static pnor_partition p{};
154 return p;
155}
156
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500157} // namespace partition
158
159PartitionTable endianFixup(const PartitionTable& in)
160{
161 PartitionTable out;
162 out.resize(in.size());
163 auto src = reinterpret_cast<const pnor_partition_table*>(in.data());
164 auto dst = reinterpret_cast<pnor_partition_table*>(out.data());
165
166 dst->data.magic = htobe32(src->data.magic);
167 dst->data.version = htobe32(src->data.version);
168 dst->data.size = htobe32(src->data.size);
169 dst->data.entry_size = htobe32(src->data.entry_size);
170 dst->data.entry_count = htobe32(src->data.entry_count);
171 dst->data.block_size = htobe32(src->data.block_size);
172 dst->data.block_count = htobe32(src->data.block_count);
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500173 dst->checksum = details::checksum(dst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500174
175 for (decltype(src->data.entry_count) i{}; i < src->data.entry_count; ++i)
176 {
177 auto psrc = &src->partitions[i];
178 auto pdst = &dst->partitions[i];
179 strncpy(pdst->data.name, psrc->data.name, PARTITION_NAME_MAX);
180 // Just to be safe
181 pdst->data.name[PARTITION_NAME_MAX] = '\0';
182 pdst->data.base = htobe32(psrc->data.base);
183 pdst->data.size = htobe32(psrc->data.size);
184 pdst->data.pid = htobe32(psrc->data.pid);
185 pdst->data.id = htobe32(psrc->data.id);
186 pdst->data.type = htobe32(psrc->data.type);
187 pdst->data.flags = htobe32(psrc->data.flags);
188 pdst->data.actual = htobe32(psrc->data.actual);
189 for (size_t j = 0; j < PARTITION_USER_WORDS; ++j)
190 {
191 pdst->data.user.data[j] = htobe32(psrc->data.user.data[j]);
192 }
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500193 pdst->checksum = details::checksum(pdst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500194 }
195
196 return out;
197}
198
Andrew Jefferyd394a782018-02-21 16:16:14 +1030199static inline void writeSizes(pnor_partition& part, size_t start, size_t end,
200 size_t blockSize)
201{
202 size_t size = end - start;
203 part.data.base = align_up(start, blockSize) / blockSize;
204 size_t sizeInBlocks = align_up(size, blockSize) / blockSize;
205 part.data.size = sizeInBlocks;
206
207 // If a a patch partition file exists, populate actual size with its file
208 // size if it is smaller than the total size.
209 fs::path patchFile(PARTITION_FILES_PATCH_LOC);
210 patchFile /= part.data.name;
211 if (fs::is_regular_file(patchFile))
212 {
213 part.data.actual =
214 std::min(size, static_cast<size_t>(fs::file_size(patchFile)));
215 }
216 else
217 {
218 part.data.actual = size;
219 }
220}
221
222static inline void writeUserdata(pnor_partition& part, uint32_t version,
223 const std::string& data)
224{
225 std::istringstream stream(data);
226 std::string flag{};
227 auto perms = 0;
228
229 while (std::getline(stream, flag, ','))
230 {
231 if (flag == "ECC")
232 {
233 part.data.user.data[0] = PARTITION_ECC_PROTECTED;
234 }
235 else if (flag == "READONLY")
236 {
237 perms |= PARTITION_READONLY;
238 }
239 else if (flag == "PRESERVED")
240 {
241 perms |= PARTITION_PRESERVED;
242 }
243 else if (flag == "REPROVISION")
244 {
245 perms |= PARTITION_REPROVISION;
246 }
247 else if (flag == "VOLATILE")
248 {
249 perms |= PARTITION_VOLATILE;
250 }
251 else if (flag == "CLEARECC")
252 {
253 perms |= PARTITION_CLEARECC;
254 }
255 }
256
257 part.data.user.data[1] = perms;
258
259 part.data.user.data[1] |= version;
260}
261
262static inline void writeDefaults(pnor_partition& part)
263{
264 part.data.pid = PARENT_PATITION_ID;
265 part.data.type = PARTITION_TYPE_DATA;
266 part.data.flags = 0; // flags unused
267}
268
269static inline void writeNameAndId(pnor_partition& part, std::string&& name,
270 const std::string& id)
271{
272 name.resize(PARTITION_NAME_MAX);
273 memcpy(part.data.name, name.c_str(), sizeof(part.data.name));
274 part.data.id = std::stoul(id);
275}
276
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030277void parseTocLine(const std::string& line, size_t blockSize,
Andrew Jefferyd394a782018-02-21 16:16:14 +1030278 pnor_partition& part)
279{
280 static constexpr auto ID_MATCH = 1;
281 static constexpr auto NAME_MATCH = 2;
282 static constexpr auto START_ADDR_MATCH = 4;
283 static constexpr auto END_ADDR_MATCH = 6;
284 static constexpr auto VERSION_MATCH = 8;
285 constexpr auto versionShift = 24;
286
287 // Parse PNOR toc (table of contents) file, which has lines like :
288 // partition01=HBB,0x00010000,0x000a0000,0x80,ECC,PRESERVED, to indicate
289 // partition information
290 std::regex regex{
291 "^partition([0-9]+)=([A-Za-z0-9_]+),"
292 "(0x)?([0-9a-fA-F]+),(0x)?([0-9a-fA-F]+),(0x)?([A-Fa-f0-9]{2})",
293 std::regex::extended};
294
295 std::smatch match;
296 if (!std::regex_search(line, match, regex))
297 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030298 std::stringstream err;
299 err << "Malformed partition description: " << line.c_str() << "\n";
300 throw MalformedTocEntry(err.str());
Andrew Jefferyd394a782018-02-21 16:16:14 +1030301 }
302
303 writeNameAndId(part, match[NAME_MATCH].str(), match[ID_MATCH].str());
304 writeDefaults(part);
305
306 unsigned long start =
307 std::stoul(match[START_ADDR_MATCH].str(), nullptr, 16);
308 unsigned long end = std::stoul(match[END_ADDR_MATCH].str(), nullptr, 16);
309 writeSizes(part, start, end, blockSize);
310
311 // Use the shift to convert "80" to 0x80000000
312 unsigned long version = std::stoul(match[VERSION_MATCH].str(), nullptr, 16);
313 writeUserdata(part, version << versionShift, match.suffix().str());
314 part.checksum = details::checksum(part.data);
Andrew Jefferyd394a782018-02-21 16:16:14 +1030315}
316
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500317} // namespace virtual_pnor
318} // namespace openpower