summaryrefslogtreecommitdiffstats
path: root/src/occBootLoader/imageHdrScript.c
blob: 5a1a76d3bdcb5f3903e0adae50dc60707aadb64c (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/occBootLoader/imageHdrScript.c $                          */
/*                                                                        */
/* OpenPOWER OnChipController Project                                     */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2014,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                                                     */

//*************************************************************************/
// Includes
//*************************************************************************/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <common_types.h>
#include <stddef.h>
#include <string.h>

//*************************************************************************/
// Externs
//*************************************************************************/

//*************************************************************************/
// Macros
//*************************************************************************/

//*************************************************************************/
// Defines/Enums
//*************************************************************************/
#define CHECKSUM_FIELD_OFFSET   offsetof(imageHdr_t, checksum)
#define CHECKSUM_GPE0_FIELD_OFFSET   offsetof(imageHdr_t, gpe0_checksum)
#define CHECKSUM_GPE1_FIELD_OFFSET   offsetof(imageHdr_t, gpe1_checksum)
#define CHECKSUM_FIELD_LEN      4
#define IMAGE_SZ_FIELD_OFFSET   offsetof(imageHdr_t, image_size)
#define IMAGE_SZ_FIELD_LEN      4
#define FAILURE_RC              -1
#define SUCCESS_RC              0
#define EP_BRANCH_INST_LEN      4
#define EP_BRANCH_INST_OFFSET   offsetof(imageHdr_t, ep_branch_inst)
#define ADDRESS_OFFSET          offsetof(imageHdr_t, ep_addr)
#define ADDRESS_LEN             4
#define VERSION_OFFSET          offsetof(imageHdr_t, version)
#define VERSION_LEN             4
#define ID_STR_OFFSET           offsetof(imageHdr_t, image_id_str)
#define GPE0_SZ_FIELD_OFFSET    offsetof(imageHdr_t, gpe0_size)
#define GPE1_SZ_FIELD_OFFSET    offsetof(imageHdr_t, gpe1_size)
#define ID_STR_LEN              IMAGE_ID_STR_SZ
#define ADDRESS_MASK            0x03FFFFFC
#define ABS_BRANCH_MASK         0x48000002
#define REL_BRANCH_MASK         0x48000000
#define BRANCH_INSTR_OFFSET     0x40
#define DUMP_HDR_STR            "dumpHdr"
#define COMBINE_IMAGE_STR       "combineImage"
#define FILE_TO_WRITE_ODE       "/obj/ppc/occc/405/image.bin"
#define FILE_TO_WRITE_GNU       "/image.bin"
#define DISPLAY_SIZE            "displaySize"
#define READELF_CMD             "readelf -S "
#define PIPE_CMD                " > elfdata "
#define ELF_FILE                "elfdata"
#define ELF_FILE_REMOVE_CMD     "rm elfdata"

//*************************************************************************/
// Structures
//*************************************************************************/

//*************************************************************************/
// Globals
//*************************************************************************/

//*************************************************************************/
// Function Prototypes
//*************************************************************************/

//*************************************************************************/
// Functions
//*************************************************************************/

// Function Specification
//
// Name: displaySize
//
// Description: Display size of the object file
//
// End Function Specification
int displaySize(char * i_file)
{
    int l_rc = SUCCESS_RC;
    unsigned long int l_size = 0;
    FILE * l_file = NULL;
    int l_delfile = 0;
    do
    {
        // create command for the system call to display size using
        // readelf tool and copy output into different file for parsing later.
        if( i_file != NULL)
        {
            l_size = strlen(i_file);
        }
        l_size += strlen(READELF_CMD);
        l_size += strlen(PIPE_CMD);
        char l_cmd[l_size+1];
        unsigned long int l_cnt = strlen(READELF_CMD);
        strncpy(l_cmd,READELF_CMD,l_cnt);
        strncpy(&l_cmd[l_cnt],i_file,strlen(i_file));
        l_cnt += strlen(i_file);
        strncpy(&l_cmd[l_cnt],PIPE_CMD,strlen(PIPE_CMD));
        l_cnt += strlen(PIPE_CMD);
        l_cmd[l_cnt] = '\0';
        // do system call
        system (l_cmd );
        // set to indicate delete temporary file
        l_delfile = 1;
        // Open the file that was written with the system call for
        // reading
        l_file = fopen(ELF_FILE, "r");

        if( NULL == l_file)
        {
            printf("Failed to open file %s for reading\n",ELF_FILE);
            l_rc = FAILURE_RC;
            break;
        }
        // seek to end to get size of file
        l_rc = fseek(l_file, 0, SEEK_END);

        if( l_rc != 0)
        {
            printf("Failed to seek end of the file: %s,rc: %d\n",
                   i_file,l_rc);
            l_rc = FAILURE_RC;
            break;
        }
        // get size
        l_size = ftell(l_file);

        //Now seek back to the beginning to start parsing
        l_rc = fseek(l_file, 0, SEEK_SET);

        if( l_rc != 0)
        {
            printf("Failed to seek start after getting size of the file: %s, "
                   "rc: %d\n",i_file,l_rc);
            l_rc = FAILURE_RC;
            break;
        }

        // parse data and display size
        char l_data[l_size];
        unsigned long int l_totalSz = 0;
        char * l_str = NULL;
        printf("===========================================================\n");
        printf("Size display for %s:\n", i_file);
        printf("%-25.25s : Address    : Size   \n","Section");
        printf("===========================================================\n");
        while (fgets(l_data,l_size,l_file) != NULL)
        {
            // Ignore lines that does not have BITS in them
            if(strstr(l_data,"BITS") != NULL)
            {
                // If there is word debug on the line then we are done parsing
                if(strstr(l_data,"debug") != NULL)
                {
                    break;
                }
                l_str = NULL;
                // We need to parse 2 different options:
                // 1) [ X] and 2) [XX] Where X is number.
                // Example of data:
                // Num   Section    Type      Addr     offset  Size
                // [ 9] .linear_wr  PROGBITS  ffff6000 046000 001000 00 WA 0 0 1
                // [10] .linear_rd  PROGBITS  ffff7000 047000 001000 00 WA 0 0 1
                // We are interested in Section, Address, offset and size.
                // Everything else we can ignore. So for ease of parsing,
                // ignore everything until first "]".
                l_str = strstr(l_data,"]");
                if( NULL != l_str )
                {
                    char l_str1[l_size];
                    char l_sec[l_size];
                    unsigned long int l_addr = 0;
                    unsigned long int l_offset = 0;
                    unsigned long int l_size = 0;
                    sscanf(l_str,"%s %s %s %x %x %x ",l_str1,l_sec,
                           l_str1,&l_addr,&l_offset,&l_size);
                    printf("%-25.25s : 0x%08x : %d\t(HEX: %x ) \n",l_sec,
                           l_addr,(int)l_size,l_size);
                    l_totalSz += l_size;
                }
            }
        } // end while loop
        printf("===========================================================\n");
        printf("%-25.25s :            : %d\t(HEX: %x )  \n","Total",
               (int)l_totalSz,l_totalSz);
        printf("===========================================================\n");

    }while(0);

    // Close file if it was open
    if( l_file != NULL)
    {
        int l_rc2 = fclose( l_file);

        if( l_rc2 != 0)
        {
            printf("Failed to close destination file: rc: %d\n",l_rc2);
        }
    }

    if( l_delfile == 1)
    {
        // remove temporary file
        system(ELF_FILE_REMOVE_CMD);
    }

    return l_rc;
}


// Function Specification
//
// Name: combineImage
//
// Description: Append input image to image.bin
//
// End Function Specification
int combineImage(FILE * i_file1)
{
    FILE * l_file2 = NULL;
    FILE * l_file = NULL;
    int l_rc = SUCCESS_RC;
    unsigned long int l_size = 0;
    bool l_odeBuild = TRUE;

    do
    {
        char * l_sbPath = getenv("BASE_OBJDIR");
        if( l_sbPath != NULL)
        {
            l_size = strlen(l_sbPath);
            l_size += strlen(FILE_TO_WRITE_GNU);
            l_odeBuild = FALSE;
        }
        else
        {
            l_sbPath = getenv("SANDBOXBASE");
            if(l_sbPath != NULL)
            {
                l_size = strlen(l_sbPath);
                l_size += strlen(FILE_TO_WRITE_ODE);
            }
            else
            {
                printf("Failed to get either SANDBOXBASE or BASE_OBJDIR environment variables\n");
                l_rc = FAILURE_RC;
                break;
            }
        }
        char l_fileToWrite[l_size+1];
        strncpy(l_fileToWrite,l_sbPath,strlen(l_sbPath));

        if ( TRUE == l_odeBuild )
        {
            strncpy(&l_fileToWrite[strlen(l_sbPath)],FILE_TO_WRITE_ODE,strlen(FILE_TO_WRITE_ODE));
        }
        else
        {
            strncpy(&l_fileToWrite[strlen(l_sbPath)],FILE_TO_WRITE_GNU,strlen(FILE_TO_WRITE_GNU));
        }
        l_fileToWrite[l_size] = '\0';
        printf("Writing to file: %s\n", l_fileToWrite);

        // Open the destination file
        l_file = fopen(l_fileToWrite, "a");

        if( NULL == l_file)
        {
            printf("Failed to open file %s for writing\n",l_fileToWrite);
            l_rc = FAILURE_RC;
            break;
        }

        // get size of file1
        l_rc = fseek(i_file1, 0, SEEK_END);

        if( l_rc != 0)
        {
            printf("Failed to seek end of the input file, rc: %d\n",l_rc);
            l_rc = FAILURE_RC;
            break;
        }

        unsigned long int l_sz1 = ftell(i_file1);

        //Now seek back to the beginning
        l_rc = fseek(i_file1, 0, SEEK_SET);

        if( l_rc != 0)
        {
            printf("Failed to seek start after getting size of the file, rc: %d\n",l_rc);
            l_rc = FAILURE_RC;
            break;
        }

        // Read full file into buffer
        unsigned int l_data[l_sz1];
        size_t l_readSz = fread(&l_data[0], 1, l_sz1, i_file1);

        if( l_readSz != l_sz1)
        {
            printf("Failed to read input file, readSz: 0x%x,l_sz: 0x%x\n",
                   l_readSz,l_sz1);
            l_rc = -1;
            break;
        }

        // Write file1 data to destination file
        size_t l_writeSz = fwrite( l_data,1,l_sz1,l_file);

        if( l_writeSz != l_sz1)
        {
            printf("Error writing data. Written Sz :0x%x,Expected Sz:0x%x\n",
                   l_writeSz,l_sz1);
            l_rc = FAILURE_RC;
        }

    }while(0);

    // Close destination file if it was open
    if( l_file != NULL)
    {
        int l_rc2 = fclose( l_file);

        if( l_rc2 != 0)
        {
            printf("Failed to close destination file: rc: %d\n",l_rc2);
        }
    }

    return l_rc;
}


// Function Specification
//
// Name: dumpHdr
//
// Description: Dump image header
//
// End Function Specification
int dumpHdr(char * i_fileStr)
{
    FILE * l_filePtr = NULL;
    int l_rc = SUCCESS_RC;
    do
    {
        // Open the file
        l_filePtr = fopen(i_fileStr, "rb");

        if( NULL == l_filePtr)
        {
            printf("Failed to open file %s for reading\n",i_fileStr);
            l_rc = FAILURE_RC;
            break;
        }
        // file is open now
        // get the file size
        l_rc = fseek(l_filePtr, 0, SEEK_END);

        if( l_rc != 0)
        {
            printf("Failed to seek end of the file: %s,rc: %d\n",
                   i_fileStr,l_rc);
            l_rc = FAILURE_RC;
            break;
        }

        unsigned long int l_sz = ftell(l_filePtr);

        //Now seek back to the beginning
        l_rc = fseek(l_filePtr, 0, SEEK_SET);

        if( l_rc != 0)
        {
            printf("Failed to seek start after getting size of the file: %s, "
                   "rc: %d\n",i_fileStr,l_rc);
            l_rc = FAILURE_RC;
            break;
        }

        l_sz = sizeof(imageHdr_t);

        // Read header from the file
        unsigned int l_hdrPtr[l_sz];
        size_t l_readSz = fread(&l_hdrPtr[0], 1, l_sz, l_filePtr);

        if( l_readSz != l_sz)
        {
            printf("Failed to read file: %s,readSz: 0x%x,l_sz: 0x%x\n",
                   i_fileStr,l_readSz,l_sz);
            l_rc = -1;
            break;
        }

        printf("==========================================================\n");
        printf("SRAM Repair Reserved:\t ");
        unsigned int idx = 0;
            for(idx = 0; idx < SRAM_REPAIR_RESERVE_SZ/4; idx++)
            {
                printf("0x%08X  ",htonl(l_hdrPtr[idx]) );
            }
        printf("\n");
        printf("ep_branch_inst:\t\t 0x%08x\n",htonl( l_hdrPtr[idx++]));
        printf("halt_inst:\t\t 0x%x\n",htonl( l_hdrPtr[idx++]));
        printf("image_size:\t\t 0x%08x \n", htonl( l_hdrPtr[idx++]));
        printf("start_addr:\t\t 0x%08x\n",htonl( l_hdrPtr[idx++]));
        printf("readonly_size:\t\t 0x%08x \n",htonl( l_hdrPtr[idx++]));
        printf("boot_writeable_addr:\t 0x%08x\n",htonl( l_hdrPtr[idx++]));
        printf("boot_writeable_size:\t 0x%08x \n",htonl( l_hdrPtr[idx++]));
        printf("zero_data_addr:\t\t 0x%08x\n", htonl( l_hdrPtr[idx++]));
        printf("zero_data_size:\t\t 0x%08x \n", htonl( l_hdrPtr[idx++]));
        printf("ep_addr:\t\t 0x%08x\n", htonl( l_hdrPtr[idx++]));
        printf("checksum:\t\t 0x%08x\n", htonl( l_hdrPtr[idx++]));
        printf("version:\t\t %.*s\n", VERSION_LEN,(char*)&l_hdrPtr[idx++]);
        printf("image_id_str:\t\t %s\n",(char*)(&l_hdrPtr[idx]));
        idx += (IMAGE_ID_STR_SZ/4);
        unsigned int i = 0;
        printf("Reserved:\t\t ");
        for(i = 0; i < RESERVED_SZ/4; i++)
        {
            printf("0x%08X  ",htonl(l_hdrPtr[idx++]) );
        }
        printf("\n");
        printf("==========================================================\n");

    }while(0);

    // Close file if open
    if( l_filePtr != NULL)
    {
        int l_rc2 = fclose( l_filePtr);

        if( l_rc2 != 0)
        {
            printf("Failed to close file: %s,rc: %d\n", i_fileStr,l_rc2);
        }
    }

    return l_rc;
}


// Function Specification
//
// Name: calImageChecksum
//
// Description: calculate image checksum
//
// End Function Specification
unsigned long int calImageChecksum(FILE * i_filePtr, bool i_gpeFile)
{
    unsigned long int l_checksum = 0;
    unsigned long int l_counter = 0;
    int l_val = 0;

    while(1)
    {
        // Skip checksum field
        if( (l_counter == CHECKSUM_FIELD_OFFSET) && !i_gpeFile )
        {
            while( l_counter != (CHECKSUM_FIELD_OFFSET + CHECKSUM_FIELD_LEN))
            {
                l_counter++;
                l_val = fgetc(i_filePtr);
            }
        }
        else if( (l_counter == CHECKSUM_GPE0_FIELD_OFFSET) && !i_gpeFile )
        {
            while( l_counter != (CHECKSUM_GPE0_FIELD_OFFSET + CHECKSUM_FIELD_LEN))
            {
                l_counter++;
                l_val = fgetc(i_filePtr);
            }
        }
        else if( (l_counter == CHECKSUM_GPE1_FIELD_OFFSET) && !i_gpeFile )
        {
            while( l_counter != (CHECKSUM_GPE1_FIELD_OFFSET + CHECKSUM_FIELD_LEN))
            {
                l_counter++;
                l_val = fgetc(i_filePtr);
            }
        }
        else
        {
            l_val = fgetc(i_filePtr);
            if (l_val == EOF) break;

            l_checksum += l_val;
            l_counter ++;
        }
    }

    fprintf(stdout,"Checksum: 0x%08X\t\tSize: 0x%08X\n", l_checksum, l_counter);
    return l_checksum;
}


// Function Specification
//
// Name: write
//
// Description: Write given data to file at given offset
//
// End Function Specification
int write(FILE * i_filePtr,
          const void * i_dataPtr,
          const unsigned long int i_dataSz,
          const unsigned long int i_dataOffset)
{
    int l_rc = SUCCESS_RC;

    l_rc = fseek(i_filePtr, i_dataOffset, SEEK_SET);

    if( l_rc != 0)
    {
        printf("Failed to seek offset: [0x%x] while writing to file,rc: %d\n",
               i_dataOffset,l_rc);
        l_rc = FAILURE_RC;
    }
    else
    {
        size_t l_writeSz = fwrite( i_dataPtr,1,i_dataSz,i_filePtr);

        if( l_writeSz != i_dataSz)
        {
            printf("Error writing data. Written Sz :0x%x,Expected Sz:0x%x\n",
                   l_writeSz,i_dataSz);
            l_rc = FAILURE_RC;
        }
    }

    return l_rc;
}


// Function Specification
//
// Name: printHelp
//
// Description: script usage
//
// End Function Specification
void printHelp()
{
    printf("Script Usage: imageHdrScript [FILE] [OPTIONS]\n\n");
    printf("This OCC Image Header Script is used for handling different image header\n");
    printf("fields and combining different OCC images into single image.\n");
    if ( NULL != getenv("SANDBOXBASE") )
    {
        printf("The path to target image is $SANDBOXBASE%s\n\n", FILE_TO_WRITE_ODE);
    }
    else
    {
        printf("The path to target image is $OCCROOT%s\n\n", FILE_TO_WRITE_GNU);
    }
    printf("Option for ELF executable file (file type: *.out):\n");
    printf("    displaySize     check section sizes in input file\n");
    printf("Options for binary image file (file type: *.bin):\n");
    printf("    combineImage    append input image to the target image\n");
    printf("    dumpHdr         dump values of each header field in input image\n\n");

    printf("If the option string is not equal to \"combineImage\", \"displaySize\", or\n");
    printf("\"dumpHdr\", the script will use that string as image version, and start to\n");
    printf("check/update image header fields.\n");
    printf("Example:\n");
    printf("    imageHdrScript IMAGE_FILE VERSION_STRING\n");
    printf("    imageHdrScript IMAGE_FILE VERSION_STRING ID_STRING\n");
}


// Function Specification
//
// Name: main
//
// Description: main for the script
//
// End Function Specification
int main(int argc, char* argv[])
{
    // NOTE: In order for this to work properly, the arguments must be in this order:
    // argv[0] = (implicitly) name of this executable
    // argv[1] = bootloader binary file
    // argv[2] = 405 binary file
    // argv[3] = gpe0 binary file
    // argv[4] = gpe1 binary file
    // argv[5] = bootloader version
    // argv[6] = 405 version
    // argv[7] = (optional) bootloader ID
    // argv[8] = (optional) 405 ID

#define ARG_BOOTLOADER_BIN  argv[1]
#define ARG_405_BIN         argv[2]
#define ARG_GPE0_BIN        argv[3]
#define ARG_GPE1_BIN        argv[4]
#define ARG_BOOTLOADER_VERS argv[5]
#define ARG_405_VERS        argv[6]
#define ARG_BOOTLOADER_ID   argv[7]
#define ARG_405_ID          argv[8]

    FILE * l_bootLdrPtr = NULL;
    FILE * l_file405Ptr = NULL;
    FILE * l_fileGPE0Ptr = NULL;
    FILE * l_fileGPE1Ptr = NULL;
    int l_rc = SUCCESS_RC;

    unsigned long int l_405_sz  = 0;
    unsigned long int l_gpe0_sz = 0;
    unsigned long int l_gpe1_sz = 0;
    unsigned long int l_bootLdr_sz = 0;

    do
    {
        if( argc <= 2)
        {
            printHelp();
            l_rc = FAILURE_RC;
            break;
        }

        //=============================================
        // Dump the image header for the specified file
        //=============================================
        if( (strcmp(argv[2],DUMP_HDR_STR)== 0))
        {
            printf("Dump Image Header\n");
            l_rc = dumpHdr(argv[1]);

            if( l_rc != 0)
            {
                printf("Problem dumping header for file: %s,rc: %d\n",
                       argv[1],l_rc);
                l_rc = FAILURE_RC;
            }
            break;
        }

        //=======================================================
        // Dump the imagesize of the image for the specified file
        //=======================================================
        if( (argc > 2) && (strcmp(argv[2],DISPLAY_SIZE)== 0))
        {
            l_rc = displaySize(argv[1]);

            if( l_rc != 0)
            {
                printf("Problem displaying size: rc: %d\n",l_rc);
                l_rc = FAILURE_RC;
            }
            break;
        }

        // At this point we know there is at least 1 argument to the program
        // but we need 6: path to bootloader, 405, gpe0, gpe1, and the versions.
        if( argc < 7 )
        {
            printf("Need the path to the bootloader, 405, gpe0, gpe1, and versions of 405 and bootloader\n");
            l_rc = FAILURE_RC;
            break;
        }

        //===============
        // Open the files
        //===============
        l_bootLdrPtr = fopen(ARG_BOOTLOADER_BIN, "r+");
        l_file405Ptr = fopen(ARG_405_BIN, "r+");
        l_fileGPE0Ptr = fopen(ARG_GPE0_BIN, "r+");
        l_fileGPE1Ptr = fopen(ARG_GPE1_BIN, "r+");

        // Verify all were successfully opened
        if( NULL == l_bootLdrPtr )
        {
            printf("Failed to open file %s for reading and writing\n", ARG_BOOTLOADER_BIN);
            l_rc = FAILURE_RC;
            break;
        }
        else if( NULL == l_file405Ptr)
        {
            printf("Failed to open file %s for reading and writing\n", ARG_405_BIN);
            l_rc = FAILURE_RC;
            break;
        }
        else if( NULL == l_fileGPE0Ptr)
        {
            printf("Failed to open file %s for reading and writing\n", ARG_GPE0_BIN);
            l_rc = FAILURE_RC;
            break;
        }
        else if( NULL == l_fileGPE1Ptr)
        {
            printf("Failed to open file %s for reading and writing\n", ARG_GPE1_BIN);
            l_rc = FAILURE_RC;
            break;
        }

        //===============================
        // Files are open, get file sizes
        //===============================

        // Bootloader
        l_rc = fseek(l_bootLdrPtr, 0, SEEK_END);
        if( l_rc != 0)
        {
            printf("Failed to seek end of the file: %s, rc: %d\n",ARG_BOOTLOADER_BIN,l_rc);
            l_rc = FAILURE_RC;
            break;
        }
        l_bootLdr_sz = ftell(l_bootLdrPtr);

        // 405
        l_rc = fseek(l_file405Ptr, 0, SEEK_END);
        if( l_rc != 0)
        {
            printf("Failed to seek end of the file: %s, rc: %d\n",ARG_405_BIN,l_rc);
            l_rc = FAILURE_RC;
            break;
        }
        l_405_sz = ftell(l_file405Ptr);

        // GPE 0
        l_rc = fseek(l_fileGPE0Ptr, 0, SEEK_END);
        if( l_rc != 0)
        {
            printf("Failed to seek end of the file: %s, rc: %d\n",ARG_GPE0_BIN,l_rc);
            l_rc = FAILURE_RC;
            break;
        }
        l_gpe0_sz = ftell(l_fileGPE0Ptr);

        // GPE 1
        l_rc = fseek(l_fileGPE1Ptr, 0, SEEK_END);
        if( l_rc != 0)
        {
            printf("Failed to seek end of the file: %s, rc: %d\n",ARG_GPE1_BIN,l_rc);
            l_rc = FAILURE_RC;
            break;
        }
        l_gpe1_sz = ftell(l_fileGPE1Ptr);

        //================================================
        // Ensure that all file sizes are 128-byte aligned
        //================================================
        if( (l_405_sz % 128 != 0) || (l_gpe0_sz % 128 != 0) ||
            (l_gpe1_sz % 128 != 0) || (l_bootLdr_sz % 128 != 0) )
        {
            printf("An Image size is not 128-byte aligned: BootLdr [%d] 405[%d] GPE0[%d] GPE1[%d]\n",
                   l_bootLdr_sz, l_405_sz, l_gpe0_sz, l_gpe1_sz);
            l_rc = FAILURE_RC;
            break;
        }

        //=================================================
        // Now seek back to the beginning of the bootloader
        // and 405 since we are writing image sizes to them
        //=================================================

        // 405
        l_rc = fseek(l_file405Ptr, 0, SEEK_SET);
        if( l_rc != 0)
        {
            printf("Failed to seek start after getting size of the file: %s, "
                   "rc: %d\n",ARG_405_BIN,l_rc);
            l_rc = FAILURE_RC;
            break;
        }

        // Bootloader
        l_rc = fseek(l_bootLdrPtr, 0, SEEK_SET);
        if( l_rc != 0)
        {
            printf("Failed to seek start after getting size of the file: %s, "
                   "rc: %d\n",ARG_BOOTLOADER_BIN,l_rc);
            l_rc = FAILURE_RC;
            break;
        }

        //==========================================================
        // Convert from host (typically little endian) to big endian
        //==========================================================
        l_bootLdr_sz = htonl(l_bootLdr_sz);
        l_405_sz = htonl(l_405_sz);
        l_gpe0_sz = htonl(l_gpe0_sz);
        l_gpe1_sz = htonl(l_gpe1_sz);

        //=======================================
        // Write image sizes to the image headers
        //=======================================

        // Write bootloader size to bootloader image header
        l_rc = write(l_bootLdrPtr, &l_bootLdr_sz, IMAGE_SZ_FIELD_LEN,IMAGE_SZ_FIELD_OFFSET);
        if( l_rc != 0)
        {
            printf("Failed to write bootloader image size in the file: %s, "
                   "rc: %d, IMAGE_SZ_FIELD_LEN: %d, "
                   "IMAGE_SZ_FIELD_OFFSET: %d\n",ARG_BOOTLOADER_BIN,l_rc,
                   IMAGE_SZ_FIELD_LEN,IMAGE_SZ_FIELD_OFFSET);
            l_rc = FAILURE_RC;
            break;

        }

        // Write 405 size to 405 image header
        l_rc = write(l_file405Ptr, &l_405_sz, IMAGE_SZ_FIELD_LEN,IMAGE_SZ_FIELD_OFFSET);
        if( l_rc != 0)
        {
            printf("Failed to write 405 image size in the file: %s, "
                   "rc: %d, IMAGE_SZ_FIELD_LEN: %d, "
                   "IMAGE_SZ_FIELD_OFFSET: %d\n",ARG_405_BIN,l_rc,
                   IMAGE_SZ_FIELD_LEN,IMAGE_SZ_FIELD_OFFSET);
            l_rc = FAILURE_RC;
            break;

        }

        // Write GPE0 size to 405 image header
        l_rc = write(l_file405Ptr, &l_gpe0_sz, IMAGE_SZ_FIELD_LEN,GPE0_SZ_FIELD_OFFSET);
        if( l_rc != 0)
        {
            printf("Failed to write GPE0 image size in the file: %s, "
                   "rc: %d, IMAGE_SZ_FIELD_LEN: %d, "
                   "GPE0_SZ_FIELD_OFFSET: %d\n",ARG_405_BIN,l_rc,
                   IMAGE_SZ_FIELD_LEN,GPE0_SZ_FIELD_OFFSET);
            l_rc = FAILURE_RC;
            break;
        }

        // Write GPE1 size to 405 image header
        l_rc = write(l_file405Ptr, &l_gpe1_sz, IMAGE_SZ_FIELD_LEN,GPE1_SZ_FIELD_OFFSET);
        if( l_rc != 0)
        {
            printf("Failed to write GPE1 image size in the file: %s, "
                   "rc: %d, IMAGE_SZ_FIELD_LEN: %d, "
                   "GPE1_SZ_FIELD_OFFSET: %d\n",ARG_405_BIN,l_rc,
                   IMAGE_SZ_FIELD_LEN,GPE1_SZ_FIELD_OFFSET);
            l_rc = FAILURE_RC;
            break;
        }

        //=====================
        // Write image versions
        //=====================
        unsigned long int l_version = 0;

        // Bootloader
        sprintf((char*)&l_version, "%s",argv[5]);
        l_rc = write(l_bootLdrPtr, &l_version, VERSION_LEN, VERSION_OFFSET);
        if( l_rc != 0)
        {
            printf("Failed to write version in the file: %s, "
                   "rc: %d, VERSION_LEN: %d, VERSION_OFFSET: %d\n",
                   ARG_BOOTLOADER_BIN, l_rc, VERSION_LEN, VERSION_OFFSET);
            l_rc = FAILURE_RC;
            break;
        }

        // 405
        sprintf((char*)&l_version, "%s",argv[6]);
        l_rc = write(l_file405Ptr, &l_version, VERSION_LEN, VERSION_OFFSET);
        if( l_rc != 0)
        {
            printf("Failed to write version in the file: %s, "
                   "rc: %d, VERSION_LEN: %d, VERSION_OFFSET: %d\n",
                   ARG_405_BIN, l_rc, VERSION_LEN, VERSION_OFFSET);
            l_rc = FAILURE_RC;
            break;
        }

        //===================================
        // If user has passed in image id
        // string(s), write it to the image(s)
        //====================================

        // Bootloader
        if( argc > 7 )
        {
            l_rc = write(l_bootLdrPtr, ARG_BOOTLOADER_ID, ID_STR_LEN, ID_STR_OFFSET);

            if( l_rc != 0)
            {
                printf("Failed to write id_str in the file: %s, "
                       "rc: %d ID_STR_LEN: %d, ID_STR_OFFSET: %d\n",
                       ARG_BOOTLOADER_BIN, l_rc, ID_STR_LEN,ID_STR_OFFSET);
                l_rc = FAILURE_RC;
                break;
            }
        }

        // 405
        if( argc > 8 )
        {
            l_rc = write(l_file405Ptr, ARG_405_ID, ID_STR_LEN, ID_STR_OFFSET);

            if( l_rc != 0)
            {
                printf("Failed to write id_str in the file: %s, "
                       "rc: %d ID_STR_LEN: %d, ID_STR_OFFSET: %d\n",
                       ARG_405_BIN, l_rc, ID_STR_LEN,ID_STR_OFFSET);
                l_rc = FAILURE_RC;
                break;
            }
        }

        //====================================================
        // Seek to bootloader/405's entry point address fields
        //====================================================

        // Bootloader
        l_rc = fseek(l_bootLdrPtr, ADDRESS_OFFSET, SEEK_SET);
        if( l_rc != 0)
        {
            printf("Failed to seek ep_address offset: 0x%x of the file: %s, "
                   "rc: %d\n",ADDRESS_OFFSET,ARG_BOOTLOADER_BIN,l_rc);
            l_rc = FAILURE_RC;
            break;
        }

        // 405
        l_rc = fseek(l_file405Ptr, ADDRESS_OFFSET, SEEK_SET);
        if( l_rc != 0)
        {
            printf("Failed to seek ep_address offset: 0x%x of the file: %s, "
                   "rc: %d\n",ADDRESS_OFFSET,ARG_405_BIN,l_rc);
            l_rc = FAILURE_RC;
            break;
        }

        unsigned long int l_405_addr = 0, l_btldr_addr = 0;

        // Read ep_addr fields

        // 405
        size_t l_readSz = fread(&l_405_addr, 1, ADDRESS_LEN, l_file405Ptr);
        if( l_readSz != ADDRESS_LEN)
        {
            printf("Failed to read address for ep_branch calculation. File: %s, "
                   "readSz: 0x%x, ADDRESS_LEN: 0x%x\n",ARG_405_BIN,l_readSz,
                   ADDRESS_LEN);
            l_rc = FAILURE_RC;
            break;
        }

        // Bootloader
        l_readSz = fread(&l_btldr_addr, 1, ADDRESS_LEN, l_bootLdrPtr);
        if( l_readSz != ADDRESS_LEN)
        {
            printf("Failed to read address for ep_branch calculation. File: %s, "
                   "readSz: 0x%x, ADDRESS_LEN: 0x%x\n",ARG_BOOTLOADER_BIN,l_readSz,
                   ADDRESS_LEN);
            l_rc = FAILURE_RC;
            break;
        }

        //==========================================================
        // Calculate branch instruction to that address and write to
        // ep_branch_inst field in the image header
        //==========================================================

        // 405
        l_405_addr = ntohl(l_405_addr);
        l_405_addr &= ADDRESS_MASK;
        l_405_addr |= ABS_BRANCH_MASK;
        l_405_addr = htonl(l_405_addr);
        l_rc = write(l_file405Ptr, &l_405_addr, EP_BRANCH_INST_LEN,
                     EP_BRANCH_INST_OFFSET);

        if( l_rc != 0)
        {
            printf("Failed to write ep_branch_inst in the file: %s, "
                   "rc: %d, EP_BRANCH_INST_LEN: %d, "
                   "EP_BRANCH_INST_OFFSET: %d\n",ARG_405_BIN,l_rc,
                   EP_BRANCH_INST_LEN,EP_BRANCH_INST_OFFSET);
            l_rc = FAILURE_RC;
            break;
        }

        // Bootloader
        l_btldr_addr = ntohl(l_btldr_addr);
        l_btldr_addr -= BRANCH_INSTR_OFFSET;
        l_btldr_addr &= ADDRESS_MASK;
        l_btldr_addr |= REL_BRANCH_MASK;
        l_btldr_addr = htonl(l_btldr_addr);
        l_rc = write(l_bootLdrPtr, &l_btldr_addr, EP_BRANCH_INST_LEN,
                     EP_BRANCH_INST_OFFSET);

        if( l_rc != 0)
        {
            printf("Failed to write ep_branch_inst in the file: %s, "
                   "rc: %d, EP_BRANCH_INST_LEN: %d, "
                   "EP_BRANCH_INST_OFFSET: %d\n",ARG_BOOTLOADER_BIN,l_rc,
                   EP_BRANCH_INST_LEN,EP_BRANCH_INST_OFFSET);
            l_rc = FAILURE_RC;
            break;
        }

        //=======================================================
        //Now seek back to the beginning for calculating checksum
        //=======================================================

        // 405
        l_rc = fseek(l_file405Ptr, 0, SEEK_SET);
        if( l_rc != 0)
        {
            printf("Failed to seek start before checksum calculation file: %s,"
                   "rc: %d\n",ARG_405_BIN,l_rc);
            l_rc = FAILURE_RC;
            break;
        }

        // Bootloader
        l_rc = fseek(l_bootLdrPtr, 0, SEEK_SET);
        if( l_rc != 0)
        {
            printf("Failed to seek start before checksum calculation file: %s,"
                   "rc: %d\n",ARG_BOOTLOADER_BIN,l_rc);
            l_rc = FAILURE_RC;
            break;
        }

        // GPE0
        l_rc = fseek(l_fileGPE0Ptr, 0, SEEK_SET);
        if( l_rc != 0)
        {
            printf("Failed to seek ep_address offset: 0x%x of the file: %s, "
                   "rc: %d\n",ADDRESS_OFFSET,ARG_GPE0_BIN,l_rc);
            l_rc = FAILURE_RC;
            break;
        }

        // GPE1
        l_rc = fseek(l_fileGPE1Ptr, 0, SEEK_SET);
        if( l_rc != 0)
        {
            printf("Failed to seek ep_address offset: 0x%x of the file: %s, "
                   "rc: %d\n",ADDRESS_OFFSET,ARG_GPE1_BIN,l_rc);
            l_rc = FAILURE_RC;
            break;
        }

        //====================
        // Calculate checksums
        //====================

        // 405
        unsigned long int l_checksum = calImageChecksum(l_file405Ptr, false);
        l_checksum = htonl(l_checksum);
        l_rc = write(l_file405Ptr, &l_checksum,CHECKSUM_FIELD_LEN,
                     CHECKSUM_FIELD_OFFSET);
        if( l_rc != 0)
        {
            printf("Failed to write image checksum in the file: %s, "
                   "rc: %d,IMAGE_SZ_FIELD_LEN: %d, "
                   "CHECKSUM_FIELD_OFFSET: %d\n",ARG_405_BIN,l_rc,
                   CHECKSUM_FIELD_LEN,CHECKSUM_FIELD_OFFSET);
            l_rc = FAILURE_RC;
            break;
        }

        // Bootloader
        l_checksum = calImageChecksum(l_bootLdrPtr, false);
        l_checksum = htonl(l_checksum);
        l_rc = write(l_bootLdrPtr, &l_checksum,CHECKSUM_FIELD_LEN,
                     CHECKSUM_FIELD_OFFSET);
        if( l_rc != 0)
        {
            printf("Failed to write image checksum in the file: %s, "
                   "rc: %d,IMAGE_SZ_FIELD_LEN: %d, "
                   "CHECKSUM_FIELD_OFFSET: %d\n",ARG_BOOTLOADER_BIN,l_rc,
                   CHECKSUM_FIELD_LEN,CHECKSUM_FIELD_OFFSET);
            l_rc = FAILURE_RC;
            break;
        }

        // GPE0
        l_checksum = calImageChecksum(l_fileGPE0Ptr, true);
        l_checksum = htonl(l_checksum);
        l_rc = write(l_file405Ptr, &l_checksum,CHECKSUM_FIELD_LEN,
                     CHECKSUM_GPE0_FIELD_OFFSET);
        if( l_rc != 0)
        {
            printf("Failed to write image checksum in the file: %s, "
                   "rc: %d,IMAGE_SZ_FIELD_LEN: %d, "
                   "CHECKSUM_GPE0_FIELD_OFFSET: %d\n",ARG_405_BIN,l_rc,
                   CHECKSUM_FIELD_LEN,CHECKSUM_GPE0_FIELD_OFFSET);
            l_rc = FAILURE_RC;
            break;
        }

        // GPE1
        l_checksum = calImageChecksum(l_fileGPE1Ptr, true);
        l_checksum = htonl(l_checksum);
        l_rc = write(l_file405Ptr, &l_checksum,CHECKSUM_FIELD_LEN,
                     CHECKSUM_GPE1_FIELD_OFFSET);
        if( l_rc != 0)
        {
            printf("Failed to write image checksum in the file: %s, "
                   "rc: %d,IMAGE_SZ_FIELD_LEN: %d, "
                   "CHECKSUM_GPE1_FIELD_OFFSET: %d\n",ARG_405_BIN,l_rc,
                   CHECKSUM_FIELD_LEN,CHECKSUM_GPE1_FIELD_OFFSET);
            l_rc = FAILURE_RC;
            break;
        }

        // Combine the images into one binary
        l_rc = combineImage(l_bootLdrPtr);
        if ( l_rc )
        {
            printf("Failed to combine file %s, rc %d\n", ARG_BOOTLOADER_BIN, l_rc);
            l_rc = FAILURE_RC;
            break;
        }
        l_rc = combineImage(l_file405Ptr);
        if ( l_rc )
        {
            printf("Failed to combine file %s, rc %d\n", ARG_405_BIN, l_rc);
            l_rc = FAILURE_RC;
            break;
        }
        l_rc = combineImage(l_fileGPE0Ptr);
        if ( l_rc )
        {
            printf("Failed to combine file %s, rc %d\n", ARG_GPE0_BIN, l_rc);
            l_rc = FAILURE_RC;
            break;
        }
        l_rc = combineImage(l_fileGPE1Ptr);
        if ( l_rc )
        {
            printf("Failed to combine file %s, rc %d", ARG_GPE1_BIN, l_rc);
            l_rc = FAILURE_RC;
            break;
        }
    }while(0);

    // Close files if open
    if( l_bootLdrPtr != NULL )
    {
        int l_rc2 = fclose(l_bootLdrPtr);

        if( l_rc2 != 0)
        {
            printf("Failed to close file: %s,rc: %d\n", ARG_BOOTLOADER_BIN,l_rc2);
        }
    }

    if( l_file405Ptr != NULL )
    {
        int l_rc2 = fclose( l_file405Ptr);

        if( l_rc2 != 0)
        {
            printf("Failed to close file: %s,rc: %d\n", ARG_405_BIN,l_rc2);
        }
    }

    if( l_fileGPE0Ptr != NULL )
    {
        int l_rc2 = fclose( l_fileGPE0Ptr);

        if( l_rc2 != 0)
        {
            printf("Failed to close file: %s,rc: %d\n", ARG_GPE0_BIN,l_rc2);
        }
    }

    if( l_fileGPE1Ptr != NULL )
    {
        int l_rc2 = fclose( l_fileGPE1Ptr);

        if( l_rc2 != 0)
        {
            printf("Failed to close file: %s,rc: %d\n", ARG_GPE1_BIN,l_rc2);
        }
    }


    return l_rc;
}
OpenPOWER on IntegriCloud