From 77c78538c94b49b58940610a5dd3c5cc0e7c14be Mon Sep 17 00:00:00 2001 From: JianjunJiang <8192542@qq.com> Date: Thu, 26 May 2022 17:49:59 +0800 Subject: [PATCH] [d1][f133]add sid payload for d1/f133, using word access instead of byte access --- chips/d1_f133.c | 20 +++- payloads/d1_f133/sid/.gitignore | 10 ++ payloads/d1_f133/sid/Makefile | 123 ++++++++++++++++++++++ payloads/d1_f133/sid/include/byteorder.h | 83 +++++++++++++++ payloads/d1_f133/sid/include/d1/reg-ccu.h | 68 ++++++++++++ payloads/d1_f133/sid/include/endian.h | 27 +++++ payloads/d1_f133/sid/include/io.h | 54 ++++++++++ payloads/d1_f133/sid/include/linkage.h | 24 +++++ payloads/d1_f133/sid/include/riscv64.h | 113 ++++++++++++++++++++ payloads/d1_f133/sid/include/stdarg.h | 34 ++++++ payloads/d1_f133/sid/include/stddef.h | 49 +++++++++ payloads/d1_f133/sid/include/stdint.h | 31 ++++++ payloads/d1_f133/sid/include/string.h | 17 +++ payloads/d1_f133/sid/include/types.h | 53 ++++++++++ payloads/d1_f133/sid/include/xboot.h | 21 ++++ payloads/d1_f133/sid/link.ld | 83 +++++++++++++++ payloads/d1_f133/sid/source/start.S | 54 ++++++++++ payloads/d1_f133/sid/source/sys-sid.c | 39 +++++++ 18 files changed, 899 insertions(+), 4 deletions(-) create mode 100644 payloads/d1_f133/sid/.gitignore create mode 100644 payloads/d1_f133/sid/Makefile create mode 100644 payloads/d1_f133/sid/include/byteorder.h create mode 100644 payloads/d1_f133/sid/include/d1/reg-ccu.h create mode 100644 payloads/d1_f133/sid/include/endian.h create mode 100644 payloads/d1_f133/sid/include/io.h create mode 100644 payloads/d1_f133/sid/include/linkage.h create mode 100644 payloads/d1_f133/sid/include/riscv64.h create mode 100644 payloads/d1_f133/sid/include/stdarg.h create mode 100644 payloads/d1_f133/sid/include/stddef.h create mode 100644 payloads/d1_f133/sid/include/stdint.h create mode 100644 payloads/d1_f133/sid/include/string.h create mode 100644 payloads/d1_f133/sid/include/types.h create mode 100644 payloads/d1_f133/sid/include/xboot.h create mode 100644 payloads/d1_f133/sid/link.ld create mode 100644 payloads/d1_f133/sid/source/start.S create mode 100644 payloads/d1_f133/sid/source/sys-sid.c diff --git a/chips/d1_f133.c b/chips/d1_f133.c index 25e36f0..ed62a69 100644 --- a/chips/d1_f133.c +++ b/chips/d1_f133.c @@ -77,12 +77,24 @@ static int chip_reset(struct xfel_ctx_t * ctx) static int chip_sid(struct xfel_ctx_t * ctx, char * sid) { + static const uint8_t payload[] = { + 0x37, 0x03, 0x40, 0x00, 0x73, 0x20, 0x03, 0x7c, 0x37, 0x03, 0x03, 0x00, + 0x1b, 0x03, 0x33, 0x01, 0x73, 0x20, 0x23, 0x7c, 0x6f, 0x00, 0x40, 0x00, + 0x13, 0x01, 0x01, 0xfe, 0x23, 0x34, 0x81, 0x00, 0x23, 0x38, 0x91, 0x00, + 0x23, 0x3c, 0x11, 0x00, 0x13, 0x04, 0x05, 0x00, 0x37, 0x15, 0x02, 0x00, + 0xef, 0x00, 0x00, 0x02, 0x83, 0x30, 0x81, 0x01, 0x03, 0x34, 0x81, 0x00, + 0x83, 0x34, 0x01, 0x01, 0x13, 0x01, 0x01, 0x02, 0x67, 0x80, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb7, 0x67, 0x00, 0x03, + 0x03, 0xa7, 0x07, 0x20, 0x23, 0x20, 0xe5, 0x00, 0x03, 0xa7, 0x47, 0x20, + 0x23, 0x22, 0xe5, 0x00, 0x03, 0xa7, 0x87, 0x20, 0x23, 0x24, 0xe5, 0x00, + 0x83, 0xa7, 0xc7, 0x20, 0x23, 0x26, 0xf5, 0x00, 0x67, 0x80, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; uint32_t id[4]; - id[0] = R32(0x03006200 + 0x0); - id[1] = R32(0x03006200 + 0x4); - id[2] = R32(0x03006200 + 0x8); - id[3] = R32(0x03006200 + 0xc); + fel_write(ctx, 0x00020000, (void *)&payload[0], sizeof(payload)); + fel_exec(ctx, 0x00020000); + fel_read(ctx, 0x00021000, (void *)id, sizeof(id)); sprintf(sid, "%08x%08x%08x%08x", id[0], id[1], id[2], id[3]); return 1; } diff --git a/payloads/d1_f133/sid/.gitignore b/payloads/d1_f133/sid/.gitignore new file mode 100644 index 0000000..88f9697 --- /dev/null +++ b/payloads/d1_f133/sid/.gitignore @@ -0,0 +1,10 @@ +# +# Normal rules +# +*~ + +# +# Generated files +# +/.obj +/output diff --git a/payloads/d1_f133/sid/Makefile b/payloads/d1_f133/sid/Makefile new file mode 100644 index 0000000..4bd0576 --- /dev/null +++ b/payloads/d1_f133/sid/Makefile @@ -0,0 +1,123 @@ +# +# Top makefile +# + +CROSS ?= ~/Xuantie-900-gcc-linux-5.10.4-musl64-x86_64-V2.4.0/bin/riscv64-unknown-linux-musl- +NAME := sid + +# +# System environment variable. +# +# +# System environment variable. +# +ifeq ($(OS), Windows_NT) + HOSTOS := windows +else + ifneq (,$(findstring Linux, $(shell uname -a))) + HOSTOS := linux + endif +endif + +# +# Load default variables. +# +ASFLAGS := -g -ggdb -Wall -O3 +CFLAGS := -g -ggdb -Wall -O3 +CXXFLAGS := -g -ggdb -Wall -O3 +LDFLAGS := -T link.ld -nostdlib +ARFLAGS := -rcs +OCFLAGS := -v -O binary +ODFLAGS := +MCFLAGS := -march=rv64gvxthead -mabi=lp64d -mcmodel=medany -fno-stack-protector + +LIBDIRS := +LIBS := +INCDIRS := +SRCDIRS := + +# +# Add external library +# +INCDIRS += include \ + include/external +SRCDIRS += source \ + source/external + +# +# You shouldn't need to change anything below this point. +# +AS := $(CROSS)gcc -x assembler-with-cpp +CC := $(CROSS)gcc +CXX := $(CROSS)g++ +LD := $(CROSS)ld +AR := $(CROSS)ar +OC := $(CROSS)objcopy +OD := $(CROSS)objdump +MKDIR := mkdir -p +CP := cp -af +RM := rm -fr +CD := cd +FIND := find + +# +# X variables +# +X_ASFLAGS := $(MCFLAGS) $(ASFLAGS) +X_CFLAGS := $(MCFLAGS) $(CFLAGS) +X_CXXFLAGS := $(MCFLAGS) $(CXXFLAGS) +X_LDFLAGS := $(LDFLAGS) +X_OCFLAGS := $(OCFLAGS) +X_LIBDIRS := $(LIBDIRS) +X_LIBS := $(LIBS) -lgcc + +X_OUT := output +X_NAME := $(patsubst %, $(X_OUT)/%, $(NAME)) +X_INCDIRS := $(patsubst %, -I %, $(INCDIRS)) +X_SRCDIRS := $(patsubst %, %, $(SRCDIRS)) +X_OBJDIRS := $(patsubst %, .obj/%, $(X_SRCDIRS)) + +X_SFILES := $(foreach dir, $(X_SRCDIRS), $(wildcard $(dir)/*.S)) +X_CFILES := $(foreach dir, $(X_SRCDIRS), $(wildcard $(dir)/*.c)) +X_CPPFILES := $(foreach dir, $(X_SRCDIRS), $(wildcard $(dir)/*.cpp)) + +X_SDEPS := $(patsubst %, .obj/%, $(X_SFILES:.S=.o.d)) +X_CDEPS := $(patsubst %, .obj/%, $(X_CFILES:.c=.o.d)) +X_CPPDEPS := $(patsubst %, .obj/%, $(X_CPPFILES:.cpp=.o.d)) +X_DEPS := $(X_SDEPS) $(X_CDEPS) $(X_CPPDEPS) + +X_SOBJS := $(patsubst %, .obj/%, $(X_SFILES:.S=.o)) +X_COBJS := $(patsubst %, .obj/%, $(X_CFILES:.c=.o)) +X_CPPOBJS := $(patsubst %, .obj/%, $(X_CPPFILES:.cpp=.o)) +X_OBJS := $(X_SOBJS) $(X_COBJS) $(X_CPPOBJS) + +VPATH := $(X_OBJDIRS) + +.PHONY: all clean +all : $(X_NAME) + +$(X_NAME) : $(X_OBJS) + @echo [LD] Linking $@.elf + @$(CC) $(X_LDFLAGS) $(X_LIBDIRS) -Wl,--cref,-Map=$@.map $^ -o $@.elf $(X_LIBS) + @echo [OC] Objcopying $@.bin + @$(OC) $(X_OCFLAGS) $@.elf $@.bin + +$(X_SOBJS) : .obj/%.o : %.S + @echo [AS] $< + @$(AS) $(X_ASFLAGS) -MD -MP -MF $@.d $(X_INCDIRS) -c $< -o $@ + +$(X_COBJS) : .obj/%.o : %.c + @echo [CC] $< + @$(CC) $(X_CFLAGS) -MD -MP -MF $@.d $(X_INCDIRS) -c $< -o $@ + +$(X_CPPOBJS) : .obj/%.o : %.cpp + @echo [CXX] $< + @$(CXX) $(X_CXXFLAGS) -MD -MP -MF $@.d $(X_INCDIRS) -c $< -o $@ + +clean: + @$(RM) .obj $(X_OUT) + +# +# Include the dependency files, should be place the last of makefile +# +sinclude $(shell $(MKDIR) $(X_OBJDIRS) $(X_OUT)) $(X_DEPS) diff --git a/payloads/d1_f133/sid/include/byteorder.h b/payloads/d1_f133/sid/include/byteorder.h new file mode 100644 index 0000000..f1fcf7a --- /dev/null +++ b/payloads/d1_f133/sid/include/byteorder.h @@ -0,0 +1,83 @@ +#ifndef __BYTEORDER_H__ +#define __BYTEORDER_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +static inline u16_t __swab16(u16_t x) +{ + return ( (x<<8) | (x>>8) ); +} + +static inline u32_t __swab32(u32_t x) +{ + return ( (x<<24) | (x>>24) | \ + ((x & (u32_t)0x0000ff00UL)<<8) | \ + ((x & (u32_t)0x00ff0000UL)>>8) ); +} + +static inline u64_t __swab64(u64_t x) +{ + return ( (x<<56) | (x>>56) | \ + ((x & (u64_t)0x000000000000ff00ULL)<<40) | \ + ((x & (u64_t)0x0000000000ff0000ULL)<<24) | \ + ((x & (u64_t)0x00000000ff000000ULL)<< 8) | \ + ((x & (u64_t)0x000000ff00000000ULL)>> 8) | \ + ((x & (u64_t)0x0000ff0000000000ULL)>>24) | \ + ((x & (u64_t)0x00ff000000000000ULL)>>40) ); +} + +/* + * swap bytes bizarrely. + * swahw32 - swap 16-bit half-words in a 32-bit word + */ +static inline u32_t __swahw32(u32_t x) +{ + return ( ((x & (u32_t)0x0000ffffUL)<<16) | ((x & (u32_t)0xffff0000UL)>>16) ); +} + +/* + * swap bytes bizarrely. + * swahb32 - swap 8-bit halves of each 16-bit half-word in a 32-bit word + */ +static inline u32_t __swahb32(u32_t x) +{ + return ( ((x & (u32_t)0x00ff00ffUL)<<8) | ((x & (u32_t)0xff00ff00UL)>>8) ); +} + +#if (BYTE_ORDER == BIG_ENDIAN) +#define cpu_to_le64(x) (__swab64((u64_t)(x))) +#define le64_to_cpu(x) (__swab64((u64_t)(x))) +#define cpu_to_le32(x) (__swab32((u32_t)(x))) +#define le32_to_cpu(x) (__swab32((u32_t)(x))) +#define cpu_to_le16(x) (__swab16((u16_t)(x))) +#define le16_to_cpu(x) (__swab16((u16_t)(x))) +#define cpu_to_be64(x) ((u64_t)(x)) +#define be64_to_cpu(x) ((u64_t)(x)) +#define cpu_to_be32(x) ((u32_t)(x)) +#define be32_to_cpu(x) ((u32_t)(x)) +#define cpu_to_be16(x) ((u16_t)(x)) +#define be16_to_cpu(x) ((u16_t)(x)) +#else +#define cpu_to_le64(x) ((u64_t)(x)) +#define le64_to_cpu(x) ((u64_t)(x)) +#define cpu_to_le32(x) ((u32_t)(x)) +#define le32_to_cpu(x) ((u32_t)(x)) +#define cpu_to_le16(x) ((u16_t)(x)) +#define le16_to_cpu(x) ((u16_t)(x)) +#define cpu_to_be64(x) (__swab64((u64_t)(x))) +#define be64_to_cpu(x) (__swab64((u64_t)(x))) +#define cpu_to_be32(x) (__swab32((u32_t)(x))) +#define be32_to_cpu(x) (__swab32((u32_t)(x))) +#define cpu_to_be16(x) (__swab16((u16_t)(x))) +#define be16_to_cpu(x) (__swab16((u16_t)(x))) +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* __BYTEORDER_H__ */ diff --git a/payloads/d1_f133/sid/include/d1/reg-ccu.h b/payloads/d1_f133/sid/include/d1/reg-ccu.h new file mode 100644 index 0000000..6130398 --- /dev/null +++ b/payloads/d1_f133/sid/include/d1/reg-ccu.h @@ -0,0 +1,68 @@ +#ifndef __D1_REG_CCU_H__ +#define __D1_REG_CCU_H__ + +#define D1_CCU_BASE (0x02001000) + +#define CCU_PLL_CPU_CTRL_REG (0x000) +#define CCU_PLL_DDR_CTRL_REG (0x010) +#define CCU_PLL_PERI0_CTRL_REG (0x020) +#define CCU_PLL_PERI1_CTRL_REG (0x028) +#define CCU_PLL_GPU_CTRL_REG (0x030) +#define CCU_PLL_VIDEO0_CTRL_REG (0x040) +#define CCU_PLL_VIDEO1_CTRL_REG (0x048) +#define CCU_PLL_VE_CTRL (0x058) +#define CCU_PLL_DE_CTRL (0x060) +#define CCU_PLL_HSIC_CTRL (0x070) +#define CCU_PLL_AUDIO0_CTRL_REG (0x078) +#define CCU_PLL_AUDIO1_CTRL_REG (0x080) +#define CCU_PLL_DDR_PAT0_CTRL_REG (0x110) +#define CCU_PLL_DDR_PAT1_CTRL_REG (0x114) +#define CCU_PLL_PERI0_PAT0_CTRL_REG (0x120) +#define CCU_PLL_PERI0_PAT1_CTRL_REG (0x124) +#define CCU_PLL_PERI1_PAT0_CTRL_REG (0x128) +#define CCU_PLL_PERI1_PAT1_CTRL_REG (0x12c) +#define CCU_PLL_GPU_PAT0_CTRL_REG (0x130) +#define CCU_PLL_GPU_PAT1_CTRL_REG (0x134) +#define CCU_PLL_VIDEO0_PAT0_CTRL_REG (0x140) +#define CCU_PLL_VIDEO0_PAT1_CTRL_REG (0x144) +#define CCU_PLL_VIDEO1_PAT0_CTRL_REG (0x148) +#define CCU_PLL_VIDEO1_PAT1_CTRL_REG (0x14c) +#define CCU_PLL_VE_PAT0_CTRL_REG (0x158) +#define CCU_PLL_VE_PAT1_CTRL_REG (0x15c) +#define CCU_PLL_DE_PAT0_CTRL_REG (0x160) +#define CCU_PLL_DE_PAT1_CTRL_REG (0x164) +#define CCU_PLL_HSIC_PAT0_CTRL_REG (0x170) +#define CCU_PLL_HSIC_PAT1_CTRL_REG (0x174) +#define CCU_PLL_AUDIO0_PAT0_CTRL_REG (0x178) +#define CCU_PLL_AUDIO0_PAT1_CTRL_REG (0x17c) +#define CCU_PLL_AUDIO1_PAT0_CTRL_REG (0x180) +#define CCU_PLL_AUDIO1_PAT1_CTRL_REG (0x184) +#define CCU_PLL_CPU_BIAS_REG (0x300) +#define CCU_PLL_DDR_BIAS_REG (0x310) +#define CCU_PLL_PERI0_BIAS_REG (0x320) +#define CCU_PLL_PERI1_BIAS_REG (0x328) +#define CCU_PLL_GPU_BIAS_REG (0x330) +#define CCU_PLL_VIDEO0_BIAS_REG (0x340) +#define CCU_PLL_VIDEO1_BIAS_REG (0x348) +#define CCU_PLL_VE_BIAS_REG (0x358) +#define CCU_PLL_DE_BIAS_REG (0x360) +#define CCU_PLL_HSIC_BIAS_REG (0x370) +#define CCU_PLL_AUDIO0_BIAS_REG (0x378) +#define CCU_PLL_AUDIO1_BIAS_REG (0x380) +#define CCU_PLL_CPU_TUN_REG (0x400) +#define CCU_CPU_AXI_CFG_REG (0x500) +#define CCU_CPU_GATING_REG (0x504) +#define CCU_PSI_CLK_REG (0x510) +#define CCU_AHB3_CLK_REG (0x51c) +#define CCU_APB0_CLK_REG (0x520) +#define CCU_APB1_CLK_REG (0x524) +#define CCU_MBUS_CLK_REG (0x540) +#define CCU_DMA_BGR_REG (0x70c) +#define CCU_DRAM_CLK_REG (0x800) +#define CCU_MBUS_MAT_CLK_GATING_REG (0x804) +#define CCU_DRAM_BGR_REG (0x80c) +#define CCU_RISCV_CLK_REG (0xd00) +#define CCU_RISCV_GATING_REG (0xd04) +#define CCU_RISCV_CFG_BGR_REG (0xd0c) + +#endif /* __D1_REG_CCU_H__ */ diff --git a/payloads/d1_f133/sid/include/endian.h b/payloads/d1_f133/sid/include/endian.h new file mode 100644 index 0000000..9ee5a73 --- /dev/null +++ b/payloads/d1_f133/sid/include/endian.h @@ -0,0 +1,27 @@ +#ifndef __RISCV64_ENDIAN_H__ +#define __RISCV64_ENDIAN_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#define LITTLE_ENDIAN (0x1234) +#define BIG_ENDIAN (0x4321) + +#if ( !defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN) ) +#define __LITTLE_ENDIAN +#endif + +#if defined(__LITTLE_ENDIAN) +#define BYTE_ORDER LITTLE_ENDIAN +#elif defined(__BIG_ENDIAN) +#define BYTE_ORDER BIG_ENDIAN +#else +#error "Unknown byte order!" +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* __RISCV64_ENDIAN_H__ */ diff --git a/payloads/d1_f133/sid/include/io.h b/payloads/d1_f133/sid/include/io.h new file mode 100644 index 0000000..fbf2562 --- /dev/null +++ b/payloads/d1_f133/sid/include/io.h @@ -0,0 +1,54 @@ +#ifndef __IO_H__ +#define __IO_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +static inline u8_t read8(virtual_addr_t addr) +{ + return( *((volatile u8_t *)(addr)) ); +} + +static inline u16_t read16(virtual_addr_t addr) +{ + return( *((volatile u16_t *)(addr)) ); +} + +static inline u32_t read32(virtual_addr_t addr) +{ + return( *((volatile u32_t *)(addr)) ); +} + +static inline u64_t read64(virtual_addr_t addr) +{ + return( *((volatile u64_t *)(addr)) ); +} + +static inline void write8(virtual_addr_t addr, u8_t value) +{ + *((volatile u8_t *)(addr)) = value; +} + +static inline void write16(virtual_addr_t addr, u16_t value) +{ + *((volatile u16_t *)(addr)) = value; +} + +static inline void write32(virtual_addr_t addr, u32_t value) +{ + *((volatile u32_t *)(addr)) = value; +} + +static inline void write64(virtual_addr_t addr, u64_t value) +{ + *((volatile u64_t *)(addr)) = value; +} + +#ifdef __cplusplus +} +#endif + +#endif /* __IO_H__ */ diff --git a/payloads/d1_f133/sid/include/linkage.h b/payloads/d1_f133/sid/include/linkage.h new file mode 100644 index 0000000..5fb691d --- /dev/null +++ b/payloads/d1_f133/sid/include/linkage.h @@ -0,0 +1,24 @@ +#ifndef __RISCV64_LINKAGE_H__ +#define __RISCV64_LINKAGE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#if __riscv_xlen == 64 +#define LREG ld +#define SREG sd +#define REGSZ 8 +#define RVPTR .dword +#elif __riscv_xlen == 32 +#define LREG lw +#define SREG sw +#define REGSZ 4 +#define RVPTR .word +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* __RISCV64_LINKAGE_H__ */ diff --git a/payloads/d1_f133/sid/include/riscv64.h b/payloads/d1_f133/sid/include/riscv64.h new file mode 100644 index 0000000..d797ce7 --- /dev/null +++ b/payloads/d1_f133/sid/include/riscv64.h @@ -0,0 +1,113 @@ +#ifndef __RISCV64_H__ +#define __RISCV64_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#define MSTATUS_UIE (1 << 0) +#define MSTATUS_SIE (1 << 1) +#define MSTATUS_MIE (1 << 3) +#define MSTATUS_UPIE (1 << 4) +#define MSTATUS_SPIE (1 << 5) +#define MSTATUS_MPIE (1 << 7) +#define MSTATUS_SPP (1 << 8) +#define MSTATUS_MPP (3 << 11) +#define MSTATUS_FS (3 << 13) +#define MSTATUS_XS (3 << 15) +#define MSTATUS_MPRV (1 << 17) +#define MSTATUS_SUM (1 << 18) +#define MSTATUS_MXR (1 << 19) +#define MSTATUS_TVM (1 << 20) +#define MSTATUS_TW (1 << 21) +#define MSTATUS_TSR (1 << 22) +#define MSTATUS32_SD (1 << 31) +#define MSTATUS_UXL (3ULL << 32) +#define MSTATUS_SXL (3ULL << 34) +#define MSTATUS64_SD (1ULL << 63) + +#define MIP_USIP (1 << 0) +#define MIP_SSIP (1 << 1) +#define MIP_MSIP (1 << 3) +#define MIP_UTIP (1 << 4) +#define MIP_STIP (1 << 5) +#define MIP_MTIP (1 << 7) +#define MIP_UEIP (1 << 8) +#define MIP_SEIP (1 << 9) +#define MIP_MEIP (1 << 11) + +#define MIE_USIE (1 << 0) +#define MIE_SSIE (1 << 1) +#define MIE_MSIE (1 << 3) +#define MIE_UTIE (1 << 4) +#define MIE_STIE (1 << 5) +#define MIE_MTIE (1 << 7) +#define MIE_UEIE (1 << 8) +#define MIE_SEIE (1 << 9) +#define MIE_MEIE (1 << 11) + +#define csr_swap(csr, val) \ +({ \ + unsigned long __v = (unsigned long)(val); \ + __asm__ __volatile__ ("csrrw %0, " #csr ", %1" \ + : "=r" (__v) : "rK" (__v) \ + : "memory"); \ + __v; \ +}) + +#define csr_read(csr) \ +({ \ + register unsigned long __v; \ + __asm__ __volatile__ ("csrr %0, " #csr \ + : "=r" (__v) : \ + : "memory"); \ + __v; \ +}) + +#define csr_write(csr, val) \ +({ \ + unsigned long __v = (unsigned long)(val); \ + __asm__ __volatile__ ("csrw " #csr ", %0" \ + : : "rK" (__v) \ + : "memory"); \ +}) + +#define csr_read_set(csr, val) \ +({ \ + unsigned long __v = (unsigned long)(val); \ + __asm__ __volatile__ ("csrrs %0, " #csr ", %1" \ + : "=r" (__v) : "rK" (__v) \ + : "memory"); \ + __v; \ +}) + +#define csr_set(csr, val) \ +({ \ + unsigned long __v = (unsigned long)(val); \ + __asm__ __volatile__ ("csrs " #csr ", %0" \ + : : "rK" (__v) \ + : "memory"); \ +}) + +#define csr_read_clear(csr, val) \ +({ \ + unsigned long __v = (unsigned long)(val); \ + __asm__ __volatile__ ("csrrc %0, " #csr ", %1" \ + : "=r" (__v) : "rK" (__v) \ + : "memory"); \ + __v; \ +}) + +#define csr_clear(csr, val) \ +({ \ + unsigned long __v = (unsigned long)(val); \ + __asm__ __volatile__ ("csrc " #csr ", %0" \ + : : "rK" (__v) \ + : "memory"); \ +}) + +#ifdef __cplusplus +} +#endif + +#endif /* __RISCV64_H__ */ diff --git a/payloads/d1_f133/sid/include/stdarg.h b/payloads/d1_f133/sid/include/stdarg.h new file mode 100644 index 0000000..074c489 --- /dev/null +++ b/payloads/d1_f133/sid/include/stdarg.h @@ -0,0 +1,34 @@ +#ifndef __STDARG_H__ +#define __STDARG_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef __builtin_va_list va_list; + +/* + * prepare to access variable args + */ +#define va_start(v, l) __builtin_va_start(v, l) + +/* + * the caller will get the value of current argument + */ +#define va_arg(v, l) __builtin_va_arg(v, l) + +/* + * end for variable args + */ +#define va_end(v) __builtin_va_end(v) + +/* + * copy variable args + */ +#define va_copy(d, s) __builtin_va_copy(d, s) + +#ifdef __cplusplus +} +#endif + +#endif /* __STDARG_H__ */ diff --git a/payloads/d1_f133/sid/include/stddef.h b/payloads/d1_f133/sid/include/stddef.h new file mode 100644 index 0000000..ca3874c --- /dev/null +++ b/payloads/d1_f133/sid/include/stddef.h @@ -0,0 +1,49 @@ +#ifndef __STDDEF_H__ +#define __STDDEF_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(__cplusplus) +#define NULL (0) +#else +#define NULL ((void *)0) +#endif + +#if (defined(__GNUC__) && (__GNUC__ >= 4)) +#define offsetof(type, member) __builtin_offsetof(type, member) +#else +#define offsetof(type, field) ((size_t)(&((type *)0)->field)) +#endif +#define container_of(ptr, type, member) ({const typeof(((type *)0)->member) *__mptr = (ptr); (type *)((char *)__mptr - offsetof(type,member));}) + +#if (defined(__GNUC__) && (__GNUC__ >= 3)) +#define likely(expr) (__builtin_expect(!!(expr), 1)) +#define unlikely(expr) (__builtin_expect(!!(expr), 0)) +#else +#define likely(expr) (!!(expr)) +#define unlikely(expr) (!!(expr)) +#endif + +#define min(a, b) ({typeof(a) _amin = (a); typeof(b) _bmin = (b); (void)(&_amin == &_bmin); _amin < _bmin ? _amin : _bmin;}) +#define max(a, b) ({typeof(a) _amax = (a); typeof(b) _bmax = (b); (void)(&_amax == &_bmax); _amax > _bmax ? _amax : _bmax;}) +#define clamp(v, a, b) min(max(a, v), b) + +#define ifloor(x) ((x) > 0 ? (int)(x) : (int)((x) - 0.9999999999)) +#define iround(x) ((x) > 0 ? (int)((x) + 0.5) : (int)((x) - 0.5)) +#define iceil(x) ((x) > 0 ? (int)((x) + 0.9999999999) : (int)(x)) +#define idiv255(x) ((((int)(x) + 1) * 257) >> 16) + +#define X(...) ("" #__VA_ARGS__ "") + +enum { + FALSE = 0, + TRUE = 1, +}; + +#ifdef __cplusplus +} +#endif + +#endif /* __STDDEF_H__ */ diff --git a/payloads/d1_f133/sid/include/stdint.h b/payloads/d1_f133/sid/include/stdint.h new file mode 100644 index 0000000..41c8de3 --- /dev/null +++ b/payloads/d1_f133/sid/include/stdint.h @@ -0,0 +1,31 @@ +#ifndef __STDINT_H__ +#define __STDINT_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +typedef s8_t int8_t; +typedef u8_t uint8_t; + +typedef s16_t int16_t; +typedef u16_t uint16_t; + +typedef s32_t int32_t; +typedef u32_t uint32_t; + +typedef s64_t int64_t; +typedef u64_t uint64_t; + +#define UINT8_MAX (0xff) +#define UINT16_MAX (0xffff) +#define UINT32_MAX (0xffffffff) +#define UINT64_MAX (0xffffffffffffffffULL) + +#ifdef __cplusplus +} +#endif + +#endif /* __STDINT_H__ */ diff --git a/payloads/d1_f133/sid/include/string.h b/payloads/d1_f133/sid/include/string.h new file mode 100644 index 0000000..126d16e --- /dev/null +++ b/payloads/d1_f133/sid/include/string.h @@ -0,0 +1,17 @@ +#ifndef __STRING_H__ +#define __STRING_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +void * memset(void * s, int c, size_t n); +void * memcpy(void * dest, const void * src, size_t len); + +#ifdef __cplusplus +} +#endif + +#endif /* __STRING_H__ */ diff --git a/payloads/d1_f133/sid/include/types.h b/payloads/d1_f133/sid/include/types.h new file mode 100644 index 0000000..5ee16ec --- /dev/null +++ b/payloads/d1_f133/sid/include/types.h @@ -0,0 +1,53 @@ +#ifndef __RISCV64_TYPES_H__ +#define __RISCV64_TYPES_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef signed char s8_t; +typedef unsigned char u8_t; + +typedef signed short s16_t; +typedef unsigned short u16_t; + +typedef signed int s32_t; +typedef unsigned int u32_t; + +typedef signed long long s64_t; +typedef unsigned long long u64_t; + +typedef signed long long intmax_t; +typedef unsigned long long uintmax_t; + +typedef signed long long ptrdiff_t; +typedef signed long long intptr_t; +typedef unsigned long long uintptr_t; + +typedef unsigned long long size_t; +typedef signed long long ssize_t; + +typedef signed long off_t; +typedef signed long long loff_t; + +typedef signed int bool_t; +typedef unsigned long irq_flags_t; + +typedef unsigned long long virtual_addr_t; +typedef unsigned long long virtual_size_t; +typedef unsigned long long physical_addr_t; +typedef unsigned long long physical_size_t; + +typedef struct { + volatile int counter; +} atomic_t; + +typedef struct { + volatile int lock; +} spinlock_t; + +#ifdef __cplusplus +} +#endif + +#endif /* __RISCV64_TYPES_H__ */ diff --git a/payloads/d1_f133/sid/include/xboot.h b/payloads/d1_f133/sid/include/xboot.h new file mode 100644 index 0000000..d95c352 --- /dev/null +++ b/payloads/d1_f133/sid/include/xboot.h @@ -0,0 +1,21 @@ +#ifndef __XBOOT_H__ +#define __XBOOT_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __cplusplus +} +#endif + +#endif /* __XBOOT_H__ */ diff --git a/payloads/d1_f133/sid/link.ld b/payloads/d1_f133/sid/link.ld new file mode 100644 index 0000000..481a141 --- /dev/null +++ b/payloads/d1_f133/sid/link.ld @@ -0,0 +1,83 @@ +OUTPUT_FORMAT("elf64-littleriscv", "elf64-littleriscv", "elf64-littleriscv") +OUTPUT_ARCH(riscv) +ENTRY(_start) + +STACK_SIZE = 0x400; + +MEMORY +{ + ram : org = 0x00020000, len = 0x00001000 /* 4 KB */ +} + +SECTIONS +{ + .text : + { + PROVIDE(__image_start = .); + PROVIDE(__text_start = .); + .obj/source/start.o (.text*) + .obj/source/sys-sid.o (.text*) + *(.text*) + *(.iplt) + *(.note.gnu.build-id) + PROVIDE(__text_end = .); + } > ram + + .ksymtab ALIGN(16) : + { + PROVIDE(__ksymtab_start = .); + KEEP(*(.ksymtab.text)) + PROVIDE(__ksymtab_end = .); + } > ram + + .romdisk ALIGN(8) : + { + PROVIDE(__romdisk_start = .); + KEEP(*(.romdisk)) + PROVIDE(__romdisk_end = .); + } > ram + + .rodata ALIGN(8) : + { + PROVIDE(__rodata_start = .); + *(.rodata*) + *(.srodata*) + PROVIDE(__rodata_end = .); + } > ram + + .data ALIGN(8) : + { + PROVIDE(__data_start = .); + PROVIDE(__global_pointer$ = . + 0x800); + *(.sdata*) + *(.data*) + . = ALIGN(8); + PROVIDE(__data_end = .); + PROVIDE(__image_end = .); + } > ram + + .bss ALIGN(8) (NOLOAD) : + { + PROVIDE(__bss_start = .); + *(.bss*) + *(.sbss*) + *(COMMON) + . = ALIGN(8); + PROVIDE(__bss_end = .); + } > ram + + .stack ALIGN(16) (NOLOAD) : + { + PROVIDE(__stack_start = .); + . += STACK_SIZE; + . = ALIGN(16); + PROVIDE(__stack_end = .); + } > ram + + /DISCARD/ : { *(.dynsym) } + /DISCARD/ : { *(.dynstr*) } + /DISCARD/ : { *(.dynamic*) } + /DISCARD/ : { *(.plt*) } + /DISCARD/ : { *(.interp*) } + /DISCARD/ : { *(.gnu*) } +} diff --git a/payloads/d1_f133/sid/source/start.S b/payloads/d1_f133/sid/source/start.S new file mode 100644 index 0000000..150f5ee --- /dev/null +++ b/payloads/d1_f133/sid/source/start.S @@ -0,0 +1,54 @@ +/* + * start.S + * + * Copyright(c) 2007-2021 Jianjun Jiang <8192542@qq.com> + * Official site: http://xboot.org + * Mobile phone: +86-18665388956 + * QQ: 8192542 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +#include +#include + + .global _start +_start: + li t1, 0x1 << 22 + csrs mxstatus, t1 + li t1, 0x30013 + csrs mcor, t1 + j reset + +reset: + addi sp, sp, -32 + sd s0, 8(sp) + sd s1, 16(sp) + sd ra, 24(sp) + mv s0, a0 + + li a0, 0x00021000 + jal sys_read_sid + + ld ra, 24(sp) + ld s0, 8(sp) + ld s1, 16(sp) + addi sp, sp, 32 + ret diff --git a/payloads/d1_f133/sid/source/sys-sid.c b/payloads/d1_f133/sid/source/sys-sid.c new file mode 100644 index 0000000..3f8192d --- /dev/null +++ b/payloads/d1_f133/sid/source/sys-sid.c @@ -0,0 +1,39 @@ +/* + * sys-sid.c + * + * Copyright(c) 2007-2021 Jianjun Jiang <8192542@qq.com> + * Official site: http://xboot.org + * Mobile phone: +86-18665388956 + * QQ: 8192542 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +#include + +void sys_read_sid(u32_t * sid) +{ + virtual_addr_t virt = 0x03006200; + + sid[0] = read32(virt + 0 * 4); + sid[1] = read32(virt + 1 * 4); + sid[2] = read32(virt + 2 * 4); + sid[3] = read32(virt + 3 * 4); +}