summaryrefslogtreecommitdiffstats
path: root/src/bootloader/bl_pnorAccess.C
blob: b8db406a0af20e89a9ae46b2a02f0d69f546b5b0 (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/bootloader/bl_pnorAccess.C $                              */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015,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                                                     */

#include <bootloader/bl_pnorAccess.H>
#include <bootloader/bootloader_trace.H>
#include <bootloader/hbblreasoncodes.H>
#include <util/singleton.H>
#include <bootloader/bootloader.H>
#ifdef PNORUTILSTEST_H
#define BOOTLOADER_TRACE(args) TRACFCOMP(g_trac_pnor,"##args")
#define BOOTLOADER_TRACE_W_BRK(args) TRACFCOMP(g_trac_pnor,"##args")
#endif

/**
* @brief Takes in a buffer containing a ToC, as well as a base address
         Returns out a boolean whether or not this toc was valid
         also returns a SectionData_t struct if the toc was valid
*/
void bl_pnorAccess::readTOC(uint8_t i_tocBuffer[PNOR::TOC_SIZE],
                            uint32_t& o_errCode,
                            PNOR::SectionData_t * o_TOC,
                            uint64_t& o_pnorStart,
                            uint64_t i_pnorEnd)
{
    do
    {
        o_errCode = PNOR::NO_ERROR;
        o_pnorStart = NULL;

        ffs_hdr* l_ffs_hdr = NULL;
        //zero out the section data for each section
        PNOR::initializeSections(o_TOC);

        BOOTLOADER_TRACE(BTLDR_TRC_PA_READTOC_ZEROSECTION_RTN);

        //make sure that the buffer is not null
        PNOR::checkForNullBuffer(i_tocBuffer, o_errCode, l_ffs_hdr);

        if(o_errCode != PNOR::NO_ERROR)
        {
            BOOTLOADER_TRACE_W_BRK(BTLDR_TRC_PA_READTOC_CHKNULLBUFFER_NULL);
            // Set TI information but caller decides to TI or not
            /*@
             * @errortype
             * @moduleid     Bootloader::MOD_PNORACC_READTOC
             * @reasoncode   Bootloader::RC_CHK_NULL_BUFFER
             * @userdata1[0:15]   TI_WITH_SRC
             * @userdata1[16:31]  TI_BOOTLOADER
             * @userdata1[32:63]  Failing address = 0
             * @userdata2[0:31]   Pointer to TOC buffer
             * @userdata2[32:63]  Error code
             * @devdesc      Invalid TOC buffer pointer
             * @custdesc     A problem occurred while running processor
             *               boot code.
             */
            bl_terminate(Bootloader::MOD_PNORACC_READTOC,
                         Bootloader::RC_CHK_NULL_BUFFER,
                         reinterpret_cast<uint64_t>(i_tocBuffer),
                         o_errCode,
                         false);
            break;
        }

        BOOTLOADER_TRACE(BTLDR_TRC_PA_READTOC_CHECKNULLBUFFER_RTN);

        //Subtract the size of the pnor from the end address to find the start
        o_pnorStart = i_pnorEnd -
                        (l_ffs_hdr->block_size * l_ffs_hdr->block_count) + 1;

        //Do a checksum on the header
        if(PNOR::pnor_ffs_checksum(l_ffs_hdr, FFS_HDR_SIZE) != 0)
        {
            o_errCode |= PNOR::CHECKSUM_ERR;
        }

        if(o_errCode != PNOR::NO_ERROR)
        {
            BOOTLOADER_TRACE_W_BRK(BTLDR_TRC_PA_READTOC_HDRCHECKSUM_ERR);
            // Set TI information but caller decides to TI or not
            /*@
             * @errortype
             * @moduleid     Bootloader::MOD_PNORACC_READTOC
             * @reasoncode   Bootloader::RC_HDR_CHECKSUM_ERR
             * @userdata1[0:15]   TI_WITH_SRC
             * @userdata1[16:31]  TI_BOOTLOADER
             * @userdata1[32:63]  Failing address = 0
             * @userdata2[0:31]   Pointer to FFS header
             * @userdata2[32:63]  Error code
             * @devdesc      FFS header checksum error
             * @custdesc     A problem occurred while running processor
             *               boot code.
             */
            bl_terminate(Bootloader::MOD_PNORACC_READTOC,
                         Bootloader::RC_HDR_CHECKSUM_ERR,
                         reinterpret_cast<uint64_t>(l_ffs_hdr),
                         o_errCode,
                         false);
            break;
        }

        BOOTLOADER_TRACE(BTLDR_TRC_PA_READTOC_HDRCHECKSUM_RTN);

        //Check out the header to make sure its all valid
        PNOR::checkHeader(l_ffs_hdr, o_errCode);

        if(o_errCode != PNOR::NO_ERROR)
        {
            BOOTLOADER_TRACE_W_BRK(BTLDR_TRC_PA_READTOC_CHECKHEADER_ERR);
            // Set TI information but caller decides to TI or not
            /*@
             * @errortype
             * @moduleid     Bootloader::MOD_PNORACC_READTOC
             * @reasoncode   Bootloader::RC_CHECK_HEADER_ERR
             * @userdata1[0:15]   TI_WITH_SRC
             * @userdata1[16:31]  TI_BOOTLOADER
             * @userdata1[32:63]  Failing address = 0
             * @userdata2[0:31]   Pointer to FFS header
             * @userdata2[32:63]  Error code
             * @devdesc      Check FFS header error(s)
             * @custdesc     A problem occurred while running processor
             *               boot code.
             */
            bl_terminate(Bootloader::MOD_PNORACC_READTOC,
                         Bootloader::RC_CHECK_HEADER_ERR,
                         reinterpret_cast<uint64_t>(l_ffs_hdr),
                         o_errCode,
                         false);
            break;
        }
        BOOTLOADER_TRACE(BTLDR_TRC_PA_READTOC_CHECKHEADER_RTN);


        //if an error is found with an entry we use this variable to hold
        //the value of the entry. That way we can record which entry is causing
        //an issue.
        ffs_entry* l_err_entry = NULL;

        //parse through the entries and check for any errors
        PNOR::parseEntries(l_ffs_hdr, o_errCode, o_TOC, l_err_entry);

        if(o_errCode != PNOR::NO_ERROR)
        {
            BOOTLOADER_TRACE_W_BRK(BTLDR_TRC_PA_READTOC_PARSEENTRIES_ERR);
            // Set TI information but caller decides to TI or not
            /*@
             * @errortype
             * @moduleid     Bootloader::MOD_PNORACC_READTOC
             * @reasoncode   Bootloader::RC_PARSE_ENTRIES_ERR
             * @userdata1[0:15]   TI_WITH_SRC
             * @userdata1[16:31]  TI_BOOTLOADER
             * @userdata1[32:63]  Failing address = 0
             * @userdata2[0:31]   Pointer to FFS entry with error
             * @userdata2[32:63]  Error code
             * @devdesc      Parse FFS entries error
             * @custdesc     A problem occurred while running processor
             *               boot code.
             */
            bl_terminate(Bootloader::MOD_PNORACC_READTOC,
                         Bootloader::RC_PARSE_ENTRIES_ERR,
                         reinterpret_cast<uint64_t>(l_err_entry),
                         o_errCode,
                         false);
            break;
        }
        BOOTLOADER_TRACE(BTLDR_TRC_PA_READTOC_PARSEENTRIES_RTN);


    } while(0);
}

void bl_pnorAccess::findTOC(uint64_t i_pnorEnd, PNOR::SectionData_t * o_TOC,
                            uint32_t& o_errCode,  uint8_t& o_tocUsed,
                            uint64_t& o_pnorStart)
{
    uint8_t *l_tocBuffer = Bootloader::g_blScratchSpace;
    do
    {
        //@TODO RTC:138268 Set up multiple side of PNOR for bootloader
        o_errCode = 0;
        o_tocUsed = 0;
        //Copy Table of Contents from PNOR flash to a local buffer
        //The first TOC is 2 TOC sizes back from the end of the flash (+ 1)
        Bootloader::handleMMIO(i_pnorEnd - PNOR::TOC_OFFSET_FROM_TOP_OF_FLASH,
                    reinterpret_cast<uint64_t>(l_tocBuffer),
                    (PNOR::TOC_SIZE),
                    Bootloader::WORDSIZE);

        BOOTLOADER_TRACE(BTLDR_TRC_PA_FINDTOC_TOC1_HANDLEMMIO_RTN);

        readTOC(l_tocBuffer, o_errCode, o_TOC, o_pnorStart, i_pnorEnd);

        if(o_errCode == PNOR::NO_ERROR)
        {
            BOOTLOADER_TRACE(BTLDR_TRC_PA_FINDTOC_TOC1_READTOC_RTN);
            o_tocUsed = 0;
            break;
        }
        else
        {
            // @TODO RTC:164445 Can remove if there is a way to find another TOC
            // TI with data from readTOC
            terminateExecuteTI();

            //If the first toc was invalid, look for the backup in the start
            Bootloader::handleMMIO(o_pnorStart,
                              reinterpret_cast<uint64_t>(l_tocBuffer),
                              (PNOR::TOC_SIZE),
                              Bootloader::WORDSIZE);

            o_errCode = 0;
            readTOC(l_tocBuffer, o_errCode, o_TOC, o_pnorStart, i_pnorEnd);
            if(o_errCode == PNOR::NO_ERROR)
            {
                o_tocUsed = 1;
                break;
            }
            BOOTLOADER_TRACE_W_BRK(BTLDR_TRC_PA_FINDTOC_READTOC_ERR);
            // TI with data from readTOC
            terminateExecuteTI();
        }

        break;
    }while(0);
}

/**
 * @brief Get the hostboot base image
 */
void bl_pnorAccess::getHBBSection(uint64_t i_pnorEnd,
                                  PNOR::SectionData_t& o_hbbSection,
                                  uint32_t& o_errCode,
                                  uint8_t& o_tocUsed,
                                  uint64_t& o_pnorStart)
{
    BOOTLOADER_TRACE(BTLDR_TRC_PA_GETHBBSECTION_START);
    do
    {
        PNOR::SectionData_t l_TOC[PNOR::NUM_SECTIONS+1];

        findTOC(i_pnorEnd, l_TOC, o_errCode, o_tocUsed, o_pnorStart);

        if(o_errCode != PNOR::NO_ERROR)
        {
            // Note findTOC should have TI'd so won't get here
            BOOTLOADER_TRACE_W_BRK(BTLDR_TRC_PA_GETHBBSECTION_FINDTOC_ERR);
            break;
        }
        o_hbbSection = l_TOC[PNOR::HB_BASE_CODE];

        if(o_hbbSection.flashAddr == PNOR::INVALID_FLASH_OFFSET)
        {
            o_errCode = PNOR::NO_HBB_SECTION;
            BOOTLOADER_TRACE_W_BRK(BTLDR_TRC_PA_GETHBBSECTION_FINDTOC_NOHBB);
            /*@
             * @errortype
             * @moduleid     Bootloader::MOD_PNORACC_GETHBBSECT
             * @reasoncode   Bootloader::RC_NO_HBB_IN_TOC
             * @userdata1[0:15]   TI_WITH_SRC
             * @userdata1[16:31]  TI_BOOTLOADER
             * @userdata1[32:63]  Failing address = 0
             * @userdata2[0:31]   Pointer to HBB Section data
             * @userdata2[32:63]  HBB Section flash address
             * @devdesc      Invalid flash address for HBB
             * @custdesc     A problem occurred while running processor
             *               boot code.
             */
            bl_terminate(Bootloader::MOD_PNORACC_GETHBBSECT,
                         Bootloader::RC_NO_HBB_IN_TOC,
                         reinterpret_cast<uint64_t>(&o_hbbSection),
                         o_hbbSection.flashAddr);
            break;
        }
    } while(0);
    BOOTLOADER_TRACE(BTLDR_TRC_PA_GETHBBSECTION_FINDTOC_RTN);

}

OpenPOWER on IntegriCloud