1

I am Developing an OS, I wants to get EDID from monitor, I am found some asm code (https://wiki.osdev.org/EDID) to get edid in ES:DI registers,

mov ax,     0x4f15
mov bl,     0x01
xor cx,     cx
xor dx,     dx
int         0x10
;AL = 0x4F if function supported
;AH = status (0 is success, 1 is fail)
;ES:DI contains the EDID

How can I get AL, AH, and ES:DI values in C File?

Actually I am developing an 64 bit UEFI OS

LoadGDT:   
    lgdt [rdi]
    mov ax, 0x10 
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    pop rdi
    mov rax, 0x08
    push rax
    push rdi
    retfq
GLOBAL LoadGDT

I am able to run these above asm code and get it in c using Global Functions in C,

14
  • 1
    It's unlikely, but possible, that you are actually using a C compiler that is targeting a 16-bit real-mode environment. Can you please confirm that you indeed understand that the assembly code is for real-mode and that your C compiler can correctly handle that? Commented May 12, 2021 at 8:19
  • 1
    @VapourDev Which variant of gcc are you using? Are you programming in real mode?
    – fuz
    Commented May 12, 2021 at 8:32
  • 2
    Gcc can't really handle 16-bit code. It will just add override prefixes and anyway you can't call BIOS interrupt services from real/long-mode because the IDT (and the segmentation assumed by the BIOS code) is different. Since you are using UEFI, just use the EFI_EDID_DISCOVERED_PROTOCOL interface. Commented May 12, 2021 at 9:50
  • 1
    @PeterCordes It seems there are no duplicates. I see if I can craft a simple example in UEFI, otherwise it would be too much of a useless answer. Commented May 12, 2021 at 14:03
  • 1
    @MargaretBloom: If you don't want to take the time for that, simply pointing out that most osdev.org pages are about legacy 16-bit BIOS interfaces / IBM-PC hardware, and how UEFI is totally different, would be a good answer. And a possible useful duplicate for future questions about other interfaces, not specifically EDID. Of course, we don't currently get a lot of questions where the answer is "that's legacy BIOS stuff, use the totally different UEFI interface." Commented May 12, 2021 at 14:07

1 Answer 1

5

That page on osdev.org contains code intended to be run when the CPU is in 16-bit real mode.
You can tell not only from the registers involved but also from the fact that int 10h is used.
This is a well-known BIOS interrupt service that is written in 16-bit real-mode code.

If you target UEFI, then your bootloader is actually an UEFI application, which is a PE32(+) image.
If the CPU is 64-bit capable, the firmware will switch into long mode (64-bit mode) and load your bootloader.
Otherwise, it will switch into protected mode (32-bit mode).
In any case, real mode is never used in UEFI.

You can call 16-bit code from protected/long mode with the use of a 16-bit code segment in the GDT/LDT but you cannot call real-mode code (i.e. code written to work with the real-mode segmentation) because segmentation works completely different between the modes.
Plus, in real mode the interrupts are dispatched through the IVT and not the IDT, you would need to get the original entry-point for interrupt 10h.

UEFI protocol EFI_EDID_DISCOVERED_PROTOCOL

Luckily, UEFI has a replacement for most basic services offered by the legacy BIOS interface.
In this case, you can use the EFI_EDID_DISCOVERED_PROTOCOL and eventually apply any override from the platform firmware with the use of EFI_EDID_OVERRIDE_PROTOCOL.

The EFI_EDID_DISCOVERED_PROTOCOL is straightforward to use, it's just a (Size, Data) pair.

typedef struct _EFI_EDID_DISCOVERED_PROTOCOL {
    UINT32   SizeOfEdid;
    UINT8   *Edid;
} EFI_EDID_DISCOVERED_PROTOCOL;

(from gnu-efi)

The format of the buffer Edid can be found in the VESA specification or even on Wikipedia.

As an example, I wrote a simple UEFI application with gnu-efi and x64_64-w64-mingw32 (a version of GCC and tools that target PEs).
I avoided using uefilib.h in order to use gnu-efi just for the definition of the structures related to EUFI.

The code sucks, it assumes at most 10 handles support the EDID protocol and I wrote only a partial structure for the EDID data (because I got bored).
But this should be enough the get the idea.

NOTE That my VM didn't return any EDID information, so the code is not completely tested!

#include <efi.h>

//You are better off using this lib
//#include <efilib.h>

EFI_GUID gEfiEdidDiscoveredProtocolGuid = EFI_EDID_DISCOVERED_PROTOCOL_GUID;
EFI_SYSTEM_TABLE* gST = NULL;

typedef struct _EDID14 {
    UINT8 Signature[8];
    UINT16 ManufacturerID;
    UINT16 ManufacturerCode;
    UINT32 Serial;
    UINT8 Week;
    UINT8 Year;
    UINT8 Major;
    UINT8 Minor;
    UINT32 InputParams;
    UINT8 HSize;
    UINT8 VSize;
    UINT8 Gamma;
    
    //...Omitted...
} EDID14_RAW;

VOID Print(CHAR16* string)
{
    gST->ConOut->OutputString(gST->ConOut, string);
}

VOID PrintHex(UINT64 number)
{
    CHAR16* digits = L"0123456789abcdef";
    CHAR16 buffer[2] = {0, 0};
    
    for (INTN i = 64-4; i >= 0; i-=4)
    {
        buffer[0] = digits[(number >> i) & 0xf];
        Print(buffer);
    }   
}

VOID PrintDec(UINT64 number)
{
    CHAR16 buffer[21] = {0};
    UINTN i = 19;
    
    do
    {
        buffer[i--] = L'0' + (number % 10);
        number = number / 10;
    }
    while (number && i >= 0);
    
    Print(buffer + i + 1);
}


#define MANUFACTURER_DECODE_LETTER(x)  ( L'A' + ( (x) & 0x1f ) - 1 )


EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable)
{
    EFI_STATUS Status = EFI_SUCCESS;
    EFI_HANDLE EDIDHandles[10];
    UINTN Size = sizeof(EFI_HANDLE) * 10;
    EFI_EDID_DISCOVERED_PROTOCOL* EDID;
     
    gST = SystemTable;
    
    if ( EFI_ERROR( (Status = SystemTable->BootServices->LocateHandle(ByProtocol, &gEfiEdidDiscoveredProtocolGuid, NULL, &Size, EDIDHandles)) ) )
    {
        Print(L"Failed to get EDID handles: "); PrintHex(Status); Print(L"\r\n");
        return Status;
    }

    for (INTN i = 0; i < Size/sizeof(EFI_HANDLE); i++)
    {
        if (EFI_ERROR( (SystemTable->BootServices->OpenProtocol(
            EDIDHandles[i], &gEfiEdidDiscoveredProtocolGuid, (VOID**)&EDID, ImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL)) ) )
        {
            Print(L"Failed to get EDID info for handle "); PrintDec(i); Print(L": "); PrintHex(Status); Print(L"\r\n");
            return Status;
        }
        
        if (EDID->SizeOfEdid == 0 || EDID->Edid == NULL)
        {
            Print(L"No EDID data for handle "); PrintDec(i); Print(L"\r\n");
            continue;
        }
        
        /*
            THIS CODE IS NOT TESTED!
            ! ! !   D O   N O T   U S E  ! ! !
        */
        EDID14_RAW* EdidData = (EDID14_RAW*)EDID->Edid;
        
        CHAR16 Manufacturer[4] = {0};
        Manufacturer[0] = MANUFACTURER_DECODE_LETTER(EdidData->ManufacturerID >> 10);
        Manufacturer[1] = MANUFACTURER_DECODE_LETTER(EdidData->ManufacturerID >> 5);
        Manufacturer[2] = MANUFACTURER_DECODE_LETTER(EdidData->ManufacturerID);
        Print(L"Manufacturer ID: "); Print(Manufacturer); Print(L"\r\n");
        Print(L"Resolution: "); PrintDec(EdidData->HSize); Print(L"X"); PrintDec(EdidData->VSize); Print(L"\r\n");
        
    }
    return Status;
}

ACPI

If you don't want to use these UEFI protocols you can use ACPI. Each display output device has a _DDC method that is documented in the ACPI specification and can be used to return the EDID data (either as a buffer of 128 or 256 bytes).
This method is conceptually simple but in practice it requires writing a full-blown ACPI parser (including the AML VM) which is a lot of work.
However, ACPI is necessary for modern OSes and so you can use it, later on, to get the EDID data without having to worry about UEFI protocols.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.