|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Collections.Generic; |
|
19 using System.Text; |
|
20 using System.Text.RegularExpressions; |
|
21 using System.IO; |
|
22 using SymbianUtils.Strings; |
|
23 using SymbianUtils.Streams; |
|
24 using SymbianImageLib.Common.Header; |
|
25 using SymbianImageLib.Common.Image; |
|
26 using SymbianStructuresLib.Compression.Common; |
|
27 using SymbianImageLib.ROM.Structures; |
|
28 |
|
29 namespace SymbianImageLib.ROM.Header |
|
30 { |
|
31 internal class SIHeaderROM : SIHeader |
|
32 { |
|
33 #region Constructors |
|
34 public SIHeaderROM( SIImage aImage, Stream aStream ) |
|
35 : base( aImage ) |
|
36 { |
|
37 aStream.Seek( 0, SeekOrigin.Begin ); |
|
38 // |
|
39 iHeaderData = new byte[ KMaximumRomHeaderSize ]; |
|
40 int amountRead = aStream.Read( iHeaderData, 0, KMaximumRomHeaderSize ); |
|
41 // |
|
42 string headerText = StringParsingUtils.BytesToString( iHeaderData, KEpocHeaderLength ); |
|
43 base.Trace( "[SymbianImageHeaderROM] Ctor() - headerText: {0}, amountRead: {1}", headerText, amountRead ); |
|
44 // |
|
45 if ( IsEpocHeader( headerText ) == false ) |
|
46 { |
|
47 throw new NotSupportedException( string.Format( "ROM Image header is unsupported: {0}", headerText ) ); |
|
48 } |
|
49 // |
|
50 ReadHeaderData( iHeaderData ); |
|
51 } |
|
52 #endregion |
|
53 |
|
54 #region Constants |
|
55 public const int KMaximumRomHeaderSize = 0x400; |
|
56 public const int KPageSize = 0x1000; |
|
57 #endregion |
|
58 |
|
59 #region From SymbianImageHeader |
|
60 public override TSymbianCompressionType CompressionType |
|
61 { |
|
62 get |
|
63 { |
|
64 TSymbianCompressionType ret = TSymbianCompressionType.ENone; |
|
65 // |
|
66 if ( iRomHdr.CompressionType == (uint) TSymbianCompressionType.EBytePair ) |
|
67 { |
|
68 ret = TSymbianCompressionType.EBytePair; |
|
69 } |
|
70 else if ( iRomHdr.CompressionType == (uint) TSymbianCompressionType.EDeflate ) |
|
71 { |
|
72 ret = TSymbianCompressionType.EDeflate; |
|
73 } |
|
74 else |
|
75 { |
|
76 // Check for byte pair... |
|
77 if ( iRomHdr.RomPageIndex != 0 ) |
|
78 { |
|
79 ret = TSymbianCompressionType.EBytePair; |
|
80 } |
|
81 } |
|
82 // |
|
83 return ret; |
|
84 } |
|
85 } |
|
86 |
|
87 public override uint HeaderSize |
|
88 { |
|
89 get |
|
90 { |
|
91 uint ret = 0; |
|
92 // |
|
93 ret += HeaderSizeLoader; |
|
94 ret += iRomHdr.Size; |
|
95 // |
|
96 return ret; |
|
97 } |
|
98 } |
|
99 #endregion |
|
100 |
|
101 #region API |
|
102 public static bool IsROM( Stream aStream ) |
|
103 { |
|
104 using ( SymbianStreamReaderLE reader = SymbianStreamReaderLE.New( aStream, SymbianStreamReaderLE.TCloseOperation.EResetPosition ) ) |
|
105 { |
|
106 string signature = reader.ReadString( KEpocHeaderLength ); |
|
107 bool ret = IsEpocHeader( signature ); |
|
108 return ret; |
|
109 } |
|
110 } |
|
111 #endregion |
|
112 |
|
113 #region Properties |
|
114 public uint UncompressedRomSize |
|
115 { |
|
116 get { return iRomHdr.UncompressedSize; } |
|
117 } |
|
118 |
|
119 public uint HeaderSizeLoader |
|
120 { |
|
121 get { return iLoaderHdr.Size; } |
|
122 } |
|
123 |
|
124 public uint RomBaseAddress |
|
125 { |
|
126 get { return iRomHdr.RomBaseAddress; } |
|
127 } |
|
128 |
|
129 public int NumberOfPages |
|
130 { |
|
131 get |
|
132 { |
|
133 int pageAreaStart = iRomHdr.PageableRomStart; |
|
134 int pageRomSize = iRomHdr.PageableRomSize; |
|
135 int numPages = ( pageAreaStart + pageRomSize + KPageSize - 1 ) / KPageSize; |
|
136 // |
|
137 return numPages; |
|
138 } |
|
139 } |
|
140 |
|
141 public uint RomPageIndexOffset |
|
142 { |
|
143 get |
|
144 { |
|
145 uint ret = iLoaderHdr.Size; |
|
146 ret += iRomHdr.RomPageIndex; |
|
147 return ret; |
|
148 } |
|
149 } |
|
150 #endregion |
|
151 |
|
152 #region Internal constants |
|
153 // The signature for Core ROM images used to be EPOCARM4ROM, but now EPOCARM5ROM has |
|
154 // also been observed. |
|
155 private const int KEpocHeaderLength = 11; |
|
156 private static readonly Regex KEpocHeaderTextRegEx = new Regex( |
|
157 "EPOCARM\\dROM", |
|
158 RegexOptions.Singleline |
|
159 | RegexOptions.CultureInvariant |
|
160 | RegexOptions.IgnorePatternWhitespace |
|
161 | RegexOptions.Compiled |
|
162 ); |
|
163 #endregion |
|
164 |
|
165 #region Internal methods |
|
166 private static bool IsEpocHeader( string aText ) |
|
167 { |
|
168 Match m = KEpocHeaderTextRegEx.Match( aText ); |
|
169 bool ret = m.Success; |
|
170 return ret; |
|
171 } |
|
172 |
|
173 private void ReadHeaderData( byte[] aBuffer ) |
|
174 { |
|
175 using ( MemoryStream stream = new MemoryStream( aBuffer ) ) |
|
176 { |
|
177 using ( BinaryReader reader = new BinaryReader( stream ) ) |
|
178 { |
|
179 iLoaderHdr.Read( reader ); |
|
180 iRomHdr.Read( reader ); |
|
181 } |
|
182 } |
|
183 } |
|
184 #endregion |
|
185 |
|
186 #region Data members |
|
187 private TRomLoaderHeader iLoaderHdr = new TRomLoaderHeader(); |
|
188 private TRomHeader iRomHdr = new TRomHeader(); |
|
189 private readonly byte[] iHeaderData; |
|
190 #endregion |
|
191 } |
|
192 } |