#
# \brief  Fiasco.OC-specific test-environment supplements
# \author Stefan Kalkowski
# \date   2010-11-22
#
# This file is meant to be used as '--include' argument for 'tool/run'.
#

##
# Install files needed to boot via PXE
#
proc install_pxe_bootloader_to_run_dir { } {
	exec cp [genode_dir]/tool/boot/pulsar [run_dir]/boot/pulsar
	exec cp [genode_dir]/tool/boot/bender [run_dir]/boot/bender
}

##
# Return the location of the Fiasco.OC user directory
#
proc l4_dir { } {
	global _l4_dir

	if {![info exists _l4_dir]} {
		if {[file exists etc/foc.conf]} {
			set _l4_dir [exec sed -n "/^L4_BUILD_DIR/s/^.*=\\s*//p" etc/foc.conf]
			if {[file exists $_l4_dir]} { return $_l4_dir }
		}

		set _l4_dir "[pwd]/l4"
		if {![file exists $_l4_dir]} {
			puts -nonewline stderr "Error: Could neither find the L4 build directory "
			puts -nonewline stderr "within '<genode-build-dir>/l4' nor at a location "
			puts -nonewline stderr "specified via 'L4_BUILD_DIR = <l4re-build-dir>' "
			puts            stderr "in <genode-build-dir>/etc/foc.conf'."
			exit 1
		}
	}
	return $_l4_dir
}

##
# Return whether the l4-buid-directory is provided from the outside
#
proc l4_dir_external { } {
	if {[l4_dir] == "[pwd]/l4"} { return 0 }
	return 1
}

##
# Return the location of the Fiasco.OC kernel directory
#
proc fiasco { } {
	global _fiasco

	if {![info exists _fiasco]} {
		if {[file exists etc/foc.conf]} {
			set _fiasco [exec sed -n "/^KERNEL/s/^.*=\\s*//p" etc/foc.conf]
			if {[file exists $_fiasco]} { return $_fiasco }
		}

		# try to fall back to version hosted with the Genode build directory
		set _fiasco "[pwd]/kernel/fiasco.oc/fiasco"
	}
	return $_fiasco
}

##
# Return whether fiasco kernel is provided from the outside
#
proc fiasco_external { } {
	if {[fiasco] == "[pwd]/kernel/fiasco.oc/fiasco"} { return 0 }
	return 1
}

##
# Reset the target system via the Fiasco.OC kernel debugger
#
proc reset_target { {spawn_id_arg -1} } {
	global spawn_id
	if { $spawn_id_arg == -1 } {
		set spawn_id_arg $spawn_id
	}
	send -i $spawn_id_arg "\033^^"
}

##################################
## Test framework API functions ##
##################################

proc create_boot_directory { } {
	exec rm -rf   [run_dir]
	exec mkdir -p [run_dir]/genode

	if {[have_spec x86]} {
		exec mkdir -p [run_dir]/fiasco
		exec mkdir -p [run_dir]/boot/grub
	}
}


proc copy_and_strip_binaries {binaries} {

	#
	# Collect contents of the boot image
	#
	foreach binary $binaries {
		exec cp bin/$binary [run_dir]/genode
		catch {
			exec [cross_dev_prefix]strip [run_dir]/genode/$binary }
	}

	#
	# Generate config file for bootstrap
	#
}


proc bin_dir { } {
	if {[have_spec x86_32]}  { return "[l4_dir]/bin/x86_586" }
	if {[have_spec x86_64]}  { return "[l4_dir]/bin/amd64_K8" }
	if {[have_spec arm_v7a]} { return "[l4_dir]/bin/arm_armv7a" }

	puts stderr "Error: Cannot determine bin directory"
	exit 1
}

set fiasco_serial_esc_arg "-serial_esc "

proc build_boot_image_x86 {binaries} {

	global fiasco_serial_esc_arg

	copy_and_strip_binaries $binaries

	set foc_targets { }
	if {![fiasco_external] && ![file exists kernel]} { lappend foc_targets kernel }
	if {![l4_dir_external]} {
		if {![file exists bootstrap]} { lappend foc_targets bootstrap }
		if {![file exists sigma0]}    { lappend foc_targets sigma0    }
	}
	if {[llength $foc_targets] > 0} { build $foc_targets }

	# assert existence of the L4 build directory
	l4_dir

	puts "using fiasco kernel [fiasco]"
	exec cp [fiasco] [run_dir]/fiasco
	puts "using sigma0/bootstrap at [l4_dir]"
	exec cp [bin_dir]/l4f/sigma0 [run_dir]/fiasco
	exec cp [bin_dir]/bootstrap [run_dir]/fiasco

	install_iso_bootloader_to_run_dir

	#
	# Generate grub config file
	#
	# The core binary is part of the 'binaries' list but it must
	# appear right after 'sigma0' as boot module. Hence the special case.
	#
	set fh [open "[run_dir]/boot/grub/menu.lst" "WRONLY CREAT TRUNC"]
	puts $fh "timeout 0"
	puts $fh "default 0"
	puts $fh "\ntitle Genode on Fiasco.OC"
	puts $fh " kernel /fiasco/bootstrap -modaddr=0x01100000"
	puts $fh " module /fiasco/fiasco $fiasco_serial_esc_arg"
	puts $fh " module /fiasco/sigma0"
	puts $fh " module /genode/core"
	puts $fh " module /genode/config"
	foreach binary $binaries {
		if {$binary != "core"} {
			puts $fh " module /genode/$binary" } }
	puts $fh " vbeset 0x117 506070"
	close $fh

	install_pxe_bootloader_to_run_dir
	create_iso_image_from_run_dir

	#
	# Generate pulsar config file
	#
	set fh [open "[run_dir]/config-52-54-00-12-34-56" "WRONLY CREAT TRUNC"]
	puts $fh " exec /boot/bender"
	puts $fh " load /fiasco/bootstrap -modaddr=0x01100000"
	puts $fh " load /fiasco/fiasco -serial_esc"
	puts $fh " load /fiasco/sigma0"
	puts $fh " load /genode/core"
	puts $fh " load /genode/config"
	foreach binary $binaries {
		if {$binary != "core"} {
			puts $fh " load /genode/$binary" } }
	close $fh

	#
	# Generate pulsar config file pointing to the config file above.
	#
	if {[info exists ::env(PXE_TFTP_DIR_BASE)] && [info exists ::env(PXE_TFTP_DIR_OFFSET)]} {
		exec ln -nfs "[pwd]" "$::env(PXE_TFTP_DIR_BASE)$::env(PXE_TFTP_DIR_OFFSET)"

		set tftp_base ""
		if {[get_cmd_switch --tftp-absolute]} {
			set tftp_base $::env(PXE_TFTP_DIR_BASE)
		}

		set fh [open "$::env(PXE_TFTP_DIR_BASE)$::env(PXE_TFTP_DIR_OFFSET)/config-00-00-00-00-00-00" "WRONLY CREAT TRUNC"]
		puts $fh " root $tftp_base$::env(PXE_TFTP_DIR_OFFSET)/[run_dir]"
		puts $fh " config config-52-54-00-12-34-56"
		close $fh
	}
}


proc build_boot_image_arm {binaries} {

	global run_target
	global fiasco_serial_esc_arg

	copy_and_strip_binaries $binaries

	build "kernel sigma0 bootstrap"

	#
	# Generate bootstrap config
	#
	set fh [open "[run_dir]/modules.list" "WRONLY CREAT TRUNC"]

	puts $fh "modaddr 0x01100000\n"
	puts $fh "entry    genode"
	puts $fh "kernel   [fiasco] $fiasco_serial_esc_arg"
	puts $fh "roottask genode/core"
	puts $fh "module   genode/config"
	foreach binary $binaries {
		if {$binary != "core"} {
			puts $fh "module   genode/$binary" } }
	close $fh


	set gen_img_cmd "make -C [l4_dir]/source O=[l4_dir] E=genode "
	append gen_img_cmd "MODULES_LIST=[pwd]/[run_dir]/modules.list "
	append gen_img_cmd "MODULE_SEARCH_PATH=[pwd]/[run_dir]:[file dirname [fiasco]]:[l4_dir] "
	append gen_img_cmd "SYSTEM_TARGET=[cross_dev_prefix] elfimage"

	set pid [eval "spawn sh -c \"$gen_img_cmd\""]
	expect { eof { } }
	if {[lindex [wait $pid] end] != 0} {
		puts stderr "Error: Single-image creation failed"
		exit -4
	}

	exec cp [bin_dir]/bootstrap.elf [run_dir]/image.elf
	build_uboot_image [run_dir]/image.elf

	puts "\nboot image: [run_dir]/image.elf\n"

	# set symbolic link to image.elf file in TFTP directory for PXE boot
	if {[info exists ::env(PXE_TFTP_DIR_BASE)] &&
	    [info exists ::env(PXE_TFTP_DIR_OFFSET)]} {
		exec ln -sf "[pwd]/[run_dir]/image.elf" "$::env(PXE_TFTP_DIR_BASE)$::env(PXE_TFTP_DIR_OFFSET)"
		if {[regexp "uboot" $run_target]} {
			exec ln -sf "[pwd]/[run_dir]/uImage" "$::env(PXE_TFTP_DIR_BASE)$::env(PXE_TFTP_DIR_OFFSET)/uImage"
		}
	}
}


proc build_boot_image {binaries} {
	if {[have_spec x86]} { return [build_boot_image_x86 $binaries] }
	if {[have_spec arm]} { return [build_boot_image_arm $binaries] }
}


proc run_genode_until {{wait_for_re forever} {timeout_value 0} {running_spawn_id -1}} {
	#
	# If a running_spawn_id is specified, wait for the expected output
	#
	if {$running_spawn_id != -1} {
		wait_for_output $wait_for_re $timeout_value $running_spawn_id
		return
	}

	#
	# Try to use one of the supported backends for running the scripts
	#
	if {[is_amt_available]} {
		spawn_amt $wait_for_re $timeout_value
		return
	}
	if {[is_qemu_available]} {
		spawn_qemu $wait_for_re $timeout_value
		return
	}
	if {[is_serial_available]} {
		spawn_serial $wait_for_re $timeout_value "L4 Bootstrapper"
		return
	}

	global run_target
	puts stderr "Error: Can't execute automatically on target '$run_target'"
	exit -1
}
