1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <zephyr/ztest.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/settings/settings.h>
12 #include <bootutil/boot_status.h>
13 #include <bootutil/image.h>
14 #include <zephyr/mcuboot_version.h>
15 
16 #define FLASH_SECTOR_SIZE 1024
17 #define FLASH_SECTOR_SIZE_KB 4
18 #define FLASH_MAX_APP_SECTORS 34
19 #define FLASH_RESERVED_SECTORS 1
20 #define FLASH_MAX_APP_SIZE ((FLASH_MAX_APP_SECTORS - FLASH_RESERVED_SECTORS) \
21 			    * FLASH_SECTOR_SIZE_KB)
22 #define RUNNING_SLOT 0
23 
ZTEST(mcuboot_shared_data,test_mode)24 ZTEST(mcuboot_shared_data, test_mode)
25 {
26 	uint8_t var[1];
27 	int rc;
28 
29 	memset(var, 0xff, sizeof(var));
30 	rc = settings_runtime_get("blinfo/mode", var, sizeof(var));
31 	zassert_equal(rc, sizeof(var), "Expected data length mismatch");
32 	zassert_equal(var[0], MCUBOOT_MODE_SWAP_USING_MOVE, "Expected data mismatch");
33 }
34 
ZTEST(mcuboot_shared_data,test_signature_type)35 ZTEST(mcuboot_shared_data, test_signature_type)
36 {
37 	uint8_t var[1];
38 	int rc;
39 
40 	memset(var, 0xff, sizeof(var));
41 	rc = settings_runtime_get("blinfo/signature_type", var, sizeof(var));
42 	zassert_equal(rc, sizeof(var), "Expected data length mismatch");
43 	zassert_equal(var[0], MCUBOOT_SIGNATURE_TYPE_RSA, "Expected data mismatch");
44 }
45 
ZTEST(mcuboot_shared_data,test_recovery)46 ZTEST(mcuboot_shared_data, test_recovery)
47 {
48 	uint8_t var[1];
49 	int rc;
50 
51 	memset(var, 0xff, sizeof(var));
52 	rc = settings_runtime_get("blinfo/recovery", var, sizeof(var));
53 	zassert_equal(rc, sizeof(var), "Expected data length mismatch");
54 	zassert_equal(var[0], MCUBOOT_RECOVERY_MODE_NONE, "Expected data mismatch");
55 }
56 
ZTEST(mcuboot_shared_data,test_running_slot)57 ZTEST(mcuboot_shared_data, test_running_slot)
58 {
59 	uint8_t var[1];
60 	int rc;
61 
62 	memset(var, 0xff, sizeof(var));
63 	rc = settings_runtime_get("blinfo/running_slot", var, sizeof(var));
64 	zassert_equal(rc, sizeof(var), "Expected data length mismatch");
65 	zassert_equal(var[0], RUNNING_SLOT, "Expected data mismatch");
66 }
67 
ZTEST(mcuboot_shared_data,test_bootloader_version)68 ZTEST(mcuboot_shared_data, test_bootloader_version)
69 {
70 	uint8_t var[8];
71 	int rc;
72 	struct image_version *version = (void *)var;
73 
74 	memset(var, 0xff, sizeof(var));
75 	rc = settings_runtime_get("blinfo/bootloader_version", var, sizeof(var));
76 	zassert_equal(rc, sizeof(var), "Expected data length mismatch");
77 
78 	zassert_equal(version->iv_major, MCUBOOT_VERSION_MAJOR,
79 		      "Expected version (major) mismatch");
80 	zassert_equal(version->iv_minor, MCUBOOT_VERSION_MINOR,
81 		      "Expected version (minor) mismatch");
82 	zassert_equal(version->iv_revision, MCUBOOT_PATCHLEVEL,
83 		      "Expected version (patch level) mismatch");
84 	zassert_equal(version->iv_build_num, 0, "Expected version (build number) mismatch");
85 }
86 
ZTEST(mcuboot_shared_data,test_max_application_size)87 ZTEST(mcuboot_shared_data, test_max_application_size)
88 {
89 	uint8_t var[4];
90 	uint32_t value;
91 	int rc;
92 
93 	memset(var, 0xff, sizeof(var));
94 	rc = settings_runtime_get("blinfo/max_application_size", var, sizeof(var));
95 	zassert_equal(rc, sizeof(var), "Expected data length mismatch");
96 	memcpy(&value, var, sizeof(value));
97 	value /= FLASH_SECTOR_SIZE;
98 	zassert_equal(value, FLASH_MAX_APP_SIZE, "Expected data mismatch");
99 }
100 
ZTEST(mcuboot_shared_data,test_invalid)101 ZTEST(mcuboot_shared_data, test_invalid)
102 {
103 	uint8_t var[4];
104 	int rc;
105 
106 	memset(var, 0xff, sizeof(var));
107 	rc = settings_runtime_get("blinfo/does_not_exist", var, sizeof(var));
108 	zassert_not_equal(rc, sizeof(var), "Expected data length (error) mismatch");
109 	zassert_not_equal(rc, 0, "Expected data length (error) mismatch");
110 }
111 
ZTEST(mcuboot_shared_data,test_bootloader_version_limited)112 ZTEST(mcuboot_shared_data, test_bootloader_version_limited)
113 {
114 	uint8_t var[2];
115 	int rc;
116 
117 	memset(var, 0xff, sizeof(var));
118 	rc = settings_runtime_get("blinfo/bootloader_version", var, sizeof(var));
119 	zassert_not_equal(rc, sizeof(var), "Expected data length mismatch");
120 }
121 
122 ZTEST_SUITE(mcuboot_shared_data, NULL, NULL, NULL, NULL, NULL);
123