flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > fasmg - Add EFI x64 sample to x86

Author
Thread Post new topic Reply to topic
Akeo



Joined: 19 Aug 2016
Posts: 8
Location: Ireland
Akeo 19 Aug 2016, 10:31
Hi,

I like the work being conducted with the generic assembler, and the whole design concept behind it, so, since I'm playing with UEFI, I created a new x86 sample for EFI (for x86_64) called efi64.asm, if you want to consider it for inclusion. With this, I think the x86 samples could provide a comprehensive portfolio of what people may be interested in.

Before the sample can be added however, the following modification needs to be applied to examples/x86/include/format/format.inc, to enable the EFI subsystem for PE.

Also, since the sample is using 'du', a new utf8.inc should probably be added to examples/x86/include/

Finally, the sample also includes an efi.inc reference to make it more readable. Should you decide to add the sample, I leave it up to you whether you want to add that .inc to the include directory, leave it at the root of the samples or just copy paste its content into efi64.asm and remove the reference.

Oh, and if you are interested, I have the whole thing on github along with some instructions on how you can test the sample with qemu on Windows.

At any rate, thank you very much for fasmg - it's awesome!
Post 19 Aug 2016, 10:31
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 19 Aug 2016, 11:38
Akeo wrote:
I like the work being conducted with the generic assembler, and the whole design concept behind it, so, since I'm playing with UEFI, I created a new x86 sample for EFI (for x86_64) called efi64.asm, if you want to consider it for inclusion. With this, I think the x86 samples could provide a comprehensive portfolio of what people may be interested in.
I decided to stop extending the examples in the basic package, as I wanted to keep them relatively simple and release more complex ones (like conversions of fasm's macro-heavy Windows examples) in separate packages.

I think we really need a common hub for such projects, to create the "comprehensive portfolio" you mentioned. Shoorick is doing great job with some older CPUs/microcontrollers, it would be great to gather it all in one place.

Akeo wrote:
Before the sample can be added however, the following modification needs to be applied to examples/x86/include/format/format.inc, to enable the EFI subsystem for PE.
You could also use the fasmg's PE formatting macros directly and include something like this in your headers (like efi.inc):
Code:
PE.Settings.Magic = 0x20B
PE.Settings.Subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION
PE.Settings.Machine = IMAGE_FILE_MACHINE_AMD64
PE.Settings.MajorSubsystemVersion = 5
PE.Settings.MinorSubsystemVersion = 0
; ...
include 'format/pe.inc'
use64    
I'm mentioning this, because this PE formatter gives you much greater flexibility that one from original fasm (for instance you can alter things like SectionAlignment/FileAlignment), and the "format" macro only emulates the legacy syntax of fasm that has much fewer options.

Akeo wrote:
Also, since the sample is using 'du', a new utf8.inc should probably be added to examples/x86/include/
I planned to have macros like this one in the separate package with Win32/Win64 headers (but it is far from being ready, unfortunately). I think that this also shows that having a common hub would be a good idea - we could perhaps avoid writing similar macros independently.

Akeo wrote:
Finally, the sample also includes an efi.inc reference to make it more readable. Should you decide to add the sample, I leave it up to you whether you want to add that .inc to the include directory, leave it at the root of the samples or just copy paste its content into efi64.asm and remove the reference.

Oh, and if you are interested, I have the whole thing on github along with some instructions on how you can test the sample with qemu on Windows.

At any rate, thank you very much for fasmg - it's awesome!

Great! I was hoping to see more works like this. And I am more than happy to hear that there are people liking my "insane" ideas of fasmg. Wink

Also: have you tried my struct macro for fasmg? It is a plain and simple macro that has many qualites of fasm's macro with the same name.
Post 19 Aug 2016, 11:38
View user's profile Send private message Visit poster's website Reply with quote
Akeo



Joined: 19 Aug 2016
Posts: 8
Location: Ireland
Akeo 19 Aug 2016, 17:43
Tomasz Grysztar wrote:
I decided to stop extending the examples in the basic package, as I wanted to keep them relatively simple and release more complex ones (like conversions of fasm's macro-heavy Windows examples) in separate packages.

I can understand where you're coming from.
Quote:
I think we really need a common hub for such projects

+1

fasm/fasmg users need to be able to find as many samples as possible in a central location, that we can expand on. There's nothing worse than not being aware that a code sample exists, that already has the basics of what you're trying to do.
Akeo wrote:
You could also use the fasmg's PE formatting macros directly and include something like this in your headers (...) I'm mentioning this, because this PE formatter gives you much greater flexibility that one from original fasm (for instance you can alter things like SectionAlignment/FileAlignment), and the "format" macro only emulates the legacy syntax of fasm that has much fewer options.

That's good to know. But I'd still like to see the EFI option added to the official PE/format header for fasmg, on account that this feature was available in fasm, and while fasmg offers more flexibility, it'd be nice to at least have it on par with fasm by default.
Quote:
I was hoping to see more works like this. And I am more than happy to hear that there are people liking my "insane" ideas of fasmg. Wink

I saw that post of yours, and hopefully I'll have something that is right up the alley of what you'd like to see listed there some time in the future (that x86_64 sample was actually a stepping stone towards that). I'll let you know then.
Quote:
Also: have you tried my struct macro for fasmg?

Not yet, but I think I'll have a closer look at that for my other fasmg project... Wink
Post 19 Aug 2016, 17:43
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 19 Aug 2016, 20:27
Akeo wrote:
That's good to know. But I'd still like to see the EFI option added to the official PE/format header for fasmg, on account that this feature was available in fasm, and while fasmg offers more flexibility, it'd be nice to at least have it on par with fasm by default.
You are right that it's better to have it complete. I added "EFI", "EFIboot" and "EFIruntime" settings, because all are supported by fasm, and at the same time I simplified the macro a bit. The updated fasmg package is ready for downloading.
There is one more thing that fasm's formatter has but these macro currently do not provide - it's the "resource from" construction. I may implement it for that Windows headers package, when I finally make one.
Post 19 Aug 2016, 20:27
View user's profile Send private message Visit poster's website Reply with quote
Akeo



Joined: 19 Aug 2016
Posts: 8
Location: Ireland
Akeo 19 Aug 2016, 20:57
Awesome!

I tested the latest and everything looks good. Many thanks! Smile
Post 19 Aug 2016, 20:57
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 20 Aug 2016, 16:52
I also suggest minor corrections to your utf8.inc:
Code:
; UTF-8

macro du? arg
  local current,__input,char
  if arg eqtype ''
    virtual at 0
      __input::
      db arg
      count = $
    end virtual
    current = 0
    while current < count
      load char byte from __input:current
      wide = char
      current = current + 1
      if char > 0C0h
        if char < 0E0h
          wide = char and 11111b
          load char byte from __input:current
          wide = wide shl 6 + (char and 111111b)
          current = current + 1
        else if char < 0F0h
          wide = char and 1111b
          load char byte from __input:current
          wide = wide shl 6 + (char and 111111b)
          load char byte from __input:current+1
          wide = wide shl 6 + (char and 111111b)
          current = current + 2
        else if char < 0F8h
          wide = char and 111b
          load char byte from __input:current
          wide = wide shl 6 + (char and 111111b)
          load char byte from __input:current+1
          wide = wide shl 6 + (char and 111111b)
          load char byte from __input:current+2
          wide = wide shl 6 + (char and 111111b)
          current = current + 3
        else if char < 0FCh
          wide = char and 11b
          load char byte from __input:current
          wide = wide shl 6 + (char and 111111b)
          load char byte from __input:current+1
          wide = wide shl 6 + (char and 111111b)
          load char byte from __input:current+2
          wide = wide shl 6 + (char and 111111b)
          load char byte from __input:current+3
          wide = wide shl 6 + (char and 111111b)
          current = current + 4
        else
          wide = char and 1
          load char byte from __input:current
          wide = wide shl 6 + (char and 111111b)
          load char byte from __input:current+1
          wide = wide shl 6 + (char and 111111b)
          load char byte from __input:current+2
          wide = wide shl 6 + (char and 111111b)
          load char byte from __input:current+3
          wide = wide shl 6 + (char and 111111b)
          load char byte from __input:current+4
          wide = wide shl 6 + (char and 111111b)
          current = current + 5
        end if
      end if
      if wide < 10000h
        dw wide
      else
        dw 0D7C0h + wide shr 10,0DC00h or (wide and 3FFh)
      end if
    end while
  else
    dw arg
  end if
end macro

macro du? args&
  iterate arg,args
    du arg
  end iterate
end macro

struc du? args&
   label . word
   du args
end struc    
You could then rewrite your data like this:
Code:
        Hello           du 0x0D, 0x0A
                        du "Hello EFI World!", 0x0D, 0x0A, 0x0D, 0x0A
                        du "Press any key to exit", 0x0D, 0x0A
                        du 0x00     
Post 20 Aug 2016, 16:52
View user's profile Send private message Visit poster's website Reply with quote
Akeo



Joined: 19 Aug 2016
Posts: 8
Location: Ireland
Akeo 20 Aug 2016, 22:06
Nice one. Very Happy

I already knew the utf8.inc I had was limited, but you solved exactly the issue that still annoyed me with multiple data items. Thank you very much for this!!

I have now applied your suggested changes to the sample.
Post 20 Aug 2016, 22:06
View user's profile Send private message Visit poster's website Reply with quote
Kevin_Zheng



Joined: 04 Jul 2003
Posts: 125
Location: China
Kevin_Zheng 20 Sep 2017, 13:38
Hi,Thomas:
The newest fasmg complier (version: hxhsr) can't complier the efi.inc, it will throw error messages:

Code:
efi.inc
struc int64
  align 8
  . dq ?
end struc
    


fasmg efi64.asm
flat assembler version g.hxhsr
efi64.asm [8] efi.inc [87]:
struct EFI_TABLE_HEADER
macro struct [2] macro EFI_TABLE_HEADER [1] macro int64 [1]:
align 8
Processed: align 8
Error: illegal instruction.

But the previous fasmg can compiler it okay, no error. Could you help me to check it?

Thank you a lot .

Kenneth Zheng


Description:
Download
Filename: efi64.asm
Filesize: 1.58 KB
Downloaded: 2268 Time(s)

Description:
Download
Filename: efi.inc
Filesize: 3.38 KB
Downloaded: 2117 Time(s)


_________________
Pure Assembly Language Funs
Post 20 Sep 2017, 13:38
View user's profile Send private message MSN Messenger Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 20 Sep 2017, 14:20
The sources you provided assembled correctly for me (I used UTF8.INC from the Windows headers package):
Code:
>fasmg efi64.asm
flat assembler  version g.hxhsr
3 passes, 1536 bytes.    
Post 20 Sep 2017, 14:20
View user's profile Send private message Visit poster's website Reply with quote
Kevin_Zheng



Joined: 04 Jul 2003
Posts: 125
Location: China
Kevin_Zheng 20 Sep 2017, 14:46
Thanks Thomas quickly helpping about it Smile Please see below steps to dupicate the complier error messages: (I've put the efi64.asm and efi.inc into this folder: c:\fasmg\examples\x86\uefi)

Code:
C:\>set path=c:\fasmg
C:\>cd c:\fasmg\examples\x86
C:\fasmg\examples\x86>make.cmd
cd uefi
fasmg efi64.asm    

flat assembler version g.hxhsr
efi64.asm [8] efi.inc [87]:
struct EFI_TABLE_HEADER
macro struct [2] macro EFI_TABLE_HEADER [1] macro int64 [1]:
align 8
Processed: align 8
Error: illegal instruction.


The root cause is that the make.cmd will set "@set include=include", it will affect the fasmg.exe to report above complier error. If we don't use this line "@set include=include", it will complier okay.

Thanks.

Kenneth Zheng

_________________
Pure Assembly Language Funs
Post 20 Sep 2017, 14:46
View user's profile Send private message MSN Messenger Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 20 Sep 2017, 17:15
Then probably your "include" directory contains invalid version of some needed include files.
Please try increasing the number of displayed error with parameter like "-e100". Perhaps you may find the true source of the problem.
Post 20 Sep 2017, 17:15
View user's profile Send private message Visit poster's website Reply with quote
Kevin_Zheng



Joined: 04 Jul 2003
Posts: 125
Location: China
Kevin_Zheng 21 Sep 2017, 01:41
Hi, Thomas:
Yes, you are right. The root cause is that the efi.inc will use "align" macro, but the "align" macro in the "C:\fasmg\examples\x86\include\format\elfexe.inc", so the efi.inc willl report "align 8 Processed: align 8 Eror: illegal instruction."..

Another question is : include instruction supports several directores? for example:
Code:
set  include=include;..\include;\fasmg\examples\include    


Thank you a lot.



Kenneth Zheng

_________________
Pure Assembly Language Funs
Post 21 Sep 2017, 01:41
View user's profile Send private message MSN Messenger Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 21 Sep 2017, 07:06
Kevin_Zheng wrote:
Another question is : include instruction supports several directores? for example:
Code:
set  include=include;..\include;\fasmg\examples\include    
Yes, exactly like that. Did I forget to document it? My bad.
Post 21 Sep 2017, 07:06
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:  


< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.