summaryrefslogtreecommitdiffstats
path: root/src/usr/pnor/pnorvalid.C
blob: 5f747e5c5e5541415191a40b74c6283eb4b3f843 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/pnor/pnorvalid.C $                                    */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2014,2019                        */
/* [+] 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                                                     */
/**
 *  @file pnorvalid.C
 *
 *  @brief Implements PNOR::validateAltMaster(), which Validates
 *         the Alternative Master Processor's connection to PNOR
 */

/*****************************************************************************/
// I n c l u d e s
/*****************************************************************************/
#include <trace/interface.H>
#include <devicefw/driverif.H>
#include <errl/errlentry.H>
#include <errl/errlmanager.H>
#include <errl/errludlogregister.H>
#include <targeting/common/targetservice.H>
#include <targeting/common/utilFilter.H>
#include <attributeenums.H>
#include "ffs.h"
#include "common/ffs_hb.H"
#include "pnorrp.H"
#include <pnor/pnorif.H>
#include <pnor/pnor_reasoncodes.H>
#include <lpc/lpcif.H>
#if defined(CONFIG_PNORDD_IS_SFC)
#include "pnor_sfcdd.H"
using PnorDD = PnorSfcDD;
#elif (defined(CONFIG_PNORDD_IS_BMCMBOX) || defined(CONFIG_PNORDD_IS_IPMI))
#include "pnor_hiomapdd.H"
using PnorDD = PnorHiomapDD;
#else
#error "No PNOR DD configured"
#endif

// Used for creating an Invalid TOC ("PNOR")
#define PNORVALID_FAKE_MAGIC 0x504E4F52

extern trace_desc_t* g_trac_pnor;

namespace PNOR
{

/**
 * @brief Validate the MAGIC field in the TOC
 *
 * @parm i_pnordd         PNOR Device Driver Object of Alt Master
 * @parm i_ffs_hdr        FFS Header to check
 * @parm i_toc0_offset    Offset Address of TOC
 * @parm i_attempt_write  If invalid MAGIC, rewrite TOC if true
 *                        (createEmptyTOC gets called)
 *
 * @return errlHndl_t  Error log if validation failed
 */
errlHndl_t validateMagic(PnorDD*  i_pnordd,
                         ffs_hdr* i_ffs_hdr,
                         uint64_t i_toc0_offset,
                         bool     i_attempt_write);

/**
 * @brief Create an Invalid TOC on Alt Master's PNOR
 *
 * @parm i_pnordd         PNOR Device Driver Object of Alt Master
 * @parm i_ffs_hdr        FFS Header to check
 * @parm i_toc0_offset    Offset Address of TOC
 *
 * @return errlHndl_t  Error log if validation failed
 */
errlHndl_t createInvalidTOC(PnorDD*  i_pnordd,
                          ffs_hdr* i_ffs_hdr,
                          uint64_t i_toc0_offset);

/**
 * @brief Validate the Alternative Master Processor's LPC connection to PNOR
 *
 * @return errlHndl_t  Error log if validation failed
 */

errlHndl_t validateAltMaster( void )
{
    errlHndl_t l_err = NULL;

    PnorDD* pnordd = NULL;

    // When reading PNOR TOC assume a single page and no ECC
    uint8_t* tocBuffer = new uint8_t[PAGESIZE];
    size_t read_size = PAGESIZE;
    const uint64_t toc0_offset = PnorRP::getInstance().getTocOffset(TOC_0);

    do{

        // Get list of all processors
        TARGETING::TargetHandleList procList;
        TARGETING::getAllChips(procList,
                               TARGETING::TYPE_PROC,
                               true); // true: return functional targets

        if( ( 0 == procList.size() ) ||
            ( NULL == procList[0] ) )
        {
            TRACFCOMP( g_trac_pnor, INFO_MRK"PNOR::validateAltMaster()> No functional processors Found! Validation skipped" );
            break;
        }

        // Loop through all processors
        for(uint32_t i=0; i<procList.size(); i++)
        {
            // Check if processor is MASTER_CANDIDATE
            TARGETING::ATTR_PROC_MASTER_TYPE_type type_enum =
                       procList[i]->getAttr<TARGETING::ATTR_PROC_MASTER_TYPE>();

            if ( type_enum != TARGETING::PROC_MASTER_TYPE_MASTER_CANDIDATE )
            {
                TRACDCOMP( g_trac_pnor, INFO_MRK"PNOR::validateAltMaster> Skipping Processor 0x%X (PROC_MASTER_TYPE=%d)",
                           TARGETING::get_huid(procList[i]), type_enum);
                continue;
            }

            TRACFCOMP( g_trac_pnor, INFO_MRK"PNOR::validateAltMaster> Validating Processor 0x%X (PROC_MASTER_TYPE=%d)",
                       TARGETING::get_huid(procList[i]), type_enum);

            // Create and initialize custom PNOR DD class for this target
            if ( pnordd )
            {
                delete pnordd;
                pnordd = NULL;

                // Delete the LPC objects we already used
                l_err = LPC::create_altmaster_objects( false, NULL );
                if ( l_err )
                {
                    // Commit Error Log, but continue the test
                    TRACFCOMP( g_trac_pnor, INFO_MRK"PNOR::validateAltMaster> Could not delete LPC objects. eid=0x%X, rc=0x%X. Committing log and Continuing", l_err->eid(), l_err->reasonCode());

                    l_err->collectTrace(PNOR_COMP_NAME);

                    // if there was an error, commit here and then proceed to
                    // the next processor
                    errlCommit(l_err,PNOR_COMP_ID);
                    continue;
                }
            }

            // Create the LPC objects we're going to use
            l_err = LPC::create_altmaster_objects( true, procList[i] );
            if ( l_err )
            {
                // Commit Error Log, but continue the test
                TRACFCOMP( g_trac_pnor, INFO_MRK"PNOR::validateAltMaster> Could not create LPC objects for %.8X. eid=0x%X, rc=0x%X. Committing log and Continuing", TARGETING::get_huid(procList[i]), l_err->eid(), l_err->reasonCode());

                l_err->collectTrace(PNOR_COMP_NAME);

                // if there was an error, commit here and then proceed to
                // the next processor
                errlCommit(l_err,PNOR_COMP_ID);
                continue;
            }

            pnordd = new PnorDD(procList[i]);

            // Read Flash
            l_err = pnordd->readFlash(tocBuffer, read_size, toc0_offset);
            if ( l_err )
            {
                // Commit Error Log, but continue the test
                TRACFCOMP( g_trac_pnor, INFO_MRK"PNOR::validateAltMaster> readFlash fail for 0x%X. eid=0x%X, rc=0x%X. Committing log and Continuing",
                           TARGETING::get_huid(procList[i]), l_err->eid(),
                           l_err->reasonCode());

                l_err->addPartCallout(
                            procList[i],
                            HWAS::PNOR_PART_TYPE,
                            HWAS::SRCI_PRIORITY_LOW,
                            HWAS::NO_DECONFIG,
                            HWAS::GARD_NULL);

                l_err->collectTrace(PNOR_COMP_NAME);

                // if there was an error, commit here and then proceed to
                // the next processor
                errlCommit(l_err,PNOR_COMP_ID);
                continue;
            }

            // Set the header pointer
            ffs_hdr* l_ffs_hdr = (ffs_hdr*) tocBuffer;

            // Recursive function to check FFS/TOC Magic and possibly write
            // new TOC if necessary
            l_err = validateMagic(pnordd,
                                  l_ffs_hdr,
                                  toc0_offset,
                                  true); // true->write TOC if necessary

            if ( l_err )
            {
                // Commit Error Log, but continue the test
                TRACFCOMP( g_trac_pnor, INFO_MRK"PNOR::validateAltMaster> validateMagic fail for 0x%X. eid=0x%X, rc=0x%X. Committing log and Continuing",
                           TARGETING::get_huid(procList[i]), l_err->eid(),
                           l_err->reasonCode());

                l_err->addPartCallout(
                            procList[i],
                            HWAS::PNOR_PART_TYPE,
                            HWAS::SRCI_PRIORITY_HIGH,
                            HWAS::NO_DECONFIG,
                            HWAS::GARD_NULL);

                l_err->collectTrace(PNOR_COMP_NAME);

                // if there was an error, commit here and then proceed to
                // the next processor
                errlCommit(l_err,PNOR_COMP_ID);
                continue;
            }


            // Now check ffs entry checksum (0 if checksums match)
            if( pnor_ffs_checksum(l_ffs_hdr, FFS_HDR_SIZE) != 0)
            {
                //@TODO - RTC:90780 - May need to handle this differently in
                //                    SP-less config
                TRACFCOMP(g_trac_pnor, "PNOR::validateAltMaster> pnor_ffs_checksum header checksums do not match on target 0x%X. Create and commit error log then continue the test",
                          TARGETING::get_huid(procList[i]));

                /*@
                 * @errortype
                 * @moduleid     PNOR::MOD_PNORVALID_MAIN
                 * @reasoncode   PNOR::RC_PARTITION_TABLE_INVALID
                 * @userdata1    Master Candidate Processor Target
                 * @userdata2    <unused>
                 * @devdesc      PNOR::validateAltMaster> Fail verifying FFS
                 *               Header on Master Candidate PNOR TOC0
                 * @custdesc     Fail verifying Flash File System (FFS) Header
                 *               on Master Candidate Processor NOR flash Table
                 *               of Contents 0 (TOC)
                 */
                l_err = new ERRORLOG::ErrlEntry(
                                      ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                      PNOR::MOD_PNORVALID_MAIN,
                                      PNOR::RC_PARTITION_TABLE_INVALID,
                                      TARGETING::get_huid(procList[i]),
                                      0);

                l_err->addPartCallout(
                            procList[i],
                            HWAS::PNOR_PART_TYPE,
                            HWAS::SRCI_PRIORITY_HIGH,
                            HWAS::NO_DECONFIG,
                            HWAS::GARD_NULL);

                TRACFBIN(g_trac_pnor, "tocBuffer", tocBuffer, 0x20);

                l_err->collectTrace(PNOR_COMP_NAME);
                errlCommit(l_err,PNOR_COMP_ID);
                continue;
            }
            else
            {
                TRACFCOMP(g_trac_pnor, "PNOR::validateAltMaster> Successful validation of Alt-Master 0x%X",
                          TARGETING::get_huid(procList[i]));
            }

        } // end of processor 'for' loop


    }while(0);

    // Delete PNOR DD class and memory buffer
    if ( pnordd )
    {
        delete pnordd;
        pnordd = NULL;

        // Delete the LPC objects we used
        l_err = LPC::create_altmaster_objects( false, NULL );
        if ( l_err )
        {
            // Commit Error Log, but continue the test
            TRACFCOMP( g_trac_pnor, INFO_MRK"PNOR::validateAltMaster> Could not delete LPC objects. eid=0x%X, rc=0x%X. Committing log and Continuing", l_err->eid(), l_err->reasonCode());

            l_err->collectTrace(PNOR_COMP_NAME);

            // if there was an error, commit here and then proceed to
            // the next processor
            errlCommit(l_err,PNOR_COMP_ID);
        }
    }

    if(tocBuffer != NULL)
    {
        delete[] tocBuffer;
        tocBuffer = NULL;
    }

    return l_err;
}



/**
 * @brief Validate the MAGIC field in the TOC
 */
errlHndl_t validateMagic(PnorDD*  i_pnordd,
                         ffs_hdr* i_ffs_hdr,
                         uint64_t i_toc0_offset,
                         bool     i_attempt_write)
{
    errlHndl_t l_err = NULL;

    do{

        /* Checking FFS Header MAGIC to make sure it looks valid */
        if ( ( i_ffs_hdr->magic != FFS_MAGIC ) &&
             ( i_ffs_hdr->magic != PNORVALID_FAKE_MAGIC ) )
        {
            TRACFCOMP(g_trac_pnor, "PNOR::validateMagic> Invalid magic number in FFS header: 0x%.4X",
                      i_ffs_hdr->magic);

            TRACFBIN(g_trac_pnor, "PNOR::validateMagic> i_ffs_hdr",
                     i_ffs_hdr, FFS_HDR_SIZE);

            // Check if we should attempt to write empty TOC
            if ( i_attempt_write == true )
            {
                // Write Invalid TOC with a specific 'MAGIC' marker
                l_err = createInvalidTOC(i_pnordd, i_ffs_hdr, i_toc0_offset);
                if ( l_err )
                {
                    TRACFCOMP(g_trac_pnor, "PNOR::validateMagic> createInvalidTOC returned error: eid=0x%X, rc=0x%X",
                              l_err->eid(), l_err->reasonCode());
                    break;
                }
                else
                {
                    // Writing TOC successful, so recursively call this function
                    l_err = validateMagic(i_pnordd,
                                          i_ffs_hdr,
                                          i_toc0_offset,
                                          false); // false->don't write TOC
                    if ( l_err )
                    {

                        TRACFCOMP(g_trac_pnor, "PNOR::validateMagic> After successful createEmptyTOC, validateMagic returned error: eid=0x%X, rc=0x%X",
                                  l_err->eid(), l_err->reasonCode());
                        break;
                    }
                }
            }
            else
            {

                //@TODO - RTC:90780 - May need to handle this differently in
                //                    SP-less config
                TRACFCOMP(g_trac_pnor, "PNOR::validateMagic> MAGIC number header still incorrect after writing empty TOC");

                /*@
                 * @errortype
                 * @moduleid     PNOR::MOD_PNORVALID_MAGIC
                 * @reasoncode   PNOR::RC_PARTITION_TABLE_INVALID
                 * @userdata1    Magic Number read
                 * @userdata2    <unused>
                 * @devdesc      PNOR::validateMagic> Fail verifying FFS Magic
                 *               Number in Header on Master
                 * @custdesc     Fail verifying Flash File System (FFS) Magic
                 *               Number in Header on Master
                 */
                l_err = new ERRORLOG::ErrlEntry(
                                      ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                      PNOR::MOD_PNORVALID_MAGIC,
                                      PNOR::RC_PARTITION_TABLE_INVALID,
                                      i_ffs_hdr->magic,
                                      0);

                TRACFBIN(g_trac_pnor, "i_ffs_hdr", i_ffs_hdr, FFS_HDR_SIZE);

                // HW Callout handled by caller
                break;
            }
        }

    }while(0);


    return l_err;
}

/**
 * @brief Create an Invalid TOC on Alt Master's PNOR
 */
errlHndl_t createInvalidTOC(PnorDD*  i_pnordd,
                            ffs_hdr* i_ffs_hdr,
                            uint64_t i_toc0_offset)
{
    errlHndl_t l_err = NULL;

    // When reading PNOR TOC assume a single page and no ECC
    size_t toc_size = PAGESIZE;

    do{
            // Create FFS Header of Empty TOC with Invalid MAGIC field
            ffs_hdr l_hdr;

            // Clear the struct and only set (invalid) MAGIC and checksum fields
            // - Thus HWSV will recognize this TOC needs to be updated
            memset(&(l_hdr), 0, FFS_HDR_SIZE);
            l_hdr.magic       = PNORVALID_FAKE_MAGIC;
            l_hdr.checksum    = pnor_ffs_checksum(&(l_hdr), FFS_HDR_SIZE_CSUM);

            // Re-use previously defined io_ffs_hdr memory space
            memset(i_ffs_hdr, 0, toc_size);
            memcpy(i_ffs_hdr, &(l_hdr), FFS_HDR_SIZE);

            TRACFBIN(g_trac_pnor, "PNOR::createInvalidTOC> New FFS Header",
                     i_ffs_hdr, FFS_HDR_SIZE);


            // Write TOC to PNOR
            l_err = i_pnordd->writeFlash(i_ffs_hdr, toc_size,
                                         i_toc0_offset);
            if ( l_err )
            {
                TRACFCOMP( g_trac_pnor, INFO_MRK"PNOR::createInvalidTOC> writeFlash fail: eid=0x%X, rc=0x%X",
                           l_err->eid(), l_err->reasonCode());

                TRACFBIN(g_trac_pnor, "EMPTY TOC", i_ffs_hdr, 0x20);
                break;
            }

            // Read Back TOC from PNOR (reset toc_size and i_ffs_hdr to be safe)
            toc_size = PAGESIZE;
            memset(i_ffs_hdr, 0, toc_size);
            l_err = i_pnordd->readFlash(i_ffs_hdr, toc_size,
                                        i_toc0_offset);
            if ( l_err )
            {
                TRACFCOMP( g_trac_pnor, INFO_MRK"PNOR::createInvalidTOC> readFlash fail: eid=0x%X, rc=0x%X",
                           l_err->eid(), l_err->reasonCode());

            }


    }while(0);

    return l_err;
}

}; //namespace PNOR

OpenPOWER on IntegriCloud