blob: 50d7d59a7203c9f52c9ecb230ecdaba3a866330b [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Deepak Kodihalli393821d2017-04-28 04:44:38 -05003#include "pnor_partition_table.hpp"
4#include "common.h"
5#include "config.h"
Deepak Kodihalli8a899692017-07-11 23:17:19 -05006#include "xyz/openbmc_project/Common/error.hpp"
7#include <phosphor-logging/elog-errors.hpp>
Deepak Kodihalli393821d2017-04-28 04:44:38 -05008#include <syslog.h>
9#include <endian.h>
10#include <regex>
11#include <fstream>
12#include <algorithm>
13
14namespace openpower
15{
16namespace virtual_pnor
17{
18
Deepak Kodihalli8a899692017-07-11 23:17:19 -050019using namespace phosphor::logging;
20using namespace sdbusplus::xyz::openbmc_project::Common::Error;
21
Deepak Kodihalli393821d2017-04-28 04:44:38 -050022namespace partition
23{
Deepak Kodihalli393821d2017-04-28 04:44:38 -050024
Andrew Jefferyf34db312018-03-09 15:27:03 +103025Table::Table(fs::path&& directory, size_t blockSize, size_t pnorSize) :
Andrew Jeffery7f9c3432018-03-01 12:07:13 +103026 szBytes(sizeof(pnor_partition_table)), directory(std::move(directory)),
27 numParts(0), blockSize(blockSize), pnorSize(pnorSize)
Deepak Kodihalli393821d2017-04-28 04:44:38 -050028{
29 preparePartitions();
30 prepareHeader();
31 hostTbl = endianFixup(tbl);
32}
33
34void Table::prepareHeader()
35{
36 decltype(auto) table = getNativeTable();
37 table.data.magic = PARTITION_HEADER_MAGIC;
38 table.data.version = PARTITION_VERSION_1;
Andrew Jeffery7f9c3432018-03-01 12:07:13 +103039 table.data.size = blocks();
Deepak Kodihalli393821d2017-04-28 04:44:38 -050040 table.data.entry_size = sizeof(pnor_partition);
41 table.data.entry_count = numParts;
Deepak Kodihallid1d79302017-07-11 23:01:39 -050042 table.data.block_size = blockSize;
43 table.data.block_count = pnorSize / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050044 table.checksum = details::checksum(table.data);
45}
46
47inline void Table::allocateMemory(const fs::path& tocFile)
48{
49 size_t num = 0;
50 std::string line;
51 std::ifstream file(tocFile.c_str());
52
53 // Find number of lines in partition file - this will help
54 // determine the number of partitions and hence also how much
55 // memory to allocate for the partitions array.
56 // The actual number of partitions may turn out to be lesser than this,
57 // in case of errors.
58 while (std::getline(file, line))
59 {
60 // Check if line starts with "partition"
61 if (std::string::npos != line.find("partition", 0))
62 {
63 ++num;
64 }
65 }
66
Andrew Jeffery7f9c3432018-03-01 12:07:13 +103067 szBytes = sizeof(pnor_partition_table) + (num * sizeof(pnor_partition));
68 tbl.resize(capacity());
Deepak Kodihalli393821d2017-04-28 04:44:38 -050069}
70
Andrew Jeffery581a4f22018-02-21 15:29:38 +103071void Table::preparePartitions()
72{
73 fs::path tocFile = directory;
74 tocFile /= PARTITION_TOC_FILE;
75 allocateMemory(tocFile);
76
77 std::ifstream file(tocFile.c_str());
78 std::string line;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050079 decltype(auto) table = getNativeTable();
80
81 while (std::getline(file, line))
82 {
Andrew Jefferyfaaa71c2018-02-21 16:29:48 +103083 pnor_partition& part = table.partitions[numParts];
84 fs::path file;
85
Andrew Jefferyf96bd162018-02-26 13:05:00 +103086 // The ToC file presented in the vpnor squashfs looks like:
87 //
88 // version=IBM-witherspoon-ibm-OP9_v1.19_1.135
89 // 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
90 // partition00=part,0x00000000,0x00002000,00,READWRITE
91 // partition01=HBEL,0x00008000,0x0002c000,00,ECC,REPROVISION,CLEARECC,READWRITE
92 // ...
93 //
94 // As such we want to skip any lines that don't begin with 'partition'
95 if (std::string::npos == line.find("partition", 0))
Andrew Jefferyfaaa71c2018-02-21 16:29:48 +103096 {
97 continue;
98 }
99
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030100 parseTocLine(line, blockSize, part);
101
Andrew Jeffery4ca1aa52018-03-02 12:08:29 +1030102 if (numParts > 0)
103 {
104 struct pnor_partition& prev = table.partitions[numParts - 1];
105 uint32_t prev_end = prev.data.base + prev.data.size;
106
107 if (part.data.id == prev.data.id)
108 {
109 MSG_ERR("ID for previous partition '%s' at block 0x%" PRIx32
110 "matches current partition '%s' at block 0x%" PRIx32
111 ": %" PRId32 "\n",
112 prev.data.name, prev.data.base, part.data.name,
113 part.data.base, part.data.id);
114 }
115
116 if (part.data.base < prev_end)
117 {
118 std::stringstream err;
119 err << "Partition '" << part.data.name << "' start block 0x"
120 << std::hex << part.data.base << "is less than the end "
121 << "block 0x" << std::hex << prev_end << " of '"
122 << prev.data.name << "'";
123 throw InvalidTocEntry(err.str());
124 }
125 }
126
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030127 file = directory / part.data.name;
128 if (!fs::exists(file))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500129 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030130 std::stringstream err;
131 err << "Partition file " << file.native() << " does not exist";
132 throw InvalidTocEntry(err.str());
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500133 }
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030134
135 ++numParts;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500136 }
137}
138
139const pnor_partition& Table::partition(size_t offset) const
140{
141 const decltype(auto) table = getNativeTable();
Andrew Jefferybd38c212018-02-27 17:44:33 +1030142 size_t blockOffset = offset / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500143
144 for (decltype(numParts) i{}; i < numParts; ++i)
145 {
Andrew Jefferycd928512018-02-28 22:33:48 +1030146 const struct pnor_partition& part = table.partitions[i];
147 if ((blockOffset >= part.data.base) &&
148 (blockOffset < (part.data.base + part.data.size)))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500149 {
Andrew Jefferycd928512018-02-28 22:33:48 +1030150 return part;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500151 }
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030152
153 /* Are we in a hole between partitions? */
154 if (blockOffset < part.data.base)
155 {
156 throw UnmappedOffset(offset, part.data.base * blockSize);
157 }
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500158 }
159
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030160 throw UnmappedOffset(offset, pnorSize);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500161}
162
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500163const pnor_partition& Table::partition(const std::string& name) const
164{
165 const decltype(auto) table = getNativeTable();
166
167 for (decltype(numParts) i{}; i < numParts; ++i)
168 {
169 if (name == table.partitions[i].data.name)
170 {
171 return table.partitions[i];
172 }
173 }
174
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030175 std::stringstream err;
176 err << "Partition " << name << " is not listed in the table of contents";
177 throw UnknownPartition(err.str());
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500178}
179
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500180} // namespace partition
181
182PartitionTable endianFixup(const PartitionTable& in)
183{
184 PartitionTable out;
185 out.resize(in.size());
186 auto src = reinterpret_cast<const pnor_partition_table*>(in.data());
187 auto dst = reinterpret_cast<pnor_partition_table*>(out.data());
188
189 dst->data.magic = htobe32(src->data.magic);
190 dst->data.version = htobe32(src->data.version);
191 dst->data.size = htobe32(src->data.size);
192 dst->data.entry_size = htobe32(src->data.entry_size);
193 dst->data.entry_count = htobe32(src->data.entry_count);
194 dst->data.block_size = htobe32(src->data.block_size);
195 dst->data.block_count = htobe32(src->data.block_count);
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500196 dst->checksum = details::checksum(dst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500197
198 for (decltype(src->data.entry_count) i{}; i < src->data.entry_count; ++i)
199 {
200 auto psrc = &src->partitions[i];
201 auto pdst = &dst->partitions[i];
202 strncpy(pdst->data.name, psrc->data.name, PARTITION_NAME_MAX);
203 // Just to be safe
204 pdst->data.name[PARTITION_NAME_MAX] = '\0';
205 pdst->data.base = htobe32(psrc->data.base);
206 pdst->data.size = htobe32(psrc->data.size);
207 pdst->data.pid = htobe32(psrc->data.pid);
208 pdst->data.id = htobe32(psrc->data.id);
209 pdst->data.type = htobe32(psrc->data.type);
210 pdst->data.flags = htobe32(psrc->data.flags);
211 pdst->data.actual = htobe32(psrc->data.actual);
212 for (size_t j = 0; j < PARTITION_USER_WORDS; ++j)
213 {
214 pdst->data.user.data[j] = htobe32(psrc->data.user.data[j]);
215 }
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500216 pdst->checksum = details::checksum(pdst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500217 }
218
219 return out;
220}
221
Andrew Jefferyd394a782018-02-21 16:16:14 +1030222static inline void writeSizes(pnor_partition& part, size_t start, size_t end,
223 size_t blockSize)
224{
225 size_t size = end - start;
226 part.data.base = align_up(start, blockSize) / blockSize;
227 size_t sizeInBlocks = align_up(size, blockSize) / blockSize;
228 part.data.size = sizeInBlocks;
229
230 // If a a patch partition file exists, populate actual size with its file
231 // size if it is smaller than the total size.
232 fs::path patchFile(PARTITION_FILES_PATCH_LOC);
233 patchFile /= part.data.name;
234 if (fs::is_regular_file(patchFile))
235 {
236 part.data.actual =
237 std::min(size, static_cast<size_t>(fs::file_size(patchFile)));
238 }
239 else
240 {
241 part.data.actual = size;
242 }
243}
244
245static inline void writeUserdata(pnor_partition& part, uint32_t version,
246 const std::string& data)
247{
248 std::istringstream stream(data);
249 std::string flag{};
250 auto perms = 0;
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030251 auto state = 0;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030252
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030253 MSG_DBG("Parsing ToC flags '%s'\n", data.c_str());
Andrew Jefferyd394a782018-02-21 16:16:14 +1030254 while (std::getline(stream, flag, ','))
255 {
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030256 if (flag == "")
257 continue;
258
Andrew Jefferyd394a782018-02-21 16:16:14 +1030259 if (flag == "ECC")
260 {
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030261 state |= PARTITION_ECC_PROTECTED;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030262 }
263 else if (flag == "READONLY")
264 {
265 perms |= PARTITION_READONLY;
266 }
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030267 else if (flag == "READWRITE")
268 {
269 perms &= ~PARTITION_READONLY;
270 }
Andrew Jefferyd394a782018-02-21 16:16:14 +1030271 else if (flag == "PRESERVED")
272 {
273 perms |= PARTITION_PRESERVED;
274 }
275 else if (flag == "REPROVISION")
276 {
277 perms |= PARTITION_REPROVISION;
278 }
279 else if (flag == "VOLATILE")
280 {
281 perms |= PARTITION_VOLATILE;
282 }
283 else if (flag == "CLEARECC")
284 {
285 perms |= PARTITION_CLEARECC;
286 }
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030287 else
288 {
289 MSG_INFO("Found unimplemented partition property: %s\n",
290 flag.c_str());
291 }
Andrew Jefferyd394a782018-02-21 16:16:14 +1030292 }
293
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030294 part.data.user.data[0] = state;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030295 part.data.user.data[1] = perms;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030296 part.data.user.data[1] |= version;
297}
298
299static inline void writeDefaults(pnor_partition& part)
300{
301 part.data.pid = PARENT_PATITION_ID;
302 part.data.type = PARTITION_TYPE_DATA;
303 part.data.flags = 0; // flags unused
304}
305
306static inline void writeNameAndId(pnor_partition& part, std::string&& name,
307 const std::string& id)
308{
309 name.resize(PARTITION_NAME_MAX);
310 memcpy(part.data.name, name.c_str(), sizeof(part.data.name));
311 part.data.id = std::stoul(id);
312}
313
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030314void parseTocLine(const std::string& line, size_t blockSize,
Andrew Jefferyd394a782018-02-21 16:16:14 +1030315 pnor_partition& part)
316{
317 static constexpr auto ID_MATCH = 1;
318 static constexpr auto NAME_MATCH = 2;
319 static constexpr auto START_ADDR_MATCH = 4;
320 static constexpr auto END_ADDR_MATCH = 6;
321 static constexpr auto VERSION_MATCH = 8;
322 constexpr auto versionShift = 24;
323
324 // Parse PNOR toc (table of contents) file, which has lines like :
325 // partition01=HBB,0x00010000,0x000a0000,0x80,ECC,PRESERVED, to indicate
326 // partition information
327 std::regex regex{
328 "^partition([0-9]+)=([A-Za-z0-9_]+),"
329 "(0x)?([0-9a-fA-F]+),(0x)?([0-9a-fA-F]+),(0x)?([A-Fa-f0-9]{2})",
330 std::regex::extended};
331
332 std::smatch match;
333 if (!std::regex_search(line, match, regex))
334 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030335 std::stringstream err;
336 err << "Malformed partition description: " << line.c_str() << "\n";
337 throw MalformedTocEntry(err.str());
Andrew Jefferyd394a782018-02-21 16:16:14 +1030338 }
339
340 writeNameAndId(part, match[NAME_MATCH].str(), match[ID_MATCH].str());
341 writeDefaults(part);
342
343 unsigned long start =
344 std::stoul(match[START_ADDR_MATCH].str(), nullptr, 16);
Andrew Jefferyb87aa322018-02-27 16:27:02 +1030345 if (start & (blockSize - 1))
346 {
347 MSG_ERR("Start offset 0x%lx for partition '%s' is not aligned to block "
348 "size 0x%zx\n",
349 start, match[NAME_MATCH].str().c_str(), blockSize);
350 }
351
Andrew Jefferyd394a782018-02-21 16:16:14 +1030352 unsigned long end = std::stoul(match[END_ADDR_MATCH].str(), nullptr, 16);
Andrew Jefferyb87aa322018-02-27 16:27:02 +1030353 if ((end - start) & (blockSize - 1))
354 {
355 MSG_ERR("Partition '%s' has a size 0x%lx that is not aligned to block "
356 "size 0x%zx\n",
357 match[NAME_MATCH].str().c_str(), (end - start), blockSize);
358 }
359
Andrew Jefferydb493412018-03-02 11:54:22 +1030360 if (start >= end)
361 {
362 std::stringstream err;
363 err << "Partition " << match[NAME_MATCH].str()
364 << " has an invalid range: start offset (0x" << std::hex << start
365 << " is beyond open end (0x" << std::hex << end << ")\n";
366 throw InvalidTocEntry(err.str());
367 }
Andrew Jefferyd394a782018-02-21 16:16:14 +1030368 writeSizes(part, start, end, blockSize);
369
370 // Use the shift to convert "80" to 0x80000000
371 unsigned long version = std::stoul(match[VERSION_MATCH].str(), nullptr, 16);
372 writeUserdata(part, version << versionShift, match.suffix().str());
373 part.checksum = details::checksum(part.data);
Andrew Jefferyd394a782018-02-21 16:16:14 +1030374}
375
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500376} // namespace virtual_pnor
377} // namespace openpower