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) 2006
|
| 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 | #* Filename:
|
| 38 | #* ---------
|
| 39 | #* strip_drdi_bin.pl
|
| 40 | #*
|
| 41 | #* Project:
|
| 42 | #* --------
|
| 43 | #*
|
| 44 | #*
|
| 45 | #* Description:
|
| 46 | #* ------------
|
| 47 | #* The script will cut the binary into main.bin and drdi.bin.
|
| 48 | #*
|
| 49 | #*
|
| 50 | #*============================================================================
|
| 51 | #* HISTORY
|
| 52 | #* Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
|
| 53 | #*------------------------------------------------------------------------------
|
| 54 | #* $Revision$
|
| 55 | #* $Modtime$
|
| 56 | #* $Log$
|
| 57 | #*------------------------------------------------------------------------------
|
| 58 | #* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
|
| 59 | #*============================================================================
|
| 60 | #****************************************************************************/
|
| 61 | #****************************************************************************
|
| 62 | # Included Modules
|
| 63 | #****************************************************************************
|
| 64 | use strict;
|
| 65 | use File::Basename;
|
| 66 |
|
| 67 | #****************************************************************************
|
| 68 | # Input and Parameters initialization
|
| 69 | #****************************************************************************
|
| 70 | my ($elf_file, $bin_file, $drdi_bin_file, $objdump)=@ARGV;
|
| 71 | my $err_flag = 0;
|
| 72 | print "Input: $elf_file $bin_file $drdi_bin_file $objdump\n";
|
| 73 | &error_handler("$elf_file does not exist!", __FILE__, __LINE__) if(!-e $elf_file);
|
| 74 | &error_handler("$bin_file does not exist!", __FILE__, __LINE__) if(!-e $bin_file);
|
| 75 | #****************************************************************************
|
| 76 | # Main Flow
|
| 77 | #****************************************************************************
|
| 78 | # 1. get the ROM_SIGNATURE_SECTION section info by objdump
|
| 79 | # e.g.,
|
| 80 | # Idx Name Size VMA LMA File off Algn
|
| 81 | # 71 ROM_SIGNATURE_SECTION 00000100 01486d40 01486d40 01486d40 2**2
|
| 82 | my $cmd = "$objdump -j ROM_SIGNATURE_SECTION -h $elf_file";
|
| 83 | print "[CMD] $cmd\n";
|
| 84 | open(my $fh, '-|', $cmd) or &error_handler("$!", __FILE__, __LINE__);
|
| 85 | if(!$fh) {
|
| 86 | &error_handler("The process of getting section info failed!", __FILE__, __LINE__);
|
| 87 | }
|
| 88 |
|
| 89 | my @words;
|
| 90 | while (my $line = <$fh>) {
|
| 91 | if($line =~ /ROM_SIGNATURE_SECTION/) {
|
| 92 | @words = split ' ',$line;
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | # 2. get LMA & Size info
|
| 97 | my $nSIGBase = hex($words[4]);
|
| 98 | my $nSIGLength = hex($words[2]);
|
| 99 |
|
| 100 | # 3. the address to cut bin
|
| 101 | my $nSecondBinBase = $nSIGBase + $nSIGLength;
|
| 102 |
|
| 103 | print "nSIGBase = $nSIGBase\n";
|
| 104 | print "nSIGLength = $nSIGLength\n";
|
| 105 | print "nSecondBinBase = $nSecondBinBase\n";
|
| 106 |
|
| 107 | # 4. split binary into bin0 and bin1 by nSecondBinBase.
|
| 108 | $cmd = "split -b $nSecondBinBase -d -a1 $bin_file $bin_file";
|
| 109 | print "[CMD] $cmd\n";
|
| 110 | my $result = system("$cmd");
|
| 111 | if($result != 0) {
|
| 112 | &error_handler("The process of splitting binary file failed!", __FILE__, __LINE__);
|
| 113 | }
|
| 114 |
|
| 115 | # 5. rename modem binary
|
| 116 | $cmd = "mv ${bin_file}0 $bin_file";
|
| 117 | print "[CMD]$cmd\n";
|
| 118 | $result = system("$cmd");
|
| 119 | if($result != 0) {
|
| 120 | &error_handler("The process of renaming ${bin_file}0 to $bin_file file failed!", __FILE__, __LINE__);
|
| 121 | }
|
| 122 |
|
| 123 | # 6. merge/rename drdi binary
|
| 124 | my $i=1;
|
| 125 | if(-e "$drdi_bin_file") {
|
| 126 | unlink("$drdi_bin_file") or &error_handler("The process of deleting $drdi_bin_file file failed!", __FILE__, __LINE__);
|
| 127 | }
|
| 128 | while(-e "${bin_file}${i}") {
|
| 129 | $cmd = "cat ${bin_file}${i} >> \"$drdi_bin_file\"";
|
| 130 | print "[CMD] $cmd\n";
|
| 131 | $result = system("$cmd");
|
| 132 | if($result != 0) {
|
| 133 | &error_handler("The process of renaming ${bin_file}${i} to $drdi_bin_file file failed!", __FILE__, __LINE__);
|
| 134 | }
|
| 135 | unlink("${bin_file}${i}") or &error_handler("The process of deleting $${bin_file}${i} file failed!", __FILE__, __LINE__);
|
| 136 | $i++;
|
| 137 | }
|
| 138 |
|
| 139 | print "The process of stripping drdi binary file is finished!\n";
|
| 140 | exit 0;
|
| 141 | #*************************************************************************************************
|
| 142 | # Error Handling Message
|
| 143 | #*************************************************************************************************
|
| 144 | sub error_handler
|
| 145 | {
|
| 146 | my ($error_msg, $file, $line_no) = @_;
|
| 147 | my $final_error_msg = "Error: $error_msg at $file line $line_no\n";
|
| 148 | print "$final_error_msg";
|
| 149 | warn "$final_error_msg";
|
| 150 | exit 1;
|
| 151 | }
|
| 152 |
|