;Entry:
; R11 - command tail
;Exit:
; R0-R10 - corrupt
; R11, R12 - preserved

.Entry_ADFMount_JFD
 LDR     R0, [R12, #GZ_File_Handle - VARS]	;file handle
 ADR     R1, JFD_tmp			;buffer address

 MOV     R2, #JFD_Track_Table + 4	;bytes to read
 SWI     XZLib_GZRead

 LDR     R2, [R1, #JFD_Track_Table]	;get length of header
 SUB     R2, R2, #JFD_Track_Table + 4	;take off what we've already read
 ADD     R1, R1, #JFD_Track_Table + 4	;move to correct location
 LDR     R0, [R12, #GZ_File_Handle - VARS]	;file handle
 SWI     XZLib_GZRead			;read remainder of header
 SUB     R1, R1, #JFD_Track_Table + 4	;correct location

 MOV     R10, #module_version
 LDR     R2, [R1, #JFD_MinVersion]	;check if we support this version
 CMP     R2, R10			;is it less than our module version no.?
 ADRHI   R0, Error_ADFFS_Version	;NO
 BHI     Entry_ADFMount_Exit		; |+ exit on error

 LDR     R9, [R1, #JFD_Track_Table]	;R9 = offset to track table
 LDR     R10, [R1, #JFD_Sector_Table]	;R10 = offset to sector table
 LDR     R5, [R1, #JFD_Data]		;R5 = offset to data table
 LDR     R6, [R1, #JFD_Size]		;R6 = size of file
 SUB     R4, R6, R5			;R4 = size of data table with obey

 LDR     R2, [R12, #File_Size_Limit - VARS]
 CMP     R6, R2				;is filesize above our limit?
 MOVHI   R0, #&80000000			;YES, set V
 ADDHIS  R0, R0, R0			;  |
 ADRVS   R0, Error_File_Size		;  |
 SUBVC   R3, R5, R9			;R3 = size of Track and Sector tables
 ADDVC   R4, R4, R3			;final size required
 BLVC    MemAlloc			;allocate memory for JFD
 SUBVC   R4, R4, R3			;correct R4 to be size of data + Obey
 BVS     Entry_ADFMount_Exit		;  |+ exit on error

 LDR     R7, [R12, #DA_address - VARS]	;increase start of buffer
 ADD     R1, R7, R3			;by size of JFD track buffer
 STR     R1, [R12, #ADF_Buffer_Address - VARS]	;R7 = Track Table address
 SUB     R0, R10, R9			;R0 = size of track table
 ADD     R8, R7, R0			;R8 = Sector Table address
 STR     R7, [R12, #APD_Tracks - VARS]
 STR     R8, [R12, #APD_Sectors - VARS]

 LDR     R0, [R12, #GZ_File_Handle - VARS]	;go to Track table
 MOV     R1, R9
 MOV     R2, #0
 SWI     XZLib_GZSeek

 LDR     R0, [R12, #GZ_File_Handle - VARS]	;read Track and Sector table in one go
 MOV     R1, R7				;address to load
 SUB     R2, R5, R9			;bytes to read
 SWI     XZLib_GZRead

 LDR     R0, [R12, #GZ_File_Handle - VARS]	;go to Data table
 MOV     R1, R5
 MOV     R2, #0
 SWI     XZLib_GZSeek

 MOV     R2, R4				;bytes to read

 ADR     R1, JFD_tmp			;buffer address
 LDR     R0, [R1, #JFD_Obey_Length]	;length of obey file
 TEQ     R0, #0				;is there an obey file?
 SUBNE   R0, R2, R0			;YES, R0=offset in buffer to Obey file
 STR     R0, [R12, #obey_file_offset - VARS]

 LDR     R0, [R12, #GZ_File_Handle - VARS]	;read data
 LDR     R1, [R12, #ADF_Buffer_Address - VARS]	;address to load
 SWI     XZLib_GZRead

 MOV     R2, #3
 STR     R2, [R12, #ADF_Mounted - VARS]	;inform DiscOp code its an JFD
 STR     R2, [R12, #ADF_Changed - VARS]	;inform FileCore the disc has changed

					;*****************************
					;check if the WIMP is running
 MOV     R0, #WimpSysInfo_ActiveTasks	;get number of WIMP tasks
 SWI     XWimp_ReadSysInfo		;PRM3-216
 TEQ     R0, #0				;R0>0 if WIMP is active
 BEQ     Entry_ADFMount_mounted		;NO, exit
					;*****************************

					;WIMP is running, prevent bad !Boot
					;via override if one exists

 ADR     R3, JFD_tmp			;JFD header
 LDR     R0, [R3, #JFD_ID]		;JFD Fxxxxxx
 ADR     R1, _jfd_number		;buffer address
 MOV     R2, #7				;length of buffer
 SWI     XOS_ConvertCardinal4		;PRM1-483

 LDR     R0, [R3, #JFD_Sequence]	;JFD floppy no
 AND     R0, R0, #&FF			;mask down to <99
 CMP     R0, #10
 MOVLT   R2, #ASC("0")			;pad with 0 if necessary
 STRLTB  R2, [R1], #1
 MOV     R2, #2				;length of buffer
 SWI     XOS_ConvertCardinal1

 ADR     R1, _filename			;filename
 MOV     R0, #23			;no path
 SWI     XOS_File			;PRM2-46
 TEQ     R0, #0				;does an override exist?
 BEQ     Entry_ADFMount_mounted		;NO, exit

 MOV     R0, #9				;find first !Application
 ADR     R2, _leaf
 MOV     R3, #1
 MOV     R4, #0
 MOV     R5, #_size - _leaf
 ADR     R6, _pling
 SWI     XOS_GBPB			;PRM2-73

 TEQ     R3, #0				;did we find a !Application
 BEQ     Entry_ADFMount_mounted		;NO, exit

 MOV     R0, #ASC(".")			;add folder path
 STRB    R0, _terminator		;remove terminator

 MOV     R0, #200			;create a WIMP task to workaround
 LDR     R1, _task			;a "feature" of Filer_Boot
 ADR     R2, _task
 MOV     R3, #0
 SWI     XWimp_Initialise		;PRM3-85
 STMFD   R13!, {R1-R12}			;preserve R11, R12 and our task ID

 ADR     R0, _filer_boot
 SWI     XOS_CLI			;execute Filer_Boot

 LDMFD   R13!, {R0,R2-R12}		;preserve registers
 LDR     R1, _task			;close task
 SWI     XWimp_CloseDown		;PRM3-172
B       Entry_ADFMount_mounted

 ._task		DCB "TASK"
 ._pling        DCB "!*", 0
 ._filer_boot	DCB "Filer_Boot "
 ._filename	DCB "ADFFS:override.F"
 ._jfd_number	DCB "1111122"
 ._terminator	DCB "."
 ._leaf		DBB 32
 ._size
 ALIGN