-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpmem.py
executable file
·907 lines (692 loc) · 26.5 KB
/
pmem.py
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
#!/usr/bin/env python3
# The MIT License (MIT)
#
# Copyright (c) 2016 Ivor Wanders
#
# 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.
"""
The data request command is utilized to retrieve the filesystem.
In the filesystem data:
0x9e1c0 Always indicates the start of the internal log of events.
0xffc40 Always indicates the start of the first log.
In the file data:
0x927c0 Always indicates the start of the internal log of events.
0xf4240 Always indicates the start of the first log.
0x186a0 Indicates some ASCII header of dates?
0x2008 - 0x4800 Unknown data, but contains b'GPS POD'
0x29810 Unknown
0x30d40 Localization data? .text?
0x61a80 Dense binary combined with string... perhaps firmware?
Contains parse / formats for strings at 0x62ed8, 0x65cc8
In the filesystem image the b'2I.L' marker is on 0xBE1C, in the file itself
this marker is at 0x41C -> Filesystem header is 47616 long?
In the fat table we find the following entry for the file:
E5 42 50 4D 45 4D 20 20 44 41 54 20 00 AD 6E 90 76 3F 76 3F 00 00 ...
file + extension | | ...
00 60 21 00 02 00 70 38 39 00
| |2 clu|filesize 3750000 bytes
If we mount the filesystem, such that we can look at the file itself and
the internal offsets in the file, instead of the filesystem offsets.
This makes everything a lot clearer:
We find on offset 0xf4240 (the position of the first GPS track):
52 42 0F 00 52 42 0F 00 01 00 00 00 FD 45 0F 00 00 A7 50 4D 45 4D 52 42 0F
^
this position is 0x0F4252! :D -> Start of log --------^
^ At this offset, the log ends 0x0F45FD
For the gps log we find at position 0xf4240 the following header:
52 42 0F 00 52 42 0F 00 01 00 00 00 FD 45 0F 00 00 A7 50 4D 45 4D 52 42 0F
^0x0F4252------offset pointing to -> ----------------^
0x0F45FD -> End of this log.
On 0x927c0 We find the internal log, with the following header:
45 32 09 00 D2 27 09 00 02 00 00 00 7B 36 09 00 01 D4 50 4D 45 4D 45 32 09
^ 0x927d2: begin log
^0x093245 points to the continuation? Or the prior?
At 0x93245 We find another part of the internal log?
50 4D 45 4D 45 32 09 00 D2 27 09 00 0C 00 02 00 00 00 00 DC 07 01 02 00 00
^ here is 0x93245, what the position of the 50 4D 45 is...
^ 0x927d2 is the start of the first log header...
50 4D 45 4D = b'PMEM' -> The identifier of the logs.
Ascii header at 0x186a0: day.month.year h:minutes uint32(0x00002976)
But we know for sure that the file starts at 0xba00 in the disk image.
"""
from .protocol import Readable, Dictionary
import ctypes
import inspect
from collections import namedtuple
import struct
import math
import traceback
"""
So, we have a filesystem, with one file, which has an offset from the
filesystem.
Then, we can discern (at least) two PMEM blocks, one contains internal
logs, the other the track logs.
Each PMEM block has a PMEMBlockHeader, which points to a
PMEMSubBlockHeader, these PMEMSubBlockHeader create a doubly linked list.
These Logheaders contain entries, which should be parsed differently for
the two PMEM Blocks.
PMEM entries are of varying length, with one byte denoting the length of
the next sample.
"""
FILESYSTEM_SIZE = 0x3c0000
# this is for creating more convoluted data types =)
class FieldEntry(ctypes.LittleEndianStructure, Readable, Dictionary):
scale = 1
ignore = None
unit = None
limits = None
@property
def value(self):
# works on self.field_
if (hasattr(self, "field_")):
# check if we ignore it.
if (self.field_ == self.ignore):
return None
else:
return None
# next we convert the value.
v = self.scale * self.field_
if (self.limits):
v = max(min(v, self.limits[1]), self.limits[0])
return v
def __iter__(self):
yield ("value", self.value)
yield ("unit", self.unit)
class Uint8ByteIgnored255Field(FieldEntry):
_fields_ = [("field_", ctypes.c_uint8), ]
scale = 1
ignore = 255
class Coordinate(FieldEntry):
_fields_ = [("field_", ctypes.c_int32), ]
scale = 1e-7
unit = "degrees"
class LatitudeField(Coordinate):
limits = (-90, 90)
key = "latitude"
class LongitudeField(Coordinate):
limits = (-180, 180)
key = "longitude"
class DistanceField(FieldEntry):
_fields_ = [("field_", ctypes.c_uint32), ]
scale = 1
unit = "m"
key = "distance"
class HeartRateField(Uint8ByteIgnored255Field):
unit = "bpm"
key = "heartrate"
class TimeField(FieldEntry):
_fields_ = [("field_", ctypes.c_int32), ]
scale = 1e-3
unit = "s"
key = "time"
class DurationField(FieldEntry):
_fields_ = [("field_", ctypes.c_int32), ]
scale = 0.1
unit = "s"
key = "duration"
class VerticalVelocityField(FieldEntry):
_fields_ = [("field_", ctypes.c_int16), ]
scale = 0.01
unit = "m/s"
key = "vertical_velocity"
class VelocityField(FieldEntry):
_fields_ = [("field_", ctypes.c_uint16), ]
scale = 0.01
ignore = 65535
unit = "m/s"
key = "speed"
class GPSSpeedField(VelocityField):
key = "gps_speed"
class WristAccSpeed(VelocityField):
key = "wristaccessory_speed"
class BikePodSpeedField(VelocityField):
key = "bikepod_speed"
class GPSHeadingField(FieldEntry):
key = "gps_heading"
_fields_ = [("field_", ctypes.c_uint16), ]
scale = 0.01 / 360.0 * 2 * math.pi
unit = "radians"
class UnsignedMeterField(FieldEntry):
_fields_ = [("field_", ctypes.c_uint32), ]
scale = 0.01
unit = "m"
class EHPEField(UnsignedMeterField):
key = "EHPE"
class EVPEField(UnsignedMeterField):
key = "EVPE"
class AltitudeField(FieldEntry):
_fields_ = [("field_", ctypes.c_int16), ]
scale = 0.01
unit = "m"
key = "altitude"
limits = (-1000, 15000)
class PressureField(FieldEntry):
scale = 0.1
unit = "hpa"
class AbsPressureField(PressureField):
_fields_ = [("field_", ctypes.c_uint16), ]
key = "absolute_pressure"
class TemperatureField(FieldEntry):
_fields_ = [("field_", ctypes.c_int16), ]
key = "temperature"
scale = 0.1
unit = "celsius"
limits = (-100, 100)
class BatteryChargeField(FieldEntry):
_fields_ = [("field_", ctypes.c_uint8), ]
key = "batterycharge"
unit = "%"
class GPSAltitudeField(UnsignedMeterField):
key = "gps_altitude"
limits = (-1000, 15000)
class GPSHDOPField(Uint8ByteIgnored255Field):
key = "gps_hdop"
class GPSVDOPField(Uint8ByteIgnored255Field):
key = "gps_vhdop"
class WristCadenceField(FieldEntry):
_fields_ = [("field_", ctypes.c_uint16), ]
ignore = 65535
unit = "rpm"
key = "wrist_cadence"
class GPSSNRField(FieldEntry):
_fields_ = [("field_", ctypes.c_uint8*16), ]
unit = ""
key = "snr"
class NumberOfSatellitesField(Uint8ByteIgnored255Field):
key = "gps_satellites"
class SeaLevelPressureField(PressureField):
_fields_ = [("field_", ctypes.c_int16), ]
key = "sealevel_pressure"
class CadenceField(Uint8ByteIgnored255Field):
key = "cadence"
class PeriodicStructure(ctypes.LittleEndianStructure, Readable, Dictionary):
_pack_ = 1
pass
sample_types = {
1: LatitudeField,
2: LongitudeField,
3: DistanceField,
4: VelocityField,
5: HeartRateField,
6: TimeField,
7: GPSSpeedField,
8: VelocityField,
9: BikePodSpeedField,
10: EHPEField,
11: EVPEField,
12: AltitudeField,
13: AltitudeField,
14: AbsPressureField,
15: TemperatureField,
16: BatteryChargeField,
17: GPSAltitudeField,
18: GPSHeadingField,
19: GPSHDOPField,
20: GPSVDOPField,
21: WristCadenceField,
22: GPSSNRField,
23: SeaLevelPressureField,
24: NumberOfSatellitesField,
25: VerticalVelocityField,
26: CadenceField,
}
class DataStructure(ctypes.LittleEndianStructure, Readable, Dictionary):
_pack_ = 1
pass
class GpsUserData(DataStructure):
_fields_ = [
("time", TimeField),
("latitude", LatitudeField),
("longitude", LongitudeField),
("gpsaltitude", ctypes.c_int16),
("gpsheading", GPSHeadingField),
("EHPE", ctypes.c_uint8),
]
class TimeBlock(DataStructure):
_fields_ = [
("year", ctypes.c_uint16),
("month", ctypes.c_uint8),
("day", ctypes.c_uint8),
("hour", ctypes.c_uint8),
("minute", ctypes.c_uint8),
("second", ctypes.c_uint8),
]
class TimeReference(DataStructure):
_fields_ = [
("local", TimeBlock), # order might be swapped
("UTC", TimeBlock)
]
class VelocityFieldKmH(FieldEntry):
_fields_ = [("field_", ctypes.c_uint16), ]
scale = 0.01 / 3.6
unit = "m/s"
class TrackHeader(DataStructure):
_fields_ = [("time", TimeBlock), # sure
("interval", ctypes.c_uint16),
("duration", DurationField), # sure
("_", ctypes.c_uint8*14), # ??
("velocity_avg", VelocityFieldKmH), # sure
("velocity_max", VelocityFieldKmH), # sure
("altitude_min", ctypes.c_int16),
("altitude_max", ctypes.c_int16),
("heartrate_avg", ctypes.c_uint8),
("heartrate_max", ctypes.c_uint8),
("_", ctypes.c_uint8),
("activity_type", ctypes.c_uint8), # sure
("activity_name", ctypes.c_char*16), # sure
("heartrate_min", ctypes.c_uint8),
("_", ctypes.c_uint8),
("_", ctypes.c_uint8),
("distance", DistanceField), # sure
("samples", ctypes.c_uint32), # sure
]
_anonymous_ = ["time"]
def __str__(self):
return "{year}-{month:0>2}-{day:0>2} {hour:0>2}:{minute:0>2}:{second:0>2} "\
"distance: {distancevalue: >5}m, samples: {samples: >6},"\
" interval: {interval: >1}s".format(
distancevalue=self.distance.value, **dict(self))
class VariableLengthField(DataStructure):
def __init__(self, blob):
# self.data = ctypes.c_uint8 * int(len(blob))
self.data = ctypes.create_string_buffer(len(blob))
@classmethod
def read(cls, byte_object):
a = cls(byte_object)
# print(byte_object)
ctypes.memmove(ctypes.addressof(a.data), bytes(byte_object),
min(len(byte_object), ctypes.sizeof(a.data)))
return a
@classmethod
def fixed_length(cls, clskey, length):
class foo(cls):
key = clskey
_fields_ = [("data", ctypes.c_uint8*length)]
return foo
def __iter__(self):
# print(dir(self))
# import code
# code.interact(local=locals())
yield ("raw", [a for a in self.data])
yield ("key", self.key)
class FallbackField(VariableLengthField):
key = "unknown_field"
class GPSTestField(FieldEntry):
_fields_ = [("field_", ctypes.c_uint8*16), ]
key = "gps_test"
class GPSDataField(VariableLengthField):
key = "gpsdata"
class GPSAccuracyField(VariableLengthField):
key = "accdata"
class LogPauseField(FieldEntry):
key = "logpause"
class LogRestartField(FieldEntry):
key = "logrestart"
class IBIDataField(FieldEntry):
_fields_ = [("field_", ctypes.c_uint8*64), ]
key = "ibidata"
class TTFFField(FieldEntry):
_fields_ = [("field_", ctypes.c_uint16), ]
scale = 0.1
unit = "s"
key = "ttff"
class DistanceSourceField(Uint8ByteIgnored255Field):
key = "distancesource"
class LapInfoField(DataStructure):
_fields_ = [("event_type", ctypes.c_uint8),
("time", TimeBlock),
("duration", DurationField),
("distance", DistanceField),
]
key = "lapinfo"
_anonymous_ = ["time"]
# event_type == 1 is manual (button is pressed)
class GPSTestData(FieldEntry):
_fields_ = [("field_", ctypes.c_uint8*61), ]
key = "gpstestdata"
episodic_types = {
1: GPSTestField,
2: GPSDataField,
3: GPSAccuracyField,
4: LogPauseField,
5: LogRestartField,
6: IBIDataField,
7: TTFFField,
8: DistanceSourceField,
9: LapInfoField,
10: GpsUserData,
11: GPSTestData,
12: TimeReference,
}
"""
episodic_lookup = {
1:Sample.partial(1, "GPS test", "16c"),
2:Sample.partial(2, "gpsdata", "x"), # length:?
3:Sample.partial(3, "accdata", "x"), # length:?
4:Sample.partial(4, "logpause", ""),
5:Sample.partial(5, "logrestart", ""),
6:Sample.partial(6, "ibidata", "64c"),
7:Sample.partial(7, "ttff", "H", "seconds", 0.1),
8:Sample.partial(8, "distancesource", "B", ignore=255),
9:Sample.partial(9, "lapinfo", "23c"),
# 10:Sample.partial(10, "gpsuserdata", "17c"),
10:lambda x: GpsUserData.read(x),
11:Sample.partial(11, "gpstestdata", "61c"),
# 12:Sample.partial(12, "timeref", "14c"),
12:lambda x: TimeReference.read(x),
}
SampleFallback = Sample.partial(0, "Unknown", "")
"""
class MEMfs():
# contains the entire filesystem.
def __init__(self, obj):
self.backend = obj
def __getitem__(self, key):
return self.backend[key]
class BPMEMfile():
# contains the BPMEM.DAT file: corrects offsets to align to FS.
offset = 0xba00
def __init__(self, fs):
self.logs = PMEMInternalLog(self, offset=0x927c0, size=0xFFFFFF)
# Unknown how long it really is...
self.tracks = PMEMTrack(self, offset=0xf4240, size=0x29f630)
# tracks run up to the end of the file in the FS.
self.fs = fs
def __getitem__(self, key):
# print("File: 0x{:0>6X} : 0x{:0>6X}".format(key.start, key.stop))
return self.fs[slice(key.start + self.offset, key.stop + self.offset,
key.step)]
"""
Every PMEMBlock contains several SubBlocks, these SubBlocks contain the
entries to the log.
"""
class PMEMBlockHeader(ctypes.LittleEndianStructure, Dictionary, Readable):
_pack_ = 1
_fields_ = [
("last", ctypes.c_uint32),
("first", ctypes.c_uint32),
("entries", ctypes.c_uint32),
("free", ctypes.c_uint32),
("pad", ctypes.c_uint16),
]
def __str__(self):
return "first: 0x{:0>6X}, last 0x{:0>6X}, entries: {:0>4d},"\
" free: 0x{:0>6X}, ({:0>4X})".format(self.first,
self.last,
self.entries,
self.free,
self.pad)
class PMEMSubBlockHeader(ctypes.LittleEndianStructure, Dictionary, Readable):
_pack_ = 1
_fields_ = [
("magic", ctypes.c_char*4),
("next", ctypes.c_uint32),
("prev", ctypes.c_uint32)
]
def __str__(self):
return "magic: {}, prev 0x{:0>6X} next 0x{:0>6X}".format(
str(self.magic), self.prev, self.next)
class PMEMBlock():
def __init__(self, file, offset, size):
self.file = file
self.offset = offset
self.size = size
self.data_start = self.offset + ctypes.sizeof(PMEMBlockHeader)
self.data_end = self.offset + size
self.logs = []
def load_block_header(self):
tmp = self.file[self.offset:(self.offset +
ctypes.sizeof(PMEMBlockHeader))]
self.header = PMEMBlockHeader.read(tmp)
def load_logs(self):
pos = self.header.first
for i in range(0, self.header.entries):
# print("Reading at: 0x{:0>6X}".format(current_position))
log_header = PMEMSubBlockHeader.read(
self.file[pos:pos+ctypes.sizeof(PMEMSubBlockHeader)])
if (log_header.next == pos):
pass
self.logs.append(self.pmem_type(
self, pos+ctypes.sizeof(PMEMSubBlockHeader), log_header))
pos = log_header.next
def __getitem__(self, key):
# handle wrapping... Just wrap it to after the own header.
def wrap(x, min=self.data_start, max=self.data_end):
return min + (x - min) % (max - min)
# We only deal with positive overflow; that is it running out of the
# size allocated to it.
# it does not both fit in the original AND overflow:
if ((key.stop > self.data_end) and (key.start < self.data_end)):
# have to get two parts..
p1 = self[slice(wrap(key.start), self.data_end, key.step)]
p2 = self[slice(self.data_start, wrap(key.stop), key.step)]
return p1 + p2
# Perform normal wrapping, without the wrap in the middle.
start_index = wrap(key.start)
stop_index = wrap(key.stop)
return self.file[slice(start_index, stop_index, key.step)]
class PMEMEntry(ctypes.LittleEndianStructure, Dictionary, Readable):
_pack_ = 1
_fields_ = [
("type", ctypes.c_uint8),
("next", ctypes.c_uint32),
("prev", ctypes.c_uint32)
]
class PMEMEntriesBlock():
def __init__(self, block, pos, log_header):
self.block = block
self.start_pos = pos
self.orig_pos = pos
self.pos = pos
self.log_header = log_header
self.entries = []
self.retrieved_entry_count = 0
def get_entry(self):
self.retrieved_entry_count += 1
# everything is an entry: headers are too!
length, = struct.unpack("<H", self.block[self.pos:self.pos+2])
self.pos += 2
data = self.block[self.pos:self.pos+length]
self.pos += length
return data
def peek_entry(self, pos):
# peek at the entry on position pos
length, = struct.unpack("<H", self.block[pos:pos+2])
if (length == 0):
return length, bytes([])
data = self.block[pos+2:pos+2+length]
return length, data
def parse(self, format, buffer, offset=0):
res = list(struct.unpack_from(">"+format, buffer, offset))
# print(res)
res.append(offset + struct.calcsize(format))
# print(res)
return res
def get_entries(self):
return self.entries
def rewind(self):
self.pos = self.orig_pos
class PMEMTrackEntries(PMEMEntriesBlock):
def get_header(self):
return self.header_metadata
def load_header(self):
self.rewind()
# self.periodic_structure = None
# should contain the periodic specification
self.header_samples = self.get_entry()
first_spec = self.process_entry(self.header_samples)
if (first_spec and inspect.isclass(first_spec) and
issubclass(first_spec, PeriodicStructure)):
self.periodic_structure = first_spec
# should contain all the metadata
self.header_metadata_bytes = self.get_entry()
second_spec = self.process_entry(self.header_metadata_bytes)
if (second_spec and isinstance(second_spec, TrackHeader)):
self.header_metadata = second_spec
if (self.header_metadata is None):
return False
if (isinstance(second_spec, PeriodicStructure)):
self.periodic_structure = second_spec
# No clue... \x03\x82\x00\x00\x00\x04
self.header_unknown1 = self.get_entry()
self.process_entry(self.header_unknown1)
# No clue... \x03\x83\x00\x00\x00\x05
self.header_unknown2 = self.get_entry()
self.process_entry(self.header_unknown2)
return True
def process_entry(self, entry_bytes):
pos = 0
entry_type, pos = self.parse("B", entry_bytes, pos)
# Defines the entries of the periodic type.
if (entry_type == 0):
pos = 0
periodic_entries = []
periodic_count, pos = self.parse("H", entry_bytes, pos)
# This probably CAN be a variable length array, it consists of:
#: \x04\x00 # count
# |type |offset |length of type 2 samples.
#: \x19\x00\x00\x00\x02\x00
#: \x03\x00\x02\x00\x04\x00
#: \x04\x00\x06\x00\x02\x00
#: \x06\x00\x08\x00\x04\x00
field_list = []
anonymous_fields = []
for p in range(periodic_count):
type, offset, length, pos = self.parse("HHH", entry_bytes, pos)
periodic_entries.append((type, offset, length))
if (type in sample_types):
fieldtype = sample_types[type]
field_list.append((fieldtype.key, fieldtype))
# anonymous_fields.append(fieldtype.key)
else:
key = "field_type_0x{:0>2X}".format(type)
field_list.append((key, VariableLengthField.fixed_length(
clskey=key, length=length)))
# next, we craft our periodicStructure:
class SpecifiedPeriodicStructure(PeriodicStructure):
_fields_ = field_list
# _anonymous_ = anonymous_fields
return SpecifiedPeriodicStructure
if (entry_type == 1):
header_metadata = TrackHeader.read(entry_bytes[pos:])
return header_metadata
# print("Found header: {}".format(self.header_metadata))
# is periodic sample.
if (entry_type == 2):
samples = []
# parse it according to the periodic entries.
if (self.periodic_structure):
return self.periodic_structure.read(entry_bytes[pos:])
else:
print("Should have periodic structure...")
# Episodic type
if (entry_type == 3):
timestamp, pos = self.parse("I", entry_bytes, pos)
episode_type, pos = self.parse("B", entry_bytes, pos)
sample = episodic_types.get(episode_type, FallbackField)
processed = sample.read(entry_bytes[pos:])
return processed
def load_entries(self):
if (self.header_metadata is not None):
for i in range(self.header_metadata.samples -
self.retrieved_entry_count):
try:
processed = self.process_entry(self.get_entry())
except struct.error as e:
print("\nFAILED processing at sample {}: {}".format(
i, str(e)))
print(traceback.format_exc(), end="")
print("Attempting to continue!\n")
if processed:
self.entries.append(processed)
class InternalLogEntry:
def __init__(self, mtype, header, time, identifier, text):
self.header = header
self.type = mtype
self.time = time
self.identifier = identifier
self.text = text
def __str__(self):
return "t: {: >10d}, (0x{:0>8X}), {}".format(
self.time, self.identifier, self.text)
class PMEMLogEntries(PMEMEntriesBlock):
def load_header(self):
self.header_bytes = self.get_entry()
self.header = TimeBlock.read(self.header_bytes[5:])
return True
def process_entry(self, entry_bytes):
pos = 0
entry_type, pos = self.parse("B", entry_bytes, pos)
if (entry_type == 5):
# Seriously, what's up with the change in endianness for timestamp?
timestamp, = struct.unpack("<I", entry_bytes[pos:pos+4])
identifier, pos = self.parse("Ix", entry_bytes, pos+4)
try:
text = entry_bytes[pos:].decode('ascii')
except UnicodeDecodeError:
text = str(entry_bytes[pos:])
entry = InternalLogEntry(entry_type,
self.header, timestamp, identifier, text)
return entry
if (entry_type == 3):
text = " ".join(["{:0>2X}".format(b) for b in entry_bytes[1:]])
entry = InternalLogEntry(entry_type, self.header, 0, 0, text)
return entry
def load_entries(self, max=1000000):
# probably for the best to take some limit on this...
for i in range(max):
data = self.get_entry()
if (not data):
break
processed = self.process_entry(data)
# print(processed)
self.entries.append(processed)
class PMEMTrack(PMEMBlock):
pmem_type = PMEMTrackEntries
class PMEMInternalLog(PMEMBlock):
pmem_type = PMEMLogEntries
if __name__ == "__main__":
import sys
with open(sys.argv[1], 'rb') as f:
fs_data = f.read()
m = MEMfs(fs_data)
data = BPMEMfile(m)
print("Tracks:")
data.tracks.load_block_header()
data.tracks.load_logs()
print(data.tracks.logs)
for track in data.tracks.logs:
if not track.load_header():
continue
track.load_entries()
for track in data.tracks.logs:
print("\n\nNew track {}".format(track.header_metadata))
samples = track.get_entries()
for sample in samples[:15]:
print(sample)
sys.exit()
data.log.load_block_header()
data.log.load_logs()
for log in data.log.logs:
if not log.load_header():
continue
log.load_entries()
samples = log.get_entries()
for sample in samples[:15]:
print(sample)