1 /* 2 * Hosted file support for semihosting syscalls. 3 * 4 * Copyright (c) 2005, 2007 CodeSourcery. 5 * Copyright (c) 2019 Linaro 6 * Copyright © 2020 by Keith Packard <keithp@keithp.com> 7 * 8 * SPDX-License-Identifier: GPL-2.0-or-later 9 */ 10 11 #ifndef SEMIHOSTING_GUESTFD_H 12 #define SEMIHOSTING_GUESTFD_H 13 14 typedef enum GuestFDType { 15 GuestFDUnused = 0, 16 GuestFDHost = 1, 17 GuestFDGDB = 2, 18 GuestFDStatic = 3, 19 } GuestFDType; 20 21 /* 22 * Guest file descriptors are integer indexes into an array of 23 * these structures (we will dynamically resize as necessary). 24 */ 25 typedef struct GuestFD { 26 GuestFDType type; 27 union { 28 int hostfd; 29 struct { 30 const uint8_t *data; 31 size_t len; 32 size_t off; 33 } staticfile; 34 }; 35 } GuestFD; 36 37 /** 38 * alloc_guestfd: 39 * 40 * Allocate an unused GuestFD index. The associated guestfd index 41 * will still be GuestFDUnused until it is initialized. 42 */ 43 int alloc_guestfd(void); 44 45 /** 46 * dealloc_guestfd: 47 * @guestfd: GuestFD index 48 * 49 * Deallocate a GuestFD index. The associated GuestFD structure 50 * will be recycled for a subsequent allocation. 51 */ 52 void dealloc_guestfd(int guestfd); 53 54 /** 55 * get_guestfd: 56 * @guestfd: GuestFD index 57 * 58 * Return the GuestFD structure associated with an initialized @guestfd, 59 * or NULL if it has not been allocated, or hasn't been initialized. 60 */ 61 GuestFD *get_guestfd(int guestfd); 62 63 /** 64 * associate_guestfd: 65 * @guestfd: GuestFD index 66 * @hostfd: host file descriptor 67 * 68 * Initialize the GuestFD for @guestfd to GuestFDHost using @hostfd. 69 */ 70 void associate_guestfd(int guestfd, int hostfd); 71 72 /** 73 * staticfile_guestfd: 74 * @guestfd: GuestFD index 75 * @data: data to be read 76 * @len: length of @data 77 * 78 * Initialize the GuestFD for @guestfd to GuestFDStatic. 79 * The @len bytes at @data will be returned to the guest on reads. 80 */ 81 void staticfile_guestfd(int guestfd, const uint8_t *data, size_t len); 82 83 #endif /* SEMIHOSTING_GUESTFD_H */ 84