flat assembler
Message board for the users of flat assembler.
![]() Goto page Previous 1, 2, 3, 4, 5, 6, 7 Next |
Author |
|
bitRAKE 14 Apr 2020, 05:03
revolution is absolutely correct. At this level of development, everything is very fragile. This aspect probably didn't come through on my example - I made a debugging tool that I could inject after each CALL. These systems are extremely flexible: different firmware, different hardware, different initialization order, etc.
I know QEMU also supports debugging, but I haven't experimented with that myself. Alternately, FDBG (search the board) could be used to single-step through the application -- looking at the register values. Code: Debug__ConOut: push 0 0 0 0 0 mov rdx,rsp call QWORD__toWideChar call WideChar__ConOut add rsp,8*5 xchg [rsp],rdi mov rdx,rdi xor eax,eax or ecx,-1 repnz scasw xchg [rsp],rdi WideChar__ConOut: enter 32,0 and spl,$F0 ; mov rdx,string mov rcx,[SystemTable] mov rcx,[rcx + EFI_SYSTEM_TABLE.ConOut] call [rcx + EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString] leave retn QWORD__toWideChar: movq xmm0,rax movdqa xmm1,xmm0 psrlw xmm0,4 punpcklbw xmm1,xmm0 movdqa xmm0,dqword [_hex] pand xmm1,dqword [_0F] pshufb xmm0,xmm1 pshufb xmm0,dqword [_REV] movdqa xmm1,xmm0 punpcklbw xmm0,[rdx] punpckhbw xmm1,[rdx] movdqu dqword [rdx],xmm0 movdqu dqword [rdx+16],xmm1 rent Code: call Debug__ConOut du " Message",13,10,0 Any tool to peer into what is happening will give you another perspective. About my perspective on your code: you don't understand the Win64ABI = only RCX,RDX,R8,R9 are used for parameters - the rest go on the stack. So, your code: Code: mov rcx,[rdx+EFISystemTable.BootServices] mov rax,[rcx+EFIBootServices.LocateHandle] mov rdx,EFILocateSearchType.ByProtocol mov r8,GUID.EFIGraphicsOutputProtocol xor r9,r9 mov r10,0x40 mov r11,Interface.EFIGraphicsOutputProtocol sub rsp,0x20 call rax add rsp,0x20 |
|||
![]() |
|
Fulgurance 14 Apr 2020, 07:54
Quote: For example, a function taking 5 integer arguments will take the first to fourth in registers, and the fifth will be pushed on top of the shadow space. So when the called function is entered, the stack will be composed of (in ascending order) the return address, followed by the shadow space (32 bytes) followed by the fifth parameter. Ah yes ... i'm not sure when i read this part, because i have just supposed the shadow space is stack, but when i have read OS Dev example, they use other register... Okay, i stop to use it ![]() When i thinking yesterday about your advice to make more control in my code, i have remember at the begin to today when i learn programming, i haver never used any control code to debug, but now i must to take this good habit. At the begin to today,nobody did say me this advice |
|||
![]() |
|
Fulgurance 14 Apr 2020, 17:35
Okay, now i have updated my code, and he call check status fonction after all EFI called function. But i have nothing ...
Look: Code: format pe64 efi entry Main section '.text' code readable executable Main: mov [SystemTable],rdx mov rcx,[rdx+EFISystemTable.BootServices] mov rax,[rcx+EFIBootServices.LocateHandle] mov rdx,EFILocateSearchType.ByProtocol mov r8,GUID.EFIGraphicsOutputProtocol xor r9,r9 mov qword [rsp + 8*4],0x40 mov qword [rsp + 8*5],Interface.EFIGraphicsOutputProtocol sub rsp,0x20 call rax call EFILocateHandleCheckStatus add rsp,0x20 mov rdx,[SystemTable] mov rcx,[rdx+EFISystemTable.BootServices] mov rax,[rcx+EFIBootServices.HandleProtocol] mov rdx,GUID.EFIGraphicsOutputProtocol mov r8,Interface.EFIGraphicsOutputProtocol sub rsp,0x20 call rax call EFIHandleProtocolCheckStatus add rsp,0x20 mov rdx,[SystemTable] mov rcx,[Interface.EFIGraphicsOutputProtocol] mov rax,[rcx+EFIGraphicsOutputProtocol.Blt] mov rdx,RectangleColor mov r8,EFIGraphicsOutputBltOperation.BufferToVideo mov r9,0x0 mov qword [rsp + 8*4],0x0 mov qword [rsp + 8*5],0x64 mov qword [rsp + 8*6],0x64 mov qword [rsp + 8*7],0xF mov qword [rsp + 8*8],0xF mov qword [rsp + 8*9],0x0 sub rsp,0x20 call rax call EFIBltCheckStatus add rsp,0x20 mov rdx,[SystemTable] jmp $ section '.data' data readable writable include "EFIBase/GUID.fasm" include "EFIBase/Interface.fasm" include "EFIBase/EFIDataTypes.fasm" include "EFITableHeader/EFITableHeader.fasm" include "EFISystemTable/EFISystemTable.fasm" include "EFIBootServices/EFIBootServices.fasm" include "EFIBootServices/IndexTables/EFILocateSearchType.fasm" include "EFIBootServices/StatusCodes/EFILocateHandle.fasm" include "EFIBootServices/StatusCodes/EFILocateHandleCheckStatus.fasm" include "EFIBootServices/StatusCodes/EFIHandleProtocol.fasm" include "EFIBootServices/StatusCodes/EFIHandleProtocolCheckStatus.fasm" include "EFISimpleTextOutputProtocol/EFISimpleTextOutputProtocol.fasm" include "EFIGraphicsOutputProtocol/EFIGraphicsOutputProtocol.fasm" include "EFIGraphicsOutputProtocol/IndexTables/EFIGraphicsOutputBltOperation.fasm" include "EFIGraphicsOutputProtocol/StatusCodes/EFIBlt.fasm" include "EFIGraphicsOutputProtocol/StatusCodes/EFIBltCheckStatus.fasm" SystemTable: dq ? SystemMessage: du '* ',0x0 Message: du 'EFI Boot: Test OK',0xD,0xA,0x0 RectangleColor: db 0xFF,0xFF,0xFF,? Exemple of some implementation: Code: EFIHandleProtocolCheckStatus: cmp rax,EFIHandleProtocol.Success je .Exit cmp rax,EFIHandleProtocol.Unsupported jne @f mov rdx,[SystemTable] mov rcx,[rdx+EFISystemTable.ConOut] mov rax,[rcx+EFISimpleTextOutputProtocol.OutputString] mov rdx,EFIHandleProtocolError.Unsupported sub rsp,0x20 call rax add rsp,0x20 @@: cmp rax,EFIHandleProtocol.InvalidParameter0 jne @f mov rdx,[SystemTable] mov rcx,[rdx+EFISystemTable.ConOut] mov rax,[rcx+EFISimpleTextOutputProtocol.OutputString] mov rdx,EFIHandleProtocolError.InvalidParameter0 sub rsp,0x20 call rax add rsp,0x20 @@: cmp rax,EFIHandleProtocol.InvalidParameter1 jne @f mov rdx,[SystemTable] mov rcx,[rdx+EFISystemTable.ConOut] mov rax,[rcx+EFISimpleTextOutputProtocol.OutputString] mov rdx,EFIHandleProtocolError.InvalidParameter1 sub rsp,0x20 call rax add rsp,0x20 @@: cmp rax,EFIHandleProtocol.InvalidParameter2 jne .Exit mov rdx,[SystemTable] mov rcx,[rdx+EFISystemTable.ConOut] mov rax,[rcx+EFISimpleTextOutputProtocol.OutputString] mov rdx,EFIHandleProtocolError.InvalidParameter2 sub rsp,0x20 call rax add rsp,0x20 .Exit: ret EFIHandleProtocolError: .Unsupported: du 'Protocol not supported',0x0 .InvalidParameter0: du 'Handle is null',0x0 .InvalidParameter1: du 'Protocol is null',0x0 .InvalidParameter2: du 'Interface is null',0x0 I think i'm tired and i have miss something, but i don't see anything ![]() |
|||
![]() |
|
bitRAKE 15 Apr 2020, 04:13
The first thing I see is that you are putting code in the data section.
![]() Just add "executable" to the section definition. (Because I'm lazy.) I like the verbosity of your error checking. How do you insure the stack is aligned to 16 bytes addresses? (This hasn't caused me a problem, but the spec does state it as a requirement.) I'm paranoid and used: Code: and spl,$F0 (The stack isn't executable either. :/ Not that you'd do that, but I've got crazy ideas sometimes. ![]() How do you define the buffer you're sending to LocateHandle()? |
|||
![]() |
|
bitRAKE 15 Apr 2020, 05:23
Ah, just noticed you adjust the stack after putting the parameters there - that's not going to work:
Code: mov rcx,[rdx+EFISystemTable.BootServices] mov rax,[rcx+EFIBootServices.LocateHandle] mov rdx,EFILocateSearchType.ByProtocol mov r8,GUID.EFIGraphicsOutputProtocol xor r9,r9 mov qword [rsp + 8*4],0x40 mov qword [rsp + 8*5],Interface.EFIGraphicsOutputProtocol sub rsp,0x20 call rax call EFILocateHandleCheckStatus add rsp,0x20 |
|||
![]() |
|
Fulgurance 15 Apr 2020, 10:29
It's true when i have read efi example, i don't understand the part with stack. Effectively, this part is very stupid.
![]() Thank you for your post, i think i have understand now. Just question, when you do that : Code: mov qword [rsp + 8*5] Is it the same thing if i use push command ? Why do you do that ? More faster ? Sure, i need to make many progress at assembly ! ![]() |
|||
![]() |
|
Fulgurance 15 Apr 2020, 11:02
Okay, i think i have followed your advice, but i have again nothing appening, no error, no graphical rect into the part of efi boot services.
(i have made just efi boot with this part without console messages) Code: format pe64 efi entry Main section '.text' code readable executable Main: mov [SystemTable],rdx and spl,0xF0 sub rsp,8*6 mov rcx,[rdx+EFISystemTable.BootServices] mov rax,[rcx+EFIBootServices.LocateHandle] mov rdx,EFILocateSearchType.ByProtocol mov r8,GUID.EFIGraphicsOutputProtocol xor r9,r9 mov qword [rsp + 8*4],0x40 mov qword [rsp + 8*5],Interface.EFIGraphicsOutputProtocol call rax call EFILocateHandleCheckStatus add rsp,8*6 mov rdx,[SystemTable] mov rcx,[rdx+EFISystemTable.BootServices] mov rax,[rcx+EFIBootServices.HandleProtocol] mov rdx,GUID.EFIGraphicsOutputProtocol mov r8,Interface.EFIGraphicsOutputProtocol call rax call EFIHandleProtocolCheckStatus mov rdx,[SystemTable] sub rsp,8*10 mov rcx,[Interface.EFIGraphicsOutputProtocol] mov rax,[rcx+EFIGraphicsOutputProtocol.Blt] mov rdx,RectangleColor mov r8,EFIGraphicsOutputBltOperation.BufferToVideo mov r9,0x0 mov qword [rsp + 8*4],0x0 mov qword [rsp + 8*5],0x64 mov qword [rsp + 8*6],0x64 mov qword [rsp + 8*7],0xF mov qword [rsp + 8*8],0xF mov qword [rsp + 8*9],0x0 call rax call EFIBltCheckStatus add rsp,8*10 mov rdx,[SystemTable] jmp $ section '.data' data readable writable executable include "EFIBase/GUID.fasm" include "EFIBase/Interface.fasm" include "EFIBase/EFIDataTypes.fasm" include "EFITableHeader/EFITableHeader.fasm" include "EFISystemTable/EFISystemTable.fasm" include "EFIBootServices/EFIBootServices.fasm" include "EFIBootServices/IndexTables/EFILocateSearchType.fasm" include "EFIBootServices/StatusCodes/EFILocateHandle.fasm" include "EFIBootServices/StatusCodes/EFILocateHandleCheckStatus.fasm" include "EFIBootServices/StatusCodes/EFIHandleProtocol.fasm" include "EFIBootServices/StatusCodes/EFIHandleProtocolCheckStatus.fasm" include "EFISimpleTextOutputProtocol/EFISimpleTextOutputProtocol.fasm" include "EFIGraphicsOutputProtocol/EFIGraphicsOutputProtocol.fasm" include "EFIGraphicsOutputProtocol/IndexTables/EFIGraphicsOutputBltOperation.fasm" include "EFIGraphicsOutputProtocol/StatusCodes/EFIBlt.fasm" include "EFIGraphicsOutputProtocol/StatusCodes/EFIBltCheckStatus.fasm" SystemTable: dq ? SystemMessage: du '* ',0x0 Message: du 'EFI Boot: Test OK',0xD,0xA,0x0 RectangleColor: db 0xFF,0xFF,0xFF,? I have removed into my library all stupid instruction with stack when it's useless. |
|||
![]() |
|
Fulgurance 15 Apr 2020, 14:02
This is the buffer:
Code: Interface: .EFIGraphicsOutputProtocol: dq ? Edit: i think it's the source of problem. I test that. But i don't understand very well what is the EFIHandle structure.Just that ? Quote: typedef VOID *EFI_HANDLE; |
|||
![]() |
|
bitRAKE 15 Apr 2020, 15:35
If that is your buffer, why tell EFI that it's of size 0x40? It is a buffer, if EFI doesn't like it - EFI will say so. Your HandleProtocol() has no stack.
It's difficult to say where things are going wrong, but I would start simple. Just print a message like before then progress CALL-by-CALL with messages. Not unlike our conversation here. ![]() |
|||
![]() |
|
Fulgurance 15 Apr 2020, 16:25
? Hum sorry, but i don't understand. If i have buffer with 64 bits size, the size is 64 bits no?
Quote: Your HandleProtocol() has no stack. ? I think i need more explanation... Yes, good example ![]() I try to make message to found the problem. For example, if i use display macro, how can i display number ? |
|||
![]() |
|
Fulgurance 15 Apr 2020, 19:54
I have tested my return error code for function, i have impression he don't work. I have tested to make error, but he don't show something...
I'm tired .... ![]() |
|||
![]() |
|
edfed 15 Apr 2020, 20:38
to display numbers, i use it in my bootsector to know how much a use, and how much is free.
Code: macro bargraphword w,c { b = (w)/16 repeat b display c end repeat display 13,10 } macro debugword w,s { d1='0'+(w) shr 8 and 0fh ; d2='0'+(w) shr 4 and 0fh ; d3='0'+(w) and 0fh ; if d1>'9' ; d1=d1+7 ; end if ; ; if d2>'9' ; d2=d2+7 ; end if ; ; if d3>'9' ; d3=d3+7 ; end if ; ; display d1,d2,d3,'h ' ; display s,13,10 ; } macro memsize m { bargraphword m,'=' debugword m,'free bytes' bargraphword 510-m,'=' debugword 510-m,'used bytes' } Code: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; free = 510-(padding-$$) ; define "free" bytes count padding rb free ; reserve "free" bytes to make line below at offset 510 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; dw 0aa55h ; magic number boot mark, used by bios to test if valid boot sector ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; include "macros.inc" memsize free |
|||
![]() |
|
Fulgurance 16 Apr 2020, 13:36
Sorry, but can you just explain me the problem with my stack? When i try to add value to SS and RSP, all code just don't work. For me, UEFI have already configured stack, no ?
|
|||
![]() |
|
edfed 16 Apr 2020, 13:59
i don't know anything about uefi. but i know you cannot add anything to a segment register. the only way to alter segments are mov to/from gp register, and push pop.
my advice about your problem is to code one thing at a time. and each time you meet a bug, you simplify the code as long as the bug is here. maybe you can first try a simple push and pop operations to try the stack. after that, you can try the add rsp,8*6, sub rsp 8*6 and see what happens. and so on. |
|||
![]() |
|
DimonSoft 16 Apr 2020, 17:47
<OffTopic>
May I insist once again that UEFI is NOT the kind of thing to start dealing with until one has sufficient experience with user-mode code and assembly programming in general? Most of the questions asked here are related to lack of understanding of pretty basic stuff. It looks like trying to build a plane without having much experience using a screwdriver. Sorry, nothing personal. </OffTopic> |
|||
![]() |
|
Roman 16 Apr 2020, 18:38
bitRAKE
Is this convert HEX to ASC2 ? QWORD__toWideChar: movq xmm0,rax movdqa xmm1,xmm0 psrlw xmm0,4 punpcklbw xmm1,xmm0 movdqa xmm0,dqword [_hex] ;_hex what is value ? pand xmm1,dqword [_0F] ;_0F what is value ? pshufb xmm0,xmm1 pshufb xmm0,dqword [_REV] ;_REV what is value ? movdqa xmm1,xmm0 punpcklbw xmm0,[rdx] punpckhbw xmm1,[rdx] movdqu dqword [rdx],xmm0 movdqu dqword [rdx+16],xmm1 |
|||
![]() |
|
revolution 16 Apr 2020, 19:04
Fulgurance wrote: Sorry, but can you just explain me the problem with my stack? When i try to add value to SS and RSP, all code just don't work. For me, UEFI have already configured stack, no ? Slow down. I think that your first step is to get a "hello world" print test working. Then you can add other calls one-by-one and check that each call succeeds before moving on to the the next call. You don't need any fancy error check yet, you only have to make sure it is working, just something basic like this: Code: print "begin" call [rax+...] cmp rax,SUCCESS_VALUE jnz .failed_1 ;... call [rax+...] cmp rax,SUCCESS_VALUE jnz .failed_2 ;... .failed_1: print "failed at stage 1" jmp exit .failed_2: print "failed at stage 2" jmp exit |
|||
![]() |
|
bitRAKE 17 Apr 2020, 01:13
Roman wrote: bitRAKE https://board.flatassembler.net/topic.php?p=213413#213413 ... or back in time a dozen years ... https://board.flatassembler.net/topic.php?p=84943#84943 |
|||
![]() |
|
Fulgurance 17 Apr 2020, 11:09
Okay. You have right, i need to do step by step.
I'm going too fast. I start to zero. I have read into UEFI documentation in first, i need to use LocateProtocol function. I try to call it, and i have call just after a StatusCode return program. But i have other return status code number. Look: Code: format pe64 efi entry Main section '.text' code readable executable Main: mov [SystemTable],rdx mov rcx,[rdx+EFISystemTable.BootServices] mov rax,[rcx+EFIBootServices.LocateProtocol] mov rdx,GUID.EFIGraphicsOutputProtocol xor r8,r8 mov r9,Interface.EFIGraphicsOutputProtocol call rax call EFIBootServicesStatusCode mov rdx,[SystemTable] jmp $ include "EFIBase/GUID.fasm" include "EFIBase/Interface.fasm" include "EFIBase/EFIDataTypes.fasm" include "EFIStatusCode/EFIBootServicesStatusCode.fasm" include "EFITableHeader/EFITableHeader.fasm" include "EFISystemTable/EFISystemTable.fasm" include "EFIBootServices/EFIBootServices.fasm" include "EFIBootServices/IndexTables/EFILocateSearchType.fasm" include "EFISimpleTextOutputProtocol/EFISimpleTextOutputProtocol.fasm" include "EFIGraphicsOutputProtocol/EFIGraphicsOutputProtocol.fasm" include "EFIGraphicsOutputProtocol/IndexTables/EFIGraphicsOutputBltOperation.fasm" section '.data' data readable writable executable SystemTable: dq ? Just Status code function: Code: EFIBootServicesStatusCode: .LocateProtocol: cmp rax,0x0 je .LocateProtocolEnd .LocateProtocolError1: cmp rax,0x1 jne .LocateProtocolError2 mov rdx,[SystemTable] mov rcx,[rdx+EFISystemTable.ConOut] mov rax,[rcx+EFISimpleTextOutputProtocol.OutputString] mov rdx,LocateProtocolMessage1 call rax jmp .LocateProtocolEnd .LocateProtocolError2: cmp rax,0x2 jne .LocateProtocolUnknowError mov rdx,[SystemTable] mov rcx,[rdx+EFISystemTable.ConOut] mov rax,[rcx+EFISimpleTextOutputProtocol.OutputString] mov rdx,LocateProtocolMessage2 call rax jmp .LocateProtocolEnd .LocateProtocolUnknowError: mov rdx,[SystemTable] mov rcx,[rdx+EFISystemTable.ConOut] mov rax,[rcx+EFISimpleTextOutputProtocol.OutputString] mov rdx,LocateProtocolMessageUnknowError call rax .LocateProtocolEnd: ret LocateProtocolMessage1: du "! EFIBootServices.LocateProtocol error: Interface or Protocol argument is null.",0x0 LocateProtocolMessage2: du "! EFIBootServices.LocateProtocol error: No Protocol was found.",0x0 LocateProtocolMessageUnknowError: du "! EFIBootServices.LocateProtocol error: Unknow error.",0x0 I have the last message as error Just question, i'm not sure. I have read it's caller's responsability to allocate 32 bytes for shadow stack. Is it mandatory if you don't have usefullness to push parameters into the stack? |
|||
![]() |
|
Goto page Previous 1, 2, 3, 4, 5, 6, 7 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.