blob: cf59c41c388bf106e2b2e638762be8eaa20b8727 [file] [log] [blame]
Andrew Jefferyacee6832018-02-21 22:17:08 +10301/*
2 * Mailbox Daemon Implementation
3 *
4 * Copyright 2018 IBM
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
Deepak Kodihalli393821d2017-04-28 04:44:38 -050019#include "pnor_partition_table.hpp"
20#include "common.h"
21#include "config.h"
Deepak Kodihalli8a899692017-07-11 23:17:19 -050022#include "xyz/openbmc_project/Common/error.hpp"
23#include <phosphor-logging/elog-errors.hpp>
Deepak Kodihalli393821d2017-04-28 04:44:38 -050024#include <syslog.h>
25#include <endian.h>
26#include <regex>
27#include <fstream>
28#include <algorithm>
29
30namespace openpower
31{
32namespace virtual_pnor
33{
34
Deepak Kodihalli8a899692017-07-11 23:17:19 -050035using namespace phosphor::logging;
36using namespace sdbusplus::xyz::openbmc_project::Common::Error;
37
Deepak Kodihalli393821d2017-04-28 04:44:38 -050038namespace partition
39{
Deepak Kodihalli393821d2017-04-28 04:44:38 -050040
Andrew Jefferyf34db312018-03-09 15:27:03 +103041Table::Table(fs::path&& directory, size_t blockSize, size_t pnorSize) :
Andrew Jeffery7f9c3432018-03-01 12:07:13 +103042 szBytes(sizeof(pnor_partition_table)), directory(std::move(directory)),
43 numParts(0), blockSize(blockSize), pnorSize(pnorSize)
Deepak Kodihalli393821d2017-04-28 04:44:38 -050044{
45 preparePartitions();
46 prepareHeader();
47 hostTbl = endianFixup(tbl);
48}
49
50void Table::prepareHeader()
51{
52 decltype(auto) table = getNativeTable();
53 table.data.magic = PARTITION_HEADER_MAGIC;
54 table.data.version = PARTITION_VERSION_1;
Andrew Jeffery7f9c3432018-03-01 12:07:13 +103055 table.data.size = blocks();
Deepak Kodihalli393821d2017-04-28 04:44:38 -050056 table.data.entry_size = sizeof(pnor_partition);
57 table.data.entry_count = numParts;
Deepak Kodihallid1d79302017-07-11 23:01:39 -050058 table.data.block_size = blockSize;
59 table.data.block_count = pnorSize / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050060 table.checksum = details::checksum(table.data);
61}
62
63inline void Table::allocateMemory(const fs::path& tocFile)
64{
65 size_t num = 0;
66 std::string line;
67 std::ifstream file(tocFile.c_str());
68
69 // Find number of lines in partition file - this will help
70 // determine the number of partitions and hence also how much
71 // memory to allocate for the partitions array.
72 // The actual number of partitions may turn out to be lesser than this,
73 // in case of errors.
74 while (std::getline(file, line))
75 {
76 // Check if line starts with "partition"
77 if (std::string::npos != line.find("partition", 0))
78 {
79 ++num;
80 }
81 }
82
Andrew Jeffery7f9c3432018-03-01 12:07:13 +103083 szBytes = sizeof(pnor_partition_table) + (num * sizeof(pnor_partition));
84 tbl.resize(capacity());
Deepak Kodihalli393821d2017-04-28 04:44:38 -050085}
86
Andrew Jeffery581a4f22018-02-21 15:29:38 +103087void Table::preparePartitions()
88{
89 fs::path tocFile = directory;
90 tocFile /= PARTITION_TOC_FILE;
91 allocateMemory(tocFile);
92
93 std::ifstream file(tocFile.c_str());
94 std::string line;
Deepak Kodihalli393821d2017-04-28 04:44:38 -050095 decltype(auto) table = getNativeTable();
96
97 while (std::getline(file, line))
98 {
Andrew Jefferyfaaa71c2018-02-21 16:29:48 +103099 pnor_partition& part = table.partitions[numParts];
100 fs::path file;
101
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030102 // The ToC file presented in the vpnor squashfs looks like:
103 //
104 // version=IBM-witherspoon-ibm-OP9_v1.19_1.135
105 // 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
106 // partition00=part,0x00000000,0x00002000,00,READWRITE
107 // partition01=HBEL,0x00008000,0x0002c000,00,ECC,REPROVISION,CLEARECC,READWRITE
108 // ...
109 //
110 // As such we want to skip any lines that don't begin with 'partition'
111 if (std::string::npos == line.find("partition", 0))
Andrew Jefferyfaaa71c2018-02-21 16:29:48 +1030112 {
113 continue;
114 }
115
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030116 parseTocLine(line, blockSize, part);
117
118 file = directory / part.data.name;
119 if (!fs::exists(file))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500120 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030121 std::stringstream err;
122 err << "Partition file " << file.native() << " does not exist";
123 throw InvalidTocEntry(err.str());
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500124 }
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030125
126 ++numParts;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500127 }
128}
129
130const pnor_partition& Table::partition(size_t offset) const
131{
132 const decltype(auto) table = getNativeTable();
Deepak Kodihallid1d79302017-07-11 23:01:39 -0500133 size_t offt = offset / blockSize;
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500134
135 for (decltype(numParts) i{}; i < numParts; ++i)
136 {
137 if ((offt >= table.partitions[i].data.base) &&
Andrew Jefferyf34db312018-03-09 15:27:03 +1030138 (offt <
139 (table.partitions[i].data.base + table.partitions[i].data.size)))
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500140 {
141 return table.partitions[i];
142 }
143 }
144
Andrew Jefferye8a79ff2018-02-27 10:56:32 +1030145 MSG_ERR("Partition corresponding to offset 0x%zx not found", offset);
Jayanth Othayoth59ce0992017-10-27 00:45:53 -0500146 elog<InternalFailure>();
147
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500148 static pnor_partition p{};
149 return p;
150}
151
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500152const pnor_partition& Table::partition(const std::string& name) const
153{
154 const decltype(auto) table = getNativeTable();
155
156 for (decltype(numParts) i{}; i < numParts; ++i)
157 {
158 if (name == table.partitions[i].data.name)
159 {
160 return table.partitions[i];
161 }
162 }
163
Andrew Jefferye8a79ff2018-02-27 10:56:32 +1030164 MSG_ERR("Partition '%s' not found", name.c_str());
Deepak Kodihalli8a899692017-07-11 23:17:19 -0500165 elog<InternalFailure>();
166 static pnor_partition p{};
167 return p;
168}
169
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500170} // namespace partition
171
172PartitionTable endianFixup(const PartitionTable& in)
173{
174 PartitionTable out;
175 out.resize(in.size());
176 auto src = reinterpret_cast<const pnor_partition_table*>(in.data());
177 auto dst = reinterpret_cast<pnor_partition_table*>(out.data());
178
179 dst->data.magic = htobe32(src->data.magic);
180 dst->data.version = htobe32(src->data.version);
181 dst->data.size = htobe32(src->data.size);
182 dst->data.entry_size = htobe32(src->data.entry_size);
183 dst->data.entry_count = htobe32(src->data.entry_count);
184 dst->data.block_size = htobe32(src->data.block_size);
185 dst->data.block_count = htobe32(src->data.block_count);
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500186 dst->checksum = details::checksum(dst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500187
188 for (decltype(src->data.entry_count) i{}; i < src->data.entry_count; ++i)
189 {
190 auto psrc = &src->partitions[i];
191 auto pdst = &dst->partitions[i];
192 strncpy(pdst->data.name, psrc->data.name, PARTITION_NAME_MAX);
193 // Just to be safe
194 pdst->data.name[PARTITION_NAME_MAX] = '\0';
195 pdst->data.base = htobe32(psrc->data.base);
196 pdst->data.size = htobe32(psrc->data.size);
197 pdst->data.pid = htobe32(psrc->data.pid);
198 pdst->data.id = htobe32(psrc->data.id);
199 pdst->data.type = htobe32(psrc->data.type);
200 pdst->data.flags = htobe32(psrc->data.flags);
201 pdst->data.actual = htobe32(psrc->data.actual);
202 for (size_t j = 0; j < PARTITION_USER_WORDS; ++j)
203 {
204 pdst->data.user.data[j] = htobe32(psrc->data.user.data[j]);
205 }
Deepak Kodihallib9cdcd22017-07-12 08:14:48 -0500206 pdst->checksum = details::checksum(pdst->data);
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500207 }
208
209 return out;
210}
211
Andrew Jefferyd394a782018-02-21 16:16:14 +1030212static inline void writeSizes(pnor_partition& part, size_t start, size_t end,
213 size_t blockSize)
214{
215 size_t size = end - start;
216 part.data.base = align_up(start, blockSize) / blockSize;
217 size_t sizeInBlocks = align_up(size, blockSize) / blockSize;
218 part.data.size = sizeInBlocks;
219
220 // If a a patch partition file exists, populate actual size with its file
221 // size if it is smaller than the total size.
222 fs::path patchFile(PARTITION_FILES_PATCH_LOC);
223 patchFile /= part.data.name;
224 if (fs::is_regular_file(patchFile))
225 {
226 part.data.actual =
227 std::min(size, static_cast<size_t>(fs::file_size(patchFile)));
228 }
229 else
230 {
231 part.data.actual = size;
232 }
233}
234
235static inline void writeUserdata(pnor_partition& part, uint32_t version,
236 const std::string& data)
237{
238 std::istringstream stream(data);
239 std::string flag{};
240 auto perms = 0;
241
242 while (std::getline(stream, flag, ','))
243 {
244 if (flag == "ECC")
245 {
246 part.data.user.data[0] = PARTITION_ECC_PROTECTED;
247 }
248 else if (flag == "READONLY")
249 {
250 perms |= PARTITION_READONLY;
251 }
252 else if (flag == "PRESERVED")
253 {
254 perms |= PARTITION_PRESERVED;
255 }
256 else if (flag == "REPROVISION")
257 {
258 perms |= PARTITION_REPROVISION;
259 }
260 else if (flag == "VOLATILE")
261 {
262 perms |= PARTITION_VOLATILE;
263 }
264 else if (flag == "CLEARECC")
265 {
266 perms |= PARTITION_CLEARECC;
267 }
268 }
269
270 part.data.user.data[1] = perms;
271
272 part.data.user.data[1] |= version;
273}
274
275static inline void writeDefaults(pnor_partition& part)
276{
277 part.data.pid = PARENT_PATITION_ID;
278 part.data.type = PARTITION_TYPE_DATA;
279 part.data.flags = 0; // flags unused
280}
281
282static inline void writeNameAndId(pnor_partition& part, std::string&& name,
283 const std::string& id)
284{
285 name.resize(PARTITION_NAME_MAX);
286 memcpy(part.data.name, name.c_str(), sizeof(part.data.name));
287 part.data.id = std::stoul(id);
288}
289
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030290void parseTocLine(const std::string& line, size_t blockSize,
Andrew Jefferyd394a782018-02-21 16:16:14 +1030291 pnor_partition& part)
292{
293 static constexpr auto ID_MATCH = 1;
294 static constexpr auto NAME_MATCH = 2;
295 static constexpr auto START_ADDR_MATCH = 4;
296 static constexpr auto END_ADDR_MATCH = 6;
297 static constexpr auto VERSION_MATCH = 8;
298 constexpr auto versionShift = 24;
299
300 // Parse PNOR toc (table of contents) file, which has lines like :
301 // partition01=HBB,0x00010000,0x000a0000,0x80,ECC,PRESERVED, to indicate
302 // partition information
303 std::regex regex{
304 "^partition([0-9]+)=([A-Za-z0-9_]+),"
305 "(0x)?([0-9a-fA-F]+),(0x)?([0-9a-fA-F]+),(0x)?([A-Fa-f0-9]{2})",
306 std::regex::extended};
307
308 std::smatch match;
309 if (!std::regex_search(line, match, regex))
310 {
Andrew Jefferyf96bd162018-02-26 13:05:00 +1030311 std::stringstream err;
312 err << "Malformed partition description: " << line.c_str() << "\n";
313 throw MalformedTocEntry(err.str());
Andrew Jefferyd394a782018-02-21 16:16:14 +1030314 }
315
316 writeNameAndId(part, match[NAME_MATCH].str(), match[ID_MATCH].str());
317 writeDefaults(part);
318
319 unsigned long start =
320 std::stoul(match[START_ADDR_MATCH].str(), nullptr, 16);
Andrew Jefferyb87aa322018-02-27 16:27:02 +1030321 if (start & (blockSize - 1))
322 {
323 MSG_ERR("Start offset 0x%lx for partition '%s' is not aligned to block "
324 "size 0x%zx\n",
325 start, match[NAME_MATCH].str().c_str(), blockSize);
326 }
327
Andrew Jefferyd394a782018-02-21 16:16:14 +1030328 unsigned long end = std::stoul(match[END_ADDR_MATCH].str(), nullptr, 16);
Andrew Jefferyb87aa322018-02-27 16:27:02 +1030329 if ((end - start) & (blockSize - 1))
330 {
331 MSG_ERR("Partition '%s' has a size 0x%lx that is not aligned to block "
332 "size 0x%zx\n",
333 match[NAME_MATCH].str().c_str(), (end - start), blockSize);
334 }
335
Andrew Jefferyd394a782018-02-21 16:16:14 +1030336 writeSizes(part, start, end, blockSize);
337
338 // Use the shift to convert "80" to 0x80000000
339 unsigned long version = std::stoul(match[VERSION_MATCH].str(), nullptr, 16);
340 writeUserdata(part, version << versionShift, match.suffix().str());
341 part.checksum = details::checksum(part.data);
Andrew Jefferyd394a782018-02-21 16:16:14 +1030342}
343
Deepak Kodihalli393821d2017-04-28 04:44:38 -0500344} // namespace virtual_pnor
345} // namespace openpower