OSDev.org

The Place to Start for Operating System Developers
It is currently Mon Aug 03, 2020 9:39 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Load DOS exe
PostPosted: Tue Mar 09, 2010 3:00 pm 
Offline
Member
Member
User avatar

Joined: Sat Nov 29, 2008 1:07 pm
Posts: 550
Location: Throw a dart at central Texas
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?

_________________
Owner of Fawkes Software.
Wierd Al wrote:
You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?


Top
 Profile  
 
 Post subject: Re: Load DOS exe
PostPosted: Tue Mar 09, 2010 3:11 pm 
Offline
Member
Member
User avatar

Joined: Fri Jan 27, 2006 12:00 am
Posts: 1444
This will run a mz exe or com file, that is loaded at ImageLoadSeg
Code:
;;;;;;;;;;;;;;;;;;;

;; 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


Top
 Profile  
 
 Post subject: Re: Load DOS exe
PostPosted: Tue Mar 09, 2010 3:33 pm 
Offline
Member
Member
User avatar

Joined: Sat Nov 29, 2008 1:07 pm
Posts: 550
Location: Throw a dart at central Texas
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:

_________________
Owner of Fawkes Software.
Wierd Al wrote:
You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group