blob: 4fa4593cb8395993b152fa6992ba6e1138b74ca9 [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) 2005
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
38use File::stat;
39use Math::BigInt;
40
41if ($#ARGV != 0) { &Usage; }
42
43my ($processed_folder) = @ARGV;
44my @Binary_Files = ();
45my @Compress_List = ();
46
47my $lzma_exe = "pcore\\tools\\7lzma.exe";
48
49die "compression tool $lzma_exe do NOT exist!\n" if (!-e $lzma_exe);
50
51if(-d "$processed_folder"){
52 opendir DIRHANDLE, "$processed_folder";
53 @Binary_Files = grep { !/^\.{1,2}$/ } readdir (DIRHANDLE);
54 close DIRHANDLE;
55} else {
56 die "$processed_folder bin file folder do NOT exist!!!\n";
57}
58
59my $base_identifier = "DCM_COMPRESS_CANDIDATE_HDR_V01";
60$base_identifier = unpack('V1', "$base_identifier");
61
62my $output_identifier = "DCMGBODY";
63
64foreach (@Binary_Files){
65 my $B_file = $_;
66 open (FILE, "<$processed_folder\\$B_file") or die "cannot open $processed_folder\\$B_file\n";
67 binmode(FILE);
68 (read FILE, $identifier, 32);
69 my $tmp_id = $identifier;
70 $identifier = unpack('V1', $identifier);
71 (read FILE, $checksum, 4);
72 $checksum = unpack('V1', $checksum);
73 push(@Compress_List,$B_file) && next if (($identifier eq $base_identifier) && (&checksum($tmp_id,$checksum)));
74
75 $identifier = "";
76 seek(FILE, 0, 0);
77 (read FILE, $identifier, 8);
78 unpack('C', $identifier);
79
80 $output_file = "$B_file" if ($identifier eq $output_identifier);
81 close FILE;
82}
83
84my $comp_files_cnt = $#Compress_List + 1;
85print "Total $comp_files_cnt files need to be compressed to $output_file\n";
86
87if($output_file eq "") {
88 print "Error: DCM output file is not existed!\n";
89 exit 1 ;
90}
91
92system("rd $processed_folder\\DYNAMIC_COMP_BIN") if (-d "$processed_folder\\DYNAMIC_COMP_BIN");
93system("md $processed_folder\\DYNAMIC_COMP_BIN");
94
95open (OUTFILE, "+<$processed_folder\\$output_file") or die "cannot open $processed_folder\\$output_file\n";
96binmode(OUTFILE);
97seek(OUTFILE, 104, 0);
98print OUTFILE pack("V1",$comp_files_cnt);
99
100my $processed_cnt = 0;
101foreach (@Compress_List) {
102
103 my $current_file = $_;
104
105 system("$lzma_exe e $processed_folder\\$current_file $processed_folder\\$current_file.gz");
106 system("rename $processed_folder\\$current_file $current_file.bin");
107 system("rename $processed_folder\\$current_file.gz $current_file");
108
109 open (FILE, "<$processed_folder\\$current_file.bin") or die "cannot open $processed_folder\\$current_file.bin\n";
110 binmode(FILE);
111 seek(FILE, 36, 0);
112 (read FILE, $img_id, 4);
113 $img_id = unpack('V1', $img_id);
114 (read FILE, $img_exec_base, 4);
115 $img_exec_base = unpack('V1', $img_exec_base);
116 (read FILE, $img_zi_base, 4);
117 $img_zi_base = unpack('V1', $img_zi_base);
118 (read FILE, $img_zi_size, 4);
119 $img_zi_size = unpack('V1', $img_zi_size);
120
121 $comp_size = stat("$processed_folder\\$current_file")->size;
122 $original_size = stat("$processed_folder\\$current_file.bin")->size;
123
124 my $header_pos = 108 + 28*$processed_cnt;
125
126 seek(OUTFILE, 0, 2);
127 my $last_position = tell(OUTFILE);
128
129 seek(OUTFILE, $header_pos, 0);
130 print OUTFILE pack("V1",$img_id);
131 print OUTFILE pack("V1",$img_exec_base);
132 print OUTFILE pack("V1",$img_zi_base);
133 print OUTFILE pack("V1",$img_zi_size);
134 print OUTFILE pack("V1",$original_size);
135 print OUTFILE pack("V1",$comp_size);
136 print OUTFILE pack("V1",$last_position);
137 seek(OUTFILE, $last_position, 0);
138 open (GZFILE, "<$processed_folder\\$current_file") or die "cannot open $processed_folder\\$current_file\n";
139 binmode GZFILE;
140 while (read(GZFILE, $buf, 4)) {
141 print OUTFILE $buf;
142 }
143 close GZFILE;
144 close FILE;
145
146 system("move /y $processed_folder\\$current_file $processed_folder\\DYNAMIC_COMP_BIN\\$current_file");
147 system("move /y $processed_folder\\$current_file.bin $processed_folder\\DYNAMIC_COMP_BIN\\$current_file.bin");
148
149 $processed_cnt ++;
150
151}
152
153close OUTFILE;
154
155exit 0;
156
157sub checksum {
158 my ($identifier,$id_checksum) = @_;
159 my $count = 0;
160 my $checksum1="";
161 my $checksum2="";
162 my $checksum="";
163 my @buffer = split(//,$identifier);
164 my @buffer2 = reverse @buffer;
165 foreach (@buffer2) {
166 if ($count % 4 < 3) {
167 $checksum1 = $checksum1.unpack("H*",$_);
168 }
169 if ($count % 4 == 3) {
170 $checksum2 = $checksum1.unpack("H*",$_);
171 $checksum = eval($checksum+hex($checksum2));
172 $checksum1=undef;
173 $checksum2=undef;
174 }
175 $count++;
176 }
177 $checksum=$checksum%(Math::BigInt->new('0x100000000'));
178 my $result = ($checksum eq $id_checksum) ? 1 : 0;
179 return $result;
180}
181
182sub Usage {
183 print "perl dcmcomp_process.pl <bin file folder>\n";
184 exit 1;
185}