blob: d7989caaf5ef8ce89e127927c852ddfc06eda2c9 [file] [log] [blame]
nabkah01002e5692022-10-10 12:36:46 +01001/*
2 * Copyright (c) 2022, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <stdarg.h>
8#include <stdbool.h>
9#include <stdio.h>
10#include <string.h>
11
12#include <arch_helpers.h>
13#include <host_shared_data.h>
14
15/*
16 * A printf formatted function used in the Realm world to log messages
17 * in the shared buffer.
18 * Locate the shared logging buffer and print its content
19 */
20void realm_printf(const char *fmt, ...)
21{
Shruti Gupta550e3e82023-08-16 13:20:11 +010022 host_shared_data_t *guest_shared_data = realm_get_my_shared_structure();
nabkah01002e5692022-10-10 12:36:46 +010023 char *log_buffer = (char *)guest_shared_data->log_buffer;
24 va_list args;
25
26 va_start(args, fmt);
nabkah01002e5692022-10-10 12:36:46 +010027 if (strnlen((const char *)log_buffer, MAX_BUF_SIZE) == MAX_BUF_SIZE) {
28 (void)memset((char *)log_buffer, 0, MAX_BUF_SIZE);
29 }
30 (void)vsnprintf((char *)log_buffer +
31 strnlen((const char *)log_buffer, MAX_BUF_SIZE),
32 MAX_BUF_SIZE, fmt, args);
nabkah01002e5692022-10-10 12:36:46 +010033 va_end(args);
34}
35
36void __attribute__((__noreturn__)) do_panic(const char *file, int line)
37{
38 realm_printf("PANIC in file: %s line: %d\n", file, line);
39 while (true) {
40 continue;
41 }
42}
43
44/* This is used from printf() when crash dump is reached */
45int console_putc(int c)
46{
Shruti Gupta550e3e82023-08-16 13:20:11 +010047 host_shared_data_t *guest_shared_data = realm_get_my_shared_structure();
nabkah01002e5692022-10-10 12:36:46 +010048 char *log_buffer = (char *)guest_shared_data->log_buffer;
49
50 if ((c < 0) || (c > 127)) {
51 return -1;
52 }
nabkah01002e5692022-10-10 12:36:46 +010053 if (strnlen((const char *)log_buffer, MAX_BUF_SIZE) == MAX_BUF_SIZE) {
54 (void)memset((char *)log_buffer, 0, MAX_BUF_SIZE);
55 }
56 *((char *)log_buffer + strnlen((const char *)log_buffer, MAX_BUF_SIZE)) = c;
nabkah01002e5692022-10-10 12:36:46 +010057
58 return c;
59}