Is there a better way for uefi on windows than edk2?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
wyj
Posts: 11
Joined: Thu Nov 27, 2014 8:23 pm

Is there a better way for uefi on windows than edk2?

Post by wyj »

Since the interface of EDK2 is so strange and troublesome...
Is there a way to link .obj files directly to .efi,
or is there a way to convert a particularly designed .dll or .exe into .efi ?

I know FASM can generate EFI images in a sample way, but can't find a "-S" option for cl

I guess a EFI image has little difference with a DLL image, for they're all PE format and x64 native code,
so maybe a manually modification can be a solution ?

BTW, Is there sth. really in EFI Byte Code ?

:)
no92
Member
Member
Posts: 307
Joined: Wed Oct 30, 2013 1:57 pm
Libera.chat IRC: no92
Location: Germany
Contact:

Re: Is there a better way for uefi on windows than edk2?

Post by no92 »

As always in programming, there's not the single one best answer. What I prefer is creating my own library and compiling it as an ELF. You can use objcopy to get the proper UEFI format. I tested this once and it worked flawlessly.

I have no idea about Windows tools, but as always, having a Linux box will help you dramatically.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Is there a better way for uefi on windows than edk2?

Post by xenos »

no92 wrote:What I prefer is creating my own library and compiling it as an ELF. You can use objcopy to get the proper UEFI format.
I like this method. Which configure parameters (target triplet?) did you use when compiling objcopy / binutils, and with which parameters do you use it for the conversion to uefi?
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Is there a better way for uefi on windows than edk2?

Post by Combuster »

Yes, EDK2 is a horrible example of how not to do things, using a proper PE-oriented toolchain is certainly the better alternative. In particular:
What I prefer is creating my own library and compiling it as an ELF. You can use objcopy to get the proper UEFI format
EDK2 on linux does exactly that, and it results in defective binaries.

Since you seem to be using Visual Studio, you're probably interested in it's /SUBSYSTEM linker option. To be honest, I wouldn't be surprised if that actually worked easier than a GNU toolchain for this matter.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Is there a better way for uefi on windows than edk2?

Post by jnc100 »

XenOS wrote:
no92 wrote:What I prefer is creating my own library and compiling it as an ELF. You can use objcopy to get the proper UEFI format.
I like this method. Which configure parameters (target triplet?) did you use when compiling objcopy / binutils, and with which parameters do you use it for the conversion to uefi?
I would be slightly cautious here. Whilst you may well get a PE file with the appropriate headers, note that the ABI used to compile the original code will still be the gcc default (probably sysV i386/AMD x64) rather than the Microsoft equivalent (which you need for UEFI). What this means is that the arguments to functions will be in the wrong place, and the stack alignment may be different, some registers may not be preserved across functions etc. This is only a problem when calling from your code to UEFI (and having UEFI functions call back to your own code e.g. with timers). There is a hack called gnu-efi which uses a stub function to rearrange function arguments for you, then uses objcopy, but it is not particularly robust.

There is an article on using gcc for UEFI here.

Regards,
John.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Is there a better way for uefi on windows than edk2?

Post by xenos »

Thanks for providing this information. I didn't know that UEFI uses a different ABI - in fact, I haven't used UEFI before, I'm just curious to support it in my OS. Well, actually using a toolchain targeted for "x86_64-w64-mingw32" and fancy file system tools looks a bit horrible to me (not in the sense that it would be difficult, but it somehow looks like using a sledgehammer to crack a nut), but as far as I understood, there is only limited / broken support for UEFI targets in GCC / binutils.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Is there a better way for uefi on windows than edk2?

Post by jnc100 »

Its been a while since I last looked at it. I've removed the dependency on mkfat to use mtools instead.

As not using the mingw32 toolchain, I suppose its possible to use the standard x86_64-elf cross-compiler with the -mabi=ms option and then objcopy it to a PE file, but I haven't had a chance to look into this.

Regards,
John.
no92
Member
Member
Posts: 307
Joined: Wed Oct 30, 2013 1:57 pm
Libera.chat IRC: no92
Location: Germany
Contact:

Re: Is there a better way for uefi on windows than edk2?

Post by no92 »

XenOS wrote:I like this method. Which configure parameters (target triplet?) did you use when compiling objcopy / binutils, and with which parameters do you use it for the conversion to uefi?
It's been a while since I did that - but as far as I can remember that was really tricky. I don't remember any more details.

What I do right now is using a mingw32-w64 gcc/binutils, which outputs a perfectly valid binary. This works perfectly for me.
wyj
Posts: 11
Joined: Thu Nov 27, 2014 8:23 pm

Re: Is there a better way for uefi on windows than edk2?

Post by wyj »

Combuster wrote:Yes, EDK2 is a horrible example of how not to do things, using a proper PE-oriented toolchain is certainly the better alternative. In particular:
What I prefer is creating my own library and compiling it as an ELF. You can use objcopy to get the proper UEFI format
EDK2 on linux does exactly that, and it results in defective binaries.

Since you seem to be using Visual Studio, you're probably interested in it's /SUBSYSTEM linker option. To be honest, I wouldn't be surprised if that actually worked easier than a GNU toolchain for this matter.
I've just tried to compile with Visual Studio subsystem EFI application,
only to be told the missing of some #defines found in auto generated files when compile with EDK2...

including,
_PCD_GET_MODE_*
_PCD_VALUE_*
_PCD_TOKEN_*

such as

#define _PCD_TOKEN_PcdVerifyNodeInList 23U
#define _PCD_GET_MODE_BOOL_PcdVerifyNodeInList _gPcd_FixedAtBuild_PcdVerifyNodeInList

EDK2 generate them with python...and _gPcd_*s seems to...as its name...fixed at build...
Post Reply