blob: a671aef29735d13551a1c9a5a6ec7d83e6527e37 [file] [log] [blame]
rjw6c1fd8f2022-11-30 14:33:01 +08001#!/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) 2008
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#* Filename:
39#* ---------
40#* CheckBinaryBlockUsage.pl
41#*
42#* Project:
43#* --------
44#* Maui_Software
45#*
46#* Description:
47#* ------------
48#* This script will check if code ROM size is enough for all binaries.
49#*
50#*------------------------------------------------------------------------------
51#* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
52#*============================================================================
53#****************************************************************************/
54
55#****************************************************************************
56# Usage
57#****************************************************************************
58BEGIN { push @INC, "pcore/" , '.\\pcore\\tools\\' } # add additional library path
59use Getopt::Std;
60use POSIX;
61
62sub Usage {
63 print "Usage: perl CheckBinaryBlockUsage.pl ~flash_cfg_tmp.c <BIN_FILE_PATH> <PLATFORM> <BUILD_SYS_PATH>\n";
64 exit(1);
65}
66
67use File::stat;
68use File::Basename;
69use Math::BigInt;
70
71#****************************************************************************
72# parsing command arguments
73#****************************************************************************
74$DebugPrint = 1;
75print "ARGV[0]:$ARGV[0] ARGV[1]:$ARGV[1] ARGV[2]:$ARGV[2] ARGV[3]:$ARGV[3] ARG_NUM $#ARGV\n" if (0 != $DebugPrint);
76while ($#ARGV != -1) {
77 if ($ARGV[0] =~ /-h/i) {
78 &Usage;
79 }
80 elsif ($#ARGV == 3) {
81 $FLASH_CFG = $ARGV[0];
82 (!-e $FLASH_CFG) && die "$FLASH_CFG does NOT EXIST!\n";
83 $binary_path = $ARGV[1];
84 $platform = $ARGV[2];
85 last;
86 }
87 else {
88 &Usage;
89 }
90}
91
92print "Running pcore\\tools\\CheckBinaryBlockUsage.pl ....\n";
93if($platform eq 'MT6280') {
94 $d = $bootloader_path;
95 print "path: $d\n";
96 if ($d =~ /\.(bin|txt)$/i ) {
97 push(@binary, $d);
98 }
99 print "@binary\n";
100}
101else{
102
103 # Get all cfg files in the target build path
104 opendir (DIR, "$binary_path") or die "build folder doesn't exist: $binary_path\n";
105 my @cfgfiles = grep {/.+\.cfg$/} readdir DIR;
106 close DIR;
107 if (@cfgfiles == 1) {
108 $cfg_file = pop @cfgfiles;
109 }
110 else {
111 die "There are more than one cfg files or no cfg files in $binary_path!\n";
112 }
113 $cfg_path = $binary_path . "/" . $cfg_file;
114 print "CFG File: $cfg_path\n" if (0 != $DebugPrint);
115
116 open(CFG_FILE_HANDLE, "$cfg_path") or die "Cannot open $cfg_path\n";
117
118 while ($line = <CFG_FILE_HANDLE>) {
119 if ($line =~ /\s*- file:/) {
120 chomp($line);
121 $line =~ s/\s*- file: //;
122 $file = $binary_path . "/" . $line;
123 push(@binary, $file);
124 }
125 }
126 close(CFG_FILE_HANDLE);
127}
128
129
130#****************************************************************************
131# parsing block size
132# block size is got from flash_opt_gen.h
133#****************************************************************************
134my $bin_path = $ARGV[3];
135my $FLASH_OPT_GEN_H = $bin_path."/flash_opt_gen.h";
136print "$bin_path..\n $FLASH_OPT_GEN_H\n"if $DebugPrint == 1;
137open (FLASH_OPT_GEN_H, "<$FLASH_OPT_GEN_H") or die "Cannot open $FLASH_OPT_GEN_H\n";
138
139#$blk_size = 128 * 1024; # Assume 128KB per block
140while (<FLASH_OPT_GEN_H>)
141{
142 if (/^#define\s+(\w+)\s+\(([\w|\-]*)\)/ || /^#define\s+(\w+)\s+([\w|\-]*)/)
143 {
144 if($1 eq 'NAND_BLOCK_SIZE')
145 {
146 $blk_size = $2*1024;#NAND_BLOCK_SIZE is KB
147 }
148 }
149}
150close FLASH_OPT_GEN_H;
151
152print "blk_size is $blk_size\n";
153
154
155# Get code rom size from ~flash_cfg_tmp.c
156open (FLASH_CFG, "<$FLASH_CFG") or die "Cannot open $FLASH_CFG\n";
157
158while ($line = <FLASH_CFG>)
159{
160 $backup = $/; undef $/;
161 $reading = <FLASH_CFG>;
162 $/ = $backup;
163}
164close FLASH_CFG; #End of open (FLASH_CFG, "<$FLASH_CFG")
165
166my ($addr, $len, $total_block, $bad_block);
167if ($reading =~ /int\s+flash_base_address\s*=\s*(.*);/)
168{
169 $addr = `perl -e "print ($1)" 2>&1`;
170 $base_block = $addr / ($blk_size);
171}
172if ($reading =~ /int\s+allocated_fat_space\s*=\s*(.*);/)
173{
174 $len = `perl -e "print ($1)" 2>&1`;
175}
176
177$total_block = 0;
178$idx = 0;
179my $ImageSize = 0;
180foreach $file (@binary) {
181 if(&CheckGFHMaxSize($file))
182 {
183 if($ImageSize == 0xFFFFFFFF)
184 {
185 $block = ceil(&get_size($file)/($blk_size));#Max Size is 0xffffffff
186 print "[1-1]GFH info, image size is $block\n";
187 }
188 else
189 {
190 $block = $ImageSize/$blk_size;#Max Size is not 0xffffffff
191 print "[1-2]GFH info, image size is $block\n";
192 }
193 }
194 else
195 {
196 $block = ceil(&get_size($file)/($blk_size));#image do not have GFH info
197 }
198 $total_block+=$block;
199 printf("\t[%d]$file size = %dKB, block # = %d\n", $idx, &get_size($file)/1024, $block) if $DebugPrint == 1;
200 $idx = $idx + 1;
201}
202
203$bad_block = ceil($total_block * (0.02));
204printf("\t[%d]Estimated Bad Block # = %d\n", $idx, $bad_block) if $DebugPrint == 1;
205$idx = $idx + 1;
206
207
208# Add 7 blocks for image list
209$img_list_blk = 7;
210$total_block = $total_block + $img_list_blk;
211printf("\t[%d]Image List Block # = %d\n", $idx, $img_list_blk) if $DebugPrint == 1;
212
213$total_block = $total_block + $bad_block;
214
215if ($total_block > $base_block) {
216 print "Error: BIN ($total_block blocks) and FAT ($base_block blocks) on NAND Flash overlap risk were detected.\n";
217 print "Please decrease ROM sizes by reducing features or adjust custom_MemoryDevice.h configuration.\n";
218 exit(1);
219}
220else {
221 print "BIN size = $total_block blocks on NAND Flash\n";
222 print "FAT start at $base_block block.\n";
223}
224exit;
225
226#if ($query_mode == 0) {
227 open (FILE_HANDLE, "<$themf") or die "cannot open $themf\n";
228 while (<FILE_HANDLE>) {
229 if (/^(\S+)\s*=\s*(\S+)/) {
230 $keyname = lc($1);
231 defined($${keyname}) && warn "$1 redefined in $themf!\n";
232 $${keyname} = uc($2);
233 }
234 }
235
236 if ($secure_support eq "TRUE") {
237 $sec_support = 1;
238 ($secure_version !~ /\d{1,4}/) && die "SECURE_VERSION: $secure_version should be a integer no longer than 4 digits!\n";
239 ($secure_jtag_enable !~ /(TRUE|FALSE)/) && die "SECURE_JTAG_ENABLE: $secure_jtag_enable should be TRUE or FALSE!\n";
240 $secure_jtag_enable = ($secure_jtag_enable =~ /TRUE/)? 1 : 0;
241 (length($secure_custom_name) > 31) && die "SECURE_CUSTOM_NAME: $secure_custom_name should NOT exceed 31 bytes!\n";
242 }
243
244my %BBtbl_EMI =
245 (
246 'MT6225' => 1,
247 'MT6228' => 1,
248 'MT6229' => 2,
249 'MT6230' => 2,
250 'MT6235' => 3,
251 'MT6235B' => 3,
252 'MT6238' => 3,
253 'MT6239' => 3,
254 'MT6268A' => 3,
255 );
256 if (($nand_flash_booting ne "NONE") || (exists $BBtbl_EMI{$platform}))
257 {
258 open (CUSTOM_EMI_H, "<$memory_cfg") or die "cannot open $memory_cfg\n";
259 $backup = $/; undef $/;
260 $reading = <CUSTOM_EMI_H>;
261 $/ = $backup;
262 close CUSTOM_EMI_H; #End of open (FLASH_CFG, "<$FLASH_CFG")
263
264 if ($reading =~ /\n\s*#define\s+DRAM_CS\s+/gs
265 and $reading =~ /\n\s*#define\s+DRAM_SIZE\s+/gs)
266 {
267 $needEMIInfo = 1;
268 }
269 }
270#}
271#****************************************************************************
272# parsing flash configuration from a file or a string
273#****************************************************************************
274my $full_hex_str = '';
275my $full_emi_str;
276
277if ($FLASH_CFG =~ /custom_MemoryDevice\.h/i)
278{
279# parsing flash configuration from custom_MemoryDevice.h (NAND_FLASH_BOOTING)
280
281 open (FLASH_CFG, "<$FLASH_CFG") or die "cannot open $FLASH_CFG\n";
282
283 while ($line = <FLASH_CFG>)
284 {
285 if ($line =~ /^\s*#define\s+NAND_BOOTING_FLASH_BASE_ADDRESS\s+(0x[0-9A-Fa-f]+)/)
286 {
287 $addr = hex $1;
288 }
289 if ($line =~ /^\s*#define\s+NAND_BOOTING_ALLOCATED_FAT_SPACE\s+(0x[0-9A-Fa-f]+)/)
290 {
291 $len = hex $1;
292 }
293 }
294 close FLASH_CFG; #End of open (FLASH_CFG, "<$FLASH_CFG")
295
296 $hex_str = sprintf("%02X%04X%04X%04X%04X%08X%08X", 1 , 0, 0, 0, 0, $addr, $len);
297 $full_hex_str .= $hex_str;
298
299 print "[Flash Cfg 0] $hex_str\n" if $DebugPrint == 1;
300
301}
302elsif ($FLASH_CFG =~ /flash_opt\.h/i)
303{
304# parsing flash configuration from flash_opt.h (NOR-XIP)
305
306 open (FLASH_CFG, "<$FLASH_CFG") or die "cannot open $FLASH_CFG\n";
307
308 $backup = $/; undef $/;
309 $reading = <FLASH_CFG>;
310 $/ = $backup;
311
312 close FLASH_CFG; #End of open (FLASH_CFG, "<$FLASH_CFG")
313
314 #// FLASH_BASE_ADDRESS_FOR_SCATTER 0x01C00000
315 #// ALLOCATED_FAT_SPACE_FOR_SCATTER 0x00400000
316 if ($reading =~ /\/\/\s+FLASH_BASE_ADDRESS_FOR_SCATTER\s+(0x[0-9A-Fa-f]+)/gs)
317 {
318 $addr = hex $1;
319 }
320 if ($reading =~ /\/\/\s+ALLOCATED_FAT_SPACE_FOR_SCATTER\s+(0x[0-9A-Fa-f]+)/gs)
321 {
322 $len = hex $1;
323 }
324
325 if (($system_drive_on_nand eq "TRUE") && ($nand_flash_booting eq "NONE"))
326 {
327 $addr = 0x0;
328 $len = 0x0;
329 }
330
331 $hex_str = sprintf("%02X%04X%04X%04X%04X%08X%08X", 1, 0, 0, 0, 0, $addr, $len);
332 $full_hex_str .= $hex_str;
333}
334elsif ($FLASH_CFG =~ /flash_cfg_tmp\.c/i)
335{
336 open (FLASH_CFG, "<$FLASH_CFG") or die "cannot open $FLASH_CFG\n";
337
338 $backup = $/; undef $/;
339 $reading = <FLASH_CFG>;
340 $/ = $backup;
341
342 close FLASH_CFG; #End of open (FLASH_CFG, "<$FLASH_CFG")
343
344 #int flash_base_address = 0x00E00000 ;
345 #int flash_base_address = ( (0xD00000) + 0x00100000) ;
346 #int allocated_fat_space = 0x00200000 ;
347 #int allocated_fat_space = ( (0x300000) - 0x00100000) ;
348 if ($reading =~ /int\s+flash_base_address\s*=\s*(.*);/)
349 {
350 $addr = `perl -e "print ($1)" 2>&1`;
351 }
352 if ($reading =~ /int\s+allocated_fat_space\s*=\s*(.*);/)
353 {
354 $len = `perl -e "print ($1)" 2>&1`;
355 }
356 $hex_str = sprintf("%02X%04X%04X%04X%04X%08X%08X", 1, 0, 0, 0, 0, $addr, $len);
357 $full_hex_str .= $hex_str;
358}
359elsif ($FLASH_CFG =~ /\.[ch]/i)
360{
361 die "Not Support $FLASH_CFG parsing Yet";
362}
363else
364{
365# parsing flash configuration from a string
366 $full_hex_str = $FLASH_CFG;
367 $full_emi_str = $EMI_INFO;
368 if ((length($full_emi_str) == 0)
369 && (($platform eq "MT6228") || ($platform eq "MT6229") || ($platform eq "MT6225") || ($platform eq "MT6230"))
370 ) {
371 die "Cannot get EMI_INFO.";
372 }
373 print "full_hex_str=$full_hex_str\n" if $DebugPrint == 1;
374
375 print "[Flash Cfg 0] $FLASH_CFG\n" if $DebugPrint == 1;
376}
377
378######################################
379#Get register values from custom_MEI.h
380######################################
381## Check DRAM setting in custom_EMI.h
382
383#if ($needEMIInfo == 1 and not defined $full_emi_str)
384#{
385# $full_emi_str = getRegisterAddress($platform, $memory_cfg);
386#}
387#elsif (not defined $full_emi_str)
388#{
389# $full_emi_str = '';
390#}
391
392##################################################################################################################
393
394######################################
395# Binary String
396######################################
397$flash_len = length($full_hex_str);
398if ( ($flash_len == 2)
399 || ((($flash_len-34)%32) !=0)
400 || ($flash_len > 34+5*32) )
401{
402 die "The length of $full_hex_str ($flash_len) should be 34+32n. (5>=n>=0)\n";
403}
404$flash_device_count = hex(substr($full_hex_str, 0, 2));
405
406if ((($flash_len-2)/32) != $flash_device_count)
407{
408 die "($flash_len-2)/32 should be $flash_device_count\n";
409}
410
411
412#****************************************************************************
413# in query, output @cfg_list with hex string
414#****************************************************************************
415if ($query_mode == 1) {
416 print "FLASH_CFG = $full_hex_str\n";
417 print "EMI_INFO = $full_emi_str\n";
418 exit(0);
419}
420
421#****************************************************************************
422# Parse symbol file
423#****************************************************************************
424(open(LOGF, "<$symFile")) || die "$symFile can NOT been opened\n";
425my %SYM_TBL =
426 (
427 'INT_Vectors' => undef,
428 'IMG_INFO' => undef,
429 'EMI_INFO' => undef,
430 'g_CustParaCfg' => undef,
431 'g_secinfo' => undef,
432 'g_secinfo_tail' => undef,
433 );
434
435my %SYM_LEN =
436 (
437 'g_CustParaCfg' => undef,
438 );
439
440my $prev_symbol;
441while(<LOGF>)
442{
443 # 0x00000000 A INT_Vectors
444 if (/^0x(\S+)\s+\w\s+(\S+)$/)
445 {
446 if (exists $SYM_TBL{$2})
447 {
448 $SYM_TBL{$2} = hex($1);
449 }
450
451
452 if (exists $SYM_LEN{$prev_symbol})
453 {
454 $SYM_LEN{$prev_symbol} = hex($1) - $SYM_TBL{$prev_symbol};
455 }
456 $prev_symbol = $2;
457 }
458}
459close(LOGF);
460
461#****************************************************************************
462# Validation symbol and feature combination
463#****************************************************************************
464die "Could not find IMG_INFO in $symFile\n" unless defined $SYM_TBL{'IMG_INFO'};
465if ($sec_support)
466{
467 die "Could not find g_secinfo in $symFile\n" unless defined $SYM_TBL{'g_secinfo'};
468}
469
470#****************************************************************************
471# Setup BINARY File Offset
472#****************************************************************************
473my %ROM_POS =
474 (
475 'IMG_INFO' => undef,
476 'EMI_INFO' => undef,
477 'g_CustParaCfg' => undef,
478 'g_secinfo' => undef,
479 'g_secinfo_tail' => undef,
480 );
481map { $ROM_POS{$_} = $SYM_TBL{$_} - $SYM_TBL{'INT_Vectors'}; } keys %ROM_POS;
482
483
484#****************************************************************************
485# set ROMINFO version
486#****************************************************************************
487if ((($system_drive_on_nand eq "TRUE") && ($nand_flash_booting eq "NONE")))
488{
489 $rom_info_ver = "MTK_ROMINFO_v05";
490}
491
492#****************************************************************************
493# append information to the file
494#****************************************************************************
495open (FILE, "+<$file") or die "cannot open $file\n";
496binmode(FILE);
497
498seek(FILE, $ROM_POS{'IMG_INFO'}, 0);
499
500 seek(FILE, 16, 1);
501
502
503 print FILE "$bin_name";
504 print FILE "\0" x (128 -length($bin_name));
505 print FILE "$verno";
506 print FILE "\0" x (64 -length($verno));
507
508if ($FLASH_CFG =~ /custom_MemoryDevice\.h/i)
509{
510 print FILE pack("H4", "ffff");
511} else {
512 print FILE pack("H4", "0000");
513}
514
515 $flash_len = length($full_hex_str);
516 print FILE pack("v1", $flash_device_count);
517
518for ($i=0; $i<$flash_device_count; $i++)
519{
520 $menuID = hex(substr($full_hex_str, 2+32*$i, 4));
521 $devID = hex(substr($full_hex_str, 6+32*$i, 4));
522 $extID1 = hex(substr($full_hex_str, 10+32*$i, 4));
523 $extID2 = hex(substr($full_hex_str, 14+32*$i, 4));
524 $fatAddr = hex(substr($full_hex_str, 18+32*$i, 8));
525 $fatLen = hex(substr($full_hex_str, 26+32*$i, 8));
526 print FILE pack("v1", $menuID);
527 print FILE pack("v1", $devID);
528 print FILE pack("v1", $extID1);
529 print FILE pack("v1", $extID2);
530 print FILE pack("V1", $fatAddr);
531 print FILE pack("V1", $fatLen);
532}
533
534if ($cust_para_support eq "TRUE")
535{
536 seek(FILE, 12+(6-$flash_device_count)*16, 1);
537 print FILE pack("V1", $SYM_TBL{'g_CustParaCfg'});
538 print FILE pack("V1", $SYM_LEN{'g_CustParaCfg'});
539}
540
541if ($sec_support == 1)
542{
543 #Parse scatter file to get SECINFO_TAIL address.
544 open(scat,"$scat") || die "Cannot open $sact. Error:$!\n";
545 my $line = 0;
546 my $rom_base = "";
547 my $rom_line = 0;
548 my $secondary_rom_base = "0";
549 my $secondary_rom_line = 0;
550 my $third_rom_base = "0";
551 my $third_rom_line = 0;
552 my $secinfo_tail_line = 0;
553 my $secinfo_tail_base = "0";
554 my $multiROM = "";
555 while(<scat>){
556 $line++;
557
558 if(index($_,"ROM") == 0){
559 $rom_line = $line;
560 if($_ =~ /[\w]+[\s](.+?)[\s]/ && $1 ne "+0x0"){
561 $rom_base = $1;
562 # Get ROM binary length.
563 if ($file =~ /(.+)\\ROM/) {
564 $bin_path = $1;
565 $rom_length = get_length($file);
566 }
567 }
568 } elsif(index($_,"SECONDARY_ROM") == 0){
569 $secondary_rom_line = $line;
570 if($_ =~ /[\w]+[\s](.+?)[\s]/){
571 $secondary_rom_base = $1;
572 if ($secondary_rom_base =~ /\+(.+)/) {
573 $secondary_rom_offset = hex $1;
574 $secondary_rom_base = $rom_length + $secondary_rom_offset;
575 $secondary_rom_length = get_length("$bin_path\\SECONDARY_ROM");
576 }
577 }
578 } elsif(index($_,"THIRD_ROM") == 0){
579 $third_rom_line = $line;
580 if($_ =~ /[\w]+[\s](.+?)[\s]/){
581 $third_rom_base = $1;
582 if ($third_rom_base =~ /\+(.+)/) {
583 $third_rom_offset = $1;
584 $third_rom_base = $secondary_rom_length + $third_rom_offset;
585 }
586 }
587 } elsif ($_ =~ /^[a-zA-Z].+/) {
588 $third_rom_line = $line;
589 if($_ =~ /[\w]+[\s](.+?)[\s]/){
590 $third_rom_base = $1;
591 if ($third_rom_base =~ /\+(.+)/) {
592 $third_rom_offset = $1;
593 $third_rom_base = $secondary_rom_length + $third_rom_offset;
594 }
595 }
596 } elsif(index($_,"SECINFO_TAIL") >= 0){
597 $secinfo_tail_line = $line;
598 }
599 }
600 close(scat);
601
602 if($secondary_rom_line == 0) {
603 $secinfo_tail_base = $rom_base;
604 } elsif($secondary_rom_line > $secinfo_tail_line){ #SECINFO_TAIL locates at ROM
605 $secinfo_tail_base = $rom_base;
606 } elsif(($secinfo_tail_line > $secondary_rom_line) && ($third_rom_line > $secinfo_tail_line)){#SECINFO_TAIL locates at SECONDARY_ROM
607 $secinfo_tail_base = $secondary_rom_base;
608 $multiROM = "SECONDARY";
609 } elsif(($secinfo_tail_line > $third_rom_line) && (third_rom_line != "0")) {#SECINFO_TAIL locates at THIRD_ROM
610 $secinfo_tail_base = $third_rom_base;
611 $multiROM = "THIRD";
612 }
613
614#************************************************************
615#Start to write secinfo to ROM
616#************************************************************
617 seek(FILE, $ROM_POS{'g_secinfo'} + 20, 0);
618 print FILE pack("V1", $secure_jtag_enable);
619 print FILE "$secure_custom_name";
620 print FILE "\0" x (32 -length($secure_custom_name));
621 seek(FILE, $ROM_POS{'g_secinfo'} + 60, 0);
622 print FILE pack("V1", $secure_version);
623
624 if ($secinfo_tail_base =~ /0x/i) {
625 $secinfo_tail_base = hex($secinfo_tail_base);
626 }
627
628 # Verify the address of g_secinfo_tail
629 seek(FILE, $ROM_POS{'g_secinfo'} + 80, 0);
630 my $addr_secinfo_tail;
631 die "cannot read $file\n" unless (my $chnum = (read FILE, $addr_secinfo_tail, 4));
632 $addr_secinfo_tail = unpack ("V1",$addr_secinfo_tail);
633 die "secinfo tail don't match\n" if ($SYM_TBL{'g_secinfo_tail'} != $addr_secinfo_tail);
634
635 if(length($multiROM) > 0){
636 if($multiROM eq "SECONDARY"){
637 $file =~ s/ROM/SECONDARY_ROM/;
638 open(multiFILE,"+<$file") || die "Cannot open $file. Error:$!\b";
639 } elsif($multiROM eq "THIRD"){
640 $file =~ s/ROM/THIRD_ROM/;
641 open(multiFILE,"$file") || die "Cannot open $file. Error:$!\b";
642 }
643# $addr_secinfo_tail = $ROM_POS{'g_secinfo_tail'} % 0x08000000;
644 $addr_secinfo_tail = $ROM_POS{'g_secinfo_tail'};
645 $addr_secinfo_tail = $addr_secinfo_tail - $secinfo_tail_base;
646 seek(multiFILE, $addr_secinfo_tail+20, 0);
647 print multiFILE pack("V1", $secure_jtag_enable);
648 print multiFILE "$secure_custom_name";
649 print multiFILE "\0" x (32 -length($secure_custom_name));
650 seek(multiFILE, $addr_secinfo_tail+60, 0);
651 print multiFILE pack("V1", $secure_version);
652 close(multiFILE);
653 } else {
654 $addr_secinfo_tail = unpack ("V1",$addr_secinfo_tail) % 0x08000000;
655
656 seek(FILE, $ROM_POS{'g_secinfo_tail'} + 20, 0);
657 print FILE pack("V1", $secure_jtag_enable);
658 print FILE "$secure_custom_name";
659 print FILE "\0" x (32 -length($secure_custom_name));
660 seek(FILE, $ROM_POS{'g_secinfo_tail'} + 60, 0);
661 print FILE pack("V1", $secure_version);
662 }
663}
664
665if ($rom_info_ver eq "MTK_ROMINFO_v05")
666{
667 $cust_para_addr = hex(0);
668 $cust_para_len = hex(0);
669 $bit_ctrl = hex(0);
670
671 #Read the bit_ctrl first then OR needed bit
672 seek(FILE, $ROM_POS{'IMG_INFO'} + 328, 0);
673 (read FILE, $bit_ctrl, 4);
674 $bit_ctrl=unpack("V1",$bit_ctrl);
675
676 if (($system_drive_on_nand eq "TRUE") && ($nand_flash_booting eq "NONE"))
677 {
678 $bit_ctrl = $bit_ctrl | 0x00000002;
679 }
680
681
682
683 seek(FILE, $ROM_POS{'IMG_INFO'} + 320, 0);
684 print FILE pack("V1", $cust_para_addr);
685
686 seek(FILE, $ROM_POS{'IMG_INFO'} + 324, 0);
687 print FILE pack("V1", $cust_para_len);
688
689 seek(FILE, $ROM_POS{'IMG_INFO'} + 328, 0);
690 print FILE pack("V1", $bit_ctrl);
691}
692
693close FILE;
694
695#************************************************************
696#Start to write IMG_FOTA_INFO to BIN file for FOTA
697#************************************************************
698
699
700#*********************************************************************************
701# Calculate checksum of IMG_FOTA_INFO string (Check sum of "MTK_FOTA_ROMINFO_V01")
702#*********************************************************************************
703
704
705#****************************************************************************
706# Write Platform ID and Project ID
707#****************************************************************************
708
709
710#****************************************************************************
711# Get array numbers
712#****************************************************************************
713
714
715#****************************************************************************
716# Write file size of each ROM image
717#****************************************************************************
718
719
720#****************************************************************************
721# Write XOR value of whole structure
722# For image information: initial key is (0x86)
723#****************************************************************************
724
725
726exit 0;
727
728#******************************************************************************
729# FUNCTION
730# get_size
731# DESCRIPTION
732# xxx
733# PARAMETERS
734# binary path
735# RETURNS
736# binary size
737#******************************************************************************
738
739sub get_size(){
740 ($path) = @_;
741 $rom_size = stat("$path")->size;
742 return $rom_size;
743}
744
745#******************************************************************************
746# FUNCTION
747# CheckGFHMaxSize
748# DESCRIPTION
749# Check whether image has GFH head info
750# PARAMETERS
751# binary path
752# RETURNS
753# Image has GFH head or not
754#******************************************************************************
755sub CheckGFHMaxSize
756{
757 my ($strFilePath) = @_;
758 my $bIsGFH = 0;
759
760 if(-e $strFilePath)
761 {
762 # Get 17bytes (0-2: 4D 4D 4D, 8-16: FILE_INFO
763 open (FILE_HANDLE, "<$strFilePath") or &error_handler("$strFilePath: open file error!");
764 #binmode(FILE_HANDLE);
765 my ($nIndex, $data) = (0, undef);
766 my @Buffer;
767
768 while(read(FILE_HANDLE, $data, 1))
769 {
770 $Buffer[$nIndex++] = $data;
771 last if($nIndex > 64);
772 }
773 close FILE_HANDLE;
774 my ($strFILE_INFO, $nMatchMMM) = (undef, 0);
775 for(0..2)
776 {
777 $nMatchMMM++ if(ord($Buffer[$_]) == 0x4D);
778 }
779 for(8..16)
780 {
781 $strFILE_INFO .= $Buffer[$_];
782 }
783 $bIsGFH =1 if($nMatchMMM == 3 and $strFILE_INFO eq "FILE_INFO");
784 if($bIsGFH == 1)
785 {
786 # GFH_FILE_INFO_v1
787 # [ 3: 0] GFH_Header.m_magic_ver
788 # [ 5: 3] GFH_Header.m_size, = size of GFH_FILE_INFO_v1 structure
789 # [ 7: 6] GFH_Header.m_type, = GFH_FILE_INFO_v1
790 # [ 19: 8] identifier, = GFH_FILE_INFO_ID = "FILE_INFO"
791 # [ 23: 20] m_file_ver
792 # [ 25: 24] m_file_type, = PRI_ROM, DSP_ROM, ...
793 # [ : 26] m_flash_dev
794 # [ : 27] m_sig_type
795 # [ 31: 28] m_load_addr
796 # [ 35: 32] m_file_len
797 # [ 39: 36] m_max_size
798 # [ 43: 40] m_content_offset
799 # [ 47: 44] m_sig_len
800 # [ 51: 48] m_jump_offset
801 # [ 55: 52] m_attr
802 $ImageSize = ord($Buffer[36]) + (ord($Buffer[37])<<8) + (ord($Buffer[38])<<16) + (ord($Buffer[39])<<24);
803 print "ImageSize is $ImageSize\n";
804 }
805 }
806 return $bIsGFH;
807}