1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2024-10-30 11:54:01 -04:00
libkernaux/src/multiboot2/info_is_valid.c

428 lines
13 KiB
C
Raw Normal View History

2020-12-06 23:46:37 -05:00
#ifdef HAVE_CONFIG_H
2020-12-06 23:42:52 -05:00
#include "config.h"
2020-12-06 23:46:37 -05:00
#endif
2020-11-30 06:40:33 -05:00
#include <kernaux/assert.h>
#include <kernaux/multiboot2.h>
2022-01-13 09:01:51 -05:00
#include <stdbool.h>
2020-12-06 05:16:15 -05:00
#include <stddef.h>
2022-01-13 09:32:19 -05:00
#include <stdint.h>
2020-12-06 05:16:15 -05:00
2022-01-12 22:59:52 -05:00
bool KernAux_Multiboot2_Info_is_valid(
const struct KernAux_Multiboot2_Info *const multiboot2_info
) {
KERNAUX_NOTNULL_RETVAL(multiboot2_info, false);
2022-01-13 09:32:19 -05:00
if (multiboot2_info->total_size <
sizeof(struct KernAux_Multiboot2_Info) +
sizeof(struct KernAux_Multiboot2_ITag_None))
{
return false;
}
2022-01-13 09:43:57 -05:00
if (multiboot2_info->total_size % KERNAUX_MULTIBOOT2_TAG_ALIGN != 0) {
return false;
}
2022-01-12 22:03:15 -05:00
const struct KernAux_Multiboot2_ITagBase *tag_base =
(struct KernAux_Multiboot2_ITagBase*)
2022-01-12 22:59:52 -05:00
KERNAUX_MULTIBOOT2_DATA(multiboot2_info);
2022-01-12 22:03:15 -05:00
const struct KernAux_Multiboot2_ITagBase *none_tag_base = NULL;
2021-12-14 14:13:18 -05:00
while (tag_base <
2022-01-12 22:03:15 -05:00
(struct KernAux_Multiboot2_ITagBase*)
2022-01-13 09:32:19 -05:00
((uint8_t*)multiboot2_info + multiboot2_info->total_size))
2021-12-14 14:13:18 -05:00
{
2022-01-12 22:03:15 -05:00
if (!KernAux_Multiboot2_ITagBase_is_valid(tag_base)) return false;
2022-01-12 21:49:25 -05:00
if (tag_base->type == KERNAUX_MULTIBOOT2_ITAG_NONE &&
2020-12-06 05:16:15 -05:00
none_tag_base == NULL
) {
none_tag_base = tag_base;
}
tag_base = KERNAUX_MULTIBOOT2_ITAG_NEXT(tag_base);
}
2021-12-14 14:13:18 -05:00
if (tag_base !=
2022-01-12 22:03:15 -05:00
(struct KernAux_Multiboot2_ITagBase*)
2022-01-13 09:32:19 -05:00
((uint8_t*)multiboot2_info + multiboot2_info->total_size))
2021-12-14 14:13:18 -05:00
{
2020-12-06 05:16:15 -05:00
return false;
}
if (none_tag_base !=
2022-01-12 22:03:15 -05:00
(struct KernAux_Multiboot2_ITagBase*)
2022-01-13 09:32:19 -05:00
((uint8_t*)tag_base -
2022-01-12 22:38:51 -05:00
sizeof(struct KernAux_Multiboot2_ITag_None)))
2021-12-14 14:13:18 -05:00
{
2020-12-06 05:16:15 -05:00
return false;
}
2020-12-06 05:16:15 -05:00
return true;
}
2022-01-12 22:03:15 -05:00
bool KernAux_Multiboot2_ITagBase_is_valid(
const struct KernAux_Multiboot2_ITagBase *const tag_base
) {
KERNAUX_NOTNULL_RETVAL(tag_base, false);
switch (tag_base->type) {
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_NONE:
return KernAux_Multiboot2_ITag_None_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_None*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_BOOT_CMD_LINE:
return KernAux_Multiboot2_ITag_BootCmdLine_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_BootCmdLine*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_BOOT_LOADER_NAME:
return KernAux_Multiboot2_ITag_BootLoaderName_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_BootLoaderName*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_MODULE:
return KernAux_Multiboot2_ITag_Module_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_Module*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_BASIC_MEMORY_INFO:
return KernAux_Multiboot2_ITag_BasicMemoryInfo_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_BasicMemoryInfo*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_BIOS_BOOT_DEVICE:
return KernAux_Multiboot2_ITag_BIOSBootDevice_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_BIOSBootDevice*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_MEMORY_MAP:
return KernAux_Multiboot2_ITag_MemoryMap_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_MemoryMap*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_VBE_INFO:
return KernAux_Multiboot2_ITag_VBEInfo_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_VBEInfo*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_FRAMEBUFFER_INFO:
return KernAux_Multiboot2_ITag_FramebufferInfo_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_FramebufferInfo*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_ELF_SYMBOLS:
return KernAux_Multiboot2_ITag_ELFSymbols_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_ELFSymbols*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_APM_TABLE:
return KernAux_Multiboot2_ITag_APMTable_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_APMTable*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_EFI_32BIT_SYSTEM_TABLE_PTR:
return KernAux_Multiboot2_ITag_EFI32bitSystemTablePtr_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_EFI32bitSystemTablePtr*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_EFI_64BIT_SYSTEM_TABLE_PTR:
return KernAux_Multiboot2_ITag_EFI64bitSystemTablePtr_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_EFI64bitSystemTablePtr*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_SMBIOS_TABLES:
return KernAux_Multiboot2_ITag_SMBIOSTables_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_SMBIOSTables*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_ACPI_OLD_RSDP:
return KernAux_Multiboot2_ITag_ACPIOldRSDP_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_ACPIOldRSDP*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_ACPI_NEW_RSDP:
return KernAux_Multiboot2_ITag_ACPINewRSDP_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_ACPINewRSDP*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_NETWORKING_INFO:
return KernAux_Multiboot2_ITag_NetworkingInfo_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_NetworkingInfo*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_EFI_MEMORY_MAP:
return KernAux_Multiboot2_ITag_EFIMemoryMap_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_EFIMemoryMap*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_EFI_BOOT_SERVICES_NOT_TERMINATED:
return KernAux_Multiboot2_ITag_EFIBootServicesNotTerminated_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_EFIBootServicesNotTerminated*)
tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_EFI_32BIT_IMAGE_HANDLE_PTR:
return KernAux_Multiboot2_ITag_EFI32bitImageHandlePtr_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_EFI32bitImageHandlePtr*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_EFI_64BIT_IMAGE_HANDLE_PTR:
return KernAux_Multiboot2_ITag_EFI64bitImageHandlePtr_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_EFI64bitImageHandlePtr*)tag_base
);
2022-01-12 21:49:25 -05:00
case KERNAUX_MULTIBOOT2_ITAG_IMAGE_LOAD_BASE_PHYS_ADDR:
return KernAux_Multiboot2_ITag_ImageLoadBasePhysAddr_is_valid(
2022-01-12 22:38:51 -05:00
(struct KernAux_Multiboot2_ITag_ImageLoadBasePhysAddr*)tag_base
);
default:
2020-12-06 05:16:15 -05:00
return false;
}
}
bool KernAux_Multiboot2_ITag_None_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_None *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_NONE &&
tag->base.size == 8
);
}
bool KernAux_Multiboot2_ITag_BootCmdLine_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_BootCmdLine *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
2021-12-16 10:26:16 -05:00
size_t index = 1;
for (
2022-01-11 04:29:56 -05:00
const char *ptr = (char*)KERNAUX_MULTIBOOT2_DATA(tag);
*ptr && index < tag->base.size;
++ptr
) {
++index;
}
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_BOOT_CMD_LINE &&
tag->base.size == 8 + index
);
}
bool KernAux_Multiboot2_ITag_BootLoaderName_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_BootLoaderName *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
2021-12-16 10:26:16 -05:00
size_t index = 1;
for (
2022-01-11 04:29:56 -05:00
const char *ptr = (char*)KERNAUX_MULTIBOOT2_DATA(tag);
*ptr && index < tag->base.size;
++ptr
) {
++index;
}
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_BOOT_LOADER_NAME &&
tag->base.size == 8 + index
);
}
bool KernAux_Multiboot2_ITag_Module_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_Module *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
2021-12-16 10:26:16 -05:00
size_t index = 1;
for (
2022-01-11 04:29:56 -05:00
const char *ptr = (char*)KERNAUX_MULTIBOOT2_DATA(tag);
*ptr && index < tag->base.size;
++ptr
) {
++index;
}
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_MODULE &&
tag->base.size == 16 + index &&
tag->mod_start < tag->mod_end
);
}
bool KernAux_Multiboot2_ITag_BasicMemoryInfo_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_BasicMemoryInfo *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_BASIC_MEMORY_INFO &&
tag->base.size == 16
);
}
bool KernAux_Multiboot2_ITag_BIOSBootDevice_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_BIOSBootDevice *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_BIOS_BOOT_DEVICE &&
tag->base.size == 20
);
}
bool KernAux_Multiboot2_ITag_MemoryMap_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_MemoryMap *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_MEMORY_MAP &&
tag->base.size >= 16 &&
tag->entry_size > 0 &&
tag->entry_size % 8 == 0 &&
(tag->base.size - 16) % tag->entry_size == 0
);
}
bool KernAux_Multiboot2_ITag_VBEInfo_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_VBEInfo *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_VBE_INFO &&
tag->base.size == 784
);
}
bool KernAux_Multiboot2_ITag_FramebufferInfo_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_FramebufferInfo *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_FRAMEBUFFER_INFO &&
tag->base.size >= 31
);
}
bool KernAux_Multiboot2_ITag_ELFSymbols_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_ELFSymbols *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_ELF_SYMBOLS &&
2022-05-24 11:09:02 -04:00
tag->base.size >= 16 &&
(
tag->ent_size == 0 ||
(tag->base.size - 16) % tag->ent_size == 0
)
);
}
bool KernAux_Multiboot2_ITag_APMTable_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_APMTable *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_APM_TABLE &&
tag->base.size == 28
);
}
bool KernAux_Multiboot2_ITag_EFI32bitSystemTablePtr_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_EFI32bitSystemTablePtr *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_EFI_32BIT_SYSTEM_TABLE_PTR &&
tag->base.size == 12
);
}
bool KernAux_Multiboot2_ITag_EFI64bitSystemTablePtr_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_EFI64bitSystemTablePtr *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_EFI_64BIT_SYSTEM_TABLE_PTR &&
tag->base.size == 16
);
}
bool KernAux_Multiboot2_ITag_SMBIOSTables_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_SMBIOSTables *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_SMBIOS_TABLES &&
tag->base.size >= 16
);
}
bool KernAux_Multiboot2_ITag_ACPIOldRSDP_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_ACPIOldRSDP *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_ACPI_OLD_RSDP &&
tag->base.size >= 8
);
}
bool KernAux_Multiboot2_ITag_ACPINewRSDP_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_ACPINewRSDP *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_ACPI_NEW_RSDP &&
tag->base.size >= 8
);
}
bool KernAux_Multiboot2_ITag_NetworkingInfo_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_NetworkingInfo *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_NETWORKING_INFO &&
tag->base.size >= 8
);
}
bool KernAux_Multiboot2_ITag_EFIMemoryMap_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_EFIMemoryMap *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_EFI_MEMORY_MAP &&
tag->base.size >= 16
);
}
bool KernAux_Multiboot2_ITag_EFIBootServicesNotTerminated_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_EFIBootServicesNotTerminated *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
tag->base.type ==
2022-01-12 21:49:25 -05:00
KERNAUX_MULTIBOOT2_ITAG_EFI_BOOT_SERVICES_NOT_TERMINATED &&
tag->base.size == 8
);
}
bool KernAux_Multiboot2_ITag_EFI32bitImageHandlePtr_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_EFI32bitImageHandlePtr *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_EFI_32BIT_IMAGE_HANDLE_PTR &&
tag->base.size == 12
);
}
bool KernAux_Multiboot2_ITag_EFI64bitImageHandlePtr_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_EFI64bitImageHandlePtr *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_EFI_64BIT_IMAGE_HANDLE_PTR &&
tag->base.size == 16
);
}
bool KernAux_Multiboot2_ITag_ImageLoadBasePhysAddr_is_valid(
2022-01-12 22:38:51 -05:00
const struct KernAux_Multiboot2_ITag_ImageLoadBasePhysAddr *const tag
) {
KERNAUX_NOTNULL_RETVAL(tag, false);
return (
2022-01-12 21:49:25 -05:00
tag->base.type == KERNAUX_MULTIBOOT2_ITAG_IMAGE_LOAD_BASE_PHYS_ADDR &&
tag->base.size == 12
);
}