blob: 2a9442bae50db1699621ce6707bb68779c17a5f1 [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 "config.h"
William A. Kennington IIId5f1d402018-10-11 13:55:04 -07004
5#include "pnor_partition_table.hpp"
6
Deepak Kodihalli8a899692017-07-11 23:17:19 -05007#include "xyz/openbmc_project/Common/error.hpp"
William A. Kennington IIId5f1d402018-10-11 13:55:04 -07008
Deepak Kodihalli393821d2017-04-28 04:44:38 -05009#include <endian.h>
William A. Kennington IIId5f1d402018-10-11 13:55:04 -070010#include <syslog.h>
11
Deepak Kodihalli393821d2017-04-28 04:44:38 -050012#include <algorithm>
William A. Kennington IIId5f1d402018-10-11 13:55:04 -070013#include <fstream>
14#include <phosphor-logging/elog-errors.hpp>
15#include <regex>
16
Evan Lojewskif1e547c2019-03-14 14:34:33 +103017extern "C" {
18#include "backend.h"
William A. Kennington IIId5f1d402018-10-11 13:55:04 -070019#include "common.h"
20#include "mboxd.h"
Evan Lojewskif1e547c2019-03-14 14:34:33 +103021}
Deepak Kodihalli393821d2017-04-28 04:44:38 -050022
23namespace openpower
24{
25namespace virtual_pnor
26{
27
Deepak Kodihalli8a899692017-07-11 23:17:19 -050028using namespace phosphor::logging;
29using namespace sdbusplus::xyz::openbmc_project::Common::Error;
30
Deepak Kodihalli393821d2017-04-28 04:44:38 -050031namespace partition
32{
Deepak Kodihalli393821d2017-04-28 04:44:38 -050033
Evan Lojewskif1e547c2019-03-14 14:34:33 +103034Table::Table(const struct backend* be) :
Andrew Jeffery742a1f62018-03-02 09:26:03 +103035 szBytes(sizeof(pnor_partition_table)), numParts(0),
Evan Lojewskif1e547c2019-03-14 14:34:33 +103036 blockSize(1 << be->erase_size_shift), pnorSize(be->flash_size)
Deepak Kodihalli393821d2017-04-28 04:44:38 -050037{
Evan Lojewskif1e547c2019-03-14 14:34:33 +103038 preparePartitions((const struct vpnor_data*)be->priv);
Deepak Kodihalli393821d2017-04-28 04:44:38 -050039 prepareHeader();
40 hostTbl = endianFixup(tbl);
41}
42
43void Table::prepareHeader()
44{
45 decltype(auto) table = getNativeTable();
46 table.data.magic = PARTITION_HEADER_MAGIC;
47 table.data.version = PARTITION_VERSION_1;
Andrew Jeffery7f9c3432018-03-01 12:07:13 +103048 table.data.size = blocks();
Deepak Kodihalli393821d2017-04-28 04:44:38 -050049 table.data.entry_size = sizeof(pnor_partition);
50 table.data.entry_count = numParts;
Deepak Kodihallid1d79302017-07-11 23:01:39 -050051 table.data.block_size = blockSize;
52 table.data.block_count = pnorSize / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050053 table.checksum = details::checksum(table.data);
54}
55
56inline void Table::allocateMemory(const fs::path& tocFile)
57{
58 size_t num = 0;
59 std::string line;
60 std::ifstream file(tocFile.c_str());
61
62 // Find number of lines in partition file - this will help
63 // determine the number of partitions and hence also how much
64 // memory to allocate for the partitions array.
65 // The actual number of partitions may turn out to be lesser than this,
66 // in case of errors.
67 while (std::getline(file, line))
68 {
69 // Check if line starts with "partition"
70 if (std::string::npos != line.find("partition", 0))
71 {
72 ++num;
73 }
74 }
75
Andrew Jeffery7f9c3432018-03-01 12:07:13 +103076 szBytes = sizeof(pnor_partition_table) + (num * sizeof(pnor_partition));
77 tbl.resize(capacity());
Deepak Kodihalli393821d2017-04-28 04:44:38 -050078}
79
Evan Lojewskif1e547c2019-03-14 14:34:33 +103080void Table::preparePartitions(const struct vpnor_data* priv)
Andrew Jeffery581a4f22018-02-21 15:29:38 +103081{
Evan Lojewskif1e547c2019-03-14 14:34:33 +103082 const fs::path roDir(priv->paths.ro_loc);
83 const fs::path patchDir(priv->paths.patch_loc);
Andrew Jeffery742a1f62018-03-02 09:26:03 +103084 fs::path tocFile = roDir / PARTITION_TOC_FILE;
Andrew Jeffery581a4f22018-02-21 15:29:38 +103085 allocateMemory(tocFile);
86
87 std::ifstream file(tocFile.c_str());
88 std::string line;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050089 decltype(auto) table = getNativeTable();
90
91 while (std::getline(file, line))
92 {
Andrew Jefferyfaaa71c2018-02-21 16:29:48 +103093 pnor_partition& part = table.partitions[numParts];
Andrew Jeffery742a1f62018-03-02 09:26:03 +103094 fs::path patch;
Andrew Jefferyfaaa71c2018-02-21 16:29:48 +103095 fs::path file;
96
Andrew Jefferyf96bd162018-02-26 13:05:00 +103097 // The ToC file presented in the vpnor squashfs looks like:
98 //
99 // version=IBM-witherspoon-ibm-OP9_v1.19_1.135
100 // 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
101 // partition00=part,0x00000000,0x00002000,00,READWRITE
102 // partition01=HBEL,0x00008000,0x0002c000,00,ECC,REPROVISION,CLEARECC,READWRITE
103 // ...
104 //
105 // As such we want to skip any lines that don't begin with 'partition'
106 if (std::string::npos == line.find("partition", 0))
Andrew Jefferyfaaa71c2018-02-21 16:29:48 +1030107 {
108 continue;
109 }
110
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030111 parseTocLine(line, blockSize, part);
112
Andrew Jeffery4ca1aa52018-03-02 12:08:29 +1030113 if (numParts > 0)
114 {
115 struct pnor_partition& prev = table.partitions[numParts - 1];
116 uint32_t prev_end = prev.data.base + prev.data.size;
117
118 if (part.data.id == prev.data.id)
119 {
120 MSG_ERR("ID for previous partition '%s' at block 0x%" PRIx32
121 "matches current partition '%s' at block 0x%" PRIx32
122 ": %" PRId32 "\n",
123 prev.data.name, prev.data.base, part.data.name,
124 part.data.base, part.data.id);
125 }
126
127 if (part.data.base < prev_end)
128 {
129 std::stringstream err;
130 err << "Partition '" << part.data.name << "' start block 0x"
131 << std::hex << part.data.base << "is less than the end "
132 << "block 0x" << std::hex << prev_end << " of '"
133 << prev.data.name << "'";
134 throw InvalidTocEntry(err.str());
135 }
136 }
137
Andrew Jeffery742a1f62018-03-02 09:26:03 +1030138 file = roDir / part.data.name;
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030139 if (!fs::exists(file))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500140 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030141 std::stringstream err;
142 err << "Partition file " << file.native() << " does not exist";
143 throw InvalidTocEntry(err.str());
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500144 }
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030145
Andrew Jeffery742a1f62018-03-02 09:26:03 +1030146 patch = patchDir / part.data.name;
147 if (fs::is_regular_file(patch))
148 {
149 const size_t size = part.data.size * blockSize;
150 part.data.actual =
151 std::min(size, static_cast<size_t>(fs::file_size(patch)));
152 }
153
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030154 ++numParts;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500155 }
156}
157
158const pnor_partition& Table::partition(size_t offset) const
159{
160 const decltype(auto) table = getNativeTable();
Andrew Jefferybd38c212018-02-27 17:44:33 +1030161 size_t blockOffset = offset / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500162
163 for (decltype(numParts) i{}; i < numParts; ++i)
164 {
Andrew Jefferycd928512018-02-28 22:33:48 +1030165 const struct pnor_partition& part = table.partitions[i];
Andrew Jeffery1a3f8432018-03-02 10:18:02 +1030166 size_t len = part.data.size;
167
Andrew Jefferycd928512018-02-28 22:33:48 +1030168 if ((blockOffset >= part.data.base) &&
Andrew Jeffery1a3f8432018-03-02 10:18:02 +1030169 (blockOffset < (part.data.base + len)))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500170 {
Andrew Jefferycd928512018-02-28 22:33:48 +1030171 return part;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500172 }
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030173
174 /* Are we in a hole between partitions? */
175 if (blockOffset < part.data.base)
176 {
177 throw UnmappedOffset(offset, part.data.base * blockSize);
178 }
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500179 }
180
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030181 throw UnmappedOffset(offset, pnorSize);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500182}
183
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500184const pnor_partition& Table::partition(const std::string& name) const
185{
186 const decltype(auto) table = getNativeTable();
187
188 for (decltype(numParts) i{}; i < numParts; ++i)
189 {
190 if (name == table.partitions[i].data.name)
191 {
192 return table.partitions[i];
193 }
194 }
195
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030196 std::stringstream err;
197 err << "Partition " << name << " is not listed in the table of contents";
198 throw UnknownPartition(err.str());
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500199}
200
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500201} // namespace partition
202
203PartitionTable endianFixup(const PartitionTable& in)
204{
205 PartitionTable out;
206 out.resize(in.size());
207 auto src = reinterpret_cast<const pnor_partition_table*>(in.data());
208 auto dst = reinterpret_cast<pnor_partition_table*>(out.data());
209
210 dst->data.magic = htobe32(src->data.magic);
211 dst->data.version = htobe32(src->data.version);
212 dst->data.size = htobe32(src->data.size);
213 dst->data.entry_size = htobe32(src->data.entry_size);
214 dst->data.entry_count = htobe32(src->data.entry_count);
215 dst->data.block_size = htobe32(src->data.block_size);
216 dst->data.block_count = htobe32(src->data.block_count);
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500217 dst->checksum = details::checksum(dst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500218
219 for (decltype(src->data.entry_count) i{}; i < src->data.entry_count; ++i)
220 {
221 auto psrc = &src->partitions[i];
222 auto pdst = &dst->partitions[i];
223 strncpy(pdst->data.name, psrc->data.name, PARTITION_NAME_MAX);
224 // Just to be safe
225 pdst->data.name[PARTITION_NAME_MAX] = '\0';
226 pdst->data.base = htobe32(psrc->data.base);
227 pdst->data.size = htobe32(psrc->data.size);
228 pdst->data.pid = htobe32(psrc->data.pid);
229 pdst->data.id = htobe32(psrc->data.id);
230 pdst->data.type = htobe32(psrc->data.type);
231 pdst->data.flags = htobe32(psrc->data.flags);
232 pdst->data.actual = htobe32(psrc->data.actual);
233 for (size_t j = 0; j < PARTITION_USER_WORDS; ++j)
234 {
235 pdst->data.user.data[j] = htobe32(psrc->data.user.data[j]);
236 }
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500237 pdst->checksum = details::checksum(pdst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500238 }
239
240 return out;
241}
242
Andrew Jefferyd394a782018-02-21 16:16:14 +1030243static inline void writeSizes(pnor_partition& part, size_t start, size_t end,
244 size_t blockSize)
245{
246 size_t size = end - start;
247 part.data.base = align_up(start, blockSize) / blockSize;
248 size_t sizeInBlocks = align_up(size, blockSize) / blockSize;
249 part.data.size = sizeInBlocks;
Andrew Jeffery742a1f62018-03-02 09:26:03 +1030250 part.data.actual = size;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030251}
252
253static inline void writeUserdata(pnor_partition& part, uint32_t version,
254 const std::string& data)
255{
256 std::istringstream stream(data);
257 std::string flag{};
258 auto perms = 0;
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030259 auto state = 0;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030260
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030261 MSG_DBG("Parsing ToC flags '%s'\n", data.c_str());
Andrew Jefferyd394a782018-02-21 16:16:14 +1030262 while (std::getline(stream, flag, ','))
263 {
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030264 if (flag == "")
265 continue;
266
Andrew Jefferyd394a782018-02-21 16:16:14 +1030267 if (flag == "ECC")
268 {
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030269 state |= PARTITION_ECC_PROTECTED;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030270 }
271 else if (flag == "READONLY")
272 {
273 perms |= PARTITION_READONLY;
274 }
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030275 else if (flag == "READWRITE")
276 {
277 perms &= ~PARTITION_READONLY;
278 }
Andrew Jefferyd394a782018-02-21 16:16:14 +1030279 else if (flag == "PRESERVED")
280 {
281 perms |= PARTITION_PRESERVED;
282 }
283 else if (flag == "REPROVISION")
284 {
285 perms |= PARTITION_REPROVISION;
286 }
287 else if (flag == "VOLATILE")
288 {
289 perms |= PARTITION_VOLATILE;
290 }
291 else if (flag == "CLEARECC")
292 {
293 perms |= PARTITION_CLEARECC;
294 }
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030295 else
296 {
297 MSG_INFO("Found unimplemented partition property: %s\n",
298 flag.c_str());
299 }
Andrew Jefferyd394a782018-02-21 16:16:14 +1030300 }
301
Andrew Jeffery7a85d222018-11-13 17:15:18 +1030302 // Awful hack: Detect the TOC partition and force it read-only.
303 //
304 // Note that as it stands in the caller code we populate the critical
305 // elements before the user data. These tests make doing so a requirement.
306 if (part.data.id == 0 && !part.data.base && part.data.size)
307 {
308 perms |= PARTITION_READONLY;
309 }
310
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030311 part.data.user.data[0] = state;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030312 part.data.user.data[1] = perms;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030313 part.data.user.data[1] |= version;
314}
315
316static inline void writeDefaults(pnor_partition& part)
317{
318 part.data.pid = PARENT_PATITION_ID;
319 part.data.type = PARTITION_TYPE_DATA;
320 part.data.flags = 0; // flags unused
321}
322
323static inline void writeNameAndId(pnor_partition& part, std::string&& name,
324 const std::string& id)
325{
326 name.resize(PARTITION_NAME_MAX);
327 memcpy(part.data.name, name.c_str(), sizeof(part.data.name));
328 part.data.id = std::stoul(id);
329}
330
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030331void parseTocLine(const std::string& line, size_t blockSize,
Andrew Jefferyd394a782018-02-21 16:16:14 +1030332 pnor_partition& part)
333{
334 static constexpr auto ID_MATCH = 1;
335 static constexpr auto NAME_MATCH = 2;
336 static constexpr auto START_ADDR_MATCH = 4;
337 static constexpr auto END_ADDR_MATCH = 6;
338 static constexpr auto VERSION_MATCH = 8;
339 constexpr auto versionShift = 24;
340
341 // Parse PNOR toc (table of contents) file, which has lines like :
342 // partition01=HBB,0x00010000,0x000a0000,0x80,ECC,PRESERVED, to indicate
343 // partition information
344 std::regex regex{
345 "^partition([0-9]+)=([A-Za-z0-9_]+),"
346 "(0x)?([0-9a-fA-F]+),(0x)?([0-9a-fA-F]+),(0x)?([A-Fa-f0-9]{2})",
347 std::regex::extended};
348
349 std::smatch match;
350 if (!std::regex_search(line, match, regex))
351 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030352 std::stringstream err;
353 err << "Malformed partition description: " << line.c_str() << "\n";
354 throw MalformedTocEntry(err.str());
Andrew Jefferyd394a782018-02-21 16:16:14 +1030355 }
356
357 writeNameAndId(part, match[NAME_MATCH].str(), match[ID_MATCH].str());
358 writeDefaults(part);
359
360 unsigned long start =
361 std::stoul(match[START_ADDR_MATCH].str(), nullptr, 16);
Andrew Jefferyb87aa322018-02-27 16:27:02 +1030362 if (start & (blockSize - 1))
363 {
364 MSG_ERR("Start offset 0x%lx for partition '%s' is not aligned to block "
365 "size 0x%zx\n",
366 start, match[NAME_MATCH].str().c_str(), blockSize);
367 }
368
Andrew Jefferyd394a782018-02-21 16:16:14 +1030369 unsigned long end = std::stoul(match[END_ADDR_MATCH].str(), nullptr, 16);
Andrew Jefferyb87aa322018-02-27 16:27:02 +1030370 if ((end - start) & (blockSize - 1))
371 {
372 MSG_ERR("Partition '%s' has a size 0x%lx that is not aligned to block "
373 "size 0x%zx\n",
374 match[NAME_MATCH].str().c_str(), (end - start), blockSize);
375 }
376
Andrew Jefferydb493412018-03-02 11:54:22 +1030377 if (start >= end)
378 {
379 std::stringstream err;
380 err << "Partition " << match[NAME_MATCH].str()
381 << " has an invalid range: start offset (0x" << std::hex << start
382 << " is beyond open end (0x" << std::hex << end << ")\n";
383 throw InvalidTocEntry(err.str());
384 }
Andrew Jefferyd394a782018-02-21 16:16:14 +1030385 writeSizes(part, start, end, blockSize);
386
387 // Use the shift to convert "80" to 0x80000000
388 unsigned long version = std::stoul(match[VERSION_MATCH].str(), nullptr, 16);
Andrew Jeffery7a85d222018-11-13 17:15:18 +1030389 // Note that we must have written the partition ID and sizes prior to
390 // populating the userdata. See the note about awful hacks in
391 // writeUserdata()
Andrew Jefferyd394a782018-02-21 16:16:14 +1030392 writeUserdata(part, version << versionShift, match.suffix().str());
393 part.checksum = details::checksum(part.data);
Andrew Jefferyd394a782018-02-21 16:16:14 +1030394}
395
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500396} // namespace virtual_pnor
397} // namespace openpower