summaryrefslogtreecommitdiffstats
path: root/src/include/usr/util/utilmclmgr.H
blob: 58a73bae48b2f2914f9ad3ba3861fd907ce8fc87 (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
/* 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                             */
/* [+] 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>

// Forward declarations
class MasterContainerLidMgrTest;

namespace MCL
{

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

// Defines to simplify syntax when checking for the MCL and POWERVM comp ids
extern const ComponentID g_MclCompId;
extern const ComponentID g_PowervmCompId;

// @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) {}
    LidInfo(uint32_t i_id): id(i_id), size(0) {}
    LidInfo(uint32_t i_id, size_t i_size): id(i_id), size(i_size) {}

    uint32_t id;
    size_t size;

    /**
     * @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);
    }

    /**
     * @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 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.
     */
    MasterContainerLidMgr();

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

    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();

    // 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;

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

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

    // Cache Components and their corresponding lids
    CompInfoMap iv_compInfoCache;

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

} // end namespace MCL

#endif
OpenPOWER on IntegriCloud