diff options
author | Angelos Mouzakitis <a.mouzakitis@virtualopensystems.com> | 2023-10-10 14:33:42 +0000 |
---|---|---|
committer | Angelos Mouzakitis <a.mouzakitis@virtualopensystems.com> | 2023-10-10 14:33:42 +0000 |
commit | af1a266670d040d2f4083ff309d732d648afba2a (patch) | |
tree | 2fc46203448ddcc6f81546d379abfaeb323575e9 /dtc/tests/check_header.c | |
parent | e02cda008591317b1625707ff8e115a4841aa889 (diff) |
Change-Id: Iaf8d18082d3991dec7c0ebbea540f092188eb4ec
Diffstat (limited to 'dtc/tests/check_header.c')
-rw-r--r-- | dtc/tests/check_header.c | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/dtc/tests/check_header.c b/dtc/tests/check_header.c new file mode 100644 index 000000000..ca26ec106 --- /dev/null +++ b/dtc/tests/check_header.c @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/* + * libfdt - Flat Device Tree manipulation + * Testcase for fdt_check_header + * Copyright (C) 2018 David Gibson + */ + +#include <stdio.h> + +#include <libfdt.h> + +#include "tests.h" + +static void *dtdup(void *dt) +{ + size_t bufsize = fdt_totalsize(dt); + void *buf = xmalloc(bufsize); + fdt_move(dt, buf, bufsize); + return buf; +} + +#define CHECK_MANGLE(exerr, code) \ + do { \ + void *fdt = dtdup(template); \ + { code } \ + err = fdt_check_header(fdt); \ + verbose_printf("\"%s\" => %s\n", #code, fdt_strerror(err)); \ + if (err != (exerr)) \ + FAIL("fdt_check_header() didn't catch mangle %s", \ + #code); \ + free(fdt); \ + } while (0) + +int main(int argc, char *argv[]) +{ + void *template; + int err; + + test_init(argc, argv); + template = load_blob(argv[1]); + + /* Check that the base dt is valid before mangling it */ + err = fdt_check_header(template); + if (err != 0) + FAIL("Base tree fails: %s", fdt_strerror(err)); + + /* Check a no-op mangle doesn't break things */ + CHECK_MANGLE(0, ; ); + + /* Mess up the magic number */ + CHECK_MANGLE(-FDT_ERR_BADMAGIC, + fdt_set_magic(fdt, fdt_magic(fdt) ^ 0x1); + ); + CHECK_MANGLE(-FDT_ERR_BADMAGIC, + fdt_set_magic(fdt, fdt_magic(fdt) ^ 0x80000000); + ); + + /* Mess up the version */ + CHECK_MANGLE(-FDT_ERR_BADVERSION, + fdt_set_version(fdt, FDT_FIRST_SUPPORTED_VERSION - 1); + fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION - 1); + ); + CHECK_MANGLE(-FDT_ERR_BADVERSION, + fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION + 1); + fdt_set_last_comp_version(fdt, FDT_LAST_SUPPORTED_VERSION + 1); + ); + CHECK_MANGLE(-FDT_ERR_BADVERSION, + fdt_set_version(fdt, FDT_FIRST_SUPPORTED_VERSION); + fdt_set_last_comp_version(fdt, FDT_LAST_SUPPORTED_VERSION); + ); + + /* Out of bounds sizes */ + CHECK_MANGLE(-FDT_ERR_TRUNCATED, + fdt_set_totalsize(fdt, FDT_V1_SIZE - 1); + ); + CHECK_MANGLE(-FDT_ERR_TRUNCATED, + fdt_set_totalsize(fdt, (uint32_t)INT_MAX + 1); + ); + + /* Truncate within various blocks */ + CHECK_MANGLE(-FDT_ERR_TRUNCATED, + fdt_set_totalsize(fdt, fdt_off_dt_struct(fdt) - 1); + ); + CHECK_MANGLE(-FDT_ERR_TRUNCATED, + fdt_set_totalsize(fdt, fdt_off_dt_strings(fdt) - 1); + ); + CHECK_MANGLE(-FDT_ERR_TRUNCATED, + fdt_set_totalsize(fdt, fdt_off_mem_rsvmap(fdt) - 1); + ); + CHECK_MANGLE(-FDT_ERR_TRUNCATED, + fdt_set_totalsize(fdt, fdt_off_dt_struct(fdt) + 1); + ); + CHECK_MANGLE(-FDT_ERR_TRUNCATED, + fdt_set_totalsize(fdt, fdt_off_dt_strings(fdt) + 1); + ); + CHECK_MANGLE(-FDT_ERR_TRUNCATED, + fdt_set_totalsize(fdt, fdt_off_mem_rsvmap(fdt) + 1); + ); + + /* Negative block sizes */ + CHECK_MANGLE(-FDT_ERR_TRUNCATED, + fdt_set_size_dt_struct(fdt, (uint32_t)-1); + ); + CHECK_MANGLE(-FDT_ERR_TRUNCATED, + fdt_set_size_dt_strings(fdt, (uint32_t)-1); + ); + CHECK_MANGLE(-FDT_ERR_TRUNCATED, + fdt_set_size_dt_struct(fdt, (uint32_t)INT_MIN); + ); + CHECK_MANGLE(-FDT_ERR_TRUNCATED, + fdt_set_size_dt_strings(fdt, (uint32_t)INT_MIN); + ); + + PASS(); +} |