Int2f:
	jmp 11f
	OldInt2f:       .long 0
	.word 0x424B    // signature "KB"
	.byte 0         // flag
	jmp 30f         // EB xx for compat
	.space 7        // padding
	30: lret
11:
	cmpw	$0x4310, %ax
	je	1f
	ljmp	*%cs:OldInt2f
1:	pushw	%cs
	popw	%es
	movw	$XMSHook, %bx
	iret

NewXMSCall:	.long 0

XMSHook:
	jmp 1f
	nop
	nop
	nop
1:
	lcall	*%cs:NewXMSCall  /* call dosemus xms */
	jz	2f
	ljmp	*%cs:OldXMSCall
2:
	lret


InitCodeStart:		/* all the below code gets discarded after init */

HookHimem:
	pushw	%es
	pushw	%di
	pushw	%cs
	popw	%es
	/* check for the XMS driver */
	movw	$0x4300, %ax
	int	$0x2f
	/* 0x8180 is a special code from dosemu2 asking to proceed */
	cmpw	$0x8180, %ax
	je	13f
	cmpb	$0x80, %al
	je	1f
13:
	/* no XMS driver - try to install internal driver */
	movb    $DOS_HELPER_XMS_HELPER, %al
	movb    $XMS_HELPER_XMS_INIT, %ah
	int	$DOS_HELPER_INT
	jc	28f
	movb	$9, %ah
	movw	$XMSMsg, %dx
	int	$0x21
	/* internal driver installed - go out */
	mov	$1, %ax    # indicate no-load
	jmp	10f
1:
	/* get entry point */
	movw	$0xffff, %bx
	movw	$0x4310, %ax
	int	$0x2f
	/* see if the call worked at all */
	cmpw	$0xffff, %bx
	je 27f
	/* see if it is sjmp (0xeb) */
	cmpb	$0xeb, %es:(%bx)
	jne	27f
	/* save old callback address */
	movw	%bx, OldXMSCall
	movw	%es, OldXMSCall+2
	/* check if already initialized */
	movb	$0x10, %ah
	movw	$0xffff, %dx
	lcall	*%cs:OldXMSCall
	orw	%ax, %ax
	jnz	28f		/* success should not happen */
	cmpb	$0x80, %bl
	jne	27f
	/* get new entry point */
	movb    $DOS_HELPER_XMS_HELPER, %al
	movb    $XMS_HELPER_GET_ENTRY_POINT, %ah
	int	$DOS_HELPER_INT
	jc	28f
	/* check if the hook was initially from dosemu */
	movw	%es, %ax
	cmpw	%ax, OldXMSCall+2
	je	27f
	/* save the new addr */
	movw	%bx, NewXMSCall
	movw	%es, NewXMSCall+2
	/* install int2f hook */
	movb	$0x35, %ah
	movb	$0x2f, %al
	int	$0x21
	movw	%bx, OldInt2f
	movw	%es, OldInt2f+2
	movb	$0x25, %ah
	movb	$0x2f, %al
	movw	$Int2f, %dx
	int	$0x21
	/* all done, UMB should work */
	clc
	mov	$0, %ax    # indicate load
	jmp	10f

27:
	movb	$9, %ah
	movw	$CantHookMsg, %dx
	int	$0x21
	jmp	9f
28:
	movb	$9, %ah
	movw	$NoXMSMsg, %dx
	int	$0x21
#	jmp	9f
9:
	stc			# report error
10:
	popw	%di
	popw	%es
	ret

NoXMSMsg:
	.ascii	"Note: XMS disabled in the config.\r\n$"

XMSMsg:
	.ascii	"dosemu XMS 3.0 & UMB support enabled\r\n$"

CantHookMsg:
	.ascii	"Unable to hook into himem.sys, UMB disabled.\r\n"
	.ascii	"Make sure himem.sys is loaded right before umb.sys in "
	.ascii  "your config.sys.\r\n$"
