blob: 58f6c1bfd827e427c51213d3a685f8f58c847aca [file] [log] [blame]
yu.dongc33b3072024-08-21 23:14:49 -07001#!/usr/bin/perl
2#
3# Copyright Statement:
4# --------------------
5# This software is protected by Copyright and the information contained
6# herein is confidential. The software may not be copied and the information
7# contained herein may not be used or disclosed except with the written
8# permission of MediaTek Inc. (C) 2011
9#
10# BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
11# THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
12# RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
13# AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
14# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
15# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
16# NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
17# SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
18# SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
19# THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
20# NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
21# SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
22#
23# BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
24# LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
25# AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
26# OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
27# MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
28#
29# THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
30# WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
31# LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
32# RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
33# THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
34#
35#
36#*****************************************************************************
37#*============================================================================
38#* HISTORY
39#* Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
40#*------------------------------------------------------------------------------
41#* $Revision$
42#* $Modtime$
43#* $Log$
44#*
45#* 12 27 2017 hanna.chiang
46#* [MOLY00294629] [Gen93] [SystemService] [Auto-Gen] [MT6763] [ZION] [AMMS] [DRDI] AMMS-DRDI and Security Boot can be enabled simultaneously
47#*
48#* .[MD Security] PBP sign length determined by assign_sign_len (Merged from R3)
49#*
50#* 12 27 2017 hanna.chiang
51#* [MOLY00294629] [Gen93] [SystemService] [Auto-Gen] [MT6763] [ZION] [AMMS] [DRDI] AMMS-DRDI and Security Boot can be enabled simultaneously
52#* .[MD Security] PBP sign length determined by assign_sign_len (Merged from R3)
53#*
54#* 11 29 2017 raymondwt.chen
55#* .PBP for AMMS-DRDI
56#*
57#* 12 20 2016 raymondwt.chen
58#* [MOLY00219375] [UMOLYA][Call for checkin] GFH header for MT6293
59#* .
60#*
61#* 09 10 2015 raymondwt.chen
62#* [MOLY00138049] [Jade] security code porting
63#* .Jade SBC change (1) always verify md1 (depends AP or MD) (2) use AP test key
64#*
65#* 09 26 2014 raymondwt.chen
66#* [MOLY00079632] [Call for check-in] UMOLY SBC
67#* .Phase-in UMOLY SBC
68#*
69#* 09 21 2012 yen-tsung.cheng
70#* [MOLY00003906] [SystemService][Auto-Gen][Internal Refinement] Support BOOT_CERT & multibin on MOLY
71#* <saved by Perforce>
72#*
73#*------------------------------------------------------------------------------
74#* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
75#*============================================================================
76#****************************************************************************
77BEGIN { push @INC, './tools/', './tools/MemoryUtility/' } # add additional library path
78use File::Basename;
79use File::Copy;
80use strict;
81use LinkerOutputParser;
82
83my($debug) = 1;
84my($iv_region_size) = 0x400;
85
86#Usage: gfh_process.pl BINFILE_PATH(folder or file) IntermediateFile MakeFile
87
88my ($bin_file) = $ARGV[0];
89my ($intermediate) = $ARGV[1];
90my ($makefile) = $ARGV[2];
91my ($sym_file) = $ARGV[3];
92my ($log_file) = $ARGV[4];
93
94my($pbp_win32)= 'tools/pbp.exe';
95my($pbp_linux)= 'tools/pbp';
96my($key) = 'tools/key_info.ini';
97#my($key_pubk) = 'tools/key_info_pubk.ini';
98my($setting) = 'tools/brom_setting.ini';
99
100my($pbp) = $^O=~/linux/i ? $pbp_linux : $pbp_win32;
101
102die "Intermediate file $intermediate doesn't exist" if not -e $intermediate;
103
104# parse SYM and get section info
105&LinkerOutputParser::FileParse($sym_file);
106my ($nSIGBase) = hex(&LinkerOutputParser::GetLinkerSymbolAddress("ROM_SIGNATURE_SECTION", LinkerSymPostfix::Base, LinkerSymPrefix::Load));
107my ($nSIGLength) = hex(&LinkerOutputParser::GetLinkerSymbolAddress("ROM_SIGNATURE_SECTION", LinkerSymPostfix::Length, LinkerSymPrefix::Image));
108my ($nSignLength) = ($nSIGBase) + ($nSIGLength) - ($iv_region_size);
109
110if(-d $bin_file)
111{#process inside bin
112 opendir(BIN_DIR, $bin_file) or die "cannot open build folder $bin_file!";
113 while (defined(my $strBinFileName = readdir(BIN_DIR)))
114 {
115 my $strBinFilePath = "$bin_file/$strBinFileName";
116 if ($strBinFileName !~ /\.txt$|\.cfg$/ and !-d $strBinFilePath)
117 {
118 &ExecutePBPByName($strBinFilePath);
119 }
120 }
121 closedir(BIN_DIR);
122}
123elsif(-e $bin_file)
124{
125 &ExecutePBPByName($bin_file);
126}
127
128exit 0;
129
130
131sub ExecutePBPByName
132{
133 my ($strBinFilePath) = @_;
134 my $result = 0;
135
136 # if GFH header exist or initial vector mark exist (the image also has GFH after IV)
137 if(&IsGFH($strBinFilePath) or &IsIVRegionExist($strBinFilePath))
138 {
139 my $strBinFileName = basename($strBinFilePath);
140 if($strBinFileName =~ /BOOT_CERT/i)
141 {#skip BOOT_CERT due to causing Error: Something wrong in GFH parsing, 8001
142 print "==> skip BOOT_CERT processing\n";
143 return;
144 }
145 elsif($strBinFileName =~ /SECURE/i)
146 {
147 $result = &call_pbp("-secro",$strBinFilePath);
148 }
149 elsif ($strBinFileName =~ /BOOTLOADER/i)
150 {
151 $result = &call_pbp("-bootloader",$strBinFilePath);
152 }
153 else
154 {
155 $result = &call_pbp("-maui",$strBinFilePath);
156 }
157
158 if($result !=0)
159 {
160 # my $Error = &IsPBPWrong($log_file);
161 # if($Error != 8001)
162 # {
163 print("Error[$result]: Failed in gfh_process.pl\n");
164 exit $result;
165 # }
166 }
167 }
168}
169
170sub call_pbp
171{
172 my($status) = 0;
173 my($arg, $file) = @_;
174
175 backupOrigBin($file) if $debug;
176 print("==> Proecssing $file\n");
177 my $cmd = "$pbp -m $intermediate -m \"$makefile\" -i $key -i $setting -assign_sign_len $nSignLength $file";
178 print("cmd=$cmd\n");
179 print "====================================================\n";
180 #$status = system("$pbp", "-m", $intermediate, "-m", $makefile, "-i", $key, "-i", $key_pubk, "-i", "$setting", $file);
181 $status = system($cmd);
182 print "====================================================\n";
183 print("[call_pbp]status=", $status, "\n\n");
184 return($status>>8) if $status != 0;
185 return 0;
186}
187
188
189sub backupOrigBin
190{
191 my($strBinFilePath) = @_;
192 my($path, $file) = (dirname($strBinFilePath), basename($strBinFilePath));
193 my($backup_path) = "$path/backup";
194 print "Backup $file to $backup_path\n";
195 mkdir($backup_path) if(! -d $backup_path);
196
197 copy($strBinFilePath, "$backup_path/$file.orig") or die "$!";
198}
199
200sub isGFHFileInfo
201{
202 my @Buffer = @{$_[0]};
203 my $is_gfh_file_info = 0;
204
205 my ($strFILEINFO, $strMMM) = (undef, undef);
206
207 # GFH_file_info pattern (0-2: 4D 4D 4D, 8-16: FILE_INFO)
208 for(0..2)
209 {
210 $strMMM .= $Buffer[$_];
211 }
212 for(8..16)
213 {
214 $strFILEINFO .= $Buffer[$_];
215 }
216
217 $is_gfh_file_info =1 if($strMMM eq "MMM" and $strFILEINFO eq "FILE_INFO");
218
219 return $is_gfh_file_info;
220}
221
222sub IsIVRegionExist
223{
224 my ($strFilePath) = @_;
225 my $is_iv_exist = 0;
226
227 if(-e $strFilePath)
228 {
229
230 # Get 17bytes (0-2: 4D 4D 4D, 8-16: FILE_INFO
231 open (FILE_HANDLE, "<$strFilePath") or &error_handler("$strFilePath: open file error!");
232 seek FILE_HANDLE, $iv_region_size, 0;
233 my ($nIndex, $data) = (0, undef);
234 my @Buffer;
235 while(read(FILE_HANDLE, $data, 1))
236 {
237 $Buffer[$nIndex++] = $data;
238 last if($nIndex > 16);
239 }
240 close FILE_HANDLE;
241
242 $is_iv_exist = isGFHFileInfo(\@Buffer);
243 }
244
245 if($is_iv_exist == 0)
246 {
247 print "Initial vector not found\n";
248 }
249 else
250 {
251 print "Initial vector found\n";
252 }
253 return $is_iv_exist;
254}
255
256sub IsGFH
257{
258 my ($strFilePath) = @_;
259 my $is_gfh = 0;
260
261 if(-e $strFilePath)
262 {
263 # Get 17bytes (0-2: 4D 4D 4D, 8-16: FILE_INFO
264 open (FILE_HANDLE, "<$strFilePath") or &error_handler("$strFilePath: open file error!");
265 #binmode(FILE_HANDLE);
266 my ($nIndex, $data) = (0, undef);
267 my @Buffer;
268 while(read(FILE_HANDLE, $data, 1))
269 {
270 $Buffer[$nIndex++] = $data;
271 last if($nIndex > 16);
272 }
273 close FILE_HANDLE;
274
275
276 $is_gfh = isGFHFileInfo(\@Buffer);
277
278 }
279
280 if($is_gfh == 0)
281 {
282 print "GFH not found\n";
283 }
284 else
285 {
286 print "GFH found\n";
287 }
288
289 return $is_gfh;
290}
291
292sub IsPBPWrong
293{
294 my ($strLogPath) = @_;
295 my $Error = 0;
296 open FILE, "<$strLogPath" or &error_handler("$strLogPath: open file error!");
297 while(<FILE>)
298 {
299 my $line = $_;
300 $Error= 1 if($line =~ /Error/);
301 if($line =~ /Error:(.*)(\d{4,})/)
302 {
303 $Error = $2;
304 print "\n[Parsing Log File]Error Message: $1$Error\n";
305 last;
306 }
307 }
308 close FILE;
309 return $Error;
310}
311
312#****************************************************************************
313# subroutine: error_handler
314# input: $error_msg: error message
315# $file: filename
316# $line_no: line number
317#****************************************************************************
318sub error_handler
319{
320 my ($error_msg) = @_;
321 my ($pack_name, $file, $line_no) = caller;
322 my $final_error_msg = "GFH_PROCESS ERROR: $error_msg at $file line $line_no\n";
323 die $final_error_msg;
324}