if { [file exists ata.raw] == 0 } then {
	# create empty block device file
	catch { exec dd if=/dev/zero of=ata.raw bs=512 count=20480 }
	# create to tro 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" | sudo sfdisk -uS -f ata.raw }
}

#
# Build
#

set build_components {
	core init drivers/pci drivers/timer
	drivers/atapi drivers/sd_card
	server/part_blk test/part_blk
}
build $build_components

create_boot_directory

#
# Generate config
#

append 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>
}

# ATA driver on PCI
append_if [have_spec pci] config {
	<start name="pci">
		<resource name="RAM" quantum="2M"/>
		<binary name="pci_drv"/>
		<provides><service name="PCI"/></provides>
	</start>
	<start name="atapi_drv">
		<resource name="RAM" quantum="1M" />
		<provides><service name="Block"/></provides>
		<config ata="yes" />
	</start>
	<start name="part_blk">
		<resource name="RAM" quantum="10M" />
		<provides><service name="Block" /></provides>
		<route>
			<any-service><child name="atapi_drv"/> <parent/><any-child/></any-service>
		</route>
		<config>
			<policy label="test-part1" partition="6"/>
			<policy label="test-part2" partition="1"/>
		</config>
	</start>
}

# SD/MMC driver for PL180
append_if [have_spec pl180] config {
	<start name="sd_card_drv">
		<resource name="RAM" quantum="1M" />
		<provides><service name="Block"/></provides>
	</start>
	<start name="part_blk">
		<resource name="RAM" quantum="10M" />
		<provides><service name="Block" /></provides>
		<route>
			<any-service><child name="sd_card_drv"/> <parent/><any-child/></any-service>
		</route>
		<config>
			<policy label="test-part1" partition="6"/>
			<policy label="test-part2" partition="1"/>
		</config>
	</start>
}

append config {
	<start name="test-part1">
		<binary name="test-part"/>
		<resource name="RAM" quantum="10M" />
		<route>
			<any-service> <child name="part_blk" /> <parent/> <any-child/> </any-service>
		</route>
		<config pattern="0x44" />
	</start>
	<start name="test-part2">
		<binary name="test-part"/>
		<resource name="RAM" quantum="10M" />
		<route>
			<any-service> <child name="part_blk" /> <parent/> <any-child/> </any-service>
		</route>
		<config pattern="0x33" />
	</start>
</config>
}

install_config $config

#
# Boot modules
#

set boot_modules {
	core init timer part_blk test-part
}

append_if [have_spec pci]   boot_modules { pci_drv atapi_drv }
append_if [have_spec pl180] boot_modules { sd_card_drv }

build_boot_image $boot_modules

#
# Qemu
#

append qemu_args " -nographic -m 64 "
append_if [have_spec   pci] qemu_args " -boot d -hda ata.raw "
append_if [have_spec pl180] qemu_args " -drive file=ata.raw,if=sd "

run_genode_until "Success.*\n.*Success.*\n" 10

grep_output {^\[init -> test-part}
unify_output {[0-9]} "x"

compare_output_to {
[init -> test-partx] Success
[init -> test-partx] Success
}