blob: 85e3b68ca810d9dad2ba3947cf9b031783475df9 [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) 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#*
38#* Filename:
39#* ---------
40#* zImageProcess.pl
41#*
42#* Project:
43#* --------
44#* Maui_Software
45#*
46#* Description:
47#* ------------
48#* This file implemented the ZIMAGE post build process.
49#*
50#*
51#* Author:
52#* -------
53#* Ke-Ting Chen (mtk03141)
54#*
55#*****************************************************************************
56package zImageProcess;
57#****************************************************************************
58# Included Modules
59#****************************************************************************
60use strict;
61use warnings;
62BEGIN { push @INC, '.\\pcore\\tools\\' } # add additional library path
63use vivaHelper;
64use File::Copy;
65
66#****************************************************************************
67# Constants
68#****************************************************************************
69my $DEBUG = 1;
70my $compressTool = "pcore\\tools\\7lzma.exe";
71my $zImageBinaryName = "ZIMAGE_ER";
72my $zImageBinaryBackupName = "ZIMAGE_ER.bin";
73my %zImageInfo = ("name" => "ZIMAGE",
74 "binary_name" => $zImageBinaryName,
75 "struct_name" => "zimage",
76 "entry_function" => \&ZImageProcess);
77my %defaultPartitionInfo = ("MT6250" => [{"ratio" => 0.75, "type" => 2},
78 {"ratio" => 0.25, "type" => 1}]);
79my $minSplitableSize = 8 * 1024;
80
81#****************************************************************************
82# subroutine: Process
83# description: Perform the ZIMAGE post build process.
84# input: Binary folder, available start address,
85# original start address ref, original size ref
86# output: Actual start address, new binary size
87#****************************************************************************
88sub ZImageProcess
89{
90 my $binaryFolder = shift;
91 my $availableStartAddress = shift;
92 my $startAddressRef = shift;
93 my $sizeRef = shift;
94
95 my $binary = "$binaryFolder\\$zImageBinaryName";
96 my $binaryBackup = "$binaryFolder\\$zImageBinaryBackupName";
97
98 my $zimageEnabled = &vivaHelper::GetMakefileOption("ZIMAGE_SUPPORT");
99 unless (defined $zimageEnabled and
100 $zimageEnabled eq "TRUE" and
101 -e $binary)
102 {
103 return 0;
104 }
105
106 # Using default partition setting
107 CompressByPartition($binary, $binaryBackup, $$startAddressRef);
108
109 # Update start address and binary size
110 # Need 4 byte align
111 $$startAddressRef = &vivaHelper::RoundUpToAlignment($availableStartAddress, 4);
112 $$sizeRef = -s $binary;
113
114 #debugLog("Base Address: $$startAddressRef");
115 #debugLog("Length: $$sizeRef");
116
117 return 1;
118}
119
120#****************************************************************************
121# subroutine: GetInfo
122# description: Get the basic information of processing
123# input: None
124# output: Info ref
125#****************************************************************************
126sub GetZImageInfo
127{
128 return \%zImageInfo;
129}
130
131#****************************************************************************
132# Constants
133#****************************************************************************
134my $bootZImageBinaryName = "BOOT_ZIMAGE_ER";
135my $bootZImageBinaryBackupName = "BOOT_ZIMAGE_ER.bin";
136my %bootZImageInfo = ("name" => "BOOT_ZIMAGE",
137 "binary_name" => $bootZImageBinaryName,
138 "struct_name" => "boot_zimage",
139 "entry_function" => \&BootZImageProcess);
140
141#****************************************************************************
142# subroutine: Process
143# description: Perform the ZIMAGE post build process.
144# input: Binary folder, available start address,
145# original start address ref, original size ref
146# output: Actual start address, new binary size
147#****************************************************************************
148sub BootZImageProcess
149{
150 my $binaryFolder = shift;
151 my $availableStartAddress = shift;
152 my $startAddressRef = shift;
153 my $sizeRef = shift;
154
155 my $binary = "$binaryFolder\\$bootZImageBinaryName";
156 my $binaryBackup = "$binaryFolder\\$bootZImageBinaryBackupName";
157
158 my %zImage2partitionInfo = ("MT6250" => [{"ratio" => 1, "type" => 2}]);
159
160 my $zimage2Enabled = &vivaHelper::GetMakefileOption("BOOT_ZIMAGE_SUPPORT");
161 unless (defined $zimage2Enabled and
162 $zimage2Enabled eq "TRUE" and
163 -e $binary)
164 {
165 return 0;
166 }
167
168 CompressByPartition($binary, $binaryBackup, $$startAddressRef, $zImage2partitionInfo{&vivaHelper::GetMakefileOption("PLATFORM")});
169
170 # Update start address and binary size
171 # Need 4 byte align
172 $$startAddressRef = &vivaHelper::RoundUpToAlignment($availableStartAddress, 4);
173 $$sizeRef = -s $binary;
174
175 #debugLog("Base Address: $$startAddressRef");
176 #debugLog("Length: $$sizeRef");
177
178 return 1;
179}
180
181#****************************************************************************
182# subroutine: GetInfo
183# description: Get the basic information of processing
184# input: None
185# output: Info ref
186#****************************************************************************
187sub GetBootZImageInfo
188{
189 return \%bootZImageInfo;
190}
191
192
193
194sub CompressByPartition
195{
196 my $binary = shift;
197 my $binaryBackup = shift;
198 my $decompressStartAddress = shift;
199 my $partitions = shift;
200
201 # The parition decompress start address should be cache line aligned
202 my $alignment = 32;
203 my $alignShift = $alignment - $decompressStartAddress % $alignment;
204
205 # Use default setting if the argument is omitted
206 $partitions = $defaultPartitionInfo{&vivaHelper::GetMakefileOption("PLATFORM")} unless defined $partitions;
207
208 # Backup the original binary
209 printLog("Backup binary...");
210 copy($binary, $binaryBackup) or
211 &vivaHelper::ErrorHandler("Cannot backup binary $binary: $!", __FILE__, __LINE__);
212
213 # Compress the binary
214 {
215 my $binarySize = -s $binary or
216 &vivaHelper::ErrorHandler("Cannot get file size $binary: $!", __FILE__, __LINE__);
217
218 my $splitPartitionNumber;
219 my @partitionSize;
220 my @partitionName;
221 my @partitionCompressedSize;
222
223 # Use default partition settings if not defined or the size is too small
224 if (not defined $partitions)
225 {
226 $partitions = [{"ratio" => [1], "type" => 1}];
227 }
228
229 # Split the binary
230 printLog("Splitting binaries...");
231 {
232 debugLog("Original Size: $binarySize");
233 my $temp;
234 my $subTotalSize = 0;
235 my $i;
236
237 for ($i = 0; $i < @$partitions; ++$i)
238 {
239 $temp = &vivaHelper::RoundUpToAlignment(int($partitions->[$i]{"ratio"} * $binarySize + 0.99), $alignment) + $alignShift;
240 $subTotalSize += $temp;
241
242 push @partitionSize, $temp;
243 push @partitionName, "~zimagePartition_$i.tmp";
244 }
245
246 # Add or subtract the rest size to the final partition
247 $partitionSize[$i - 1] += $binarySize - $subTotalSize;
248
249 for ($i = 0; $i < @partitionSize; ++$i)
250 {
251 debugLog("$partitionName[$i]\t$partitionSize[$i]");
252 }
253
254 # Split
255 $splitPartitionNumber = &vivaHelper::SplitBinaryBySize($binary, \@partitionSize, \@partitionName);
256 }
257
258 # Compress
259 printLog("Compressing binaries...");
260 {
261 for (my $i = 0; $i < $splitPartitionNumber; ++$i)
262 {
263 if (0 != system("$compressTool e $partitionName[$i] $partitionName[$i]_comp"))
264 {
265 &vivaHelper::ErrorHandler("Cannot compress ZIMAGE binary $partitionName[$i]: $!", __FILE__, __LINE__);
266 }
267 else
268 {
269 my $temp = -s $partitionName[$i]."_comp";
270 push @partitionCompressedSize, $temp or
271 &vivaHelper::ErrorHandler("Cannot get file size $partitionName[$i]_comp: $!", __FILE__, __LINE__);
272
273 # Pad the binary after getting the size
274 &vivaHelper::PaddingBinaryToAlignment($partitionName[$i]."_comp", 4);
275 debugLog("$partitionName[$i]_comp\t$temp");
276 }
277 }
278 }
279
280 # Making the header
281 printLog("Making header...");
282 {
283 # Write partition number
284 my $buffer;
285 my $sourceAddress;
286 my $destinationAddress;
287
288 open my $output, ">~zimageHeader.tmp" or
289 &vivaHelper::ErrorHandler("Cannot open output file ~zimageHeader.tmp: $!", __FILE__, __LINE__);
290 binmode $output;
291
292 # Write partition number
293 debugLog("Partition number: $splitPartitionNumber");
294 $buffer = pack("L", $splitPartitionNumber);
295 print $output $buffer;
296
297 # Write partition info
298 $sourceAddress = 4 + 20 * ($splitPartitionNumber);
299 $destinationAddress = 0;
300
301 for (my $i = 0; $i < $splitPartitionNumber; ++$i)
302 {
303 my $type = $partitions->[$i]{"type"};
304 my $sourceSize = $partitionCompressedSize[$i];
305 my $destinationSize = $partitionSize[$i];
306
307 debugLog("Partition[$i]: $type, $sourceAddress, $sourceSize, $destinationAddress, $destinationSize");
308 $buffer = pack("LLLLL", $type, $sourceAddress, $sourceSize, $destinationAddress, $destinationSize);
309 print $output $buffer;
310
311 $sourceAddress += &vivaHelper::RoundUpToAlignment($sourceSize, 4);
312 $destinationAddress += $destinationSize;
313 }
314
315 close $output;
316 }
317
318 # Concatenate all the compressed binaries and the header
319 printLog("Concatenating binaries...");
320 {
321 my @concatenateList = ("~zimageHeader.tmp");
322 for (my $i = 0; $i < $splitPartitionNumber; ++$i)
323 {
324 push @concatenateList, $partitionName[$i]."_comp";
325 }
326
327 open my $output, ">$binary" or
328 &vivaHelper::ErrorHandler("Cannot open output file $binary: $!", __FILE__, __LINE__);
329 binmode $output;
330
331 &vivaHelper::ConcatenateBinary($output, \@concatenateList);
332
333 close $output;
334 }
335
336 # Delete all temp files
337 printLog("Deleting temp files...");
338 {
339 unlink("~zimageHeader.tmp") or &vivaHelper::ErrorHandler("Cannot delete file ~zimageHeader.tmp: $!", __FILE__, __LINE__);
340 for (my $i = 0; $i < $splitPartitionNumber; ++$i)
341 {
342 unlink($partitionName[$i]) or &vivaHelper::ErrorHandler("Cannot delete file $partitionName[$i]: $!", __FILE__, __LINE__);
343 unlink($partitionName[$i]."_comp") or &vivaHelper::ErrorHandler("Cannot delete file $partitionName[$i]_comp: $!", __FILE__, __LINE__);
344 }
345 }
346 }
347}
348
349sub printLog
350{
351 print "[ZIMAGE] $_[0]\n";
352}
353
354sub debugLog
355{
356 if ($DEBUG)
357 {
358 printLog("[DEBUG] $_[0]");
359 }
360}
361
3621;