1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/* IBM_PROLOG_BEGIN_TAG */
/* This is an automatically generated prolog. */
/* */
/* $Source: src/usr/util/runtime/utillidmgr_rt.C $ */
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2013,2017 */
/* [+] International Business Machines Corp. */
/* */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */
/* implied. See the License for the specific language governing */
/* permissions and limitations under the License. */
/* */
/* IBM_PROLOG_END_TAG */
#include <stdio.h>
#include <algorithm>
#include <util/utillidmgr.H>
#include <util/util_reasoncodes.H>
#include <errl/errlmanager.H>
#include <vfs/vfs.H>
#include <runtime/interface.h>
#include <initservice/initserviceif.H>
#include <secureboot/containerheader.H>
#include <trace/interface.H>
#include "../utilbase.H"
#include <util/utillidpnor.H>
#include <pnor/pnor_reasoncodes.H>
UtilLidMgr::UtilLidMgr(uint32_t i_lidId) :
iv_isLidInPnor(false), iv_lidBuffer(nullptr), iv_lidSize(0),
iv_isLidInVFS(false), iv_isLidInHbResvMem(false)
{
iv_spBaseServicesEnabled = INITSERVICE::spBaseServicesEnabled();
updateLid(i_lidId);
}
UtilLidMgr::~UtilLidMgr()
{
errlHndl_t l_err = nullptr;
l_err = cleanup();
if(l_err)
{
//cleanup errors are extermely rare
ERRORLOG::errlCommit( l_err, UTIL_COMP_ID );
}
}
errlHndl_t UtilLidMgr::setLidId(uint32_t i_lidId)
{
errlHndl_t l_err = nullptr;
//must call cleanup before updateLid
l_err = cleanup();
updateLid(i_lidId);
return l_err;
}
errlHndl_t UtilLidMgr::getLidSize(size_t& o_lidSize)
{
errlHndl_t l_err = loadLid();
o_lidSize = iv_lidSize;
return l_err;
}
errlHndl_t UtilLidMgr::getLid(void* i_dest, size_t i_destSize)
{
errlHndl_t l_err = loadLid();
if (iv_lidBuffer != nullptr)
{
memcpy(i_dest, iv_lidBuffer, std::min(i_destSize, iv_lidSize));
}
return l_err;
}
errlHndl_t UtilLidMgr::getStoredLidImage(void*& o_pLidImage,
size_t& o_lidImageSize)
{
errlHndl_t l_err = nullptr;
if((nullptr == iv_lidBuffer) || (0 == iv_lidSize))
{
l_err = loadLid();
}
if(l_err)
{
o_lidImageSize = 0;
o_pLidImage = nullptr;
}
else
{
o_lidImageSize = iv_lidSize;
o_pLidImage = iv_lidBuffer;
}
return l_err;
}
errlHndl_t UtilLidMgr::releaseLidImage(void)
{
// we already figured out where the data is, remember that
bool l_inPnor = iv_isLidInPnor;
bool l_inVFS = iv_isLidInVFS;
bool l_inHbResvMem = iv_isLidInHbResvMem;
errlHndl_t l_err = cleanup();
// restore the presence info
iv_isLidInPnor = l_inPnor;
iv_isLidInVFS = l_inVFS;
iv_isLidInHbResvMem = l_inHbResvMem;
return l_err;
}
errlHndl_t UtilLidMgr::loadLid()
{
if (nullptr != iv_lidBuffer) return nullptr;
UTIL_FT("UtilLidMgr::loadLid");
const char* l_addr = nullptr;
size_t l_size = 0;
errlHndl_t l_errl = nullptr;
do
{
if(iv_isLidInHbResvMem)
{
const auto pnorSectionId = Util::getLidPnorSection(
static_cast<Util::LidId>(iv_lidId));
UTIL_FT("UtilLidMgr::loadLid> iv_isLidInHbResvMem=true");
iv_lidBuffer = reinterpret_cast<void*>(g_hostInterfaces->
get_reserved_mem(
PNOR::SectionIdToString(pnorSectionId),
0));
// If nullptr returned, set size to 0 to indicate we could not find
// the lid in HB resv memory
if (iv_lidBuffer == nullptr)
{
UTIL_FT("UtilLidMgr::loadLid - resv mem section not found");
iv_lidSize = 0;
}
else
{
UTIL_FT("UtilLidMgr::loadLid - resv mem section found");
// Ensure Section has a Secure Header
if (!PNOR::cmpSecurebootMagicNumber(
reinterpret_cast<uint8_t*>(iv_lidBuffer)))
{
UTIL_FT(ERR_MRK"UtilLidMgr::loadLid: currently don't support "
"a reserved memory area without a secure header. Section = %s",
PNOR::SectionIdToString(pnorSectionId));
uint64_t l_actualBytes = 0;
memcpy(&l_actualBytes,
iv_lidBuffer,
sizeof(ROM_MAGIC_NUMBER));
/*@
* @errortype ERRL_SEV_INFORMATIONAL
* @moduleid Util::UTIL_LIDMGR_RT
* @reasoncode PNOR::RC_BAD_SECURE_MAGIC_NUM
* @userdata1 Section attempting to be loaded
* @userdata2 First 4 bytes of vaddr
* @devdesc Error loading lid from reserved memory without secure header
* @custdesc Firmware Error
*/
l_errl = new ERRORLOG::ErrlEntry(
ERRORLOG::ERRL_SEV_INFORMATIONAL,
Util::UTIL_LIDMGR_RT,
PNOR::RC_BAD_SECURE_MAGIC_NUM,
pnorSectionId,
l_actualBytes,
true/*SW Error*/);
break;
}
UTIL_FT("UtilLidMgr::loadLid - resv mem section has secure header");
// Build a container header object to parse protected size
SECUREBOOT::ContainerHeader l_conHdr(iv_lidBuffer);
iv_lidSize = l_conHdr.payloadTextSize();
// Increment by page size to not expose secure header
iv_lidBuffer = static_cast<uint8_t*>(iv_lidBuffer) +
PAGESIZE;
}
}
else if(iv_isLidInVFS)
{
UTIL_FT("UtilLidMgr::loadLid> iv_isLidInVFS=true");
l_errl = VFS::module_address(iv_lidFileName, l_addr, l_size);
if (l_errl)
{
break;
}
iv_lidBuffer = const_cast<void*>(reinterpret_cast<const void*>
(l_addr));
iv_lidSize = l_size;
}
else if (iv_isLidInPnor)
{
UTIL_FT("UtilLidMgr::loadLid> iv_isLidInPnor=true");
iv_lidSize = iv_lidPnorInfo.size;
iv_lidBuffer = reinterpret_cast<char *>(iv_lidPnorInfo.vaddr);
}
else if( g_hostInterfaces->lid_load
&& iv_spBaseServicesEnabled )
{
UTIL_FT("UtilLidMgr::loadLid> Calling lid_load(0x%.8X)", iv_lidId);
int rc = g_hostInterfaces->lid_load(iv_lidId, &iv_lidBuffer,
&iv_lidSize);
if (0 != rc)
{
/*@
* @errortype ERRL_SEV_INFORMATIONAL
* @moduleid Util::UTIL_LIDMGR_RT
* @reasoncode Util::UTIL_LIDMGR_RC_FAIL
* @userdata1 Return code from lid_load call.
* @userdata2 Lid number
* @devdesc Unable to load LID via host interface.
*/
l_errl = new ERRORLOG::ErrlEntry(
ERRORLOG::ERRL_SEV_INFORMATIONAL,
Util::UTIL_LIDMGR_RT,
Util::UTIL_LIDMGR_RC_FAIL,
rc,
iv_lidId,
true/*SW Error*/);
break;
}
}
// Could not find the lid anywhere
if( iv_lidSize == 0 )
{
/*@
* @errortype ERRL_SEV_INFORMATIONAL
* @moduleid Util::UTIL_LIDMGR_RT
* @reasoncode Util::UTIL_LIDMGR_NOT_FOUND
* @userdata1 Lid number
* @devdesc Unable to find Lid.
*/
l_errl = new ERRORLOG::ErrlEntry(
ERRORLOG::ERRL_SEV_INFORMATIONAL,
Util::UTIL_LIDMGR_RT,
Util::UTIL_LIDMGR_NOT_FOUND,
iv_lidId,
0,
true/*SW Error*/);
break;
}
} while (0);
return l_errl;
}
errlHndl_t UtilLidMgr::cleanup()
{
errlHndl_t l_err = nullptr;
if ((!iv_isLidInVFS) && (!iv_isLidInPnor) && (nullptr != iv_lidBuffer) &&
!iv_isLidInHbResvMem)
{
int l_rc = g_hostInterfaces->lid_unload(iv_lidBuffer);
if (l_rc)
{
/*@
* @errortype ERRL_SEV_INFORMATIONAL
* @moduleid Util::UTIL_LIDMGR_RT
* @reasoncode Util::UTIL_LIDMGR_UNLOAD_RC_FAIL
* @userdata1 Return code from lid_unload call.
* @devdesc Unable to unload LID via host interface.
*/
l_err = new ERRORLOG::ErrlEntry(
ERRORLOG::ERRL_SEV_INFORMATIONAL,
Util::UTIL_LIDMGR_RT,
Util::UTIL_LIDMGR_UNLOAD_RC_FAIL,
l_rc);
}
}
iv_lidBuffer = nullptr;
iv_lidSize = 0;
iv_isLidInPnor = false;
iv_isLidInVFS = false;
iv_isLidInHbResvMem = false;
return l_err;
}
void UtilLidMgr::updateLid(uint32_t i_lidId)
{
UTIL_FT("UtilLidMgr::updateLid - i_lidId=0x%.8X", i_lidId);
iv_lidId = i_lidId;
// First check if lid is already in hostboot reserved memory
// In securemode the lid is pre-verified
if (TARGETING::is_sapphire_load() && lidInHbResvMem(iv_lidId))
{
UTIL_FT("UtilLidMgr::updateLid - lid in Hb Resv Mem");
getLidPnorSectionInfo(iv_lidId, iv_lidPnorInfo);
iv_isLidInHbResvMem = true;
}
// Check if PNOR is access is supported
else if (!g_hostInterfaces->pnor_read ||
iv_spBaseServicesEnabled )
{
iv_isLidInPnor = false;
}
else
{
UTIL_FT("UtilLidMgr::updateLid - lid in PNOR");
// If it's in PNOR it's not technically a lid
// so use a slightly different extension
iv_isLidInPnor = getLidPnorSectionInfo(iv_lidId, iv_lidPnorInfo);
}
sprintf(iv_lidFileName, "%x.lidbin", iv_lidId);
iv_isLidInVFS = VFS::module_exists(iv_lidFileName);
}
const uint32_t * UtilLidMgr::getLidList(size_t * o_num)
{
static uint32_t lidlist[] =
{
Util::OCC_LIDID,
Util::OCC_CONTAINER_LIDID,
Util::WOF_LIDID,
Util::WOF_CONTAINER_LIDID,
Util::NIMBUS_HCODE_LIDID,
Util::HWREFIMG_RINGOVD_LIDID,
Util::HCODE_CONTAINER_LIDID,
Util::HWREFIMG_RINGOVD_LIDID
};
*o_num = sizeof(lidlist)/sizeof(lidlist[0]);
return lidlist;
}
bool UtilLidMgr::lidInHbResvMem(const uint32_t i_lidId) const
{
return i_lidId == Util::OCC_LIDID ||
i_lidId == Util::OCC_CONTAINER_LIDID ||
i_lidId == Util::WOF_LIDID ||
i_lidId == Util::WOF_CONTAINER_LIDID ||
i_lidId == Util::NIMBUS_HCODE_LIDID ||
i_lidId == Util::CUMULUS_HCODE_LIDID ||
i_lidId == Util::HCODE_CONTAINER_LIDID ||
i_lidId == Util::HWREFIMG_RINGOVD_LIDID;
}
//------------------------------------------------------------------------
struct registerLidMgr
{
registerLidMgr()
{
runtimeInterfaces_t * rt_intf = getRuntimeInterfaces();
rt_intf->get_lid_list = &UtilLidMgr::getLidList;
}
};
registerLidMgr g_registerLidMgr;
|