#
# \brief  Test of Block session interface provided by server/part_blk
#

set block_count 20480

if { [file exists bin/ata.raw] == 0 } then {
	# create empty block device file
	catch { exec dd if=/dev/zero of=bin/ata.raw bs=512 count=$block_count }

	if [catch { set sfdisk [ exec which sfdisk ] }] {
	   puts "sfdisk needs to be installed!"
	   exit 1
	}

	# create two primary partitions (one is extented) and two logical paritions
	puts "using sfdisk to partition disk image, requires root privileges"
	catch { exec echo "2048,4096,c\n4096,16386,5\n0,0\n0,0\n6144,4096,c\n12288,8192,c\n" | $sfdisk -uS -f bin/ata.raw }
}

#
# Build
#

build {
	core init
	drivers/timer
	server/rom_blk
	server/part_blk
	test/blk/cli
}
create_boot_directory

#
# Generate config
#

install_config {
<config prio_levels="1" verbose="yes">
	<parent-provides>
		<service name="ROM"/>
		<service name="RAM"/>
		<service name="IRQ"/>
		<service name="IO_MEM"/>
		<service name="IO_PORT"/>
		<service name="CAP"/>
		<service name="PD"/>
		<service name="RM"/>
		<service name="CPU"/>
		<service name="LOG"/>
		<service name="SIGNAL" />
	</parent-provides>
	<default-route>
		<any-service> <parent/> <any-child/> </any-service>
	</default-route>
	<start name="timer">
		<resource name="RAM" quantum="1M"/>
		<provides><service name="Timer"/></provides>
	</start>
	<start name="rom_blk">
		<resource name="RAM" quantum="32M"/>
		<provides><service name="Block"/></provides>
		<config file="ata.raw" block_size="512"/>
	</start>
	<start name="part_blk">
		<resource name="RAM" quantum="10M" />
		<provides><service name="Block" /></provides>
		<route>
			<any-service><child name="rom_blk"/> <parent/><any-child/></any-service>
		</route>
		<config>
			<policy label="test-part1" partition="6"/>
			<policy label="test-part2" partition="1"/>
		</config>
	</start>
	<start name="test-part1">
		<binary name="test-blk-cli"/>
		<resource name="RAM" quantum="5M" />
		<route>
			<any-service> <child name="part_blk" /> <parent/> <any-child/> </any-service>
		</route>
	</start>
	<start name="test-part2">
		<binary name="test-blk-cli"/>
		<resource name="RAM" quantum="5M" />
		<route>
			<any-service> <child name="part_blk" /> <parent/> <any-child/> </any-service>
		</route>
	</start>
</config> }

#
# Boot modules
#

build_boot_image { core init timer rom_blk part_blk test-blk-cli ata.raw }

#
# Qemu
#

append qemu_args " -nographic -m 128 "

run_genode_until "Tests finished successfully.*\n.*Tests finished successfully.*\n" 50
exec rm bin/ata.raw
puts "Test succeeded"
