blob: 376b191ff6939bf0e36722df6ff9bb8f6dd46e2f [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 Jeffery742a1f62018-03-02 09:26:03 +103025Table::Table(const struct mbox_context* ctx) :
26 szBytes(sizeof(pnor_partition_table)), numParts(0),
27 blockSize(1 << ctx->erase_size_shift), pnorSize(ctx->flash_size)
Deepak Kodihalli393821d2017-04-28 04:44:38 -050028{
Andrew Jeffery742a1f62018-03-02 09:26:03 +103029 preparePartitions(ctx);
Deepak Kodihalli393821d2017-04-28 04:44:38 -050030 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 Jeffery742a1f62018-03-02 09:26:03 +103071void Table::preparePartitions(const struct mbox_context* ctx)
Andrew Jeffery581a4f22018-02-21 15:29:38 +103072{
Andrew Jeffery742a1f62018-03-02 09:26:03 +103073 const fs::path roDir = ctx->paths.ro_loc;
74 const fs::path patchDir = ctx->paths.patch_loc;
75 fs::path tocFile = roDir / PARTITION_TOC_FILE;
Andrew Jeffery581a4f22018-02-21 15:29:38 +103076 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];
Andrew Jeffery742a1f62018-03-02 09:26:03 +103085 fs::path patch;
Andrew Jefferyfaaa71c2018-02-21 16:29:48 +103086 fs::path file;
87
Andrew Jefferyf96bd162018-02-26 13:05:00 +103088 // The ToC file presented in the vpnor squashfs looks like:
89 //
90 // version=IBM-witherspoon-ibm-OP9_v1.19_1.135
91 // 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
92 // partition00=part,0x00000000,0x00002000,00,READWRITE
93 // partition01=HBEL,0x00008000,0x0002c000,00,ECC,REPROVISION,CLEARECC,READWRITE
94 // ...
95 //
96 // As such we want to skip any lines that don't begin with 'partition'
97 if (std::string::npos == line.find("partition", 0))
Andrew Jefferyfaaa71c2018-02-21 16:29:48 +103098 {
99 continue;
100 }
101
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030102 parseTocLine(line, blockSize, part);
103
Andrew Jeffery4ca1aa52018-03-02 12:08:29 +1030104 if (numParts > 0)
105 {
106 struct pnor_partition& prev = table.partitions[numParts - 1];
107 uint32_t prev_end = prev.data.base + prev.data.size;
108
109 if (part.data.id == prev.data.id)
110 {
111 MSG_ERR("ID for previous partition '%s' at block 0x%" PRIx32
112 "matches current partition '%s' at block 0x%" PRIx32
113 ": %" PRId32 "\n",
114 prev.data.name, prev.data.base, part.data.name,
115 part.data.base, part.data.id);
116 }
117
118 if (part.data.base < prev_end)
119 {
120 std::stringstream err;
121 err << "Partition '" << part.data.name << "' start block 0x"
122 << std::hex << part.data.base << "is less than the end "
123 << "block 0x" << std::hex << prev_end << " of '"
124 << prev.data.name << "'";
125 throw InvalidTocEntry(err.str());
126 }
127 }
128
Andrew Jeffery742a1f62018-03-02 09:26:03 +1030129 file = roDir / part.data.name;
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030130 if (!fs::exists(file))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500131 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030132 std::stringstream err;
133 err << "Partition file " << file.native() << " does not exist";
134 throw InvalidTocEntry(err.str());
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500135 }
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030136
Andrew Jeffery742a1f62018-03-02 09:26:03 +1030137 patch = patchDir / part.data.name;
138 if (fs::is_regular_file(patch))
139 {
140 const size_t size = part.data.size * blockSize;
141 part.data.actual =
142 std::min(size, static_cast<size_t>(fs::file_size(patch)));
143 }
144
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030145 ++numParts;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500146 }
147}
148
149const pnor_partition& Table::partition(size_t offset) const
150{
151 const decltype(auto) table = getNativeTable();
Andrew Jefferybd38c212018-02-27 17:44:33 +1030152 size_t blockOffset = offset / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500153
154 for (decltype(numParts) i{}; i < numParts; ++i)
155 {
Andrew Jefferycd928512018-02-28 22:33:48 +1030156 const struct pnor_partition& part = table.partitions[i];
157 if ((blockOffset >= part.data.base) &&
158 (blockOffset < (part.data.base + part.data.size)))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500159 {
Andrew Jefferycd928512018-02-28 22:33:48 +1030160 return part;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500161 }
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030162
163 /* Are we in a hole between partitions? */
164 if (blockOffset < part.data.base)
165 {
166 throw UnmappedOffset(offset, part.data.base * blockSize);
167 }
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500168 }
169
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030170 throw UnmappedOffset(offset, pnorSize);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500171}
172
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500173const pnor_partition& Table::partition(const std::string& name) const
174{
175 const decltype(auto) table = getNativeTable();
176
177 for (decltype(numParts) i{}; i < numParts; ++i)
178 {
179 if (name == table.partitions[i].data.name)
180 {
181 return table.partitions[i];
182 }
183 }
184
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030185 std::stringstream err;
186 err << "Partition " << name << " is not listed in the table of contents";
187 throw UnknownPartition(err.str());
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500188}
189
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500190} // namespace partition
191
192PartitionTable endianFixup(const PartitionTable& in)
193{
194 PartitionTable out;
195 out.resize(in.size());
196 auto src = reinterpret_cast<const pnor_partition_table*>(in.data());
197 auto dst = reinterpret_cast<pnor_partition_table*>(out.data());
198
199 dst->data.magic = htobe32(src->data.magic);
200 dst->data.version = htobe32(src->data.version);
201 dst->data.size = htobe32(src->data.size);
202 dst->data.entry_size = htobe32(src->data.entry_size);
203 dst->data.entry_count = htobe32(src->data.entry_count);
204 dst->data.block_size = htobe32(src->data.block_size);
205 dst->data.block_count = htobe32(src->data.block_count);
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500206 dst->checksum = details::checksum(dst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500207
208 for (decltype(src->data.entry_count) i{}; i < src->data.entry_count; ++i)
209 {
210 auto psrc = &src->partitions[i];
211 auto pdst = &dst->partitions[i];
212 strncpy(pdst->data.name, psrc->data.name, PARTITION_NAME_MAX);
213 // Just to be safe
214 pdst->data.name[PARTITION_NAME_MAX] = '\0';
215 pdst->data.base = htobe32(psrc->data.base);
216 pdst->data.size = htobe32(psrc->data.size);
217 pdst->data.pid = htobe32(psrc->data.pid);
218 pdst->data.id = htobe32(psrc->data.id);
219 pdst->data.type = htobe32(psrc->data.type);
220 pdst->data.flags = htobe32(psrc->data.flags);
221 pdst->data.actual = htobe32(psrc->data.actual);
222 for (size_t j = 0; j < PARTITION_USER_WORDS; ++j)
223 {
224 pdst->data.user.data[j] = htobe32(psrc->data.user.data[j]);
225 }
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500226 pdst->checksum = details::checksum(pdst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500227 }
228
229 return out;
230}
231
Andrew Jefferyd394a782018-02-21 16:16:14 +1030232static inline void writeSizes(pnor_partition& part, size_t start, size_t end,
233 size_t blockSize)
234{
235 size_t size = end - start;
236 part.data.base = align_up(start, blockSize) / blockSize;
237 size_t sizeInBlocks = align_up(size, blockSize) / blockSize;
238 part.data.size = sizeInBlocks;
Andrew Jeffery742a1f62018-03-02 09:26:03 +1030239 part.data.actual = size;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030240}
241
242static inline void writeUserdata(pnor_partition& part, uint32_t version,
243 const std::string& data)
244{
245 std::istringstream stream(data);
246 std::string flag{};
247 auto perms = 0;
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030248 auto state = 0;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030249
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030250 MSG_DBG("Parsing ToC flags '%s'\n", data.c_str());
Andrew Jefferyd394a782018-02-21 16:16:14 +1030251 while (std::getline(stream, flag, ','))
252 {
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030253 if (flag == "")
254 continue;
255
Andrew Jefferyd394a782018-02-21 16:16:14 +1030256 if (flag == "ECC")
257 {
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030258 state |= PARTITION_ECC_PROTECTED;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030259 }
260 else if (flag == "READONLY")
261 {
262 perms |= PARTITION_READONLY;
263 }
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030264 else if (flag == "READWRITE")
265 {
266 perms &= ~PARTITION_READONLY;
267 }
Andrew Jefferyd394a782018-02-21 16:16:14 +1030268 else if (flag == "PRESERVED")
269 {
270 perms |= PARTITION_PRESERVED;
271 }
272 else if (flag == "REPROVISION")
273 {
274 perms |= PARTITION_REPROVISION;
275 }
276 else if (flag == "VOLATILE")
277 {
278 perms |= PARTITION_VOLATILE;
279 }
280 else if (flag == "CLEARECC")
281 {
282 perms |= PARTITION_CLEARECC;
283 }
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030284 else
285 {
286 MSG_INFO("Found unimplemented partition property: %s\n",
287 flag.c_str());
288 }
Andrew Jefferyd394a782018-02-21 16:16:14 +1030289 }
290
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030291 part.data.user.data[0] = state;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030292 part.data.user.data[1] = perms;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030293 part.data.user.data[1] |= version;
294}
295
296static inline void writeDefaults(pnor_partition& part)
297{
298 part.data.pid = PARENT_PATITION_ID;
299 part.data.type = PARTITION_TYPE_DATA;
300 part.data.flags = 0; // flags unused
301}
302
303static inline void writeNameAndId(pnor_partition& part, std::string&& name,
304 const std::string& id)
305{
306 name.resize(PARTITION_NAME_MAX);
307 memcpy(part.data.name, name.c_str(), sizeof(part.data.name));
308 part.data.id = std::stoul(id);
309}
310
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030311void parseTocLine(const std::string& line, size_t blockSize,
Andrew Jefferyd394a782018-02-21 16:16:14 +1030312 pnor_partition& part)
313{
314 static constexpr auto ID_MATCH = 1;
315 static constexpr auto NAME_MATCH = 2;
316 static constexpr auto START_ADDR_MATCH = 4;
317 static constexpr auto END_ADDR_MATCH = 6;
318 static constexpr auto VERSION_MATCH = 8;
319 constexpr auto versionShift = 24;
320
321 // Parse PNOR toc (table of contents) file, which has lines like :
322 // partition01=HBB,0x00010000,0x000a0000,0x80,ECC,PRESERVED, to indicate
323 // partition information
324 std::regex regex{
325 "^partition([0-9]+)=([A-Za-z0-9_]+),"
326 "(0x)?([0-9a-fA-F]+),(0x)?([0-9a-fA-F]+),(0x)?([A-Fa-f0-9]{2})",
327 std::regex::extended};
328
329 std::smatch match;
330 if (!std::regex_search(line, match, regex))
331 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030332 std::stringstream err;
333 err << "Malformed partition description: " << line.c_str() << "\n";
334 throw MalformedTocEntry(err.str());
Andrew Jefferyd394a782018-02-21 16:16:14 +1030335 }
336
337 writeNameAndId(part, match[NAME_MATCH].str(), match[ID_MATCH].str());
338 writeDefaults(part);
339
340 unsigned long start =
341 std::stoul(match[START_ADDR_MATCH].str(), nullptr, 16);
Andrew Jefferyb87aa322018-02-27 16:27:02 +1030342 if (start & (blockSize - 1))
343 {
344 MSG_ERR("Start offset 0x%lx for partition '%s' is not aligned to block "
345 "size 0x%zx\n",
346 start, match[NAME_MATCH].str().c_str(), blockSize);
347 }
348
Andrew Jefferyd394a782018-02-21 16:16:14 +1030349 unsigned long end = std::stoul(match[END_ADDR_MATCH].str(), nullptr, 16);
Andrew Jefferyb87aa322018-02-27 16:27:02 +1030350 if ((end - start) & (blockSize - 1))
351 {
352 MSG_ERR("Partition '%s' has a size 0x%lx that is not aligned to block "
353 "size 0x%zx\n",
354 match[NAME_MATCH].str().c_str(), (end - start), blockSize);
355 }
356
Andrew Jefferydb493412018-03-02 11:54:22 +1030357 if (start >= end)
358 {
359 std::stringstream err;
360 err << "Partition " << match[NAME_MATCH].str()
361 << " has an invalid range: start offset (0x" << std::hex << start
362 << " is beyond open end (0x" << std::hex << end << ")\n";
363 throw InvalidTocEntry(err.str());
364 }
Andrew Jefferyd394a782018-02-21 16:16:14 +1030365 writeSizes(part, start, end, blockSize);
366
367 // Use the shift to convert "80" to 0x80000000
368 unsigned long version = std::stoul(match[VERSION_MATCH].str(), nullptr, 16);
369 writeUserdata(part, version << versionShift, match.suffix().str());
370 part.checksum = details::checksum(part.data);
Andrew Jefferyd394a782018-02-21 16:16:14 +1030371}
372
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500373} // namespace virtual_pnor
374} // namespace openpower