assert_spec platform_arndale

#
# Build
#
set build_components {
	app/cli_monitor
	app/gdb_monitor
	app/launchpad
	app/scout
	core
	drivers/ahci
	drivers/framebuffer
	drivers/platform
	drivers/sd_card
	drivers/timer
	drivers/uart
	drivers/usb
	init
	l4linux
	lib/libc_noux
	noux
	server/ffat_fs
	server/fs_rom
	server/nic_bridge
	server/liquid_framebuffer
	server/nitpicker
	server/part_blk
	server/tar_fs
	server/terminal
	server/terminal_crosslink
	server/terminal_log
	server/terminal_mux
	test/affinity
	test/blk/cli
	test/gdb_monitor
	test/input
	test/lwip/http_srv
	test/nitpicker
}

lappend build_components noux-pkg/vim
lappend build_components noux-pkg/gdb_arm

build $build_components
create_boot_directory

exec tar cfv bin/vim.tar -h -C bin/vim .

#
# 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="kdb_uart_drv">
		<resource name="RAM" quantum="1M"/>
		<provides>
			<service name="Terminal"/>
			<service name="Uart"/>
		</provides>
		<config>
			<policy label="terminal_mux" uart="0" detect_size="yes"/>
		</config>
	</start>
	<start name="terminal_mux">
		<resource name="RAM" quantum="8M"/>
		<provides><service name="Terminal"/></provides>
		<route>
			<service name="Terminal"> <child name="kdb_uart_drv"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>
	<start name="terminal_log">
		<resource name="RAM" quantum="2M"/>
		<provides>
			<service name="LOG"/>
		</provides>
		<route>
			<service name="Terminal"> <child name="terminal_mux"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>
	<start name="platform_drv">
		<resource name="RAM" quantum="1M"/>
		<provides><service name="Regulator"/></provides>
		<route>
			<service name="LOG"> <child name="terminal_log"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>
	<start name="usb_drv">
		<resource name="RAM" quantum="12M"/>
		<provides>
			<service name="Nic"/>
			<service name="Input"/>
		</provides>
		<config uhci="yes" ehci="yes">
			<hid />
			<nic mac="2e:60:90:0c:4e:01" />
		</config>
		<route>
			<service name="LOG"> <child name="terminal_log"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>
	<start name="usb30_drv">
		<binary name="usb_drv"/>
		<resource name="RAM" quantum="12M"/>
		<provides><service name="Block"/></provides>
		<config xhci="yes"><storage /></config>
		<route>
			<service name="LOG"> <child name="terminal_log"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>
	<start name="nic_bridge">
		<resource name="RAM" quantum="4M"/>
		<provides><service name="Nic"/></provides>
		<route>
			<service name="Nic"> <child name="usb_drv"/> </service>
			<service name="LOG"> <child name="terminal_log"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>
	<start name="sd_card_drv">
		<resource name="RAM" quantum="1M"/>
		<provides><service name="Block"/></provides>
		<route>
			<service name="LOG"> <child name="terminal_log"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>
	<start name="ahci">
		<resource name="RAM" quantum="1M"/>
		<provides><service name="Block"/></provides>
	</start>
	<start name="part_blk_emmc">
		<binary name="part_blk"/>
		<resource name="RAM" quantum="10M" />
		<provides><service name="Block" /></provides>
		<route>
			<service name="LOG"> <child name="terminal_log"/>  </service>
			<service name="Block"> <child name="sd_card_drv"/> </service>
			<any-service><parent/><any-child/></any-service>
		</route>
		<config>
			<policy label="cli_monitor -> block_emmc"          partition="1"/>
			<policy label="cli_monitor -> l4linux_emmc -> sda" partition="2"/>
		</config>
	</start>
	<start name="part_blk_sata">
		<binary name="part_blk"/>
		<resource name="RAM" quantum="10M" />
		<provides><service name="Block" /></provides>
		<route>
			<service name="LOG">   <child name="terminal_log"/> </service>
			<service name="Block"> <child name="ahci"/>         </service>
			<any-service><parent/><any-child/></any-service>
		</route>
		<config>
			<policy label="cli_monitor -> vim_sata -> ffat_fs_sata" partition="1"/>
			<policy label="cli_monitor -> l4linux_sata -> sda"      partition="2"/>
		</config>
	</start>
	<start name="part_blk_usb">
		<binary name="part_blk"/>
		<resource name="RAM" quantum="10M" />
		<provides><service name="Block" /></provides>
		<route>
			<service name="LOG"> <child name="terminal_log"/>  </service>
			<service name="Block"> <child name="usb30_drv"/> </service>
			<any-service><parent/><any-child/></any-service>
		</route>
		<config>
			<policy label="cli_monitor -> block_usb"           partition="2"/>
			<policy label="cli_monitor -> l4linux_usb -> sda"  partition="1"/>
		</config>
	</start>
	<start name="cli_monitor" priority="-1">
		<resource name="RAM" quantum="1800M"/>
		<config>
			<preservation name="RAM" quantum="32M" />
			<subsystem name="l4linux" help="L4Linux with network">
				<binary name="l4linux"/>
				<resource name="RAM" quantum="128M"/>
				<config args="mem=2G console=ttyS0 l4x_rd=initrd.gz l4x_cpus=2 l4x_cpus_map=0,1"/>
			</subsystem>
			<subsystem name="l4linux_emmc" help="L4Linux accessing the second eMMC partition">
				<binary name="l4linux"/>
				<resource name="RAM" quantum="128M"/>
				<config args="mem=2G console=ttyS0 l4x_rd=initrd.gz l4x_cpus=2 l4x_cpus_map=0,1">
					<block label="sda" />
				</config>
			</subsystem>
			<subsystem name="l4linux_sata" help="L4Linux accessing the second SATA partition">
				<binary name="l4linux"/>
				<resource name="RAM" quantum="128M"/>
				<config args="mem=2G console=ttyS0 l4x_rd=initrd.gz l4x_cpus=2 l4x_cpus_map=0,1">
					<block label="sda" />
				</config>
			</subsystem>
			<subsystem name="l4linux_usb" help="L4Linux accessing the first USB disk partition">
				<binary name="l4linux"/>
				<resource name="RAM" quantum="128M"/>
				<config args="mem=2G console=ttyS0 l4x_rd=initrd.gz l4x_cpus=2 l4x_cpus_map=0,1">
					<block label="sda" />
				</config>
			</subsystem>
			<subsystem name="affinity" help="test execution on multiple CPUs">
				<binary name="test-affinity"/>
				<resource name="RAM" quantum="1M"/>
			</subsystem>
			<subsystem name="block_emmc" help="block device test on first eMMC partition">
				<binary name="test-blk-cli"/>
				<resource name="RAM" quantum="1M" />
			</subsystem>
			<subsystem name="block_usb" help="block device test on second USB disk partition">
				<binary name="test-blk-cli"/>
				<resource name="RAM" quantum="1M" />
			</subsystem>
			<subsystem name="vim_sata" help="VIM editor accessing the first SATA partition">
				<binary name="init"/>
				<resource name="RAM" quantum="1024M" />
				<config>
					<parent-provides>
						<service name="ROM"/>
						<service name="RAM"/>
						<service name="IRQ"/>
						<service name="IO_MEM"/>
						<service name="CAP"/>
						<service name="PD"/>
						<service name="RM"/>
						<service name="CPU"/>
						<service name="LOG"/>
						<service name="SIGNAL"/>
						<service name="Timer"/>
						<service name="Terminal"/>
						<service name="Block"/>
					</parent-provides>
					<default-route>
						<any-service> <parent/> <any-child/> </any-service>
					</default-route>
					<start name="ffat_fs_sata">
						<binary name="ffat_fs"/>
						<resource name="RAM" quantum="10M"/>
						<provides><service name="File_system"/></provides>
						<config>
							<policy label="vim_sata_part1 -> part1" root="/" writeable="yes" />
						</config>
					</start>
					<start name="vim_sata_part1">
						<binary name="noux"/>
						<resource name="RAM" quantum="100M"/>
						<config>
							<fstab>
								<tar name="vim.tar" />
								<dir name="mnt"> <fs label="part1" /> </dir>
							</fstab>
							<start name="/bin/vim">
								<env name="TERM" value="linux" />
								<arg value="--noplugin" />
								<arg value="-n" />
								<arg value="-N" />
								<arg value="--cmd" />
								<arg value="set laststatus=2" />
								<arg value="--cmd" />
								<arg value="set hls" />
							</start>
						</config>
						<route>
							<service name="File_system">
								<if-arg key="label" value="part1"/><child name="ffat_fs_sata"/>
							</service>
							<any-service><parent/><any-child/></any-service>
						</route>
					</start>
				</config>
			</subsystem>
			<subsystem name="graphical_demo" help="Graphical demo scenario">
				<binary name="init"/>
				<resource name="RAM" quantum="1024M" />
				<config>
					<parent-provides>
						<service name="ROM"/>
						<service name="RAM"/>
						<service name="IRQ"/>
						<service name="IO_MEM"/>
						<service name="CAP"/>
						<service name="PD"/>
						<service name="RM"/>
						<service name="CPU"/>
						<service name="LOG"/>
						<service name="SIGNAL"/>
						<service name="Timer"/>
						<service name="Regulator"/>
						<service name="Input"/>
					</parent-provides>
					<default-route>
						<any-service> <parent/> <any-child/> </any-service>
					</default-route>
					<start name="fb_drv">
						<resource name="RAM" quantum="6M"/>
						<provides><service name="Framebuffer"/></provides>
					</start>
					<start name="nitpicker">
						<resource name="RAM" quantum="1M"/>
						<provides><service name="Nitpicker"/></provides>
						<config>
						<global-keys>
						<key name="KEY_SCROLLLOCK" operation="xray" />
						<key name="KEY_SYSRQ"      operation="kill" />
						<key name="KEY_PRINT"      operation="kill" />
						<key name="KEY_F11"        operation="kill" />
						<key name="KEY_F12"        operation="xray" />
						</global-keys>
						</config>
						<route>
							<service name="Framebuffer">
								<child name="fb_drv"/>
							</service>
							<any-service> <parent/> <any-child/> </any-service>
						</route>
					</start>
					<start name="Terminal">
						<binary name="liquid_fb"/>
						<resource name="RAM" quantum="6M"/>
						<provides>
							<service name="Framebuffer"/>
							<service name="Input"/>
						</provides>
						<config xpos="5" ypos="10" width="640" height="480" title="Terminal" animate="off"/>
					</start>
					<start name="terminal_cli">
						<binary name="terminal"/>
						<resource name="RAM" quantum="4M"/>
						<provides><service name="Terminal"/></provides>
						<route>
							<service name="Framebuffer">
								<child name="Terminal"/>
							</service>
							<service name="Input"><child name="Terminal"/></service>
							<any-service> <parent/> <any-child/> </any-service>
						</route>
					</start>
					<start name="tar_fs">
						<resource name="RAM" quantum="20M"/>
						<provides><service name="File_system"/></provides>
						<config>
							<archive name="gdb.tar" />
							<policy label="fs_rom" root="/gdb" />
							<policy label="cli_monitor" root="/" />
						</config>
					</start>
					<start name="fs_rom">
						<resource name="RAM" quantum="200M"/>
						<provides><service name="ROM"/></provides>
					</start>
					<start name="cli_monitor">
						<resource name="RAM" quantum="2G"/>
						<config>
							<preservation name="RAM" quantum="16M" />
							<subsystem name="gdb_test" help="GDB test application">
								<binary name="test-gdb_monitor"/>
								<resource name="RAM" quantum="100M"/>
							</subsystem>
							<subsystem name="testnit" help="Graphical test application">
								<binary name="testnit"/>
								<resource name="RAM" quantum="100M"/>
							</subsystem>
							<subsystem name="scout" help="Demo browser">
								<binary name="scout"/>
								<resource name="RAM" quantum="100M"/>
							</subsystem>
						</config>
						<route>
							<service name="Terminal">
								<child name="terminal_cli"/>
							</service>
							<service name="ROM"><child name="fs_rom"/></service>
							<service name="File_system">
								<child name="tar_fs"/>
							</service>
							<any-service> <parent/> <any-child/> </any-service>
						</route>
					</start>
				</config>
			</subsystem>
		</config>
		<route>
			<service name="LOG"> <child name="terminal_log"/> </service>
			<service name="Terminal"> <child name="terminal_mux"/> </service>
			<service name="Nic"> <child name="nic_bridge"/> </service>
			<service name="Block">
					<if-arg key="label" value="l4linux_sata -> sda"/><child name="part_blk_sata"/>
			</service>
			<service name="Block">
					<if-arg key="label" value="l4linux_emmc -> sda"/><child name="part_blk_emmc"/>
			</service>
			<service name="Block">
					<if-arg key="label" value="l4linux_usb -> sda"/><child name="part_blk_usb"/>
			</service>
			<service name="Block">
					<if-arg key="label" value="block_emmc"/><child name="part_blk_emmc"/>
			</service>
			<service name="Block">
					<if-arg key="label" value="block_usb"/><child name="part_blk_usb"/>
			</service>
			<service name="Block">
					<if-arg key="label" value="vim_sata -> ffat_fs_sata"/><child name="part_blk_sata"/>
			</service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>
}
append config {
</config>
}
install_config $config

set gdb_config_fd [open "bin/gdb_command_config" w]
puts $gdb_config_fd {<config>
	<parent-provides>
		<service name="ROM"/>
		<service name="RAM"/>
		<service name="CAP"/>
		<service name="PD"/>
		<service name="RM"/>
		<service name="CPU"/>
		<service name="LOG"/>
		<service name="SIGNAL"/>
		<service name="Timer"/>
		<service name="Nitpicker"/>
		<service name="File_system"/>
	</parent-provides>
	<start name="GDB">
		<binary name="liquid_fb"/>
		<resource name="RAM" quantum="6M"/>
		<provides>
			<service name="Framebuffer"/>
			<service name="Input"/>
		</provides>
		<route>
			<any-service><parent/><any-child/></any-service>
		</route>
		<config xpos="600" ypos="150" width="640" height="480" title="GDB" animate="off"/>
	</start>
	<start name="terminal">
		<resource name="RAM" quantum="4M"/>
		<provides><service name="Terminal"/></provides>
		<route>
			<service name="Framebuffer"><child name="GDB"/></service>
			<service name="Input"><child name="GDB"/></service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>
	<start name="terminal_gdb">
		<binary name="terminal_crosslink"/>
		<resource name="RAM" quantum="1M"/>
		<provides> <service name="Terminal"/> </provides>
		<route>
			<any-service><parent/><any-child/></any-service>
		</route>
	</start>
	<start name="noux">
		<resource name="RAM" quantum="50M"/>
		<route>
			<service name="Terminal">
				<if-arg key="label" value=""/><child name="terminal"/>
			</service>
			<service name="Terminal">
				<if-arg key="label" value="noux(terminal_fs)"/><child name="terminal_gdb"/>
			</service>
			<any-service> <any-child/> <parent/> </any-service>
		</route>
		<config>

			<!-- the GDB <start> node gets generated above this comment -->

			<fstab>
				<fs label="gdb"/>
				<dir name="dev"> <terminal name="gdb"/> </dir>
			</fstab>
		</config>
	</start>
	<start name="gdb_monitor">

		<!-- the <config> node gets generated above this comment -->

		<resource name="RAM" quantum="1G"/>
		<route>
			<service name="Terminal"><child name="terminal_gdb"/></service>
			<any-service> <any-child/> <parent/> </any-service>
		</route>
	</start>
</config>}
close $gdb_config_fd

set gdb_binaries {
	test-gdb_monitor
	testnit
	scout
	libc.lib.so
	ld.lib.so
	libc_log.lib.so
}
set gdb_stripped_binaries {
	expat.lib.so
	gdb_monitor
	init
	launchpad
	libc_lock_pipe.lib.so
	libc_noux.lib.so
	libc_terminal.lib.so
	libm.lib.so
	liquid_fb
	ncurses.lib.so
	noux
	terminal
	terminal_crosslink
}

exec sh -c "find bin/gdb_arm/ -type f | (xargs [cross_dev_prefix]strip || true) 2>/dev/null"
exec mkdir -p bin/gdb/src
foreach binary ${gdb_binaries} {
	set source_files [ exec [cross_dev_prefix]objdump -dl bin/${binary} | grep "^/.*:.*" | sed -e "s/:.*//" | uniq ]
	foreach source_file ${source_files} {
		# resolve '..' to avoid problems with 'tar' with parts like '/a/b/../'
		# where '/a' exists, but '/a/b' does not
		set source_file [file normalize ${source_file}]
		if [file exists ${source_file}] {
			set dirname [ exec dirname ${source_file}]
			exec mkdir -p bin/gdb/src${dirname}
			exec ln -sf ${source_file} bin/gdb/src${source_file}
		}
	}
}
foreach binary ${gdb_stripped_binaries} {
	exec sh -c "cp bin/${binary} bin/gdb/${binary}"
	exec sh -c "[cross_dev_prefix]strip bin/gdb/${binary}"
}
foreach binary ${gdb_binaries} {
	exec ln -s ../${binary} bin/gdb/${binary}
}
exec ln -s ../gdb_command_config bin/gdb/gdb_command_config
exec tar cfh bin/gdb.tar -C bin gdb
exec tar ufh bin/gdb.tar -C bin/gdb_arm bin
exec rm -rf bin/gdb

#
# Boot modules
#
set boot_modules {
	ahci
	cli_monitor
	core
	fb_drv
	ffat_fs
	fs_rom
	init
	initrd.gz
	kdb_uart_drv
	l4linux
	ld.lib.so
	libc.lib.so
	libc_log.lib.so
	libc_noux.lib.so
	libc_terminal.lib.so
	libm.lib.so
	lwip.lib.so
	ncurses.lib.so
	nic_bridge
	liquid_fb
	nitpicker
	noux
	part_blk
	platform_drv
	sd_card_drv
	tar_fs
	terminal
	terminal_log
	terminal_mux
	test-affinity
	test-blk-cli
	timer
	usb_drv
	vim.tar
	gdb.tar
}

set uri "http://genode.org/files/l4linux/initrd-arm-mdev.gz"
if {![file exists bin/initrd.gz]} {
	puts "Download initramfs ..."
	exec >& /dev/null wget -c -O bin/initrd.gz $uri
}
exec >& /dev/null wget -O bin/initrd.gz.md5 $uri.md5
cd bin
exec md5sum -c initrd.gz.md5
cd ..

set fiasco_serial_esc_arg ""
build_boot_image  [join $boot_modules " "]
run_genode_until forever
