-
Notifications
You must be signed in to change notification settings - Fork 2
/
yabit.py
executable file
·1240 lines (948 loc) · 28.7 KB
/
yabit.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
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# yabit - Yet Another BootImage Tool
# Copyright (C) 2018 Eugenio "g7" Paolantonio
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the <organization> nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
yabit is a python written, device tree-aware tool to create, extract and
update Android BootImages.
"""
import enum
import struct
import argparse
import sys
import os
import io
import hashlib
import traceback
import logging
from collections import namedtuple, OrderedDict
DEFAULT_BASE = 0x10000000
DEFAULT_KERNEL_OFFSET = 0x00008000
DEFAULT_INITRAMFS_OFFSET = 0x01000000
DEFAULT_SECOND_IMAGE_OFFSET = 0x00f00000
DEFAULT_TAGS_OFFSET = 0x00000100
DEFAULT_PAGE_SIZE = 2048
PAGES_TO_READ = 4
# Set-up logging
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s: %(message)s"))
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
class ParseResult(namedtuple("_ParseResult", ["status", "start", "end"])):
def __new__(cls, *args, **kwargs):
kwargs.setdefault("start", 0)
kwargs.setdefault("end", 0)
return super().__new__(cls, *args, **kwargs)
class StopReason(enum.IntEnum):
STRUCT_END = 0x01
WORD = 0x02
END = 0x04
SIZE = 0x08
class ParseStatus(enum.IntEnum):
FOUND = 1
NOT_FOUND = 2
MAGIC_WORD_FOUND = 3
class BlockType(enum.IntEnum):
STRUCT = 1
NORMAL = 2
class DumpAction(enum.Enum):
NOTHING = "nothing"
EVERYTHING = "everything"
HEADER = "header"
KERNEL = "kernel"
INITRAMFS = "initramfs"
DTBS = "dtbs"
#SECOND_IMAGE = "second_image"
def __str__(self):
"""
Required for argparse
"""
return self.value
FakeStructEnum = namedtuple("FakeStructEnum", ["name", "value"])
class StructEnum(enum.Enum):
PAD = "x"
CHAR = "c" # 1
SIGNED_CHAR = "b" # 1
UNSIGNED_CHAR = "B" # 1
BOOLEAN = "?" # 1
SHORT = "h" # 2
UNSIGNED_SHORT = "H" # 2
INTEGER = "i" # 4
UNSIGNED_INTEGER = "I" # 4
LONG = "l" # 4
UNSIGNED_LONG = "L" # 4
LONG_LONG = "q" # 8
UNSIGNED_LONG_LONG = "Q" # 8
SSIZE_T = "n"
SIZE_T = "N"
FLOAT = "f" # 4
DOUBLE = "d" # 8
STRING = "s"
PASCAL_STRING = "p"
def __mul__(self, factor):
"""
Handles multiplications.
"""
return FakeStructEnum(
name=self.name,
value="%d%s" % (factor, self.value)
)
class BaseBlock:
"""
A searchable block of bytes defined by a magic word.
"""
MAGIC_WORD = None
PARAMS = {
"content" : None
}
DEFAULTS = {}
BLOCK_TYPE = BlockType.NORMAL
CONTENT_PARAM = "content"
STOP_REASON = StopReason.END
STOP_WORD = b""
def __init__(self, page_size=DEFAULT_PAGE_SIZE):
"""
Initializes the class.
:param: page_size: the page size to use (defaults to DEFAULT_PAGE_SIZE)
"""
self.page_size = page_size
self.content = {}
self.content.update(self.DEFAULTS)
self.found = False
@property
def size(self):
"""
Returns the full size of the block.
"""
if self.BLOCK_TYPE == BlockType.NORMAL and self.CONTENT_PARAM in self.content:
return len(self.content[self.CONTENT_PARAM])
return 0
def __getitem__(self, item):
"""
Returns the item from the content dictionary, or padding bytes
if it doesn't exist.
"""
if item in self.content:
return self.content[item]
if not item in self.PARAMS:
raise AttributeError("%s not in PARAMS" % item)
return None
def __contains__(self, item):
"""
Forwards the query to the inner content dictionary.
"""
return self.content.__contains__(item)
def parse(self, chunk, is_end, current_size=0):
"""
Parses a chunk of bytes.
:param: chunk: the chunk to parse
:param: last_chunk: the last chunk, for context (or b"")
:param: is_end: True if the file reached its end, False if not
:returns: if successful, returns the bytes to seek to position
the cursor just at the end of the block. In case of failure, it
returns -1.
"""
logger.debug("Current size is %d" % current_size)
if current_size >= self.size and self.STOP_REASON & StopReason.SIZE:
logger.debug("Handling StopReason.SIZE")
return ParseResult(status=ParseStatus.FOUND, end=(len(chunk) - (current_size - self.size)))
elif not is_end and self.STOP_REASON & StopReason.WORD:
logger.debug("Handling StopReason.WORD")
if self.MAGIC_WORD is not None:
magic_start = chunk.find(self.MAGIC_WORD, 0)
magic_end = chunk.find(self.STOP_WORD, 1 if self.STOP_WORD == self.MAGIC_WORD else 0)
if magic_start >= 0 and (magic_end - magic_start) > 0:
# We both have a start and an end
self.content["content"] = chunk[magic_start:magic_end]
# Position ourselves at the end of the block
return ParseResult(status=ParseStatus.FOUND, start=magic_start, end=magic_end)
elif magic_start >= 0:
# Got a start
return ParseResult(status=ParseStatus.MAGIC_WORD_FOUND, start=magic_start)
else:
# Assume STOP_WORD is defined
magic_end = chunk.find(self.STOP_WORD, 0)
if magic_end >= 0:
# Got it!
return ParseResult(status=ParseStatus.FOUND, end=magic_end)
elif is_end and self.STOP_REASON & StopReason.END:
# We reached the end, so we possibly got the whole
# block
logger.debug("Handling StopReason.END")
magic_start = chunk.find(self.MAGIC_WORD) if self.MAGIC_WORD is not None else 0
if magic_start >= 0:
return ParseResult(status=ParseStatus.FOUND, start=magic_start, end=len(chunk))
return ParseResult(status=ParseStatus.NOT_FOUND)
def set(self, chunk, result, base=0):
"""
Reads from the given chunk and sets the content accordingly.
:param: chunk: the chunk to read
:param: result: a ParseResult object.
:param: base: the base where results' indications start at
"""
self.content[self.CONTENT_PARAM] = chunk[result.start + base:result.end + base]
def analyse(self, file_obj):
# Analyse iterates through the file object and for every chunk
# calls the class' parse() method.
#
# Depending on the result of the method, the following happens:
# * If the status is MAGIC_WORD_FOUND, every chunk is appended
# to the cached_content until FOUND happens
# * If the status is NOT_FOUND, the chunk is cached so that can
# be passed to parse() along with the next one
# * If the status is FOUND, set() is called, which sets the
# content inside the instance. Then the file object is seeked
# to the reported end of the block.
#
# If SIZE is among the StopReasons, every chunk is cached in a
# special variable, `full_content`.
begin = file_obj.tell()
logger.debug("Read begins at %d" % begin)
# If the only STOP_REASON is SIZE, then proceed in reading
# the whole block.
if self.STOP_REASON == StopReason.SIZE:
# FIXME: read in chunks
logger.debug("Avoid calling parse() as the only reason is SIZE")
self.set(
file_obj.read(self.size),
ParseResult(
status=ParseStatus.FOUND,
end=self.size
)
)
return
with_full_content = ((self.STOP_REASON & StopReason.SIZE) > 0)
cached_content = b""
cached_content_begin = 0
full_content = b""
while True:
chunk = file_obj.read(self.page_size * PAGES_TO_READ)
chunk_with_cache = cached_content + chunk
current_size = (file_obj.tell() - begin)
is_end = (chunk == b"")
result = self.parse(chunk_with_cache, is_end, current_size=current_size)
logger.debug("Got ParseResult %s" % (result,))
if not is_end and with_full_content:
# Append to the full cache
full_content += chunk
if result.status == ParseStatus.FOUND:
# Found! Set, seek and return
if with_full_content:
# FIXME
result = ParseResult(status=result.status, start=0, end=cached_content_begin+result.end)
self.set(full_content, result, base=0)
else:
self.set(chunk_with_cache, result)
boundary = begin + result.end
logger.debug("Seeking to %d, begin is %d, result.end is %d" % (boundary, begin, result.end))
file_obj.seek(boundary)
self.found = True
return
elif result.status == ParseStatus.MAGIC_WORD_FOUND:
# Begin caching every chunk
cached_content = chunk_with_cache
elif result.status == ParseStatus.NOT_FOUND:
# Cache this page
cached_content = chunk
cached_content_begin = current_size - len(chunk)
if is_end:
# If this is the end and we are here, nothing has been
# found.
return
def dump(self):
"""
Dumps the block.
:returns: the block
"""
return self[self.CONTENT_PARAM]
def write(self, file_obj):
"""
Writes the block to an already-opened file object.
:param: file_obj: the file_obj that we should write on.
"""
file_obj.write(self.dump())
def __repr__(self):
"""
Returns a string representation of the object.
:returns: a string containing an helpful representation of
the object.
"""
return "<%s: size %d bytes>" % (self.__class__.__name__, self.size)
class StructBlock(BaseBlock):
STOP_REASON = StopReason.STRUCT_END
def __init__(self, page_size=DEFAULT_PAGE_SIZE):
"""
Initialises the class.
:param: page_size: the page size to use (defaults to DEFAULT_PAGE_SIZE)
"""
super().__init__(page_size=page_size)
self.__params_without_padding = [
x
for x, y in self.PARAMS.items()
if not y.name == "PAD"
]
self.__cached_format = None
self.__cached_size = None
@property
def format(self):
"""
Returns the full format of the block.
"""
if not self.__cached_format:
self.__cached_format = "".join(
[
obj.value
for name, obj in self.PARAMS.items()
]
)
return self.__cached_format
@property
def size(self):
"""
Returns the full size of the block.
"""
if not self.__cached_size:
self.__cached_size = struct.calcsize(self.format)
return self.__cached_size
def __getitem__(self, item):
"""
Returns the item from the content dictionary, or padding bytes
if it doesn't exist.
"""
if not self.content.get(item, None) is None:
return self.content[item]
if not item in self.PARAMS:
raise AttributeError("%s not in PARAMS" % item)
return b"\x00" * struct.calcsize(self.PARAMS[item].value)
def __setitem__(self, item, value):
self.content[item] = value
def parse(self, chunk, is_end, current_size=None):
"""
Parses a chunk of bytes.
:param: chunk: the chunk to parse
:param: is_end: True if the file reached its end, False if not
:returns: if successful, returns the bytes to seek to position
the cursor just at the end of the block. In case of failure, it
returns -1.
"""
if not is_end and self.STOP_REASON == StopReason.STRUCT_END:
magic_start = chunk.find(self.MAGIC_WORD)
if magic_start >= 0:
# Found the magic word!
end = magic_start + self.size
if end < len(chunk):
# Everything is into the boundaries of what we've read
return ParseResult(status=ParseStatus.FOUND, start=magic_start, end=end)
else:
return ParseResult(status=ParseStatus.MAGIC_WORD_FOUND)
return ParseResult(status=ParseStatus.NOT_FOUND)
def set(self, chunk, result, base=0):
"""
Reads from the given chunk and sets the content accordingly.
:param: chunk: the chunk to read
:param: result: a ParseResult object.
:param: base: the base where results' indications start at
"""
for attr_name, value in zip(
self.__params_without_padding,
struct.unpack(self.format, chunk[result.start + base:result.end + base])
):
self.content[attr_name] = value
def dump(self):
result = b""
for attr_name, attr_format in self.PARAMS.items():
if attr_format.name == "PAD":
result += struct.pack(attr_format.value)
else:
result += struct.pack(attr_format.value, self[attr_name])
return result
class DelimitedBlock(BaseBlock):
"""
A block with a known size.
"""
CONTENT_PARAM = "content"
STOP_REASON = StopReason.SIZE
PARAMS = {
"content": None
}
def __init__(self, size, page_size=DEFAULT_PAGE_SIZE):
"""
Initialises the class.
:param: size: the size of the block
"""
super().__init__(page_size=page_size)
self.__cached_size = None
if size is not None:
# Some childs (HeaderizedBlock) pick the size via other
# means
self.size = size
@property
def size(self):
if self.__cached_size is not None:
return self.__cached_size
return 0
@size.setter
def size(self, new_size):
"""
Sets the new size.
"""
self.__cached_size = new_size
self.PARAMS[self.CONTENT_PARAM] = StructEnum.PAD * new_size
class Padding(BaseBlock):
"""
Padding.
"""
def get_remaining(self, file_obj):
"""
Returns the remaining bytes to write in order to properly pad
the section.
:param: file_obj: the opened file object
:returns: the number of bytes to write
"""
mod = file_obj.tell() % self.page_size
if mod > 0:
return self.page_size - mod
else:
return 0
def analyse(self, file_obj):
"""
Analyses the supplied file object.
"""
remaining = self.get_remaining(file_obj)
logger.debug("Padding: skipping %d bytes" % remaining)
file_obj.seek(
file_obj.tell() + remaining
)
def dump(self):
"""
Useless here.
"""
raise Exception("Padding.dump() is unsupported. Use write() instead.")
def write(self, file_obj):
"""
Writes the padding to the file.
:param: file_obj: the file where the padding should be written
to
"""
remaining = self.get_remaining(file_obj)
logger.debug("Padding: writing %d bytes" % remaining)
file_obj.write(b"\x00" * remaining)
class Header(StructBlock):
"""
The boot.img header. This is composed as follows:
___________________________
| Magic Word |
| Kernel Size |
| Kernel load address |
| Initramfs Size |
| Initramfs load address |
| Second image size |
| Second image load address |
| Kernel tags (?) address |
| Page size |
| null |
| Product name |
| cmdline |
| IMG ID |
-----------------------------
"""
MAGIC_WORD = b"ANDROID!"
PARAMS = OrderedDict(
[
("magic_word", StructEnum.STRING * 8),
("kernel_size", StructEnum.UNSIGNED_INTEGER),
("kernel_load_address", StructEnum.UNSIGNED_INTEGER),
("initramfs_size", StructEnum.UNSIGNED_INTEGER),
("initramfs_load_address", StructEnum.UNSIGNED_INTEGER),
("second_image_size", StructEnum.UNSIGNED_INTEGER),
("second_image_load_address", StructEnum.UNSIGNED_INTEGER),
("kernel_tags_load_address", StructEnum.UNSIGNED_INTEGER),
("page_size", StructEnum.UNSIGNED_INTEGER),
("pad", StructEnum.PAD * 8),
("product_name", StructEnum.STRING * 16),
("cmdline", StructEnum.STRING * 512),
("img_id", StructEnum.STRING * 32),
("cmdline_extra", StructEnum.STRING * 1024)
]
)
DEFAULTS = dict(
[
("magic_word", MAGIC_WORD),
("kernel_size", 0),
("initramfs_size", 0),
("second_image_size", 0), # TODO: support second image
("page_size", DEFAULT_PAGE_SIZE)
]
)
@property
def page_size(self):
"""
Hack that allows to get the embedded page_size of the header,
if available.
"""
if "page_size" in self.content:
return self.content["page_size"]
return DEFAULT_PAGE_SIZE
@page_size.setter
def page_size(self, value):
"""
Sets the page size in the content too.
"""
if not hasattr(self, "content"):
self.content = {}
self.content.update(self.DEFAULTS)
self.content["page_size"] = value
def __setitem__(self, item, value):
"""
Override to properly handle cmdline.
"""
if item == "cmdline_extra":
raise Exception("Please do not manually set cmdline_extra, use cmdline instead.")
elif item != "cmdline":
return super().__setitem__(item, value)
value_length = len(value)
if value_length > 512+1024: # cmdline + cmdline_extra
raise Exception("Value too long")
elif value_length <= 512:
# Fits in cmdline
self.content["cmdline"] = value
self.content["cmdline_extra"] = None
else:
# Should use cmdline_extra
self.content["cmdline"] = value[:512]
self.content["cmdline_extra"] = value[512:]
class HeaderizedBlock(DelimitedBlock):
"""
A delimited block with support for getting sizes from an Header
block.
"""
# Change this to the field of the header to lookup when
# looking at the block size, e.g. "kernel_size".
SIZE_FIELD = None
def __init__(self, header, page_size=None):
"""
Initialises the class.
:param: header: an Header() object
:param: page_size: the page size to use, or None (default).
If None is specified, the page_size is taken from the Header.
constant.
"""
super().__init__(None, page_size=page_size or header.page_size)
self.header = header
@property
def size(self):
"""
Returns the size of the block by looking at the header.
:returns: the block size.
"""
return self.header[self.SIZE_FIELD]
@size.setter
def size(self, value):
"""
Sets the block size.
:param: value: the size to set
"""
self.header[self.SIZE_FIELD] = value
class DeviceTree(BaseBlock):
"""
A Device Tree (DTB).
"""
MAGIC_WORD = b"\xd0\x0d\xfe\xed"
STOP_REASON = StopReason.WORD | StopReason.END
STOP_WORD = MAGIC_WORD
PARAMS = {
"content": StructEnum.PAD
}
class Kernel(HeaderizedBlock):
"""
The Kernel image
"""
CONTENT_PARAM = "kernel"
STOP_REASON = StopReason.WORD | StopReason.SIZE
STOP_WORD = DeviceTree.MAGIC_WORD
PARAMS = {
"kernel" : None,
"dtbs" : []
}
SIZE_FIELD = "kernel_size"
def __init__(self, *args, **kwargs):
"""
Initialises the class.
:param: *args: the args to pass on to the parent constructor
:param: **kwargs: the kwargs to pass on to the parent constructor
"""
super().__init__(*args, **kwargs)
self._kernel_real_size = None
def analyse(self, file_obj):
"""
Analyses the object.
"""
# Start by getting the innerkernel
starts_at = file_obj.tell()
inner_kernel = InnerKernel(self.size)
inner_kernel.analyse(file_obj)
# Set the found kernel...
self.content["kernel"] = inner_kernel["kernel"]
self.content["dtbs"] = []
# When we get here, there are two situations:
# The first is were no DTBs were found so we are at the
# boundary of what we can read;
# The second is that a DTB is found so we need to keep digging
ends_at = file_obj.tell()
logger.debug("Kernel: ends at %d" % ends_at)
# Update _kernel_real_size
self._kernel_real_size = ends_at - starts_at
logger.debug(
"Kernel: starts at %d, real size is %d, size with DTBs is %d" % (
starts_at,
self._kernel_real_size,
self.size
)
)
if self.size - self._kernel_real_size > 0:
# We should hunt for DTBs...
# Create a BytesIO object with the rest of the kernel image.
# We don't care about chunking as it's going to be a few KBs
# anyways
logger.debug("Kernel: Searching for DTBs...")
rest = file_obj.read(self.size - self._kernel_real_size)
with io.BytesIO(rest) as f:
while True:
devicetree = DeviceTree()
devicetree.analyse(f)
if devicetree["content"] is not None:
logger.debug("Kernel: DTB found")
self.content.setdefault("dtbs", []).append(devicetree)
else:
# End of the story
break
else:
logger.debug("Kernel: Skipping searching for DTBs as this kernel doesn't have any")
def update_size(self):
"""
Updates the kernel size in the Header with the correct kernel
size + DTBs.
"""
if self._kernel_real_size is None:
# analyse() hasn't been called
return
# Update sizes
self.size = self._kernel_real_size + sum((len(x["content"]) for x in self.content["dtbs"]))
def dump(self):
"""
Dumps kernel + DeviceTree
"""
return self[self.CONTENT_PARAM] + b"".join((x.dump() for x in self.content.get("dtbs", [])))
class InnerKernel(DelimitedBlock):
"""
The actual kernel image.
"""
CONTENT_PARAM = "kernel"
STOP_REASON = StopReason.WORD | StopReason.SIZE
STOP_WORD = DeviceTree.MAGIC_WORD
PARAMS = {
"kernel" : None,
}
class Initramfs(HeaderizedBlock):
"""
The initramfs image
"""
CONTENT_PARAM = "initramfs"
PARAMS = {
"initramfs" : None,
}
SIZE_FIELD = "initramfs_size"
class BootImgContext:
"""
A boot.img context
"""
def __init__(self, image=None, page_size=DEFAULT_PAGE_SIZE):
"""
Initialises the class.
"""
self.image = image
self.page_size = page_size
# Image structure
self.header = Header()
self.kernel = Kernel(self.header)
self.initramfs = Initramfs(self.header)
self.padding = Padding()
# TODO: second image support
@property
def blocks(self):
"""
Returns the blocks.
"""
for x in [
self.header,
self.padding,
self.kernel,
self.padding,
self.initramfs,
self.padding
]:
yield x
def load(self):
"""
Loads the image, if specified.
"""
if self.image is None:
# Nothing to load
return
with open(self.image, "rb") as f:
for block in self.blocks:
logger.debug("Loading block %s" % block)
# This allows the pagesize to be adjusted after reading
# the header
block.page_size = self.header.page_size
block.analyse(f)
def dump_to(self, to, what=[DumpAction.EVERYTHING]):
"""
Dumps the components of the image in the specified directory.
:param: to: the directory where to store the files
:param: what: a list of DumpActions. Defaults to [DumpAction.EVERYTHING]
"""
# TODO:
action_map = {
DumpAction.HEADER : self.header,
DumpAction.KERNEL : self.kernel,
DumpAction.INITRAMFS : self.initramfs,
DumpAction.DTBS : lambda x: None,
}
if not os.path.exists(to):
os.makedirs(to)
if DumpAction.EVERYTHING in what:
what = [x for x in DumpAction.__members__.values() if x not in (DumpAction.EVERYTHING, DumpAction.NOTHING)]