summaryrefslogtreecommitdiffstats
path: root/src/boot/sbeCompression.py
blob: a9ac9a238d0f3ca889d5ea6f437a444170ba07e8 (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
#!/usr/bin/python
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/boot/sbeCompression.py $
#
# OpenPOWER sbe Project
#
# Contributors Listed Below - COPYRIGHT 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
import os
import subprocess
import re
import random
import sys
import binascii
import fileinput
import argparse
import struct
import operator
err = False

def compress(inputFile, compressedFile):

    try:
      f = open(inputFile, "rb")
    except IOError as e :
      print "I/O error File for File to be compressed."
      sys.exit()

    try:
      fW = open(compressedFile, "wb")
    except IOError as e :
      print "I/O error File for compressed file."
      sys.exit()

    if os.stat(inputFile).st_size < 4 :
      print "File is less than four bytes."
      sys.exit()

    instDict = dict()
    for i in range(0, os.stat(inputFile).st_size / 4 ):

       fourByt = f.read(4)

       if fourByt in instDict:

          iCount = instDict[fourByt]
          instDict[fourByt] = iCount + 1;

       else :

          iCount = 1
          instDict[fourByt] = iCount

    sortedList =  sorted(instDict.iteritems(), key=operator.itemgetter(1), reverse = True)

    sortedList[256:] = []
    instList = []
    iCount = 0
    for k, v in sortedList:
      instList.append(k)

    for x in instList:
       fW.write(x)

    fileSize = os.stat(inputFile).st_size
    fW.write(struct.pack(">Q",fileSize))

    f.seek(0, 0)
    strBits = ""
    count = 0

    #Create a bitmap for each four bytes of binary. 
    for i in range(0, os.stat(inputFile).st_size / 4 ):

       fourByt = f.read(4)
       if fourByt in instList:
          strBits += '1'

       else :
          strBits += '0'

       if ((len(strBits) == 32) or (i == (os.stat(inputFile).st_size / 4) - 1)):
          value = int(strBits, 2)
          fW.write(struct.pack('>I', value))
          strBits = ""
          count = count + 1

    value = 0
    #To make the bit map eight byte alligned in compressed image.
    if ((count % 2) == 0):
      padCount = count
    else:
      padCount = count + 1

    for i in range(count, padCount):
      fW.write(struct.pack('>I', value))

    f.seek(0, 0)

    for i in range(0, os.stat(inputFile).st_size / 4 ):

       fourByt = f.read(4)

       if fourByt in instList:
          ind = instList.index(fourByt)
          fW.write(struct.pack('>B', ind))

       else:
          fW.write(fourByt)

    f.close()
    fW.close()

def main( argv ):

    parser = argparse.ArgumentParser( description = "SBE Compression Parser" )
    parser.add_argument( '-l', '--imageLoc', type=str, help = 'Seeprom Binary Location' )
    parser.add_argument( '-i', '--image', type=str, help = 'Seeprom Binary ' )

    args = parser.parse_args()
    imagePath = args.imageLoc
    image = args.image

    #Make a copy of SEEPROM binary.
    cmd1 = "cp " + imagePath + "/" + image + " " + imagePath +  "/" + image + ".orig"
    rc = os.system(cmd1)
    if rc:
      print "Unable to make copy of seeprom binary"
      sys.exit()

    #Extract base from SEEPROM binary.
    cmd2 = imagePath + "/p9_xip_tool " + imagePath + "/" + image + " extract .base " + imagePath + "/" + image + ".base"
    rc = os.system(cmd2)
    if rc:
      print "Unable to extract the base from seeprom binary"
      sys.exit()

    #Compress the base section
    compress(imagePath + "/" + image + ".base", imagePath + "/" + image + ".base.compressed")

    #Delete the base section from SEEPEOM binary.
    cmd3 = imagePath + "/p9_xip_tool " + imagePath + "/" + image + " delete .base"
    rc = os.system(cmd3)
    if rc:
      print "Unable to delete base section from seeprom binary"
      sys.exit()

    #Append the base section from SEEPEOM binary.
    cmd4 = imagePath + "/p9_xip_tool " + imagePath +  "/" + image + " append .base " + imagePath + "/" + image + ".base.compressed"
    rc = os.system(cmd4)
    if rc:
      print "Unable to append the base section"
      sys.exit()

if __name__ == "__main__":
    main( sys.argv )

OpenPOWER on IntegriCloud