Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions Image_to_bundle.py

This file was deleted.

10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ endif
ARCH = $(shell uname -m)
OS = $(shell uname -s)

BUNDLE_SCRIPT_x86_64 = vmlinux_to_bundle.py
BUNDLE_SCRIPT_aarch64 = Image_to_bundle.py
KBUNDLE_TYPE_x86_64 = vmlinux
KBUNDLE_TYPE_aarch64 = Image

KERNEL_BINARY_x86_64 = $(KERNEL_SOURCES)/vmlinux
KERNEL_BINARY_aarch64 = $(KERNEL_SOURCES)/arch/arm64/boot/Image
Expand Down Expand Up @@ -70,16 +70,16 @@ $(KERNEL_BINARY_$(ARCH)): $(KERNEL_SOURCES)

$(KERNEL_C_BUNDLE): $(KERNEL_BINARY_$(ARCH))
@echo "Generating $(KERNEL_C_BUNDLE) from $(KERNEL_BINARY_$(ARCH))..."
@python3 $(BUNDLE_SCRIPT_$(ARCH)) $(KERNEL_BINARY_$(ARCH))
@python3 bin2cbundle.py -t $(KBUNDLE_TYPE_$(ARCH)) $(KERNEL_BINARY_$(ARCH)) kernel.c

ifeq ($(SEV),1)
$(QBOOT_C_BUNDLE): $(QBOOT_BINARY)
@echo "Generating $(QBOOT_C_BUNDLE) from $(QBOOT_BINARY)..."
@python3 qboot_to_bundle.py $(QBOOT_BINARY)
@python3 bin2cbundle.py -t qboot $(QBOOT_BINARY) qboot.c

$(INITRD_C_BUNDLE): $(INITRD_BINARY)
@echo "Generating $(INITRD_C_BUNDLE) from $(INITRD_BINARY)..."
@python3 initrd_to_bundle.py $(INITRD_BINARY)
@python3 bin2cbundle.py -t initrd $(INITRD_BINARY) initrd.c
endif

$(KRUNFW_BINARY_$(OS)): $(KERNEL_C_BUNDLE) $(QBOOT_C_BUNDLE) $(INITRD_C_BUNDLE)
Expand Down
173 changes: 173 additions & 0 deletions bin2cbundle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
try:
from elftools.elf.elffile import ELFFile
except:
pass
import argparse
import sys

# Use Apple Silicon's page size for rounding.
PAGE_SIZE = 16384
AARCH64_LOAD_ADDR = '0x80000000'

def write_header(ofile, bundle_name):
ofile.write('#include <stddef.h>\n')
ofile.write('__attribute__ ((aligned (4096))) char {}_BUNDLE[] = \n"'.format(bundle_name))


def write_padding(ofile, padding, col):
while padding > 0:
ofile.write('\\x0')

if col == 15:
ofile.write('"\n"')
col = 0
else:
col = col + 1

padding = padding - 1


def write_elf_cbundle(ifile, ofile) -> int:
elffile = ELFFile(ifile)
entry = elffile['e_entry']

load_segments = [ ]
for segment in elffile.iter_segments():
if segment['p_type'] == 'PT_LOAD':
load_segments.append(segment)

col = 0
total_size = 0
prev_paddr = None
load_addr = elffile['e_entry']

for segment in load_segments:
if prev_paddr != None:
padding = segment['p_paddr'] - prev_paddr - prev_filesz
write_padding(ofile, padding, col)
total_size = total_size + padding

assert((segment['p_paddr'] - load_addr) == total_size)

for byte in segment.data():
ofile.write('\\x{:x}'.format(byte))

if col == 15:
ofile.write('"\n"')
col = 0
else:
col = col + 1

prev_paddr = segment['p_paddr']
prev_filesz = segment['p_filesz']
total_size = total_size + prev_filesz

rounded_size = int((total_size + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE
padding = rounded_size - total_size
write_padding(ofile, padding, col)

return load_addr


def write_raw_cbundle(ifile, ofile) -> int:
col = 0
total_size = 0
byte = ifile.read(1)
while byte:
ofile.write('\\x{:x}'.format(byte[0]))

if col == 15:
ofile.write('"\n"')
col = 0
else:
col = col + 1

total_size = total_size + 1
byte = ifile.read(1)

rounded_size = int((total_size + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE
padding = rounded_size - total_size
write_padding(ofile, padding, col)


def write_footer_generic(ofile, bundle_name):
footer = """
char * krunfw_get_{}(size_t *size)
{{
*size = sizeof({}_BUNDLE) - 1;
return &{}_BUNDLE[0];
}}
"""
ofile.write('";\n')
ofile.write(footer.format(bundle_name.lower(), bundle_name, bundle_name))


def write_footer_kernel(ofile, load_addr):
footer = """
char * krunfw_get_kernel(size_t *load_addr, size_t *size)
{{
*load_addr = {};
*size = sizeof(KERNEL_BUNDLE) - 1;
return &KERNEL_BUNDLE[0];
}}

int krunfw_get_version()
{{
return ABI_VERSION;
}}
"""
ofile.write('";\n')
ofile.write(footer.format(load_addr))


def main() -> int:
parser = argparse.ArgumentParser(description='Generate C blob from a binary')

parser.add_argument('input_file', type=str,
help='Input file')
parser.add_argument('output_file', type=str,
help='Output file')
parser.add_argument('-t', type=str, help='Bundle type (vmlinux, Image, qboot, initrd)')

args = parser.parse_args()

bundle_name = None
ifmt = None
if args.t == 'vmlinux':
bundle_name = 'KERNEL'
ifmt = 'elf'
elif args.t == 'Image':
bundle_name = 'KERNEL'
ifmt = 'raw'
elif args.t == 'qboot':
bundle_name = 'QBOOT'
ifmt = 'raw'
elif args.t == 'initrd':
bundle_name = 'INITRD'
ifmt = 'raw'
else:
print('Invalid bundle type')
return -1

ifile = open(args.input_file, 'rb')
ofile = open(args.output_file, 'w')

write_header(ofile, bundle_name)

if ifmt == 'elf':
load_addr = write_elf_cbundle(ifile, ofile)
elif ifmt == 'raw':
write_raw_cbundle(ifile, ofile)

if bundle_name == 'KERNEL':
if ifmt == 'raw':
load_addr = AARCH64_LOAD_ADDR;
write_footer_kernel(ofile, load_addr)
else:
write_footer_generic(ofile, bundle_name)

return 0


if __name__ == '__main__':
sys.exit(main())
42 changes: 0 additions & 42 deletions initrd_to_bundle.py

This file was deleted.

42 changes: 0 additions & 42 deletions qboot_to_bundle.py

This file was deleted.

Loading