OSDev.org

The Place to Start for Operating System Developers
It is currently Sat Apr 27, 2024 1:55 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: printf in EFI environment with gnuefi
PostPosted: Wed Feb 14, 2024 11:02 pm 
Offline

Joined: Sat Feb 10, 2024 8:57 pm
Posts: 3
Hello,

I started to study a bit about writing code for the EFI environment using gnuefi, I started by the classical hello world which should be pretty straight forward.
I decided to follow https://wiki.osdev.org/GNU-EFI which guided me through the whole process of compiling my code and generating the EFI image successfully, which I'm using with QEMU
to test. However when executing the main.efi binary it seems to be printing in another encoding, resulting in a bunch of chinese characters being printed when running in no graphic mode (on
graphic mode it looks like a bunch of /r). No errors are shown.
Here you can see a chunk of my program's output to the terminal:

FS0:\> main.efi
꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯
꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯
꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯
꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯
꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯
꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯
꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯
꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯
꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯
꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯
꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯
꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯꾯

The source code and the Makefile I'm using can be found at:
https://github.com/benatto/osstudy/tree ... oader/uefi

I've tried to compile one of gnuefi programs and none of the result are the same, which makes me believe it's something in the way I'm compiling the programs.

May I have your help on this? I ran out of ideas for now.

Thanks in advance,


Top
 Profile  
 
 Post subject: Re: printf in EFI environment with gnuefi
PostPosted: Thu Feb 15, 2024 1:51 am 
Offline
Member
Member

Joined: Wed Mar 30, 2011 12:31 am
Posts: 676
Quote:
No errors are shown.


Really?

Code:
gcc -Ignu-efi/inc -fpic -ffreestanding -fno-stack-protector -fno-stack-check -fshort-wchar \
-ggdb -mno-red-zone -maccumulate-outgoing-args -c main.c -o main.o
main.c: In function ‘efi_main’:
main.c:9:24: error: ‘msg’ undeclared (first use in this function)
    9 |         Print(L"%s\n", msg);
      |                        ^~~
main.c:9:24: note: each undeclared identifier is reported only once for each function it appears in
make: *** [Makefile:12: main.o] Error 1


In the code you have on Github, you never define "msg". You are missing code.

UEFI is generally a UTF-16-native environment, and the "%s" format specifier in GNU-EFI's Print function expects a pointer to a UTF-16 string. The Korean hangul 꾯 is represented in UTF-16 as 0xAFAF, so you have passed in a pointer to memory that has 0xAF repeated over and over.

_________________
toaruos on github | toaruos.org | gitlab | twitter | bim - a text editor


Top
 Profile  
 
 Post subject: Re: printf in EFI environment with gnuefi
PostPosted: Thu Feb 15, 2024 7:53 am 
Offline

Joined: Sat Feb 10, 2024 8:57 pm
Posts: 3
Quote:
In the code you have on Github, you never define "msg". You are missing code.

UEFI is generally a UTF-16-native environment, and the "%s" format specifier in GNU-EFI's Print function expects a pointer to a UTF-16 string. The Korean hangul 꾯 is represented in UTF-16 as 0xAFAF, so you have passed in a pointer to memory that has 0xAF repeated over and over.


Ouch, sorry I ended up committing a wrong and incomplete version on the file. I have pushed the right version, but this is how it looks like:

Code:
#include <efi.h>                                               
#include <efilib.h>                                             
                                                               
EFI_STATUS                                                     
EFIAPI                                                         
efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{                                                               
    InitializeLib(ImageHandle, SystemTable);                   
    Print(L"Hello world\n");                                   
    return EFI_SUCCESS;                                         
}                                                               



Top
 Profile  
 
 Post subject: Re: printf in EFI environment with gnuefi
PostPosted: Thu Feb 15, 2024 9:06 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5146
Whoever wrote that wiki article missed the .rodata section. It works fine once you tell objcopy to include it.


Top
 Profile  
 
 Post subject: Re: printf in EFI environment with gnuefi
PostPosted: Thu Feb 15, 2024 9:48 pm 
Offline

Joined: Sat Feb 10, 2024 8:57 pm
Posts: 3
Octocontrabass wrote:
Whoever wrote that wiki article missed the .rodata section. It works fine once you tell objcopy to include it.


ouch, I haven't noticed that! Thanks, it worked fine right now!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 34 guests


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 post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group