diff --git a/src/multiboot2/header_is_valid.c b/src/multiboot2/header_is_valid.c index 8cc2bf4..fb8cc84 100644 --- a/src/multiboot2/header_is_valid.c +++ b/src/multiboot2/header_is_valid.c @@ -5,12 +5,56 @@ #include #include +#include +#include bool KernAux_Multiboot2_Header_is_valid( const struct KernAux_Multiboot2_Header *const multiboot2_header ) { - // TODO: write this - return false; + if (multiboot2_header->total_size < + sizeof(struct KernAux_Multiboot2_Header) + + sizeof(struct KernAux_Multiboot2_HTag_None)) + { + return false; + } + + const struct KernAux_Multiboot2_HTagBase *tag_base = + (struct KernAux_Multiboot2_HTagBase*) + KERNAUX_MULTIBOOT2_DATA(multiboot2_header); + + const struct KernAux_Multiboot2_HTagBase *none_tag_base = NULL; + + while (tag_base < + (struct KernAux_Multiboot2_HTagBase*) + ((uint8_t*)multiboot2_header + multiboot2_header->total_size)) + { + if (!KernAux_Multiboot2_HTagBase_is_valid(tag_base)) return false; + + if (tag_base->type == KERNAUX_MULTIBOOT2_HTAG_NONE && + none_tag_base == NULL + ) { + none_tag_base = tag_base; + } + + tag_base = KERNAUX_MULTIBOOT2_HTAG_NEXT(tag_base); + } + + if (tag_base != + (struct KernAux_Multiboot2_HTagBase*) + ((uint8_t*)multiboot2_header + multiboot2_header->total_size)) + { + return false; + } + + if (none_tag_base != + (struct KernAux_Multiboot2_HTagBase*) + ((uint8_t*)tag_base - + sizeof(struct KernAux_Multiboot2_HTag_None))) + { + return false; + } + + return true; } bool KernAux_Multiboot2_HTagBase_is_valid( diff --git a/src/multiboot2/info_is_valid.c b/src/multiboot2/info_is_valid.c index 7e35057..64be3bd 100644 --- a/src/multiboot2/info_is_valid.c +++ b/src/multiboot2/info_is_valid.c @@ -6,11 +6,17 @@ #include #include +#include bool KernAux_Multiboot2_Info_is_valid( const struct KernAux_Multiboot2_Info *const multiboot2_info ) { - if (multiboot2_info->total_size <= 8) return false; + if (multiboot2_info->total_size < + sizeof(struct KernAux_Multiboot2_Info) + + sizeof(struct KernAux_Multiboot2_ITag_None)) + { + return false; + } const struct KernAux_Multiboot2_ITagBase *tag_base = (struct KernAux_Multiboot2_ITagBase*) @@ -20,7 +26,7 @@ bool KernAux_Multiboot2_Info_is_valid( while (tag_base < (struct KernAux_Multiboot2_ITagBase*) - ((unsigned char*)multiboot2_info + multiboot2_info->total_size)) + ((uint8_t*)multiboot2_info + multiboot2_info->total_size)) { if (!KernAux_Multiboot2_ITagBase_is_valid(tag_base)) return false; @@ -35,14 +41,14 @@ bool KernAux_Multiboot2_Info_is_valid( if (tag_base != (struct KernAux_Multiboot2_ITagBase*) - ((unsigned char*)multiboot2_info + multiboot2_info->total_size)) + ((uint8_t*)multiboot2_info + multiboot2_info->total_size)) { return false; } if (none_tag_base != (struct KernAux_Multiboot2_ITagBase*) - ((unsigned char*)tag_base - + ((uint8_t*)tag_base - sizeof(struct KernAux_Multiboot2_ITag_None))) { return false;