int semihosting_get_flen(const char *file_name) { int file_handle, length; assert(semihosting_connection_supported()); file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB); if (file_handle == -1) return file_handle; /* Find the length of the file */ length = semihosting_file_length(file_handle); return semihosting_file_close(file_handle) ? -1 : length; }
long semihosting_download_file(const char *file_name, size_t buf_size, uintptr_t buf) { long ret = -EINVAL; size_t length; long file_handle; /* Null pointer check */ if (!buf) return ret; assert(semihosting_connection_supported()); file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB); if (file_handle == -1) return ret; /* Find the actual length of the file */ length = semihosting_file_length(file_handle); if (length == -1) goto semihosting_fail; /* Signal error if we do not have enough space for the file */ if (length > buf_size) goto semihosting_fail; /* * A successful read will return 0 in which case we pass back * the actual number of bytes read. Else we pass a negative * value indicating an error. */ ret = semihosting_file_read(file_handle, &length, buf); if (ret) goto semihosting_fail; else ret = length; semihosting_fail: semihosting_file_close(file_handle); return ret; }
return 0; } /* Open a file on the semi-hosting device */ static int sh_file_open(io_dev_info_t *dev_info __unused, const uintptr_t spec, io_entity_t *entity) { int result = -ENOENT; long sh_result; const io_file_spec_t *file_spec = (const io_file_spec_t *)spec; assert(file_spec != NULL); assert(entity != NULL); sh_result = semihosting_file_open(file_spec->path, file_spec->mode); if (sh_result > 0) { entity->info = (uintptr_t)sh_result; result = 0; } return result; } /* Seek to a particular file offset on the semi-hosting device */ static int sh_file_seek(io_entity_t *entity, int mode, ssize_t offset) { long file_handle, sh_result; assert(entity != NULL);