if {![have_spec x86] || [have_spec linux]} {
	puts "Platform is unsupported."
	exit 0
}

build { app/driver_manager test/driver_manager lib/ld }

create_boot_directory

import_from_depot genodelabs/src/[base_src] \
                  genodelabs/pkg/drivers_managed-pc \
                  genodelabs/src/report_rom \
                  genodelabs/src/dynamic_rom \
                  genodelabs/src/init

install_config {
<config prio_levels="2">
	<parent-provides>
		<service name="ROM"/>
		<service name="IRQ"/>
		<service name="IO_MEM"/>
		<service name="IO_PORT"/>
		<service name="PD"/>
		<service name="RM"/>
		<service name="CPU"/>
		<service name="LOG"/>
	</parent-provides>
	<default-route>
		<any-service> <parent/> <any-child/> </any-service>
	</default-route>
	<default caps="100"/>

	<start name="timer">
		<resource name="RAM" quantum="1M"/>
		<provides><service name="Timer"/></provides>
	</start>

	<start name="report_rom">
		<binary name="report_rom"/>
		<resource name="RAM" quantum="1M"/>
		<provides> <service name="Report"/> <service name="ROM"/> </provides>
		<config verbose="yes">
			<policy label="test-driver_manager -> block_devices" report="drivers -> block_devices"/>
		</config>
	</start>

	<start name="drivers" caps="2500">
		<resource name="RAM" quantum="64M" constrain_phys="yes"/>
		<binary name="init"/>
		<route>
			<service name="ROM" label="config"> <parent label="drivers.config"/> </service>
			<service name="ROM" label="capslock"> <child name="dynamic_rom"/> </service>
			<service name="ROM" label="numlock">  <child name="dynamic_rom"/> </service>
			<service name="ROM" label="system">   <child name="dynamic_rom"/> </service>
			<service name="Timer">  <child name="timer"/> </service>
			<service name="Report"> <child name="report_rom"/> </service>
			<any-service> <parent/> </any-service>
		</route>
		<provides>
			<service name="Input"/>
			<service name="Framebuffer"/>
			<service name="Block"/>
		</provides>
	</start>

	<start name="dynamic_rom">
		<resource name="RAM" quantum="4M"/>
		<provides> <service name="ROM"/> </provides>
		<config verbose="yes">
			<rom name="capslock">
				<inline> <capslock enabled="no"/> </inline>
				<sleep milliseconds="500" />
				<inline> <capslock enabled="yes"/> </inline>
				<sleep milliseconds="500" />
			</rom>
			<rom name="numlock">
				<inline> <numlock enabled="no"/> </inline>
				<sleep milliseconds="1000" />
				<inline> <numlock enabled="yes"/> </inline>
				<sleep milliseconds="1000" />
			</rom>
			<rom name="system">
				<inline> <system state=""/> </inline>
				<sleep milliseconds="10000" />
			</rom>
			<rom name="numlock_remap">
				<inline> <system state=""/> </inline>
				<sleep milliseconds="10000" />
			</rom>
		</config>
	</start>

	<start name="test-driver_manager">
		<resource name="RAM" quantum="1M"/>
		<config>
			<check_ahci_block_device label="ahci-0" block_count="65536" block_size="512"
			                         model="QEMU HARDDISK"/>
			<check_input/>
			<check_framebuffer/>
		</config>
		<route>
			<service name="ROM" label="block_devices"> <child name="report_rom"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>

</config>}

set fd [open [run_dir]/genode/fb_drv.config "w"]
puts $fd {
<config width="1024" height="768" buffered="yes"> <!-- used by VESA driver -->
	<report connectors="yes"/>
	<connector name="eDP-36" width="1920" height="1080" enabled="true"/>
</config>}
close $fd


#
# Override 'drivers.config' as supplied with the 'drivers_managed-pc' pkg
# with the (possibly modified) version found in the source tree. This avoids
# the need to re-create the pkg while testing modifications of this file.
#
file copy -force [genode_dir]/repos/gems/recipes/raw/drivers_managed-pc/drivers.config \
                 [run_dir]/genode/drivers.config

build_boot_image { driver_manager test-driver_manager ld.lib.so }

#
# Build ext2 file-system images to be supplied to Qemu
#
if {[have_include "power_on/qemu"]} {

	set mke2fs [check_installed mke2fs]
	set dd     [check_installed dd]

	catch { exec $dd if=/dev/zero of=bin/hdd_disk.raw bs=1M count=32 }
	catch { exec $mke2fs -F bin/hdd_disk.raw }
}

#
# Qemu opts for supplying hdd_disk.raw as AHCI disk
#
append qemu_args " -m 128 "
append qemu_args " -nographic "
append qemu_args " -device ahci,id=ahci "
append qemu_args " -drive id=hdd,file=bin/hdd_disk.raw,format=raw,if=none -device ide-hd,drive=hdd,bus=ahci.0 "

run_genode_until {.*all expected devices present and accessible.*\n} 120

