1 /*
2  * Copyright 2023 Yonatan Schachter
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/bindesc.h>
9 #include <zephyr/drivers/flash.h>
10 
11 #ifdef CONFIG_ARCH_POSIX
12 #define SOC_NV_FLASH_NODE DT_CHILD(DT_INST(0, zephyr_sim_flash), flash_0)
13 #else
14 #define SOC_NV_FLASH_NODE DT_CHILD(DT_INST(0, zephyr_sim_flash), flash_sim_0)
15 #endif /* CONFIG_ARCH_POSIX */
16 
17 #if (defined(CONFIG_ARCH_POSIX) || defined(CONFIG_BOARD_QEMU_X86))
18 static const struct device *const flash_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller));
19 #else
20 static const struct device *const flash_dev = DEVICE_DT_GET(DT_NODELABEL(sim_flash_controller));
21 #endif
22 
23 #define FLASH_SIMULATOR_ERASE_UNIT DT_PROP(SOC_NV_FLASH_NODE, erase_block_size)
24 
25 static __aligned(BINDESC_ALIGNMENT) const uint8_t descriptors[] = {
26 	0x46, 0x60, 0xa4, 0x7e, 0x5a, 0x3e, 0x86, 0xb9, /* magic */
27 	0x00, 0x18, 0x06, 0x00, /* tag: 0x1800 (app version string), length: 0x0006 */
28 	0x31, 0x2e, 0x30, 0x2e, 0x30, 0x00, /* "1.0.0" */
29 	0x00, 0x00, /* padding */
30 	0x01, 0x1b, 0x04, 0x00, /* tag: 0x1b01 (compiler name), length: 0x0004 */
31 	0x47, 0x4e, 0x55, 0x00, /* "GNU" */
32 	0x02, 0x1b, 0x07, 0x00, /* tag: 0x1b02 (compiler version), length: 0x0007 */
33 	0x31, 0x32, 0x2e, 0x32, 0x2e, 0x30, 0x00, /* "12.2.0" */
34 	0x00, /* padding */
35 	0x00, 0x19, 0x07, 0x00, /* tag: 0x1900 (kernel version string), length: 0x0007 */
36 	0x33, 0x2e, 0x35, 0x2e, 0x39, 0x39, 0x00, /* "3.5.99" */
37 	0x00, /* padding */
38 	0xff, 0xff, 0x00, 0x00, /* tag: 0xffff (descriptors end), length: 0x0000 */
39 };
40 
test_setup(void)41 static void *test_setup(void)
42 {
43 	flash_erase(flash_dev, 0, FLASH_SIMULATOR_ERASE_UNIT);
44 	flash_write(flash_dev, 0, descriptors, sizeof(descriptors));
45 
46 	return NULL;
47 }
48 
test_bindesc_read(struct bindesc_handle * handle)49 static void test_bindesc_read(struct bindesc_handle *handle)
50 {
51 	const char *result;
52 
53 	bindesc_find_str(handle, BINDESC_ID_KERNEL_VERSION_STRING, &result);
54 	zassert_mem_equal("3.5.99", result, sizeof("3.5.99"));
55 
56 	bindesc_find_str(handle, BINDESC_ID_APP_VERSION_STRING, &result);
57 	zassert_mem_equal("1.0.0", result, sizeof("1.0.0"));
58 
59 	bindesc_find_str(handle, BINDESC_ID_C_COMPILER_NAME, &result);
60 	zassert_mem_equal("GNU", result, sizeof("GNU"));
61 
62 	bindesc_find_str(handle, BINDESC_ID_C_COMPILER_VERSION, &result);
63 	zassert_mem_equal("12.2.0", result, sizeof("12.2.0"));
64 }
65 
ZTEST(bindesc_read,test_bindesc_read_from_flash)66 ZTEST(bindesc_read, test_bindesc_read_from_flash)
67 {
68 	struct bindesc_handle handle;
69 
70 	bindesc_open_flash(&handle, 0, flash_dev);
71 
72 	test_bindesc_read(&handle);
73 }
74 
ZTEST(bindesc_read,test_bindesc_read_from_ram)75 ZTEST(bindesc_read, test_bindesc_read_from_ram)
76 {
77 	struct bindesc_handle handle;
78 
79 	bindesc_open_ram(&handle, descriptors, sizeof(descriptors));
80 
81 	test_bindesc_read(&handle);
82 }
83 
84 ZTEST_SUITE(bindesc_read, NULL, test_setup, NULL, NULL, NULL);
85