blob: 783b8b0fe8b9289395100fb58b88a2ee5d827809 [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) 2013
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#* xboot_pack.pl
41#*
42#* Project:
43#* --------
44#* MOLY
45#*
46#* Description:
47#* ------------
48#* Pack xboot image
49#*
50#* Author:
51#* -------
52#* Cindy Tu (mtk03468)
53#*
54#****************************************************************************
55use strict;
56
57#****************************************************************************
58# Constants
59#****************************************************************************
60my $XBOOT_PACK_VERNO = " m0.01"; # v0.01 by mtk3468, initial version
61
62#Usage:xboot_pack.pl DA_BINFILE_PATH(filename) BL_BINFILE_PATH(filename) EXT_BINFILE_PATH(filename) MOLY_Image(filename) DST_BINFILE_PATH(filename)
63
64#****************************************************************************
65# Global Variables
66#****************************************************************************
67my $IMG_TBL_MAGIC=0x2454E7D3;
68my $IMG_TBL_ADDR_MAGIC=0x14;
69my $IMG_TBL_BASE_ADDR=0x20;
70my $IMG_TBL_SIZE=0x10;
71my $IMG_TYPE_DA=0x06;
72my $IMG_TYPE_MINI_BL=0x02;
73my $IMG_TYPE_EXT_BL=0x03;
74my $IMG_TYPE_MOLY=0x04;
75my $IMAGE_ALIGN=0x100;
76my $DA_MAX_SIZE=0x10000;
77my $BL_MAX_SIZE=0x40000;
78my $IMAGE_BIN_START=0x100;
79
80my $da_base;
81my $mini_bl_base;
82my $ext_bl_base;
83my $moly_base;
84
85my @img_tbl_buffer;
86
87#****************************************************************************
88# Input Parameters
89#****************************************************************************
90my ($xboot_da_file) = $ARGV[0];
91my ($mini_bl_bin_file) = $ARGV[1];
92my ($ext_bl_bin_file) = $ARGV[2];
93my ($moly_bin_file) = $ARGV[3];
94my ($dst_bin_file) = $ARGV[4];
95my ($net_path) = $ARGV[5];
96my ($partial_source) = $ARGV[6];
97
98print "Input: $xboot_da_file $mini_bl_bin_file $ext_bl_bin_file $moly_bin_file\n";
99
100#****************************************************************************
101# Parameter Check
102#****************************************************************************
103die "xboot DA file $xboot_da_file doesn't exist" if not -e $xboot_da_file;
104die "mini BL file $mini_bl_bin_file doesn't exist" if not -e $mini_bl_bin_file;
105die "ext BL file $ext_bl_bin_file doesn't exist" if not -e $ext_bl_bin_file;
106die "moly binary file $moly_bin_file doesn't exist" if not -e $moly_bin_file;
107
108#****************************************************************************
109# Functions
110#****************************************************************************
111
112#Write img param
113open (FILE_HANDLE, ">$dst_bin_file") or &error_handler("$dst_bin_file: open file error!");
114binmode(FILE_HANDLE);
115&ImgTblGen();
116print FILE_HANDLE @img_tbl_buffer;
117close FILE_HANDLE;
118
119#Write Xboot DA
120&ConcaFile($xboot_da_file, $dst_bin_file, 1);
121&ConcaFile($mini_bl_bin_file, $dst_bin_file, 1);
122&ConcaFile($ext_bl_bin_file, $dst_bin_file, 1);
123&ConcaFile($moly_bin_file, $dst_bin_file, 0);
124
125print "Partial $partial_source";
126if($partial_source eq "TRUE")
127{
128 system("/proj/wcp1sm/Guardian $net_path/$dst_bin_file");
129}
130
131#****************************************************************************
132# subroutine: Construct img param region
133# input: NA
134# output: NA
135#****************************************************************************
136sub ImgTblGen
137{
138 my ($loop, $nIndex, $crcIndex) = (0, 0, 0);
139 my $da_bin_size = (-s $xboot_da_file);
140 my $mini_bl_size = (-s $mini_bl_bin_file);
141 my $ext_bl_size = (-s $ext_bl_bin_file);
142 my $entries = '';
143
144 #check file size
145 if( $da_bin_size > $DA_MAX_SIZE )
146 {
147 die "xboot DA file should not exist $DA_MAX_SIZE";
148 }
149
150 if( $mini_bl_size > $BL_MAX_SIZE )
151 {
152 die "mini BL file should not exist $DA_MAX_SIZE";
153 }
154
155 if( $ext_bl_size > $BL_MAX_SIZE )
156 {
157 die "ext BL file should not exist $DA_MAX_SIZE";
158 }
159
160 #calculate binary offset
161 $da_base = $IMAGE_BIN_START;
162 $mini_bl_base = ($da_base + $da_bin_size + $IMAGE_ALIGN - 1) & ~($IMAGE_ALIGN - 1);
163 $ext_bl_base = ($mini_bl_base + $mini_bl_size + $IMAGE_ALIGN - 1) & ~($IMAGE_ALIGN - 1);
164 $moly_base = ($ext_bl_base + $ext_bl_size + $IMAGE_ALIGN - 1) & ~($IMAGE_ALIGN - 1);
165
166 print "da_base=$da_base, mini_bl_base=$mini_bl_base, ext_bl_base=$ext_bl_base, moly_base=$moly_base\n";
167
168 #padding reserved head
169 for ($loop=0; $loop < $IMG_TBL_ADDR_MAGIC ; $loop=$loop+1, $nIndex=$nIndex+1) {
170 $img_tbl_buffer[$nIndex] = chr(0xFF);
171 }
172 #Write img base at magic addr
173 $img_tbl_buffer[$nIndex++] = &Dec2ASCIIString($IMG_TBL_BASE_ADDR);
174
175 #padding reserved
176 for ($loop = 0; $loop < $IMG_TBL_BASE_ADDR - ($IMG_TBL_ADDR_MAGIC+4) ; $loop=$loop+1, $nIndex=$nIndex+1) {
177 $img_tbl_buffer[$nIndex] = chr(0xFF);
178 }
179
180 #Write img tbl magic
181 $img_tbl_buffer[$nIndex++] = &Dec2ASCIIString($IMG_TBL_MAGIC);
182
183 #Write img tbl reserve
184 $img_tbl_buffer[$nIndex++] = chr(0xFF);
185 $img_tbl_buffer[$nIndex++] = chr(0xFF);
186
187 #Write img tbl size
188 $img_tbl_buffer[$nIndex++] = chr($IMG_TBL_SIZE);
189 $img_tbl_buffer[$nIndex++] = chr(0x0);
190 $crcIndex = $nIndex++;
191
192 #Write img tbl entry
193 $img_tbl_buffer[$nIndex++] = &Dec2ASCIIString( ($IMG_TYPE_DA << 24) | ($da_base >> 8));
194 $entries .= $img_tbl_buffer[$nIndex-1];
195 $img_tbl_buffer[$nIndex++] = &Dec2ASCIIString( ($IMG_TYPE_MINI_BL << 24) | ($mini_bl_base >> 8));
196 $entries .= $img_tbl_buffer[$nIndex-1];
197 $img_tbl_buffer[$nIndex++] = &Dec2ASCIIString( ($IMG_TYPE_EXT_BL << 24) | ($ext_bl_base >> 8));
198 $entries .= $img_tbl_buffer[$nIndex-1];
199 $img_tbl_buffer[$nIndex++] = &Dec2ASCIIString( ($IMG_TYPE_MOLY << 24) | ($moly_base >> 8));
200 $entries .= $img_tbl_buffer[$nIndex-1];
201
202 #Write img tbl crc
203 my $crc = &Crc32($entries);
204 $img_tbl_buffer[$crcIndex] = &Dec2ASCIIString($crc);
205
206 my $tblstr = join('', @img_tbl_buffer);
207 #&PrintHex($tblstr);
208
209 #padding reserved
210 for ($loop = 0; $loop < $IMAGE_BIN_START - length($tblstr) ; $loop=$loop+1, $nIndex=$nIndex+1) {
211 $img_tbl_buffer[$nIndex] = chr(0xFF);
212 }
213}
214
215#****************************************************************************
216# subroutine: Concatenate src file to dst file
217# input: Src file, dst file
218# output: NA
219#****************************************************************************
220sub ConcaFile
221{
222 my ($src, $dst, $pad) = @_;
223 my $b;
224 my $src_bin_file_size = (-s $src);
225 my $align_size;
226 my $loop;
227 my @padding;
228
229 open (DST_FILE_HANDLE, ">>$dst") or &error_handler("$dst: open file error!");
230 binmode(DST_FILE_HANDLE);
231
232 open (SRC_FILE_HANDLE, "<$src") or &error_handler("$src: open file error!");
233 binmode(SRC_FILE_HANDLE);
234
235 #append src file
236 while(read(SRC_FILE_HANDLE, $b, 1))
237 {
238 print DST_FILE_HANDLE $b;
239 }
240
241 if($pad == 1)
242 {
243 #padding reserved
244 $align_size = ($src_bin_file_size + $IMAGE_ALIGN - 1) & ~($IMAGE_ALIGN - 1);
245 for ($loop = 0; $loop < $align_size - $src_bin_file_size ; $loop=$loop+1) {
246 $padding[$loop] = chr(0xFF);
247 }
248 print DST_FILE_HANDLE @padding;
249 }
250
251 close SRC_FILE_HANDLE;
252 close DST_FILE_HANDLE;
253}
254
255#****************************************************************************
256# subroutine: Dec2HexASCII, without "0x" for prefix, big endian
257# input: Integer value
258# output: Hex ASCII without "0x"
259#****************************************************************************
260sub Dec2ASCIIString
261{
262 my ($dec) = @_;
263 my $str = "";
264 if($dec>0x00FF0000)
265 {
266 $str .= chr(($dec>> 0)&0xFF);
267 $str .= chr(($dec>> 8)&0xFF);
268 $str .= chr(($dec>>16)&0xFF);
269 $str .= chr(($dec>>24)&0xFF);
270 }
271 elsif($dec>0x0000FF00)
272 {
273 $str .= chr($dec&0xFF);
274 $str .= chr(($dec>> 8)&0xFF);
275 $str .= chr(($dec>>16)&0xFF);
276 $str .= chr(0x00);
277 }
278 elsif($dec>0x000000FF)
279 {
280 $str .= chr($dec&0xFF);
281 $str .= chr(($dec>> 8)&0xFF);
282 $str .= chr(0x00);
283 $str .= chr(0x00);
284 }
285 else
286 {
287 $str .= chr($dec&0xFF);
288 $str .= chr(0x00);
289 $str .= chr(0x00);
290 $str .= chr(0x00);
291 }
292
293}
294
295#****************************************************************************
296# subroutine: Calculate the CRC32
297# input: Input string
298# output: CRC value
299#****************************************************************************
300sub Crc32 {
301 my ($input, $init_value, $polynomial) = @_;
302
303 $init_value = 0 unless (defined $init_value);
304 $polynomial = 0xedb88320 unless (defined $polynomial);
305
306 my @lookup_table;
307
308 for (my $i=0; $i<256; $i++) {
309 my $x = $i;
310 for (my $j=0; $j<8; $j++) {
311 if ($x & 1) {
312 $x = ($x >> 1) ^ $polynomial;
313 } else {
314 $x = $x >> 1;
315 }
316 }
317 $lookup_table[$i]=$x;
318 }
319
320 my $crc = $init_value;
321
322 foreach my $x (unpack ('C*', $input)) {
323 $crc = $lookup_table[ ($crc ^ $x) & 0xff ] ^ (($crc >> 8) & 0xffffff);
324 }
325
326 return $crc;
327}
328
329#****************************************************************************
330# subroutine: Print string in HEX value
331# input: Input string
332# output: NA
333#****************************************************************************
334sub PrintHex
335{
336 ## Initial string
337 my ($string) = @_;
338
339 ## convert each character from the string into HEX code
340 $string =~ s/(.)/sprintf("%02X",ord($1))/eg;
341
342 print "$string\n";
343}