Skip to content

Commit

Permalink
drivers: Implement semihosting based console driver for log
Browse files Browse the repository at this point in the history
Semihosting is a mechanism that enables target to communicate and use
I/O facilities on a host computer which is running a debugger, such as
GDB. The I/O facilities include character {read|write} {from|to} the
semihosting host side console or a file. In other words, OP-TEE OS can
output log to the host side console or the host side file, if there is a
semihosting host and OP-TEE OS requests the semihosting operations to
that host.

This commit implements a simple console driver which uses semihosting
operations to read/write the character. There are two paths to output
the log:
- If the caller of semihosting_console_init() provides the path of the
  file, the driver will try to open that file, and output the log to
  that host side file.
- If the caller of semihosting_console_init() does not provide the path
  of the file, the driver will try to output the log to host side
  console directly.

Signed-off-by: Alvin Chang <[email protected]>
  • Loading branch information
gagachang committed Feb 21, 2024
1 parent 793625c commit cbfaa08
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
56 changes: 56 additions & 0 deletions core/drivers/semihosting_console.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: BSD-2-Clause
/*
* Copyright (c) 2024 Andes Technology Corporation
*/

#include <compiler.h>
#include <drivers/semihosting_console.h>
#include <kernel/semihosting.h>
#include <util.h>

static void semihosting_console_putc(struct serial_chip *chip __unused, int ch)
{
semihosting_writec(ch);
}

static int semihosting_console_getchar(struct serial_chip *chip __unused)
{
return semihosting_readc();
}

static const struct serial_ops semihosting_console_ops = {
.putc = semihosting_console_putc,
.getchar = semihosting_console_getchar,
};
DECLARE_KEEP_PAGER(semihosting_console_ops);

static void semihosting_console_fd_putc(struct serial_chip *chip __unused,
int ch)
{
struct semihosting_console_data *pd =
container_of(chip, struct semihosting_console_data, chip);

semihosting_write(pd->fd, &ch, 1);
}

static const struct serial_ops semihosting_console_fd_ops = {
.putc = semihosting_console_fd_putc,
};
DECLARE_KEEP_PAGER(semihosting_console_fd_ops);

void semihosting_console_init(struct semihosting_console_data *pd,
const char *file_path)
{
if (file_path) {
/* Output log to given file. */
pd->chip.ops = &semihosting_console_fd_ops;
pd->file_path = file_path;
pd->fd = semihosting_open(pd->file_path,
O_RDWR | O_CREAT | O_TRUNC);
} else {
/* Output log to semihosting console. */
pd->chip.ops = &semihosting_console_ops;
pd->file_path = NULL;
pd->fd = -1;
}
}
1 change: 1 addition & 0 deletions core/drivers/sub.mk
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ srcs-$(CFG_CBMEM_CONSOLE) += cbmem_console.c
srcs-$(CFG_RISCV_PLIC) += plic.c
srcs-$(CFG_HISILICON_CRYPTO_DRIVER) += hisi_trng.c
srcs-$(CFG_WIDEVINE_HUK) += widevine_huk.c
srcs-$(CFG_SEMIHOSTING_CONSOLE) += semihosting_console.c

subdirs-y += crypto
subdirs-$(CFG_BNXT_FW) += bnxt
Expand Down
25 changes: 25 additions & 0 deletions core/include/drivers/semihosting_console.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* SPDX-License-Identifier: BSD-2-Clause */
/*
* Copyright (c) 2024 Andes Technology Corporation
*/
#ifndef __DRIVERS_SEMIHOSTING_CONSOLE_H
#define __DRIVERS_SEMIHOSTING_CONSOLE_H

#include <drivers/serial.h>

struct semihosting_console_data {
struct serial_chip chip;
const char *file_path;
int fd;
};

/*
* Initialize console which uses architecture-specific semihosting mechanism.
* If "file_path" is not NULL, OP-TEE OS will try to output log to that file.
* Otherwise, if "file_path" is NULL, OP-TEE OS will try to output log to the
* semihosting console.
*/
void semihosting_console_init(struct semihosting_console_data *pd,
const char *file_path);

#endif /* __DRIVERS_SEMIHOSTING_CONSOLE_H */

0 comments on commit cbfaa08

Please sign in to comment.