blob: aa0b2fb189925e401b7974addc114c217bada6b2 [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
17#include "common.h"
18#include "mboxd.h"
Deepak Kodihalli393821d2017-04-28 04:44:38 -050019
20namespace openpower
21{
22namespace virtual_pnor
23{
24
Deepak Kodihalli8a899692017-07-11 23:17:19 -050025using namespace phosphor::logging;
26using namespace sdbusplus::xyz::openbmc_project::Common::Error;
27
Deepak Kodihalli393821d2017-04-28 04:44:38 -050028namespace partition
29{
Deepak Kodihalli393821d2017-04-28 04:44:38 -050030
Andrew Jeffery742a1f62018-03-02 09:26:03 +103031Table::Table(const struct mbox_context* ctx) :
32 szBytes(sizeof(pnor_partition_table)), numParts(0),
33 blockSize(1 << ctx->erase_size_shift), pnorSize(ctx->flash_size)
Deepak Kodihalli393821d2017-04-28 04:44:38 -050034{
Andrew Jeffery742a1f62018-03-02 09:26:03 +103035 preparePartitions(ctx);
Deepak Kodihalli393821d2017-04-28 04:44:38 -050036 prepareHeader();
37 hostTbl = endianFixup(tbl);
38}
39
40void Table::prepareHeader()
41{
42 decltype(auto) table = getNativeTable();
43 table.data.magic = PARTITION_HEADER_MAGIC;
44 table.data.version = PARTITION_VERSION_1;
Andrew Jeffery7f9c3432018-03-01 12:07:13 +103045 table.data.size = blocks();
Deepak Kodihalli393821d2017-04-28 04:44:38 -050046 table.data.entry_size = sizeof(pnor_partition);
47 table.data.entry_count = numParts;
Deepak Kodihallid1d79302017-07-11 23:01:39 -050048 table.data.block_size = blockSize;
49 table.data.block_count = pnorSize / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050050 table.checksum = details::checksum(table.data);
51}
52
53inline void Table::allocateMemory(const fs::path& tocFile)
54{
55 size_t num = 0;
56 std::string line;
57 std::ifstream file(tocFile.c_str());
58
59 // Find number of lines in partition file - this will help
60 // determine the number of partitions and hence also how much
61 // memory to allocate for the partitions array.
62 // The actual number of partitions may turn out to be lesser than this,
63 // in case of errors.
64 while (std::getline(file, line))
65 {
66 // Check if line starts with "partition"
67 if (std::string::npos != line.find("partition", 0))
68 {
69 ++num;
70 }
71 }
72
Andrew Jeffery7f9c3432018-03-01 12:07:13 +103073 szBytes = sizeof(pnor_partition_table) + (num * sizeof(pnor_partition));
74 tbl.resize(capacity());
Deepak Kodihalli393821d2017-04-28 04:44:38 -050075}
76
Andrew Jeffery742a1f62018-03-02 09:26:03 +103077void Table::preparePartitions(const struct mbox_context* ctx)
Andrew Jeffery581a4f22018-02-21 15:29:38 +103078{
Andrew Jeffery742a1f62018-03-02 09:26:03 +103079 const fs::path roDir = ctx->paths.ro_loc;
80 const fs::path patchDir = ctx->paths.patch_loc;
81 fs::path tocFile = roDir / PARTITION_TOC_FILE;
Andrew Jeffery581a4f22018-02-21 15:29:38 +103082 allocateMemory(tocFile);
83
84 std::ifstream file(tocFile.c_str());
85 std::string line;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050086 decltype(auto) table = getNativeTable();
87
88 while (std::getline(file, line))
89 {
Andrew Jefferyfaaa71c2018-02-21 16:29:48 +103090 pnor_partition& part = table.partitions[numParts];
Andrew Jeffery742a1f62018-03-02 09:26:03 +103091 fs::path patch;
Andrew Jefferyfaaa71c2018-02-21 16:29:48 +103092 fs::path file;
93
Andrew Jefferyf96bd162018-02-26 13:05:00 +103094 // The ToC file presented in the vpnor squashfs looks like:
95 //
96 // version=IBM-witherspoon-ibm-OP9_v1.19_1.135
97 // 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
98 // partition00=part,0x00000000,0x00002000,00,READWRITE
99 // partition01=HBEL,0x00008000,0x0002c000,00,ECC,REPROVISION,CLEARECC,READWRITE
100 // ...
101 //
102 // As such we want to skip any lines that don't begin with 'partition'
103 if (std::string::npos == line.find("partition", 0))
Andrew Jefferyfaaa71c2018-02-21 16:29:48 +1030104 {
105 continue;
106 }
107
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030108 parseTocLine(line, blockSize, part);
109
Andrew Jeffery4ca1aa52018-03-02 12:08:29 +1030110 if (numParts > 0)
111 {
112 struct pnor_partition& prev = table.partitions[numParts - 1];
113 uint32_t prev_end = prev.data.base + prev.data.size;
114
115 if (part.data.id == prev.data.id)
116 {
117 MSG_ERR("ID for previous partition '%s' at block 0x%" PRIx32
118 "matches current partition '%s' at block 0x%" PRIx32
119 ": %" PRId32 "\n",
120 prev.data.name, prev.data.base, part.data.name,
121 part.data.base, part.data.id);
122 }
123
124 if (part.data.base < prev_end)
125 {
126 std::stringstream err;
127 err << "Partition '" << part.data.name << "' start block 0x"
128 << std::hex << part.data.base << "is less than the end "
129 << "block 0x" << std::hex << prev_end << " of '"
130 << prev.data.name << "'";
131 throw InvalidTocEntry(err.str());
132 }
133 }
134
Andrew Jeffery742a1f62018-03-02 09:26:03 +1030135 file = roDir / part.data.name;
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030136 if (!fs::exists(file))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500137 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030138 std::stringstream err;
139 err << "Partition file " << file.native() << " does not exist";
140 throw InvalidTocEntry(err.str());
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500141 }
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030142
Andrew Jeffery742a1f62018-03-02 09:26:03 +1030143 patch = patchDir / part.data.name;
144 if (fs::is_regular_file(patch))
145 {
146 const size_t size = part.data.size * blockSize;
147 part.data.actual =
148 std::min(size, static_cast<size_t>(fs::file_size(patch)));
149 }
150
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030151 ++numParts;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500152 }
153}
154
155const pnor_partition& Table::partition(size_t offset) const
156{
157 const decltype(auto) table = getNativeTable();
Andrew Jefferybd38c212018-02-27 17:44:33 +1030158 size_t blockOffset = offset / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500159
160 for (decltype(numParts) i{}; i < numParts; ++i)
161 {
Andrew Jefferycd928512018-02-28 22:33:48 +1030162 const struct pnor_partition& part = table.partitions[i];
Andrew Jeffery1a3f8432018-03-02 10:18:02 +1030163 size_t len = part.data.size;
164
Andrew Jefferycd928512018-02-28 22:33:48 +1030165 if ((blockOffset >= part.data.base) &&
Andrew Jeffery1a3f8432018-03-02 10:18:02 +1030166 (blockOffset < (part.data.base + len)))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500167 {
Andrew Jefferycd928512018-02-28 22:33:48 +1030168 return part;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500169 }
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030170
171 /* Are we in a hole between partitions? */
172 if (blockOffset < part.data.base)
173 {
174 throw UnmappedOffset(offset, part.data.base * blockSize);
175 }
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500176 }
177
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030178 throw UnmappedOffset(offset, pnorSize);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500179}
180
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500181const pnor_partition& Table::partition(const std::string& name) const
182{
183 const decltype(auto) table = getNativeTable();
184
185 for (decltype(numParts) i{}; i < numParts; ++i)
186 {
187 if (name == table.partitions[i].data.name)
188 {
189 return table.partitions[i];
190 }
191 }
192
Andrew Jefferyae1edb92018-02-28 23:16:48 +1030193 std::stringstream err;
194 err << "Partition " << name << " is not listed in the table of contents";
195 throw UnknownPartition(err.str());
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500196}
197
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500198} // namespace partition
199
200PartitionTable endianFixup(const PartitionTable& in)
201{
202 PartitionTable out;
203 out.resize(in.size());
204 auto src = reinterpret_cast<const pnor_partition_table*>(in.data());
205 auto dst = reinterpret_cast<pnor_partition_table*>(out.data());
206
207 dst->data.magic = htobe32(src->data.magic);
208 dst->data.version = htobe32(src->data.version);
209 dst->data.size = htobe32(src->data.size);
210 dst->data.entry_size = htobe32(src->data.entry_size);
211 dst->data.entry_count = htobe32(src->data.entry_count);
212 dst->data.block_size = htobe32(src->data.block_size);
213 dst->data.block_count = htobe32(src->data.block_count);
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500214 dst->checksum = details::checksum(dst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500215
216 for (decltype(src->data.entry_count) i{}; i < src->data.entry_count; ++i)
217 {
218 auto psrc = &src->partitions[i];
219 auto pdst = &dst->partitions[i];
220 strncpy(pdst->data.name, psrc->data.name, PARTITION_NAME_MAX);
221 // Just to be safe
222 pdst->data.name[PARTITION_NAME_MAX] = '\0';
223 pdst->data.base = htobe32(psrc->data.base);
224 pdst->data.size = htobe32(psrc->data.size);
225 pdst->data.pid = htobe32(psrc->data.pid);
226 pdst->data.id = htobe32(psrc->data.id);
227 pdst->data.type = htobe32(psrc->data.type);
228 pdst->data.flags = htobe32(psrc->data.flags);
229 pdst->data.actual = htobe32(psrc->data.actual);
230 for (size_t j = 0; j < PARTITION_USER_WORDS; ++j)
231 {
232 pdst->data.user.data[j] = htobe32(psrc->data.user.data[j]);
233 }
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500234 pdst->checksum = details::checksum(pdst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500235 }
236
237 return out;
238}
239
Andrew Jefferyd394a782018-02-21 16:16:14 +1030240static inline void writeSizes(pnor_partition& part, size_t start, size_t end,
241 size_t blockSize)
242{
243 size_t size = end - start;
244 part.data.base = align_up(start, blockSize) / blockSize;
245 size_t sizeInBlocks = align_up(size, blockSize) / blockSize;
246 part.data.size = sizeInBlocks;
Andrew Jeffery742a1f62018-03-02 09:26:03 +1030247 part.data.actual = size;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030248}
249
250static inline void writeUserdata(pnor_partition& part, uint32_t version,
251 const std::string& data)
252{
253 std::istringstream stream(data);
254 std::string flag{};
255 auto perms = 0;
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030256 auto state = 0;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030257
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030258 MSG_DBG("Parsing ToC flags '%s'\n", data.c_str());
Andrew Jefferyd394a782018-02-21 16:16:14 +1030259 while (std::getline(stream, flag, ','))
260 {
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030261 if (flag == "")
262 continue;
263
Andrew Jefferyd394a782018-02-21 16:16:14 +1030264 if (flag == "ECC")
265 {
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030266 state |= PARTITION_ECC_PROTECTED;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030267 }
268 else if (flag == "READONLY")
269 {
270 perms |= PARTITION_READONLY;
271 }
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030272 else if (flag == "READWRITE")
273 {
274 perms &= ~PARTITION_READONLY;
275 }
Andrew Jefferyd394a782018-02-21 16:16:14 +1030276 else if (flag == "PRESERVED")
277 {
278 perms |= PARTITION_PRESERVED;
279 }
280 else if (flag == "REPROVISION")
281 {
282 perms |= PARTITION_REPROVISION;
283 }
284 else if (flag == "VOLATILE")
285 {
286 perms |= PARTITION_VOLATILE;
287 }
288 else if (flag == "CLEARECC")
289 {
290 perms |= PARTITION_CLEARECC;
291 }
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030292 else
293 {
294 MSG_INFO("Found unimplemented partition property: %s\n",
295 flag.c_str());
296 }
Andrew Jefferyd394a782018-02-21 16:16:14 +1030297 }
298
Andrew Jeffery0f9fbd62018-03-02 12:00:42 +1030299 part.data.user.data[0] = state;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030300 part.data.user.data[1] = perms;
Andrew Jefferyd394a782018-02-21 16:16:14 +1030301 part.data.user.data[1] |= version;
302}
303
304static inline void writeDefaults(pnor_partition& part)
305{
306 part.data.pid = PARENT_PATITION_ID;
307 part.data.type = PARTITION_TYPE_DATA;
308 part.data.flags = 0; // flags unused
309}
310
311static inline void writeNameAndId(pnor_partition& part, std::string&& name,
312 const std::string& id)
313{
314 name.resize(PARTITION_NAME_MAX);
315 memcpy(part.data.name, name.c_str(), sizeof(part.data.name));
316 part.data.id = std::stoul(id);
317}
318
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030319void parseTocLine(const std::string& line, size_t blockSize,
Andrew Jefferyd394a782018-02-21 16:16:14 +1030320 pnor_partition& part)
321{
322 static constexpr auto ID_MATCH = 1;
323 static constexpr auto NAME_MATCH = 2;
324 static constexpr auto START_ADDR_MATCH = 4;
325 static constexpr auto END_ADDR_MATCH = 6;
326 static constexpr auto VERSION_MATCH = 8;
327 constexpr auto versionShift = 24;
328
329 // Parse PNOR toc (table of contents) file, which has lines like :
330 // partition01=HBB,0x00010000,0x000a0000,0x80,ECC,PRESERVED, to indicate
331 // partition information
332 std::regex regex{
333 "^partition([0-9]+)=([A-Za-z0-9_]+),"
334 "(0x)?([0-9a-fA-F]+),(0x)?([0-9a-fA-F]+),(0x)?([A-Fa-f0-9]{2})",
335 std::regex::extended};
336
337 std::smatch match;
338 if (!std::regex_search(line, match, regex))
339 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030340 std::stringstream err;
341 err << "Malformed partition description: " << line.c_str() << "\n";
342 throw MalformedTocEntry(err.str());
Andrew Jefferyd394a782018-02-21 16:16:14 +1030343 }
344
345 writeNameAndId(part, match[NAME_MATCH].str(), match[ID_MATCH].str());
346 writeDefaults(part);
347
348 unsigned long start =
349 std::stoul(match[START_ADDR_MATCH].str(), nullptr, 16);
Andrew Jefferyb87aa322018-02-27 16:27:02 +1030350 if (start & (blockSize - 1))
351 {
352 MSG_ERR("Start offset 0x%lx for partition '%s' is not aligned to block "
353 "size 0x%zx\n",
354 start, match[NAME_MATCH].str().c_str(), blockSize);
355 }
356
Andrew Jefferyd394a782018-02-21 16:16:14 +1030357 unsigned long end = std::stoul(match[END_ADDR_MATCH].str(), nullptr, 16);
Andrew Jefferyb87aa322018-02-27 16:27:02 +1030358 if ((end - start) & (blockSize - 1))
359 {
360 MSG_ERR("Partition '%s' has a size 0x%lx that is not aligned to block "
361 "size 0x%zx\n",
362 match[NAME_MATCH].str().c_str(), (end - start), blockSize);
363 }
364
Andrew Jefferydb493412018-03-02 11:54:22 +1030365 if (start >= end)
366 {
367 std::stringstream err;
368 err << "Partition " << match[NAME_MATCH].str()
369 << " has an invalid range: start offset (0x" << std::hex << start
370 << " is beyond open end (0x" << std::hex << end << ")\n";
371 throw InvalidTocEntry(err.str());
372 }
Andrew Jefferyd394a782018-02-21 16:16:14 +1030373 writeSizes(part, start, end, blockSize);
374
375 // Use the shift to convert "80" to 0x80000000
376 unsigned long version = std::stoul(match[VERSION_MATCH].str(), nullptr, 16);
377 writeUserdata(part, version << versionShift, match.suffix().str());
378 part.checksum = details::checksum(part.data);
Andrew Jefferyd394a782018-02-21 16:16:14 +1030379}
380
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500381} // namespace virtual_pnor
382} // namespace openpower