From 79c8bcad6a33e94a0e0708e387065be43c7577e9 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 17 Dec 2022 16:26:20 +0400 Subject: [PATCH 1/6] Add fixtures (#150) --- .gitignore | 11 +++ ChangeLog | 3 +- Makefile.am | 4 + README.md | 3 +- bindings/mruby/test/cmdline.rb | 4 +- bindings/mruby/test/sprintf.rb | 6 +- .../ruby/spec/lib/kernaux/cmdline_spec.rb | 5 +- .../ruby/spec/lib/kernaux/sprintf_spec.rb | 6 +- configure.ac | 12 ++- fixtures/Makefile.am | 53 ++++++++++++ {common => fixtures}/cmdline.yml | 0 fixtures/multiboot2_bin_examples_gen.c.in | 84 +++++++++++++++++++ .../multiboot2_header_example0.h | 0 .../multiboot2_header_example1.h | 0 .../multiboot2_header_example2.h | 0 .../multiboot2_info_example0.h | 0 .../multiboot2_info_example1.h | 0 .../multiboot2_info_example2.h | 0 {common => fixtures}/printf.yml | 0 {common => fixtures}/printf_fmt.yml | 0 {common => fixtures}/printf_orig.yml | 0 tests/Makefile.am | 52 ++++++------ tests/multiboot2_header_print0.c | 2 +- tests/multiboot2_header_print1.c | 2 +- tests/multiboot2_header_print2.c | 2 +- tests/multiboot2_info_print0.c | 2 +- tests/multiboot2_info_print1.c | 2 +- tests/multiboot2_info_print2.c | 2 +- tests/test_multiboot2_common_packing.c | 4 +- tests/test_multiboot2_header_helpers.c | 4 +- tests/test_multiboot2_info_helpers.c | 4 +- tests/test_multiboot2_info_validation.c | 4 +- 32 files changed, 217 insertions(+), 54 deletions(-) create mode 100644 fixtures/Makefile.am rename {common => fixtures}/cmdline.yml (100%) create mode 100644 fixtures/multiboot2_bin_examples_gen.c.in rename {tests => fixtures}/multiboot2_header_example0.h (100%) rename {tests => fixtures}/multiboot2_header_example1.h (100%) rename {tests => fixtures}/multiboot2_header_example2.h (100%) rename {tests => fixtures}/multiboot2_info_example0.h (100%) rename {tests => fixtures}/multiboot2_info_example1.h (100%) rename {tests => fixtures}/multiboot2_info_example2.h (100%) rename {common => fixtures}/printf.yml (100%) rename {common => fixtures}/printf_fmt.yml (100%) rename {common => fixtures}/printf_orig.yml (100%) diff --git a/.gitignore b/.gitignore index 2c4d0ded..d5e2e458 100644 --- a/.gitignore +++ b/.gitignore @@ -42,6 +42,7 @@ /Makefile.in /examples/Makefile.in +/fixtures/Makefile.in /include/Makefile.in /libc/Makefile.in /libc/include/Makefile.in @@ -77,6 +78,7 @@ /Makefile /examples/Makefile +/fixtures/Makefile /include/Makefile /libc/Makefile /libc/include/Makefile @@ -84,6 +86,15 @@ /include/kernaux/version.h +/fixtures/multiboot2_bin_examples_gen +/fixtures/multiboot2_bin_examples_gen.c +/fixtures/multiboot2_header_example0.bin +/fixtures/multiboot2_header_example1.bin +/fixtures/multiboot2_header_example2.bin +/fixtures/multiboot2_info_example0.bin +/fixtures/multiboot2_info_example1.bin +/fixtures/multiboot2_info_example2.bin + /examples/assert /examples/cmdline /examples/generic_display diff --git a/ChangeLog b/ChangeLog index 4958063d..6c0267a2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,10 +1,11 @@ 2022-12-17 Alex Kotov + * configure.ac: Feature "--(enable|disable)-fixtures" has been added * src/multiboot2/*_print.c: Print some values in hex 2022-12-16 Alex Kotov - * configure.ac: Feature "--with[out]-multiboot2" has been added + * configure.ac: Package "--with[out]-multiboot2" has been added * include/kernaux/multiboot2.h: Has been made stable 2022-12-14 Alex Kotov diff --git a/Makefile.am b/Makefile.am index cb51c70e..de9909f1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -13,6 +13,10 @@ endif SUBDIRS += . +if ENABLE_FIXTURES +SUBDIRS += fixtures +endif + if ENABLE_CHECKS SUBDIRS += examples tests endif diff --git a/README.md b/README.md index 79f7fa7c..96754bbb 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,7 @@ stable options. * `--enable-checks-pthreads` - enable tests that require pthreads * `--enable-checks-python` - enable tests that require Python 3 with YAML and Jinja2 +* `--enable-fixtures` - enable fixtures for tests and bindings #### Packages @@ -195,7 +196,7 @@ environment. ``` ./autogen.sh # if present -./configure --enable-checks # or --enable-checks-all, but see prerequisites +./configure --enable-fixtures --enable-checks # or --enable-checks-all, but see prerequisites make ``` diff --git a/bindings/mruby/test/cmdline.rb b/bindings/mruby/test/cmdline.rb index 1a305f15..c24536c0 100644 --- a/bindings/mruby/test/cmdline.rb +++ b/bindings/mruby/test/cmdline.rb @@ -70,8 +70,8 @@ if KernAux::Version.with_cmdline? end end - assert 'usign common tests' do - cmdline_yml = File.expand_path('../../../../common/cmdline.yml', __FILE__) + assert 'usign fixtures' do + cmdline_yml = File.expand_path('../../../../fixtures/cmdline.yml', __FILE__) YAML.load(File.read(cmdline_yml)).each do |test| escape_str = lambda do |str| diff --git a/bindings/mruby/test/sprintf.rb b/bindings/mruby/test/sprintf.rb index d656b5ab..affabf1f 100644 --- a/bindings/mruby/test/sprintf.rb +++ b/bindings/mruby/test/sprintf.rb @@ -65,12 +65,12 @@ if KernAux::Version.with_printf? end [ - ['', 'using regular tests'], - ['_orig', 'using original tests'], + ['', 'using regular fixtures'], + ['_orig', 'using original fixtures'], ].each do |(suffix, description)| assert description do printf_yml = - File.expand_path("../../../../common/printf#{suffix}.yml", __FILE__) + File.expand_path("../../../../fixtures/printf#{suffix}.yml", __FILE__) YAML.load(File.read(printf_yml)).each do |test| expected = test['result'] diff --git a/bindings/ruby/spec/lib/kernaux/cmdline_spec.rb b/bindings/ruby/spec/lib/kernaux/cmdline_spec.rb index b5d427b5..280d5041 100644 --- a/bindings/ruby/spec/lib/kernaux/cmdline_spec.rb +++ b/bindings/ruby/spec/lib/kernaux/cmdline_spec.rb @@ -91,8 +91,9 @@ KernAux::Version.with_cmdline? and RSpec.describe KernAux, '.cmdline' do end end - context 'using common tests' do - cmdline_yml = File.expand_path('../../../../../common/cmdline.yml', __dir__) + context 'using fixtures' do + cmdline_yml = + File.expand_path('../../../../../fixtures/cmdline.yml', __dir__) YAML.safe_load_file(cmdline_yml).each do |test| escape_str = lambda do |str| diff --git a/bindings/ruby/spec/lib/kernaux/sprintf_spec.rb b/bindings/ruby/spec/lib/kernaux/sprintf_spec.rb index c4593cbf..e172052b 100644 --- a/bindings/ruby/spec/lib/kernaux/sprintf_spec.rb +++ b/bindings/ruby/spec/lib/kernaux/sprintf_spec.rb @@ -39,12 +39,12 @@ KernAux::Version.with_printf? and RSpec.describe KernAux, '.sprintf' do end [ - ['', 'using regular tests'], - ['_orig', 'using original tests'], + ['', 'using regular fixtures'], + ['_orig', 'using original fixtures'], ].each do |(suffix, description)| context description do printf_yml = File.expand_path( - "../../../../../common/printf#{suffix}.yml", + "../../../../../fixtures/printf#{suffix}.yml", __dir__, ) diff --git a/configure.ac b/configure.ac index efb63a9d..80b44450 100644 --- a/configure.ac +++ b/configure.ac @@ -26,6 +26,8 @@ AC_CONFIG_SRCDIR([src/assert.c]) AC_CONFIG_FILES([ Makefile examples/Makefile + fixtures/Makefile + fixtures/multiboot2_bin_examples_gen.c include/Makefile libc/Makefile libc/include/Makefile @@ -49,6 +51,7 @@ AC_ARG_ENABLE([float], AS_HELP_STRING([--disable-float], [dis AC_ARG_ENABLE([werror], AS_HELP_STRING([--disable-werror], [disable -Werror])) dnl Features (disabled by default) +AC_ARG_ENABLE([fixtures], AS_HELP_STRING([--enable-fixtures], [enable fixtures for tests and bindings])) AC_ARG_ENABLE([freestanding], AS_HELP_STRING([--enable-freestanding], [build for freestanding environment])) AC_ARG_ENABLE([split-libc], AS_HELP_STRING([--enable-split-libc], [split off libc])) AC_ARG_ENABLE([checks], AS_HELP_STRING([--enable-checks], [enable usual tests and examples])) @@ -134,6 +137,7 @@ AS_IF([test "$enable_float" = no ], [enable_float=no], [ena AS_IF([test "$enable_werror" = no ], [enable_werror=no], [enable_werror=yes]) dnl Features (disabled by default) +AS_IF([test "$enable_fixtures" = yes], [enable_fixtures=yes], [enable_fixtures=no]) AS_IF([test "$enable_freestanding" = yes], [enable_freestanding=yes], [enable_freestanding=no]) AS_IF([test "$enable_split_libc" = yes], [enable_split_libc=yes], [enable_split_libc=no]) AS_IF([test "$enable_checks" = yes], [enable_checks=yes], [enable_checks=no]) @@ -170,8 +174,10 @@ AS_IF([test "$with_libc" = yes], [with_libc=yes], [wit # Test args # ############# -AS_IF([test "$enable_checks" = yes -a "$enable_freestanding" = yes], AC_MSG_ERROR([can not build freestanding tests])) -AS_IF([test "$enable_checks" = yes -a "$with_libc" = yes], AC_MSG_ERROR([can not use package `libc' with tests])) +AS_IF([test "$enable_checks" = yes -a "$enable_freestanding" = yes], AC_MSG_ERROR([can not build freestanding tests])) +AS_IF([test "$enable_fixtures" = yes -a "$enable_freestanding" = yes], AC_MSG_ERROR([can not build freestanding fixtures])) +AS_IF([test "$enable_checks" = yes -a "$with_libc" = yes], AC_MSG_ERROR([can not use package `libc' with tests])) +AS_IF([test "$enable_fixtures" = yes -a "$with_libc" = yes], AC_MSG_ERROR([can not use package `libc' with fixtures])) AS_IF([test "$with_printf" = yes -a "$with_ntoa" = no], AC_MSG_ERROR([package `printf' requires package `ntoa'])) AS_IF([test "$with_printf" = yes -a "$with_printf_fmt" = no], AC_MSG_ERROR([package `printf' requires package `printf-fmt'])) @@ -197,6 +203,7 @@ AM_CONDITIONAL([ENABLE_FLOAT], [test "$enable_float" = yes]) AM_CONDITIONAL([ENABLE_WERROR], [test "$enable_werror" = yes]) dnl Features (disabled by default) +AM_CONDITIONAL([ENABLE_FIXTURES], [test "$enable_fixtures" = yes]) AM_CONDITIONAL([ENABLE_FREESTANDING], [test "$enable_freestanding" = yes]) AM_CONDITIONAL([ENABLE_SPLIT_LIBC], [test "$enable_split_libc" = yes]) AM_CONDITIONAL([ENABLE_CHECKS], [test "$enable_checks" = yes]) @@ -249,6 +256,7 @@ AS_IF([test "$enable_werror" = yes], [AC_DEFINE([ENABLE_WERROR], dnl Features (disabled by default) AS_IF([test "$enable_split_libc" = yes], [AC_DEFINE([ENABLE_SPLIT_LIBC], [1], [split off libc])]) +AS_IF([test "$enable_fixtures" = yes], [AC_DEFINE([ENABLE_FIXTURES], [1], [enabled fixtures for tests and bindings])]) AS_IF([test "$enable_freestanding" = yes], [AC_DEFINE([ENABLE_FREESTANDING], [1], [build for freestanding environment])]) AS_IF([test "$enable_checks" = yes], [AC_DEFINE([ENABLE_CHECKS], [1], [enabled usual tests and examples])]) AS_IF([test "$enable_checks_cppcheck" = yes], [AC_DEFINE([ENABLE_CHECKS_CPPCHECK], [1], [enabled cppcheck])]) diff --git a/fixtures/Makefile.am b/fixtures/Makefile.am new file mode 100644 index 00000000..39b92e03 --- /dev/null +++ b/fixtures/Makefile.am @@ -0,0 +1,53 @@ +include $(top_srcdir)/make/shared.am + +noinst_PROGRAMS = +nodist_noinst_DATA = + +######################################### +# multiboot2_(header|info)_example*.bin # +######################################### + +if WITH_MULTIBOOT2 +nodist_noinst_DATA += \ + multiboot2_header_example0.bin \ + multiboot2_header_example1.bin \ + multiboot2_header_example2.bin \ + multiboot2_info_example0.bin \ + multiboot2_info_example1.bin \ + multiboot2_info_example2.bin +endif + +multiboot2_header_example0.bin: multiboot2_bin_examples_gen + ./multiboot2_bin_examples_gen header 0 + +multiboot2_header_example1.bin_examples_gen: multiboot2_bin + ./multiboot2_bin_examples_gen header 1 + +multiboot2_header_example2.bin: multiboot2_bin_examples_gen + ./multiboot2_bin_examples_gen header 2 + +multiboot2_info_example0.bin: multiboot2_bin_examples_gen + ./multiboot2_bin_examples_gen info 0 + +multiboot2_info_example1.bin: multiboot2_bin_examples_gen + ./multiboot2_bin_examples_gen info 1 + +multiboot2_info_example2.bin: multiboot2_bin_examples_gen + ./multiboot2_bin_examples_gen info 2 + +############################### +# multiboot2_bin_examples_gen # +############################### + +if WITH_MULTIBOOT2 +noinst_PROGRAMS += multiboot2_bin_examples_gen +multiboot2_bin_examples_gen_LDADD = $(top_builddir)/libkernaux.la +nodist_multiboot2_bin_examples_gen_SOURCES = multiboot2_bin_examples_gen.c +multiboot2_bin_examples_gen_SOURCES = \ + multiboot2_header_example0.h \ + multiboot2_header_example1.h \ + multiboot2_header_example2.h \ + multiboot2_info_example0.h \ + multiboot2_info_example1.h \ + multiboot2_info_example2.h +endif diff --git a/common/cmdline.yml b/fixtures/cmdline.yml similarity index 100% rename from common/cmdline.yml rename to fixtures/cmdline.yml diff --git a/fixtures/multiboot2_bin_examples_gen.c.in b/fixtures/multiboot2_bin_examples_gen.c.in new file mode 100644 index 00000000..668906a8 --- /dev/null +++ b/fixtures/multiboot2_bin_examples_gen.c.in @@ -0,0 +1,84 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#include +#include +#include +#include + +#include "multiboot2_header_example0.h" +#include "multiboot2_header_example1.h" +#include "multiboot2_header_example2.h" +#include "multiboot2_info_example0.h" +#include "multiboot2_info_example1.h" +#include "multiboot2_info_example2.h" + +static void assert_cb( + const char *const file, + const int line, + const char *const msg +) { + fprintf(stderr, "%s:%d:%s\n", file, line, msg); + abort(); +} + +#define EXAMPLE(type, number) do { \ + static const char *const filename = \ + "@abs_top_builddir@/fixtures/multiboot2_"#type"_example"#number".bin"; \ + FILE *const file = fopen(filename, "w"); \ + assert(file); \ + assert( \ + fwrite( \ + &multiboot2_##type##_example##number, \ + 1, \ + sizeof(multiboot2_##type##_example##number), \ + file \ + ) == sizeof(multiboot2_##type##_example##number) \ + ); \ + assert(fclose(file) == 0); \ +} while (0) + +int main(const int argc, const char *const *const argv) +{ + kernaux_assert_cb = assert_cb; + + assert(argc == 3); + + const char *const type = argv[1]; + const char *const number = argv[2]; + + if (strcmp(type, "header") == 0) { + if (strcmp(number, "0") == 0) { + EXAMPLE(header, 0); + } + else if (strcmp(number, "1") == 0) { + EXAMPLE(header, 1); + } + else if (strcmp(number, "2") == 0) { + EXAMPLE(header, 2); + } + else { + abort(); + } + } else if (strcmp(type, "info") == 0) { + if (strcmp(number, "0") == 0) { + EXAMPLE(info, 0); + } + else if (strcmp(number, "1") == 0) { + EXAMPLE(info, 1); + } + else if (strcmp(number, "2") == 0) { + EXAMPLE(info, 2); + } + else { + abort(); + } + } else { + abort(); + } + + exit(EXIT_SUCCESS); +} diff --git a/tests/multiboot2_header_example0.h b/fixtures/multiboot2_header_example0.h similarity index 100% rename from tests/multiboot2_header_example0.h rename to fixtures/multiboot2_header_example0.h diff --git a/tests/multiboot2_header_example1.h b/fixtures/multiboot2_header_example1.h similarity index 100% rename from tests/multiboot2_header_example1.h rename to fixtures/multiboot2_header_example1.h diff --git a/tests/multiboot2_header_example2.h b/fixtures/multiboot2_header_example2.h similarity index 100% rename from tests/multiboot2_header_example2.h rename to fixtures/multiboot2_header_example2.h diff --git a/tests/multiboot2_info_example0.h b/fixtures/multiboot2_info_example0.h similarity index 100% rename from tests/multiboot2_info_example0.h rename to fixtures/multiboot2_info_example0.h diff --git a/tests/multiboot2_info_example1.h b/fixtures/multiboot2_info_example1.h similarity index 100% rename from tests/multiboot2_info_example1.h rename to fixtures/multiboot2_info_example1.h diff --git a/tests/multiboot2_info_example2.h b/fixtures/multiboot2_info_example2.h similarity index 100% rename from tests/multiboot2_info_example2.h rename to fixtures/multiboot2_info_example2.h diff --git a/common/printf.yml b/fixtures/printf.yml similarity index 100% rename from common/printf.yml rename to fixtures/printf.yml diff --git a/common/printf_fmt.yml b/fixtures/printf_fmt.yml similarity index 100% rename from common/printf_fmt.yml rename to fixtures/printf_fmt.yml diff --git a/common/printf_orig.yml b/fixtures/printf_orig.yml similarity index 100% rename from common/printf_orig.yml rename to fixtures/printf_orig.yml diff --git a/tests/Makefile.am b/tests/Makefile.am index 7db522c0..6796b1b9 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -14,7 +14,7 @@ multiboot2_header_print0_LDADD = $(top_builddir)/libkernaux.la multiboot2_header_print0_SOURCES = \ main.c \ multiboot2_header_print0.c \ - multiboot2_header_example0.h + ../fixtures/multiboot2_header_example0.h endif ############################ @@ -27,7 +27,7 @@ multiboot2_header_print1_LDADD = $(top_builddir)/libkernaux.la multiboot2_header_print1_SOURCES = \ main.c \ multiboot2_header_print1.c \ - multiboot2_header_example1.h + ../fixtures/multiboot2_header_example1.h endif ############################ @@ -40,7 +40,7 @@ multiboot2_header_print2_LDADD = $(top_builddir)/libkernaux.la multiboot2_header_print2_SOURCES = \ main.c \ multiboot2_header_print2.c \ - multiboot2_header_example2.h + ../fixtures/multiboot2_header_example2.h endif ########################## @@ -53,7 +53,7 @@ multiboot2_info_print0_LDADD = $(top_builddir)/libkernaux.la multiboot2_info_print0_SOURCES = \ main.c \ multiboot2_info_print0.c \ - multiboot2_info_example0.h + ../fixtures/multiboot2_info_example0.h endif ########################## @@ -66,7 +66,7 @@ multiboot2_info_print1_LDADD = $(top_builddir)/libkernaux.la multiboot2_info_print1_SOURCES = \ main.c \ multiboot2_info_print1.c \ - multiboot2_info_example1.h + ../fixtures/multiboot2_info_example1.h endif ########################## @@ -79,7 +79,7 @@ multiboot2_info_print2_LDADD = $(top_builddir)/libkernaux.la multiboot2_info_print2_SOURCES = \ main.c \ multiboot2_info_print2.c \ - multiboot2_info_example2.h + ../fixtures/multiboot2_info_example2.h endif ################## @@ -121,7 +121,7 @@ test_cmdline_gen_SOURCES = \ test_cmdline_gen.c \ cmdline_gen.py \ cmdline_gen.jinja \ - $(top_srcdir)/common/cmdline.yml \ + $(top_srcdir)/fixtures/cmdline.yml \ cmdline_test.h \ cmdline_test.c endif @@ -129,8 +129,8 @@ endif CLEANFILES += test_cmdline_gen.c -test_cmdline_gen.c: $(top_srcdir)/tests/cmdline_gen.py $(top_srcdir)/tests/cmdline_gen.jinja $(top_srcdir)/common/cmdline.yml - $(PYTHON) $(top_srcdir)/tests/cmdline_gen.py $(top_srcdir)/tests/cmdline_gen.jinja $(top_srcdir)/common/cmdline.yml test_cmdline_gen.c +test_cmdline_gen.c: $(top_srcdir)/tests/cmdline_gen.py $(top_srcdir)/tests/cmdline_gen.jinja $(top_srcdir)/fixtures/cmdline.yml + $(PYTHON) $(top_srcdir)/tests/cmdline_gen.py $(top_srcdir)/tests/cmdline_gen.jinja $(top_srcdir)/fixtures/cmdline.yml test_cmdline_gen.c ############ # test_elf # @@ -190,8 +190,8 @@ test_multiboot2_common_packing_LDADD = $(top_builddir)/libkernaux.la test_multiboot2_common_packing_SOURCES = \ main.c \ test_multiboot2_common_packing.c \ - multiboot2_header_example2.h \ - multiboot2_info_example2.h + ../fixtures/multiboot2_header_example2.h \ + ../fixtures/multiboot2_info_example2.h endif ################################## @@ -204,8 +204,8 @@ test_multiboot2_header_helpers_LDADD = $(top_builddir)/libkernaux.la test_multiboot2_header_helpers_SOURCES = \ main.c \ test_multiboot2_header_helpers.c \ - multiboot2_header_example1.h \ - multiboot2_header_example2.h + ../fixtures/multiboot2_header_example1.h \ + ../fixtures/multiboot2_header_example2.h endif ################################ @@ -234,8 +234,8 @@ test_multiboot2_header_validation_LDADD = $(top_builddir)/libkernaux.la test_multiboot2_header_validation_SOURCES = \ main.c \ test_multiboot2_header_validation.c \ - multiboot2_header_example1.h \ - multiboot2_header_example2.h + ../fixtures/multiboot2_header_example1.h \ + ../fixtures/multiboot2_header_example2.h endif ################################ @@ -248,8 +248,8 @@ test_multiboot2_info_helpers_LDADD = $(top_builddir)/libkernaux.la test_multiboot2_info_helpers_SOURCES = \ main.c \ test_multiboot2_info_helpers.c \ - multiboot2_info_example1.h \ - multiboot2_info_example2.h + ../fixtures/multiboot2_info_example1.h \ + ../fixtures/multiboot2_info_example2.h endif ############################## @@ -278,8 +278,8 @@ test_multiboot2_info_validation_LDADD = $(top_builddir)/libkernaux.la test_multiboot2_info_validation_SOURCES = \ main.c \ test_multiboot2_info_validation.c \ - multiboot2_info_example1.h \ - multiboot2_info_example2.h + ../fixtures/multiboot2_info_example1.h \ + ../fixtures/multiboot2_info_example2.h endif ############# @@ -359,14 +359,14 @@ test_printf_fmt_gen_SOURCES = \ test_printf_fmt_gen.c \ printf_fmt_gen.py \ printf_fmt_gen.jinja \ - $(top_srcdir)/common/printf_fmt.yml + $(top_srcdir)/fixtures/printf_fmt.yml endif endif CLEANFILES += test_printf_fmt_gen.c -test_printf_fmt_gen.c: $(top_srcdir)/tests/printf_fmt_gen.py $(top_srcdir)/tests/printf_fmt_gen.jinja $(top_srcdir)/common/printf_fmt.yml - $(PYTHON) $(top_srcdir)/tests/printf_fmt_gen.py $(top_srcdir)/tests/printf_fmt_gen.jinja $(top_srcdir)/common/printf_fmt.yml test_printf_fmt_gen.c +test_printf_fmt_gen.c: $(top_srcdir)/tests/printf_fmt_gen.py $(top_srcdir)/tests/printf_fmt_gen.jinja $(top_srcdir)/fixtures/printf_fmt.yml + $(PYTHON) $(top_srcdir)/tests/printf_fmt_gen.py $(top_srcdir)/tests/printf_fmt_gen.jinja $(top_srcdir)/fixtures/printf_fmt.yml test_printf_fmt_gen.c ################### # test_printf_gen # @@ -381,15 +381,15 @@ test_printf_gen_SOURCES = \ test_printf_gen.c \ printf_gen.py \ printf_gen.jinja \ - $(top_srcdir)/common/printf.yml \ - $(top_srcdir)/common/printf_orig.yml + $(top_srcdir)/fixtures/printf.yml \ + $(top_srcdir)/fixtures/printf_orig.yml endif endif CLEANFILES += test_printf_gen.c -test_printf_gen.c: $(top_srcdir)/tests/printf_gen.py $(top_srcdir)/tests/printf_gen.jinja $(top_srcdir)/common/printf.yml $(top_srcdir)/common/printf_orig.yml - $(PYTHON) $(top_srcdir)/tests/printf_gen.py $(top_srcdir)/tests/printf_gen.jinja $(top_srcdir)/common/printf.yml $(top_srcdir)/common/printf_orig.yml test_printf_gen.c +test_printf_gen.c: $(top_srcdir)/tests/printf_gen.py $(top_srcdir)/tests/printf_gen.jinja $(top_srcdir)/fixtures/printf.yml $(top_srcdir)/fixtures/printf_orig.yml + $(PYTHON) $(top_srcdir)/tests/printf_gen.py $(top_srcdir)/tests/printf_gen.jinja $(top_srcdir)/fixtures/printf.yml $(top_srcdir)/fixtures/printf_orig.yml test_printf_gen.c #################### # test_units_human # diff --git a/tests/multiboot2_header_print0.c b/tests/multiboot2_header_print0.c index 89836d3f..407e2218 100644 --- a/tests/multiboot2_header_print0.c +++ b/tests/multiboot2_header_print0.c @@ -12,7 +12,7 @@ #include #include -#include "multiboot2_header_example0.h" +#include "../fixtures/multiboot2_header_example0.h" static void my_putc(void *display KERNAUX_UNUSED, char c) { diff --git a/tests/multiboot2_header_print1.c b/tests/multiboot2_header_print1.c index 0eab3d22..fe16b403 100644 --- a/tests/multiboot2_header_print1.c +++ b/tests/multiboot2_header_print1.c @@ -12,7 +12,7 @@ #include #include -#include "multiboot2_header_example1.h" +#include "../fixtures/multiboot2_header_example1.h" static void my_putc(void *display KERNAUX_UNUSED, char c) { diff --git a/tests/multiboot2_header_print2.c b/tests/multiboot2_header_print2.c index 50b41bba..1d003864 100644 --- a/tests/multiboot2_header_print2.c +++ b/tests/multiboot2_header_print2.c @@ -12,7 +12,7 @@ #include #include -#include "multiboot2_header_example2.h" +#include "../fixtures/multiboot2_header_example2.h" static void my_putc(void *display KERNAUX_UNUSED, char c) { diff --git a/tests/multiboot2_info_print0.c b/tests/multiboot2_info_print0.c index 642a7aa1..ddcf46dd 100644 --- a/tests/multiboot2_info_print0.c +++ b/tests/multiboot2_info_print0.c @@ -12,7 +12,7 @@ #include #include -#include "multiboot2_info_example0.h" +#include "../fixtures/multiboot2_info_example0.h" static void my_putc(void *display KERNAUX_UNUSED, char c) { diff --git a/tests/multiboot2_info_print1.c b/tests/multiboot2_info_print1.c index 095821a0..8770af93 100644 --- a/tests/multiboot2_info_print1.c +++ b/tests/multiboot2_info_print1.c @@ -12,7 +12,7 @@ #include #include -#include "multiboot2_info_example1.h" +#include "../fixtures/multiboot2_info_example1.h" static void my_putc(void *display KERNAUX_UNUSED, char c) { diff --git a/tests/multiboot2_info_print2.c b/tests/multiboot2_info_print2.c index 30ed471d..8e0f1bf2 100644 --- a/tests/multiboot2_info_print2.c +++ b/tests/multiboot2_info_print2.c @@ -12,7 +12,7 @@ #include #include -#include "multiboot2_info_example2.h" +#include "../fixtures/multiboot2_info_example2.h" static void my_putc(void *display KERNAUX_UNUSED, char c) { diff --git a/tests/test_multiboot2_common_packing.c b/tests/test_multiboot2_common_packing.c index b54653f4..f9215fba 100644 --- a/tests/test_multiboot2_common_packing.c +++ b/tests/test_multiboot2_common_packing.c @@ -7,8 +7,8 @@ #include #include -#include "multiboot2_header_example2.h" -#include "multiboot2_info_example2.h" +#include "../fixtures/multiboot2_header_example2.h" +#include "../fixtures/multiboot2_info_example2.h" #define HEAD_SIZEOF1(type, inst, size) \ do { \ diff --git a/tests/test_multiboot2_header_helpers.c b/tests/test_multiboot2_header_helpers.c index 5af933d1..10d2aa1d 100644 --- a/tests/test_multiboot2_header_helpers.c +++ b/tests/test_multiboot2_header_helpers.c @@ -4,8 +4,8 @@ #include -#include "multiboot2_header_example1.h" -#include "multiboot2_header_example2.h" +#include "../fixtures/multiboot2_header_example1.h" +#include "../fixtures/multiboot2_header_example2.h" void test_main() { diff --git a/tests/test_multiboot2_info_helpers.c b/tests/test_multiboot2_info_helpers.c index 7a6c34eb..bd9592ea 100644 --- a/tests/test_multiboot2_info_helpers.c +++ b/tests/test_multiboot2_info_helpers.c @@ -7,8 +7,8 @@ #include -#include "multiboot2_info_example1.h" -#include "multiboot2_info_example2.h" +#include "../fixtures/multiboot2_info_example1.h" +#include "../fixtures/multiboot2_info_example2.h" #include diff --git a/tests/test_multiboot2_info_validation.c b/tests/test_multiboot2_info_validation.c index 45017566..0aebdf43 100644 --- a/tests/test_multiboot2_info_validation.c +++ b/tests/test_multiboot2_info_validation.c @@ -7,8 +7,8 @@ #include -#include "multiboot2_info_example1.h" -#include "multiboot2_info_example2.h" +#include "../fixtures/multiboot2_info_example1.h" +#include "../fixtures/multiboot2_info_example2.h" #include From f46438fa85197802a5dc8f1fadb43480485d648b Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 17 Dec 2022 16:46:03 +0400 Subject: [PATCH 2/6] Fix typo & test fixtures on CI (#151) --- .cirrus.yml | 2 +- .github/workflows/main.yml | 2 +- .openbsd.yml | 2 +- fixtures/Makefile.am | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.cirrus.yml b/.cirrus.yml index b05e32b5..41d3ee46 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -12,7 +12,7 @@ main_freebsd_task: - pip install --user Jinja2 PyYAML main_build_script: - ./autogen.sh - - ./configure --enable-debug --enable-checks-all CFLAGS='-O3' + - ./configure --enable-debug --enable-fixtures --enable-checks-all CFLAGS='-O3' - make - sudo make install main_test_script: diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 29622346..9e4175b2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -34,7 +34,7 @@ jobs: - name: autogen run: ./autogen.sh - name: configure - run: ./configure ${{matrix.debug}} --enable-checks --enable-checks-pthreads --enable-checks-python CC='${{matrix.cc}}' CFLAGS='${{matrix.opt}}' + run: ./configure ${{matrix.debug}} --enable-fixtures --enable-checks --enable-checks-pthreads --enable-checks-python CC='${{matrix.cc}}' CFLAGS='${{matrix.opt}}' - name: make run: make - name: check diff --git a/.openbsd.yml b/.openbsd.yml index cfd6ca31..e5838687 100644 --- a/.openbsd.yml +++ b/.openbsd.yml @@ -31,7 +31,7 @@ tasks: - build: | cd libkernaux ./autogen.sh - ./configure --enable-debug --enable-checks-all CFLAGS='-O3' + ./configure --enable-debug --enable-fixtures --enable-checks-all CFLAGS='-O3' make doas make install - test: | diff --git a/fixtures/Makefile.am b/fixtures/Makefile.am index 39b92e03..b04300f8 100644 --- a/fixtures/Makefile.am +++ b/fixtures/Makefile.am @@ -20,7 +20,7 @@ endif multiboot2_header_example0.bin: multiboot2_bin_examples_gen ./multiboot2_bin_examples_gen header 0 -multiboot2_header_example1.bin_examples_gen: multiboot2_bin +multiboot2_header_example1.bin: multiboot2_bin_examples_gen ./multiboot2_bin_examples_gen header 1 multiboot2_header_example2.bin: multiboot2_bin_examples_gen From a1262ac64de2e7cbd34b0eaee57e3d7143f9491d Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sun, 18 Dec 2022 09:18:55 +0400 Subject: [PATCH 3/6] Fix printf hack in the bindings (#152) --- .cirrus.yml | 2 +- .github/workflows/mruby.yml | 3 --- .github/workflows/ruby.yml | 3 --- README.md | 6 ++++++ bindings/mruby/Rakefile | 14 +++++++++++++- bindings/mruby/src/assert.c | 3 --- bindings/mruby/src/cmdline.c | 4 ---- bindings/mruby/src/main.h | 6 ++++++ bindings/mruby/src/ntoa.c | 2 -- bindings/mruby/src/printf.c | 21 +++++++++------------ bindings/ruby/Rakefile | 16 ++++++++++++++-- bindings/ruby/ext/default/main.h | 6 ++++++ bindings/ruby/ext/default/printf.c | 21 +++++++++------------ 13 files changed, 64 insertions(+), 43 deletions(-) diff --git a/.cirrus.yml b/.cirrus.yml index 41d3ee46..ba5f2662 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -46,7 +46,7 @@ ruby_freebsd_task: CPATH: '/usr/local/include' LIBRARY_PATH: '/usr/local/lib' dependencies_script: - - pkg install --yes autoconf automake git libtool wget + - pkg install --yes autoconf automake cppcheck git libtool wget dependencies_ruby_script: - wget https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.3.tar.gz - tar -xzf ruby-3.0.3.tar.gz diff --git a/.github/workflows/mruby.yml b/.github/workflows/mruby.yml index 4e083655..afd12f76 100644 --- a/.github/workflows/mruby.yml +++ b/.github/workflows/mruby.yml @@ -46,6 +46,3 @@ jobs: - working-directory: bindings/mruby name: lint run: rake - - working-directory: bindings/mruby - name: cppcheck - run: cppcheck --quiet --error-exitcode=1 --std=c99 --enable=warning,style,performance,portability . diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 2ea62d7d..d5a2ca6d 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -44,6 +44,3 @@ jobs: - working-directory: bindings/ruby name: test & lint run: SKIP_COVERAGE='${{matrix.packages.skip_coverage}}' rake - - working-directory: bindings/ruby - name: cppcheck - run: cppcheck --quiet --error-exitcode=1 --std=c99 --enable=warning,style,performance,portability . diff --git a/README.md b/README.md index 96754bbb..833b7618 100644 --- a/README.md +++ b/README.md @@ -202,6 +202,12 @@ make You can test with `make check`. +#### See also + +* [GitHub Actions](/.github/) for **GNU/Linux** build environment +* [Cirrus CI](/.cirrus.yml) for **FreeBSD** build environment +* [sourcehut CI](/.openbsd.yml) for **OpenBSD** build environment + ### Cross Create configuration script with `./autogen.sh` (if present). diff --git a/bindings/mruby/Rakefile b/bindings/mruby/Rakefile index 567628a1..d71d39f6 100644 --- a/bindings/mruby/Rakefile +++ b/bindings/mruby/Rakefile @@ -4,7 +4,7 @@ desc 'Run default checks' task default: :lint desc 'Run code analysis tools' -task lint: :rubocop +task lint: %i[rubocop cppcheck] desc 'Fix code style (rubocop --auto-correct)' task fix: 'rubocop:auto_correct' @@ -15,3 +15,15 @@ begin rescue LoadError nil end + +desc 'Run cppcheck' +task :cppcheck do + sh( + 'cppcheck', + '--quiet', + '--error-exitcode=1', + '--std=c99', + '--enable=warning,style,performance,portability', + __dir__, + ) +end diff --git a/bindings/mruby/src/assert.c b/bindings/mruby/src/assert.c index b9f5d669..0942a994 100644 --- a/bindings/mruby/src/assert.c +++ b/bindings/mruby/src/assert.c @@ -1,8 +1,5 @@ #include "main.h" -#include -#include - #include #include #include diff --git a/bindings/mruby/src/cmdline.c b/bindings/mruby/src/cmdline.c index 78266758..9dd1aa7f 100644 --- a/bindings/mruby/src/cmdline.c +++ b/bindings/mruby/src/cmdline.c @@ -1,9 +1,5 @@ #include "main.h" -#include -#include -#include - #include #include #include diff --git a/bindings/mruby/src/main.h b/bindings/mruby/src/main.h index 52700414..a38d8053 100644 --- a/bindings/mruby/src/main.h +++ b/bindings/mruby/src/main.h @@ -4,6 +4,12 @@ #include #include +#include +#include +#include +#include +#include + void current_mrb_start(mrb_state *mrb); void current_mrb_finish(mrb_state *mrb); mrb_state *current_mrb_get(); diff --git a/bindings/mruby/src/ntoa.c b/bindings/mruby/src/ntoa.c index d1dd3846..f8f9c03d 100644 --- a/bindings/mruby/src/ntoa.c +++ b/bindings/mruby/src/ntoa.c @@ -1,7 +1,5 @@ #include "main.h" -#include - #include #include #include diff --git a/bindings/mruby/src/printf.c b/bindings/mruby/src/printf.c index 02b05f06..71bc1ef8 100644 --- a/bindings/mruby/src/printf.c +++ b/bindings/mruby/src/printf.c @@ -29,8 +29,7 @@ void init_printf(mrb_state *const mrb) mrb_value rb_KernAux_sprintf(mrb_state *const mrb, mrb_value self) { - // FIXME: const - char *format; + const char *format; mrb_value *args; mrb_int argc; mrb_get_args(mrb, "z*", &format, &args, &argc); @@ -45,12 +44,9 @@ mrb_value rb_KernAux_sprintf(mrb_state *const mrb, mrb_value self) continue; } - // FIXME: unnecessary - const char *const old_format = format; ++format; struct KernAux_PrintfFmt_Spec spec = - // FIXME: no type cast - KernAux_PrintfFmt_Spec_create_out((const char**)&format); + KernAux_PrintfFmt_Spec_create_out(&format); if (spec.set_width) { TAKE_ARG; @@ -87,14 +83,16 @@ mrb_value rb_KernAux_sprintf(mrb_state *const mrb, mrb_value self) DynArg_use_str(&dynarg, RSTRING_CSTR(mrb, arg_rb)); } + // 1 additional byte for the '%' character. + // 1 additional byte for the terminating '\0' character. + char old_format[2 + spec.format_limit - spec.format_start]; + memset(old_format, '\0', sizeof(old_format)); + old_format[0] = '%'; + strncpy(&old_format[1], spec.format_start, sizeof(old_format) - 2); + char buffer[BUFFER_SIZE]; int slen; - // FIXME: it's a hack - // TODO: convert printf format spec to string - const char tmp = *format; - *format = '\0'; - if (spec.set_width) { if (spec.set_precision) { if (dynarg.use_dbl) { @@ -133,7 +131,6 @@ mrb_value rb_KernAux_sprintf(mrb_state *const mrb, mrb_value self) } } - *format = tmp; mrb_str_cat(mrb, result, buffer, slen); } diff --git a/bindings/ruby/Rakefile b/bindings/ruby/Rakefile index 2675fc72..5bf5204a 100644 --- a/bindings/ruby/Rakefile +++ b/bindings/ruby/Rakefile @@ -20,13 +20,13 @@ CLEAN << 'doc' CLEAN << 'spec/examples.txt' desc 'Run default checks' -task default: %i[test lint yard:cov] +task default: %i[test lint] desc 'Run tests' task test: :spec desc 'Run code analysis tools' -task lint: :rubocop +task lint: %i[rubocop cppcheck yard:cov] desc 'Fix code style (rubocop --auto-correct)' task fix: 'rubocop:auto_correct' @@ -66,6 +66,18 @@ task :console do sh 'bundle', 'exec', File.expand_path(File.join('bin', 'console'), __dir__) end +desc 'Run cppcheck' +task :cppcheck do + sh( + 'cppcheck', + '--quiet', + '--error-exitcode=1', + '--std=c99', + '--enable=warning,style,performance,portability', + __dir__, + ) +end + namespace :yard do desc 'Measure documentation coverage' task :cov do diff --git a/bindings/ruby/ext/default/main.h b/bindings/ruby/ext/default/main.h index 74d21872..ddbb2ff6 100644 --- a/bindings/ruby/ext/default/main.h +++ b/bindings/ruby/ext/default/main.h @@ -4,6 +4,12 @@ #include #include +#include +#include +#include +#include +#include + extern ID rb_intern_call; extern ID rb_intern_freeze; extern ID rb_intern_LESS; diff --git a/bindings/ruby/ext/default/printf.c b/bindings/ruby/ext/default/printf.c index b2785b6c..e73ddf06 100644 --- a/bindings/ruby/ext/default/printf.c +++ b/bindings/ruby/ext/default/printf.c @@ -31,8 +31,7 @@ VALUE rb_KernAux_sprintf(const int argc, VALUE *const argv, VALUE self) { if (argc == 0) rb_raise(rb_eArgError, "too few arguments"); - // FIXME: const - char *format = StringValueCStr(argv[0]); + const char *format = StringValueCStr(argv[0]); int arg_index = 1; VALUE result = rb_str_new_literal(""); @@ -43,12 +42,9 @@ VALUE rb_KernAux_sprintf(const int argc, VALUE *const argv, VALUE self) continue; } - // FIXME: unnecessary - const char *const old_format = format; ++format; struct KernAux_PrintfFmt_Spec spec = - // FIXME: no type cast - KernAux_PrintfFmt_Spec_create_out((const char**)&format); + KernAux_PrintfFmt_Spec_create_out(&format); if (spec.set_width) { TAKE_ARG; @@ -82,14 +78,16 @@ VALUE rb_KernAux_sprintf(const int argc, VALUE *const argv, VALUE self) DynArg_use_str(&dynarg, StringValueCStr(arg_rb)); } + // 1 additional byte for the '%' character. + // 1 additional byte for the terminating '\0' character. + char old_format[2 + spec.format_limit - spec.format_start]; + memset(old_format, '\0', sizeof(old_format)); + old_format[0] = '%'; + strncpy(&old_format[1], spec.format_start, sizeof(old_format) - 2); + char buffer[BUFFER_SIZE]; int slen; - // FIXME: it's a hack - // TODO: convert printf format spec to string - const char tmp = *format; - *format = '\0'; - if (spec.set_width) { if (spec.set_precision) { if (dynarg.use_dbl) { @@ -128,7 +126,6 @@ VALUE rb_KernAux_sprintf(const int argc, VALUE *const argv, VALUE self) } } - *format = tmp; rb_str_cat(result, buffer, slen); } From 6ec080ddfa54b7050f775361359807bd6c2ee200 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 19 Dec 2022 12:40:13 +0400 Subject: [PATCH 4/6] Improve build system (#153) --- .gitignore | 73 +--- build/.keep | 0 configure.ac | 2 + examples/.gitignore | 21 ++ fixtures/.gitignore | 8 + fixtures/Makefile.am | 8 + fixtures/multiboot2_header_example0.txt | 11 + fixtures/multiboot2_header_example1.txt | 52 +++ fixtures/multiboot2_header_example2.txt | 100 ++++++ fixtures/multiboot2_info_example0.txt | 8 + fixtures/multiboot2_info_example1.txt | 129 +++++++ fixtures/multiboot2_info_example2.txt | 234 +++++++++++++ include/.gitignore | 1 + tests/.gitignore | 33 ++ tests/Makefile.am | 10 +- tests/test_multiboot2_header_print.c | 218 ------------ tests/test_multiboot2_header_print.c.in | 61 ++++ tests/test_multiboot2_info_print.c | 441 ------------------------ tests/test_multiboot2_info_print.c.in | 61 ++++ 19 files changed, 740 insertions(+), 731 deletions(-) create mode 100644 build/.keep create mode 100644 examples/.gitignore create mode 100644 fixtures/.gitignore create mode 100644 fixtures/multiboot2_header_example0.txt create mode 100644 fixtures/multiboot2_header_example1.txt create mode 100644 fixtures/multiboot2_header_example2.txt create mode 100644 fixtures/multiboot2_info_example0.txt create mode 100644 fixtures/multiboot2_info_example1.txt create mode 100644 fixtures/multiboot2_info_example2.txt create mode 100644 include/.gitignore create mode 100644 tests/.gitignore delete mode 100644 tests/test_multiboot2_header_print.c create mode 100644 tests/test_multiboot2_header_print.c.in delete mode 100644 tests/test_multiboot2_info_print.c create mode 100644 tests/test_multiboot2_info_print.c.in diff --git a/.gitignore b/.gitignore index d5e2e458..3eca0975 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +/build/* +!/build/.keep + ########################## # Common generated files # ########################## @@ -38,8 +41,6 @@ /m4/* !/m4/.keep -# Custom - /Makefile.in /examples/Makefile.in /fixtures/Makefile.in @@ -58,7 +59,6 @@ /libtool /stamp-h1 -# Temporary /confcache /confdefs.h /confinc.mk @@ -74,8 +74,6 @@ /tests/test_*.log /tests/test_*.trs -# Custom - /Makefile /examples/Makefile /fixtures/Makefile @@ -83,68 +81,3 @@ /libc/Makefile /libc/include/Makefile /tests/Makefile - -/include/kernaux/version.h - -/fixtures/multiboot2_bin_examples_gen -/fixtures/multiboot2_bin_examples_gen.c -/fixtures/multiboot2_header_example0.bin -/fixtures/multiboot2_header_example1.bin -/fixtures/multiboot2_header_example2.bin -/fixtures/multiboot2_info_example0.bin -/fixtures/multiboot2_info_example1.bin -/fixtures/multiboot2_info_example2.bin - -/examples/assert -/examples/cmdline -/examples/generic_display -/examples/generic_malloc -/examples/generic_mutex -/examples/macro_bits -/examples/macro_cast -/examples/macro_container_of -/examples/macro_packing -/examples/macro_static_test -/examples/memmap -/examples/multiboot2_header_macro -/examples/ntoa -/examples/panic -/examples/pfa -/examples/printf_file -/examples/printf_file_va -/examples/printf_fmt -/examples/printf_str -/examples/printf_str_va -/examples/units_human - -/tests/multiboot2_header_print0 -/tests/multiboot2_header_print1 -/tests/multiboot2_header_print2 -/tests/multiboot2_info_print0 -/tests/multiboot2_info_print1 -/tests/multiboot2_info_print2 -/tests/test_arch_i386 -/tests/test_cmdline -/tests/test_cmdline_gen -/tests/test_cmdline_gen.c -/tests/test_elf -/tests/test_free_list -/tests/test_mbr -/tests/test_memmap -/tests/test_multiboot2_common_packing -/tests/test_multiboot2_header_helpers -/tests/test_multiboot2_header_print -/tests/test_multiboot2_header_validation -/tests/test_multiboot2_info_helpers -/tests/test_multiboot2_info_print -/tests/test_multiboot2_info_validation -/tests/test_ntoa -/tests/test_ntoa_assert -/tests/test_pfa -/tests/test_pfa_assert -/tests/test_printf -/tests/test_printf_fmt_gen -/tests/test_printf_fmt_gen.c -/tests/test_printf_gen -/tests/test_printf_gen.c -/tests/test_units_human diff --git a/build/.keep b/build/.keep new file mode 100644 index 00000000..e69de29b diff --git a/configure.ac b/configure.ac index 80b44450..6b632b05 100644 --- a/configure.ac +++ b/configure.ac @@ -33,6 +33,8 @@ AC_CONFIG_FILES([ libc/include/Makefile include/kernaux/version.h tests/Makefile + tests/test_multiboot2_header_print.c + tests/test_multiboot2_info_print.c ]) AM_INIT_AUTOMAKE([1.16 subdir-objects]) diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 00000000..61817aee --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1,21 @@ +/assert +/cmdline +/generic_display +/generic_malloc +/generic_mutex +/macro_bits +/macro_cast +/macro_container_of +/macro_packing +/macro_static_test +/memmap +/multiboot2_header_macro +/ntoa +/panic +/pfa +/printf_file +/printf_file_va +/printf_fmt +/printf_str +/printf_str_va +/units_human diff --git a/fixtures/.gitignore b/fixtures/.gitignore new file mode 100644 index 00000000..248bdd92 --- /dev/null +++ b/fixtures/.gitignore @@ -0,0 +1,8 @@ +/multiboot2_bin_examples_gen +/multiboot2_bin_examples_gen.c +/multiboot2_header_example0.bin +/multiboot2_header_example1.bin +/multiboot2_header_example2.bin +/multiboot2_info_example0.bin +/multiboot2_info_example1.bin +/multiboot2_info_example2.bin diff --git a/fixtures/Makefile.am b/fixtures/Makefile.am index b04300f8..060b2b28 100644 --- a/fixtures/Makefile.am +++ b/fixtures/Makefile.am @@ -3,6 +3,14 @@ include $(top_srcdir)/make/shared.am noinst_PROGRAMS = nodist_noinst_DATA = +EXTRA_DIST = \ + multiboot2_header_example0.txt \ + multiboot2_header_example1.txt \ + multiboot2_header_example2.txt \ + multiboot2_info_example0.txt \ + multiboot2_info_example1.txt \ + multiboot2_info_example2.txt + ######################################### # multiboot2_(header|info)_example*.bin # ######################################### diff --git a/fixtures/multiboot2_header_example0.txt b/fixtures/multiboot2_header_example0.txt new file mode 100644 index 00000000..4a9ceda1 --- /dev/null +++ b/fixtures/multiboot2_header_example0.txt @@ -0,0 +1,11 @@ +Multiboot 2 header { + u32 magic: 0xe85250d6 + u32 arch: 0 (i386) + u32 size: 24 + u32 checksum: 0x17adaf12 +} +Multiboot 2 header tag { + u16 type: 0 (none) + u16 flags: 0x0 () + u32 size: 8 +} diff --git a/fixtures/multiboot2_header_example1.txt b/fixtures/multiboot2_header_example1.txt new file mode 100644 index 00000000..eb8385e3 --- /dev/null +++ b/fixtures/multiboot2_header_example1.txt @@ -0,0 +1,52 @@ +Multiboot 2 header { + u32 magic: 0xe85250d6 + u32 arch: 4 (MIPS32) + u32 size: 104 + u32 checksum: 0x17adaebe +} +Multiboot 2 header tag { + u16 type: 4 (flags) + u16 flags: 0x0 () + u32 size: 12 + u32 console_flags: 0x0 () +} +Multiboot 2 header tag { + u16 type: 4 (flags) + u16 flags: 0x0 () + u32 size: 12 + u32 console_flags: 0x1 ( + REQUIRE_CONSOLE + ) +} +Multiboot 2 header tag { + u16 type: 4 (flags) + u16 flags: 0x0 () + u32 size: 12 + u32 console_flags: 0x2 ( + EGA_SUPPORT + ) +} +Multiboot 2 header tag { + u16 type: 4 (flags) + u16 flags: 0x0 () + u32 size: 12 + u32 console_flags: 0x3 ( + REQUIRE_CONSOLE | + EGA_SUPPORT + ) +} +Multiboot 2 header tag { + u16 type: 1 (information request) + u16 flags: 0x1 ( + OPTIONAL + ) + u32 size: 12 + u32 mbi_tag_types[]: [ + 0 (none) + ] +} +Multiboot 2 header tag { + u16 type: 0 (none) + u16 flags: 0x0 () + u32 size: 8 +} diff --git a/fixtures/multiboot2_header_example2.txt b/fixtures/multiboot2_header_example2.txt new file mode 100644 index 00000000..7d66af38 --- /dev/null +++ b/fixtures/multiboot2_header_example2.txt @@ -0,0 +1,100 @@ +Multiboot 2 header { + u32 magic: 0xe85250d6 + u32 arch: 0 (i386) + u32 size: 272 + u32 checksum: 0x17adae1a +} +Multiboot 2 header tag { + u16 type: 1 (information request) + u16 flags: 0x0 () + u32 size: 96 + u32 mbi_tag_types[]: [ + 0 (none) + 1 (boot cmd line) + 2 (boot loader name) + 3 (module) + 4 (basic memory info) + 5 (BIOS boot device) + 6 (memory map) + 7 (VBE info) + 8 (framebuffer info) + 9 (ELF symbols) + 10 (APM table) + 11 (EFI 32bit system table ptr) + 12 (EFI 64bit system table ptr) + 13 (SMBIOS tables) + 14 (ACPI old RSDP) + 15 (ACPI new RSDP) + 16 (networking info) + 17 (EFI memory map) + 18 (EFI boot services not terminated) + 19 (EFI 32bit image handle ptr) + 20 (EFI 64bit image handle ptr) + 21 (image load base phys addr) + ] +} +Multiboot 2 header tag { + u16 type: 2 (address) + u16 flags: 0x0 () + u32 size: 24 + u32 header_addr: 0xcafebabe + u32 load_addr: 0xdeadbeaf + u32 load_end_addr: 0xdeadbabe + u32 bss_end_addr: 0xcafebeaf +} +Multiboot 2 header tag { + u16 type: 3 (entry address) + u16 flags: 0x0 () + u32 size: 12 + u32 entry_addr: 0xcafebabe +} +Multiboot 2 header tag { + u16 type: 4 (flags) + u16 flags: 0x0 () + u32 size: 12 + u32 console_flags: 0x0 () +} +Multiboot 2 header tag { + u16 type: 5 (framebuffer) + u16 flags: 0x0 () + u32 size: 20 + u32 width: 80 + u32 height: 25 + u32 depth: 8 +} +Multiboot 2 header tag { + u16 type: 6 (module alignment) + u16 flags: 0x0 () + u32 size: 8 +} +Multiboot 2 header tag { + u16 type: 7 (EFI boot services) + u16 flags: 0x0 () + u32 size: 8 +} +Multiboot 2 header tag { + u16 type: 8 (EFI i386 entry address) + u16 flags: 0x0 () + u32 size: 12 + u32 entry_addr: 0xcafebabe +} +Multiboot 2 header tag { + u16 type: 9 (EFI amd64 entry address) + u16 flags: 0x0 () + u32 size: 12 + u32 entry_addr: 0xdeadbeaf +} +Multiboot 2 header tag { + u16 type: 10 (relocatable header) + u16 flags: 0x0 () + u32 size: 24 + u32 min_addr: 0xcafebabe + u32 max_addr: 0xdeadbeaf + u32 align: 8 + u32 preference: 1 (lowest) +} +Multiboot 2 header tag { + u16 type: 0 (none) + u16 flags: 0x0 () + u32 size: 8 +} diff --git a/fixtures/multiboot2_info_example0.txt b/fixtures/multiboot2_info_example0.txt new file mode 100644 index 00000000..9fbe2896 --- /dev/null +++ b/fixtures/multiboot2_info_example0.txt @@ -0,0 +1,8 @@ +Multiboot 2 info { + u32 size: 16 + u32 reserved: 0x0 +} +Multiboot 2 info tag { + u32 type: 0 (none) + u32 size: 8 +} diff --git a/fixtures/multiboot2_info_example1.txt b/fixtures/multiboot2_info_example1.txt new file mode 100644 index 00000000..93efa0e0 --- /dev/null +++ b/fixtures/multiboot2_info_example1.txt @@ -0,0 +1,129 @@ +Multiboot 2 info { + u32 size: 864 + u32 reserved: 0x0 +} +Multiboot 2 info tag { + u32 type: 21 (image load base phys addr) + u32 size: 12 + u32 load_base_addr: 0x400000 +} +Multiboot 2 info tag { + u32 type: 1 (boot cmd line) + u32 size: 21 + char cmdline[]: "hello kernel" +} +Multiboot 2 info tag { + u32 type: 2 (boot loader name) + u32 size: 30 + char name[]: "GRUB 2.02-2ubuntu8.20" +} +Multiboot 2 info tag { + u32 type: 10 (APM table) + u32 size: 28 + u16 version: 258 + u16 cseg: 61440 + u32 offset: 54479 + u16 cseg_16: 61440 + u16 dseg: 61440 + u16 flags: 3 + u16 cseg_len: 65520 + u16 cseg_16_len: 65520 + u16 dseg_len: 65520 +} +Multiboot 2 info tag { + u32 type: 3 (module) + u32 size: 29 + u32 mod_start: 0x102000 + u32 mod_end: 0x10329c + char cmdline[]: "hello module" +} +Multiboot 2 info tag { + u32 type: 3 (module) + u32 size: 17 + u32 mod_start: 0x104000 + u32 mod_end: 0x105254 + char cmdline[]: "" +} +Multiboot 2 info tag { + u32 type: 6 (memory map) + u32 size: 160 + u32 entry_size: 24 + u32 entry_version: 0 + varies(entry_size) entries[]: [ + [0] entry: { + u64 base_addr: 0x0 + u64 length: 654336 + u32 type: 1 + u32 reserved: 0x0 + } + [1] entry: { + u64 base_addr: 0x9fc00 + u64 length: 1024 + u32 type: 2 + u32 reserved: 0x0 + } + [2] entry: { + u64 base_addr: 0xf0000 + u64 length: 65536 + u32 type: 2 + u32 reserved: 0x0 + } + [3] entry: { + u64 base_addr: 0x100000 + u64 length: 133038080 + u32 type: 1 + u32 reserved: 0x0 + } + [4] entry: { + u64 base_addr: 0x7fe0000 + u64 length: 131072 + u32 type: 2 + u32 reserved: 0x0 + } + [5] entry: { + u64 base_addr: 0xfffc0000 + u64 length: 262144 + u32 type: 2 + u32 reserved: 0x0 + } + ] +} +Multiboot 2 info tag { + u32 type: 9 (ELF symbols) + u32 size: 420 + u32 num: 10 + u32 entsize: 40 + u32 shndx: 9 +} +Multiboot 2 info tag { + u32 type: 4 (basic memory info) + u32 size: 16 + u32 mem_lower: 639 + u32 mem_upper: 129920 +} +Multiboot 2 info tag { + u32 type: 5 (BIOS boot device) + u32 size: 20 + u32 biosdev: 224 + u32 partition: 4294967295 + u32 sub_partition: 4294967295 +} +Multiboot 2 info tag { + u32 type: 8 (framebuffer info) + u32 size: 32 + u64 framebuffer_addr: 0xb8000 + u32 framebuffer_pitch: 160 + u32 framebuffer_width: 80 + u32 framebuffer_height: 25 + u8 framebuffer_bpp: 16 + u8 framebuffer_type: 2 + u16 reserved: 0x0 +} +Multiboot 2 info tag { + u32 type: 14 (ACPI old RSDP) + u32 size: 28 +} +Multiboot 2 info tag { + u32 type: 0 (none) + u32 size: 8 +} diff --git a/fixtures/multiboot2_info_example2.txt b/fixtures/multiboot2_info_example2.txt new file mode 100644 index 00000000..6ac5c405 --- /dev/null +++ b/fixtures/multiboot2_info_example2.txt @@ -0,0 +1,234 @@ +Multiboot 2 info { + u32 size: 1816 + u32 reserved: 0x0 +} +Multiboot 2 info tag { + u32 type: 1 (boot cmd line) + u32 size: 23 + char cmdline[]: "Hello, Kernel!" +} +Multiboot 2 info tag { + u32 type: 2 (boot loader name) + u32 size: 30 + char name[]: "GRUB 2.02-2ubuntu8.20" +} +Multiboot 2 info tag { + u32 type: 3 (module) + u32 size: 33 + u32 mod_start: 0x7b + u32 mod_end: 0x1c8 + char cmdline[]: "Hello, Module 1!" +} +Multiboot 2 info tag { + u32 type: 3 (module) + u32 size: 33 + u32 mod_start: 0x7b + u32 mod_end: 0x1c8 + char cmdline[]: "Hello, Module 2!" +} +Multiboot 2 info tag { + u32 type: 4 (basic memory info) + u32 size: 16 + u32 mem_lower: 123 + u32 mem_upper: 456 +} +Multiboot 2 info tag { + u32 type: 5 (BIOS boot device) + u32 size: 20 + u32 biosdev: 0 + u32 partition: 1 + u32 sub_partition: 2 +} +Multiboot 2 info tag { + u32 type: 6 (memory map) + u32 size: 160 + u32 entry_size: 24 + u32 entry_version: 0 + varies(entry_size) entries[]: [ + [0] entry: { + u64 base_addr: 0x0 + u64 length: 654336 + u32 type: 1 + u32 reserved: 0x0 + } + [1] entry: { + u64 base_addr: 0x9fc00 + u64 length: 1024 + u32 type: 2 + u32 reserved: 0x0 + } + [2] entry: { + u64 base_addr: 0xf0000 + u64 length: 65536 + u32 type: 2 + u32 reserved: 0x0 + } + [3] entry: { + u64 base_addr: 0x100000 + u64 length: 133038080 + u32 type: 1 + u32 reserved: 0x0 + } + [4] entry: { + u64 base_addr: 0x7fe0000 + u64 length: 131072 + u32 type: 2 + u32 reserved: 0x0 + } + [5] entry: { + u64 base_addr: 0xfffc0000 + u64 length: 262144 + u32 type: 2 + u32 reserved: 0x0 + } + ] +} +Multiboot 2 info tag { + u32 type: 7 (VBE info) + u32 size: 784 + u16 vbe_mode: 0 + u16 vbe_interface_seg: 123 + u16 vbe_interface_off: 456 + u16 vbe_interface_len: 789 + u8 vbe_control_info[]: [ + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 123 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 12 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 123 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + ] + u8 vbe_mode_info[]: [ + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + ] +} +Multiboot 2 info tag { + u32 type: 8 (framebuffer info) + u32 size: 40 + u64 framebuffer_addr: 0x7b + u32 framebuffer_pitch: 456 + u32 framebuffer_width: 123 + u32 framebuffer_height: 456 + u8 framebuffer_bpp: 8 + u8 framebuffer_type: 1 + u16 reserved: 0x0 +} +Multiboot 2 info tag { + u32 type: 9 (ELF symbols) + u32 size: 420 + u32 num: 10 + u32 entsize: 40 + u32 shndx: 9 +} +Multiboot 2 info tag { + u32 type: 10 (APM table) + u32 size: 28 + u16 version: 0 + u16 cseg: 123 + u32 offset: 456 + u16 cseg_16: 789 + u16 dseg: 123 + u16 flags: 1 + u16 cseg_len: 456 + u16 cseg_16_len: 789 + u16 dseg_len: 123 +} +Multiboot 2 info tag { + u32 type: 11 (EFI 32bit system table ptr) + u32 size: 12 + u32 pointer: 0 +} +Multiboot 2 info tag { + u32 type: 12 (EFI 64bit system table ptr) + u32 size: 16 + u64 pointer: 0 +} +Multiboot 2 info tag { + u32 type: 13 (SMBIOS tables) + u32 size: 24 + u8 major: 1 + u8 minor: 2 + u8 reserved[6]: [0x0, 0x0, 0x0, 0x0, 0x0, 0x0] +} +Multiboot 2 info tag { + u32 type: 14 (ACPI old RSDP) + u32 size: 16 +} +Multiboot 2 info tag { + u32 type: 15 (ACPI new RSDP) + u32 size: 16 +} +Multiboot 2 info tag { + u32 type: 16 (networking info) + u32 size: 16 +} +Multiboot 2 info tag { + u32 type: 17 (EFI memory map) + u32 size: 24 + u32 descriptor_size: 123 + u32 descriptor_version: 1 +} +Multiboot 2 info tag { + u32 type: 18 (EFI boot services not terminated) + u32 size: 8 +} +Multiboot 2 info tag { + u32 type: 19 (EFI 32bit image handle ptr) + u32 size: 12 + u32 pointer: 0 +} +Multiboot 2 info tag { + u32 type: 20 (EFI 64bit image handle ptr) + u32 size: 16 + u64 pointer: 0 +} +Multiboot 2 info tag { + u32 type: 21 (image load base phys addr) + u32 size: 12 + u32 load_base_addr: 0x7b +} +Multiboot 2 info tag { + u32 type: 0 (none) + u32 size: 8 +} diff --git a/include/.gitignore b/include/.gitignore new file mode 100644 index 00000000..5441d2ad --- /dev/null +++ b/include/.gitignore @@ -0,0 +1 @@ +/kernaux/version.h diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 00000000..e98bf740 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1,33 @@ +/multiboot2_header_print0 +/multiboot2_header_print1 +/multiboot2_header_print2 +/multiboot2_info_print0 +/multiboot2_info_print1 +/multiboot2_info_print2 +/test_arch_i386 +/test_cmdline +/test_cmdline_gen +/test_cmdline_gen.c +/test_elf +/test_free_list +/test_mbr +/test_memmap +/test_multiboot2_common_packing +/test_multiboot2_header_helpers +/test_multiboot2_header_print +/test_multiboot2_header_print.c +/test_multiboot2_header_validation +/test_multiboot2_info_helpers +/test_multiboot2_info_print +/test_multiboot2_info_print.c +/test_multiboot2_info_validation +/test_ntoa +/test_ntoa_assert +/test_pfa +/test_pfa_assert +/test_printf +/test_printf_fmt_gen +/test_printf_fmt_gen.c +/test_printf_gen +/test_printf_gen.c +/test_units_human diff --git a/tests/Makefile.am b/tests/Makefile.am index 6796b1b9..04419860 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -217,7 +217,10 @@ TESTS += test_multiboot2_header_print test_multiboot2_header_print_DEPENDENCIES = \ multiboot2_header_print0 \ multiboot2_header_print1 \ - multiboot2_header_print2 + multiboot2_header_print2 \ + ../fixtures/multiboot2_header_example0.txt \ + ../fixtures/multiboot2_header_example1.txt \ + ../fixtures/multiboot2_header_example2.txt test_multiboot2_header_print_LDADD = $(top_builddir)/libkernaux.la test_multiboot2_header_print_SOURCES = \ main.c \ @@ -261,7 +264,10 @@ TESTS += test_multiboot2_info_print test_multiboot2_info_print_DEPENDENCIES = \ multiboot2_info_print0 \ multiboot2_info_print1 \ - multiboot2_info_print2 + multiboot2_info_print2 \ + ../fixtures/multiboot2_info_example0.txt \ + ../fixtures/multiboot2_info_example1.txt \ + ../fixtures/multiboot2_info_example2.txt test_multiboot2_info_print_LDADD = $(top_builddir)/libkernaux.la test_multiboot2_info_print_SOURCES = \ main.c \ diff --git a/tests/test_multiboot2_header_print.c b/tests/test_multiboot2_header_print.c deleted file mode 100644 index 9d4e87f4..00000000 --- a/tests/test_multiboot2_header_print.c +++ /dev/null @@ -1,218 +0,0 @@ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include - -#ifndef __USE_POSIX2 -#define __USE_POSIX2 -#endif -#include - -static const char output0[] = - "Multiboot 2 header {\n" - " u32 magic: 0xe85250d6\n" - " u32 arch: 0 (i386)\n" - " u32 size: 24\n" - " u32 checksum: 0x17adaf12\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 0 (none)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 8\n" - "}\n"; - -static const char output1[] = - "Multiboot 2 header {\n" - " u32 magic: 0xe85250d6\n" - " u32 arch: 4 (MIPS32)\n" - " u32 size: 104\n" - " u32 checksum: 0x17adaebe\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 4 (flags)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 12\n" - " u32 console_flags: 0x0 ()\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 4 (flags)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 12\n" - " u32 console_flags: 0x1 (\n" - " REQUIRE_CONSOLE\n" - " )\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 4 (flags)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 12\n" - " u32 console_flags: 0x2 (\n" - " EGA_SUPPORT\n" - " )\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 4 (flags)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 12\n" - " u32 console_flags: 0x3 (\n" - " REQUIRE_CONSOLE |\n" - " EGA_SUPPORT\n" - " )\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 1 (information request)\n" - " u16 flags: 0x1 (\n" - " OPTIONAL\n" - " )\n" - " u32 size: 12\n" - " u32 mbi_tag_types[]: [\n" - " 0 (none)\n" - " ]\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 0 (none)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 8\n" - "}\n"; - -static const char output2[] = - "Multiboot 2 header {\n" - " u32 magic: 0xe85250d6\n" - " u32 arch: 0 (i386)\n" - " u32 size: 272\n" - " u32 checksum: 0x17adae1a\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 1 (information request)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 96\n" - " u32 mbi_tag_types[]: [\n" - " 0 (none)\n" - " 1 (boot cmd line)\n" - " 2 (boot loader name)\n" - " 3 (module)\n" - " 4 (basic memory info)\n" - " 5 (BIOS boot device)\n" - " 6 (memory map)\n" - " 7 (VBE info)\n" - " 8 (framebuffer info)\n" - " 9 (ELF symbols)\n" - " 10 (APM table)\n" - " 11 (EFI 32bit system table ptr)\n" - " 12 (EFI 64bit system table ptr)\n" - " 13 (SMBIOS tables)\n" - " 14 (ACPI old RSDP)\n" - " 15 (ACPI new RSDP)\n" - " 16 (networking info)\n" - " 17 (EFI memory map)\n" - " 18 (EFI boot services not terminated)\n" - " 19 (EFI 32bit image handle ptr)\n" - " 20 (EFI 64bit image handle ptr)\n" - " 21 (image load base phys addr)\n" - " ]\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 2 (address)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 24\n" - " u32 header_addr: 0xcafebabe\n" - " u32 load_addr: 0xdeadbeaf\n" - " u32 load_end_addr: 0xdeadbabe\n" - " u32 bss_end_addr: 0xcafebeaf\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 3 (entry address)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 12\n" - " u32 entry_addr: 0xcafebabe\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 4 (flags)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 12\n" - " u32 console_flags: 0x0 ()\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 5 (framebuffer)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 20\n" - " u32 width: 80\n" - " u32 height: 25\n" - " u32 depth: 8\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 6 (module alignment)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 8\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 7 (EFI boot services)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 8\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 8 (EFI i386 entry address)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 12\n" - " u32 entry_addr: 0xcafebabe\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 9 (EFI amd64 entry address)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 12\n" - " u32 entry_addr: 0xdeadbeaf\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 10 (relocatable header)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 24\n" - " u32 min_addr: 0xcafebabe\n" - " u32 max_addr: 0xdeadbeaf\n" - " u32 align: 8\n" - " u32 preference: 1 (lowest)\n" - "}\n" - "Multiboot 2 header tag {\n" - " u16 type: 0 (none)\n" - " u16 flags: 0x0 ()\n" - " u32 size: 8\n" - "}\n"; - -void test_main() -{ - { - FILE *const fd = popen("./multiboot2_header_print0", "r"); - assert(fd != NULL); - - for (const char *ch = output0; *ch; ++ch) { - assert(fgetc(fd) == *ch); - } - - const int status = pclose(fd); - assert(status == 0); - } - - { - FILE *const fd = popen("./multiboot2_header_print1", "r"); - assert(fd != NULL); - - for (const char *ch = output1; *ch; ++ch) { - assert(fgetc(fd) == *ch); - } - - const int status = pclose(fd); - assert(status == 0); - } - - { - FILE *const fd = popen("./multiboot2_header_print2", "r"); - assert(fd != NULL); - - for (const char *ch = output2; *ch; ++ch) { - assert(fgetc(fd) == *ch); - } - - const int status = pclose(fd); - assert(status == 0); - } -} diff --git a/tests/test_multiboot2_header_print.c.in b/tests/test_multiboot2_header_print.c.in new file mode 100644 index 00000000..00cc39e7 --- /dev/null +++ b/tests/test_multiboot2_header_print.c.in @@ -0,0 +1,61 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#ifndef __USE_POSIX2 +#define __USE_POSIX2 +#endif +#include + +void test_main() +{ + { + FILE *const print_file = + popen("@abs_top_builddir@/tests/multiboot2_header_print0", "r"); + FILE *const text_file = + fopen("@abs_top_srcdir@/fixtures/multiboot2_header_example0.txt", "r"); + assert(print_file != NULL); + assert(text_file != NULL); + + while (!feof(text_file)) { + assert(fgetc(print_file) == fgetc(text_file)); + } + + assert(pclose(print_file) == 0); + assert(fclose(text_file) == 0); + } + + { + FILE *const print_file = + popen("@abs_top_builddir@/tests/multiboot2_header_print1", "r"); + FILE *const text_file = + fopen("@abs_top_srcdir@/fixtures/multiboot2_header_example1.txt", "r"); + assert(print_file != NULL); + assert(text_file != NULL); + + while (!feof(text_file)) { + assert(fgetc(print_file) == fgetc(text_file)); + } + + assert(pclose(print_file) == 0); + assert(fclose(text_file) == 0); + } + + { + FILE *const print_file = + popen("@abs_top_builddir@/tests/multiboot2_header_print2", "r"); + FILE *const text_file = + fopen("@abs_top_srcdir@/fixtures/multiboot2_header_example2.txt", "r"); + assert(print_file != NULL); + assert(text_file != NULL); + + while (!feof(text_file)) { + assert(fgetc(print_file) == fgetc(text_file)); + } + + assert(pclose(print_file) == 0); + assert(fclose(text_file) == 0); + } +} diff --git a/tests/test_multiboot2_info_print.c b/tests/test_multiboot2_info_print.c deleted file mode 100644 index 2f5d7cff..00000000 --- a/tests/test_multiboot2_info_print.c +++ /dev/null @@ -1,441 +0,0 @@ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include -#include -#include - -#ifndef __USE_POSIX2 -#define __USE_POSIX2 -#endif -#include - -static const char output0[] = - "Multiboot 2 info {\n" - " u32 size: 16\n" - " u32 reserved: 0x0\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 0 (none)\n" - " u32 size: 8\n" - "}\n"; - -static const char output1[] = - "Multiboot 2 info {\n" - " u32 size: 864\n" - " u32 reserved: 0x0\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 21 (image load base phys addr)\n" - " u32 size: 12\n" - " u32 load_base_addr: 0x400000\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 1 (boot cmd line)\n" - " u32 size: 21\n" - " char cmdline[]: \"hello kernel\"\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 2 (boot loader name)\n" - " u32 size: 30\n" - " char name[]: \"GRUB 2.02-2ubuntu8.20\"\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 10 (APM table)\n" - " u32 size: 28\n" - " u16 version: 258\n" - " u16 cseg: 61440\n" - " u32 offset: 54479\n" - " u16 cseg_16: 61440\n" - " u16 dseg: 61440\n" - " u16 flags: 3\n" - " u16 cseg_len: 65520\n" - " u16 cseg_16_len: 65520\n" - " u16 dseg_len: 65520\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 3 (module)\n" - " u32 size: 29\n" - " u32 mod_start: 0x102000\n" - " u32 mod_end: 0x10329c\n" - " char cmdline[]: \"hello module\"\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 3 (module)\n" - " u32 size: 17\n" - " u32 mod_start: 0x104000\n" - " u32 mod_end: 0x105254\n" - " char cmdline[]: \"\"\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 6 (memory map)\n" - " u32 size: 160\n" - " u32 entry_size: 24\n" - " u32 entry_version: 0\n" - " varies(entry_size) entries[]: [\n" - " [0] entry: {\n" - " u64 base_addr: 0x0\n" - " u64 length: 654336\n" - " u32 type: 1\n" - " u32 reserved: 0x0\n" - " }\n" - " [1] entry: {\n" - " u64 base_addr: 0x9fc00\n" - " u64 length: 1024\n" - " u32 type: 2\n" - " u32 reserved: 0x0\n" - " }\n" - " [2] entry: {\n" - " u64 base_addr: 0xf0000\n" - " u64 length: 65536\n" - " u32 type: 2\n" - " u32 reserved: 0x0\n" - " }\n" - " [3] entry: {\n" - " u64 base_addr: 0x100000\n" - " u64 length: 133038080\n" - " u32 type: 1\n" - " u32 reserved: 0x0\n" - " }\n" - " [4] entry: {\n" - " u64 base_addr: 0x7fe0000\n" - " u64 length: 131072\n" - " u32 type: 2\n" - " u32 reserved: 0x0\n" - " }\n" - " [5] entry: {\n" - " u64 base_addr: 0xfffc0000\n" - " u64 length: 262144\n" - " u32 type: 2\n" - " u32 reserved: 0x0\n" - " }\n" - " ]\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 9 (ELF symbols)\n" - " u32 size: 420\n" - " u32 num: 10\n" - " u32 entsize: 40\n" - " u32 shndx: 9\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 4 (basic memory info)\n" - " u32 size: 16\n" - " u32 mem_lower: 639\n" - " u32 mem_upper: 129920\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 5 (BIOS boot device)\n" - " u32 size: 20\n" - " u32 biosdev: 224\n" - " u32 partition: 4294967295\n" - " u32 sub_partition: 4294967295\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 8 (framebuffer info)\n" - " u32 size: 32\n" - " u64 framebuffer_addr: 0xb8000\n" - " u32 framebuffer_pitch: 160\n" - " u32 framebuffer_width: 80\n" - " u32 framebuffer_height: 25\n" - " u8 framebuffer_bpp: 16\n" - " u8 framebuffer_type: 2\n" - " u16 reserved: 0x0\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 14 (ACPI old RSDP)\n" - " u32 size: 28\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 0 (none)\n" - " u32 size: 8\n" - "}\n"; - -static const char output2_part1[] = - "Multiboot 2 info {\n" - " u32 size: 1816\n" - " u32 reserved: 0x0\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 1 (boot cmd line)\n" - " u32 size: 23\n" - " char cmdline[]: \"Hello, Kernel!\"\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 2 (boot loader name)\n" - " u32 size: 30\n" - " char name[]: \"GRUB 2.02-2ubuntu8.20\"\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 3 (module)\n" - " u32 size: 33\n" - " u32 mod_start: 0x7b\n" - " u32 mod_end: 0x1c8\n" - " char cmdline[]: \"Hello, Module 1!\"\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 3 (module)\n" - " u32 size: 33\n" - " u32 mod_start: 0x7b\n" - " u32 mod_end: 0x1c8\n" - " char cmdline[]: \"Hello, Module 2!\"\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 4 (basic memory info)\n" - " u32 size: 16\n" - " u32 mem_lower: 123\n" - " u32 mem_upper: 456\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 5 (BIOS boot device)\n" - " u32 size: 20\n" - " u32 biosdev: 0\n" - " u32 partition: 1\n" - " u32 sub_partition: 2\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 6 (memory map)\n" - " u32 size: 160\n" - " u32 entry_size: 24\n" - " u32 entry_version: 0\n" - " varies(entry_size) entries[]: [\n" - " [0] entry: {\n" - " u64 base_addr: 0x0\n" - " u64 length: 654336\n" - " u32 type: 1\n" - " u32 reserved: 0x0\n" - " }\n" - " [1] entry: {\n" - " u64 base_addr: 0x9fc00\n" - " u64 length: 1024\n" - " u32 type: 2\n" - " u32 reserved: 0x0\n" - " }\n" - " [2] entry: {\n" - " u64 base_addr: 0xf0000\n" - " u64 length: 65536\n" - " u32 type: 2\n" - " u32 reserved: 0x0\n" - " }\n" - " [3] entry: {\n" - " u64 base_addr: 0x100000\n" - " u64 length: 133038080\n" - " u32 type: 1\n" - " u32 reserved: 0x0\n" - " }\n" - " [4] entry: {\n" - " u64 base_addr: 0x7fe0000\n" - " u64 length: 131072\n" - " u32 type: 2\n" - " u32 reserved: 0x0\n" - " }\n" - " [5] entry: {\n" - " u64 base_addr: 0xfffc0000\n" - " u64 length: 262144\n" - " u32 type: 2\n" - " u32 reserved: 0x0\n" - " }\n" - " ]\n" - "}\n"; - -static const char output2_part2[] = - "Multiboot 2 info tag {\n" - " u32 type: 7 (VBE info)\n" - " u32 size: 784\n" - " u16 vbe_mode: 0\n" - " u16 vbe_interface_seg: 123\n" - " u16 vbe_interface_off: 456\n" - " u16 vbe_interface_len: 789\n" - " u8 vbe_control_info[]: [\n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 123 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 12 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 123\n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " ]\n" - " u8 vbe_mode_info[]: [\n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255\n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n" - " ]\n" - "}\n"; - -static const char output2_part3[] = - "Multiboot 2 info tag {\n" - " u32 type: 8 (framebuffer info)\n" - " u32 size: 40\n" - " u64 framebuffer_addr: 0x7b\n" - " u32 framebuffer_pitch: 456\n" - " u32 framebuffer_width: 123\n" - " u32 framebuffer_height: 456\n" - " u8 framebuffer_bpp: 8\n" - " u8 framebuffer_type: 1\n" - " u16 reserved: 0x0\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 9 (ELF symbols)\n" - " u32 size: 420\n" - " u32 num: 10\n" - " u32 entsize: 40\n" - " u32 shndx: 9\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 10 (APM table)\n" - " u32 size: 28\n" - " u16 version: 0\n" - " u16 cseg: 123\n" - " u32 offset: 456\n" - " u16 cseg_16: 789\n" - " u16 dseg: 123\n" - " u16 flags: 1\n" - " u16 cseg_len: 456\n" - " u16 cseg_16_len: 789\n" - " u16 dseg_len: 123\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 11 (EFI 32bit system table ptr)\n" - " u32 size: 12\n" - " u32 pointer: 0\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 12 (EFI 64bit system table ptr)\n" - " u32 size: 16\n" - " u64 pointer: 0\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 13 (SMBIOS tables)\n" - " u32 size: 24\n" - " u8 major: 1\n" - " u8 minor: 2\n" - " u8 reserved[6]: [0x0, 0x0, 0x0, 0x0, 0x0, 0x0]\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 14 (ACPI old RSDP)\n" - " u32 size: 16\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 15 (ACPI new RSDP)\n" - " u32 size: 16\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 16 (networking info)\n" - " u32 size: 16\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 17 (EFI memory map)\n" - " u32 size: 24\n" - " u32 descriptor_size: 123\n" - " u32 descriptor_version: 1\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 18 (EFI boot services not terminated)\n" - " u32 size: 8\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 19 (EFI 32bit image handle ptr)\n" - " u32 size: 12\n" - " u32 pointer: 0\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 20 (EFI 64bit image handle ptr)\n" - " u32 size: 16\n" - " u64 pointer: 0\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 21 (image load base phys addr)\n" - " u32 size: 12\n" - " u32 load_base_addr: 0x7b\n" - "}\n" - "Multiboot 2 info tag {\n" - " u32 type: 0 (none)\n" - " u32 size: 8\n" - "}\n"; - -void test_main() -{ - { - FILE *const fd = popen("./multiboot2_info_print0", "r"); - assert(fd != NULL); - - for (const char *ch = output0; *ch; ++ch) { - assert(fgetc(fd) == *ch); - } - - const int status = pclose(fd); - assert(status == 0); - } - - { - FILE *const fd = popen("./multiboot2_info_print1", "r"); - assert(fd != NULL); - - for (const char *ch = output1; *ch; ++ch) { - assert(fgetc(fd) == *ch); - } - - const int status = pclose(fd); - assert(status == 0); - } - - { - const size_t part1_len = strlen(output2_part1); - const size_t part2_len = strlen(output2_part2); - const size_t part3_len = strlen(output2_part3); - char *const output2 = malloc(1 + part1_len + part2_len + part3_len); - assert(output2); - strcpy(output2, output2_part1); - strcat(output2, output2_part2); - strcat(output2, output2_part3); - - FILE *const fd = popen("./multiboot2_info_print2", "r"); - assert(fd != NULL); - - for (const char *ch = output2; *ch; ++ch) { - assert(fgetc(fd) == *ch); - } - - const int status = pclose(fd); - assert(status == 0); - } -} diff --git a/tests/test_multiboot2_info_print.c.in b/tests/test_multiboot2_info_print.c.in new file mode 100644 index 00000000..d4155a99 --- /dev/null +++ b/tests/test_multiboot2_info_print.c.in @@ -0,0 +1,61 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#ifndef __USE_POSIX2 +#define __USE_POSIX2 +#endif +#include + +void test_main() +{ + { + FILE *const print_file = + popen("@abs_top_builddir@/tests/multiboot2_info_print0", "r"); + FILE *const text_file = + fopen("@abs_top_srcdir@/fixtures/multiboot2_info_example0.txt", "r"); + assert(print_file != NULL); + assert(text_file != NULL); + + while (!feof(text_file)) { + assert(fgetc(print_file) == fgetc(text_file)); + } + + assert(pclose(print_file) == 0); + assert(fclose(text_file) == 0); + } + + { + FILE *const print_file = + popen("@abs_top_builddir@/tests/multiboot2_info_print1", "r"); + FILE *const text_file = + fopen("@abs_top_srcdir@/fixtures/multiboot2_info_example1.txt", "r"); + assert(print_file != NULL); + assert(text_file != NULL); + + while (!feof(text_file)) { + assert(fgetc(print_file) == fgetc(text_file)); + } + + assert(pclose(print_file) == 0); + assert(fclose(text_file) == 0); + } + + { + FILE *const print_file = + popen("@abs_top_builddir@/tests/multiboot2_info_print2", "r"); + FILE *const text_file = + fopen("@abs_top_srcdir@/fixtures/multiboot2_info_example2.txt", "r"); + assert(print_file != NULL); + assert(text_file != NULL); + + while (!feof(text_file)) { + assert(fgetc(print_file) == fgetc(text_file)); + } + + assert(pclose(print_file) == 0); + assert(fclose(text_file) == 0); + } +} From e829231328467b6842774c1a0dbc16ba42f28291 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 19 Dec 2022 15:59:08 +0400 Subject: [PATCH 5/6] Print Multiboot 2 ELF section headers (#154) --- ChangeLog | 4 + fixtures/multiboot2_info_example1.txt | 147 ++++++++++++++++++- fixtures/multiboot2_info_example2.txt | 147 ++++++++++++++++++- include/kernaux/elf.h | 202 ++++++++++++++++++-------- include/kernaux/multiboot2.h | 2 +- src/elf.c | 87 +++++------ src/multiboot2/header_print.c | 34 +++-- src/multiboot2/info_print.c | 191 +++++++++++++++++++++--- tests/test_elf.c | 23 +-- 9 files changed, 660 insertions(+), 177 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6c0267a2..84faee21 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2022-12-19 Alex Kotov + + * src/multiboot2/*_print.c: Print Multiboot 2 ELF section headers + 2022-12-17 Alex Kotov * configure.ac: Feature "--(enable|disable)-fixtures" has been added diff --git a/fixtures/multiboot2_info_example1.txt b/fixtures/multiboot2_info_example1.txt index 93efa0e0..3d73f613 100644 --- a/fixtures/multiboot2_info_example1.txt +++ b/fixtures/multiboot2_info_example1.txt @@ -50,37 +50,37 @@ Multiboot 2 info tag { u32 entry_size: 24 u32 entry_version: 0 varies(entry_size) entries[]: [ - [0] entry: { + [0]: { u64 base_addr: 0x0 u64 length: 654336 u32 type: 1 u32 reserved: 0x0 } - [1] entry: { + [1]: { u64 base_addr: 0x9fc00 u64 length: 1024 u32 type: 2 u32 reserved: 0x0 } - [2] entry: { + [2]: { u64 base_addr: 0xf0000 u64 length: 65536 u32 type: 2 u32 reserved: 0x0 } - [3] entry: { + [3]: { u64 base_addr: 0x100000 u64 length: 133038080 u32 type: 1 u32 reserved: 0x0 } - [4] entry: { + [4]: { u64 base_addr: 0x7fe0000 u64 length: 131072 u32 type: 2 u32 reserved: 0x0 } - [5] entry: { + [5]: { u64 base_addr: 0xfffc0000 u64 length: 262144 u32 type: 2 @@ -94,6 +94,141 @@ Multiboot 2 info tag { u32 num: 10 u32 entsize: 40 u32 shndx: 9 + varies(entsize) section_headers[]: [ + [0]: { + name: 0 + type: 0 (NULL) + flags: 0x0 () + addr: 0x0 + offset: 0x0 + size: 0 + link: 0 + info: 0 + addralign: 0 + entsize: 0 + } + [1]: { + name: 27 + type: 1 (PROGBITS) + flags: 0x6 ( + ALLOC | + EXECINSTR + ) + addr: 0x400000 + offset: 0x1000 + size: 13718 + link: 0 + info: 0 + addralign: 4096 + entsize: 0 + } + [2]: { + name: 33 + type: 1 (PROGBITS) + flags: 0x2 ( + ALLOC + ) + addr: 0x404000 + offset: 0x5000 + size: 2824 + link: 0 + info: 0 + addralign: 4096 + entsize: 0 + } + [3]: { + name: 41 + type: 1 (PROGBITS) + flags: 0x2 ( + ALLOC + ) + addr: 0x404b08 + offset: 0x5b08 + size: 2692 + link: 0 + info: 0 + addralign: 4 + entsize: 0 + } + [4]: { + name: 51 + type: 1 (PROGBITS) + flags: 0x3 ( + WRITE | + ALLOC + ) + addr: 0x406000 + offset: 0x7000 + size: 1 + link: 0 + info: 0 + addralign: 4096 + entsize: 0 + } + [5]: { + name: 57 + type: 8 (NOBITS) + flags: 0x3 ( + WRITE | + ALLOC + ) + addr: 0x407000 + offset: 0x7001 + size: 43328 + link: 0 + info: 0 + addralign: 4096 + entsize: 0 + } + [6]: { + name: 62 + type: 1 (PROGBITS) + flags: 0x30 () + addr: 0x100000 + offset: 0x7001 + size: 17 + link: 0 + info: 0 + addralign: 1 + entsize: 1 + } + [7]: { + name: 1 + type: 2 (SYMTAB) + flags: 0x0 () + addr: 0x100014 + offset: 0x7014 + size: 3248 + link: 8 + info: 72 + addralign: 4 + entsize: 16 + } + [8]: { + name: 9 + type: 3 (STRTAB) + flags: 0x0 () + addr: 0x100cc4 + offset: 0x7cc4 + size: 3536 + link: 0 + info: 0 + addralign: 1 + entsize: 0 + } + [9]: { + name: 17 + type: 3 (STRTAB) + flags: 0x0 () + addr: 0x101a94 + offset: 0x8a94 + size: 71 + link: 0 + info: 0 + addralign: 1 + entsize: 0 + } + ] } Multiboot 2 info tag { u32 type: 4 (basic memory info) diff --git a/fixtures/multiboot2_info_example2.txt b/fixtures/multiboot2_info_example2.txt index 6ac5c405..d32769c5 100644 --- a/fixtures/multiboot2_info_example2.txt +++ b/fixtures/multiboot2_info_example2.txt @@ -45,37 +45,37 @@ Multiboot 2 info tag { u32 entry_size: 24 u32 entry_version: 0 varies(entry_size) entries[]: [ - [0] entry: { + [0]: { u64 base_addr: 0x0 u64 length: 654336 u32 type: 1 u32 reserved: 0x0 } - [1] entry: { + [1]: { u64 base_addr: 0x9fc00 u64 length: 1024 u32 type: 2 u32 reserved: 0x0 } - [2] entry: { + [2]: { u64 base_addr: 0xf0000 u64 length: 65536 u32 type: 2 u32 reserved: 0x0 } - [3] entry: { + [3]: { u64 base_addr: 0x100000 u64 length: 133038080 u32 type: 1 u32 reserved: 0x0 } - [4] entry: { + [4]: { u64 base_addr: 0x7fe0000 u64 length: 131072 u32 type: 2 u32 reserved: 0x0 } - [5] entry: { + [5]: { u64 base_addr: 0xfffc0000 u64 length: 262144 u32 type: 2 @@ -160,6 +160,141 @@ Multiboot 2 info tag { u32 num: 10 u32 entsize: 40 u32 shndx: 9 + varies(entsize) section_headers[]: [ + [0]: { + name: 0 + type: 0 (NULL) + flags: 0x0 () + addr: 0x0 + offset: 0x0 + size: 0 + link: 0 + info: 0 + addralign: 0 + entsize: 0 + } + [1]: { + name: 27 + type: 1 (PROGBITS) + flags: 0x6 ( + ALLOC | + EXECINSTR + ) + addr: 0x400000 + offset: 0x1000 + size: 13718 + link: 0 + info: 0 + addralign: 4096 + entsize: 0 + } + [2]: { + name: 33 + type: 1 (PROGBITS) + flags: 0x2 ( + ALLOC + ) + addr: 0x404000 + offset: 0x5000 + size: 2824 + link: 0 + info: 0 + addralign: 4096 + entsize: 0 + } + [3]: { + name: 41 + type: 1 (PROGBITS) + flags: 0x2 ( + ALLOC + ) + addr: 0x404b08 + offset: 0x5b08 + size: 2692 + link: 0 + info: 0 + addralign: 4 + entsize: 0 + } + [4]: { + name: 51 + type: 1 (PROGBITS) + flags: 0x3 ( + WRITE | + ALLOC + ) + addr: 0x406000 + offset: 0x7000 + size: 1 + link: 0 + info: 0 + addralign: 4096 + entsize: 0 + } + [5]: { + name: 57 + type: 8 (NOBITS) + flags: 0x3 ( + WRITE | + ALLOC + ) + addr: 0x407000 + offset: 0x7001 + size: 43328 + link: 0 + info: 0 + addralign: 4096 + entsize: 0 + } + [6]: { + name: 62 + type: 1 (PROGBITS) + flags: 0x30 () + addr: 0x100000 + offset: 0x7001 + size: 17 + link: 0 + info: 0 + addralign: 1 + entsize: 1 + } + [7]: { + name: 1 + type: 2 (SYMTAB) + flags: 0x0 () + addr: 0x100014 + offset: 0x7014 + size: 3248 + link: 8 + info: 72 + addralign: 4 + entsize: 16 + } + [8]: { + name: 9 + type: 3 (STRTAB) + flags: 0x0 () + addr: 0x100cc4 + offset: 0x7cc4 + size: 3536 + link: 0 + info: 0 + addralign: 1 + entsize: 0 + } + [9]: { + name: 17 + type: 3 (STRTAB) + flags: 0x0 () + addr: 0x101a94 + offset: 0x8a94 + size: 71 + link: 0 + info: 0 + addralign: 1 + entsize: 0 + } + ] } Multiboot 2 info tag { u32 type: 10 (APM table) diff --git a/include/kernaux/elf.h b/include/kernaux/elf.h index 5fde2266..793cdc31 100644 --- a/include/kernaux/elf.h +++ b/include/kernaux/elf.h @@ -7,87 +7,173 @@ extern "C" { #include -#include +#include #include +struct KernAux_ELF_Ident { + uint8_t magic_0x7f; + uint8_t magic_E; + uint8_t magic_L; + uint8_t magic_F; + uint8_t class_; + uint8_t data; + uint8_t version; + uint8_t unused[9]; +} +KERNAUX_PACKED; + +KERNAUX_STATIC_TEST_STRUCT_SIZE(KernAux_ELF_Ident, 16); + +// KernAux_ELF_Ident.class_ +#define KERNAUX_ELF_CLASS_NONE 0 // Invalid class +#define KERNAUX_ELF_CLASS_32 1 // 32-bit objects +#define KERNAUX_ELF_CLASS_64 2 // 64-bit objects + +// KernAux_ELF_Ident.data +#define KERNAUX_ELF_DATA_NONE 0 // Invalid data encoding +#define KERNAUX_ELF_DATA_2LSB 1 // 0x01020304 == [0x04, 0x03, 0x02, 0x01] +#define KERNAUX_ELF_DATA_2MSB 2 // 0x01020304 == [0x01, 0x02, 0x03, 0x04] + struct KernAux_ELF_Header { - unsigned magic_0x7f : 8; - unsigned magic_E : 8; - unsigned magic_L : 8; - unsigned magic_F : 8; - unsigned bitness : 8; - unsigned endianness : 8; - unsigned header_version : 8; - unsigned os_abi : 8; - unsigned unused1 : 32; - unsigned unused2 : 32; - unsigned obj_type : 16; - unsigned isa : 16; - unsigned elf_version : 32; - unsigned entrypoint : 32; - unsigned prog_table_pos : 32; - unsigned sect_table_pos : 32; - unsigned arch_flags : 32; - unsigned header_size : 16; - unsigned prog_entr_size : 16; - unsigned prog_entr_num : 16; - unsigned sect_entr_size : 16; - unsigned sect_entr_num : 16; - unsigned sect_names_idx : 16; + struct KernAux_ELF_Ident ident; + uint16_t type; + uint16_t machine; + uint32_t version; + uint32_t entry; + uint32_t phoff; + uint32_t shoff; + uint32_t flags; + uint16_t ehsize; + uint16_t phentsize; + uint16_t phnum; + uint16_t shentsize; + uint16_t shnum; + uint16_t shstrndx; } KERNAUX_PACKED; KERNAUX_STATIC_TEST_STRUCT_SIZE(KernAux_ELF_Header, 52); -struct KernAux_ELF_ProgramEntry { - unsigned type : 32; - unsigned offset : 32; - unsigned virt_addr : 32; - unsigned phys_addr : 32; - unsigned file_size : 32; - unsigned mem_size : 32; - unsigned flags : 32; - unsigned align : 32; +// KernAux_ELF_Header.type +#define KERNAUX_ELF_TYPE_NONE 0 // No file type +#define KERNAUX_ELF_TYPE_REL 1 // Relocatable file +#define KERNAUX_ELF_TYPE_EXEC 2 // Executable file +#define KERNAUX_ELF_TYPE_DYN 3 // Shared object file +#define KERNAUX_ELF_TYPE_CORE 4 // Core file +#define KERNAUX_ELF_TYPE_LOPROC 0xff00 // Processor-specific +#define KERNAUX_ELF_TYPE_HIPROC 0xffff // Processor-specific + +// KernAux_ELF_Header.machine +#define KERNAUX_ELF_MACHINE_NONE 0 // No machine +#define KERNAUX_ELF_MACHINE_M32 1 // AT&T WE 32100 +#define KERNAUX_ELF_MACHINE_SPARC 2 // SPARC +#define KERNAUX_ELF_MACHINE_386 3 // Intel 80386 +#define KERNAUX_ELF_MACHINE_68K 4 // Motorola 68000 +#define KERNAUX_ELF_MACHINE_88K 5 // Motorola 88000 +#define KERNAUX_ELF_MACHINE_860 7 // Intel 80860 +#define KERNAUX_ELF_MACHINE_MIPS 8 // MIPS RS3000 + +// KernAux_ELF_Header.version +#define KERNAUX_ELF_VERSION_NONE 0 // Invalid version +#define KERNAUX_ELF_VERSION_CURRENT 1 // Current version + +struct KernAux_ELF_Section { + uint32_t name; + uint32_t type; + uint32_t flags; + uint32_t addr; + uint32_t offset; + uint32_t size; + uint32_t link; + uint32_t info; + uint32_t addralign; + uint32_t entsize; } KERNAUX_PACKED; -KERNAUX_STATIC_TEST_STRUCT_SIZE(KernAux_ELF_ProgramEntry, 32); +KERNAUX_STATIC_TEST_STRUCT_SIZE(KernAux_ELF_Section, 40); -struct KernAux_ELF_SectionEntry { - unsigned name : 32; - unsigned type : 32; - unsigned flags : 32; - unsigned vaddr : 32; - unsigned file_offset : 32; - unsigned file_size : 32; - unsigned link : 32; - unsigned info : 32; - unsigned alignment : 32; - unsigned ent_size : 32; +#define KERNAUX_ELF_SECT_TYPE_NULL 0 +#define KERNAUX_ELF_SECT_TYPE_PROGBITS 1 +#define KERNAUX_ELF_SECT_TYPE_SYMTAB 2 +#define KERNAUX_ELF_SECT_TYPE_STRTAB 3 +#define KERNAUX_ELF_SECT_TYPE_RELA 4 +#define KERNAUX_ELF_SECT_TYPE_HASH 5 +#define KERNAUX_ELF_SECT_TYPE_DYNAMIC 6 +#define KERNAUX_ELF_SECT_TYPE_NOTE 7 +#define KERNAUX_ELF_SECT_TYPE_NOBITS 8 +#define KERNAUX_ELF_SECT_TYPE_REL 9 +#define KERNAUX_ELF_SECT_TYPE_SHLIB 10 +#define KERNAUX_ELF_SECT_TYPE_DYNSYM 11 +#define KERNAUX_ELF_SECT_TYPE_LOPROC 0x70000000 +#define KERNAUX_ELF_SECT_TYPE_HIPROC 0x7fffffff +#define KERNAUX_ELF_SECT_TYPE_LOUSER 0x80000000 +#define KERNAUX_ELF_SECT_TYPE_HIUSER 0xffffffff + +#define KERNAUX_ELF_SECT_FLAGS_WRITE 0x1 +#define KERNAUX_ELF_SECT_FLAGS_ALLOC 0x2 +#define KERNAUX_ELF_SECT_FLAGS_EXECINSTR 0x4 +#define KERNAUX_ELF_SECT_FLAGS_MASKPROC 0xf0000000 + +const char *KernAux_ELF_Section_Type_to_str(uint32_t type); + +struct KernAux_ELF_Symbol { + uint32_t name; + uint32_t value; + uint32_t size; + uint8_t info; + uint8_t other; + uint16_t shndx; } KERNAUX_PACKED; -KERNAUX_STATIC_TEST_STRUCT_SIZE(KernAux_ELF_SectionEntry, 40); +KERNAUX_STATIC_TEST_STRUCT_SIZE(KernAux_ELF_Symbol, 16); -struct KernAux_ELF_RelocationEntry { - unsigned virt_addr : 32; - unsigned info : 32; +#define KERNAUX_ELF_SYM_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) +#define KERNAUX_ELF_SYM_BIND(info) ((info) >> 4) +#define KERNAUX_ELF_SYM_TYPE(info) ((into) & 0xf) + +#define KERNAUX_ELF_SYM_BIND_LOCAL 0 +#define KERNAUX_ELF_SYM_BIND_GLOBAL 1 +#define KERNAUX_ELF_SYM_BIND_WEAK 2 +#define KERNAUX_ELF_SYM_BIND_LOPROC 13 +#define KERNAUX_ELF_SYM_BIND_HIPROC 15 + +#define KERNAUX_ELF_SYM_TYPE_NOTYPE 0 +#define KERNAUX_ELF_SYM_TYPE_OBJECT 1 +#define KERNAUX_ELF_SYM_TYPE_FUNC 2 +#define KERNAUX_ELF_SYM_TYPE_SECTION 3 +#define KERNAUX_ELF_SYM_TYPE_FILE 4 +#define KERNAUX_ELF_SYM_TYPE_LOPROC 13 +#define KERNAUX_ELF_SYM_TYPE_HIPROC 15 + +struct KernAux_ELF_Program { + uint32_t type; + uint32_t offset; + uint32_t vaddr; + uint32_t paddr; + uint32_t filesz; + uint32_t memsz; + uint32_t flags; + uint32_t align; } KERNAUX_PACKED; -KERNAUX_STATIC_TEST_STRUCT_SIZE(KernAux_ELF_RelocationEntry, 8); +KERNAUX_STATIC_TEST_STRUCT_SIZE(KernAux_ELF_Program, 32); + +#define KERNAUX_ELF_PROG_TYPE_NULL 0 +#define KERNAUX_ELF_PROG_TYPE_LOAD 1 +#define KERNAUX_ELF_PROG_TYPE_DYNAMIC 2 +#define KERNAUX_ELF_PROG_TYPE_INTERP 3 +#define KERNAUX_ELF_PROG_TYPE_NOTE 4 +#define KERNAUX_ELF_PROG_TYPE_SHLIB 5 +#define KERNAUX_ELF_PROG_TYPE_PHDR 6 +#define KERNAUX_ELF_PROG_TYPE_LOPROC 0x70000000 +#define KERNAUX_ELF_PROG_TYPE_HIPROC 0x7fffffff #include -typedef struct KernAux_ELF_ProgramEntry KernAux_ELF_ProgramTable[]; - -typedef struct KernAux_ELF_SectionEntry KernAux_ELF_SectionTable[]; - -typedef struct KernAux_ELF_RelocationEntry KernAux_ELF_RelocationTable[]; - -bool KernAux_ELF_Header_is_valid(const struct KernAux_ELF_Header *header); - #ifdef __cplusplus } #endif diff --git a/include/kernaux/multiboot2.h b/include/kernaux/multiboot2.h index 63d740fd..fbd67a58 100644 --- a/include/kernaux/multiboot2.h +++ b/include/kernaux/multiboot2.h @@ -366,7 +366,7 @@ struct KernAux_Multiboot2_ITag_ELFSymbols { uint32_t entsize; uint32_t shndx; - // DATA: varies section_headers[] + // DATA: varies(entsize) section_headers[] } KERNAUX_PACKED; diff --git a/src/elf.c b/src/elf.c index 95da3cbe..125cbd11 100644 --- a/src/elf.c +++ b/src/elf.c @@ -2,56 +2,47 @@ #include "config.h" #endif -#include #include +#include -bool KernAux_ELF_Header_is_valid( - const struct KernAux_ELF_Header *const header -) { - KERNAUX_ASSERT(header); +#include - if (!( - header->magic_0x7f == 0x7f && - header->magic_E == 'E' && - header->magic_L == 'L' && - header->magic_F == 'F' && - header->header_version == 1 && - header->elf_version == 1 - )) { - return false; +const char *KernAux_ELF_Section_Type_to_str(const uint32_t type) +{ + switch (type) { + case KERNAUX_ELF_SECT_TYPE_NULL: + return "NULL"; + case KERNAUX_ELF_SECT_TYPE_PROGBITS: + return "PROGBITS"; + case KERNAUX_ELF_SECT_TYPE_SYMTAB: + return "SYMTAB"; + case KERNAUX_ELF_SECT_TYPE_STRTAB: + return "STRTAB"; + case KERNAUX_ELF_SECT_TYPE_RELA: + return "RELA"; + case KERNAUX_ELF_SECT_TYPE_HASH: + return "HASH"; + case KERNAUX_ELF_SECT_TYPE_DYNAMIC: + return "DYNAMIC"; + case KERNAUX_ELF_SECT_TYPE_NOTE: + return "NOTE"; + case KERNAUX_ELF_SECT_TYPE_NOBITS: + return "NOBITS"; + case KERNAUX_ELF_SECT_TYPE_REL: + return "REL"; + case KERNAUX_ELF_SECT_TYPE_SHLIB: + return "SHLIB"; + case KERNAUX_ELF_SECT_TYPE_DYNSYM: + return "DYNSYM"; + case KERNAUX_ELF_SECT_TYPE_LOPROC: + return "LOPROC"; + case KERNAUX_ELF_SECT_TYPE_HIPROC: + return "HIPROC"; + case KERNAUX_ELF_SECT_TYPE_LOUSER: + return "LOUSER"; + case KERNAUX_ELF_SECT_TYPE_HIUSER: + return "HIUSER"; + default: + return NULL; } - - if (!( - header->bitness == 1 || // 32 bit - header->bitness == 2 // 64 bit - )) { - return false; - } - - if (!( - header->endianness == 1 || // Little endian - header->endianness == 2 // Big endian - )) { - return false; - } - - if (!(header->os_abi <= 0x12 && header->os_abi != 0x05)) { - return false; - } - - if (!( - header->obj_type == 0x00 || // NONE - header->obj_type == 0x01 || // REL - header->obj_type == 0x02 || // EXEC - header->obj_type == 0x03 || // DYN - header->obj_type == 0x04 || // CORE - header->obj_type == 0xfe00 || // LOOS - header->obj_type == 0xfeff || // HIOS - header->obj_type == 0xff00 || // LOPROC - header->obj_type == 0xffff // HIPROC - )) { - return false; - } - - return true; } diff --git a/src/multiboot2/header_print.c b/src/multiboot2/header_print.c index bffe35fb..f0e95065 100644 --- a/src/multiboot2/header_print.c +++ b/src/multiboot2/header_print.c @@ -225,26 +225,30 @@ void KernAux_Multiboot2_HTag_InfoReq_print( // Print data: - PRINTLN(" u32 mbi_tag_types[]: ["); + if ((tag->base.size - sizeof(*tag)) / sizeof(uint32_t) == 0) { + PRINTLN(" u32 mbi_tag_types[]: []"); + } else { + PRINTLN(" u32 mbi_tag_types[]: ["); - const uint32_t *const mbi_tag_types = - (const uint32_t*)KERNAUX_MULTIBOOT2_DATA(tag); + const uint32_t *const mbi_tag_types = + (const uint32_t*)KERNAUX_MULTIBOOT2_DATA(tag); - for ( - size_t index = 0; - index < (tag->base.size - sizeof(*tag)) / sizeof(uint32_t); - ++index - ) { - KERNAUX_CAST_CONST(unsigned long, type, mbi_tag_types[index]); + for ( + size_t index = 0; + index < (tag->base.size - sizeof(*tag)) / sizeof(uint32_t); + ++index + ) { + KERNAUX_CAST_CONST(unsigned long, type, mbi_tag_types[index]); - PRINTLNF(" %lu (%s)", - type, - KernAux_Multiboot2_ITag_to_str(type) - ); + PRINTLNF(" %lu (%s)", + type, + KernAux_Multiboot2_ITag_to_str(type) + ); + } + + PRINTLN(" ]"); } - PRINTLN(" ]"); - FOOTER; } diff --git a/src/multiboot2/info_print.c b/src/multiboot2/info_print.c index 81b18777..95992757 100644 --- a/src/multiboot2/info_print.c +++ b/src/multiboot2/info_print.c @@ -3,10 +3,12 @@ #endif #include +#include #include #include #include +#include #include #include @@ -39,6 +41,44 @@ #define FOOTER do { PRINTLN("}"); } while (0) +#define INDENT do { \ + for (unsigned index = 0; index < basic_indentation; ++index) PRINT(" "); \ +} while (0) + +#define INDENT_MORE do { \ + for (unsigned index = 0; index < indentation_delta; ++index) PRINT(" "); \ +} while (0) + +static const struct { + uint32_t number; + const char *name; +} section_flag_names[] = { + { + .number = KERNAUX_ELF_SECT_FLAGS_WRITE, + .name = "WRITE", + }, + { + .number = KERNAUX_ELF_SECT_FLAGS_ALLOC, + .name = "ALLOC", + }, + { + .number = KERNAUX_ELF_SECT_FLAGS_EXECINSTR, + .name = "EXECINSTR", + }, + { + .number = KERNAUX_ELF_SECT_FLAGS_MASKPROC, + .name = "MASKPROC", + }, +}; + +static void KernAux_ELF_Section_Flags_print( + uint16_t flags, + KernAux_Display display, + unsigned basic_indentation, + unsigned indentation_delta, + bool indent_first +); + void KernAux_Multiboot2_Info_print( const struct KernAux_Multiboot2_Info *const multiboot2_info, const KernAux_Display display @@ -308,31 +348,43 @@ void KernAux_Multiboot2_ITag_MemoryMap_print( // Print data: - PRINTLN (" varies(entry_size) entries[]: ["); + if (tag->entry_size == 0 || + (tag->base.size - sizeof(*tag)) / tag->entry_size == 0) + { + PRINTLN (" varies(entry_size) entries[]: []"); + } else { + PRINTLN (" varies(entry_size) entries[]: ["); - const struct KernAux_Multiboot2_ITag_MemoryMap_EntryBase *const entries = - (struct KernAux_Multiboot2_ITag_MemoryMap_EntryBase*) - KERNAUX_MULTIBOOT2_DATA((struct KernAux_Multiboot2_ITag_MemoryMap*)tag); + const struct KernAux_Multiboot2_ITag_MemoryMap_EntryBase* + const entries = + (struct KernAux_Multiboot2_ITag_MemoryMap_EntryBase*) + KERNAUX_MULTIBOOT2_DATA(tag); - for ( - size_t index = 0; - index < (tag->base.size - sizeof(*tag)) / tag->entry_size; - ++index - ) { - KERNAUX_CAST_CONST(unsigned long long, base_addr, entries[index].base_addr); - KERNAUX_CAST_CONST(unsigned long long, length, entries[index].length); - KERNAUX_CAST_CONST(unsigned long, type, entries[index].type); - KERNAUX_CAST_CONST(unsigned long, reserved, entries[index].reserved); + for ( + size_t index = 0; + index < (tag->base.size - sizeof(*tag)) / tag->entry_size; + ++index + ) { + KERNAUX_CAST_CONST(unsigned long long, base_addr, + entries[index].base_addr); + KERNAUX_CAST_CONST(unsigned long long, length, + entries[index].length); + KERNAUX_CAST_CONST(unsigned long, type, + entries[index].type); + KERNAUX_CAST_CONST(unsigned long, reserved, + entries[index].reserved); - PRINTLNF(" [%zu] entry: {", index); - PRINTLNF(" u64 base_addr: 0x%llx", base_addr); - PRINTLNF(" u64 length: %llu", length); - PRINTLNF(" u32 type: %lu", type); - PRINTLNF(" u32 reserved: 0x%lx", reserved); - PRINTLN (" }"); + PRINTLNF(" [%zu]: {", index); + PRINTLNF(" u64 base_addr: 0x%llx", base_addr); + PRINTLNF(" u64 length: %llu", length); + PRINTLNF(" u32 type: %lu", type); + PRINTLNF(" u32 reserved: 0x%lx", reserved); + PRINTLN (" }"); + } + + PRINTLN(" ]"); } - PRINTLN(" ]"); FOOTER; } @@ -426,7 +478,62 @@ void KernAux_Multiboot2_ITag_ELFSymbols_print( PRINTLNF(" u32 entsize: %lu", entsize); PRINTLNF(" u32 shndx: %lu", shndx); - // TODO: Print data? + // Print data: + + if (tag->num == 0) { + PRINTLN(" varies(entsize) section_headers[]: []"); + } else { + PRINTLN(" varies(entsize) section_headers[]: ["); + + const struct KernAux_ELF_Section *section = + (const struct KernAux_ELF_Section*) + KERNAUX_MULTIBOOT2_DATA(tag); + + for (size_t index = 0; index < tag->num; ++index) { + KERNAUX_CAST_CONST(unsigned long, name, section->name); + KERNAUX_CAST_CONST(unsigned long, type, section->type); + KERNAUX_CAST_CONST(unsigned long, addr, section->addr); + KERNAUX_CAST_CONST(unsigned long, offset, section->offset); + KERNAUX_CAST_CONST(unsigned long, size, section->size); + KERNAUX_CAST_CONST(unsigned long, link, section->link); + KERNAUX_CAST_CONST(unsigned long, info, section->info); + KERNAUX_CAST_CONST(unsigned long, addralign, section->addralign); + KERNAUX_CAST_CONST(unsigned long, s_entsize, section->entsize); + + const char *const type_name = +#ifdef WITH_ELF + KernAux_ELF_Section_Type_to_str(section->type); +#else + "?"; +#endif + + PRINTLNF(" [%zu]: {", index); + PRINTLNF(" name: %lu", name); + PRINTLNF(" type: %lu (%s)", type, type_name); + PRINT (" flags: "); + KernAux_ELF_Section_Flags_print( + section->flags, + display, + 6, + 2, + false + ); + PRINTLNF(" addr: 0x%lx", addr); + PRINTLNF(" offset: 0x%lx", offset); + PRINTLNF(" size: %lu", size); + PRINTLNF(" link: %lu", link); + PRINTLNF(" info: %lu", info); + PRINTLNF(" addralign: %lu", addralign); + PRINTLNF(" entsize: %lu", s_entsize); + PRINTLN (" }"); + + section = + (const struct KernAux_ELF_Section*) + (((const uint8_t*)section) + tag->entsize); + } + + PRINTLN(" ]"); + } FOOTER; } @@ -610,3 +717,45 @@ void KernAux_Multiboot2_ITag_ImageLoadBasePhysAddr_print( FOOTER; } + +void KernAux_ELF_Section_Flags_print( + const uint16_t flags, + const KernAux_Display display, + const unsigned basic_indentation, + const unsigned indentation_delta, + const bool indent_first +) { + KERNAUX_CAST_CONST(unsigned long, flags_ul, flags); + + if (indent_first) INDENT; + PRINTF("0x%lx (", flags_ul); + + bool is_first = true; + + for ( + size_t index = 0; + index < sizeof(section_flag_names) / sizeof(section_flag_names[0]); + ++index + ) { + if (flags & section_flag_names[index].number) { + if (is_first) { + PRINTLN(""); + } else { + PRINTLN(" |"); + } + + INDENT; + INDENT_MORE; + PRINTF("%s", section_flag_names[index].name); + is_first = false; + } + } + + if (is_first) { + PRINTLN(")"); + } else { + PRINTLN(""); + INDENT; + PRINTLN(")"); + } +} diff --git a/tests/test_elf.c b/tests/test_elf.c index 29f77fab..d71c24a2 100644 --- a/tests/test_elf.c +++ b/tests/test_elf.c @@ -4,25 +4,4 @@ #include -#include -#include -#include -#include - -#define BUFFER_SIZE (1024 * 1024) - -void test_main(int argc, char **argv) -{ - assert(argc >= 1); - - FILE *const fd = fopen(argv[0], "r"); - assert(fd); - - uint8_t buffer[BUFFER_SIZE]; - const size_t size = fread(buffer, sizeof(uint8_t), BUFFER_SIZE, fd); - assert(size > 0); - - assert(KernAux_ELF_Header_is_valid((struct KernAux_ELF_Header*)buffer)); - - fclose(fd); -} +void test_main() {} From bf13320c2f03cf1020cd503188b42a35642c2bac Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Tue, 20 Dec 2022 11:12:30 +0400 Subject: [PATCH 6/6] FreeBSD port (#155) --- .cirrus.yml | 16 ++++++++++++++ pkgs/freebsd/.gitignore | 1 + pkgs/freebsd/devel/libkernaux/Makefile | 18 ++++++++++++++++ pkgs/freebsd/devel/libkernaux/distinfo | 3 +++ pkgs/freebsd/devel/libkernaux/pkg-descr | 3 +++ pkgs/freebsd/devel/libkernaux/pkg-plist | 28 +++++++++++++++++++++++++ 6 files changed, 69 insertions(+) create mode 100644 pkgs/freebsd/.gitignore create mode 100644 pkgs/freebsd/devel/libkernaux/Makefile create mode 100644 pkgs/freebsd/devel/libkernaux/distinfo create mode 100644 pkgs/freebsd/devel/libkernaux/pkg-descr create mode 100644 pkgs/freebsd/devel/libkernaux/pkg-plist diff --git a/.cirrus.yml b/.cirrus.yml index ba5f2662..62fd6488 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -86,3 +86,19 @@ rust_freebsd_task: - ~/.cargo/bin/cargo test - ~/.cargo/bin/cargo clippy - ~/.cargo/bin/cargo fmt --check + +main_freebsd_port_task: + name: Main (FreeBSD port) + only_if: "changesInclude('.cirrus.yml', 'pkgs/freebsd/**')" + dependencies_script: + - pkg install --yes portfmt portlint porttools + port_prepare_script: + - echo 'DEVELOPER=yes' >> /etc/make.conf + - rm -rf /usr/ports/devel/libkernaux/ + - cp -r $CIRRUS_WORKING_DIR/pkgs/freebsd/devel/libkernaux /usr/ports/devel/ + port_test_script: + - cd /usr/ports/devel/libkernaux/ + - portfmt -D Makefile + - portclippy --strict Makefile + - portlint -A + - port test . diff --git a/pkgs/freebsd/.gitignore b/pkgs/freebsd/.gitignore new file mode 100644 index 00000000..caefc693 --- /dev/null +++ b/pkgs/freebsd/.gitignore @@ -0,0 +1 @@ +/devel/libkernaux/work/ diff --git a/pkgs/freebsd/devel/libkernaux/Makefile b/pkgs/freebsd/devel/libkernaux/Makefile new file mode 100644 index 00000000..b3c122a5 --- /dev/null +++ b/pkgs/freebsd/devel/libkernaux/Makefile @@ -0,0 +1,18 @@ +PORTNAME= libkernaux +DISTVERSION= 0.6.1 +CATEGORIES= devel +MASTER_SITES= https://github.com/tailix/libkernaux/releases/download/v${DISTVERSION}/ + +MAINTAINER= kotovalexarian@gmail.com +COMMENT= Auxiliary library for kernel development +WWW= https://github.com/tailix/libkernaux + +LICENSE= MIT +LICENSE_FILE= ${WRKSRC}/COPYING + +USES= libtool +USE_LDCONFIG= yes +GNU_CONFIGURE= yes +CONFIGURE_ARGS+= --enable-shared + +.include diff --git a/pkgs/freebsd/devel/libkernaux/distinfo b/pkgs/freebsd/devel/libkernaux/distinfo new file mode 100644 index 00000000..9afb268d --- /dev/null +++ b/pkgs/freebsd/devel/libkernaux/distinfo @@ -0,0 +1,3 @@ +TIMESTAMP = 1671469243 +SHA256 (libkernaux-0.6.1.tar.gz) = 16fc83a36826cad527ec0a232032b7b131b5be0468ccff9163df12e3e3986b9f +SIZE (libkernaux-0.6.1.tar.gz) = 486595 diff --git a/pkgs/freebsd/devel/libkernaux/pkg-descr b/pkgs/freebsd/devel/libkernaux/pkg-descr new file mode 100644 index 00000000..01173d62 --- /dev/null +++ b/pkgs/freebsd/devel/libkernaux/pkg-descr @@ -0,0 +1,3 @@ +Auxiliary library for kernel development. + +It's in the early stage of the development so there is no detailed description. diff --git a/pkgs/freebsd/devel/libkernaux/pkg-plist b/pkgs/freebsd/devel/libkernaux/pkg-plist new file mode 100644 index 00000000..9be2c859 --- /dev/null +++ b/pkgs/freebsd/devel/libkernaux/pkg-plist @@ -0,0 +1,28 @@ +include/kernaux.h +include/kernaux/arch/i386.h +include/kernaux/arch/riscv64.h +include/kernaux/arch/x86.h +include/kernaux/arch/x86_64.h +include/kernaux/assert.h +include/kernaux/cmdline.h +include/kernaux/elf.h +include/kernaux/free_list.h +include/kernaux/generic/malloc.h +include/kernaux/generic/mutex.h +include/kernaux/macro.h +include/kernaux/macro/packing_end.run +include/kernaux/macro/packing_start.run +include/kernaux/mbr.h +include/kernaux/memmap.h +include/kernaux/multiboot2.h +include/kernaux/multiboot2/header_macro.h +include/kernaux/ntoa.h +include/kernaux/pfa.h +include/kernaux/printf.h +include/kernaux/printf_fmt.h +include/kernaux/units.h +include/kernaux/version.h +lib/libkernaux.a +lib/libkernaux.so +lib/libkernaux.so.0 +lib/libkernaux.so.0.0.0