aboutsummaryrefslogtreecommitdiff
path: root/SrcShared/EmROMReader.h
blob: 902f64aa0efe67f39f2e269397b30efb12abb23a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 2000-2001 Palm, Inc. or its subsidiaries.
	All rights reserved.

	This file is part of the Palm OS Emulator.

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
\* ===================================================================== */

#ifndef EmROMReader_h
#define EmROMReader_h

/*
	This file defines EmROMReader, a class used to parse up raw ROM
	images and return information about them, such as the version number
	in the version resource, card header information, and whether or not
	the splash screen is in color.  This information is used when trying
	to determine on what device or devices the ROM can run.

	NOTE: this class used to be a template class, parameterized on LAS
	and PAS in EmPalmStructs.h.  The intent was that it could be used to
	access a raw ROM image from disk (using LAS) or the ROM image as
	utilized in the emulated address space (using PAS).  However, VC++
	had problems trying to compile the nested classes in the EmROMReader
	class template, so that feature was sacrificed.
*/

#include "EmPalmStructs.h"

// Define this to 1 if you want to dump a list of databases
// to the log file as the ROM is being enumerated.  This is
// handy when trying to find something in the ROM that differentiates
// it from other ROMs.

#define LOG_DATABASES_HACK	0

#if LOG_DATABASES_HACK
#include "Logging.h"		// LogAppendMsg
#endif

#include <vector>
#include <map>
#include <string>

typedef LAS A;

class EmROMReader
{
	public:
		typedef A::ptr_type ptr_type;

		class ChunkType
		{
			public:

				ChunkType(void);
				ChunkType(const EmAliasROMHeapChunkHdr2Type<A>& chunk, UInt32 addr);
				ChunkType(const EmAliasROMHeapChunkHdr1Type<A>& chunk, UInt32 addr);

			public:

				bool			Free(void)			{ return fFree; }
				int				Owner(void)			{ return fOwner; }
				unsigned long	TotalSize(void)		{ return fSize; }
				size_t			Size(void)			{ return fSize - fHdrSize - fSizeAdj; }
				ptr_type		GetHdrPtr(void)		{ return fPtr; }
				ptr_type		GetPtr(void)		{ return A::add(fPtr, (int)(fHdrSize)); }
				UInt32			LocalID(void)		{ return fAddress; }

				bool			Contains(ptr_type pos, size_t len=1)
													{ return (A::diff(pos, fPtr) >= 0) &&
														((A::diff(pos, fPtr) + len) <= fSize); }

			private:

				friend class EmROMReader;

				unsigned long	fSize;
				bool			fFree;
				unsigned long	fSizeAdj;
				int				fLockCount;
				int				fHOffset;
				int 			fOwner;
				ptr_type		fPtr;
				unsigned long	fAddress;
				int				fHdrSize;

				unsigned long	recordNumber;
				unsigned long	dbNumber;

				enum
				{
					typeOther,
					typeRecord,
					typeDBHeader,
					typeDBTable,
					typeFree
				} 				type;
		};

		typedef map<unsigned long, ChunkType> ChunkList;

		class DatabaseItemType
		{
			public:

				DatabaseItemType(void);
				DatabaseItemType(const EmAliasROMDBRecordHeader1Type<A> record, EmROMReader* rom);
				DatabaseItemType(const EmAliasROMDBResourceHeader1Type<A> resource, EmROMReader* rom);

			public:

				bool			Deleted(void)	{ return fDeleted; }
				bool			Dirty(void)		{ return fDirty; }
				bool			Busy(void)		{ return fBusy; }
				bool			Secret(void)	{ return fSecret; }
				int				Category(void)	{ return fCategory; }
				unsigned long	ID(void)		{ return fID; }
				unsigned long	Type(void)		{ return fType; }
				bool			Record(void)	{ return fRecord; }
				ChunkType*		Chunk(void) 	{ return fChunk; }

			private:

				friend class EmROMReader;

				bool			fRecord;
				unsigned long	fID;
				unsigned long	fType;
				int				fCategory;
				bool			fDeleted;
				bool			fDirty;
				bool			fBusy;
				bool			fSecret;
				UInt32			fChunkLID;

				ChunkType*		fChunk;
		};

		typedef vector<DatabaseItemType>	DatabaseItemList;

		class DatabaseType
		{
			public:

				DatabaseType(void);
				DatabaseType(ChunkType* hdr, EmROMReader* rom);

			public:

				string&				Name(void)				{ return fName; }
				const string&		Name(void)	const		{ return fName; }
				DatabaseItemList&	Items(void)				{ return fItems; }

				unsigned long 		CreationTime(void)		{ return fCreationTime; }
				unsigned long		ModificationTime(void)	{ return fModificationTime; }
				unsigned long		BackupTime(void)		{ return fBackupTime; }
				unsigned int		Flags(void)				{ return fFlags; }
				unsigned int		Version(void)			{ return fVersion; }
				unsigned long		UniqueIDSeed(void)		{ return fUniqueIDSeed; }

				unsigned long		Creator(void)			{ return fCreator; }
				unsigned long		Type(void)				{ return fType; }

			private:

				friend class EmROMReader;

				DatabaseItemList	fItems;
				string				fName;
				unsigned long		fCreator, fType;
				unsigned long		fCreationTime, fModificationTime, fBackupTime;
				unsigned int		fFlags, fVersion;
				unsigned long		fUniqueIDSeed;
				ChunkType*			fAppInfoChunk;
				ChunkType*			fSortInfoChunk;
		};

		typedef vector<DatabaseType> DatabaseList;
		typedef map<unsigned long, map<int, int> > FeatureList;

	public:

		EmROMReader(ptr_type arenaPtr, size_t arenaSize);
		~EmROMReader(void);

		bool AcquireCardHeader(void);
		UInt32 GuessCardAddress(void);
		void SetCardAddress(UInt32 address);
		bool AcquireROMHeap(void);
		bool AcquireDatabases(void);
		bool AcquireFeatures(void);
		bool AcquireSplashDB(void);

		bool ContainsDB(string dbName)	const;

		void			Version(int& major,
								int& minor,
								int& fix,
								int& stage,
								int& build) const;
		unsigned long	Version(void) const;

		static int		IsBitmapColor (ChunkType& chunk, bool bootScreen);

	public:

		ChunkList& 	Chunks(void)	{ return fChunks; }
		DatabaseList&	Databases(void)	{ return fDatabases; }
		FeatureList&	Features(void)	{ return fFeatures; }
		ChunkType*		GetSplashChunk(void) const { return fSplashChunk; }
		ptr_type		GetCardHeaderPtr(void) const { return fPtr; }
		ptr_type		GetCardStorePtr(void)  const { return A::add(fPtr, EmAliasROMCardHeader5Type<A>::GetSize()); }
		bool GetFlag328(void)			const { return fFlag328; }
		bool GetFlagEZ(void)			const { return fFlagEZ; }
		bool GetFlagVZ(void)			const { return fFlagVZ; }
		bool GetFlagSZ(void)			const { return fFlagSZ; }
		UInt16 GetCardVersion(void)		const { return fCardVersion; }
		string GetCardName(void)		const { return fCardName; }
		string GetCardManufacturer(void) const { return fCardManuf; }
		UInt16 GetStoreVersion(void)	const { return fStoreVersion; }
		UInt32 GetCompanyID(void)		const { return fCompanyID; }
		UInt32 GetHalID(void)			const { return fHalID; }
		UInt32 GetRomVersion(void)		const { return fRomVersion; }
		string GetRomVersionString(void) const { return fRomVersionString; }

	private:

		ptr_type		fPtr;
		size_t			fSize;
		ptr_type		fEndPtr;

		UInt32			fCardAddress;

		ChunkList		fChunks;
		DatabaseList	fDatabases;
		FeatureList		fFeatures;

		bool			fFlag328;
		bool			fFlagEZ;
		bool			fFlagVZ;
		bool			fFlagSZ;

		UInt16			fCardVersion;
		string			fCardName, fCardManuf;

		UInt16			fStoreVersion;

		UInt32			fCompanyID, fHalID;
		UInt32			fRomVersion;
		string			fRomVersionString;

		ChunkType*		fSplashChunk;
};

#endif	// EmROMReader_h