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

Add funcs KernAux_Mbr*_is_valid

This commit is contained in:
Alex Kotov 2022-01-17 11:34:42 +05:00
parent e461163b8e
commit 96f71999ee
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
5 changed files with 90 additions and 0 deletions

1
.gitignore vendored
View file

@ -73,6 +73,7 @@
/tests/test_cmdline
/tests/test_elf
/tests/test_itoa
/tests/test_mbr
/tests/test_multiboot2_header_helpers
/tests/test_multiboot2_header_print
/tests/test_multiboot2_header_validation

View file

@ -60,6 +60,7 @@ endif
if WITH_MBR
libkernaux_a_SOURCES += src/mbr.c
TESTS += tests/test_mbr
endif
if WITH_MULTIBOOT2
@ -160,6 +161,10 @@ tests_test_itoa_SOURCES = \
$(libkernaux_a_SOURCES) \
tests/test_itoa.c
tests_test_mbr_SOURCES = \
$(libkernaux_a_SOURCES) \
tests/test_mbr.c
tests_test_multiboot2_header_helpers_SOURCES = \
$(libkernaux_a_SOURCES) \
tests/test_multiboot2_header_helpers.c \

View file

@ -5,6 +5,7 @@
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#define KERNAUX_MBR_SIZE 512
@ -38,6 +39,10 @@ struct KernAux_Mbr {
}
__attribute__((packed));
bool KernAux_Mbr_is_valid(const struct KernAux_Mbr *mbr);
bool KernAux_Mbr_Info_is_valid(const struct KernAux_Mbr_Info *mbr_info);
bool KernAux_Mbr_Entry_is_valid(const struct KernAux_Mbr_Entry *mbr_entry);
#ifdef __cplusplus
}
#endif

View file

@ -2,4 +2,38 @@
#include "config.h"
#endif
#include <kernaux/assert.h>
#include <kernaux/mbr.h>
#include <stdbool.h>
#include <stddef.h>
bool KernAux_Mbr_is_valid(const struct KernAux_Mbr *const mbr)
{
KERNAUX_NOTNULL_RETVAL(mbr, false);
return KernAux_Mbr_Info_is_valid(&mbr->info);
}
bool KernAux_Mbr_Info_is_valid(const struct KernAux_Mbr_Info *const mbr_info)
{
KERNAUX_NOTNULL_RETVAL(mbr_info, false);
if (mbr_info->magic != KERNAUX_MBR_MAGIC) return false;
for (size_t index = 0; index < KERNAUX_MBR_ENTRIES; ++index) {
if (!KernAux_Mbr_Entry_is_valid(&mbr_info->entries[index])) {
return false;
}
}
return true;
}
bool KernAux_Mbr_Entry_is_valid(const struct KernAux_Mbr_Entry *const mbr_entry)
{
KERNAUX_NOTNULL_RETVAL(mbr_entry, false);
// TODO: implement this
return true;
}

45
tests/test_mbr.c Normal file
View file

@ -0,0 +1,45 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <kernaux/mbr.h>
#include <assert.h>
#include <string.h>
static void create_valid_mbr(struct KernAux_Mbr *mbr);
int main()
{
struct KernAux_Mbr mbr;
create_valid_mbr(&mbr);
assert(KernAux_Mbr_is_valid(&mbr));
assert(KernAux_Mbr_Info_is_valid(&mbr.info));
for (size_t index = 0; index < KERNAUX_MBR_ENTRIES; ++index) {
assert(KernAux_Mbr_Entry_is_valid(&mbr.info.entries[index]));
}
create_valid_mbr(&mbr);
mbr.info.magic = 123;
assert(!KernAux_Mbr_is_valid(&mbr));
assert(!KernAux_Mbr_Info_is_valid(&mbr.info));
for (size_t index = 0; index < KERNAUX_MBR_ENTRIES; ++index) {
assert(KernAux_Mbr_Entry_is_valid(&mbr.info.entries[index]));
}
// TODO: test partition table
return 0;
}
void create_valid_mbr(struct KernAux_Mbr *const mbr)
{
memset(mbr, 0, sizeof(*mbr));
mbr->info.magic = KERNAUX_MBR_MAGIC;
mbr->info.disk_id = 0xCAFEBABE;
// TODO: fill partition table
}