1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2024-11-13 11:04:27 -05:00

Add function "KernAux_Multiboot2_is_valid"

This commit is contained in:
Alex Kotov 2020-11-28 05:02:44 +05:00
parent 23bd35c27b
commit a00a597393
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
3 changed files with 316 additions and 0 deletions

View file

@ -37,12 +37,16 @@ enum KernAux_Multiboot2_TagType {
struct KernAux_Multiboot2 {
unsigned int total_size : 32;
unsigned int reserved1 : 32;
unsigned char data[];
}
__attribute__((packed));
struct KernAux_Multiboot2_TagBase {
enum KernAux_Multiboot2_TagType type : 32;
unsigned int size : 32;
unsigned char data[];
}
__attribute__((packed));
@ -302,6 +306,11 @@ __attribute__((packed));
* Tag validation functions *
****************************/
unsigned char KernAux_Multiboot2_is_valid(
const struct KernAux_Multiboot2 *multiboot2
)
__attribute__((nonnull));
unsigned char KernAux_Multiboot2_TagBase_is_valid(
const struct KernAux_Multiboot2_TagBase *tag_base
)

View file

@ -1,5 +1,47 @@
#include <kernaux/multiboot2.h>
unsigned char KernAux_Multiboot2_is_valid(
const struct KernAux_Multiboot2 *const multiboot2
) {
if (multiboot2->total_size <= 8) {
return 0;
}
const struct KernAux_Multiboot2_TagBase *tag_base =
(struct KernAux_Multiboot2_TagBase*)multiboot2->data;
const struct KernAux_Multiboot2_TagBase *none_tag_base = (void*)0;
while ((void*)tag_base < (void*)multiboot2 + multiboot2->total_size) {
if (!KernAux_Multiboot2_TagBase_is_valid(tag_base)) {
return 0;
}
if (tag_base->type == KERNAUX_MULTIBOOT2_TAGTYPE_NONE &&
none_tag_base == 0
) {
none_tag_base = tag_base;
}
tag_base = (struct KernAux_Multiboot2_TagBase*)(
(void*)tag_base +
tag_base->size
);
}
if ((void*)tag_base != (void*)multiboot2 + multiboot2->total_size) {
return 0;
}
if (none_tag_base !=
(void*)tag_base - sizeof(struct KernAux_Multiboot2_Tag_None)
) {
return 0;
}
return 1;
}
unsigned char KernAux_Multiboot2_TagBase_is_valid(
const struct KernAux_Multiboot2_TagBase *const tag_base
) {

View file

@ -467,12 +467,277 @@ tag_vbe_info_invalid_size = {
.vbe_mode_info = {0, 0, 0},
};
/**************
* Multiboot2 *
**************/
static const struct {
struct KernAux_Multiboot2 multiboot2;
struct KernAux_Multiboot2_Tag_None tag_none;
} multiboot2_empty_valid = {
.multiboot2 = {
.total_size = 8 + 8,
.reserved1 = 0,
},
.tag_none = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_NONE,
.size = 8,
},
},
};
static const struct {
struct KernAux_Multiboot2 multiboot2;
struct KernAux_Multiboot2_Tag_BasicMemoryInfo tag_basic_memory_info;
struct KernAux_Multiboot2_Tag_None tag_none;
} multiboot2_with_some_additional_tag_valid = {
.multiboot2 = {
.total_size = 8 + 16 + 8,
.reserved1 = 0,
},
.tag_basic_memory_info = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_BASIC_MEMORY_INFO,
.size = 16,
},
.mem_lower = 123,
.mem_upper = 123,
},
.tag_none = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_NONE,
.size = 8,
},
},
};
static const struct {
struct KernAux_Multiboot2 multiboot2;
struct KernAux_Multiboot2_Tag_BasicMemoryInfo tag_basic_memory_info;
struct KernAux_Multiboot2_Tag_BIOSBootDevice tag_bios_boot_device;
struct KernAux_Multiboot2_Tag_None tag_none;
} multiboot2_with_more_additional_tags_valid = {
.multiboot2 = {
.total_size = 8 + 16 + 20 + 8,
.reserved1 = 0,
},
.tag_basic_memory_info = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_BASIC_MEMORY_INFO,
.size = 16,
},
.mem_lower = 123,
.mem_upper = 123,
},
.tag_bios_boot_device = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_BIOS_BOOT_DEVICE,
.size = 20,
},
.bios_dev = 123,
.partition = 456,
.sub_partition = 789,
},
.tag_none = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_NONE,
.size = 8,
},
},
};
static const struct {
struct KernAux_Multiboot2 multiboot2;
struct KernAux_Multiboot2_Tag_None tag_none;
} multiboot2_empty_invalid_size = {
.multiboot2 = {
.total_size = 8,
.reserved1 = 0,
},
.tag_none = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_NONE,
.size = 8,
},
},
};
static const struct KernAux_Multiboot2 multiboot2_without_none_tag_invalid = {
.total_size = 8,
.reserved1 = 0,
};
static const struct {
struct KernAux_Multiboot2 multiboot2;
struct KernAux_Multiboot2_Tag_BasicMemoryInfo tag_basic_memory_info;
} multiboot2_with_invalid_last_tag_invalid = {
.multiboot2 = {
.total_size = 8 + 16,
.reserved1 = 0,
},
.tag_basic_memory_info = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_BASIC_MEMORY_INFO,
.size = 16,
},
.mem_lower = 123,
.mem_upper = 123,
},
};
static const struct {
struct KernAux_Multiboot2 multiboot2;
struct KernAux_Multiboot2_Tag_BasicMemoryInfo tag_basic_memory_info;
struct KernAux_Multiboot2_Tag_None tag_none1;
struct KernAux_Multiboot2_Tag_BIOSBootDevice tag_bios_boot_device;
struct KernAux_Multiboot2_Tag_None tag_none2;
} multiboot2_with_early_none_tag_invalid = {
.multiboot2 = {
.total_size = 8 + 16 + 8 + 20 + 8,
.reserved1 = 0,
},
.tag_basic_memory_info = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_BASIC_MEMORY_INFO,
.size = 16,
},
.mem_lower = 123,
.mem_upper = 123,
},
.tag_none1 = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_NONE,
.size = 8,
},
},
.tag_bios_boot_device = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_BIOS_BOOT_DEVICE,
.size = 20,
},
.bios_dev = 123,
.partition = 456,
.sub_partition = 789,
},
.tag_none2 = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_NONE,
.size = 8,
},
},
};
static const struct {
struct KernAux_Multiboot2 multiboot2;
struct KernAux_Multiboot2_Tag_BasicMemoryInfo tag_basic_memory_info;
struct KernAux_Multiboot2_Tag_BIOSBootDevice tag_bios_boot_device;
struct KernAux_Multiboot2_Tag_None tag_none;
} multiboot2_with_more_additional_tags_invalid_size_too_big = {
.multiboot2 = {
.total_size = 8 + 16 + 20 + 8 + 1,
.reserved1 = 0,
},
.tag_basic_memory_info = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_BASIC_MEMORY_INFO,
.size = 16,
},
.mem_lower = 123,
.mem_upper = 123,
},
.tag_bios_boot_device = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_BIOS_BOOT_DEVICE,
.size = 20,
},
.bios_dev = 123,
.partition = 456,
.sub_partition = 789,
},
.tag_none = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_NONE,
.size = 8,
},
},
};
static const struct {
struct KernAux_Multiboot2 multiboot2;
struct KernAux_Multiboot2_Tag_BasicMemoryInfo tag_basic_memory_info;
struct KernAux_Multiboot2_Tag_BIOSBootDevice tag_bios_boot_device;
struct KernAux_Multiboot2_Tag_None tag_none;
} multiboot2_with_more_additional_tags_invalid_size_too_small = {
.multiboot2 = {
.total_size = 8 + 16 + 20 + 8 - 1,
.reserved1 = 0,
},
.tag_basic_memory_info = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_BASIC_MEMORY_INFO,
.size = 16,
},
.mem_lower = 123,
.mem_upper = 123,
},
.tag_bios_boot_device = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_BIOS_BOOT_DEVICE,
.size = 20,
},
.bios_dev = 123,
.partition = 456,
.sub_partition = 789,
},
.tag_none = {
.base = {
.type = KERNAUX_MULTIBOOT2_TAGTYPE_NONE,
.size = 8,
},
},
};
/********
* main *
********/
int main()
{
// Multiboot2
assert(KernAux_Multiboot2_is_valid(&multiboot2_empty_valid.multiboot2));
assert(KernAux_Multiboot2_is_valid(
&multiboot2_with_some_additional_tag_valid.multiboot2)
);
assert(KernAux_Multiboot2_is_valid(
&multiboot2_with_more_additional_tags_valid.multiboot2)
);
assert(!KernAux_Multiboot2_is_valid(
&multiboot2_empty_invalid_size.multiboot2
));
assert(!KernAux_Multiboot2_is_valid(&multiboot2_without_none_tag_invalid));
assert(!KernAux_Multiboot2_is_valid(
&multiboot2_with_invalid_last_tag_invalid.multiboot2
));
assert(!KernAux_Multiboot2_is_valid(
&multiboot2_with_early_none_tag_invalid.multiboot2
));
assert(!KernAux_Multiboot2_is_valid(
&multiboot2_with_more_additional_tags_invalid_size_too_big.multiboot2)
);
assert(!KernAux_Multiboot2_is_valid(
&multiboot2_with_more_additional_tags_invalid_size_too_small.multiboot2)
);
// TagBase
assert(KernAux_Multiboot2_TagBase_is_valid(&tag_none_valid.base));