summaryrefslogtreecommitdiffstats
path: root/src/usr/pnor/pnor_common.C
blob: 2414a15c22b4e4ad537414065917ed81cdd73d75 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/pnor/pnor_common.C $                                  */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2014,2017                        */
/* [+] Google Inc.                                                        */
/* [+] 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 "pnor_common.H"
#include <pnor/pnor_reasoncodes.H>

#include "ffs.h"           //Common header file with BuildingBlock.
#include "common/ffs_hb.H" //Hostboot def of user data in ffs_entry struct
#include <sys/mm.h>

#include <initservice/initserviceif.H>
#include <util/align.H>
#include <errl/errlmanager.H>
#include <config.h>        // @FIXME RTC 132398
#include <secureboot/trustedbootif.H>
#include <devicefw/driverif.H>

// Trace definition
trace_desc_t* g_trac_pnor = NULL;
TRAC_INIT(&g_trac_pnor, PNOR_COMP_NAME, 4*KILOBYTE, TRACE::BUFFER_SLOW); //4K

// Easy macro replace for unit testing
//#define TRACUCOMP(args...)  TRACFCOMP(args)
#define TRACUCOMP(args...)

/**
 * Eyecatcher strings for PNOR TOC entries
 */
extern const char* cv_EYECATCHER[];


/*
 * @brief determine the physical offset of the ffs entry
 *        (to be used before readTOC is called)
 */
void PNOR::findPhysicalOffset(ffs_hdr* i_tocAddress,
                                const char* i_entryName,
                                uint64_t & o_offset)
{
    for(uint32_t i = 0; i < i_tocAddress->entry_count; i++)
    {
        ffs_entry* l_curEntry = (&i_tocAddress->entries[i]);
        if(strcmp(i_entryName,l_curEntry->name) == 0)
        {
            o_offset = ((uint64_t)l_curEntry->base)*PAGESIZE;
            break;
        }
    }
}

/*
 * @brief used to translate mmio offset stored in mbox scratch 3
 *        to physical offset of HBB Image
 */
errlHndl_t  PNOR::mmioToPhysicalOffset(uint64_t& o_hbbAddress)
{
    errlHndl_t l_err = NULL;
    do
    {
#if 0 // @FIXME RTC 132398
        uint64_t l_hbbMMIO = 0;
        size_t l_size = sizeof(uint64_t);
        TARGETING::Target* l_masterProc =
            TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL;

        //MBOX_SCRATCH_REG3 = 0x5003A
        l_err = DeviceFW::deviceRead(l_masterProc, &l_hbbMMIO,l_size,
                   DEVICE_SCOM_ADDRESS(INITSERVICE::SPLESS::MBOX_SCRATCH_REG3));
        if (l_err)
        {
            TRACFCOMP(g_trac_pnor,"PNOR::mmioToPhysicalOffset: Failed to read"
                    " HB MMIO offset");
            break;
        }
        //All SCOMS are 64-bit, HB MMIO is stored in higher 32-bits.
        //ANDing with TOP_OF_FLASH to maskout anything in the higher bits
        l_hbbMMIO    = (l_hbbMMIO >> 32) & PNOR::LPC_TOP_OF_FLASH_OFFSET;
        o_hbbAddress = ((9*l_hbbMMIO) - (9*PNOR::LPC_SFC_MMIO_OFFSET)
                                 - PNOR::PNOR_SIZE) /8;

#ifdef CONFIG_PNOR_INIT_FOUR_BYTE_ADDR
        // If the PNOR came up in 3-byte mode, then make sure to mask off
        // the address appropriately.
        o_hbbAddress &= 0x00ffffffu;
#endif

#else                      // @FIXME RTC 132398
        o_hbbAddress = 1;  // @FIXME RTC 132398
#endif
    } while (0);
    return l_err;
}

/*
 * @brief used to translate HBB Address to MMIO offset
 */
void  PNOR::physicalToMmioOffset(uint64_t  i_hbbAddress,
                                 uint64_t& o_mmioOffset)
{
    //Left shifting 32-bits because SCOMS store a 64-bit value
    //and HBB Offset is stored in the higher 32-bits
    o_mmioOffset = ((PNOR::LPC_SFC_MMIO_OFFSET + i_hbbAddress +
                   ((PNOR::PNOR_SIZE - i_hbbAddress)/9)) |
                   PNOR::LPC_FW_SPACE) << 32;
}

/*
 *  @brief: parse the TOCs read from memory and store section information
 *          from one of the verified TOCs
 */
errlHndl_t PNOR::parseTOC( uint8_t* i_tocBuffer,SectionData_t * o_TOC)
{
    TRACUCOMP(g_trac_pnor,"PNOR::parseTOC>");
    errlHndl_t l_errhdl = NULL;

    do{
        // Zero out my table
        PNOR::initializeSections(o_TOC);

        uint32_t l_errCode = 0;
        ffs_hdr* l_ffs_hdr = NULL;

        TRACFCOMP(g_trac_pnor, "PNOR::parseTOC verifying TOC");

        PNOR::checkForNullBuffer(i_tocBuffer, l_errCode, l_ffs_hdr);
        //Check if the buffer is null
        if(l_errCode != NO_ERROR)
        {
            TRACFCOMP(g_trac_pnor, "Null TOC Buffer found while checking TOC" );
            /*@
            * @errortype
            * @moduleid PNOR::MOD_PNORRP_READTOC
            * @reasoncode PNOR::RC_NULL_TOC_BUFFER
            * @userdata1 Address of toc buffer
            * @userdata2 Error code
            * @devdesc Expected buffer to have contents of TOC,
            *                      instead was NULL
            * @custdesc A problem occurred while reading PNOR
            *                       partition table
            */
            l_errhdl = new ERRORLOG::ErrlEntry(
                                    ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                    PNOR::MOD_PNORRP_READTOC,
                                    PNOR::RC_NULL_TOC_BUFFER,
                                    reinterpret_cast<uint64_t>(&i_tocBuffer),
                                    l_errCode,
                                    true /*Add HB SW Callout*/);
            l_errhdl->collectTrace(PNOR_COMP_NAME);
            break;
        }


        if(PNOR::pnor_ffs_checksum(l_ffs_hdr, FFS_HDR_SIZE) != 0)
        {
            l_errCode |= CHECKSUM_ERR;
        }

        //Checksum on header
        if (l_errCode != NO_ERROR)
        {
            TRACFCOMP(g_trac_pnor, "PNOR::parseTOC Checksum error in TOC's header");
            /* @errortype
            * @moduleid PNOR::MOD_PNORRP_READTOC
            * @reasoncode PNOR::RC_TOC_HDR_CHECKSUM_ERR
            * @userdata1 Address of toc buffer
            * @userdata2 Error Code
            * @devdesc Hdr of TOC of PNOR failed checksum
            * @custdesc A problem occurred while reading PNOR
            *                     partition table
            */
            l_errhdl = new ERRORLOG::ErrlEntry(
                                    ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                    PNOR::MOD_PNORRP_READTOC,
                                    PNOR::RC_TOC_HDR_CHECKSUM_ERR,
                                    reinterpret_cast<uint64_t>(&i_tocBuffer),
                                    l_errCode,
                                    false );
            l_errhdl->addPartCallout(
                            TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL,
                            HWAS::PNOR_PART_TYPE,
                            HWAS::SRCI_PRIORITY_HIGH);
            l_errhdl->collectTrace(PNOR_COMP_NAME);
            break;
        }

        PNOR::checkHeader(l_ffs_hdr, l_errCode);
        if(l_errCode != NO_ERROR)
        {
            TRACFCOMP(g_trac_pnor, "PNOR::parseTOC Error found parsing hdr of TOC " );
            /* @errortype
            * @moduleid PNOR::MOD_PNORRP_READTOC
            * @reasoncode PNOR::RC_BAD_TOC_HEADER
            * @userdata1 Address of toc buffer
            * @userdata2 Error Code
            * @devdesc Hdr of TOC of PNOR failed series of tests
            * @custdesc A problem occurred while reading PNOR
            *                     partition table
            */
            l_errhdl = new ERRORLOG::ErrlEntry(
                                    ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                    PNOR::MOD_PNORRP_READTOC,
                                    PNOR::RC_BAD_TOC_HEADER,
                                    reinterpret_cast<uint64_t>(&i_tocBuffer),
                                    l_errCode,
                                    false );
            l_errhdl->addPartCallout(
                            TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL,
                            HWAS::PNOR_PART_TYPE,
                            HWAS::SRCI_PRIORITY_HIGH);
            l_errhdl->collectTrace(PNOR_COMP_NAME);
            break;
        }

        TRACFCOMP(g_trac_pnor, "PNOR::parseTOC: FFS Block size=0x%.8X,"
        " Partition Table Size = 0x%.8x, entry_count=%d",
        l_ffs_hdr->block_size,l_ffs_hdr->size,l_ffs_hdr->entry_count);

        ffs_entry* l_err_entry = NULL;

        PNOR::parseEntries(l_ffs_hdr, l_errCode, o_TOC, l_err_entry);

        if(l_errCode != NO_ERROR)
        {
            TRACFCOMP(g_trac_pnor, "PNOR::parseTOC parseEntries"
                            " parse entries returned an error");
            o_TOC = NULL;
            /* @errortype
            * @moduleid PNOR::MOD_PNORRP_READTOC
            * @reasoncode PNOR::RC_PNOR_PARSE_ENTRIES_ERR
            * @userdata1 Address of toc buffer
            * @userdata2 Error Code
            * @devdesc Error while parsing pnor TOC entries
            * @custdesc A problem occurred while reading PNOR
            *                     partition table
            */
            l_errhdl = new ERRORLOG::ErrlEntry(
                                    ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                    PNOR::MOD_PNORRP_READTOC,
                                    PNOR::RC_PNOR_PARSE_ENTRIES_ERR,
                                    reinterpret_cast<uint64_t>(&i_tocBuffer),
                                    l_errCode,
                                    true /*Add HB SW Callout*/);
            l_errhdl->addPartCallout(
                            TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL,
                            HWAS::PNOR_PART_TYPE,
                            HWAS::SRCI_PRIORITY_HIGH);
            l_errhdl->collectTrace(PNOR_COMP_NAME);
            TRACFBIN(g_trac_pnor, "Entry in TOC that caused error during parsing", l_err_entry, FFS_ENTRY_SIZE);
            break;
        }


        //Walk through all the entries in the table and record some info
        for(uint32_t i=0; i<l_ffs_hdr->entry_count; i++)
        {
            uint32_t l_secId = PNOR::INVALID_SECTION;
            ffs_entry* cur_entry = &(l_ffs_hdr->entries[i]);
            TRACUCOMP(g_trac_pnor, "PNOR::parseTOC: TOC %d,  Entry %d, name=%s, pointer=0x%X",l_tocBeingChecked, i,cur_entry->name, (uint64_t)cur_entry);

            //Figure out section enum
            PNOR::getSectionEnum(cur_entry, &l_secId);
            if(l_secId == PNOR::INVALID_SECTION)
            {
              TRACFCOMP(g_trac_pnor, "PNOR::parseTOC:  Unrecognized Section name(%s), skipping",cur_entry->name);
              continue;
            }

            TRACFCOMP(g_trac_pnor,"PNOR::parseTOC: User Data %s",
                      cur_entry->name);

            if (o_TOC[l_secId].integrity == FFS_INTEG_ECC_PROTECT)
            {
              TRACFCOMP(g_trac_pnor, "PNOR::TOC: ECC enabled for %s",
                              cur_entry->name);

            }

            // @TODO RTC 168021 Remove legacy extensions when all
            // secure sections are supported
            auto isSecure = PNOR::isSecureSection(l_secId);
            if (   o_TOC[l_secId].version == FFS_VERS_SHA512
                && !isSecure)
            {
                // For non-secure sections with a SHA512 header, the
                // flash address has incremented past the header, so
                // back up by the header size (accounting for ECC) in order
                // to extend the header
                auto addr = o_TOC[l_secId].flashAddr;
                size_t headerSize =
                    (o_TOC[l_secId].integrity == FFS_INTEG_ECC_PROTECT) ?
                    PAGESIZE_PLUS_ECC : PAGESIZE;
                addr -= headerSize;

                l_errhdl = PNOR::extendHash(addr, headerSize,
                                            cv_EYECATCHER[l_secId]);
                if (l_errhdl)
                {
                    break;
                }
            }
        }

        for(int tmpId = 0;
        tmpId < PNOR::NUM_SECTIONS;
        tmpId ++ )
        {
            TRACFCOMP(g_trac_pnor, "%s:    size=0x%.8X  flash=0x%.8X ",
                  cv_EYECATCHER[tmpId], o_TOC[tmpId].size,
                  o_TOC[tmpId].flashAddr);
        }

    } while (0);

    TRACUCOMP(g_trac_pnor, "< PNOR::parseTOC" );
    return l_errhdl;
}

// @TODO RTC 168021 Remove legacy extensions when all secure sections are
// supported
errlHndl_t PNOR::extendHash(uint64_t i_addr, size_t i_size, const char* i_name)
{
    errlHndl_t l_errhdl = NULL;

    do {
        #ifndef __HOSTBOOT_RUNTIME
        // Read data from the PNOR DD
        uint8_t* l_buf = new uint8_t[i_size]();
        TARGETING::Target* l_target = TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL;
        l_errhdl = DeviceFW::deviceRead(l_target, l_buf, i_size,
                                        DEVICE_PNOR_ADDRESS(0,i_addr));
        if (l_errhdl)
        {
            break;
        }

        SHA512_t l_hash = {0};
        SECUREBOOT::hashBlob(l_buf, i_size, l_hash);
        l_errhdl = TRUSTEDBOOT::pcrExtend(TRUSTEDBOOT::PCR_0, l_hash,
                                          sizeof(SHA512_t), i_name);
        delete[] l_buf;

        if (l_errhdl)
        {
            break;
        }
        #endif
    } while(0);

    return l_errhdl;
}

bool PNOR::isInhibitedSection(const uint32_t i_section)
{
// TODO securebootp9 for now we won't be inhibiting overrides
#ifdef CONFIG_SECUREBOOT
//    return (i_section == ATTR_PERM ||
//            i_section == ATTR_TMP) &&
//            SECUREBOOT::enabled();
    return false;
#else
    return false;
#endif
}

OpenPOWER on IntegriCloud