rulimine/common/drivers/edid.c

105 lines
2.3 KiB
C
Raw Normal View History

2021-03-06 01:48:52 +03:00
#include <stdint.h>
#include <stddef.h>
#include <drivers/edid.h>
#include <mm/pmm.h>
2022-08-27 00:44:47 +03:00
#include <lib/misc.h>
2021-03-06 01:48:52 +03:00
#include <lib/libc.h>
#include <lib/print.h>
#if defined (BIOS)
2021-03-06 01:48:52 +03:00
#include <lib/real.h>
struct edid_info_struct *get_edid_info(void) {
static struct edid_info_struct *buf = NULL;
if (buf == NULL)
buf = conv_mem_alloc(sizeof(struct edid_info_struct));
2021-03-06 01:48:52 +03:00
struct rm_regs r = {0};
r.eax = 0x4f15;
r.ebx = 0x0001;
r.edi = (uint32_t)rm_off(buf);
r.ds = (uint32_t)rm_seg(buf);
r.es = r.ds;
2021-03-06 01:48:52 +03:00
rm_int(0x10, &r, &r);
if ((r.eax & 0x00ff) != 0x4f)
goto fail;
if ((r.eax & 0xff00) != 0)
goto fail;
for (size_t i = 0; i < sizeof(struct edid_info_struct); i++)
if (((uint8_t *)buf)[i] != 0)
goto success;
2021-03-06 01:48:52 +03:00
fail:
2021-05-11 07:46:42 +03:00
printv("edid: Could not fetch EDID data.\n");
2021-03-06 01:48:52 +03:00
return NULL;
success:
2021-05-11 07:46:42 +03:00
printv("edid: Success.\n");
return buf;
2021-03-06 01:48:52 +03:00
}
#endif
#if defined (UEFI)
2021-03-06 01:48:52 +03:00
#include <efi.h>
struct edid_info_struct *get_edid_info(void) {
struct edid_info_struct *buf = ext_mem_alloc(sizeof(struct edid_info_struct));
EFI_STATUS status;
EFI_HANDLE tmp_handles[1];
EFI_HANDLE *handles = tmp_handles;
UINTN handles_size = sizeof(EFI_HANDLE);
2021-03-06 01:48:52 +03:00
EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
status = gBS->LocateHandle(ByProtocol, &gop_guid, NULL, &handles_size, handles);
2021-03-06 01:48:52 +03:00
if (status != EFI_SUCCESS && status != EFI_BUFFER_TOO_SMALL)
goto fail_n;
2021-03-06 01:48:52 +03:00
handles = ext_mem_alloc(handles_size);
status = gBS->LocateHandle(ByProtocol, &gop_guid, NULL, &handles_size, handles);
2021-03-06 01:48:52 +03:00
if (status)
goto fail;
EFI_EDID_ACTIVE_PROTOCOL *edid = NULL;
EFI_GUID edid_guid = EFI_EDID_ACTIVE_PROTOCOL_GUID;
status = gBS->HandleProtocol(handles[0], &edid_guid, (void **)&edid);
2021-03-06 01:48:52 +03:00
if (status)
goto fail;
if (edid->SizeOfEdid < sizeof(struct edid_info_struct))
2021-03-06 01:48:52 +03:00
goto fail;
memcpy(buf, edid->Edid, sizeof(struct edid_info_struct));
for (size_t i = 0; i < sizeof(struct edid_info_struct); i++)
if (((uint8_t *)buf)[i] != 0)
goto success;
2021-03-06 01:48:52 +03:00
fail:
pmm_free(handles, handles_size);
fail_n:
2021-05-11 07:46:42 +03:00
printv("edid: Could not fetch EDID data.\n");
2021-03-06 01:48:52 +03:00
return NULL;
success:
pmm_free(handles, handles_size);
2021-05-11 07:46:42 +03:00
printv("edid: Success.\n");
return buf;
2021-03-06 01:48:52 +03:00
}
#endif