flat assembler
Message board for the users of flat assembler.

Index > OS Construction > [solved] Problem with custom struc and macro

Author
Thread Post new topic Reply to topic
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 08 Apr 2020, 20:55
Hello, today to make UEFI coding more easier, i have following UEFI fasm implementation to make my own library.

But when i use it, i have an error when i use custom types. Why ?

Code:
struc EFIInteger8
{
    .db ?
}

struc EFIInteger16
{
    align 2
    .dw ?
}

struc EFIInteger32
{
    align 4
    .dd ?
}

struc EFIInteger64
{
    align 8
    .dq ?
}

macro EFITable name
{
  virtual at 0
    name name
  end virtual
}
    


Code:
struc EFITableHeader
{
    .Signature:     EFIInteger64
    .Revision:      EFIInteger32
    .HeaderSize:    EFIInteger32
    .CRC32:         EFIInteger32
    .Reserved:      EFIInteger32
}

EFITable EFITableHeader
    
Post 08 Apr 2020, 20:55
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 804
Location: Russian Federation, Sochi
ProMiNick 08 Apr 2020, 21:38
. not a fasm special symbol & shoud be separated from data directive.
".Signature: EFIInteger64" labelless definition - required EFIInteger64 to be implemented as macro not struc.
".Signature EFIInteger64" labeled definition - required EFIInteger64 to be implemented as struc & as thou see here no colon.
Post 08 Apr 2020, 21:38
View user's profile Send private message Send e-mail Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 08 Apr 2020, 22:57
Thanks, your advice solve my problem Smile

But now i need help. I use my library to set correctly UEFI fonction. It's work fine to print some character string, but when i try to change the console color, nothing work.... Why ? (i have put comment into code)



Code:
format pe64 efi
entry Main

section '.text' code readable executable

Main:
;mov rcx,[rdx+EFISystemTable.ConOut]
;mov rax,[rcx+EFISimpleTextOutputProtocol.SetAttribute]
;mov rdx,0010000b
;sub rsp,0x20
;call rax
;add rsp,0x20

mov rcx,[rdx+EFISystemTable.ConOut]
mov rax,[rcx+EFISimpleTextOutputProtocol.OutputString]
mov rdx,SystemMessage
sub rsp,0x20
call rax
add rsp,0x20

;mov rcx,[SystemTable+EFISystemTable.ConOut]
;mov rax,[rcx+EFISimpleTextOutputProtocol.SetAttribute]
;mov rdx,Message
;sub rsp,1111000b
;call rax
;add rsp,0x20

mov rcx,[rdx+EFISystemTable.ConOut]
mov rax,[rcx+EFISimpleTextOutputProtocol.OutputString]
mov rdx,Message
sub rsp,0x20
call rax
add rsp,0x20

jmp $

section '.data' data readable writable

include "UEFIDataTypes.fasm"
include "EFITableHeader.fasm"
include "EFISystemTable.fasm"
include "EFISimpleTextOutputProtocol.fasm"

SystemMessage:  du '* EFI Boot: '
Message:        du 'Test OK',0xD,0xA,0x0
    
Post 08 Apr 2020, 22:57
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 08 Apr 2020, 23:01
Fulgurance wrote:
Code:
sub rsp,1111000b    
Question
Post 08 Apr 2020, 23:01
View user's profile Send private message Visit poster's website Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 09 Apr 2020, 07:41
Hum yes... I need to sleep i think... I test that
Thanks you !
Post 09 Apr 2020, 07:41
View user's profile Send private message Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 09 Apr 2020, 08:24
I have updated my code, but the same problem. When i would like to call setAttribute EFI function, nothing append, and i haven't any text in screen... (text function work without)

Code:
format pe64 efi
entry Main

section '.text' code readable executable

Main:
mov rcx,[rdx+EFISystemTable.ConOut]
mov rax,[rcx+EFISimpleTextOutputProtocol.SetAttribute]
mov rdx,0010000b
sub rsp,0x20
call rax
add rsp,0x20

mov rcx,[rdx+EFISystemTable.ConOut]
mov rax,[rcx+EFISimpleTextOutputProtocol.OutputString]
mov rdx,SystemMessage
sub rsp,0x20
call rax
add rsp,0x20

mov rcx,[rdx+EFISystemTable.ConOut]
mov rax,[rcx+EFISimpleTextOutputProtocol.SetAttribute]
mov rdx,1111000b
sub rsp,0x20
call rax
add rsp,0x20

mov rcx,[rdx+EFISystemTable.ConOut]
mov rax,[rcx+EFISimpleTextOutputProtocol.OutputString]
mov rdx,Message
sub rsp,0x20
call rax
add rsp,0x20

jmp $

section '.data' data readable writable

include "UEFIDataTypes.fasm"
include "EFITableHeader.fasm"
include "EFISystemTable.fasm"
include "EFISimpleTextOutputProtocol.fasm"

SystemMessage:  du '* EFI Boot: '
Message:        du 'Test OK',0xD,0xA,0x0
    
Post 09 Apr 2020, 08:24
View user's profile Send private message Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 09 Apr 2020, 08:27
My implementation:
Code:
struc EFIInteger8
{
    . db ?
}

struc EFIInteger16
{
    align 2
    . dw ?
}

struc EFIInteger32
{
    align 4
    . dd ?
}

struc EFIInteger64
{
    align 8
    . dq ?
}

macro EFITable name
{
    virtual at 0
    name name
    end virtual
}
    
Post 09 Apr 2020, 08:27
View user's profile Send private message Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 09 Apr 2020, 08:28
Code:
struc EFITableHeader
{
    .Signature     EFIInteger64
    .Revision      EFIInteger32
    .HeaderSize    EFIInteger32
    .CRC32         EFIInteger32
    .Reserved      EFIInteger32
}

EFITable EFITableHeader
    
Post 09 Apr 2020, 08:28
View user's profile Send private message Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 09 Apr 2020, 08:28
Code:
struc EFISystemTable
{
    .Hdr                   EFITableHeader
    .FirmwareVendor        EFIInteger64
    .FirmwareRevision      EFIInteger32
    .ConsoleInHandle       EFIInteger64
    .ConIn                 EFIInteger64
    .ConsoleOutHandle      EFIInteger64
    .ConOut                EFIInteger64
    .StandardErrorHandle   EFIInteger64
    .StdErr                EFIInteger64
    .RuntimeServices       EFIInteger64
    .BootServices          EFIInteger64
    .NumberOfTableEntries  EFIInteger64
    .ConfigurationTable    EFIInteger64
}

EFITable EFISystemTable
    
Post 09 Apr 2020, 08:28
View user's profile Send private message Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 09 Apr 2020, 08:28
Code:
struc EFISimpleTextOutputProtocol
{
    .Reset             EFIInteger64
    .OutputString      EFIInteger64
    .TestString        EFIInteger64
    .QueryMode         EFIInteger64
    .SetMode           EFIInteger64
    .SetAttribute      EFIInteger64
    .ClearScreen       EFIInteger64
    .SetCursorPosition EFIInteger64
    .EnableCursor      EFIInteger64
    .Mode              EFIInteger64
}

EFITable EFISimpleTextOutputProtocol
    
Post 09 Apr 2020, 08:28
View user's profile Send private message Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 09 Apr 2020, 19:34
Nobody have any idea ?
Post 09 Apr 2020, 19:34
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 09 Apr 2020, 20:13
The address in rdx is changed so all those "[rdx+EFISystemTable.ConOut]" return bogus data.
Post 09 Apr 2020, 20:13
View user's profile Send private message Visit poster's website Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 09 Apr 2020, 20:57
I have already thinking about that, but if i replace to memory, nothing work...

I don't understand why ...

Code:
format pe64 efi
entry Main

section '.text' code readable executable

Main:
mov [SystemTable],rdx

mov rcx,[SystemTable+EFISystemTable.ConOut]
mov rax,[rcx+EFISimpleTextOutputProtocol.SetAttribute]
mov rdx,0010000b
sub rsp,0x20
call rax
add rsp,0x20

mov rcx,[SystemTable+EFISystemTable.ConOut]
mov rax,[rcx+EFISimpleTextOutputProtocol.OutputString]
mov rdx,SystemMessage
sub rsp,0x20
call rax
add rsp,0x20

mov rcx,[SystemTable+EFISystemTable.ConOut]
mov rax,[rcx+EFISimpleTextOutputProtocol.SetAttribute]
mov rdx,1111000b
sub rsp,0x20
call rax
add rsp,0x20

mov rcx,[SystemTable+EFISystemTable.ConOut]
mov rax,[rcx+EFISimpleTextOutputProtocol.OutputString]
mov rdx,Message
sub rsp,0x20
call rax
add rsp,0x20

jmp $

section '.data' data readable writable

include "UEFIDataTypes.fasm"
include "EFITableHeader.fasm"
include "EFISystemTable.fasm"
include "EFISimpleTextOutputProtocol.fasm"

SystemTable:    dq ?
SystemMessage:  du '* EFI Boot: '
Message:        du 'Test OK',0xD,0xA,0x0
    
Post 09 Apr 2020, 20:57
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 09 Apr 2020, 21:08
SystemTable is a pointer, not a structure.
Code:
mov rcx,[SystemTable]
mov rcx,[rcx+EFISystemTable.ConOut]    
Post 09 Apr 2020, 21:08
View user's profile Send private message Visit poster's website Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 09 Apr 2020, 23:22
I don't understand. rdx isn't pointer to EFI SYSTEM TABLE ?


Last edited by Fulgurance on 09 Apr 2020, 23:38; edited 2 times in total
Post 09 Apr 2020, 23:22
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 09 Apr 2020, 23:26
You placed rdx into [SystemTable]. So you need to recover the value. But the value isn't the structure, it is a pointer to the structure. The structure is somewhere else in memory. The structure wasn't copied to SystemTable. SystemTable only has eight bytes (dq ?), just enough to hold a pointer and nothing else.
Post 09 Apr 2020, 23:26
View user's profile Send private message Visit poster's website Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 09 Apr 2020, 23:38
But your advice work thanks ! I have doing that:

Code:
format pe64 efi
entry Main

section '.text' code readable executable

Main:
mov [SystemTable],rdx

mov rcx,[rdx+EFISystemTable.ConOut]
mov rax,[rcx+EFISimpleTextOutputProtocol.SetAttribute]
mov rdx,0010000b
sub rsp,0x20
call rax
add rsp,0x20

mov rdx,[SystemTable]

mov rcx,[rdx+EFISystemTable.ConOut]
mov rax,[rcx+EFISimpleTextOutputProtocol.OutputString]
mov rdx,SystemMessage
sub rsp,0x20
call rax
add rsp,0x20

mov rdx,[SystemTable]

mov rcx,[rdx+EFISystemTable.ConOut]
mov rax,[rcx+EFISimpleTextOutputProtocol.SetAttribute]
mov rdx,1111000b
sub rsp,0x20
call rax
add rsp,0x20

mov rdx,[SystemTable]

mov rcx,[rdx+EFISystemTable.ConOut]
mov rax,[rcx+EFISimpleTextOutputProtocol.OutputString]
mov rdx,Message
sub rsp,0x20
call rax
add rsp,0x20

mov rdx,[SystemTable]

jmp $

section '.data' data readable writable

include "UEFIDataTypes.fasm"
include "EFITableHeader.fasm"
include "EFISystemTable.fasm"
include "EFISimpleTextOutputProtocol.fasm"

SystemTable:    dq ?
SystemMessage:  du '* EFI Boot: ',0x0
Message:        du 'Test OK',0xD,0xA,0x0    


Do you think it's possible to optimize that ?
Post 09 Apr 2020, 23:38
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20445
Location: In your JS exploiting you and your system
revolution 09 Apr 2020, 23:43
Fulgurance wrote:
Do you think it's possible to optimize that ?
Yup. You can remove the final "mov rdx,[SystemTable]", it isn't needed.

You could also copy rdx to another register instead of putting it in memory.
Code:
mov r14,rdx ; now use r14
mov rcx,[r14+EFISystemTable.ConOut]    
Or just go direct
Code:
mov r14,[rdx+EFISystemTable.ConOut] ; do this only once
;...
call [r14+EFISimpleTextOutputProtocol.SetAttribute]
;...
call [r14+EFISimpleTextOutputProtocol.OutputString]    
Post 09 Apr 2020, 23:43
View user's profile Send private message Visit poster's website Reply with quote
Fulgurance



Joined: 27 Nov 2017
Posts: 276
Fulgurance 10 Apr 2020, 12:36
Thanks you ! Very Happy
Post 10 Apr 2020, 12:36
View user's profile Send private message 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.