Added minimalistic printf UART support (enabled with `DEBUG_UART`).

pull/255/head
David Garske 2022-11-22 10:27:23 -08:00 committed by Daniele Lacamera
parent bfed41889b
commit bc89cb6594
4 changed files with 137 additions and 22 deletions

17
arch.mk
View File

@ -176,26 +176,21 @@ endif
## RISCV ## RISCV
ifeq ($(ARCH),RISCV) ifeq ($(ARCH),RISCV)
CROSS_COMPILE?=riscv32-unknown-elf- CROSS_COMPILE?=riscv32-unknown-elf-
CFLAGS+=-DUSE_M_TIME -g -march=rv32imac -mabi=ilp32 -mcmodel=medany -nostartfiles -DARCH_RISCV CFLAGS+=-fno-builtin-printf -DUSE_M_TIME -g -march=rv32imac -mabi=ilp32 -mcmodel=medany -nostartfiles -DARCH_RISCV
LDFLAGS+=-march=rv32imac -mabi=ilp32 -mcmodel=medany LDFLAGS+=-march=rv32imac -mabi=ilp32 -mcmodel=medany
MATH_OBJS += ./lib/wolfssl/wolfcrypt/src/sp_c32.o MATH_OBJS += ./lib/wolfssl/wolfcrypt/src/sp_c32.o
ifeq ($(DEBUG_UART),0)
CFLAGS+=-fno-builtin-printf
endif
# Prune unused functions and data # Prune unused functions and data
CFLAGS +=-ffunction-sections -fdata-sections CFLAGS +=-ffunction-sections -fdata-sections
LDFLAGS+=-Wl,--gc-sections LDFLAGS+=-Wl,--gc-sections
OBJS+=src/boot_riscv.o src/vector_riscv.o OBJS+=src/boot_riscv.o src/vector_riscv.o
ARCH_FLASH_OFFSET?=0x20010000 ARCH_FLASH_OFFSET=0x20010000
endif endif
# powerpc # powerpc
ifeq ($(ARCH),PPC) ifeq ($(ARCH),PPC)
CROSS_COMPILE?=powerpc-linux-gnu- CROSS_COMPILE?=powerpc-linux-gnu-
CFLAGS+=-g -nostartfiles
LDFLAGS+=-Wl,--build-id=none LDFLAGS+=-Wl,--build-id=none
ifeq ($(DEBUG_UART),0) ifeq ($(DEBUG_UART),0)
@ -267,8 +262,12 @@ ifeq ($(TARGET),t2080)
# Power PC big endian # Power PC big endian
ARCH_FLAGS=-m32 -mhard-float -mcpu=e6500 ARCH_FLAGS=-m32 -mhard-float -mcpu=e6500
CFLAGS+=$(ARCH_FLAGS) -DBIG_ENDIAN_ORDER CFLAGS+=$(ARCH_FLAGS) -DBIG_ENDIAN_ORDER
CFLAGS+=-DMMU -DWOLFBOOT_DUALBOOT -pipe -feliminate-unused-debug-types CFLAGS+=-DMMU -DWOLFBOOT_DUALBOOT
LDFLAGS+=$(ARCH_FLAGS) -Wl,--hash-style=gnu -Wl,--as-needed CFLAGS+=-pipe # use pipes instead of temp files
CFLAGS+=-feliminate-unused-debug-types
LDFLAGS+=$(ARCH_FLAGS)
LDFLAGS+=-Wl,--hash-style=both # generate both sysv and gnu symbol hash table
LDFLAGS+=-Wl,--as-needed # remove weak functions not used
UPDATE_OBJS:=src/update_ram.o UPDATE_OBJS:=src/update_ram.o
ifeq ($(SPMATH),1) ifeq ($(SPMATH),1)
MATH_OBJS += ./lib/wolfssl/wolfcrypt/src/sp_c32.o MATH_OBJS += ./lib/wolfssl/wolfcrypt/src/sp_c32.o

View File

@ -213,6 +213,10 @@ enum ifc_amask_sizes {
#define SATA_ENBL (*(volatile uint32_t *)(0xB1003F4C)) /* also saw 0xB4003F4C */ #define SATA_ENBL (*(volatile uint32_t *)(0xB1003F4C)) /* also saw 0xB4003F4C */
/* DDR */
/* NAII 68PPC2 - 8GB discrete DDR3 IM8G08D3EBDG-15E */
#ifdef DEBUG_UART #ifdef DEBUG_UART
static void uart_init(void) static void uart_init(void)
{ {
@ -239,13 +243,13 @@ static void uart_init(void)
UART_LCR(UART_SEL) = (UART_LCR_WLS); UART_LCR(UART_SEL) = (UART_LCR_WLS);
} }
static void uart_write(const char* buf, uint32_t sz) void uart_write(const char* buf, uint32_t sz)
{ {
uint32_t pos = 0; uint32_t pos = 0;
while (sz-- > 0) { while (sz-- > 0) {
while (!(UART_LSR(UART_SEL) & UART_LSR_THRE)) while (!(UART_LSR(UART_SEL) & UART_LSR_THRE))
; ;
UART_THR(0) = buf[pos++]; UART_THR(UART_SEL) = buf[pos++];
} }
} }
#endif /* DEBUG_UART */ #endif /* DEBUG_UART */
@ -352,6 +356,7 @@ void hal_init(void)
uart_write("CPLD FW Rev: 0x", 15); uart_write("CPLD FW Rev: 0x", 15);
tohexstr(fw, buf); tohexstr(fw, buf);
uart_write(buf, (uint32_t)sizeof(buf));
uart_write("\n", 1); uart_write("\n", 1);
#endif #endif

View File

@ -25,11 +25,16 @@
#define WOLFBOOT_PRINTF_INCLUDED #define WOLFBOOT_PRINTF_INCLUDED
#if defined(DEBUG_ZYNQ) && !defined(PRINTF_ENABLED) #if defined(DEBUG_ZYNQ) && !defined(PRINTF_ENABLED)
# define PRINTF_ENABLED # define PRINTF_ENABLED
#endif #endif
#if defined(WOLFBOOT_DEBUG_EFI) && !defined(PRINTF_ENABLED) #if defined(WOLFBOOT_DEBUG_EFI) && !defined(PRINTF_ENABLED)
# define PRINTF_ENABLED # define PRINTF_ENABLED
#endif
#if defined(DEBUG_UART) && !defined(PRINTF_ENABLED) && !defined(NO_PRINTF_UART)
# define PRINTF_ENABLED
void uart_write(const char* buf, unsigned int sz);
#endif #endif
#ifdef PRINTF_ENABLED #ifdef PRINTF_ENABLED
@ -42,6 +47,10 @@
# include "efi/efilib.h" # include "efi/efilib.h"
/* NOTE: %s arguments will not work as EFI uses widechar string */ /* NOTE: %s arguments will not work as EFI uses widechar string */
# define wolfBoot_printf(_f_, ...) Print(L##_f_, ##__VA_ARGS__) # define wolfBoot_printf(_f_, ...) Print(L##_f_, ##__VA_ARGS__)
# elif defined(DEBUG_UART)
/* use minimal printf support in string.h */
void uart_printf(const char* fmt, ...);
# define wolfBoot_printf(_f_, ...) uart_printf(_f_, ##__VA_ARGS__)
# else # else
# define wolfBoot_printf(_f_, ...) printf(_f_, ##__VA_ARGS__) # define wolfBoot_printf(_f_, ...) printf(_f_, ##__VA_ARGS__)
# endif # endif

View File

@ -26,6 +26,13 @@
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
#ifdef DEBUG_UART
#include "printf.h"
#ifdef PRINTF_ENABLED
#include <stdarg.h>
#endif
#endif
int islower(int c) int islower(int c)
{ {
return (c >= 'a' && c <= 'z'); return (c >= 'a' && c <= 'z');
@ -54,13 +61,13 @@ int isalpha(int c)
#if !defined(__IAR_SYSTEMS_ICC__) && !defined(PLATFORM_X86_64_EFI) #if !defined(__IAR_SYSTEMS_ICC__) && !defined(PLATFORM_X86_64_EFI)
void *memset(void *s, int c, size_t n) void *memset(void *s, int c, size_t n)
{ {
unsigned char *d = (unsigned char *)s; unsigned char *d = (unsigned char *)s;
while (n--) { while (n--) {
*d++ = (unsigned char)c; *d++ = (unsigned char)c;
} }
return s; return s;
} }
#endif /* IAR */ #endif /* IAR */
@ -87,7 +94,7 @@ int strcmp(const char *s1, const char *s2)
s2++; s2++;
} }
return diff; return diff;
} }
int strcasecmp(const char *s1, const char *s2) int strcasecmp(const char *s1, const char *s2)
@ -104,7 +111,7 @@ int strcasecmp(const char *s1, const char *s2)
s2++; s2++;
} }
return diff; return diff;
} }
int strncasecmp(const char *s1, const char *s2, size_t n) int strncasecmp(const char *s1, const char *s2, size_t n)
@ -123,7 +130,7 @@ int strncasecmp(const char *s1, const char *s2, size_t n)
if (++i > n) if (++i > n)
break; break;
} }
return diff; return diff;
} }
size_t strlen(const char *s) size_t strlen(const char *s)
@ -225,7 +232,7 @@ int memcmp(const void *_s1, const void *_s2, size_t n)
n--; n--;
} }
return diff; return diff;
} }
#ifndef __IAR_SYSTEMS_ICC__ #ifndef __IAR_SYSTEMS_ICC__
@ -246,3 +253,98 @@ void *memmove(void *dst, const void *src, size_t n)
} }
} }
#endif #endif
#if defined(PRINTF_ENABLED) && defined(DEBUG_UART)
/* minimal printf for 32-bit */
void uart_writenum(int num, int base)
{
int i = 0;
char buf[sizeof(int)*2+1+1];
const char* kDigitLut = "0123456789ABCDEF";
unsigned int val = (unsigned int)num;
if (base == 10 && num < 0) { /* handle negative */
buf[i++] = '-';
val = -num;
}
do {
buf[i++] = kDigitLut[(val % base)];
val /= base;
} while (val > 0U);
uart_write(buf, i);
}
void uart_vprintf(const char* fmt, va_list argp)
{
char* fmtp = (char*)fmt;
while (fmtp != NULL && *fmtp != '\0') {
/* print non formatting characters */
if (*fmtp != '%') {
uart_write(fmtp++, 1);
continue;
}
fmtp++; /* skip % */
/* find formatters */
while (*fmtp != '\0') {
if (*fmtp >= '0' && *fmtp <= '9') {
/* length formatter - skip */
fmtp++;
}
else if (*fmtp == 'l') {
/* long - skip */
fmtp++;
}
else if (*fmtp == 'z') {
/* auto type - skip */
fmtp++;
}
else {
break;
}
}
switch (*fmtp) {
case '%':
uart_write(fmtp, 1);
break;
case 'u':
case 'i':
case 'd':
{
int n = (int)va_arg(argp, int);
uart_writenum(n, 10);
break;
}
case 'x':
case 'p':
{
int n = (int)va_arg(argp, int);
uart_writenum(n, 16);
break;
}
case 's':
{
char* str = (char*)va_arg(argp, char*);
uart_write(str, (uint32_t)strlen(str));
break;
}
case 'c':
{
char c = (char)va_arg(argp, int);
uart_write(&c, 1);
break;
}
default:
break;
}
fmtp++;
};
}
void uart_printf(const char* fmt, ...)
{
va_list argp;
va_start(argp, fmt);
uart_vprintf(fmt, argp);
va_end(argp);
}
#endif /* PRINTF_ENABLED && DEBUG_UART */