Added hello world project
This commit is contained in:
		
							
								
								
									
										54
									
								
								hello-world/bsp/libwrap/libwrap.mk
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								hello-world/bsp/libwrap/libwrap.mk
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,54 @@ | ||||
| # See LICENSE for license details. | ||||
|  | ||||
| ifndef _SIFIVE_MK_LIBWRAP | ||||
| _SIFIVE_MK_LIBWRAP := # defined | ||||
|  | ||||
| LIBWRAP_DIR := $(dir $(lastword $(MAKEFILE_LIST))) | ||||
| LIBWRAP_DIR := $(LIBWRAP_DIR:/=) | ||||
|  | ||||
| LIBWRAP_SRCS := \ | ||||
| 	stdlib/malloc.c \ | ||||
| 	sys/open.c \ | ||||
| 	sys/lseek.c \ | ||||
| 	sys/read.c \ | ||||
| 	sys/write.c \ | ||||
| 	sys/fstat.c \ | ||||
| 	sys/stat.c \ | ||||
| 	sys/close.c \ | ||||
| 	sys/link.c \ | ||||
| 	sys/unlink.c \ | ||||
| 	sys/execve.c \ | ||||
| 	sys/fork.c \ | ||||
| 	sys/getpid.c \ | ||||
| 	sys/kill.c \ | ||||
| 	sys/wait.c \ | ||||
| 	sys/isatty.c \ | ||||
| 	sys/times.c \ | ||||
| 	sys/sbrk.c \ | ||||
| 	sys/_exit.c \ | ||||
| 	misc/write_hex.c | ||||
|  | ||||
| LIBWRAP_SRCS := $(foreach f,$(LIBWRAP_SRCS),$(LIBWRAP_DIR)/$(f)) | ||||
| LIBWRAP_OBJS := $(LIBWRAP_SRCS:.c=.o) | ||||
|  | ||||
| LIBWRAP_SYMS := malloc free \ | ||||
| 	open lseek read write fstat stat close link unlink \ | ||||
| 	execve fork getpid kill wait \ | ||||
| 	isatty times sbrk _exit | ||||
|  | ||||
| LIBWRAP := libwrap.a | ||||
|  | ||||
| LINK_DEPS += $(LIBWRAP) | ||||
|  | ||||
| LDFLAGS += $(foreach s,$(LIBWRAP_SYMS),-Wl,--wrap=$(s)) | ||||
| LDFLAGS += -L. -Wl,--start-group -lwrap -lc -Wl,--end-group | ||||
|  | ||||
| CLEAN_OBJS += $(LIBWRAP_OBJS) | ||||
|  | ||||
| $(LIBWRAP_OBJS): %.o: %.c $(HEADERS) | ||||
| 	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< | ||||
|  | ||||
| $(LIBWRAP): $(LIBWRAP_OBJS) | ||||
| 	$(AR) rcs $@ $^ | ||||
|  | ||||
| endif # _SIFIVE_MK_LIBWRAP | ||||
							
								
								
									
										19
									
								
								hello-world/bsp/libwrap/misc/write_hex.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								hello-world/bsp/libwrap/misc/write_hex.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <stdint.h> | ||||
| #include <unistd.h> | ||||
| #include "platform.h" | ||||
|  | ||||
| void write_hex(int fd, uint32_t hex) | ||||
| { | ||||
|   uint8_t ii; | ||||
|   uint8_t jj; | ||||
|   char towrite; | ||||
|   write(fd , "0x", 2); | ||||
|   for (ii = 8 ; ii > 0; ii--) { | ||||
|     jj = ii - 1; | ||||
|     uint8_t digit = ((hex & (0xF << (jj*4))) >> (jj*4)); | ||||
|     towrite = digit < 0xA ? ('0' + digit) : ('A' +  (digit - 0xA)); | ||||
|     write(fd, &towrite, 1); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										17
									
								
								hello-world/bsp/libwrap/stdlib/malloc.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								hello-world/bsp/libwrap/stdlib/malloc.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| /* See LICENSE for license details. */ | ||||
|  | ||||
| /* These functions are intended for embedded RV32 systems and are | ||||
|    obviously incorrect in general. */ | ||||
|  | ||||
| void* __wrap_malloc(unsigned long sz) | ||||
| { | ||||
|   extern void* sbrk(long); | ||||
|   void* res = sbrk(sz); | ||||
|   if ((long)res == -1) | ||||
|     return 0; | ||||
|   return res; | ||||
| } | ||||
|  | ||||
| void __wrap_free(void* ptr) | ||||
| { | ||||
| } | ||||
							
								
								
									
										17
									
								
								hello-world/bsp/libwrap/sys/_exit.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								hello-world/bsp/libwrap/sys/_exit.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <unistd.h> | ||||
| #include "platform.h" | ||||
|  | ||||
| void __wrap__exit(int code) | ||||
| { | ||||
| //volatile uint32_t* leds = (uint32_t*) (GPIO_BASE_ADDR + GPIO_OUT_OFFSET); | ||||
|   const char message[] = "\nProgam has exited with code:"; | ||||
| //*leds = (~(code)); | ||||
|  | ||||
|   write(STDERR_FILENO, message, sizeof(message) - 1); | ||||
|   write_hex(STDERR_FILENO, code); | ||||
|   write(STDERR_FILENO, "\n", 1); | ||||
|  | ||||
|   for (;;); | ||||
| } | ||||
							
								
								
									
										9
									
								
								hello-world/bsp/libwrap/sys/close.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								hello-world/bsp/libwrap/sys/close.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include "stub.h" | ||||
|  | ||||
| int __wrap_close(int fd) | ||||
| { | ||||
|   return _stub(EBADF); | ||||
| } | ||||
							
								
								
									
										9
									
								
								hello-world/bsp/libwrap/sys/execve.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								hello-world/bsp/libwrap/sys/execve.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include "stub.h" | ||||
|  | ||||
| int __wrap_execve(const char* name, char* const argv[], char* const env[]) | ||||
| { | ||||
|   return _stub(ENOMEM); | ||||
| } | ||||
							
								
								
									
										9
									
								
								hello-world/bsp/libwrap/sys/fork.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								hello-world/bsp/libwrap/sys/fork.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include "stub.h" | ||||
|  | ||||
| int fork(void) | ||||
| { | ||||
|   return _stub(EAGAIN); | ||||
| } | ||||
							
								
								
									
										16
									
								
								hello-world/bsp/libwrap/sys/fstat.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								hello-world/bsp/libwrap/sys/fstat.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include <unistd.h> | ||||
| #include <sys/stat.h> | ||||
| #include "stub.h" | ||||
|  | ||||
| int __wrap_fstat(int fd, struct stat* st) | ||||
| { | ||||
|   if (isatty(fd)) { | ||||
|     st->st_mode = S_IFCHR; | ||||
|     return 0; | ||||
|   } | ||||
|  | ||||
|   return _stub(EBADF); | ||||
| } | ||||
							
								
								
									
										6
									
								
								hello-world/bsp/libwrap/sys/getpid.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								hello-world/bsp/libwrap/sys/getpid.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| int __wrap_getpid(void) | ||||
| { | ||||
|   return 1; | ||||
| } | ||||
							
								
								
									
										11
									
								
								hello-world/bsp/libwrap/sys/isatty.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								hello-world/bsp/libwrap/sys/isatty.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <unistd.h> | ||||
|  | ||||
| int __wrap_isatty(int fd) | ||||
| { | ||||
|   if (fd == STDOUT_FILENO || fd == STDERR_FILENO) | ||||
|     return 1; | ||||
|  | ||||
|   return 0; | ||||
| } | ||||
							
								
								
									
										9
									
								
								hello-world/bsp/libwrap/sys/kill.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								hello-world/bsp/libwrap/sys/kill.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include "stub.h" | ||||
|  | ||||
| int __wrap_kill(int pid, int sig) | ||||
| { | ||||
|   return _stub(EINVAL); | ||||
| } | ||||
							
								
								
									
										9
									
								
								hello-world/bsp/libwrap/sys/link.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								hello-world/bsp/libwrap/sys/link.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include "stub.h" | ||||
|  | ||||
| int __wrap_link(const char *old_name, const char *new_name) | ||||
| { | ||||
|   return _stub(EMLINK); | ||||
| } | ||||
							
								
								
									
										14
									
								
								hello-world/bsp/libwrap/sys/lseek.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								hello-world/bsp/libwrap/sys/lseek.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include <unistd.h> | ||||
| #include <sys/types.h> | ||||
| #include "stub.h" | ||||
|  | ||||
| off_t __wrap_lseek(int fd, off_t ptr, int dir) | ||||
| { | ||||
|   if (isatty(fd)) | ||||
|     return 0; | ||||
|  | ||||
|   return _stub(EBADF); | ||||
| } | ||||
							
								
								
									
										9
									
								
								hello-world/bsp/libwrap/sys/open.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								hello-world/bsp/libwrap/sys/open.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include "stub.h" | ||||
|  | ||||
| int __wrap_open(const char* name, int flags, int mode) | ||||
| { | ||||
|   return _stub(ENOENT); | ||||
| } | ||||
							
								
								
									
										9
									
								
								hello-world/bsp/libwrap/sys/openat.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								hello-world/bsp/libwrap/sys/openat.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include "stub.h" | ||||
|  | ||||
| int __wrap_openat(int dirfd, const char* name, int flags, int mode) | ||||
| { | ||||
|   return _stub(ENOENT); | ||||
| } | ||||
							
								
								
									
										30
									
								
								hello-world/bsp/libwrap/sys/read.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								hello-world/bsp/libwrap/sys/read.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <stdint.h> | ||||
| #include <errno.h> | ||||
| #include <unistd.h> | ||||
| #include <sys/types.h> | ||||
|  | ||||
| #include "platform.h" | ||||
| #include "stub.h" | ||||
|  | ||||
| ssize_t __wrap_read(int fd, void* ptr, size_t len) | ||||
| { | ||||
|   uint8_t * current = (uint8_t *)ptr; | ||||
|   volatile uint32_t * uart_rx = (uint32_t *)(UART0_BASE_ADDR + UART_REG_RXFIFO); | ||||
|   volatile uint8_t * uart_rx_cnt = (uint8_t *)(UART0_BASE_ADDR + UART_REG_RXCTRL + 2); | ||||
|  | ||||
|   ssize_t result = 0; | ||||
|  | ||||
|   if (isatty(fd)) { | ||||
|     for (current = (uint8_t *)ptr; | ||||
|         (current < ((uint8_t *)ptr) + len) && (*uart_rx_cnt > 0); | ||||
|         current ++) { | ||||
|       *current = *uart_rx; | ||||
|       result++; | ||||
|     } | ||||
|     return result; | ||||
|   } | ||||
|  | ||||
|   return _stub(EBADF); | ||||
| } | ||||
							
								
								
									
										16
									
								
								hello-world/bsp/libwrap/sys/sbrk.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								hello-world/bsp/libwrap/sys/sbrk.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <stddef.h> | ||||
|  | ||||
| void *__wrap_sbrk(ptrdiff_t incr) | ||||
| { | ||||
|   extern char _end[]; | ||||
|   extern char _heap_end[]; | ||||
|   static char *curbrk = _end; | ||||
|  | ||||
|   if ((curbrk + incr < _end) || (curbrk + incr > _heap_end)) | ||||
|     return NULL - 1; | ||||
|  | ||||
|   curbrk += incr; | ||||
|   return curbrk - incr; | ||||
| } | ||||
							
								
								
									
										10
									
								
								hello-world/bsp/libwrap/sys/stat.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								hello-world/bsp/libwrap/sys/stat.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include <sys/stat.h> | ||||
| #include "stub.h" | ||||
|  | ||||
| int __wrap_stat(const char* file, struct stat* st) | ||||
| { | ||||
|   return _stub(EACCES); | ||||
| } | ||||
							
								
								
									
										10
									
								
								hello-world/bsp/libwrap/sys/stub.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								hello-world/bsp/libwrap/sys/stub.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | ||||
| /* See LICENSE of license details. */ | ||||
| #ifndef _SIFIVE_SYS_STUB_H | ||||
| #define _SIFIVE_SYS_STUB_H | ||||
|  | ||||
| static inline int _stub(int err) | ||||
| { | ||||
|   return -1; | ||||
| } | ||||
|  | ||||
| #endif /* _SIFIVE_SYS_STUB_H */ | ||||
							
								
								
									
										10
									
								
								hello-world/bsp/libwrap/sys/times.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								hello-world/bsp/libwrap/sys/times.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include <sys/times.h> | ||||
| #include "stub.h" | ||||
|  | ||||
| clock_t __wrap_times(struct tms* buf) | ||||
| { | ||||
|   return _stub(EACCES); | ||||
| } | ||||
							
								
								
									
										9
									
								
								hello-world/bsp/libwrap/sys/unlink.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								hello-world/bsp/libwrap/sys/unlink.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include "stub.h" | ||||
|  | ||||
| int __wrap_unlink(const char* name) | ||||
| { | ||||
|   return _stub(ENOENT); | ||||
| } | ||||
							
								
								
									
										9
									
								
								hello-world/bsp/libwrap/sys/wait.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								hello-world/bsp/libwrap/sys/wait.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include "stub.h" | ||||
|  | ||||
| int wait(int* status) | ||||
| { | ||||
|   return _stub(ECHILD); | ||||
| } | ||||
							
								
								
									
										29
									
								
								hello-world/bsp/libwrap/sys/write.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								hello-world/bsp/libwrap/sys/write.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <stdint.h> | ||||
| #include <errno.h> | ||||
| #include <unistd.h> | ||||
| #include <sys/types.h> | ||||
|  | ||||
| #include "platform.h" | ||||
| #include "stub.h" | ||||
|  | ||||
| ssize_t __wrap_write(int fd, const void* ptr, size_t len) | ||||
| { | ||||
|   const uint8_t * current = (const char *)ptr; | ||||
|  | ||||
|   if (isatty(fd)) { | ||||
|     for (size_t jj = 0; jj < len; jj++) { | ||||
|       while (UART0_REG(UART_REG_TXFIFO) & 0x80000000) ; | ||||
|       UART0_REG(UART_REG_TXFIFO) = current[jj]; | ||||
|  | ||||
|       if (current[jj] == '\n') { | ||||
|         while (UART0_REG(UART_REG_TXFIFO) & 0x80000000) ; | ||||
|         UART0_REG(UART_REG_TXFIFO) = '\r'; | ||||
|       } | ||||
|     } | ||||
|     return len; | ||||
|   } | ||||
|  | ||||
|   return _stub(EBADF); | ||||
| } | ||||
		Reference in New Issue
	
	Block a user