assert_spec foc
assert_spec 32bit

#
# Build
#

set use_platform_drv [expr [have_spec platform_arndale] || [have_spec platform_imx53]]
set use_usb_drv      [expr [have_spec omap4] || [have_spec exynos5]]
set use_nic_drv      [expr !$use_usb_drv]
set use_atapi_drv    [have_spec x86]
set use_sd_card_drv  [expr [have_spec omap4] || [have_spec exynos5] || [have_spec pl180]]

# generic components
set build_components {
	core
	init
	drivers/timer
	drivers/framebuffer
	server/nitpicker
	server/nit_fb
	server/terminal
	server/terminal_log
	l4android
}

lappend_if [have_spec   pci] build_components drivers/pci
lappend_if [have_spec  acpi] build_components drivers/acpi
lappend_if [have_spec   ps2] build_components drivers/input/ps2
lappend_if [have_spec usb]   build_components drivers/usb
lappend_if $use_usb_drv      build_components drivers/usb
lappend_if $use_nic_drv      build_components drivers/nic
lappend_if $use_atapi_drv    build_components drivers/atapi
lappend_if $use_sd_card_drv  build_components drivers/sd_card
lappend_if $use_platform_drv build_components drivers/platform

build $build_components
create_boot_directory

#
# Config
#

set config  {
<config prio_levels="2">
	<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="nitpicker">
		<resource name="RAM" quantum="2M"/>
		<provides><service name="Nitpicker"/></provides>
		<route>
			<service name="Input">       <child name="input_drv"/> </service>
			<service name="Framebuffer"> <child name="fb_drv"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>
	<start name="android_fb">
		<binary name="nit_fb"/>
		<resource name="RAM" quantum="4M"/>
		<provides>
			<service name="Framebuffer"/>
			<service name="Input"/>
		</provides>
		<config xpos="0" ypos="0" width="640" height="480" refresh_rate="25"/>
	</start>
	<start name="android_term_fb">
		<binary name="nit_fb"/>
		<resource name="RAM" quantum="4M"/>
		<provides>
			<service name="Framebuffer"/>
			<service name="Input"/>
		</provides>
		<config xpos="0" ypos="482" width="1024" height="268" refresh_rate="25"/>
	</start>
	<start name="log_term_fb">
		<binary name="nit_fb"/>
		<resource name="RAM" quantum="4M"/>
		<provides>
			<service name="Framebuffer"/>
			<service name="Input"/>
		</provides>
		<config xpos="642" ypos="0" width="380" height="480" refresh_rate="25"/>
	</start>
	<start name="android_term">
		<binary name="terminal"/>
		<resource name="RAM" quantum="1M"/>
		<provides><service name="Terminal"/></provides>
		<route>
			<service name="Input">       <child name="android_term_fb"/> </service>
			<service name="Framebuffer"> <child name="android_term_fb"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>
	<start name="log_term">
		<binary name="terminal"/>
		<resource name="RAM" quantum="1M"/>
		<provides><service name="Terminal"/></provides>
		<route>
			<service name="Input">       <child name="log_term_fb"/> </service>
			<service name="Framebuffer"> <child name="log_term_fb"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>
	<start name="terminal_log">
		<resource name="RAM" quantum="1M"/>
		<provides><service name="LOG"/></provides>
		<route>
			<service name="Terminal"> <child name="log_term"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>
	<start name="l4android">
		<resource name="RAM" quantum="512M"/>
		<config args="mem=128M console=ttyS0 l4x_rd=root-ginger.gz">
			<block label="sda"/>
		</config>
		<route>
			<service name="Input">       <child name="android_fb"/> </service>
			<service name="Framebuffer"> <child name="android_fb"/> </service>
			<service name="Terminal">    <child name="android_term"/> </service>
			<service name="LOG">         <child name="terminal_log"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>}

append_if [have_spec acpi] config {
	<start name="acpi">
		<resource name="RAM" quantum="8M"/>
		<binary name="acpi_drv"/>
		<provides>
			<service name="PCI"/>
			<service name="IRQ" />
		</provides>
		<route>
			<service name="PCI"> <any-child /> </service>
			<any-service> <parent/> <any-child /> </any-service>
		</route>
	</start>}

append_if [expr ![have_spec acpi] && [have_spec pci]] config {
	<start name="pci_drv">
		<resource name="RAM" quantum="5M"/>
		<provides><service name="PCI"/></provides>
	</start>}

append_if $use_platform_drv config {
	<start name="platform_drv">
		<resource name="RAM" quantum="1M"/>
		<provides>
			<service name="Regulator"/>
		</provides>
	</start>}

append_if [have_spec gpio] config {
	<start name="gpio_drv">
		<resource name="RAM" quantum="4M"/>
		<provides><service name="Gpio"/></provides>
		<config/>
	</start>}

append_if $use_usb_drv config {
	<start name="usb_drv">
		<resource name="RAM" quantum="12M"/>
		<provides>
			<service name="Nic"/>
			<service name="Input"/>
		</provides>
		<config uhci="yes" ehci="yes" xhci="yes">
			<nic mac="2e:60:90:0c:4e:01" />
			<hid/>
		</config>
	</start>}

append_if $use_nic_drv config {
	<start name="nic_drv">
		<resource name="RAM" quantum="4M"/>
		<provides><service name="Nic"/></provides>
	</start>}

append_if $use_atapi_drv config {
	<start name="blk_drv">
		<start name="atapi_drv"/>
		<resource name="RAM" quantum="1M" />
		<provides><service name="Block" /></provides>
		<config ata="yes"/>
	</start>}

append_if $use_sd_card_drv config {
	<start name="blk_drv">
	    <binary name="sd_card_drv"/>
		<resource name="RAM" quantum="1M"/>
		<provides><service name="Block"/></provides>
	</start>}

append_if [have_spec framebuffer] config {
	<start name="fb_drv">
		<resource name="RAM" quantum="4M"/>
		<provides><service name="Framebuffer"/></provides>
		<config width="1024" height="768" depth="16" output="HDMI" buffered="yes" />
	</start>}

append_if [have_spec ps2] config {
	<start name="input_drv">
		<binary name="ps2_drv"/>
		<resource name="RAM" quantum="1M"/>
		<provides><service name="Input"/></provides>
	</start>}

append config {
</config>}

install_config $config


#
# Boot modules
#

set boot_modules {
	core
	init
	timer
	nitpicker
	nit_fb
	terminal
	terminal_log
	l4android
	root-ginger.gz
}

lappend_if [have_spec        acpi] boot_modules acpi_drv
lappend_if [have_spec         pci] boot_modules pci_drv
lappend_if [have_spec        gpio] boot_modules gpio_drv
lappend_if [have_spec         ps2] boot_modules ps2_drv
lappend_if [have_spec framebuffer] boot_modules fb_drv
lappend_if $use_usb_drv            boot_modules usb_drv
lappend_if $use_nic_drv            boot_modules nic_drv
lappend_if $use_atapi_drv          boot_modules atapi_drv
lappend_if $use_sd_card_drv        boot_modules sd_card_drv
lappend_if $use_platform_drv       boot_modules platform_drv

if {[have_spec x86]} {
	set initrd_uri "http://genode.org/files/release-11.11/l4lx/root-ia32-ginger.gz"
	set system_uri "http://genode.org/files/release-11.11/l4lx/system-ia32-ginger.img"
} elseif {[have_spec arm]} {
	set initrd_uri "http://genode.org/files/release-11.11/l4lx/root-arm-ginger.gz"
	set system_uri "http://genode.org/files/release-11.11/l4lx/system-arm-ginger.img"
}
if {![file exists bin/root-ginger.gz]} {
	puts "Download initramfs ..."
	exec curl $initrd_uri > bin/root-ginger.gz 2> /dev/null
}
if {![file exists bin/system-ginger.img]} {
	puts "Download system image ..."
	exec curl $system_uri > bin/system-ginger.img 2> /dev/null
}
exec >& /dev/null wget -O bin/root-ginger.gz.md5 $initrd_uri.md5
exec >& /dev/null wget -O bin/system-ginger.img.md5 $system_uri.md5
cd bin
exec md5sum -c root-ginger.gz.md5
exec md5sum -c system-ginger.img.md5
cd ..

build_boot_image  [join $boot_modules " "]

append qemu_args " -m 512 "
append qemu_args " -serial mon:stdio "
append_if $use_atapi_drv   qemu_args " -hda bin/system-ginger.img "
append_if $use_sd_card_drv qemu_args " -drive file=bin/system-ginger.img,if=sd,cache=writeback "
append_if [have_spec     x86] qemu_args " -net nic,model=e1000 "
append_if [have_spec lan9118] qemu_args " -net nic,model=lan9118 "
append_if $use_nic_drv        qemu_args " -net user -redir tcp:5555::5555 "

run_genode_until forever
