Home | History | Annotate | Download | only in semihosting
      1 /*
      2  * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are met:
      6  *
      7  * Redistributions of source code must retain the above copyright notice, this
      8  * list of conditions and the following disclaimer.
      9  *
     10  * Redistributions in binary form must reproduce the above copyright notice,
     11  * this list of conditions and the following disclaimer in the documentation
     12  * and/or other materials provided with the distribution.
     13  *
     14  * Neither the name of ARM nor the names of its contributors may be used
     15  * to endorse or promote products derived from this software without specific
     16  * prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
     22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include <assert.h>
     32 #include <errno.h>
     33 #include <semihosting.h>
     34 #include <string.h>
     35 
     36 #ifndef SEMIHOSTING_SUPPORTED
     37 #define SEMIHOSTING_SUPPORTED  1
     38 #endif
     39 
     40 long semihosting_call(unsigned long operation,
     41 			void *system_block_address);
     42 
     43 typedef struct {
     44 	const char *file_name;
     45 	unsigned long mode;
     46 	size_t name_length;
     47 } smh_file_open_block_t;
     48 
     49 typedef struct {
     50 	long handle;
     51 	uintptr_t buffer;
     52 	size_t length;
     53 } smh_file_read_write_block_t;
     54 
     55 typedef struct {
     56 	long handle;
     57 	ssize_t location;
     58 } smh_file_seek_block_t;
     59 
     60 typedef struct {
     61 	char *command_line;
     62 	size_t command_length;
     63 } smh_system_block_t;
     64 
     65 long semihosting_connection_supported(void)
     66 {
     67 	return SEMIHOSTING_SUPPORTED;
     68 }
     69 
     70 long semihosting_file_open(const char *file_name, size_t mode)
     71 {
     72 	smh_file_open_block_t open_block;
     73 
     74 	open_block.file_name = file_name;
     75 	open_block.mode = mode;
     76 	open_block.name_length = strlen(file_name);
     77 
     78 	return semihosting_call(SEMIHOSTING_SYS_OPEN,
     79 				(void *) &open_block);
     80 }
     81 
     82 long semihosting_file_seek(long file_handle, ssize_t offset)
     83 {
     84 	smh_file_seek_block_t seek_block;
     85 	long result;
     86 
     87 	seek_block.handle = file_handle;
     88 	seek_block.location = offset;
     89 
     90 	result = semihosting_call(SEMIHOSTING_SYS_SEEK,
     91 				  (void *) &seek_block);
     92 
     93 	if (result)
     94 		result = semihosting_call(SEMIHOSTING_SYS_ERRNO, 0);
     95 
     96 	return result;
     97 }
     98 
     99 long semihosting_file_read(long file_handle, size_t *length, uintptr_t buffer)
    100 {
    101 	smh_file_read_write_block_t read_block;
    102 	long result = -EINVAL;
    103 
    104 	if ((length == NULL) || (buffer == (uintptr_t)NULL))
    105 		return result;
    106 
    107 	read_block.handle = file_handle;
    108 	read_block.buffer = buffer;
    109 	read_block.length = *length;
    110 
    111 	result = semihosting_call(SEMIHOSTING_SYS_READ,
    112 				  (void *) &read_block);
    113 
    114 	if (result == *length) {
    115 		return -EINVAL;
    116 	} else if (result < *length) {
    117 		*length -= result;
    118 		return 0;
    119 	} else
    120 		return result;
    121 }
    122 
    123 long semihosting_file_write(long file_handle,
    124 			    size_t *length,
    125 			    const uintptr_t buffer)
    126 {
    127 	smh_file_read_write_block_t write_block;
    128 
    129 	if ((length == NULL) || (buffer == (uintptr_t)NULL))
    130 		return -EINVAL;
    131 
    132 	write_block.handle = file_handle;
    133 	write_block.buffer = (uintptr_t)buffer; /* cast away const */
    134 	write_block.length = *length;
    135 
    136 	*length = semihosting_call(SEMIHOSTING_SYS_WRITE,
    137 				   (void *) &write_block);
    138 
    139 	return *length;
    140 }
    141 
    142 long semihosting_file_close(long file_handle)
    143 {
    144 	return semihosting_call(SEMIHOSTING_SYS_CLOSE,
    145 				(void *) &file_handle);
    146 }
    147 
    148 long semihosting_file_length(long file_handle)
    149 {
    150 	return semihosting_call(SEMIHOSTING_SYS_FLEN,
    151 				(void *) &file_handle);
    152 }
    153 
    154 char semihosting_read_char(void)
    155 {
    156 	return semihosting_call(SEMIHOSTING_SYS_READC, NULL);
    157 }
    158 
    159 void semihosting_write_char(char character)
    160 {
    161 	semihosting_call(SEMIHOSTING_SYS_WRITEC, (void *) &character);
    162 }
    163 
    164 void semihosting_write_string(char *string)
    165 {
    166 	semihosting_call(SEMIHOSTING_SYS_WRITE0, (void *) string);
    167 }
    168 
    169 long semihosting_system(char *command_line)
    170 {
    171 	smh_system_block_t system_block;
    172 
    173 	system_block.command_line = command_line;
    174 	system_block.command_length = strlen(command_line);
    175 
    176 	return semihosting_call(SEMIHOSTING_SYS_SYSTEM,
    177 				(void *) &system_block);
    178 }
    179 
    180 long semihosting_get_flen(const char *file_name)
    181 {
    182 	long file_handle;
    183 	size_t length;
    184 
    185 	assert(semihosting_connection_supported());
    186 
    187 	file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB);
    188 	if (file_handle == -1)
    189 		return file_handle;
    190 
    191 	/* Find the length of the file */
    192 	length = semihosting_file_length(file_handle);
    193 
    194 	return semihosting_file_close(file_handle) ? -1 : length;
    195 }
    196 
    197 long semihosting_download_file(const char *file_name,
    198 			      size_t buf_size,
    199 			      uintptr_t buf)
    200 {
    201 	long ret = -EINVAL;
    202 	size_t length;
    203 	long file_handle;
    204 
    205 	/* Null pointer check */
    206 	if (!buf)
    207 		return ret;
    208 
    209 	assert(semihosting_connection_supported());
    210 
    211 	file_handle = semihosting_file_open(file_name, FOPEN_MODE_RB);
    212 	if (file_handle == -1)
    213 		return ret;
    214 
    215 	/* Find the actual length of the file */
    216 	length = semihosting_file_length(file_handle);
    217 	if (length == -1)
    218 		goto semihosting_fail;
    219 
    220 	/* Signal error if we do not have enough space for the file */
    221 	if (length > buf_size)
    222 		goto semihosting_fail;
    223 
    224 	/*
    225 	 * A successful read will return 0 in which case we pass back
    226 	 * the actual number of bytes read. Else we pass a negative
    227 	 * value indicating an error.
    228 	 */
    229 	ret = semihosting_file_read(file_handle, &length, buf);
    230 	if (ret)
    231 		goto semihosting_fail;
    232 	else
    233 		ret = length;
    234 
    235 semihosting_fail:
    236 	semihosting_file_close(file_handle);
    237 	return ret;
    238 }
    239