summaryrefslogtreecommitdiffstats
path: root/src/usr/secureboot/base/settings.C
blob: ec873c47c103b208361ed5128a9f17da9563a5e7 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/secureboot/base/settings.C $                          */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2013,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                                                     */
#include <errl/errlentry.H>
#include <errl/errlmanager.H>
#include <devicefw/userif.H>
#include <secureboot/service.H>
#include <secureboot/secure_reasoncodes.H>
#include <targeting/common/util.H>
#include <targeting/common/target.H>
#include <initservice/initserviceif.H>
#include <secureboot/settings.H>
#include <console/consoleif.H>
#include <kernel/console.H>

// SECUREBOOT : General driver traces
#include "../common/securetrace.H"

namespace SECUREBOOT
{
    using namespace TARGETING;
    using namespace ERRORLOG;

    void Settings::_init()
    {
        uint64_t securitySwitchValue = 0;

        // read security switch register
        auto l_errl = getSecuritySwitch(securitySwitchValue,
                                        MASTER_PROCESSOR_CHIP_TARGET_SENTINEL);

        if (NULL != l_errl)
        {

            // Grab errlog reason code before committing.
            uint16_t l_rc = l_errl->reasonCode();

            errlCommit(l_errl, SECURE_COMP_ID);
            // we need to shutdown here because getSecuritySwitch does not
            // return a fatal error log in some cases
            INITSERVICE::doShutdown(l_rc);
        }

        // cache only the enabled flag
        iv_enabled = (0 != (securitySwitchValue &
                            static_cast<uint64_t>(ProcSecurity::SabBit)));

        SB_INF("getEnabled() state:%i",iv_enabled);
        printk("SECUREBOOT::enabled() state:%i\n", iv_enabled);

        // Report if secure boot is disabled
        #ifdef CONFIG_SECUREBOOT
        if (!iv_enabled)
        {
            #ifdef CONFIG_CONSOLE
            CONSOLE::displayf(SECURE_COMP_NAME, "Booting in non-secure mode.");
            #endif

            uint64_t cbsValue = 0;
            l_errl = getProcCbsControlRegister(
                cbsValue,
                MASTER_PROCESSOR_CHIP_TARGET_SENTINEL);

            if (l_errl)
            {
                SB_ERR("getEnabled(): Failed in call to "
                    "getProcCbsControlRegister().");

                // commit the CBS control register error
                ERRORLOG::errlCommit(l_errl, SECURE_COMP_ID);

                // we're already in the error path so we just keep going
                // knowing the register is suspect
            }

            SB_INF("Booting in non-secure mode. "
                "CBS Control/Status Register (0x50001) = 0x%016llX, "
                "Security Switch Register (0x10005) = 0x%016llX.",
                cbsValue, securitySwitchValue);
        }
        else
        {
            #ifdef CONFIG_CONSOLE
            CONSOLE::displayf(SECURE_COMP_NAME, "Booting in secure mode.");
            #endif
        }
        #endif
    }

    bool Settings::getEnabled() const
    {
        return iv_enabled;
    }

    errlHndl_t Settings::getJumperState(SecureJumperState& o_state,
                                        Target* i_pProc) const
    {
        uint64_t l_regValue = 0;
        o_state = SecureJumperState::SECURITY_DEASSERTED;

        errlHndl_t l_errl = nullptr;

        do
        {
            l_errl = getProcCbsControlRegister(l_regValue, i_pProc);

            SB_DBG("getJumperState() err:%i reg:%.16llX huid:%.8X",
                !!l_errl, l_regValue, get_huid(i_pProc));

            if (l_errl)
            {
                break;
            }

            // If the "Secure Mode Disable" (SMD) bit is 0b1 in the CBS
            // Control/Status register, hardware security is deasserted;
            // otherwise (0b0), hardware security is asserted
            o_state = (l_regValue &
                static_cast<uint64_t>(ProcCbsControl::JumperStateBit)) ?
                     SecureJumperState::SECURITY_DEASSERTED :
                     SecureJumperState::SECURITY_ASSERTED;

            SB_INF("getJumperState() state:%i huid:%.8X", o_state,
                                                            get_huid(i_pProc));

        } while(0);

        return l_errl;
    }

    errlHndl_t Settings::getProcCbsControlRegister(uint64_t& o_regValue,
                                                   Target* i_pProc) const
    {
        // the supplied target input parameter is validated in one place
        // inside the readSecurityRegister function
        return readSecurityRegister(i_pProc,
                    static_cast<uint64_t>(ProcCbsControl::StatusRegister),
                    o_regValue);
    }

    errlHndl_t Settings::getSecuritySwitch(uint64_t& o_regValue,
                                           Target* i_pProc) const
    {
        auto l_errl = readSecurityRegister(i_pProc,
                    static_cast<uint64_t>(ProcSecurity::SwitchRegister),
                    o_regValue);
        SB_INF("getSecuritySwitch() err_rc:0x%.4X huid:0x%.8X reg:0x%.16llX",
            ERRL_GETRC_SAFE(l_errl), get_huid(i_pProc), o_regValue);

        return l_errl;
    }

    errlHndl_t Settings::clearSecuritySwitchBits(
        const std::vector<SECUREBOOT::ProcSecurity>& i_bits,
              TARGETING::Target* const               i_pTarget) const
    {
        uint64_t bitsToClear = 0;
        for(const auto &bit : i_bits)
        {
            bitsToClear |= static_cast<uint64_t>(bit);
        }

        auto pError = writeSecurityRegister(
            i_pTarget,
            static_cast<uint64_t>(ProcSecurity::SwitchRegisterClear),
            bitsToClear);

        if(pError)
        {
            SB_ERR("clearSecuritySwitchBits: writeSecurityRegister "
                "(SwitchRegisterClear) failed. Target HUID = 0x%08X, data = "
                "0x%016llX.",
                get_huid(i_pTarget),bitsToClear);
            SB_ERR("clearSecuritySwitchBits: plid=0x%08X, eid=0x%08X, "
                "reason=0x%04X",
                ERRL_GETPLID_SAFE(pError),
                ERRL_GETEID_SAFE(pError),
                ERRL_GETRC_SAFE(pError));
        }

        return pError;
    }

    errlHndl_t Settings::setSecuritySwitchBits(
        const std::vector<SECUREBOOT::ProcSecurity>& i_bits,
              TARGETING::Target* const               i_pTarget) const
    {
        uint64_t bitsToSet = 0;
        for(const auto &bit : i_bits)
        {
            bitsToSet |= static_cast<uint64_t>(bit);
        }

        auto pError = writeSecurityRegister(
            i_pTarget,
            static_cast<uint64_t>(ProcSecurity::SwitchRegister),
            bitsToSet);

        if(pError)
        {
            SB_ERR("setSecuritySwitchBits: writeSecurityRegister "
                "(SwitchRegister) failed. Target HUID = 0x%08X, data = "
                "0x%016llX.",
                get_huid(i_pTarget),bitsToSet);
            SB_ERR("setSecuritySwitchBits: plid=0x%08X, eid=0x%08X, "
                "reason=0x%04X",
                ERRL_GETPLID_SAFE(pError),
                ERRL_GETEID_SAFE(pError),
                ERRL_GETRC_SAFE(pError));
        }

        return pError;
    }

    errlHndl_t Settings::writeSecurityRegister(
              TARGETING::Target* const i_pTarget,
        const uint64_t                 i_scomAddress,
        const uint64_t                 i_data) const
    {
        errlHndl_t pError = nullptr;

        do
        {

        // Target must be the sentinel or some other non-NULL proc value
        if (   (i_pTarget != TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL)
            && (   (i_pTarget == nullptr)
                || (   (i_pTarget->getAttr<TARGETING::ATTR_TYPE>())
                    != (TARGETING::TYPE_PROC) ) ) )
        {
            SB_ERR("writeSecurityRegister: Caller invoked API with bad target; "
                "Target HUID = 0x%08X.",get_huid(i_pTarget));
            /*@
             * @errortype
             * @severity   ERRL_SEV_UNRECOVERABLE
             * @moduleid   SECUREBOOT::MOD_SECURE_WRITE_REG
             * @reasoncode SECUREBOOT::RC_SECURE_BAD_TARGET
             * @userdata1  Target pointer value
             * @userdata2  Target's HUID or 0 if NULL target pointer
             * @devdesc    Invalid target used to write security
             *             register.
             * @custdesc   Unexpected internal firmware error.
             */
            pError = new ERRORLOG::ErrlEntry(
                ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                SECUREBOOT::MOD_SECURE_WRITE_REG,
                SECUREBOOT::RC_SECURE_BAD_TARGET,
                reinterpret_cast<uint64_t>(i_pTarget),
                TO_UINT64(get_huid(i_pTarget)),
                true);
            pError->collectTrace(SECURE_COMP_NAME, ERROR_TRACE_SIZE);
            addSecureUserDetailsToErrlog(pError);
            break;
        }

        // Write security switch settings to processor
        const size_t expSize = sizeof(i_data);
        size_t actSize = expSize;
        pError = deviceWrite(
            i_pTarget,
            const_cast<uint64_t*>(&i_data), actSize,
            DEVICE_SCOM_ADDRESS(i_scomAddress));
        if (nullptr != pError)
        {
            SB_ERR("writeSecurityRegister: deviceWrite failed; target HUID = "
                "0x%08X, SCOM addr = 0x%016llX, data = 0x%016llX.",
                get_huid(i_pTarget),i_scomAddress,i_data);
            break;
        }

        if(actSize != expSize)
        {
            SB_ERR("writeSecurityRegister: size returned from device write (%d) is not the expected size of %d",
                   actSize, expSize);
            /*@
             * @errortype
             * @severity        ERRORLOG::ERRL_SEV_UNRECOVERABLE
             * @moduleid        SECUREBOOT::MOD_SECURE_WRITE_REG
             * @reasoncode      SECUREBOOT::RC_DEVICE_WRITE_ERR
             * @userdata1       Actual size written
             * @userdata2       Expected size written
             * @devdesc         Device write did not return expected size
             * @custdesc        Firmware Error
             */
            pError = new ERRORLOG::ErrlEntry(
                            ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                            SECUREBOOT::MOD_SECURE_WRITE_REG,
                            SECUREBOOT::RC_DEVICE_WRITE_ERR,
                            actSize,
                            expSize,
                            true);
            pError->collectTrace(SECURE_COMP_NAME);
            addSecureUserDetailsToErrlog(pError);
            break;
        }

        } while(0);

        if(pError)
        {
            SB_ERR("writeSecurityRegister: plid=0x%08X, eid=0x%08X, "
                "reason=0x%04X",
                ERRL_GETPLID_SAFE(pError),
                ERRL_GETEID_SAFE(pError),
                ERRL_GETRC_SAFE(pError));
        }

        return pError;
    }

    errlHndl_t Settings::readSecurityRegister(Target* i_pProc,
                                            const uint64_t i_scomAddress,
                                            uint64_t& o_regValue) const
    {
        errlHndl_t l_errl = nullptr;
        size_t size = sizeof(o_regValue);

        do
        {

        // make sure we are not passed a null target pointer or the wrong
        // target type (must be a processor target) or the sentinel
        if ( i_pProc != MASTER_PROCESSOR_CHIP_TARGET_SENTINEL &&
            (i_pProc == nullptr || i_pProc->getAttr<ATTR_TYPE>() != TYPE_PROC)
           )
        {
            /*@
             * @errortype
             * @moduleid         SECUREBOOT::MOD_SECURE_READ_REG
             * @reasoncode       SECUREBOOT::RC_SECURE_BAD_TARGET
             * @userdata1        Target pointer value
             * @userdata2        Target's HUID or 0 if null
             *                   target pointer.
             * @devdesc          Invalid target used to read security
             *                   switch register.
             * @custdesc         Internal Firmware error.
             */
            l_errl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                         SECUREBOOT::MOD_SECURE_READ_REG,
                                         SECUREBOOT::RC_SECURE_BAD_TARGET,
                                         reinterpret_cast<uint64_t>(i_pProc),
                                         TO_UINT64(get_huid(i_pProc)),
                                         true /* Add HB Software Callout */ );
            l_errl->collectTrace(SECURE_COMP_NAME, ERROR_TRACE_SIZE);
            addSecureUserDetailsToErrlog(l_errl);
            break;
        }

        // Make sure the processor is SCOMable
        if (i_pProc != MASTER_PROCESSOR_CHIP_TARGET_SENTINEL &&
            !i_pProc->getAttr<ATTR_SCOM_SWITCHES>().useXscom)
        {
            SB_ERR("readSecurityRegister: Processor security register read too early");
            /*@
             * @errortype
             * @severity        ERRORLOG::ERRL_SEV_UNRECOVERABLE
             * @moduleid        SECUREBOOT::MOD_SECURE_READ_REG
             * @reasoncode      SECUREBOOT::RC_PROC_NOT_SCOMABLE
             * @userdata1       Use XSCOM bool
             * @userdata2       Target's HUID
             * @devdesc         Processor security register read too early
             * @custdesc        Firmware Error
             */
            l_errl = new ERRORLOG::ErrlEntry(
                            ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                            SECUREBOOT::MOD_SECURE_READ_REG,
                            SECUREBOOT::RC_PROC_NOT_SCOMABLE,
                            i_pProc->getAttr<ATTR_SCOM_SWITCHES>().useXscom,
                            TO_UINT64(get_huid(i_pProc)),
                            true);
            l_errl->collectTrace(SECURE_COMP_NAME);
            addSecureUserDetailsToErrlog(l_errl);
            break;
        }

        // Read security switch setting from processor.
        l_errl = deviceRead(i_pProc,
                            &o_regValue, size,
                            DEVICE_SCOM_ADDRESS(i_scomAddress));

        if (nullptr != l_errl)
        {
            break;
        }

        if (size != sizeof(o_regValue))
        {
            SB_ERR("readSecurityRegister: size returned from device read (%d) is not the expected size of %d",
                   size, sizeof(o_regValue));
            /*@
             * @errortype
             * @severity        ERRORLOG::ERRL_SEV_UNRECOVERABLE
             * @moduleid        SECUREBOOT::MOD_SECURE_READ_REG
             * @reasoncode      SECUREBOOT::RC_DEVICE_READ_ERR
             * @userdata1       Actual size read
             * @userdata2       Expected size read
             * @devdesc         Processor security register read too early
             * @custdesc        Firmware Error
             */
            l_errl = new ERRORLOG::ErrlEntry(
                            ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                            SECUREBOOT::MOD_SECURE_READ_REG,
                            SECUREBOOT::RC_DEVICE_READ_ERR,
                            size,
                            sizeof(o_regValue),
                            true);
            l_errl->collectTrace(SECURE_COMP_NAME);
            addSecureUserDetailsToErrlog(l_errl);
            break;
        }

        } while(0);

        return l_errl;
    }
}
OpenPOWER on IntegriCloud