blob: dd2d41eee0e38a38d60448ba165b205edd0694cf [file] [log] [blame]
David Brazdil17e76652020-01-29 14:44:19 +00001/*
2 * Copyright 2020 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
David Brazdil17e76652020-01-29 14:44:19 +00007 */
8
9#include "hf/plat/console.h"
10
11#include "test/hftest.h"
12
13/* clang-format off */
14#define CMD_GET_COMMAND_LINE "[hftest_ctrl:get_command_line]\n"
15#define CMD_FINISHED "[hftest_ctrl:finished]\n"
16/* clang-format on */
17
18static char command_line[128];
19
20static void write(const char *str)
21{
22 while (*str != '\0') {
23 plat_console_putchar(*str);
24 str++;
25 }
26}
27
28static bool read(char *buf, size_t max_len, struct memiter *str)
29{
30 char c;
31 size_t len = 0;
32
33 while (true) {
34 c = plat_console_getchar();
35 if (c == '\r' || c == '\n') {
36 memiter_init(str, buf, len);
37 return true;
38 }
39
40 if (len < max_len) {
41 buf[len++] = c;
42 } else {
43 return false;
44 }
45 }
46}
47
David Brazdilb856be62020-03-25 10:14:55 +000048bool hftest_ctrl_start(const struct fdt *fdt, struct memiter *cmd)
David Brazdil17e76652020-01-29 14:44:19 +000049{
50 (void)fdt;
51
David Brazdil399fe492020-01-31 13:30:24 +000052 /* Let the console driver map its memory as device memory. */
53 plat_console_mm_init(hftest_mm_get_stage1(), hftest_mm_get_ppool());
54
David Brazdil17e76652020-01-29 14:44:19 +000055 /* Initialize the console */
56 plat_console_init();
57
58 /* Inform the host that we are ready to receive the command line. */
59 write(CMD_GET_COMMAND_LINE);
60
61 /* Read command line from the console. */
62 read(command_line, ARRAY_SIZE(command_line), cmd);
63
64 return true;
65}
66
67void hftest_ctrl_finish(void)
68{
69 /*
70 * Inform the host that this test has finished running and all
71 * subsequent logs belong to the next run.
72 */
73 write(CMD_FINISHED);
74
75 /* Reboot the device. */
76 hftest_device_reboot();
77}