Page 1 of 1

Load DOS exe

Posted: Tue Mar 09, 2010 3:00 pm
by Firestryke31
I must admit, my Google-fu skills are poor today. I can't find how to do this.

I managed to find a couple of sites explaining what the various fields in the MZ header meant (though not to any great detail) but nothing on actually loading and running a MS-DOS MZ exe file. The reason I want this is because I want to have my bootloader run the MZ part of a PE exe, which will then parse and run the PE part. The PE part will be written in such a way that hopefully it will be loader-agnostic so I can use it with my loader, GRUB, and IIRC EFI (or UEFI or whatever it's called).

TL;DR: What's the bare minimum I need to run a MZ exe assuming it's being run from a boot sector?

Re: Load DOS exe

Posted: Tue Mar 09, 2010 3:11 pm
by Dex
This will run a mz exe or com file, that is loaded at ImageLoadSeg

Code: Select all

;;;;;;;;;;;;;;;;;;;

;; Type checking ;;

;;;;;;;;;;;;;;;;;;;



	cli				; for stack adjustments

	mov	ax, ImageLoadSeg

	mov	es, ax

	cmp	word [es:0], 5A4Dh	; "MZ" signature?

	je	RelocateEXE		; yes, it's an EXE program



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Setup and Run COM program ;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



	mov	ax, es

	sub	ax, 10h 		; "org 100h" stuff :)

	mov	es, ax

	mov	ds, ax

	mov	ss, ax

	xor	sp, sp

	push	es

	push	word 100h

	jmp	Run



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Relocate, setup and run EXE program ;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



RelocateEXE:

	mov	ds, ax

	add	ax, [ds:08h]		; ax = image base

	mov	cx, [ds:06h]		; cx = reloc items

	mov	bx, [ds:18h]		; bx = reloc table pointer

	jcxz	RelocationDone

ReloCycle:

	mov	di, [ds:bx]		; di = item ofs

	mov	dx, [ds:bx+2]		; dx = item seg (rel)

	add	dx, ax			; dx = item seg (abs)

	push	ds

	mov	ds, dx			; ds = dx

	add	[ds:di], ax		; fixup

	pop	ds

	add	bx, 4			; point to next entry

	loop	ReloCycle

RelocationDone:

	mov	bx, ax

	add	bx, [ds:0Eh]

	mov	ss, bx			; ss for EXE

	mov	sp, [ds:10h]		; sp for EXE

	add	ax, [ds:16h]		; cs

	push	ax

	push	word [ds:14h]		; ip

Run:

	mov	dl, [cs:bsDriveNumber]	; let program know boot drive

	mov	dh, 0xff		; let DexOS know it booted from bootprog

	sti

	retf


Also google "bootprog" for full code

Re: Load DOS exe

Posted: Tue Mar 09, 2010 3:33 pm
by Firestryke31
Thank you. I was forgetting the relocations as well as the image base adjustment, so hopefully now I can figure out how to reduce my FAT16 bootsector size enough to support finding the file, loading the file, and parsing/running the file. Then I get to do it again for FAT12 and FAT32, and maybe even NTFS. Fun! :mrgreen: