##
# Create disk image with contents of the run directory
#
# \param --image-disk_size   disk size in MiB
# \param --image-disk_shim   nickname of certificate to use to sign GRUB2
#

source [genode_dir]/tool/run/grub2.inc
source [genode_dir]/tool/run/shim.inc


proc image_disk_size { } { return [get_cmd_arg --image-disk_size 0] }
proc image_disk_shim { } { return [get_cmd_arg --image-disk_shim ""] }


proc prepare_grub2_files { } {

	set sgdisk [installed_command sgdisk]
	set mcopy  [installed_command mcopy]

	# make copy of template grub2 header, for signing use the larger header
	if {[image_disk_shim] == ""} {
		exec cp [get_grub2_dir]/boot/grub2-head.img [run_dir].header
	} else {
		exec cp [get_grub2_dir]/boot/grub2-head-big.img [run_dir].header
	}

	set efi_partition_info   [exec $sgdisk -i 2 [run_dir].header]
	set efi_first_sector     [regexp -all -line -inline {First sector: ([0-9]+)} $efi_partition_info]
	set efi_first_sector     [lindex $efi_first_sector 1]
	set efi_partition_offset [expr $efi_first_sector * 512]

	exec mkdir -p [run_dir]/tmp/efi/boot

	if {[image_disk_shim] == ""} {
		# use unsigned efi binaries
		exec cp [get_grub2_dir]/boot/grub2/grub2_32.efi [run_dir]/tmp/efi/boot/bootia32.efi
		exec cp [get_grub2_dir]/boot/grub2/grub2_64.efi [run_dir]/tmp/efi/boot/bootx64.efi
	} else {
		# sign grub2 efi binary
		setup_shim_and_sign_grub2 [image_disk_shim] [run_dir]/tmp/efi/boot
	}

	foreach file [exec ls [run_dir]/tmp/efi/boot] {
		exec $mcopy -i [run_dir].header@@$efi_partition_offset -s [run_dir]/tmp/efi/boot/$file ::efi/boot/$file
	}

	exec rm -rf [run_dir]/tmp
}

##
# Create disk image with the content of the run directory
#
proc run_image { } {

	set sgdisk [installed_command sgdisk]

	prepare_grub2_files

	# remove template partition
	exec $sgdisk --delete=3 [run_dir].header

	# calculate size of grub2 header and the size of Genode scenario
	set size_header [expr [regsub {\s.*} [exec du -b  [run_dir].header] {}]]
	set size_run    [expr [regsub {\s.*} [exec du -skL [run_dir]] {}]]

	if {[image_disk_size] > 0} {
		set disk_size_kb [expr [image_disk_size] * 1024]
	} else {
		# set disk size to 1.5 times the run folder size; will be shrinked later
		set disk_size_kb [expr ($size_run * 3 / 2) / 32 * 32]
	}

	# setup partition with content
	exec dd if=/dev/zero of=[run_dir].partition bs=1k count=$disk_size_kb 2>/dev/null
	exec [installed_command mkfs.ext2] -L GENODE -q -T default [run_dir].partition

	# copy content to disk image
	foreach file [exec find [run_dir]] {
		set filename [string replace $file 0 [string length [run_dir]] ""]
		if {[string length $filename] == 0} {
			continue
		}

		if {[file isdirectory $file]} {
			exec [installed_command e2mkdir] [run_dir].partition:$filename
		} else {
			exec [installed_command e2cp] $file [run_dir].partition:$filename
		}
	}

	if {[image_disk_size] == 0} {
		# resize image to only needed size and get size of resized image
		exec [installed_command resize2fs] -M [run_dir].partition 2>/dev/null
		set disk_size_b [expr [regsub {\s.*} [exec wc -c [run_dir].partition] {}]]
		set disk_size_kb [expr $disk_size_b / 1024]
	}

	# calculate start/end sector of content partition
	set first_sector [expr $size_header / 512]
	set last_sector [expr ((($size_header + ($disk_size_kb * 1024)) / 512) - 1)]

	# add free space for the backup gpt at the end of disk
	set sector_backup [exec $sgdisk --info=1 [run_dir].header]
	set sector_backup [regexp -all -line -inline {First sector: ([0-9]+)} $sector_backup]
	set sector_backup [lindex $sector_backup 1]

	exec dd if=/dev/zero of=[run_dir].empty bs=512 count=$sector_backup 2>/dev/null

	# merge final image from GRUB2 head and partition
	exec cat [run_dir].header [run_dir].partition [run_dir].empty > [run_dir].img

	# cleanup
	exec rm [run_dir].empty
	exec rm [run_dir].header
	exec rm [run_dir].partition

	# move backup GPT to the end of the disk
	exec $sgdisk --move-second-header [run_dir].img

	# create partition table entry pointing to the content
	exec $sgdisk --new=3:$first_sector:$last_sector [run_dir].img
	exec $sgdisk --change-name=3:GENODE [run_dir].img

	# create hybrid MBR
	exec $sgdisk --hybrid [run_dir].img

	set size_image [expr [regsub {\s.*} [exec du -sk [run_dir].img] {}]]
	puts "Created image file [run_dir].img (${size_image}kiB)"
}


proc install_disk_bootloader_to_run_dir { } {
	exec mkdir -p [run_dir]/boot/grub
	exec cp [genode_dir]/tool/boot/bender   [run_dir]/boot/bender
	exec cp [genode_dir]/tool/boot/boot.png [run_dir]/boot/boot.png
	exec cp [get_grub2_dir]/boot/font.pf2   [run_dir]/boot/font.pf2
}
