blob: da4d0d6e044f91f454bea1fd6e5395069f41b909 [file] [log] [blame]
David Brazdil17e76652020-01-29 14:44:19 +00001/*
2 * Copyright 2020 The Hafnium Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "hf/plat/console.h"
18
19#include "test/hftest.h"
20
21/* clang-format off */
22#define CMD_GET_COMMAND_LINE "[hftest_ctrl:get_command_line]\n"
23#define CMD_FINISHED "[hftest_ctrl:finished]\n"
24/* clang-format on */
25
26static char command_line[128];
27
28static void write(const char *str)
29{
30 while (*str != '\0') {
31 plat_console_putchar(*str);
32 str++;
33 }
34}
35
36static bool read(char *buf, size_t max_len, struct memiter *str)
37{
38 char c;
39 size_t len = 0;
40
41 while (true) {
42 c = plat_console_getchar();
43 if (c == '\r' || c == '\n') {
44 memiter_init(str, buf, len);
45 return true;
46 }
47
48 if (len < max_len) {
49 buf[len++] = c;
50 } else {
51 return false;
52 }
53 }
54}
55
56bool hftest_ctrl_start(const struct fdt_header *fdt, struct memiter *cmd)
57{
58 (void)fdt;
59
60 /* Initialize the console */
61 plat_console_init();
62
63 /* Inform the host that we are ready to receive the command line. */
64 write(CMD_GET_COMMAND_LINE);
65
66 /* Read command line from the console. */
67 read(command_line, ARRAY_SIZE(command_line), cmd);
68
69 return true;
70}
71
72void hftest_ctrl_finish(void)
73{
74 /*
75 * Inform the host that this test has finished running and all
76 * subsequent logs belong to the next run.
77 */
78 write(CMD_FINISHED);
79
80 /* Reboot the device. */
81 hftest_device_reboot();
82}