blob: d00e9563a472dd0453cdc5348cf97cce7515c0b3 [file] [log] [blame]
Abdellatif El Khlifi10a69ce2023-08-04 14:33:39 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Functional tests for UCLASS_FFA class
4 *
5 * Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
6 *
7 * Authors:
8 * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
9 */
10
Caleb Connolly55ce3e92024-08-30 13:34:40 +010011#include <charset.h>
Caleb Connolly29cab7c2024-08-30 13:34:37 +010012#include <u-boot/uuid.h>
Abdellatif El Khlifi10a69ce2023-08-04 14:33:39 +010013#include <test/lib.h>
14#include <test/test.h>
15#include <test/ut.h>
16
Caleb Connolly55ce3e92024-08-30 13:34:40 +010017#include <efi.h>
18
Abdellatif El Khlifi10a69ce2023-08-04 14:33:39 +010019/* test UUID */
20#define TEST_SVC_UUID "ed32d533-4209-99e6-2d72-cdd998a79cc0"
Caleb Connolly55ce3e92024-08-30 13:34:40 +010021/* U-Boot default fw image namespace */
22#define DEFAULT_FW_IMAGE_NAMESPACE "8c9f137e-91dc-427b-b2d6-b420faebaf2a"
Abdellatif El Khlifi10a69ce2023-08-04 14:33:39 +010023
24#define UUID_SIZE 16
25
26/* The UUID binary data (little-endian format) */
27static const u8 ref_uuid_bin[UUID_SIZE] = {
28 0x33, 0xd5, 0x32, 0xed,
29 0x09, 0x42, 0xe6, 0x99,
30 0x72, 0x2d, 0xc0, 0x9c,
31 0xa7, 0x98, 0xd9, 0xcd
32};
33
34static int lib_test_uuid_to_le(struct unit_test_state *uts)
35{
36 const char *uuid_str = TEST_SVC_UUID;
37 u8 ret_uuid_bin[UUID_SIZE] = {0};
38
39 ut_assertok(uuid_str_to_le_bin(uuid_str, ret_uuid_bin));
40 ut_asserteq_mem(ref_uuid_bin, ret_uuid_bin, UUID_SIZE);
41
42 return 0;
43}
Abdellatif El Khlifi10a69ce2023-08-04 14:33:39 +010044LIB_TEST(lib_test_uuid_to_le, 0);
Caleb Connolly55ce3e92024-08-30 13:34:40 +010045
Caleb Connollyde6f1dd2024-08-30 13:34:41 +010046#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
47/* Test UUID attribute bits (version, variant) */
48static int lib_test_uuid_bits(struct unit_test_state *uts)
49{
50 unsigned char uuid[16];
51 efi_guid_t guid;
52 int i;
53
54 /*
55 * Reduce the chance of a randomly generated UUID disguising
56 * a regression by testing multiple times.
57 */
58 for (i = 0; i < 5; i++) {
59 /* Test UUID v4 */
60 gen_rand_uuid((unsigned char *)&uuid);
61
62 printf("v4 UUID: %pUb\n", (efi_guid_t *)uuid);
63
64 /* version 4 */
65 ut_assert((uuid[6] & 0xf0) == 0x40);
66 /* variant 1 */
67 ut_assert((uuid[8] & UUID_VARIANT_MASK) == (UUID_VARIANT << UUID_VARIANT_SHIFT));
68
69 /* Test v5, use the v4 UUID as the namespace */
70 gen_v5_guid((struct uuid *)uuid,
71 &guid, "test", 4, NULL);
72
73 printf("v5 GUID: %pUl\n", (efi_guid_t *)uuid);
74
75 /* This is a GUID so bits 6 and 7 are swapped (little endian). Version 5 */
76 ut_assert((guid.b[7] & 0xf0) == 0x50);
77 /* variant 1 */
78 ut_assert((guid.b[8] & UUID_VARIANT_MASK) == (UUID_VARIANT << UUID_VARIANT_SHIFT));
79 }
80
81 return 0;
82}
83
84LIB_TEST(lib_test_uuid_bits, 0);
85#endif
86
Caleb Connolly55ce3e92024-08-30 13:34:40 +010087struct dynamic_uuid_test_data {
88 const char *compatible;
89 const u16 *images[4];
90 const char *expected_uuids[4];
91};
92
93static int lib_test_dynamic_uuid_case(struct unit_test_state *uts,
94 const struct dynamic_uuid_test_data *data)
95{
96 struct uuid namespace;
97 int j;
98
99 ut_assertok(uuid_str_to_bin(DEFAULT_FW_IMAGE_NAMESPACE, (unsigned char *)&namespace,
100 UUID_STR_FORMAT_GUID));
101
102 for (j = 0; data->images[j]; j++) {
103 const char *expected_uuid = data->expected_uuids[j];
104 const u16 *image = data->images[j];
105 efi_guid_t uuid;
106 char uuid_str[37];
107
108 gen_v5_guid(&namespace, &uuid,
109 data->compatible, strlen(data->compatible),
110 image, u16_strlen(image) * sizeof(uint16_t),
111 NULL);
112 uuid_bin_to_str((unsigned char *)&uuid, uuid_str, UUID_STR_FORMAT_GUID);
113
114 ut_asserteq_str(expected_uuid, uuid_str);
115 }
116
117 return 0;
118}
119
120static int lib_test_dynamic_uuid(struct unit_test_state *uts)
121{
122 int ret, i;
123 const struct dynamic_uuid_test_data test_data[] = {
124 {
125 .compatible = "sandbox",
126 .images = {
127 u"SANDBOX-UBOOT",
128 u"SANDBOX-UBOOT-ENV",
129 u"SANDBOX-FIT",
130 NULL,
131 },
132 .expected_uuids = {
133 "985f2937-7c2e-5e9a-8a5e-8e063312964b",
134 "9e339473-c2eb-530a-a69b-0cd6bbbed40e",
135 "46610520-469e-59dc-a8dd-c11832b877ea",
136 NULL,
137 }
138 },
139 {
140 .compatible = "qcom,qrb4210-rb2",
141 .images = {
142 u"QUALCOMM-UBOOT",
143 NULL,
144 },
145 .expected_uuids = {
146 "d5021fac-8dd0-5ed7-90c2-763c304aaf86",
147 NULL,
148 }
149 },
150 };
151
152 for (i = 0; i < ARRAY_SIZE(test_data); i++) {
153 ret = lib_test_dynamic_uuid_case(uts, &test_data[i]);
154 if (ret)
155 return ret;
156 }
157
158 return 0;
159}
160
161LIB_TEST(lib_test_dynamic_uuid, 0);