rjw | 6c1fd8f | 2022-11-30 14:33:01 +0800 | [diff] [blame] | 1 | #!/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 | package dcmcomp_process;
|
| 38 | #****************************************************************************
|
| 39 | # Included Modules
|
| 40 | #****************************************************************************
|
| 41 | BEGIN { push @INC, '.\\pcore\\tools\\' } # add additional library path
|
| 42 | use vivaHelper;
|
| 43 | use File::stat;
|
| 44 | use Math::BigInt;
|
| 45 |
|
| 46 | #****************************************************************************
|
| 47 | # Constants
|
| 48 | #****************************************************************************
|
| 49 | my $dcmcmpBinaryName = "DYNAMIC_COMP_CODE";
|
| 50 | my $lzma_exe = "pcore\\tools\\7lzma.exe";
|
| 51 | my %info = ("name" => "DCM with Compression",
|
| 52 | "binary_name" => $dcmcmpBinaryName,
|
| 53 | "struct_name" => "dcmcmp",
|
| 54 | "entry_function" => \&Process);
|
| 55 |
|
| 56 |
|
| 57 | #****************************************************************************
|
| 58 | # subroutine: Process
|
| 59 | # description: Perform the ZIMAGE post build process.
|
| 60 | # input: Binary folder, available start address,
|
| 61 | # original start address ref, original size ref
|
| 62 | # output: Actual start address, new binary size
|
| 63 | #****************************************************************************
|
| 64 | sub Process
|
| 65 | {
|
| 66 | my $binaryFolder = shift;
|
| 67 | my $availableStartAddress = shift;
|
| 68 | my $startAddressRef = shift;
|
| 69 | my $sizeRef = shift;
|
| 70 |
|
| 71 | my $dcmcmpEnabled = &vivaHelper::GetMakefileOption("DCM_COMPRESSION_SUPPORT");
|
| 72 | if (defined $dcmcmpEnabled and $dcmcmpEnabled eq "TRUE")
|
| 73 | {
|
| 74 | my $binary = "$binaryFolder\\$dcmcmpBinaryName";
|
| 75 | processBody($binaryFolder);
|
| 76 |
|
| 77 | # Update start address and binary size
|
| 78 | # Need 4 byte align
|
| 79 | $$startAddressRef = &vivaHelper::RoundUpToAlignment($availableStartAddress, 4);
|
| 80 | $$sizeRef = -s $binary;
|
| 81 |
|
| 82 | return 1;
|
| 83 | }
|
| 84 | else
|
| 85 | {
|
| 86 | return 0;
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | #****************************************************************************
|
| 91 | # subroutine: GetInfo
|
| 92 | # description: Get the basic information of processing
|
| 93 | # input: None
|
| 94 | # output: Info ref
|
| 95 | #****************************************************************************
|
| 96 | sub GetInfo
|
| 97 | {
|
| 98 | return \%info;
|
| 99 | }
|
| 100 |
|
| 101 | #****************************************************************************
|
| 102 | # subroutine: processBody
|
| 103 | # description: Perform the ZIMAGE post build process.
|
| 104 | # input: Binary folder
|
| 105 | # output: None
|
| 106 | #****************************************************************************
|
| 107 | sub processBody
|
| 108 | {
|
| 109 | my ($processed_folder) = @_;
|
| 110 | my @Binary_Files = ();
|
| 111 | my @Compress_List = ();
|
| 112 |
|
| 113 | my $lzma_exe = "pcore\\tools\\7lzma.exe";
|
| 114 |
|
| 115 | die "compression tool $lzma_exe do NOT exist!\n" if (!-e $lzma_exe);
|
| 116 |
|
| 117 | if(-d "$processed_folder"){
|
| 118 | opendir DIRHANDLE, "$processed_folder";
|
| 119 | @Binary_Files = grep { !/^\.{1,2}$/ and -f "$processed_folder\\$_"} readdir (DIRHANDLE);
|
| 120 | close DIRHANDLE;
|
| 121 | } else {
|
| 122 | die "$processed_folder bin file folder do NOT exist!!!\n";
|
| 123 | }
|
| 124 |
|
| 125 | my $base_identifier = "DCM_COMPRESS_CANDIDATE_HDR_V01";
|
| 126 | $base_identifier = unpack('V1', "$base_identifier");
|
| 127 |
|
| 128 | my $output_identifier = "DCMGBODY";
|
| 129 |
|
| 130 | foreach (@Binary_Files){
|
| 131 | my $B_file = $_;
|
| 132 | open (FILE, "<$processed_folder\\$B_file") or die "cannot open $processed_folder\\$B_file\n";
|
| 133 | binmode(FILE);
|
| 134 | (read FILE, $identifier, 32);
|
| 135 | my $tmp_id = $identifier;
|
| 136 | $identifier = unpack('V1', $identifier);
|
| 137 | (read FILE, $checksum, 4);
|
| 138 | $checksum = unpack('V1', $checksum);
|
| 139 | push(@Compress_List,$B_file) && next if (($identifier eq $base_identifier) && (&checksum($tmp_id,$checksum)));
|
| 140 |
|
| 141 | $identifier = "";
|
| 142 | seek(FILE, 0, 0);
|
| 143 | (read FILE, $identifier, 8);
|
| 144 | unpack('C', $identifier);
|
| 145 |
|
| 146 | $output_file = "$B_file" if ($identifier eq $output_identifier);
|
| 147 | close FILE;
|
| 148 | }
|
| 149 |
|
| 150 | my $comp_files_cnt = $#Compress_List + 1;
|
| 151 | print "Total $comp_files_cnt files need to be compressed to $output_file\n";
|
| 152 |
|
| 153 | if($output_file eq "") {
|
| 154 | print "Error: DCM output file is not existed!\n";
|
| 155 | exit 1 ;
|
| 156 | }
|
| 157 |
|
| 158 | system("rd $processed_folder\\DYNAMIC_COMP_BIN") if (-d "$processed_folder\\DYNAMIC_COMP_BIN");
|
| 159 | system("md $processed_folder\\DYNAMIC_COMP_BIN");
|
| 160 |
|
| 161 | system("copy $processed_folder\\$output_file $processed_folder\\DYNAMIC_COMP_BIN\\$output_file.bin");
|
| 162 |
|
| 163 | open (OUTFILE, "+<$processed_folder\\$output_file") or die "cannot open $processed_folder\\$output_file\n";
|
| 164 | binmode(OUTFILE);
|
| 165 | seek(OUTFILE, 104, 0);
|
| 166 | print OUTFILE pack("V1",$comp_files_cnt);
|
| 167 |
|
| 168 | my $processed_cnt = 0;
|
| 169 | foreach (@Compress_List) {
|
| 170 |
|
| 171 | my $current_file = $_;
|
| 172 |
|
| 173 | system("$lzma_exe e $processed_folder\\$current_file $processed_folder\\$current_file.gz");
|
| 174 | system("rename $processed_folder\\$current_file $current_file.bin");
|
| 175 | system("rename $processed_folder\\$current_file.gz $current_file");
|
| 176 |
|
| 177 | open (FILE, "<$processed_folder\\$current_file.bin") or die "cannot open $processed_folder\\$current_file.bin\n";
|
| 178 | binmode(FILE);
|
| 179 | seek(FILE, 36, 0);
|
| 180 | (read FILE, $img_id, 4);
|
| 181 | $img_id = unpack('V1', $img_id);
|
| 182 | (read FILE, $img_exec_base, 4);
|
| 183 | $img_exec_base = unpack('V1', $img_exec_base);
|
| 184 | (read FILE, $img_zi_base, 4);
|
| 185 | $img_zi_base = unpack('V1', $img_zi_base);
|
| 186 | (read FILE, $img_zi_size, 4);
|
| 187 | $img_zi_size = unpack('V1', $img_zi_size);
|
| 188 |
|
| 189 | $comp_size = stat("$processed_folder\\$current_file")->size;
|
| 190 | $original_size = stat("$processed_folder\\$current_file.bin")->size;
|
| 191 |
|
| 192 | my $header_pos = 108 + 28*$processed_cnt;
|
| 193 |
|
| 194 | seek(OUTFILE, 0, 2);
|
| 195 | my $last_position = tell(OUTFILE);
|
| 196 |
|
| 197 | seek(OUTFILE, $header_pos, 0);
|
| 198 | print OUTFILE pack("V1",$img_id);
|
| 199 | print OUTFILE pack("V1",$img_exec_base);
|
| 200 | print OUTFILE pack("V1",$img_zi_base);
|
| 201 | print OUTFILE pack("V1",$img_zi_size);
|
| 202 | print OUTFILE pack("V1",$original_size);
|
| 203 | print OUTFILE pack("V1",$comp_size);
|
| 204 | print OUTFILE pack("V1",$last_position);
|
| 205 | seek(OUTFILE, $last_position, 0);
|
| 206 | open (GZFILE, "<$processed_folder\\$current_file") or die "cannot open $processed_folder\\$current_file\n";
|
| 207 | binmode GZFILE;
|
| 208 | while (read(GZFILE, $buf, 4)) {
|
| 209 | print OUTFILE $buf;
|
| 210 | }
|
| 211 | close GZFILE;
|
| 212 | close FILE;
|
| 213 |
|
| 214 | system("move /y $processed_folder\\$current_file $processed_folder\\DYNAMIC_COMP_BIN\\$current_file");
|
| 215 | system("move /y $processed_folder\\$current_file.bin $processed_folder\\DYNAMIC_COMP_BIN\\$current_file.bin");
|
| 216 |
|
| 217 | $processed_cnt ++;
|
| 218 |
|
| 219 | }
|
| 220 |
|
| 221 | close OUTFILE;
|
| 222 | }
|
| 223 |
|
| 224 | sub checksum {
|
| 225 | my ($identifier,$id_checksum) = @_;
|
| 226 | my $count = 0;
|
| 227 | my $checksum1="";
|
| 228 | my $checksum2="";
|
| 229 | my $checksum="";
|
| 230 | my @buffer = split(//,$identifier);
|
| 231 | my @buffer2 = reverse @buffer;
|
| 232 | foreach (@buffer2) {
|
| 233 | if ($count % 4 < 3) {
|
| 234 | $checksum1 = $checksum1.unpack("H*",$_);
|
| 235 | }
|
| 236 | if ($count % 4 == 3) {
|
| 237 | $checksum2 = $checksum1.unpack("H*",$_);
|
| 238 | $checksum = eval($checksum+hex($checksum2));
|
| 239 | $checksum1=undef;
|
| 240 | $checksum2=undef;
|
| 241 | }
|
| 242 | $count++;
|
| 243 | }
|
| 244 | $checksum=$checksum%(Math::BigInt->new('0x100000000'));
|
| 245 | my $result = ($checksum eq $id_checksum) ? 1 : 0;
|
| 246 | return $result;
|
| 247 | }
|
| 248 |
|
| 249 | sub Usage {
|
| 250 | print "perl dcmcomp_process.pl <bin file folder>\n";
|
| 251 | exit 1;
|
| 252 | } |