You are not logged in.

#1 2015-12-27 15:26:47

unixman
Member
Registered: 2015-03-12
Posts: 64

[solved]uefi: hello word with asm

hi. im trying to to make an uefi app just print "hello"  with assembly language as without using any C library(tianocore, gnuefi etc)

So far i wrote below :

;rdi rcx rdx r8  r9  [rsp+32]  <== efi(windows) calling conventions
;rax rdi rsi rdx r10 r8        <== Linux calling conventions
;? = things which i want to know or not sure

;  TO  COMPILE

;nasm -f elf64 hello.asm    (?)
;ld -nostdlib -T f.lds  -shared -Bsymbolic hello.o -o hello.so      (?)
;objcopy -j .text -j .data -j .dynamic -j .dynsym -j .reloc --target efi-app-x86-64 hello.so hello.efi    (?)

section .data
zz db `h\0e\0l\0l\0o\0\n\0\r\0\0\0` ; <==  "hello\n\r" with efi syntax(ucs2)

section .text
global _start
_start:
	lea   rbp,[rsp+16]       ; rbp <- systab (?, also lea ?)
	mov   rdi,[rbp+48]	     ; rdi <- systab->ConOut->OutputString (?)
	mov   rcx,[rbp+44]	     ; rcx <- systab->ConOut (?)
	mov   rdx,zz
	call  [rdi]	   
	;leave  ?   
	ret

section .reloc
label1:  
	;db  0
	; stuff so on ?

Above code  compiled successfully. So the "objdump -x hello.efi"  output is:

objdump -x hello.efi

hello.efi:     file format pei-x86-64
hello.efi
architecture: i386:x86-64, flags 0x00000133:
HAS_RELOC, EXEC_P, HAS_SYMS, HAS_LOCALS, D_PAGED
start address 0x0000000010001000

Characteristics 0x206
	executable
	line numbers stripped
	debugging information removed

Time/Date		Thu Jan  1 00:00:24 1970
Magic			020b	(PE32+)
MajorLinkerVersion	2
MinorLinkerVersion	25
SizeOfCode		00000200
SizeOfInitializedData	00000800
SizeOfUninitializedData	00000200
AddressOfEntryPoint	0000000010001000
BaseOfCode		0000000010001000
ImageBase		0000000000000000
SectionAlignment	0000000000001000
FileAlignment		0000000000000200
MajorOSystemVersion	0
MinorOSystemVersion	0
MajorImageVersion	0
MinorImageVersion	0
MajorSubsystemVersion	0
MinorSubsystemVersion	0
Win32Version		00000000
SizeOfImage		10004000
SizeOfHeaders		00000400
CheckSum		000098a5
Subsystem		0000000a	(EFI application)
DllCharacteristics	00000000
SizeOfStackReserve	0000000000000000
SizeOfStackCommit	0000000000000000
SizeOfHeapReserve	0000000000000000
SizeOfHeapCommit	0000000000000000
LoaderFlags		00000000
NumberOfRvaAndSizes	00000010

The Data Directory
Entry 0 0000000000000000 00000000 Export Directory [.edata (or where ever we found it)]
Entry 1 0000000000000000 00000000 Import Directory [parts of .idata]
Entry 2 0000000000000000 00000000 Resource Directory [.rsrc]
Entry 3 0000000000000000 00000000 Exception Directory [.pdata]
Entry 4 0000000000000000 00000000 Security Directory
Entry 5 0000000010001038 00000001 Base Relocation Directory [.reloc]
Entry 6 0000000000000000 00000000 Debug Directory
Entry 7 0000000000000000 00000000 Description Directory
Entry 8 0000000000000000 00000000 Special Directory
Entry 9 0000000000000000 00000000 Thread Storage Directory [.tls]
Entry a 0000000000000000 00000000 Load Configuration Directory
Entry b 0000000000000000 00000000 Bound Import Directory
Entry c 0000000000000000 00000000 Import Address Table Directory
Entry d 0000000000000000 00000000 Delay Import Directory
Entry e 0000000000000000 00000000 CLR Runtime Header
Entry f 0000000000000000 00000000 Reserved


PE File Base Relocations (interpreted .reloc section contents)

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .text         00000020  0000000010001000  0000000010001000  00000400  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  1 .reloc        00000001  0000000010001038  0000000010001038  00000638  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  2 .data         00000010  0000000010002000  0000000010002000  00000a00  2**2
                  CONTENTS, ALLOC, LOAD, DATA
  3 .dynamic      00000100  0000000010002010  0000000010002010  00000c10  2**3
                  CONTENTS, ALLOC, LOAD, DATA
  4 .dynsym       00000090  0000000010003000  0000000010003000  00001000  2**3
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
SYMBOL TABLE:
[  0](sec  3)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x0000000000000000 zz
[  1](sec  2)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x0000000000000000 label1
[  2](sec  4)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x0000000000000000 _DYNAMIC
[  3](sec  4)(fl 0x00)(ty   0)(scl   3) (nx 0) 0x0000000000000000 _GLOBAL_OFFSET_TABLE_
[  4](sec  1)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x0000000000000000 _start
[  5](sec  1)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x0000000000000000 _text
[  6](sec  3)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x0000000000000000 _data

Then i moved the app to my efi partition and call it from  efi shell to test whether it works properly or not.

Unfortunally the app cant print anythings even an error or warning it just hangs my ivybridge lappy.

As you see above i used a custom "f.lds" ld script to link my app without modified distrubuted by gnuefi team.

It just set alignment of sections  to efi page size(4096).

Doc says "efi use physical addressing  so relocation of binary mandotory."

i set start address 0x10000000 by hand via f.lds increase the change of loading image to memory successfully without relocation.

Because doc says efi first try to loads app to preferred physical address. Below is output of "dmesg | grep e820". is it related?

what is the meaning of "usable"  in it.

dmesg | grep e820

[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000008ffff] usable
[    0.000000] BIOS-e820: [mem 0x0000000000090000-0x00000000000bffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000000100000-0x000000001fffffff] usable
[    0.000000] BIOS-e820: [mem 0x0000000020000000-0x00000000201fffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000020200000-0x000000003267ffff] usable
[    0.000000] BIOS-e820: [mem 0x0000000032680000-0x0000000032881fff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000032882000-0x000000003304efff] usable
[    0.000000] BIOS-e820: [mem 0x000000003304f000-0x000000003304ffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000033050000-0x00000000d838bfff] usable
[    0.000000] BIOS-e820: [mem 0x00000000d838c000-0x00000000daeeefff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000daeef000-0x00000000daf9efff] ACPI NVS
[    0.000000] BIOS-e820: [mem 0x00000000daf9f000-0x00000000daffefff] ACPI data
[    0.000000] BIOS-e820: [mem 0x00000000dafff000-0x00000000daffffff] usable
[    0.000000] BIOS-e820: [mem 0x00000000db000000-0x00000000df9fffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000f80f8000-0x00000000f80f8fff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000fed1c000-0x00000000fed1ffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000100000000-0x000000011f5fffff] usable
[    0.000000] e820: update [mem 0xc69c5018-0xc69d2057] usable ==> usable
[    0.000000] e820: update [mem 0xc69b4018-0xc69c4057] usable ==> usable
[    0.000000] e820: update [mem 0x00000000-0x00000fff] usable ==> reserved
[    0.000000] e820: remove [mem 0x000a0000-0x000fffff] usable
[    0.000000] e820: last_pfn = 0x11f600 max_arch_pfn = 0x400000000
[    0.000000] e820: last_pfn = 0xdb000 max_arch_pfn = 0x400000000
[    0.000000] e820: [mem 0xdfa00000-0xf80f7fff] available for PCI devices
[    0.639094] e820: reserve RAM buffer [mem 0x32680000-0x33ffffff]
[    0.639096] e820: reserve RAM buffer [mem 0x3304f000-0x33ffffff]
[    0.639098] e820: reserve RAM buffer [mem 0xc69b4018-0xc7ffffff]
[    0.639100] e820: reserve RAM buffer [mem 0xc69c5018-0xc7ffffff]
[    0.639101] e820: reserve RAM buffer [mem 0xd838c000-0xdbffffff]
[    0.639104] e820: reserve RAM buffer [mem 0xdb000000-0xdbffffff]
[    0.639105] e820: reserve RAM buffer [mem 0x11f600000-0x11fffffff]

what the things which i have to done for my app able to run proporly.

Last edited by unixman (2016-02-29 13:45:16)

Offline

#2 2016-02-29 11:01:42

unixman
Member
Registered: 2015-03-12
Posts: 64

Re: [solved]uefi: hello word with asm

a bit late but it's solved:) http://forum.nasm.us/index.php?topic=2191.0

simply put the pe32 header in front of the code then compile with "nasm -f bin".  complete code:

bits 64
section .header 
DOS:
	dw 0x5a4d		;e_magic
	times 29 dw 0	;unused
	dd 0x40		;e_lfanew

PECOFF:
        dd `PE\0\0`	;Signature
	dw 0x8664		;Machine
	dw 3			;NumberOfSections
	dd 0			;TimeDateStamp
	dd 0			;PointerToSymbolTable
	dd 0			;NumberOfSymbols
	dw 160		;SizeOfOptionalHeader
	dw 0x202		;Characteristics
	dw 0x20b		;Magic
    db 0			;MajorLinkerVersion
    db 0			;MinorLinkerVersion
    dd 0x200		;SizeOfCode
    dd 0x400		;SizeOfInitializedData
    dd 0			;SizeOfUninitializedData
    dd 0x0			;AddressOfEntryPoint
    dd 0x0			;BaseOfCode
    dq 0x0			;ImageBase
    dd 0x1000		;SectionAlignment
    dd 0x200		;FileAlignment
    dw 0			;MajorOperatingSystemVersion
    dw 0			;MinorOperatingSystemVersion
    dw 0			;MajorImageVersion
    dw 0			;MinorImageVersion
    dw 0			;MajorSubsystemVersion
    dw 0			;MinorSubsystemVersion
    dd 0			;Reserved
    dd 0x3000		;SizeOfImage
    dd 0x200		;SizeOfHeaders
    dd 0			;CheckSum
    dw 10			;Subsystem
    dw 0			;DllCharacteristics
    dq 0			;SizeOfStackReserve
    dq 0			;SizeOfStackCommit
    dq 0			;SizeOfHeapReserve
    dq 0			;SizeOfHeapCommit
    dd 0			;LoaderFlags
    dd 6			;NumberOfRvaAndSizes

DIRS:
	times 40 db 0	;unused dirs for this app
	dd 0x1000		;VirtualAddress(.reloc)
	dd 8			;Size(.reloc)

SECTS:
.1:
	dq  `.text`			;Name
	dd  codesize		;VirtualSize
	dd  0x0				;VirtualAddress	
	dd  0x200			;SizeOfRawData
	dd  0x200			;PointerToRawData
	dd  0				;PointerToRelocations
	dd  0				;PointerToLinenumbers
	dw  0				;NumberOfRelocations
	dw  0				;NumberOfLinenumbers
	dd  0x60500020		;Characteristics

.2:
	dq  `.reloc`
	dd  0x8		
	dd  0x1000
	dd  0x200
	dd  0x400
	dd  0
	dd  0
	dw  0
	dw  0
	dd  0x42100040

.3:
	dq  `.data`
	dd  datasize
	dd  0x2000
	dd  0x200
	dd  0x600
	dd  0
	dd  0
	dw  0
	dw  0
	dd  0xc0100040

section .text follows=.header align=0x200
	sub rsp, 40

	mov rcx, [rdx+64]         
	lea rdx, [rel hello]
	call     [rcx+8]		

	add rsp, 40 
	ret

codesize equ $ - $$

section .reloc follows=.text align=0x200
	dd 0					;PageRVA
	dd 8					;BlockSize

section .data follows=.reloc align=0x200 vstart=0x2200    ;yeah this is the trick!
hello:
	db __utf16__ `hello world!\n\r\0`

datasize equ $ - $$
align 0x200,db 0

Offline

#3 2016-02-29 12:18:32

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: [solved]uefi: hello word with asm

Thanks for sharing the solution, unixman. Please mark the thread as solved.

Offline

Board footer

Powered by FluxBB