summaryrefslogtreecommitdiffstats
path: root/libpit/Source/libpit.h
blob: c7bfe8b44fd8e5aaee205928cd7baf2d06340434 (plain) (blame)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* Copyright (c) 2010-2013 Benjamin Dobell, Glass Echidna
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.*/

#ifndef LIBPIT_H
#define LIBPIT_H

#ifdef WIN32
#pragma warning(disable : 4996)
#endif

#if (!(defined _MSC_VER) || (_MSC_VER < 1700))

#ifndef nullptr
#define nullptr 0
#endif

#endif

// C/C++ Standard Library
#include <cstring>
#include <string>
#include <vector>

namespace libpit
{
	class PitEntry
	{
		public:

			enum
			{
				kDataSize = 132,
				kPartitionNameMaxLength = 32,
				kFlashFilenameMaxLength = 32,
				kFotaFilenameMaxLength = 32
			};

			enum
			{
				kBinaryTypeApplicationProcessor = 0,
				kBinaryTypeCommunicationProcessor = 1
			};

			enum
			{
				kDeviceTypeOneNand = 0,
				kDeviceTypeFile, // FAT
				kDeviceTypeMMC,
				kDeviceTypeAll // ?
			};

			enum
			{
				kAttributeWrite = 1,
				kAttributeSTL = 1 << 1
			};

			enum
			{
				kUpdateAttributeFota = 1,
				kUpdateAttributeSecure = 1 << 1
			};

		private:

			unsigned int binaryType;
			unsigned int deviceType;
			unsigned int identifier;
			unsigned int attributes;
			unsigned int updateAttributes;

			unsigned int blockSizeOrOffset;
			unsigned int blockCount;

			unsigned int fileOffset; // Obsolete
			unsigned int fileSize; // Obsolete

			char partitionName[kPartitionNameMaxLength];
			char flashFilename[kFlashFilenameMaxLength]; // USB flash filename
			char fotaFilename[kFotaFilenameMaxLength]; // Firmware over the air

		public:

			PitEntry();
			~PitEntry();

			bool Matches(const PitEntry *otherPitEntry) const;

			bool IsFlashable(void) const
			{
				return strlen(partitionName) != 0;
			}

			unsigned int GetBinaryType(void) const
			{
				return binaryType;
			}

			void SetBinaryType(unsigned int binaryType)
			{
				this->binaryType = binaryType;
			}

			unsigned int GetDeviceType(void) const
			{
				return deviceType;
			}

			void SetDeviceType(unsigned int deviceType)
			{
				this->deviceType = deviceType;
			}

			unsigned int GetIdentifier(void) const
			{
				return identifier;
			}

			void SetIdentifier(unsigned int identifier)
			{
				this->identifier = identifier;
			}

			unsigned int GetAttributes(void) const
			{
				return attributes;
			}

			void SetAttributes(unsigned int attributes)
			{
				this->attributes = attributes;
			}

			unsigned int GetUpdateAttributes(void) const
			{
				return updateAttributes;
			}

			void SetUpdateAttributes(unsigned int updateAttributes)
			{
				this->updateAttributes = updateAttributes;
			}
			
			// Different versions of Loke (secondary bootloaders) on different devices intepret this differently.
			unsigned int GetBlockSizeOrOffset(void) const
			{
				return blockSizeOrOffset;
			}

			void SetBlockSizeOrOffset(unsigned int blockSizeOrOffset)
			{
				this->blockSizeOrOffset = blockSizeOrOffset;
			}

			unsigned int GetBlockCount(void) const
			{
				return blockCount;
			}

			void SetBlockCount(unsigned int blockCount)
			{
				this->blockCount = blockCount;
			}

			unsigned int GetFileOffset(void) const
			{
				return fileOffset;
			}

			void SetFileOffset(unsigned int fileOffset)
			{
				this->fileOffset = fileOffset;
			}

			unsigned int GetFileSize(void) const
			{
				return fileSize;
			}

			void SetFileSize(unsigned int fileSize)
			{
				this->fileSize = fileSize;
			}

			const char *GetPartitionName(void) const
			{
				return partitionName;
			}

			void SetPartitionName(const char *partitionName)
			{
				// This isn't strictly necessary but ensures no junk is left in our PIT file.
				memset(this->partitionName, 0, 64);

				if (strlen(partitionName) < 64)
					strcpy(this->partitionName, partitionName);
				else
					memcpy(this->partitionName, partitionName, 63);
			}

			const char *GetFlashFilename(void) const
			{
				return flashFilename;
			}

			void SetFlashFilename(const char *flashFilename)
			{
				// This isn't strictly necessary but ensures no junk is left in our PIT file.
				memset(this->flashFilename, 0, kFlashFilenameMaxLength);

				if (strlen(partitionName) < 32)
					strcpy(this->flashFilename, flashFilename);
				else
					memcpy(this->flashFilename, flashFilename, 31);
			}

			const char *GetFotaFilename(void) const
			{
				return fotaFilename;
			}

			void SetFotaFilename(const char *fotaFilename)
			{
				// This isn't strictly necessary but ensures no junk is left in our PIT file.
				memset(this->fotaFilename, 0, kFotaFilenameMaxLength);

				if (strlen(partitionName) < 32)
					strcpy(this->fotaFilename, fotaFilename);
				else
					memcpy(this->fotaFilename, fotaFilename, 31);
			}
	};

	class PitData
	{
		public:

			enum
			{
				kFileIdentifier = 0x12349876,
				kHeaderDataSize = 28,
				kPaddedSizeMultiplicand = 4096
			};

		private:

			unsigned int entryCount; // 0x04
			unsigned int unknown1;   // 0x08
			unsigned int unknown2;   // 0x0C

			unsigned short unknown3; // 0x10
			unsigned short unknown4; // 0x12

			unsigned short unknown5; // 0x14
			unsigned short unknown6; // 0x16

			unsigned short unknown7; // 0x18
			unsigned short unknown8; // 0x1A

			// Entries start at 0x1C
			std::vector<PitEntry *> entries;

			static int UnpackInteger(const unsigned char *data, unsigned int offset)
			{
#ifdef WORDS_BIGENDIAN
				int value = (data[offset] << 24) | (data[offset + 1] << 16) |
					(data[offset + 2] << 8) | data[offset + 3];
#else
				// Flip endianness
				int value = data[offset] | (data[offset + 1] << 8) |
					(data[offset + 2] << 16) | (data[offset + 3] << 24);
#endif
				return (value);
			}

			static int UnpackShort(const unsigned char *data, unsigned int offset)
			{
#ifdef WORDS_BIGENDIAN
				short value = (data[offset] << 8) | data[offset + 1];
#else
				// Flip endianness
				short value = data[offset] | (data[offset + 1] << 8);
#endif
				return (value);
			}

			static void PackInteger(unsigned char *data, unsigned int offset, unsigned int value)
			{
#ifdef WORDS_BIGENDIAN
				data[offset] = (value & 0xFF000000) >> 24;
				data[offset + 1] = (value & 0x00FF0000) >> 16;
				data[offset + 2] = (value & 0x0000FF00) >> 8;
				data[offset + 3] = value & 0x000000FF;
#else
				// Flip endianness
				data[offset] = value & 0x000000FF;
				data[offset + 1] = (value & 0x0000FF00) >> 8;
				data[offset + 2] = (value & 0x00FF0000) >> 16;
				data[offset + 3] = (value & 0xFF000000) >> 24;
#endif
			}

			static void PackShort(unsigned char *data, unsigned int offset, unsigned short value)
			{
#ifdef WORDS_BIGENDIAN
				data[offset] = (value & 0xFF00) >> 8;
				data[offset + 1] = value & 0x00FF;
#else
				// Flip endianness
				data[offset] = value & 0x00FF;
				data[offset + 1] = (value & 0xFF00) >> 8;
#endif
			}

		public:

			PitData();
			~PitData();

			bool Unpack(const unsigned char *data);
			void Pack(unsigned char *data) const;

			bool Matches(const PitData *otherPitData) const;

			void Clear(void);

			PitEntry *GetEntry(unsigned int index);
			const PitEntry *GetEntry(unsigned int index) const;

			PitEntry *FindEntry(const char *partitionName);
			const PitEntry *FindEntry(const char *partitionName) const;

			PitEntry *FindEntry(unsigned int partitionIdentifier);
			const PitEntry *FindEntry(unsigned int partitionIdentifier) const;

			unsigned int GetEntryCount(void) const
			{
				return entryCount;
			}

			unsigned int GetDataSize(void) const
			{
				return PitData::kHeaderDataSize + entryCount * PitEntry::kDataSize;
			}

			unsigned int GetPaddedSize(void) const
			{
				unsigned int dataSize = GetDataSize();
				unsigned int paddedSize = (dataSize / kPaddedSizeMultiplicand) * kPaddedSizeMultiplicand;

				if (dataSize % kPaddedSizeMultiplicand != 0)
					paddedSize += kPaddedSizeMultiplicand;

				return paddedSize;
			}

			unsigned int GetUnknown1(void) const
			{
				return unknown1;
			}

			unsigned int GetUnknown2(void) const
			{
				return unknown2;
			}

			unsigned short GetUnknown3(void) const
			{
				return unknown3;
			}

			unsigned short GetUnknown4(void) const
			{
				return unknown4;
			}

			unsigned short GetUnknown5(void) const
			{
				return unknown5;
			}

			unsigned short GetUnknown6(void) const
			{
				return unknown6;
			}

			unsigned short GetUnknown7(void) const
			{
				return unknown7;
			}

			unsigned short GetUnknown8(void) const
			{
				return unknown8;
			}
	};
}

#endif