summaryrefslogtreecommitdiffstats
path: root/src/build/buildpnor/bpm-utils/insertBpmFwCrc.py
blob: 0631ec5d2b959c04be0fd628a0d70a8fbcecc41e (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
#!/usr/bin/env python
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/build/buildpnor/bpm-utils/insertBpmFwCrc.py $
#
# OpenPOWER HostBoot Project
#
# Contributors Listed Below - COPYRIGHT 2019
# [+] International Business Machines Corp.
#
#
# Copyright (c) 2019 SMART Modular Technologies, Inc.
# All Rights Reserved.
#
# 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

import os, sys, time, datetime, glob
from subprocess import Popen, PIPE
import shlex

#
# Function to insert the CRC signature in the
# firmware image file, inFile, and to generate the
# signature file, outFile.
#
def InsertCrcSignature(inFile, outFile, crcProgram):

    #
    # Open the outFile
    #
    try:
        print("\nOpening outFile = %s" % outFile)
        outFileObj = open(outFile, "w")
    except Exception as e:
        print "\nException {0} occured, args: {1!r}".format(type(e).__name__, e.args)
        sys.exit(999)

    #
    # Read from the inFile and copy the data to the outFile.
    #
    # At the right address, insert the SignatureFile data
    # @FF7A
    # AA 55 [Start_Lo] [Start_Hi] [Crc_Lo] [Crc_Hi]
    #
    # generated from the imageCrc tool, to the outFile.
    #
    # The address insertion has to be in the proper ascending order of
    # addresses given in the inFile in to the outFile.
    #
    # For example the outFile should have the following:
    # =================================================
    # ...
    # @8000 / @A000   << The firmware start address is always either one of them.
    # ....
    # ....
    # @FF7A   <<<< The "@" addresses must be in ascending order in the Firmware Image file.
    # AA 55 [Start_Lo] [Start_Hi] [Crc_Lo] [Crc_Hi]
    #
    # ....
    # @FFDA
    # ....

    # Call imageCrc here
    proc = Popen([crcProgram, inFile], stdout=PIPE, stderr=PIPE)
    out,err = proc.communicate()
    exitCode = proc.returncode

    if exitCode:
        print "\nCRC generation using imageCrc utility for {0} failed, below are stdout and stderr".format(inFile)
        print "Command stdout - \n{0}".format(out)
        print "Command stderr - \n{0}".format(err)
        sys.exit(999)
    else:
        print "\nCRC generation using imageCrc utility successful"
        print "Command output - \n{0}".format(out)

    # Parse through output of imageCrc and get addr and signature
    splitLines_crc = out.splitlines()
    for counter in range(0, len(splitLines_crc)):

        if splitLines_crc[counter].startswith("@"):
            crcAddressLine = splitLines_crc[counter]
            crcSignature = splitLines_crc[counter+1].strip().upper()

    # Open infile here
    # Keep reading infile lines, when read address > the address in signature,
    # insert signature there
    try:
        crcWritten = 0
        print("\nOpening inFile = %s and writing to outFile now..." % inFile)
        with open(inFile, "r") as ins:
            #
            # TODO: Have the logic to insert the signature from the
            # SignatureFile inserted at the correct location in the
            # outFile.
            #

            for line in ins:
                if line.startswith("@"):
                    inputFileAddr = line.strip()
                    # If crc already in input file, check if it is equal to calculate value
                    # If equal, write only once, if not equal, write calculated value
                    if crcWritten == 0 and inputFileAddr == crcAddressLine:
                        outFileObj.write(line.strip()+' \n')
                        if ins.next().upper() == crcSignature:
                            print "Correct crc already present at {0} in input file, will skip writing calculated crc again to output file".format(inputFileAddr)
                        else:
                            print "Incorrect crc present at {0} in input file, will write calculated crc {1} to output file".format(inputFileAddr, crcSignature)
                        outFileObj.write(crcSignature+' \n')
                        crcWritten = 1
                        continue
                    # If crc not present, then write calculated value
                    elif crcWritten == 0 and inputFileAddr > crcAddressLine:
                        outFileObj.write(crcAddressLine+'\n')
                        outFileObj.write(crcSignature+' \n')
                        crcWritten = 1
                    outFileObj.write(inputFileAddr.strip()+'\n')
                else:
                    outFileObj.write(line.strip()+' \n')

    except Exception as e:
        print "\nException {0} occured, args: {1!r}".format(type(e).__name__, e.args)
        sys.exit(999)

    print("\nClosing Files\n")
    outFileObj.close()

## End of insertCrcSignature #########


#
# Main
#
if __name__ == '__main__':

    inFile=""
    outFile=""
    crcProgram=""

    if (len(sys.argv) < 4):
        print "\nUsage: %s <IN FILE> <OUT FILE> <CRC PROGRAM>\n" % sys.argv[0]
        sys.exit(1)
    else:
        #
        # Name of the firmware image file without the signature
        #
        inFile = sys.argv[1]
        if '/' not in inFile:
            inFile = os.getcwd() + '/' + inFile

        if not os.path.exists(inFile) or os.path.getsize(inFile) == 0:
            print "\nInput File {0} does not exist or is zero in size".format(inFile)
        #
        # Name of the firmware image file to be generated with the signature
        #
        outFile = sys.argv[2]
        if '/' not in outFile:
            outFile = os.getcwd() + '/' + outFile

        if os.path.exists(outFile):
            print "\nOutput File {0} already exists, will be overwritten".format(outFile)

        #
        # Name of the CRC calculation program to be used to calculate the
        # firmware image CRC
        #
        crcProgram = sys.argv[3]
        if '/' not in crcProgram:
            crcProgram = os.getcwd() + '/' + crcProgram

        if not os.path.exists(crcProgram) or os.path.getsize(crcProgram) == 0:
            print "\nCRC Program {0} does not exist or is zero in size".format(crcProgram)

    #
    # Call the function to insert the CRC signature
    #
    InsertCrcSignature(inFile, outFile, crcProgram)
OpenPOWER on IntegriCloud