blob: fbf6d103df909dbd24a0242f7e7cc9d36af915b6 [file] [log] [blame]
T Michael Turney540b8ec2020-01-24 08:42:47 -08001#!/usr/bin/env python3
T Michael Turney101098c2018-05-01 15:59:37 -07002#============================================================================
3#
4#/** @file qgpt.py
5#
6# GENERAL DESCRIPTION
7# Generates QCom GPT header for wrapping Bootblock
8#
9# Copyright (c) 2018, The Linux Foundation. All rights reserved.
10#
11# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions are
13# met:
14# * Redistributions of source code must retain the above copyright
15# notice, this list of conditions and the following disclaimer.
16# * Redistributions in binary form must reproduce the above
17# copyright notice, this list of conditions and the following
18# disclaimer in the documentation and/or other materials provided
19# with the distribution.
20# * Neither the name of The Linux Foundation nor the names of its
21# contributors may be used to endorse or promote products derived
22# from this software without specific prior written permission.
23#
24# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
25# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
27# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
28# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35#
36#**/
37#
38
39import os
40import math
41import random
42import re
43import struct
44import sys
45import tempfile
46
47from binascii import crc32
48from optparse import OptionParser
49from types import *
50
51
52def UpdateMBR(options, GPTBlobBuffer):
53 i = 0x1BE
54 GPTBlobBuffer[i + 0] = 0x00 # not bootable
55 GPTBlobBuffer[i + 1] = 0x00 # head
56 GPTBlobBuffer[i + 2] = 0x01 # sector
57 GPTBlobBuffer[i + 3] = 0x00 # cylinder
58 GPTBlobBuffer[i + 4] = 0xEE # type
59 GPTBlobBuffer[i + 5] = 0xFF # head
60 GPTBlobBuffer[i + 6] = 0xFF # sector
61 GPTBlobBuffer[i + 7] = 0xFF # cylinder
62 GPTBlobBuffer[i + 8:i + 8 + 4] = [0x01, 0x00, 0x00, 0x00]
63
64 GPTBlobBuffer[i + 12:i + 16] = [0x00, 0x0f, 0x00, 0x00]
65
66 # magic byte for MBR partitioning - always at this location regardless of
67 # options.sector
68 GPTBlobBuffer[510:512] = [0x55, 0xAA]
69 return i
70
71
72def UpdatePartitionEntry(options, GPTBlobBuffer):
73
74 i = 2 * options.sector_size
75 # GUID of Boot Block
76 GPTBlobBuffer[i:i + 16] = [0x2c, 0xba, 0xa0, 0xde, 0xdd, 0xcb, 0x05, 0x48,
77 0xb4, 0xf9, 0xf4, 0x28, 0x25, 0x1c, 0x3e, 0x98]
78 i += 16
79
80 #This is to set Unique Partition GUID. Below Hex Value is : 00ChezaBootblock00
81 GPTBlobBuffer[i:i + 16] = [0x00, 0x43, 0x68, 0x65, 0x7a, 0x61, 0x42, 0x6f,
82 0x6f, 0x74, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x00]
83 i += 16
84
85 # LBA of BootBlock Start Content
86 GPTBlobBuffer[i:i + 8] = [0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
87 i += 8
88
89 # End LBA of BootBlock Content
90 GPTBlobBuffer[i] = options.end_lba & 0xFF
91 GPTBlobBuffer[i+1] = (options.end_lba>>8) & 0xFF
92 GPTBlobBuffer[i+2] = (options.end_lba>>16) & 0xFF
93 GPTBlobBuffer[i+3] = (options.end_lba>>24) & 0xFF
94 GPTBlobBuffer[i+4] = (options.end_lba>>32) & 0xFF
95 GPTBlobBuffer[i+5] = (options.end_lba>>40) & 0xFF
96 GPTBlobBuffer[i+6] = (options.end_lba>>48) & 0xFF
97 GPTBlobBuffer[i+7] = (options.end_lba>>56) & 0xFF
98 i += 8
99
100 # Attributes
101 GPTBlobBuffer[i:i + 8] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
102 i += 8
103
104 # Label
105 GPTBlobBuffer[i:i + 17] = [0x62, 0x00, 0x6f, 0x00, 0x6f, 0x00, 0x74, 0x00,
106 0x62, 0x00, 0x6c, 0x00, 0x6f, 0x00, 0x63, 0x00, 0x6b]
107
108 return i
109
110def UpdateGPTHeader(options, GPTBlobBuffer):
111
112 i = options.sector_size
113 # Signature and Revision and HeaderSize i.e. "EFI PART" and 00 00 01 00
114 # and 5C 00 00 00
115 GPTBlobBuffer[i:i + 16] = [0x45, 0x46, 0x49, 0x20, 0x50, 0x41, 0x52, 0x54,
116 0x00, 0x00, 0x01, 0x00, 0x5C, 0x00, 0x00, 0x00]
117 i += 16
118
119 # CRC is zeroed out till calculated later
120 GPTBlobBuffer[i:i + 4] = [0x00, 0x00, 0x00, 0x00]
121 i += 4
122
123 # Reserved, set to 0
124 GPTBlobBuffer[i:i + 4] = [0x00, 0x00, 0x00, 0x00]
125 i += 4
126
127 # Current LBA
128 GPTBlobBuffer[i:i + 8] = [0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
129 i += 8
130
131 # Backup LBA, No Backup Gpt Used
132 GPTBlobBuffer[i:i + 8] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
133 i += 8
134
135 # First Usuable LBA (qc_sec + bootblock location)
136 GPTBlobBuffer[i:i + 8] = [0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
137 i += 8
138
139 # Last Usuable LBA (qc_sec + bootblock end location)
140 GPTBlobBuffer[i] = options.end_lba & 0xFF
141 GPTBlobBuffer[i+1] = (options.end_lba>>8) & 0xFF
142 GPTBlobBuffer[i+2] = (options.end_lba>>16) & 0xFF
143 GPTBlobBuffer[i+3] = (options.end_lba>>24) & 0xFF
144 GPTBlobBuffer[i+4] = (options.end_lba>>32) & 0xFF
145 GPTBlobBuffer[i+5] = (options.end_lba>>40) & 0xFF
146 GPTBlobBuffer[i+6] = (options.end_lba>>48) & 0xFF
147 GPTBlobBuffer[i+7] = (options.end_lba>>56) & 0xFF
148 i += 8
149
150 # GUID
151 GPTBlobBuffer[i:i + 16] = [0x32,0x1B,0x10,0x98,0xE2,0xBB,0xF2,0x4B,
152 0xA0,0x6E,0x2B,0xB3,0x3D,0x00,0x0C,0x20]
153 i += 16
154
155 # Partition Table Entry LBA
156 GPTBlobBuffer[i:i + 8] = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
157 i += 8
158
159 # Number of Partition Entries
160 GPTBlobBuffer[i:i + 4] = [0x01, 0x00, 0x00, 0x00]
161 i += 4
162
163 # Size of One Partition Entry
164 GPTBlobBuffer[i:i + 4] = [0x80, 0x00, 0x00, 0x00]
165 i += 4
166
167 # CRC of Partition Entry
168
169 PartEntry = GPTBlobBuffer[options.sector_size*2:options.sector_size*2 + 128]
Douglas Anderson1bdfe8c2019-11-20 11:18:52 -0800170 CalcEntryCRC = crc32(b''.join(struct.pack("B", x) for x in PartEntry))
T Michael Turney101098c2018-05-01 15:59:37 -0700171
172 GPTBlobBuffer[i] = CalcEntryCRC & 0xFF
173 GPTBlobBuffer[i+1] = (CalcEntryCRC>>8) & 0xFF
174 GPTBlobBuffer[i+2] = (CalcEntryCRC>>16) & 0xFF
175 GPTBlobBuffer[i+3] = (CalcEntryCRC>>24) & 0xFF
176 i += 4
177
178 # CRC of Partition Table Header
179 GPTHeader = GPTBlobBuffer[options.sector_size:options.sector_size + 92]
Douglas Anderson1bdfe8c2019-11-20 11:18:52 -0800180 CalcEntryCRC = crc32(b''.join(struct.pack("B", x) for x in GPTHeader))
T Michael Turney101098c2018-05-01 15:59:37 -0700181 i = options.sector_size + 16
182
183 GPTBlobBuffer[i] = CalcEntryCRC & 0xFF
184 GPTBlobBuffer[i+1] = (CalcEntryCRC>>8) & 0xFF
185 GPTBlobBuffer[i+2] = (CalcEntryCRC>>16) & 0xFF
186 GPTBlobBuffer[i+3] = (CalcEntryCRC>>24) & 0xFF
187
188 return i
189
190
191if __name__ == '__main__':
192 usage = 'usage: %prog [OPTIONS] INFILE OUTFILE\n\n' + \
193 'Packages IMAGE in a GPT format.'
194 parser = OptionParser(usage)
195 parser.add_option('-s', type="int", dest='sector_size', default=4096,
196 help='Sector size in bytes [Default:4096(4KB)]',
197 metavar='SIZE')
198
199 (options, args) = parser.parse_args()
200 if len(args) != 2:
201 print("Invalid arguments! Exiting...\n")
202 parser.print_help()
203 sys.exit(1)
204
205 if options.sector_size != 4096 and options.sector_size != 512:
206 print("Invalid Sector Size")
207 sys.exit(1)
208
209 options.inputfile = args[0]
210 options.outputfile = args[1]
211
Douglas Anderson1bdfe8c2019-11-20 11:18:52 -0800212 with open(options.inputfile, 'rb+') as fin:
T Michael Turney101098c2018-05-01 15:59:37 -0700213 bb_buffer = fin.read()
214
215 # Round up to next sector if bootblock size not evenly divisible
Douglas Anderson1bdfe8c2019-11-20 11:18:52 -0800216 options.end_lba = ((len(bb_buffer) + options.sector_size - 1) //
T Michael Turney101098c2018-05-01 15:59:37 -0700217 options.sector_size)
218 # Add 3 sectors for MBR, GPT header and GPT partition entry
219 options.end_lba += 3
220 # Subtract one because this is last usable LBA, not amount of LBAs
221 options.end_lba -= 1
222
223 GPTBlobBuffer = [0] * (options.sector_size*3) #Size of MBR+GPT+PART_ENTRY
224
225 UpdateMBR(options, GPTBlobBuffer)
226
227 UpdatePartitionEntry(options, GPTBlobBuffer)
228
229 UpdateGPTHeader(options, GPTBlobBuffer)
230
231 with open(options.outputfile, 'wb') as fout:
232 for b in GPTBlobBuffer:
233 fout.write(struct.pack("B", b))
234 fout.write(bb_buffer)