|
| 1 | +#include "libtock/tock.h" |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | + |
| 5 | +#include <libtock/kernel/ipc.h> |
| 6 | + |
| 7 | +#define ENCRYPTION_SRAM_START 0x2000A000 |
| 8 | +#define ENCRYPTION_SRAM_END 0x2000BFFF |
| 9 | +#define SELF_SRAM_START 0x2000C000 |
| 10 | +#define SELF_SRAM_END 0x2000CFFF |
| 11 | + |
| 12 | +#define LOG_WIDTH 32 |
| 13 | + |
| 14 | +bool started = false; |
| 15 | +bool log_done = false; |
| 16 | +size_t screen_service = -1; |
| 17 | +char log_buf[LOG_WIDTH] __attribute__((aligned(LOG_WIDTH))); |
| 18 | + |
| 19 | +const char SCREEN_SERVICE_NAME[] = "org.tockos.tutorials.attestation.screen"; |
| 20 | + |
| 21 | +static void ipc_callback(__attribute__ ((unused)) int pid, |
| 22 | + __attribute__ ((unused)) int len, |
| 23 | + __attribute__ ((unused)) int arg2, |
| 24 | + __attribute__ ((unused)) void* ud) { |
| 25 | + started = true; |
| 26 | +} |
| 27 | + |
| 28 | +static void log_done_callback(__attribute__ ((unused)) int pid, |
| 29 | + __attribute__ ((unused)) int len, |
| 30 | + __attribute__ ((unused)) int arg2, |
| 31 | + __attribute__ ((unused)) void* ud) { |
| 32 | + log_done = true; |
| 33 | +} |
| 34 | + |
| 35 | +static void wait_for_start(void) { |
| 36 | + // Register an IPC callback and wait for it to be called by the |
| 37 | + // screen app based on the user's app selection. |
| 38 | + ipc_register_service_callback("org.tockos.tutorials.attestation.suspicious", ipc_callback, |
| 39 | + NULL); |
| 40 | + yield_for(&started); |
| 41 | +} |
| 42 | + |
| 43 | +static int setup_logging() { |
| 44 | + returncode_t ret; |
| 45 | + |
| 46 | + // Find the PID of the screen logging service |
| 47 | + ret = ipc_discover(SCREEN_SERVICE_NAME, &screen_service); |
| 48 | + if (ret != RETURNCODE_SUCCESS) { |
| 49 | + printf("Screen logging service not found.\n"); |
| 50 | + return ret; |
| 51 | + } |
| 52 | + |
| 53 | + // Set up a callback and share so we can supply log messages |
| 54 | + // and know when they've been completely logged. |
| 55 | + ipc_register_client_callback(screen_service, log_done_callback, NULL); |
| 56 | + ipc_share(screen_service, log_buf, LOG_WIDTH); |
| 57 | + |
| 58 | + return 0; |
| 59 | +} |
| 60 | + |
| 61 | +static int log_to_screen(const char* message) { |
| 62 | + returncode_t ret; |
| 63 | + |
| 64 | + // Copy up to the log buffer's size of the message, with room for a null byte. |
| 65 | + uint16_t len = strnlen(message, sizeof(log_buf) - 1); |
| 66 | + memcpy(log_buf, message, len); |
| 67 | + |
| 68 | + // Add the null byte. |
| 69 | + log_buf[len] = '\0'; |
| 70 | + |
| 71 | + // Start the logging process. |
| 72 | + ret = ipc_notify_service(screen_service); |
| 73 | + if (ret != RETURNCODE_SUCCESS) { |
| 74 | + printf("Failed to request a log to screen.\n"); |
| 75 | + return ret; |
| 76 | + } |
| 77 | + |
| 78 | + // Wait for the log to complete. |
| 79 | + yield_for(&log_done); |
| 80 | + log_done = false; |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +static void dump_memory(uint32_t *start, uint32_t *end, const char *label) { |
| 86 | + for (uint32_t *addr = start; addr < end; addr++) { |
| 87 | + printf("[%s] %p: %08lX\n", label, addr, *addr); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +int main(void) { |
| 92 | + returncode_t ret; |
| 93 | + |
| 94 | + // Wait to receive the signal to start from the app selector. |
| 95 | + wait_for_start(); |
| 96 | + |
| 97 | + // Set up logging service. |
| 98 | + ret = setup_logging(); |
| 99 | + if (ret < 0) { |
| 100 | + printf("ERROR: cannot set up logging\n"); |
| 101 | + } |
| 102 | + |
| 103 | + // Dump our own SRAM. |
| 104 | + ret = log_to_screen("Dumping own SRAM..."); |
| 105 | + if (ret < 0) { |
| 106 | + printf("ERROR: cannot log to screen\n"); |
| 107 | + } |
| 108 | + dump_memory((uint32_t *)SELF_SRAM_START, (uint32_t *)SELF_SRAM_END, "SELF"); |
| 109 | + ret = log_to_screen("Dumping own SRAM complete!"); |
| 110 | + if (ret < 0) { |
| 111 | + printf("ERROR: cannot log to screen\n"); |
| 112 | + } |
| 113 | + |
| 114 | + // Dump the encryption service SRAM. |
| 115 | + ret = log_to_screen("Dumping encryption SRAM..."); |
| 116 | + if (ret < 0) { |
| 117 | + printf("ERROR: cannot log to screen\n"); |
| 118 | + } |
| 119 | + dump_memory((uint32_t *)ENCRYPTION_SRAM_START, (uint32_t *)ENCRYPTION_SRAM_END, "ENCRYPTION"); |
| 120 | + ret = log_to_screen("Dumping encryption SRAM complete!"); |
| 121 | + if (ret < 0) { |
| 122 | + printf("ERROR: cannot log to screen\n"); |
| 123 | + } |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
0 commit comments