This example shows how to print "Hello World!"
to the appropriate port using the UART driver.
The first UART port is used for printing. When running the example simulation (the target sim
that is used by default), -serial stdio
is indicated in the QEMU flags. This means that the first UART port will be printed only to the standard stream of the host machine.
EDL descriptions of entities
Client.edl
entity client.Client
Init description
The client entity does not call other entities when the UART driver is used.
This example contains two init description files:
init_qemu.yaml.in
for running in the QEMU emulator.init_hw.yaml.in
for running on devices.init_qemu.yaml.in
entities:
- name: client.Client
path: client_qemu
@INIT_client_qemu_ENTITY_CONNECTIONS@
@INIT_EXTERNAL_ENTITIES@
init_hw.yaml.in
entities:
- name: client.Client
path: client_hw
@INIT_client_hw_ENTITY_CONNECTIONS@
@INIT_EXTERNAL_ENTITIES@
Security configuration
The security configuration in this example allows all entities to run and allows interaction with the Core
entity.
security.psl
/* Security configuration of the "uart" example. */
/* Define execute interface. */
execute: kl.core.Execute
/* Import the file with the declaration of basic security policy aliases. */
use nk.base._
/* Declaration of entities. */
use EDL Einit
use EDL kl.core.Core
use EDL kl.drivers.UART
use EDL client.Client
/* Startup of entities is allowed. */
execute {
grant ()
}
/* Request messages are allowed */
request {
grant ()
}
/* Response messages are allowed */
response {
grant ()
}
/* Calls to the security interface are ignored. */
security {
grant ()
}
Implementation of the client entity
uart.c
#include <coresrv/syscalls.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <uart/uart.h>
int main(void)
{
static const char str[] = "Hello world!\n";
/* First UART port. */
UartHandle uartH = 0;
/* NOTE: calling printf will not print anything because
vfs is not used in this example. */
printf(str);
/* Initializes the UART driver. */
int rc = UartInit();
if (rc != UART_OK) {
printk("UartInit() failed\n");
return EXIT_FAILURE;
}
/* Opens the port. */
rc = UartOpenPort(PORT_NAME, &uartH);
if (rc != UART_OK) {
printk("UartOpenPort() failed\n");
return EXIT_FAILURE;
}
int i;
/* Prints the passed string to the UART port. */
for (i = 0; i < sizeof(str) - 1; ++i)
UartWriteByte(uartH, (uint8_t)str[i]);
return EXIT_SUCCESS;
}
Solution build instructions
The CMake build system is used in this example.
EDL descriptions of the Client
entity are handled by using the nk_build_edl_files
function provided by NK.
Client entity build instructions
client/CMakeLists.txt
project (client)
# Tools for using the NK parser.
include (platform/nk)
nk_build_edl_files (client_edl_files NK_MODULE "client" EDL "${CMAKE_SOURCE_DIR}/resources/edl/Client.edl")
add_executable (client_qemu "uart.c")
add_dependencies (client_qemu client_edl_files)
set_target_compiler_flags_default (client_qemu "STANDARD_GNU_C99:YES" "STRICT_WARNINGS:NO")
target_compile_definitions (client_qemu PRIVATE "-DPORT_NAME=\"uart0\"")
target_link_libraries (client_qemu ${uart_CLIENT_LIB})
#set_target_properties (client_qemu PROPERTIES LINK_FLAGS "-Ttext 0x00800000")
add_executable (client_hw "uart.c")
add_dependencies (client_hw client_edl_files)
set_target_compiler_flags_default (client_hw "STANDARD_GNU_C99:YES" "STRICT_WARNINGS:NO")
target_compile_definitions (client_hw PRIVATE "-DPORT_NAME=\"${PORT_HW}\"")
target_link_libraries (client_hw ${uart_CLIENT_LIB})
#set_target_properties (client PROPERTIES LINK_FLAGS "-Ttext 0x00800000")
Instructions for building the Einit entity and the system image
einit/CMakeLists.txt
project (einit)
# Tools for using the NK parser.
include (platform/image)
# KasperskyOS image for target hardware platform.
build_kos_hw_image (kos-image
EINIT_ENTITY EinitHw
CONNECTIONS_CFG "init_hw.yaml.in"
SECURITY_PSL "security.psl"
IMAGE_FILES client_hw)
set (QEMU_FLAGS "-nographic")
# KasperskyOS image for QEMU with simulation targets (sim, gdbsim, gdb).
build_kos_qemu_image (kos-qemu-image
EINIT_ENTITY EinitQemu
CONNECTIONS_CFG "init_qemu.yaml.in"
SECURITY_PSL "security.psl"
QEMU_FLAGS "${QEMU_FLAGS}"
IMAGE_FILES client_qemu)
#set_target_properties (EinitQemu PROPERTIES LINK_FLAGS "-Ttext 0x00200000")
Solution build instructions
CMakeLists.txt
cmake_minimum_required (VERSION 3.12)
project(uart)
# Initialize the CMake library
include (platform)
initialize_platform ()
# Add Doxygen documentation
include (platform/doxygen)
add_project_documentation_main_target ()
# This command finds all components of the uart package.
find_package (uart REQUIRED)
include_directories (${uart_INCLUDE})
add_subdirectory (client)
add_subdirectory (einit)
Build using CMake
To build and run the example, run the following script:
/opt/KasperskyOS-Community-Edition-<version>/examples/uart/cross-build.sh