assert_spec x86

set path_microcode "[exec [genode_dir]/tool/ports/current --no-print-directory microcode_intel]/src/app/intel/intel-ucode"
set microcode_files [glob -nocomplain -dir $path_microcode *-*-*]

if { [get_cmd_switch --autopilot] } {

	if {[have_include "power_on/qemu"]} {
		puts "\nRun script does not support Qemu.\n"
		exit 0
	}

	# platform_info data about CPUs on other kernels missing
	assert_spec nova

	if { [have_spec x86_64] } {
		copy_file $path_microcode/06-3c-03 bin/micro.code
	} else {
		copy_file $path_microcode/06-17-06 bin/micro.code
	}
}

if {![file exists bin/micro.code]} {
	puts "\nMissing bin/micro.code file for your target machine."
	puts "Please select for your target CPU the microcode patch located in $path_microcode/xx-xx-xx."
	puts "and copy it to bin/micro.code. xx-xx-xx stands for your target CPU, family-model-stepping\n"
	exit 0
}

proc apply_microcode { } { return true }

build "core init test/microcode"

create_boot_directory

install_config {
	<config>
		<parent-provides>
			<service name="LOG"/>
			<service name="PD"/>
			<service name="CPU"/>
			<service name="ROM"/>
		</parent-provides>
		<default-route>
			<any-service> <parent/> </any-service>
		</default-route>
		<default caps="50"/>
		<start name="test-microcode">
			<resource name="RAM" quantum="10M"/>
		</start>
	</config>
}

foreach file $microcode_files {
	copy_file $file bin/ }


set boot_modules { core ld.lib.so init test-microcode }


set microcode_files [glob -tails -dir bin {[0-9,a-f][0-9,a-f]-[0-9,a-f][0-9,a-f]-[0-9,a-f][0-9,a-f]}]
append boot_modules $microcode_files


build_boot_image $boot_modules


append qemu_args "-nographic "
append qemu_args "-smp 4 "

run_genode_until "microcode check done" 30

# cleanup
if { [get_cmd_switch --autopilot] } {
	file delete -force bin/micro.code
	foreach file $microcode_files {
	  file delete bin/$file
	}
}

# check results
grep_output {\[init -\> test-microcode}

# no errors please
set filtered_output $output
grep_output {Error: }
compare_output_to {}

# no warnings please
set output $filtered_output
grep_output {Warning: }
compare_output_to {}
