blob: f4b55889e290b52e87abc5239f8fca11cfabc623 [file] [log] [blame]
AKASHI Takahiro80bc2762019-01-21 12:13:01 +09001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * efi_selftest_hii
4 *
5 * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited
6 *
7 * Test HII database protocols
8 */
9
10#include <efi_selftest.h>
AKASHI Takahiro80bc2762019-01-21 12:13:01 +090011#include "efi_selftest_hii_data.c"
12
13#define PRINT_TESTNAME efi_st_printf("%s:\n", __func__)
14
15static struct efi_boot_services *boottime;
16
17static const efi_guid_t hii_database_protocol_guid =
18 EFI_HII_DATABASE_PROTOCOL_GUID;
19static const efi_guid_t hii_string_protocol_guid =
20 EFI_HII_STRING_PROTOCOL_GUID;
21
22static struct efi_hii_database_protocol *hii_database_protocol;
23static struct efi_hii_string_protocol *hii_string_protocol;
24
25/*
26 * Setup unit test.
27 *
28 * @handle: handle of the loaded image
29 * @systable: system table
30 *
Heinrich Schuchardtfbe90212022-01-20 19:48:20 +010031 * Return: EFI_ST_SUCCESS for success
AKASHI Takahiro80bc2762019-01-21 12:13:01 +090032 */
33static int setup(const efi_handle_t handle,
34 const struct efi_system_table *systable)
35{
36 efi_status_t ret;
37
38 boottime = systable->boottime;
39
40 /* HII database protocol */
41 ret = boottime->locate_protocol(&hii_database_protocol_guid, NULL,
42 (void **)&hii_database_protocol);
43 if (ret != EFI_SUCCESS) {
44 hii_database_protocol = NULL;
45 efi_st_error("HII database protocol is not available.\n");
46 return EFI_ST_FAILURE;
47 }
48
49 /* HII string protocol */
50 ret = boottime->locate_protocol(&hii_string_protocol_guid, NULL,
51 (void **)&hii_string_protocol);
52 if (ret != EFI_SUCCESS) {
53 hii_string_protocol = NULL;
54 efi_st_error("HII string protocol is not available.\n");
55 return EFI_ST_FAILURE;
56 }
57
58 return EFI_ST_SUCCESS;
59}
60
61/*
62 * HII database protocol tests
63 */
64
65/**
66 * test_hii_database_new_package_list() - test creation and removal of
67 * package list
68 *
69 * This test adds a new package list and then tries to remove it using
70 * the provided handle.
71 *
72 * @Return: status code
73 */
74static int test_hii_database_new_package_list(void)
75{
76 efi_hii_handle_t handle;
77 efi_status_t ret;
78
79 PRINT_TESTNAME;
80 ret = hii_database_protocol->new_package_list(hii_database_protocol,
81 (struct efi_hii_package_list_header *)packagelist1,
82 NULL, &handle);
83 if (ret != EFI_SUCCESS || !handle) {
84 efi_st_error("new_package_list returned %u\n",
85 (unsigned int)ret);
86 return EFI_ST_FAILURE;
87 }
88
89 ret = hii_database_protocol->remove_package_list(hii_database_protocol,
90 handle);
91 if (ret != EFI_SUCCESS) {
92 efi_st_error("remove_package_list returned %u\n",
93 (unsigned int)ret);
94 return EFI_ST_FAILURE;
95 }
96
97 return EFI_ST_SUCCESS;
98}
99
100/**
101 * test_hii_database_update_package_list() - test update of package list
102 *
103 * This test adds a new package list and then tries to update it using
104 * another package list.
105 *
106 * @Return: status code
107 */
108static int test_hii_database_update_package_list(void)
109{
110 efi_hii_handle_t handle = NULL;
111 efi_status_t ret;
112 int result = EFI_ST_FAILURE;
113
114 PRINT_TESTNAME;
115 ret = hii_database_protocol->new_package_list(hii_database_protocol,
116 (struct efi_hii_package_list_header *)packagelist1,
117 NULL, &handle);
118 if (ret != EFI_SUCCESS || !handle) {
119 efi_st_error("new_package_list returned %u\n",
120 (unsigned int)ret);
121 return EFI_ST_FAILURE;
122 }
123
124 ret = hii_database_protocol->update_package_list(hii_database_protocol,
125 handle,
126 (struct efi_hii_package_list_header *)packagelist2);
127 if (ret != EFI_SUCCESS || !handle) {
128 efi_st_error("new_package_list returned %u\n",
129 (unsigned int)ret);
130 goto out;
131 }
132
133 result = EFI_ST_SUCCESS;
134
135out:
136 if (handle) {
137 ret = hii_database_protocol->remove_package_list(
138 hii_database_protocol, handle);
139 if (ret != EFI_SUCCESS) {
140 efi_st_error("remove_package_list returned %u\n",
141 (unsigned int)ret);
142 return EFI_ST_FAILURE;
143 }
144 }
145
146 return result;
147}
148
149/**
150 * test_hii_database_list_package_lists() - test listing of package lists
151 *
152 * This test adds two package lists and then tries to enumerate them
153 * against different package types. We will get an array of handles.
154 *
155 * @Return: status code
156 */
157static int test_hii_database_list_package_lists(void)
158{
159 efi_hii_handle_t handle1 = NULL, handle2 = NULL, *handles;
160 efi_uintn_t handles_size;
161 efi_status_t ret;
162 int result = EFI_ST_FAILURE;
163
164 PRINT_TESTNAME;
165 ret = hii_database_protocol->new_package_list(hii_database_protocol,
166 (struct efi_hii_package_list_header *)packagelist1,
167 NULL, &handle1);
168 if (ret != EFI_SUCCESS || !handle1) {
169 efi_st_error("new_package_list returned %u\n",
170 (unsigned int)ret);
171 goto out;
172 }
173
174 ret = hii_database_protocol->new_package_list(hii_database_protocol,
175 (struct efi_hii_package_list_header *)packagelist2,
176 NULL, &handle2);
177 if (ret != EFI_SUCCESS || !handle2) {
178 efi_st_error("new_package_list returned %u\n",
179 (unsigned int)ret);
180 goto out;
181 }
182
183 /* TYPE_ALL */
184 handles = NULL;
185 handles_size = 0;
186 ret = hii_database_protocol->list_package_lists(hii_database_protocol,
187 EFI_HII_PACKAGE_TYPE_ALL, NULL,
188 &handles_size, handles);
189 if (ret != EFI_BUFFER_TOO_SMALL) {
190 efi_st_error("list_package_lists returned %u\n",
191 (unsigned int)ret);
192 goto out;
193 }
Heinrich Schuchardt9ad046e2019-02-12 21:38:02 +0100194 ret = boottime->allocate_pool(EFI_LOADER_DATA, handles_size,
195 (void **)&handles);
196 if (ret != EFI_SUCCESS) {
197 efi_st_error("AllocatePool failed\n");
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900198 goto out;
199 }
200 ret = hii_database_protocol->list_package_lists(hii_database_protocol,
201 EFI_HII_PACKAGE_TYPE_ALL, NULL,
202 &handles_size, handles);
203 if (ret != EFI_SUCCESS) {
204 efi_st_error("list_package_lists returned %u\n",
205 (unsigned int)ret);
206 goto out;
207 }
Heinrich Schuchardt9ad046e2019-02-12 21:38:02 +0100208 ret = boottime->free_pool(handles);
209 if (ret != EFI_SUCCESS) {
210 efi_st_error("FreePool failed\n");
211 goto out;
212 }
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900213
214 /* STRINGS */
215 handles = NULL;
216 handles_size = 0;
217 ret = hii_database_protocol->list_package_lists(hii_database_protocol,
218 EFI_HII_PACKAGE_STRINGS, NULL,
219 &handles_size, handles);
220 if (ret != EFI_BUFFER_TOO_SMALL) {
221 efi_st_error("list_package_lists returned %u\n",
222 (unsigned int)ret);
223 ret = EFI_ST_FAILURE;
224 goto out;
225 }
Heinrich Schuchardt9ad046e2019-02-12 21:38:02 +0100226 ret = boottime->allocate_pool(EFI_LOADER_DATA, handles_size,
227 (void **)&handles);
228 if (ret != EFI_SUCCESS) {
229 efi_st_error("AllocatePool failed\n");
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900230 ret = EFI_ST_FAILURE;
231 goto out;
232 }
233 ret = hii_database_protocol->list_package_lists(hii_database_protocol,
234 EFI_HII_PACKAGE_STRINGS, NULL,
235 &handles_size, handles);
236 if (ret != EFI_SUCCESS) {
237 efi_st_error("list_package_lists returned %u\n",
238 (unsigned int)ret);
239 ret = EFI_ST_FAILURE;
240 goto out;
241 }
Heinrich Schuchardt9ad046e2019-02-12 21:38:02 +0100242 ret = boottime->free_pool(handles);
243 if (ret != EFI_SUCCESS) {
244 efi_st_error("FreePool failed\n");
245 goto out;
246 }
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900247
248 /* GUID */
249 handles = NULL;
250 handles_size = 0;
251 ret = hii_database_protocol->list_package_lists(hii_database_protocol,
252 EFI_HII_PACKAGE_TYPE_GUID, &package_guid,
253 &handles_size, handles);
254 if (ret != EFI_BUFFER_TOO_SMALL) {
255 efi_st_error("list_package_lists returned %u\n",
256 (unsigned int)ret);
257 ret = EFI_ST_FAILURE;
258 goto out;
259 }
Heinrich Schuchardt9ad046e2019-02-12 21:38:02 +0100260 ret = boottime->allocate_pool(EFI_LOADER_DATA, handles_size,
261 (void **)&handles);
262 if (ret != EFI_SUCCESS) {
263 efi_st_error("AllocatePool failed\n");
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900264 ret = EFI_ST_FAILURE;
265 goto out;
266 }
267 ret = hii_database_protocol->list_package_lists(hii_database_protocol,
268 EFI_HII_PACKAGE_TYPE_GUID, &package_guid,
269 &handles_size, handles);
270 if (ret != EFI_SUCCESS) {
271 efi_st_error("list_package_lists returned %u\n",
272 (unsigned int)ret);
273 ret = EFI_ST_FAILURE;
274 goto out;
275 }
Heinrich Schuchardt9ad046e2019-02-12 21:38:02 +0100276 ret = boottime->free_pool(handles);
277 if (ret != EFI_SUCCESS) {
278 efi_st_error("FreePool failed\n");
279 ret = EFI_ST_FAILURE;
280 goto out;
281 }
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900282
283 /* KEYBOARD_LAYOUT */
284 handles = NULL;
285 handles_size = 0;
286 ret = hii_database_protocol->list_package_lists(hii_database_protocol,
287 EFI_HII_PACKAGE_KEYBOARD_LAYOUT, NULL,
288 &handles_size, handles);
289 if (ret != EFI_BUFFER_TOO_SMALL) {
290 efi_st_error("list_package_lists returned %u\n",
291 (unsigned int)ret);
292 ret = EFI_ST_FAILURE;
293 goto out;
294 }
Heinrich Schuchardt9ad046e2019-02-12 21:38:02 +0100295 ret = boottime->allocate_pool(EFI_LOADER_DATA, handles_size,
296 (void **)&handles);
297 if (ret != EFI_SUCCESS) {
298 efi_st_error("AllocatePool failed\n");
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900299 ret = EFI_ST_FAILURE;
300 goto out;
301 }
302 ret = hii_database_protocol->list_package_lists(hii_database_protocol,
303 EFI_HII_PACKAGE_KEYBOARD_LAYOUT, NULL,
304 &handles_size, handles);
305 if (ret != EFI_SUCCESS) {
306 efi_st_error("list_package_lists returned %u\n",
307 (unsigned int)ret);
308 ret = EFI_ST_FAILURE;
309 goto out;
310 }
Heinrich Schuchardt9ad046e2019-02-12 21:38:02 +0100311 ret = boottime->free_pool(handles);
312 if (ret != EFI_SUCCESS) {
313 efi_st_error("FreePool failed\n");
314 ret = EFI_ST_FAILURE;
315 goto out;
316 }
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900317
318 result = EFI_ST_SUCCESS;
319
320out:
321 if (handle1) {
322 ret = hii_database_protocol->remove_package_list(
323 hii_database_protocol, handle1);
324 if (ret != EFI_SUCCESS)
325 efi_st_error("remove_package_list returned %u\n",
326 (unsigned int)ret);
327 }
328 if (handle2) {
329 ret = hii_database_protocol->remove_package_list(
330 hii_database_protocol, handle2);
331 if (ret != EFI_SUCCESS)
332 efi_st_error("remove_package_list returned %u\n",
333 (unsigned int)ret);
334 }
335
336 return result;
337}
338
339/**
340 * test_hii_database_export_package_lists() - test export of package lists
341 *
342 * @Return: status code
343 */
344static int test_hii_database_export_package_lists(void)
345{
346 PRINT_TESTNAME;
347 /* export_package_lists() not implemented yet */
348 return EFI_ST_SUCCESS;
349}
350
351/**
352 * test_hii_database_register_package_notify() - test registration of
353 * notification function
354 *
355 * @Return: status code
356 */
357static int test_hii_database_register_package_notify(void)
358{
359 PRINT_TESTNAME;
360 /* register_package_notify() not implemented yet */
361 return EFI_ST_SUCCESS;
362}
363
364/**
365 * test_hii_database_unregister_package_notify() - test removal of
366 * notification function
367 *
368 * @Return: status code
369 */
370static int test_hii_database_unregister_package_notify(void)
371{
372 PRINT_TESTNAME;
373 /* unregsiter_package_notify() not implemented yet */
374 return EFI_ST_SUCCESS;
375}
376
377/**
378 * test_hii_database_find_keyboard_layouts() - test listing of
379 * all the keyboard layouts in the system
380 *
381 * This test adds two package lists, each of which has two keyboard layouts
382 * and then tries to enumerate them. We will get an array of handles.
383 *
384 * @Return: status code
385 */
386static int test_hii_database_find_keyboard_layouts(void)
387{
388 efi_hii_handle_t handle1 = NULL, handle2 = NULL;
389 efi_guid_t *guids;
390 u16 guids_size;
391 efi_status_t ret;
392 int result = EFI_ST_FAILURE;
393
394 PRINT_TESTNAME;
395 ret = hii_database_protocol->new_package_list(hii_database_protocol,
396 (struct efi_hii_package_list_header *)packagelist1,
397 NULL, &handle1);
398 if (ret != EFI_SUCCESS || !handle1) {
399 efi_st_error("new_package_list returned %u\n",
400 (unsigned int)ret);
401 goto out;
402 }
403
404 ret = hii_database_protocol->new_package_list(hii_database_protocol,
405 (struct efi_hii_package_list_header *)packagelist2,
406 NULL, &handle2);
407 if (ret != EFI_SUCCESS || !handle2) {
408 efi_st_error("new_package_list returned %u\n",
409 (unsigned int)ret);
410 goto out;
411 }
412
413 guids = NULL;
414 guids_size = 0;
415 ret = hii_database_protocol->find_keyboard_layouts(
416 hii_database_protocol, &guids_size, guids);
417 if (ret != EFI_BUFFER_TOO_SMALL) {
418 efi_st_error("find_keyboard_layouts returned %u\n",
419 (unsigned int)ret);
420 goto out;
421 }
Heinrich Schuchardt9ad046e2019-02-12 21:38:02 +0100422 ret = boottime->allocate_pool(EFI_LOADER_DATA, guids_size,
423 (void **)&guids);
424 if (ret != EFI_SUCCESS) {
425 efi_st_error("AllocatePool failed\n");
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900426 goto out;
427 }
428 ret = hii_database_protocol->find_keyboard_layouts(
429 hii_database_protocol, &guids_size, guids);
430 if (ret != EFI_SUCCESS) {
431 efi_st_error("find_keyboard_layouts returned %u\n",
432 (unsigned int)ret);
433 goto out;
434 }
Heinrich Schuchardt9ad046e2019-02-12 21:38:02 +0100435 ret = boottime->free_pool(guids);
436 if (ret != EFI_SUCCESS) {
437 efi_st_error("FreePool failed\n");
438 goto out;
439 }
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900440
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900441 result = EFI_ST_SUCCESS;
442
443out:
444 if (handle1) {
445 ret = hii_database_protocol->remove_package_list(
446 hii_database_protocol, handle1);
447 if (ret != EFI_SUCCESS)
448 efi_st_error("remove_package_list returned %u\n",
449 (unsigned int)ret);
450 }
451 if (handle2) {
452 ret = hii_database_protocol->remove_package_list(
453 hii_database_protocol, handle2);
454 if (ret != EFI_SUCCESS)
455 efi_st_error("remove_package_list returned %u\n",
456 (unsigned int)ret);
457 }
458
459 return result;
460}
461
462/**
463 * test_hii_database_get_keyboard_layout() - test retrieval of keyboard layout
464 *
465 * This test adds two package lists, each of which has two keyboard layouts
466 * and then tries to get a handle to keyboard layout with a specific guid
467 * and the current one.
468 *
469 * @Return: status code
470 */
471static int test_hii_database_get_keyboard_layout(void)
472{
473 efi_hii_handle_t handle1 = NULL, handle2 = NULL;
474 struct efi_hii_keyboard_layout *kb_layout;
475 u16 kb_layout_size;
476 efi_status_t ret;
477 int result = EFI_ST_FAILURE;
478
479 PRINT_TESTNAME;
480 ret = hii_database_protocol->new_package_list(hii_database_protocol,
481 (struct efi_hii_package_list_header *)packagelist1,
482 NULL, &handle1);
483 if (ret != EFI_SUCCESS || !handle1) {
484 efi_st_error("new_package_list returned %u\n",
485 (unsigned int)ret);
486 goto out;
487 }
488
489 ret = hii_database_protocol->new_package_list(hii_database_protocol,
490 (struct efi_hii_package_list_header *)packagelist2,
491 NULL, &handle2);
492 if (ret != EFI_SUCCESS || !handle2) {
493 efi_st_error("new_package_list returned %u\n",
494 (unsigned int)ret);
495 goto out;
496 }
497
498 /* specific keyboard_layout(guid11) */
499 kb_layout = NULL;
500 kb_layout_size = 0;
501 ret = hii_database_protocol->get_keyboard_layout(hii_database_protocol,
502 &kb_layout_guid11, &kb_layout_size, kb_layout);
503 if (ret != EFI_BUFFER_TOO_SMALL) {
504 efi_st_error("get_keyboard_layout returned %u\n",
505 (unsigned int)ret);
506 goto out;
507 }
Heinrich Schuchardt9ad046e2019-02-12 21:38:02 +0100508 ret = boottime->allocate_pool(EFI_LOADER_DATA, kb_layout_size,
509 (void **)&kb_layout);
510 if (ret != EFI_SUCCESS) {
511 efi_st_error("AllocatePool failed\n");
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900512 goto out;
513 }
514 ret = hii_database_protocol->get_keyboard_layout(hii_database_protocol,
515 &kb_layout_guid11, &kb_layout_size, kb_layout);
516 if (ret != EFI_SUCCESS) {
517 efi_st_error("get_keyboard_layout returned %u\n",
518 (unsigned int)ret);
519 goto out;
520 }
Heinrich Schuchardt9ad046e2019-02-12 21:38:02 +0100521 ret = boottime->free_pool(kb_layout);
522 if (ret != EFI_SUCCESS) {
523 efi_st_error("FreePool failed\n");
524 goto out;
525 }
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900526
527 /* current */
528 kb_layout = NULL;
529 kb_layout_size = 0;
530 ret = hii_database_protocol->get_keyboard_layout(hii_database_protocol,
531 NULL, &kb_layout_size, kb_layout);
532 if (ret != EFI_INVALID_PARAMETER) {
533 efi_st_error("get_keyboard_layout returned %u\n",
534 (unsigned int)ret);
535 goto out;
536 }
537
538 result = EFI_ST_SUCCESS;
539
540out:
541 if (handle1) {
542 ret = hii_database_protocol->remove_package_list(
543 hii_database_protocol, handle1);
544 if (ret != EFI_SUCCESS)
545 efi_st_error("remove_package_list returned %u\n",
546 (unsigned int)ret);
547 }
548 if (handle2) {
549 ret = hii_database_protocol->remove_package_list(
550 hii_database_protocol, handle2);
551 if (ret != EFI_SUCCESS)
552 efi_st_error("remove_package_list returned %u\n",
553 (unsigned int)ret);
554 }
555
556 return result;
557}
558
559/**
560 * test_hii_database_set_keyboard_layout() - test change of
561 * current keyboard layout
562 *
563 * @Return: status code
564 */
565static int test_hii_database_set_keyboard_layout(void)
566{
Vincent Stehlé9160c7e2023-01-06 10:46:41 +0100567 efi_status_t ret;
568
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900569 PRINT_TESTNAME;
Vincent Stehlé9160c7e2023-01-06 10:46:41 +0100570
571 /* Invalid key guid. */
572 ret = hii_database_protocol->set_keyboard_layout(
573 hii_database_protocol, NULL);
574 if (ret != EFI_INVALID_PARAMETER) {
575 efi_st_error("set_keyboard_layout returned %u not invalid\n",
576 (unsigned int)ret);
577 return EFI_ST_FAILURE;
578 }
579
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900580 /* set_keyboard_layout() not implemented yet */
581 return EFI_ST_SUCCESS;
582}
583
584/**
585 * test_hii_database_get_package_list_handle() - test retrieval of
586 * driver associated with a package list
587 *
588 * This test adds a package list, and then tries to get a handle to driver
589 * which is associated with a package list.
590 *
591 * @Return: status code
592 */
593static int test_hii_database_get_package_list_handle(void)
594{
595 efi_hii_handle_t handle = NULL;
596 efi_handle_t driver_handle;
597 efi_status_t ret;
598 int result = EFI_ST_FAILURE;
599
600 PRINT_TESTNAME;
601 driver_handle = (efi_handle_t)0x12345678; /* dummy */
602 ret = hii_database_protocol->new_package_list(hii_database_protocol,
603 (struct efi_hii_package_list_header *)packagelist1,
604 driver_handle, &handle);
605 if (ret != EFI_SUCCESS || !handle) {
606 efi_st_error("new_package_list returned %u\n",
607 (unsigned int)ret);
608 return EFI_ST_FAILURE;
609 }
610
611 driver_handle = NULL;
612 ret = hii_database_protocol->get_package_list_handle(
613 hii_database_protocol, handle, &driver_handle);
614 if (ret != EFI_SUCCESS || driver_handle != (efi_handle_t)0x12345678) {
615 efi_st_error("get_package_list_handle returned %u, driver:%p\n",
616 (unsigned int)ret, driver_handle);
617 goto out;
618 }
619
620 result = EFI_ST_SUCCESS;
621
622out:
623 if (handle) {
624 ret = hii_database_protocol->remove_package_list(
625 hii_database_protocol, handle);
626 if (ret != EFI_SUCCESS) {
627 efi_st_error("remove_package_list returned %u\n",
628 (unsigned int)ret);
629 return EFI_ST_FAILURE;
630 }
631 }
632
633 return result;
634}
635
636static int test_hii_database_protocol(void)
637{
638 int ret;
639
640 ret = test_hii_database_new_package_list();
641 if (ret != EFI_ST_SUCCESS)
642 return EFI_ST_FAILURE;
643
644 ret = test_hii_database_update_package_list();
645 if (ret != EFI_ST_SUCCESS)
646 return EFI_ST_FAILURE;
647
648 ret = test_hii_database_list_package_lists();
649 if (ret != EFI_ST_SUCCESS)
650 return EFI_ST_FAILURE;
651
652 ret = test_hii_database_export_package_lists();
653 if (ret != EFI_ST_SUCCESS)
654 return EFI_ST_FAILURE;
655
656 ret = test_hii_database_register_package_notify();
657 if (ret != EFI_ST_SUCCESS)
658 return EFI_ST_FAILURE;
659
660 ret = test_hii_database_unregister_package_notify();
661 if (ret != EFI_ST_SUCCESS)
662 return EFI_ST_FAILURE;
663
664 ret = test_hii_database_find_keyboard_layouts();
665 if (ret != EFI_ST_SUCCESS)
666 return EFI_ST_FAILURE;
667
668 ret = test_hii_database_get_keyboard_layout();
669 if (ret != EFI_ST_SUCCESS)
670 return EFI_ST_FAILURE;
671
672 ret = test_hii_database_set_keyboard_layout();
673 if (ret != EFI_ST_SUCCESS)
674 return EFI_ST_FAILURE;
675
676 ret = test_hii_database_get_package_list_handle();
677 if (ret != EFI_ST_SUCCESS)
678 return EFI_ST_FAILURE;
679
680 return EFI_ST_SUCCESS;
681}
682
683/*
684 * HII string protocol tests
685 */
686
687/**
688 * test_hii_string_new_string() - test creation of a new string entry
689 *
690 * This test adds a package list, and then tries to add a new string
691 * entry for a specific language.
692 *
693 * @Return: status code
694 */
695static int test_hii_string_new_string(void)
696{
697 efi_hii_handle_t handle = NULL;
698 efi_string_id_t id;
699 efi_status_t ret;
700 int result = EFI_ST_FAILURE;
701
702 PRINT_TESTNAME;
703 ret = hii_database_protocol->new_package_list(hii_database_protocol,
704 (struct efi_hii_package_list_header *)packagelist1,
705 NULL, &handle);
706 if (ret != EFI_SUCCESS || !handle) {
707 efi_st_error("new_package_list returned %u\n",
708 (unsigned int)ret);
709 return EFI_ST_FAILURE;
710 }
711
712 ret = hii_string_protocol->new_string(hii_string_protocol, handle,
713 &id, (u8 *)"en-US",
Simon Glass90975372022-01-23 12:55:12 -0700714 u"Japanese", u"Japanese", NULL);
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900715 if (ret != EFI_SUCCESS) {
716 efi_st_error("new_string returned %u\n",
717 (unsigned int)ret);
718 goto out;
719 }
720 efi_st_printf("new string id is %u\n", id);
721
722 result = EFI_ST_SUCCESS;
723
724out:
725 if (handle) {
726 ret = hii_database_protocol->remove_package_list(
727 hii_database_protocol, handle);
728 if (ret != EFI_SUCCESS) {
729 efi_st_error("remove_package_list returned %u\n",
730 (unsigned int)ret);
731 return EFI_ST_FAILURE;
732 }
733 }
734
735 return result;
736}
737
738/**
739 * test_hii_string_get_string() - test retrieval of a string entry
740 *
741 * This test adds a package list, create a new string entry and then tries
742 * to get it with its string id.
743 *
744 * @Return: status code
745 */
746static int test_hii_string_get_string(void)
747{
748 efi_hii_handle_t handle = NULL;
749 efi_string_id_t id;
750 efi_string_t string;
751 efi_uintn_t string_len;
752 efi_status_t ret;
753 int result = EFI_ST_FAILURE;
754
755 PRINT_TESTNAME;
756 ret = hii_database_protocol->new_package_list(hii_database_protocol,
757 (struct efi_hii_package_list_header *)packagelist1,
758 NULL, &handle);
759 if (ret != EFI_SUCCESS || !handle) {
760 efi_st_error("new_package_list returned %u\n",
761 (unsigned int)ret);
762 return EFI_ST_FAILURE;
763 }
764
765 ret = hii_string_protocol->new_string(hii_string_protocol, handle,
766 &id, (u8 *)"en-US",
Simon Glass90975372022-01-23 12:55:12 -0700767 u"Japanese", u"Japanese", NULL);
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900768 if (ret != EFI_SUCCESS) {
769 efi_st_error("new_string returned %u\n",
770 (unsigned int)ret);
771 goto out;
772 }
773
774 string = NULL;
775 string_len = 0;
776 ret = hii_string_protocol->get_string(hii_string_protocol,
777 (u8 *)"en-US", handle, id, string, &string_len, NULL);
778 if (ret != EFI_BUFFER_TOO_SMALL) {
779 efi_st_error("get_string returned %u\n",
780 (unsigned int)ret);
781 goto out;
782 }
783 string_len += sizeof(u16);
Heinrich Schuchardt9ad046e2019-02-12 21:38:02 +0100784 ret = boottime->allocate_pool(EFI_LOADER_DATA, string_len,
785 (void **)&string);
786 if (ret != EFI_SUCCESS) {
787 efi_st_error("AllocatePool failed\n");
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900788 goto out;
789 }
790 ret = hii_string_protocol->get_string(hii_string_protocol,
791 (u8 *)"en-US", handle, id, string, &string_len, NULL);
792 if (ret != EFI_SUCCESS) {
793 efi_st_error("get_string returned %u\n",
794 (unsigned int)ret);
795 goto out;
796 }
797
Heinrich Schuchardt84a975a2019-03-19 20:08:46 +0100798 if (efi_st_strcmp_16_8(string, "Japanese")) {
799 efi_st_error("get_string returned incorrect string\n");
800 goto out;
801 }
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900802
803 result = EFI_ST_SUCCESS;
804
805out:
806 if (handle) {
807 ret = hii_database_protocol->remove_package_list(
808 hii_database_protocol, handle);
809 if (ret != EFI_SUCCESS) {
810 efi_st_error("remove_package_list returned %u\n",
811 (unsigned int)ret);
812 return EFI_ST_FAILURE;
813 }
814 }
815
816 return result;
817}
818
819/**
820 * test_hii_string_set_string() - test change of a string entry
821 *
822 * This test adds a package list, create a new string entry and then tries
823 * to modify it.
824 *
825 * @Return: status code
826 */
827static int test_hii_string_set_string(void)
828{
829 efi_hii_handle_t handle = NULL;
830 efi_string_id_t id;
831 efi_status_t ret;
832 int result = EFI_ST_FAILURE;
833
834 PRINT_TESTNAME;
835 ret = hii_database_protocol->new_package_list(hii_database_protocol,
836 (struct efi_hii_package_list_header *)packagelist1,
837 NULL, &handle);
838 if (ret != EFI_SUCCESS || !handle) {
839 efi_st_error("new_package_list returned %u\n",
840 (unsigned int)ret);
841 return EFI_ST_FAILURE;
842 }
843
844 ret = hii_string_protocol->new_string(hii_string_protocol, handle,
845 &id, (u8 *)"en-US",
Simon Glass90975372022-01-23 12:55:12 -0700846 u"Japanese", u"Japanese", NULL);
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900847 if (ret != EFI_SUCCESS) {
848 efi_st_error("new_string returned %u\n",
849 (unsigned int)ret);
850 goto out;
851 }
852
853 ret = hii_string_protocol->set_string(hii_string_protocol, handle,
854 id, (u8 *)"en-US",
Simon Glass90975372022-01-23 12:55:12 -0700855 u"Nihongo", NULL);
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900856 if (ret != EFI_SUCCESS) {
857 efi_st_error("set_string returned %u\n",
858 (unsigned int)ret);
859 goto out;
860 }
861
862 result = EFI_ST_SUCCESS;
863
864out:
865 if (handle) {
866 ret = hii_database_protocol->remove_package_list(
867 hii_database_protocol, handle);
868 if (ret != EFI_SUCCESS) {
869 efi_st_error("remove_package_list returned %u\n",
870 (unsigned int)ret);
871 return EFI_ST_FAILURE;
872 }
873 }
874
875 return result;
876}
877
878/**
879 * test_hii_string_get_languages() - test listing of languages
880 *
881 * This test adds a package list, and then tries to enumerate languages
882 * in it. We will get an string of language names.
883 *
884 * @Return: status code
885 */
886static int test_hii_string_get_languages(void)
887{
888 efi_hii_handle_t handle = NULL;
889 u8 *languages;
890 efi_uintn_t languages_len;
891 efi_status_t ret;
892 int result = EFI_ST_FAILURE;
893
894 PRINT_TESTNAME;
895 ret = hii_database_protocol->new_package_list(hii_database_protocol,
896 (struct efi_hii_package_list_header *)packagelist1,
897 NULL, &handle);
898 if (ret != EFI_SUCCESS || !handle) {
899 efi_st_error("new_package_list returned %u\n",
900 (unsigned int)ret);
901 return EFI_ST_FAILURE;
902 }
903
904 languages = NULL;
905 languages_len = 0;
906 ret = hii_string_protocol->get_languages(hii_string_protocol, handle,
907 languages, &languages_len);
908 if (ret != EFI_BUFFER_TOO_SMALL) {
909 efi_st_error("get_languages returned %u\n",
910 (unsigned int)ret);
911 goto out;
912 }
Heinrich Schuchardt9ad046e2019-02-12 21:38:02 +0100913 ret = boottime->allocate_pool(EFI_LOADER_DATA, languages_len,
914 (void **)&languages);
915 if (ret != EFI_SUCCESS) {
916 efi_st_error("AllocatePool failed\n");
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900917 goto out;
918 }
919 ret = hii_string_protocol->get_languages(hii_string_protocol, handle,
920 languages, &languages_len);
921 if (ret != EFI_SUCCESS) {
922 efi_st_error("get_languages returned %u\n",
923 (unsigned int)ret);
924 goto out;
925 }
926
927 efi_st_printf("got languages are %s\n", languages);
928
929 result = EFI_ST_SUCCESS;
930
931out:
932 if (handle) {
933 ret = hii_database_protocol->remove_package_list(
934 hii_database_protocol, handle);
935 if (ret != EFI_SUCCESS) {
936 efi_st_error("remove_package_list returned %u\n",
937 (unsigned int)ret);
938 return EFI_ST_FAILURE;
939 }
940 }
941
942 return result;
943}
944
945/**
946 * test_hii_string_get_secondary_languages() - test listing of secondary
947 * languages
948 *
949 * This test adds a package list, and then tries to enumerate secondary
950 * languages with a specific language. We will get an string of language names.
951 *
952 * @Return: status code
953 */
954static int test_hii_string_get_secondary_languages(void)
955{
956 efi_hii_handle_t handle = NULL;
957 u8 *languages;
958 efi_uintn_t languages_len;
959 efi_status_t ret;
960 int result = EFI_ST_FAILURE;
961
962 PRINT_TESTNAME;
963 ret = hii_database_protocol->new_package_list(hii_database_protocol,
964 (struct efi_hii_package_list_header *)packagelist1,
965 NULL, &handle);
966 if (ret != EFI_SUCCESS || !handle) {
967 efi_st_error("new_package_list returned %u\n",
968 (unsigned int)ret);
969 return EFI_ST_FAILURE;
970 }
971
972 languages = NULL;
973 languages_len = 0;
974 ret = hii_string_protocol->get_secondary_languages(hii_string_protocol,
975 handle, (u8 *)"en-US", languages, &languages_len);
976 if (ret == EFI_NOT_FOUND) {
977 efi_st_printf("no secondary languages\n");
978 result = EFI_ST_SUCCESS;
979 goto out;
980 }
981 if (ret != EFI_BUFFER_TOO_SMALL) {
982 efi_st_error("get_secondary_languages returned %u\n",
983 (unsigned int)ret);
984 goto out;
985 }
Heinrich Schuchardt9ad046e2019-02-12 21:38:02 +0100986 ret = boottime->allocate_pool(EFI_LOADER_DATA, languages_len,
987 (void **)&languages);
988 if (ret != EFI_SUCCESS) {
989 efi_st_error("AllocatePool failed\n");
AKASHI Takahiro80bc2762019-01-21 12:13:01 +0900990 goto out;
991 }
992 ret = hii_string_protocol->get_secondary_languages(hii_string_protocol,
993 handle, (u8 *)"en-US", languages, &languages_len);
994 if (ret != EFI_SUCCESS) {
995 efi_st_error("get_secondary_languages returned %u\n",
996 (unsigned int)ret);
997 goto out;
998 }
999
1000 efi_st_printf("got secondary languages are %s\n", languages);
1001
1002 result = EFI_ST_SUCCESS;
1003
1004out:
1005 if (handle) {
1006 ret = hii_database_protocol->remove_package_list(
1007 hii_database_protocol, handle);
1008 if (ret != EFI_SUCCESS) {
1009 efi_st_error("remove_package_list returned %u\n",
1010 (unsigned int)ret);
1011 return EFI_ST_FAILURE;
1012 }
1013 }
1014
1015 return result;
1016}
1017
1018static int test_hii_string_protocol(void)
1019{
1020 int ret;
1021
1022 ret = test_hii_string_new_string();
1023 if (ret != EFI_ST_SUCCESS)
1024 return EFI_ST_FAILURE;
1025
1026 ret = test_hii_string_get_string();
1027 if (ret != EFI_ST_SUCCESS)
1028 return EFI_ST_FAILURE;
1029
1030 ret = test_hii_string_set_string();
1031 if (ret != EFI_ST_SUCCESS)
1032 return EFI_ST_FAILURE;
1033
1034 ret = test_hii_string_get_languages();
1035 if (ret != EFI_ST_SUCCESS)
1036 return EFI_ST_FAILURE;
1037
1038 ret = test_hii_string_get_secondary_languages();
1039 if (ret != EFI_ST_SUCCESS)
1040 return EFI_ST_FAILURE;
1041
1042 return EFI_ST_SUCCESS;
1043}
1044
1045/*
1046 * Execute unit test.
1047 *
Heinrich Schuchardtfbe90212022-01-20 19:48:20 +01001048 * Return: EFI_ST_SUCCESS for success, EFI_ST_FAILURE for failure
AKASHI Takahiro80bc2762019-01-21 12:13:01 +09001049 */
1050static int execute(void)
1051{
1052 int ret;
1053
1054 /* HII database protocol */
1055 ret = test_hii_database_protocol();
1056 if (ret != EFI_ST_SUCCESS)
1057 return EFI_ST_FAILURE;
1058
1059 /* HII string protocol */
1060 ret = test_hii_string_protocol();
1061 if (ret != EFI_ST_SUCCESS)
1062 return EFI_ST_FAILURE;
1063
1064 return EFI_ST_SUCCESS;
1065}
1066
1067EFI_UNIT_TEST(hii) = {
1068 .name = "HII database protocols",
1069 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
1070 .setup = setup,
1071 .execute = execute,
1072};