From 753d86014bb9c16fcfb2432b99b94d3e9670a56e Mon Sep 17 00:00:00 2001 From: arvinzzz Date: Sat, 4 Dec 2021 04:50:49 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E6=94=AF=E6=8C=81newlib=E4=B8=8Emus?= =?UTF-8?q?l=E5=88=87=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 系统支持newlib,并且可以与musl-C自由切换 1. 新增newlib支持,适配newlib的预留钩子,针对系统能力适配newlib头文件(补充宏定义,结构体定义,posix能力开关), 采用标准C与自研posix接口声明与结构体定义混合作为C库 2. 梳理kal的依赖关系,kal作为内核对外提供的标准接口,下属包含cmsis、libc、posix。 cmsis为内核对cmsis接口的支持,libc为内核对三方C库的适配及使用,posix为内核自研提供的posix标准接口。 若采用musl-C,则kal中选取libc/musl,posix共同作为C库对外支持; 若采用newlib-C,则kal中选取libc/newlib,部分posix共同作为C库对外支持。 3. fs整理,components/fs下提供基于posix标准的接口改为内部对外接口,供musl以及newlib切换 适配使用 close: #I4ENQ1 Signed-off-by: arvinzzz Change-Id: I72eda5ac5499f72c67e800e22b0f39eb288f2b94 --- BUILD.gn | 9 +- Kconfig | 44 +-- arch/Kconfig | 7 + arch/arm/cortex-m3/keil/los_interrupt.c | 4 + arch/arm/cortex-m33/gcc/NTZ/los_interrupt.c | 4 + .../gcc/TZ/non_secure/los_interrupt.c | 4 + arch/arm/cortex-m4/gcc/los_interrupt.c | 4 + arch/arm/cortex-m4/iar/los_interrupt.c | 4 + arch/arm/cortex-m7/gcc/los_interrupt.c | 4 + components/fs/BUILD.gn | 23 +- components/fs/Kconfig | 42 +++ components/fs/fatfs/Kconfig | 31 ++- components/fs/littlefs/Kconfig | 44 +++ components/fs/littlefs/lfs_api.h | 2 - components/fs/vfs/BUILD.gn | 40 +++ components/fs/vfs/Kconfig | 34 +++ components/fs/{ => vfs}/fs_operations.h | 0 components/fs/{fs.c => vfs/los_fs.c} | 48 ++-- components/fs/vfs/los_fs.h | 101 +++++++ components/fs/{ => vfs}/vfs_config.h | 12 +- kal/BUILD.gn | 18 +- kal/Kconfig | 32 +++ kal/cmsis/BUILD.gn | 2 +- kal/cmsis/Kconfig | 36 +++ kal/libc/BUILD.gn | 38 +++ kal/libc/Kconfig | 44 +++ kal/libc/musl/BUILD.gn | 46 +++ kal/libc/musl/Kconfig | 39 +++ kal/libc/musl/fs.c | 146 ++++++++++ kal/{posix/src => libc/musl}/malloc.c | 160 +++++------ kal/libc/newlib/BUILD.gn | 50 ++++ kal/libc/newlib/Kconfig | 39 +++ kal/libc/newlib/porting/include/dirent.h | 76 +++++ kal/libc/newlib/porting/include/endian.h | 36 +++ kal/libc/newlib/porting/include/limits.h | 43 +++ kal/libc/newlib/porting/include/malloc.h | 44 +++ kal/libc/newlib/porting/include/mqueue.h | 64 +++++ kal/libc/newlib/porting/include/net/if.h | 157 +++++++++++ kal/libc/newlib/porting/include/netinet/in.h | 230 +++++++++++++++ kal/libc/newlib/porting/include/netinet/ip.h | 115 ++++++++ kal/libc/newlib/porting/include/semaphore.h | 60 ++++ .../porting/include/sys/_pthreadtypes.h | 116 ++++++++ kal/libc/newlib/porting/include/sys/fcntl.h | 38 +++ .../newlib/porting/include/sys/features.h | 55 ++++ kal/libc/newlib/porting/include/sys/ioctl.h | 126 +++++++++ kal/libc/newlib/porting/include/sys/mount.h | 61 ++++ kal/libc/newlib/porting/include/sys/prctl.h | 63 +++++ kal/libc/newlib/porting/include/sys/sched.h | 54 ++++ kal/libc/newlib/porting/include/sys/socket.h | 262 ++++++++++++++++++ kal/libc/newlib/porting/include/sys/stat.h | 123 ++++++++ kal/libc/newlib/porting/include/sys/statfs.h | 59 ++++ kal/libc/newlib/porting/include/time.h | 40 +++ kal/libc/newlib/porting/src/fs.c | 186 +++++++++++++ kal/libc/newlib/porting/src/malloc.c | 96 +++++++ kal/libc/newlib/porting/src/other_adapt.c | 60 ++++ kal/libc/newlib/porting/src/time.c | 54 ++++ kal/posix/BUILD.gn | 31 ++- kal/posix/Kconfig | 62 +++++ kal/posix/src/pthread_cond.c | 28 +- kal/posix/src/semaphore.c | 1 + kal/posix/src/time.c | 45 ++- 61 files changed, 3280 insertions(+), 216 deletions(-) create mode 100644 components/fs/Kconfig create mode 100644 components/fs/littlefs/Kconfig create mode 100644 components/fs/vfs/BUILD.gn create mode 100644 components/fs/vfs/Kconfig rename components/fs/{ => vfs}/fs_operations.h (100%) rename components/fs/{fs.c => vfs/los_fs.c} (93%) create mode 100644 components/fs/vfs/los_fs.h rename components/fs/{ => vfs}/vfs_config.h (100%) create mode 100644 kal/Kconfig create mode 100644 kal/cmsis/Kconfig create mode 100644 kal/libc/BUILD.gn create mode 100644 kal/libc/Kconfig create mode 100644 kal/libc/musl/BUILD.gn create mode 100644 kal/libc/musl/Kconfig create mode 100644 kal/libc/musl/fs.c rename kal/{posix/src => libc/musl}/malloc.c (97%) create mode 100644 kal/libc/newlib/BUILD.gn create mode 100644 kal/libc/newlib/Kconfig create mode 100644 kal/libc/newlib/porting/include/dirent.h create mode 100644 kal/libc/newlib/porting/include/endian.h create mode 100644 kal/libc/newlib/porting/include/limits.h create mode 100644 kal/libc/newlib/porting/include/malloc.h create mode 100644 kal/libc/newlib/porting/include/mqueue.h create mode 100644 kal/libc/newlib/porting/include/net/if.h create mode 100644 kal/libc/newlib/porting/include/netinet/in.h create mode 100644 kal/libc/newlib/porting/include/netinet/ip.h create mode 100644 kal/libc/newlib/porting/include/semaphore.h create mode 100644 kal/libc/newlib/porting/include/sys/_pthreadtypes.h create mode 100644 kal/libc/newlib/porting/include/sys/fcntl.h create mode 100644 kal/libc/newlib/porting/include/sys/features.h create mode 100644 kal/libc/newlib/porting/include/sys/ioctl.h create mode 100644 kal/libc/newlib/porting/include/sys/mount.h create mode 100644 kal/libc/newlib/porting/include/sys/prctl.h create mode 100644 kal/libc/newlib/porting/include/sys/sched.h create mode 100644 kal/libc/newlib/porting/include/sys/socket.h create mode 100644 kal/libc/newlib/porting/include/sys/stat.h create mode 100644 kal/libc/newlib/porting/include/sys/statfs.h create mode 100644 kal/libc/newlib/porting/include/time.h create mode 100644 kal/libc/newlib/porting/src/fs.c create mode 100644 kal/libc/newlib/porting/src/malloc.c create mode 100644 kal/libc/newlib/porting/src/other_adapt.c create mode 100644 kal/libc/newlib/porting/src/time.c create mode 100644 kal/posix/Kconfig diff --git a/BUILD.gn b/BUILD.gn index fa0ac5dd..24a452b4 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -102,6 +102,9 @@ config("stdinc_config") { ] cflags += [ "-nostdinc" ] asmflags = cflags + if (defined(LOSCFG_LIBC_NEWLIB)) { + cflags -= [ "-nostdinc" ] + } } config("ssp_config") { @@ -211,7 +214,7 @@ config("los_config") { #":arch_config", ":kconfig_config", - #":stdinc_config", + ":stdinc_config", ":dialect_config", ":optimize_config", ":ssp_config", @@ -244,8 +247,6 @@ config("public") { configs += [ "$device_path:public" ] } } - - configs += [ "$LITEOSTHIRDPARTY/musl/porting/liteos_m/kernel:include" ] } group("modules") { @@ -267,8 +268,6 @@ group("modules") { deps += [ device_path ] } } - - deps += [ "$LITEOSTHIRDPARTY/musl/porting/liteos_m/kernel" ] } # when HAVE_DEVICE_SDK is not reached, gn raises an error. so we just use it as diff --git a/Kconfig b/Kconfig index 98747fa4..62868924 100644 --- a/Kconfig +++ b/Kconfig @@ -373,53 +373,15 @@ endmenu ######################### config options of compatibility ############## menu "Compat" -config COMPAT_POSIX - bool "Enable Posix" - default y - help - Answer Y to enable LiteOS support posix interface. - -config COMPAT_CMSIS - bool "Enable CMSIS v2" - default n - - help - Answer Y to enable LiteOS support CMSIS v2 interface. +rsource "kal/Kconfig" endmenu ######################## config options of filesystem ################## menu "FileSystem" -config FS_VFS - bool "Enable VFS" - default y - help - Answer Y to enable LiteOS support virtual filesystem. - -source "components/fs/fatfs/Kconfig" - -config FS_LITTLEFS - bool "Enable littlefs" - default n - depends on FS_VFS - select SUPPORT_LITTLEFS - help - Answer Y to enable LiteOS support littlefs. - -config LFS_MAX_MOUNT_SIZE - int "Littlefs max mounts" - default 3 - depends on FS_LITTLEFS - -config SUPPORT_FATFS - bool - default n - -config SUPPORT_LITTLEFS - bool - default n +rsource "components/fs/Kconfig" endmenu @@ -429,7 +391,7 @@ config NET_LWIP bool "Enable Lwip" default n select NET_LWIP_SACK - select COMPAT_CMSIS + select KAL_CMSIS help Answer Y to enable LiteOS support lwip. diff --git a/arch/Kconfig b/arch/Kconfig index 86dce950..0c6c1040 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -44,3 +44,10 @@ config IRQ_USE_STANDALONE_STACK depends on ARCH_ARM_AARCH64 || ARCH_ARM_AARCH32 help This option will support using standalone interrupt stack. + +config ARCH_UNALIGNED_EXC + bool "Enable Unaligned Exception" + default y + depends on ARCH_ARM + help + This option will enable unaligned exception. diff --git a/arch/arm/cortex-m3/keil/los_interrupt.c b/arch/arm/cortex-m3/keil/los_interrupt.c index 92d2cbc9..352b39eb 100644 --- a/arch/arm/cortex-m3/keil/los_interrupt.c +++ b/arch/arm/cortex-m3/keil/los_interrupt.c @@ -523,7 +523,11 @@ LITE_OS_SEC_TEXT_INIT VOID HalHwiInit(VOID) *(volatile UINT32 *)OS_NVIC_SHCSR |= (USGFAULT | BUSFAULT | MEMFAULT); /* Enable DIV 0 and unaligned exception */ +#ifdef LOSCFG_ARCH_UNALIGNED_EXC *(volatile UINT32 *)OS_NVIC_CCR |= (DIV0FAULT | UNALIGNFAULT); +#else + *(volatile UINT32 *)OS_NVIC_CCR |= (DIV0FAULT); +#endif return; } diff --git a/arch/arm/cortex-m33/gcc/NTZ/los_interrupt.c b/arch/arm/cortex-m33/gcc/NTZ/los_interrupt.c index 489abf21..6666583d 100755 --- a/arch/arm/cortex-m33/gcc/NTZ/los_interrupt.c +++ b/arch/arm/cortex-m33/gcc/NTZ/los_interrupt.c @@ -513,7 +513,11 @@ LITE_OS_SEC_TEXT_INIT VOID HalHwiInit(VOID) *(volatile UINT32 *)OS_NVIC_SHCSR |= (USGFAULT | BUSFAULT | MEMFAULT); /* Enable DIV 0 and unaligned exception */ +#ifdef LOSCFG_ARCH_UNALIGNED_EXC *(volatile UINT32 *)OS_NVIC_CCR |= (DIV0FAULT | UNALIGNFAULT); +#else + *(volatile UINT32 *)OS_NVIC_CCR |= (DIV0FAULT); +#endif return; } diff --git a/arch/arm/cortex-m33/gcc/TZ/non_secure/los_interrupt.c b/arch/arm/cortex-m33/gcc/TZ/non_secure/los_interrupt.c index 489abf21..6666583d 100755 --- a/arch/arm/cortex-m33/gcc/TZ/non_secure/los_interrupt.c +++ b/arch/arm/cortex-m33/gcc/TZ/non_secure/los_interrupt.c @@ -513,7 +513,11 @@ LITE_OS_SEC_TEXT_INIT VOID HalHwiInit(VOID) *(volatile UINT32 *)OS_NVIC_SHCSR |= (USGFAULT | BUSFAULT | MEMFAULT); /* Enable DIV 0 and unaligned exception */ +#ifdef LOSCFG_ARCH_UNALIGNED_EXC *(volatile UINT32 *)OS_NVIC_CCR |= (DIV0FAULT | UNALIGNFAULT); +#else + *(volatile UINT32 *)OS_NVIC_CCR |= (DIV0FAULT); +#endif return; } diff --git a/arch/arm/cortex-m4/gcc/los_interrupt.c b/arch/arm/cortex-m4/gcc/los_interrupt.c index 8fadd0b4..986b44b5 100644 --- a/arch/arm/cortex-m4/gcc/los_interrupt.c +++ b/arch/arm/cortex-m4/gcc/los_interrupt.c @@ -533,7 +533,11 @@ LITE_OS_SEC_TEXT_INIT VOID HalHwiInit(VOID) *(volatile UINT32 *)OS_NVIC_SHCSR |= (USGFAULT | BUSFAULT | MEMFAULT); /* Enable DIV 0 and unaligned exception */ +#ifdef LOSCFG_ARCH_UNALIGNED_EXC *(volatile UINT32 *)OS_NVIC_CCR |= (DIV0FAULT | UNALIGNFAULT); +#else + *(volatile UINT32 *)OS_NVIC_CCR |= (DIV0FAULT); +#endif return; } diff --git a/arch/arm/cortex-m4/iar/los_interrupt.c b/arch/arm/cortex-m4/iar/los_interrupt.c index 1a6e66e0..106acde2 100644 --- a/arch/arm/cortex-m4/iar/los_interrupt.c +++ b/arch/arm/cortex-m4/iar/los_interrupt.c @@ -524,7 +524,11 @@ LITE_OS_SEC_TEXT_INIT VOID HalHwiInit(VOID) *(volatile UINT32 *)OS_NVIC_SHCSR |= (USGFAULT | BUSFAULT | MEMFAULT); /* Enable DIV 0 and unaligned exception */ +#ifdef LOSCFG_ARCH_UNALIGNED_EXC *(volatile UINT32 *)OS_NVIC_CCR |= (DIV0FAULT | UNALIGNFAULT); +#else + *(volatile UINT32 *)OS_NVIC_CCR |= (DIV0FAULT); +#endif return; } diff --git a/arch/arm/cortex-m7/gcc/los_interrupt.c b/arch/arm/cortex-m7/gcc/los_interrupt.c index 4d3340dc..fab6b729 100644 --- a/arch/arm/cortex-m7/gcc/los_interrupt.c +++ b/arch/arm/cortex-m7/gcc/los_interrupt.c @@ -513,7 +513,11 @@ LITE_OS_SEC_TEXT_INIT VOID HalHwiInit(VOID) *(volatile UINT32 *)OS_NVIC_SHCSR |= (USGFAULT | BUSFAULT | MEMFAULT); /* Enable DIV 0 and unaligned exception */ +#ifdef LOSCFG_ARCH_UNALIGNED_EXC *(volatile UINT32 *)OS_NVIC_CCR |= (DIV0FAULT | UNALIGNFAULT); +#else + *(volatile UINT32 *)OS_NVIC_CCR |= (DIV0FAULT); +#endif return; } diff --git a/components/fs/BUILD.gn b/components/fs/BUILD.gn index 3078c167..97c21780 100644 --- a/components/fs/BUILD.gn +++ b/components/fs/BUILD.gn @@ -29,24 +29,11 @@ import("//kernel/liteos_m/liteos.gni") -module_switch = defined(LOSCFG_FS_VFS) -module_name = "fs_operations" -kernel_module(module_name) { - sources = [ "fs.c" ] -} - -group("fs") { - deps = [ ":$module_name" ] - deps += [ - "fatfs", +module_name = get_path_info(rebase_path("."), "name") +module_group(module_name) { + modules = [ + "vfs", "littlefs", - ] -} - -config("public") { - include_dirs = [ "." ] - configs = [ - "fatfs:public", - "littlefs:public", + "fatfs", ] } diff --git a/components/fs/Kconfig b/components/fs/Kconfig new file mode 100644 index 00000000..a119254e --- /dev/null +++ b/components/fs/Kconfig @@ -0,0 +1,42 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +rsource "vfs/Kconfig" +rsource "littlefs/Kconfig" +rsource "fatfs/Kconfig" + +config SUPPORT_FATFS + bool + default n + depends on FS_FAT + +config SUPPORT_LITTLEFS + bool + default n + depends on FS_LITTLEFS \ No newline at end of file diff --git a/components/fs/fatfs/Kconfig b/components/fs/fatfs/Kconfig index 1d401bbd..705b9101 100644 --- a/components/fs/fatfs/Kconfig +++ b/components/fs/fatfs/Kconfig @@ -1,9 +1,38 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + config FS_FAT bool "Enable FAT" default n depends on FS_VFS select SUPPORT_FATFS - select COMPAT_CMSIS + select KAL_CMSIS help Answer Y to enable LiteOS support fat filesystem. diff --git a/components/fs/littlefs/Kconfig b/components/fs/littlefs/Kconfig new file mode 100644 index 00000000..3543e3ad --- /dev/null +++ b/components/fs/littlefs/Kconfig @@ -0,0 +1,44 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +config FS_LITTLEFS + bool "Enable Little FS" + default n + depends on FS_VFS + help + Answer Y to enable LiteOS support Little FS filesystem. + +if FS_LITTLEFS +config LFS_MAX_MOUNT_SIZE + int "Maximum number of mount points" + default 3 + help + This is a global maximum number of mount points. + +endif # FS_LITTLEFS diff --git a/components/fs/littlefs/lfs_api.h b/components/fs/littlefs/lfs_api.h index a101217e..fe334919 100644 --- a/components/fs/littlefs/lfs_api.h +++ b/components/fs/littlefs/lfs_api.h @@ -45,8 +45,6 @@ #define INVALID_FD (-1) -typedef unsigned mode_t; - #ifndef VFS_ERROR #define VFS_ERROR (-1) #endif diff --git a/components/fs/vfs/BUILD.gn b/components/fs/vfs/BUILD.gn new file mode 100644 index 00000000..94061d2d --- /dev/null +++ b/components/fs/vfs/BUILD.gn @@ -0,0 +1,40 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import("//kernel/liteos_m/liteos.gni") + +module_switch = defined(LOSCFG_FS_VFS) +module_name = get_path_info(rebase_path("."), "name") +kernel_module(module_name) { + sources = [ "los_fs.c" ] +} + +config("public") { + include_dirs = [ "." ] +} diff --git a/components/fs/vfs/Kconfig b/components/fs/vfs/Kconfig new file mode 100644 index 00000000..b51be4b5 --- /dev/null +++ b/components/fs/vfs/Kconfig @@ -0,0 +1,34 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +config FS_VFS + bool "Enable FS VFS" + default y + help + Answer Y to enable LiteOS support VFS. diff --git a/components/fs/fs_operations.h b/components/fs/vfs/fs_operations.h similarity index 100% rename from components/fs/fs_operations.h rename to components/fs/vfs/fs_operations.h diff --git a/components/fs/fs.c b/components/fs/vfs/los_fs.c similarity index 93% rename from components/fs/fs.c rename to components/fs/vfs/los_fs.c index b28f2eea..71a05b6c 100644 --- a/components/fs/fs.c +++ b/components/fs/vfs/los_fs.c @@ -28,8 +28,10 @@ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "fs_operations.h" + +#include "los_fs.h" #include "los_config.h" +#include "fs_operations.h" #if (LOSCFG_SUPPORT_FATFS == 1) #include "fatfs.h" #endif @@ -188,9 +190,9 @@ static struct FsMap *MountFindfs(const char *fileSystemtype) return NULL; } -int mount(const char *source, const char *target, - const char *filesystemtype, unsigned long mountflags, - const void *data) +int LOS_FsMount(const char *source, const char *target, + const char *filesystemtype, unsigned long mountflags, + const void *data) { static int initFlag = 0; @@ -213,7 +215,7 @@ int mount(const char *source, const char *target, return g_fs->fsMops->Mount(source, target, filesystemtype, mountflags, data); } -int umount(const char *target) +int LOS_FsUmount(const char *target) { if (g_fs == NULL) { errno = ENODEV; @@ -226,7 +228,7 @@ int umount(const char *target) return g_fs->fsMops->Umount(target); } -int umount2(const char *target, int flag) +int LOS_FsUmount2(const char *target, int flag) { if (g_fs == NULL) { errno = ENODEV; @@ -239,7 +241,7 @@ int umount2(const char *target, int flag) return g_fs->fsMops->Umount2(target, flag); } -int open(const char *path, int oflag, ...) +int LOS_Open(const char *path, int oflag, ...) { #ifdef LOSCFG_RANDOM_DEV unsigned flags = O_RDONLY | O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_LARGEFILE | O_TRUNC | O_EXCL | O_DIRECTORY; @@ -294,7 +296,7 @@ int open(const char *path, int oflag, ...) return g_fs->fsFops->Open(path, oflag); } -int close(int fd) +int LOS_Close(int fd) { #ifdef LOSCFG_RANDOM_DEV if (fd == RANDOM_DEV_FD) { @@ -317,7 +319,7 @@ int close(int fd) return g_fs->fsFops->Close(fd); } -ssize_t read(int fd, void *buf, size_t nbyte) +ssize_t LOS_Read(int fd, void *buf, size_t nbyte) { #ifdef LOSCFG_RANDOM_DEV if (fd == RANDOM_DEV_FD) { @@ -355,7 +357,7 @@ ssize_t read(int fd, void *buf, size_t nbyte) return g_fs->fsFops->Read(fd, buf, nbyte); } -ssize_t write(int fd, const void *buf, size_t nbyte) +ssize_t LOS_Write(int fd, const void *buf, size_t nbyte) { #ifdef LOSCFG_RANDOM_DEV if (fd == RANDOM_DEV_FD) { @@ -379,7 +381,7 @@ ssize_t write(int fd, const void *buf, size_t nbyte) return g_fs->fsFops->Write(fd, buf, nbyte); } -off_t lseek(int fd, off_t offset, int whence) +off_t LOS_Lseek(int fd, off_t offset, int whence) { if (g_fs == NULL) { errno = ENODEV; @@ -392,7 +394,7 @@ off_t lseek(int fd, off_t offset, int whence) return g_fs->fsFops->Seek(fd, offset, whence); } -int unlink(const char *path) +int LOS_Unlink(const char *path) { if (g_fs == NULL) { errno = ENODEV; @@ -405,7 +407,7 @@ int unlink(const char *path) return g_fs->fsFops->Unlink(path); } -int fstat(int fd, struct stat *buf) +int LOS_Fstat(int fd, struct stat *buf) { if (g_fs == NULL) { errno = ENODEV; @@ -418,7 +420,7 @@ int fstat(int fd, struct stat *buf) return g_fs->fsFops->Fstat(fd, buf); } -int stat(const char *path, struct stat *buf) +int LOS_Stat(const char *path, struct stat *buf) { if (g_fs == NULL) { errno = ENODEV; @@ -431,7 +433,7 @@ int stat(const char *path, struct stat *buf) return g_fs->fsFops->Getattr(path, buf); } -int fsync(int fd) +int LOS_Fsync(int fd) { if (g_fs == NULL) { errno = ENODEV; @@ -444,7 +446,7 @@ int fsync(int fd) return g_fs->fsFops->Fsync(fd); } -int mkdir(const char *path, mode_t mode) +int LOS_Mkdir(const char *path, mode_t mode) { if (g_fs == NULL) { errno = ENODEV; @@ -457,7 +459,7 @@ int mkdir(const char *path, mode_t mode) return g_fs->fsFops->Mkdir(path, mode); } -DIR *opendir(const char *dirName) +DIR *LOS_Opendir(const char *dirName) { if (g_fs == NULL) { errno = ENODEV; @@ -470,7 +472,7 @@ DIR *opendir(const char *dirName) return g_fs->fsFops->Opendir(dirName); } -struct dirent *readdir(DIR *dir) +struct dirent *LOS_Readdir(DIR *dir) { if (g_fs == NULL) { errno = ENODEV; @@ -483,7 +485,7 @@ struct dirent *readdir(DIR *dir) return g_fs->fsFops->Readdir(dir); } -int closedir(DIR *dir) +int LOS_Closedir(DIR *dir) { if (g_fs == NULL) { errno = ENODEV; @@ -496,7 +498,7 @@ int closedir(DIR *dir) return g_fs->fsFops->Closedir(dir); } -int rmdir(const char *path) +int LOS_Rmdir(const char *path) { if (g_fs == NULL) { errno = ENODEV; @@ -509,7 +511,7 @@ int rmdir(const char *path) return g_fs->fsFops->Rmdir(path); } -int rename(const char *oldName, const char *newName) +int LOS_Rename(const char *oldName, const char *newName) { if (g_fs == NULL) { errno = ENODEV; @@ -522,7 +524,7 @@ int rename(const char *oldName, const char *newName) return g_fs->fsFops->Rename(oldName, newName); } -int statfs(const char *path, struct statfs *buf) +int LOS_Statfs(const char *path, struct statfs *buf) { if (g_fs == NULL) { errno = ENODEV; @@ -535,7 +537,7 @@ int statfs(const char *path, struct statfs *buf) return g_fs->fsMops->Statfs(path, buf); } -int ftruncate(int fd, off_t length) +int LOS_Ftruncate(int fd, off_t length) { if (g_fs == NULL) { errno = ENODEV; diff --git a/components/fs/vfs/los_fs.h b/components/fs/vfs/los_fs.h new file mode 100644 index 00000000..530673ea --- /dev/null +++ b/components/fs/vfs/los_fs.h @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @defgroup los_vfs fs + * @ingroup kernel + */ + +#ifndef _LOS_VFS_H_ +#define _LOS_VFS_H_ + +#include "los_config.h" +#include "dirent.h" +#include "sys/mount.h" +#include "sys/statfs.h" +#include "sys/stat.h" +#include "unistd.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +int LOS_Open(const char *path, int oflag, ...); + +int LOS_Close(int fd); + +ssize_t LOS_Read(int fd, void *buf, size_t nbyte); + +ssize_t LOS_Write(int fd, const void *buf, size_t nbyte); + +off_t LOS_Lseek(int fd, off_t offset, int whence); + +int LOS_Unlink(const char *path); + +int LOS_Fstat(int fd, struct stat *buf); + +int LOS_Stat(const char *path, struct stat *buf); + +int LOS_Fsync(int fd); + +int LOS_Mkdir(const char *path, mode_t mode); + +DIR *LOS_Opendir(const char *dirName); + +struct dirent *LOS_Readdir(DIR *dir); + +int LOS_Closedir(DIR *dir); + +int LOS_Rmdir(const char *path); + +int LOS_Rename(const char *oldName, const char *newName); + +int LOS_Statfs(const char *path, struct statfs *buf); + +int LOS_Ftruncate(int fd, off_t length); + +int LOS_FsUmount2(const char *target, int flag); + +int LOS_FsUmount(const char *target); + +int LOS_FsMount(const char *source, const char *target, + const char *filesystemtype, unsigned long mountflags, + const void *data); + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif /* _LOS_FS_H_ */ \ No newline at end of file diff --git a/components/fs/vfs_config.h b/components/fs/vfs/vfs_config.h similarity index 100% rename from components/fs/vfs_config.h rename to components/fs/vfs/vfs_config.h index 95b382b8..73102c2d 100644 --- a/components/fs/vfs_config.h +++ b/components/fs/vfs/vfs_config.h @@ -82,12 +82,6 @@ #define CONFIG_NUNGET_CHARS 0 #define MIN_START_FD 3 // 0,1,2 are used for stdin,stdout,stderr respectively -#define FD_SET_TOTAL_SIZE (FD_SETSIZE + CONFIG_NEXPANED_DESCRIPTORS) -#define FD_SETSIZE (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS) -#define CONFIG_NEXPANED_DESCRIPTORS (CONFIG_NTIME_DESCRIPTORS + CONFIG_NQUEUE_DESCRIPTORS) -#define TIMER_FD_OFFSET FD_SETSIZE -#define MQUEUE_FD_OFFSET (FD_SETSIZE + CONFIG_NTIME_DESCRIPTORS) - /* net configure */ #ifdef LOSCFG_NET_LWIP_SACK // enable socket and net function @@ -129,6 +123,12 @@ #define CONFIG_NQUEUE_DESCRIPTORS 256 +#define FD_SETSIZE (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS) +#define CONFIG_NEXPANED_DESCRIPTORS (CONFIG_NTIME_DESCRIPTORS + CONFIG_NQUEUE_DESCRIPTORS) +#define FD_SET_TOTAL_SIZE (FD_SETSIZE + CONFIG_NEXPANED_DESCRIPTORS) +#define TIMER_FD_OFFSET FD_SETSIZE +#define MQUEUE_FD_OFFSET (FD_SETSIZE + CONFIG_NTIME_DESCRIPTORS) + /* directory configure */ #define VFS_USING_WORKDIR // enable current working directory diff --git a/kal/BUILD.gn b/kal/BUILD.gn index a4153d40..4e59b8e7 100644 --- a/kal/BUILD.gn +++ b/kal/BUILD.gn @@ -29,16 +29,12 @@ import("//kernel/liteos_m/liteos.gni") -group("kal") { - deps = [ "cmsis" ] - deps += [ "posix" ] - deps += [ "libsec" ] -} - -config("public") { - configs = [ - "cmsis:public", - "posix:public", - "libsec:public", +module_name = get_path_info(rebase_path("."), "name") +module_group(module_name) { + modules = [ + "cmsis", + "libc", + "libsec", + "posix", ] } diff --git a/kal/Kconfig b/kal/Kconfig new file mode 100644 index 00000000..ece0fa54 --- /dev/null +++ b/kal/Kconfig @@ -0,0 +1,32 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +rsource "cmsis/Kconfig" +rsource "libc/Kconfig" +rsource "posix/Kconfig" \ No newline at end of file diff --git a/kal/cmsis/BUILD.gn b/kal/cmsis/BUILD.gn index 3c45faba..2e5ccea9 100644 --- a/kal/cmsis/BUILD.gn +++ b/kal/cmsis/BUILD.gn @@ -29,7 +29,7 @@ import("//kernel/liteos_m/liteos.gni") -module_switch = defined(LOSCFG_COMPAT_CMSIS) +module_switch = defined(LOSCFG_KAL_CMSIS) module_name = get_path_info(rebase_path("."), "name") kernel_module(module_name) { sources = [ "cmsis_liteos2.c" ] diff --git a/kal/cmsis/Kconfig b/kal/cmsis/Kconfig new file mode 100644 index 00000000..031d7c70 --- /dev/null +++ b/kal/cmsis/Kconfig @@ -0,0 +1,36 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +config KAL_CMSIS + bool "Enable KAL CMSIS" + default y + help + Answer Y to enable LiteOS Kernel Abstraction Layer support CMSIS API. + + diff --git a/kal/libc/BUILD.gn b/kal/libc/BUILD.gn new file mode 100644 index 00000000..6c5933d4 --- /dev/null +++ b/kal/libc/BUILD.gn @@ -0,0 +1,38 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import("//kernel/liteos_m/liteos.gni") + +module_name = get_path_info(rebase_path("."), "name") +module_group(module_name) { + modules = [ + "musl", + "newlib", + ] +} diff --git a/kal/libc/Kconfig b/kal/libc/Kconfig new file mode 100644 index 00000000..8a77612e --- /dev/null +++ b/kal/libc/Kconfig @@ -0,0 +1,44 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +choice + prompt "Choose libc implementation" + default LIBC_MUSL + help + Choose libc implementation. + +config LIBC_MUSL + bool "musl libc" +rsource "musl/Kconfig" + +config LIBC_NEWLIB + bool "newlibc" +rsource "newlib/Kconfig" + +endchoice diff --git a/kal/libc/musl/BUILD.gn b/kal/libc/musl/BUILD.gn new file mode 100644 index 00000000..711aa42a --- /dev/null +++ b/kal/libc/musl/BUILD.gn @@ -0,0 +1,46 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import("//kernel/liteos_m/liteos.gni") + +module_switch = defined(LOSCFG_LIBC_MUSL) +module_name = get_path_info(rebase_path("."), "name") +kernel_module(module_name) { + sources = [ "malloc.c" ] + + if (defined(LOSCFG_LIBC_MUSL_FS)) { + sources += [ "fs.c" ] + } + + deps = [ "//third_party/musl/porting/liteos_m/kernel" ] +} + +config("public") { + include_dirs = [ "//third_party/musl/porting/liteos_m/kernel/include" ] +} diff --git a/kal/libc/musl/Kconfig b/kal/libc/musl/Kconfig new file mode 100644 index 00000000..a90394d3 --- /dev/null +++ b/kal/libc/musl/Kconfig @@ -0,0 +1,39 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +if LIBC_MUSL + +config LIBC_MUSL_FS + bool "Enable POSIX file system API support" + default y + depends on FS_VFS + help + This enables POSIX style file system related APIs. + +endif # LIBC_MUSL diff --git a/kal/libc/musl/fs.c b/kal/libc/musl/fs.c new file mode 100644 index 00000000..4de67492 --- /dev/null +++ b/kal/libc/musl/fs.c @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "los_config.h" +#include "los_fs.h" +#include "stdarg.h" +#include "dirent.h" +#include "sys/mount.h" +#include "sys/statfs.h" +#include "sys/stat.h" +#include "unistd.h" + +int mount(const char *source, const char *target, + const char *filesystemtype, unsigned long mountflags, + const void *data) +{ + return LOS_FsMount(source, target, filesystemtype, mountflags, data); +} + +int umount(const char *target) +{ + return LOS_FsUmount(target); +} + +int umount2(const char *target, int flag) +{ + return LOS_FsUmount2(target, flag); +} + +int open(const char *path, int oflag, ...) +{ + va_list vaList; + va_start(vaList, oflag); + int ret; + ret = LOS_Open(path, oflag, vaList); + va_end(vaList); + return ret; +} + +int close(int fd) +{ + return LOS_Close(fd); +} + +ssize_t read(int fd, void *buf, size_t nbyte) +{ + return LOS_Read(fd, buf, nbyte); +} + +ssize_t write(int fd, const void *buf, size_t nbyte) +{ + return LOS_Write(fd, buf, nbyte); +} + +off_t lseek(int fd, off_t offset, int whence) +{ + return LOS_Lseek(fd, offset, whence); +} + +int unlink(const char *path) +{ + return LOS_Unlink(path); +} + +int fstat(int fd, struct stat *buf) +{ + return LOS_Fstat(fd, buf); +} + +int stat(const char *path, struct stat *buf) +{ + return LOS_Stat(path, buf); +} + +int fsync(int fd) +{ + return LOS_Fsync(fd); +} + +int mkdir(const char *path, mode_t mode) +{ + return LOS_Mkdir(path, mode); +} + +DIR *opendir(const char *dirName) +{ + return LOS_Opendir(dirName); +} + +struct dirent *readdir(DIR *dir) +{ + return LOS_Readdir(dir); +} + +int closedir(DIR *dir) +{ + return LOS_Closedir(dir); +} + +int rmdir(const char *path) +{ + return LOS_Unlink(path); +} + +int rename(const char *oldName, const char *newName) +{ + return LOS_Rename(oldName, newName); +} + +int statfs(const char *path, struct statfs *buf) +{ + return LOS_Statfs(path, buf); +} + +int ftruncate(int fd, off_t length) +{ + return LOS_Ftruncate(fd, length); +} diff --git a/kal/posix/src/malloc.c b/kal/libc/musl/malloc.c similarity index 97% rename from kal/posix/src/malloc.c rename to kal/libc/musl/malloc.c index c2062384..434cba04 100644 --- a/kal/posix/src/malloc.c +++ b/kal/libc/musl/malloc.c @@ -27,83 +27,83 @@ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "securec.h" -#include "los_config.h" -#include "los_memory.h" - - -void *calloc(size_t nitems, size_t size) -{ - size_t real_size; - void *ptr = NULL; - - if (nitems == 0 || size == 0) { - return NULL; - } - - real_size = (size_t)(nitems * size); - ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, real_size); - if (ptr != NULL) { - (void)memset_s(ptr, real_size, 0, real_size); - } - return ptr; -} - -void free(void *ptr) -{ - if (ptr == NULL) { - return; - } - - LOS_MemFree(OS_SYS_MEM_ADDR, ptr); -} - -void *malloc(size_t size) -{ - if (size == 0) { - return NULL; - } - - return LOS_MemAlloc(OS_SYS_MEM_ADDR, size); -} - -void *zalloc(size_t size) -{ - void *ptr = NULL; - - if (size == 0) { - return NULL; - } - - ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, size); - if (ptr != NULL) { - (void)memset_s(ptr, size, 0, size); - } - return ptr; -} - -void *memalign(size_t boundary, size_t size) -{ - if (size == 0) { - return NULL; - } - - return LOS_MemAllocAlign(OS_SYS_MEM_ADDR, size, boundary); -} - -void *realloc(void *ptr, size_t size) -{ - if (ptr == NULL) { - return malloc(size); - } - - if (size == 0) { - free(ptr); - return NULL; - } - - return LOS_MemRealloc(OS_SYS_MEM_ADDR, ptr, size); -} - + */ + +#include "securec.h" +#include "los_config.h" +#include "los_memory.h" + + +void *calloc(size_t nitems, size_t size) +{ + size_t real_size; + void *ptr = NULL; + + if (nitems == 0 || size == 0) { + return NULL; + } + + real_size = (size_t)(nitems * size); + ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, real_size); + if (ptr != NULL) { + (void)memset_s(ptr, real_size, 0, real_size); + } + return ptr; +} + +void free(void *ptr) +{ + if (ptr == NULL) { + return; + } + + LOS_MemFree(OS_SYS_MEM_ADDR, ptr); +} + +void *malloc(size_t size) +{ + if (size == 0) { + return NULL; + } + + return LOS_MemAlloc(OS_SYS_MEM_ADDR, size); +} + +void *zalloc(size_t size) +{ + void *ptr = NULL; + + if (size == 0) { + return NULL; + } + + ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, size); + if (ptr != NULL) { + (void)memset_s(ptr, size, 0, size); + } + return ptr; +} + +void *memalign(size_t boundary, size_t size) +{ + if (size == 0) { + return NULL; + } + + return LOS_MemAllocAlign(OS_SYS_MEM_ADDR, size, boundary); +} + +void *realloc(void *ptr, size_t size) +{ + if (ptr == NULL) { + return malloc(size); + } + + if (size == 0) { + free(ptr); + return NULL; + } + + return LOS_MemRealloc(OS_SYS_MEM_ADDR, ptr, size); +} + diff --git a/kal/libc/newlib/BUILD.gn b/kal/libc/newlib/BUILD.gn new file mode 100644 index 00000000..008c9793 --- /dev/null +++ b/kal/libc/newlib/BUILD.gn @@ -0,0 +1,50 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import("//kernel/liteos_m/liteos.gni") + +module_switch = defined(LOSCFG_LIBC_NEWLIB) +module_name = get_path_info(rebase_path("."), "name") +kernel_module(module_name) { + sources = [ + "porting/src/fs.c", + "porting/src/malloc.c", + "porting/src/other_adapt.c", + "porting/src/time.c", + ] + + if (defined(LOSCFG_LIBC_NEWLIB_FS)) { + sources += + [ "//third_party/musl/porting/liteos_m/kernel/src/misc/realpath.c" ] + } +} + +config("public") { + include_dirs = [ "porting/include" ] +} diff --git a/kal/libc/newlib/Kconfig b/kal/libc/newlib/Kconfig new file mode 100644 index 00000000..3f3e92f7 --- /dev/null +++ b/kal/libc/newlib/Kconfig @@ -0,0 +1,39 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +if LIBC_NEWLIB + +config LIBC_NEWLIB_FS + bool "Enable POSIX file system API support" + default y + depends on FS_VFS + help + This enables POSIX style file system related APIs. + +endif # LIBC_NEWLIB diff --git a/kal/libc/newlib/porting/include/dirent.h b/kal/libc/newlib/porting/include/dirent.h new file mode 100644 index 00000000..c3c29905 --- /dev/null +++ b/kal/libc/newlib/porting/include/dirent.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_DIRENT_H +#define _ADAPT_DIRENT_H + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define d_fileno d_ino + +#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) +#define DT_UNKNOWN 0 +#define DT_FIFO 1 +#define DT_CHR 2 +#define DT_DIR 4 +#define DT_BLK 6 +#define DT_REG 8 +#define DT_LNK 10 +#define DT_SOCK 12 +#define DT_WHT 14 +#define IFTODT(x) ((x) >> 12 & 017) +#define DTTOIF(x) ((x) << 12) +#endif + +typedef struct __dirstream DIR; + +struct dirent { + ino_t d_ino; + off_t d_off; + unsigned short d_reclen; + unsigned char d_type; + char d_name[NAME_MAX]; +}; + +int closedir(DIR *dir); +DIR *opendir(const char *dirName); +struct dirent *readdir(DIR *dir); + +#ifdef __cplusplus +} +#endif + +#endif /* !_ADAPT_DIRENT_H */ diff --git a/kal/libc/newlib/porting/include/endian.h b/kal/libc/newlib/porting/include/endian.h new file mode 100644 index 00000000..31f135c3 --- /dev/null +++ b/kal/libc/newlib/porting/include/endian.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_ENDIAN_H +#define _ADAPT_ENDIAN_H + +#include_next + +#endif /* !_ADAPT_ENDIAN_H */ diff --git a/kal/libc/newlib/porting/include/limits.h b/kal/libc/newlib/porting/include/limits.h new file mode 100644 index 00000000..9a40c708 --- /dev/null +++ b/kal/libc/newlib/porting/include/limits.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_LIMITS_H +#define _ADAPT_LIMITS_H + +#include_next + +#define SSIZE_MAX LONG_MAX + +#undef PATH_MAX +#undef NAME_MAX +#define PATH_MAX 256 +#define NAME_MAX 256 + +#endif /* !_ADAPT_LIMITS_H */ diff --git a/kal/libc/newlib/porting/include/malloc.h b/kal/libc/newlib/porting/include/malloc.h new file mode 100644 index 00000000..10019e15 --- /dev/null +++ b/kal/libc/newlib/porting/include/malloc.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_MALLOC_H +#define _ADAPT_MALLOC_H + +#include + +void __wrap__free_r(struct _reent *reent, void *aptr); +size_t __wrap__malloc_usable_size_r(struct _reent *reent, void *aptr); +void *__wrap__malloc_r(struct _reent *reent, size_t nbytes); +void *__wrap__memalign_r(struct _reent *reent, size_t align, size_t nbytes); +void *__wrap__realloc_r(struct _reent *reent, void *aptr, size_t nbytes); + +#include_next + +#endif /* !_ADAPT_MALLOC_H */ diff --git a/kal/libc/newlib/porting/include/mqueue.h b/kal/libc/newlib/porting/include/mqueue.h new file mode 100644 index 00000000..cf5229f3 --- /dev/null +++ b/kal/libc/newlib/porting/include/mqueue.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_MQUEUE_H +#define _ADAPT_MQUEUE_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef int mqd_t; + +struct mq_attr { + long mq_flags, mq_maxmsg, mq_msgsize, mq_curmsgs, _unused[4]; +}; + +struct sigevent; + +int mq_close(mqd_t personal); +int mq_getattr(mqd_t personal, struct mq_attr *attr); +mqd_t mq_open(const char *mqName, int openFlag, ...); +ssize_t mq_receive(mqd_t personal, char *msg_ptr, size_t msg_len, unsigned *msg_prio); +int mq_send(mqd_t personal, const char *msg_ptr, size_t msg_len, unsigned msg_prio); +int mq_setattr(mqd_t personal, const struct mq_attr *__restrict new, struct mq_attr *__restrict old); +ssize_t mq_timedreceive(mqd_t personal, char *__restrict msg, size_t msg_len, \ + unsigned *__restrict msg_prio, const struct timespec *__restrict absTimeout); +int mq_timedsend(mqd_t personal, const char *msg, size_t msg_len, unsigned msg_prio, const struct timespec *absTimeout); +int mq_unlink(const char *name); + +#ifdef __cplusplus +} +#endif + +#endif /* !_ADAPT_MQUEUE_H */ diff --git a/kal/libc/newlib/porting/include/net/if.h b/kal/libc/newlib/porting/include/net/if.h new file mode 100644 index 00000000..b2ba84ed --- /dev/null +++ b/kal/libc/newlib/porting/include/net/if.h @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_IF_H +#define _ADAPT_IF_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define IF_NAMESIZE 16 + +#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) + +#include + +#define IFF_UP 0x1 +#define IFF_BROADCAST 0x2 +#define IFF_DEBUG 0x4 +#define IFF_LOOPBACK 0x8 +#define IFF_POINTOPOINT 0x10 +#define IFF_NOTRAILERS 0x20 +#define IFF_RUNNING 0x40 +#define IFF_NOARP 0x80 +#define IFF_PROMISC 0x100 +#define IFF_ALLMULTI 0x200 +#define IFF_MASTER 0x400 +#define IFF_SLAVE 0x800 +#define IFF_MULTICAST 0x1000 +#define IFF_PORTSEL 0x2000 +#define IFF_AUTOMEDIA 0x4000 +#define IFF_DYNAMIC 0x8000 +#define IFF_LOWER_UP 0x10000 +#define IFF_DORMANT 0x20000 +#define IFF_ECHO 0x40000 +#define IFF_VOLATILE (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST | \ + IFF_ECHO | IFF_MASTER | IFF_SLAVE | IFF_RUNNING| \ + IFF_LOWER_UP | IFF_DORMANT) + +struct ifaddr { + struct sockaddr ifa_addr; + union { + struct sockaddr ifu_broadaddr; + struct sockaddr ifu_dstaddr; + } ifa_ifu; + struct iface *ifa_ifp; + struct ifaddr *ifa_next; +}; + +#define ifa_broadaddr ifa_ifu.ifu_broadaddr +#define ifa_dstaddr ifa_ifu.ifu_dstaddr + +struct ifmap { + unsigned long int mem_start; + unsigned long int mem_end; + unsigned short int base_addr; + unsigned char irq; + unsigned char dma; + unsigned char port; +}; + +#define IFHWADDRLEN 6 +#define IFNAMSIZ IF_NAMESIZE + +struct ifreq { + union { + char ifrn_name[IFNAMSIZ]; + } ifr_ifrn; + union { + struct sockaddr ifru_addr; + struct sockaddr ifru_dstaddr; + struct sockaddr ifru_broadaddr; + struct sockaddr ifru_netmask; + struct sockaddr ifru_hwaddr; + short int ifru_flags; + int ifru_ivalue; + int ifru_mtu; + struct ifmap ifru_map; + char ifru_slave[IFNAMSIZ]; + char ifru_newname[IFNAMSIZ]; + char *ifru_data; + } ifr_ifru; +}; + +#define ifr_name ifr_ifrn.ifrn_name +#define ifr_hwaddr ifr_ifru.ifru_hwaddr +#define ifr_addr ifr_ifru.ifru_addr +#define ifr_dstaddr ifr_ifru.ifru_dstaddr +#define ifr_broadaddr ifr_ifru.ifru_broadaddr +#define ifr_netmask ifr_ifru.ifru_netmask +#define ifr_flags ifr_ifru.ifru_flags +#define ifr_metric ifr_ifru.ifru_ivalue +#define ifr_mtu ifr_ifru.ifru_mtu +#define ifr_map ifr_ifru.ifru_map +#define ifr_slave ifr_ifru.ifru_slave +#define ifr_data ifr_ifru.ifru_data +#define ifr_ifindex ifr_ifru.ifru_ivalue +#define ifr_bandwidth ifr_ifru.ifru_ivalue +#define ifr_qlen ifr_ifru.ifru_ivalue +#define ifr_newname ifr_ifru.ifru_newname + +struct ifconf { + int ifc_len; + union { + char *ifcu_buf; + struct ifreq *ifcu_req; + } ifc_ifcu; +}; + +#define ifc_buf ifc_ifcu.ifcu_buf +#define ifc_req ifc_ifcu.ifcu_req +#endif + +struct if_nameindex { + unsigned int if_index; + char *if_name; +}; + +unsigned int if_nametoindex(const char *name); +char *if_indextoname(unsigned int index, char *name); +struct if_nameindex *if_nameindex(void); +void if_freenameindex(struct if_nameindex *ifNameIndex); + +#ifdef __cplusplus +} +#endif + +#endif /* !_ADAPT_IF_H */ diff --git a/kal/libc/newlib/porting/include/netinet/in.h b/kal/libc/newlib/porting/include/netinet/in.h new file mode 100644 index 00000000..d49f46b7 --- /dev/null +++ b/kal/libc/newlib/porting/include/netinet/in.h @@ -0,0 +1,230 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_NETINET_IN_H +#define _ADAPT_NETINET_IN_H + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define INADDR_ANY ((in_addr_t) 0x00000000) +#define INADDR_BROADCAST ((in_addr_t) 0xffffffff) +#define INADDR_NONE ((in_addr_t) 0xffffffff) +#define INADDR_LOOPBACK ((in_addr_t) 0x7f000001) + +#define INADDR_UNSPEC_GROUP ((in_addr_t) 0xe0000000) +#define INADDR_ALLHOSTS_GROUP ((in_addr_t) 0xe0000001) +#define INADDR_ALLRTRS_GROUP ((in_addr_t) 0xe0000002) +#define INADDR_ALLSNOOPERS_GROUP ((in_addr_t) 0xe000006a) +#define INADDR_MAX_LOCAL_GROUP ((in_addr_t) 0xe00000ff) + +#undef INET_ADDRSTRLEN +#undef INET6_ADDRSTRLEN +#define INET_ADDRSTRLEN 16 +#define INET6_ADDRSTRLEN 46 + +#define IPPORT_RESERVED 1024 + +#define IPPROTO_IP 0 +#define IPPROTO_HOPOPTS 0 +#define IPPROTO_ICMP 1 +#define IPPROTO_IGMP 2 +#define IPPROTO_IPIP 4 +#define IPPROTO_TCP 6 +#define IPPROTO_EGP 8 +#define IPPROTO_PUP 12 +#define IPPROTO_UDP 17 +#define IPPROTO_IDP 22 +#define IPPROTO_TP 29 +#define IPPROTO_DCCP 33 +#define IPPROTO_IPV6 41 +#define IPPROTO_ROUTING 43 +#define IPPROTO_FRAGMENT 44 +#define IPPROTO_RSVP 46 +#define IPPROTO_GRE 47 +#define IPPROTO_ESP 50 +#define IPPROTO_AH 51 +#define IPPROTO_ICMPV6 58 +#define IPPROTO_NONE 59 +#define IPPROTO_DSTOPTS 60 +#define IPPROTO_MTP 92 +#define IPPROTO_BEETPH 94 +#define IPPROTO_ENCAP 98 +#define IPPROTO_PIM 103 +#define IPPROTO_COMP 108 +#define IPPROTO_SCTP 132 +#define IPPROTO_MH 135 +#define IPPROTO_UDPLITE 136 +#define IPPROTO_MPLS 137 +#define IPPROTO_RAW 255 +#define IPPROTO_MAX 256 + +#define __ARE_4_EQUAL(a,b) \ + (!( (0[a] - 0[b]) | (1[a] - 1[b]) | (2[a] - 2[b]) | (3[a] - 3[b]) )) + +#define IN_CLASSA(a) ((((in_addr_t)(a)) & 0x80000000) == 0) +#define IN_CLASSA_NET 0xff000000 +#define IN_CLASSA_NSHIFT 24 +#define IN_CLASSA_HOST (0xffffffff & ~IN_CLASSA_NET) +#define IN_CLASSA_MAX 128 +#define IN_CLASSB(a) ((((in_addr_t)(a)) & 0xc0000000) == 0x80000000) +#define IN_CLASSB_NET 0xffff0000 +#define IN_CLASSB_NSHIFT 16 +#define IN_CLASSB_HOST (0xffffffff & ~IN_CLASSB_NET) +#define IN_CLASSB_MAX 65536 +#define IN_CLASSC(a) ((((in_addr_t)(a)) & 0xe0000000) == 0xc0000000) +#define IN_CLASSC_NET 0xffffff00 +#define IN_CLASSC_NSHIFT 8 +#define IN_CLASSC_HOST (0xffffffff & ~IN_CLASSC_NET) +#define IN_CLASSD(a) ((((in_addr_t)(a)) & 0xf0000000) == 0xe0000000) +#define IN_MULTICAST(a) IN_CLASSD(a) +#define IN_EXPERIMENTAL(a) ((((in_addr_t)(a)) & 0xe0000000) == 0xe0000000) +#define IN_BADCLASS(a) ((((in_addr_t)(a)) & 0xf0000000) == 0xf0000000) + +#define IN_LOOPBACKNET 127 + +#define IP_TOS 1 +#define IP_TTL 2 +#define IP_HDRINCL 3 +#define IP_OPTIONS 4 +#define IP_ROUTER_ALERT 5 +#define IP_RECVOPTS 6 +#define IP_RETOPTS 7 +#define IP_PKTINFO 8 +#define IP_PKTOPTIONS 9 +#define IP_PMTUDISC 10 +#define IP_MTU_DISCOVER 10 +#define IP_RECVERR 11 +#define IP_RECVTTL 12 +#define IP_RECVTOS 13 +#define IP_MTU 14 +#define IP_FREEBIND 15 +#define IP_IPSEC_POLICY 16 +#define IP_XFRM_POLICY 17 +#define IP_PASSSEC 18 +#define IP_TRANSPARENT 19 +#define IP_ORIGDSTADDR 20 +#define IP_RECVORIGDSTADDR IP_ORIGDSTADDR +#define IP_MINTTL 21 +#define IP_NODEFRAG 22 +#define IP_CHECKSUM 23 +#define IP_BIND_ADDRESS_NO_PORT 24 +#define IP_RECVFRAGSIZE 25 +#define IP_MULTICAST_IF 32 +#define IP_MULTICAST_TTL 33 +#define IP_MULTICAST_LOOP 34 +#define IP_ADD_MEMBERSHIP 35 +#define IP_DROP_MEMBERSHIP 36 +#define IP_UNBLOCK_SOURCE 37 +#define IP_BLOCK_SOURCE 38 +#define IP_ADD_SOURCE_MEMBERSHIP 39 +#define IP_DROP_SOURCE_MEMBERSHIP 40 +#define IP_MSFILTER 41 +#define IP_MULTICAST_ALL 49 +#define IP_UNICAST_IF 50 + +#define IP_RECVRETOPTS IP_RETOPTS + +#define IP_PMTUDISC_DONT 0 +#define IP_PMTUDISC_WANT 1 +#define IP_PMTUDISC_DO 2 +#define IP_PMTUDISC_PROBE 3 +#define IP_PMTUDISC_INTERFACE 4 +#define IP_PMTUDISC_OMIT 5 + +#define IP_DEFAULT_MULTICAST_TTL 1 +#define IP_DEFAULT_MULTICAST_LOOP 1 +#define IP_MAX_MEMBERSHIPS 20 + +typedef uint16_t in_port_t; +typedef uint32_t in_addr_t; + +struct in_addr { + in_addr_t s_addr; +}; + +struct sockaddr_in { + sa_family_t sin_family; + in_port_t sin_port; + struct in_addr sin_addr; + uint8_t sin_zero[8]; +}; + +struct ip_opts { + struct in_addr ip_dst; + char ip_opts[40]; +}; + +#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) +struct ip_mreq { + struct in_addr imr_multiaddr; + struct in_addr imr_interface; +}; +#endif + +struct in6_addr { + union { + uint8_t __s6_addr[16]; + uint16_t __s6_addr16[8]; + uint32_t __s6_addr32[4]; + } __in6_union; +}; +#define s6_addr __in6_union.__s6_addr +#define s6_addr16 __in6_union.__s6_addr16 +#define s6_addr32 __in6_union.__s6_addr32 + +struct sockaddr_in6 { + sa_family_t sin6_family; + in_port_t sin6_port; + uint32_t sin6_flowinfo; + struct in6_addr sin6_addr; + uint32_t sin6_scope_id; +}; + +struct ipv6_mreq { + struct in6_addr ipv6mr_multiaddr; + unsigned ipv6mr_interface; +}; + +uint32_t htonl(uint32_t); +uint16_t htons(uint16_t); +uint32_t ntohl(uint32_t); +uint16_t ntohs(uint16_t); + +#ifdef __cplusplus +} +#endif + +#endif /* !_ADAPT_NETINET_IN_H */ diff --git a/kal/libc/newlib/porting/include/netinet/ip.h b/kal/libc/newlib/porting/include/netinet/ip.h new file mode 100644 index 00000000..55d1c45e --- /dev/null +++ b/kal/libc/newlib/porting/include/netinet/ip.h @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_NETINET_IP_H +#define _ADAPT_NETINET_IP_H + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct timestamp { + uint8_t len; + uint8_t ptr; +#if BYTE_ORDER == LITTLE_ENDIAN + unsigned int flags:4; + unsigned int overflow:4; +#else + unsigned int overflow:4; + unsigned int flags:4; +#endif + uint32_t data[9]; + }; + +struct iphdr { +#if BYTE_ORDER == LITTLE_ENDIAN + unsigned int ihl:4; + unsigned int version:4; +#else + unsigned int version:4; + unsigned int ihl:4; +#endif + uint8_t tos; + uint16_t tot_len; + uint16_t id; + uint16_t frag_off; + uint8_t ttl; + uint8_t protocol; + uint16_t check; + uint32_t saddr; + uint32_t daddr; +}; + +struct ip { +#if BYTE_ORDER == LITTLE_ENDIAN + unsigned int ip_hl:4; + unsigned int ip_v:4; +#else + unsigned int ip_v:4; + unsigned int ip_hl:4; +#endif + uint8_t ip_tos; + uint16_t ip_len; + uint16_t ip_id; + uint16_t ip_off; + uint8_t ip_ttl; + uint8_t ip_p; + uint16_t ip_sum; + struct in_addr ip_src, ip_dst; +}; + +#define IP_RF 0x8000 +#define IP_DF 0x4000 +#define IP_MF 0x2000 +#define IP_OFFMASK 0x1fff + +struct ip_timestamp { + uint8_t ipt_code; + uint8_t ipt_len; + uint8_t ipt_ptr; +#if BYTE_ORDER == LITTLE_ENDIAN + unsigned int ipt_flg:4; + unsigned int ipt_oflw:4; +#else + unsigned int ipt_oflw:4; + unsigned int ipt_flg:4; +#endif + uint32_t data[9]; +}; + +#ifdef __cplusplus +} +#endif + +#endif /* !_ADAPT_NETINET_IP_H */ diff --git a/kal/libc/newlib/porting/include/semaphore.h b/kal/libc/newlib/porting/include/semaphore.h new file mode 100644 index 00000000..6062d69d --- /dev/null +++ b/kal/libc/newlib/porting/include/semaphore.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_SEMAPHORE_H +#define _ADAPT_SEMAPHORE_H + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define SEM_FAILED ((sem_t *)0) +#define __SEM_NUM 4 + +typedef struct { + volatile int __val[__SEM_NUM * sizeof(long) / sizeof(int)]; +} sem_t; + +int sem_init(sem_t *sem, int shared, unsigned value); +int sem_destroy(sem_t *sem); +int sem_wait(sem_t *sem); +int sem_post(sem_t *sem); +int sem_timedwait(sem_t *__restrict sem, const struct timespec *__restrict timeout); +int sem_getvalue(sem_t *__restrict sem, int *__restrict currVal); + +#ifdef __cplusplus +} +#endif + +#endif /* !_ADAPT_SEMAPHORE_H */ diff --git a/kal/libc/newlib/porting/include/sys/_pthreadtypes.h b/kal/libc/newlib/porting/include/sys/_pthreadtypes.h new file mode 100644 index 00000000..e864aa79 --- /dev/null +++ b/kal/libc/newlib/porting/include/sys/_pthreadtypes.h @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_SYS_PTHREADTYPES_H +#define _ADAPT_SYS_PTHREADTYPES_H + +#define pthread_t __pthread_t_discard +#define pthread_attr_t __pthread_attr_t_discard +#define pthread_mutex_t __pthread_mutex_t_discard +#define pthread_mutexattr_t __pthread_mutexattr_t_discard +#define pthread_cond_t __pthread_cond_t_discard +#define pthread_condattr_t __pthread_condattr_t_discard +#define pthread_once_t __pthread_once_t_discard +#define pthread_barrierattr_t __pthread_barrierattr_t_discard +#define pthread_spinlock_t __pthread_spinlock_t_discard + +#include_next + +#undef pthread_t +#undef pthread_attr_t +#undef pthread_mutex_t +#undef pthread_mutexattr_t +#undef pthread_cond_t +#undef pthread_condattr_t +#undef pthread_once_t +#undef pthread_barrierattr_t +#undef pthread_spinlock_t +#undef _PTHREAD_MUTEX_INITIALIZER +#undef _PTHREAD_COND_INITIALIZER +#undef _PTHREAD_ONCE_INIT + +#undef PTHREAD_STACK_MIN + +#include "los_config.h" +#define PTHREAD_STACK_MIN LOSCFG_BASE_CORE_TSK_MIN_STACK_SIZE + +typedef unsigned long pthread_t; /* identify a thread */ + +typedef struct { + unsigned int detachstate; + unsigned int schedpolicy; + struct sched_param schedparam; + unsigned int inheritsched; + unsigned int scope; + unsigned int stackaddr_set; + void *stackaddr; + unsigned int stacksize_set; + size_t stacksize; +} pthread_attr_t; + +#include "los_list.h" +typedef struct { unsigned int magic; unsigned int handle; } pthread_mutex_t; + +typedef struct { unsigned type; } pthread_mutexattr_t; + +#define _MUX_MAGIC 0xEBCFDEA0 +#define _MUX_INVALID_HANDLE 0xEEEEEEEF + +#define _PTHREAD_MUTEX_INITIALIZER { _MUX_MAGIC, _MUX_INVALID_HANDLE } + +#ifdef _GNU_SOURCE +#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP { _MUX_MAGIC, _MUX_INVALID_HANDLE } +#endif + +#include "los_event.h" + +typedef struct pthread_cond { + volatile int count; /**< The number of tasks blocked by condition */ + EVENT_CB_S event; /**< Event object*/ + pthread_mutex_t* mutex; /**< Mutex locker for condition variable protection */ + volatile int value; /**< Condition variable state value*/ + int clock; +} pthread_cond_t; + +#define _PTHREAD_COND_INITIALIZER { 0 } + +typedef struct { int clock; } pthread_condattr_t; + +typedef __uint32_t pthread_key_t; /* thread-specific data keys */ + +typedef int pthread_once_t; + +#define _PTHREAD_ONCE_INIT { 0 } + +typedef struct { unsigned __attr; } pthread_barrierattr_t; + +typedef int pthread_spinlock_t; + +#endif /* !_ADAPT_SYS_PTHREADTYPES_H */ diff --git a/kal/libc/newlib/porting/include/sys/fcntl.h b/kal/libc/newlib/porting/include/sys/fcntl.h new file mode 100644 index 00000000..8ecae833 --- /dev/null +++ b/kal/libc/newlib/porting/include/sys/fcntl.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_SYS_FCNTL_H +#define _ADAPT_SYS_FCNTL_H + +#include_next + +#define O_NDELAY _FNDELAY + +#endif /* !_ADAPT_SYS_FCNTL_H */ diff --git a/kal/libc/newlib/porting/include/sys/features.h b/kal/libc/newlib/porting/include/sys/features.h new file mode 100644 index 00000000..d5444864 --- /dev/null +++ b/kal/libc/newlib/porting/include/sys/features.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_SYS_FEATURES_H +#define _ADAPT_SYS_FEATURES_H + +#define _GNU_SOURCE +#define __USE_NEWLIB__ + +/* adapt time.h */ +#define _POSIX_TIMERS 1 +#define _POSIX_CPUTIME 1 +#define _POSIX_THREAD_CPUTIME 1 +#define _POSIX_MONOTONIC_CLOCK 1 +#define _POSIX_CLOCK_SELECTION 1 + +/* adapt sys/signal.h */ +#define _POSIX_REALTIME_SIGNALS 1 + +/* adapt pthread */ +#define _POSIX_THREADS 1 +#define _POSIX_TIMEOUTS +#define _POSIX_THREAD_PRIORITY_SCHEDULING +#define _UNIX98_THREAD_MUTEX_ATTRIBUTES + +#include_next + +#endif /* !_ADAPT_SYS_FEATURES_H */ diff --git a/kal/libc/newlib/porting/include/sys/ioctl.h b/kal/libc/newlib/porting/include/sys/ioctl.h new file mode 100644 index 00000000..dccaf852 --- /dev/null +++ b/kal/libc/newlib/porting/include/sys/ioctl.h @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_SYS_IOCTL_H +#define _ADAPT_SYS_IOCTL_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define TCGETS 0x5401 +#define TCSETS 0x5402 +#define TCSETSW 0x5403 +#define TCSETSF 0x5404 +#define TCGETA 0x5405 +#define TCSETA 0x5406 +#define TCSETAW 0x5407 +#define TCSETAF 0x5408 +#define TCSBRK 0x5409 +#define TCXONC 0x540A +#define TCFLSH 0x540B +#define TIOCEXCL 0x540C +#define TIOCNXCL 0x540D +#define TIOCSCTTY 0x540E +#define TIOCGPGRP 0x540F +#define TIOCSPGRP 0x5410 +#define TIOCOUTQ 0x5411 +#define TIOCSTI 0x5412 +#define TIOCGWINSZ 0x5413 +#define TIOCSWINSZ 0x5414 +#define TIOCMGET 0x5415 +#define TIOCMBIS 0x5416 +#define TIOCMBIC 0x5417 +#define TIOCMSET 0x5418 +#define TIOCGSOFTCAR 0x5419 +#define TIOCSSOFTCAR 0x541A +#define FIONREAD 0x541B +#define TIOCINQ FIONREAD +#define TIOCLINUX 0x541C +#define TIOCCONS 0x541D +#define TIOCGSERIAL 0x541E +#define TIOCSSERIAL 0x541F +#define TIOCPKT 0x5420 +#define FIONBIO 0x5421 + +#define SIOCADDRT 0x890B +#define SIOCDELRT 0x890C +#define SIOCRTMSG 0x890D + +#define SIOCGIFNAME 0x8910 +#define SIOCSIFLINK 0x8911 +#define SIOCGIFCONF 0x8912 +#define SIOCGIFFLAGS 0x8913 +#define SIOCSIFFLAGS 0x8914 +#define SIOCGIFADDR 0x8915 +#define SIOCSIFADDR 0x8916 +#define SIOCGIFDSTADDR 0x8917 +#define SIOCSIFDSTADDR 0x8918 +#define SIOCGIFBRDADDR 0x8919 +#define SIOCSIFBRDADDR 0x891a +#define SIOCGIFNETMASK 0x891b +#define SIOCSIFNETMASK 0x891c +#define SIOCGIFMETRIC 0x891d +#define SIOCSIFMETRIC 0x891e +#define SIOCGIFMEM 0x891f +#define SIOCSIFMEM 0x8920 +#define SIOCGIFMTU 0x8921 +#define SIOCSIFMTU 0x8922 +#define SIOCSIFNAME 0x8923 +#define SIOCSIFHWADDR 0x8924 +#define SIOCGIFENCAP 0x8925 +#define SIOCSIFENCAP 0x8926 +#define SIOCGIFHWADDR 0x8927 +#define SIOCGIFSLAVE 0x8929 +#define SIOCSIFSLAVE 0x8930 +#define SIOCADDMULTI 0x8931 +#define SIOCDELMULTI 0x8932 +#define SIOCGIFINDEX 0x8933 +#define SIOGIFINDEX SIOCGIFINDEX + +#define _IOC(a, b, c, d) (((a) << 30) | ((b) << 8) | (c) | ((d) << 16)) +#define _IOC_NONE 0U +#define _IOC_WRITE 1U +#define _IOC_READ 2U + +#define _IO(a, b) _IOC(_IOC_NONE, (a), (b), 0) +#define _IOW(a, b, c) _IOC(_IOC_WRITE, (a), (b), sizeof(c)) +#define _IOR(a, b, c) _IOC(_IOC_READ, (a), (b), sizeof(c)) +#define _IOWR(a, b, c) _IOC(_IOC_READ | _IOC_WRITE, (a), (b), sizeof(c)) + +int ioctl(int, int, ...); + +#ifdef __cplusplus +} +#endif + +#endif /* !_ADAPT_SYS_IOCTL_H */ diff --git a/kal/libc/newlib/porting/include/sys/mount.h b/kal/libc/newlib/porting/include/sys/mount.h new file mode 100644 index 00000000..6acb2de3 --- /dev/null +++ b/kal/libc/newlib/porting/include/sys/mount.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_SYS_MOUNT_H +#define _ADAPT_SYS_MOUNT_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define MS_RDONLY 1 +#define MS_NOSUID 2 +#define MS_NODEV 4 +#define MS_NOEXEC 8 +#define MS_SYNCHRONOUS 16 +#define MS_REMOUNT 32 +#define MS_MANDLOCK 64 +#define MS_DIRSYNC 128 + +#define MNT_FORCE 1 +#define MNT_DETACH 2 +#define MNT_EXPIRE 4 +#define UMOUNT_NOFOLLOW 8 + +int mount(const char *source, const char *target, const char *filesystemtype, \ + unsigned long mountflags, const void *data); +int umount(const char *target); +int umount2(const char *target, int flag); + +#ifdef __cplusplus +} +#endif + +#endif /* !_ADAPT_SYS_MOUNT_H */ diff --git a/kal/libc/newlib/porting/include/sys/prctl.h b/kal/libc/newlib/porting/include/sys/prctl.h new file mode 100644 index 00000000..daa345cc --- /dev/null +++ b/kal/libc/newlib/porting/include/sys/prctl.h @@ -0,0 +1,63 @@ +#ifndef _ADAPT_SYS_PRCTL_H +#define _ADAPT_SYS_PRCTL_H + +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +struct prctl_mm_map { + uint64_t start_code; + uint64_t end_code; + uint64_t start_data; + uint64_t end_data; + uint64_t start_brk; + uint64_t brk; + uint64_t start_stack; + uint64_t arg_start; + uint64_t arg_end; + uint64_t env_start; + uint64_t env_end; + uint64_t *auxv; + uint32_t auxv_size; + uint32_t exe_fd; +}; + +int prctl(int, ...); + +#ifdef __cplusplus +} +#endif + +#endif /* !_ADAPT_SYS_PRCTL_H */ diff --git a/kal/libc/newlib/porting/include/sys/sched.h b/kal/libc/newlib/porting/include/sys/sched.h new file mode 100644 index 00000000..f97f12dc --- /dev/null +++ b/kal/libc/newlib/porting/include/sys/sched.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_SYS_SCHED_H +#define _ADAPT_SYS_SCHED_H + +#include + +#define sched_param sched_param_discard + +#include_next + +#undef sched_param + +struct sched_param { + int sched_priority; + int __reserved1; + struct { + time_t __reserved1; + long __reserved2; + } __reserved2[2]; + int __reserved3; +}; + +typedef struct cpu_set_t { unsigned long __bits[128 / sizeof(long)]; } cpu_set_t; + +#endif /* !_ADAPT_SYS_SCHED_H */ \ No newline at end of file diff --git a/kal/libc/newlib/porting/include/sys/socket.h b/kal/libc/newlib/porting/include/sys/socket.h new file mode 100644 index 00000000..713a6408 --- /dev/null +++ b/kal/libc/newlib/porting/include/sys/socket.h @@ -0,0 +1,262 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_SYS_SOCKET_H +#define _ADAPT_SYS_SOCKET_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef unsigned socklen_t; +typedef unsigned short sa_family_t; + +struct iovec { + void *iov_base; + size_t iov_len; +}; + +struct msghdr { + void *msg_name; + socklen_t msg_namelen; + struct iovec *msg_iov; +#if LONG_MAX > 0x7fffffff && BYTE_ORDER == BIG_ENDIAN + int __pad1; +#endif + int msg_iovlen; +#if LONG_MAX > 0x7fffffff && BYTE_ORDER == LITTLE_ENDIAN + int __pad1; +#endif + void *msg_control; +#if LONG_MAX > 0x7fffffff && BYTE_ORDER == BIG_ENDIAN + int __pad2; +#endif + socklen_t msg_controllen; +#if LONG_MAX > 0x7fffffff && BYTE_ORDER == LITTLE_ENDIAN + int __pad2; +#endif + int msg_flags; +}; + +struct cmsghdr { +#if LONG_MAX > 0x7fffffff && BYTE_ORDER == BIG_ENDIAN + int __pad1; +#endif + socklen_t cmsg_len; +#if LONG_MAX > 0x7fffffff && BYTE_ORDER == LITTLE_ENDIAN + int __pad1; +#endif + int cmsg_level; + int cmsg_type; +}; + +#ifdef _GNU_SOURCE +struct ucred { + pid_t pid; + uid_t uid; + gid_t gid; +}; + +struct mmsghdr { + struct msghdr msg_hdr; + unsigned int msg_len; +}; + +struct timespec; + +int sendmmsg (int, struct mmsghdr *, unsigned int, unsigned int); +int recvmmsg (int, struct mmsghdr *, unsigned int, unsigned int, struct timespec *); +#endif + +struct linger { + int l_onoff; + int l_linger; +}; + +#define SHUT_RD 0 +#define SHUT_WR 1 +#define SHUT_RDWR 2 + +#ifndef SOCK_STREAM +#define SOCK_STREAM 1 +#define SOCK_DGRAM 2 +#endif + +#define SOCK_RAW 3 +#define SOCK_RDM 4 +#define SOCK_SEQPACKET 5 +#define SOCK_DCCP 6 +#define SOCK_PACKET 10 + +#ifndef SOCK_CLOEXEC +#define SOCK_CLOEXEC 02000000 +#define SOCK_NONBLOCK 04000 +#endif + +#define PF_UNSPEC 0 +#define PF_LOCAL 1 +#define PF_UNIX PF_LOCAL +#define PF_FILE PF_LOCAL +#define PF_INET 2 + +#define AF_UNSPEC PF_UNSPEC +#define AF_LOCAL PF_LOCAL +#define AF_UNIX AF_LOCAL +#define AF_FILE AF_LOCAL +#define AF_INET PF_INET + +#ifndef SO_DEBUG +#define SO_DEBUG 1 +#define SO_REUSEADDR 2 +#define SO_TYPE 3 +#define SO_ERROR 4 +#define SO_DONTROUTE 5 +#define SO_BROADCAST 6 +#define SO_SNDBUF 7 +#define SO_RCVBUF 8 +#define SO_KEEPALIVE 9 +#define SO_OOBINLINE 10 +#define SO_NO_CHECK 11 +#define SO_PRIORITY 12 +#define SO_LINGER 13 +#define SO_BSDCOMPAT 14 +#define SO_REUSEPORT 15 +#define SO_PASSCRED 16 +#define SO_PEERCRED 17 +#define SO_RCVLOWAT 18 +#define SO_SNDLOWAT 19 +#define SO_ACCEPTCONN 30 +#define SO_PEERSEC 31 +#define SO_SNDBUFFORCE 32 +#define SO_RCVBUFFORCE 33 +#define SO_PROTOCOL 38 +#define SO_DOMAIN 39 +#endif + +#ifndef SO_RCVTIMEO +#if LONG_MAX == 0x7fffffff +#define SO_RCVTIMEO 66 +#define SO_SNDTIMEO 67 +#else +#define SO_RCVTIMEO 20 +#define SO_SNDTIMEO 21 +#endif +#endif + +#define SO_BINDTODEVICE 25 + +#ifndef SOL_SOCKET +#define SOL_SOCKET 1 +#endif + +#define MSG_OOB 0x0001 +#define MSG_PEEK 0x0002 +#define MSG_DONTROUTE 0x0004 +#define MSG_CTRUNC 0x0008 +#define MSG_PROXY 0x0010 +#define MSG_TRUNC 0x0020 +#define MSG_DONTWAIT 0x0040 +#define MSG_EOR 0x0080 +#define MSG_WAITALL 0x0100 +#define MSG_FIN 0x0200 +#define MSG_SYN 0x0400 +#define MSG_CONFIRM 0x0800 +#define MSG_RST 0x1000 +#define MSG_ERRQUEUE 0x2000 +#define MSG_NOSIGNAL 0x4000 +#define MSG_MORE 0x8000 +#define MSG_WAITFORONE 0x10000 +#define MSG_BATCH 0x40000 +#define MSG_ZEROCOPY 0x4000000 +#define MSG_FASTOPEN 0x20000000 +#define MSG_CMSG_CLOEXEC 0x40000000 + +#define __CMSG_LEN(cmsg) (((cmsg)->cmsg_len + sizeof(long) - 1) & ~(long)(sizeof(long) - 1)) +#define __CMSG_NEXT(cmsg) ((unsigned char *)(cmsg) + __CMSG_LEN(cmsg)) +#define __MHDR_END(mhdr) ((unsigned char *)(mhdr)->msg_control + (mhdr)->msg_controllen) + +#define CMSG_DATA(cmsg) ((unsigned char *) (((struct cmsghdr *)(cmsg)) + 1)) +#define CMSG_NXTHDR(mhdr, cmsg) ((cmsg)->cmsg_len < sizeof (struct cmsghdr) || \ + __CMSG_LEN(cmsg) + sizeof(struct cmsghdr) >= __MHDR_END(mhdr) - (unsigned char *)(cmsg) \ + ? 0 : (struct cmsghdr *)__CMSG_NEXT(cmsg)) +#define CMSG_FIRSTHDR(mhdr) ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0) + +#define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) +#define CMSG_SPACE(len) (CMSG_ALIGN (len) + CMSG_ALIGN (sizeof (struct cmsghdr))) +#define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len)) + +#define SCM_RIGHTS 0x01 +#define SCM_CREDENTIALS 0x02 + +struct sockaddr { + sa_family_t sa_family; + char sa_data[14]; +}; + +struct sockaddr_storage { + sa_family_t ss_family; + char __ss_padding[128 - sizeof(long) - sizeof(sa_family_t)]; + unsigned long __ss_align; +}; + +int socket(int, int, int); +int socketpair(int, int, int, int [2]); + +int shutdown(int, int); + +int bind(int, const struct sockaddr *, socklen_t); +int connect(int, const struct sockaddr *, socklen_t); +int listen(int, int); +int accept(int, struct sockaddr *__restrict, socklen_t *__restrict); +int accept4(int, struct sockaddr *__restrict, socklen_t *__restrict, int); + +int getsockname(int, struct sockaddr *__restrict, socklen_t *__restrict); +int getpeername(int, struct sockaddr *__restrict, socklen_t *__restrict); + +ssize_t send(int, const void *, size_t, int); +ssize_t recv(int, void *, size_t, int); +ssize_t sendto(int, const void *, size_t, int, const struct sockaddr *, socklen_t); +ssize_t recvfrom(int, void *__restrict, size_t, int, struct sockaddr *__restrict, socklen_t *__restrict); +ssize_t sendmsg(int, const struct msghdr *, int); +ssize_t recvmsg(int, struct msghdr *, int); + +int getsockopt(int, int, int, void *__restrict, socklen_t *__restrict); +int setsockopt(int, int, int, const void *, socklen_t); + +int sockatmark(int); + +#ifdef __cplusplus +} +#endif + +#endif /* !_ADAPT_SYS_SOCKET_H */ diff --git a/kal/libc/newlib/porting/include/sys/stat.h b/kal/libc/newlib/porting/include/sys/stat.h new file mode 100644 index 00000000..b3330fa1 --- /dev/null +++ b/kal/libc/newlib/porting/include/sys/stat.h @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_SYS_STAT_H +#define _ADAPT_SYS_STAT_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +#define S_IFMT 0170000 + +#define S_IFDIR 0040000 +#define S_IFCHR 0020000 +#define S_IFBLK 0060000 +#define S_IFREG 0100000 +#define S_IFIFO 0010000 +#define S_IFLNK 0120000 +#define S_IFSOCK 0140000 + +#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) +#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR) +#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK) +#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) +#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO) +#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK) +#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK) + +#ifndef S_IRUSR +#define S_ISUID 04000 +#define S_ISGID 02000 +#define S_ISVTX 01000 +#define S_IRUSR 0400 +#define S_IWUSR 0200 +#define S_IXUSR 0100 +#define S_IRWXU 0700 +#define S_IRGRP 0040 +#define S_IWGRP 0020 +#define S_IXGRP 0010 +#define S_IRWXG 0070 +#define S_IROTH 0004 +#define S_IWOTH 0002 +#define S_IXOTH 0001 +#define S_IRWXO 0007 +#endif + +#define st_atime st_atim.tv_sec +#define st_mtime st_mtim.tv_sec +#define st_ctime st_ctim.tv_sec + +struct stat { + dev_t st_dev; + int __st_dev_padding; + long __st_ino_truncated; + mode_t st_mode; + nlink_t st_nlink; + uid_t st_uid; + gid_t st_gid; + dev_t st_rdev; + int __st_rdev_padding; + off_t st_size; + blksize_t st_blksize; + blkcnt_t st_blocks; + struct { + long tv_sec; + long tv_nsec; + } __st_atim32, __st_mtim32, __st_ctim32; + ino_t st_ino; + struct timespec st_atim; + struct timespec st_mtim; + struct timespec st_ctim; +}; + +int stat(const char *__restrict, struct stat *__restrict); +int fstat(int, struct stat *); +int mkdir(const char *, mode_t); + +#if defined(_GNU_SOURCE) +#define stat64 stat +#define fstat64 fstat +#define blkcnt64_t blkcnt_t +#define fsblkcnt64_t fsblkcnt_t +#define fsfilcnt64_t fsfilcnt_t +#define ino64_t ino_t +#define off64_t off_t +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* !_ADAPT_SYS_STAT_H */ diff --git a/kal/libc/newlib/porting/include/sys/statfs.h b/kal/libc/newlib/porting/include/sys/statfs.h new file mode 100644 index 00000000..6371fd39 --- /dev/null +++ b/kal/libc/newlib/porting/include/sys/statfs.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_SYS_STATFS_H +#define _ADAPT_SYS_STATFS_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct __fsid_t { + int __val[2]; +} fsid_t; + +struct statfs { + unsigned long f_type, f_bsize; + fsblkcnt_t f_blocks, f_bfree, f_bavail; + fsfilcnt_t f_files, f_ffree; + fsid_t f_fsid; + unsigned long f_namelen, f_frsize, f_flags, f_spare[4]; +}; + +int statfs(const char *, struct statfs *); + +#ifdef __cplusplus +} +#endif + +#endif /* !_ADAPT_SYS_STATFS_H */ diff --git a/kal/libc/newlib/porting/include/time.h b/kal/libc/newlib/porting/include/time.h new file mode 100644 index 00000000..b1659482 --- /dev/null +++ b/kal/libc/newlib/porting/include/time.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ADAPT_TIME_H +#define _ADAPT_TIME_H + +#define _timezone timezone +#define __TM_GMTOFF __tm_gmtoff +#define __TM_ZONE __tm_zone + +#include_next + +#endif /* !_ADAPT_TIME_H */ diff --git a/kal/libc/newlib/porting/src/fs.c b/kal/libc/newlib/porting/src/fs.c new file mode 100644 index 00000000..8531370d --- /dev/null +++ b/kal/libc/newlib/porting/src/fs.c @@ -0,0 +1,186 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "los_config.h" +#include "los_fs.h" +#include "stdio.h" +#include "stdarg.h" + +#ifdef LOSCFG_LIBC_NEWLIB_FS +int mount(const char *source, const char *target, + const char *filesystemtype, unsigned long mountflags, + const void *data) +{ + return LOS_FsMount(source, target, filesystemtype, mountflags, data); +} + +int umount(const char *target) +{ + return LOS_FsUmount(target); +} + +int umount2(const char *target, int flag) +{ + return LOS_FsUmount2(target, flag); +} + +int _open(const char *path, int oflag, ...) +{ + va_list vaList; + va_start(vaList, oflag); + int ret; + ret = LOS_Open(path, oflag); + va_end(vaList); + return ret; +} + +int _close(int fd) +{ + return LOS_Close(fd); +} + +ssize_t _read(int fd, void *buf, size_t nbyte) +{ + return LOS_Read(fd, buf, nbyte); +} + +ssize_t _write(int fd, const void *buf, size_t nbyte) +{ + return LOS_Write(fd, buf, nbyte); +} + +off_t _lseek(int fd, off_t offset, int whence) +{ + return LOS_Lseek(fd, offset, whence); +} + +int _unlink(const char *path) +{ + return LOS_Unlink(path); +} + +int _fstat(int fd, struct stat *buf) +{ + return LOS_Fstat(fd, buf); +} + +int _stat(const char *path, struct stat *buf) +{ + return LOS_Stat(path, buf); +} + +int fsync(int fd) +{ + return LOS_Fsync(fd); +} + +int mkdir(const char *path, mode_t mode) +{ + return LOS_Mkdir(path, mode); +} + +DIR *opendir(const char *dirName) +{ + return LOS_Opendir(dirName); +} + +struct dirent *readdir(DIR *dir) +{ + return LOS_Readdir(dir); +} + +int closedir(DIR *dir) +{ + return LOS_Closedir(dir); +} + +int rmdir(const char *path) +{ + return LOS_Unlink(path); +} + +int rename(const char *oldName, const char *newName) +{ + return LOS_Rename(oldName, newName); +} + +int statfs(const char *path, struct statfs *buf) +{ + return LOS_Statfs(path, buf); +} + +int ftruncate(int fd, off_t length) +{ + return LOS_Ftruncate(fd, length); +} + +#else /* #ifdef LOSCFG_FS_VFS */ + +int _open(const char *path, int oflag, ...) +{ + return -1; +} + +int _close(int fd) +{ + return -1; +} + +ssize_t _read(int fd, void *buf, size_t nbyte) +{ + return -1; +} + +ssize_t _write(int fd, const void *buf, size_t nbyte) +{ + return -1; +} + +off_t _lseek(int fd, off_t offset, int whence) +{ + return -1; +} + +int _unlink(const char *path) +{ + return -1; +} + +int _fstat(int fd, struct stat *buf) +{ + return -1; +} + +int _stat(const char *path, struct stat *buf) +{ + return -1; +} + +#endif diff --git a/kal/libc/newlib/porting/src/malloc.c b/kal/libc/newlib/porting/src/malloc.c new file mode 100644 index 00000000..3a980844 --- /dev/null +++ b/kal/libc/newlib/porting/src/malloc.c @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "securec.h" +#include "los_config.h" +#include "los_memory.h" +#include + +void *zalloc(size_t size) +{ + void *ptr = NULL; + + if (size == 0) { + return NULL; + } + + ptr = LOS_MemAlloc(OS_SYS_MEM_ADDR, size); + if (ptr != NULL) { + (void)memset_s(ptr, size, 0, size); + } + return ptr; +} + +void __wrap__free_r(struct _reent *reent, void *aptr) +{ + if (aptr == NULL) { + return; + } + + LOS_MemFree(OS_SYS_MEM_ADDR, aptr); +} + +size_t __wrap__malloc_usable_size_r(struct _reent *reent, void *aptr) +{ + return 0; +} + +void *__wrap__malloc_r(struct _reent *reent, size_t nbytes) +{ + if (nbytes == 0) { + return NULL; + } + + return LOS_MemAlloc(OS_SYS_MEM_ADDR, nbytes); +} + +void *__wrap__memalign_r(struct _reent *reent, size_t align, size_t nbytes) +{ + if (nbytes == 0) { + return NULL; + } + + return LOS_MemAllocAlign(OS_SYS_MEM_ADDR, nbytes, align); +} + +void *__wrap__realloc_r(struct _reent *reent, void *aptr, size_t nbytes) +{ + if (aptr == NULL) { + return malloc(nbytes); + } + + if (nbytes == 0) { + free(aptr); + return NULL; + } + + return LOS_MemRealloc(OS_SYS_MEM_ADDR, aptr, nbytes); +} diff --git a/kal/libc/newlib/porting/src/other_adapt.c b/kal/libc/newlib/porting/src/other_adapt.c new file mode 100644 index 00000000..4d215b1c --- /dev/null +++ b/kal/libc/newlib/porting/src/other_adapt.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "los_config.h" +#include +#include +#include +#include +#include + +int _isatty(int file) +{ + return file <= 2; +} + +int _kill(int i, int j) +{ + return 0; +} + +int _getpid(void) +{ + return 0; +} + +void _exit(int status) +{ + write(1, "exit\n", 5); + (VOID)pthread_exit(&status); + while (1) { + } +} + diff --git a/kal/libc/newlib/porting/src/time.c b/kal/libc/newlib/porting/src/time.c new file mode 100644 index 00000000..2eb2a6b2 --- /dev/null +++ b/kal/libc/newlib/porting/src/time.c @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "los_tick.h" +#include +#include + +#define SECS_PER_MIN 60 + +int _gettimeofday(struct timeval *__tp, void *__tzp) +{ + struct timespec ts; + struct timezone *tz = (struct timezone *)__tzp; + + if (__tp != NULL) { + if (-1 == clock_gettime(CLOCK_REALTIME, &ts)) { + return -1; + } + __tp->tv_sec = ts.tv_sec; + __tp->tv_usec = ts.tv_nsec / OS_SYS_NS_PER_US; + } + if (tz != NULL) { + tz->tz_minuteswest = timezone / SECS_PER_MIN; + tz->tz_dsttime = 0; + } + return 0; +} \ No newline at end of file diff --git a/kal/posix/BUILD.gn b/kal/posix/BUILD.gn index 8da23522..54f81159 100644 --- a/kal/posix/BUILD.gn +++ b/kal/posix/BUILD.gn @@ -29,21 +29,34 @@ import("//kernel/liteos_m/liteos.gni") -module_switch = defined(LOSCFG_COMPAT_POSIX) +module_switch = defined(LOSCFG_POSIX_API) module_name = get_path_info(rebase_path("."), "name") kernel_module(module_name) { sources = [ "src/errno.c", "src/libc.c", - "src/malloc.c", - "src/mqueue.c", - "src/pthread.c", - "src/pthread_attr.c", - "src/pthread_cond.c", - "src/pthread_mutex.c", - "src/semaphore.c", - "src/time.c", ] + + if (defined(LOSCFG_POSIX_THREAD_API)) { + sources += [ + "src/pthread.c", + "src/pthread_attr.c", + "src/pthread_cond.c", + "src/pthread_mutex.c", + ] + } + + if (defined(LOSCFG_POSIX_CLOCK_API)) { + sources += [ "src/time.c" ] + } + + if (defined(LOSCFG_POSIX_SEM_API)) { + sources += [ "src/semaphore.c" ] + } + + if (defined(LOSCFG_POSIX_MQUEUE_API)) { + sources += [ "src/mqueue.c" ] + } } config("public") { diff --git a/kal/posix/Kconfig b/kal/posix/Kconfig new file mode 100644 index 00000000..1ccaafc6 --- /dev/null +++ b/kal/posix/Kconfig @@ -0,0 +1,62 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +config POSIX_API + bool "Enable POSIX API" + default y + help + Answer Y to enable LiteOS support POSIX API. + +if POSIX_API + +config POSIX_THREAD_API + bool "Enable POSIX Thread API" + default y + help + Answer Y to enable LiteOS support POSIX Thread API. + +config POSIX_SEM_API + bool "Enable POSIX Semaphore API" + default y + help + Answer Y to enable LiteOS support POSIX Semaphore API. + +config POSIX_CLOCK_API + bool "Enable POSIX Semaphore API" + default y + help + Answer Y to enable LiteOS support POSIX Clock API. + +config POSIX_MQUEUE_API + bool "Enable POSIX Semaphore API" + default y + help + Answer Y to enable LiteOS support POSIX Mqueue API. + +endif # POSIX_API diff --git a/kal/posix/src/pthread_cond.c b/kal/posix/src/pthread_cond.c index 1ce795c0..e7d62e61 100644 --- a/kal/posix/src/pthread_cond.c +++ b/kal/posix/src/pthread_cond.c @@ -100,7 +100,7 @@ STATIC INLINE INT32 CondInitCheck(const pthread_cond_t *cond) int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) { - int ret = ENOERR; + int ret = 0; pthread_condattr_t condAttr; if (cond == NULL) { @@ -137,19 +137,19 @@ int pthread_cond_destroy(pthread_cond_t *cond) } if (CondInitCheck(cond)) { - return ENOERR; + return 0; } if (LOS_EventDestroy(&cond->event) != LOS_OK) { return EBUSY; } - if (pthread_mutex_destroy(cond->mutex) != ENOERR) { + if (pthread_mutex_destroy(cond->mutex) != 0) { PRINT_ERR("%s mutex destroy fail!\n", __FUNCTION__); return EINVAL; } free(cond->mutex); cond->mutex = NULL; - return ENOERR; + return 0; } STATIC VOID PthreadCountSub(pthread_cond_t *cond) @@ -164,7 +164,7 @@ STATIC VOID PthreadCountSub(pthread_cond_t *cond) int pthread_cond_broadcast(pthread_cond_t *cond) { - int ret = ENOERR; + int ret = 0; if (cond == NULL) { return EINVAL; @@ -184,7 +184,7 @@ int pthread_cond_broadcast(pthread_cond_t *cond) int pthread_cond_signal(pthread_cond_t *cond) { - int ret = ENOERR; + int ret = 0; if (cond == NULL) { return EINVAL; @@ -211,7 +211,7 @@ STATIC INT32 ProcessReturnVal(pthread_cond_t *cond, INT32 val) /* 0: event does not occur */ case 0: case BROADCAST_EVENT: - ret = ENOERR; + ret = 0; break; case LOS_ERRNO_EVENT_READ_TIMEOUT: PthreadCountSub(cond); @@ -241,7 +241,7 @@ int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, if (CondInitCheck(cond)) { ret = pthread_cond_init(cond, NULL); - if (ret != ENOERR) { + if (ret != 0) { return ret; } } @@ -265,13 +265,13 @@ int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, return EINVAL; } - if (pthread_mutex_unlock(mutex) != ENOERR) { + if (pthread_mutex_unlock(mutex) != 0) { PRINT_ERR("%s: %d failed\n", __FUNCTION__, __LINE__); } ret = (INT32)LOS_EventRead(&(cond->event), 0x0f, LOS_WAITMODE_OR | LOS_WAITMODE_CLR, absTicks); - if (pthread_mutex_lock(mutex) != ENOERR) { + if (pthread_mutex_lock(mutex) != 0) { PRINT_ERR("%s: %d failed\n", __FUNCTION__, __LINE__); } @@ -289,7 +289,7 @@ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) if (CondInitCheck(cond)) { ret = pthread_cond_init(cond, NULL); - if (ret != ENOERR) { + if (ret != 0) { return ret; } } @@ -298,11 +298,11 @@ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) cond->count++; (VOID)pthread_mutex_unlock(cond->mutex); - if (pthread_mutex_unlock(mutex) != ENOERR) { + if (pthread_mutex_unlock(mutex) != 0) { PRINT_ERR("%s: %d failed\n", __FUNCTION__, __LINE__); } ret = (INT32)LOS_EventRead(&(cond->event), 0x0f, LOS_WAITMODE_OR | LOS_WAITMODE_CLR, LOS_WAIT_FOREVER); - if (pthread_mutex_lock(mutex) != ENOERR) { + if (pthread_mutex_lock(mutex) != 0) { PRINT_ERR("%s: %d failed\n", __FUNCTION__, __LINE__); } @@ -310,7 +310,7 @@ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) /* 0: event does not occur */ case 0: case BROADCAST_EVENT: - ret = ENOERR; + ret = 0; break; default: PthreadCountSub(cond); diff --git a/kal/posix/src/semaphore.c b/kal/posix/src/semaphore.c index 87686681..52145a92 100644 --- a/kal/posix/src/semaphore.c +++ b/kal/posix/src/semaphore.c @@ -29,6 +29,7 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include #include "los_sem.h" diff --git a/kal/posix/src/time.c b/kal/posix/src/time.c index 0f54fca3..508b786e 100644 --- a/kal/posix/src/time.c +++ b/kal/posix/src/time.c @@ -47,6 +47,7 @@ /* accumulative time delta from discontinuous modify */ STATIC struct timespec g_accDeltaFromSet; +#ifndef __USE_NEWLIB__ STATIC const UINT16 g_daysInMonth[2][13] = { /* Normal years. */ { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, @@ -56,6 +57,10 @@ STATIC const UINT16 g_daysInMonth[2][13] = { STATIC const UINT8 g_montbl[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; +/* internal shared struct tm object for localtime and gmtime */ +static struct tm g_tm; +#endif + /* * Time zone information, stored in seconds, * negative values indicate the east of UTC, @@ -63,9 +68,6 @@ STATIC const UINT8 g_montbl[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, */ long timezone = -8 * 60 * 60; // defaults to CST: 8 hours east of the Prime Meridian -/* internal shared struct tm object for localtime and gmtime */ -static struct tm g_tm; - int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) { UINT64 nseconds; @@ -295,10 +297,18 @@ int clock_settime(clockid_t clockID, const struct timespec *tp) case CLOCK_MONOTONIC_RAW: case CLOCK_PROCESS_CPUTIME_ID: case CLOCK_BOOTTIME: +#ifdef CLOCK_REALTIME_ALARM case CLOCK_REALTIME_ALARM: +#endif +#ifdef CLOCK_BOOTTIME_ALARM case CLOCK_BOOTTIME_ALARM: +#endif +#ifdef CLOCK_SGI_CYCLE case CLOCK_SGI_CYCLE: +#endif +#ifdef CLOCK_TAI case CLOCK_TAI: +#endif case CLOCK_THREAD_CPUTIME_ID: errno = ENOTSUP; return -1; @@ -329,10 +339,18 @@ int clock_gettime(clockid_t clockID, struct timespec *tp) case CLOCK_THREAD_CPUTIME_ID: case CLOCK_PROCESS_CPUTIME_ID: case CLOCK_BOOTTIME: +#ifdef CLOCK_REALTIME_ALARM case CLOCK_REALTIME_ALARM: +#endif +#ifdef CLOCK_BOOTTIME_ALARM case CLOCK_BOOTTIME_ALARM: +#endif +#ifdef CLOCK_SGI_CYCLE case CLOCK_SGI_CYCLE: +#endif +#ifdef CLOCK_TAI case CLOCK_TAI: +#endif errno = ENOTSUP; return -1; default: @@ -360,10 +378,18 @@ int clock_getres(clockid_t clockID, struct timespec *tp) case CLOCK_THREAD_CPUTIME_ID: case CLOCK_PROCESS_CPUTIME_ID: case CLOCK_BOOTTIME: +#ifdef CLOCK_REALTIME_ALARM case CLOCK_REALTIME_ALARM: +#endif +#ifdef CLOCK_BOOTTIME_ALARM case CLOCK_BOOTTIME_ALARM: +#endif +#ifdef CLOCK_SGI_CYCLE case CLOCK_SGI_CYCLE: +#endif +#ifdef CLOCK_TAI case CLOCK_TAI: +#endif errno = ENOTSUP; return -1; default: @@ -387,10 +413,18 @@ int clock_nanosleep(clockid_t clk, int flags, const struct timespec *req, struct case CLOCK_MONOTONIC: case CLOCK_PROCESS_CPUTIME_ID: case CLOCK_BOOTTIME: +#ifdef CLOCK_REALTIME_ALARM case CLOCK_REALTIME_ALARM: +#endif +#ifdef CLOCK_BOOTTIME_ALARM case CLOCK_BOOTTIME_ALARM: +#endif +#ifdef CLOCK_SGI_CYCLE case CLOCK_SGI_CYCLE: +#endif +#ifdef CLOCK_TAI case CLOCK_TAI: +#endif if (flags == 0 || flags == TIMER_ABSTIME) { return ENOTSUP; } @@ -426,7 +460,7 @@ time_t time(time_t *timer) } return ts.tv_sec; } - +#ifndef __USE_NEWLIB__ /* * Compute the `struct tm' representation of T, * offset OFFSET seconds east of UTC, @@ -607,6 +641,7 @@ int gettimeofday(struct timeval *tv, void *ptz) } return 0; } +#endif int settimeofday(const struct timeval *tv, const struct timezone *tz) { @@ -643,7 +678,7 @@ int settimeofday(const struct timeval *tv, const struct timezone *tz) return 0; } -int usleep(unsigned useconds) +int usleep(useconds_t useconds) { struct timespec specTime = { 0 }; UINT64 nanoseconds = (UINT64)useconds * OS_SYS_NS_PER_US;