summaryrefslogtreecommitdiffstats
path: root/src/include/usr/util/utilmclmgr.H
blob: 357578d9f6f286ed95e7ac2dd1d76d8deff7cc54 (plain)
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/include/usr/util/utilmclmgr.H $                           */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2017,2019                        */
/* [+] 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                                                     */
#ifndef __MASTERCONTAINERLIDMGR_H
#define __MASTERCONTAINERLIDMGR_H

#include <vector>
#include <map>
#include <secureboot/containerheader.H>
#include <errl/errlentry.H>
#include <usr/vmmconst.h>
#include <securerom/ROM.H>
#include <limits.h>

// Forward declarations
class MasterContainerLidMgrTest;

namespace MCL
{

/**
 *  @brief Structure used to hold information about a container loaded into
 *      memory using the MCL manager
 */
struct LoadedContainerInfo_t
{
    void*  pSecureHeader; ///< Virtual address of secure header; nullptr if N/A
    void*  pContent;      ///< Virtual address of container logical content
    size_t size;          ///< Size of container logical content in bytes

    /**
     *  @brief Builds a default LoadedContainerInfo_t
     */
    LoadedContainerInfo_t()
        : pSecureHeader(nullptr),
          pContent(nullptr),
          size(0)
    {
    }
};

// Component ID(name) within MCL
typedef std::array<uint8_t,16> ComponentID;

// Component ID in string form
// NOTE: ComponentID in the MCL does not include NULL terminator so include room
typedef char CompIdString[17];

// Constants to simplify checking for the MCL and POWERVM/UCD9090 comp ids
extern const ComponentID g_MclCompId;
extern const ComponentID g_PowervmCompId;
extern const ComponentID g_OpalCompId;
extern const ComponentID g_UcdCompId;
extern const ComponentID g_NvdimmCompId;

// @enum Permission Types for MCL Component
enum class CompFlags : uint16_t
{
    UNSIGNED = 0x0000,
    SIGNED = 0x8000,
    PRE_VERIFY = 0x4000,
    SIGNED_PRE_VERIFY = SIGNED|PRE_VERIFY,
};

/**
 * @brief  Comp Flags logical AND overload
 *
 * @param[in]  lhs - CompFlags to compare to
 * @param[in]  rhs - CompFlags to compare to
 *
 * @return CompFlags - The result of logically AND'ing two CompFlags
 */
inline CompFlags operator&(const CompFlags &lhs, const CompFlags &rhs)
{
    return  static_cast<CompFlags>(
                static_cast<uint16_t>(lhs) & static_cast<uint16_t>(rhs)
            );
}

/**
 * @brief  Comp Flags logical OR overload
 *
 * @param[in]  lhs - CompFlags to compare to
 * @param[in]  rhs - CompFlags to compare to
 *
 * @return CompFlags - The result of logically OR'ing two CompFlags
 */
inline CompFlags operator|(const CompFlags &lhs, const CompFlags &rhs)
{
    return  static_cast<CompFlags>(
                static_cast<uint16_t>(lhs) | static_cast<uint16_t>(rhs)
            );
}

// MCL header section
struct MclHeader
{
    uint32_t version;
    uint32_t offsetToCompSection;
    uint8_t numComponents;
    uint8_t reserved[7];
} __attribute__ ((packed));

// Structure for each component within the MCL
struct MclCompSection
{
    ComponentID compId;
    uint32_t sizeCompList;
    uint32_t numLids;
    CompFlags flags;
    uint8_t reserved[6];
    // Array size determined by numLids
    uint32_t lidArray[];
    //padding to 16 byte boundary
} __attribute__ ((packed));

// Padded size for MCL components
extern const size_t MclCompSectionPadSize;

// @brief Structure that holds lid ids and sizes
struct LidInfo
{
    LidInfo(): id(0), size(0), vAddr(nullptr) {}
    LidInfo(uint32_t i_id): id(i_id), size(0), vAddr(nullptr) {}
    LidInfo(uint32_t i_id, size_t i_size): id(i_id), size(i_size),
        vAddr(nullptr) {}

    uint32_t id; // LID ID
    size_t size; // Size of LID
    void* vAddr; // Virtual address where LID was loaded

    /**
     * @brief  Lid Info equality comparison
     *
     * @param[in]  rhs - LidInfo to compare to
     * @return bool - true if Lid Infos are equal, false otherwise
     */
    bool operator==(const LidInfo& rhs) const
    {
        return (   (id == rhs.id)
                && (size == rhs.size)
                && (vAddr == rhs.vAddr));
    }

    /**
     * @brief  Lid Info inequality comparison
     *
     * @param[in]  rhs - LidInfo to compare to
     * @return bool - true if Lid Infos are not equal, false otherwise
     */
    bool operator!=(const LidInfo& rhs) const
    {
        return !(*this == rhs);
    }

};

// @brief Structure that holds information on each component in the MCL
struct CompInfo
{
    CompFlags flags;
    uint64_t mainstoreAddr;
    size_t totalSize;
    size_t protectedSize;
    size_t unprotectedSize;
    std::vector<LidInfo> lidIds;

    // Constructors
    CompInfo()
        : flags(CompFlags::UNSIGNED), mainstoreAddr(0), totalSize(0),
          protectedSize(0), unprotectedSize(0), lidIds{} {}
    CompInfo(CompFlags i_flags)
        : flags(i_flags), mainstoreAddr(0), totalSize(0), protectedSize(0),
          unprotectedSize(0), lidIds{} {}

    /**
     * @brief  Comp Info equality comparison
     *
     * @param[in]  rhs - CompInfo to compare to
     * @return bool - true if Comp Infos are equal, false otherwise
     */
    bool operator==(const CompInfo& rhs) const
    {
        return (flags == rhs.flags &&
                mainstoreAddr == rhs.mainstoreAddr &&
                totalSize == rhs.totalSize &&
                protectedSize == rhs.protectedSize &&
                unprotectedSize == rhs.unprotectedSize &&
                lidIds == rhs.lidIds);
    }

    /**
     * @brief  Comp Info inequality comparison
     *
     * @param[in]  rhs - CompInfo to compare to
     * @return bool - true if Comp Infos are not equal, false otherwise
     */
    bool operator!=(const CompInfo& rhs) const
    {
        return !(*this == rhs);
    }

    /**
     * @brief  Print Comp Info in human friendly format
     * @return N/A
     */
    void print() const;
};

// Structure for Comp Info cache
typedef std::map<ComponentID, CompInfo> CompInfoMap;

/**
 * @brief Convert Component ID to a char*
 *        Helpful for trace
 *
 * @param[in] i_compId - Component ID to convert
 * @param[out] o_compIdStr - Component ID String
 *
 * @return N/A
 */
void compIdToString(const ComponentID i_compId, CompIdString o_compIdStr);

/**
 * @brief Convert/truncate Component ID to a uint64_t
 *        Helpful for FFDC
 *
 * @param[in] i_compId - Component ID to convert
 *
 * @return uint64_t - truncated Component ID
 */
uint64_t compIdToInt(const ComponentID i_compId);

/**
 * @brief Extract list of lid ids from lid info vector
 *
 * @param[in] i_lidIds - Vector to pull lid ids out of
 *
 * @return RomVerifyIds - IDs associated with secure component
 */
RomVerifyIds extractLidIds(const std::vector<LidInfo>& i_lidIds);

// @brief Class to manager the Master Container Lid provided by the FSP
class MasterContainerLidMgr
{

    public:

    /**
     * @brief Default Constructor
     *     Initializes memory spaces, loads, and parses the MCL.
     *
     * @param[in] i_loadOnly Only load content into memory on subsequent
     *     requests to process components.  Do not not move the content to
     *     Hostboot reserved memory.
     */
    MasterContainerLidMgr(bool i_loadOnly = false);

    /**
     * @brief Destructor. Cleans up memory allocated for class
     */
    ~MasterContainerLidMgr();

    /**
     * @brief Process each component in the MCL.
     *        See: processComponent for more info
     */
    errlHndl_t processComponents();

    /**
     *  @brief Process a single, named component from the MCL.
     *      Loads the specified component into the managed mainstore memory
     *      region.  If component is marked pre-verified, cryptographically
     *      verifies the component and extends its measurement to the TPM.
     *      If MCL manager is in non-load-only mode, copies the content into
     *      Hostboot reserved memory region as well.
     *
     *  @param[in] i_compId Component ID to load
     *  @param[out] o_info  Information (LID ID, size, virtual address, etc.)
     *      for the LIDs that were loaded.
     *
     *  @note: The container will go out of scope if another container is loaded
     *      or the MCL manager goes out of scope.
     *
     *  @note: Component info will be reset on each call
     *
     *  @return errlHndl_t Error log handle
     *  @retval nullptr    Success
     *  @retval !nullptr   Error; Error log handle points to valid error log
     */
    errlHndl_t processSingleComponent(
        const ComponentID&           i_compId,
              CompInfo&              o_info);

    /**
     * @brief TPM extend information for secure components
     *
     * @param[in] i_compId - Component Id
     * @param[in] i_conHdr - Container header with information to extend
     *
     * @return Error handle if error
     */
    static errlHndl_t tpmExtend(const ComponentID& i_compId,
                                const SECUREBOOT::ContainerHeader& i_conHdr);

    /**
     *  @brief Caches a copy of the PHyp secure header
     *
     *  @param[in] i_pHeader Pointer to PHyp secure header;
     *      Must not be nullptr or API will assert.  Input header must be
     *      exactly PAGESIZE bytes
     */
    static void cachePhypHeader(const uint8_t* i_pHeader)
    {
        assert(i_pHeader != nullptr,
            "Bug! PHyp header pointer was nullptr");
        memcpy(cv_pPhypHeader,i_pHeader,PAGESIZE);
    }

    /**
     *  @brief Returns pointer to the cached PHyp secure header
     *
     *  @return const uint8_t* Pointer to cached Phyp secure header
     */
    static const uint8_t* getPhypHeader()
    {
        return cv_pPhypHeader;
    };

    protected:

    /**
     * @brief Custom Constructor.
     *        Same as default cstor, but passes in a custom MCL
     *        NOTE: protected cstor to be used by test cases only
     *              nullptr indicates to load from UtilLidMgr
     * @param[in] i_pMcl - pointer to custom MCL
     * @param[in] i_size - size of custom MCL
     */
    MasterContainerLidMgr(const void* i_pMcl,
                          const size_t i_size);

    private:

    /**
     * @brief Common function for all constructors to call to initialize the MCL
     *
     * @param[in] i_pMcl - Pointer to custom MCL if provided
     *                     NOTE: nullptr indicates to load from UtilLidMgr
     * @param[in] i_pMcl - Size of Custom MCL
     *
     * @return N/A
     */
    void initMcl(const void* i_pMcl = nullptr, const size_t i_mclSize = 0);

    /**
     * @brief Responsible for allocating space for MCL mgr component parsing
     *        NOTE: Will shutdown if error occurs
     *
     * @param[in] i_physAddr  - Starting physical address to initialize memory
     * @param[in] i_size - Size to allocate
     * @param[in/out] io_pVaddr - Pointer to store virtual address pointer from
     *                            block map
     *                            NOTE: no-op unless the pointer is a nullptr
     *
     * @return N/A
     */
    void initMem(const uint64_t i_physAddr, const size_t i_size,
                 void *&io_pVaddr);

    /**
     * @brief Responsible for deallocating space used by MCL component parsing
     *        NOTE: Will shutdown if error occurs
     *
     * @param[in] i_physAddr - Starting physical address to deallocate from
     * @param[in/out] io_pVaddr - Pointer to virtual memory to release
     *                            NOTE: no-op if the pointer is nullptr
     *
     * @return N/A
     */
    void releaseMem(const uint64_t i_physAddr, void *&io_pVaddr);

    /**
     * @brief Parse MCL and store important information in Comp Info Cache
     *        Will also print out Comp Info Cache
     * @return N/A
     */
    void parseMcl();

    /**
     * @brief Print MCL Comp Info Cache in human friendly format
     * @return N/A
     */
    void printCompInfoCache();

    /**
     * @brief Process single component in the MCL.
     *        Loads, verifies, tpm extends lids and loads into hb resv memory
     *
     * @param[in] i_compId - Component Id
     * @param[in] io_compInfo - Component info of component to process
     *
     * @return Error handle if error
     */
    errlHndl_t processComponent(const ComponentID& i_compId,
                                CompInfo& io_compInfo);

    /**
     * @brief Load all lids associated with a Component into contiguous memory
     *        NOTE: updates the lid sizes in CompInfo
     *
     * @param[in] io_compInfo - Component info of component to verify
     * @param[out] o_totalSize - Size of all lids reported by the FSP
     * @param[in] i_isPhypComp - Indicates if PHYP lids
     *
     * @return Error handle if error
     */
    errlHndl_t loadLids(CompInfo& io_compInfo, size_t& o_totalSize,
                        bool i_isPhypComp);

    /**
     * @brief Verify and Extend Component
     *
     * @param[in] i_compId - Component Id
     * @param[in] io_compInfo - Component info of component to verify
     *
     * @return Error handle if error
     *         Note: Will not return on Secure Verification Error
     */
    errlHndl_t verifyExtend(const ComponentID& i_compId,
                            CompInfo& io_compInfo);

    // Physical addresses reserved for the MCL itself
    uint64_t iv_mclAddr;

    // Maximum size of memory for the MCL itself
    size_t iv_mclSize;

    // Physical addresses reserved for temp MCL mgr space
    uint64_t iv_tmpAddr;

    // Maximum size of memory for temp MCL mgr workspace
    size_t iv_tmpSize;

    // Maximum size based on MCL or temp components
    size_t iv_maxSize;

    // Pointer to MCL virtual address space
    void* iv_pMclVaddr;

    // Pointer to MCL temp virtual address space
    void* iv_pTempVaddr;

    // Pointer to either MCL or TMP virtual address spaces
    void* iv_pVaddr;

    // Cache Components and their corresponding lids
    CompInfoMap iv_compInfoCache;

    // Indicates that Master Container Lid has a header
    bool iv_hasHeader;

    // Cache current comp id string for easy tracing
    CompIdString iv_curCompIdStr;

    // When processing a component, only load the component and verify / measure
    // it (do not copy it to reserved memory).
    bool iv_loadOnly;

    // Cached PHyp header
    static uint8_t cv_pPhypHeader[PAGE_SIZE];

    // Allow test cases to call custom constructors and have direct access
    friend class ::MasterContainerLidMgrTest;
};

} // end namespace MCL

#endif
OpenPOWER on IntegriCloud