Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 2 | /* |
Sean Anderson | 56e3fe8 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 3 | * Copyright (C) 2022 Sean Anderson <sean.anderson@seco.com> |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 4 | * Copyright 2014 Broadcom Corporation |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
Sean Anderson | 56e3fe8 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 8 | * This code has been tested on arm64/aarch64 fastmodel only. An untested |
| 9 | * placeholder exists for armv7 architectures, but since they are commonly |
| 10 | * available in silicon now, fastmodel usage makes less sense for them. |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 11 | */ |
| 12 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 14 | #include <semihosting.h> |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 15 | |
| 16 | #define SYSOPEN 0x01 |
| 17 | #define SYSCLOSE 0x02 |
Sean Anderson | 1e9f443 | 2022-03-22 16:59:23 -0400 | [diff] [blame] | 18 | #define SYSWRITEC 0x03 |
| 19 | #define SYSWRITE0 0x04 |
Sean Anderson | 56e3fe8 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 20 | #define SYSWRITE 0x05 |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 21 | #define SYSREAD 0x06 |
Sean Anderson | 1e9f443 | 2022-03-22 16:59:23 -0400 | [diff] [blame] | 22 | #define SYSREADC 0x07 |
Sean Anderson | 56e3fe8 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 23 | #define SYSSEEK 0x0A |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 24 | #define SYSFLEN 0x0C |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 25 | #define SYSERRNO 0x13 |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 26 | |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 27 | /* |
| 28 | * Call the handler |
| 29 | */ |
Linus Walleij | 1c2e27e | 2015-03-23 11:06:10 +0100 | [diff] [blame] | 30 | static noinline long smh_trap(unsigned int sysnum, void *addr) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 31 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 32 | register long result asm("r0"); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 33 | #if defined(CONFIG_ARM64) |
| 34 | asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr)); |
Vadzim Dambrouski | 98d3894 | 2015-10-19 19:40:14 +0300 | [diff] [blame] | 35 | #elif defined(CONFIG_CPU_V7M) |
| 36 | asm volatile ("bkpt #0xAB" : "=r" (result) : "0"(sysnum), "r"(addr)); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 37 | #else |
| 38 | /* Note - untested placeholder */ |
| 39 | asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr)); |
| 40 | #endif |
| 41 | return result; |
| 42 | } |
| 43 | |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 44 | /** |
| 45 | * smh_errno() - Read the host's errno |
| 46 | * |
| 47 | * This gets the value of the host's errno and negates it. The host's errno may |
| 48 | * or may not be set, so only call this function if a previous semihosting call |
| 49 | * has failed. |
| 50 | * |
| 51 | * Return: a negative error value |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 52 | */ |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 53 | static int smh_errno(void) |
| 54 | { |
| 55 | long ret = smh_trap(SYSERRNO, NULL); |
| 56 | |
| 57 | if (ret > 0 && ret < INT_MAX) |
| 58 | return -ret; |
| 59 | return -EIO; |
| 60 | } |
| 61 | |
Sean Anderson | a3a6c69 | 2022-03-22 16:59:15 -0400 | [diff] [blame] | 62 | long smh_open(const char *fname, enum smh_open_mode mode) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 63 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 64 | long fd; |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 65 | struct smh_open_s { |
| 66 | const char *fname; |
| 67 | unsigned long mode; |
| 68 | size_t len; |
| 69 | } open; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 70 | |
Sean Anderson | a3a6c69 | 2022-03-22 16:59:15 -0400 | [diff] [blame] | 71 | debug("%s: file \'%s\', mode \'%u\'\n", __func__, fname, mode); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 72 | |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 73 | open.fname = fname; |
| 74 | open.len = strlen(fname); |
| 75 | open.mode = mode; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 76 | |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 77 | /* Open the file on the host */ |
| 78 | fd = smh_trap(SYSOPEN, &open); |
| 79 | if (fd == -1) |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 80 | return smh_errno(); |
Linus Walleij | 7cc8ca8 | 2014-12-15 11:06:05 +0100 | [diff] [blame] | 81 | return fd; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Sean Anderson | 56e3fe8 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 84 | /** |
| 85 | * struct smg_rdwr_s - Arguments for read and write |
| 86 | * @fd: A file descriptor returned from smh_open() |
| 87 | * @memp: Pointer to a buffer of memory of at least @len bytes |
| 88 | * @len: The number of bytes to read or write |
| 89 | */ |
| 90 | struct smh_rdwr_s { |
| 91 | long fd; |
| 92 | void *memp; |
| 93 | size_t len; |
| 94 | }; |
| 95 | |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 96 | long smh_read(long fd, void *memp, size_t len) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 97 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 98 | long ret; |
Sean Anderson | 56e3fe8 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 99 | struct smh_rdwr_s read; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 100 | |
Vadzim Dambrouski | 2738215 | 2015-10-19 19:40:15 +0300 | [diff] [blame] | 101 | debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 102 | |
| 103 | read.fd = fd; |
| 104 | read.memp = memp; |
| 105 | read.len = len; |
| 106 | |
| 107 | ret = smh_trap(SYSREAD, &read); |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 108 | if (ret < 0) |
| 109 | return smh_errno(); |
| 110 | return len - ret; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Sean Anderson | 56e3fe8 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 113 | long smh_write(long fd, const void *memp, size_t len, ulong *written) |
| 114 | { |
| 115 | long ret; |
| 116 | struct smh_rdwr_s write; |
| 117 | |
| 118 | debug("%s: fd %ld, memp %p, len %zu\n", __func__, fd, memp, len); |
| 119 | |
| 120 | write.fd = fd; |
| 121 | write.memp = (void *)memp; |
| 122 | write.len = len; |
| 123 | |
| 124 | ret = smh_trap(SYSWRITE, &write); |
| 125 | *written = len - ret; |
| 126 | if (ret) |
| 127 | return smh_errno(); |
| 128 | return 0; |
| 129 | } |
| 130 | |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 131 | long smh_close(long fd) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 132 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 133 | long ret; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 134 | |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 135 | debug("%s: fd %ld\n", __func__, fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 136 | |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 137 | ret = smh_trap(SYSCLOSE, &fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 138 | if (ret == -1) |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 139 | return smh_errno(); |
| 140 | return 0; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Sean Anderson | adf073a | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 143 | long smh_flen(long fd) |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 144 | { |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 145 | long ret; |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 146 | |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 147 | debug("%s: fd %ld\n", __func__, fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 148 | |
Linus Walleij | 6fda265 | 2014-12-15 11:05:56 +0100 | [diff] [blame] | 149 | ret = smh_trap(SYSFLEN, &fd); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 150 | if (ret == -1) |
Sean Anderson | 003eaa8 | 2022-03-22 16:59:16 -0400 | [diff] [blame] | 151 | return smh_errno(); |
Darwin Rambo | d32d411 | 2014-06-09 11:12:59 -0700 | [diff] [blame] | 152 | return ret; |
| 153 | } |
| 154 | |
Sean Anderson | 56e3fe8 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 155 | long smh_seek(long fd, long pos) |
| 156 | { |
| 157 | long ret; |
| 158 | struct smh_seek_s { |
| 159 | long fd; |
| 160 | long pos; |
| 161 | } seek; |
| 162 | |
| 163 | debug("%s: fd %ld pos %ld\n", __func__, fd, pos); |
| 164 | |
| 165 | seek.fd = fd; |
| 166 | seek.pos = pos; |
| 167 | |
| 168 | ret = smh_trap(SYSSEEK, &seek); |
| 169 | if (ret) |
| 170 | return smh_errno(); |
| 171 | return 0; |
| 172 | } |
Sean Anderson | 1e9f443 | 2022-03-22 16:59:23 -0400 | [diff] [blame] | 173 | |
| 174 | int smh_getc(void) |
| 175 | { |
| 176 | return smh_trap(SYSREADC, NULL); |
| 177 | } |
| 178 | |
| 179 | void smh_putc(char ch) |
| 180 | { |
| 181 | smh_trap(SYSWRITEC, &ch); |
| 182 | } |
| 183 | |
| 184 | void smh_puts(const char *s) |
| 185 | { |
| 186 | smh_trap(SYSWRITE0, (char *)s); |
| 187 | } |