Skip to content

Instantly share code, notes, and snippets.

@DragoonX6
Created May 7, 2023 19:22
Embed
What would you like to do?
#!/usr/bin/bash
function update_xen_image()
{
local config="xen.conf"
local kernel="/boot/vmlinuz-linux-zen-hardened"
local ramdisk="initramfs-linux-zen-hardened.img"
local ucode="xen-efi-intel-ucode.bin"
# Get the end address of the padding section as hexadecimal in UPPERCASE for bc
local base="$(objdump -h xen.efi | perl -ane '/\.pad/ && printf "%016x\n", hex($F[2]) + hex($F[3])' | tr "a-z" "A-Z")"
# Section offset for the added files, padded to power of 2 boundaries
local offset=0
# Round up to next power of 2
local snippet='my $v = hex($ARGV[0]); $v -= 1; $v |= $v >> 1; $v |= $v >> 2; $v |= $v >> 4; $v |= $v >> 8; $v |= $v >> 16; $v += 1; print sprintf("%X", $v);'
local cmdline=();
for name in config ucode kernel ramdisk;
do
if [ -z "${!name}" ];
then
continue
fi
# Calculate the address of the section
local vma=$(echo "obase=16;ibase=16;$base + $offset" | bc)
# Create the variable holding the commandline snippet
cmdline+=("--add-section .$name=\"${!name}\" --change-section-vma .$name=0x$vma")
# Convert file size to hex
local size=$(echo "obase=16;$(wc -c ${!name} | cut -d ' ' -f 1)" | bc)
# Add the file size to the offset
offset=$(echo "obase=16;ibase=16;$offset + $size" | bc)
# Pad the offset to the next power of 2
offset=$(perl -e "$snippet" "0x$offset")
done
local run_str="objcopy ${cmdline[@]} \"xen.efi\" \"xen-signed.efi\""
bash -c "$run_str"
}
update_xen_image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment