-
Notifications
You must be signed in to change notification settings - Fork 97
/
pnorrp.C
1833 lines (1653 loc) · 69.2 KB
/
pnorrp.C
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* IBM_PROLOG_BEGIN_TAG */
/* This is an automatically generated prolog. */
/* */
/* $Source: src/usr/pnor/pnorrp.C $ */
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2011,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 "pnorrp.H"
#include "spnorrp.H"
#include <pnor/pnor_reasoncodes.H>
#include <initservice/taskargs.H>
#include <sys/msg.h>
#include <trace/interface.H>
#include <errl/errlmanager.H>
#include <targeting/common/targetservice.H>
#include <devicefw/userif.H>
#include <limits.h>
#include <string.h>
#include <sys/mm.h>
#include <errno.h>
#include <initservice/initserviceif.H>
#include "ffs.h" //Common header file with BuildingBlock.
#include "common/ffs_hb.H"//Hostboot definition of user data in ffs_entry struct
#include <pnor/ecc.H>
#include <kernel/console.H>
#include <endian.h>
#include <util/align.H>
#include <config.h>
#include <pnor/pnorif.H>
#include "pnor_common.H"
#include <hwas/common/hwasCallout.H>
#include <console/consoleif.H>
#ifdef CONFIG_SECUREBOOT
#include <secureboot/service.H>
#include <secureboot/containerheader.H>
#include <secureboot/settings.H>
#include <secureboot/header.H>
#include <secureboot/trustedbootif.H>
#endif
extern trace_desc_t* g_trac_pnor;
// Easy macro replace for unit testing
//#define TRACUCOMP(args...) TRACFCOMP(args)
#define TRACUCOMP(args...)
using namespace PNOR;
/**
* @brief set up _start() task entry procedure for PNOR daemon
*/
TASK_ENTRY_MACRO( PnorRP::init );
/********************
Public Methods
********************/
/**
* @brief Returns information about a given side of PNOR
*/
errlHndl_t PNOR::getSideInfo( PNOR::SideId i_side,
PNOR::SideInfo_t& o_info)
{
return Singleton<PnorRP>::instance().getSideInfo(i_side,o_info);
}
/**
* @brief Return the size and address of a given section of PNOR data
*/
errlHndl_t PNOR::getSectionInfo( PNOR::SectionId i_section,
PNOR::SectionInfo_t& o_info )
{
return Singleton<PnorRP>::instance().getSectionInfo(i_section,o_info);
}
/**
* @brief Clear pnor section
*/
errlHndl_t PNOR::clearSection(PNOR::SectionId i_section)
{
return Singleton<PnorRP>::instance().clearSection(i_section);
}
/**
* @brief Write the data for a given section into PNOR
*/
errlHndl_t PNOR::flush( PNOR::SectionId i_section)
{
errlHndl_t l_err = NULL;
do {
PNOR::SectionInfo_t l_info;
l_err = getSectionInfo(i_section, l_info);
if (l_err)
{
TRACFCOMP(g_trac_pnor, "PNOR::flush: getSectionInfo errored,"
" secId: %d", (int)i_section);
break;
}
int l_rc = mm_remove_pages (RELEASE,
reinterpret_cast<void*>(l_info.vaddr), l_info.size);
if (l_rc)
{
TRACFCOMP(g_trac_pnor, "PNOR::flush: mm_remove_pages errored,"
" secId: %d, rc: %d", (int)i_section, l_rc);
/*@
* @errortype
* @moduleid PNOR::MOD_PNORRP_FLUSH
* @reasoncode PNOR::RC_MM_REMOVE_PAGES_FAILED
* @userdata1 section Id
* @userdata2 RC
* @devdesc mm_remove_pages failed
* @custdesc Failed to remove pages
*/
l_err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
PNOR::MOD_PNORRP_FLUSH,
PNOR::RC_MM_REMOVE_PAGES_FAILED,
i_section, l_rc, true);
break;
}
} while (0);
return l_err;
}
/**
* @brief check and fix correctable ECC for a given pnor section
*/
errlHndl_t PNOR::fixECC(PNOR::SectionId i_section)
{
return Singleton<PnorRP>::instance().fixECC(i_section);
}
// @TODO RTC 173489
// Remove API once FSP fully supports signing of PNOR sections that did not
// previously have a sha512 header
errlHndl_t PNOR::readHeaderMagic(
const PNOR::SectionId i_secId,
const PNOR::SectionData_t& i_TOC,
const size_t i_size,
void* const o_pData)
{
errlHndl_t pError = nullptr;
assert(o_pData != nullptr,"Output buffer pointer was nullptr");
do {
size_t size = i_size;
auto pTarget = TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL;
// Read first <=8 bytes of section data from the PNOR DD
// Note: Do not need to worry about ECC as the 9th byte is the first
// ECC byte.
pError = DeviceFW::deviceRead(pTarget, o_pData, size,
DEVICE_PNOR_ADDRESS(0,i_TOC.flashAddr));
if (pError)
{
break;
}
} while(0);
return pError;
}
/**
* STATIC
* @brief Static Initializer
*/
void PnorRP::init( errlHndl_t &io_rtaskRetErrl )
{
TRACUCOMP(g_trac_pnor, "PnorRP::init> " );
uint64_t rc = 0;
uint64_t rcs = 0; // spnorrp return code
errlHndl_t l_errl = NULL;
if( Singleton<PnorRP>::instance().didStartupFail(rc)
#ifdef CONFIG_SECUREBOOT
|| Singleton<SPnorRP>::instance().didStartupFail(rcs)
#endif
)
{
/*@
* @errortype ERRL_SEV_CRITICAL_SYS_TERM
* @moduleid PNOR::MOD_PNORRP_DIDSTARTUPFAIL
* @reasoncode PNOR::RC_BAD_STARTUP_RC
* @userdata1 return code pnorrp
* @userdata2 return code spnorrp
*
* @devdesc PNOR startup task returned an error.
* @custdesc A problem occurred while accessing the boot flash.
*/
l_errl = new ERRORLOG::ErrlEntry(
ERRORLOG::ERRL_SEV_CRITICAL_SYS_TERM,
PNOR::MOD_PNORRP_DIDSTARTUPFAIL,
PNOR::RC_BAD_STARTUP_RC,
rc,
rcs,
true /*Add HB SW Callout*/ );
l_errl->collectTrace(PNOR_COMP_NAME);
}
else
{
// Extend base image (HBB) when Hostboot first starts. Since HBB is
// never re-loaded, inhibit extending this image in runtime code.
#ifndef __HOSTBOOT_RUNTIME
#ifdef CONFIG_SECUREBOOT
// Extend the base image to the TPM, regardless of how it was obtained
l_errl = TRUSTEDBOOT::extendBaseImage();
#endif
#endif
}
io_rtaskRetErrl=l_errl;
}
/********************
Helper Methods
********************/
/**
* @brief Static function wrapper to pass into task_create
*/
void* wait_for_message( void* unused )
{
TRACUCOMP(g_trac_pnor, "wait_for_message> " );
Singleton<PnorRP>::instance().waitForMessage();
return NULL;
}
/********************
Private/Protected Methods
********************/
/**
* @brief Constructor
*/
PnorRP::PnorRP()
:
iv_TOC_used(TOC_0)
,iv_msgQ(NULL)
,iv_startupRC(0)
,iv_shutdown_pending(false)
{
TRACDCOMP(g_trac_pnor, "PnorRP::PnorRP> " );
// setup everything in a separate function
initDaemon();
TRACFCOMP(g_trac_pnor, "< PnorRP::PnorRP : Startup Errors=%X ", iv_startupRC );
}
/**
* @brief Destructor
*/
PnorRP::~PnorRP()
{
TRACDCOMP(g_trac_pnor, "PnorRP::~PnorRP> " );
// delete the message queue we created
if( iv_msgQ )
{
msg_q_destroy( iv_msgQ );
}
// should kill the task we spawned, but that isn't needed right now
TRACDCOMP(g_trac_pnor, "< PnorRP::~PnorRP" );
}
/**
* @brief Initialize the daemon
*/
void PnorRP::initDaemon()
{
TRACUCOMP(g_trac_pnor, "PnorRP::initDaemon> " );
errlHndl_t l_errhdl = NULL;
do
{
// create a message queue
iv_msgQ = msg_q_create();
INITSERVICE::registerShutdownEvent( iv_msgQ,
PNOR::MSG_SHUTDOWN,
INITSERVICE::PNOR_RP_PRIORITY);
// create a Block, passing in the message queue
int rc = mm_alloc_block( iv_msgQ, (void*) BASE_VADDR, TOTAL_SIZE );
if( rc )
{
TRACFCOMP( g_trac_pnor, "PnorRP::initDaemon> Error from mm_alloc_block : rc=%d", rc );
/*@
* @errortype
* @moduleid PNOR::MOD_PNORRP_INITDAEMON
* @reasoncode PNOR::RC_EXTERNAL_ERROR
* @userdata1 Requested Address
* @userdata2 rc from mm_alloc_block
* @devdesc PnorRP::initDaemon> Error from mm_alloc_block
* @custdesc A problem occurred while accessing the boot flash.
*/
l_errhdl = new ERRORLOG::ErrlEntry(
ERRORLOG::ERRL_SEV_UNRECOVERABLE,
PNOR::MOD_PNORRP_INITDAEMON,
PNOR::RC_EXTERNAL_ERROR,
TO_UINT64(BASE_VADDR),
TO_UINT64(rc),
true /*Add HB SW Callout*/);
l_errhdl->collectTrace(PNOR_COMP_NAME);
break;
}
//Register this memory range to be FLUSHed during a shutdown.
INITSERVICE::registerBlock(reinterpret_cast<void*>(BASE_VADDR),
TOTAL_SIZE,PNOR_PRIORITY);
//Find and read the TOC in the PNOR to compute the sections and set
//their correct permissions
l_errhdl = findTOC();
if( l_errhdl )
{
TRACFCOMP(g_trac_pnor, ERR_MRK"PnorRP::initDaemon: Failed to findTOC");
errlCommit(l_errhdl, PNOR_COMP_ID);
INITSERVICE::doShutdown(PNOR::RC_FINDTOC_FAILED);
}
l_errhdl = readTOC();
if( l_errhdl )
{
TRACFCOMP(g_trac_pnor, ERR_MRK"PnorRP::initDaemon: Failed to readTOC");
break;
}
l_errhdl = setSideInfo ();
if(l_errhdl)
{
TRACFCOMP(g_trac_pnor, "PnorRP::initDaemon> setSideInfo failed");
break;
}
// @TODO RTC 168021 Remove the non-secure extension path and
// always used the converged HBB extension path.
// If secured, extend base image (HBB) when Hostboot first starts.
// Since HBB is never re-loaded, inhibit extending this image in
// runtime code.
#ifndef __HOSTBOOT_RUNTIME
#ifdef CONFIG_SECUREBOOT
if(!SECUREBOOT::enabled())
{
// If compliant bootloader was present, it saved the HBB header
// to a known location accessible to HBB. Until that bootloader
// is widely distributed, when in non-secure mode in lab,
// manufacturing, etc., read the header directly from PNOR.
PNOR::SideInfo_t pnorInfo = {PNOR::WORKING};
l_errhdl = PnorRP::getSideInfo(PNOR::WORKING, pnorInfo);
if(l_errhdl != nullptr)
{
break;
}
const SectionData_t* const pHbb = &iv_TOC[PNOR::HB_BASE_CODE];
const bool ecc = (pHbb->integrity == FFS_INTEG_ECC_PROTECT) ?
true :false;
uint8_t pHeader[PAGESIZE] = {0};
uint64_t fatalError = 0;
l_errhdl = readFromDevice(
pnorInfo.hbbAddress,
pHbb->chip,
ecc,
pHeader,
fatalError);
// If fatalError != 0 there is an uncorrectable ECC error (UE).
// In that case, continue on with inaccurate data, as
// readFromDevice API will initiate a shutdown
if(l_errhdl != nullptr)
{
break;
}
// Cache the header
(void)SECUREBOOT::baseHeader().setNonSecurely(
pHeader);
}
#endif
#endif
// start task to wait on the queue
task_create( wait_for_message, NULL );
} while(0);
if( l_errhdl )
{
iv_startupRC = l_errhdl->reasonCode();
errlCommit(l_errhdl,PNOR_COMP_ID);
}
// Not supporting PNOR error in VPO
#ifndef CONFIG_VPO_COMPILE
// call ErrlManager function - tell him that PNOR is ready!
ERRORLOG::ErrlManager::errlResourceReady(ERRORLOG::PNOR);
#endif
TRACUCOMP(g_trac_pnor, "< PnorRP::initDaemon" );
}
errlHndl_t PnorRP::getSideInfo( PNOR::SideId i_side,
PNOR::SideInfo_t& o_info)
{
errlHndl_t l_err = NULL;
do
{
//check to make sure side id is valid
if (i_side != PNOR::INVALID_SIDE)
{
memcpy (&o_info, &iv_side[i_side], sizeof(SideInfo_t));
}
else
{
TRACFCOMP(g_trac_pnor, "Side:%d is currently not supported",
(int)i_side);
/*@
* @errortype
* @moduleid PNOR::MOD_PNORRP_GETSIDEINFO
* @reasoncode PNOR::RC_INVALID_PNOR_SIDE
* @userdata1 Requested SIDE
* @userdata2 0
* @devdesc PnorRP::getSideInfo> Side not supported
* @custdesc Processor NOR flash: Side not supported
*/
l_err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
PNOR::MOD_PNORRP_GETSIDEINFO,
PNOR::RC_INVALID_PNOR_SIDE,
TO_UINT64(i_side),
0,true);
break;
}
} while (0);
return l_err;
}
/**
* @brief Return the size and address of a given section of PNOR data
*/
errlHndl_t PnorRP::getSectionInfo( PNOR::SectionId i_section,
PNOR::SectionInfo_t& o_info )
{
//TRACDCOMP(g_trac_pnor, "PnorRP::getSectionInfo> i_section=%d", i_section );
errlHndl_t l_errhdl = NULL;
PNOR::SectionId id = i_section;
do
{
// Abort this operation if we had a startup failure
uint64_t rc = 0;
if( didStartupFail(rc) )
{
TRACFCOMP( g_trac_pnor, "PnorRP::getSectionInfo> RP not properly initialized, failing : rc=%X", rc );
/*@
* @errortype
* @moduleid PNOR::MOD_PNORRP_GETSECTIONINFO
* @reasoncode PNOR::RC_STARTUP_FAIL
* @userdata1 Requested Section
* @userdata2 Startup RC
* @devdesc PnorRP::getSectionInfo> RP not properly initialized
* @custdesc Processor NOR flash:
* A problem occurred while accessing the boot flash.
*/
l_errhdl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
PNOR::MOD_PNORRP_GETSECTIONINFO,
PNOR::RC_STARTUP_FAIL,
TO_UINT64(i_section),
rc,
true /*Add HB SW Callout*/);
l_errhdl->collectTrace(PNOR_COMP_NAME);
// set the return section to our invalid data
id = PNOR::INVALID_SECTION;
break;
}
// inhibit any attempt to getSectionInfo on any attribute override
// sections if secureboot is enabled
bool l_inhibited = isInhibitedSection(id);
// Zero-length means the section is invalid
if( 0 == iv_TOC[id].size
// attribute overrides inhibited by secure boot
|| l_inhibited
)
{
TRACFCOMP( g_trac_pnor, "PnorRP::getSectionInfo> Invalid Section Requested : i_section=%d", i_section );
#ifdef CONFIG_SECUREBOOT
if (l_inhibited)
{
TRACFCOMP( g_trac_pnor, "PnorRP::getSectionInfo> "
"attribute override inhibited by secureboot");
}
#endif
uint64_t size = iv_TOC[i_section].size;
TRACFCOMP(g_trac_pnor, "o_info={ id=%d, size=%d }",
iv_TOC[i_section].id, size );
/*@
* @errortype
* @moduleid PNOR::MOD_PNORRP_GETSECTIONINFO
* @reasoncode PNOR::RC_INVALID_SECTION
* @userdata1 Size of section
* @userdata2[0:7] TOC used
* @userdata2[8:15] Inhibited flag
* @userdata2[16:23] Requested Section
* @devdesc PnorRP::getSectionInfo> Invalid Address for
* read/write or prohibited by secureboot
* @custdesc A problem occurred while accessing the boot
* flash.
*/
l_errhdl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
PNOR::MOD_PNORRP_GETSECTIONINFO,
PNOR::RC_INVALID_SECTION,
size,
TO_UINT64(FOUR_UINT8_TO_UINT32(
iv_TOC_used,
l_inhibited,
i_section,
0)),
true /*Add HB SW Callout*/);
l_errhdl->collectTrace(PNOR_COMP_NAME);
// set the return section to our invalid data
id = PNOR::INVALID_SECTION;
break;
}
if (PNOR::INVALID_SECTION != id)
{
TRACDCOMP( g_trac_pnor, "PnorRP::getSectionInfo: i_section=%d, id=%d", i_section, iv_TOC[i_section].id );
// copy my data into the external format
o_info.id = iv_TOC[id].id;
o_info.name = SectionIdToString(id);
#ifdef CONFIG_SECUREBOOT
o_info.secure = iv_TOC[id].secure;
o_info.secureProtectedPayloadSize = 0; // for non secure sections
// the protected payload size
// defaults to zero
// If a secure section and has a secure header handle secure
// sections in SPnorRP's address space
if (o_info.secure)
{
uint8_t* l_vaddr = reinterpret_cast<uint8_t*>(iv_TOC[id].virtAddr);
// By adding VMM_VADDR_SPNOR_DELTA twice we can translate a pnor
// address into a secure pnor address, since pnor, temp, and spnor
// spaces are equidistant.
// See comments in SPnorRP::verifySections() method in spnorrp.C
// and the definition of VMM_VADDR_SPNOR_DELTA in vmmconst.h
// for specifics.
o_info.vaddr = reinterpret_cast<uint64_t>(l_vaddr)
+ VMM_VADDR_SPNOR_DELTA
+ VMM_VADDR_SPNOR_DELTA;
// Get size of the secured payload for the secure section
// Note: the payloadSize we get back is untrusted because
// we are parsing the header in pnor (non secure space).
size_t payloadTextSize = 0;
// Do an existence check on the container to see if it's non-empty
// and has valid beginning bytes. For optional Secure PNOR sections.
if (PNOR::cmpSecurebootMagicNumber(l_vaddr))
{
SECUREBOOT::ContainerHeader l_conHdr(l_vaddr);
payloadTextSize = l_conHdr.payloadTextSize();
assert(payloadTextSize > 0,"Non-zero payload text size expected.");
}
else
{
uint32_t l_badMagicHeader = 0;
memcpy(&l_badMagicHeader, l_vaddr, sizeof(ROM_MAGIC_NUMBER));
TRACFCOMP( g_trac_pnor, ERR_MRK"PnorRP::getSectionInfo: magic number not valid to parse container for section = %s magic number = 0x%X",
o_info.name, l_badMagicHeader);
/*@
* @errortype
* @severity ERRORLOG::ERRL_SEV_UNRECOVERABLE
* @moduleid PNOR::MOD_PNORRP_GETSECTIONINFO
* @reasoncode PNOR::RC_BAD_SECURE_MAGIC_NUM
* @userdata1 Requested Section
* @userdata2 Bad magic number
* @devdesc PNOR section does not have the known secureboot magic number
* @custdesc Corrupted flash image or firmware error during system boot
*/
l_errhdl = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
PNOR::MOD_PNORRP_GETSECTIONINFO,
PNOR::RC_BAD_SECURE_MAGIC_NUM,
TO_UINT64(i_section),
TO_UINT64(l_badMagicHeader),
true /*Add HB SW Callout*/);
l_errhdl->collectTrace(PNOR_COMP_NAME);
break;
}
// skip secure header for secure sections at this point in time
o_info.vaddr += PAGESIZE;
// now that we've skipped the header we also need to adjust the
// size of the section to reflect that.
// Note: For unsecured sections, the header skip and size decrement
// was done previously in pnor_common.C
o_info.size -= PAGESIZE;
// cache the value in SectionInfo struct so that we can
// parse the container header less often
o_info.secureProtectedPayloadSize = payloadTextSize;
}
else
#endif
{
o_info.vaddr = iv_TOC[id].virtAddr;
}
o_info.flashAddr = iv_TOC[id].flashAddr;
o_info.size = iv_TOC[id].size;
o_info.eccProtected = ((iv_TOC[id].integrity & FFS_INTEG_ECC_PROTECT)
!= 0) ? true : false;
o_info.sha512Version = ((iv_TOC[id].version & FFS_VERS_SHA512)
!= 0) ? true : false;
o_info.sha512perEC = ((iv_TOC[id].version & FFS_VERS_SHA512_PER_EC)
!= 0) ? true : false;
o_info.readOnly = ((iv_TOC[id].misc & FFS_MISC_READ_ONLY)
!= 0) ? true : false;
o_info.reprovision = ((iv_TOC[id].misc & FFS_MISC_REPROVISION)
!= 0) ? true : false;
}
} while(0);
return l_errhdl;
}
/*
* @brief Finds the toc locations based on hostboot base address
*/
errlHndl_t PnorRP::findTOC()
{
TRACDCOMP(g_trac_pnor, ENTER_MRK"PnorRP::findTOC...");
errlHndl_t l_err = NULL;
do {
const uint32_t l_shiftAmount = 32;
uint64_t l_chip = 0;
uint64_t l_fatalError = 0;
bool l_foundTOC = false;
uint64_t l_toc = PNOR::PNOR_SIZE - 1;
ffs_hdr* l_ffs_hdr = 0;
uint64_t l_hbbAddr = 0;
uint8_t l_tocBuffer [PAGESIZE];
//get the HBB Address we booted from
l_err = PNOR::mmioToPhysicalOffset(l_hbbAddr);
if (l_err)
{
TRACFCOMP(g_trac_pnor, "PnorRP::findTOC> mmioToPhysicalOffset failed");
break;
}
//if we booted from a lower address, then we need to increment
//sides as we find tocs, otherwise decrement.
bool l_inc = (ALIGN_DOWN_X(l_hbbAddr, l_shiftAmount*MEGABYTE) == 0);
uint64_t l_tempHBB = l_hbbAddr;
//while TOC not found and we are within the flash size
while((!l_foundTOC) && (l_tempHBB > 0) && (l_tempHBB < PNOR_SIZE))
{
//Align HBB down -- looking at 0x0 or 0x2000000
l_toc = ALIGN_DOWN_X(l_tempHBB, l_shiftAmount*MEGABYTE);
l_err = readFromDevice(l_toc, l_chip, false, l_tocBuffer,
l_fatalError);
if(l_err)
{
TRACFCOMP(g_trac_pnor,"findTOC: readFromDevice failed "
"searching for primaryTOC");
break;
}
l_ffs_hdr = (ffs_hdr*)l_tocBuffer;
l_foundTOC = ((l_ffs_hdr->magic == FFS_MAGIC) &&
(PNOR::pnor_ffs_checksum(l_ffs_hdr,FFS_HDR_SIZE) == 0));
if (!l_foundTOC)
{
//If TOC not found at 0x0 or 0x2000000
//Align HBB down + 8000 -- looking at 0x8000 or 0x2008000
l_toc += TOC_SIZE;
l_err = readFromDevice(l_toc, l_chip, false, l_tocBuffer,
l_fatalError);
if(l_err)
{
TRACFCOMP(g_trac_pnor,"findTOC: readFromDevice failed "
"searching for backupTOC for A-B-D arrangement");
break;
}
l_ffs_hdr = (ffs_hdr*)l_tocBuffer;
l_foundTOC =
((l_ffs_hdr->magic == FFS_MAGIC) &&
(PNOR::pnor_ffs_checksum(l_ffs_hdr, FFS_HDR_SIZE) == 0));
}
if (!l_foundTOC)
{
//If toc not found at 0x8000 or 0x2008000
//Align HBB up (l_shiftAmount) - 8000
// -- looking at 0x1FF8000 or 0x3FF8000
l_toc = ALIGN_X(l_tempHBB, l_shiftAmount*MEGABYTE);
l_toc -= TOC_SIZE;
l_err = readFromDevice(l_toc, l_chip, false, l_tocBuffer,
l_fatalError);
if(l_err)
{
TRACFCOMP(g_trac_pnor,"findTOC: readFromDevice failed"
"searching for backupTOC for A-D-B arrangement");
break;
}
l_ffs_hdr = (ffs_hdr*)l_tocBuffer;
l_foundTOC =
((l_ffs_hdr->magic == FFS_MAGIC) &&
(PNOR::pnor_ffs_checksum(l_ffs_hdr, FFS_HDR_SIZE) == 0));
}
//Setup for next time -- look for the other side
l_tempHBB = (l_inc) ? (l_tempHBB + l_shiftAmount*MEGABYTE) :
(l_tempHBB - l_shiftAmount*MEGABYTE);
}
if(l_err)
{
break;
}
//found at least one TOC
if(l_foundTOC)
{
TRACDCOMP(g_trac_pnor, "findTOC> found at least one toc at 0x%X", l_toc);
//look for BACKUP_PART and read it
uint64_t l_backupTOC = INVALID_OFFSET;
PNOR::findPhysicalOffset(l_ffs_hdr,"BACKUP_PART",l_backupTOC);
//figure out if the toc found belongs to the side we booted from
//or if it belongs to the other side
uint64_t l_foundHBB;
PNOR::findPhysicalOffset(l_ffs_hdr, "HBB", l_foundHBB);
bool l_isActiveTOC = (l_foundHBB == l_hbbAddr);
l_isActiveTOC = 1; //@FIXME RTC 138268 needs multiple sides support
printk("forced to true - l_isActiveTOC=%d\n",
l_isActiveTOC); //@FIXME RTC 138268
#ifdef CONFIG_PNOR_TWO_SIDE_SUPPORT
uint64_t l_otherPrimaryTOC = INVALID_OFFSET;
uint64_t l_otherBackupTOC = INVALID_OFFSET;
uint8_t l_otherPrimaryTOCBuff [PAGESIZE];
uint8_t l_otherBackupTOCBuff [PAGESIZE];
uint8_t l_backupTOCBuffer [PAGESIZE];
//look for OTHER_SIDE
PNOR::findPhysicalOffset(l_ffs_hdr, "OTHER_SIDE",l_otherPrimaryTOC);
//reading to look for OTHER_SIDE's backup
bool l_foundOtherBackup = false;
bool l_foundOtherPrimary = false;
if (l_otherPrimaryTOC != INVALID_OFFSET)
{
l_err = readFromDevice(l_otherPrimaryTOC,l_chip,
false, l_otherPrimaryTOCBuff,l_fatalError);
l_ffs_hdr = (ffs_hdr*)l_otherPrimaryTOCBuff;
if(l_err)
{
TRACFCOMP(g_trac_pnor, "findTOC: readFromDevice failed"
" while looking for other side's primary TOC");
errlCommit(l_err, PNOR_COMP_ID);
}
else if ((l_ffs_hdr->magic == FFS_MAGIC) &&
(PNOR::pnor_ffs_checksum(l_ffs_hdr, FFS_HDR_SIZE)==0))
{
//if otherPrimaryTOC is valid,
//then we can find it's backup
PNOR::findPhysicalOffset(l_ffs_hdr, "BACKUP_PART",
l_otherBackupTOC);
l_foundOtherPrimary = true;
}
}
if ((!l_foundOtherPrimary) && (l_backupTOC != INVALID_OFFSET))
{
//if otherPrimaryTOC is not valid, find the other backup
//through BACKUP_PART's OTHER_SIDE
l_err = readFromDevice (l_backupTOC, l_chip, false,
l_backupTOCBuffer, l_fatalError);
l_ffs_hdr = (ffs_hdr*)l_backupTOCBuffer;
if (l_err)
{
TRACFCOMP(g_trac_pnor, "findTOC: readFromDevice failed"
" while reading for backup TOC");
errlCommit(l_err, PNOR_COMP_ID);
}
else if ((l_ffs_hdr->magic == FFS_MAGIC)
&& (PNOR::pnor_ffs_checksum(l_ffs_hdr,FFS_HDR_SIZE)==0))
{
PNOR::findPhysicalOffset(l_ffs_hdr,"OTHER_SIDE",
l_otherBackupTOC);
l_foundOtherBackup = true;
}
}
//figure out if other side's toc belongs to the side we booted from
if(l_foundOtherPrimary)
{
PNOR::findPhysicalOffset((ffs_hdr*)l_otherPrimaryTOCBuff, "HBB",
l_foundHBB);
}
else if (l_foundOtherBackup)
{
l_err = readFromDevice (l_otherBackupTOC, l_chip, false,
l_otherBackupTOCBuff, l_fatalError);
l_ffs_hdr = (ffs_hdr*)l_backupTOCBuffer;
if (l_err)
{
TRACFCOMP(g_trac_pnor, "findTOC: readFromDevice failed"
" while reading other side's backup TOC");
errlCommit(l_err, PNOR_COMP_ID);
}
else if ((l_ffs_hdr->magic == FFS_MAGIC) &&
(PNOR::pnor_ffs_checksum(l_ffs_hdr,FFS_HDR_SIZE)==0))
{
PNOR::findPhysicalOffset(l_ffs_hdr,"HBB",
l_foundHBB);
}
}
bool l_isOtherActiveTOC = (l_foundHBB == l_hbbAddr);
if (l_isActiveTOC)
{
iv_TocOffset[WORKING].first = l_toc;
iv_TocOffset[WORKING].second = l_backupTOC;
iv_TocOffset[ALTERNATE].first = l_otherPrimaryTOC;
iv_TocOffset[ALTERNATE].second = l_otherBackupTOC;
}
else if (l_isOtherActiveTOC)
{
iv_TocOffset[WORKING].first = l_otherPrimaryTOC;
iv_TocOffset[WORKING].second = l_otherBackupTOC;
iv_TocOffset[ALTERNATE].first = l_toc;
iv_TocOffset[ALTERNATE].second = l_backupTOC;
}
else
{
TRACFCOMP(g_trac_pnor,"findTOC>No valid TOC found, looked"
"at following addresses PrimaryTOC:0x%08X, BackupTOC:"
"0x%08X", l_toc, l_backupTOC);
INITSERVICE::doShutdown(PNOR::RC_PARTITION_TABLE_CORRUPTED);
}
TRACFCOMP(g_trac_pnor,"findTOC>activePrimary:0x%X,activeBackup:0x%X"
"altPrimary:0x%X, altBackup:0x%X",iv_TocOffset[WORKING].first,
iv_TocOffset[WORKING].second, iv_TocOffset[ALTERNATE].first,
iv_TocOffset[ALTERNATE].second);
#else
if (l_isActiveTOC)
{
iv_TocOffset[WORKING].first = l_toc;
iv_TocOffset[WORKING].second = l_backupTOC;
TRACFCOMP(g_trac_pnor, "findTOC> activePrimary:0x%X, "
"activeBackup:0x%X", iv_TocOffset[WORKING].first,
iv_TocOffset[WORKING].second);
}
else
{
TRACFCOMP(g_trac_pnor,"findTOC>No valid TOC found, looked"
"at following addresses PrimaryTOC:0x%08X, BackupTOC:"
"0x%08X", l_toc, l_backupTOC);
INITSERVICE::doShutdown(PNOR::RC_PARTITION_TABLE_CORRUPTED);
}
#endif
}
else
{
printk("no valid toc - l_foundTOC=%d, l_toc=%lX, l_tocBuffer=%p\n",
l_foundTOC, l_toc, l_tocBuffer);
sync();
//no valid TOC found
TRACFCOMP(g_trac_pnor, "No valid TOC found");
if (l_err)
{
errlCommit(l_err, PNOR_COMP_ID);
}
INITSERVICE::doShutdown(PNOR::RC_PARTITION_TABLE_NOT_FOUND);
}
} while (0);
TRACDCOMP(g_trac_pnor, EXIT_MRK"findTOC");
return l_err;
}
/**
* @brief Read the TOC and store section information
*/
errlHndl_t PnorRP::readTOC()
{
TRACUCOMP(g_trac_pnor, "PnorRP::readTOC>" );
errlHndl_t l_errhdl = nullptr;
errlHndl_t l_primary_errhdl = nullptr;
errlHndl_t l_secondary_errhdl = nullptr;
uint8_t* toc0Buffer = new uint8_t[PAGESIZE];
uint8_t* toc1Buffer = new uint8_t[PAGESIZE];
PNOR::SectionData_t l_secondary_TOC[PNOR::NUM_SECTIONS+1];
uint64_t fatal_error = 0;
do {
//Initialize toc bufferes to invalid value
//If these buffers are not read from device,
//then parseTOC will see invalid data
memset(toc0Buffer, 0xFF, PAGESIZE);
memset(toc1Buffer, 0xFF, PAGESIZE);
//If the first toc is at a valid offset try to read and parse it
if (iv_TocOffset[WORKING].first != INVALID_OFFSET)
{
l_errhdl = readFromDevice(iv_TocOffset[WORKING].first, 0, false,
toc0Buffer, fatal_error );
if (l_errhdl)
{
TRACFCOMP(g_trac_pnor,"readTOC:readFromDevice failed for TOC0");
break;
}
l_primary_errhdl = PNOR::parseTOC(toc0Buffer, iv_TOC);
//For now assume TOC_0 worked and we are using it
iv_TOC_used = TOC_0;
}
//If the second toc is at a valid offset try to read and parse it
if (iv_TocOffset[WORKING].second != INVALID_OFFSET)
{
l_errhdl = readFromDevice(iv_TocOffset[WORKING].second, 0, false,
toc1Buffer, fatal_error );
if (l_errhdl)
{
TRACFCOMP(g_trac_pnor,"readTOC:readFromDevice failed for TOC1");
break;
}
l_secondary_errhdl = PNOR::parseTOC(toc1Buffer, l_secondary_TOC);
}
//if at least one of them is good, only report errors as informational
if((l_primary_errhdl != nullptr) && (l_secondary_errhdl == nullptr))
{
//Found an error in primary TOC, report it
//@TODO RTC:144079 Try to fix PNOR for detected TOC failures
//Set the error severity to INFORMATIONAL
l_primary_errhdl->setSev(ERRORLOG::ERRL_SEV_INFORMATIONAL);
//commit error logs for corrupted TOC
errlCommit(l_primary_errhdl, PNOR_COMP_ID);
//use the secondary TOC if first is bad
iv_TOC_used = TOC_1;
memcpy(iv_TOC, l_secondary_TOC, (sizeof(l_secondary_TOC)));
}
//If we found an error in the secondary TOC
else if((l_secondary_errhdl != nullptr) && (l_primary_errhdl == nullptr))
{
//@TODO RTC:144079 Try to fix PNOR for detected TOC failures
//Set the error severity to INFORMATIONAL
l_secondary_errhdl->setSev(ERRORLOG::ERRL_SEV_INFORMATIONAL);
//commit error logs for corrupted TOC
errlCommit(l_secondary_errhdl, PNOR_COMP_ID);
}
//In the case that both TOCs failing, have errors shut down the system
else if(l_primary_errhdl != nullptr && l_secondary_errhdl != nullptr)
{
//commit both error logs for each corrupted TOC
l_primary_errhdl->setSev(ERRORLOG::ERRL_SEV_UNRECOVERABLE);
errlCommit(l_primary_errhdl, PNOR_COMP_ID);
l_secondary_errhdl->setSev(ERRORLOG::ERRL_SEV_UNRECOVERABLE);
errlCommit(l_secondary_errhdl, PNOR_COMP_ID);