Sean Anderson | b10f724 | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
| 2 | /* |
| 3 | * Copyright (C) 2022 Sean Anderson <sean.anderson@seco.com> |
| 4 | */ |
| 5 | |
| 6 | #ifndef _SEMIHOSTING_H |
| 7 | #define _SEMIHOSTING_H |
| 8 | |
Sean Anderson | eff77c3 | 2022-03-22 16:59:15 -0400 | [diff] [blame] | 9 | /** |
| 10 | * enum smh_open_mode - Numeric file modes for use with smh_open() |
| 11 | * MODE_READ: 'r' |
| 12 | * MODE_BINARY: 'b' |
| 13 | * MODE_PLUS: '+' |
| 14 | * MODE_WRITE: 'w' |
| 15 | * MODE_APPEND: 'a' |
| 16 | * |
| 17 | * These modes represent the mode string used by fopen(3) in a form which can |
| 18 | * be passed to smh_open(). These do NOT correspond directly to %O_RDONLY, |
| 19 | * %O_CREAT, etc; see fopen(3) for details. In particular, @MODE_PLUS |
| 20 | * effectively results in adding %O_RDWR, and @MODE_WRITE will add %O_TRUNC. |
| 21 | * For compatibility, @MODE_BINARY should be added when opening non-text files |
| 22 | * (such as images). |
| 23 | */ |
| 24 | enum smh_open_mode { |
| 25 | MODE_READ = 0x0, |
| 26 | MODE_BINARY = 0x1, |
| 27 | MODE_PLUS = 0x2, |
| 28 | MODE_WRITE = 0x4, |
| 29 | MODE_APPEND = 0x8, |
| 30 | }; |
| 31 | |
Sean Anderson | 79f6ad6 | 2022-03-22 16:59:17 -0400 | [diff] [blame] | 32 | /** |
| 33 | * smh_open() - Open a file on the host |
| 34 | * @fname: The name of the file to open |
| 35 | * @mode: The mode to use when opening the file |
| 36 | * |
| 37 | * Return: Either a file descriptor or a negative error on failure |
| 38 | */ |
Sean Anderson | eff77c3 | 2022-03-22 16:59:15 -0400 | [diff] [blame] | 39 | long smh_open(const char *fname, enum smh_open_mode mode); |
Sean Anderson | 79f6ad6 | 2022-03-22 16:59:17 -0400 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * smh_read() - Read data from a file |
| 43 | * @fd: A file descriptor returned from smh_open() |
| 44 | * @memp: Pointer to a buffer of memory of at least @len bytes |
| 45 | * @len: The number of bytes to read |
| 46 | * |
| 47 | * Return: |
| 48 | * * The number of bytes read on success, with 0 indicating %EOF |
| 49 | * * A negative error on failure |
| 50 | */ |
Sean Anderson | b10f724 | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 51 | long smh_read(long fd, void *memp, size_t len); |
Sean Anderson | 79f6ad6 | 2022-03-22 16:59:17 -0400 | [diff] [blame] | 52 | |
| 53 | /** |
Sean Anderson | 12a05b3 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 54 | * smh_write() - Write data to a file |
| 55 | * @fd: A file descriptor returned from smh_open() |
| 56 | * @memp: Pointer to a buffer of memory of at least @len bytes |
| 57 | * @len: The number of bytes to read |
| 58 | * @written: Pointer which will be updated with the actual bytes written |
| 59 | * |
| 60 | * Return: 0 on success or negative error on failure |
| 61 | */ |
| 62 | long smh_write(long fd, const void *memp, size_t len, ulong *written); |
| 63 | |
| 64 | /** |
Sean Anderson | 79f6ad6 | 2022-03-22 16:59:17 -0400 | [diff] [blame] | 65 | * smh_close() - Close an open file |
| 66 | * @fd: A file descriptor returned from smh_open() |
| 67 | * |
| 68 | * Return: 0 on success or negative error on failure |
| 69 | */ |
Sean Anderson | b10f724 | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 70 | long smh_close(long fd); |
Sean Anderson | 79f6ad6 | 2022-03-22 16:59:17 -0400 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * smh_flen() - Get the length of a file |
| 74 | * @fd: A file descriptor returned from smh_open() |
| 75 | * |
| 76 | * Return: The length of the file, in bytes, or a negative error on failure |
| 77 | */ |
Sean Anderson | b10f724 | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 78 | long smh_flen(long fd); |
| 79 | |
Sean Anderson | 12a05b3 | 2022-03-22 16:59:18 -0400 | [diff] [blame] | 80 | /** |
| 81 | * smh_seek() - Seek to a position in a file |
| 82 | * @fd: A file descriptor returned from smh_open() |
| 83 | * @pos: The offset (in bytes) to seek to |
| 84 | * |
| 85 | * Return: 0 on success or negative error on failure |
| 86 | */ |
| 87 | long smh_seek(long fd, long pos); |
| 88 | |
Sean Anderson | 3ea744e | 2022-03-22 16:59:23 -0400 | [diff] [blame^] | 89 | /** |
| 90 | * smh_getc() - Read a character from stdin |
| 91 | * |
| 92 | * Return: The character read, or a negative error on failure |
| 93 | */ |
| 94 | int smh_getc(void); |
| 95 | |
| 96 | /** |
| 97 | * smh_putc() - Print a character on stdout |
| 98 | * @ch: The character to print |
| 99 | */ |
| 100 | void smh_putc(char ch); |
| 101 | |
| 102 | /** |
| 103 | * smh_write0() - Print a nul-terminated string on stdout |
| 104 | * @s: The string to print |
| 105 | */ |
| 106 | void smh_puts(const char *s); |
| 107 | |
Sean Anderson | b10f724 | 2022-03-22 16:59:14 -0400 | [diff] [blame] | 108 | #endif /* _SEMIHOSTING_H */ |