blob: 73e98d17e3bf656ea6729bf439e287dfc6a4c2cc [file] [log] [blame]
yu.dongc33b3072024-08-21 23:14:49 -07001#!/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#* ckFlashInfo.pl
40#*
41#* Project:
42#* --------
43#*
44#*
45#* Description:
46#* ------------
47#* This script will
48#* 1. parse PartNumber_cfg.ini to obtain a series of part numbers
49#* 2. read MDL to get flash ID of these part numbers
50#* 3. compare these flash ID and report error if there are duplicated ones
51#*
52#* Author:
53#* -------
54#* Claudia Lo (mtk01876)
55#*
56#*============================================================================
57#* HISTORY
58#* Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
59#*------------------------------------------------------------------------------
60#* $Revision$
61#* $Modtime$
62#* $Log$
63#*
64#*
65#*------------------------------------------------------------------------------
66#* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
67#*============================================================================
68#****************************************************************************/
69
70#****************************************************************************
71# Included Modules
72#****************************************************************************
73use strict;
74use Win32::OLE qw(in with);
75use Win32::OLE::Const 'Microsoft Excel';
76$Win32::OLE::Warn = 3; # die on errors...
77
78#****************************************************************************
79# Constants
80#****************************************************************************
81my $CKFLASHINFO_VERNO = " v0.00";
82 # v0.00 , by Claudia at 2009/12/11 , initial version
83 #
84
85my $DebugPrint = 1; # 1 for debug; 0 for non-debug
86
87my $PARTNUMBER_CFG_INI = $ARGV[0];
88my $MEMORY_DEVICE_LIST_XLS = Win32::GetCwd()."\\".$ARGV[1];
89my $MEMORY_DEVICE_LIST_XLS_E = $ARGV[1];
90my $CUSTOM_MEMORY_DEVICE_HDR = $ARGV[2];
91
92# to align error message file name format
93$MEMORY_DEVICE_LIST_XLS_E =~ s/^.\\|^\\//;
94
95#****************************************************************************
96# parse custom_MemoryDevice.h to extract MEMORY_DEVICE_TYPE & PART_NUMBER
97#****************************************************************************
98open CUSTOM_MEMORY_DEVICE_HDR, "<$CUSTOM_MEMORY_DEVICE_HDR" or &error_handler("$CUSTOM_MEMORY_DEVICE_HDR: file error!", __FILE__, __LINE__);
99my $MEMORY_DEVICE_TYPE;
100while (<CUSTOM_MEMORY_DEVICE_HDR>)
101{
102 if (/^#define\s+MEMORY_DEVICE_TYPE\s+(\w*)/)
103 {
104 $MEMORY_DEVICE_TYPE = $1;
105 }
106}
107close CUSTOM_MEMORY_DEVICE_HDR;
108
109&error_handler("$CUSTOM_MEMORY_DEVICE_HDR: MEMORY_DEVICE_TYPE undefined!", __FILE__, __LINE__) if (!defined $MEMORY_DEVICE_TYPE);
110
111if ($DebugPrint == 1)
112{
113 print "MEMORY_DEVICE_TYPE: $MEMORY_DEVICE_TYPE\n";
114}
115
116#****************************************************************************
117# parse EMI database to get flash IDs
118#****************************************************************************
119my %PN_FlashID;
120if ($MEMORY_DEVICE_TYPE eq 'NOR_RAM_MCP')
121{
122 &fs_parse_mdl_key_content($MEMORY_DEVICE_LIST_XLS, 'NOR_RAM_MCP', 'Part Number', 'Flash ID', \%PN_FlashID);
123} # if ($MEMORY_DEVICE_TYPE eq 'NOR_RAM_MCP')
124elsif ($MEMORY_DEVICE_TYPE eq 'NOR_LPSDRAM_MCP')
125{
126 &fs_parse_mdl_key_content($MEMORY_DEVICE_LIST_XLS, 'NOR_RAM_MCP', 'Part Number', 'Flash ID', \%PN_FlashID);
127}# elsif ($MEMORY_DEVICE_TYPE eq 'NOR_LPSDRAM_MCP')
128elsif ($MEMORY_DEVICE_TYPE eq 'LPSDRAM')
129{
130 &fs_parse_mdl_key_content($MEMORY_DEVICE_LIST_XLS, 'LPSDRAM', 'Part Number', 'Flash ID', \%PN_FlashID);
131}# elsif ($MEMORY_DEVICE_TYPE eq 'LPSDRAM')
132else
133{
134 &error_handler("$CUSTOM_MEMORY_DEVICE_HDR: invalid MEMORY_DEVICE_TYPE!", __FILE__, __LINE__);
135}
136
137#****************************************************************************
138# parse PartNumber_cfg.ini to obtain a series of part numbers,
139# retrieve their flash IDs, and check whether the flash IDs are duplicated
140#****************************************************************************
141open PARTNUMBER_CFG_INI_HANDLE, "<$PARTNUMBER_CFG_INI";
142my %PN_FlashID_cfg;
143while (<PARTNUMBER_CFG_INI_HANDLE>)
144{
145 if (/([\w|\-]+)\n*/)
146 {
147 my $part_number = $1;
148 if ($DebugPrint == 1)
149 {
150 print "Part Number: $1, flash ID: $PN_FlashID{$part_number}\n";
151 }
152
153 &error_handler("$PARTNUMBER_CFG_INI: Invalid part number $part_number! Please assign valid part numbers!\n", __FILE__, __LINE__) if (!defined $PN_FlashID{$part_number});
154 if ((defined $PN_FlashID{$part_number}) and ($PN_FlashID{$part_number} =~ /\s*0x[A-Fa-f0-9]{4}\s*,*/))
155 {
156 my @id_list = split /\,/, $PN_FlashID{$part_number};
157 foreach (@id_list)
158 {
159 if (/\s*0x[A-Fa-f0-9]{4}\s*/)
160 {
161 }
162 else
163 {
164 &error_handler("$PARTNUMBER_CFG_INI: Invalid Flash ID of part number $part_number! Please choose another part number with valid flash ID!\n", __FILE__, __LINE__);
165 }
166 }
167 foreach (keys %PN_FlashID_cfg)
168 {
169 &error_handler("$PARTNUMBER_CFG_INI: Duplicated Flash ID of part number $_ and $part_number! Please assign part numbers with different flash IDs, otherwise the flash tool cannot determine which load to download!\n", __FILE__, __LINE__) if (uc($PN_FlashID{$part_number}) eq uc($PN_FlashID_cfg{$_}));
170 }
171 }
172 else
173 {
174 &error_handler("$PARTNUMBER_CFG_INI: Invalid Flash ID of part number $part_number! Please choose another part number with valid flash ID!\n", __FILE__, __LINE__);
175 }
176 $PN_FlashID_cfg{$part_number} = $PN_FlashID{$part_number};
177 }
178}
179
180close PARTNUMBER_CFG_INI_HANDLE;
181
182if ($DebugPrint == 1)
183{
184 my $idx = 1;
185 foreach (keys %PN_FlashID_cfg)
186 {
187 print "Part Number $idx: $_, flash ID $PN_FlashID_cfg{$_}\n";
188 $idx++;
189 }
190}
191
192
193exit;
194
195
196#****************************************************************************
197# subroutine: xls_cell_value
198# return: excel cell value no matter it's in merge area or not
199# input: $sheet: specified Excel Sheet
200# $row: specified row number
201# $col: specified column number
202#****************************************************************************
203sub xls_cell_value
204{
205 my ($sheet, $row, $col) = @_;
206
207 if ($sheet->Cells($row, $col)->{'MergeCells'})
208 {
209 my $ma = $sheet->Cells($row, $col)->{'MergeArea'};
210 return ($ma->Cells(1, 1)->{'Value'});
211 }
212 else
213 {
214 return ($sheet->Cells($row, $col)->{'Value'})
215 }
216}
217
218#****************************************************************************
219# subroutine: fs_parse_mdl_key_content
220# return: hash of matching rows and indexing rows
221# input: $file: excel file to be read
222# $sheet: sheet to open
223# $key: the column in the file to be keys
224# $content: the column in the file to be contents
225# $target_href: the output hash of all key-content mappings in the file
226#****************************************************************************
227sub fs_parse_mdl_key_content
228{
229 my ($file, $sheet, $key, $content, $target_href) = @_;
230
231 ### get already active Excel application or open new
232 my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit');
233
234 ### copy the Excel file to a temp file and open it;
235 ### this will prevent error due to simultaneous Excel access
236 my $Book = $Excel->Workbooks->Open($file);
237
238 ###
239 my $WorkSheet = $Book->Worksheets($sheet);
240
241 ### find the key and content columns
242 my ($key_col, $content_col);
243 my $col = 1;
244 while (defined &xls_cell_value($WorkSheet, 1, $col))
245 {
246 if (&xls_cell_value($WorkSheet, 1, $col) eq $key)
247 {
248 $key_col = $col
249 }
250 if (&xls_cell_value($WorkSheet, 1, $col) eq $content)
251 {
252 $content_col = $col
253 }
254 $col++;
255 }
256
257 if ($DebugPrint == 1)
258 {
259 print "key_col = $key_col; content_col = $content_col\n";
260 }
261
262 ### collect all key contents in the output href
263 my $row = 2;
264 my $empty_flag = 0; # exit if two consecutive empty lines are encountered
265 while (1)
266 {
267 if (!defined &xls_cell_value($WorkSheet, $row, $key_col))
268 {
269 if ($empty_flag == 0)
270 {
271 $empty_flag = 1;
272 }
273 elsif ($empty_flag == 1)
274 {
275 last;
276 }
277
278 }
279 else
280 {
281 $empty_flag = 0;
282 }
283
284 my $key_val = &xls_cell_value($WorkSheet, $row, $key_col);
285 my $content_val = &xls_cell_value($WorkSheet, $row, $content_col);
286 if ($content_val =~ /\w+/)
287 {
288 my @key_list = split /\s/, $key_val;
289#print "key_val = $key_val, content_val = $content_val\n";
290 foreach (@key_list)
291 {
292 if (/\w+/)
293 {
294 if (!defined $target_href->{$_})
295 {
296 $target_href->{$_} = $content_val;
297 }
298 }
299 }
300 }
301 $row++;
302 }
303
304 ### close the temp Excel file
305 $Book->Close(1);
306}
307
308#****************************************************************************
309# subroutine: error_handler
310# input: $error_msg: error message
311#****************************************************************************
312sub error_handler
313{
314 my ($error_msg, $file, $line_no) = @_;
315
316 my $final_error_msg = "CKFLASHID ERROR: $error_msg at $file line $line_no\n";
317 print $final_error_msg;
318 die $final_error_msg;
319}