1 /*
2  * Copyright 2020 The Hafnium Authors.
3  *
4  * 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.
7  */
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 
18 static char command_line[128];
19 
write(const char * str)20 static void write(const char *str)
21 {
22 	while (*str != '\0') {
23 		plat_console_putchar(*str);
24 		str++;
25 	}
26 }
27 
read(char * buf,size_t max_len,struct memiter * str)28 static 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 
hftest_ctrl_start(const struct fdt * fdt,struct memiter * cmd)48 bool hftest_ctrl_start(const struct fdt *fdt, struct memiter *cmd)
49 {
50 	(void)fdt;
51 
52 	/* Let the console driver map its memory as device memory. */
53 	plat_console_mm_init(hftest_mm_get_stage1(), hftest_mm_get_ppool());
54 
55 	/* 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 
hftest_ctrl_finish(void)67 void 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 
hftest_ctrl_reboot(void)76 void hftest_ctrl_reboot(void)
77 {
78 	/* Reboot the device. */
79 	hftest_device_reboot();
80 }
81