summaryrefslogtreecommitdiffstats
path: root/src/import/tools/imageProcs/p9_ipl_build.C
blob: 71babfadb3bff7a8077fba4f3cedc8c0b0d85084 (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
474
475
476
/* 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,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                                                     */
/*----------------------------------------------------------------------------*/
/* *! 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 : p9_ipl_build <sbe image> <unsigend hw image> <dd level>         */
/*                                                                            */
/* *! ASSUMPTIONS :                                                           */
/*    - sysPhase=0 is assumed which puts the SBE image together for IPL.      */
/*                                                                            */
/* *! COMMENTS :                                                              */
/*----------------------------------------------------------------------------*/
#include <string>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <dirent.h>

#include "p9_ipl_build.H"
#include <p9_xip_image.h>
#include <p9_tor.H>
#include <p9_infrastruct_help.H>

using namespace P9_TOR;

// prefix of our debug file name
const char* CHIP_TYPE = "p9_";

///
/// @brief Create a filename containing the DD level and chip type
///
/// @param[in]   i_fn SBE image file name
/// @param[in]   i_ddLevel - DD level of ring
///
/// @retval  std::string - holding the newly created file name.
///
std::string getDDSpecificFileName( const char* i_fn,
                                   uint32_t i_ddLevel )
{
    // create our dd specific file name
    std::stringstream ss;
    std::string fn = i_fn;
    auto found = fn.find_last_of("/");

    ss << CHIP_TYPE << std::hex << i_ddLevel << "." << fn.substr(found + 1);
    return ss.str();
}


///
/// @brief retrieve a block of DD level rings from the unsigned hw image
///
/// @param[in]  i_hwImage - pointer to a an unsigned hw image.
/// @param[in]  i_ddLevel - DD level of rings to append
/// @param[out] o_ringBlock - DD level block of rings from the hw image.
/// @param[out] o_blockSize - size of ring block returned
///
/// @return IMGBUILD_SUCCESS, or failure value.
///
int get_dd_level_rings_from_hw_image( char* i_hwImage,
                                      uint32_t i_ddLevel,
                                      void** o_ringBlock,
                                      uint32_t& o_blockSize )
{
    int rc = IMGBUILD_SUCCESS;

    P9XipSection l_ringsSection;
    *o_ringBlock = NULL;
    o_blockSize = 0;
    RingType_t l_ringType = ALLRING;
    uint8_t unused_parm = 0;

    // Get the block of rings from the HW image
    rc = p9_xip_get_section(i_hwImage, P9_XIP_SECTION_HW_RINGS, &l_ringsSection);

    if (rc)
    {
        MY_ERR("Call to p9_xip_get_section ID(%d) failed rc=%d\n", P9_XIP_SECTION_HW_RINGS, rc);
        rc = IMGBUILD_ERR_GET_SECTION;
    }
    else
    {
        // Check if any content in .rings
        if (l_ringsSection.iv_size == 0)
        {
            MY_ERR("Ring section size in HW image is zero. No TOR. We need rings here.\n");
            rc = IMGBUILD_EMPTY_RING_SECTION;
            return rc;
        }

        // Make a pointer to the start of the rings section
        void* ringsSection = i_hwImage + l_ringsSection.iv_offset;

        do
        {

            // Call the first time to get a size of the pending section
            rc = tor_get_block_of_rings( ringsSection,
                                         i_ddLevel,
                                         SBE,
                                         l_ringType,
                                         BASE,
                                         unused_parm,
                                         o_ringBlock,
                                         o_blockSize );

            if(rc)
            {
                MY_ERR("error calling tor API rc = %d\n", rc);
                rc = IMGBUILD_ERR_SECTION_SIZING;
                break;
            }

            if( o_blockSize == 0 )
            {
                rc = IMGBUILD_NO_RINGS_FOUND;
                MY_INF("No rings for dd_level %#02x found\n", i_ddLevel);
                break;
            }

            // *o_ringBlock is freed by caller
            *o_ringBlock = malloc(o_blockSize);

            if (o_ringBlock != NULL)
            {
                rc = tor_get_block_of_rings( ringsSection,
                                             i_ddLevel,
                                             SBE,
                                             l_ringType,
                                             BASE,
                                             unused_parm,
                                             o_ringBlock,
                                             o_blockSize );

                if(rc)
                {
                    MY_ERR("error calling tor API rc = %d\n", rc);
                    rc = IMGBUILD_ERR_RING_SEARCH;
                }
            }
            else
            {
                MY_ERR("failed to allocate memory for ring block\n");
                rc = IMGBUILD_ERR_MEMORY;
            }

            MY_DBG("o_blockSize = %d\n", o_blockSize);
            MY_DBG("o_ringBlock = %p\n", o_ringBlock);

        }
        while(0);
    }

    return rc;
};


///
/// @brief appends a block of rings to the previously un-populated .rings
///        section of the P9 SBE image
///
///  the passed in image pointer should point to an in memory sbe image
///  callers should ensure that the location is large enough for the existing
///  image and the new ring section.
///
/// @param[in]   io_sbeImage - pointer to an unsigned SBE image
/// @param[i/o]  io_sbeImageSize - size of image after section has been appended
/// @param[i]    i_ringBlock - pointer to a block of rings to be appended to
//                             the sbe image
/// @param[i]    i_blockSize - size of the block of rings to append.
///
/// @return 0 on success non-zero on failure
///
int append_ring_block_to_image( char* io_sbeImage,
                                size_t& io_sbeImageSize,
                                char* i_ringBlock,
                                uint32_t i_blockSize )
{
    uint32_t unused_parm = 0;
    int rc = IMGBUILD_SUCCESS;

    // Append block of rings to the sbe image in Memory
    rc = p9_xip_append( io_sbeImage,
                        P9_XIP_SECTION_SBE_RINGS,
                        i_ringBlock,
                        i_blockSize,
                        io_sbeImageSize,
                        &unused_parm );

    if(rc)
    {
        MY_ERR("error appending ring section = %d\n", rc);
        rc = IMGBUILD_ERR_APPEND;
    }

    MY_DBG("i_ringBlock = %p\n", i_ringBlock);

    return rc;
};

///
/// @brief Create an SBE image customized with DD level rings
///
/// @param[in]  i_fnSbeImage - File name of SBE image
/// @param[in]  i_hwImage - pointer to a memory mapped Unsigned hardware image
/// @param[i]   i_ddLevel - DD level of rings to append to the SBE image
///
int ipl_build( char* i_fnSbeImage,
               void* i_hwImage,
               uint32_t i_ddLevel )
{

    char* sbeImage    = NULL;
    void* l_ringBlock = NULL;
    int   rc          = 0;


    std::string ddSpecificFileName = getDDSpecificFileName(i_fnSbeImage, i_ddLevel);

    std::ifstream  sbeImageFile;
    std::ofstream  ddSpecificImage;

    sbeImageFile.open(i_fnSbeImage, std::ios::binary | std::ios::in | std::ios::out);


    if (!sbeImageFile)
    {
        MY_ERR("Failed to open %s\n", i_fnSbeImage);
        rc = IMGBUILD_ERR_FILE_ACCESS;
    }
    else
    {
        do
        {
            // get a filebuf pointer to make it easy to work with
            std::filebuf* pbuf = sbeImageFile.rdbuf();

            // get the file size
            std::size_t sbeImageSize = pbuf->pubseekoff(0,
                                       sbeImageFile.end, sbeImageFile.in);

            pbuf->pubseekpos(0, sbeImageFile.in);

            // allocate some space to hold the file data
            sbeImage =  (char*)malloc(sbeImageSize);

            if(sbeImage == NULL)
            {
                MY_ERR("Failed to allocate memory for the SBE image\n");
                rc = IMGBUILD_ERR_MEMORY;
                break;
            }

            bzero(sbeImage, sbeImageSize);

            // copy the SBE image into memory
            pbuf->sgetn(sbeImage, sbeImageSize);

            // Validate the SBE image
            rc = p9_xip_validate(sbeImage, sbeImageSize);

            if(rc)
            {
                MY_ERR("The SBE image failed validation w/rc = %d", rc);
                rc = IMGBUILD_INVALID_IMAGE;
                break;
            }

            uint32_t    l_blockSize = 0;

            char* hwImagePtr = static_cast<char*>(i_hwImage);

            rc = get_dd_level_rings_from_hw_image( hwImagePtr,
                                                   i_ddLevel,
                                                   &l_ringBlock,
                                                   l_blockSize );

            if(rc == IMGBUILD_SUCCESS)
            {
                // update our SBE image size to include the new block of rings
                // and make sure it's 8 byte aligned
                sbeImageSize += ((l_blockSize + 7) / 8) * 8;

                // grow our workspace
                void* tmp = realloc(sbeImage, sbeImageSize);

                if(tmp == NULL)
                {
                    MY_ERR("error resizing workspace..giving up errno=%d", errno);
                    rc = IMGBUILD_ERR_MEMORY;
                    break;
                }

                // use the new, larger space
                sbeImage = static_cast<char*>(tmp);

                rc = append_ring_block_to_image( sbeImage,
                                                 sbeImageSize,
                                                 (char*)l_ringBlock,
                                                 l_blockSize );

                if(rc == IMGBUILD_SUCCESS)
                {
                    // looks like it worked, create a debug file and write the
                    // customized image to it
                    ddSpecificImage.open(ddSpecificFileName.c_str(), std::ios::binary | std::ios::out);

                    if(!ddSpecificImage)
                    {
                        MY_ERR("failed to open %s for writing\n", ddSpecificFileName.c_str());
                        rc = IMGBUILD_ERR_FILE_ACCESS;
                    }
                    else
                    {
                        std::filebuf* outbuf = ddSpecificImage.rdbuf();

                        outbuf->sputn(sbeImage, sbeImageSize);

                        MY_INF("DD specific file created as %s\n", ddSpecificFileName.c_str());

                        // rewind to the beginning of the original file and write this
                        // into it.
                        pbuf->pubseekpos(0, sbeImageFile.in);

                        pbuf->sputn(sbeImage, sbeImageSize);

                    }
                }
                else
                {
                    MY_ERR("creating dd specific SBE image failed rc=%d\n", rc);
                }
            }
        }
        while(0);

        free(sbeImage);
        free(l_ringBlock);

        ddSpecificImage.close();
        sbeImageFile.close();
    }

    return rc;
}


#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 = IMGBUILD_SUCCESS;
    char*       fnSbeImage, *fnHwImage;
    uint32_t    ddLevel = 0;
    uint32_t    fdHwImage = 0;
    struct stat stbuf;
    uint32_t    sizeHwImage;
    void*       hwImage;

    // ==========================================================================
    // 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>
    //
    fnSbeImage = argv[1];
    fnHwImage  = argv[2];
    ddLevel    = strtol(argv[3], NULL, 16);

    MY_INF("  Input/output SBE image fn = %s\n", fnSbeImage);
    MY_INF("  Input HW image fn         = %s\n", fnHwImage);
    MY_INF("  DD level                  = %#02x\n", ddLevel);

    // Memory map HW image.
    fdHwImage = open(fnHwImage, O_RDONLY);

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

    sizeHwImage = stbuf.st_size;
    hwImage = mmap(0, sizeHwImage, PROT_READ, MAP_SHARED, fdHwImage, 0);

    if (hwImage == MAP_FAILED)
    {
        MY_ERR("mmap() of HW image failed.\n");
        rc = IMGBUILD_MEM_MAP_FAILED;
    }
    else
    {
        do
        {
            // Validate image.
            rc = p9_xip_validate(hwImage, sizeHwImage);

            if (rc)
            {
                MY_ERR("p9_xip_validate() of HW image failed: rc=%d\n", rc);
                rc = IMGBUILD_INVALID_IMAGE;
                break;
            }

            // Update the SBE image.
            rc = ipl_build( fnSbeImage,
                            hwImage,
                            ddLevel );

            if (rc == IMGBUILD_SUCCESS)
            {
                MY_INF("SBE image build was SUCCESSFUL.\n");
            }
            else
            {
                MY_ERR("SBE image build was UNSUCCESSFUL (rc=%i).\n", rc);
            }
        }
        while(0);

        munmap(hwImage, sizeHwImage);
    }

    close(fdHwImage);

    return rc;
}
OpenPOWER on IntegriCloud