From 1bdfe67f93faca67ddf7b2eac85593cc72770ca6 Mon Sep 17 00:00:00 2001 From: ling cui Date: Mon, 14 Dec 2020 09:39:30 +0800 Subject: [PATCH] Sample code of protection session Added sample code about how to create/destroy protected session and get FW version --- vendor/intel/Makefile.am | 5 +- vendor/intel/protectedsessiondemo.c | 163 ++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 vendor/intel/protectedsessiondemo.c diff --git a/vendor/intel/Makefile.am b/vendor/intel/Makefile.am index 4ed48dc2..1fdebec4 100644 --- a/vendor/intel/Makefile.am +++ b/vendor/intel/Makefile.am @@ -20,7 +20,7 @@ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -bin_PROGRAMS = avcstreamoutdemo +bin_PROGRAMS = avcstreamoutdemo protectedsessiondemo AM_CPPFLAGS = \ -Wall \ @@ -38,6 +38,9 @@ TEST_LIBS = \ avcstreamoutdemo_LDADD = $(TEST_LIBS) avcstreamoutdemo_SOURCES = avcstreamoutdemo.c +protectedsessiondemo_LDADD = $(TEST_LIBS) +protectedsessiondemo_SOURCES = protectedsessiondemo.c + valgrind: $(bin_PROGRAMS) for a in $(bin_PROGRAMS); do \ valgrind --leak-check=full --show-reachable=yes .libs/$$a; \ diff --git a/vendor/intel/protectedsessiondemo.c b/vendor/intel/protectedsessiondemo.c new file mode 100644 index 00000000..f4d160e4 --- /dev/null +++ b/vendor/intel/protectedsessiondemo.c @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2020 Intel Corporation. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. + * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/* + * it is a real program to show how VAAPI protected session work, + * It does protected session create/destroy, and execute firmware version query command. + * + * ./protectedsessiondemo : execute query and output result. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "va_display.h" + +#define CHECK_VASTATUS(va_status,func) \ +if (va_status != VA_STATUS_SUCCESS) { \ + fprintf(stderr,"%s: failed with error 0x%x, exit\n", func, va_status); \ + exit(1); \ +} + +#define INPUT_SIZE 4 // size of input array for getting fw version +#define OUTPUT_SIZE 23 // size of output array for getting fw version +#define FW_VERSION_OFFSET 9 // offset for fw version +#define EXEC_FUNCID 0x40001000 // function id for query cmd execution + +int main(int argc,char **argv) +{ + int num_entrypoint = 0; + int entrypoint = 0; + VAConfigAttrib attrib = {0}; + VAConfigID config_id = 0; + int major_ver = 0; + int minor_ver = 0; + VADisplay va_dpy = NULL; + VAStatus va_status = VA_STATUS_ERROR_OPERATION_FAILED; + VAProtectedSessionID protected_session = 0xFF; + VABufferID buffer = 0; + VAProtectedSessionExecuteBuffer exec_buff = {0}; + uint32_t input_get_fw[INPUT_SIZE]; + memset(input_get_fw, 0, sizeof(input_get_fw)); + uint32_t output_get_fw[OUTPUT_SIZE]; + memset(output_get_fw, 0, sizeof(output_get_fw)); + VAEntrypoint entrypoints[6]; + memset(entrypoints, 0, sizeof(entrypoints)); + + va_init_display_args(&argc, argv); + va_dpy = va_open_display(); + va_status = vaInitialize(va_dpy, &major_ver, &minor_ver); + CHECK_VASTATUS(va_status, "vaInitialize"); + + va_status = vaQueryConfigEntrypoints( + va_dpy, + VAProfileProtected, + entrypoints, + &num_entrypoint); + CHECK_VASTATUS(va_status, "vaQueryConfigEntrypoints"); + + for(entrypoint = 0; entrypoint < num_entrypoint; entrypoint++) + { + if (entrypoints[entrypoint] == VAEntrypointProtectedTEEComm) + break; + } + if (entrypoint == num_entrypoint) + { + /* not find VAEntrypointProtectedTEEComm entry point */ + assert(0); + } + + /* entrypoint found, find out VAConfigAttribProtectedTeeFwVersion is support */ + attrib.type = VAConfigAttribProtectedTeeFwVersion; + va_status = vaGetConfigAttributes( + va_dpy, + VAProfileProtected, + VAEntrypointProtectedTEEComm, + &attrib, + 1); + CHECK_VASTATUS(va_status, "vaGetConfigAttributes"); + if (!attrib.value) + { + printf("\nAttribute type VAConfigAttribProtectedTeeFwVersion is not supported\n"); + } + + /* create config*/ + va_status = vaCreateConfig( + va_dpy, + VAProfileProtected, + VAEntrypointProtectedTEEComm, + &attrib, + 1, + &config_id); + CHECK_VASTATUS(va_status, "vaCreateConfig"); + + /* create protected session */ + va_status = vaCreateProtectedSession(va_dpy, config_id, &protected_session); + CHECK_VASTATUS(va_status, "vaCreateProtectedSession"); + + /* get fw version */ + /* first create a VA buffer with type VAProtectedSessionExecuteBufferType */ + /* then getting FW version via vaProtectedSessionExecute()*/ + input_get_fw[0] = 0x40002; + input_get_fw[1] = 0x28; + exec_buff.function_id = EXEC_FUNCID; + exec_buff.input.data = input_get_fw; + exec_buff.input.data_size = sizeof(input_get_fw); + exec_buff.output.data = output_get_fw; + exec_buff.output.data_size = sizeof(output_get_fw); + exec_buff.output.max_data_size = sizeof(output_get_fw); + va_status = vaCreateBuffer( + va_dpy, + protected_session, + VAProtectedSessionExecuteBufferType, + sizeof(exec_buff), + 1, + &exec_buff, + &buffer); + CHECK_VASTATUS(va_status, "vaCreateBuffer"); + + va_status = vaProtectedSessionExecute(va_dpy, protected_session, buffer); + CHECK_VASTATUS(va_status, "vaProtectedSessionExecute"); + vaDestroyBuffer(va_dpy, buffer); + + printf("\nFW version 0x%x\n", *(output_get_fw + FW_VERSION_OFFSET)); + + /* destroy protected session */ + va_status = vaDestroyProtectedSession(va_dpy, protected_session); + CHECK_VASTATUS(va_status, "vaDestroyProtectedSession"); + + vaDestroyConfig(va_dpy,config_id); + vaTerminate(va_dpy); + va_close_display(va_dpy); + return 0; +}