SubDir TOP hog-apps drivesetup_gpt ;


#
# Build
#

SetupHeadersHoG ;

Application PartitionSetupGPT :
	DriveSetupGPT.cpp
	;


#
# Test-run in Qemu  (invoke with "jam hog-drivesetup-gpt.emu6")
#

AddInteractiveComponents	hog-drivesetup-gpt.run ;
AddComponentService			hog-drivesetup-gpt.run : 1M : pc_rtc : Rtc ;
AddComponentAsStart			hog-drivesetup-gpt.run : 4M : report_rom :
		"name=\"storage_report_rom\" caps=\"120\"" :
	"	<binary name=\"report_rom\"/>
	\		<provides> <service name=\"Report\"/> <service name=\"ROM\"/> </provides>
	\		<config verbose=\"yes\">
	\			<policy label_prefix=\"PartitionSetupGPT -> partitions\"	report=\"part_block -> partitions\"/>
	\		</config>
	"
	;

AddComponentService  hog-drivesetup-gpt.run
	: 2M
	: ahci  # was: ahci_drv
	: Block
	: "<config>
			<default-policy device=\"0\" writeable=\"yes\" />
#			<default-policy device=\"0\" writeable=\"no\" />
		</config>
		"
	;

#AddComponentClient  hog-drivesetup-gpt.run
#	: 2M
#	: gpt_write
#	: "
## I Probably SHould Use This:    <config wipe="yes"/>
##	\		<config  verbose=\"yes\" initialize=\"yes\">
#	\		<config verbose=\"yes\" initialize=\"yes\" align=\"4K\">
#	\			<actions>
##	\				<add entry=\"1\" type=\"BIOS\"  label=\"GRUB BIOS\"  start=\"2048\"  size=\"1M\"/>
#	\				<add           type=\"BDP\"   label=\"FAT32 Data\" size=\"max\"/>
##	\				<modify label=\"GENODE\" new_label=\"GENODE*\" new_size=\"max\"/>
#	\			</actions>
# 	\		</config>
#	\		<route>
#		# route "Block" toward AHCI_drv, not to sub-partition of drive #
#		# - ahci(_drv) is within "use drv manager" sub-init...
#		# - part_block, however, is separate, right ?
#	\			<service name=\"Block\">
#						# use label " default" (instead of "ahci-0" or "ahci-1" etc), so that ahci(_drv) will be called (by driver_manager) with either port 0 or 1 or 2... dynamically
#	\				<child name=\"drivers_init\" label=\"basic-layers: default\" />  <!-- for the USE_DRV_MANAGER case ; careful to preserve the whitespace in ' default' (see main.cc:399) -->
#	\				<child name=\"ahci\" />  <!-- for the 'static drivers' case -->
#	\			</service>
# 	\			<any-service> <parent/> <any-child/> </any-service>
#	\		</route>
#	"
#	;

#AddComponentService  hog-drivesetup-gpt.run : 10M :
#	part_block : Block
#	: "
#	\		<config use_gpt=\"yes\" io_buffer=\"4M\"  >
#	\			<report partitions=\"yes\"/>
#		#no need, we're only interested in the report, not in accessing the partitions themselves:
#		#	\			<default-policy partition=\"1\" writeable=\"no\"/>
#	\		</config>
#	\		<route>
#	\			<service name=\"Report\"> <child name=\"storage_report_rom\"/> </service>
#					# Handle both USE_DRV_MANAGER and 'static' drivers cases
#					# Careful to preserve the whitespace in " default" here:
#	\			<service name=\"Block\">
#	\				<child name=\"drivers_init\" label=\"ahci: default\" />
#	\				<child name=\"ahci\" />
#	\			</service>
#	\			<any-service> <parent/> <any-child/> </any-service>
#	\		</route>
#	"
#	;

Config_Vfs on hog-drivesetup-gpt.run +=
	"
	\				<dir name=\"dev\">
	\					<fs label=\"downstream\"/>  <!-- for Broker / bro2app -->
	\				</dir>
	\				<dir name=\"boot\">
	\					<fs label=\"system_tools\"/>  <!-- the BRoster client needs to inspect 'part_block', 'gpt_write' and 'mmkntfs' before asking dynit server to spawn them -->
	\				</dir>
	"
	;
Config_Route on hog-drivesetup-gpt.run +=
	"			<service name=\"ROM\" label=\"partitions\"> <child name=\"storage_report_rom\"/> </service>
	\			<service name=\"File_system\" label=\"downstream\">
	\				<child name=\"vfs\"/>
	\			</service>
	\			<service name=\"File_system\" label=\"system_tools\">
	\				<child name=\"vfs_ram\"/>
	\			</service>
	"
	;
AddPosixApp
	hog-drivesetup-gpt.run : 11M : 175 : PartitionSetupGPT
	;

#################

if 1
{
# I can omit the .run suffix and it still works ??
#	AddBootModule  hog-drivesetup-gpt :		zlib.lib.so ;
	AddBootModule  hog-drivesetup-gpt.run : zlib.lib.so ;
	AddBootModule  hog-drivesetup-gpt.run : vfs_pipe.lib.so ;
#	AddBootModule  hog-drivesetup-gpt.run : nano3d ;
	AddBootModule  hog-drivesetup-gpt.run : part_block ;
	
	AddBootModule  hog-drivesetup-gpt.run : gpt_write ;
	AddBootModule  hog-drivesetup-gpt.run : mkntfs ;
}


rule AddStackBeIpc  runscenario
{
	# Genode services: pipes
	AddComponentService  $(runscenario) : 2M
		: vfs : File_system
	#+ vfs_bro
		:	"
		\		<config ld_verbose=\"yes\">
		\			<vfs>
		\				<dir name=\"dev\">
		\					<log/>
		\				</dir>
		\				<dir name=\"export\">
		\					<dir name=\"bro2app\">
		\						<dir name=\"1\"> <pipe> <fifo name=\"pipe\"/> </pipe> </dir>
		\						<dir name=\"2\"> <pipe> <fifo name=\"pipe\"/> </pipe> </dir>
		\						<dir name=\"3\"> <pipe> <fifo name=\"pipe\"/> </pipe> </dir>
		\						<dir name=\"4\"> <pipe> <fifo name=\"pipe\"/> </pipe> </dir>
		\						<dir name=\"5\"> <pipe> <fifo name=\"pipe\"/> </pipe> </dir>
		\						<dir name=\"6\"> <pipe> <fifo name=\"pipe\"/> </pipe> </dir>
		\					</dir>
		\				</dir>
		\			</vfs>
		\			<default-policy root=\"/export\" writeable=\"yes\"/>
		\		</config>
			"
		;
	# Hai-on-Genode services: broker, based on pipes
	AddComponentService  $(runscenario) : 3M
		: broker_server : Hai_Broker
		:	"
		\		<config ld_verbose=\"yes\">
		\			<libc  rtc=\"/dev/rtc\" stdin=\"/dev/null\" stdout=\"/dev/log\" stderr=\"/dev/log\"/>
		\			<vfs>
		\				<dir name=\"dev\">
		\					<log/>
		\					<null/>
		\					<rtc/>
		\						<!-- VFS server's named pipes: /dev/bro2app/1 and so on -->
		\					<fs label=\"downstream\"/>
		\				</dir>
		\			</vfs>
		\		</config>
		\	<route>
		\		<service name=\"File_system\" label=\"downstream\"> <child name=\"vfs\"/> </service>
		\		<any-service> <parent/> <any-child/> </any-service>
		\	</route>
			"
		;
	# Hai services: registrar, based on broker
	AddComponentClient  $(runscenario)
		: 18M
		: registrar
		: "
		\		<config ld_verbose=\"yes\">
		\			<libc  rtc=\"/dev/rtc\" stdin=\"/dev/null\" stdout=\"/dev/log\" stderr=\"/dev/log\"/>
		\			<vfs>
		\				<dir name=\"dev\">
		\					<log/>
		\					<null/>
		\					<fs label=\"downstream\"/>
		\				</dir>
		\			</vfs>
		\		</config>
		\	<route>
		\		<service name=\"Report\" label=\"system\">		<child name=\"platform_report_rom\"/> </service>
		\		<service name=\"Report\" label=\"dynit_config\">		<child name=\"dynit_report_rom\"/> </service>
		\		<service name=\"Report\" label=\"depotdeploy.config\">	<child name=\"dynit_report_rom\"/> </service>
		\		<service name=\"File_system\" label=\"downstream\">	<child name=\"vfs\"/>		</service>
	#	\		<service name=\"File_system\" label=\"general\">	<child name=\"vfs_ram\"/>	</service>
		\		<any-service> <parent/> <any-child/> </any-service>
		\	</route>
			"
			;
}

# Add "Dynit" and its supporting broker, registrar and FS:
AddStackBeIpc  hog-drivesetup-gpt.run ;

if 1
{
	AddComponentAsStart  hog-drivesetup-gpt.run : 2M :
		vfs : "name=\"vfs_ram\" caps=\"120\"" :
		"		<binary name=\"vfs\" />
		\		<provides> <service name=\"File_system\"/> </provides>
		\		<config ld_verbose=\"no\" >
		\			<vfs>
		\				<dir name=\"boot\">
		\					<dir name=\"apps\">
		\						<dir name=\"genode\">
		\							<rom name=\"part_block\"/>
		\							<rom name=\"gpt_write\"/>
		\							<rom name=\"mkntfs\"/>
	#	\							<rom name=\"nano3d\"/>
		\						</dir>
		\					</dir>
		\				</dir>
		\			</vfs>
		\			<default-policy		root=\"/boot\"		writeable=\"yes\"/>
		\		</config>
		"
		;
	AddDynitLauncher  hog-drivesetup-gpt.run : vfs_ram : 50M ;
}


local ntfs_test_image_path =
	/tmp/hog_test_disk_image_GPT.raw
	;
DiskCreate $(ntfs_test_image_path) ;


RunQemu
	hog-drivesetup-gpt.run
	:
		# CAREFUL TO ONLY EVER PROVIDE A TEST FILE HERE, NOT AN ACTUAL STORAGE DEVICE
		# ! ! ! The run scenario might ERASE all partitions on the provided 'device' ! ! !
		$(ntfs_test_image_path)
		# ! ! ! The run scenario might ERASE all partitions on the provided 'device' ! ! !
		# CAREFUL TO ONLY EVER PROVIDE A TEST FILE HERE, NOT AN ACTUAL STORAGE DEVICE
	;

