mirror of
https://github.com/tailix/libkernaux.git
synced 2025-04-14 17:32:55 -04:00
Add function "KernAux_Multiboot2_boot_cmd_line"
This commit is contained in:
parent
a1a7adf722
commit
2367a9e4c7
6 changed files with 185 additions and 4 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -26,5 +26,6 @@
|
|||
|
||||
/tests/multiboot2_print1
|
||||
/tests/multiboot2_print2
|
||||
/tests/test_multiboot2_helpers
|
||||
/tests/test_multiboot2_print
|
||||
/tests/test_multiboot2_validation
|
||||
|
|
|
@ -5,6 +5,7 @@ AM_CFLAGS = -std=c99 -Wall -Wextra -I$(top_srcdir)/include
|
|||
lib_LIBRARIES = libkernaux.a
|
||||
|
||||
TESTS = \
|
||||
tests/test_multiboot2_helpers \
|
||||
tests/test_multiboot2_print \
|
||||
tests/test_multiboot2_validation
|
||||
|
||||
|
@ -15,6 +16,7 @@ noinst_PROGRAMS = \
|
|||
|
||||
libkernaux_a_SOURCES = \
|
||||
src/arch/i386.S \
|
||||
src/multiboot2/helpers.c \
|
||||
src/multiboot2/is_valid.c \
|
||||
src/multiboot2/print.c \
|
||||
src/pfa.c
|
||||
|
@ -27,6 +29,10 @@ tests_multiboot2_print2_SOURCES = \
|
|||
$(libkernaux_a_SOURCES) \
|
||||
tests/multiboot2_print2.c
|
||||
|
||||
tests_test_multiboot2_helpers_SOURCES = \
|
||||
$(libkernaux_a_SOURCES) \
|
||||
tests/test_multiboot2_helpers.c
|
||||
|
||||
tests_test_multiboot2_print_SOURCES = \
|
||||
$(libkernaux_a_SOURCES) \
|
||||
tests/test_multiboot2_print.c
|
||||
|
|
|
@ -304,6 +304,15 @@ struct KernAux_Multiboot2_Tag_MemoryMap_EntryBase {
|
|||
}
|
||||
__attribute__((packed));
|
||||
|
||||
/********************
|
||||
* Helper functions *
|
||||
********************/
|
||||
|
||||
const char *KernAux_Multiboot2_boot_cmd_line(
|
||||
const struct KernAux_Multiboot2 *multiboot2
|
||||
)
|
||||
__attribute__((nonnull));
|
||||
|
||||
/*******************
|
||||
* Print functions *
|
||||
*******************/
|
||||
|
|
27
src/multiboot2/helpers.c
Normal file
27
src/multiboot2/helpers.c
Normal file
|
@ -0,0 +1,27 @@
|
|||
#include <kernaux/multiboot2.h>
|
||||
|
||||
const char *KernAux_Multiboot2_boot_cmd_line(
|
||||
const struct KernAux_Multiboot2 *const multiboot2
|
||||
) {
|
||||
const struct KernAux_Multiboot2_TagBase *tag_base =
|
||||
(struct KernAux_Multiboot2_TagBase*)multiboot2->data;
|
||||
|
||||
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_BOOT_CMD_LINE) {
|
||||
const struct KernAux_Multiboot2_Tag_BootCmdLine *const tag =
|
||||
(struct KernAux_Multiboot2_Tag_BootCmdLine*)tag_base;
|
||||
|
||||
return tag->cmdline;
|
||||
}
|
||||
|
||||
tag_base = (struct KernAux_Multiboot2_TagBase*)(
|
||||
(void*)tag_base + ((tag_base->size + 7) & ~7)
|
||||
);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -77,10 +77,6 @@ void KernAux_Multiboot2_print(
|
|||
print(" size: %u\n", multiboot2->total_size);
|
||||
print(" reserved1: %u\n", multiboot2->reserved1);
|
||||
|
||||
if (multiboot2->total_size <= 8) {
|
||||
return;
|
||||
}
|
||||
|
||||
const struct KernAux_Multiboot2_TagBase *tag_base =
|
||||
(struct KernAux_Multiboot2_TagBase*)multiboot2->data;
|
||||
|
||||
|
|
142
tests/test_multiboot2_helpers.c
Normal file
142
tests/test_multiboot2_helpers.c
Normal file
|
@ -0,0 +1,142 @@
|
|||
#include <kernaux/multiboot2.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
static const struct {
|
||||
struct KernAux_Multiboot2 multiboot2;
|
||||
struct KernAux_Multiboot2_Tag_None tag_none;
|
||||
} multiboot2_without_boot_cmd_line = {
|
||||
.multiboot2 = {
|
||||
.total_size = sizeof(multiboot2_without_boot_cmd_line),
|
||||
.reserved1 = 0,
|
||||
},
|
||||
.tag_none = {
|
||||
.base = {
|
||||
.type = KERNAUX_MULTIBOOT2_TAGTYPE_NONE,
|
||||
.size = sizeof(multiboot2_without_boot_cmd_line.tag_none),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static const struct {
|
||||
struct KernAux_Multiboot2 multiboot2;
|
||||
|
||||
struct {
|
||||
struct KernAux_Multiboot2_Tag_BootCmdLine tag;
|
||||
char cmdline[14];
|
||||
} tag_boot_cmd_line;
|
||||
|
||||
unsigned char _align1[2];
|
||||
|
||||
struct KernAux_Multiboot2_Tag_None tag_none;
|
||||
} multiboot2_with_some_boot_cmd_line = {
|
||||
.multiboot2 = {
|
||||
.total_size = sizeof(multiboot2_with_some_boot_cmd_line),
|
||||
.reserved1 = 0,
|
||||
},
|
||||
.tag_boot_cmd_line = {
|
||||
.tag = {
|
||||
.base = {
|
||||
.type = KERNAUX_MULTIBOOT2_TAGTYPE_BOOT_CMD_LINE,
|
||||
.size = sizeof(
|
||||
multiboot2_with_some_boot_cmd_line.tag_boot_cmd_line
|
||||
),
|
||||
},
|
||||
},
|
||||
.cmdline = "Hello, World!\0",
|
||||
},
|
||||
.tag_none = {
|
||||
.base = {
|
||||
.type = KERNAUX_MULTIBOOT2_TAGTYPE_NONE,
|
||||
.size = sizeof(multiboot2_with_some_boot_cmd_line.tag_none),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static const struct {
|
||||
struct KernAux_Multiboot2 multiboot2;
|
||||
|
||||
struct {
|
||||
struct KernAux_Multiboot2_Tag_BootCmdLine tag;
|
||||
char cmdline[14];
|
||||
} tag_boot_cmd_line1;
|
||||
|
||||
unsigned char _align1[2];
|
||||
|
||||
struct {
|
||||
struct KernAux_Multiboot2_Tag_BootCmdLine tag;
|
||||
char cmdline[13];
|
||||
} tag_boot_cmd_line2;
|
||||
|
||||
unsigned char _align2[3];
|
||||
|
||||
struct KernAux_Multiboot2_Tag_None tag_none;
|
||||
} multiboot2_with_two_boot_cmd_lines = {
|
||||
.multiboot2 = {
|
||||
.total_size = sizeof(multiboot2_with_two_boot_cmd_lines),
|
||||
.reserved1 = 0,
|
||||
},
|
||||
.tag_boot_cmd_line1 = {
|
||||
.tag = {
|
||||
.base = {
|
||||
.type = KERNAUX_MULTIBOOT2_TAGTYPE_BOOT_CMD_LINE,
|
||||
.size = sizeof(
|
||||
multiboot2_with_two_boot_cmd_lines.tag_boot_cmd_line1
|
||||
),
|
||||
},
|
||||
},
|
||||
.cmdline = "Hello, World!\0",
|
||||
},
|
||||
.tag_boot_cmd_line2 = {
|
||||
.tag = {
|
||||
.base = {
|
||||
.type = KERNAUX_MULTIBOOT2_TAGTYPE_BOOT_CMD_LINE,
|
||||
.size = sizeof(
|
||||
multiboot2_with_two_boot_cmd_lines.tag_boot_cmd_line2
|
||||
),
|
||||
},
|
||||
},
|
||||
.cmdline = "Hello again!\0",
|
||||
},
|
||||
.tag_none = {
|
||||
.base = {
|
||||
.type = KERNAUX_MULTIBOOT2_TAGTYPE_NONE,
|
||||
.size = sizeof(multiboot2_with_two_boot_cmd_lines.tag_none),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
assert(KernAux_Multiboot2_is_valid(
|
||||
&multiboot2_without_boot_cmd_line.multiboot2
|
||||
));
|
||||
|
||||
assert(KernAux_Multiboot2_is_valid(
|
||||
&multiboot2_with_some_boot_cmd_line.multiboot2
|
||||
));
|
||||
|
||||
assert(KernAux_Multiboot2_is_valid(
|
||||
&multiboot2_with_two_boot_cmd_lines.multiboot2
|
||||
));
|
||||
|
||||
assert(
|
||||
KernAux_Multiboot2_boot_cmd_line(
|
||||
&multiboot2_without_boot_cmd_line.multiboot2
|
||||
) == 0
|
||||
);
|
||||
|
||||
assert(
|
||||
KernAux_Multiboot2_boot_cmd_line(
|
||||
&multiboot2_with_some_boot_cmd_line.multiboot2
|
||||
) == multiboot2_with_some_boot_cmd_line.tag_boot_cmd_line.cmdline
|
||||
);
|
||||
|
||||
assert(
|
||||
KernAux_Multiboot2_boot_cmd_line(
|
||||
&multiboot2_with_two_boot_cmd_lines.multiboot2
|
||||
) == multiboot2_with_two_boot_cmd_lines.tag_boot_cmd_line1.cmdline
|
||||
);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue