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 | #* CommonUtility.pm |
| 40 | #* |
| 41 | #* Project: |
| 42 | #* -------- |
| 43 | #* |
| 44 | #* |
| 45 | #* Description: |
| 46 | #* ------------ |
| 47 | #* This script is to provide common utility |
| 48 | #* |
| 49 | #* Author: |
| 50 | #* ------- |
| 51 | #* Carl Kao (mtk08237) |
| 52 | #* |
| 53 | #****************************************************************************/ |
| 54 | #**************************************************************************** |
| 55 | # Included Modules |
| 56 | #**************************************************************************** |
| 57 | use strict; |
| 58 | BEGIN { push @INC, './pcore/tools/' } # add additional library path |
| 59 | package CommonUtil; |
| 60 | use POSIX qw(floor ceil); |
| 61 | #**************************************************************************** |
| 62 | # oo >>> Finished |
| 63 | #**************************************************************************** |
| 64 | return 1; |
| 65 | |
| 66 | #**************************************************************************** |
| 67 | # CommonUtility Version |
| 68 | #**************************************************************************** |
| 69 | sub CommonUtil_verno |
| 70 | { |
| 71 | return " u0.03"; |
| 72 | # u0.03, 2016/09/06, Tero, Add leading zeros to Dec2Hex |
| 73 | # u0.02_UMOLY, 2015/01/26, Carl, Refine GetMPUAligned for adding debug log |
| 74 | # u0.01_UMOLY = v0.08 |
| 75 | # v0.08 , 20141105 by carl, add GetMPUAligned |
| 76 | # v0.07 , 20131215 by mei, Support space before = in HashStringParser() |
| 77 | # v0.06 , 20130625 by mei, Support HashStringGenerator() and HashStringParser() |
| 78 | # v0.05 , 20130115 by mei, Add WriteFileContent() |
| 79 | # v0.04 , 20120625 by mei, Change chomp by replacing \r\n directly |
| 80 | # v0.03 , 20120528 by mei, Support path and filename case sensitive on Linux |
| 81 | # v0.02 , 20120512 by mei, strengthen funcationality for ParseCSV() |
| 82 | # v0.01 , 20120504 by mei, initial version |
| 83 | } |
| 84 | |
| 85 | |
| 86 | #**************************************************************************** |
| 87 | # subroutine: error_handler |
| 88 | # input: $error_msg: error message |
| 89 | #**************************************************************************** |
| 90 | sub error_handler |
| 91 | { |
| 92 | my ($error_msg, $file, $line_no, $strTitle) = @_; |
| 93 | |
| 94 | my $final_error_msg = "$strTitle ERROR: $error_msg at $file line $line_no : $!\n"; |
| 95 | print $final_error_msg; |
| 96 | die $final_error_msg; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | #**************************************************************************** |
| 101 | # subroutine: Dec2Hex: translate dec number to hex string |
| 102 | # input: Number in dec |
| 103 | # output: hex in string |
| 104 | #**************************************************************************** |
| 105 | #Hex2Dex() : hex string to dec number = hex() |
| 106 | sub Dec2Hex |
| 107 | { |
| 108 | my ($num) = @_; |
| 109 | return sprintf("0x%.08x", $num); |
| 110 | } |
| 111 | |
| 112 | #**************************************************************************** |
| 113 | # subroutine: GetFileContent |
| 114 | # input: strFilePath |
| 115 | # output: strFileContent after chomp |
| 116 | #**************************************************************************** |
| 117 | sub GetFileContent |
| 118 | { |
| 119 | my ($strFilePath) = @_; |
| 120 | my $content; |
| 121 | my ($pack_name, $file, $line_no) = caller; |
| 122 | open FILE, "<$strFilePath" or &error_handler("$strFilePath: open file error!", $file, $line_no, 'CommonUtil::GetFileContent'); |
| 123 | { |
| 124 | local $/; |
| 125 | $content = <FILE>; |
| 126 | } |
| 127 | close FILE; |
| 128 | chomp($content); |
| 129 | return $content; |
| 130 | } |
| 131 | |
| 132 | #**************************************************************************** |
| 133 | # subroutine: WriteFileContent |
| 134 | # input: $strFilePath, $$strFileContent, |
| 135 | # $bAppend: 0 or undef= not append in the file, 1=need to append |
| 136 | # output: x |
| 137 | #**************************************************************************** |
| 138 | sub WriteFileContent |
| 139 | { |
| 140 | my ($strFilePath, $strContent, $bAppend) = @_; |
| 141 | my $strAppend = ""; |
| 142 | $strAppend = ">" if($bAppend); |
| 143 | open FILE, ">$strAppend$strFilePath" or &error_handler("$strFilePath: open file error!", __FILE__, __LINE__, 'CommonUtil::WriteFileContent'); |
| 144 | print FILE $strContent; |
| 145 | close FILE; |
| 146 | } |
| 147 | |
| 148 | #**************************************************************************** |
| 149 | # subroutine: ParseCSV |
| 150 | # input: 1. strFilePath |
| 151 | # 2. $bSkipFirstLine: 1=skip, 0 or undef=not to skip |
| 152 | # output: 1. \@ContentList: array reference [[column, column, column...], [column, column, column...], ...] |
| 153 | # 2. \@LineMeaning: if $bSkipFirstLine==1, return undef; if not, return FirstLine in [colume, colume,...] |
| 154 | #**************************************************************************** |
| 155 | sub ParseCSV |
| 156 | { |
| 157 | my ($strFilePath, $bSkipFirstLine) = @_; |
| 158 | my @ContentList; |
| 159 | my @LineMeaning; |
| 160 | my %IndexByName; |
| 161 | open FILE, "<$strFilePath" or &error_handler("$strFilePath: open file error!", __FILE__, __LINE__, 'CommonUtil::ParseCSV'); |
| 162 | if(1 != $bSkipFirstLine) |
| 163 | { |
| 164 | my $FirstLine = <FILE>; |
| 165 | $FirstLine =~ s/\r|\n//g; |
| 166 | @LineMeaning = split(/,/, $FirstLine, -1); |
| 167 | for(my $i=0; $i<=$#LineMeaning ; $i++) |
| 168 | { |
| 169 | $IndexByName{$LineMeaning[$i]} = $i; |
| 170 | } |
| 171 | } |
| 172 | while(<FILE>) |
| 173 | { |
| 174 | $_ =~ s/\r|\n//g; |
| 175 | my @entry = split(/,/, $_, -1); |
| 176 | for(my $i= (scalar(@LineMeaning)- scalar(@entry)); (($i>0) && (1 != $bSkipFirstLine)); $i--) |
| 177 | { |
| 178 | push @entry, ""; |
| 179 | } |
| 180 | my @Removed_Empty; # to replace " " by "" |
| 181 | foreach my $item(@entry) |
| 182 | { |
| 183 | my $orgitem = $item; |
| 184 | $item =~ s/\s//g; |
| 185 | push (@Removed_Empty, ($item eq "") ? "" : $orgitem ); |
| 186 | } |
| 187 | push @ContentList, \@Removed_Empty; |
| 188 | } |
| 189 | close FILE; |
| 190 | return (\@ContentList, \%IndexByName); |
| 191 | } |
| 192 | |
| 193 | #**************************************************************************** |
| 194 | # subroutine: ConnetString |
| 195 | # input: 1. \@token |
| 196 | # 2. string to conect |
| 197 | # 3. $bConnectEmptyString: 1=support emptystring connect, 0 or undef=not support it |
| 198 | # output: string |
| 199 | #**************************************************************************** |
| 200 | sub ConnetString |
| 201 | { |
| 202 | my ($token_ref, $strConnector, $bConnectEmptyString) = @_; |
| 203 | my $strResult; |
| 204 | if($bConnectEmptyString) |
| 205 | { |
| 206 | $strResult = join($strConnector, @$token_ref); |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | foreach my $i (@$token_ref) |
| 211 | { |
| 212 | my $temp = $i; |
| 213 | $temp =~ s/\s//g; |
| 214 | if($strResult eq "" and $temp ne "") |
| 215 | { |
| 216 | $strResult = $i; |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | $strResult = join($strConnector, $strResult, $i) if($i ne ""); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | return $strResult; |
| 225 | } |
| 226 | #**************************************************************************** |
| 227 | # subroutine: HashStringGenerator |
| 228 | # input: 1. \%Hash |
| 229 | # output: string |
| 230 | #**************************************************************************** |
| 231 | sub HashStringGenerator |
| 232 | { |
| 233 | my ($href) = @_; |
| 234 | my $strOutput; |
| 235 | map { $strOutput .= "$_=".$href->{$_}."\n";} sort keys %$href; |
| 236 | return $strOutput; |
| 237 | } |
| 238 | |
| 239 | #**************************************************************************** |
| 240 | # subroutine: HashStringParser |
| 241 | # input: 1.HashStringGenerator's content 2. output:\%Hash |
| 242 | # output: N/A |
| 243 | #**************************************************************************** |
| 244 | sub HashStringParser |
| 245 | { |
| 246 | my ($strHash, $Output_href)= @_; |
| 247 | my @items = split(/\n/, $strHash); |
| 248 | foreach my $item (@items) |
| 249 | { |
| 250 | my @KeyValue = split(/\=/, $item); |
| 251 | my $key = $KeyValue[0]; |
| 252 | my $value = $KeyValue[1]; |
| 253 | $key =~ s/\s//; |
| 254 | $value =~ s/\s//; |
| 255 | $Output_href->{$key} = $value; |
| 256 | $Output_href->{uc($key)} = $value; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | sub GetMBAligned |
| 261 | { |
| 262 | my ($nSize, $nMBAligned) = @_; |
| 263 | return ceil($nSize/($nMBAligned*1024*1024)) *$nMBAligned*1024*1024; |
| 264 | } |
| 265 | |
| 266 | sub GetKBAligned |
| 267 | { |
| 268 | my ($nSize, $nKBAligned) = @_; |
| 269 | return ceil($nSize/($nKBAligned*1024)) *$nKBAligned*1024; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | # ask by MPU owner |
| 274 | sub GetMPUAligned |
| 275 | { |
| 276 | my ($nOriAddr, $strTarget) = @_; |
| 277 | |
| 278 | my $nAlignSize = 2**(ceil(log($nOriAddr)/log(2))-13); |
| 279 | my $nAlignAddr = CommonUtil::GetKBAligned($nOriAddr, $nAlignSize); |
| 280 | |
| 281 | if($strTarget ne "") { |
| 282 | my ($strOriAddr, $strAlignAddr) = (CommonUtil::Dec2Hex($nOriAddr), CommonUtil::Dec2Hex($nAlignAddr)); |
| 283 | |
| 284 | if($nAlignSize<1024) { |
| 285 | print "$strTarget $nAlignSize KB MPU Alignment: $strOriAddr -> new:$strAlignAddr\n"; |
| 286 | } else { |
| 287 | print "$strTarget ".($nAlignSize/1024)." MB MPU Alignment: $strOriAddr -> new:$strAlignAddr\n" ; |
| 288 | } |
| 289 | } |
| 290 | return $nAlignAddr; |
| 291 | } |
| 292 | |
| 293 | |