|
| 1 | +// Hardware Root of Trust (HWRoT) Hardfault Driver Application |
| 2 | +// |
| 3 | +// When selected by the main screen HWRoT Demo application, calls a command |
| 4 | +// on a driver which is intended to hardfault all applications. |
| 5 | + |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | +#include <stdlib.h> |
| 9 | + |
| 10 | +#include <libtock/kernel/ipc.h> |
| 11 | +#include <libtock/tock.h> |
| 12 | + |
| 13 | +#define LOG_WIDTH 32 |
| 14 | + |
| 15 | +bool started = false; |
| 16 | +bool log_done = false; |
| 17 | +size_t screen_service = -1; |
| 18 | +char log_buf[LOG_WIDTH] __attribute__((aligned(LOG_WIDTH))); |
| 19 | + |
| 20 | +const char SCREEN_SERVICE_NAME[] = "org.tockos.tutorials.attestation.screen"; |
| 21 | + |
| 22 | +static void ipc_callback(__attribute__ ((unused)) int pid, |
| 23 | + __attribute__ ((unused)) int len, |
| 24 | + __attribute__ ((unused)) int arg2, |
| 25 | + __attribute__ ((unused)) void* ud) { |
| 26 | + started = true; |
| 27 | +} |
| 28 | + |
| 29 | +static void log_done_callback(__attribute__ ((unused)) int pid, |
| 30 | + __attribute__ ((unused)) int len, |
| 31 | + __attribute__ ((unused)) int arg2, |
| 32 | + __attribute__ ((unused)) void* ud) { |
| 33 | + log_done = true; |
| 34 | +} |
| 35 | + |
| 36 | +static void wait_for_start(void) { |
| 37 | + // Register an IPC callback and wait for it to be called by the |
| 38 | + // screen app based on the user's app selection. |
| 39 | + ipc_register_service_callback("org.tockos.tutorials.attestation.questionable", ipc_callback, |
| 40 | + NULL); |
| 41 | + yield_for(&started); |
| 42 | +} |
| 43 | + |
| 44 | +static int setup_logging() { |
| 45 | + returncode_t ret; |
| 46 | + |
| 47 | + // Find the PID of the screen logging service |
| 48 | + ret = ipc_discover(SCREEN_SERVICE_NAME, &screen_service); |
| 49 | + if (ret != RETURNCODE_SUCCESS) { |
| 50 | + printf("Screen logging service not found.\n"); |
| 51 | + return ret; |
| 52 | + } |
| 53 | + |
| 54 | + // Set up a callback and share so we can supply log messages |
| 55 | + // and know when they've been completely logged. |
| 56 | + ipc_register_client_callback(screen_service, log_done_callback, NULL); |
| 57 | + ipc_share(screen_service, log_buf, LOG_WIDTH); |
| 58 | + |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +static int log_to_screen(const char* message) { |
| 63 | + returncode_t ret; |
| 64 | + |
| 65 | + // Copy up to the log buffer's size of the message, with room for a null byte. |
| 66 | + uint16_t len = strnlen(message, sizeof(log_buf) - 1); |
| 67 | + memcpy(log_buf, message, len); |
| 68 | + |
| 69 | + // Add the null byte. |
| 70 | + log_buf[len] = '\0'; |
| 71 | + |
| 72 | + // Start the logging process. |
| 73 | + ret = ipc_notify_service(screen_service); |
| 74 | + if (ret != RETURNCODE_SUCCESS) { |
| 75 | + printf("Failed to request a log to screen.\n"); |
| 76 | + return ret; |
| 77 | + } |
| 78 | + |
| 79 | + // Wait for the log to complete. |
| 80 | + yield_for(&log_done); |
| 81 | + log_done = false; |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +int main(void) { |
| 87 | + returncode_t ret; |
| 88 | + |
| 89 | + // Wait to receive the signal to start from the app selector. |
| 90 | + wait_for_start(); |
| 91 | + |
| 92 | + // Set up logging service. |
| 93 | + ret = setup_logging(); |
| 94 | + if (ret < 0) { |
| 95 | + printf("ERROR: cannot set up logging\n"); |
| 96 | + } |
| 97 | + |
| 98 | + // Declare our intention to hardfault everything. |
| 99 | + ret = log_to_screen("Faulting all applications...\n"); |
| 100 | + if (ret < 0) { |
| 101 | + printf("ERROR: cannot log to screen\n"); |
| 102 | + } |
| 103 | + |
| 104 | + // Bring everything down! |
| 105 | + syscall_return_t cr = command(0x99998, 1, 0, 0); |
| 106 | + if (cr.type != TOCK_SYSCALL_SUCCESS) { |
| 107 | + return tock_command_return_novalue_to_returncode(cr); |
| 108 | + } |
| 109 | + |
| 110 | + // Note if the application is still running. |
| 111 | + ret = log_to_screen("This should never be seen!"); |
| 112 | + if (ret < 0) { |
| 113 | + printf("ERROR: cannot log to screen\n"); |
| 114 | + } |
| 115 | + |
| 116 | + return 0; |
| 117 | +} |
0 commit comments