flat assembler
Message board for the users of flat assembler.

Index > Main > x64 UEFI support

Goto page 1, 2, 3  Next
Author
Thread Post new topic Reply to topic
XVilka



Joined: 04 May 2011
Posts: 42
Location: Russia
XVilka 29 May 2012, 12:18
Hello!
I'm trying to write small hello world application for UEFI, like in article http://x86asm.net/articles/uefi-programming-first-steps/index.html
But that example doesnt work on any UEFI hardware I have - it have wrong header. Also for UEFI it should not produce MZ DOS stub, just fill it by zeroes.
Code:
format pe64 dll efi
entry main

section '.text' code executable readable

include 'efi.inc'

main:
    ...
section '.data' data readable writeable

Handle      dq ?
SystemTable dq ?
_hello      du 'Hello World',13,10,'(From EFI app written in FASM)',13,10,0

section '.reloc' fixups data discardable
    
Post 29 May 2012, 12:18
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8358
Location: Kraków, Poland
Tomasz Grysztar 29 May 2012, 12:29
You can create the empty stub this way:
Code:
format PE64 EFI DLL on 'nul'    

And what is wrong with the header?
Post 29 May 2012, 12:29
View user's profile Send private message Visit poster's website Reply with quote
XVilka



Joined: 04 May 2011
Posts: 42
Location: Russia
XVilka 29 May 2012, 13:18
Thx! Thats looks better.
Attached hello.efi - invalid UEFI image
Here is output from UEFI Shell:
Code:
fs0:\> load hello.efi
load: fs0:\HELLO.EFI is not a image
Exit status code: Invalid Parameter
    

Also attached valid UEFI image:
Code:
fs0:\> load valid.efi
ReadyToBoot()load: Image fs0:\valid.efi loaded at 2ADD000 - Success
    


Description: valid efi image
Download
Filename: valid.7z
Filesize: 609 Bytes
Downloaded: 880 Time(s)

Description: invalid efi image
Download
Filename: hello.7z
Filesize: 406 Bytes
Downloaded: 814 Time(s)

Post 29 May 2012, 13:18
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 29 May 2012, 16:41
The example I gave worked on early Intel UEFI boards for sure. It is possible they added some extra checks since then. But I don't have UEFI testing board anymore - I am afraid you'd have to figure our what exactly causes problems yourself.
Post 29 May 2012, 16:41
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
XVilka



Joined: 04 May 2011
Posts: 42
Location: Russia
XVilka 29 May 2012, 16:55
btw, updated a bit 'efi.inc' file, attached


Description: UEFI include
Download
Filename: efi.inc
Filesize: 5.58 KB
Downloaded: 945 Time(s)

Post 29 May 2012, 16:55
View user's profile Send private message Visit poster's website Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 514
Location: Czech republic, Slovak republic
Feryno 30 May 2012, 08:53
Hi,
I'll try to run you files at my home PC (AMD FX-4100, ASUS M5A99X EVO with AMI UEFI version 0901) but won't have time to do that earlier than at weekend (I'm currently too busy and outside all free time).
The PC I'm sitting in front of just now has BIOS, not UEFI.
Could you try EFIRUNTIME flag also?
Could you try to copy your program into USB disk into /efi/boot/bootx64.efi (your program should be compiled with EFIBOOT flag then) and boot it?
What MB and CPU do you have? If you have Intel CPU, could you test this please:
http://board.flatassembler.net/topic.php?p=142450#142450
(whether it even runs, it uses some special detection procedures scanning Intel Debug feature MSRs, AMD has them stable but Intel uses 3 versions of these MSRs and maybe more in feature, I didn't want to assign MSRs where are they after cpuid more than 100 various models)
Post 30 May 2012, 08:53
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
XVilka



Joined: 04 May 2011
Posts: 42
Location: Russia
XVilka 30 May 2012, 10:33
Feryno: what kind of output you need? I have both Intel-based and AMD based UEFI boards, both modern.
Btw, are there any way to save output/log of debugger somewhere?
It will be very useful for both using and testing.
(Posted in FDBG thread)

About invalid images - see attached source hello.asm + uefi.inc and produced efi image - invalid efi image (can't be loaded in shell or from option rom)
This is just stub, so can't understand whats wrong.


Description: hello2.asm + uefi.inc + hello2.efi
Download
Filename: uefi.7z
Filesize: 2.13 KB
Downloaded: 878 Time(s)

Post 30 May 2012, 10:33
View user's profile Send private message Visit poster's website Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 514
Location: Czech republic, Slovak republic
Feryno 30 May 2012, 13:22
It will be some flag somewhere
The uefi fdbg is very limited (version 0000), you can't even load your program, must be compiled with the debugger into 1 file
Thank for reporting well behaviour on Intel CPU
Post 30 May 2012, 13:22
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 514
Location: Czech republic, Slovak republic
Feryno 31 May 2012, 05:16
does this help or not at your PC:
Code:
format pe64 dll efiruntime    

or this:
Code:
format pe64 dll efiboot    

personally I also managed to run uefi shell from external drive (USB flash), have also AMI UEFI
from the uefi shell I managed to run only incompiled commands, never succeeded with running any external program
maybe the load command built-in uefi shell is only for loading drivers and not common applications? (so the idea using EFIRUNTIME flag)
Post 31 May 2012, 05:16
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
XVilka



Joined: 04 May 2011
Posts: 42
Location: Russia
XVilka 31 May 2012, 07:19
so, results are same for all
hello2.efi:
Code:
format PE64 dll efi on 'nul'
    
hello3.efi:
Code:
format PE64 dll efiboot on 'nul'
    
hello4.efi:
Code:
format PE64 dll efiruntime on 'nul'
    
*.efi not a image
Produced files have these differences in header:

  • hello2.efi - 0x56 byte at 0x48 offset, 'BA AB 00 00 0A' at 0x98 offset
  • hello3.efi - 0x59 byte at 0x48 offset, 'BE AB 00 00 0B' at 0x98 offset
  • hello4.efi - 0x5D byte at 0x48 offset, 'C3 AB 00 00 0C' at 0x98 offset

First timestamp, then 4 bytes checksum, so produced files differ only in right way - with PE.OptionalHeader.Subsystem at 0x9C offset. strange.
all other looks same
It doesnt start even if i place it in /efi/boot/bootx64.efi
Post 31 May 2012, 07:19
View user's profile Send private message Visit poster's website Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 514
Location: Czech republic, Slovak republic
Feryno 31 May 2012, 10:00
yes, differences are in timestamp+checksum and the last byte means:
0Ah=10=EFI
0B=EFIBOOT
0C=EFIRUNTIME

When I played with efi shell (months ago) I tried to execute external programs directly = without the load command, but never succeeded...
According the video at this page the load command should not be necessary:
http://diit.cz/clanek/instalace-windows-7-x64-pres-efi
look at the video in the middle of the page

I also remembered that something changed in FASM few months ago (maybe year, time passes with different speed for me now, so have bad personal approximations) when added support for large PE images
then I was unable to run win64 drivers, had to revert to older version of FASM, but it lacked some instructions I needed so I used newer version and edited something in FASM source and recompiled the binary
Post 31 May 2012, 10:00
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 514
Location: Czech republic, Slovak republic
Feryno 31 May 2012, 10:07
found that modification necessary for win x64 drivers, luckily have an USB flash disk with me and searched archives with string I rememberer 'compiled with'
; compiled with patched version of fasm 1.69.31
; this had to be modified in FASM:
; formats.inc
; orignal: mov dword [edx+16h],20B002Fh; flags and magic value
; corrected: mov dword [edx+16h],20B002Eh; flags and magic value
; orignal: mov byte [edx+5Eh+1],1
; corrected: mov byte [edx+5Eh+1],0
Post 31 May 2012, 10:07
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8358
Location: Kraków, Poland
Tomasz Grysztar 31 May 2012, 10:30
Feryno wrote:
; orignal: mov dword [edx+16h],20B002Fh; flags and magic value
; corrected: mov dword [edx+16h],20B002Eh; flags and magic value
This difference is the IMAGE_FILE_RELOCS_STRIPPED flag, it gets cleared when you put fixups (even the empty ones) somewhere.

Feryno wrote:
; orignal: mov byte [edx+5Eh+1],1
; corrected: mov byte [edx+5Eh+1],0
This one is IMAGE_DLL_CHARACTERISTICS_NX_COMPAT, currently it is zero by default and is set by specifying "NX" in the "format PE" line.
Post 31 May 2012, 10:30
View user's profile Send private message Visit poster's website Reply with quote
XVilka



Joined: 04 May 2011
Posts: 42
Location: Russia
XVilka 31 May 2012, 11:17
And here attached parsed PE header of valid (successfully booting EFI image, even with "load valid.efi" command from the shell), in CSV format

And here it is available online http://pastie.org/private/nqpnpykoaoq5oawbgkjdja


Description: PE header
Download
Filename: valid_hdr.zip
Filesize: 7.23 KB
Downloaded: 816 Time(s)

Post 31 May 2012, 11:17
View user's profile Send private message Visit poster's website Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 514
Location: Czech republic, Slovak republic
Feryno 31 May 2012, 12:49
found also this difference:
DllCharacteristics:
valid 0
invalid 40h
it is the word after word subsystem
Post 31 May 2012, 12:49
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8358
Location: Kraków, Poland
Tomasz Grysztar 31 May 2012, 13:28
Feryno wrote:
found also this difference:
DllCharacteristics:
valid 0
invalid 40h
it is the word after word subsystem
This is the IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE (which means that DLL can be relocated), which is set in conjunction with resetting the IMAGE_FILE_RELOCS_STRIPPED flag when the fixups are put into your source.
Post 31 May 2012, 13:28
View user's profile Send private message Visit poster's website Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 514
Location: Czech republic, Slovak republic
Feryno 01 Jun 2012, 09:42
IMAGE_OPTIONAL_HEADER64.ImageBase = 0 for valid, 400000h for invalid
XVilka could you test these headers:
Code:
format PE64 dll efi on 'nul' at 0    
Code:
format PE64 dll efiboot on 'nul' at 0    
Post 01 Jun 2012, 09:42
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
XVilka



Joined: 04 May 2011
Posts: 42
Location: Russia
XVilka 01 Jun 2012, 10:48
Of course you mean
Code:
format PE64 dll efi at 0 on 'nul'    

Though both still give me error when trying to do "load" command or just calling directly.
It is strange, that "load fdbg.efi" give me error, that "Image fs0:\fdbg.efi is not a driver", while just typing "fdbg.efi" and pressing enter works ok

So, looks like "load" command works only for drivers, while direct execution works only for applications.
Post 01 Jun 2012, 10:48
View user's profile Send private message Visit poster's website Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 514
Location: Czech republic, Slovak republic
Feryno 01 Jun 2012, 12:40
Yes, it seems the load command is used to load drivers only (EFIRUNTIME should be driver?, EFIBOOT should be boot manager?, EFI should be common program?)
fdbg.efi is compiled as an application (EFI)
I even didn't succeed to run the fdbg.efi using uefi shell, had success only booting it /efi/boot/bootx64.efi
Must try it again, now it is rainy otside so I can not work there and return home to perform some tests.
fdbg.efi has quite huge sections, maybe UEFI refuses to load too small images or images with too small sections
maybe UEFI refuses to load images with empty relocs ? I must test this also.
Post 01 Jun 2012, 12:40
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
XVilka



Joined: 04 May 2011
Posts: 42
Location: Russia
XVilka 04 Jun 2012, 07:24
Feryno: So, any news about testing (you wrote, that you can do weekend)? Or may be new ideas?
Post 04 Jun 2012, 07:24
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2, 3  Next

< Last Thread | Next Thread >
Forum Rules:
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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.