David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 The Hafnium Authors. |
| 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 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. |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 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 | |
| 20 | static void write(const char *str) |
| 21 | { |
| 22 | while (*str != '\0') { |
| 23 | plat_console_putchar(*str); |
| 24 | str++; |
| 25 | } |
| 26 | } |
| 27 | |
| 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 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 48 | bool hftest_ctrl_start(const struct fdt *fdt, struct memiter *cmd) |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 49 | { |
| 50 | (void)fdt; |
| 51 | |
David Brazdil | 399fe49 | 2020-01-31 13:30:24 +0000 | [diff] [blame] | 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 | |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 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 | |
| 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); |
Olivier Deprez | ea0e019 | 2022-06-03 08:47:52 +0200 | [diff] [blame] | 74 | } |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 75 | |
Olivier Deprez | ea0e019 | 2022-06-03 08:47:52 +0200 | [diff] [blame] | 76 | void hftest_ctrl_reboot(void) |
| 77 | { |
David Brazdil | 17e7665 | 2020-01-29 14:44:19 +0000 | [diff] [blame] | 78 | /* Reboot the device. */ |
| 79 | hftest_device_reboot(); |
| 80 | } |