summaryrefslogtreecommitdiffstats
path: root/src/import/tools/imageProcs/p9_ipl_build.C
blob: 45e4018ddda9022702d122de25607c44911d14ce (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/tools/imageProcs/p9_ipl_build.C $                  */
/*                                                                        */
/* OpenPOWER sbe Project                                                  */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2016                             */
/* [+] 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                                                     */
/*------------------------------------------------------------------------------*/
/* *! TITLE : p9_ipl_build.C                                                    */
/* *! DESCRIPTION : Copies RS4 delta ring states from unsigned HW image to DD-  */
//                  specific PNOR SBE image.
/* *! OWNER NAME : Michael Olsen                  cmolsen@us.ibm.com            */
//
/* *! EXTENDED DESCRIPTION :                                                    */
//
/* *! USAGE : To build -                                                        */
//              buildecmdprcd -C "p9_image_help_base.C" -c "p9_xip_image.c" p9_ipl_build.C
//
/* *! ASSUMPTIONS :                                                             */
//    - sysPhase=0 is assumed which puts the SBE image together for IPL.
//
/* *! COMMENTS :                                                                */
//    - .strings and .toc are removed as "a unit" before validating.
//
/*------------------------------------------------------------------------------*/

#include <p9_image_help.H>

#define THIS_HELP  ("\nUSAGE:\n\tp9_ipl_build  -help [anything]\n\t  or\n" \
                    "\tp9_ipl_build \n" \
                    "\t\t<Input/output SBE image file>\n" \
                    "\t\t<Input HW reference image file\n" \
                    "\t\t<DD level [hex value]>\n" )

//  main() input parms:
//  arg1:   Input/output SBE image file
//  arg2:   Input HW image file
//  arg3:   DD level [hex value]
int main( int argc, char* argv[])
{
    int         rc = 0;
    char*        fnImageSbe, *fnImageHw;
    uint32_t    ddLevel = 0;
    uint32_t    fdImageHw = 0;
    struct stat stbuf;
    uint32_t    sizeImageHw;
    void*        imageHw;

    P9_XIP_ERROR_STRINGS(g_errorStrings);

    // ==========================================================================
    // Convert input parms from char to <type>
    // ==========================================================================
    MY_DBG("\n--> Processing Input Parameters...\n");

    if (argc < 4 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--h") == 0)
    {
        MY_INF("%s\n", THIS_HELP);
        return 1;
    }

    // Convert input parms from char to <type>
    //
    fnImageSbe = argv[1];
    fnImageHw  = argv[2];
    ddLevel    = strtol(argv[3], NULL, 16);

    MY_INF("  Input/output SBE image fn = %s\n", fnImageSbe);
    MY_INF("  Input HW image fn         = %s\n", fnImageHw);
    MY_INF("  DD level                  = 0x%02x\n", ddLevel);

    // ==========================================================================
    // Memory map HW reference image.
    // ==========================================================================
    MY_DBG("Memory map HW ref image.\n");
    fdImageHw = open(fnImageHw, O_RDONLY);

    if (fstat(fdImageHw, &stbuf) != 0)
    {
        MY_ERR("Could not fstat the HW ref image file.\n");
        return 1;
    }

    sizeImageHw = stbuf.st_size;
    imageHw = mmap(0, sizeImageHw, PROT_READ, MAP_SHARED, fdImageHw, 0);

    if (imageHw == MAP_FAILED)
    {
        MY_ERR("mmap() of HW ref image failed.\n");
        return 1;
    }

    // ...validate image.
    rc = p9_xip_validate(imageHw, sizeImageHw);

    if (rc)
    {
        MY_ERR("p9_xip_validate() of HW ref image failed: %s\n", P9_XIP_ERROR_STRING(g_errorStrings, rc));
        return 1;
    }

    // Update the SBE image.
    //
    MY_INF("Updating the SBE image... \n");

    rc = ipl_build( fnImageSbe,
                    imageHw,
                    ddLevel );

    if (rc == IMGBUILD_SUCCESS)
    {
        MY_INF("SBE image build was SUCCESSFUL.\n");
    }
    else if (rc == IMGBUILD_RING_SEARCH_EOS_NO_MATCH)
    {
        MY_INF("SBE image build was SUCCESSFUL but no RS4 rings appended (rc=%i).\n", rc);
    }
    else
    {
        MY_ERR("SBE image build was UNSUCCESSFUL (rc=%i).\n", rc);
        return 1;
    }

    close(fdImageHw);

    return rc;

}



//  Parameter list:
//  char      *io_fnImageSbe:   Filename of SBE I/O image
//  void      *i_imageHw:       Pointer to memory mapped HW Reference image
//  uint32_t  i_ddLevel:        DD level
//
int ipl_build( char*      io_fnImageSbe,
               void*      i_imageHw,
               uint32_t  i_ddLevel )
{
    int             rc = 0, rcLoc = 0, rcSearch = 0, countRings = 0;
    void*            ringBuffer = NULL;
    uint32_t        ringBlockSize = 0;
    void*            nextRing = NULL;
    uint8_t         iRingType = 0;
    uint8_t         xipSectionId = 0;
    uint8_t         bDone = 0;

//CMO-When removing sections, mmap SBE image file here first.

#if 0  //CMO-for now.. we probably need different impl for p9
    uint32_t        sizeImage = 0;
    P9XipSection    xipSection;

    // ==========================================================================
    // First, remove all unnecessary sections in the output image.
    // ==========================================================================
    //
    rcLoc1 = sbe_xip_delete_section( i_imageOut, P9_XIP_SECTION_HBBL);
    rcLoc2 = sbe_xip_image_size(i_imageOut, &sizeImage);
    rcLoc =  sbe_xip_validate(i_imageOut, sizeImage);

    if (rcLoc1 || rcLoc2 || rcLoc)
    {
        fprintf(stderr, "_delete_section(.hbbl) (rcLoc1=%i), _image_size() (rcLoc2=%i) and/or _validate() (rcLoc=%i) failed.\n",
                rcLoc1, rcLoc2, rcLoc);
        return IMGBUILD_ERR_SECTION_DELETE;
    }

    fprintf(stdout, "Image size (after .hbbl delete): %i\n", sizeImage);

    rcLoc1 = sbe_xip_delete_section( i_imageOut, P9_XIP_SECTION_RINGS);
    rcLoc2 = sbe_xip_image_size(i_imageOut, &sizeImage);
    rcLoc =  sbe_xip_validate(i_imageOut, sizeImage);

    if (rcLoc1 || rcLoc2 || rcLoc)
    {
        fprintf(stderr,
                "_delete_section(.rings) (rcLoc1=%i), _image_size() (rcLoc2=%i) and/or _validate() (rcLoc=%i) failed.\n", rcLoc1,
                rcLoc2, rcLoc);
        return IMGBUILD_ERR_SECTION_DELETE;
    }

    fprintf(stdout, "Image size (after .rings delete): %i\n", sizeImage);

    rcLoc1 = sbe_xip_delete_section( i_imageOut, P9_XIP_SECTION_OVERLAYS);
    rcLoc2 = sbe_xip_image_size(i_imageOut, &sizeImage);
    rcLoc =  sbe_xip_validate(i_imageOut, sizeImage);

    if (rcLoc1 || rcLoc2 || rcLoc)
    {
        fprintf(stderr,
                "_delete_section(.overlays) (rcLoc1=%i), _image_size() (rcLoc2=%i) and/or _validate() (rcLoc=%i) failed.\n", rcLoc1,
                rcLoc2, rcLoc);
        return IMGBUILD_ERR_SECTION_DELETE;
    }

    fprintf(stdout, "Image size (after .overlays delete): %i\n", sizeImage);

    rcLoc1 = sbe_xip_delete_section( i_imageOut, P9_XIP_SECTION_PIBMEM0);
    rcLoc2 = sbe_xip_image_size(i_imageOut, &sizeImage);
    rcLoc =  sbe_xip_validate(i_imageOut, sizeImage);

    if (rcLoc1 || rcLoc2 || rcLoc)
    {
        fprintf(stderr,
                "_delete_section(.pibmem0) (rcLoc1=%i), _image_size() (rcLoc2=%i) and/or _validate() (rcLoc=%i) failed.\n", rcLoc1,
                rcLoc2, rcLoc);
        return IMGBUILD_ERR_SECTION_DELETE;
    }

    fprintf(stdout, "Image size (after .pibmem0 delete): %i\n", sizeImage);

    rcLoc1 = sbe_xip_delete_section( i_imageOut, P9_XIP_SECTION_HALT);
    rcLoc2 = sbe_xip_image_size(i_imageOut, &sizeImage);
    rcLoc =  sbe_xip_validate(i_imageOut, sizeImage);

    if (rcLoc1 || rcLoc2 || rcLoc)
    {
        fprintf(stderr, "_delete_section(.halt) (rcLoc1=%i), _image_size() (rcLoc2=%i) and/or _validate() (rcLoc=%i) failed.\n",
                rcLoc1, rcLoc2, rcLoc);
        return IMGBUILD_ERR_SECTION_DELETE;
    }

    fprintf(stdout, "Image size (after .halt delete): %i\n", sizeImage);

    rcLoc1 = sbe_xip_delete_section( i_imageOut, P9_XIP_SECTION_STRINGS);
    rcLoc2 = sbe_xip_delete_section( i_imageOut, P9_XIP_SECTION_TOC);
    sbe_xip_image_size(i_imageOut, &sizeImage);
    rcLoc =  sbe_xip_validate(i_imageOut, sizeImage);

    if (rcLoc1 || rcLoc2 || rcLoc)
    {
        fprintf(stderr,
                "_delete_section(.strings) (rcLoc1=%i), _delete_section(.toc) (rcLoc2=%i) and/or _validate() (rcLoc=%i) failed.\n",
                rcLoc1, rcLoc2, rcLoc);
        return IMGBUILD_ERR_SECTION_DELETE;
    }

    fprintf(stdout, "Image size (after .strings and .toc delete): %i\n", sizeImage);

    rcLoc1 = sbe_xip_delete_section( i_imageOut, P9_XIP_SECTION_DATA);
    rcLoc2 = sbe_xip_image_size(i_imageOut, &sizeImage);
    rcLoc =  sbe_xip_validate(i_imageOut, sizeImage);

    if (rcLoc1 || rcLoc2 || rcLoc)
    {
        fprintf(stderr, "_delete_section(.data) (rcLoc1=%i), _image_size() (rcLoc2=%i) and/or _validate() (rcLoc=%i) failed.\n",
                rcLoc1, rcLoc2, rcLoc);
        return IMGBUILD_ERR_SECTION_DELETE;
    }

    fprintf(stdout, "Image size (after .data delete): %i\n", sizeImage);

    rcLoc1 = sbe_xip_delete_section( i_imageOut, P9_XIP_SECTION_TEXT);
    rcLoc2 = sbe_xip_image_size(i_imageOut, &sizeImage);
    rcLoc =  sbe_xip_validate(i_imageOut, sizeImage);

    if (rcLoc1 || rcLoc2 || rcLoc)
    {
        fprintf(stderr, "_delete_section(.text) (rcLoc1=%i), _image_size() (rcLoc2=%i) and/or _validate() (rcLoc=%i) failed.\n",
                rcLoc1, rcLoc2, rcLoc);
        return IMGBUILD_ERR_SECTION_DELETE;
    }

    fprintf(stdout, "Image size (after .text delete): %i\n", sizeImage);


    // ==========================================================================
    // Re-append .pibmem0
    // ==========================================================================
    rc = sbe_xip_get_section( i_imageSbe, P9_XIP_SECTION_PIBMEM0, &xipSection);

    if (rc)
    {
        MY_INF("ERROR : sbe_xip_get_section() failed: %s", P9_XIP_ERROR_STRING(g_errorStrings, rc));
        MY_INF("Probable cause:");
        MY_INF("\tThe section (=P9_XIP_SECTION_PIBMEM0=%i) was not found.", P9_XIP_SECTION_RINGS);
        return IMGBUILD_ERR_KEYWORD_NOT_FOUND;
    }

    rc = sbe_xip_append(     i_imageOut,
                             P9_XIP_SECTION_PIBMEM0,
                             (void*)((uintptr_t)i_imageSbe + xipSection.iv_offset),
                             xipSection.iv_size,
                             i_sizeImageOutMax,
                             0);

    if (rc)
    {
        MY_INF("sbe_xip_append() failed: %s", P9_XIP_ERROR_STRING(g_errorStrings, rc));
        return IMGBUILD_ERR_APPEND;
    }

#endif // #if 0

//CMO-When removing sections, close the mmapped SBE image file here.

    for (iRingType = 0; iRingType < RING_SECTION_ID_SIZE; iRingType++)
    {

        xipSectionId = RING_SECTION_ID[iRingType];
        nextRing = NULL;
        bDone = 0;

        /****************************************************************************
         *                          SEARCH LOOP - Begin                                *
         ****************************************************************************/

        do
        {

            MY_DBG("nextRing (at top)=0x%016lx\n", (uint64_t)nextRing);

            ringBlockSize = FIXED_RING_BUF_SIZE;
            ringBuffer = malloc(ringBlockSize);

            if (!ringBuffer)
            {
                MY_ERR("malloc() for fixed ring buffer failed.\n");
                exit(1);
            }

            // ==========================================================================
            // Get ring layout from HW ref image
            // ==========================================================================
            rcLoc = get_ring_from_image( i_imageHw,
                                         i_ddLevel,
                                         0,             // sysPhase = IPL-SBE image build
                                         ringBuffer,    // Contains copy of RS4 ring, incl layout
                                         &ringBlockSize,// Contains ring block size on return
                                         &nextRing,     // Points to next ring (may not need for P9)
                                         xipSectionId );
            rcSearch = rcLoc;

            if ( rcSearch != IMGBUILD_RING_SEARCH_MATCH &&
                 rcSearch != IMGBUILD_RING_SEARCH_EOS_MATCH &&
                 rcSearch != IMGBUILD_RING_SEARCH_EOS_NO_MATCH)
            {
                MY_ERR("ERROR : Error during retrieval of delta rings from the image (rcSearch=%i).\n", rcSearch);
                MY_ERR("No further RS4 rings will be appended to the IPL image.\n");
                MY_ERR("The IPL image is incomplete.\n");
                return IMGBUILD_ERR_INCOMPLETE_IMG_BUILD;
            }

            if ( rcSearch == IMGBUILD_RING_SEARCH_MATCH ||
                 rcSearch == IMGBUILD_RING_SEARCH_EOS_MATCH )
            {
                MY_DBG("Retrieving ring was successful.\n");
                countRings++;
            }

            // Check if we're done due to failure to find any more rings.
            //
            if ( rcSearch == IMGBUILD_RING_SEARCH_EOS_NO_MATCH )
            {
                MY_INF("Number of RS4 rings appended to ring section (ID=%i): %i\n", xipSectionId, countRings);
                rc = IMGBUILD_SUCCESS;
                bDone = 1;
            }

            if (!bDone)
            {
                rc = append_ring_to_image( io_fnImageSbe,
                                           ringBuffer,
                                           ringBlockSize );
                free(ringBuffer);

                if (rc)
                {
                    MY_ERR("ring_section_append() failed: rc=%i   Stopping.\n", rc);
                    exit(1);
                }
                else
                {
                    MY_DBG("Image update successful.\n");
                }

            } // End of if(!bDone)

            // Check if we're done due to having reached EOS of ring section and found a ring.
            //
            if ( rcSearch == IMGBUILD_RING_SEARCH_EOS_MATCH )
            {
                MY_INF("Number of RS4 rings appended to ring section (ID=%i): %i\n", xipSectionId, countRings);
                rc = IMGBUILD_SUCCESS;
                bDone = 1;
            }


        }
        while (!bDone);

        /***************************************************************************
         *    SEARCH LOOP - End
         ***************************************************************************/

        MY_INF("Done adding rings to ring section ID = %i\n", xipSectionId);

    } // End of for(iRingType=...) loop

    exit(1);
    return rc;
}
OpenPOWER on IntegriCloud