#!/usr/bin/env python import sys, os from struct import pack as pk, unpack as up def atmosphere_target_firmware(major, minor, micro, rev = 0): return (major << 24) | (minor << 16) | (micro << 8) | rev def align_up(val, algn): val += algn - 1 return val - (val % algn) def find_rela(kernel, dynamic): rela_offset, rela_size = (None, None) while True: dyn_type, dyn_val = up('= bss_start) bss_size = bss_end - bss_start assert (bss_end == kernel_end) assert (kernel_end <= len(kernel)) rela_offset, rela_size = find_rela(kernel, dynamic) assert (rela_size == len(kernel) - kernel_end) assert (bss_start <= rela_offset and rela_offset + rela_size <= bss_end) assert (kernel[bss_start:bss_end] == (b'\x00'* bss_size)) kernel = kernel[:rela_offset] + kernel[bss_end:] + (b'\x00' * (bss_size - (rela_size + (rela_offset - bss_start)))) assert (kernel_end == len(kernel)) embedded_ini = b'' try: with open('ini.bin', 'rb') as f: embedded_ini = f.read() except: pass embedded_ini_offset = align_up(kernel_end, 0x1000) embedded_ini_end = embedded_ini_offset + len(embedded_ini) # TODO: Create and embed an INI, eventually. kernel_ldr_offset = align_up(embedded_ini_end, 0x1000) + (0x1000 if len(embedded_ini) == 0 else 0) kernel_ldr_end = kernel_ldr_offset + len(kernel_ldr) mesosphere_end = align_up(kernel_ldr_end, 0x1000) with open(argv[3], 'wb') as f: f.write(kernel[:kernel_metadata_offset + 4]) f.write(pk('